Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MvStudentT.random #4359

Merged
merged 2 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is the first release to support Python3.9 and to drop Python3.6.
- In `sample_posterior_predictive` the `vars` kwarg was removed in favor of `var_names` (see [#4343](https://github.com/pymc-devs/pymc3/pull/4343)).
- The notebook gallery has been moved to https://github.com/pymc-devs/pymc-examples (see [#4348](https://github.com/pymc-devs/pymc3/pull/4348)).
- `math.logsumexp` now matches `scipy.special.logsumexp` when arrays contain infinite values (see [#4360](https://github.com/pymc-devs/pymc3/pull/4360)).
- Fixed mathematical formulation in `MvStudentT` random method. (see [#4359](https://github.com/pymc-devs/pymc3/pull/4359))

## PyMC3 3.10.0 (7 December 2020)

Expand Down
10 changes: 6 additions & 4 deletions pymc3/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ class MvStudentT(_QuadFormBase):
1+\frac{1}{\nu}
({\mathbf x}-{\mu})^T
{\Sigma}^{-1}({\mathbf x}-{\mu})
\right]^{(\nu+p)/2}}
\right]^{-(\nu+p)/2}}

======== =============================================
Support :math:`x \in \mathbb{R}^k`
Support :math:`x \in \mathbb{R}^p`
Mean :math:`\mu` if :math:`\nu > 1` else undefined
Variance :math:`\frac{\nu}{\mu-2}\Sigma`
if :math:`\nu>2` else undefined
Expand Down Expand Up @@ -393,8 +393,10 @@ def random(self, point=None, size=None):

samples = dist.random(point, size)

chi2 = np.random.chisquare
return (np.sqrt(nu) * samples.T / chi2(nu, size)).T + mu
chi2_samples = np.random.chisquare(nu, size)
# Add distribution shape to chi2 samples
chi2_samples = chi2_samples.reshape(chi2_samples.shape + (1,) * len(self.shape))
return (samples / np.sqrt(chi2_samples / nu)) + mu

def logp(self, value):
"""
Expand Down
6 changes: 3 additions & 3 deletions pymc3/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,9 @@ def ref_rand_evd(size, mu, evds, sigma):

def test_mv_t(self):
def ref_rand(size, nu, Sigma, mu):
normal = st.multivariate_normal.rvs(cov=Sigma, size=size).T
chi2 = st.chi2.rvs(df=nu, size=size)
return mu + np.sqrt(nu) * (normal / chi2).T
normal = st.multivariate_normal.rvs(cov=Sigma, size=size)
chi2 = st.chi2.rvs(df=nu, size=size)[..., None]
return mu + (normal / np.sqrt(chi2 / nu))

for n in [2, 3]:
pymc3_random(
Expand Down