示例#1
0
def test_varpro_jacobian():
    np.random.seed(1)
    M = 6
    m = 2
    n = 1
    p = 2

    # Samples
    X = np.random.uniform(-1, 1, size=(M, m))

    # Synthetic function
    a = np.random.randn(m)
    b = np.random.randn(m)
    fX = np.dot(a, X.T)**2 + np.dot(b, X.T)**3

    # Random point
    U, _ = np.linalg.qr(np.random.randn(m, n))

    U_flat = U.flatten()

    for basis in ['legendre', 'arnoldi']:
        pra = PolynomialRidgeApproximation(degree=p,
                                           subspace_dimension=n,
                                           scale=False,
                                           basis=basis)
        # This sets the basis
        Y = (U.T @ X.T).T
        #	pra.basis = pra.Basis(pra.degree, Y)
        #pra._varpro_jacobian(X, fX, U)
        res = lambda U: pra._varpro_residual(X, fX, U)
        jac = lambda U: pra._varpro_jacobian(X, fX, U)

        err = check_jacobian(U_flat, res, jac, hvec=[1e-7])

        assert err < 1e-6

    # Check with scaling on
    pra = PolynomialRidgeApproximation(degree=p,
                                       subspace_dimension=n,
                                       scale=True)
    #pra.set_scale(X, U)
    res = lambda U: pra._varpro_residual(X, fX, U)
    jac = lambda U: pra._varpro_jacobian(X, fX, U)

    err = check_jacobian(U_flat, res, jac)
    assert err < 1e-6

    # Check with scaling on for Hermite basis
    pra = PolynomialRidgeApproximation(degree=p,
                                       subspace_dimension=n,
                                       scale=True,
                                       basis='hermite')
    #	pra.set_scale(X, U)
    res = lambda U: pra._varpro_residual(X, fX, U)
    jac = lambda U: pra._varpro_jacobian(X, fX, U)

    err = check_jacobian(U_flat, res, jac)
    assert err < 1e-6
示例#2
0
def test_varpro_jacobian():
    np.random.seed(1)
    M = 100
    m = 10
    n = 2
    p = 5

    # Samples
    X = np.random.uniform(-1, 1, size=(M, m))

    # Synthetic function
    a = np.random.randn(m)
    b = np.random.randn(m)
    fX = np.dot(a, X.T)**2 + np.dot(b, X.T)**3

    # Random point
    U, _ = np.linalg.qr(np.random.randn(m, n))

    U_flat = U.flatten()

    pra = PolynomialRidgeApproximation(degree=p,
                                       subspace_dimension=n,
                                       scale=False)
    res = lambda U: pra._varpro_residual(X, fX, U)
    jac = lambda U: pra._varpro_jacobian(X, fX, U)

    err = check_jacobian(U_flat, res, jac)

    assert err < 1e-6

    # Check with scaling on
    pra = PolynomialRidgeApproximation(degree=p,
                                       subspace_dimension=n,
                                       scale=True)
    pra.set_scale(X, U)
    res = lambda U: pra._varpro_residual(X, fX, U)
    jac = lambda U: pra._varpro_jacobian(X, fX, U)

    err = check_jacobian(U_flat, res, jac)
    assert err < 1e-6

    # Check with scaling on for Hermite basis
    pra = PolynomialRidgeApproximation(degree=p,
                                       subspace_dimension=n,
                                       scale=True,
                                       basis='hermite')
    pra.set_scale(X, U)
    res = lambda U: pra._varpro_residual(X, fX, U)
    jac = lambda U: pra._varpro_jacobian(X, fX, U)

    err = check_jacobian(U_flat, res, jac)
    assert err < 1e-6