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

Allow OrderedProbit distribution to take vector inputs #5418

Merged
merged 5 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,9 @@ def dist(cls, eta, cutpoints, sigma=1, *args, **kwargs):
_log_p = at.concatenate(
[
at.shape_padright(normal_lccdf(0, sigma, probits[..., 0])),
log_diff_normal_cdf(0, sigma, probits[..., :-1], probits[..., 1:]),
log_diff_normal_cdf(
0, at.shape_padright(sigma), probits[..., :-1], probits[..., 1:]
),
at.shape_padright(normal_lcdf(0, sigma, probits[..., -1])),
],
axis=-1,
Expand Down
33 changes: 32 additions & 1 deletion pymc/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np
import numpy.random as nr
import numpy.testing as npt
import pandas as pd
import pytest
import scipy.stats as st

Expand Down Expand Up @@ -368,7 +369,7 @@ def check_pymc_params_match_rv_op(self):
assert_almost_equal(expected_value, actual_variable.eval(), decimal=self.decimal)

def check_rv_size(self):
# test sizes
# test sizes
sizes_to_check = self.sizes_to_check or [None, (), 1, (1,), 5, (4, 5), (2, 4, 2)]
sizes_expected = self.sizes_expected or [(), (), (1,), (1,), (5,), (4, 5), (2, 4, 2)]
for size, expected in zip(sizes_to_check, sizes_expected):
Expand Down Expand Up @@ -1698,6 +1699,35 @@ class TestOrderedProbit(BaseTestDistributionRandom):
"check_rv_size",
]

def test_vector_inputs(self):
"""
This test checks when providing vector inputs for `eta` and `sigma` parameters using advanced indexing.
"""
df = pd.DataFrame({
'X' : ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B'],
'Y' : [1, 1, 1, 2, 2, 3, 4, 5, 1, 1, 1, 1, 2, 2, 3, 3]
})

df.Y = df.Y.astype(int)
grp_idx = pd.Categorical(df.X).codes
K = df.Y.nunique()

with pm.Model() as opb:
cutpoints = pm.Normal("cutpoints", 0.0, 1.5, shape=K-1,
transform=pm.distributions.transforms.ordered,
initval=np.arange(K-1))

mu = pm.Normal("mu", mu=K/2, sd=K, shape=2)
sigma = pm.HalfNormal("sigma", 1, shape=2)

y_obs = pm.OrderedProbit("y_obs",
cutpoints=cutpoints,
eta=mu[grp_idx],
sigma=sigma[grp_idx],
observed=df.Y-1)

assert df.Y.shape == y_obs.eval().shape


class TestOrderedMultinomial(BaseTestDistributionRandom):
pymc_dist = _OrderedMultinomial
Expand Down Expand Up @@ -1825,6 +1855,7 @@ def check_errors(self):
shape=15,
)


def check_random_variable_prior(self):
"""
This test checks for shape correctness when using MatrixNormal distribution
Expand Down