Example #1
0
def test_distribution_sphere(n=15,
                             p=10,
                             sigma=1.,
                             nsample=2000,
                             sample_constraints=False):

    # see if we really are sampling from
    # correct distribution
    # by comparing to an accept-reject sampler

    con, y = _generate_constraints()[:2]
    accept_reject_sample = []

    hit_and_run_sample, W = AC.sample_from_sphere(con,
                                                  y,
                                                  ndraw=25000,
                                                  burnin=10000)
    statistic = lambda x: np.fabs(x).max()
    family = discrete_family([statistic(s) for s in hit_and_run_sample], W)
    radius = np.linalg.norm(y)

    count = 0

    pvalues = []

    while True:

        U = np.random.standard_normal(n)
        U /= np.linalg.norm(U)
        U *= radius

        if con(U):
            accept_reject_sample.append(U)
            count += 1

            true_sample = np.array(
                [statistic(s) for s in accept_reject_sample])
            if (count + 1) % 100 == 0:

                pvalues.extend([family.cdf(0, t) for t in true_sample])
                print np.mean(pvalues), np.std(pvalues)

                if sample_constraints:
                    con, y = _generate_constraints()[:2]

                hit_and_run_sample, W = AC.sample_from_sphere(con,
                                                              y,
                                                              ndraw=10000,
                                                              burnin=10000)
                family = discrete_family(
                    [statistic(s) for s in hit_and_run_sample], W)
                radius = np.linalg.norm(y)
                accept_reject_sample = []

        if count >= nsample:
            break

    U = np.linspace(0, 1, 101)
    plt.plot(U, sm.distributions.ECDF(pvalues)(U))
    plt.plot([0, 1], [0, 1])
def test_distribution_sphere(n=15, p=10, sigma=1.,
                             nsim=2000,
                             sample_constraints=False,
                             burnin=10000,
                             ndraw=10000):

    # see if we really are sampling from 
    # correct distribution
    # by comparing to an accept-reject sampler

    con, y = _generate_constraints()[:2]
    accept_reject_sample = []

    hit_and_run_sample, W = AC.sample_from_sphere(con, y, 
                                                  ndraw=ndraw,
                                                  burnin=burnin)
    statistic = lambda x: np.fabs(x).max()
    family = discrete_family([statistic(s) for s in hit_and_run_sample], W)
    radius = np.linalg.norm(y)

    count = 0

    pvalues = []

    while True:

        U = np.random.standard_normal(n)
        U /= np.linalg.norm(U)
        U *= radius

        if con(U):
            accept_reject_sample.append(U)
            count += 1

            true_sample = np.array([statistic(s) for s in accept_reject_sample])

            if (count + 1) % int(nsim / 10) == 0:

                pvalues.extend([family.cdf(0, t) for t in true_sample])
                print np.mean(pvalues), np.std(pvalues)

                if sample_constraints:
                    con, y = _generate_constraints()[:2]

                hit_and_run_sample, W = AC.sample_from_sphere(con, y, 
                                                              ndraw=ndraw,
                                                              burnin=burnin)
                family = discrete_family([statistic(s) for s in hit_and_run_sample], W)
                radius = np.linalg.norm(y)
                accept_reject_sample = []

        if count >= nsim:
            break

    U = np.linspace(0, 1, 101)
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
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
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
Example #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
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)
Example #8
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)
Example #9
0
def test_conditional_sampling(n=20, p=25, sigma=20):
    """
    goodness of fit samples from
    inactive constraints intersect a sphere

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

    con, y, L = _generate_constraints(n=n, p=p, sigma=sigma)
    print L.active.shape
    con = L.inactive_constraints
    conditional_con = con.conditional(L._X_E.T, np.dot(L._X_E.T, y))

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

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

    T2 = (np.dot(L.R_E, Z.T)**2).sum(0) - np.linalg.norm(np.dot(L.R_E, y))**2
    nt.assert_true(np.linalg.norm(T2) < 1.e-7)
def test_conditional_sampling(n=20, p=25, sigma=20):
    """
    goodness of fit samples from
    inactive constraints intersect a sphere

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

    con, y, L = _generate_constraints(n=n, p=p, sigma=sigma)
    print L.active.shape
    con = L.inactive_constraints
    conditional_con = con.conditional(L._X_E.T, np.dot(L._X_E.T, y))

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

    T2 = (np.dot(L.R_E, Z.T)**2).sum(0) - np.linalg.norm(np.dot(L.R_E, y))**2
    nt.assert_true(np.linalg.norm(T2) < 1.e-7)