def test_Fit_Gamma_3P(): dist = Gamma_Distribution(alpha=50, beta=2, gamma=500) rawdata = dist.random_samples(20, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) fit = Fit_Gamma_3P(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False) assert_allclose(fit.alpha, 161.86588917785375,rtol=rtol,atol=atol) assert_allclose(fit.beta, 0.5429156736189351,rtol=rtol,atol=atol) assert_allclose(fit.gamma, 515.4451173341464,rtol=rtol,atol=atol) assert_allclose(fit.AICc, 150.01356065455715,rtol=rtol,atol=atol) assert_allclose(fit.Cov_alpha_beta, 11.303022319282686,rtol=rtol,atol=atol) assert_allclose(fit.loglik, -71.25678032727858,rtol=rtol,atol=atol) assert_allclose(fit.initial_guess[1], 0.5957708701483985,rtol=rtol,atol=atol)
def test_Fit_Gamma_2P(): dist = Gamma_Distribution(alpha=50, beta=2) rawdata = dist.random_samples(20, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) fit = Fit_Gamma_2P(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False) assert_allclose(fit.alpha, 30.895318057143644,rtol=rtol,atol=atol) assert_allclose(fit.beta, 2.5300452644724367,rtol=rtol,atol=atol) assert_allclose(fit.gamma, 0,rtol=rtol,atol=atol) assert_allclose(fit.AICc, 154.33194705093553,rtol=rtol,atol=atol) assert_allclose(fit.Cov_alpha_beta, 11.610666775358292,rtol=rtol,atol=atol) assert_allclose(fit.loglik, -74.81303234899717,rtol=rtol,atol=atol) assert_allclose(fit.initial_guess[1], 4.878548406768285,rtol=rtol,atol=atol)
def __update_params(_, self): value1 = self.s0.val value2 = self.s1.val value3 = self.s2.val if self.name == 'Weibull': dist = Weibull_Distribution(alpha=value1, beta=value2, gamma=value3) elif self.name == 'Loglogistic': dist = Loglogistic_Distribution(alpha=value1, beta=value2, gamma=value3) elif self.name == 'Gamma': dist = Gamma_Distribution(alpha=value1, beta=value2, gamma=value3) elif self.name == 'Loglogistic': dist = Loglogistic_Distribution(alpha=value1, beta=value2, gamma=value3) elif self.name == 'Lognormal': dist = Lognormal_Distribution(mu=value1, sigma=value2, gamma=value3) elif self.name == 'Beta': dist = Beta_Distribution(alpha=value1, beta=value2) elif self.name == 'Normal': dist = Normal_Distribution(mu=value1, sigma=value2) elif self.name == 'Exponential': dist = Exponential_Distribution(Lambda=value1, gamma=value2) else: raise ValueError(str(self.name + ' is an unknown distribution name')) plt.sca(self.ax_pdf) plt.cla() dist.PDF() plt.title('PDF') plt.xlabel('') plt.ylabel('') plt.sca(self.ax_cdf) plt.cla() dist.CDF() plt.title('CDF') plt.xlabel('') plt.ylabel('') plt.sca(self.ax_sf) plt.cla() dist.SF() plt.title('SF') plt.xlabel('') plt.ylabel('') plt.sca(self.ax_hf) plt.cla() dist.HF() plt.title('HF') plt.xlabel('') plt.ylabel('') plt.sca(self.ax_chf) plt.cla() dist.CHF() plt.title('CHF') plt.xlabel('') plt.ylabel('') plt.subplots_adjust(left=0.07, right=0.98, top=0.9, bottom=0.25, wspace=0.18, hspace=0.30) plt.suptitle(dist.param_title_long, fontsize=15) plt.draw()
def test_Fit_Gamma_3P(): dist = Gamma_Distribution(alpha=50, beta=2, gamma=500) rawdata = dist.random_samples(20, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) MLE = Fit_Gamma_3P(failures=data.failures, right_censored=data.right_censored, method='MLE', show_probability_plot=False, print_results=False) assert_allclose(MLE.alpha, 161.8637212853173, rtol=1e-6, atol=1e-4) # needs bigger tolerance for Python 3.8 and 3.9 assert_allclose(MLE.beta, 0.5429184966902371, rtol=1e-6, atol=1e-6) # needs bigger tolerance for Python 3.8 and 3.9 assert_allclose(MLE.gamma, 515.4451173341464, rtol=rtol, atol=atol) assert_allclose(MLE.AICc, 150.0135606540687, rtol=rtol, atol=atol) assert_allclose(MLE.BIC, 151.50075747473068, rtol=rtol, atol=atol) assert_allclose(MLE.loglik, -71.25678032703435, rtol=rtol, atol=atol) assert_allclose(MLE.AD, 38.63647775048046, rtol=rtol, atol=atol) assert_allclose( MLE.Cov_alpha_beta, 11.302538880460721, rtol=1e-4, atol=0.0002) # needs bigger tolerance for Python 3.8 and 3.9 LS = Fit_Gamma_3P(failures=data.failures, right_censored=data.right_censored, method='LS', show_probability_plot=False, print_results=False) assert_allclose(LS.alpha, 67.82751726558604, rtol=1e-6, atol=1e-4) # needs bigger tolerance for Python 3.8 and 3.9 assert_allclose(LS.beta, 31.29226170989501, rtol=1e-6, atol=1e-6) # needs bigger tolerance for Python 3.8 and 3.9 assert_allclose(LS.gamma, 505.13533298746347, rtol=rtol, atol=atol) assert_allclose(LS.AICc, 2727.0287032370256, rtol=rtol, atol=atol) assert_allclose(LS.BIC, 2728.5159000576878, rtol=rtol, atol=atol) assert_allclose(LS.loglik, -1359.7643516185128, rtol=rtol, atol=atol) assert_allclose(LS.AD, 574.8571449781781, rtol=rtol, atol=atol) assert_allclose( LS.Cov_alpha_beta, 2.4557691690459174, rtol=1e-4, atol=0.0002) # needs bigger tolerance for Python 3.8 and 3.9
def test_Fit_Gamma_2P(): dist = Gamma_Distribution(alpha=50, beta=2) rawdata = dist.random_samples(20, seed=5) data = make_right_censored_data(data=rawdata, threshold=dist.mean) MLE = Fit_Gamma_2P(failures=data.failures, right_censored=data.right_censored, method='MLE', show_probability_plot=False, print_results=False) assert_allclose(MLE.alpha, 30.895317427895733, rtol=rtol, atol=atol) assert_allclose(MLE.beta, 2.5300452519936405, rtol=rtol, atol=atol) assert_allclose(MLE.gamma, 0, rtol=rtol, atol=atol) assert_allclose(MLE.AICc, 154.33194705093553, rtol=rtol, atol=atol) assert_allclose(MLE.BIC, 155.61752924510233, rtol=rtol, atol=atol) assert_allclose(MLE.loglik, -74.81303234899717, rtol=rtol, atol=atol) assert_allclose(MLE.AD, 38.004356262808585, rtol=rtol, atol=atol) assert_allclose(MLE.Cov_alpha_beta, -11.610946543514364, rtol=rtol, atol=atol) LS = Fit_Gamma_2P(failures=data.failures, right_censored=data.right_censored, method='LS', show_probability_plot=False, print_results=False) assert_allclose(LS.alpha, 25.803340662553182, rtol=rtol, atol=atol) assert_allclose(LS.beta, 2.8344248030280284, rtol=rtol, atol=atol) assert_allclose(LS.gamma, 0, rtol=rtol, atol=atol) assert_allclose(LS.AICc, 154.55898223226797, rtol=rtol, atol=atol) assert_allclose(LS.BIC, 155.84456442643477, rtol=rtol, atol=atol) assert_allclose(LS.loglik, -74.92654993966339, rtol=rtol, atol=atol) assert_allclose(LS.AD, 38.01670664187149, rtol=rtol, atol=atol) assert_allclose(LS.Cov_alpha_beta, -5.761109354575602, rtol=rtol, atol=atol)
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 __update_distribution(name, self): self.name = name if self.name == 'Weibull': dist = Weibull_Distribution(alpha=100, beta=2, gamma=0) param_names = ['Alpha', 'Beta', 'Gamma'] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0.1, valmax=500, valinit=dist.alpha) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.2, valmax=25, valinit=dist.beta) try: # clear the slider axis if it exists plt.sca(self.ax2) plt.cla() except ValueError: # if the slider axis does no exist (because it was destroyed by a 2P distribution) then recreate it self.ax2 = plt.axes([0.1, 0.05, 0.8, 0.03], facecolor=self.background_color) self.s2 = Slider(self.ax2, param_names[2], valmin=0, valmax=500, valinit=dist.gamma) elif self.name == 'Gamma': dist = Gamma_Distribution(alpha=100, beta=5, gamma=0) param_names = ['Alpha', 'Beta', 'Gamma'] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0.1, valmax=500, valinit=dist.alpha) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.2, valmax=25, valinit=dist.beta) try: # clear the slider axis if it exists plt.sca(self.ax2) plt.cla() except ValueError: # if the slider axis does no exist (because it was destroyed by a 2P distribution) then recreate it self.ax2 = plt.axes([0.1, 0.05, 0.8, 0.03], facecolor=self.background_color) self.s2 = Slider(self.ax2, param_names[2], valmin=0, valmax=500, valinit=dist.gamma) elif self.name == 'Loglogistic': dist = Loglogistic_Distribution(alpha=100, beta=8, gamma=0) param_names = ['Alpha', 'Beta', 'Gamma'] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0.1, valmax=500, valinit=dist.alpha) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.2, valmax=50, valinit=dist.beta) try: # clear the slider axis if it exists plt.sca(self.ax2) plt.cla() except ValueError: # if the slider axis does no exist (because it was destroyed by a 2P distribution) then recreate it self.ax2 = plt.axes([0.1, 0.05, 0.8, 0.03], facecolor=self.background_color) self.s2 = Slider(self.ax2, param_names[2], valmin=0, valmax=500, valinit=dist.gamma) elif self.name == 'Lognormal': dist = Lognormal_Distribution(mu=2.5, sigma=0.5, gamma=0) param_names = ['Mu', 'Sigma', 'Gamma'] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0, valmax=5, valinit=dist.mu) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.01, valmax=2, valinit=dist.sigma) try: # clear the slider axis if it exists plt.sca(self.ax2) plt.cla() except ValueError: # if the slider axis does no exist (because it was destroyed by a 2P distribution) then recreate it self.ax2 = plt.axes([0.1, 0.05, 0.8, 0.03], facecolor=self.background_color) self.s2 = Slider(self.ax2, param_names[2], valmin=0, valmax=500, valinit=dist.gamma) elif self.name == 'Normal': dist = Normal_Distribution(mu=0, sigma=10) param_names = ['Mu', 'Sigma', ''] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=-100, valmax=100, valinit=dist.mu) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.01, valmax=20, valinit=dist.sigma) try: # clear the slider axis if it exists self.ax2.remove() # this will destroy the axes except KeyError: pass elif self.name == 'Exponential': dist = Exponential_Distribution(Lambda=1, gamma=0) param_names = ['Lambda', 'Gamma', ''] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0.001, valmax=5, valinit=dist.Lambda) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0, valmax=500, valinit=dist.gamma) try: # clear the slider axis if it exists self.ax2.remove() # this will destroy the axes except KeyError: pass elif self.name == 'Beta': dist = Beta_Distribution(alpha=2, beta=2) param_names = ['Alpha', 'Beta', ''] plt.sca(self.ax0) plt.cla() self.s0 = Slider(self.ax0, param_names[0], valmin=0.01, valmax=5, valinit=dist.alpha) plt.sca(self.ax1) plt.cla() self.s1 = Slider(self.ax1, param_names[1], valmin=0.01, valmax=5, valinit=dist.beta) try: # clear the slider axis if it exists self.ax2.remove() # this will destroy the axes except KeyError: pass else: raise ValueError(str(self.name + ' is an unknown distribution name')) plt.suptitle(dist.param_title_long, fontsize=15) distribution_explorer.__update_params(None, self) distribution_explorer.__interactive(self) plt.draw()
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()
def HistogramPLOT_all(data, month, year): #Initiate Situation = [] mon = [ 'January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] #Get just Full day data logicF = (data["isFULL"].apply(lambda x: x) == (1)) data01 = data[logicF].copy() data01.fillna(method='ffill', inplace=True) logicY = (data01["DateTime"].apply(lambda x: x.year) == (year)) data01 = data01[logicY].copy() fig = plt.figure(figsize=(24, 18), dpi=80, facecolor='w', edgecolor='r') #Plotting 12 graph xvals = np.linspace(0, 30, 1000) for i in range(month): ax = plt.subplot2grid((4, 3), (int(np.floor(i / 3)), int(i % 3))) logic = (data01["DateTime"].apply(lambda x: x.month)) == (i + 1) ws = data01['WS95'][logic] ws = ws + 0.0001 failures = [] censored = [] threshold = 30 for item in ws: if item > threshold: censored.append(threshold) else: failures.append(item) xvals = np.linspace(0, 30, 1000) print(ws.shape) if (np.sum(logic) != 0): ax.hist(ws, bins=30, normed=True) hist, edge = np.histogram(np.array(ws), bins=1000, range=(0, 30), normed=True) wb2 = Fit_Weibull_2P(failures=failures, show_probability_plot=False, print_results=False) wb3 = Fit_Weibull_3P(failures=failures, show_probability_plot=False, print_results=False) gm2 = Fit_Gamma_2P(failures=failures, show_probability_plot=False, print_results=False) gm3 = Fit_Gamma_3P(failures=failures, show_probability_plot=False, print_results=False) ln2 = Fit_Lognormal_2P(failures=failures, show_probability_plot=False, print_results=False) wbm = Fit_Weibull_Mixture(failures=failures, right_censored=censored, show_plot=False, print_results=False) wb2_pdf = Weibull_Distribution(alpha=wb2.alpha, beta=wb2.beta).PDF( xvals=xvals, show_plot=True, label='Weibull_2P') wb3_pdf = Weibull_Distribution(alpha=wb3.alpha, beta=wb3.beta, gamma=wb3.gamma).PDF( xvals=xvals, show_plot=True, label='Weibull_3P') gm2_pdf = Gamma_Distribution(alpha=gm2.alpha, beta=gm2.beta).PDF(xvals=xvals, show_plot=True, label='Gamma_2P') gm3_pdf = Gamma_Distribution(alpha=gm3.alpha, beta=gm3.beta, gamma=gm3.gamma).PDF(xvals=xvals, show_plot=True, label='Gamma_3P') ln2_pdf = Lognormal_Distribution(mu=ln2.mu, sigma=ln2.sigma).PDF( xvals=xvals, show_plot=True, label='Lognormal_2P') part1_pdf = Weibull_Distribution(alpha=wbm.alpha_1, beta=wbm.beta_1).PDF( xvals=xvals, show_plot=False) part2_pdf = Weibull_Distribution(alpha=wbm.alpha_2, beta=wbm.beta_2).PDF( xvals=xvals, show_plot=False) Mixture_PDF = part1_pdf * wbm.proportion_1 + part2_pdf * wbm.proportion_2 ax.plot(xvals, Mixture_PDF, label='Weibull_Mixture') ax.legend() ax.set_ylim(0, 0.16) ax.set_xlim(0, 30) ax.set_xticks([0, 5, 10, 15, 20, 25, 30]) ax.tick_params(axis="x", labelsize=20) ax.tick_params(axis="y", labelsize=20) ax.set_title('{}'.format(mon[i]), fontweight='bold', size=20) plt.tight_layout() plt.show()
def test_Gamma_Distribution(): dist = Gamma_Distribution(alpha=5, beta=2, gamma=10) assert_allclose(dist.mean, 20, rtol=rtol, atol=atol) assert_allclose(dist.standard_deviation, 7.0710678118654755, rtol=rtol, atol=atol) assert_allclose(dist.variance, 50, rtol=rtol, atol=atol) assert_allclose(dist.skewness, 1.414213562373095, rtol=rtol, atol=atol) assert_allclose(dist.kurtosis, 6, rtol=rtol, atol=atol) assert dist.param_title_long == 'Gamma Distribution (α=5,β=2,γ=10)' assert_allclose(dist.quantile(0.2), 14.121941545164923, rtol=rtol, atol=atol) assert_allclose(dist.inverse_SF(q=0.7), 15.486746053517457, rtol=rtol, atol=atol) assert_allclose(dist.mean_residual_life(20), 6.666666666666647, rtol=rtol, atol=atol) xvals = [dist.gamma - 1, dist.quantile(0.001), dist.quantile(0.01), dist.quantile(0.1), dist.quantile(0.9), dist.quantile(0.99), dist.quantile(0.999)] assert_allclose(dist.PDF(xvals=xvals, show_plot=False), [0.0, 0.008677353779839614, 0.02560943552734864, 0.06249207734544239, 0.015909786387521992, 0.001738163417685293, 0.00018045617911753266], rtol=rtol, atol=atol) assert_allclose(dist.CDF(xvals=xvals, show_plot=False), [0.0, 0.001, 0.01, 0.1, 0.9, 0.99, 0.999], rtol=rtol, atol=atol) assert_allclose(dist.SF(xvals=xvals, show_plot=False), [1.0, 0.999, 0.99, 0.9, 0.1, 0.01, 0.001], rtol=rtol, atol=atol) assert_allclose(dist.HF(xvals=xvals, show_plot=False), [0.0, 0.008686039819659272, 0.025868116694291555, 0.06943564149493599, 0.15909786387522004, 0.17381634176852898, 0.18045617911753245], rtol=rtol, atol=atol) assert_allclose(dist.CHF(xvals=xvals, show_plot=False), [-0.0, 0.0010005003335835344, 0.01005033585350145, 0.10536051565782628, 2.3025850929940463, 4.605170185988089, 6.907755278982136], rtol=rtol, atol=atol)