-
-
Notifications
You must be signed in to change notification settings - Fork 464
Add getters for distribution parameters, closes #1233 #1234
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
8eb388e
1dd0516
3e77c27
679a36b
681f4d8
34c735f
b5960a3
2f8cd34
639bde4
00cb3b3
21eb5cc
389fd1d
ae7fcda
1d5a677
9f2c2b0
895ea33
04a34eb
361889c
e004eb4
9ac5d95
a31471e
e61d57b
38dc461
b623d3a
26e6887
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 |
---|---|---|
|
@@ -140,6 +140,11 @@ where F: Float, Exp1: Distribution<F> | |
lambda_inverse: F::one() / lambda, | ||
}) | ||
} | ||
|
||
/// Returns the (inverse of the) shape parameter (`lambda`) of the distribution. | ||
pub fn lambda_inverse(&self) -> F { | ||
self.lambda_inverse | ||
} | ||
Comment on lines
+144
to
+147
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. Storing 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. Further to my comments below: we may want to keep this method (but maybe also add |
||
} | ||
|
||
impl<F> Distribution<F> for Exp<F> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,30 @@ where | |
}; | ||
Ok(Gamma { repr }) | ||
} | ||
|
||
/// Returns the shape parameter (`shape`) of the distribution. | ||
pub fn shape(&self) -> F { | ||
match self.repr { | ||
// By definition of `one`. | ||
One(_) => F::one(), | ||
// By definition of `small shape`. | ||
Small(gamma) => gamma.inv_shape.recip(), | ||
// By definition of `large shape`. | ||
Large(gamma) => gamma.d + F::from(1. / 3.).unwrap(), | ||
} | ||
} | ||
|
||
/// Returns the scale parameter (`scale`) of the distribution. | ||
pub fn scale(&self) -> F { | ||
match self.repr { | ||
// By definition of `one`. | ||
One(exp) => exp.lambda_inverse().recip(), | ||
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. This is actually wrong: It seems like it would be worth adding |
||
// By definition of `small shape`. | ||
Small(gamma) => gamma.large_shape.scale, | ||
// By definition of `large shape`. | ||
Large(gamma) => gamma.scale, | ||
} | ||
} | ||
} | ||
|
||
impl<F> GammaSmallShape<F> | ||
|
@@ -350,6 +374,16 @@ where | |
}; | ||
Ok(ChiSquared { repr }) | ||
} | ||
|
||
/// Returns the degrees-of-freedom (`k`) of the distribution. | ||
pub fn k(&self) -> F { | ||
match self.repr { | ||
DoFExactlyOne => F::one(), | ||
// Since `DoFAnythingElse` is computed using Gamma(shape = 0.5 * k, ...), | ||
// we revert the operation k = (1. / 0.5) * shape = 2. * shape. | ||
Comment on lines
+382
to
+383
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. Comments like these are probably not useful.. reviewers can't rely on them and probably no one else has a use for them. So probably they should be removed? |
||
DoFAnythingElse(gamma) => F::from(2.).unwrap() * gamma.shape(), | ||
} | ||
} | ||
} | ||
impl<F> Distribution<F> for ChiSquared<F> | ||
where | ||
|
@@ -447,6 +481,16 @@ where | |
dof_ratio: n / m, | ||
}) | ||
} | ||
|
||
/// Returns the m parameter (`m`) of the distribution. | ||
pub fn m(&self) -> F { | ||
self.numer.k() | ||
} | ||
|
||
/// Returns the n parameter (`n`) of the distribution. | ||
pub fn n(&self) -> F { | ||
self.denom.k() | ||
} | ||
} | ||
impl<F> Distribution<F> for FisherF<F> | ||
where | ||
|
@@ -500,6 +544,11 @@ where | |
dof: n, | ||
}) | ||
} | ||
|
||
/// Return the degrees-of-freedom (`n`) of the distribution. | ||
pub fn n(&self) -> F { | ||
self.chi.k() | ||
} | ||
} | ||
impl<F> Distribution<F> for StudentT<F> | ||
where | ||
|
@@ -650,6 +699,24 @@ where | |
}) | ||
} | ||
} | ||
|
||
/// Returns the alpha shape (`alpha`) of the distribution. | ||
pub fn alpha(&self) -> F { | ||
if self.switched_params { | ||
self.b | ||
} else { | ||
self.a | ||
} | ||
} | ||
|
||
/// Returns the beta shape (`beta`) of the distribution. | ||
pub fn beta(&self) -> F { | ||
if self.switched_params { | ||
self.a | ||
} else { | ||
self.b | ||
} | ||
} | ||
} | ||
|
||
impl<F> Distribution<F> for Beta<F> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,6 +304,16 @@ where F: Float, StandardNormal: Distribution<F> | |
pub fn from_zscore(&self, zscore: F) -> F { | ||
self.norm.from_zscore(zscore).exp() | ||
} | ||
|
||
/// Returns the mean (`μ`) of the distribution. | ||
pub fn mean(&self) -> F { | ||
self.norm.mean() | ||
} | ||
|
||
/// Returns the standard deviation (`σ`) of the distribution. | ||
pub fn std_dev(&self) -> F { | ||
self.norm.std_dev() | ||
} | ||
Comment on lines
+308
to
+316
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. The method names and descriptions are incorrect: these are the log-space mean and standard deviation. I suggest calling the methods The actual mean of a log-normal is |
||
} | ||
|
||
impl<F> Distribution<F> for LogNormal<F> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,25 @@ where | |
inverse_gaussian, | ||
}) | ||
} | ||
|
||
/// Returns the alpha parameter (`alpha`) of the distribution. | ||
pub fn alpha(&self) -> F { | ||
// By definition: | ||
// gamma = sqrt(alpha^2 - beta^2) with alpha > 0 | ||
// mu = 1 / gamma | ||
// Therefore: | ||
// mu = 1 / sqrt(alpha^2 - beta^2) | ||
// mu^2 = 1 / (alpha^2 - beta^2) | ||
// 1 / mu^2 = alpha^2 - beta^2 | ||
// 1 / mu^2 + beta^2 = alpha^2 | ||
// sqrt(1 / mu^2 + beta^2) = alpha with alpha > 0 | ||
F::sqrt(self.inverse_gaussian.mean().powi(2).recip() + self.beta.powi(2)) | ||
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. I think this computation may produce slightly different 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. Indeed, I've commented in #1233 about the issue, I've proposed a set of alternatives, that's why this PR is just a draft. |
||
} | ||
|
||
/// Returns the beta parameter (`beta`) of the distribution. | ||
pub fn beta(&self) -> F { | ||
self.beta | ||
} | ||
} | ||
|
||
impl<F> Distribution<F> for NormalInverseGaussian<F> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,11 @@ where F: Float, Standard: Distribution<F>, OpenClosed01: Distribution<F> | |
b: two.powf(a_minus_1), | ||
}) | ||
} | ||
|
||
/// Returns the a parameter (`a`) of the distribution. | ||
pub fn a(&self) -> F { | ||
self.a_minus_1 + F::one() | ||
} | ||
} | ||
|
||
impl<F> Distribution<F> for Zeta<F> | ||
|
@@ -205,6 +210,35 @@ where F: Float, Standard: Distribution<F> { | |
}) | ||
} | ||
|
||
/// Returns the cardinality of the set of elements (`n`) of the distribution. | ||
pub fn n(&self) -> F { | ||
match self.s != F::one() { | ||
true => { | ||
// By definition: | ||
// t = (n^(1 - s) - s) * q | ||
// Therefore: | ||
// t / q = n^(1 - s) - s | ||
// t / q + s = n^(1 - s) | ||
// (t / q + s)^(1/(1 - s)) = n | ||
F::powf(self.t / self.q + self.s, F::recip(F::one() - self.s)) | ||
}, | ||
false => { | ||
// By definition: | ||
// t = 1 - ln(n) | ||
// Therefore: | ||
// t - 1 = - ln(n) | ||
// 1 - t = ln(n) | ||
// e^(1 - t) = n | ||
F::exp(F::one() - self.t) | ||
}, | ||
} | ||
} | ||
Comment on lines
+214
to
+235
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. Another complex case to check (or exclude) |
||
|
||
/// Returns the frequency rank exponent (`s`) of the distribution. | ||
pub fn s(&self) -> F { | ||
self.s | ||
} | ||
|
||
/// Inverse cumulative density function | ||
#[inline] | ||
fn inv_cdf(&self, p: F) -> F { | ||
|
Uh oh!
There was an error while loading. Please reload this page.