Last active
December 22, 2023 17:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This is great for uncensored data. How could this be amended to include right censored data?
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
Can you use exponweib's "logpdf" instead of np.log(pdf(...)) (there may be some numerical-stability benefits)?