Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.gamma.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
warnings.filterwarnings('ignore')
if force_beta is None:
guess = [sp[2], sp[0]]
result = minimize(value_and_grad(Fit_Gamma_2P.LL), guess, args=(failures, right_censored), jac=True, method='nelder-mead', tol=1e-10)
else:
guess = [sp[2]]
result = minimize(value_and_grad(Fit_Gamma_2P.LL_fb), guess, args=(failures, right_censored, force_beta), jac=True, method='nelder-mead', tol=1e-10)
if result.success is True:
params = result.x
self.success = True
if force_beta is None:
self.alpha = params[0]
self.beta = params[1]
else:
self.alpha = params[0]
self.beta = force_beta
else:
self.success = False
print('WARNING: Fitting using Autograd FAILED for Gamma_2P. The fit from Scipy was used instead so results may not be accurate.')
self.alpha = sp[2]
self.beta = sp[0]
self.gamma = sp[1]
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.gamma.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
warnings.filterwarnings('ignore')
if force_beta is None:
guess = [sp[2], sp[0]]
result = minimize(value_and_grad(Fit_Gamma_2P.LL), guess, args=(failures, right_censored), jac=True, method='nelder-mead', tol=1e-10)
else:
guess = [sp[2]]
result = minimize(value_and_grad(Fit_Gamma_2P.LL_fb), guess, args=(failures, right_censored, force_beta), jac=True, method='nelder-mead', tol=1e-10)
if result.success is True:
params = result.x
self.success = True
if force_beta is None:
self.alpha = params[0]
self.beta = params[1]
else:
self.alpha = params[0]
self.beta = force_beta
else:
self.success = False
print('WARNING: Fitting using Autograd FAILED for Gamma_2P. The fit from Scipy was used instead so results may not be accurate.')
def LL(params, T_f, T_rc): # log likelihood function (2 parameter Gamma)
LL_f = 0
LL_rc = 0
LL_f += Fit_Gamma_2P.logf(T_f, params[0], params[1]).sum() # failure times
LL_rc += Fit_Gamma_2P.logR(T_rc, params[0], params[1]).sum() # right censored times
return -(LL_f + LL_rc)
if max(failures) < 1:
xvals = np.linspace(10 ** -3, 2, 1000)
else:
xvals = np.logspace(-2, 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:
alpha = __fitted_dist_params.alpha
beta = __fitted_dist_params.beta
else:
from reliability.Fitters import Fit_Gamma_2P
fit = Fit_Gamma_2P(failures=failures, right_censored=right_censored, show_probability_plot=False, print_results=False)
alpha = fit.alpha
beta = fit.beta
gf = Gamma_Distribution(alpha=alpha, beta=beta).CDF(show_plot=False, xvals=xvals)
if 'label' in kwargs:
label = kwargs.pop('label')
else:
label = str('Fitted Gamma_2P (α=' + str(round_to_decimals(alpha, dec)) + ', β=' + str(round_to_decimals(beta, 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:
self.distribution = Gamma_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_Gamma_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:
hessian_matrix = hessian(Fit_Gamma_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)
self.alpha_SE = abs(covariance_matrix[0][0]) ** 0.5
self.beta_SE = ''
self.Cov_alpha_beta = ''
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_lower = ''
Data = {'Parameter': ['Alpha', 'Beta'],
'Point Estimate': [self.alpha, self.beta],
'Standard Error': [self.alpha_SE, self.beta_SE],
'Lower CI': [self.alpha_lower, self.beta_lower],
'Upper CI': [self.alpha_upper, self.beta_upper]}
df = pd.DataFrame(Data, columns=['Parameter', 'Point Estimate', 'Standard Error', 'Lower CI', 'Upper CI'])
self.results = df.set_index('Parameter')
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
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
params = [self.alpha, self.beta]
k = len(params)
n = len(all_data)
LL2 = 2 * Fit_Gamma_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 = Gamma_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_Gamma_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:
hessian_matrix = hessian(Fit_Gamma_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)
self.alpha_SE = abs(covariance_matrix[0][0]) ** 0.5
self.beta_SE = ''
self.Cov_alpha_beta = ''
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)))