Skip to content

BUG: Truncated gamma distribution has incorrect logp #6931

Closed
@tvwenger

Description

@tvwenger

Describe the issue:

A Gamma distribution that is wrapped in Truncated has the wrong logp when beta is small. For beta = 1.0 the results are correct, but for beta = 0.01 the results are incorrect. The code below produces the following distributions.

plot_gamma_prob(1.0, 5.0) # beta = 1.0, truncated at 5.0

gamma_beta1 0

plot_gamma_prob(0.01, 1000.0) # beta = 0.01, truncated at 1000.0

gamma_beta0 01

Reproduceable code example:

import numpy as np
import matplotlib.pyplot as plt
import pymc as pm
print(pm.__version__)

def plot_gamma_prob(beta, x_max):
    x = np.linspace(0.0, 2.0 * x_max, 1000)

    gamma = pm.Gamma.dist(alpha=3.0, beta=beta)
    p_gamma = np.exp(pm.logp(gamma, x).eval())

    trunc_gamma = pm.Truncated.dist(pm.Gamma.dist(alpha=3.0, beta=beta), upper=x_max)
    p_trunc_gamma = np.exp(pm.logp(trunc_gamma, x).eval())

    fig, ax = plt.subplots()
    ax.plot(x, p_gamma, "k-", label="Gamma")
    ax.plot(x, p_trunc_gamma, "r-", label="Truncated Gamma")
    ax.set_xlabel("x")
    ax.set_ylabel("P(x)")
    ax.legend(loc="best")
    fig.tight_layout()
    fig.savefig(f"gamma_beta{beta}.png")
    plt.close(fig)

plot_gamma_prob(1.0, 5.0)
plot_gamma_prob(0.01, 1000.0)

Error message:

No response

PyMC version information:

pymc v. 5.8.2

Context for the issue:

This seems like an important bug!

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