1use crate::{Distribution, OpenClosed01};
12use core::fmt;
13use num_traits::Float;
14use rand::Rng;
15
16#[derive(Clone, Copy, Debug, PartialEq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub struct Frechet<F>
49where
50 F: Float,
51 OpenClosed01: Distribution<F>,
52{
53 location: F,
54 scale: F,
55 shape: F,
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum Error {
61 LocationNotFinite,
63 ScaleNotPositive,
65 ShapeNotPositive,
67}
68
69impl fmt::Display for Error {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.write_str(match self {
72 Error::LocationNotFinite => "location is not finite in Frechet distribution",
73 Error::ScaleNotPositive => "scale is not positive and finite in Frechet distribution",
74 Error::ShapeNotPositive => "shape is not positive and finite in Frechet distribution",
75 })
76 }
77}
78
79#[cfg(feature = "std")]
80impl std::error::Error for Error {}
81
82impl<F> Frechet<F>
83where
84 F: Float,
85 OpenClosed01: Distribution<F>,
86{
87 pub fn new(location: F, scale: F, shape: F) -> Result<Frechet<F>, Error> {
89 if scale <= F::zero() || scale.is_infinite() || scale.is_nan() {
90 return Err(Error::ScaleNotPositive);
91 }
92 if shape <= F::zero() || shape.is_infinite() || shape.is_nan() {
93 return Err(Error::ShapeNotPositive);
94 }
95 if location.is_infinite() || location.is_nan() {
96 return Err(Error::LocationNotFinite);
97 }
98 Ok(Frechet {
99 location,
100 scale,
101 shape,
102 })
103 }
104}
105
106impl<F> Distribution<F> for Frechet<F>
107where
108 F: Float,
109 OpenClosed01: Distribution<F>,
110{
111 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> F {
112 let x: F = rng.sample(OpenClosed01);
113 self.location + self.scale * (-x.ln()).powf(-self.shape.recip())
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 #[should_panic]
123 fn test_zero_scale() {
124 Frechet::new(0.0, 0.0, 1.0).unwrap();
125 }
126
127 #[test]
128 #[should_panic]
129 fn test_infinite_scale() {
130 Frechet::new(0.0, f64::INFINITY, 1.0).unwrap();
131 }
132
133 #[test]
134 #[should_panic]
135 fn test_nan_scale() {
136 Frechet::new(0.0, f64::NAN, 1.0).unwrap();
137 }
138
139 #[test]
140 #[should_panic]
141 fn test_zero_shape() {
142 Frechet::new(0.0, 1.0, 0.0).unwrap();
143 }
144
145 #[test]
146 #[should_panic]
147 fn test_infinite_shape() {
148 Frechet::new(0.0, 1.0, f64::INFINITY).unwrap();
149 }
150
151 #[test]
152 #[should_panic]
153 fn test_nan_shape() {
154 Frechet::new(0.0, 1.0, f64::NAN).unwrap();
155 }
156
157 #[test]
158 #[should_panic]
159 fn test_infinite_location() {
160 Frechet::new(f64::INFINITY, 1.0, 1.0).unwrap();
161 }
162
163 #[test]
164 #[should_panic]
165 fn test_nan_location() {
166 Frechet::new(f64::NAN, 1.0, 1.0).unwrap();
167 }
168
169 #[test]
170 fn test_sample_against_cdf() {
171 fn quantile_function(x: f64) -> f64 {
172 (-x.ln()).recip()
173 }
174 let location = 0.0;
175 let scale = 1.0;
176 let shape = 1.0;
177 let iterations = 100_000;
178 let increment = 1.0 / iterations as f64;
179 let probabilities = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9];
180 let mut quantiles = [0.0; 9];
181 for (i, p) in probabilities.iter().enumerate() {
182 quantiles[i] = quantile_function(*p);
183 }
184 let mut proportions = [0.0; 9];
185 let d = Frechet::new(location, scale, shape).unwrap();
186 let mut rng = crate::test::rng(1);
187 for _ in 0..iterations {
188 let replicate = d.sample(&mut rng);
189 for (i, q) in quantiles.iter().enumerate() {
190 if replicate < *q {
191 proportions[i] += increment;
192 }
193 }
194 }
195 assert!(proportions
196 .iter()
197 .zip(&probabilities)
198 .all(|(p_hat, p)| (p_hat - p).abs() < 0.003))
199 }
200
201 #[test]
202 fn frechet_distributions_can_be_compared() {
203 assert_eq!(Frechet::new(1.0, 2.0, 3.0), Frechet::new(1.0, 2.0, 3.0));
204 }
205}