1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use crate::{ consts::{ONE_OVER_PI, PI_OVER_4, THREE_HALVES, TWO_OVER_PI}, prelude::*, }; use rand::Rng; use spaces::real::Interval; use std::fmt; locscale_params! { Params { a<f64>, w<f64> } } impl Params { #[inline(always)] pub fn b(&self) -> crate::params::Loc<f64> { crate::params::Loc(self.a.0 + self.w.0) } } new_dist!(Arcsine<Params>); macro_rules! get_params { ($self:ident) => { ($self.0.a.0, $self.0.b().0) } } impl Arcsine { pub fn new(a: f64, w: f64) -> Result<Arcsine, failure::Error> { Params::new(a, w).map(|p| Arcsine(p)) } pub fn new_unchecked(a: f64, w: f64) -> Arcsine { Arcsine(Params::new_unchecked(a, w)) } } impl Default for Arcsine { fn default() -> Arcsine { Arcsine(Params::new_unchecked(0.0, 1.0)) } } impl Distribution for Arcsine { type Support = Interval; type Params = Params; fn support(&self) -> Interval { let (a, b) = get_params!(self); Interval::bounded(a, b) } fn params(&self) -> Params { self.0 } fn cdf(&self, x: &f64) -> Probability { let a = self.0.a.0; let w = self.0.w.0; let xab = (x - a) / w; Probability::new_unchecked(TWO_OVER_PI * xab.sqrt().asin()) } fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> f64 { unimplemented!() } } impl ContinuousDistribution for Arcsine { fn pdf(&self, x: &f64) -> f64 { let (a, b) = get_params!(self); let xab = (x - a) * (b - x); ONE_OVER_PI / xab.sqrt() } } impl UnivariateMoments for Arcsine { fn mean(&self) -> f64 { let (a, b) = get_params!(self); (a + b) / 2.0 } fn variance(&self) -> f64 { let w = self.0.w.0; w * w / 8.0 } fn skewness(&self) -> f64 { 0.0 } fn kurtosis(&self) -> f64 { THREE_HALVES } fn excess_kurtosis(&self) -> f64 { -THREE_HALVES } } impl Quantiles for Arcsine { fn quantile(&self, _: Probability) -> f64 { unimplemented!() } fn median(&self) -> f64 { self.mean() } } impl Modes for Arcsine { fn modes(&self) -> Vec<f64> { let (a, b) = get_params!(self); vec![a, b] } } impl Entropy for Arcsine { fn entropy(&self) -> f64 { PI_OVER_4.ln() } } impl fmt::Display for Arcsine { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let (a, b) = get_params!(self); write!(f, "Arcsine({}, {})", a, b) } } #[cfg(test)] mod tests { use super::{Arcsine, UnivariateMoments, Quantiles, Modes}; #[test] fn test_mean() { assert_eq!(Arcsine::new_unchecked(0.0, 1.0).mean(), 0.5); assert_eq!(Arcsine::new_unchecked(-1.0, 1.0).mean(), -0.5); assert_eq!(Arcsine::new_unchecked(-1.0, 2.0).mean(), 0.0); } #[test] fn test_variance() { assert_eq!(Arcsine::new_unchecked(0.0, 1.0).variance(), 1.0 / 8.0); assert_eq!(Arcsine::new_unchecked(-1.0, 1.0).variance(), 1.0 / 8.0); assert_eq!(Arcsine::new_unchecked(-1.0, 2.0).variance(), 1.0 / 2.0); } #[test] fn test_skewness() { assert_eq!(Arcsine::new_unchecked(0.0, 1.0).skewness(), 0.0); assert_eq!(Arcsine::new_unchecked(-1.0, 1.0).skewness(), 0.0); assert_eq!(Arcsine::new_unchecked(-1.0, 2.0).skewness(), 0.0); } #[test] fn test_median() { assert_eq!(Arcsine::new_unchecked(0.0, 1.0).median(), 0.5); assert_eq!(Arcsine::new_unchecked(-1.0, 1.0).median(), -0.5); assert_eq!(Arcsine::new_unchecked(-1.0, 2.0).median(), 0.0); } #[test] fn test_mode() { assert_eq!(Arcsine::new_unchecked(0.0, 1.0).modes(), vec![0.0, 1.0]); assert_eq!(Arcsine::new_unchecked(-1.0, 1.0).modes(), vec![-1.0, 0.0]); assert_eq!(Arcsine::new_unchecked(-1.0, 2.0).modes(), vec![-1.0, 1.0]); } }