Skip to content

Commit 76533e6

Browse files
FreezyLemonYeungOnion
authored andcommitted
refactor!: decouple distribution traits from rand
This changes the behavior of Cauchy::mean() and Cauchy::variance() from the old default implementation (based on RNG sampling) to simply returning `None`. Both mean and variance are undefined for the cauchy distribution. This also changes the trait signature of both Distribution<T> and DiscreteDistribution<T> to not be subtraits of rand::distribution::Distribution<T>. Our distributions do still implement this trait iff the `rand` feature is enabled.
1 parent cfd95a9 commit 76533e6

File tree

1 file changed

+4
-38
lines changed

1 file changed

+4
-38
lines changed

src/statistics/traits.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use ::num_traits::float::Float;
22

3-
const STEPS: usize = 1_000;
4-
53
/// The `Min` trait specifies than an object has a minimum value
64
pub trait Min<T> {
75
/// Returns the minimum value in the domain of a given distribution
@@ -35,7 +33,7 @@ pub trait Max<T> {
3533
/// ```
3634
fn max(&self) -> T;
3735
}
38-
pub trait DiscreteDistribution<T: Float>: ::rand::distributions::Distribution<u64> {
36+
pub trait DiscreteDistribution<T: Float> {
3937
/// Returns the mean, if it exists.
4038
fn mean(&self) -> Option<T> {
4139
None
@@ -58,14 +56,8 @@ pub trait DiscreteDistribution<T: Float>: ::rand::distributions::Distribution<u6
5856
}
5957
}
6058

61-
pub trait Distribution<T: Float>: ::rand::distributions::Distribution<T> {
59+
pub trait Distribution<T: Float> {
6260
/// Returns the mean, if it exists.
63-
/// The default implementation returns an estimation
64-
/// based on random samples. This is a crude estimate
65-
/// for when no further information is known about the
66-
/// distribution. More accurate statements about the
67-
/// mean can and should be given by overriding the
68-
/// default implementation.
6961
///
7062
/// # Examples
7163
///
@@ -77,23 +69,9 @@ pub trait Distribution<T: Float>: ::rand::distributions::Distribution<T> {
7769
/// assert_eq!(0.5, n.mean().unwrap());
7870
/// ```
7971
fn mean(&self) -> Option<T> {
80-
// TODO: Does not need cryptographic rng
81-
let mut rng = ::rand::rngs::OsRng;
82-
let mut mean = T::zero();
83-
let mut steps = T::zero();
84-
for _ in 0..STEPS {
85-
steps = steps + T::one();
86-
mean = mean + Self::sample(self, &mut rng);
87-
}
88-
Some(mean / steps)
72+
None
8973
}
9074
/// Returns the variance, if it exists.
91-
/// The default implementation returns an estimation
92-
/// based on random samples. This is a crude estimate
93-
/// for when no further information is known about the
94-
/// distribution. More accurate statements about the
95-
/// variance can and should be given by overriding the
96-
/// default implementation.
9775
///
9876
/// # Examples
9977
///
@@ -105,19 +83,7 @@ pub trait Distribution<T: Float>: ::rand::distributions::Distribution<T> {
10583
/// assert_eq!(1.0 / 12.0, n.variance().unwrap());
10684
/// ```
10785
fn variance(&self) -> Option<T> {
108-
// TODO: Does not need cryptographic rng
109-
let mut rng = ::rand::rngs::OsRng;
110-
let mut mean = T::zero();
111-
let mut variance = T::zero();
112-
let mut steps = T::zero();
113-
for _ in 0..STEPS {
114-
steps = steps + T::one();
115-
let sample = Self::sample(self, &mut rng);
116-
variance = variance + (steps - T::one()) * (sample - mean) * (sample - mean) / steps;
117-
mean = mean + (sample - mean) / steps;
118-
}
119-
steps = steps - T::one();
120-
Some(variance / steps)
86+
None
12187
}
12288
/// Returns the standard deviation, if it exists.
12389
///

0 commit comments

Comments
 (0)