Skip to content
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

BUG: pymc.find_hessian() returns incorrect value for version 5.10.3 #7082

Closed
mghanava opened this issue Dec 29, 2023 · 3 comments
Closed

BUG: pymc.find_hessian() returns incorrect value for version 5.10.3 #7082

mghanava opened this issue Dec 29, 2023 · 3 comments
Labels

Comments

@mghanava
Copy link

Describe the issue:

Hi developers,

I have analytically found the second derivative of following posterior at MAP of 2/3 to be 0.15713. The likelihood is simply Binomial with a Uniform prior on [0, 1]. I can get the exact analytical value from an older version like 4.0.0 but in the new version it returns 0.64!
Pr \left( p \vert W, L \right) &= 840 p^6 \left( 1- p \right)^3

Reproduceable code example:

import numpy as np
import pymc as pm

data = np.repeat((0, 1), (3, 6))
with pm.Model() as normal_approximation:
    p = pm.Uniform("p", 0, 1)  # uniform priors
    w = pm.Binomial("w", n=len(data), p=p, observed=data.sum())  # binomial likelihood
    mean_q = pm.find_MAP()

    p_value = normal_approximation.rvs_to_values[p]
    p_value.tag.transform = None
    p_value.name = p.name

    std_q = ((1 / pm.find_hessian(mean_q, vars=[p])) ** 0.5)[0]

# display summary of quadratic approximation
print("Mean, Standard deviation\np {:.2}, {:.2}".format(mean_q["p"], std_q[0]))

Error message:

No response

PyMC version information:

pymc 5.10.3 hd8ed1ab_0 conda-forge
pymc-base 5.10.3 pyhd8ed1ab_0 conda-forge
pyparsing 3.1.1 pyhd8ed1ab_0 conda-forge
pytensor 2.18.4 py310hc6cd4ac_0 conda-forge
pytensor-base 2.18.4 py310hcc13569_0 conda-forge
python 3.10.13 hd12c33a_1_cpython conda-forge
Running on linux through WSL2 (windows 11)
environment created through conda from conda-forge channel

Context for the issue:

I encountered this issue for the statistical rethinking course. You can refer to the code 2.6 in the provided notebook.
https://github.com/pymc-devs/pymc-resources/blob/main/Rethinking_2/Chp_02.ipynb

@mghanava mghanava added the bug label Dec 29, 2023
Copy link

welcome bot commented Dec 29, 2023

Welcome Banner
🎉 Welcome to PyMC! 🎉 We're really excited to have your input into the project! 💖

If you haven't done so already, please make sure you check out our Contributing Guidelines and Code of Conduct.

@ricardoV94
Copy link
Member

ricardoV94 commented Dec 30, 2023

This is a duplicate of #5443

In the current versions it matters if you define the model with transforms or not. This exact example is used im the docstrings of this utility: https://www.pymc.io/projects/docs/en/stable/api/model/generated/pymc.model.transform.conditioning.remove_value_transforms.html

I imagine you are looking for the value of the hessian with the transform, but you removed it?

Let me know if I am missing something.

@mghanava
Copy link
Author

Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants