Weibull Adstock Function

Hi, I am using PYMC 5, for marketing modeling. One of the steps during the model is to transform the variables using an adstock function. I am trying to use Weibull Probability Density Function (PDF) here:

def weibull_pdf(x, lambda_, k):
    x = np.asarray(x)
    pdf = (k / lambda_) * (x / lambda_)**(k - 1) * np.exp(-((x / lambda_)**k))
    return pdf

This function is giving me an error message and I think with aesara there is an issue where this cannot be into the adequate variable type during the process.

import aesara.tensor as at

def weibull_pdf(x, lambda_, k):
    x = at.as_tensor_variable(x)
    pdf = (k / lambda_) * (x / lambda_)**(k - 1) * at.exp(-((x / lambda_)**k))
    return pdf

A fake model to test the function:

with pm.Model() as model:
    
    lambda_ = pm.Exponential('lambda_', 1.0)
    k = pm.Exponential('k', 1.0)
    x = np.linspace(0, 5, 100)

    weibull = weibull_pdf(x, lambda_, k)
    y_obs = pm.Normal('y_obs', mu=weibull, sigma=0.1, observed=np.random.normal(weibull, 0.1))

    trace = pm.sample(1000)
NotImplementedError: Cannot convert lambda_ to a tensor variable.

PS: Chatgpt cannot help on this…
Thank you

Welcome!

A couple of things. First, if you are using PyMC v5.x, you should not have aesara installed. Second, you shouldn’t need to convert it to a tensor in your weibull_pdf() function. Hopefully that helps. Your function works just fine for me on PyMC 5.8 and 5.9.

1 Like

Converting is fine, if it’s already a TensorVariable nothing will happen. Just chabge your inport to import pytensor.tensor as pt and replace every at for a pt

1 Like

Thank you! you are right, no need for aesara, after removing aesara I just replaced the at.exp by pm.math.exp.

1 Like