예제 #1
0
def chi_square_test(sample, alpha):
    m = mt.ceil(1 + 3.322 * mt.log(len(sample), 10))
    k = 2
    f = m - 1 - k
    c = chi2.ppf(1 - alpha, f)

    h = (max(sample) - min(sample)) / m

    mean = mou.mean_sample(sample)
    std = mou.std_sample(sample)

    class_ = [min(sample)]
    for i in range(0, m):
        class_.append(class_[i] + h)

    e = []
    for i in range(1, len(class_)):
        if i == 1:
            e.append(npd.phi(class_[i], mean, std) * len(sample))
        elif i == (len(class_)):
            e.append((1 - npd.phi(class_[i - 1], mean, std)) * len(sample))
        else:
            e.append((npd.phi(class_[i], mean, std) -
                      npd.phi(class_[i - 1], mean, std)) * len(sample))

    n = []
    t = 0
    i = 1
    sample = sorted(sample)
    sample.append(max(sample) + 1)

    for j in range(0, len(sample) - 1):
        if sample[j + 1] > class_[i]:
            n.append((j + 1) - t)
            t = j + 1
            i = i + 1

    test_1 = []
    for i in range(0, len(n)):
        test_1.append(((e[i] - n[i])**2) / e[i])

    statistics = sum(test_1)

    if statistics < c:
        print(
            'Chi-Square Statistics = {}\nP-Value = {}\nConsidering that Chi-Square Statistics < P-Value, with a confidence level of {}%, the normal distribution is acceptable.'
            .format(statistics, c, (1 - alpha) * 100))

    elif statistics > c:
        print(
            'Chi-Square Statistics = {}\nP-Value = {}\nConsidering that Chi-Square Statistics > P-Value, with a confidence level of {}%, the normal distribution is not acceptable.'
            .format(statistics, c, (1 - alpha) * 100))
def p_failure_lognorm_single_load(R_mean, S_mean, R_std, S_std):
    beta = single_load_lognorm_beta_value(R_mean, S_mean, R_std, S_std)
    p_f = 1 - npd.phi(beta, 0, 1)

    print('The probability of failure is {}% with beta-value of {}.'.format(p_f*100, beta))

    return p_f, beta
def p_failure_norm_multiple_load(R_mean, S_mean_array, R_std, S_std_array):
    beta = multiple_load_norm_beta(R_mean, S_mean_array, R_std, S_std_array)

    p_f = 1 - npd.phi(beta, 0, 1)
    print('\n\nProbability of failure = {}%\nBeta-value = {}\n\n.'.format(p_f*100, beta))

    return p_f, beta
def afosm_l_ls(R_mean, S_mean, R_std, S_std): #reduced hasofer-lind method for linear limit state equation

    beta_HL = (R_mean - S_mean)/(R_std**2 + S_std**2)**0.5

    R_line = np.arange(-R_mean, R_mean + 2*R_mean/100, 2*R_mean/100)

    def S_line_calc(R_line):
        S_line = (R_std*R_line + R_mean - S_mean)/S_std
        return S_line

    S_line = S_line_calc(R_line)

    r_linha_ast = -(R_std/(R_std**2 + S_std**2)**0.5)*beta_HL
    s_linha_ast = (S_std/(R_std**2 + S_std**2)**0.5)*beta_HL

    plt.plot(R_line, S_line, label = 'Limit State Line', color = 'black')
    plt.scatter(r_linha_ast, s_linha_ast, label = 'Design Point', color = 'red')
    plt.scatter(0, 0, color = 'blue', label = 'Checking Point')
    plt.legend()
    plt.show()

    p_f = 1 - npd.phi(beta_HL, 0, 1)

    print('\nBy using the analytical model for the Hasofer-Lind Method with the linear limit state equation: ')
    print('Reliability index: {}\nProbability of Failure = {}%.\n'.format(beta_HL, p_f*100))
def afosm(g, d_input):
    d = d_input
    R = np.eye(d)
    marginals = np.array(R, ndmin=1)
    d_2 = len(marginals)
    x_init = np.tile([0.1],[d_2,1]) #initial search point

    # objective function from the book
    dist_fun = lambda u: np.linalg.norm(u) 

    # method for minimization
    alg = 'SLSQP'

    H = lambda u: g(u)
    cons = ({'type': 'eq', 'fun': lambda u: -(H(u.reshape(-1,1)))})
    
    #constraints 
    result = sp.optimize.minimize(dist_fun, x0 = x_init, constraints = cons, method=alg)

    #geting main results
    beta_value = result.fun
    iterations = result.nit
    u_ast = result.x

    if d == 2:
        plt.figure()
        xx      = np.linspace(-10, 10, 200)
        [X1,X2] = np.meshgrid(xx,xx)
        xnod    = np.array([X1,X2])
        ZX      = g(xnod)
        plt.pcolor(X1,X2,ZX, cmap = 'RdBu')
        plt.contour(X1,X2,ZX,[0]) 
        plt.scatter(0,0, color = 'dimgray', label = 'Standard space origin')     
        plt.plot([0, u_ast[0]],[0, u_ast[1]],label = 'SLSQP solver')    # reliability index beta
        plt.scatter(u_ast[0], u_ast[1], color = 'black', label = 'Design Point')                       # design point in standard    
        plt.title('Standard space')
        plt.xlabel(r"$X_1^{*}$'")
        plt.ylabel(r"$X_2^{*}$'")
        plt.legend()
        plt.show()


    p_f = 1 - npd.phi(beta_value, 0, 1)
    print('------------------------')
    print('\nSolver:'algo)
    print('Iterations: {}\nReliability index = {}\nProbability of failure = {}%\n\n'.format(iterations, beta_value, p_f*100))
    print('------------------------')

    return p_f, beta_value, u_ast