Skip to content

Test multinomial distribution with probability vector containing zeros #4487

Closed
@ricardoV94

Description

@ricardoV94

A previous bug in the logp of the Multinomial made it return nan when the probability vector contained zeros. This was behind this issue #3069, and was fixed by #3059

The minimal example of issues #3069 is shown below. It had to do with with the last row of the state_label_tran which includes a zero-probability for the first class.

import numpy as np
import pymc3 as pm

state_label_tran = np.array([
    [0.3, 0.2, 0.5],
    [0.1, 0.5, 0.4],
    [0.0, 0.05, 0.95],  # Would work fine without this row
])
shape = len(state_label_tran)

with pm.Model() as m:
    label_dist = [
        pm.Multinomial.dist(p=state_label_tran[i], n=1, shape=shape)
        for i in range(len(state_label_tran))
    ]
    label = pm.Mixture('label', w=np.ones(shape)/shape, comp_dists=label_dist, observed=[0, 1, 0])

print(label.logp())  # nan before PR #3059, and -1.386... afterwards

But it's enough to check the Multinomial directly:

with pm.Model() as m:
    label = pm.Multinomial('m', n=1, p=[0, 0.05, 0.95], observed=[0, 1, 0])
print(label.logp())  # nan before, and -2.9957 afterwards

None of our current Multinomial tests check for a probability vector that includes zeros (except for test_batch_dirichlet_multinomial which sometimes does so randomly). We should add one.

Originally posted by @ricardoV94 in #3069 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions