-
Notifications
You must be signed in to change notification settings - Fork 4
/
Examples_and_Tests.py
759 lines (596 loc) · 20.3 KB
/
Examples_and_Tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# coding: utf-8
# License
# ===
#
# Copyright (c) 2017 Jeff Alstott
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Except as contained in this notice, the name of the authors shall not be used
# in advertising or otherwise to promote the sale, use or other dealings in this
# Software without prior written authorization from the authors.
# Initial setup
# ===
# In[1]:
### Initial setup
get_ipython().magic('pylab inline')
import pandas as pd
import seaborn as sns
sns.set_color_codes()
from matplotlib import rc
# rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
rc('font',**{'family':'serif','serif':['DejaVu Sans']})
# rc('text', usetex=True)
# In[2]:
from pystan_time_series import TimeSeriesModel
# Stan settings and testing functions
# ===
# In[3]:
### Stan settings and testing functions
n_jobs = 4
n_iterations = 500
from scipy.stats import percentileofscore
def parameter_within_95(model_fit, parameter, parameter_stan, ind=None):
parameter_samples = model_fit[parameter_stan]
if ind is not None:
parameter = parameter[ind]
parameter_samples = parameter_samples[:,:,ind]
parameter_samples = parameter_samples.squeeze()
if parameter_samples.ndim==1:
parameter_samples = atleast_2d(parameter_samples).T
if shape(parameter) == ():
parameter = array([parameter for i in range(parameter_samples.shape[1])])
else:
parameter = array(parameter)
if parameter_samples.ndim>2:
parameter_samples = parameter_samples.reshape(parameter_samples.shape[0],
prod(parameter_samples.shape[1:]))
true_parameters_inferred_scores = array(list(map(percentileofscore,
parameter_samples.T,
parameter.ravel())))
true_parameters_inferred_score_within_95CI = ((true_parameters_inferred_scores>=2.5) &
(true_parameters_inferred_scores<=97.5)
)
return true_parameters_inferred_score_within_95CI
def plot_time_series_inference(model_fit, var='Y_latent', x=None,
ax=None, ind=0, D=1, **kwargs):
from scipy.stats import scoreatpercentile
ci_thresholds = [2.5, 25, 50, 75, 97.5]
data = model_fit[var].squeeze()
if data.ndim==3:
data = data[:,ind,:]
elif data.ndim>3:
data = data[:,ind,:,D]
CIs = scoreatpercentile(data, ci_thresholds, axis=0)
CIs = pd.DataFrame(data=CIs.T, columns=ci_thresholds)
if ax is None:
ax=gca()
if x is None:
x = arange(data.shape[1])
ax.fill_between(x, CIs[2.5], CIs[97.5],alpha=.5, **kwargs)
ax.fill_between(x, CIs[25], CIs[75], **kwargs)
ax.plot(x, CIs[50], **kwargs)
def check_div(fit, parameters):
div = concatenate([s['divergent__'] for s in fit.get_sampler_params(inc_warmup=False)]).astype('bool')
if sum(div==0):
print("\x1b[32m\"No divergences\"\x1b[0m")
else:
###ndtest from https://github.com/syrte/ndtest
from ndtest import ks2d2s
divergences = {}
non_divergences = {}
for parameter in parameters:
divergences[parameter] = fit[parameter][div].squeeze()
non_divergences[parameter] = fit[parameter][~div].squeeze()
if divergences[parameter].ndim>2:
N = divergences[parameter].shape[3]
for n in arange(N):
divergences[parameter+'.%i'%n] = divergences[parameter][:,:,n]
non_divergences[parameter+'.%i'%n] = non_divergences[parameter][:,:,n]
del divergences[parameter]
del non_divergences[parameter]
any_unevenly_distributed = False
for k1 in divergences.keys():
for k2 in divergences.keys():
if k1==k2:
continue
x = divergences[k1].ravel()
y = divergences[k2].ravel()
x_non = non_divergences[k1].ravel()
y_non = non_divergences[k2].ravel()
p = ks2d2s(x_non, y_non, x, y)
if p<.05:
any_unevenly_distributed = True
figure()
scatter(x_non, y_non,
alpha=.1, label='Non-Divergent')
scatter(x,y,
alpha=1, label='Divergent')
xlabel(k1)
ylabel(k2)
legend()
title("KS test p=%.2f"%(p))
if any_unevenly_distributed:
print("\x1b[31m\"%.2f divergences, which appear to be non-spurious\"\x1b[0m"%(div.mean()))
else:
print("\x1b[32m\"%.2f divergences, which appear to be spurious\"\x1b[0m"%(div.mean()))
from pystan.misc import _summary
import stan_utility
def test_model_fit(fit, parameters, max_depth=10):
if type(parameters[0])==tuple:
fit_params = []
for data_param, fit_param in parameters:
print(fit_param)
if hasattr(data_param, '__len__') and len(data_param)!=fit[fit_param].shape[1]:
inds = len(data_param)
within_95 = 0.0
for i in range(inds):
within_95 += parameter_within_95(fit, data_param, fit_param, ind=i)
within_95 /= inds
else:
within_95 = parameter_within_95(fit, data_param, fit_param)
if within_95.mean()>.9:
c = '32'
else:
c = '31'
print("\x1b[%sm\"%.0f%% of values recovered\"\x1b[0m"%(c, within_95.mean()*100))
Rhats = _summary(fit, pars=fit_param)['summary'][:,-1]
if all(abs(Rhats-1)<.1):
c = '32'
else:
c = '31'
print("\x1b[%sm\"Maximum Rhat of %.2f\"\x1b[0m"%(c,max(Rhats)))
fit_params.append(fit_param)
stan_utility.check_treedepth(fit,max_depth=max_depth)
stan_utility.check_energy(fit)
check_div(fit, fit_param)
from time import time
def plot_distribution(data, **kwargs):
from scipy.stats import scoreatpercentile
from bisect import bisect_left
p = sns.kdeplot(data, **kwargs)
p = p.get_lines()[-1]
x,y = p.get_data()
c = p.get_color()
lower = scoreatpercentile(data, 2.5)
upper = scoreatpercentile(data, 97.5)
lower_ind = bisect_left(x,lower)
upper_ind = bisect_left(x,upper)
fill_between(x[lower_ind:upper_ind], y[lower_ind:upper_ind], alpha=.4, color=c)
return
# ARMA model
# ===
# Normally distributed shocks around a constant level
# ---
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# In[4]:
### Simply noise
n = 20
t = 100
sigma = 1
mu = 4
Y = (randn(t,n)*sigma)+mu
Y[:5] = nan #Some data is missing. We can model it!
Y[20] = nan
Y[-5:] = nan
model = TimeSeriesModel(Y=Y)
start_time = time()
max_depth = 15
model.sampling(n_jobs=n_jobs, iter=n_iterations, control={'max_treedepth':max_depth})
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma')]
test_model_fit(model.fit, parameter_pairs, max_depth=max_depth)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Show priors and how they update to posteriors
# In[5]:
### Show priors and how they update to posteriors
model_priors = TimeSeriesModel(Y=Y, return_priors=True)
start_time = time()
model_priors.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
plot_distribution(model_priors.fit['mu'][:,0,0], label='Prior')
plot_distribution(model.fit['mu'][:,0,0], label='Posterior')
plot((mu,mu), (0,ylim()[1]*.5), 'k', label='True Value', linewidth=1)
legend()
title(r"$\mu$")
figure()
plot_distribution(model_priors.fit['sigma'][:,0,0], label='Prior')
plot_distribution(model.fit['sigma'][:,0,0], label='Posterior')
plot((sigma,sigma), (0,ylim()[1]*.5), 'k', label='True Value', linewidth=1)
xlim(xmin=0)
xscale('symlog')
legend()
title(r"$\sigma$")
# Add an autoregressive component
# ---
# (An AR(1) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t + \phi_1 Y_{t-1}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\phi \sim normal(0,4)$
# In[6]:
phi = array([.5])
p = len(phi)
Y = (randn(t,n)*sigma)+mu
for i in range(1+p,t):
Y[i] += dot(phi,Y[i-p:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, p=p)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (phi, 'phi')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Add a second-order autoregressive component
# ---
# (An AR(2) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t + \phi_1 Y_{t-1} + \phi_2 Y_{t-2}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\phi \sim normal(0,4)$
# In[7]:
phi = array([.5, -.5])
p = len(phi)
Y = (randn(t,n)*sigma)+mu
for i in range(1+p,t):
Y[i] += dot(phi[::-1],Y[i-p:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, p=p)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (phi, 'phi')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Normally distributed shocks, with a moving average component
# ---
# (An MA(1) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t + \theta \epsilon_{t-1}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\theta \sim normal(0,4)$
# In[8]:
theta = array([.1])
q = len(theta)
errs = (randn(t,n)*sigma)
Y = mu+errs
for i in range(1+q,t):
Y[i] += dot(theta[::-1],errs[i-q:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, q=q)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Add a second-order moving average component
# ---
# (An MA(2) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t + \theta_1 \epsilon_{t-1} + \theta_2 \epsilon_{t-2}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\theta \sim normal(0,4)$
# In[9]:
theta = array([.7, .1])
q = len(theta)
errs = (randn(t,n)*sigma)
Y = mu+errs
for i in range(1+q,t):
Y[i] += dot(theta[::-1],errs[i-q:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, q=q)
start_time = time()
max_depth = 15
model.sampling(n_jobs=n_jobs, iter=4*n_iterations, control={'max_treedepth':max_depth})
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs, max_depth=max_depth)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Both autoregressive and moving average components
# ----
# (An ARMA(2,2) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t \sim \mu + \epsilon_t + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + \theta_1 \epsilon_{t-1} + \theta_2 \epsilon_{t-2}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\phi \sim normal(0,4)$
# - $\theta \sim normal(0,4)$
# In[18]:
phi = array([.8, -.2])
p = len(phi)
theta = array([.4,.1])
q = len(theta)
errs = (randn(t,n)*sigma)
Y = mu+errs
for i in range(1+max(p,q),t):
Y[i] += dot(phi[::-1],Y[i-p:i]) + dot(theta,errs[i-q:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, p=p, q=q)
start_time = time()
max_depth = 15
model.sampling(n_jobs=n_jobs, iter=4*n_iterations, control={'max_treedepth':max_depth})
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (phi, 'phi'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs, max_depth=max_depth)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Changes have normally distributed shocks, with a moving average component
# ---
# (An IMA(1,1) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t-Y_{t-1} \sim \mu + \epsilon_t + \theta \epsilon_{t-1}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\theta \sim normal(0,4)$
# In[11]:
theta = array([.1])
q = len(theta)
errs = (randn(t,n)*sigma)
Y = mu+errs
for i in range(1+q,t):
Y[i] += dot(theta[::-1],errs[i-q:i])
Y = cumsum(Y, axis=0)
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, q=q, difference=[1])
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Require changes are positive
# ---
# (A monotonically-increasing IMA(1,1) model)
#
# $\epsilon_t \sim normal(0, \sigma)$
#
# $Y_t-Y_{t-1} \sim \mu + \epsilon_t + \theta \epsilon_{t-1}$
#
# $Y_t-Y_{t-1} > 0$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\theta \sim normal(0,4)$
# In[12]:
theta = array([.1])
q = len(theta)
from scipy.stats import truncnorm
Y = mu*ones((t,n))
errs = zeros((t,n))
for n_i in range(n):
for t_i in range(1,q):
expected_level = mu+Y[t_i-1,n_i]
Y[t_i,n_i] = truncnorm(-Y[t_i-1,n_i], inf, expected_level, sigma).rvs()
errs[t_i,n_i] = Y[t_i,n_i] - expected_level
for t_i in range(q,t):
expected_level = mu+dot(theta[::-1],errs[t_i-q:t_i,n_i])+Y[t_i-1,n_i]
Y[t_i,n_i] = truncnorm(-Y[t_i-1,n_i], inf, expected_level, sigma).rvs()
errs[t_i,n_i] = Y[t_i,n_i] - expected_level
Y[20:22] = nan
model = TimeSeriesModel(Y=Y, q=q, difference=[1], monotonic=[1])
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Make shocks t-distributed
# ---
# (A IMA(1,1) model, but with t-distributed shocks)
#
# $\epsilon_t \sim t(\nu, 0, \sigma)$
#
# $Y_t-Y_{t-1} \sim \mu + \epsilon_t + \theta \epsilon_{t-1}$
#
# Priors:
# - $\mu \sim normal(0,4)$
# - $\sigma \sim cauchy(0,4)$
# - $\theta \sim normal(0,4)$
# - $\nu \sim caucy(0,4)$
# In[13]:
nu = 3
theta = array([.1])
q = len(theta)
errs = (standard_t(nu, (t,n))*sigma)
Y = mu+errs
for i in range(1+q,t):
Y[i] += dot(theta[::-1],errs[i-q:i])
Y = cumsum(Y, axis=0)
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, q=q, difference=[1], use_student=True)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(nu, 'nu'), (mu, 'mu'), (sigma, 'sigma'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Combine inference of time series' parameters by partially pooling
# ---
# (An MA(1) model, with partial pooling of the estimation of $\mu$, $\sigma$ and $\theta$)
#
# $Y_{i,t} \sim \mu_i + \epsilon_t + \theta_i \epsilon_{t-1}$
#
# $\epsilon_t \sim \text{normal}(0, \sigma_i)$
#
# $[\mu_i, \sigma_i, \theta_i] \sim [\hat{\mu}, \hat{\sigma}, \hat{\theta}] + \text{multinormal}(0,\text{diag}(\tau)*\Omega*\text{diag}(\tau))$
#
#
# Priors:
# - $\hat{\mu} \sim normal(0,4)$
# - $\hat{\sigma} \sim cauchy(0,4)$
# - $\hat{\theta} \sim normal(0,4)$
# - $\tau \sim cauchy(0,1)$ (How much each parameter varies across the time series)
# - $\Omega \sim LKJ(1)$ (How the parameters correlate with each other across the time series)
# In[14]:
mu_hat = 4
sigma_hat = 1
theta_hat = .1
Omega = matrix([[1,.5,0,],
[.5,1,0,],
[0,0,1,]])
tau = array([1,1,1])
cov = diag(tau)*Omega*diag(tau)
from scipy.special import logit, expit
parameters = multivariate_normal(array([mu_hat,
log(sigma_hat),
logit((theta_hat+1)/2)]),
cov=cov,
size=n)
mu = parameters[:,0]
sigma = exp(parameters[:,1])
theta = expit(parameters[:,2])*2-1
q = 1
Y = zeros((t,n))
for n_ind in arange(n):
errs = randn(t)*sigma[n_ind]
Y[:,n_ind] = mu[n_ind]+errs
for i in range(1+q,t):
Y[i,n_ind] += (theta[n_ind]*errs[i-q:i])
Y[20:25] = nan
model = TimeSeriesModel(Y=Y, q=q, use_partial_pooling=True)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (theta, 'theta'), (tau, 'tau')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)
# Time series is multi-dimensional, and the different dimensions can influence each other
# ---
# (A VAR model, with a MA(1) component)
#
#
# $\vec{Y}_{t} = [Y_{1,t}, Y_{2,t}, Y_{3,t}...Y_{D,t}]$
#
# $\vec{Y}_{t} \sim \vec{\mu} + \vec{\epsilon}_{t} + \vec{\theta} \vec{\epsilon}_{t-1} + \mathbf{P}\vec{Y}_{t-1}$
#
# $\vec{\epsilon}_t \sim \text{normal}(0, \vec{\sigma})$
#
#
# where $\mathbf{P}$ is a $D x D$ matrix
#
#
# Priors (for each element in the vector or matrix):
# - $\vec{\mu} \sim normal(0,4)$
# - $\vec{\sigma} \sim cauchy(0,4)$
# - $\mathbf{P} \sim normal(0,4)$
# - $\vec{\theta} \sim normal(0,4)$
# In[15]:
D = 3
mu = rand(D)
sigma = rand(D)
p = 1
phi = .1*rand(p,D,D)
theta = array([.2])
q = len(theta)
Y = zeros((n,t,D))
errs = (randn(n,t,D)*sigma)
for n_i in range(n):
for t_i in range(max(q,p),t):
Y[n_i,t_i] += mu+dot(theta[::-1],errs[n_i,t_i-q:t_i])+errs[n_i,t_i]
for p_i in range(p):
Y[n_i,t_i] += dot(phi[p_i],Y[n_i,t_i-p_i])
Y[:,20:25] = nan
model = TimeSeriesModel(Y=Y, p=p, q=q)
start_time = time()
model.sampling(n_jobs=n_jobs, iter=2000)#n_iterations)
finish_time = time()
print("Fitting took %.2f minutes"%((finish_time-start_time)/60))
parameter_pairs = [(mu, 'mu'), (sigma, 'sigma'), (phi, 'phi'), (theta, 'theta')]
test_model_fit(model.fit, parameter_pairs)
# print(model.fit)
for i in range(min(5,n)):
figure()
plot_time_series_inference(model.fit, ind=i)