コード例 #1
0
def test_gaussian_kde_1d_integration():
    """Make sure that 1D distribution integration is correct."""
    dist = chaospy.GaussianKDE([0, 1, 2])
    t = numpy.mgrid[-2.6:4.6:2e5j]
    scale = numpy.ptp(t)
    assert numpy.isclose(numpy.mean(dist.pdf(t) * scale), 1.)
    assert numpy.isclose(numpy.mean(t * dist.pdf(t) * scale), 1.)
コード例 #2
0
def QoI_Dist(poly, dist, sample=10000, **kws):
    """
    Constructs distributions for the quantity of interests.

    The function constructs a kernel density estimator (KDE) for each
    polynomial (poly) by sampling it.  With the KDEs, distributions (Dists) are
    constructed.  The Dists can be used for e.g. plotting probability density
    functions (PDF), or to make a second uncertainty quantification simulation
    with that newly generated Dists.

    Args:
        poly (numpoly.ndpoly):
            Polynomial of interest.
        dist (Distribution):
            Defines the space where the samples for the KDE is taken from the
            poly.
        sample (int):
            Number of samples used in estimation to construct the KDE.

    Returns:
        (Distribution):
            The constructed quantity of interest (QoI) distributions, where
            ``qoi_dists.shape==poly.shape``.

    Examples:
        >>> dist = chaospy.Normal(0, 1)
        >>> x = chaospy.variable(1)
        >>> poly = chaospy.polynomial([x])
        >>> qoi_dist = chaospy.QoI_Dist(poly, dist)
        >>> values = qoi_dist.pdf([-0.75, 0., 0.75])
        >>> values.round(8)
        array([0.29143989, 0.39939823, 0.29531414])

    """
    shape = poly.shape
    poly = poly.flatten()
    dim = len(dist)

    #sample from the input dist
    samples = numpy.atleast_2d(dist.sample(sample, **kws))
    qoi_dist = chaospy.GaussianKDE(poly(*samples))
    return qoi_dist
コード例 #3
0
def test_gaussian_kde_rotation():
    """Make sure rotation does not affect mapping."""
    dist = chaospy.GaussianKDE([[0, 0, 2], [0, 2, 0], [2, 0, 0]],
                               rotation=[0, 1, 2])
    grid = numpy.mgrid[0.01:0.99:2j, 0.01:0.99:2j, 0.01:0.99:2j].reshape(3, 8)
    inverse = dist.inv(grid)
    assert numpy.allclose(dist.fwd(inverse), grid)
    assert numpy.allclose(dist.pdf(inverse), [
        2.550e-05, 2.553e-05, 2.553e-05, 2.552e-05, 2.525e-05, 2.522e-05,
        2.522e-05, 2.519e-05
    ])
    assert numpy.allclose(
        inverse,
        [[
            -1.38424, -1.38424, -1.38424, -1.38424, 3.19971, 3.19971, 3.19971,
            3.19971
        ],
         [
             -1.31003, -1.31003, 3.31003, 3.31003, -1.48391, -1.48391, 1.48429,
             1.48429
         ],
         [
             0.51571, 3.48391, -1.48391, 1.48413, -1.48391, 1.48429, -1.48391,
             1.48429
         ]],
        rtol=1e-5,
    )
    dist = chaospy.GaussianKDE([[0, 0, 2], [0, 2, 0], [2, 0, 0]],
                               rotation=[2, 1, 0])
    inverse = dist.inv(grid)
    assert numpy.allclose(dist.fwd(inverse), grid)
    assert numpy.allclose(dist.pdf(inverse), [
        2.550e-05, 2.525e-05, 2.553e-05, 2.522e-05, 2.553e-05, 2.522e-05,
        2.552e-05, 2.519e-05
    ])
    assert numpy.allclose(
        inverse,
        [[
            0.51571, -1.48391, -1.48391, -1.48391, 3.48391, 1.48429, 1.48413,
            1.48429
        ],
         [
             -1.31003, -1.48391, 3.31003, 1.48429, -1.31003, -1.48391, 3.31003,
             1.48429
         ],
         [
             -1.38424, 3.19971, -1.38424, 3.19971, -1.38424, 3.19971, -1.38424,
             3.19971
         ]],
        rtol=1e-5,
    )
    dist = chaospy.GaussianKDE([[0, 0, 2], [0, 2, 0], [2, 0, 0]],
                               rotation=[0, 2, 1])
    inverse = dist.inv(grid)
    assert numpy.allclose(dist.fwd(inverse), grid)
    assert numpy.allclose(dist.pdf(inverse), [
        2.550e-05, 2.553e-05, 2.553e-05, 2.552e-05, 2.525e-05, 2.522e-05,
        2.522e-05, 2.519e-05
    ])
    assert numpy.allclose(
        inverse,
        [[
            -1.38424, -1.38424, -1.38424, -1.38424, 3.19971, 3.19971, 3.19971,
            3.19971
        ],
         [
             0.51571, -1.48391, 3.48391, 1.48413, -1.48391, -1.48391, 1.48429,
             1.48429
         ],
         [
             -1.31003, 3.31003, -1.31003, 3.31003, -1.48391, 1.48429, -1.48391,
             1.48429
         ]],
        rtol=1e-5,
    )
コード例 #4
0
def test_gaussian_kde_2d_integration():
    """Make sure that 2D distribution integration is correct."""
    dist = chaospy.GaussianKDE([[0, 2], [2, 0]])
    samples = dist.sample(1e4)
    assert numpy.allclose(numpy.mean(samples, axis=-1), 1, rtol=1e-1)
コード例 #5
0
plt.savefig('sobols_second.png')

# plot the total Sobol results
plt.figure()
for k in results.sobols_total()['te'].keys():
    plt.plot(rho, results.sobols_total()['te'][k], label=k)
plt.legend(loc=0)
plt.xlabel('rho [m]')
plt.ylabel('sobols_total')
plt.title(old_campaign.campaign_dir)
plt.savefig('sobols_total.png')

# plot the distributions
plt.figure()
for i, D in enumerate(results.raw_data['output_distributions']['te'].samples):
    pdf_kde_samples = cp.GaussianKDE(D)
    _Te = np.linspace(pdf_kde_samples.lower, pdf_kde_samples.upper[0], 101)
    plt.loglog(_Te, pdf_kde_samples.pdf(_Te), 'b-', alpha=0.25)
    plt.loglog(
        results.describe('te', 'mean')[i],
        pdf_kde_samples.pdf(results.describe('te', 'mean')[i]), 'bo')
    plt.loglog(
        results.describe('te', 'mean')[i] - results.describe('te', 'std')[i],
        pdf_kde_samples.pdf(
            results.describe('te', 'mean')[i] -
            results.describe('te', 'std')[i]), 'b*')
    plt.loglog(
        results.describe('te', 'mean')[i] + results.describe('te', 'std')[i],
        pdf_kde_samples.pdf(
            results.describe('te', 'mean')[i] +
            results.describe('te', 'std')[i]), 'b*')