Skip to content

documenting get_moment #5244

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

Merged
merged 6 commits into from
Dec 12, 2021
Merged

Conversation

amyoshino
Copy link
Member

@amyoshino amyoshino commented Dec 7, 2021

This PR updates the file developer_guide_implementing_distribution.md documenting the get_moment() method based on the issue #5210

@ricardoV94 I included your instructions from #5078 in the docs where I think they fit better in. Let me know what you think or if I missed anything.

Closes #5210

@ricardoV94 ricardoV94 changed the title documenting get_model documenting get_moment Dec 8, 2021
@@ -202,6 +212,10 @@ blah = pm.Blah.dist([0, 0], [1, 2])
blah.eval()
# array([0.62778803, 1.95165513])

# Test the get_moment
pm.distributions.distribution.get_moment(blah).eval()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pm.distributions.distribution.get_moment(blah).eval()
from pm.distributions.distribution import get_moment
get_moment(blah).eval()

from pymc.distributions import Blah
from pymc.initial_point import make_initial_point_fn

def assert_moment_is_expected(model, expected):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't copy the body of assert_moment_is_expected, just mention it exists, what it does and and how it should be used (which you show below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! That makes sense if eventually the function assert_moment_is_expected changes in the test file we don't have to track the changes and update the documentation. I will work on these changes a commit them soon.

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I left some suggestions below

revision_001 after the first commit.
@amyoshino
Copy link
Member Author

I changed and committed a new file based on your suggestions.
I re-read it all and found two places I forgot to mention the get_method method in the objectives of this document, those were added as well.

Again, thanks for reviewing!

@@ -202,6 +213,10 @@ blah = pm.Blah.dist([0, 0], [1, 2])
blah.eval()
# array([0.62778803, 1.95165513])

# Test the get_moment
get_moment(blah).eval()
# array([0.5, 1. ])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These would be array([0., 0.]) with the pseudo code above no?

Copy link
Member Author

@amyoshino amyoshino Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I'm getting from get_moment() for blah = pm.Uniform.dist([0, 0], [1, 2])

I did some tests and I think this is the right output. Considering that the first argument is location and second is scale (or lower and upper), then first interval is [0, 1] and second interval is [0, 2]. With the formula mean = 0.5 * [a, b], then we have [0.5, 1.0]. I compared with scipy implementation and got the same result. Let me now if I am mistaken here :)

import pymc as pm
from pymc.distributions.distribution import get_moment
from scipy.stats import uniform 

# PyMC implementation
blah = pm.Uniform.dist([0, 0], [1, 2])
get_moment(blah).eval()
>> array([0.5, 1. ])

# spicy implementation
uniform.mean([0, 0], [1, 2])
>> array([0.5, 1. ])

I see that there is an error in pre-commit / pre-commit (pull_request) that is related with Trim Trailing Whitespace. Is this something I did wrong? Let me know how I can fix it.

Thank you,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comments & code here are correct. I think Ricardo referred to the example implementation above.

The test case further down also assumes that Blah == Normal, so maybe make this example also with a normal get_moment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification! Yes, I agree it makes sense to keep it uniform by changing this example from Uniform to Normal. I will commit the changes below.

@codecov
Copy link

codecov bot commented Dec 8, 2021

Codecov Report

Merging #5244 (db1336e) into main (95bd5e5) will increase coverage by 0.19%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5244      +/-   ##
==========================================
+ Coverage   78.94%   79.14%   +0.19%     
==========================================
  Files          88       88              
  Lines       14237    14306      +69     
==========================================
+ Hits        11240    11323      +83     
+ Misses       2997     2983      -14     
Impacted Files Coverage Δ
pymc/distributions/dist_math.py 86.78% <0.00%> (-0.92%) ⬇️
pymc/sampling_jax.py 0.00% <0.00%> (ø)
pymc/initial_point.py 100.00% <0.00%> (ø)
pymc/distributions/bound.py 100.00% <0.00%> (ø)
pymc/distributions/discrete.py 98.46% <0.00%> (+0.10%) ⬆️
pymc/distributions/continuous.py 96.87% <0.00%> (+0.14%) ⬆️
pymc/aesaraf.py 90.10% <0.00%> (+0.39%) ⬆️
pymc/parallel_sampling.py 87.33% <0.00%> (+1.00%) ⬆️
pymc/distributions/multivariate.py 73.20% <0.00%> (+1.29%) ⬆️

@amyoshino
Copy link
Member Author

I think I was able to solve the error in pre-commit / pre-commit (pull_request) that is related with Trim Trailing Whitespace. I ran the pre-commit locally and it looks like passing now.

My sublime text wasn't automatically trimming trailing whitespace on save.

image

@@ -202,6 +213,10 @@ blah = pm.Blah.dist([0, 0], [1, 2])
blah.eval()
# array([0.62778803, 1.95165513])

# Test the get_moment
get_moment(blah).eval()
# array([0.5, 1. ])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comments & code here are correct. I think Ricardo referred to the example implementation above.

The test case further down also assumes that Blah == Normal, so maybe make this example also with a normal get_moment?

@amyoshino
Copy link
Member Author

amyoshino commented Dec 11, 2021

@michaelosthege Thank you very much for the comments and suggestions! I have accepted them all and added a commit that changes the examples we have discussed from Uniform to Normal.
I hope the parameters I chose are ok. Let me know if something else is needed.

Thank you,

@michaelosthege michaelosthege merged commit 0c90e82 into pymc-devs:main Dec 12, 2021
@amyoshino
Copy link
Member Author

Thank you @ricardoV94 @michaelosthege for your reviews and guidance. It was fun working on this PR, the first one we never forget :)

@amyoshino amyoshino deleted the document-get_moment branch December 12, 2021 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document get_moment
3 participants