Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
plt.subplot(122) # CDF
plt.bar(center, hist_cumulative * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).CDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).CDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).CDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).CDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Cumulative Distribution Function')
plt.xlabel('Data')
plt.ylabel('Cumulative probability density')
plt.suptitle('Histogram plot of each fitted distribution')
plt.legend()
plt.subplot(121) # PDF
# make this histogram. Can't use plt.hist due to need to scale the heights when there's censored data
num_bins = min(int(len(X) / 2), 30)
hist, bins = np.histogram(X, bins=num_bins, density=True)
hist_cumulative = np.cumsum(hist) / sum(hist)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).PDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).PDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).PDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).PDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).PDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).PDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
plt.subplot(122) # CDF
plt.bar(center, hist_cumulative * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
if best_dist == 'Weibull_2P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta)
elif best_dist == 'Weibull_3P':
self.best_distribution = Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma)
elif best_dist == 'Gamma_2P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta)
elif best_dist == 'Gamma_3P':
self.best_distribution = Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma)
elif best_dist == 'Lognormal_2P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma)
elif best_dist == 'Lognormal_3P':
self.best_distribution = Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma)
elif best_dist == 'Exponential_1P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_1P_lambda)
elif best_dist == 'Exponential_2P':
self.best_distribution = Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma)
elif best_dist == 'Normal_2P':
self.best_distribution = Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma)
elif best_dist == 'Beta_2P':
self.best_distribution = Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta)
# print the results
if print_results is True: # printing occurs by default
pd.set_option('display.width', 200) # prevents wrapping after default 80 characters
pd.set_option('display.max_columns', 9) # shows the dataframe without ... truncation
print(self.results)
if show_histogram_plot is True:
Fit_Everything.histogram_plot(self) # plotting occurs by default
if show_PP_plot is True:
Fit_Everything.P_P_plot(self) # plotting occurs by default
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).PDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Probability Density Function')
plt.xlabel('Data')
plt.ylabel('Probability density')
plt.legend()
plt.subplot(122) # CDF
plt.bar(center, hist_cumulative * self._frac_fail, align='center', width=width, alpha=0.2, color='k', edgecolor='k')
Weibull_Distribution(alpha=self.Weibull_2P_alpha, beta=self.Weibull_2P_beta).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta$)')
Weibull_Distribution(alpha=self.Weibull_3P_alpha, beta=self.Weibull_3P_beta, gamma=self.Weibull_3P_gamma).CDF(xvals=xvals, label=r'Weibull ($\alpha , \beta , \gamma$)')
Gamma_Distribution(alpha=self.Gamma_2P_alpha, beta=self.Gamma_2P_beta).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta$)')
Gamma_Distribution(alpha=self.Gamma_3P_alpha, beta=self.Gamma_3P_beta, gamma=self.Gamma_3P_gamma).CDF(xvals=xvals, label=r'Gamma ($\alpha , \beta , \gamma$)')
Exponential_Distribution(Lambda=self.Expon_1P_lambda).CDF(xvals=xvals, label=r'Exponential ($\lambda$)')
Exponential_Distribution(Lambda=self.Expon_2P_lambda, gamma=self.Expon_2P_gamma).CDF(xvals=xvals, label=r'Exponential ($\lambda , \gamma$)')
Lognormal_Distribution(mu=self.Lognormal_2P_mu, sigma=self.Lognormal_2P_sigma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma$)')
Lognormal_Distribution(mu=self.Lognormal_3P_mu, sigma=self.Lognormal_3P_sigma, gamma=self.Lognormal_3P_gamma).CDF(xvals=xvals, label=r'Lognormal ($\mu , \sigma , \gamma$)')
Normal_Distribution(mu=self.Normal_2P_mu, sigma=self.Normal_2P_sigma).CDF(xvals=xvals, label=r'Normal ($\mu , \sigma$)')
if max(X) <= 1: # condition for Beta Dist to be fitted
Beta_Distribution(alpha=self.Beta_2P_alpha, beta=self.Beta_2P_beta).CDF(xvals=xvals, label=r'Beta ($\alpha , \beta$)')
plt.legend()
plt.xlim([xmin, xmax])
plt.title('Cumulative Distribution Function')
plt.xlabel('Data')
plt.ylabel('Cumulative probability density')
plt.suptitle('Histogram plot of each fitted distribution')
plt.legend()
label = str('Fitted Exponential_2P\n(λ=' + str(round_to_decimals(Lambda, dec)) + ', γ=' + str(round_to_decimals(gamma, dec)) + ')')
if 'color' in kwargs: ####
data_color = kwargs.get('color') ####
else: ####
data_color = 'k' ####
xlabel = 'Time - gamma' ####
failures = failures - gamma + 0.009 # this 0.009 adjustment is to avoid taking the log of 0. It causes negligible difference to the fit and plot. 0.009 is chosen to be the same as Weibull_Fit_3P adjustment.
if right_censored is not None:
right_censored = right_censored - gamma + 0.009 # this 0.009 adjustment is to avoid taking the log of 0. It causes negligible difference to the fit and plot. 0.009 is chosen to be the same as Weibull_Fit_3P adjustment.
#### recalculate the xvals for the plotting range when gamma>0
if max(failures) - gamma < 1:
xvals = np.logspace(-5, 1, 1000)
else:
xvals = np.logspace(-4, np.ceil(np.log10(max(failures) - gamma)) + 1, 1000) ####needed to adjust the lower lim here so it is > 0
ef = Exponential_Distribution(Lambda=Lambda, Lambda_SE=Lambda_SE, CI=CI) ####added extra params and removed .CDF
# plot the failure points and format the scale and axes
x, y = plotting_positions(failures=failures, right_censored=right_censored, h1=h1, h2=h2)
plt.scatter(x, y, marker='.', linewidth=2, c=data_color)
plt.gca().set_yscale('function', functions=(axes_transforms.weibull_forward, axes_transforms.weibull_inverse))
plt.xscale('log')
plt.grid(b=True, which='major', color='k', alpha=0.3, linestyle='-')
plt.grid(b=True, which='minor', color='k', alpha=0.08, linestyle='-')
plt.ylim([0.0001, 0.9999])
xrange = plt.gca().get_xlim() # this ensures the previously plotted objects are considered when setting the range
xrange_min = min(min(x), xrange[0])
xrange_max = max(max(x), xrange[1])
if xrange_min <= 0:
xrange_min = 1e-2
pts_min_log = 10 ** (int(np.floor(np.log10(xrange_min)))) # second smallest point is rounded down to nearest power of 10
pts_max_log = 10 ** (int(np.ceil(np.log10(xrange_max)))) # largest point is rounded up to nearest power of 10