Skip to content

Commit 4c002fd

Browse files
committed
Merge pull request #968 from springcoil/lightspeed
ENH: Timeseries GARCH and Lightspeed model
2 parents 5853344 + f127fe6 commit 4c002fd

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

pymc3/examples/garch_example.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from pymc3 import Normal, sample, Model, Bound
2-
import theano.tensor as tt
2+
import theano.tensor as T
33
import numpy as np
4-
import math
4+
55
"""
6-
Example from STAN
6+
Example from STAN - slightly altered
77
// GARCH(1,1)
88
99
data {
@@ -29,27 +29,28 @@
2929
r ~ normal(mu,sigma);
3030
}
3131
"""
32-
T = 8
33-
r = np.array([28, 8, -3, 7, -1, 1, 18, 12])
34-
sigma1 = np.array([15, 10, 16, 11, 9, 11, 10, 18])
35-
alpha0 = np.array([10, 10, 16, 8, 9, 11, 12, 18])
32+
J = 8
33+
r = np.array([28, 8, -3, 7, -1, 1, 18, 12])
34+
sigma1 = np.array([15, 10, 16, 11, 9, 11, 10, 18])
35+
alpha0 = np.array([10, 10, 16, 8, 9, 11, 12, 18])
3636

3737
with Model() as garch:
38-
39-
alpha1 = Normal('alpha1', 0, 1, shape=T)
40-
BoundedNormal = Bound(Normal, upper=(1-alpha1))
38+
alpha1 = Normal('alpha1', 0, 1, shape=J)
39+
BoundedNormal = Bound(Normal, upper=(1 - alpha1))
4140
beta1 = BoundedNormal('beta1', 0, sd=1e6)
4241
mu = Normal('mu', 0, sd=1e6)
4342

44-
theta = tt.sqrt(alpha0 + alpha1*tt.pow(r - mu, 2) + beta1 * tt.pow(sigma1, 2))
43+
theta = T.sqrt(alpha0 + alpha1 * T.pow(r - mu, 2) + beta1 * T.pow(sigma1, 2))
4544

4645
obs = Normal('obs', mu, sd=theta, observed=r)
4746

47+
4848
def run(n=1000):
4949
if n == "short":
5050
n = 50
5151
with garch:
5252
tr = sample(n)
5353

54+
5455
if __name__ == '__main__':
55-
run()
56+
run()

pymc3/examples/lightspeed_example.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
import pymc3 as pm
3+
4+
light_speed = np.array([28, 26, 33, 24, 34, -44, 27, 16, 40, -2, 29, 22, 24, 21, 25,
5+
30, 23, 29, 31, 19, 24, 20, 36, 32, 36, 28, 25, 21, 28, 29,
6+
37, 25, 28, 26, 30, 32, 36, 26, 30, 22, 36, 23, 27, 27, 28,
7+
27, 31, 27, 26, 33, 26, 32, 32, 24, 39, 28, 24, 25, 32, 25,
8+
29, 27, 28, 29, 16, 23])
9+
10+
model_1 = pm.Model()
11+
12+
with model_1:
13+
# priors as specified in stan model
14+
# mu = pm.Uniform('mu', lower = -tt.inf, upper= np.inf)
15+
# sigma = pm.Uniform('sigma', lower = 0, upper= np.inf)
16+
17+
18+
# using vague priors works
19+
mu = pm.Uniform('mu', lower=light_speed.std() / 1000.0, upper=light_speed.std() * 1000.0)
20+
sigma = pm.Uniform('sigma', lower=light_speed.std() / 1000.0, upper=light_speed.std() * 1000.0)
21+
22+
# define likelihood
23+
y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=light_speed)
24+
25+
# inference fitting the model
26+
27+
# I have to use slice because the following command
28+
# trace = pm.sample(5000)
29+
# produce the error
30+
# ValueError: Cannot construct a ufunc with more than 32 operands
31+
# (requested number were: inputs = 51 and outputs = 1)valueerror
32+
33+
34+
def run(n=5000):
35+
with model_1:
36+
xstart = pm.find_MAP()
37+
xstep = pm.Slice()
38+
trace = pm.sample(5000, xstep, xstart, random_seed=123, progressbar=True)
39+
40+
pm.summary(trace)
41+
42+
43+
if __name__ == '__main__':
44+
run()

0 commit comments

Comments
 (0)