-
Notifications
You must be signed in to change notification settings - Fork 130
Optimize log1mexp(log1mexp(x)) -> x
#471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looks like numerical precision issues: import pymc as pm
import numpy as np
import pandas as pd
import pytensor.tensor as pt
rng = np.random.default_rng(42)
X = pd.DataFrame(rng.normal(size=(30, 5)))
y = rng.choice(np.arange(1, 12, 1), size=30)
cens = rng.choice([0, 1], size=30, p=[0.7, 0.3])
with pm.Model() as model:
X_data = pm.MutableData('X_data_obs', X)
beta = pm.Normal("beta", 0.0, 1, shape=5)
mu = pm.Normal('mu', 0, 1)
s = pm.HalfNormal('s', 5.)
eta = pm.math.dot(beta, X_data.T)
reg = pt.exp(-( (mu + eta)) / s)
weibull_dist = pm.Weibull.dist(beta=reg, alpha=s)
pot1 = pm.Potential("pot1", pt.log1mexp(pm.logcdf(weibull_dist, y)))
pot2 = pm.Potential("pot2", -((y / reg) ** s))
a, b = model.compile_logp([pot1, pot2], sum=False)(model.initial_point())
list(zip(list(a), list(b)))
|
Funnily enough the logcdf is just x = pt.scalar("x")
y = pt.log1mexp(pt.log1mexp(x))
y.eval({x: -1000}) # array(-inf) Should be easy to add a "useless rewrite" |
log1mexp(log1mexp(x)) -> x
Not at all familiar with pytensor but is that as easy as it sounds? |
The optimization is easy. Would look something like this pytensor/pytensor/tensor/rewriting/math.py Line 359 in 081a0b4
This won't fix the issue on the PyMC model because there's currently a switch between the two log1mexp to check if the parameters a,b are valid. But if you set We could get rid of this switch by checking it must be positive but that's a story for another time. |
Describe the issue:
As discussed in pymc-devs/pymc-examples#580 (comment)
There are problems invoking the pm.Censored functionality when trying to building Weibull AFT models using pm.Censored instead of the hand-crafted likelihood pattern using Potentials.
Reproduceable code example:
Error message:
Context for the issue:
Trying to use pm.Censored pattern for survival regression models in a PyMC example rather than the pm.Potential pattern
The text was updated successfully, but these errors were encountered: