예제 #1
0
def HistogramPLOT_wbm(data, month, year):
    #Initiate
    Situation = []
    mon = [
        'January', 'Febuary', 'March', 'April', 'May', 'June', 'July',
        'August', 'September', 'October', 'November', 'December'
    ]
    data01 = data[['DateTime', 'WS95']].copy()
    data01.dropna(how='any', inplace=True)
    logicY = (data01["DateTime"].apply(lambda x: x.year) == (year))
    data01 = data01[logicY].copy()
    fig = plt.figure(figsize=(20, 32), 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)
        if (np.sum(logic) != 0):
            ax.hist(ws, bins=30, density=True)
            hist, edge = np.histogram(np.array(ws),
                                      bins=1000,
                                      range=(0, 30),
                                      density=True)
            wbm = Fit_Weibull_Mixture(failures=failures,
                                      right_censored=censored,
                                      show_plot=False,
                                      print_results=False)
            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.18)
        ax.set_xlim(0, 30)
        ax.set_xticks([0, 5, 10, 15, 20, 25, 30])
        ax.tick_params(axis="x", labelsize=30)
        ax.tick_params(axis="y", labelsize=26)
        ax.set_title('{}'.format(mon[i]), fontweight='bold', size=30)
    plt.tight_layout()
    plt.show()
def test_histogram():
    plt.ion()
    dist = Weibull_Distribution(alpha=30, beta=4)
    samples = dist.random_samples(500, seed=2)
    plt.subplot(121)
    histogram(samples, white_above=dist.mean)
    plt.subplot(122)
    histogram(samples, white_above=dist.mean, cumulative=True)
    plt.show()
    plt.close()
    plt.ioff()
예제 #3
0
def test_Fit_Weibull_2P():
    dist = Weibull_Distribution(alpha=50, beta=2)
    rawdata = dist.random_samples(20, seed=5)
    data = make_right_censored_data(data=rawdata, threshold=dist.mean)
    fit = Fit_Weibull_2P(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False)
    assert_allclose(fit.alpha, 45.099010886086354,rtol=rtol,atol=atol)
    assert_allclose(fit.beta, 2.7827531773597975,rtol=rtol,atol=atol)
    assert_allclose(fit.gamma, 0,rtol=rtol,atol=atol)
    assert_allclose(fit.AICc, 115.66971887883678,rtol=rtol,atol=atol)
    assert_allclose(fit.Cov_alpha_beta, 0.9178064889295382,rtol=rtol,atol=atol)
    assert_allclose(fit.loglik, -55.4819182629478,rtol=rtol,atol=atol)
    assert_allclose(fit.initial_guess[1], 2.96571536864614,rtol=rtol,atol=atol)
예제 #4
0
def test_Fit_Weibull_3P():
    dist = Weibull_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_Weibull_3P(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False)
    assert_allclose(fit.alpha, 41.38429989624438,rtol=rtol,atol=atol)
    assert_allclose(fit.beta, 0.6941872050001636,rtol=rtol,atol=atol)
    assert_allclose(fit.gamma, 514.5074549826453,rtol=rtol,atol=atol)
    assert_allclose(fit.AICc, 114.68964821342475,rtol=rtol,atol=atol)
    assert_allclose(fit.Cov_alpha_beta, 1.3076497743444904,rtol=rtol,atol=atol)
    assert_allclose(fit.loglik, -53.59482410671237,rtol=rtol,atol=atol)
    assert_allclose(fit.initial_guess[1], 1.2001459605994476,rtol=rtol,atol=atol)
예제 #5
0
def test_Fit_Weibull_Mixture():
    group_1 = Weibull_Distribution(alpha=10, beta=3).random_samples(40, seed=2)
    group_2 = Weibull_Distribution(alpha=40, beta=4).random_samples(60, seed=2)
    raw_data = np.hstack([group_1, group_2])
    data = make_right_censored_data(data=raw_data, threshold=40)
    fit = Fit_Weibull_Mixture(failures=data.failures, right_censored=data.right_censored, print_results=False, show_probability_plot=False)
    assert_allclose(fit.alpha_1, 8.711417800323375,rtol=rtol,atol=atol)
    assert_allclose(fit.alpha_2, 36.96927163816745, rtol=rtol, atol=atol)
    assert_allclose(fit.beta_1, 3.8780149153215278, rtol=rtol, atol=atol)
    assert_allclose(fit.beta_2, 4.901762153365169, rtol=rtol, atol=atol)
    assert_allclose(fit.AICc, 678.8135631521253, rtol=rtol, atol=atol)
    assert_allclose(fit.loglik, -334.08763263989243, rtol=rtol, atol=atol)
예제 #6
0
def test_Fit_Weibull_CR():
    d1 = Weibull_Distribution(alpha=50, beta=2)
    d2 = Weibull_Distribution(alpha=40, beta=10)
    CR_model = Competing_Risks_Model(distributions=[d1, d2])
    raw_data = CR_model.random_samples(100, seed=2)
    data = make_right_censored_data(data=raw_data, threshold=40)
    fit = Fit_Weibull_CR(failures=data.failures, right_censored=data.right_censored, print_results=False, show_probability_plot=False)
    assert_allclose(fit.alpha_1, 53.046588163359885,rtol=rtol,atol=atol)
    assert_allclose(fit.alpha_2, 38.029009901631845, rtol=rtol, atol=atol)
    assert_allclose(fit.beta_1, 1.9386519084349318, rtol=rtol, atol=atol)
    assert_allclose(fit.beta_2, 9.036248817085612, rtol=rtol, atol=atol)
    assert_allclose(fit.AICc, 665.5312987516257, rtol=rtol, atol=atol)
    assert_allclose(fit.loglik, -328.5551230600234, rtol=rtol, atol=atol)
예제 #7
0
def test_Fit_Weibull_CR():
    d1 = Weibull_Distribution(alpha=50, beta=2)
    d2 = Weibull_Distribution(alpha=40, beta=10)
    CR_model = Competing_Risks_Model(distributions=[d1, d2])
    raw_data = CR_model.random_samples(100, seed=2)
    data = make_right_censored_data(data=raw_data, threshold=40)
    MLE = Fit_Weibull_CR(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False)
    assert_allclose(MLE.alpha_1, 53.05674752263902, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta_1, 1.9411091317375062, rtol=rtol, atol=atol)
    assert_allclose(MLE.alpha_2, 38.026383998212154, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta_2, 9.033349805988692, rtol=rtol, atol=atol)
    assert_allclose(MLE.AICc, 665.5311523940719, rtol=rtol, atol=atol)
    assert_allclose(MLE.BIC, 675.5307805064454, rtol=rtol, atol=atol)
    assert_allclose(MLE.loglik, -328.5550498812465, rtol=rtol, atol=atol)
    assert_allclose(MLE.AD, 34.0918038201449, rtol=rtol, atol=atol)
예제 #8
0
def test_similar_distributions():
    dist = Weibull_Distribution(alpha=50, beta=3.3)
    results = similar_distributions(distribution=dist,
                                    include_location_shifted=True,
                                    show_plot=False,
                                    print_results=False)
    assert_allclose(results.results[0].alpha,
                    49.22635661916984,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[0].beta,
                    3.2573154824967254,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[0].gamma,
                    0.723515074225681,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[1].mu,
                    44.84713832685586,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[1].sigma,
                    14.922616880719513,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[2].alpha,
                    5.760745623490939,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[2].beta,
                    7.7849536438935685,
                    rtol=rtol,
                    atol=atol)
    assert_allclose(results.results[2].gamma, 0, rtol=rtol, atol=atol)
예제 #9
0
def test_crosshairs():
    plt.ion(
    )  # this is the key to enabling plt.close() to take control of a plot that is being blocked by plt.show() inside the plot generating function.
    Weibull_Distribution(alpha=50, beta=2).CDF()
    crosshairs(xlabel='t', ylabel='F')
    plt.close()
    plt.ioff()
예제 #10
0
def test_Fit_Weibull_2P():
    data = Weibull_Distribution(alpha=50, beta=2).random_samples(20, seed=5)
    fit = Fit_Weibull_2P(failures=data, show_probability_plot=False, print_results=False)
    assert fit.alpha == 47.507980313141516
    assert fit.beta == 2.492960611854891

#need to add more tests.
예제 #11
0
def test_stress_strength():
    stress = Weibull_Distribution(alpha=40, beta=4)
    strength = Lognormal_Distribution(mu=1.8, sigma=0.25, gamma=50)
    result = stress_strength(stress=stress,
                             strength=strength,
                             print_results=False,
                             show_plot=False)
    assert_allclose(result, 0.021559141113795574, rtol=rtol, atol=atol)
def test_Probability_of_failure():
    stress = Weibull_Distribution(alpha=40, beta=4)
    strength = Lognormal_Distribution(mu=1.8, sigma=0.25, gamma=50)
    result = Probability_of_failure(stress=stress,
                                    strength=strength,
                                    print_results=False,
                                    show_distribution_plot=False)
    assert_allclose(result, 0.02155359226336879, rtol=rtol, atol=atol)
예제 #13
0
def test_Fit_Weibull_Mixture():
    d1 = Weibull_Distribution(alpha=10, beta=3)
    d2 = Weibull_Distribution(alpha=40, beta=4)
    dist = Mixture_Model(distributions=[d1, d2], proportions=[0.2, 0.8])
    raw_data = dist.random_samples(100, seed=2)
    data = make_right_censored_data(data=raw_data, threshold=dist.mean)

    MLE = Fit_Weibull_Mixture(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False)
    assert_allclose(MLE.alpha_1, 11.06604639424718, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta_1, 2.735078296796997, rtol=rtol, atol=atol)
    assert_allclose(MLE.alpha_2, 34.325433665495346, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta_2, 7.60238532821206, rtol=rtol, atol=atol)
    assert_allclose(MLE.proportion_1, 0.23640116719132157, rtol=rtol, atol=atol)
    assert_allclose(MLE.proportion_2, 0.7635988328086785, rtol=rtol, atol=atol)
    assert_allclose(MLE.AICc, 471.97390405380236, rtol=rtol, atol=atol)
    assert_allclose(MLE.BIC, 484.3614571114024, rtol=rtol, atol=atol)
    assert_allclose(MLE.loglik, -230.66780309073096, rtol=rtol, atol=atol)
    assert_allclose(MLE.AD, 320.1963544647712, rtol=rtol, atol=atol)
    def __init__(self):
        # initialise the 5 plots
        plt.figure('Distribution Explorer', figsize=(12, 7))
        self.name = 'Weibull'  # starting value
        dist = Weibull_Distribution(alpha=100, beta=2, gamma=0)
        plt.suptitle(dist.param_title_long, fontsize=15)
        self.ax_pdf = plt.subplot(231)
        dist.PDF()
        plt.title('PDF')
        plt.xlabel('')
        plt.ylabel('')
        self.ax_cdf = plt.subplot(232)
        dist.CDF()
        plt.title('CDF')
        plt.xlabel('')
        plt.ylabel('')
        self.ax_sf = plt.subplot(233)
        dist.SF()
        plt.title('SF')
        plt.xlabel('')
        plt.ylabel('')
        self.ax_hf = plt.subplot(234)
        dist.HF()
        plt.title('HF')
        plt.xlabel('')
        plt.ylabel('')
        self.ax_chf = plt.subplot(235)
        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)

        # initialise the sliders
        x0 = 0.1
        width = 0.8
        height = 0.03
        self.active_color = 'steelblue'
        self.background_color = 'whitesmoke'
        self.ax0 = plt.axes([x0, 0.15, width, height], facecolor=self.background_color)
        self.ax1 = plt.axes([x0, 0.1, width, height], facecolor=self.background_color)
        self.ax2 = plt.axes([x0, 0.05, width, height], facecolor=self.background_color)
        self.s0 = Slider(self.ax0, 'Alpha', valmin=0.1, valmax=500, valinit=dist.alpha, facecolor=self.active_color)
        self.s1 = Slider(self.ax1, 'Beta', valmin=0.2, valmax=25, valinit=dist.beta, facecolor=self.active_color)
        self.s2 = Slider(self.ax2, 'Gamma', valmin=0, valmax=500, valinit=dist.gamma, facecolor=self.active_color)
        plt.subplots_adjust(left=0.07, right=0.98, top=0.9, bottom=0.25, wspace=0.18, hspace=0.30)

        # initialise the radio button
        radio_ax = plt.axes([0.708, 0.25, 0.27, 0.28], facecolor=self.background_color)
        radio_ax.set_title('Distribution')
        self.radio = RadioButtons(radio_ax, ('Weibull', 'Gamma', 'Normal', 'Lognormal', 'Beta', 'Exponential', 'Loglogistic'), active=0, activecolor=self.active_color)

        # begin the interactive section
        distribution_explorer.__interactive(self, initial_run=True)
예제 #15
0
def test_Fit_Weibull_3P():
    dist = Weibull_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_Weibull_3P(failures=data.failures,
                         right_censored=data.right_censored,
                         method='MLE',
                         show_probability_plot=False,
                         print_results=False)
    assert_allclose(MLE.alpha, 33.012333115455654, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta, 1.3273429690012193, rtol=rtol, atol=atol)
    assert_allclose(MLE.gamma, 513.7219668227023, rtol=rtol, atol=atol)
    assert_allclose(MLE.AICc, 116.53275812088592, rtol=rtol, atol=atol)
    assert_allclose(MLE.BIC, 118.0199549415479, rtol=rtol, atol=atol)
    assert_allclose(MLE.loglik, -54.51637906044296, rtol=rtol, atol=atol)
    assert_allclose(MLE.AD, 55.6067996515715, rtol=rtol, atol=atol)
    assert_allclose(MLE.Cov_alpha_beta,
                    -0.7687692976797089,
                    rtol=rtol,
                    atol=atol)

    LS = Fit_Weibull_3P(failures=data.failures,
                        right_censored=data.right_censored,
                        method='LS',
                        show_probability_plot=False,
                        print_results=False)
    assert_allclose(LS.alpha, 32.639290779819824, rtol=rtol, atol=atol)
    assert_allclose(LS.beta, 1.2701961119432184, rtol=rtol, atol=atol)
    assert_allclose(LS.gamma, 514.5065549826453, rtol=rtol, atol=atol)
    assert_allclose(LS.AICc, 119.47369772704523, rtol=rtol, atol=atol)
    assert_allclose(LS.BIC, 120.96089454770721, rtol=rtol, atol=atol)
    assert_allclose(LS.loglik, -55.98684886352262, rtol=rtol, atol=atol)
    assert_allclose(LS.AD, 55.70853682331155, rtol=rtol, atol=atol)
    assert_allclose(LS.Cov_alpha_beta,
                    -0.8435523816679948,
                    rtol=rtol,
                    atol=atol)
예제 #16
0
def test_Fit_Weibull_2P():
    dist = Weibull_Distribution(alpha=50, beta=2)
    rawdata = dist.random_samples(20, seed=5)
    data = make_right_censored_data(data=rawdata, threshold=dist.mean)

    MLE = Fit_Weibull_2P(failures=data.failures,
                         right_censored=data.right_censored,
                         method='MLE',
                         show_probability_plot=False,
                         print_results=False)
    assert_allclose(MLE.alpha, 45.099010886086354, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta, 2.7827531773597984, rtol=rtol, atol=atol)
    assert_allclose(MLE.gamma, 0, rtol=rtol, atol=atol)
    assert_allclose(MLE.AICc, 115.66971887883678, rtol=rtol, atol=atol)
    assert_allclose(MLE.BIC, 116.95530107300358, rtol=rtol, atol=atol)
    assert_allclose(MLE.loglik, -55.4819182629478, rtol=rtol, atol=atol)
    assert_allclose(MLE.AD, 55.60004028891652, rtol=rtol, atol=atol)
    assert_allclose(MLE.Cov_alpha_beta,
                    -0.9178064889295378,
                    rtol=rtol,
                    atol=atol)

    LS = Fit_Weibull_2P(failures=data.failures,
                        right_censored=data.right_censored,
                        method='LS',
                        show_probability_plot=False,
                        print_results=False)
    assert_allclose(LS.alpha, 42.91333312142757, rtol=rtol, atol=atol)
    assert_allclose(LS.beta, 2.9657153686461033, rtol=rtol, atol=atol)
    assert_allclose(LS.gamma, 0, rtol=rtol, atol=atol)
    assert_allclose(LS.AICc, 115.93668384456019, rtol=rtol, atol=atol)
    assert_allclose(LS.BIC, 117.222266038727, rtol=rtol, atol=atol)
    assert_allclose(LS.loglik, -55.61540074580951, rtol=rtol, atol=atol)
    assert_allclose(LS.AD, 55.62807482958476, rtol=rtol, atol=atol)
    assert_allclose(LS.Cov_alpha_beta,
                    -0.1119680481788733,
                    rtol=rtol,
                    atol=atol)
예제 #17
0
def test_Competing_Risks_Model():
    distributions = [Weibull_Distribution(alpha=30, beta=2), Normal_Distribution(mu=35, sigma=5)]
    dist = Competing_Risks_Model(distributions=distributions)
    assert_allclose(dist.mean, 23.707625152181073, rtol=rtol, atol=atol)
    assert_allclose(dist.standard_deviation, 9.832880925543204, rtol=rtol, atol=atol)
    assert_allclose(dist.variance, 96.68554729591138, rtol=rtol, atol=atol)
    assert_allclose(dist.skewness, -0.20597940178753704, rtol=rtol, atol=atol)
    assert_allclose(dist.kurtosis, 2.1824677678598667, rtol=rtol, atol=atol)
    assert dist.name2 == 'Competing risks using 2 distributions'
    assert_allclose(dist.quantile(0.2), 14.170859470541174, rtol=rtol, atol=atol)
    assert_allclose(dist.inverse_SF(q=0.7), 17.908811127053173, rtol=rtol, atol=atol)
    assert_allclose(dist.mean_residual_life(20), 9.862745898092886, rtol=rtol, atol=atol)
    xvals = [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.00210671, 0.00661657, 0.01947571, 0.02655321, 0.00474024, 0.00062978], rtol=rtol, atol=atol)
    assert_allclose(dist.CDF(xvals=xvals, show_plot=False), [0.0010001,  0.00999995, 0.09999943, 0.90000184, 0.99000021, 0.99900003], rtol=rtol, atol=atol)
    assert_allclose(dist.SF(xvals=xvals, show_plot=False), [0.9989999,  0.99000005, 0.90000057, 0.09999816, 0.00999979, 0.00099997], rtol=rtol, atol=atol)
    assert_allclose(dist.HF(xvals=xvals, show_plot=False), [0.00210882, 0.00668341, 0.02163966, 0.265537, 0.47403341, 0.62980068], rtol=rtol, atol=atol)
    assert_allclose(dist.CHF(xvals=xvals, show_plot=False), [1.00059934e-03, 1.00502826e-02, 1.05359884e-01, 2.30260350e+00, 4.60519097e+00, 6.90778668e+00], rtol=rtol, atol=atol)
예제 #18
0
def test_Mixture_Model():
    distributions = [Weibull_Distribution(alpha=30, beta=2), Normal_Distribution(mu=35, sigma=5)]
    dist = Mixture_Model(distributions=distributions, proportions=[0.6,0.4])
    assert_allclose(dist.mean, 29.952084649328917, rtol=rtol, atol=atol)
    assert_allclose(dist.standard_deviation, 11.95293368817564, rtol=rtol, atol=atol)
    assert_allclose(dist.variance, 142.87262375392413, rtol=rtol, atol=atol)
    assert_allclose(dist.skewness, 0.015505959874527537, rtol=rtol, atol=atol)
    assert_allclose(dist.kurtosis, 3.4018343377801674, rtol=rtol, atol=atol)
    assert dist.name2 == 'Mixture using 2 distributions'
    assert_allclose(dist.quantile(0.2), 19.085648329240094, rtol=rtol, atol=atol)
    assert_allclose(dist.inverse_SF(q=0.7), 24.540270766923847, rtol=rtol, atol=atol)
    assert_allclose(dist.mean_residual_life(20), 14.686456940211107, rtol=rtol, atol=atol)
    xvals = [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.0016309, 0.00509925, 0.01423464, 0.01646686, 0.00134902, 0.00016862], rtol=rtol, atol=atol)
    assert_allclose(dist.CDF(xvals=xvals, show_plot=False), [0.00099994, 0.00999996, 0.10000006, 0.90000056, 0.99000001, 0.999], rtol=rtol, atol=atol)
    assert_allclose(dist.SF(xvals=xvals, show_plot=False), [0.99900006, 0.99000004, 0.89999994, 0.09999944, 0.00999999, 0.001], rtol=rtol, atol=atol)
    assert_allclose(dist.HF(xvals=xvals, show_plot=False), [0.00163253, 0.00515076, 0.01581627, 0.16466956, 0.13490177, 0.16861429], rtol=rtol, atol=atol)
    assert_allclose(dist.CHF(xvals=xvals, show_plot=False), [1.00043950e-03, 1.00502998e-02, 1.05360581e-01, 2.30259070e+00, 4.60517090e+00, 6.90775056e+00], rtol=rtol, atol=atol)
예제 #19
0
def test_similar_distributions():
    # ignores the runtime warning from scipy when the nelder-mean or powell optimizers are used and jac is not required
    warnings.filterwarnings(action="ignore", category=RuntimeWarning)
    dist = Weibull_Distribution(alpha=50, beta=3.3)
    results = similar_distributions(distribution=dist,
                                    include_location_shifted=True,
                                    show_plot=False,
                                    print_results=False)
    assert_allclose(results.results[0].alpha,
                    49.22622520639563,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[0].beta,
                    3.2573074120881964,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[0].gamma,
                    0.7236421159037678,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[1].mu,
                    44.847138326837566,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[1].sigma,
                    14.922616862230697,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[2].alpha,
                    5.760746660148767,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[2].beta,
                    7.784952297226461,
                    rtol=rtol_big,
                    atol=atol_big)
    assert_allclose(results.results[2].gamma, 0, rtol=rtol_big, atol=atol_big)
 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 __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()
예제 #22
0
    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()
예제 #23
0
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()
예제 #24
0
def test_Weibull_Distribution():
    dist = Weibull_Distribution(alpha=5, beta=2)
    assert dist.mean == 4.4311346272637895
    assert dist.standard_deviation == 2.316256875880522
    assert dist.variance == 5.365045915063796
예제 #25
0
def test_Weibull_Distribution():
    dist = Weibull_Distribution(alpha=5, beta=2, gamma=10)
    assert_allclose(dist.mean, 14.4311346272637895, rtol=rtol, atol=atol)
    assert_allclose(dist.standard_deviation, 2.316256875880522, rtol=rtol, atol=atol)
    assert_allclose(dist.variance, 5.365045915063796, rtol=rtol, atol=atol)
    assert_allclose(dist.skewness, 0.6311106578189344, rtol=rtol, atol=atol)
    assert_allclose(dist.kurtosis, 3.2450893006876456, rtol=rtol, atol=atol)
    assert dist.param_title_long == 'Weibull Distribution (α=5,β=2,γ=10)'
    assert_allclose(dist.quantile(0.2), 12.361903635387193, rtol=rtol, atol=atol)
    assert_allclose(dist.inverse_SF(q=0.7), 12.9861134604144417, rtol=rtol, atol=atol)
    assert_allclose(dist.mean_residual_life(20), 1.1316926249544481, 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.012639622357755485, 0.03969953988653618, 0.11685342455082046, 0.06069708517540586, 0.008583864105157392, 0.0010513043539513882], 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.012652274632387873, 0.04010054533993554, 0.12983713838980052, 0.6069708517540585, 0.8583864105157389, 1.0513043539513862], rtol=rtol, atol=atol)
    assert_allclose(dist.CHF(xvals=xvals, show_plot=False), [0.0, 0.0010005003335835354, 0.010050335853501409, 0.10536051565782631, 2.3025850929940455, 4.605170185988091, 6.907755278982135], rtol=rtol, atol=atol)
예제 #26
0
    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()
예제 #27
0
def generate_data(output_dir, CPU_factor, GPU_factor, MEM_factor):

	'''
	#generated wit mixture weibull in R
	alpha_mem = 1208
	beta_mem  = 0.871

	alpha_gpu = 59666
	beta_gpu  = 0.934

	alpha_cpu = 289484
	beta_cpu  = 2.778
	'''

	##alpha and beta of real data
	#'''
	alpha_mem = 295647
	beta_mem = 0.918
	
	alpha_gpu = 44721
	beta_gpu = 0.551

	alpha_cpu = 1595690
	beta_cpu = 0.758
	

	#'''
	####################################################################
	#extract real mtbf data
	x_cpu = []
	x_gpu = []
	x_mem = []
	file_name = "../Data/Titan_Data/GPU_mtbf_epoch1.txt"
	with open(file_name) as log:
		for line in log:
				x_gpu.append(int(line))
	
	file_name = "../Data/Titan_Data/CPU_mtbf_epoch1.txt"
	with open(file_name) as log:
		for line in log:
				x_cpu.append(int(line))

	file_name = "../Data/Titan_Data/MEM_mtbf_epoch1.txt"
	with open(file_name) as log:
		for line in log:
			x_mem.append(int(line))

	n_samples_gpu = int(int(len(x_gpu)) * float(GPU_factor))
	n_samples_cpu = int(int(len(x_cpu)) * float(CPU_factor))
	n_samples_mem = int(int(len(x_mem)) * float(MEM_factor))
	
	

	gpu = Weibull_Distribution(alpha=alpha_gpu,beta=beta_gpu).random_samples(n_samples_gpu)
	cpu = Weibull_Distribution(alpha=alpha_cpu,beta=beta_cpu).random_samples(n_samples_cpu)
	mem = Weibull_Distribution(alpha=alpha_mem,beta=beta_mem).random_samples(n_samples_mem)

	shape, loc, scale = ss.weibull_min.fit(gpu, floc=0)
	mean_gpu, var = weibull_min.stats(shape,loc,scale, moments='mv')
	print("gpu total failures = "+ str(n_samples_gpu))
	print("gpu shape = " + str(shape))
	print("gpu scale = " + str(scale))
	print("gpu Mean: "+str(mean_gpu))
	print("-----------------")
	shape, loc, scale = ss.weibull_min.fit(cpu, floc=0)
	mean_cpu, var = weibull_min.stats(shape,loc,scale, moments='mv')
	print("cpu total failures = "+ str(n_samples_cpu))
	print("cpu shape = " + str(shape))
	print("cpu scale = " + str(scale))
	print("cpu Mean: "+str(mean_cpu))
	print("-----------------")
	shape, loc, scale = ss.weibull_min.fit(mem, floc=0)
	mean_mem, var = weibull_min.stats(shape,loc,scale, moments='mv')
	print("mem total failures = "+ str(n_samples_mem))
	print("mem shape = " + str(shape))
	print("mem scale = " + str(scale))
	print("mem Mean: "+str(mean_mem))

	#new proportions
	t = 0
	t = n_samples_mem+n_samples_cpu+n_samples_gpu
	
	proportion_cpu = n_samples_cpu / t
	proportion_gpu = n_samples_gpu / t
	proportion_mem = n_samples_mem / t

	mixture_mean = mean_gpu * proportion_gpu + mean_mem * proportion_mem + mean_cpu * proportion_cpu
	print("-----------------")
	print("Total failures = "+ str(n_samples_mem+n_samples_cpu+n_samples_gpu))
	print("mixture mean without mixture= " + str(mixture_mean/60/60))

	print("-----------------")
	xvals = np.linspace(0,50,t)
	wei_cpu = Weibull_Distribution(alpha=alpha_cpu,beta=beta_cpu).CDF(xvals=xvals,show_plot=False)
	wei_gpu = Weibull_Distribution(alpha=alpha_gpu,beta=beta_gpu).CDF(xvals=xvals,show_plot=False)
	wei_mem = Weibull_Distribution(alpha=alpha_mem,beta=beta_mem).CDF(xvals=xvals,show_plot=False)
	Mixture_CDF = wei_gpu * proportion_gpu + wei_cpu * proportion_cpu + wei_mem * proportion_mem
	shape, loc, scale = ss.weibull_min.fit(Mixture_CDF, floc=0)
	meanw, var = weibull_min.stats(shape,loc,scale, moments='mv')
	print("Mix Mean: "+str(meanw/60/60))


	#save all data (cpu + gpu + mem)
	all_data = np.hstack([gpu,cpu,mem])
	file = open(output_dir + "TOTAL_mtbf_epoch1_exascale.txt", 'w+')
	for i in all_data:
		if int(i) != 0:
			file.write(str(int(i))+"\n")

	file.close()
	sys.exit()