-
Notifications
You must be signed in to change notification settings - Fork 7
Add LogUniform distribution #465
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| "Normal", | ||
| "Rayleigh", | ||
| "Uniform", | ||
| "LogUniform", | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -382,6 +383,10 @@ class Uniform(Distribution): | |
| If ``False``, no transformation is applied. | ||
| If a transformation is applied, the lower and upper bounds are the | ||
| lower and upper bounds of the underlying uniform distribution. | ||
| Note that this differs from the usual definition of a log-uniform | ||
| distribution, where the logarithm of the variable is uniformly | ||
| distributed between the logarithms of the bounds (see also | ||
| :class:`LogUniform`). | ||
| """ | ||
|
|
||
| def __init__( | ||
|
|
@@ -411,6 +416,45 @@ def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float: | |
| return uniform.ppf(q, loc=self._low, scale=self._high - self._low) | ||
|
|
||
|
|
||
| class LogUniform(Distribution): | ||
| """A log-uniform or reciprocal distribution. | ||
|
|
||
| A random variable is log-uniformly distributed between ``low`` and ``high`` | ||
| if its logarithm is uniformly distributed between ``log(low)`` and | ||
| ``log(high)``. | ||
|
|
||
| :param low: The lower bound of the distribution. | ||
| :param high: The upper bound of the distribution. | ||
| :param trunc: The truncation limits of the distribution. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| low: float, | ||
| high: float, | ||
| trunc: tuple[float, float] | None = None, | ||
| ): | ||
| self._logbase = np.exp(1) | ||
| self._low = self._log(low) | ||
| self._high = self._log(high) | ||
| super().__init__(log=self._logbase, trunc=trunc) | ||
|
|
||
| def __repr__(self): | ||
| return self._repr({"low": self._low, "high": self._high}) | ||
|
|
||
| def _sample(self, shape=None) -> np.ndarray | float: | ||
| return np.random.uniform(low=self._low, high=self._high, size=shape) | ||
|
|
||
| def _pdf_untransformed_untruncated(self, x) -> np.ndarray | float: | ||
| return uniform.pdf(x, loc=self._low, scale=self._high - self._low) | ||
|
|
||
| def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float: | ||
| return uniform.cdf(x, loc=self._low, scale=self._high - self._low) | ||
|
|
||
| def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float: | ||
| return uniform.ppf(q, loc=self._low, scale=self._high - self._low) | ||
|
Comment on lines
+445
to
+455
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily for this PR, but could support (optional) provision of some
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that should be added. Will do that when refactoring this whole distribution handling. |
||
|
|
||
|
|
||
| class Laplace(Distribution): | ||
| """A (log-)Laplace 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.
Not sure how to write this most clearly... 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.
Thanks, but not sure it's clearer. I'll keep the old one for now.