return np.zeros(x.shape[0])


def covariance(xx):
    xx = np.atleast_2d(xx)
    dd = xx[:, :1] - xx[:, 1:]
    ll = np.atleast_2d(integral_range)
    return np.exp(- np.sum((dd / ll) ** 2., axis=1))

# Discretization of the random field using Karhunen-Loeve expansion
# from its given theoretical moments
random_field = KarhunenLoeveExpansion(
    mean,
    covariance,
    truncation_order,
    [lower_bound, upper_bound],
    domain_expand_factor=domain_expand_factor,
    verbose=verbose,
    galerkin_scheme=galerkin_scheme,
    legendre_galerkin_order=legendre_galerkin_order,
    legendre_quadrature_order=legendre_quadrature_order)
truncation_order = random_field._truncation_order

# Plot the relative covariance discretization error
res = 50
x1, x2 = np.meshgrid(np.linspace(lower_bound, upper_bound, res),
                     np.linspace(lower_bound, upper_bound, res))
xx = np.vstack([x1.ravel(), x2.ravel()]).T
approximated_covariance = \
    random_field.compute_approximated_covariance(xx)
true_covariance = covariance(xx)
covariance_error = true_covariance - approximated_covariance
    xx = np.atleast_2d(xx)
    y1 = np.vstack([sample_paths[i](xx[:, :2])
                    for i in xrange(n_sample_paths)])
    y2 = np.vstack([sample_paths[i](xx[:, 2:])
                    for i in xrange(n_sample_paths)])
    cov = np.sum((y1 - y1.mean(axis=0)) * (y2 - y2.mean(axis=0)),
                 axis=0) / (n_sample_paths - 1.)
    return cov

# Discretization of the random field using Karhunen-Loeve expansion
# from its estimated theoretical moments
estimated_random_field = KarhunenLoeveExpansion(
    estimated_mean,
    estimated_covariance,
    truncation_order,
    [lower_bound, upper_bound],
    domain_expand_factor=1.,
    verbose=verbose,
    galerkin_scheme=galerkin_scheme,
    legendre_galerkin_order=legendre_galerkin_order,
    legendre_quadrature_order=legendre_quadrature_order)
truncation_order = estimated_random_field._truncation_order

# Plot eigenvalues and eigenfunctions
for i in xrange(truncation_order):
    fig = pl.figure()
    ax = Axes3D(fig)
    pl.title('Eigensolution \#%d ($\lambda_{%d} = %.2f$)'
             % (i, i, estimated_random_field._eigenvalues[i]))
    ax.plot_surface(x1, x2,
                    np.reshape(estimated_random_field._eigenfunctions[i](xx),
                               (res, res)),
Пример #3
0
    y1 = np.vstack(
        [sample_paths[i](xx[:, :2]) for i in xrange(n_sample_paths)])
    y2 = np.vstack(
        [sample_paths[i](xx[:, 2:]) for i in xrange(n_sample_paths)])
    cov = np.sum((y1 - y1.mean(axis=0)) *
                 (y2 - y2.mean(axis=0)), axis=0) / (n_sample_paths - 1.)
    return cov


# Discretization of the random field using Karhunen-Loeve expansion
# from its estimated theoretical moments
estimated_random_field = KarhunenLoeveExpansion(
    estimated_mean,
    estimated_covariance,
    truncation_order, [lower_bound, upper_bound],
    domain_expand_factor=1.,
    verbose=verbose,
    galerkin_scheme=galerkin_scheme,
    legendre_galerkin_order=legendre_galerkin_order,
    legendre_quadrature_order=legendre_quadrature_order)
truncation_order = estimated_random_field._truncation_order

# Plot eigenvalues and eigenfunctions
for i in xrange(truncation_order):
    fig = pl.figure()
    ax = Axes3D(fig)
    pl.title('Eigensolution \#%d ($\lambda_{%d} = %.2f$)' %
             (i, i, estimated_random_field._eigenvalues[i]))
    ax.plot_surface(x1,
                    x2,
                    np.reshape(estimated_random_field._eigenfunctions[i](xx),
Пример #4
0
    return np.zeros(x.shape[0])


def covariance(xx):
    xx = np.atleast_2d(xx)
    dd = np.abs(xx[:, :2] - xx[:, 2:])
    ll = np.atleast_2d(integral_range)
    return np.exp(-np.sum((dd / ll)**2., axis=1))


# Discretization of the random field using Karhunen-Loeve expansion
random_field = KarhunenLoeveExpansion(
    mean,
    covariance,
    truncation_order, [lower_bound, upper_bound],
    domain_expand_factor=domain_expand_factor,
    verbose=verbose,
    galerkin_scheme=galerkin_scheme,
    legendre_galerkin_order=legendre_galerkin_order,
    legendre_quadrature_order=legendre_quadrature_order)
truncation_order = random_field._truncation_order

# Plot the relative variance discretization error
res = 50
x1, x2 = np.meshgrid(np.linspace(lower_bound[0], upper_bound[0], res),
                     np.linspace(lower_bound[1], upper_bound[1], res))
xx = np.vstack([x1.ravel(), x2.ravel()]).T
xxxx = np.hstack([xx, xx])
approximated_variance = random_field.compute_approximated_covariance(xxxx)
variance = covariance(xxxx)
variance_error = variance - approximated_variance