Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A recurring task in applied statistics: you have a sample \(x_1, \ldots, x_n\) (independent claim sizes, waiting times, durations, counts, etc.) and you want to know its distribution (summarize how it’s spread); i.e obtain a parametric model \(F_\theta\) for it. You may need it to compute a tail probability or a high quantile (a 99.5% loss level, say) effortlessly, to feed a Monte Carlo simulation, to price a contract, or simply to summarize three hundred numbers with two or three parameters.
statsinference is a small Techtonique Python package that does this in a few lines. If you come from R, think of it as MASS::fitdistr plus fitdistrplus in a single Python object. It fits candidate distributions by maximum likelihood, ranks them by AIC or BIC, reports goodness-of-fit statistics, draws fitdistrplus-style diagnostic plots, and simulates from the fitted model.
!pip install statsinference
import numpy as np
from statsinference import StatsInference
x = np.random.default_rng(1).gamma(2, 1.5, size=300)
# Let it choose among every distribution whose support fits the data
si = StatsInference(x).select(criterion="aic", verbose=1, progress=True)
si.best_dist # 'gamma'
si.estimate, si.sd # estimates and standard errors
si.table # ranking: loglik, AIC, BIC, delta, weight, KS
si.summary() # estimates, loglik, AIC/BIC, KS / CvM / AD
si.plot() # density, CDF, Q-Q and P-P panels
si.plot_compare(top=3) # the three best overlaid
sims = si.simulate(1000, seed=123) # draws from the best fit
alt = si.simulate(1000, seed=123, dist="weibull") # or from another candidate
# Or fit one distribution directly
StatsInference(x).fit("weibull").summary()
Fitting: 0%| | 0/10 [00:00<?, ?dist/s]
distribution k loglik AIC BIC delta weight
------------------------------------------------------------------------------
gamma 2 -574.258 1152.516 1159.924 0.000 0.835
weibull 2 -575.880 1155.760 1163.168 3.244 0.165
chi-squared 1 -585.582 1173.163 1176.867 20.647 0.000
lognormal 2 -597.735 1199.470 1206.878 46.954 0.000
t 3 -602.874 1211.748 1222.860 59.232 0.000
logistic 2 -605.949 1215.899 1223.306 63.382 0.000
exponential 1 -620.026 1242.052 1245.756 89.536 0.000
normal 2 -621.862 1247.723 1255.131 95.207 0.000
cauchy 2 -638.698 1281.395 1288.803 128.879 0.000
f 2 -695.182 1394.364 1401.772 241.848 0.000
Best by AIC: gamma
Fitting of the distribution 'gamma' by maximum likelihood
Parameters:
estimate Std. Error
shape 2.256 0.1723
rate 0.7763 0.06638
Loglikelihood: -574.3 AIC: 1153 BIC: 1160
n = 300 converged: True
KS: 0.03959 (p = 0.719) CvM: 0.06412 AD: 0.3859
Fitting of the distribution 'weibull' by maximum likelihood
Parameters:
estimate Std. Error
shape 1.587 0.06866
scale 3.245 0.1246
Loglikelihood: -575.9 AIC: 1156 BIC: 1163
n = 300 converged: True
KS: 0.03838 (p = 0.754) CvM: 0.08612 AD: 0.6695
"Fitting of the distribution 'weibull' by maximum likelihood\nParameters:\n estimate Std. Error\n shape 1.587 0.06866\n scale 3.245 0.1246\nLoglikelihood: -575.9 AIC: 1156 BIC: 1163\nn = 300 converged: True\nKS: 0.03838 (p = 0.754) CvM: 0.08612 AD: 0.6695"
display(sims)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
