From 6e3f7b6328761100d4d61bfdf971196fdc57454c Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Thu, 25 Aug 2022 21:19:00 -0500 Subject: [PATCH 1/6] Rename cov_func/cov to scale_func/scale for TP/MvStudentT --- pymc/distributions/multivariate.py | 35 ++++++++++++++++++------------ pymc/gp/gp.py | 15 +++++++++---- pymc/tests/test_gp.py | 16 +++++++------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index cf4ed753bd1..e988157ff56 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -352,42 +352,49 @@ class MvStudentT(Continuous): nu : tensor_like of float Degrees of freedom, should be a positive scalar. Sigma : tensor_like of float, optional - Covariance matrix. Use `cov` in new code. + Scale matrix. Use `scale` in new code. mu : tensor_like of float, optional Vector of means. - cov : tensor_like of float, optional - The covariance matrix. + scale : tensor_like of float, optional + The scale matrix. tau : tensor_like of float, optional The precision matrix. chol : tensor_like of float, optional - The cholesky factor of the covariance matrix. + The cholesky factor of the scale matrix. lower : bool, default=True Whether the cholesky fatcor is given as a lower triangular matrix. """ rv_op = mv_studentt @classmethod - def dist(cls, nu, Sigma=None, mu=None, cov=None, tau=None, chol=None, lower=True, **kwargs): + def dist(cls, nu, Sigma=None, mu=None, scale=None, tau=None, chol=None, lower=True, **kwargs): + if kwargs.get('cov') is not None: + warnings.warn( + "Use the scale argument to specify the scale matrix." + "cov will be removed in future versions." + DeprecationWarning, + ) + scale = kwargs.get('cov') if Sigma is not None: - if cov is not None: - raise ValueError("Specify only one of cov and Sigma") - cov = Sigma + if scale is not None: + raise ValueError("Specify only one of scale and Sigma") + scale = Sigma nu = at.as_tensor_variable(floatX(nu)) mu = at.as_tensor_variable(floatX(mu)) - cov = quaddist_matrix(cov, chol, tau, lower) + scale = quaddist_matrix(scale, chol, tau, lower) # Aesara is stricter about the shape of mu, than PyMC used to be - mu = at.broadcast_arrays(mu, cov[..., -1])[0] + mu = at.broadcast_arrays(mu, scale[..., -1])[0] - return super().dist([nu, mu, cov], **kwargs) + return super().dist([nu, mu, scale], **kwargs) - def moment(rv, size, nu, mu, cov): + def moment(rv, size, nu, mu, scale): moment = mu if not rv_size_is_none(size): moment_size = at.concatenate([size, [mu.shape[-1]]]) moment = at.full(moment_size, moment) return moment - def logp(value, nu, mu, cov): + def logp(value, nu, mu, scale): """ Calculate log-probability of Multivariate Student's T distribution at specified value. @@ -401,7 +408,7 @@ def logp(value, nu, mu, cov): ------- TensorVariable """ - quaddist, logdet, ok = quaddist_parse(value, mu, cov) + quaddist, logdet, ok = quaddist_parse(value, mu, scale) k = floatX(value.shape[-1]) norm = gammaln((nu + k) / 2.0) - gammaln(nu / 2.0) - 0.5 * k * at.log(nu * np.pi) diff --git a/pymc/gp/gp.py b/pymc/gp/gp.py index e947f8edf3c..cc227b6a03f 100644 --- a/pymc/gp/gp.py +++ b/pymc/gp/gp.py @@ -251,8 +251,8 @@ class TP(Latent): Parameters ---------- - cov_func : None, 2D array, or instance of Covariance - The covariance function. Defaults to zero. + scale_func : None, 2D array, or instance of Covariance + The scale function. Defaults to zero. mean_func : None, instance of Mean The mean function. Defaults to zero. nu : float @@ -264,11 +264,18 @@ class TP(Latent): Processes as Alternatives to Gaussian Processes. arXiv preprint arXiv:1402.4306. """ - def __init__(self, *, mean_func=Zero(), cov_func=Constant(0.0), nu=None): + def __init__(self, *, mean_func=Zero(), scale_func=Constant(0.0), cov_func=None, nu=None): if nu is None: raise ValueError("Student's T process requires a degrees of freedom parameter, 'nu'") + if cov_func is not None: + warnings.warn( + "Use the scale_func argument to specify the scale function." + "cov_func will be removed in future versions." + DeprecationWarning, + ) + scale_func = cov_func self.nu = nu - super().__init__(mean_func=mean_func, cov_func=cov_func) + super().__init__(mean_func=mean_func, cov_func=scale_func) def __add__(self, other): raise TypeError("Student's T processes aren't additive") diff --git a/pymc/tests/test_gp.py b/pymc/tests/test_gp.py index 6e398bd0040..80d6a37803c 100644 --- a/pymc/tests/test_gp.py +++ b/pymc/tests/test_gp.py @@ -1068,8 +1068,8 @@ def setup_method(self): def testTPvsLatent(self): with pm.Model() as model: - cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) - tp = pm.gp.TP(cov_func=cov_func, nu=self.nu) + scale_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) + tp = pm.gp.TP(scale_func=scale_func, nu=self.nu) f = tp.prior("f", self.X, reparameterize=False) p = tp.conditional("p", self.Xnew) assert tuple(f.shape.eval()) == (self.X.shape[0],) @@ -1079,22 +1079,22 @@ def testTPvsLatent(self): def testTPvsLatentReparameterized(self): with pm.Model() as model: - cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) - tp = pm.gp.TP(cov_func=cov_func, nu=self.nu) + scale_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) + tp = pm.gp.TP(scale_func=scale_func, nu=self.nu) f = tp.prior("f", self.X, reparameterize=True) p = tp.conditional("p", self.Xnew) assert tuple(f.shape.eval()) == (self.X.shape[0],) assert tuple(p.shape.eval()) == (self.Xnew.shape[0],) - chol = np.linalg.cholesky(cov_func(self.X).eval()) + chol = np.linalg.cholesky(scale_func(self.X).eval()) f_rotated = np.linalg.solve(chol, self.y) tp_logp = model.compile_logp()({"f_rotated_": f_rotated, "p": self.pnew}) npt.assert_allclose(self.gp_latent_logp, tp_logp, atol=0, rtol=1e-2) def testAdditiveTPRaises(self): with pm.Model() as model: - cov_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) - gp1 = pm.gp.TP(cov_func=cov_func, nu=10) - gp2 = pm.gp.TP(cov_func=cov_func, nu=10) + scale_func = pm.gp.cov.ExpQuad(3, [0.1, 0.2, 0.3]) + gp1 = pm.gp.TP(scale_func=scale_func, nu=10) + gp2 = pm.gp.TP(scale_func=scale_func, nu=10) with pytest.raises(Exception) as e_info: gp1 + gp2 From 1857620d139823d7d41d7d227804dbe28d550eca Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Thu, 25 Aug 2022 22:39:35 -0400 Subject: [PATCH 2/6] Fixed syntax error --- pymc/distributions/multivariate.py | 2 +- pymc/gp/gp.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index e988157ff56..cead001029d 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -371,7 +371,7 @@ def dist(cls, nu, Sigma=None, mu=None, scale=None, tau=None, chol=None, lower=Tr if kwargs.get('cov') is not None: warnings.warn( "Use the scale argument to specify the scale matrix." - "cov will be removed in future versions." + "cov will be removed in future versions.", DeprecationWarning, ) scale = kwargs.get('cov') diff --git a/pymc/gp/gp.py b/pymc/gp/gp.py index cc227b6a03f..10c3d148cb0 100644 --- a/pymc/gp/gp.py +++ b/pymc/gp/gp.py @@ -270,7 +270,7 @@ def __init__(self, *, mean_func=Zero(), scale_func=Constant(0.0), cov_func=None, if cov_func is not None: warnings.warn( "Use the scale_func argument to specify the scale function." - "cov_func will be removed in future versions." + "cov_func will be removed in future versions.", DeprecationWarning, ) scale_func = cov_func From dccba6af45f6c4d70df2b89240620ab594ee0cd2 Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Thu, 25 Aug 2022 22:43:39 -0400 Subject: [PATCH 3/6] Black formatting --- pymc/distributions/multivariate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index cead001029d..47775c59364 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -368,13 +368,13 @@ class MvStudentT(Continuous): @classmethod def dist(cls, nu, Sigma=None, mu=None, scale=None, tau=None, chol=None, lower=True, **kwargs): - if kwargs.get('cov') is not None: + if kwargs.get("cov") is not None: warnings.warn( "Use the scale argument to specify the scale matrix." "cov will be removed in future versions.", DeprecationWarning, ) - scale = kwargs.get('cov') + scale = kwargs.get("cov") if Sigma is not None: if scale is not None: raise ValueError("Specify only one of scale and Sigma") From f2fbe1a7a9472d4a06c8bb92c313493477d5ca50 Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Fri, 26 Aug 2022 10:27:23 -0400 Subject: [PATCH 4/6] pop optional cov argument from kwargs --- pymc/distributions/multivariate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index 47775c59364..d1a8cf5e025 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -374,7 +374,7 @@ def dist(cls, nu, Sigma=None, mu=None, scale=None, tau=None, chol=None, lower=Tr "cov will be removed in future versions.", DeprecationWarning, ) - scale = kwargs.get("cov") + scale = kwargs.pop("cov") if Sigma is not None: if scale is not None: raise ValueError("Specify only one of scale and Sigma") From cb3bc7f0c1965f8c2e585292ad3c8498cf822700 Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Fri, 26 Aug 2022 15:13:21 -0400 Subject: [PATCH 5/6] Replaced DeprecationWarning with FutureWarning --- pymc/distributions/multivariate.py | 3 ++- pymc/gp/gp.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index d1a8cf5e025..23c674c4df7 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -15,6 +15,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from asyncio import Future import warnings from functools import reduce @@ -372,7 +373,7 @@ def dist(cls, nu, Sigma=None, mu=None, scale=None, tau=None, chol=None, lower=Tr warnings.warn( "Use the scale argument to specify the scale matrix." "cov will be removed in future versions.", - DeprecationWarning, + FutureWarning, ) scale = kwargs.pop("cov") if Sigma is not None: diff --git a/pymc/gp/gp.py b/pymc/gp/gp.py index 10c3d148cb0..89e2f2b0e1f 100644 --- a/pymc/gp/gp.py +++ b/pymc/gp/gp.py @@ -271,7 +271,7 @@ def __init__(self, *, mean_func=Zero(), scale_func=Constant(0.0), cov_func=None, warnings.warn( "Use the scale_func argument to specify the scale function." "cov_func will be removed in future versions.", - DeprecationWarning, + FutureWarning, ) scale_func = cov_func self.nu = nu From 0fa32343656f4a853bb853515ec4095dec91d73a Mon Sep 17 00:00:00 2001 From: fonnesbeck Date: Fri, 26 Aug 2022 15:17:21 -0400 Subject: [PATCH 6/6] Removed unused import --- pymc/distributions/multivariate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pymc/distributions/multivariate.py b/pymc/distributions/multivariate.py index 23c674c4df7..f0976ea9d6b 100644 --- a/pymc/distributions/multivariate.py +++ b/pymc/distributions/multivariate.py @@ -15,7 +15,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from asyncio import Future import warnings from functools import reduce