Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
else:
self.success = False
print('WARNING: Fitting using Autograd FAILED for Weibull_3P. The fit from Scipy was used instead so the results may not be accurate.')
sp = ss.weibull_min.fit(all_data, optimizer='powell')
self.alpha = sp[2]
self.beta = sp[0]
self.gamma = sp[1]
params = [self.alpha, self.beta, self.gamma]
self.loglik2 = LL2
if n - k - 1 > 0:
self.AICc = 2 * k + LL2 + (2 * k ** 2 + 2 * k) / (n - k - 1)
else:
self.AICc = 'Insufficient data'
self.BIC = np.log(n) * k + LL2
self.distribution = Weibull_Distribution(alpha=self.alpha, beta=self.beta, gamma=self.gamma)
# confidence interval estimates of parameters
Z = -ss.norm.ppf((1 - CI) / 2)
hessian_matrix = hessian(Fit_Weibull_3P.LL)(np.array(tuple(params)), np.array(tuple(failures)), np.array(tuple(right_censored)))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.alpha_SE = abs(covariance_matrix[0][0]) ** 0.5
self.beta_SE = abs(covariance_matrix[1][1]) ** 0.5
self.gamma_SE = abs(covariance_matrix[2][2]) ** 0.5
self.alpha_upper = self.alpha * (np.exp(Z * (self.alpha_SE / self.alpha)))
self.alpha_lower = self.alpha * (np.exp(-Z * (self.alpha_SE / self.alpha)))
self.beta_upper = self.beta * (np.exp(Z * (self.beta_SE / self.beta)))
self.beta_lower = self.beta * (np.exp(-Z * (self.beta_SE / self.beta)))
self.gamma_upper = self.gamma * (np.exp(Z * (self.gamma_SE / self.gamma))) # here we assume gamma can only be positive as there are bounds placed on it in the optimizer. Minitab assumes positive or negative so bounds are different
self.gamma_lower = self.gamma * (np.exp(-Z * (self.gamma_SE / self.gamma)))
Data = {'Parameter': ['Alpha', 'Beta', 'Gamma'],
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).PDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
plt.subplot(122) # CDF
plt.bar(center, hist_cumulative * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).CDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).CDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).CDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).CDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Cumulative Distribution Function')
plt.xlabel('Data')
plt.ylabel('Cumulative probability density')
plt.suptitle('Histogram plot of each fitted distribution')
plt.legend()
xmin = 0
xmax = max(X) + delta
xvals = np.linspace(xmin, xmax, 1000)
plt.figure(figsize=(14, 6))
plt.subplot(121) # PDF
# make this histogram. Can't use plt.hist due to need to scale the heights when there's censored data
num_bins = min(int(len(X) / 2), 30)
hist, bins = np.histogram(X, bins=num_bins, density=True)
hist_cumulative = np.cumsum(hist) / sum(hist)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).PDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).PDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).PDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).PDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).PDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
plt.subplot(122) # CDF
plt.bar(center, hist_cumulative * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).CDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).CDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).CDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).CDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Cumulative Distribution Function')
plt.xlabel('Data')
plt.ylabel('Cumulative probability density')
plt.suptitle('Histogram plot of each fitted distribution')
if print_results is True:
print('Parameters:', '\nAlpha 1:', self.alpha_1, '\nBeta 1:', self.beta_1, '\nAlpha 2:', self.alpha_2, '\nBeta 2:', self.beta_2, '\nProportion 1:', self.proportion_1)
if show_plot is True:
xvals = np.linspace(0, max(failures) * 1.05, 1000)
plt.figure(figsize=(14, 6))
plt.subplot(121)
# make the histogram. Can't use plt.hist due to need to scale the heights when there's censored data
num_bins = min(int(len(failures) / 2), 30)
hist, bins = np.histogram(failures, bins=num_bins, density=True)
hist_cumulative = np.cumsum(hist) / sum(hist)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2
frac_failing = len(failures) / len(all_data)
plt.bar(center, hist * frac_failing, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
yvals_p1_pdf = Weibull_Distribution(alpha=self.alpha_1, beta=self.beta_1).PDF(xvals=xvals, show_plot=False)
yvals_p2_pdf = Weibull_Distribution(alpha=self.alpha_2, beta=self.beta_2).PDF(xvals=xvals, show_plot=False)
plt.plot(xvals, yvals_p1_pdf * self.proportion_1)
plt.plot(xvals, yvals_p2_pdf * self.proportion_2)
plt.title('Weibull Mixture PDF')
plt.xlabel('Failure Times')
plt.ylabel('Probability Density')
plt.subplot(122)
# make the histogram. Can't use plt.hist due to need to scale the heights when there's censored data
plt.bar(center, hist_cumulative * frac_failing, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
yvals_p1_cdf = Weibull_Distribution(alpha=self.alpha_1, beta=self.beta_1).CDF(xvals=xvals, show_plot=False)
yvals_p2_cdf = Weibull_Distribution(alpha=self.alpha_2, beta=self.beta_2).CDF(xvals=xvals, show_plot=False)
y_mixture = yvals_p1_cdf * self.proportion_1 + yvals_p2_cdf * self.proportion_2
plt.plot(xvals, y_mixture)
plt.title('Weibull Mixture CDF')
plt.xlabel('Failure Times')
if sort_by in ['BIC', 'bic']:
df2 = df.reindex(df.BIC.sort_values().index)
elif sort_by in ['AICc', 'AIC', 'aic', 'aicc']:
df2 = df.reindex(df.AICc.sort_values().index)
else:
raise ValueError('Invalid input to sort_by. Options are BIC or AICc. Default is BIC')
df3 = df2.set_index('Distribution').fillna('')
if self.Beta_2P_BIC == 0: # remove beta if it was not fitted (due to data being outside of 0 to 1 range)
df3 = df3.drop('Beta_2P', axis=0)
self.results = df3
# creates a distribution object of the best fitting distribution and assigns its name
best_dist = df3.index.values[0]
self.best_distribution_name = best_dist
if best_dist == 'Weibull_2P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta)
elif best_dist == 'Weibull_3P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma)
elif best_dist == 'Gamma_2P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta)
elif best_dist == 'Gamma_3P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma)
elif best_dist == 'Lognormal_2P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma)
elif best_dist == 'Lognormal_3P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma)
elif best_dist == 'Exponential_1P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_1P_lambda)
elif best_dist == 'Exponential_2P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma)
elif best_dist == 'Normal_2P':
self.best_distribution = Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma)
self.success = False
print('WARNING: Fitting using Autograd FAILED for Weibull_2P. The fit from Scipy was used instead so results may not be accurate.')
self.alpha = sp[2]
self.beta = sp[0]
params = [self.alpha, self.beta]
k = len(params)
n = len(all_data)
LL2 = 2 * Fit_Weibull_2P.LL(params, failures, right_censored)
self.loglik2 = LL2
if n - k - 1 > 0:
self.AICc = 2 * k + LL2 + (2 * k ** 2 + 2 * k) / (n - k - 1)
else:
self.AICc = 'Insufficient data'
self.BIC = np.log(n) * k + LL2
self.distribution = Weibull_Distribution(alpha=self.alpha, beta=self.beta)
# confidence interval estimates of parameters
Z = -ss.norm.ppf((1 - CI) / 2)
if force_beta is None:
hessian_matrix = hessian(Fit_Weibull_2P.LL)(np.array(tuple(params)), np.array(tuple(failures)), np.array(tuple(right_censored)))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.alpha_SE = abs(covariance_matrix[0][0]) ** 0.5
self.beta_SE = abs(covariance_matrix[1][1]) ** 0.5
self.Cov_alpha_beta = abs(covariance_matrix[0][1])
self.alpha_upper = self.alpha * (np.exp(Z * (self.alpha_SE / self.alpha)))
self.alpha_lower = self.alpha * (np.exp(-Z * (self.alpha_SE / self.alpha)))
self.beta_upper = self.beta * (np.exp(Z * (self.beta_SE / self.beta)))
self.beta_lower = self.beta * (np.exp(-Z * (self.beta_SE / self.beta)))
else: # this is for when force beta is specified
hessian_matrix = hessian(Fit_Weibull_2P.LL_fb)(np.array(tuple([self.alpha])), np.array(tuple(failures)), np.array(tuple(right_censored)), np.array(tuple([force_beta])))
covariance_matrix = np.linalg.inv(hessian_matrix)
xmax = max(X) + delta
xvals = np.linspace(xmin, xmax, 1000)
plt.figure(figsize=(14, 6))
plt.subplot(121) # PDF
# make this histogram. Can't use plt.hist due to need to scale the heights when there's censored data
num_bins = min(int(len(X) / 2), 30)
hist, bins = np.histogram(X, bins=num_bins, density=True)
hist_cumulative = np.cumsum(hist) / sum(hist)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).PDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).PDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).PDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
elif sort_by in ['AICc', 'AIC', 'aic', 'aicc']:
df2 = df.reindex(df.AICc.sort_values().index)
else:
raise ValueError('Invalid input to sort_by. Options are BIC or AICc. Default is BIC')
df3 = df2.set_index('Distribution').fillna('')
if self.Beta_2P_BIC == 0: # remove beta if it was not fitted (due to data being outside of 0 to 1 range)
df3 = df3.drop('Beta_2P', axis=0)
self.results = df3
# creates a distribution object of the best fitting distribution and assigns its name
best_dist = df3.index.values[0]
self.best_distribution_name = best_dist
if best_dist == 'Weibull_2P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta)
elif best_dist == 'Weibull_3P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma)
elif best_dist == 'Gamma_2P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta)
elif best_dist == 'Gamma_3P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma)
elif best_dist == 'Lognormal_2P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma)
elif best_dist == 'Lognormal_3P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma)
elif best_dist == 'Exponential_1P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_1P_lambda)
elif best_dist == 'Exponential_2P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma)
elif best_dist == 'Normal_2P':
self.best_distribution = Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma)
elif best_dist == 'Beta_2P':
self.best_distribution = Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta)