コード例 #1
0
def test_optimal_tilt():

    A = np.vstack(-np.identity(4))
    b = -np.array([1, 2, 3, 4.])
    con = AC.constraints(A,
                         b,
                         covariance=2 * np.identity(4),
                         mean=np.array([3, -4.3, -2.2, 1.2]))
    eta = np.array([1, 0, 0, 0.])

    tilt = optimal_tilt(con, eta)
    print tilt.smooth_objective(np.zeros(5), mode='both')
    opt_tilt = tilt.fit(max_its=20)
    print con.mean + opt_tilt

    A = np.vstack([-np.identity(4), np.identity(4)])
    b = np.array([-1, -2, -3, -4., 3, 4, 5, 11])
    con = AC.constraints(A,
                         b,
                         covariance=2 * np.identity(4),
                         mean=np.array([3, -4.3, 12.2, 20.2]))
    eta = np.array([1, 0, 0, 0.])

    tilt = optimal_tilt(con, eta)
    print tilt.smooth_objective(np.zeros(5), mode='both')
    opt_tilt = tilt.fit(max_its=20)
    print con.mean + opt_tilt
コード例 #2
0
def test_stack():

    A, b = np.random.standard_normal((4,30)), np.random.standard_normal(4)

    con1 = AC.constraints(A,b)

    A, b = np.random.standard_normal((5,30)), np.random.standard_normal(5)
    E, f = np.random.standard_normal((3,30)), np.random.standard_normal(3)

    con2 = AC.constraints(A,b)

    return AC.stack(con1, con2)
コード例 #3
0
def test_stack():

    A, b = np.random.standard_normal((4, 30)), np.random.standard_normal(4)

    con1 = AC.constraints(A, b)

    A, b = np.random.standard_normal((5, 30)), np.random.standard_normal(5)
    E, f = np.random.standard_normal((3, 30)), np.random.standard_normal(3)

    con2 = AC.constraints(A, b)

    return AC.stack(con1, con2)
コード例 #4
0
def test_sample_sphere():

    p = 10
    A = np.identity(10)[:3]
    b = 2 * np.ones(3)
    mean = -np.ones(p)
    noise = np.random.standard_normal(p) * 0.1
    noise[-3:] = 0.
    initial = noise + mean
    eta = np.ones(p)

    bound = 5
    s1 = AC.sample_truncnorm_white_sphere(
        A,
        b,
        initial,
        eta,
        lambda state: bound + 0.01 * np.random.sample() * np.linalg.norm(state
                                                                         )**2,
        burnin=1000,
        ndraw=1000,
        how_often=5)

    con = AC.constraints(A, b)
    con.covariance = np.diag([1] * 7 + [0] * 3)
    con.mean[:] = mean
    print con(initial)
    s2 = AC.sample_from_sphere(con, initial)
    return s1, s2
コード例 #5
0
def test_chisq_central(nsim=None, burnin=8000, ndraw=2000):

    n, p = 4, 10
    A, b = np.random.standard_normal((n, p)), np.zeros(n)
    con = AC.constraints(A,b)

    while True:
        z = np.random.standard_normal(p)
        if con(z):
            break

    S = np.identity(p)[:3]
    Z = AC.sample_from_constraints(con, z, ndraw=ndraw, burnin=burnin)
    P = []
    for i in range(Z.shape[0]/10):
        P.append(chisq.quadratic_test(Z[10*i], S, con))

#     no plots in the test!
#     ecdf = sm.distributions.ECDF(P)

#     plt.clf()
#     x = np.linspace(0,1,101)
#     plt.plot(x, ecdf(x), c='red')
#     plt.plot([0,1],[0,1], c='blue', linewidth=2)

    nt.assert_true(np.fabs(np.mean(P)-0.5) < 0.03)
    nt.assert_true(np.fabs(np.std(P)-1/np.sqrt(12)) < 0.03)
コード例 #6
0
def test_sample_sphere():

    p = 10
    A = np.identity(10)[:3]
    b = 2 * np.ones(3)
    mean = -np.ones(p)
    noise = np.random.standard_normal(p) * 0.1
    noise[-3:] = 0.
    initial = noise + mean
    eta = np.ones(p)

    bound = 5
    s1 = AC.sample_truncnorm_white_sphere(A,
                                         b, 
                                         initial,
                                         eta,
                                         lambda state: bound + 0.01 * np.random.sample() * np.linalg.norm(state)**2,
                                         burnin=1000,
                                         ndraw=1000,
                                         how_often=5)

    con = AC.constraints(A, b)
    con.covariance = np.diag([1]*7 + [0]*3)
    con.mean[:] = mean
    print con(initial)
    s2 = AC.sample_from_sphere(con, initial)
    return s1, s2
コード例 #7
0
def test_chisq_central(nsim=None, burnin=8000, ndraw=2000):

    n, p = 4, 10
    A, b = np.random.standard_normal((n, p)), np.zeros(n)
    con = AC.constraints(A, b)

    while True:
        z = np.random.standard_normal(p)
        if con(z):
            break

    S = np.identity(p)[:3]
    Z = AC.sample_from_constraints(con, z, ndraw=ndraw, burnin=burnin)
    P = []
    for i in range(Z.shape[0] / 10):
        P.append(chisq.quadratic_test(Z[10 * i], S, con))


#     no plots in the test!
#     ecdf = sm.distributions.ECDF(P)

#     plt.clf()
#     x = np.linspace(0,1,101)
#     plt.plot(x, ecdf(x), c='red')
#     plt.plot([0,1],[0,1], c='blue', linewidth=2)

    nt.assert_true(np.fabs(np.mean(P) - 0.5) < 0.03)
    nt.assert_true(np.fabs(np.std(P) - 1 / np.sqrt(12)) < 0.03)
コード例 #8
0
def test_sample_sphere(burnin=1000,
                       ndraw=1000,
                       nsim=None):

    p = 10
    A = np.identity(10)[:3]
    b = 2 * np.ones(3)
    mean = -np.ones(p)
    noise = np.random.standard_normal(p) * 0.1
    noise[-3:] = 0.
    initial = noise + mean
    eta = np.ones(p)

    bound = 5
    s1 = AC.sample_truncnorm_white_sphere(A,
                                          b, 
                                          initial,
                                          eta,
                                          how_often=20,
                                          burnin=burnin,
                                          ndraw=ndraw)

    con = AC.constraints(A, b)
    con.covariance = np.diag([1]*7 + [0]*3)
    con.mean[:] = mean
    s2 = AC.sample_from_sphere(con, initial, ndraw=ndraw, burnin=burnin)
    return s1, s2
コード例 #9
0
def test_sample_sphere(burnin=1000, ndraw=1000):

    p = 10
    A = np.identity(10)[:3]
    b = 2 * np.ones(3)
    mean = -np.ones(p)
    noise = np.random.standard_normal(p) * 0.1
    noise[-3:] = 0.
    initial = noise + mean
    eta = np.ones(p)

    bound = 5
    s1 = AC.sample_truncnorm_white_sphere(A,
                                          b,
                                          initial,
                                          eta,
                                          how_often=20,
                                          burnin=burnin,
                                          ndraw=ndraw)

    con = AC.constraints(A, b)
    con.covariance = np.diag([1] * 7 + [0] * 3)
    con.mean[:] = mean
    s2 = AC.sample_from_sphere(con, initial, ndraw=ndraw, burnin=burnin)
    return s1, s2
コード例 #10
0
def test_gaussian_chain():

    n = 30

    A = np.eye(n)[:3]
    b = np.ones(A.shape[0])

    con = constraints(A, b)
    state = np.random.standard_normal(n)
    state[:3] = 0

    gaussian_chain = gaussian_hit_and_run(con, state, nstep=100)

    counter = 0
    for step in gaussian_chain:
        counter += 1

        if counter >= 100:
            break

    test_statistic = lambda z: np.sum(z)

    parallel = parallel_test(gaussian_chain,
                             gaussian_chain.state,
                             test_statistic,
                             ndraw=20)

    serial = serial_test(gaussian_chain,
                         gaussian_chain.state,
                         test_statistic,
                         ndraw=20)

    return parallel, serial
コード例 #11
0
def test_conditional_simple():

    A = np.ones((1, 2))
    b = np.array([1])
    con = AC.constraints(A, b)  #X1+X2<= 1

    C = np.array([[0, 1]])
    d = np.array([2])  #X2=2

    new_con = con.conditional(C, d)
    while True:
        W = np.random.standard_normal(2)
        W -= np.dot(np.linalg.pinv(C), np.dot(C, W) - d)
        if con(W):
            break
    Z1 = AC.sample_from_constraints(new_con, W, ndraw=10000)

    counter = 0
    new_sample = []
    while True:
        W = np.random.standard_normal()  # conditional distribution
        if W < -1:
            new_sample.append(W)
            counter += 1

        if counter >= 10000:
            break

    a1 = Z1[:, 0]
    a2 = np.array(new_sample)
    test = np.fabs(
        (a1.mean() - a2.mean()) / (np.std(a1) * np.sqrt(2)) * np.sqrt(10000))
    nt.assert_true(test < 5)
コード例 #12
0
def test_conditional():

    p = 200
    k1, k2 = 5, 3
    b = np.random.standard_normal((k1, ))
    A = np.random.standard_normal((k1, p))
    con = AC.constraints(A, b)
    w = np.random.standard_normal(p)
    con.mean = w
    C = np.random.standard_normal((k2, p))
    d = np.random.standard_normal(k2)
    new_con = con.conditional(C, d)

    while True:
        W = np.random.standard_normal(p)
        W -= np.dot(np.linalg.pinv(C), np.dot(C, W) - d)
        if new_con(W) and con(W):
            break

    Z = AC.sample_from_constraints(new_con, W, ndraw=5000)

    tol = 0

    nt.assert_true(np.linalg.norm(np.dot(Z, C.T) - d[None, :]) < 1.e-7)

    V = (np.dot(Z, new_con.linear_part.T) - new_con.offset[None, :]).max(1)
    V2 = (np.dot(Z, con.linear_part.T) - con.offset[None, :]).max(1)
    print('failing:', (V > tol).sum(), (V2 > tol).sum(),
          np.linalg.norm(np.dot(C, W) - d))
    nt.assert_true(np.sum(V > tol) < 0.001 * V.shape[0])
コード例 #13
0
def test_gaussian_chain():

    n = 30

    A = np.eye(n)[:3]
    b = np.ones(A.shape[0])

    con = constraints(A, b)
    state = np.random.standard_normal(n)
    state[:3] = 0

    gaussian_chain = gaussian_hit_and_run(con, state, nstep=100)

    counter = 0
    for step in gaussian_chain:
        counter += 1
        
        if counter >= 100:
            break

    test_statistic = lambda z: np.sum(z)

    parallel = parallel_test(gaussian_chain, 
                             gaussian_chain.state,
                             test_statistic,
                             ndraw=20)

    serial = serial_test(gaussian_chain, 
                         gaussian_chain.state,
                         test_statistic,
                         ndraw=20)

    return parallel, serial
コード例 #14
0
def test_conditional():

    p = 200
    k1, k2 = 5, 3
    b = np.random.standard_normal((k1,))
    A = np.random.standard_normal((k1,p))
    con = AC.constraints(A,b)
    w = np.random.standard_normal(p)
    con.mean = w
    C = np.random.standard_normal((k2,p))
    d = np.random.standard_normal(k2)
    new_con = con.conditional(C, d)

    while True:
        W = np.random.standard_normal(p)
        W -= np.dot(np.linalg.pinv(C), np.dot(C, W) - d)  
        if new_con(W) and con(W):
            break

    Z = AC.sample_from_constraints(new_con, W, ndraw=5000)

    tol = 0
    
    nt.assert_true(np.linalg.norm(np.dot(Z, C.T) - d[None,:]) < 1.e-7)

    V = (np.dot(Z, new_con.linear_part.T) - new_con.offset[None,:]).max(1)
    V2 = (np.dot(Z, con.linear_part.T) - con.offset[None,:]).max(1)
    print ('failing:', 
           (V>tol).sum(), 
           (V2>tol).sum(), 
           np.linalg.norm(np.dot(C, W) - d))
    nt.assert_true(np.sum(V > tol) < 0.001*V.shape[0])
コード例 #15
0
def test_softmax_sigma_not1():

    sigma = 2
    A = np.array([[-1., 0.]])
    b = np.array([-1.])

    con = constraints(A, b, covariance=sigma**2 * np.identity(2))
    observed = np.array([1.4, 2.3])

    simple_estimator = con.estimate_mean(observed)
    softmax_loss = softmax(con, sigma=sigma)
    est2 = softmax_loss.smooth_objective(observed, 'grad') * sigma**2
    np.testing.assert_allclose(est2, simple_estimator)

    loss = softmax_conjugate(con, observed, sigma=sigma)

    loss.coefs[:] = 1.
    f, coefs = loss._solve_conjugate_problem(simple_estimator / sigma**2,
                                             niter=2000)

    np.testing.assert_allclose(coefs, observed)

    np.testing.assert_allclose(
        loss.smooth_objective(simple_estimator / sigma**2, 'grad'), observed)

    print loss.smooth_objective(coefs, 'both')[0]
    print est2, simple_estimator

    G1 = softmax_loss.smooth_objective(observed, 'grad')
    np.testing.assert_allclose(loss.smooth_objective(G1, 'grad'), observed)

    G2 = loss.smooth_objective(G1, 'grad')
    np.testing.assert_allclose(softmax_loss.smooth_objective(G2, 'grad'), G1)
コード例 #16
0
def test_conditional_simple():

    A = np.ones((1,2))
    b = np.array([1])
    con = AC.constraints(A,b) #X1+X2<= 1

    C = np.array([[0,1]])
    d = np.array([2])   #X2=2

    new_con = con.conditional(C,d)
    while True:
        W = np.random.standard_normal(2)
        W -= np.dot(np.linalg.pinv(C), np.dot(C, W) - d)  
        if con(W):
            break
    Z1 = AC.sample_from_constraints(new_con, W, ndraw=10000)

    counter = 0
    new_sample = []
    while True:
        W = np.random.standard_normal() # conditional distribution
        if W < -1:
            new_sample.append(W)
            counter += 1

        if counter >= 10000:
            break

    a1 = Z1[:,0]
    a2 = np.array(new_sample)
    test = np.fabs((a1.mean() - a2.mean()) / (np.std(a1) * np.sqrt(2)) * np.sqrt(10000))
    nt.assert_true(test < 5)
コード例 #17
0
def test_sequentially_constrained():
    S = -np.identity(10)[:3]
    b = -6 * np.ones(3)
    C = constraints(S, b)
    W = sample(C, 5000, temps=np.linspace(0, 200, 1001))
    U = np.linspace(0, 1, 101)
    D = sm.distributions.ECDF((ndist.cdf(W[0]) - ndist.cdf(6)) / ndist.sf(6))
    plt.plot(U, D(U))
コード例 #18
0
def test_sequentially_constrained():
    S = -np.identity(10)[:3]
    b = -6 * np.ones(3)
    C = constraints(S, b)
    W = sample(C, 5000, temps=np.linspace(0, 200, 1001))
    U = np.linspace(0, 1, 101)
    D = sm.distributions.ECDF((ndist.cdf(W[0]) - ndist.cdf(6)) / ndist.sf(6))
    plt.plot(U, D(U))
コード例 #19
0
def test_sampling():
    """
    See that means and covariances are approximately correct
    """
    C = AC.constraints(np.identity(3), np.inf*np.ones(3))
    C.mean = np.array([3,4,5.2])
    W = np.random.standard_normal((5,3))
    S = np.dot(W.T, W) / 30.
    C.covariance = S
    V = AC.sample_from_constraints(C, np.zeros(3), ndraw=500000)

    nt.assert_true(np.linalg.norm(V.mean(0)-C.mean) < 0.01)
    nt.assert_true(np.linalg.norm(np.einsum('ij,ik->ijk', V, V).mean(0) - 
                                  np.outer(V.mean(0), V.mean(0)) - S) < 0.01)
コード例 #20
0
def test_sampling():
    """
    See that means and covariances are approximately correct
    """
    C = AC.constraints(np.identity(3), np.inf*np.ones(3))
    C.mean = np.array([3,4,5.2])
    W = np.random.standard_normal((5,3))
    S = np.dot(W.T, W) / 30.
    C.covariance = S
    V = AC.sample_from_constraints(C, np.zeros(3), ndraw=500000)

    nt.assert_true(np.linalg.norm(V.mean(0)-C.mean) < 0.01)
    nt.assert_true(np.linalg.norm(np.einsum('ij,ik->ijk', V, V).mean(0) - 
                                  np.outer(V.mean(0), V.mean(0)) - S) < 0.01)
コード例 #21
0
def test_optimal_tilt():

    A = np.vstack(-np.identity(4))
    b = -np.array([1,2,3,4.])
    con = AC.constraints(A, b, covariance=2 * np.identity(4),
                     mean=np.array([3,-4.3,-2.2,1.2]))
    eta = np.array([1,0,0,0.])

    tilt = optimal_tilt(con, eta)
    print tilt.smooth_objective(np.zeros(5), mode='both')
    opt_tilt = tilt.fit(max_its=20)
    print con.mean + opt_tilt

    A = np.vstack([-np.identity(4),
                    np.identity(4)])
    b = np.array([-1,-2,-3,-4.,3,4,5,11])
    con = AC.constraints(A, b, covariance=2 * np.identity(4),
                     mean=np.array([3,-4.3,12.2,20.2]))
    eta = np.array([1,0,0,0.])

    tilt = optimal_tilt(con, eta)
    print tilt.smooth_objective(np.zeros(5), mode='both')
    opt_tilt = tilt.fit(max_its=20)
    print con.mean + opt_tilt
コード例 #22
0
def test_simulate_nonwhitened():
    n, p = 50, 200

    X = np.random.standard_normal((n,p))
    cov = np.dot(X.T, X)

    W = np.random.standard_normal((3,p))
    con = AC.constraints(W, np.ones(3), covariance=cov)

    while True:
        z = np.random.standard_normal(p)
        if np.dot(W, z).max() <= 1:
            break

    Z = AC.sample_from_constraints(con, z)
    nt.assert_true((np.dot(Z, W.T) - 1).max() < 0)
コード例 #23
0
def test_simulate_nonwhitened():
    n, p = 50, 200

    X = np.random.standard_normal((n, p))
    cov = np.dot(X.T, X)

    W = np.random.standard_normal((3, p))
    con = AC.constraints(W, 3 * np.ones(3), covariance=cov)

    while True:
        z = np.random.standard_normal(p)
        if np.dot(W, z).max() <= 3:
            break

    Z = AC.sample_from_constraints(con, z, burnin=100, ndraw=100)

    nt.assert_true((np.dot(Z, W.T) - 3).max() < 1.e-5)
コード例 #24
0
    def full_sim(L, b, p):
        k, q = L.shape
        A1 = np.random.standard_normal((p, q))
        A2 = L[:p]
        A3 = np.array([np.arange(q)**(i / 2.) for i in range(1, 4)])

        con = AC.constraints(L, b)

        def sim(A):

            while True:
                y = np.random.standard_normal(L.shape[1])
                if con(y):
                    break
            return chisq.quadratic_test(y, np.identity(con.dim), con)

        return sim(A1), sim(A2), sim(A3)
コード例 #25
0
def test_chisq_noncentral(nsim=1000, burnin=2000, ndraw=8000):

    mu = np.arange(6)
    ncp = np.linalg.norm(mu[:3])**2

    A, b = np.random.standard_normal((4, 6)), np.zeros(4)
    con = AC.constraints(A, b, mean=mu)

    ro.numpy2ri.activate()
    ro.r('fncp=%f' % ncp)
    ro.r('f = function(x) {pchisq(x,3,ncp=fncp)}')

    def F(x):
        if x != np.inf:
            return np.array(ro.r('f(%f)' % x))
        else:
            return np.array([1.])

    # find a feasible point

    while True:
        z = np.random.standard_normal(mu.shape)
        if con(z):
            break

    P = []
    for i in range(nsim):
        Z = AC.sample_from_constraints(con, z, ndraw=ndraw, burnin=burnin)
        u = Z[-1]
        u[:3] = u[:3] / np.linalg.norm(u[:3])
        L, V, U = con.bounds(u, Z[-1])[:3]
        if L > 0:
            Ln = L**2
            Un = U**2
            Vn = V**2
        else:
            Ln = 0
            Un = U**2
            Vn = V**2

        P.append(np.array((F(Un) - F(Vn)) / (F(Un) - F(Ln))))

    P = np.array(P).reshape(-1)
    P = P[P > 0]
    P = P[P < 1]
    ro.numpy2ri.deactivate()
コード例 #26
0
def test_regreg_transform():

    A, b = np.random.standard_normal((4, 30)), np.random.standard_normal(4)
    A = rr.astransform(A)
    con = AC.constraints(A, b, covariance=np.identity(30))

    while True:
        Z = np.random.standard_normal(30)  # conditional distribution
        if con.value(Z) < 0:
            break

    C = np.random.standard_normal((2, 30))
    conditional = con.conditional(C, C.dot(Z))
    W = np.random.standard_normal(30)

    print(conditional.pivot(W, Z))
    print(con.pivot(W, Z))
コード例 #27
0
    def full_sim(L, b, p):
        k, q = L.shape
        A1 = np.random.standard_normal((p,q))
        A2 = L[:p]
        A3 = np.array([np.arange(q)**(i/2.) for i in range(1,4)])

        con = AC.constraints(L, b)
        
        def sim(A):

            while True:
                y = np.random.standard_normal(L.shape[1])
                if con(y):
                    break
            return chisq.quadratic_test(y, np.identity(con.dim),
                                        con)

        return sim(A1), sim(A2), sim(A3)
コード例 #28
0
def test_softmax():

    A = np.array([[-1.,0.]])
    b = np.array([-1.])

    con = constraints(A, b)
    observed = np.array([1.4,2.3])

    simple_estimator = con.estimate_mean(observed)
    f, coefs = _solve_softmax_problem(simple_estimator, con, observed)

    np.testing.assert_allclose(coefs, observed)

    loss = softmax_conjugate(con,
                             observed)
    np.testing.assert_allclose(loss.smooth_objective(simple_estimator, 'grad'), observed)

    loss.smooth_objective(coefs, 'both')
コード例 #29
0
def test_chisq_noncentral(nsim=1000, burnin=2000, ndraw=8000):

    mu = np.arange(6)
    ncp = np.linalg.norm(mu[:3])**2

    A, b = np.random.standard_normal((4,6)), np.zeros(4)
    con = AC.constraints(A,b, mean=mu)

    ro.r('fncp=%f' % ncp)
    ro.r('f = function(x) {pchisq(x,3,ncp=fncp)}')
    def F(x):
        if x != np.inf:
            return np.array(ro.r('f(%f)' % x))
        else:
            return np.array([1.])

    # find a feasible point

    while True:
        z = np.random.standard_normal(mu.shape)
        if con(z):
            break

    P = []
    for i in range(nsim):
        Z = AC.sample_from_constraints(con, z, ndraw=ndraw, burnin=burnin)
        u = Z[-1]
        u[:3] = u[:3] / np.linalg.norm(u[:3])
        L, V, U = con.bounds(u, Z[-1])[:3]
        if L > 0:
            Ln = L**2
            Un = U**2
            Vn = V**2
        else:
            Ln = 0
            Un = U**2
            Vn = V**2

        P.append(np.array((F(Un) - F(Vn)) / (F(Un) - F(Ln))))

    P = np.array(P).reshape(-1)
    P = P[P > 0]
    P = P[P < 1]
コード例 #30
0
def test_chisq_central(nsim=None, burnin=8000, ndraw=2000):

    n, p = 4, 10
    A, b = np.random.standard_normal((n, p)), np.zeros(n)
    con = AC.constraints(A, b)

    while True:
        z = np.random.standard_normal(p)
        if con(z):
            break

    S = np.identity(p)[:3]
    Z = AC.sample_from_constraints(con, z, ndraw=ndraw, burnin=burnin)
    P = []
    for i in range(int(Z.shape[0] / 10)):
        P.append(chisq.quadratic_test(Z[10 * i], S, con))

    nt.assert_true(np.fabs(np.mean(P) - 0.5) < 0.03)
    nt.assert_true(np.fabs(np.std(P) - 1 / np.sqrt(12)) < 0.03)
コード例 #31
0
def test_conditional_sampling(n=20, p=25, sigma=20,
                              ndraw=1000,
                              burnin=1000,
                              nsim=None):
    """
    goodness of fit samples from
    inactive constraints intersect a sphere

    this test verifies the sampler is doing what it should
    """

    con, y, L, X = _generate_constraints(n=n, p=p, sigma=sigma)

    X_E = X[:,L.active]
    C_Ei = np.linalg.pinv(X_E.T.dot(X_E))
    R_E = lambda z: z - X_E.dot(C_Ei.dot(X_E.T.dot(z)))

    X_minus_E = X[:,L.inactive]
    RX_minus_E = R_E(X_minus_E)
    inactive_bound = L.feature_weights[L.inactive]
    active_subgrad = L.feature_weights[L.active] * L.active_signs
    irrep_term = X_minus_E.T.dot(X_E.dot(C_Ei.dot(active_subgrad)))

    inactive_constraints = AC.constraints(
                             np.vstack([RX_minus_E.T,
                                        -RX_minus_E.T]),
                             np.hstack([inactive_bound - irrep_term,
                                        inactive_bound + irrep_term]),
                             covariance = np.identity(n)) 

    con = inactive_constraints
    conditional_con = con.conditional(X_E.T, np.dot(X_E.T, y))

    Z, W = AC.sample_from_sphere(conditional_con, 
                                 y,
                                 ndraw=ndraw,
                                 burnin=burnin)  
    
    T1 = np.dot(X_E.T, Z.T) - np.dot(X_E.T, y)[:,None]
    nt.assert_true(np.linalg.norm(T1) < 1.e-7)

    T2 = (R_E(Z.T)**2).sum(0) - np.linalg.norm(R_E(y))**2
    nt.assert_true(np.linalg.norm(T2) < 1.e-7)
コード例 #32
0
def test_conditional_sampling(n=20,
                              p=25,
                              sigma=20,
                              ndraw=1000,
                              burnin=1000,
                              nsim=None):
    """
    goodness of fit samples from
    inactive constraints intersect a sphere

    this test verifies the sampler is doing what it should
    """

    con, y, L, X = _generate_constraints(n=n, p=p, sigma=sigma)

    X_E = X[:, L.active]
    C_Ei = np.linalg.pinv(X_E.T.dot(X_E))
    R_E = lambda z: z - X_E.dot(C_Ei.dot(X_E.T.dot(z)))

    X_minus_E = X[:, L.inactive]
    RX_minus_E = R_E(X_minus_E)
    inactive_bound = L.feature_weights[L.inactive]
    active_subgrad = L.feature_weights[L.active] * L.active_signs
    irrep_term = X_minus_E.T.dot(X_E.dot(C_Ei.dot(active_subgrad)))

    inactive_constraints = AC.constraints(
        np.vstack([RX_minus_E.T, -RX_minus_E.T]),
        np.hstack([inactive_bound - irrep_term, inactive_bound + irrep_term]),
        covariance=np.identity(n))

    con = inactive_constraints
    conditional_con = con.conditional(X_E.T, np.dot(X_E.T, y))

    Z, W = AC.sample_from_sphere(conditional_con,
                                 y,
                                 ndraw=ndraw,
                                 burnin=burnin)

    T1 = np.dot(X_E.T, Z.T) - np.dot(X_E.T, y)[:, None]
    nt.assert_true(np.linalg.norm(T1) < 1.e-7)

    T2 = (R_E(Z.T)**2).sum(0) - np.linalg.norm(R_E(y))**2
    nt.assert_true(np.linalg.norm(T2) < 1.e-7)
コード例 #33
0
def test_pivots_intervals():

    A, b = np.random.standard_normal((4,30)), np.random.standard_normal(4)

    con = AC.constraints(A,b)
    while True:
        w = np.random.standard_normal(30)
        if con(w):
            break

    Z = AC.sample_from_constraints(con, w)[-1]
    u = np.zeros(con.dim)
    u[4] = 1

    # call pivot
    con.pivot(u, Z)
    con.pivot(u, Z, alternative='less')
    con.pivot(u, Z, alternative='greater')

    con.interval(u, Z, UMAU=True)
    con.interval(u, Z, UMAU=False)
コード例 #34
0
def _generate_constraints(n=15, p=10, sigma=1):
    while True:
        y = np.random.standard_normal(n) * sigma
        beta = np.zeros(p)
        X = np.random.standard_normal((n,p)) + 0.3 * np.random.standard_normal(n)[:,None]
        X /= (X.std(0)[None,:] * np.sqrt(n))
        y += np.dot(X, beta) * sigma
        lam_theor = 0.3 * choose_lambda(X, quantile=0.9)
        L = lasso.sqrt_lasso(X, y, lam_theor)
        L.fit(solve_args={'tol':1.e-12, 'min_its':150})

        con = L.constraints
        if con is not None and L.active.shape[0] >= 3:
            break

    offset = con.offset
    linear_part = -L.active_signs[:,None] * np.linalg.pinv(X[:,L.active])
    con = AC.constraints(linear_part, offset)
    con.covariance = np.identity(con.covariance.shape[0])
    con.mean *= 0
    return con, y, L, X
コード例 #35
0
def test_pivots_intervals():

    A, b = np.random.standard_normal((4, 30)), np.random.standard_normal(4)

    con = AC.constraints(A, b)
    while True:
        w = np.random.standard_normal(30)
        if con(w):
            break

    Z = AC.sample_from_constraints(con, w)[-1]
    u = np.zeros(con.dim)
    u[4] = 1

    # call pivot
    con.pivot(u, Z)
    con.pivot(u, Z, alternative='less')
    con.pivot(u, Z, alternative='greater')

    con.interval(u, Z, UMAU=True)
    con.interval(u, Z, UMAU=False)
コード例 #36
0
def _generate_constraints(n=15, p=10, sigma=1):
    while True:
        y = np.random.standard_normal(n) * sigma
        beta = np.zeros(p)
        X = np.random.standard_normal(
            (n, p)) + 0.3 * np.random.standard_normal(n)[:, None]
        X /= (X.std(0)[None, :] * np.sqrt(n))
        y += np.dot(X, beta) * sigma
        lam_theor = 0.3 * choose_lambda(X, quantile=0.9)
        L = lasso.sqrt_lasso(X, y, lam_theor)
        L.fit(solve_args={'tol': 1.e-12, 'min_its': 150})

        con = L.constraints
        if con is not None and L.active.shape[0] >= 3:
            break

    offset = con.offset
    linear_part = -L.active_signs[:, None] * np.linalg.pinv(X[:, L.active])
    con = AC.constraints(linear_part, offset)
    con.covariance = np.identity(con.covariance.shape[0])
    con.mean *= 0
    return con, y, L, X
コード例 #37
0
def test_softmax():

    A = np.array([[-1., 0.]])
    b = np.array([-1.])

    con = constraints(A, b)
    observed = np.array([1.4, 2.3])

    simple_estimator = con.estimate_mean(observed)
    softmax_loss = softmax(con)
    est2 = softmax_loss.smooth_objective(observed, 'grad')
    np.testing.assert_allclose(est2, simple_estimator)

    loss = softmax_conjugate(con, observed)

    f, coefs = loss._solve_conjugate_problem(simple_estimator)

    np.testing.assert_allclose(coefs, observed)

    np.testing.assert_allclose(loss.smooth_objective(simple_estimator, 'grad'),
                               observed)
    loss.smooth_objective(coefs, 'both')
コード例 #38
0
def compute_sampler_quantiles(n=500, p=100, signal_fac=1.2, s=5, sigma=1., rho=0., randomizer_scale=1, full_dispersion=True):

    inst, const = gaussian_instance, lasso.gaussian
    signal = np.sqrt(signal_fac * 2 * np.log(p))

    while True:
        X, Y, beta = inst(n=n,
                          p=p,
                          signal=signal,
                          s=s,
                          equicorrelated=False,
                          rho=rho,
                          sigma=sigma,
                          random_signs=True)[:3]

        idx = np.arange(p)
        sigmaX = rho ** np.abs(np.subtract.outer(idx, idx))
        print("snr", beta.T.dot(sigmaX).dot(beta) / ((sigma ** 2.) * n))

        n, p = X.shape

        if full_dispersion:
            dispersion = np.linalg.norm(Y - X.dot(np.linalg.pinv(X).dot(Y))) ** 2 / (n - p)
            sigma_ = np.sqrt(dispersion)
        W = np.ones(X.shape[1]) * np.sqrt(2 * np.log(p)) * sigma_

        conv = const(X,
                     Y,
                     W,
                     randomizer_scale=randomizer_scale * sigma_)

        signs = conv.fit()
        nonzero = signs != 0
        (observed_target,
         cov_target,
         cov_target_score,
         alternatives) = selected_targets(conv.loglike,
                                          conv._W,
                                          nonzero,
                                          dispersion=dispersion)

        true_mean = np.linalg.pinv(X[:, nonzero]).dot(X.dot(beta))
        estimate, observed_info_mean, _, pval, intervals, _ = conv.selective_MLE(observed_target,
                                                                                 cov_target,
                                                                                 cov_target_score,
                                                                                 alternatives)

        opt_linear, opt_offset = conv.opt_transform
        target_precision = np.linalg.inv(cov_target)
        randomizer_cov, randomizer_precision = conv.randomizer.cov_prec
        score_linear = np.identity(p)
        target_linear = score_linear.dot(cov_target_score.T.dot(target_precision))
        target_offset = conv.observed_score_state - target_linear.dot(observed_target)

        nopt = opt_linear.shape[1]
        ntarget = target_linear.shape[1]

        implied_precision = np.zeros((ntarget + nopt, ntarget + nopt))
        implied_precision[:ntarget, :ntarget] = target_linear.T.dot(randomizer_precision).dot(target_linear) + target_precision
        implied_precision[:ntarget, ntarget:] = target_linear.T.dot(randomizer_precision).dot(opt_linear)
        implied_precision[ntarget:, :ntarget] = opt_linear.T.dot(randomizer_precision).dot(target_linear)
        implied_precision[ntarget:, ntarget:] = opt_linear.T.dot(randomizer_precision).dot(opt_linear)
        implied_cov = np.linalg.inv(implied_precision)

        conditioned_value = target_offset + opt_offset
        implied_mean = implied_cov.dot(np.hstack((target_precision.dot(true_mean)-target_linear.T.dot(randomizer_precision).dot(conditioned_value),
                                                  -opt_linear.T.dot(randomizer_precision).dot(conditioned_value))))

        A_scaling = np.zeros((nopt, ntarget+nopt))
        A_scaling[:,ntarget:] = -np.identity(nopt)
        b_scaling = np.zeros(nopt)
        affine_con = constraints(A_scaling,
                                 b_scaling,
                                 mean=implied_mean,
                                 covariance=implied_cov)

        initial_point = np.zeros(ntarget+nopt)
        initial_point[ntarget:] = conv.observed_opt_state

        sampler = sample_from_constraints(affine_con,
                                          initial_point,
                                          ndraw=500000,
                                          burnin=1000)

        print("sampler", sampler.shape, sampler[:,:ntarget].shape)
        mle_sample = []
        for j in range(sampler.shape[0]):
            estimate, _, _, _, _, _ = conv.selective_MLE(sampler[j,:ntarget],
                                                         cov_target,
                                                         cov_target_score,
                                                         alternatives)
            mle_sample.append(estimate)
            print("iteration ", j)
        mle_sample = np.asarray(mle_sample)
        print("check", mle_sample.shape, np.mean(mle_sample, axis=0) - true_mean)

        for i in range(nonzero.sum()):
            temp = 251 + i
            ax = plt.subplot(temp)
            stats.probplot(mle_sample[:,i], dist="norm", plot=pylab)
            plt.subplots_adjust(hspace=.5, wspace=.5)
        pylab.show()

        sampler_quantiles = np.vstack([np.percentile(mle_sample, 5, axis=0), np.percentile(mle_sample, 95, axis=0)])

        normal_quantiles = np.vstack((norm.ppf(0.05, loc=true_mean, scale=np.sqrt(np.diag(observed_info_mean))),
                                      norm.ppf(0.95, loc=true_mean, scale=np.sqrt(np.diag(observed_info_mean)))))

        print("sampler quantiles", sampler_quantiles.T)
        print("normal quantiles", normal_quantiles.T)
        break
コード例 #39
0
def test_sequentially_constrained(ndraw=100, nsim=50):
    S = -np.identity(10)[:3]
    b = -6 * np.ones(3)
    C = constraints(S, b)
    W = sample(C, nsim, temps=np.linspace(0, 200, 1001))
    U = np.linspace(0, 1, 101)
コード例 #40
0
def test_sequentially_constrained(burnin=None, ndraw=1000, nsim=None):
    S = -np.identity(10)[:3]
    b = -6 * np.ones(3)
    C = constraints(S, b)
    W = sample(C, nsim, temps=np.linspace(0, 200, 1001))
    U = np.linspace(0, 1, 101)