def test_Fit_Everything(): dist = Beta_Distribution(alpha=5, beta=4) rawdata = dist.random_samples(200, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) fit = Fit_Everything(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False,show_histogram_plot=False,show_PP_plot=False,print_results=False) assert_allclose(fit.best_distribution.alpha, 0.5796887217806559,rtol=rtol,atol=atol) assert_allclose(fit.best_distribution.beta, 4.205258772699503,rtol=rtol,atol=atol) assert_allclose(fit.Beta_2P_BIC, 30.739845510058352,rtol=rtol,atol=atol)
def melhor_distribuicao(Falhas): raw_data = list(Falhas) data = make_right_censored_data(raw_data, threshold=1500) # right censor the data results = Fit_Everything(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, show_histogram_plot=False, show_PP_plot=False, print_results=False, exclude=[ 'Exponential_2P', 'Gamma_3P', 'Gamma_2P', 'Lognormal_3P', 'Loglogistic_3P', 'Gumbel_2P' ]) return results.best_distribution_name, results.best_distribution.parameters
def __init__(self, distribution, include_location_shifted=True, show_plot=True, print_results=True, number_of_distributions_to_show=3): # ensure the input is a distribution object if type(distribution) not in [ Weibull_Distribution, Normal_Distribution, Lognormal_Distribution, Exponential_Distribution, Gamma_Distribution, Beta_Distribution ]: raise ValueError( 'distribution must be a probability distribution object from the reliability.Distributions module. First define the distribution using Reliability.Distributions.___' ) # sample the CDF from 0.001 to 0.999. These samples will be used to fit all other distributions. RVS = distribution.quantile( np.linspace(0.001, 0.999, 698) ) # 698 samples is the ideal number for the points to align. Evidenced using plot_points. # filter out negative values RVS_filtered = [] negative_values_error = False for item in RVS: if item > 0: RVS_filtered.append(item) else: negative_values_error = True if negative_values_error is True: print( 'WARNING: The input distribution has non-negligible area for x<0. Samples from this region have been discarded to enable other distributions to be fitted.' ) fitted_results = Fit_Everything( failures=RVS_filtered, print_results=False, show_probability_plot=False, show_histogram_plot=False, show_PP_plot=False ) # fit all distributions to the filtered samples ranked_distributions = list(fitted_results.results.index.values) ranked_distributions.remove( distribution.name2 ) # removes the fitted version of the original distribution ranked_distributions_objects = [] ranked_distributions_labels = [] sigfig = 2 for dist_name in ranked_distributions: if dist_name == 'Weibull_2P': ranked_distributions_objects.append( Weibull_Distribution(alpha=fitted_results.Weibull_2P_alpha, beta=fitted_results.Weibull_2P_beta)) ranked_distributions_labels.append( str('Weibull_2P (α=' + str(round(fitted_results.Weibull_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Weibull_2P_beta, sigfig)) + ')')) elif dist_name == 'Gamma_2P': ranked_distributions_objects.append( Gamma_Distribution(alpha=fitted_results.Gamma_2P_alpha, beta=fitted_results.Gamma_2P_beta)) ranked_distributions_labels.append( str('Gamma_2P (α=' + str(round(fitted_results.Gamma_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Gamma_2P_beta, sigfig)) + ')')) elif dist_name == 'Normal_2P': ranked_distributions_objects.append( Normal_Distribution(mu=fitted_results.Normal_2P_mu, sigma=fitted_results.Normal_2P_sigma)) ranked_distributions_labels.append( str('Normal_2P (μ=' + str(round(fitted_results.Normal_2P_mu, sigfig)) + ',σ=' + str(round(fitted_results.Normal_2P_sigma, sigfig)) + ')')) elif dist_name == 'Lognormal_2P': ranked_distributions_objects.append( Lognormal_Distribution( mu=fitted_results.Lognormal_2P_mu, sigma=fitted_results.Lognormal_2P_sigma)) ranked_distributions_labels.append( str('Lognormal_2P (μ=' + str(round(fitted_results.Lognormal_2P_mu, sigfig)) + ',σ=' + str(round(fitted_results.Lognormal_2P_sigma, sigfig)) + ')')) elif dist_name == 'Exponential_1P': ranked_distributions_objects.append( Exponential_Distribution( Lambda=fitted_results.Expon_1P_lambda)) ranked_distributions_labels.append( str('Exponential_1P (lambda=' + str(round(fitted_results.Expon_1P_lambda, sigfig)) + ')')) elif dist_name == 'Beta_2P': ranked_distributions_objects.append( Beta_Distribution(alpha=fitted_results.Beta_2P_alpha, beta=fitted_results.Beta_2P_beta)) ranked_distributions_labels.append( str('Beta_2P (α=' + str(round(fitted_results.Beta_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Beta_2P_beta, sigfig)) + ')')) if include_location_shifted is True: if dist_name == 'Weibull_3P': ranked_distributions_objects.append( Weibull_Distribution( alpha=fitted_results.Weibull_3P_alpha, beta=fitted_results.Weibull_3P_beta, gamma=fitted_results.Weibull_3P_gamma)) ranked_distributions_labels.append( str('Weibull_3P (α=' + str( round(fitted_results.Weibull_3P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Weibull_3P_beta, sigfig)) + ',γ=' + str(round(fitted_results.Weibull_3P_gamma, sigfig)) + ')')) elif dist_name == 'Gamma_3P': ranked_distributions_objects.append( Gamma_Distribution( alpha=fitted_results.Gamma_3P_alpha, beta=fitted_results.Gamma_3P_beta, gamma=fitted_results.Gamma_3P_gamma)) ranked_distributions_labels.append( str('Gamma_3P (α=' + str(round(fitted_results.Gamma_3P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Gamma_3P_beta, sigfig)) + ',γ=' + str(round(fitted_results.Gamma_3P_gamma, sigfig)) + ')')) elif dist_name == 'Lognormal_3P': ranked_distributions_objects.append( Lognormal_Distribution( mu=fitted_results.Lognormal_3P_mu, sigma=fitted_results.Lognormal_3P_sigma, gamma=fitted_results.Lognormal_3P_gamma)) ranked_distributions_labels.append( str('Lognormal_3P (μ=' + str( round(fitted_results.Lognormal_3P_mu, sigfig)) + ',σ=' + str( round(fitted_results.Lognormal_3P_sigma, sigfig)) + ',γ=' + str( round(fitted_results.Lognormal_3P_gamma, sigfig)) + ')')) elif dist_name == 'Exponential_2P': ranked_distributions_objects.append( Exponential_Distribution( Lambda=fitted_results.Expon_1P_lambda, gamma=fitted_results.Expon_2P_gamma)) ranked_distributions_labels.append( str('Exponential_1P (lambda=' + str( round(fitted_results.Expon_1P_lambda, sigfig)) + ',γ=' + str(round(fitted_results.Expon_2P_gamma, sigfig)) + ')')) number_of_distributions_fitted = len(ranked_distributions_objects) self.results = ranked_distributions_objects self.most_similar_distribution = ranked_distributions_objects[0] if print_results is True: print('The input distribution was:') print(distribution.param_title_long) if number_of_distributions_fitted < number_of_distributions_to_show: number_of_distributions_to_show = number_of_distributions_fitted print('\nThe top', number_of_distributions_to_show, 'most similar distributions are:') counter = 0 while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: dist = ranked_distributions_objects[counter] print(dist.param_title_long) counter += 1 if show_plot is True: plt.figure(figsize=(14, 6)) plt.suptitle( str('Plot of similar distributions to ' + distribution.param_title_long)) counter = 0 xlower = distribution.quantile(0.001) xupper = distribution.quantile(0.999) x_delta = xupper - xlower plt.subplot(121) distribution.PDF(label=str('Input distribution [' + distribution.name2 + ']'), linestyle='--') while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: ranked_distributions_objects[counter].PDF( label=ranked_distributions_labels[counter]) counter += 1 plt.xlim([xlower - x_delta * 0.1, xupper + x_delta * 0.1]) plt.legend() plt.title('PDF') counter = 0 plt.subplot(122) distribution.CDF(label=str('Input distribution [' + distribution.name2 + ']'), linestyle='--') while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: ranked_distributions_objects[counter].CDF( label=ranked_distributions_labels[counter]) counter += 1 plt.xlim([xlower - x_delta * 0.1, xupper + x_delta * 0.1]) plt.legend() plt.title('CDF') plt.subplots_adjust(left=0.08, right=0.95) plt.show()
def test_Fit_Everything(): dist = Beta_Distribution(alpha=5, beta=4) rawdata = dist.random_samples(200, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) MLE = Fit_Everything(failures=data.failures, right_censored=data.right_censored, method='MLE', show_probability_plot=False, show_histogram_plot=False, show_PP_plot=False, show_best_distribution_probability_plot=False, print_results=False) LS = Fit_Everything(failures=data.failures, right_censored=data.right_censored, method='LS', show_probability_plot=False, show_histogram_plot=False, show_PP_plot=False, show_best_distribution_probability_plot=False, print_results=False) assert_allclose(MLE.best_distribution.alpha, 0.5796887225805948, rtol=rtol, atol=atol) # best fit here is a Beta distribution assert_allclose(MLE.best_distribution.beta, 4.205258710807067, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_alpha, 0.5796887225805948, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_beta, 4.205258710807067, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_AICc, 22.509958498975394, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_BIC, 29.04567952648771, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_loglik, -9.224522396695818, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_2P_AD, 543.31193295208, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_alpha, 0.5796887225805948, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_beta, 4.205258710807067, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_AICc, 24.571493772983473, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_BIC, 34.343996893035744, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_loglik, -9.224522396695818, rtol=rtol, atol=atol) assert_allclose(MLE.Weibull_3P_AD, 543.31193295208, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_alpha, 0.06343366643685251, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_beta, 8.730724670235508, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_AICc, 29.72088918292124, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_BIC, 36.25661021043356, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_loglik, -12.829987738668741, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_2P_AD, 543.5598195358288, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_alpha, 0.06343366643685251, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_beta, 8.730724670235508, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_AICc, 31.78242445692932, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_BIC, 41.55492757698159, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_loglik, -12.829987738668741, rtol=rtol, atol=atol) assert_allclose(MLE.Gamma_3P_AD, 543.5598195358288, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_alpha, 0.5327695781726263, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_beta, 4.959959950671738, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_AICc, 26.2468431389576, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_BIC, 32.78256416646992, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_loglik, -11.092964716686922, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_2P_AD, 543.3968941075816, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_alpha, 0.5327695781726263, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_beta, 4.959959950671738, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_AICc, 28.30837841296568, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_BIC, 38.08088153301795, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_loglik, -11.092964716686922, rtol=rtol, atol=atol) assert_allclose(MLE.Loglogistic_3P_AD, 543.3968941075816, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_mu, -0.6258670209896524, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_sigma, 0.3859306240146529, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_AICc, 36.58934382876143, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_BIC, 43.125064856273745, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_loglik, -16.264215061588835, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_2P_AD, 543.7578077426027, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_mu, -0.6258670209896524, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_sigma, 0.3859306240146529, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_AICc, 38.65087910276951, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_BIC, 48.42338222282178, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_loglik, -16.264215061588835, rtol=rtol, atol=atol) assert_allclose(MLE.Lognormal_3P_AD, 543.7578077426027, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_mu, 0.5313204293962966, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_sigma, 0.14842166096827056, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_AICc, 23.0363966340782, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_BIC, 29.572117661590518, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_loglik, -9.487741464247222, rtol=rtol, atol=atol) assert_allclose(MLE.Normal_2P_AD, 543.3042437249142, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_mu, 0.5706624792367315, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_sigma, 0.10182903954122995, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_AICc, 26.09054970134011, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_BIC, 32.626270728852425, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_loglik, -11.014817997878176, rtol=rtol, atol=atol) assert_allclose(MLE.Gumbel_2P_AD, 543.3089024789034, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_alpha, 5.586642953718748, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_beta, 4.950693419749502, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_AICc, 24.204124482547897, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_BIC, 30.739845510060213, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_loglik, -10.07160538848207, rtol=rtol, atol=atol) assert_allclose(MLE.Beta_2P_AD, 543.3809275359781, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_lambda, 1.5845505775713558, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_gamma, 0.12428161981215716, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_AICc, 127.11230931613672, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_BIC, 133.64803034364903, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_loglik, -61.52569780527648, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_2P_AD, 548.8966650502098, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_lambda, 1.1776736956890317, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_AICc, 192.73284561137785, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_BIC, 196.01096095772388, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_loglik, -95.35632179558792, rtol=rtol, atol=atol) assert_allclose(MLE.Exponential_1P_AD, 551.326873807673, rtol=rtol, atol=atol) assert_allclose(LS.best_distribution.mu, 0.5350756091376212, rtol=rtol, atol=atol) # best fit here is a Normal distribution assert_allclose(LS.best_distribution.sigma, 0.15352298167936318, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_alpha, 0.5948490848650297, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_beta, 3.850985192722524, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_AICc, 24.002343535956285, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_BIC, 30.538064563468602, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_loglik, -9.970714915186264, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_2P_AD, 543.3536598333712, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_alpha, 0.5796887225805948, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_beta, 4.205258710807067, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_AICc, 24.571493772983473, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_BIC, 34.343996893035744, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_loglik, -9.224522396695818, rtol=rtol, atol=atol) assert_allclose(LS.Weibull_3P_AD, 543.31193295208, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_alpha, 0.047474493713487956, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_beta, 11.56120649983023, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_AICc, 34.77520772749797, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_BIC, 41.31092875501029, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_loglik, -15.357147010957107, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_2P_AD, 543.5555679280225, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_alpha, 0.06343366643685251, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_beta, 8.730724670235508, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_AICc, 31.78242445692932, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_BIC, 41.55492757698159, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_loglik, -12.829987738668741, rtol=rtol, atol=atol) assert_allclose(LS.Gamma_3P_AD, 543.5598195358288, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_alpha, 0.5489258630949324, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_beta, 4.282869717868545, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_AICc, 29.55884374185365, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_BIC, 36.09456476936597, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_loglik, -12.748965018134946, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_2P_AD, 543.4725652046802, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_alpha, 0.5327695781726263, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_beta, 4.959959950671738, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_AICc, 28.30837841296568, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_BIC, 38.08088153301795, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_loglik, -11.092964716686922, rtol=rtol, atol=atol) assert_allclose(LS.Loglogistic_3P_AD, 543.3968941075816, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_mu, -0.5829545855241497, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_sigma, 0.42938026719038264, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_AICc, 39.2494098877054, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_BIC, 45.785130915217714, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_loglik, -17.59424809106082, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_2P_AD, 543.6895545238489, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_mu, -0.6258670209896524, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_sigma, 0.3859306240146529, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_AICc, 38.65087910276951, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_BIC, 48.42338222282178, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_loglik, -16.264215061588835, rtol=rtol, atol=atol) assert_allclose(LS.Lognormal_3P_AD, 543.7578077426027, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_mu, 0.5350756091376212, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_sigma, 0.15352298167936318, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_AICc, 23.270071653194492, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_BIC, 29.80579268070681, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_loglik, -9.604578973805367, rtol=rtol, atol=atol) assert_allclose(LS.Normal_2P_AD, 543.3018089629097, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_mu, 0.5575543755580943, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_sigma, 0.09267958281580514, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_AICc, 28.66352107358925, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_BIC, 35.19924210110157, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_loglik, -12.301303684002747, rtol=rtol, atol=atol) assert_allclose(LS.Gumbel_2P_AD, 543.3456378838292, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_alpha, 6.54242621734743, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_beta, 5.795236872686599, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_AICc, 25.745158997195162, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_BIC, 32.28088002470748, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_loglik, -10.842122645805702, rtol=rtol, atol=atol) assert_allclose(LS.Beta_2P_AD, 543.3718252593867, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_lambda, 1.1858797968873822, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_gamma, 0.12338161981215715, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_AICc, 136.25275877909922, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_BIC, 142.78847980661155, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_loglik, -66.09592253675774, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_2P_AD, 546.5849877012892, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_lambda, 1.0678223705385204, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_AICc, 193.7910857336068, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_BIC, 197.06920107995282, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_loglik, -95.88544185670239, rtol=rtol, atol=atol) assert_allclose(LS.Exponential_1P_AD, 549.85986679373, rtol=rtol, atol=atol)
def __init__(self, distribution=None, include_location_shifted=True, show_plot=True, print_results=True, monte_carlo_trials=1000, number_of_distributions_to_show=3): if type(distribution) not in [ Weibull_Distribution, Normal_Distribution, Lognormal_Distribution, Exponential_Distribution, Gamma_Distribution, Beta_Distribution ]: raise ValueError( 'distribution must be a probability distribution object from the reliability.Distributions module. First define the distribution using Reliability.Distributions.___' ) if monte_carlo_trials < 100: print( 'WARNING: Using less than 100 monte carlo trials will lead to extremely inaccurate results. The number of monte carlo trials has been changed to 100 to ensure accuracy.' ) monte_carlo_trials = 100 elif monte_carlo_trials >= 100 and monte_carlo_trials < 1000: print( 'WARNING: Using less than 1000 monte carlo trials will lead to inaccurate results.' ) if monte_carlo_trials > 10000: print( 'The recommended number of monte carlo trials is 1000. Using over 10000 may take a long time to calculate.' ) RVS = distribution.random_samples( number_of_samples=monte_carlo_trials ) # draw random samples from the original distribution # filter out negative values RVS_filtered = [] negative_values_error = False for item in RVS: if item > 0: RVS_filtered.append(item) else: negative_values_error = True if negative_values_error is True: print( 'WARNING: The input distribution has non-negligible area for x<0. Monte carlo samples from this region have been discarded to enable other distributions to be fitted.' ) fitted_results = Fit_Everything( failures=RVS_filtered, print_results=False, show_probability_plot=False, show_histogram_plot=False, show_PP_plot=False ) # fit all distributions to the filtered samples ranked_distributions = list(fitted_results.results.index.values) ranked_distributions.remove( distribution.name2 ) # removes the fitted version of the original distribution ranked_distributions_objects = [] ranked_distributions_labels = [] sigfig = 2 for dist_name in ranked_distributions: if dist_name == 'Weibull_2P': ranked_distributions_objects.append( Weibull_Distribution(alpha=fitted_results.Weibull_2P_alpha, beta=fitted_results.Weibull_2P_beta)) ranked_distributions_labels.append( str('Weibull_2P (α=' + str(round(fitted_results.Weibull_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Weibull_2P_beta, sigfig)) + ')')) elif dist_name == 'Gamma_2P': ranked_distributions_objects.append( Gamma_Distribution(alpha=fitted_results.Gamma_2P_alpha, beta=fitted_results.Gamma_2P_beta)) ranked_distributions_labels.append( str('Gamma_2P (α=' + str(round(fitted_results.Gamma_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Gamma_2P_beta, sigfig)) + ')')) elif dist_name == 'Normal_2P': ranked_distributions_objects.append( Normal_Distribution(mu=fitted_results.Normal_2P_mu, sigma=fitted_results.Normal_2P_sigma)) ranked_distributions_labels.append( str('Normal_2P (μ=' + str(round(fitted_results.Normal_2P_mu, sigfig)) + ',σ=' + str(round(fitted_results.Normal_2P_sigma, sigfig)) + ')')) elif dist_name == 'Lognormal_2P': ranked_distributions_objects.append( Lognormal_Distribution( mu=fitted_results.Lognormal_2P_mu, sigma=fitted_results.Lognormal_2P_sigma)) ranked_distributions_labels.append( str('Lognormal_2P (μ=' + str(round(fitted_results.Lognormal_2P_mu, sigfig)) + ',σ=' + str(round(fitted_results.Lognormal_2P_sigma, sigfig)) + ')')) elif dist_name == 'Exponential_1P': ranked_distributions_objects.append( Exponential_Distribution( Lambda=fitted_results.Expon_1P_lambda)) ranked_distributions_labels.append( str('Exponential_1P (lambda=' + str(round(fitted_results.Expon_1P_lambda, sigfig)) + ')')) elif dist_name == 'Beta_2P': ranked_distributions_objects.append( Beta_Distribution(alpha=fitted_results.Beta_2P_alpha, beta=fitted_results.Beta_2P_beta)) ranked_distributions_labels.append( str('Beta_2P (α=' + str(round(fitted_results.Beta_2P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Beta_2P_beta, sigfig)) + ')')) if include_location_shifted is True: if dist_name == 'Weibull_3P': ranked_distributions_objects.append( Weibull_Distribution( alpha=fitted_results.Weibull_3P_alpha, beta=fitted_results.Weibull_3P_beta, gamma=fitted_results.Weibull_3P_gamma)) ranked_distributions_labels.append( str('Weibull_3P (α=' + str( round(fitted_results.Weibull_3P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Weibull_3P_beta, sigfig)) + ',γ=' + str(round(fitted_results.Weibull_3P_gamma, sigfig)) + ')')) elif dist_name == 'Gamma_3P': ranked_distributions_objects.append( Gamma_Distribution( alpha=fitted_results.Gamma_3P_alpha, beta=fitted_results.Gamma_3P_beta, gamma=fitted_results.Gamma_3P_gamma)) ranked_distributions_labels.append( str('Gamma_3P (α=' + str(round(fitted_results.Gamma_3P_alpha, sigfig)) + ',β=' + str(round(fitted_results.Gamma_3P_beta, sigfig)) + ',γ=' + str(round(fitted_results.Gamma_3P_gamma, sigfig)) + ')')) elif dist_name == 'Lognormal_3P': ranked_distributions_objects.append( Lognormal_Distribution( mu=fitted_results.Lognormal_3P_mu, sigma=fitted_results.Lognormal_3P_sigma, gamma=fitted_results.Lognormal_3P_gamma)) ranked_distributions_labels.append( str('Lognormal_3P (μ=' + str( round(fitted_results.Lognormal_3P_mu, sigfig)) + ',σ=' + str( round(fitted_results.Lognormal_3P_sigma, sigfig)) + ',γ=' + str( round(fitted_results.Lognormal_3P_gamma, sigfig)) + ')')) elif dist_name == 'Exponential_2P': ranked_distributions_objects.append( Exponential_Distribution( Lambda=fitted_results.Expon_1P_lambda, gamma=fitted_results.Expon_2P_gamma)) ranked_distributions_labels.append( str('Exponential_1P (lambda=' + str( round(fitted_results.Expon_1P_lambda, sigfig)) + ',γ=' + str(round(fitted_results.Expon_2P_gamma, sigfig)) + ')')) number_of_distributions_fitted = len(ranked_distributions_objects) self.results = ranked_distributions_objects self.most_similar_distribution = ranked_distributions_objects[0] if print_results is True: print('The input distribution was:') print(distribution.param_title_long) if number_of_distributions_fitted < number_of_distributions_to_show: number_of_distributions_to_show = number_of_distributions_fitted print('\nThe top', number_of_distributions_to_show, 'most similar distributions are:') counter = 0 while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: dist = ranked_distributions_objects[counter] print(dist.param_title_long) counter += 1 if show_plot is True: plt.figure(figsize=(14, 6)) plt.suptitle( str('Plot of similar distributions to ' + distribution.param_title_long)) counter = 0 xlower = distribution.quantile(0.001) xupper = distribution.quantile(0.999) x_delta = xupper - xlower plt.subplot(121) distribution.PDF(label='Input distribution', linestyle='--') while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: ranked_distributions_objects[counter].PDF( label=ranked_distributions_labels[counter]) counter += 1 plt.xlim([xlower - x_delta * 0.1, xupper + x_delta * 0.1]) plt.legend() plt.title('PDF') counter = 0 plt.subplot(122) distribution.CDF(label='Input distribution', linestyle='--') while counter < number_of_distributions_to_show and counter < number_of_distributions_fitted: ranked_distributions_objects[counter].CDF( label=ranked_distributions_labels[counter]) counter += 1 plt.xlim([xlower - x_delta * 0.1, xupper + x_delta * 0.1]) plt.legend() plt.title('CDF') plt.subplots_adjust(left=0.08, right=0.95) plt.show()
# Print error message if at least one negative failure time is present for the given component if error == True: print( f"Error: {df.columns[i]} has at least one negative failure time. Negative values are not accepted. The analysis cannot move forward." ) # Perform reliability analysis if error == False: # Print component being analyzed print(f"Reliability analysis {df.columns[i]}:\n") # Fit all probability distributions available from 'reliability' library output = Fit_Everything(failures=df.iloc[:, i].dropna().tolist(), show_probability_plot=False, show_PP_plot=False) # Define the probability distribution that best fitted the failure times for the given component output.best_distribution.plot() # Define the desired time of failure 't' t = float(input("Type in the desired time before failure: ")) # Time 't' validation # Validate that no negative time was inserted while t < 0: print( "Error: negative value insterted. Please insert a positive value greater than 0:" ) t = float(input("Type in the desired time before failure: "))