]
if dnm in lrdnms:
    from model_lr import *
else:
    from model_poiss import *

print('running ' + str(dnm) + ' ' + str(alg) + ' ' + str(ID))

if not os.path.exists('results/'):
    os.mkdir('results')

if not os.path.exists('results/' + dnm + '_samples.npy'):
    print('No MCMC samples found -- running STAN')
    # run sampler
    N_samples = 10000
    sampler(dnm, dnm in lrdnms, '../data/', 'results/', N_samples)

print('Loading dataset ' + dnm)
Z, Zt, D = load_data('../data/' + dnm + '.npz')
print('Loading posterior samples for ' + dnm)
samples = np.load('results/' + dnm + '_samples.npy')
# TODO FIX SAMPLER TO NOT HAVE TO DO THIS
samples = np.hstack((samples[:, 1:], samples[:, 0][:, np.newaxis]))

# fit a gaussian to the posterior samples
# used for pihat computation for Hilbert coresets with noise to simulate uncertainty in a good pihat
mup = samples.mean(axis=0)
Sigp = np.cov(samples, rowvar=False)

# create the prior -- also used for the above purpose
mu0 = np.zeros(mup.shape[0])
Esempio n. 2
0
if not stan_samples:  # use laplace approximation (save, load from results/)
    if not os.path.exists('results/' + dnm + '_samples.npy'):
        print('sampling using laplace')
        mup_laplace, LSigp_laplace, LSigpInv_laplace = get_laplace(
            np.ones(Z.shape[0]), Z, Z.mean(axis=0)[:D], diag=samplediag)
        samples_laplace = mup_laplace + np.random.randn(
            N_samples, mup_laplace.shape[0]).dot(LSigp_laplace.T)
        np.save(os.path.join('results/' + dnm + '_samples.npy'),
                samples_laplace)
    else:
        print('Loading posterior samples for ' + dnm)
    samples = np.load('results/' + dnm + '_samples.npy', allow_pickle=True)
else:  # use stan sampler (save, load from results/pystan_samples/)
    if not os.path.exists('results/' + dnm + '_samples.npy'):
        print('No MCMC samples found -- running STAN')
        sampler(dnm, True, '../data/', 'results/pystan_samples/', N_samples)
    else:
        print('Loading posterior samples for ' + dnm)
    samples = np.load('results/' + dnm + '_samples.npy', allow_pickle=True)
samples = np.hstack((samples[:, 1:], samples[:, 0][:, np.newaxis]))

#fit a gaussian to the posterior samples
#used for pihat computation for Hilbert coresets with noise to simulate uncertainty in a good pihat
mup = samples.mean(axis=0)
Sigp = np.cov(samples, rowvar=False)
LSigp = np.linalg.cholesky(Sigp)
LSigpInv = sl.solve_triangular(LSigp,
                               np.eye(LSigp.shape[0]),
                               lower=True,
                               overwrite_b=True,
                               check_finite=False)
X = np.linspace(0, 50, 1000)
y = [0] * len(events)
plt.plot(X, f1(X))
plt.plot(events, y, 'r+')
plt.show()

theta = [1, 10, 0.002]
s_f = theta[0]
l = theta[1]
s_n = theta[2]
V = 50
upper_bound = 2
t1 = 0
t2 = 50

mcmc_sample = sampler(t1, t2, theta, upper_bound)
#obs_test, M_test, thinned_pos_test,gp_thinned_test,gp_obs_test = M_sampler(burnin = 100, niter = 400)
obs_test, M_test, thinned_pos_test, gp_thinned_test, gp_obs_test = mcmc_sample.M_sampler(
    events, f1(np.array(events)), burnin=100, niter=1)


def sigmoid(z):
    return 1 / (1 + np.exp(-z))


def extract_position(length):
    # extract samples postition and gp function value
    pos= list(filter(lambda x:x != None, \
                     [thinned_pos_test[i] if x==length else None for i,x in enumerate(M_test)]))

    gp_pos= list(filter(lambda x:x != None, \