Skip to content

Bug in sampling Binomial with non-scalar shape #4223

Closed
@bridgeland

Description

@bridgeland

When a Binomial RV without observations has a vector shape, it samples incorrectly, sampling mostly zeros no matter the values of n and p. It works fine for scalar shapes.

Example:

import pymc3 as pm 
import numpy as np

with pm.Model() as model:
    pm.Binomial('scalar_binomial', n=1, p=0.25)
    pm.Binomial('arrayed_binomial', n=1, p=0.25, shape=10)
    pm.Binomial(
        'arrayed_binomial_alt', n=np.ones(10), 
        p=np.full(10, 0.25), shape=10)
    
    trace = pm.sample(draws=600, chains=2, tune=500)
    summary = pm.summary(
        trace, 
        var_names=[
            'scalar_binomial', 'arrayed_binomial', 
            'arrayed_binomial_alt'])
summary 

The three RVs are identical binomials, except that the first RV scalar_binomial is a scalar, the second RV arrayed_binomial has shape 10 with scalar arguments, and the third RV arrayed_binomial_alt has shape 10 with (identical) arrayed arguments. Note also that nothing in this model is constrained by observations.

Sampling issues warnings about all-NaN slices:

image

The results are peculiar:

image

scalar_binomial is as expected. arrayed_binomial sees no non-zero samples in this execution, and means of 0.0. Other executions do see some non-zero samples. arrayed_binomial_alt sees no non-zero binomial samples, in this execution or in any others. Everything is nothing.

The same model structure with normals instead of binomials produces expected results:

import pymc3 as pm 
import numpy as np

with pm.Model() as model:
    pm.Normal('scalar_normal', mu=1, sigma=0.25)
    pm.Normal('arrayed_normal', mu=1, sigma=0.25, shape=10)
    pm.Normal(
        'arrayed_normal_alt', mu=np.ones(10), 
        sigma=np.full(10, 0.25), shape=10)
    
    trace = pm.sample(
        draws=600, chains=2, tune=500, 
        step=pm.Metropolis())      # Metropolis so the sampling is identical to above
    summary = pm.summary(
        trace, 
        var_names=[
            'scalar_normal', 'arrayed_normal', 'arrayed_normal_alt'])
summary

Normal results:

image

Versions and main components

  • PyMC3 Version: 3.9.3
  • Theano Version: 1.0.5
  • Python Version: 3.8.5
  • Operating system: macOS 10.15.7
  • How did you install PyMC3: conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions