-
Notifications
You must be signed in to change notification settings - Fork 3
Added logitnormal distribution #23
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import pytensor.tensor as pt | ||
|
|
||
| from distributions.helper import ( | ||
| cdf_bounds, | ||
| continuous_entropy, | ||
| continuous_kurtosis, | ||
| continuous_mean, | ||
| continuous_skewness, | ||
| continuous_variance, | ||
| ppf_bounds_cont, | ||
| ) | ||
| from distributions.normal import ppf as normal_ppf | ||
|
|
||
| # Support bounds for logitnormal (open interval (0, 1)) | ||
| _LOWER = 0.001 | ||
| _UPPER = 0.999 | ||
|
|
||
|
|
||
| def _logit(x): | ||
| return pt.log(x) - pt.log1p(-x) | ||
|
|
||
|
|
||
| def _expit(y): | ||
| return pt.sigmoid(y) | ||
|
|
||
|
|
||
| def mean(mu, sigma): | ||
| return continuous_mean(_LOWER, _UPPER, logpdf, mu, sigma) | ||
|
|
||
|
|
||
| def mode(mu, sigma): | ||
| return _expit(mu) | ||
|
|
||
|
|
||
| def median(mu, sigma): | ||
| shape = pt.broadcast_arrays(mu, sigma)[0] | ||
| return pt.full_like(shape, _expit(mu)) | ||
|
|
||
|
|
||
| def var(mu, sigma): | ||
| return continuous_variance(_LOWER, _UPPER, logpdf, mu, sigma) | ||
|
|
||
|
|
||
| def std(mu, sigma): | ||
| return pt.sqrt(var(mu, sigma)) | ||
|
|
||
|
|
||
| def skewness(mu, sigma): | ||
| return continuous_skewness(_LOWER, _UPPER, logpdf, mu, sigma) | ||
|
|
||
|
|
||
| def kurtosis(mu, sigma): | ||
| return continuous_kurtosis(_LOWER, _UPPER, logpdf, mu, sigma) | ||
|
|
||
|
|
||
| def entropy(mu, sigma): | ||
| return continuous_entropy(_LOWER, _UPPER, logpdf, mu, sigma) | ||
|
|
||
|
|
||
| def pdf(x, mu, sigma): | ||
| return pt.exp(logpdf(x, mu, sigma)) | ||
|
|
||
|
|
||
| def logpdf(x, mu, sigma): | ||
| logit_x = _logit(x) | ||
| return pt.switch( | ||
| pt.or_(pt.le(x, 0), pt.ge(x, 1)), | ||
| -pt.inf, | ||
| -0.5 * ((logit_x - mu) / sigma) ** 2 | ||
| - pt.log(sigma) | ||
| - 0.5 * pt.log(2 * pt.pi) | ||
| - pt.log(x) | ||
| - pt.log1p(-x), | ||
| ) | ||
|
|
||
|
|
||
| def cdf(x, mu, sigma): | ||
| logit_x = _logit(x) | ||
| prob = 0.5 * (1 + pt.erf((logit_x - mu) / (sigma * pt.sqrt(2)))) | ||
| return cdf_bounds(prob, x, 0, 1) | ||
|
|
||
|
|
||
| def logcdf(x, mu, sigma): | ||
| logit_x = _logit(x) | ||
| z = (logit_x - mu) / sigma | ||
| return pt.switch( | ||
| pt.le(x, 0), | ||
| -pt.inf, | ||
| pt.switch( | ||
| pt.ge(x, 1), | ||
| 0.0, | ||
| pt.switch( | ||
| pt.lt(z, -1.0), | ||
| pt.log(pt.erfcx(-z / pt.sqrt(2.0)) / 2.0) - pt.sqr(z) / 2.0, | ||
| pt.log1p(-pt.erfc(z / pt.sqrt(2.0)) / 2.0), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def sf(x, mu, sigma): | ||
| return pt.exp(logsf(x, mu, sigma)) | ||
|
|
||
|
|
||
| def logsf(x, mu, sigma): | ||
| logit_x = _logit(x) | ||
| z = (logit_x - mu) / sigma | ||
| return pt.switch( | ||
| pt.le(x, 0), | ||
| 0.0, | ||
| pt.switch( | ||
| pt.ge(x, 1), | ||
| -pt.inf, | ||
| pt.switch( | ||
| pt.gt(z, 1.0), | ||
| pt.log(pt.erfcx(z / pt.sqrt(2.0)) / 2.0) - pt.sqr(z) / 2.0, | ||
| pt.log1p(-0.5 * (1 + pt.erf(z / pt.sqrt(2.0)))), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def ppf(q, mu, sigma): | ||
| return ppf_bounds_cont(_expit(normal_ppf(q, mu, sigma)), q, 0, 1) | ||
|
|
||
|
|
||
| def isf(q, mu, sigma): | ||
| return ppf(1 - q, mu, sigma) | ||
|
|
||
|
|
||
| def rvs(mu, sigma, size=None, random_state=None): | ||
| return _expit(pt.random.normal(mu, sigma, rng=random_state, size=size)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """Test Logit-Normal distribution against empirical samples.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from distributions import logitnormal as LogitNormal | ||
| from tests.helper_empirical import run_empirical_tests | ||
| from tests.helper_scipy import make_params | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "params", | ||
| [ | ||
| [0.0, 1.0], # Standard logit-normal (centered) | ||
| [0.0, 0.001], # Narrower distribution | ||
| [1.0, 1.0], # Shifted right (mode > 0.5) | ||
| [-1.0, 1.0], # Shifted left (mode < 0.5) | ||
| [0.0, 2.0], # Wider distribution (approaches U-shape) | ||
| [2.0, 0.5], # Strongly shifted right | ||
| ], | ||
| ) | ||
| def test_logitnormal_vs_random(params): | ||
| """Test Logit-Normal distribution against random samples.""" | ||
| p_params = make_params(*params, dtype="float64") | ||
| support = (0, 1) | ||
|
|
||
| run_empirical_tests( | ||
| p_dist=LogitNormal, | ||
| p_params=p_params, | ||
| support=support, | ||
| name="logitnormal", | ||
| sample_size=500_000, | ||
| mean_rtol=1e-2, | ||
| var_rtol=1e-2, | ||
| std_rtol=1e-2, | ||
| skewness_rtol=2e-1, | ||
| kurtosis_rtol=2e-1, | ||
| quantiles_rtol=3e-2, | ||
| cdf_rtol=5e-2, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In
helper.py, we have functions to numerically compute moments for discrete distributions. We should move this and other methods there, so we can reuse them for other distributions. In preliz, distributions has axvalsmethod https://github.com/arviz-devs/preliz/blob/28bbd018963cbc010d3f13e62124eb4653ec1459/preliz/distributions/distributions.py#L507 that we used for plotting or, in this cas,e to get a reasonable range of values to evaluate some functions. We could have something similar here.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 ported a few things over to helper.py. Let me know what you think.