statrs/statistics/traits.rs
1use ::num_traits::float::Float;
2
3/// The `Min` trait specifies than an object has a minimum value
4pub trait Min<T> {
5 /// Returns the minimum value in the domain of a given distribution
6 /// if it exists, otherwise `None`.
7 ///
8 /// # Examples
9 ///
10 /// ```
11 /// use statrs::statistics::Min;
12 /// use statrs::distribution::Uniform;
13 ///
14 /// let n = Uniform::new(0.0, 1.0).unwrap();
15 /// assert_eq!(0.0, n.min());
16 /// ```
17 fn min(&self) -> T;
18}
19
20/// The `Max` trait specifies that an object has a maximum value
21pub trait Max<T> {
22 /// Returns the maximum value in the domain of a given distribution
23 /// if it exists, otherwise `None`.
24 ///
25 /// # Examples
26 ///
27 /// ```
28 /// use statrs::statistics::Max;
29 /// use statrs::distribution::Uniform;
30 ///
31 /// let n = Uniform::new(0.0, 1.0).unwrap();
32 /// assert_eq!(1.0, n.max());
33 /// ```
34 fn max(&self) -> T;
35}
36pub trait DiscreteDistribution<T: Float> {
37 /// Returns the mean, if it exists.
38 fn mean(&self) -> Option<T> {
39 None
40 }
41 /// Returns the variance, if it exists.
42 fn variance(&self) -> Option<T> {
43 None
44 }
45 /// Returns the standard deviation, if it exists.
46 fn std_dev(&self) -> Option<T> {
47 self.variance().map(|var| var.sqrt())
48 }
49 /// Returns the entropy, if it exists.
50 fn entropy(&self) -> Option<T> {
51 None
52 }
53 /// Returns the skewness, if it exists.
54 fn skewness(&self) -> Option<T> {
55 None
56 }
57}
58
59pub trait Distribution<T: Float> {
60 /// Returns the mean, if it exists.
61 ///
62 /// # Examples
63 ///
64 /// ```
65 /// use statrs::statistics::Distribution;
66 /// use statrs::distribution::Uniform;
67 ///
68 /// let n = Uniform::new(0.0, 1.0).unwrap();
69 /// assert_eq!(0.5, n.mean().unwrap());
70 /// ```
71 fn mean(&self) -> Option<T> {
72 None
73 }
74 /// Returns the variance, if it exists.
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// use statrs::statistics::Distribution;
80 /// use statrs::distribution::Uniform;
81 ///
82 /// let n = Uniform::new(0.0, 1.0).unwrap();
83 /// assert_eq!(1.0 / 12.0, n.variance().unwrap());
84 /// ```
85 fn variance(&self) -> Option<T> {
86 None
87 }
88 /// Returns the standard deviation, if it exists.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use statrs::statistics::Distribution;
94 /// use statrs::distribution::Uniform;
95 ///
96 /// let n = Uniform::new(0.0, 1.0).unwrap();
97 /// assert_eq!((1f64 / 12f64).sqrt(), n.std_dev().unwrap());
98 /// ```
99 fn std_dev(&self) -> Option<T> {
100 self.variance().map(|var| var.sqrt())
101 }
102 /// Returns the entropy, if it exists.
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use statrs::statistics::Distribution;
108 /// use statrs::distribution::Uniform;
109 ///
110 /// let n = Uniform::new(0.0, 1.0).unwrap();
111 /// assert_eq!(0.0, n.entropy().unwrap());
112 /// ```
113 fn entropy(&self) -> Option<T> {
114 None
115 }
116 /// Returns the skewness, if it exists.
117 ///
118 /// # Examples
119 ///
120 /// ```
121 /// use statrs::statistics::Distribution;
122 /// use statrs::distribution::Uniform;
123 ///
124 /// let n = Uniform::new(0.0, 1.0).unwrap();
125 /// assert_eq!(0.0, n.skewness().unwrap());
126 /// ```
127 fn skewness(&self) -> Option<T> {
128 None
129 }
130}
131
132/// The `Mean` trait implements the calculation of a mean.
133// TODO: Clarify the traits of multidimensional distributions
134pub trait MeanN<T> {
135 fn mean(&self) -> Option<T>;
136}
137
138// TODO: Clarify the traits of multidimensional distributions
139pub trait VarianceN<T> {
140 fn variance(&self) -> Option<T>;
141}
142
143/// The `Median` trait returns the median of the distribution.
144pub trait Median<T> {
145 /// Returns the median.
146 ///
147 /// # Examples
148 ///
149 /// ```
150 /// use statrs::statistics::Median;
151 /// use statrs::distribution::Uniform;
152 ///
153 /// let n = Uniform::new(0.0, 1.0).unwrap();
154 /// assert_eq!(0.5, n.median());
155 /// ```
156 fn median(&self) -> T;
157}
158
159/// The `Mode` trait specifies that an object has a closed form solution
160/// for its mode(s)
161pub trait Mode<T> {
162 /// Returns the mode, if one exists.
163 ///
164 /// # Examples
165 ///
166 /// ```
167 /// use statrs::statistics::Mode;
168 /// use statrs::distribution::Uniform;
169 ///
170 /// let n = Uniform::new(0.0, 1.0).unwrap();
171 /// assert_eq!(Some(0.5), n.mode());
172 /// ```
173 fn mode(&self) -> T;
174}