Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('WARNING: Fitting using Autograd FAILED for Expon_2P. The fit from Scipy was used instead so results may not be accurate.')
sp = ss.expon.fit(all_data, optimizer='powell')
self.Lambda = sp[1]
self.gamma = sp[0]
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 = Exponential_Distribution(Lambda=self.Lambda, gamma=self.gamma)
# confidence interval estimates of parameters. Uses Expon_1P because gamma (while optimized) cannot be used in the MLE solution as the solution is unbounded
Z = -ss.norm.ppf((1 - CI) / 2)
hessian_matrix = hessian(Fit_Expon_1P.LL)(np.array(tuple([self.Lambda])), np.array(tuple(failures - self.gamma)), np.array(tuple(right_censored - self.gamma)))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.Lambda_SE = abs(covariance_matrix[0][0]) ** 0.5
self.gamma_SE = 0
self.Lambda_upper = self.Lambda * (np.exp(Z * (self.Lambda_SE / self.Lambda)))
self.Lambda_lower = self.Lambda * (np.exp(-Z * (self.Lambda_SE / self.Lambda)))
self.gamma_upper = self.gamma
self.gamma_lower = self.gamma
self.Lambda_inv = 1 / self.Lambda
self.Lambda_SE_inv = abs(1 / self.Lambda * np.log(self.Lambda / self.Lambda_upper) / Z)
self.Lambda_lower_inv = 1 / self.Lambda_upper
self.Lambda_upper_inv = 1 / self.Lambda_lower
Data = {'Parameter': ['Lambda', '1/Lambda', 'Gamma'],
'Point Estimate': [self.Lambda, self.Lambda_inv, self.gamma],
'Standard Error': [self.Lambda_SE, self.Lambda_SE_inv, self.gamma_SE],
'Lower CI': [self.Lambda_lower, self.Lambda_lower_inv, self.gamma_lower],
if type(failures) == list:
failures = np.array(failures)
if type(failures) != np.ndarray:
raise TypeError('failures must be a list or array of failure data')
if type(right_censored) == list:
right_censored = np.array(right_censored)
if type(right_censored) != np.ndarray:
raise TypeError('right_censored must be a list or array of right censored failure data')
all_data = np.hstack([failures, right_censored])
# solve it
self.gamma = 0
sp = ss.expon.fit(all_data, floc=0, optimizer='powell') # scipy's answer is used as an initial guess. Scipy is only correct when there is no censored data
guess = [1 / sp[1]]
warnings.filterwarnings('ignore') # necessary to supress the warning about the jacobian when using the nelder-mead optimizer
result = minimize(value_and_grad(Fit_Expon_1P.LL), guess, args=(failures, right_censored), jac=True, tol=1e-6, method='nelder-mead')
if result.success is True:
params = result.x
self.success = True
self.Lambda = params[0]
else:
self.success = False
print('WARNING: Fitting using Autograd FAILED for Expon_1P. The fit from Scipy was used instead so results may not be accurate.')
self.Lambda = 1 / sp[1]
params = [self.Lambda]
k = len(params)
n = len(all_data)
LL2 = 2 * Fit_Expon_1P.LL(params, failures, right_censored)
self.loglik2 = LL2
if n - k - 1 > 0:
params = [self.Lambda]
k = len(params)
n = len(all_data)
LL2 = 2 * Fit_Expon_1P.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 = Exponential_Distribution(Lambda=self.Lambda)
# confidence interval estimates of parameters
Z = -ss.norm.ppf((1 - CI) / 2)
hessian_matrix = hessian(Fit_Expon_1P.LL)(np.array(tuple(params)), np.array(tuple(failures)), np.array(tuple(right_censored)))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.Lambda_SE = abs(covariance_matrix[0][0]) ** 0.5
self.Lambda_upper = self.Lambda * (np.exp(Z * (self.Lambda_SE / self.Lambda)))
self.Lambda_lower = self.Lambda * (np.exp(-Z * (self.Lambda_SE / self.Lambda)))
SE_inv = abs(1 / self.Lambda * np.log(self.Lambda / self.Lambda_upper) / Z)
Data = {'Parameter': ['Lambda', '1/Lambda'],
'Point Estimate': [self.Lambda, 1 / self.Lambda],
'Standard Error': [self.Lambda_SE, SE_inv],
'Lower CI': [self.Lambda_lower, 1 / self.Lambda_upper],
'Upper CI': [self.Lambda_upper, 1 / self.Lambda_lower]}
df = pd.DataFrame(Data, columns=['Parameter', 'Point Estimate', 'Standard Error', 'Lower CI', 'Upper CI'])
self.results = df.set_index('Parameter')
if print_results is True:
pd.set_option('display.width', 200) # prevents wrapping after default 80 characters
pd.set_option('display.max_columns', 9) # shows the dataframe without ... truncation
def LL(params, T_f, T_rc): # log likelihood function (1 parameter Expon)
LL_f = 0
LL_rc = 0
LL_f += Fit_Expon_1P.logf(T_f, params[0]).sum() # failure times
LL_rc += Fit_Expon_1P.logR(T_rc, params[0]).sum() # right censored times
return -(LL_f + LL_rc)
AICc_total_weib = 0
BIC_total_weib = 0
AICc = True
AICc_weib = True
for i, stress in enumerate(unique_stresses_f):
failure_current_stress_df = f_df[f_df['stress'] == stress]
FAILURES = failure_current_stress_df['times'].values
if right_censored is not None:
if stress in unique_stresses_rc:
right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
RIGHT_CENSORED = right_cens_current_stress_df['times'].values
else:
RIGHT_CENSORED = None
else:
RIGHT_CENSORED = None
expon_fit = Fit_Expon_1P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False)
weib_fit = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_beta=np.average(weibull_fit_beta_array))
expon_fit_lambda_array.append(expon_fit.Lambda)
if type(expon_fit.AICc) == str:
AICc = False
else:
AICc_total += expon_fit.AICc
if type(weib_fit.AICc) == str:
AICc_weib = False
else:
AICc_total_weib += weib_fit.AICc
BIC_total += expon_fit.BIC
BIC_total_weib += weib_fit.BIC
if show_plot is True:
expon_fit.distribution.CDF(linestyle='--', color=color_list[i], xvals=xvals, plot_CI=False) # plotting of the confidence intervals has been turned off
Probability_plotting.Weibull_probability_plot(failures=FAILURES, right_censored=RIGHT_CENSORED,plot_CI=False, color=color_list[i], label=str(stress))
plt.legend(title='Stress')
self.__Weibull_2P_params = Fit_Weibull_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Weibull_2P_alpha = self.__Weibull_2P_params.alpha
self.Weibull_2P_beta = self.__Weibull_2P_params.beta
self.Weibull_2P_BIC = self.__Weibull_2P_params.BIC
self.Weibull_2P_AICc = self.__Weibull_2P_params.AICc
self._parametric_CDF_Weibull_2P = self.__Weibull_2P_params.distribution.CDF(xvals=d, show_plot=False)
self.__Gamma_2P_params = Fit_Gamma_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Gamma_2P_alpha = self.__Gamma_2P_params.alpha
self.Gamma_2P_beta = self.__Gamma_2P_params.beta
self.Gamma_2P_BIC = self.__Gamma_2P_params.BIC
self.Gamma_2P_AICc = self.__Gamma_2P_params.AICc
self._parametric_CDF_Gamma_2P = self.__Gamma_2P_params.distribution.CDF(xvals=d, show_plot=False)
self.__Expon_1P_params = Fit_Expon_1P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Expon_1P_lambda = self.__Expon_1P_params.Lambda
self.Expon_1P_BIC = self.__Expon_1P_params.BIC
self.Expon_1P_AICc = self.__Expon_1P_params.AICc
self._parametric_CDF_Exponential_1P = self.__Expon_1P_params.distribution.CDF(xvals=d, show_plot=False)
if max(failures) <= 1:
self.__Beta_2P_params = Fit_Beta_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Beta_2P_alpha = self.__Beta_2P_params.alpha
self.Beta_2P_beta = self.__Beta_2P_params.beta
self.Beta_2P_BIC = self.__Beta_2P_params.BIC
self.Beta_2P_AICc = self.__Beta_2P_params.AICc
self._parametric_CDF_Beta_2P = self.__Beta_2P_params.distribution.CDF(xvals=d, show_plot=False)
else:
self.Beta_2P_alpha = 0
self.Beta_2P_beta = 0
self.Beta_2P_BIC = 0
def LL(params, T_f, T_rc): # log likelihood function (1 parameter Expon)
LL_f = 0
LL_rc = 0
LL_f += Fit_Expon_1P.logf(T_f, params[0]).sum() # failure times
LL_rc += Fit_Expon_1P.logR(T_rc, params[0]).sum() # right censored times
return -(LL_f + LL_rc)
if max(failures) < 1:
xvals = np.logspace(-5, 1, 1000)
else:
xvals = np.logspace(np.floor(np.log10(min(failures))) - 3, np.ceil(np.log10(max(failures))) + 1, 1000)
if __fitted_dist_params is not None:
if __fitted_dist_params.gamma > 0:
fit_gamma = True
if fit_gamma is False:
if __fitted_dist_params is not None:
Lambda = __fitted_dist_params.Lambda
Lambda_SE = __fitted_dist_params.Lambda_SE ####
else:
from reliability.Fitters import Fit_Expon_1P
fit = Fit_Expon_1P(failures=failures, right_censored=right_censored, CI=CI, show_probability_plot=False, print_results=False)
Lambda = fit.Lambda
Lambda_SE = fit.Lambda_SE ####
if 'label' in kwargs:
label = kwargs.pop('label')
else:
label = str('Fitted Exponential_1P (λ=' + str(round_to_decimals(Lambda, dec)) + ')')
if 'color' in kwargs: ####
data_color = kwargs.get('color') ####
else: ####
data_color = 'k' ####
xlabel = 'Time' ####
elif fit_gamma is True:
if __fitted_dist_params is not None:
Lambda = __fitted_dist_params.Lambda
Lambda_SE = __fitted_dist_params.Lambda_SE ####
gamma = __fitted_dist_params.gamma ####
xvals = np.logspace(-2, np.ceil(np.log10(max(failures))) + 1, 1000)
if CI <= 0 or CI >= 1:
raise ValueError('CI must be between 0 and 1. Default is 0.95 for 95% Confidence interval.')
if __fitted_dist_params is not None:
if __fitted_dist_params.gamma > 0:
fit_gamma = True
if fit_gamma is False:
if __fitted_dist_params is not None:
Lambda = __fitted_dist_params.Lambda
Lambda_SE = __fitted_dist_params.Lambda_SE
else:
from reliability.Fitters import Fit_Expon_1P
fit = Fit_Expon_1P(failures=failures, right_censored=right_censored, CI=CI, show_probability_plot=False, print_results=False)
Lambda = fit.Lambda
Lambda_SE = fit.Lambda_SE
if 'label' in kwargs:
label = kwargs.pop('label')
else:
label = str('Fitted Exponential_1P (λ=' + str(round_to_decimals(Lambda, dec)) + ')')
if 'color' in kwargs:
data_color = kwargs.get('color')
else:
data_color = 'k'
xlabel = 'Time'
elif fit_gamma is True:
if __fitted_dist_params is not None:
Lambda = __fitted_dist_params.Lambda
Lambda_SE = __fitted_dist_params.Lambda_SE
gamma = __fitted_dist_params.gamma