Exemplo n.º 1
0
def interp2d_derivs(d, mu, f, f_prime, bds=1, verbose=True):
    sg = SmolyakGrid(d, mu, -bds, bds)

    f_on_grid = f(sg.grid)
    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, d)
    # Make sure it is bounded by -2, 2
    test_points = (bds - 0.05) * test_points/np.max(np.abs(test_points))

    true_vals = f(test_points)
    true_vals_prime = f_prime(test_points)
    i_vals = si.interpolate(test_points)
    i_vals, i_vals_prime = si.interpolate(test_points, deriv=True)

    mean_ad = np.mean(np.abs(i_vals - true_vals))
    max_ad = np.max(np.abs(i_vals - true_vals))
    min_ad = np.min(np.abs(i_vals - true_vals))

    mean_ad_prime = np.mean(np.abs(i_vals_prime - true_vals_prime))
    max_ad_prime = np.max(np.abs(i_vals_prime - true_vals_prime))
    min_ad_prime = np.min(np.abs(i_vals_prime - true_vals_prime))

    if verbose:
        msg = "mean abs diff is {}\nmax abs diff is {}\nmin abs diff is {}"
        print("Interpolation results\n" + "#" * 21)
        print(msg.format(mean_ad, max_ad, min_ad))

        print("Derivative results\n" + "#" * 18)
        print(msg.format(mean_ad_prime, max_ad_prime, min_ad_prime))
Exemplo n.º 2
0
def interp2d_derivs(d, mu, f, f_prime, bds=1, verbose=True):
    sg = SmolyakGrid(d, mu, -bds, bds)

    f_on_grid = f(sg.grid)
    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, d)
    # Make sure it is bounded by -2, 2
    test_points = (bds - 0.05) * test_points / np.max(np.abs(test_points))

    true_vals = f(test_points)
    true_vals_prime = f_prime(test_points)
    i_vals = si.interpolate(test_points)
    i_vals, i_vals_prime = si.interpolate(test_points, deriv=True)

    mean_ad = np.mean(np.abs(i_vals - true_vals))
    max_ad = np.max(np.abs(i_vals - true_vals))
    min_ad = np.min(np.abs(i_vals - true_vals))

    mean_ad_prime = np.mean(np.abs(i_vals_prime - true_vals_prime))
    max_ad_prime = np.max(np.abs(i_vals_prime - true_vals_prime))
    min_ad_prime = np.min(np.abs(i_vals_prime - true_vals_prime))

    if verbose:
        msg = "mean abs diff is {}\nmax abs diff is {}\nmin abs diff is {}"
        print("Interpolation results\n" + "#" * 21)
        print(msg.format(mean_ad, max_ad, min_ad))

        print("Derivative results\n" + "#" * 18)
        print(msg.format(mean_ad_prime, max_ad_prime, min_ad_prime))
Exemplo n.º 3
0
def interp_2d1(d, mu, f):
    sg = SmolyakGrid(d, mu, np.array([-1, -1.]), np.array([1., 1.]))

    f_on_grid = f(sg.grid)

    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, 2)
    # Make sure it is bounded by -2, 2
    test_points = test_points / np.max(np.abs(test_points))

    true_vals = f(test_points)
    interp_vals = si.interpolate(test_points)

    mean_ad = np.mean(np.abs(interp_vals - true_vals))
    max_ad = np.max(np.abs(interp_vals - true_vals))
    min_ad = np.min(np.abs(interp_vals - true_vals))

    print(msg.format(mean_ad, max_ad, min_ad))
    return
Exemplo n.º 4
0
def interp_2d1(d, mu, f):
    sg = SmolyakGrid(d, mu, np.array([-1, -1.]), np.array([1., 1.]))

    f_on_grid = f(sg.grid)

    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, 2)
    # Make sure it is bounded by -2, 2
    test_points = test_points/np.max(np.abs(test_points))

    true_vals = f(test_points)
    interp_vals = si.interpolate(test_points)

    mean_ad = np.mean(np.abs(interp_vals - true_vals))
    max_ad = np.max(np.abs(interp_vals - true_vals))
    min_ad = np.min(np.abs(interp_vals - true_vals))

    print(msg.format(mean_ad, max_ad, min_ad))
    return
Exemplo n.º 5
0
def interp_2d(d, mu, f):
    lb = -2 * np.ones(d)
    ub = 2 * np.ones(d)
    sg = SmolyakGrid(d, mu, lb, ub)

    f_on_grid = f(sg.grid)

    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, d)
    # Make sure it is bounded by -2, 2
    test_points = 2 * test_points / np.max(np.abs(test_points))

    true_vals = f(test_points)
    interp_vals = si.interpolate(test_points)

    mean_ad = np.mean(np.abs(interp_vals - true_vals))
    max_ad = np.max(np.abs(interp_vals - true_vals))
    min_ad = np.min(np.abs(interp_vals - true_vals))

    print(msg.format(mean_ad, max_ad, min_ad))
    return
Exemplo n.º 6
0
def interp_2d(d, mu, f):
    lb = -2 * np.ones(d)
    ub = 2 * np.ones(d)
    sg = SmolyakGrid(d, mu, lb, ub)

    f_on_grid = f(sg.grid)

    si = SmolyakInterp(sg, f_on_grid)

    np.random.seed(42)
    test_points = np.random.randn(100, d)
    # Make sure it is bounded by -2, 2
    test_points = 2*test_points/np.max(np.abs(test_points))

    true_vals = f(test_points)
    interp_vals = si.interpolate(test_points)

    mean_ad = np.mean(np.abs(interp_vals - true_vals))
    max_ad = np.max(np.abs(interp_vals - true_vals))
    min_ad = np.min(np.abs(interp_vals - true_vals))

    print(msg.format(mean_ad, max_ad, min_ad))
    return
Exemplo n.º 7
0
def test_derivatives():
    d = 5
    N = 100
    mu = 2
    f = lambda x: (x).sum(axis=1)

    import numpy.random

    ub = numpy.random.random(d) + 6
    lb = numpy.random.random(d) - 5
    sg = SmolyakGrid(d, mu, lb=lb, ub=ub)

    values = f(sg.grid)
    si = SmolyakInterp(sg, values)
    gg = numpy.random.random((N, d))

    res, res_s, res_c, res_x = si.interpolate(gg,
                                              deriv=True,
                                              deriv_th=True,
                                              deriv_X=True)

    T = sg.grid.shape[0]

    assert (res.shape == (N, ))
    assert (res_s.shape == (N, d))
    assert (res_c.shape == (N, T))
    assert (res_x.shape == (N, T))

    # res_s should be identically 1
    assert (abs(res_s - 1.0).max() < 1e-8)

    epsilon = 1e-6

    # Test derivatives w.r.t. values

    si2 = SmolyakInterp(sg, values)

    def ff(y):
        x = y.reshape(values.shape)
        si2.update_theta(x)
        return si2.interpolate(gg).ravel()

    y0 = values.ravel()
    r0 = ff(y0)
    jac = numpy.zeros((len(r0), len(y0)))
    for n in range(len(y0)):
        yi = y0.copy()
        yi[n] += epsilon
        jac[:, n] = (ff(yi) - r0) / epsilon
    jac = jac.reshape((N, T))
    assert (abs(jac - res_x).max() < 1e-7)
    # note that accuracy of either numerical or direct computation is not very accurate

    # Test derivatives w.r.t. coefficients

    theta_0 = si.theta.copy()

    def ff_c(y_c):
        si2.theta = y_c.reshape(theta_0.shape)
        return si2.interpolate(gg).ravel()

    r0 = ff_c(theta_0)
    jac = numpy.zeros((len(r0), len(theta_0)))
    for n in range(len(y0)):
        ti = theta_0.copy()
        ti[n] += epsilon
        jac[:, n] = (ff_c(ti) - r0) / epsilon
    jac = jac.reshape((N, T))

    assert (abs(jac - res_c).max() < 1e-7)