Skip to content

Instantly share code, notes, and snippets.

@aw3s0me
Forked from plasmaman/weibull.py
Created November 22, 2015 22:25
Show Gist options
  • Select an option

  • Save aw3s0me/cb2891d188e88df38f00 to your computer and use it in GitHub Desktop.

Select an option

Save aw3s0me/cb2891d188e88df38f00 to your computer and use it in GitHub Desktop.
Python code for estimating the shape and scale parameters for a two-parameter Weibull distribution. It uses scipy.optimize.fmin to minimize the Likelihood function.
from scipy.stats import exponweib
from scipy.optimize import fmin
import numpy as np
# x is your data array
# returns [shape, scale]
def fitweibull(x):
def optfun(theta):
return -np.sum(np.log(exponweib.pdf(x, 1, theta[0], scale = theta[1], loc = 0)))
logx = np.log(x)
shape = 1.2 / np.std(logx)
scale = np.exp(np.mean(logx) + (0.572 / shape))
return fmin(optfun, [shape, scale], xtol = 0.01, ftol = 0.01, disp = 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment