Skip to content

Instantly share code, notes, and snippets.

@yuvalta
Created November 19, 2020 09:55
Show Gist options
  • Save yuvalta/ab420b080897fd741e424bc63253ec98 to your computer and use it in GitHub Desktop.
Save yuvalta/ab420b080897fd741e424bc63253ec98 to your computer and use it in GitHub Desktop.
Fit distributions and check goodness of fiting
from scipy import stats
def fitDist(list_of_data):
list_of_dists = ['dweibull', 'erlang', 'expon', 'gamma','lognorm', 'norm', 'pareto','uniform', 'weibull_min', 'weibull_max']
results = []
for i in list_of_dists:
dist = getattr(stats, i)
param = dist.fit(list_of_data)
a = stats.kstest(list_of_data, i, args=param)
results.append((i, a[0], a[1]))
results.sort(key=lambda x: float(x[2]), reverse=True)
for j in results:
print("{}: statistic={}, pvalue={}".format(j[0], j[1], j[2]))
# OUTPUT: the results are sorted based on the highest p-value - most fitting distribution
#
# lognorm: statistic=0.08151809053646897, pvalue=0.7099384232837425
# weibull_min: statistic=0.1433543643561933, pvalue=0.10186497830108568
# pareto: statistic=0.17285038643818018, pvalue=0.026768835378203133
# erlang: statistic=0.21420242876681705, pvalue=0.0026817745586541486
# gamma: statistic=0.3863965456262737, pvalue=6.453380068067353e-10
# expon: statistic=0.3880651672810395, pvalue=5.308769005172491e-10
# norm: statistic=0.4136814145992531, pvalue=2.342556004002841e-11
# dweibull: statistic=0.4322169930468518, pvalue=2.113211201836775e-12
# weibull_max: statistic=0.8801135189665196, pvalue=7.067801597034558e-65
# uniform: statistic=0.9042039833453933, pvalue=9.988248721291695e-72
#
#
# https://medium.com/@amirarsalan.rajabi/distribution-fitting-with-python-scipy-bb70a42c0aed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment