1
1
import numpy as np
2
- from pymc3 import Model , Normal
2
+ from pymc3 import Model , Normal , HalfNormal , DiscreteUniform , Poisson , switch , Exponential
3
3
from pymc3 .theanof import inputvars
4
- from pymc3 .variational .advi import variational_gradient_estimate
4
+ from pymc3 .variational .advi import variational_gradient_estimate , advi , advi_minibatch
5
5
from theano import function
6
+ import theano .tensor as tt
6
7
7
- def test_advi ():
8
+ from nose .tools import assert_raises
9
+
10
+ def test_elbo ():
8
11
mu0 = 1.5
9
12
sigma = 1.0
10
13
y_obs = np .array ([1.6 , 1.4 ])
@@ -18,7 +21,7 @@ def test_advi():
18
21
19
22
# Create variational gradient tensor
20
23
grad , elbo , shared , uw = variational_gradient_estimate (
21
- vars , model , n_mcsamples = 1000000 )
24
+ vars , model , n_mcsamples = 10000 )
22
25
23
26
# Variational posterior parameters
24
27
uw_ = np .array ([1.88 , np .log (1 )])
@@ -34,3 +37,57 @@ def test_advi():
34
37
0.5 * (np .log (2 * np .pi ) + 1 ))
35
38
36
39
np .testing .assert_allclose (elbo_mc , elbo_true , rtol = 0 , atol = 1e-1 )
40
+
41
+ disaster_data = np .ma .masked_values ([4 , 5 , 4 , 0 , 1 , 4 , 3 , 4 , 0 , 6 , 3 , 3 , 4 , 0 , 2 , 6 ,
42
+ 3 , 3 , 5 , 4 , 5 , 3 , 1 , 4 , 4 , 1 , 5 , 5 , 3 , 4 , 2 , 5 ,
43
+ 2 , 2 , 3 , 4 , 2 , 1 , 3 , - 999 , 2 , 1 , 1 , 1 , 1 , 3 , 0 , 0 ,
44
+ 1 , 0 , 1 , 1 , 0 , 0 , 3 , 1 , 0 , 3 , 2 , 2 , 0 , 1 , 1 , 1 ,
45
+ 0 , 1 , 0 , 1 , 0 , 0 , 0 , 2 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 2 ,
46
+ 3 , 3 , 1 , - 999 , 2 , 1 , 1 , 1 , 1 , 2 , 4 , 2 , 0 , 0 , 1 , 4 ,
47
+ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 ], value = - 999 )
48
+ year = np .arange (1851 , 1962 )
49
+
50
+ def test_check_discrete ():
51
+ with Model () as disaster_model :
52
+ switchpoint = DiscreteUniform ('switchpoint' , lower = year .min (), upper = year .max (), testval = 1900 )
53
+
54
+ # Priors for pre- and post-switch rates number of disasters
55
+ early_rate = Exponential ('early_rate' , 1 )
56
+ late_rate = Exponential ('late_rate' , 1 )
57
+
58
+ # Allocate appropriate Poisson rates to years before and after current
59
+ rate = switch (switchpoint >= year , early_rate , late_rate )
60
+
61
+ disasters = Poisson ('disasters' , rate , observed = disaster_data )
62
+
63
+ # This should raise ValueError
64
+ assert_raises (ValueError , advi , model = disaster_model , n = 10 )
65
+
66
+ def test_check_discrete_minibatch ():
67
+ disaster_data_t = tt .vector ()
68
+ disaster_data_t .tag .test_value = np .zeros (len (disaster_data ))
69
+
70
+ with Model () as disaster_model :
71
+
72
+ switchpoint = DiscreteUniform (
73
+ 'switchpoint' , lower = year .min (), upper = year .max (), testval = 1900 )
74
+
75
+ # Priors for pre- and post-switch rates number of disasters
76
+ early_rate = Exponential ('early_rate' , 1 )
77
+ late_rate = Exponential ('late_rate' , 1 )
78
+
79
+ # Allocate appropriate Poisson rates to years before and after current
80
+ rate = switch (switchpoint >= year , early_rate , late_rate )
81
+
82
+ disasters = Poisson ('disasters' , rate , observed = disaster_data_t )
83
+
84
+ def create_minibatch ():
85
+ while True :
86
+ return disaster_data
87
+
88
+ # This should raise ValueError
89
+ assert_raises (
90
+ ValueError , advi_minibatch , model = disaster_model , n = 10 ,
91
+ minibatch_RVs = [disasters ], minibatch_tensors = [disaster_data_t ],
92
+ minibatches = [create_minibatch ()], verbose = False )
93
+
0 commit comments