Skip to content

Instantly share code, notes, and snippets.

@plasmaman
Last active December 22, 2023 17:42
Show Gist options
  • Save plasmaman/5508278 to your computer and use it in GitHub Desktop.
Save plasmaman/5508278 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)
@DibsdalecOXMT
Copy link

This is great for uncensored data. How could this be amended to include right censored data?

@franvil
Copy link

franvil commented Nov 20, 2021

Hello!! The factors 1.2 and 0.572 into the code I don´t know from where they appear. Is there a formula that I don´t know to calculate the Weibull´s parameters?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment