@@ -193,6 +193,55 @@ def logp(self, x):
193
193
T .all (x >= 0 ), T .all (x <= n ), T .eq (T .sum (x ), n ),
194
194
n >= 0 )
195
195
196
+ def posdef (AA ):
197
+ try :
198
+ fct = np .linalg .cholesky (AA )
199
+ return 1
200
+ except np .linalg .LinAlgError :
201
+ return 0
202
+
203
+
204
+ class PosDefMatrix (theano .Op ):
205
+ """
206
+ Check if input is positive definite. Input should be a square matrix.
207
+
208
+ """
209
+
210
+ #Properties attribute
211
+ __props__ = ()
212
+
213
+
214
+ #Compulsory if itypes and otypes are not defined
215
+
216
+ def make_node (self , x ):
217
+ x = theano .tensor .as_tensor_variable (x )
218
+ assert x .ndim == 2
219
+ o = T .TensorType (dtype = 'int8' , broadcastable = [])()
220
+ return theano .Apply (self , [x ], [o ])
221
+
222
+ # Python implementation:
223
+ def perform (self , node , inputs , outputs ):
224
+
225
+ (x ,) = inputs
226
+ (z ,) = outputs
227
+ try :
228
+ z [0 ] = np .array (posdef (x ), dtype = 'int8' )
229
+ except Exception :
230
+ print ('Failed to check if positive definite' , x )
231
+ raise
232
+
233
+ def infer_shape (self , node , shapes ):
234
+ return [[]]
235
+
236
+ def grad (self , inp , grads ):
237
+ x , = inp
238
+ return [x .zeros_like (theano .config .floatX )]
239
+
240
+ def __str__ (self ):
241
+ return "MatrixIsPositiveDefinite"
242
+
243
+ matrix_pos_def = PosDefMatrix ()
244
+
196
245
197
246
class Wishart (Continuous ):
198
247
r"""
@@ -256,59 +305,11 @@ def logp(self, X):
256
305
- trace (matrix_inverse (V ).dot (X ))
257
306
- n * p * T .log (2 ) - n * T .log (IVI )
258
307
- 2 * multigammaln (n / 2. , p )) / 2 ,
259
- T .all (eigh (X )[0 ] > 0 ), T .eq (X , X .T ),
308
+ matrix_pos_def (X ),
309
+ T .eq (X , X .T ),
260
310
n > (p - 1 ))
261
311
262
312
263
- def posdef (AA ):
264
- try :
265
- fct = np .linalg .cholesky (AA )
266
- return 1
267
- except np .linalg .LinAlgError :
268
- return 0
269
-
270
-
271
- class PosDefMatrix (theano .Op ):
272
- """
273
- Check if input is positive definite. Input should be a square matrix.
274
-
275
- """
276
-
277
- #Properties attribute
278
- __props__ = ()
279
-
280
-
281
- #Compulsory if itypes and otypes are not defined
282
-
283
- def make_node (self , x ):
284
- x = theano .tensor .as_tensor_variable (x )
285
- assert x .ndim == 2
286
- o = T .TensorType (dtype = 'int8' , broadcastable = [])()
287
- return theano .Apply (self , [x ], [o ])
288
-
289
- # Python implementation:
290
- def perform (self , node , inputs , outputs ):
291
-
292
- (x ,) = inputs
293
- (z ,) = outputs
294
- try :
295
- z [0 ] = np .array (posdef (x ), dtype = 'int8' )
296
- except Exception :
297
- print ('Failed to check if positive definite' , x )
298
- raise
299
-
300
- def infer_shape (self , node , shapes ):
301
- return [[]]
302
-
303
- def grad (self , inp , grads ):
304
- x , = inp
305
- return [x .zeros_like (theano .config .floatX )]
306
-
307
- def __str__ (self ):
308
- return "MatrixIsPositiveDefinite"
309
-
310
- matrix_pos_def = PosDefMatrix ()
311
-
312
313
313
314
class LKJCorr (Continuous ):
314
315
r"""
0 commit comments