pymc-devs / pymc-resources

PyMC educational resources
MIT License
1.96k stars 745 forks source link

Move to v4 for Rethinking 2 #194

Closed percevalve closed 2 years ago

percevalve commented 2 years ago

Move to v2 for Rethinking 2

Starting with Statistical Rethinking 2 as this is the one I know the best.

Was using the script from pymc-example (scripts/rerun.py)

Some of the file got transformed automatically:

This PR include #152 .

For Rethinking_2/Chp_04.ipynb:

Other change needed was when a Panda Series was used to define a model, this was triggering a Elementwise error, adding .values turned out to be enough.

As the traces are az.InferenceData, calculating the mean returns an array (I meant an Xarray), so added .item(0) to keep the compatibility with the formula as they exist, not sure this is the most elegant solution.

Same thing for the size of the posterior distribution, you cannot just do len(trace['var'], so I used .sizes["sample"], again not sure this is the most elegant solution, but it works.

For the #152 :

review-notebook-app[bot] commented 2 years ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

review-notebook-app[bot] commented 2 years ago

View / edit / reply to this conversation on ReviewNB

aloctavodia commented on 2022-06-02T16:59:54Z ----------------------------------------------------------------

This need to fixed https://github.com/pymc-devs/pymc/issues/5443

see how you are overestimating the standard deviation


review-notebook-app[bot] commented 2 years ago

View / edit / reply to this conversation on ReviewNB

aloctavodia commented on 2022-06-02T16:59:55Z ----------------------------------------------------------------

Line #2.    sns.kdeplot(x[4, :].flatten(), bw_method=0.01, ax=ax[0])

alternatively, we can use az.plot_kde


percevalve commented 2 years ago

Issue https://github.com/https://github.com/pymc-devs/pymc/issues/5443 seems to be over my pay grade, going to exclude Rethinking_2/Chp_02.ipynb from this PR.

Yes indeed, using az.plot_kde makes more sense, and removes a dependency with seaborn.

aloctavodia commented 2 years ago

Issue pymc-devs/pymc#5443 seems to be over my pay grade, going to exclude Rethinking_2/Chp_02.ipynb from this PR.

Yes indeed, using az.plot_kde makes more sense, and removes a dependency with seaborn.

Sorry for not being clear: This is the change needed in Rethinking_2/Chp_02.ipynb

data = np.repeat((0, 1), (3, 6))
with pm.Model() as m:
    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 = m.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]))
review-notebook-app[bot] commented 2 years ago

View / edit / reply to this conversation on ReviewNB

aloctavodia commented on 2022-06-03T14:57:08Z ----------------------------------------------------------------

This cell also needs to be fixed, similar to the previous one.