5
5
from pymc3 .gp .cov import Covariance , Constant
6
6
from pymc3 .gp .mean import Zero
7
7
from pymc3 .gp .util import (conditioned_vars ,
8
- infer_shape , stabilize , cholesky , solve_lower , solve_upper )
8
+ infer_shape , stabilize , cholesky , solve_lower , solve_upper )
9
9
from pymc3 .distributions import draw_values
10
10
11
11
__all__ = ['Latent' , 'Marginal' , 'TP' , 'MarginalSparse' ]
@@ -113,7 +113,7 @@ def _build_prior(self, name, X, reparameterize=True, **kwargs):
113
113
114
114
def prior (self , name , X , reparameterize = True , ** kwargs ):
115
115
R"""
116
- Returns the GP prior distribution evaluated over the input
116
+ Returns the GP prior distribution evaluated over the input
117
117
locations `X`.
118
118
119
119
This is the prior probability over the space
@@ -142,6 +142,8 @@ def prior(self, name, X, reparameterize=True, **kwargs):
142
142
return f
143
143
144
144
def _get_given_vals (self , given ):
145
+ if given is None :
146
+ given = {}
145
147
if 'gp' in given :
146
148
cov_total = given ['gp' ].cov_func
147
149
mean_total = given ['gp' ].mean_func
@@ -165,9 +167,9 @@ def _build_conditional(self, Xnew, X, f, cov_total, mean_total):
165
167
cov = Kss - tt .dot (tt .transpose (A ), A )
166
168
return mu , cov
167
169
168
- def conditional (self , name , Xnew , given = {} , ** kwargs ):
170
+ def conditional (self , name , Xnew , given = None , ** kwargs ):
169
171
R"""
170
- Returns the conditional distribution evaluated over new input
172
+ Returns the conditional distribution evaluated over new input
171
173
locations `Xnew`.
172
174
173
175
Given a set of function values `f` that
@@ -194,7 +196,6 @@ def conditional(self, name, Xnew, given={}, **kwargs):
194
196
Extra keyword arguments that are passed to `MvNormal` distribution
195
197
constructor.
196
198
"""
197
-
198
199
givens = self ._get_given_vals (given )
199
200
mu , cov = self ._build_conditional (Xnew , * givens )
200
201
chol = cholesky (stabilize (cov ))
@@ -255,7 +256,7 @@ def _build_prior(self, name, X, reparameterize=True, **kwargs):
255
256
256
257
def prior (self , name , X , reparameterize = True , ** kwargs ):
257
258
R"""
258
- Returns the TP prior distribution evaluated over the input
259
+ Returns the TP prior distribution evaluated over the input
259
260
locations `X`.
260
261
261
262
This is the prior probability over the space
@@ -295,7 +296,7 @@ def _build_conditional(self, Xnew, X, f):
295
296
296
297
def conditional (self , name , Xnew , ** kwargs ):
297
298
R"""
298
- Returns the conditional distribution evaluated over new input
299
+ Returns the conditional distribution evaluated over new input
299
300
locations `Xnew`.
300
301
301
302
Given a set of function values `f` that
@@ -379,7 +380,7 @@ def _build_marginal_likelihood(self, X, noise):
379
380
380
381
def marginal_likelihood (self , name , X , y , noise , is_observed = True , ** kwargs ):
381
382
R"""
382
- Returns the marginal likelihood distribution, given the input
383
+ Returns the marginal likelihood distribution, given the input
383
384
locations `X` and the data `y`.
384
385
385
386
This is integral over the product of the GP prior and a normal likelihood.
@@ -423,6 +424,9 @@ def marginal_likelihood(self, name, X, y, noise, is_observed=True, **kwargs):
423
424
return pm .MvNormal (name , mu = mu , chol = chol , shape = shape , ** kwargs )
424
425
425
426
def _get_given_vals (self , given ):
427
+ if given is None :
428
+ given = {}
429
+
426
430
if 'gp' in given :
427
431
cov_total = given ['gp' ].cov_func
428
432
mean_total = given ['gp' ].mean_func
@@ -460,9 +464,9 @@ def _build_conditional(self, Xnew, pred_noise, diag, X, y, noise,
460
464
cov += noise (Xnew )
461
465
return mu , stabilize (cov )
462
466
463
- def conditional (self , name , Xnew , pred_noise = False , given = {} , ** kwargs ):
467
+ def conditional (self , name , Xnew , pred_noise = False , given = None , ** kwargs ):
464
468
R"""
465
- Returns the conditional distribution evaluated over new input
469
+ Returns the conditional distribution evaluated over new input
466
470
locations `Xnew`.
467
471
468
472
Given a set of function values `f` that the GP prior was over, the
@@ -499,7 +503,7 @@ def conditional(self, name, Xnew, pred_noise=False, given={}, **kwargs):
499
503
shape = infer_shape (Xnew , kwargs .pop ("shape" , None ))
500
504
return pm .MvNormal (name , mu = mu , chol = chol , shape = shape , ** kwargs )
501
505
502
- def predict (self , Xnew , point = None , diag = False , pred_noise = False , given = {} ):
506
+ def predict (self , Xnew , point = None , diag = False , pred_noise = False , given = None ):
503
507
R"""
504
508
Return the mean vector and covariance matrix of the conditional
505
509
distribution as numpy arrays, given a `point`, such as the MAP
@@ -521,11 +525,13 @@ def predict(self, Xnew, point=None, diag=False, pred_noise=False, given={}):
521
525
given : dict
522
526
Same as `conditional` method.
523
527
"""
528
+ if given is None :
529
+ given = {}
524
530
525
531
mu , cov = self .predictt (Xnew , diag , pred_noise , given )
526
532
return draw_values ([mu , cov ], point = point )
527
533
528
- def predictt (self , Xnew , diag = False , pred_noise = False , given = {} ):
534
+ def predictt (self , Xnew , diag = False , pred_noise = False , given = None ):
529
535
R"""
530
536
Return the mean vector and covariance matrix of the conditional
531
537
distribution as symbolic variables.
@@ -544,7 +550,6 @@ def predictt(self, Xnew, diag=False, pred_noise=False, given={}):
544
550
given : dict
545
551
Same as `conditional` method.
546
552
"""
547
-
548
553
givens = self ._get_given_vals (given )
549
554
mu , cov = self ._build_conditional (Xnew , pred_noise , diag , * givens )
550
555
return mu , cov
@@ -646,7 +651,7 @@ def _build_marginal_likelihood_logp(self, X, Xu, y, sigma):
646
651
trace = ((1.0 / (2.0 * sigma2 )) *
647
652
(tt .sum (self .cov_func (X , diag = True )) -
648
653
tt .sum (tt .sum (A * A , 0 ))))
649
- else : # DTC
654
+ else : # DTC
650
655
Lamd = tt .ones_like (Qffd ) * sigma2
651
656
trace = 0.0
652
657
A_l = A / Lamd
@@ -661,7 +666,7 @@ def _build_marginal_likelihood_logp(self, X, Xu, y, sigma):
661
666
662
667
def marginal_likelihood (self , name , X , Xu , y , sigma , is_observed = True , ** kwargs ):
663
668
R"""
664
- Returns the approximate marginal likelihood distribution, given the input
669
+ Returns the approximate marginal likelihood distribution, given the input
665
670
locations `X`, inducing point locations `Xu`, data `y`, and white noise
666
671
standard deviations `sigma`.
667
672
@@ -708,7 +713,7 @@ def _build_conditional(self, Xnew, pred_noise, diag, X, Xu, y, sigma, cov_total,
708
713
if self .approx == "FITC" :
709
714
Kffd = cov_total (X , diag = True )
710
715
Lamd = tt .clip (Kffd - Qffd , 0.0 , np .inf ) + sigma2
711
- else : # VFE or DTC
716
+ else : # VFE or DTC
712
717
Lamd = tt .ones_like (Qffd ) * sigma2
713
718
A_l = A / Lamd
714
719
L_B = cholesky (tt .eye (Xu .shape [0 ]) + tt .dot (A_l , tt .transpose (A )))
@@ -733,6 +738,8 @@ def _build_conditional(self, Xnew, pred_noise, diag, X, Xu, y, sigma, cov_total,
733
738
return mu , stabilize (cov )
734
739
735
740
def _get_given_vals (self , given ):
741
+ if given is None :
742
+ given = {}
736
743
if 'gp' in given :
737
744
cov_total = given ['gp' ].cov_func
738
745
mean_total = given ['gp' ].mean_func
@@ -745,9 +752,9 @@ def _get_given_vals(self, given):
745
752
X , Xu , y , sigma = self .X , self .Xu , self .y , self .sigma
746
753
return X , Xu , y , sigma , cov_total , mean_total
747
754
748
- def conditional (self , name , Xnew , pred_noise = False , given = {} , ** kwargs ):
755
+ def conditional (self , name , Xnew , pred_noise = False , given = None , ** kwargs ):
749
756
R"""
750
- Returns the approximate conditional distribution of the GP evaluated over
757
+ Returns the approximate conditional distribution of the GP evaluated over
751
758
new input locations `Xnew`.
752
759
753
760
Parameters
0 commit comments