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)
Example #2
0
 def get_prior():
     prior_dict = {
         'lam': dists.Normal(loc=0., scale=1.),#W
         'log_alpha': dists.LogD(dists.Gamma(a=.1, b=.1)),#W
         'log_delta': dists.LogD(dists.Gamma(a=.1, b=.1)),
     }
     return dists.StructDist(prior_dict)
Example #3
0
 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)
 def get_prior():
     prior_dict = {
         'log_eta': dists.LogD(dists.Gamma(a=0.1, b=0.1)),
         'log_tau_minus_one': dists.LogD(dists.Gamma(a=1.0, b=1.0)),
         'log_c': dists.LogD(dists.Gamma(a=0.1, b=0.1)),
         'log_lam': dists.LogD(dists.Gamma(a=0.1, b=0.1))
     }
     #prior_dict = {
     #        'log_eta':dists.LogD(dists.Gamma(a=1.0, b=1.0)),
     #        'log_tau_minus_one':dists.LogD(dists.Gamma(a=1.0, b=1.0)),
     #        'log_c':dists.LogD(dists.Gamma(a=1.0, b=1.0)),
     #        'log_lam':dists.LogD(dists.Gamma(a=1.0, b=1.0))}
     return dists.StructDist(prior_dict)
Example #5
0
 def PX(self, t, xp):
     if self.r <= 0:
         return LDP(loc=xp,
                    r=self.r,
                    b=self.b,
                    sigma=self.sigma,
                    dt=dt[t],
                    step=100.)
     else:
         return dists.Gamma(a=2 * self.r / self.sigma**2,
                            b=2 * self.b /
                            self.sigma**2)  # gamma stationary distribution
Example #6
0
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 = {}
Example #7
0
    def update_theta(self, theta, x):
        new_theta = theta.copy()
        sigma, rho = 0.2, 0.95  # fixed values
        xlag = np.array(x[1:] + [
            0.,
        ])
        dx = (x - rho * xlag) / (1. - rho)
        s = sigma / (1. - rho)**2
        new_theta['mu'] = self.prior.laws['mu'].posterior(dx, sigma=s).rvs()
        return new_theta


prior_dict = {
    'mu': dists.Normal(scale=2.),
    'rho': dists.Uniform(a=-1., b=1.),
    'sigma': dists.Gamma()
}
my_prior = dists.StructDist(prior_dict)

# real data
raw_data = np.loadtxt('data.txt', skiprows=2, usecols=(3, ), comments='(C)')
full_data = np.diff(np.log(raw_data))
data = full_data[:50]

pg = PGStochVol(ssm_cls=StochVol,
                data=data,
                prior=my_prior,
                Nx=200,
                niter=1000)
pg.run()  # may take several seconds...
Example #8
0
 def get_prior():
     prior_dict = {
         'log_alpha': dists.LogD(dists.Gamma(a=.1, b=.1)),
         'log_lambda': dists.LogD(dists.Gamma(a=.1, b=.1))
     }
     return dists.StructDist(prior_dict)
Example #9
0
 def PX0(self):
     # return dists.TruncNormal(mu=0, sigma=1, a=0, b=np.inf)
     a = 2 * self.kappa / self.sigma**2
     b = 2 * self.kappa * self.theta / self.sigma**2 + 1
     return dists.Gamma(a, b)
Example #10
0
from matplotlib import pyplot as plt
import numpy as np
import numpy.random as random 
import pandas
import seaborn as sb
from scipy import stats
from statsmodels.tsa.stattools import acf

import particles
from particles import distributions as dists
from particles import kalman
from particles import mcmc
from particles import smc_samplers

# prior 
dict_prior = {'varX': dists.Gamma(a=.5, b=1.),
              'varY': dists.Gamma(a=.5, b=.1),
              'rho':dists.Uniform(a=-1., b=1.)
             }
prior = dists.StructDist(dict_prior)

# State-space model 
class ReparamLinGauss(kalman.LinearGauss):
    def __init__(self, varX=1., varY=1., rho=0.):
        sigmaX = np.sqrt(varX)
        sigmaY = np.sqrt(varY)
        sigma0 = sigmaX
        # Note: We take X_0 ~ N(0, sigmaX^2) so that Gibbs step is tractable
        kalman.LinearGauss.__init__(self, sigmaX=sigmaX, sigmaY=sigmaY, rho=rho,
                                    sigma0=sigma0)
 
plt.plot([m['mean'] for m in pf.summaries.moments],
         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
Example #12
0
 def get_prior():
     prior_dict = {
         'log_half_nu_minus_one': dists.LogD(dists.Gamma(a=1., b=1.)),
         'log_sigma': dists.LogD(dists.Gamma(a=.1, b=.1))
     }
     return dists.StructDist(prior_dict)
Example #13
0
 def get_prior():
     prior_dict = {
             'log_eta':dists.LogD(dists.Gamma(a=0.1, b=0.1)),
             'log_c':dists.LogD(dists.Gamma(a=0.1, b=0.1)),
             'log_lam':dists.LogD(dists.Gamma(a=0.1, b=0.1))}
     return dists.StructDist(prior_dict)
Example #14
0
import numpy as np
import numpy.random as random
import pandas
import seaborn as sb
from scipy import stats
from statsmodels.tsa.stattools import acf

import particles
from particles import distributions as dists
from particles import kalman
from particles import mcmc
from particles import smc_samplers

# prior
dict_prior = {
    'varX': dists.Gamma(a=.5, b=1.),
    'varY': dists.Gamma(a=.5, b=.1),
    'rho': dists.Uniform(a=-1., b=1.)
}
prior = dists.StructDist(dict_prior)


# State-space model
class ReparamLinGauss(kalman.LinearGauss):
    def __init__(self, varX=1., varY=1., rho=0.):
        sigmaY = np.sqrt(varY) if varY > 0. else 0.
        sigmaX = np.sqrt(varX) if varX > 0. else 0.
        sigma0 = sigmaX
        # Note: We take X_0 ~ N(0, sigmaX^2) so that Gibbs step is tractable
        kalman.LinearGauss.__init__(self,
                                    sigmaX=sigmaX,
Example #15
0
        ssms.ThetaLogistic.__init__(self, **kwargs)
        self.sigmaX = 1. / np.sqrt(self.precX)
        self.sigmaY = 1. / np.sqrt(self.precY)


ssm_cls = ThetaLogisticReparametrised

# data
data = np.loadtxt('../../datasets/nutria.txt')

# prior
dict_prior = OrderedDict()
dict_prior['tau0'] = dists.TruncNormal(b=3.)
dict_prior['tau1'] = dists.TruncNormal(b=3.)
dict_prior['tau2'] = dists.TruncNormal(b=3.)
dict_prior['precX'] = dists.Gamma(a=2., b=1.)
dict_prior['precY'] = dists.Gamma(a=2., b=1.)
prior = dists.StructDist(dict_prior)


# Particle Gibbs
class PGibbs(mcmc.ParticleGibbs):
    def update_theta(self, theta, x):
        new_theta = theta.copy()
        ax = np.array(x)
        dax = np.diff(ax)
        ay = np.array(data).flatten()
        tau0 = theta['tau0']
        tau1 = theta['tau1']
        tau2 = theta['tau2']
    print("number of measured states ", len(true_states))
    print("true EOL ", true_states[-1])
    data = np.random.normal(loc = true_states, scale = true_v)
    data = data#[0:6]#[0:]

    # Define a prior
    # This prior has the true parameters as mean
    # prior_dict = {'c': dists.Normal(loc=9.12e-11,scale=1e-11),
    #               'm': dists.Normal(loc = 1.4353, scale = 1e-1),
    #               'a0':dists.Gamma(1/2, 2), # Chi squared with k=1
    #               "v":dists.Gamma(1/2,2)}  # This is chi squared with k=1

    # This prior is not exactly the same as the true parameter
    prior_dict = {'c': dists.Normal(loc=8e-11,scale=2e-11),
                  'm': dists.Normal(loc = 1.5, scale = 1e-1),
                  'a0':dists.Gamma(1/2, 2), # Chi squared with k=1
                  "v":dists.Gamma(1/2,2)}  # This is chi squared with k=1

    # # This prior is significantly different from the true parameters
    # prior_dict = {'c': dists.Normal(loc=8e-11,scale=1e-11),
    #               'm': dists.Normal(loc = 1.5, scale = 5e-1),
    #               'a0':dists.Gamma(1/2, 2), # Chi squared with k=1
    #               "v":dists.Gamma(1/2,2)}  # This is chi squared with k=1

    my_prior = dists.StructDist(prior_dict)

    # Run the smc2 inference
    fk_smc2 = ssp.SMC2(ssm_cls=ParisLaw, data=data, prior=my_prior, init_Nx=8000)#, ar_to_increase_Nx=0.1)
    alg_smc2 = particles.SMC(fk=fk_smc2, N=10000, store_history=True, verbose=True)
    alg_smc2.run()
Example #17
0
import warnings

warnings.simplefilter('ignore')  # hide warnings

from matplotlib import pyplot as plt
import numpy as np

from particles import distributions as dists

prior_dict = {
    'mu': dists.Normal(scale=2.),
    'rho': dists.Uniform(a=-1., b=1.),
    'sigma': dists.Gamma()
}
my_prior = dists.StructDist(prior_dict)

theta = my_prior.rvs(size=500)  # sample 500 theta-parameters

plt.style.use('ggplot')
plt.hist(theta['sigma'], 30)
plt.xlabel('sigma')

plt.figure()
z = my_prior.logpdf(theta)
plt.hist(z, 30)
plt.xlabel('log-pdf')

plt.figure()
another_prior_dict = {
    'rho': dists.Uniform(a=-1., b=1.),
    'log_sigma': dists.LogD(dists.Gamma())