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

Create Ordered Multinomial distribution #4773

Merged
merged 20 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions pymc3/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
Multinomial,
MvNormal,
MvStudentT,
OrderedMultinomial,
Wishart,
WishartBartlett,
)
Expand Down Expand Up @@ -159,6 +160,7 @@
"Dirichlet",
"Multinomial",
"DirichletMultinomial",
"OrderedMultinomial",
"Wishart",
"WishartBartlett",
"LKJCholeskyCov",
Expand Down
27 changes: 27 additions & 0 deletions pymc3/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"Dirichlet",
"Multinomial",
"DirichletMultinomial",
"OrderedMultinomial",
"Wishart",
"WishartBartlett",
"LKJCorr",
Expand Down Expand Up @@ -687,6 +688,32 @@ def _distr_parameters_for_repr(self):
return ["n", "a"]


class OrderedMultinomial(Multinomial):
rv_op = multinomial
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def dist(cls, eta, cutpoints, n, compute_p=True, *args, **kwargs):
eta = at.as_tensor_variable(floatX(eta))
cutpoints = at.as_tensor_variable(cutpoints)
n = at.as_tensor_variable(n)
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved

pa = sigmoid(cutpoints - at.shape_padright(eta))
p_cum = at.concatenate(
[
at.zeros_like(at.shape_padright(pa[..., 0])),
pa,
at.ones_like(at.shape_padright(pa[..., 0])),
],
axis=-1,
)
if compute_p:
p = pm.Deterministic("complete_p", p_cum[..., 1:] - p_cum[..., :-1])
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved
else:
p = p_cum[..., 1:] - p_cum[..., :-1]

return super().dist(n, p, *args, **kwargs)


def posdef(AA):
try:
linalg.cholesky(AA)
Expand Down