Esempio n. 1
0
def test_1():
    r("require('coda')")

    random = np.random.RandomState(1)
    for i in range(10):
        x = generate_AR1(phi=0.95, sigma=1, n_steps=1000, c=0, y0=0, random_state=random)
        r.assign('x', x)
        tau = r('nrow(x)/effectiveSize(x)')[0]
        np.testing.assert_approx_equal(tau, integrated_autocorr2(x))
Esempio n. 2
0
def test_1():
    r("require('coda')")

    random = np.random.RandomState(1)
    for i in range(10):
        x = generate_AR1(phi=0.95,
                         sigma=1,
                         n_steps=1000,
                         c=0,
                         y0=0,
                         random_state=random)
        r.assign('x', x)
        tau = r('nrow(x)/effectiveSize(x)')[0]
        np.testing.assert_approx_equal(tau, integrated_autocorr2(x))
Esempio n. 3
0
    def summarize(self):
        counts_nz = np.count_nonzero(self.countsmat_)
        cnz = self.countsmat_[np.nonzero(self.countsmat_)]

        correlation_times = integrated_autocorr2(self.all_timescales_[:, :5])

        fmt = lambda x: ', '.join(['{:.2f}'.format(xx) for xx in x])
        fmt_d = lambda x: ', '.join(
            ['{:d}'.format(xx) for xx in x.astype(int)])

        return """Bayesian Markov State Model
---------------------------
Lag time         : {lag_time}
Alpha            : {alpha}
Beta             : {beta}
n_samples        : {n_samples}
n_steps / sample : {n_steps}
epsilon          : {epsilon}

Number of states  : {n_states}
Rejection Rate    : {rej}
Walltime / sample : {walltime:.3} s
Timescales:
    mean : [{mean_ts}]  units
    stdev: [{std_ts}]  units
Timescale correlation times:
    [{correlation_times}]  HMC steps
Approximate number of independent samples:
    [{independent_samples}]
""".format(lag_time=self.lag_time,
           alpha=self.prior_alpha,
           beta=self.prior_beta,
           n_samples=self.n_samples,
           n_steps=self.n_steps,
           epsilon=self.epsilon,
           n_states=self.n_states_,
           rej=self.diag_['rej'],
           mean_ts=fmt(self.all_timescales_.mean(0)[:5]),
           std_ts=fmt(self.all_timescales_.std(0)[:5]),
           correlation_times=fmt(correlation_times),
           walltime=self.walltime_ / self.n_samples,
           independent_samples=fmt_d(self.n_samples / correlation_times))
Esempio n. 4
0
    def summarize(self):
        counts_nz = np.count_nonzero(self.countsmat_)
        cnz = self.countsmat_[np.nonzero(self.countsmat_)]

        correlation_times = integrated_autocorr2(self.all_timescales_[:, :5])

        fmt = lambda x: ', '.join(['{:.2f}'.format(xx) for xx in x])
        fmt_d = lambda x: ', '.join(['{:d}'.format(xx) for xx in x.astype(int)])

        return """Bayesian Markov State Model
---------------------------
Lag time         : {lag_time}
Alpha            : {alpha}
Beta             : {beta}
n_samples        : {n_samples}
n_steps / sample : {n_steps}
epsilon          : {epsilon}

Number of states  : {n_states}
Rejection Rate    : {rej}
Walltime / sample : {walltime:.3} s
Timescales:
    mean : [{mean_ts}]  units
    stdev: [{std_ts}]  units
Timescale correlation times:
    [{correlation_times}]  HMC steps
Approximate number of independent samples:
    [{independent_samples}]
""".format(
        lag_time=self.lag_time, alpha=self.prior_alpha, beta=self.prior_beta,
        n_samples=self.n_samples, n_steps=self.n_steps, epsilon=self.epsilon,
        n_states=self.n_states_, rej=self.diag_['rej'],
        mean_ts=fmt(self.all_timescales_.mean(0)[:5]),
        std_ts=fmt(self.all_timescales_.std(0)[:5]),
        correlation_times=fmt(correlation_times),
        walltime = self.walltime_ / self.n_samples,
        independent_samples=fmt_d(self.n_samples/correlation_times)
        )
Esempio n. 5
0
def test_2():
    x = np.random.randn(100, 2)
    integrated_autocorr2(x)
Esempio n. 6
0
tau3 = []
tau4 = []
tau5 = []
tau6 = []

n_trials = 10
for i in range(n_trials):
    y = generate_AR1(phi=PHI,
                     sigma=1,
                     n_steps=n_steps,
                     c=0,
                     y0=0,
                     random_state=None)

    tau1.append([integrated_autocorr1(y[:n]) for n in grid])
    tau2.append([integrated_autocorr2(y[:n]) for n in grid])
    tau3.append([integrated_autocorr3(y[:n]) for n in grid])
    tau4.append([integrated_autocorr4(y[:n]) for n in grid])
    tau5.append([integrated_autocorr5(y[:n]) for n in grid])
    tau6.append([integrated_autocorr6(y[:n]) for n in grid])

pp.errorbar(grid,
            y=np.mean(tau1, axis=0),
            yerr=np.std(tau1, axis=0),
            c='b',
            label='tau 1')
pp.errorbar(grid - 1,
            y=np.mean(tau2, axis=0),
            yerr=np.std(tau2, axis=0),
            c='r',
            label='tau 2')
Esempio n. 7
0
n_steps = 1000000
grid = np.logspace(2, np.log10(n_steps), 10)

tau1 = []
tau2 = []
tau3 = []
tau4 = []
tau5 = []
tau6 = []

n_trials = 10
for i in range(n_trials):
    y = generate_AR1(phi=PHI, sigma=1, n_steps=n_steps, c=0, y0=0, random_state=None)

    tau1.append([integrated_autocorr1(y[:n]) for n in grid])
    tau2.append([integrated_autocorr2(y[:n]) for n in grid])
    tau3.append([integrated_autocorr3(y[:n]) for n in grid])
    tau4.append([integrated_autocorr4(y[:n]) for n in grid])
    tau5.append([integrated_autocorr5(y[:n]) for n in grid])
    tau6.append([integrated_autocorr6(y[:n]) for n in grid])

pp.errorbar(grid, y=np.mean(tau1, axis=0), yerr=np.std(tau1, axis=0), c='b',    label='tau 1')
pp.errorbar(grid-1, y=np.mean(tau2, axis=0), yerr=np.std(tau2, axis=0), c='r',    label='tau 2')
pp.errorbar(grid-5, y=np.mean(tau3, axis=0), yerr=np.std(tau3, axis=0), c='g',    label='tau 3')
pp.errorbar(grid-10, y=np.mean(tau4, axis=0), yerr=np.std(tau4, axis=0), c='gold', label='tau 4')
pp.errorbar(grid-20, y=np.mean(tau5, axis=0), yerr=np.std(tau5, axis=0), c='m',    label='tau 5')
pp.errorbar(grid-30, y=np.mean(tau6, axis=0), yerr=np.std(tau6, axis=0), c='cyan', label='tau 6')


pp.plot(grid, [TRUE]*len(grid), 'k-')
pp.xscale('log')
Esempio n. 8
0
def test_2():
    x = np.random.randn(100, 2)
    integrated_autocorr2(x)