Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
all_data_shifted = np.hstack([failures_shifted, right_censored_shifted])
sp = ss.lognorm.fit(all_data_shifted, floc=0, optimizer='powell') # scipy's answer is used as an initial guess. Scipy is only correct when there is no censored data
guess = [np.log(sp[2]), sp[0]]
warnings.filterwarnings('ignore') # necessary to supress the warning about the jacobian when using the nelder-mead optimizer
result = minimize(value_and_grad(Fit_Lognormal_2P.LL), guess, args=(failures_shifted, right_censored_shifted), jac=True, tol=1e-2, method='nelder-mead')
if result.success is True:
params = result.x
mu = params[0]
sigma = params[1]
else:
print('WARNING: Fitting using Autograd FAILED for the gamma optimisation section of Lognormal_3P. The fit from Scipy was used instead so results may not be accurate.')
mu = sp[2]
sigma = sp[0]
LL2 = 2 * Fit_Lognormal_2P.LL([mu, sigma], failures_shifted, right_censored_shifted)
return LL2
if type(right_censored) != np.ndarray:
raise TypeError('right_censored must be a list or array of right censored failure data')
self.gamma = 0
all_data = np.hstack([failures, right_censored])
# solve it
sp = ss.lognorm.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
if force_sigma is None:
bnds = [(0.0001, None), (0.0001, None)] # bounds of solution
guess = [np.log(sp[2]), sp[0]]
result = minimize(value_and_grad(Fit_Lognormal_2P.LL), guess, args=(failures, right_censored), jac=True, bounds=bnds, tol=1e-6)
else:
bnds = [(0.0001, None)] # bounds of solution
guess = [np.log(sp[2])]
result = minimize(value_and_grad(Fit_Lognormal_2P.LL_fs), guess, args=(failures, right_censored, force_sigma), jac=True, bounds=bnds, tol=1e-6)
if result.success is True:
params = result.x
self.success = True
if force_sigma is None:
self.mu = params[0]
self.sigma = params[1]
else:
self.mu = params[0]
self.sigma = force_sigma
else:
self.success = False
warnings.warn('Fitting using Autograd FAILED for Lognormal_2P. The fit from Scipy was used instead so results may not be accurate.')
self.mu = np.log(sp[2])
self.sigma = sp[0]
'''
__BIC_minimizer is used by the minimize function to get the sigma which gives the lowest overall BIC
'''
BIC_tot = 0
for stress in 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
lognormal_fit_common_shape = Fit_Lognormal_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_sigma=common_shape_X)
BIC_tot += lognormal_fit_common_shape.BIC
return BIC_tot
# within this loop, each list of failures and right censored values will be unpacked for each unique stress and plotted as a probability plot as well as the CDF of the common sigma plot
AICc_total = 0
BIC_total = 0
AICc = 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
lognormal_fit_common_shape = Fit_Lognormal_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_sigma=common_shape)
lognormal_fit_mu_array_common_shape.append(lognormal_fit_common_shape.mu)
if type(lognormal_fit_common_shape.AICc) == str:
AICc = False
else:
AICc_total += lognormal_fit_common_shape.AICc
BIC_total += lognormal_fit_common_shape.BIC
if show_plot is True:
lognormal_fit_common_shape.distribution.CDF(linestyle='--', color=color_list[i], xvals=xvals)
Probability_plotting.Lognormal_probability_plot(failures=FAILURES, right_censored=RIGHT_CENSORED, color=color_list[i], label=str(stress))
plt.legend(title='Stress')
plt.xlim(10 ** (xmin + 1), 10 ** (xmax - 1))
if common_shape_method == 'BIC':
plt.title(str('ALT Lognormal Probability Plot\nOptimal BIC ' + r'$\sigma$ = ' + str(round(common_shape, 4))))
elif common_shape_method == 'weighted_average':
plt.title(str('ALT Lognormal Probability Plot\nWeighted average ' + r'$\sigma$ = ' + str(round(common_shape, 4))))
elif common_shape_method == 'average':
self.distribution = Lognormal_Distribution(mu=self.mu, sigma=self.sigma)
# confidence interval estimates of parameters
Z = -ss.norm.ppf((1 - CI) / 2)
if force_sigma is None:
hessian_matrix = hessian(Fit_Lognormal_2P.LL)(np.array(tuple(params)), np.array(tuple(failures)), np.array(tuple(right_censored)))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.mu_SE = abs(covariance_matrix[0][0]) ** 0.5
self.sigma_SE = abs(covariance_matrix[1][1]) ** 0.5
self.Cov_mu_sigma = abs(covariance_matrix[0][1])
self.mu_upper = self.mu + (Z * self.mu_SE) # these are unique to normal and lognormal mu params
self.mu_lower = self.mu + (-Z * self.mu_SE)
self.sigma_upper = self.sigma * (np.exp(Z * (self.sigma_SE / self.sigma)))
self.sigma_lower = self.sigma * (np.exp(-Z * (self.sigma_SE / self.sigma)))
else:
hessian_matrix = hessian(Fit_Lognormal_2P.LL_fs)(np.array(tuple([self.mu])), np.array(tuple(failures)), np.array(tuple(right_censored)), np.array(tuple([force_sigma])))
covariance_matrix = np.linalg.inv(hessian_matrix)
self.mu_SE = abs(covariance_matrix[0][0]) ** 0.5
self.sigma_SE = ''
self.Cov_mu_sigma = ''
self.mu_upper = self.mu + (Z * self.mu_SE) # these are unique to normal and lognormal mu params
self.mu_lower = self.mu + (-Z * self.mu_SE)
self.sigma_upper = ''
self.sigma_lower = ''
Data = {'Parameter': ['Mu', 'Sigma'],
'Point Estimate': [self.mu, self.sigma],
'Standard Error': [self.mu_SE, self.sigma_SE],
'Lower CI': [self.mu_lower, self.sigma_lower],
'Upper CI': [self.mu_upper, self.sigma_upper]}
df = pd.DataFrame(Data, columns=['Parameter', 'Point Estimate', 'Standard Error', 'Lower CI', 'Upper CI'])
self.results = df.set_index('Parameter')
self.__Lognormal_3P_params = Fit_Lognormal_3P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Lognormal_3P_mu = self.__Lognormal_3P_params.mu
self.Lognormal_3P_sigma = self.__Lognormal_3P_params.sigma
self.Lognormal_3P_gamma = self.__Lognormal_3P_params.gamma
self.Lognormal_3P_BIC = self.__Lognormal_3P_params.BIC
self.Lognormal_3P_AICc = self.__Lognormal_3P_params.AICc
self._parametric_CDF_Lognormal_3P = self.__Lognormal_3P_params.distribution.CDF(xvals=d, show_plot=False)
self.__Normal_2P_params = Fit_Normal_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Normal_2P_mu = self.__Normal_2P_params.mu
self.Normal_2P_sigma = self.__Normal_2P_params.sigma
self.Normal_2P_BIC = self.__Normal_2P_params.BIC
self.Normal_2P_AICc = self.__Normal_2P_params.AICc
self._parametric_CDF_Normal_2P = self.__Normal_2P_params.distribution.CDF(xvals=d, show_plot=False)
self.__Lognormal_2P_params = Fit_Lognormal_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
self.Lognormal_2P_mu = self.__Lognormal_2P_params.mu
self.Lognormal_2P_sigma = self.__Lognormal_2P_params.sigma
self.Lognormal_2P_BIC = self.__Lognormal_2P_params.BIC
self.Lognormal_2P_AICc = self.__Lognormal_2P_params.AICc
self._parametric_CDF_Lognormal_2P = self.__Lognormal_2P_params.distribution.CDF(xvals=d, show_plot=False)
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
if max(failures) < 1:
xvals = np.linspace(10 ** -3, 2, 1000)
else:
xvals = np.logspace(xmin_log - 2, xmax_log + 2, 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:
mu = __fitted_dist_params.mu
sigma = __fitted_dist_params.sigma
else:
from reliability.Fitters import Fit_Lognormal_2P
fit = Fit_Lognormal_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
mu = fit.mu
sigma = fit.sigma
lnf = Lognormal_Distribution(mu=mu, sigma=sigma).CDF(show_plot=False, xvals=xvals)
if 'label' in kwargs:
label = kwargs.pop('label')
else:
label = str('Fitted Lognormal_2P (μ=' + str(round_to_decimals(mu, dec)) + ', σ=' + str(round_to_decimals(sigma, dec)) + ')')
if 'color' in kwargs:
color = kwargs.pop('color')
data_color = color
else:
color = 'red'
data_color = 'k'
plt.xlabel('Time')
elif fit_gamma is True:
if __fitted_dist_params is not None:
def LL_fs(params, T_f, T_rc, force_sigma): # log likelihood function (2 parameter lognormal) FORCED SIGMA
LL_f = 0
LL_rc = 0
LL_f += Fit_Lognormal_2P.logf(T_f, params[0], force_sigma).sum() # failure times
LL_rc += Fit_Lognormal_2P.logR(T_rc, params[0], force_sigma).sum() # right censored times
return -(LL_f + LL_rc)