-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Restrict domain on alpha in the CAR distribution #6801
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
Conversation
pymc/distributions/multivariate.py
Outdated
alpha = pt.as_tensor_variable(floatX(alpha)) | ||
msg = "the domain of alpha is: -1 < alpha < 1." | ||
alpha = Assert(msg)(alpha, pt.lt(alpha, 1) and pt.gt(alpha, -1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't put this assert here. Instead raise an error in the rng_fn
if the alpha is actually problematic for random draws.
Otherwise Asserts will complicate the graph needlessly.
Anyway the reason you don't see the exception is probably that you need to use pt.and_
instead of python and
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup the pt.and_
was what I was missing. So instead of using assert in the rng function, I just raise a ValueError? Is that right?
if alpha >= 1 or alpha <= -1:
raise ValueError("the domain of alpha is: -1 < alpha < 1")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but alpha could be a vector so you need a np.all(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha - okay sent in a new commit with these changes.
What do you mean? |
Here the alpha is fine:
but it returns |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6801 +/- ##
==========================================
- Coverage 91.92% 85.59% -6.33%
==========================================
Files 95 95
Lines 16197 16263 +66
==========================================
- Hits 14889 13921 -968
- Misses 1308 2342 +1034
|
During compilation the check will be removed if it's not needed (i.e. alpha is constant and in the required range). |
Oh right of course! yeah the logp is behaving properly too. |
I'm happy with the PR at this point - just having trouble with codecov. It looks like coverage increases on the file I changed but I'm getting a lot of indirect coverage issues that are causing it to fail. No clue what I need to fix. Does anyone know why these might be coming up? |
pymc/distributions/multivariate.py
Outdated
@@ -2077,6 +2078,9 @@ def rng_fn(cls, rng: np.random.RandomState, mu, W, alpha, tau, size): | |||
Journal of the Royal Statistical Society Series B, Royal Statistical Society, | |||
vol. 63(2), pages 325-338. DOI: 10.1111/1467-9868.00288 | |||
""" | |||
if np.all(alpha >= 1) or np.all(alpha <= -1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you mean this?
if np.all(alpha >= 1) or np.all(alpha <= -1): | |
if np.any(alpha >= 1) or np.any(alpha <= -1): |
car_dist = pm.CAR.dist(W=W, alpha=alpha, mu=mu, tau=tau) | ||
|
||
with pytest.raises(ValueError, match="the domain of alpha is: -1 < alpha < 1"): | ||
pm.draw(car_dist) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth to test the logp as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think so too. Is it okay if I write it within the same test or do we need two separate ones? I went with putting them in the same test with the latest commit but happy to reorganize it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either is OK, it's clear what you're testing either way
The codecov here on GitHub is a bit flaky, don't worry about it |
@@ -835,6 +835,23 @@ def test_car_matrix_check(sparse): | |||
car_dist = pm.CAR.dist(mu, W, alpha, tau) | |||
|
|||
|
|||
@pytest.mark.parametrize("alpha", [1, -1]) | |||
def test_car_alpha(alpha): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_car_alpha(alpha): | |
def test_car_alpha_bounds(alpha): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! It looks like codecov is comparing your branch to 7b08fc1, which was from a couple weeks ago, not current main, which is causing those indirect coverage changes. Am I correct in guessing that this is why it's a non-issue then for merging @ricardoV94?
I never investigated why codecov bot is so flaky so it's nice to have an hypothesis. Many times I had to ignore because it was clearly wrong (as is here) |
#6792
Hi, I think I need some help writing the checks correctly. The intended change is supposed to be fairly trivial. the alpha parameter should be restricted to be (-1,1). I tried making the changes that I thought would enforce them but they are not working. The
make_node()
check is not triggering when I create the random variable with.dist
or when I usepm.draw()
. Thelopg()
check is triggering all the time, even when alpha is in the acceptable range.Maybe something we can discuss @fonnesbeck @bwengals today?
📚 Documentation preview 📚: https://pymc--6801.org.readthedocs.build/en/6801/