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 4 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
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
48 changes: 48 additions & 0 deletions pymc/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,26 @@ class TestOrderedLogistic(BaseTestDistributionRandom):
"check_rv_size",
]

@pytest.mark.parametrize(
"eta, cutpoints, expected",
[
(0, [-2.0, 0, 2.0], (4,)),
([-1], [-2.0, 0, 2.0], (1, 4)),
([1.0, -2.0], [-1.0, 0, 1.0], (2, 4)),
([[1.0, -1.0, 0.0], [-1.0, 3.0, 5.0]], [-2.0, 0, 1.0], (2, 3, 4)),
],
)
def test_shape_inputs(self, eta, cutpoints, expected):
danhphan marked this conversation as resolved.
Show resolved Hide resolved
"""
This test checks when providing different shapes for `eta` parameters.
"""
categorical = _OrderedLogistic.dist(
eta=eta,
cutpoints=cutpoints,
)
p = categorical.owner.inputs[3].eval()
assert p.shape == expected


class TestOrderedProbit(BaseTestDistributionRandom):
pymc_dist = _OrderedProbit
Expand All @@ -1698,6 +1718,34 @@ class TestOrderedProbit(BaseTestDistributionRandom):
"check_rv_size",
]

@pytest.mark.parametrize(
"eta, cutpoints, sigma, expected",
[
(0, [-2.0, 0, 2.0], 1.0, (4,)),
([-1], [-2.0, 0, 2.0], [2.0], (1, 4)),
([1.0, -2.0], [-1.0, 0, 1.0], 1.0, (2, 4)),
([1.0, -2.0, 3.0], [-2.0, 0, 2.0], [-1.0, -2.0, 5.0], (3, 4)),
([[1.0, -1.0, 0.0], [-1.0, 3.0, 5.0]], [-2.0, 0, 1.0], [-1.0, -2.0, 5.0], (2, 3, 4)),
(
[[1.0, -2.0, 3.0], [1.0, 2.0, -4.0]],
[-2.0, 0, 1.0],
[[0.0, 2.0, -4.0], [-1.0, 1.0, 3.0]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these sigma are negative. We should test with valid sigma values (> 0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test with 2d cutpoints missing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ricardoV94, I will change the sigma to all positive.

Also, cutpoints should be always 1 dimension (to my understanding) as it represents (n-1) cut points of a categorical feature with n categories. I am not sure if there is any cases that needs 2d cutpoints.

Copy link
Member

@ricardoV94 ricardoV94 Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our distributions, when possible, can always be "batched". That means we can arbitrarily increase the dimensionality of the distribution by adding parameters with more dimensions. The last axes represent the parameters for each "atomic" distribution in the batch

For example the Categorical distribution is happy to take 2D, 3D, ... ND dimensional probability parameters, as long as they add up to 1 over the last axis

pm.Categorical.dist(
  np.full((4, 2, 3), [
    [0., .1, .9],
    [.9, .1, 0]
  ])
).eval()

The same should apply here if possible. See this issue where we are pursuing this for all multivariate distributions: #5383

The reason why this is useful is vectorization. Specifying a (3, 3) shaped distribution with different cutpoints can be much more efficient than specifying 3 times a (3,) shaped distribution with the different cutpoints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was exactly how this issue started by the way, just with the batching across sigma and eta, and fixed cutpoints. But it could have been the other way around.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi yes, the batch dimension totally make sense. My initial thought was that for dealing with a large data set, batch_size should be managed in pm.Data (similar to DataLoader in pytorch). Although I have not checked pm.Data yet :)

Anyways, I will check the case of 2d cutpoints as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe now my crazy example from before makes more sense?

OrderedProbit.dist(
  eta=np.zeros((5, 2)), 
  sigma=np.ones((2, 5, 2)), 
  cutpoints=[[-2, 0, 2], [-2, 0, 2]]
)  #shape should be (2, 5, 2, 4)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, it makes sense. I have added the tests for 2d cutpoints and positve sigma.

Already run push, but not sure why it has not updated in this PR:
danhphan@9b311bf

(2, 3, 4),
),
],
)
def test_shape_inputs(self, eta, cutpoints, sigma, expected):
"""
This test checks when providing different shapes for `eta` and `sigma` parameters.
"""
categorical = _OrderedProbit.dist(
eta=eta,
cutpoints=cutpoints,
sigma=sigma,
)
p = categorical.owner.inputs[3].eval()
assert p.shape == expected


class TestOrderedMultinomial(BaseTestDistributionRandom):
pymc_dist = _OrderedMultinomial
Expand Down