Skip to content

BUG: basic.icdf creates tensor variable of type of the distribution, rather than probability type (float64?) #6648

Closed
@gokuld

Description

@gokuld

Describe the issue:

In basic.py, we have this definition of the icdf helper function:

def icdf(rv: TensorVariable, value: TensorLike, **kwargs) -> TensorVariable:
    """Create a graph for the inverse CDF of a  Random Variable."""
    value = pt.as_tensor_variable(value, dtype=rv.dtype)
    try:
        return _icdf_helper(rv, value, **kwargs)
    except NotImplementedError:
        # Try to rewrite rv
        fgraph, rv_values, _ = construct_ir_fgraph({rv: value})
        [ir_rv] = fgraph.outputs
        return _icdf_helper(ir_rv, value, **kwargs)

I specifically have a concern about value = pt.as_tensor_variable(value, dtype=rv.dtype)
Should not the input type to icdf function be a floating point type? (Type of probability value).
rv.dtype can be integer too, such as for discrete distributions and is not the right type for the probability value input to the icdf function.

Reproduceable code example:

import pytensor as pt
import pymc as pm
from pymc.logprob.basic import icdf
from pymc.pytensorf import inputvars

dist = pm.Geometric.dist(p=0.1)
dist_icdf = icdf(dist, dist.type())
dist_icdf_fn = pt.function(list(inputvars(dist_icdf)), dist_icdf)
dist_icdf_fn(0.1)

Error message:

TensorType(int64, ()) cannot store accurately value 0.1, it would be represented as 0. If you do not mind this precision loss, you can: 1) explicitly convert your data to a numpy array of dtype int64, or 2) set "allow_input_downcast=True" when calling "function".

PyMC version information:

pytensor: 2.10.1
Python: 3.10.10
PyMC: latest commit b7764dd

Context for the issue:

I would like to implement a util function for a self consistency test for ICDF and logcdf of discrete distributions.

That is, to check if:
value = icdf(dist, exp(logcdf(dist, value)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions