nutpie icon indicating copy to clipboard operation
nutpie copied to clipboard

Nutpie and PYMC Hurdle Gamma Distribution

Open zwelitunyiswa opened this issue 1 year ago • 8 comments

Pardon my ignorance, but I cannot get nutpie to sample from PYMC's hurdle-gamma likelihood.

I get "TypeError: The fgraph of ScalarLoop must be exclusively composed of scalar operations."

Is this just not possible at this time?

Screenshot 2024-12-03 at 11 18 51 AM

zwelitunyiswa avatar Dec 03 '24 16:12 zwelitunyiswa

This looks like an issue with the numba backend of pytensor. Can you provide an example I can run to further debug this?

In the meantime, you can also give the jax backend a go:

compiled = nutpie.compile_pymc_model(model, backend="jax", gradient_backend="jax")
trace = nutpie.sample(compiled)

aseyboldt avatar Dec 03 '24 16:12 aseyboldt

Sure. Here is a simple model that replicates the error:

x = pm.HurdleGamma.dist(psi=.2, alpha=1, beta=2)
x_draws = pm.draw(x, draws=500, random_seed=1)
data = pd.DataFrame({'variable':x_draws})

with pm.Model() as model:
    
    alpha = pm.HalfNormal("alpha", sigma=2)
    beta = pm.HalfNormal("beta", sigma=2)
    psi = pm.Beta("psi", alpha=2, beta=2)

    obs = pm.HurdleGamma("obs", psi=psi, alpha=alpha, beta=beta, observed=data)

    idata = pm.sample(nuts_sampler='nutpie')

This fails with the same "TypeError: The fgraph of ScalarLoop must be exclusively composed of scalar operations."

zwelitunyiswa avatar Dec 03 '24 17:12 zwelitunyiswa

compiled = nutpie.compile_pymc_model(model, backend="jax", gradient_backend="jax")
trace = nutpie.sample(compiled)

I tried that with the simple model above and I got this a "RuntimeError: All initialization points failed" error. Image attached.

Screenshot 2024-12-03 at 12 18 33 PM

zwelitunyiswa avatar Dec 03 '24 17:12 zwelitunyiswa

Sorry for the delay. I opened a pytensor issue for the problem.

aseyboldt avatar Dec 17 '24 09:12 aseyboldt

No worries. Thank you for looking into it and opening the issue.

zwelitunyiswa avatar Dec 17 '24 19:12 zwelitunyiswa

@aseyboldt I installed the current dev version of pytensor as I saw that they had resolved the issue you posted there #https://github.com/pymc-devs/pytensor/issues/1130.

The hurdle-gamma now runs via Nutpie. I tested it on the sample script #https://github.com/pymc-devs/nutpie/issues/163#issuecomment-2515135570 and it ran. However, it drops down into object mode for same parameters and is slower than pymc's native sampler for this sample code and on some proprietary data and model I have (10 seconds via pymc default sampler vs 12 min for nutpie for my prop).

I am not sure if this should go to another issue and this one should be closed. It seems that it is more a problem with the hurdle-gamma implementation in PYMC #https://github.com/pymc-devs/pymc/issues/7630#issue-2761073423 . Please let me know if I should close this issue.

Screenshot 2025-01-08 at 5 56 53 AM

zwelitunyiswa avatar Jan 08 '25 11:01 zwelitunyiswa

Thanks for the update. I think you are right, there still seems to be a missing implementation for an op in pytensor in the gradient of the log density. I'll open a separate issue for that.

aseyboldt avatar Jan 08 '25 13:01 aseyboldt

We should also just implement hurdles properly. There's no reason to involve the cdf as it's just a trick for Mixture to work with a Continuous and the DiracDelta(0) components.

We are truncating the continuous component at epsilon, and that's why those Ops show up in the grad.

ricardoV94 avatar Jan 08 '25 13:01 ricardoV94

@ricardoV94 to implement it properly, you mean write the density in a straightforward way instead of using all this Mixture and truncation logic in _hurdle_mixture right?

zaxtax avatar Apr 21 '25 19:04 zaxtax

@ricardoV94 to implement it properly, you mean write the density in a straightforward way instead of using all this Mixture and truncation logic in _hurdle_mixture right?

Yup 👍

ricardoV94 avatar Apr 21 '25 19:04 ricardoV94

I'll close this here, we still track the underlying issue in pymc-devs/pytensor#1149

aseyboldt avatar May 28 '25 19:05 aseyboldt