kalman.LinearGauss.__init__(self, sigmaX=sigmaX, sigmaY=sigmaY, rho=rho,
                                    sigma0=sigma0)
 
data = np.loadtxt('./simulated_linGauss_T100_varX1_varY.04_rho.9.txt')


niter = 10 ** 5
burnin = int(niter/ 10)
algos = OrderedDict()

# PMMH algorithms 
scales = list(reversed([0.025, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.7, 1.]))
for scale in scales: 
    key = 'scale=%g' % scale 
    algos[key] = mcmc.PMMH(ssm_cls=ReparamLinGauss, prior=prior, 
                           data=data, Nx=100, niter=niter, 
                           adaptive=False, rw_cov=scale ** 2 * np.eye(3),
                           verbose=10, scale=scale)
# Run the algorithms 
####################

for alg_name, alg in algos.items(): 
    print('\nRunning ' + alg_name)
    alg.run()
    print('CPU time: %.2f min' % (alg.cpu_time / 60))
    print('mean squared jumping distance: %f' % msjd(alg.chain.theta[burnin:]))

# Plots
#######
savefigs = False
plt.style.use('ggplot')
예제 #2
0
# data was simulated as follows:
# _, data = ReparamLinGauss(varX=1., varY=(0.2)**2, rho=.9).simulate(100)
data = np.loadtxt('./simulated_linGauss_T100_varX1_varY.04_rho.9.txt')

niter = 10**3
algos = OrderedDict()
rw_cov = (0.15)**2 * np.eye(3)

# PMMH algorithms
Nxs = list(range(100, 1100, 100))  # needs list for Python 3

for Nx in Nxs:
    algos[Nx] = mcmc.PMMH(ssm_cls=ReparamLinGauss,
                          prior=prior,
                          data=data,
                          Nx=Nx,
                          niter=niter,
                          adaptive=False,
                          rw_cov=rw_cov,
                          verbose=10)

# Run the algorithms
####################

for Nx, alg in algos.items():
    print('Nx = %i' % Nx)
    alg.run()
    print('CPU time: %.2f min' % (alg.cpu_time / 60))

# PLOTS
#######
savefigs = True  # False if you don't want to save plots as pdfs
예제 #3
0
#sb.boxplot(x=[r['output'].logLt for r in results], y=[r['qmc'] for r in results])

smooth_trajectories = pf.hist.backward_sampling(10)
plt.figure()
plt.plot(smooth_trajectories)

prior_dict = {
    'mu': dists.Normal(),
    'sigma': dists.Gamma(a=1., b=1.),
    'rho': dists.Beta(9., 1.)
}
my_prior = dists.StructDist(prior_dict)

from particles import mcmc  # where the MCMC algorithms (PMMH, Particle Gibbs, etc) live
pmmh = mcmc.PMMH(ssm_cls=StochVol,
                 prior=my_prior,
                 data=data,
                 Nx=50,
                 niter=1000)
pmmh.run()  # Warning: takes a few seconds

plt.figure()
# plot the marginals
burnin = 100  # discard the 100 first iterations
for i, param in enumerate(prior_dict.keys()):
    plt.subplot(2, 2, i + 1)
    sb.distplot(pmmh.chain.theta[param][burnin:], 40)
    plt.title(param)

plt.show()