def get_prior(): prior_dict = { 'log_eta': dists.LogD(dists.Gamma(a=.1, b=.1)), 'logit_sigma': dists.LogitD(dists.Beta(a=1., b=1.)), 'log_c': dists.LogD(dists.Gamma(a=.1, b=.1)) } return dists.StructDist(prior_dict)
def get_prior(): prior_dict = { 'log_eta': dists.LogD(dists.Gamma(a=.1, b=.1)), #W 'log_tau_minus_one': dists.LogD(dists.Uniform(a=0., b=4.)), #W 'logit_sigma': dists.LogitD(dists.Beta(a=1., b=1.)), 'log_c': dists.LogD(dists.Gamma(a=.1, b=.1)) #W } return dists.StructDist(prior_dict)
import particles from particles import datasets as dts from particles import distributions as dists from particles import resampling as rs from particles import smc_samplers as ssp from particles import state_space_models from particles.collectors import Moments # data data = dts.GBP_vs_USD_9798().data # prior dictprior = { 'mu': dists.Normal(scale=2.), 'sigma': dists.Gamma(a=2., b=2.), 'rho': dists.Beta(a=9., b=1.), 'phi': dists.Uniform(a=-1., b=1.) } prior = dists.StructDist(dictprior) # moment function def qtiles(W, x): alphas = np.linspace(0.05, 0.95, 19) return rs.wquantiles_str_array(W, x.theta, alphas=alphas) # algorithms N = 10**3 # re-run with N= 10^4 for the second CDF plots fks = {} fk_opts = {
label='filtered volatility') plt.legend() #results=particles.multiSMC(fk=fk_model,N=100,nruns=30,qmc={'SMC':False,'SQMC':True}) #results = particles.multiSMC(fk=fk_model, N=100, nruns=30, qmc={'SMC':False, 'SQMC':True}) #plt.figure() #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()):