pm.Deterministic remains unchanged although the parameters are changing

Hi all,

When I tried to debug my program, I arrived to some kind of misunderstanding. I definitely see that random nodes are changing, but pm.Deterministic remains the same.

For example, the following code:

with pm.Model() as model:    
    D = tt.as_tensor_variable(5, dtype="int64") 
    
    μ = pm.HalfCauchy('μ', 5.0, initval = 1.0)
    σ = pm.HalfCauchy('σ', 6.0, initval = 2.0)
    
    ### Lognormal distribution
    σlog = tt.sqrt(tt.log((σ / μ)**2 + 1));
    μlog = tt.log(μ) - σlog**2 / 2;
    comp = pm.Lognormal.dist(mu = μlog, sigma = σlog)
    p, _ = ae.scan(lambda t: tt.exp(pm.logcdf(comp, t + 0.5)), sequences = tt.arange(0, D + 1))
    pm.Deterministic('p', p)
  
    trace = pm.sample(3, pm.Metropolis(), tune=0, chains=1, progressbar=False, init='advi')

produces the output:

display(trace.posterior['μ'][0].values)
display(trace.posterior['σ'][0].values)
trace.posterior['p'][0].values
array([1.26028111, 0.42585018, 0.53549947])
array([2.        , 2.77220024, 2.4475724 ])
array([[0.2183644 , 0.51650629, 0.66358024, 0.7495661 , 0.80524404,
        0.84378139],
       [0.2183644 , 0.51650629, 0.66358024, 0.7495661 , 0.80524404,
        0.84378139],
       [0.2183644 , 0.51650629, 0.66358024, 0.7495661 , 0.80524404,
        0.84378139]])

as you can see μ and σ are changing, but p remains the same. In my opinion, p should be updated also, because the distribution is also changing.

The second minor observation, the initval remains valid only when I run my code the first time, but then their values are neglected. I wonder if it is the intended behavior.

Thank you for your help and any advice would be very welcome (as I have already spent quite a lot of time debugging)

-Andrei

You’re right that p should be updating. I think this is related to how random number generation was handled inside Scan Ops in v3. Can you upgrade to v4? This issue was fixed when the asesara team did a large refactoring of how random variables work inside computational graphs. Otherwise you’ll have to dig up the old documentation about how to handle this situation.

Actually, I am on version 4 :confused:

the gist is here: 20221014-pymc-question.ipynb · GitHub

Would you have some chance to test it on your system?

Ah, I jumped to a conclusion because of the tt. in the code.

Why are you using .dist with comp? This will also cause problems with updating. comp = pm.Lognormal('comp', mu = μlog, sigma = σlog) should work?

Edit: No, it didn’t. I’ll look at it more closely.

Edit2: I think it’s just because you’re calling pm.sample without a likelihood term (i.e. there’s no data). If you call pm.sample_prior_predictive instead, p will update according to draws from the prior distributions over mu and sigma. But you should also not be using the .dist method in this case.

1 Like

Oh, you are absolutely correct with “Edit2” guess! Many thanks!

The reason why I had no likelihood in my program, because it was a part of my debugging process when I tried to narrow my search. So I removed the likelihood. I will report with the likelihood later, but for now - it looks like the solution of my problem.

Just for a note: In my setting, I do some kind of wrapper around discretizing the lognormal CDFs. Previously I used the same coding and it looked legit.

Cool, if you know what you’re doing carry on of course. I don’t know how information flows though .dist nodes in the computational graph, though. I thought I read that they aren’t updated like standard random variable nodes, but I don’t have any citation on hand for you. @ricardoV94 ?