Skip to content

Commit a8ade40

Browse files
committed
add tests, fix tt.sqaure -> tt.square
1 parent e3967b1 commit a8ade40

File tree

2 files changed

+184
-29
lines changed

2 files changed

+184
-29
lines changed

pymc3/gp/gp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def conditional(self, name, Xnew, pred_noise=False, given={}, **kwargs):
479479
shape = infer_shape(Xnew, kwargs.pop("shape", None))
480480
return pm.MvNormal(name, mu=mu, chol=chol, shape=shape, **kwargs)
481481

482-
def predict(self, Xnew, point, diag=False, pred_noise=False, given={}):
482+
def predict(self, Xnew, point=None, diag=False, pred_noise=False, given={}):
483483
R"""
484484
Return the mean vector and covariance matrix of the conditional
485485
distribution as numpy arrays, given a `point`, such as the MAP
@@ -702,7 +702,7 @@ def _build_conditional(self, Xnew, X, Xu, y, sigma, cov_total, mean_total,
702702
C = solve_lower(L_B, As)
703703
if diag:
704704
Kss = self.cov_func(Xnew, diag=True)
705-
var = Kss - tt.sum(tt.sqaure(As), 0) + tt.sum(tt.square(C), 0)
705+
var = Kss - tt.sum(tt.square(As), 0) + tt.sum(tt.square(C), 0)
706706
if pred_noise:
707707
var += sigma2
708708
return mu, var

pymc3/tests/test_gp.py

Lines changed: 182 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# pylint:disable=unused-variable
2-
from .helpers import SeededTest
32
from functools import reduce
43
from operator import add
54
import pymc3 as pm
@@ -9,6 +8,7 @@
98
import numpy.testing as npt
109
import pytest
1110

11+
np.random.seed(101)
1212

1313
class TestZeroMean(object):
1414
def test_value(self):
@@ -483,12 +483,12 @@ class TestMarginalVsLatent(object):
483483
Compare the logp of models Marginal, noise=0 and Latent.
484484
"""
485485
def setup_method(self):
486-
X = np.random.randn(20,3)
487-
y = np.random.randn(20)
488-
Xnew = np.random.randn(200, 3)
489-
pnew = np.random.randn(200)
486+
X = np.random.randn(50,3)
487+
y = np.random.randn(50)*0.01
488+
Xnew = np.random.randn(60, 3)
489+
pnew = np.random.randn(60)*0.01
490490
with pm.Model() as model:
491-
cov_func = pm.gp.cov.ExpQuad(3, [1,2,3])
491+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
492492
mean_func = pm.gp.mean.Constant(0.5)
493493
gp = pm.gp.Marginal(mean_func, cov_func)
494494
f = gp.marginal_likelihood("f", X, y, noise=0.0)
@@ -501,25 +501,25 @@ def setup_method(self):
501501

502502
def testLatent1(self):
503503
with pm.Model() as model:
504-
cov_func = pm.gp.cov.ExpQuad(3, [1,2,3])
504+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
505505
mean_func = pm.gp.mean.Constant(0.5)
506506
gp = pm.gp.Latent(mean_func, cov_func)
507507
f = gp.prior("f", self.X, reparameterize=False)
508508
p = gp.conditional("p", self.Xnew)
509509
latent_logp = model.logp({"f": self.y, "p": self.pnew})
510-
npt.assert_allclose(latent_logp, self.logp, atol=0, rtol=1e-3)
510+
npt.assert_allclose(latent_logp, self.logp, atol=0, rtol=1e-2)
511511

512512
def testLatent2(self):
513513
with pm.Model() as model:
514-
cov_func = pm.gp.cov.ExpQuad(3, [1,2,3])
514+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
515515
mean_func = pm.gp.mean.Constant(0.5)
516516
gp = pm.gp.Latent(mean_func, cov_func)
517517
f = gp.prior("f", self.X, reparameterize=True)
518518
p = gp.conditional("p", self.Xnew)
519519
chol = np.linalg.cholesky(cov_func(self.X).eval())
520520
y_rotated = np.linalg.solve(chol, self.y - 0.5)
521521
latent_logp = model.logp({"f_rotated_": y_rotated, "p": self.pnew})
522-
npt.assert_allclose(latent_logp, self.logp, atol=0, rtol=1e-3)
522+
npt.assert_allclose(latent_logp, self.logp, atol=0, rtol=1e-2)
523523

524524

525525
class TestMarginalVsMarginalSparse(object):
@@ -528,12 +528,12 @@ class TestMarginalVsMarginalSparse(object):
528528
Should be nearly equal when inducing points are same as inputs.
529529
"""
530530
def setup_method(self):
531-
X = np.random.randn(20,3)
532-
y = np.random.randn(20)
533-
Xnew = np.random.randn(200, 3)
534-
pnew = np.random.randn(200)
531+
X = np.random.randn(50,3)
532+
y = np.random.randn(50)*0.01
533+
Xnew = np.random.randn(60, 3)
534+
pnew = np.random.randn(60)*0.01
535535
with pm.Model() as model:
536-
cov_func = pm.gp.cov.ExpQuad(3, [1,2,3])
536+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
537537
mean_func = pm.gp.mean.Constant(0.5)
538538
gp = pm.gp.Marginal(mean_func, cov_func)
539539
sigma = 0.1
@@ -545,33 +545,56 @@ def setup_method(self):
545545
self.y = y
546546
self.sigma = sigma
547547
self.pnew = pnew
548+
self.gp = gp
548549

549550
@pytest.mark.parametrize('approx', ['FITC', 'VFE', 'DTC'])
550551
def testApproximations(self, approx):
551552
with pm.Model() as model:
552-
cov_func = pm.gp.cov.ExpQuad(3, [1,2,3])
553+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
553554
mean_func = pm.gp.mean.Constant(0.5)
554555
gp = pm.gp.MarginalSparse(mean_func, cov_func, approx=approx)
555556
f = gp.marginal_likelihood("f", self.X, self.X, self.y, self.sigma)
556557
p = gp.conditional("p", self.Xnew)
557558
approx_logp = model.logp({"f": self.y, "p": self.pnew})
558-
npt.assert_allclose(approx_logp, self.logp, atol=0, rtol=1e-3)
559+
npt.assert_allclose(approx_logp, self.logp, atol=0, rtol=1e-2)
560+
561+
def testPredictCov(self):
562+
with pm.Model() as model:
563+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
564+
mean_func = pm.gp.mean.Constant(0.5)
565+
gp = pm.gp.MarginalSparse(mean_func, cov_func, approx="DTC")
566+
f = gp.marginal_likelihood("f", self.X, self.X, self.y, self.sigma)
567+
mu1, cov1 = self.gp.predict(self.Xnew, pred_noise=True)
568+
mu2, cov2 = gp.predict(self.Xnew, pred_noise=True)
569+
npt.assert_allclose(mu1, mu2, atol=0, rtol=1e-3)
570+
npt.assert_allclose(cov1, cov2, atol=0, rtol=1e-3)
571+
572+
def testPredictVar(self):
573+
with pm.Model() as model:
574+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
575+
mean_func = pm.gp.mean.Constant(0.5)
576+
gp = pm.gp.MarginalSparse(mean_func, cov_func, approx="DTC")
577+
f = gp.marginal_likelihood("f", self.X, self.X, self.y, self.sigma)
578+
mu1, var1 = self.gp.predict(self.Xnew, diag=True)
579+
mu2, var2 = gp.predict(self.Xnew, diag=True)
580+
npt.assert_allclose(mu1, mu2, atol=0, rtol=1e-3)
581+
npt.assert_allclose(var1, var2, atol=0, rtol=1e-3)
559582

560583

561584
class TestGPAdditive(object):
562585
def setup_method(self):
563-
self.X = np.random.randn(20,3)
564-
self.y = np.random.randn(20)
565-
self.Xnew = np.random.randn(200, 3)
586+
self.X = np.random.randn(50,3)
587+
self.y = np.random.randn(50)*0.01
588+
self.Xnew = np.random.randn(60, 3)
566589
self.noise = pm.gp.cov.WhiteNoise(0.1)
567-
self.covs = (pm.gp.cov.ExpQuad(3, [1,2,3]),
568-
pm.gp.cov.ExpQuad(3, [1,2,3]),
569-
pm.gp.cov.ExpQuad(3, [1,2,3]))
590+
self.covs = (pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]),
591+
pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]),
592+
pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]))
570593
self.means = (pm.gp.mean.Constant(0.5),
571594
pm.gp.mean.Constant(0.5),
572595
pm.gp.mean.Constant(0.5))
573596

574-
def testAddMarginal(self):
597+
def testAdditiveMarginal(self):
575598
with pm.Model() as model1:
576599
gp1 = pm.gp.Marginal(self.means[0], self.covs[0])
577600
gp2 = pm.gp.Marginal(self.means[1], self.covs[1])
@@ -585,7 +608,7 @@ def testAddMarginal(self):
585608
gptot = pm.gp.Marginal(reduce(add, self.means), reduce(add, self.covs))
586609
fsum = gptot.marginal_likelihood("f", self.X, self.y, noise=self.noise)
587610
model2_logp = model2.logp({"fsum": self.y})
588-
npt.assert_allclose(model1_logp, model2_logp, atol=0, rtol=1e-3)
611+
npt.assert_allclose(model1_logp, model2_logp, atol=0, rtol=1e-2)
589612

590613
with model1:
591614
fp1 = gpsum.conditional("fp1", self.Xnew, given={"X": self.X, "y": self.y,
@@ -594,13 +617,145 @@ def testAddMarginal(self):
594617
fp2 = gptot.conditional("fp2", self.Xnew)
595618

596619
fp = np.random.randn(self.Xnew.shape[0])
597-
npt.assert_allclose(fp1.logp({"fp1": fp}), fp2.logp({"fp2": fp}), atol=0, rtol=1e-3)
620+
npt.assert_allclose(fp1.logp({"fp1": fp}), fp2.logp({"fp2": fp}), atol=0, rtol=1e-2)
621+
622+
@pytest.mark.parametrize('approx', ['FITC', 'VFE', 'DTC'])
623+
def testAdditiveMarginalSparse(self, approx):
624+
Xu = np.random.randn(10, 1)
625+
sigma = 0.1
626+
with pm.Model() as model1:
627+
gp1 = pm.gp.MarginalSparse(self.means[0], self.covs[0], approx=approx)
628+
gp2 = pm.gp.MarginalSparse(self.means[1], self.covs[1], approx=approx)
629+
gp3 = pm.gp.MarginalSparse(self.means[2], self.covs[2], approx=approx)
630+
631+
gpsum = gp1 + gp2 + gp3
632+
fsum = gpsum.marginal_likelihood("f", self.X, Xu, self.y, sigma=sigma)
633+
model1_logp = model1.logp({"fsum": self.y})
634+
635+
with pm.Model() as model2:
636+
gptot = pm.gp.MarginalSparse(reduce(add, self.means), reduce(add, self.covs), approx=approx)
637+
fsum = gptot.marginal_likelihood("f", self.X, Xu, self.y, sigma=sigma)
638+
model2_logp = model2.logp({"fsum": self.y})
639+
npt.assert_allclose(model1_logp, model2_logp, atol=0, rtol=1e-2)
640+
641+
with model1:
642+
fp1 = gpsum.conditional("fp1", self.Xnew, given={"X": self.X, "Xu": Xu, "y": self.y,
643+
"sigma": sigma, "gp": gpsum})
644+
with model2:
645+
fp2 = gptot.conditional("fp2", self.Xnew)
646+
647+
fp = np.random.randn(self.Xnew.shape[0])
648+
npt.assert_allclose(fp1.logp({"fp1": fp}), fp2.logp({"fp2": fp}), atol=0, rtol=1e-2)
649+
650+
def testAdditiveLatent(self):
651+
with pm.Model() as model1:
652+
gp1 = pm.gp.Latent(self.means[0], self.covs[0])
653+
gp2 = pm.gp.Latent(self.means[1], self.covs[1])
654+
gp3 = pm.gp.Latent(self.means[2], self.covs[2])
655+
656+
gpsum = gp1 + gp2 + gp3
657+
fsum = gpsum.prior("fsum", self.X, reparameterize=False)
658+
model1_logp = model1.logp({"fsum": self.y})
659+
660+
with pm.Model() as model2:
661+
gptot = pm.gp.Latent(reduce(add, self.means), reduce(add, self.covs))
662+
fsum = gptot.prior("fsum", self.X, reparameterize=False)
663+
model2_logp = model2.logp({"fsum": self.y})
664+
npt.assert_allclose(model1_logp, model2_logp, atol=0, rtol=1e-2)
665+
666+
with model1:
667+
fp1 = gpsum.conditional("fp1", self.Xnew, given={"X": self.X, "f": self.y, "gp": gpsum})
668+
with model2:
669+
fp2 = gptot.conditional("fp2", self.Xnew)
670+
671+
fp = np.random.randn(self.Xnew.shape[0])
672+
npt.assert_allclose(fp1.logp({"fp1": fp}), fp2.logp({"fp2": fp}), atol=0, rtol=1e-2)
673+
674+
675+
def testAdditiveSparseRaises(self):
676+
# cant add different approximations
677+
with pm.Model() as model:
678+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
679+
gp1 = pm.gp.MarginalSparse(cov_func=cov_func, approx="DTC")
680+
gp2 = pm.gp.MarginalSparse(cov_func=cov_func, approx="FITC")
681+
with pytest.raises(Exception) as e_info:
682+
gp1 + gp2
683+
684+
def testAdditiveTypeRaises1(self):
685+
with pm.Model() as model:
686+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
687+
gp1 = pm.gp.MarginalSparse(cov_func=cov_func, approx="DTC")
688+
gp2 = pm.gp.Marginal(cov_func=cov_func)
689+
with pytest.raises(Exception) as e_info:
690+
gp1 + gp2
691+
692+
def testAdditiveTypeRaises2(self):
693+
with pm.Model() as model:
694+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
695+
gp1 = pm.gp.Latent(cov_func=cov_func)
696+
gp2 = pm.gp.Marginal(cov_func=cov_func)
697+
with pytest.raises(Exception) as e_info:
698+
gp1 + gp2
598699

599700

600701
class TestTP(object):
601702
R"""
602703
Compare TP with high degress of freedom to GP
603704
"""
604705
def setup_method(self):
605-
pass
706+
X = np.random.randn(20,3)
707+
y = np.random.randn(20)*0.01
708+
Xnew = np.random.randn(50, 3)
709+
pnew = np.random.randn(50)*0.01
710+
with pm.Model() as model:
711+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
712+
gp = pm.gp.Latent(cov_func=cov_func)
713+
f = gp.prior("f", X, reparameterize=False)
714+
p = gp.conditional("p", Xnew)
715+
self.X = X
716+
self.y = y
717+
self.Xnew = Xnew
718+
self.pnew = pnew
719+
self.latent_logp = model.logp({"f": y, "p": pnew})
720+
self.plogp = p.logp({"f": y, "p": pnew})
721+
722+
def testTPvsLatent(self):
723+
with pm.Model() as model:
724+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
725+
tp = pm.gp.TP(cov_func=cov_func, nu=10000)
726+
f = tp.prior("f", self.X, reparameterize=False)
727+
p = tp.conditional("p", self.Xnew)
728+
tp_logp = model.logp({"f": self.y, "p": self.pnew})
729+
npt.assert_allclose(self.latent_logp, tp_logp, atol=0, rtol=1e-2)
730+
731+
def testTPvsLatentReparameterized(self):
732+
with pm.Model() as model:
733+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
734+
tp = pm.gp.TP(cov_func=cov_func, nu=10000)
735+
f = tp.prior("f", self.X, reparameterize=True)
736+
p = tp.conditional("p", self.Xnew)
737+
chol = np.linalg.cholesky(cov_func(self.X).eval())
738+
y_rotated = np.linalg.solve(chol, self.y)
739+
# testing full model logp unreliable due to introduction of chi2__log__
740+
plogp = p.logp({"f_rotated_": y_rotated, "p": self.pnew, "chi2__log__": np.log(1e20)})
741+
npt.assert_allclose(self.plogp, plogp, atol=0, rtol=1e-2)
742+
743+
def testAdditiveTPRaises(self):
744+
with pm.Model() as model:
745+
cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3])
746+
gp1 = pm.gp.TP(cov_func=cov_func, nu=10)
747+
gp2 = pm.gp.TP(cov_func=cov_func, nu=10)
748+
with pytest.raises(Exception) as e_info:
749+
gp1 + gp2
750+
751+
752+
753+
754+
755+
756+
757+
758+
759+
760+
606761

0 commit comments

Comments
 (0)