-
Notifications
You must be signed in to change notification settings - Fork 7
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 priors #329
Refactor priors #329
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #329 +/- ##
===========================================
+ Coverage 74.51% 74.65% +0.13%
===========================================
Files 53 54 +1
Lines 5222 5381 +159
Branches 910 923 +13
===========================================
+ Hits 3891 4017 +126
- Misses 984 1012 +28
- Partials 347 352 +5 ☔ View full report in Codecov by Sentry. |
5313345
to
b28e5c1
Compare
721ba31
to
d511639
Compare
d511639
to
9381d7c
Compare
Requires some clarification related to normalization of the prior density. Related to PEtab-dev/PEtab#402. EDIT: Needs clarification, but the current implementation matches what was done before. |
a272255
to
86643ec
Compare
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.
Looks good, thanks 👍
petab/v1/distributions.py
Outdated
def __init__( | ||
self, | ||
type_: str, | ||
transformation: str, | ||
parameters: tuple, | ||
bounds: tuple = None, | ||
parameter_scale: bool = False, | ||
): |
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.
Support supplying some rng: np.Generator = None
and otherwise set self.rng = np.random.default_rng()
in __init__
?
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.
Makes sense. I'll add that separately. For now, it's handled the same way as before, using the default numpy random state.
|
||
# Kolmogorov-Smirnov test to check if the sample is drawn from the CDF | ||
_, p = kstest(sample, cdf) | ||
assert p > 0.05, (p, distribution) |
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.
Should this be higher than 0.05
? e.g. p > 0.5
should also always work for 10000 samples, just guessing. Anyway, fine as is.
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.
With p > 0.5 this will fail frequently. Even with p > 0.05 it fails often enough so that I set a seed.
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.
That's surprising! I would have guessed that 10,000 samples from a "simple" distribution should be enough to say "these distributions look similar"... anyway fine 👍
a6429c8
to
9013648
Compare
Introduce `Distribution` classes for handling prior distributions and sampling from them. Later on this can be extended to noise models for measurements.
9013648
to
e25785f
Compare
0b704ba
to
cc6a7e7
Compare
cc6a7e7
to
047dc46
Compare
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.
Looks neat, thanks!
petab/v1/distributions.py
Outdated
def __init__(self, log: bool | float = False): | ||
if log is True: | ||
log = np.exp(1) | ||
self._log = log |
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 find this more readable in _undo_log
and __apply_log
self._log = log | |
self._log_base = log |
match type_, transformation: | ||
case (C.UNIFORM, _) | (C.PARAMETER_SCALE_UNIFORM, C.LIN): | ||
self.distribution = Uniform(*parameters) | ||
case (C.NORMAL, _) | (C.PARAMETER_SCALE_NORMAL, C.LIN): | ||
self.distribution = Normal(*parameters) | ||
case (C.LAPLACE, _) | (C.PARAMETER_SCALE_LAPLACE, C.LIN): | ||
self.distribution = Laplace(*parameters) | ||
case (C.PARAMETER_SCALE_UNIFORM, C.LOG): | ||
self.distribution = Uniform(*parameters, log=True) | ||
case (C.LOG_NORMAL, _) | (C.PARAMETER_SCALE_NORMAL, C.LOG): | ||
self.distribution = Normal(*parameters, log=True) | ||
case (C.LOG_LAPLACE, _) | (C.PARAMETER_SCALE_LAPLACE, C.LOG): | ||
self.distribution = Laplace(*parameters, log=True) | ||
case (C.PARAMETER_SCALE_UNIFORM, C.LOG10): | ||
self.distribution = Uniform(*parameters, log=10) | ||
case (C.PARAMETER_SCALE_NORMAL, C.LOG10): | ||
self.distribution = Normal(*parameters, log=10) | ||
case (C.PARAMETER_SCALE_LAPLACE, C.LOG10): | ||
self.distribution = Laplace(*parameters, log=10) | ||
case _: | ||
raise ValueError( | ||
"Unsupported distribution type / transformation: " | ||
f"{type_} / {transformation}" | ||
) |
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.
base 2? 😀
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.
Once in PEtab 🙄
tests/v1/test_distributions.py
Outdated
# pdf -> cdf | ||
def cdf(x): | ||
return cumulative_trapezoid(distribution.pdf(x), x) |
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.
Makes sense to ensure that the samples match the PDF within PEtab. For the LIN
transform
cases, could you additionally compare to scipy.stats.(log)(uniform/normal/laplace)
? I guess the LOG
and LOG10
transform
s will be dropped in v2 so less important to test I guess
Introduces
Prior
andDistribution
classes for handling PEtab-specific prior distributions, and (PEtab-version-invariant) univariate probability distributions. Supports sampling from them, and evaluating negative log-priors (#312). Later on, this can be extended to noise models for measurements and computing loglikelihoods.This also adds a notebook demonstrating the various prior options which are a common source confusion.
Closes #311.
👀 notebook: https://petab--329.org.readthedocs.build/projects/libpetab-python/en/329/example/distributions.html