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

refactor kumaraswamy #4706

Merged
merged 6 commits into from
May 17, 2021
Merged

Conversation

farhanreynaldo
Copy link
Contributor

This PR closes a subset of #4686, which is Kumaraswamy distribution. But, I am not quite sure why the check_pymc_draws_match_reference test failed.

@ricardoV94 ricardoV94 mentioned this pull request May 17, 2021
26 tasks
@ricardoV94
Copy link
Member

Completely optional for this PR but would you be interested in implementing the logcdf method for this distribution?

Gauging from wikipedia it has a very simple form: https://en.m.wikipedia.org/wiki/Kumaraswamy_distribution

@ricardoV94
Copy link
Member

Failing test is unrelated to this PR. Documented in #4661

@farhanreynaldo
Copy link
Contributor Author

Completely optional for this PR but would you be interested in implementing the logcdf method for this distribution?

Gauging from wikipedia it has a very simple form: https://en.m.wikipedia.org/wiki/Kumaraswamy_distribution

My math is a little bit rusty, but does the following logcdf correct?

-b * at.log(1 - value ** a)

Because when I check against the following code, it doesn't match.

at.log(1 - (1 - value ** a) ** b)

@ricardoV94
Copy link
Member

ricardoV94 commented May 17, 2021

You are missing the first 1 - .... If you want to remove the exponent you would need log(1 - exp(b * log( 1 - value ** a)) but perhaps the outer log will suffice numerically. We can replace log(1 - x) by log1p(-x) which is a bit more stable.

That is log1p(- (1 - value ** a) ** b)

@farhanreynaldo
Copy link
Contributor Author

farhanreynaldo commented May 17, 2021

You are missing the first 1 - .... If you want to remove the exponent you would need log(1 - exp(b * log( 1 - value ** a)) but perhaps the outer log will suffice numerically. We can replace log(1 - x) by log1p(-x) which is a bit more stable.

That is log1p(- (1 - value ** a) ** b)

I totally missed that! 😩 Thank you for clarifying the equation.

I should also add the following test to test_kumaraswamy, right?

def test_kumaraswamy(self):
        ...
        def scipy_log_cdf(value, a, b):
            return np.log1p(-(1 - value ** a) ** b)

        self.check_logp(Kumaraswamy, Unit, {"a": Rplus, "b": Rplus}, scipy_log_cdf)

@ricardoV94
Copy link
Member

It should be check_logcdf, but other than that yes :)

-------
TensorVariable
"""
return bound(at.log1p(-((1 - value ** a) ** b)), value >= 0, value <= 1)
Copy link
Member

@ricardoV94 ricardoV94 May 17, 2021

Choose a reason for hiding this comment

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

Value above one should give a logcdf of 0, so we need a switch: at.switch(value < 1, normal expression, 0), and to remove value <= 1 as a bound condition. We should still keep the a > 0 and b > 0 conditions as in the logp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does tt equal to at? Because I assume tt is Theano tensor and it is not available.

Copy link
Member

@ricardoV94 ricardoV94 May 17, 2021

Choose a reason for hiding this comment

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

Yes of course. It was a mistake, I am still used to writing with theano's abbreviation :p

Fixed the comment

@ricardoV94
Copy link
Member

ricardoV94 commented May 17, 2021

Looks to be on the right track. I left you some comments. In the meantime you can add a release note mentioning the logcdf method was added to this distribution in the "New features": https://github.com/pymc-devs/pymc3/blob/v4/RELEASE-NOTES.md

@ricardoV94
Copy link
Member

The failing test seems to be a precision issue. This means we may want to break the exponent as in your first suggestion. I'll push some changes in a second.

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.

I think it's ready! Thanks for the PR

@ricardoV94 ricardoV94 merged commit d848b9c into pymc-devs:v4 May 17, 2021
@farhanreynaldo
Copy link
Contributor Author

I think it's ready! Thanks for the PR

Thank you so much!

twiecki pushed a commit that referenced this pull request Jun 5, 2021
* refactor kumaraswamy
* add logcdf method

Co-authored-by: Farhan Reynaldo <farhanreynaldo@gmail.com>
Co-authored-by: Ricardo <ricardo.vieira1994@gmail.com>
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.

None yet

2 participants