Пример #1
0
def SDPR_gibbs(beta_margin,
               N,
               ld_boundaries,
               ref_ld_mat,
               mcmc_samples,
               burn,
               max_cluster,
               save_mcmc,
               VS=True):
    M = max_cluster
    trace = {
        'alpha': [],
        'num_cluster': [],
        'beta': np.zeros(shape=(mcmc_samples, len(beta_margin))),
        'suffstats': [],
        'h2': []
    }

    # initialize
    state = gibbs.initial_state(data=beta_margin,
                                N=N,
                                num_clusters=M,
                                a0k=.1,
                                b0k=.1)
    state['suffstats'] = gibbs.update_suffstats(state)
    state['cluster_var'] = gibbs.sample_sigma2(state, VS=True)

    for i in range(mcmc_samples):
        # update everything
        gibbs.gibbs(state,
                    ld_boundaries=ld_boundaries,
                    ref_ld_mat=ref_ld_mat,
                    VS=VS)

        if (i > burn):
            trace['h2'].append(state['h2'] * state['eta']**2)

        # record the result
        trace['beta'][i, ] = state['beta'] * state['eta']
        # trace['pi'][i,:] = np.array(state['pi'].values())
        # trace['cluster_var'][i,:] = np.array(state['cluster_var'].values())
        # trace['alpha'].append(state['alpha'])
        # trace['num_cluster'].append( np.sum(np.array(state['pi'].values()) > .0001) )
        # trace['suffstats'].append(state['suffstats'])

        util.progressBar(value=i + 1, endvalue=mcmc_samples)

    # calculate posterior average
    poster_mean = np.mean(trace['beta'][burn:mcmc_samples], axis=0)

    print 'h2: ' + str(np.median(trace['h2']))

    if save_mcmc is not None:
        f = gzip.open(args.save_mcmc, 'wb')
        pickle.dump(trace, f, protocol=2)
        f.close()

    return poster_mean
Пример #2
0
h = plt.figure()
plt.plot(xp, yp, '.')
plt.title("Conditional Poisson Point Process")
plt.show()
h.savefig("ppp.pdf", bbox_inches="tight")

# Neymann-scott point process
xn, yn = spat_pp.neyman_scott(10, 0, M, 0, M, 10, 10)
h = plt.figure()
plt.plot(xn, yn, '.')
plt.title("Neymann-Scott Point Process")
plt.show()
h.savefig("nspp.pdf", bbox_inches="tight")

# Gibbs Point Process
xg, yg = gibbs.gibbs(N, 0, M, 0, M, 1000)
h = plt.figure()
plt.plot(xg, yg, '.')
plt.title("Gibbs Point Process (Regular)")
plt.show()
h.savefig("gpp.pdf", bbox_inches="tight")

xg, yg = gibbs.gibbs(N, 0, M, 0, M, 2000,
                     eFunction=gibbs.agregatedEnergyFunction)
h = plt.figure()
plt.plot(xg, yg, '.')
plt.title("Gibbs Point Process (Agregated)")
plt.show()
h.savefig("gpp_a.pdf", bbox_inches="tight")

Пример #3
0
    ######
    diff_ind = 2
    M = 2  # number of segments
    n_MCMC = 1
    mus = np.ones(M) * np.mean(means)
    vs = np.ones(M)

    # samples M-1 (M=mus.shape[0]) values from 0 to n-2 last index can't be a changepoint
    locs = helpers.sample_combinations(n - 1, M - 1, None) - 1
    # intialized using empirical segment means based on the sampled locations
    seg_means, _ = helpers.compute_seg_means(seq1, locs)

    a, b, c, d = metropolis_within_gibbs(seq1, n_MCMC, n, M, mus, vs, alpha,
                                         beta, i, sd, locs, seg_means,
                                         diff_ind)
    e, f, g = gibbs(seq1, n_MCMC, n, M, mus, vs, alpha, beta, i, sd, locs,
                    seg_means, diff_ind)

print("2 of 5")
n = 20000
diff_ind = 2
means = np.array([2, 4])
locs = np.array([10000])
seq1, _, _ = helpers.get_sequence(2, n, 3, diff_ind, 218)

problem = 2
for i in rep:
    print(i)
    np.random.seed(i * problem)
    ######
    # n=20000, 1 changepoint, small mean diff, runs for 3 min
    ######
Пример #4
0
    def update_mu():
        for j in range(J):
            update_muj(j)

    def update_sig2():
        def ll(sig2: float):
            out = 0.0
            for j in range(J):
                out += stats.norm.logpdf(y[j], s.mu[j], math.sqrt(sig2))

            return out

        def lp(sig2: float):
            return logpdf_invgamma(sig2, sig2Prior_a, sig2Prior_b)

        s.sig2 = mcmc.metLogAdaptive(s.sig2, ll, lp, tunerSig2)

    update_mu()
    update_sig2()


class State:
    def __init__(self, mu: list, sig2: float):
        self.mu = mu
        self.sig2 = sig2


init = State([0.0] * J, 1.0)
out = gibbs(init, update, nmcmc=1000, burn=1000, printFreq=1)
Пример #5
0
              "aUL": [1]*L,
              "bUL": [1]*L}
    n1 = n2 = 10
    pML = [.8] * 4
    pUL = [.2] * 4
    pM = .2
    niters = 2
    # READ IN ARGUMENTS
    # hypers_file = int(sys.argv[1])
    # hypers = json.loads(hypers_file)

    # CHECK VALID INPUT

    # (init_dict, hypers_dict) = make_prior(init,hypers,L)
    # check_valid_prior(init)

    # MAKE FAKE DATA WITH ARGUMENTS
    Gamma = make_data.make_fake_data(n1,n2,pM,pML,pUL)

    # MAKE INITIAL Z
    Z_init = gibbs.make_Z_init(n1, n2)

    # perform gibbs on fake data
    start = time.time()
    trace, Z_trace = gibbs.gibbs(Gamma, niters, init, hypers, Z_init, n1, n2)
    end = time.time()
    print(end-start)
    zName, traceName = make_file_names(Gamma.shape[0], L)
    Z_trace.to_csv(zName+'.csv', mode='w')
    trace.to_csv(traceName+'.csv', mode='w')