Esempio n. 1
0
import particles
from particles import state_space_models as ssm

# Data and parameter values from Pitt & Shephard
raw_data = np.loadtxt('../../datasets/GBP_vs_USD_9798.txt',
                      skiprows=2,
                      usecols=(3, ),
                      comments='(C)')
T = 201
data = 100. * np.diff(np.log(raw_data[:(T + 1)]))
my_ssm = ssm.StochVol(mu=2 * np.log(.5992), sigma=0.178, rho=0.9702)

# FK models
models = OrderedDict()
models['bootstrap'] = ssm.Bootstrap(ssm=my_ssm, data=data)
models['guided'] = ssm.GuidedPF(ssm=my_ssm, data=data)
models['apf'] = ssm.AuxiliaryPF(ssm=my_ssm, data=data)

# Get results
results = particles.multiSMC(fk=models, N=10**3, nruns=250, moments=True)

# Golden standard
bigN = 10**5
bigpf = particles.SMC(fk=models['bootstrap'], N=bigN, qmc=True, moments=True)
print('One SQMC run with N=%i' % bigN)
bigpf.run()

# PLOTS
# =====
plt.style.use('ggplot')
savefig = False  # True if you want to save figs as pdfs
Esempio n. 2
0
sigmaY = .2
rho = 0.9
T = 1000
N = 200

# define ss model, simulate data
ssm = kalman.LinearGauss(sigmaX=sigmaX, sigmaY=sigmaY, rho=rho)
true_states, data = ssm.simulate(T)

# computes true log-likelihood
kf = kalman.Kalman(ssm=ssm, data=data)
kf.filter()
true_loglik = np.sum(kf.logpyt)

# FK model
fk_model = state_space_models.GuidedPF(ssm=ssm, data=data)

# Run SMC algorithm for different values of ESS_min
alphas = list(np.linspace(0., .1, 11)) + list(np.linspace(0.15, 1., 18))
results = particles.multiSMC(fk=fk_model,
                             N=N,
                             ESSrmin=alphas,
                             nruns=200,
                             nprocs=1)

# PLOTS
#======
plt.style.use('ggplot')
savefigs = True  # False if you don't want to save plots as pdfs

# inter-quartile range of log-likelihood estimate as a function of ESSmin
def fkmod(**kwargs):
    return ssms.GuidedPF(ssm=ssms.ThetaLogistic(**kwargs), data=data)
Esempio n. 4
0
alpha0 = 0.3
T = 50
dims = range(5, 21, 5)

# instantiate models 
models = OrderedDict()
true_loglik, true_filt_means = {}, {}
for d in dims: 
    ssm = kalman.MVLinearGauss_Guarniero_etal(alpha=alpha0, dx=d)
    _, data = ssm.simulate(T)
    kf = kalman.Kalman(ssm=ssm, data=data)
    kf.filter()
    true_loglik[d] = np.cumsum(kf.logpyt) 
    true_filt_means[d] = [f.mean for f in kf.filt]
    models['boot_%i' % d] = ssms.Bootstrap(ssm=ssm, data=data)
    models['guided_%i' % d] = ssms.GuidedPF(ssm=ssm, data=data)

# Get results 
N = 10**4 
results = particles.multiSMC(fk=models, qmc=[False, True], N=N, moments=True,
                          nruns=100, nprocs=1) 

# Format results 
results_mse = []
for d in dims: 
    for t in range(T): 
        # this returns the estimate of E[X_t(1)|Y_{0:t}]
        estimate = lambda r: r['output'].summaries.moments[t]['mean'][0] 
        for type_fk in ['guided', 'boot']:
            for qmc in [False, True]:  
                est = np.array( [estimate(r) for r in results 
Esempio n. 5
0
dy = 80  # number of neurons
dx = 6 # 3 for position, 3 for velocity
T = 25
a0 = random.normal(loc=2.5, size=dy)  # from Koyama et al
# the b's are generated uniformly on the unit sphere in R^6 (see Koyama et al)
b0 = random.normal(size=(dy, dx))
b0 = b0 / (linalg.norm(b0, axis=1)[:, np.newaxis])
delta0 = 0.03; tau0 = 1.
x0 = np.zeros(dx)

# models
chosen_ssm = NeuralDecoding(a=a0, b=b0, x0=x0, delta=delta0, tau=tau0)
_, data = chosen_ssm.simulate(T)
models = OrderedDict()
models['boot'] = ssms.Bootstrap(ssm=chosen_ssm, data=data)
models['guided'] = ssms.GuidedPF(ssm=chosen_ssm, data=data)
# models['apf'] = ssms.AuxiliaryPF(ssm=chosen_ssm, data=data)
# Uncomment this if you want to include the APF in the comparison

N = 10**4; nruns = 50
results = particles.multiSMC(fk=models, N=N, nruns=nruns, nprocs=1,
                             collect=[Moments], store_history=True)

## PLOTS
#########
savefigs = True  # False if you don't want to save plots as pdfs
plt.style.use('ggplot')

# arbitrary time
t0 = 9
Esempio n. 6
0
dx = 6  # 3 for position, 3 for velocity
T = 25
a0 = random.normal(loc=2.5, size=dy)  # from Kass' paper
# the b's are generated uniformly on the unit sphere in R^6 (see Kass' paper)
b0 = random.normal(size=(dy, dx))
b0 = b0 / (linalg.norm(b0, axis=1)[:, np.newaxis])
delta0 = 0.03
tau0 = 1.
x0 = np.zeros(dx)  # TODO

# models
chosen_ssm = NeuralDecoding(a=a0, b=b0, x0=x0, delta=delta0, tau=tau0)
_, data = chosen_ssm.simulate(T)
models = OrderedDict()
models['boot'] = state_space_models.Bootstrap(ssm=chosen_ssm, data=data)
models['guided'] = state_space_models.GuidedPF(ssm=chosen_ssm, data=data)
# models['apf'] = state_space_models.AuxiliaryPF(ssm=chosen_ssm, data=data)
# Uncomment this if you want to include the APF in the comparison

N = 10**4
nruns = 50
results = particles.multiSMC(fk=models,
                             N=N,
                             nruns=nruns,
                             nprocs=1,
                             moments=True,
                             store_history=True)

## PLOTS
#########
savefigs = True  # False if you don't want to save plots as pdfs
Esempio n. 7
0
    evaluate.dist_cmp({
        'px': cir.PX(dist_args['t'], dist_args['xp']),
        'norm': cir.proposal_normal(**dist_args),
        't': cir.proposal_t(**dist_args)  # model.distributions.t_nn(df=30, loc=dist_norm.mu, scale=dist_norm.sigma)
        # 'tq': cir.proposal_t_quantile(**dist_args)
    }, title=f"Transition density and proposals at t={t}", ax=ax)

fig, ax = plt.subplots()
with np.errstate(divide='ignore', invalid='ignore'):
    anim = FuncAnimation(fig, update, frames=np.arange(1, 100), interval=300)
    # anim.save('compare_densities2.gif', dpi=80, writer='imagemagick')
    plt.show()

# Trajectories of means of PX and proposals ===========================================================================
cir = CIR.CIR(tau=tau, H=H, **cir_args)  # CIR with normal proposal
alg = particles.SMC(fk=ssm.GuidedPF(ssm=cir, data=real_y), **default_args)
alg.run()
xh = alg.summaries.moments  # Estimated posterior mean
dists = pd.DataFrame({'px': np.array([cir.PX(t, xp).stats[0] for t, xp in enumerate(real_x[:99], start=1)]).reshape((99,)),
        'norm': np.array([cir.proposal_normal(t, xp, real_y).mu for t, xp in enumerate(real_x[:99], start=1)]).reshape((99,)),
        't': np.array([cir.proposal_t(t, xp, real_y).stats[0] for t, xp in enumerate(real_x[:99], start=1)]).reshape((99,))
         })
dists.plot()


# Proposals and their tails ===========================================================================================
# Cross-sectional analysis
importlib.reload(CIR)
cir = CIR.CIR(tau=tau, H=H, **cir_args)  # CIR with normal proposal
alg = particles.SMC(fk=ssm.GuidedPF(ssm=cir, data=real_y), **default_args)
alg.run()
Esempio n. 8
0
# %% Settings parameters
N = 100  # 5000
T = 1 * 10**2  # 5 * 10 ** 4
R = 100  # Number of independent replications
my_seed = 3035802483
# Use same random seeds in R independent replications
MAX_INT_32 = np.iinfo(np.int32).max
seeds = [np.random.randint(MAX_INT_32) for i in range(R)]

tau = [0.25, 0.5, 1, 3, 5, 10]
#%% Define model and generate real data
# Run this cell to generate data or run the second cell below to load data generated and saved before.
cir = CIR()
real_x, real_y = cir.simulate(T)
fk_PF = ssm.GuidedPF(ssm=CIR(), data=real_y)
CIR_plot(real_x)

#%% Save the generated data
# Run this cell to save the newly generated data into the Records/CIRYYYYmmdd-HHMMSS folder.

name = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
os.mkdir(f"./Records/CIR{name}")  # Create folder

# Save the real_states plot
plt.savefig(f"./Records/CIR{name}/real_states.png")
# Save the generated data
np.savez(file=f"./Records/CIR{name}/data", real_x=real_x, real_y=real_y)
# Save parameters of the model where data from.
config = configparser.ConfigParser()
config["PARAMETERS"] = {**cir.default_params, "Delta": cir.Delta, "H": cir.H}
Esempio n. 9
0
#parameter values
alpha0 = 0.4
T = 50
dims = range(5, 21, 5)

# instantiate models
models = OrderedDict()
true_loglik, true_filt_means = {}, {}
for d in dims:
    my_ssm = ssm.MVLinearGauss_Guarniero_etal(alpha=alpha0, dx=d)
    _, data = my_ssm.simulate(T)
    truth = my_ssm.kalman_filter(data)
    true_loglik[d] = truth.logpyts.cumsum()
    true_filt_means[d] = truth.filt.means
    models['boot_%i' % d] = ssm.Bootstrap(ssm=my_ssm, data=data)
    models['guided_%i' % d] = ssm.GuidedPF(ssm=my_ssm, data=data)

# Get results
N = 10**4
results = particles.multiSMC(fk=models,
                             qmc=[False, True],
                             N=N,
                             moments=True,
                             nruns=100,
                             nprocs=0)

# Format results
results_mse = []
for d in dims:
    for t in range(T):
        # this returns the estimate of E[X_t(1)|Y_{0:t}]