From 48e46d68ea6b68db19df32e620791a2d91244543 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Thu, 13 May 2021 22:15:27 -0400 Subject: [PATCH 01/12] Refactoring ChiSquared distribution --- pymc3/distributions/continuous.py | 18 +++++++++++++++++- pymc3/tests/test_distributions.py | 1 - pymc3/tests/test_distributions_random.py | 8 +++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index a2b9bd4f47..b5182826e2 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2602,6 +2602,21 @@ def logcdf(value, alpha, beta): ) +class ChiSquaredRV(RandomVariable): + name = "chi_squared" + ndim_supp = 0 + ndims_params = [0] + dtype = "floatX" + _print_name = ("ChiSquared", "\\operatorname{ChiSquared}") + + @classmethod + def rng_fn(cls, rng, nu, size=None): + return stats.chi2(nu, size=size, random_state=rng) + + +chi_squared = ChiSquaredRV() + + class ChiSquared(Gamma): r""" :math:`\chi^2` log-likelihood. @@ -2641,7 +2656,8 @@ class ChiSquared(Gamma): Degrees of freedom (nu > 0). """ - def __init__(self, nu, *args, **kwargs): + @classmethod + def dist(cls, nu, *args, **kwargs): self.nu = nu = at.as_tensor_variable(floatX(nu)) super().__init__(alpha=nu / 2.0, beta=0.5, *args, **kwargs) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 63466b7902..e47c41b596 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1030,7 +1030,6 @@ def test_half_normal(self): lambda value, sigma: sp.halfnorm.logcdf(value, scale=sigma), ) - @pytest.mark.xfail(reason="Distribution not refactored yet") def test_chi_squared(self): self.check_logp( ChiSquared, diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 82d4f4e091..74eb06cb2f 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -295,10 +295,16 @@ class TestStudentT(BaseTestCases.BaseTestCase): params = {"nu": 5.0, "mu": 0.0, "lam": 1.0} -@pytest.mark.xfail(reason="This distribution has not been refactored for v4") class TestChiSquared(BaseTestCases.BaseTestCase): distribution = pm.ChiSquared params = {"nu": 2.0} + expected_rv_op_params = {"nu": 2.0} + reference_dist_params = {"df": 2.0} + reference_dist = seeded_numpy_distribution_builder("chisquare") + tests_to_run = [ + "check_pymc_params_match_rv_op", + "check_pymc_draws_match_reference", + ] @pytest.mark.xfail(reason="This distribution has not been refactored for v4") From 4de21920c9761a7e9acbc1a376a0dc3d6dfcf266 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Thu, 13 May 2021 22:26:39 -0400 Subject: [PATCH 02/12] Refactoring ChiSquared (minor edit) --- pymc3/distributions/continuous.py | 1 + pymc3/tests/test_distributions_random.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index b5182826e2..5f0d976eec 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2655,6 +2655,7 @@ class ChiSquared(Gamma): nu: int Degrees of freedom (nu > 0). """ + rv_op = chi_squared @classmethod def dist(cls, nu, *args, **kwargs): diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 74eb06cb2f..72e5356c91 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -304,6 +304,7 @@ class TestChiSquared(BaseTestCases.BaseTestCase): tests_to_run = [ "check_pymc_params_match_rv_op", "check_pymc_draws_match_reference", + "check_rv_size", ] From 53c8cc85cb4434623e84127a53712a1e0b2523c7 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Thu, 13 May 2021 22:27:57 -0400 Subject: [PATCH 03/12] Refactoring Chisquared (another one-line change) --- pymc3/distributions/continuous.py | 2 +- pymc3/tests/test_distributions_random.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 5f0d976eec..496bea685b 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2659,7 +2659,7 @@ class ChiSquared(Gamma): @classmethod def dist(cls, nu, *args, **kwargs): - self.nu = nu = at.as_tensor_variable(floatX(nu)) + nu = at.as_tensor_variable(floatX(nu)) super().__init__(alpha=nu / 2.0, beta=0.5, *args, **kwargs) diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 72e5356c91..d99107120d 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -306,7 +306,7 @@ class TestChiSquared(BaseTestCases.BaseTestCase): "check_pymc_draws_match_reference", "check_rv_size", ] - + @pytest.mark.xfail(reason="This distribution has not been refactored for v4") class TestExGaussian(BaseTestCases.BaseTestCase): From ba3099b8da8a07ad1e241f14f1123849991ec8d4 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Thu, 20 May 2021 18:21:17 -0400 Subject: [PATCH 04/12] Trying to rebase/merge my branch with updated upstream v4 --- pymc3/tests/test_distributions_random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index d99107120d..72e5356c91 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -306,7 +306,7 @@ class TestChiSquared(BaseTestCases.BaseTestCase): "check_pymc_draws_match_reference", "check_rv_size", ] - + @pytest.mark.xfail(reason="This distribution has not been refactored for v4") class TestExGaussian(BaseTestCases.BaseTestCase): From be682a2e527799d87deb11fac026b37eecdcc544 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Thu, 20 May 2021 18:35:10 -0400 Subject: [PATCH 05/12] Using aesara chisquare op (r.f. PR #414) and renamed ChiSquared to ChiSquare --- docs/source/api/distributions/continuous.rst | 2 +- pymc3/distributions/__init__.py | 4 ++-- pymc3/distributions/continuous.py | 22 ++++---------------- pymc3/distributions/multivariate.py | 4 ++-- pymc3/gp/gp.py | 2 +- pymc3/tests/test_distributions.py | 4 ++-- pymc3/tests/test_distributions_random.py | 5 ++--- 7 files changed, 14 insertions(+), 29 deletions(-) diff --git a/docs/source/api/distributions/continuous.rst b/docs/source/api/distributions/continuous.rst index 9222dcb797..3399ccf039 100644 --- a/docs/source/api/distributions/continuous.rst +++ b/docs/source/api/distributions/continuous.rst @@ -25,7 +25,7 @@ Continuous InverseGamma Weibull Lognormal - ChiSquared + ChiSquare Wald Pareto ExGaussian diff --git a/pymc3/distributions/__init__.py b/pymc3/distributions/__init__.py index 807b483712..ee51a8d351 100644 --- a/pymc3/distributions/__init__.py +++ b/pymc3/distributions/__init__.py @@ -27,7 +27,7 @@ AsymmetricLaplace, Beta, Cauchy, - ChiSquared, + ChiSquare, ExGaussian, Exponential, Flat, @@ -125,7 +125,7 @@ "Bound", "Lognormal", "HalfStudentT", - "ChiSquared", + "ChiSquare", "HalfNormal", "Wald", "Pareto", diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index c1d2869a54..02ccffa354 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -28,6 +28,7 @@ BetaRV, WeibullRV, cauchy, + chisquare, exponential, gamma, gumbel, @@ -87,7 +88,7 @@ "Weibull", "HalfStudentT", "Lognormal", - "ChiSquared", + "ChiSquare", "HalfNormal", "Wald", "Pareto", @@ -2548,22 +2549,7 @@ def logcdf(value, alpha, beta): ) -class ChiSquaredRV(RandomVariable): - name = "chi_squared" - ndim_supp = 0 - ndims_params = [0] - dtype = "floatX" - _print_name = ("ChiSquared", "\\operatorname{ChiSquared}") - - @classmethod - def rng_fn(cls, rng, nu, size=None): - return stats.chi2(nu, size=size, random_state=rng) - - -chi_squared = ChiSquaredRV() - - -class ChiSquared(Gamma): +class ChiSquare(Gamma): r""" :math:`\chi^2` log-likelihood. @@ -2601,7 +2587,7 @@ class ChiSquared(Gamma): nu: int Degrees of freedom (nu > 0). """ - rv_op = chi_squared + rv_op = chisquare @classmethod def dist(cls, nu, *args, **kwargs): diff --git a/pymc3/distributions/multivariate.py b/pymc3/distributions/multivariate.py index 4eb6b01817..3bdce21e45 100644 --- a/pymc3/distributions/multivariate.py +++ b/pymc3/distributions/multivariate.py @@ -40,7 +40,7 @@ from pymc3.aesaraf import floatX, intX from pymc3.distributions import transforms -from pymc3.distributions.continuous import ChiSquared, Normal +from pymc3.distributions.continuous import ChiSquare, Normal from pymc3.distributions.dist_math import bound, factln, logpow from pymc3.distributions.distribution import Continuous, Discrete from pymc3.distributions.special import gammaln, multigammaln @@ -905,7 +905,7 @@ def WishartBartlett(name, S, nu, is_cholesky=False, return_cholesky=False, testv tril_testval = None c = at.sqrt( - ChiSquared("%s_c" % name, nu - np.arange(2, 2 + n_diag), shape=n_diag, testval=diag_testval) + ChiSquare("%s_c" % name, nu - np.arange(2, 2 + n_diag), shape=n_diag, testval=diag_testval) ) pm._log.info("Added new variable %s_c to model diagonal of Wishart." % name) z = Normal("%s_z" % name, 0.0, 1.0, shape=n_tril, testval=tril_testval) diff --git a/pymc3/gp/gp.py b/pymc3/gp/gp.py index 17e232f0c2..31fbb41fe3 100644 --- a/pymc3/gp/gp.py +++ b/pymc3/gp/gp.py @@ -278,7 +278,7 @@ def _build_prior(self, name, X, reparameterize=True, **kwargs): cov = stabilize(self.cov_func(X)) shape = infer_shape(X, kwargs.pop("shape", None)) if reparameterize: - chi2 = pm.ChiSquared(name + "_chi2_", self.nu) + chi2 = pm.ChiSquare(name + "_chi2_", self.nu) v = pm.Normal(name + "_rotated_", mu=0.0, sigma=1.0, size=shape, **kwargs) f = pm.Deterministic(name, (at.sqrt(self.nu) / chi2) * (mu + cholesky(cov).dot(v))) else: diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index a0869edbc4..062663b1da 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -48,7 +48,7 @@ Bound, Categorical, Cauchy, - ChiSquared, + ChiSquare, Constant, DensityDist, Dirichlet, @@ -1032,7 +1032,7 @@ def test_half_normal(self): def test_chi_squared(self): self.check_logp( - ChiSquared, + ChiSquare, Rplus, {"nu": Rplusdunif}, lambda value, nu: sp.chi2.logpdf(value, df=nu), diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index df9b085402..2c44f44a53 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -277,9 +277,8 @@ class TestAsymmetricLaplace(BaseTestCases.BaseTestCase): params = {"kappa": 1.0, "b": 1.0, "mu": 0.0} -@pytest.mark.xfail(reason="This distribution has not been refactored for v4") -class TestChiSquared(BaseTestCases.BaseTestCase): - distribution = pm.ChiSquared +class TestChiSquare(BaseTestCases.BaseTestCase): + distribution = pm.ChiSquare params = {"nu": 2.0} expected_rv_op_params = {"nu": 2.0} reference_dist_params = {"df": 2.0} From dff62a192c6e7cf74f7307d8558c9fe8552176f5 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Sun, 30 May 2021 18:40:03 -0400 Subject: [PATCH 06/12] Added logpdf & logcdf to the ChiSquare class --- pymc3/distributions/continuous.py | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 02ccffa354..040ae2ceb4 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2594,6 +2594,39 @@ def dist(cls, nu, *args, **kwargs): nu = at.as_tensor_variable(floatX(nu)) super().__init__(alpha=nu / 2.0, beta=0.5, *args, **kwargs) + def logp(value, nu): + """ + Calculate log-probability of ChiSquare distribution at specified value. + + Parameters + ---------- + value: numeric + Value(s) for which log-probability is calculated. If the log probabilities for multiple + values are desired the values must be provided in a numpy array or Aesara tensor + + Returns + ------- + TensorVariable + """ + return Gamma.logpdf(value, nu/2, 2) + + def logcdf(value, nu): + """ + Compute the log of the cumulative distribution function for ChiSquare distribution + at the specified value. + + Parameters + ---------- + value: numeric or np.ndarray or `TensorVariable` + Value(s) for which log CDF is calculated. If the log CDF for + multiple values are desired the values must be provided in a numpy + array or `TensorVariable`. + Returns + ------- + TensorVariable + """ + return Gamma.logcdf(value, nu/2, 2) + # TODO: Remove this once logpt for multiplication is working! class WeibullBetaRV(WeibullRV): From e7ba22404f387f64b5b155388a810c8b2e8d564c Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Mon, 31 May 2021 07:43:55 -0400 Subject: [PATCH 07/12] Corrected function name --- pymc3/distributions/continuous.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 040ae2ceb4..31b8557ee5 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2608,7 +2608,7 @@ def logp(value, nu): ------- TensorVariable """ - return Gamma.logpdf(value, nu/2, 2) + return Gamma.logp(value, nu/2, 2) def logcdf(value, nu): """ From 78ca870db2f86a9f7524f8bf0250af5a9849604b Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Tue, 1 Jun 2021 23:37:06 -0400 Subject: [PATCH 08/12] Updating branch --- pymc3/distributions/continuous.py | 6 +++--- pymc3/tests/test_distributions.py | 10 +++++++-- pymc3/tests/test_distributions_random.py | 26 ++++++++++++------------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 31b8557ee5..4a34cf8d18 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2592,7 +2592,7 @@ class ChiSquare(Gamma): @classmethod def dist(cls, nu, *args, **kwargs): nu = at.as_tensor_variable(floatX(nu)) - super().__init__(alpha=nu / 2.0, beta=0.5, *args, **kwargs) + return super().__init__([nu / 2.0, 0.5], *args, **kwargs) def logp(value, nu): """ @@ -2608,7 +2608,7 @@ def logp(value, nu): ------- TensorVariable """ - return Gamma.logp(value, nu/2, 2) + return Gamma.logp(value, nu / 2, 2) def logcdf(value, nu): """ @@ -2625,7 +2625,7 @@ def logcdf(value, nu): ------- TensorVariable """ - return Gamma.logcdf(value, nu/2, 2) + return Gamma.logcdf(value, nu / 2, 2) # TODO: Remove this once logpt for multiplication is working! diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 062663b1da..e7f7bed408 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1030,13 +1030,19 @@ def test_half_normal(self): lambda value, sigma: sp.halfnorm.logcdf(value, scale=sigma), ) - def test_chi_squared(self): + def test_chisquare(self): self.check_logp( ChiSquare, Rplus, - {"nu": Rplusdunif}, + {"nu": Rplus}, lambda value, nu: sp.chi2.logpdf(value, df=nu), ) + self.check_logcdf( + ChiSquare, + Rplus, + {"nu": Rplus}, + lambda value, nu: sp.chi2.logcdf(value, df=nu), + ) @pytest.mark.xfail(reason="Distribution not refactored yet") def test_wald_logp(self): diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 2c44f44a53..02ce2c51c5 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -277,19 +277,6 @@ class TestAsymmetricLaplace(BaseTestCases.BaseTestCase): params = {"kappa": 1.0, "b": 1.0, "mu": 0.0} -class TestChiSquare(BaseTestCases.BaseTestCase): - distribution = pm.ChiSquare - params = {"nu": 2.0} - expected_rv_op_params = {"nu": 2.0} - reference_dist_params = {"df": 2.0} - reference_dist = seeded_numpy_distribution_builder("chisquare") - tests_to_run = [ - "check_pymc_params_match_rv_op", - "check_pymc_draws_match_reference", - "check_rv_size", - ] - - @pytest.mark.xfail(reason="This distribution has not been refactored for v4") class TestExGaussian(BaseTestCases.BaseTestCase): distribution = pm.ExGaussian @@ -760,6 +747,19 @@ class TestInverseGammaMuSigma(BaseTestDistribution): tests_to_run = ["check_pymc_params_match_rv_op"] +class TestChiSquare(BaseTestDistribution): + pymc_dist = pm.ChiSquare + pymc_dist_params = {"nu": 2.0} + expected_rv_op_params = {"nu": 2.0} + reference_dist_params = {"df": 2.0} + reference_dist = seeded_numpy_distribution_builder("chisquare") + tests_to_run = [ + "check_pymc_params_match_rv_op", + "check_pymc_draws_match_reference", + "check_rv_size", + ] + + class TestBinomial(BaseTestDistribution): pymc_dist = pm.Binomial pymc_dist_params = {"n": 100, "p": 0.33} From f3c0144f3b76d03b1b1d8e9d0bac4ccabfe68e1b Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Fri, 4 Jun 2021 15:55:14 -0400 Subject: [PATCH 09/12] Refactoring ChiSquared: bug fixed, tests work locally --- docs/source/api/distributions/continuous.rst | 2 +- pymc3/distributions/__init__.py | 4 ++-- pymc3/distributions/continuous.py | 10 +++++----- pymc3/distributions/multivariate.py | 4 ++-- pymc3/gp/gp.py | 2 +- pymc3/tests/test_distributions.py | 9 +++++---- pymc3/tests/test_distributions_random.py | 4 ++-- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/docs/source/api/distributions/continuous.rst b/docs/source/api/distributions/continuous.rst index 3399ccf039..9222dcb797 100644 --- a/docs/source/api/distributions/continuous.rst +++ b/docs/source/api/distributions/continuous.rst @@ -25,7 +25,7 @@ Continuous InverseGamma Weibull Lognormal - ChiSquare + ChiSquared Wald Pareto ExGaussian diff --git a/pymc3/distributions/__init__.py b/pymc3/distributions/__init__.py index ee51a8d351..807b483712 100644 --- a/pymc3/distributions/__init__.py +++ b/pymc3/distributions/__init__.py @@ -27,7 +27,7 @@ AsymmetricLaplace, Beta, Cauchy, - ChiSquare, + ChiSquared, ExGaussian, Exponential, Flat, @@ -125,7 +125,7 @@ "Bound", "Lognormal", "HalfStudentT", - "ChiSquare", + "ChiSquared", "HalfNormal", "Wald", "Pareto", diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 4a34cf8d18..ac57b7d471 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -88,7 +88,7 @@ "Weibull", "HalfStudentT", "Lognormal", - "ChiSquare", + "ChiSquared", "HalfNormal", "Wald", "Pareto", @@ -2549,7 +2549,7 @@ def logcdf(value, alpha, beta): ) -class ChiSquare(Gamma): +class ChiSquared(PositiveContinuous): r""" :math:`\chi^2` log-likelihood. @@ -2592,11 +2592,11 @@ class ChiSquare(Gamma): @classmethod def dist(cls, nu, *args, **kwargs): nu = at.as_tensor_variable(floatX(nu)) - return super().__init__([nu / 2.0, 0.5], *args, **kwargs) + return super().dist([nu], *args, **kwargs) def logp(value, nu): """ - Calculate log-probability of ChiSquare distribution at specified value. + Calculate log-probability of ChiSquared distribution at specified value. Parameters ---------- @@ -2612,7 +2612,7 @@ def logp(value, nu): def logcdf(value, nu): """ - Compute the log of the cumulative distribution function for ChiSquare distribution + Compute the log of the cumulative distribution function for ChiSquared distribution at the specified value. Parameters diff --git a/pymc3/distributions/multivariate.py b/pymc3/distributions/multivariate.py index 3bdce21e45..4eb6b01817 100644 --- a/pymc3/distributions/multivariate.py +++ b/pymc3/distributions/multivariate.py @@ -40,7 +40,7 @@ from pymc3.aesaraf import floatX, intX from pymc3.distributions import transforms -from pymc3.distributions.continuous import ChiSquare, Normal +from pymc3.distributions.continuous import ChiSquared, Normal from pymc3.distributions.dist_math import bound, factln, logpow from pymc3.distributions.distribution import Continuous, Discrete from pymc3.distributions.special import gammaln, multigammaln @@ -905,7 +905,7 @@ def WishartBartlett(name, S, nu, is_cholesky=False, return_cholesky=False, testv tril_testval = None c = at.sqrt( - ChiSquare("%s_c" % name, nu - np.arange(2, 2 + n_diag), shape=n_diag, testval=diag_testval) + ChiSquared("%s_c" % name, nu - np.arange(2, 2 + n_diag), shape=n_diag, testval=diag_testval) ) pm._log.info("Added new variable %s_c to model diagonal of Wishart." % name) z = Normal("%s_z" % name, 0.0, 1.0, shape=n_tril, testval=tril_testval) diff --git a/pymc3/gp/gp.py b/pymc3/gp/gp.py index 31fbb41fe3..17e232f0c2 100644 --- a/pymc3/gp/gp.py +++ b/pymc3/gp/gp.py @@ -278,7 +278,7 @@ def _build_prior(self, name, X, reparameterize=True, **kwargs): cov = stabilize(self.cov_func(X)) shape = infer_shape(X, kwargs.pop("shape", None)) if reparameterize: - chi2 = pm.ChiSquare(name + "_chi2_", self.nu) + chi2 = pm.ChiSquared(name + "_chi2_", self.nu) v = pm.Normal(name + "_rotated_", mu=0.0, sigma=1.0, size=shape, **kwargs) f = pm.Deterministic(name, (at.sqrt(self.nu) / chi2) * (mu + cholesky(cov).dot(v))) else: diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index e7f7bed408..0e62ae2894 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -16,6 +16,7 @@ import sys import aesara +aesara.config.floatX = "float32" import aesara.tensor as at import numpy as np import numpy.random as nr @@ -48,7 +49,7 @@ Bound, Categorical, Cauchy, - ChiSquare, + ChiSquared, Constant, DensityDist, Dirichlet, @@ -1030,15 +1031,15 @@ def test_half_normal(self): lambda value, sigma: sp.halfnorm.logcdf(value, scale=sigma), ) - def test_chisquare(self): + def test_chisquared(self): self.check_logp( - ChiSquare, + ChiSquared, Rplus, {"nu": Rplus}, lambda value, nu: sp.chi2.logpdf(value, df=nu), ) self.check_logcdf( - ChiSquare, + ChiSquared, Rplus, {"nu": Rplus}, lambda value, nu: sp.chi2.logcdf(value, df=nu), diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 02ce2c51c5..47344c1272 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -747,8 +747,8 @@ class TestInverseGammaMuSigma(BaseTestDistribution): tests_to_run = ["check_pymc_params_match_rv_op"] -class TestChiSquare(BaseTestDistribution): - pymc_dist = pm.ChiSquare +class TestChiSquared(BaseTestDistribution): + pymc_dist = pm.ChiSquared pymc_dist_params = {"nu": 2.0} expected_rv_op_params = {"nu": 2.0} reference_dist_params = {"df": 2.0} From 163d0b0c679a330f946d93a0bdfeb03c00dd8450 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Fri, 4 Jun 2021 16:07:46 -0400 Subject: [PATCH 10/12] Minor fix: removed float32 specification --- pymc3/tests/test_distributions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 0e62ae2894..d3e4b7eb27 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -16,7 +16,6 @@ import sys import aesara -aesara.config.floatX = "float32" import aesara.tensor as at import numpy as np import numpy.random as nr From b18336ad7dd24937ee0147ff1b17f7a58ab2748a Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Mon, 7 Jun 2021 10:50:00 -0400 Subject: [PATCH 11/12] :sunny: underflow to -inf seems normal in float32 --- pymc3/tests/test_distributions.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index d3e4b7eb27..81b780a18e 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1030,13 +1030,19 @@ def test_half_normal(self): lambda value, sigma: sp.halfnorm.logcdf(value, scale=sigma), ) - def test_chisquared(self): + def test_chisquared_logp(self): self.check_logp( ChiSquared, Rplus, {"nu": Rplus}, lambda value, nu: sp.chi2.logpdf(value, df=nu), ) + + @pytest.mark.xfail( + condition=(aesara.config.floatX == "float32"), + reason="Fails on float32 due to numerical issues", + ) + def test_chisquared_logcdf(self): self.check_logcdf( ChiSquared, Rplus, From 074b3af86a521ddb1131464ce6391e2a680452b7 Mon Sep 17 00:00:00 2001 From: Larry Dong Date: Mon, 7 Jun 2021 10:53:23 -0400 Subject: [PATCH 12/12] Minor fix in documentation --- pymc3/distributions/continuous.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index ac57b7d471..cd2244b39e 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -2584,7 +2584,7 @@ class ChiSquared(PositiveContinuous): Parameters ---------- - nu: int + nu: float Degrees of freedom (nu > 0). """ rv_op = chisquare