def test_with_ugly(self):
        np.random.seed(42)


        def grad_log_prob(x):
            return -(x/5.0 + np.sin(x))*(1.0/5.0 + np.cos(x))

        def log_prob(x):
            return -(x/5.0 + np.sin(x) )**2.0/2.0

        generator = mh_generator(log_density=log_prob,x_start=1.0)
        tester = GaussianSteinTest(grad_log_prob,41)

        selector = SampleSelector(generator, sample_size=1000,thinning=20,tester=tester, max_iterations=5)

        data,converged = selector.points_from_stationary()

        tester = GaussianSteinTest(grad_log_prob,41)
        assert tester.compute_pvalue(data)>0.05

        assert converged is True
    def test_on_one_dim_gaussian(self):
        np.random.seed(42)

        def log_normal(x):
                return  -np.dot(x,x)/2

        generator = mh_generator(log_density=log_normal)

        def gradient_of_log_of_normal(x):
            return  -x


        tester = GaussianSteinTest(gradient_of_log_of_normal,10)


        selector = SampleSelector(generator, sample_size=2000,thinning=15,tester=tester)

        data,converged = selector.points_from_stationary()

        tester = GaussianSteinTest(gradient_of_log_of_normal,10)
        assert tester.compute_pvalue(data)>0.05
        assert converged
 def test_power_growth(self):
     np.random.seed(42)
     data = np.random.randn(10000,4)+0.01*np.random.rand()
     me = GaussianSteinTest(self.grad_log_normal,10)
     p1 = me.compute_pvalue(data)
     me = GaussianSteinTest(self.grad_log_normal,1)
     p2 = me.compute_pvalue(data)
     assert p1 <p2
    def test_on_one_dim_gaussian(self):
        np.random.seed(42)

        def log_normal(x):
            return -np.dot(x, x) / 2

        generator = mh_generator(log_density=log_normal)

        def gradient_of_log_of_normal(x):
            return -x

        tester = GaussianSteinTest(gradient_of_log_of_normal, 10)

        selector = SampleSelector(generator,
                                  sample_size=2000,
                                  thinning=15,
                                  tester=tester)

        data, converged = selector.points_from_stationary()

        tester = GaussianSteinTest(gradient_of_log_of_normal, 10)
        assert tester.compute_pvalue(data) > 0.05
        assert converged
    def test_with_ugly(self):
        np.random.seed(42)

        def grad_log_prob(x):
            return -(x / 5.0 + np.sin(x)) * (1.0 / 5.0 + np.cos(x))

        def log_prob(x):
            return -(x / 5.0 + np.sin(x))**2.0 / 2.0

        generator = mh_generator(log_density=log_prob, x_start=1.0)
        tester = GaussianSteinTest(grad_log_prob, 41)

        selector = SampleSelector(generator,
                                  sample_size=1000,
                                  thinning=20,
                                  tester=tester,
                                  max_iterations=5)

        data, converged = selector.points_from_stationary()

        tester = GaussianSteinTest(grad_log_prob, 41)
        assert tester.compute_pvalue(data) > 0.05

        assert converged is True
Exemple #6
0
from stat_test.linear_time import GaussianSteinTest

__author__ = 'kcx'
import numpy as np


def grad_log_normal(m):
    def grad_log_mix(x):
        e2mx = np.exp(2 * m * x)
        nom = m - e2mx * m + x + e2mx * x
        denom = 1 + e2mx
        return -nom / denom

    return grad_log_mix


m = 5

me = GaussianSteinTest(grad_log_normal(m), m)

X = np.random.randn(10000) - m

print(me.compute_pvalue(X))
Exemple #7
0
import numpy as np

samples = np.load('./samples.npy')

np.random.seed(SEED)
X = gen_X(SAMPLE_SIZE)


def grad_log_pob(theta):
    s = []
    for t in theta:
        s.append(np.sum(manual_grad(t[0], t[1], X), axis=0))
    return np.array(s)


me = GaussianSteinTest(grad_log_pob, 1)

times_we_look_at = range(0, CHAIN_SIZE, 1)
# arr = np.empty((0,2))
#
#
# for time in times_we_look_at:
#     chain_at_time = samples[:,time]
#     print(time)
#     list_of_chain_slices = np.split(chain_at_time,NUMBER_OF_TESTS)
#     for chains_slice in list_of_chain_slices:
#         assert chains_slice.shape == (NO_OF_SAMPELS_IN_TEST,2)
#         pval = me.compute_pvalue(chains_slice)
#         arr = np.vstack((arr, np.array([time,pval])))
#
#
__author__ = 'kcx'
import  numpy as np

samples = np.load('./samples.npy')

np.random.seed(SEED)
X = gen_X(SAMPLE_SIZE)


def grad_log_pob(theta):
    s=[]
    for t in theta:
        s.append( np.sum(manual_grad(t[0],t[1],X),axis=0))
    return np.array(s)

me = GaussianSteinTest(grad_log_pob,1)

times_we_look_at = range(1,SGLD_CHAIN_SIZE,999)
arr = np.empty((0,2))


for time in times_we_look_at:
    chain_at_time = samples[:,time]
    print(time)
    list_of_chain_slices = np.split(chain_at_time,NUMBER_OF_TESTS)
    for chains_slice in list_of_chain_slices:
        assert chains_slice.shape == (NO_OF_SAMPELS_IN_TEST,2)
        pval = me.compute_pvalue(chains_slice)
        arr = np.vstack((arr, np.array([time,pval])))

__author__ = 'kcx'
import numpy as np


def log_normal(x):
    return -np.dot(x, x) / 2


thining_jump = 20
chain_size = 10000
results = np.zeros((thining_jump, 3))

for thining in range(1, thining_jump, 2):
    print('thining ', thining)
    pval = []

    for i in range(1000):
        x = metropolis_hastings(log_normal,
                                chain_size=chain_size,
                                thinning=thining)

        me = GaussianSteinTest(x, log_normal)

        pval.append(me.compute_pvalue())

    res = np.percentile(pval, [5, 10, 15]) * 100.0
    results[thining] = res

print(results)

np.save('temp_quantiles.npy', results)
Exemple #10
0
    return  -x

m=2

N = 250*m


dfs = range(1, 10, 2)
mc_reps = 200
res = np.empty((0,2))

for df in dfs:
    for mc in range(mc_reps):

        X = np.random.standard_t(df,N)
        me = GaussianSteinTest(grad_log_normal,m)
        pvalue = me.compute_pvalue(X)
        res = np.vstack((res,np.array([df, pvalue])))

for mc in range(mc_reps):

        X = np.random.randn(N)
        me = GaussianSteinTest(grad_log_normal,m)
        pvalue = me.compute_pvalue(X)
        res = np.vstack((res,np.array([np.Inf, pvalue])))

# import matplotlib.pyplot as plt
# plt.plot(sorted(res[:,1]))
# plt.show()

np.save('results.npy',res)
 def test_on_four_dim_gaussian_with_three_freqs(self):
     np.random.seed(42)
     data = np.random.randn(10000,4)
     me = GaussianSteinTest(self.grad_log_normal,3)
     assert me.compute_pvalue(data) > 0.05
 def test_on_one_dim_gaussian(self):
     np.random.seed(42)
     data = np.random.randn(10000)
     me = GaussianSteinTest(self.grad_log_normal,1)
     assert me.compute_pvalue(data)>0.05
Exemple #13
0
from stat_test.linear_time import GaussianSteinTest

__author__ = 'kcx'
import numpy as np



def grad_log_normal(m):
    def grad_log_mix(x):
        e2mx = np.exp(2 * m * x)
        nom = m - e2mx * m + x + e2mx * x
        denom = 1 + e2mx
        return -nom / denom

    return grad_log_mix

m=5

me = GaussianSteinTest(grad_log_normal(m),m)

X = np.random.randn(10000)-m

print(me.compute_pvalue(X))
    return -x


m = 2

N = 250 * m

dfs = range(1, 10, 2)
mc_reps = 200
res = np.empty((0, 2))

for df in dfs:
    for mc in range(mc_reps):

        X = np.random.standard_t(df, N)
        me = GaussianSteinTest(grad_log_normal, m)
        pvalue = me.compute_pvalue(X)
        res = np.vstack((res, np.array([df, pvalue])))

for mc in range(mc_reps):

    X = np.random.randn(N)
    me = GaussianSteinTest(grad_log_normal, m)
    pvalue = me.compute_pvalue(X)
    res = np.vstack((res, np.array([np.Inf, pvalue])))

# import matplotlib.pyplot as plt
# plt.plot(sorted(res[:,1]))
# plt.show()

np.save('results.npy', res)
__author__ = 'kcx'
import numpy as np



def log_normal(x):
    return -np.dot(x,x)/2


thining_jump = 20
chain_size = 10000
results = np.zeros((thining_jump,3))

for thining in range(1,thining_jump,2):
    print('thining ', thining)
    pval = []

    for i in range(1000):
        x= metropolis_hastings(log_normal, chain_size=chain_size, thinning=thining)

        me = GaussianSteinTest(x,log_normal)

        pval.append(me.compute_pvalue())


    res = np.percentile(pval, [5,10,15])*100.0
    results[thining] = res

print(results)

np.save('temp_quantiles.npy',results)