コード例 #1
0
ファイル: compound.py プロジェクト: kelidas/scratch
def run(mu, cov, plot = True):

    # discretize the control variable (x-axis)
    x_arr = np.linspace(0, 10.0, 1000)[1:]

    #===========================================================================
    # Randomization
    #===========================================================================
    s = SPIRRID(q = BasicPDF(),
                e_arr = x_arr,
                n_int = 100,
                tvars = dict(mu = mu,
                             cov = cov
                             ),
                sampling_type = 'TGrid',
                codegen_type = 'numpy',
                )
    #s.codegen.cached_dG = False
    #s.codegen.compiled_eps_loop = False

    #===========================================================================
    # Calculate
    #===========================================================================
    # calculate compounded distribution values
    cpd_pdf = s.mu_q_arr
    cpd_cdf = np.cumsum(cpd_pdf) * np.diff(x_arr)[0]

    # calculate first raw moment
    raw_1 = np.trapz(cpd_pdf * x_arr, x_arr)

    # calculate second raw moment
    raw_2 = np.trapz(cpd_pdf * x_arr ** 2, x_arr)
    std = np.sqrt(raw_2 - raw_1 ** 2)

    # calculate values of lognormal distrib
    lognorm_pdf = lognormal_pdf(x_arr, raw_1, std)
    lognorm_cdf = lognormal_cdf(x_arr, raw_1, std)

    # calculate error
    rms_err = rms_error(lognorm_pdf, cpd_pdf)
    max_err = max_error(lognorm_pdf, cpd_pdf)
    chi_err = chi_error(lognorm_pdf, cpd_pdf)
    ks_err = ks_error(lognorm_cdf, cpd_cdf)

    # generate samples
    samples = s.sampling.get_samples(50)
    sample_args = [ sam[:, np.newaxis] for sam in samples]
    q_arr = s.q(x_arr[None, :], *sample_args)

    emp_data = np.loadtxt('data_emp.txt', delimiter = '\t')

    #===========================================================================
    # Print
    #===========================================================================

    print 'check unity\t', np.trapz(cpd_pdf, x_arr)
    print 'mean\t\t', raw_1
    print 'std\t\t', std
    print 'rms error\t', rms_err
    print 'max error\t', max_err
    print 'chi error\t', chi_err
    print 'ks error\t', ks_err

    #===========================================================================
    # Plot
    #===========================================================================
    if plot:
        p.figure()
        p.title(mu.type)
        # plot samples
        #p.plot(x_arr, q_arr.T, color = 'gray')
        for s in q_arr:
            p.plot(x_arr, s, color = 'gray', linewidth = mu.pdf((s * x_arr).sum() / 100.) * 5)
        # plot compounded distribution
        p.plot(x_arr, cpd_pdf, 'b-', label = 'cpd')
        # plot lognormal distribution, method of moments
        p.plot(x_arr, lognorm_pdf, color = 'red', linewidth = 2, label = 'lognorm')
        p.plot(x_arr, lognormal_pdf(x_arr, np.mean(emp_data[:, 0]), np.std(emp_data[:, 0])), color = 'cyan', linewidth = 2, label = 'lognorm_data')
        p.plot(x_arr, mu.pdf(x_arr), color = 'green', label = 'mu_dist')
        p.legend(loc = 0)

        p.figure()
        p.title(mu.type)
        p.plot(emp_data[:, 0], emp_data[:, 1], 'k-', linewidth = 3, label = 'emp')
        p.plot(x_arr, cpd_cdf, 'b-', label = 'cpd')
        # plot lognormal CDF, method of moments
        p.plot(x_arr, lognorm_cdf, color = 'red', linewidth = 2, label = 'lognorm')
        p.plot(x_arr, lognormal_cdf(x_arr, np.mean(emp_data[:, 0]), np.std(emp_data[:, 0])), color = 'cyan', linewidth = 2, label = 'lognorm_data')
        p.legend(loc = 0)

        p.figure()
        p.title(mu.type)
        p.loglog(emp_data[:, 0], emp_data[:, 1], 'k-', linewidth = 3, label = 'emp')
        p.loglog(x_arr, cpd_cdf, 'b-', label = 'cpd')
        p.loglog(x_arr, lognorm_cdf, color = 'red', linewidth = 2, label = 'lognorm')
        p.loglog(x_arr, lognormal_cdf(x_arr, np.mean(emp_data[:, 0]), np.std(emp_data[:, 0])), color = 'cyan', linewidth = 2, label = 'lognorm_data')
        p.ylim(1e-4, 1)
        p.xlim(1, 10)
        p.legend(loc = 0)

    #p.show()
    return rms_err
コード例 #2
0
ファイル: compound_maple.py プロジェクト: kelidas/scratch
def run():

    m_mu, std_mu = 3., 0.68
    m_cov, std_cov = 0.18, 0.067

    # discretize the control variable (x-axis)
    x_arr = np.linspace(0, 6.0, 1000)[1:]

    #===========================================================================
    # Randomization
    #===========================================================================
    s = SPIRRID(q = BasicPDF(),
                e_arr = x_arr,
                n_int = 100,
                tvars = dict(mu = RV('norm', m_mu, std_mu), #RV('beta', m_cov, std_cov),#BETA('beta', mu = m_mu, sig = std_mu, a = a, b = b),
                             cov = RV('norm', m_cov, std_cov)
                             ),
                sampling_type = 'TGrid',
                codegen_type = 'numpy',
                )

    #===========================================================================
    # Calculate
    #===========================================================================
    # calculate compounded distribution values
    cpd_pdf = s.mu_q_arr
    cpd_cdf = np.cumsum(cpd_pdf) * np.diff(x_arr)[0]

    # calculate first raw moment
    raw_1 = np.trapz(cpd_pdf * x_arr, x_arr)

    # calculate second raw moment
    raw_2 = np.trapz(cpd_pdf * x_arr ** 2, x_arr)
    std = np.sqrt(raw_2 - raw_1 ** 2)

    # calculate values of lognormal distrib
    lognorm_pdf = lognormal_pdf(x_arr, raw_1, std)
    lognorm_cdf = lognormal_cdf(x_arr, raw_1, std)

    # calculate error
    rms_err = rms_error(lognorm_pdf, cpd_pdf)
    max_err = max_error(lognorm_pdf, cpd_pdf)
    chi_err = chi_error(lognorm_pdf, cpd_pdf)

    # generate samples
    samples = s.sampling.get_samples(20)
    sample_args = [ sam[:, np.newaxis] for sam in samples]
    q_arr = s.q(x_arr[None, :], *sample_args)

    #===========================================================================
    # Print
    #===========================================================================

    print 'check unity\t', np.trapz(cpd_pdf, x_arr)
    print 'mean\t\t', raw_1
    print 'std\t\t', std
    print 'rms error\t', rms_err
    print 'max error\t', max_err
    print 'chi error\t', chi_err

    #===========================================================================
    # Plot
    #===========================================================================
    p.figure()
    # plot samples
    #p.plot(x_arr, q_arr.T, color = 'gray')
    # plot compounded distribution
    p.plot(x_arr, cpd_pdf, 'b-')
    # plot lognormal distribution, method of moments
    p.plot(x_arr, lognorm_pdf, color = 'red', linewidth = 2)
    data = np.loadtxt('maple_cpd.txt', delimiter = ',')
    p.plot(data[:, 0], data[:, 1], color = 'violet')

    p.figure()
    p.plot(x_arr, cpd_cdf, 'b-')
    # plot lognormal CDF, method of moments
    p.plot(x_arr, lognorm_cdf, color = 'red', linewidth = 2)

    p.show()