-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictive_distributions.py
235 lines (173 loc) · 5.96 KB
/
predictive_distributions.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
from __future__ import division
import numpy as np
na = np.newaxis
import abc
from util.stats import sample_mniw, sample_invwishart
'''predictive samplers for basic distributions'''
class PredictiveDistribution(object):
__metaclass__ = abc.ABCMeta
def sample_next(self,*args,**kwargs):
val = self._sample(*args,**kwargs)
self._update_hypparams(val)
return val
@abc.abstractmethod
def copy(self):
pass
@abc.abstractmethod
def _update_hypparams(self,x):
pass
@abc.abstractmethod
def _sample(self):
pass
###############
# Durations #
###############
class Poisson(PredictiveDistribution):
def __init__(self,alpha_0,beta_0):
self.alpha_n = alpha_0
self.beta_n = beta_0
def _update_hypparams(self,k):
self.alpha_n += k
self.beta_n += 1
def _sample(self):
return np.random.poisson(np.random.gamma(self.alpha_n,1./self.beta_n))+1
def copy(self):
return Poisson(self.alpha_n,self.beta_n)
class NegativeBinomial(PredictiveDistribution): # TODO
pass
##################
# Observations #
##################
class FixedNoiseDiagonal(PredictiveDistribution):
def __init__(self,variances):
self.scales = np.sqrt(variances)
def _update_hypparams(self,y):
pass
def _sample(self):
return self.scales*np.random.randn(self.scales.shape[0])
def copy(self):
return self
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,self.weights)
def __repr__(self):
return str(self)
class FixedNoise(PredictiveDistribution):
def __init__(self,noisechol):
self.noisechol = noisechol
def _update_hypparams(self,y):
pass
def _sample(self):
return self.noisechol.dot(np.random.randn(self.noisechol.shape[0]))
def copy(self):
return self
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,self.noisechol)
def __repr__(self):
return str(self)
class InverseWishartNoise(PredictiveDistribution):
def __init__(self,n_0,S_0):
self.S_0 = S_0
self.n_n = n_0
self.yyt = np.zeros(S_0.shape)
def _update_hypparams(self,y):
self.n_n += 1
self.yyt += y[:,na] * y
def _sample(self):
Sigma = sample_invwishart(self.S_0 + self.yyt,self.n_n)
return np.linalg.cholesky(Sigma).dot(np.random.randn(Sigma.shape[0]))
def copy(self):
new = self.__new__(self.__class__)
new.n_n = self.n_n
new.S_0 = self.S_0
new.yyt = self.yyt.copy()
return new
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,(self.n_n,self.S_0 + self.yyt))
def __repr__(self):
return str(self)
class MNIWAR(PredictiveDistribution):
'''Conjugate Matrix-Normal-Inverse-Wishart prior'''
def __init__(self,n_0,sigma_0,M,K):
# hyperparameters
self.n = n_0
self.sigma_0 = sigma_0
self.M_n = M
self.K_n = K
self.sigma_n = sigma_0.copy()
# statistics
self.Sytyt = K.copy()
self.Syyt = M.dot(K)
self.Syy = M.dot(K).dot(M.T)
# temporary variables to cut down on mallocs
self.Sy_yt = np.empty(self.sigma_0.shape)
self._ylags = np.zeros(self.M_n.shape[1])
# error handling
self._broken = False
def _update_hypparams(self,y):
ylags = self._ylags # gets info passed from previous _sample call, state!
self.Syy += y[:,na] * y
self.Sytyt += ylags[:,na] * ylags
self.Syyt += y[:,na] * ylags
M_n = np.linalg.solve(self.Sytyt,self.Syyt.T).T
np.dot(-M_n,self.Syyt.T,out=self.Sy_yt)
self.Sy_yt += self.Syy
self.n += 1
np.add(self.Sy_yt,self.sigma_0,out=self.sigma_n)
self.M_n = M_n
self.K_n = self.Sytyt
try:
pass
# assert np.allclose(self.sigma_n,self.sigma_n.T) and (np.linalg.eigvals(self.sigma_n) > 0).all()
# assert np.allclose(self.K_n,self.K_n.T) and (np.linalg.eigvals(self.K_n) > 0).all()
except AssertionError:
print 'WARNING: particle exploded'
self._broken = True
def _sample(self,lagged_outputs):
if not self._broken:
try:
ylags = self._pad_ylags(lagged_outputs)
A,sigma = sample_mniw(self.n,self.sigma_n,self.M_n,np.linalg.inv(self.K_n))
return A.dot(ylags) + np.linalg.cholesky(sigma).dot(np.random.randn(sigma.shape[0]))
except np.linalg.LinAlgError:
print 'WARNING: particle broke'
self._broken = True
return -99999*np.ones(self.M_n.shape[0])
def _pad_ylags(self,lagged_outputs):
ylags = self._ylags
ylags[...] = 0
# plug in lagged data
temp = np.array(lagged_outputs)
temp.shape = (-1,)
ylags[:temp.shape[0]] = temp
# plug in affine drift
ylags[-1] = 1
return ylags
def copy(self):
new = self.__new__(self.__class__)
new.n = self.n
new.sigma_0 = self.sigma_0
new.sigma_n = self.sigma_n.copy()
new.M_n = self.M_n
new.K_n = self.K_n
new.Sytyt = self.Sytyt.copy()
new.Syyt = self.Syyt.copy()
new.Syy = self.Syy.copy()
new.Sy_yt = self.Sy_yt.copy()
new._ylags = self._ylags.copy()
new._broken = self._broken
return new
def __str__(self):
return '\n'.join(map(str,sample_mniw(self.n,self.sigma_n,self.M_n,np.linalg.inv(self.K_n))))
def __repr__(self):
return str(self)
class NIWNonConjAR(PredictiveDistribution): # TODO
# Gibbs steps on copy
# normal, normal, inverse wishart
# should use an IW class and a Gaussian class that do blocked gibbs
# like pyhsmm distribution classes but which take stats, not data, and only
# need to draw samples
pass
class _InvWishartCov(object):
pass
class _Gaussian(object):
pass