]>
git.proxmox.com Git - cargo.git/blob - vendor/rand/src/distributions/cauchy.rs
1 // Copyright 2018 Developers of the Rand project.
2 // Copyright 2016-2017 The Rust Project Developers.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! The Cauchy distribution.
14 use crate::distributions
::Distribution
;
15 use std
::f64::consts
::PI
;
17 /// The Cauchy distribution `Cauchy(median, scale)`.
19 /// This distribution has a density function:
20 /// `f(x) = 1 / (pi * scale * (1 + ((x - median) / scale)^2))`
21 #[deprecated(since="0.7.0", note="moved to rand_distr crate")]
22 #[derive(Clone, Copy, Debug)]
29 /// Construct a new `Cauchy` with the given shape parameters
30 /// `median` the peak location and `scale` the scale factor.
31 /// Panics if `scale <= 0`.
32 pub fn new(median
: f64, scale
: f64) -> Cauchy
{
33 assert
!(scale
> 0.0, "Cauchy::new called with scale factor <= 0");
41 impl Distribution
<f64> for Cauchy
{
42 fn sample
<R
: Rng
+ ?Sized
>(&self, rng
: &mut R
) -> f64 {
44 let x
= rng
.gen
::<f64>();
45 // get standard cauchy random number
46 // note that π/2 is not exactly representable, even if x=0.5 the result is finite
47 let comp_dev
= (PI
* x
).tan();
48 // shift and scale according to parameters
49 let result
= self.median
+ self.scale
* comp_dev
;
56 use crate::distributions
::Distribution
;
59 fn median(mut numbers
: &mut [f64]) -> f64 {
61 let mid
= numbers
.len() / 2;
65 fn sort(numbers
: &mut [f64]) {
66 numbers
.sort_by(|a
, b
| a
.partial_cmp(b
).unwrap());
70 #[cfg(not(miri))] // Miri doesn't support transcendental functions
71 fn test_cauchy_averages() {
72 // NOTE: given that the variance and mean are undefined,
73 // this test does not have any rigorous statistical meaning.
74 let cauchy
= Cauchy
::new(10.0, 5.0);
75 let mut rng
= crate::test
::rng(123);
76 let mut numbers
: [f64; 1000] = [0.0; 1000];
79 numbers
[i
] = cauchy
.sample(&mut rng
);
82 let median
= median(&mut numbers
);
83 println
!("Cauchy median: {}", median
);
84 assert
!((median
- 10.0).abs() < 0.4); // not 100% certain, but probable enough
85 let mean
= sum
/ 1000.0;
86 println
!("Cauchy mean: {}", mean
);
87 // for a Cauchy distribution the mean should not converge
88 assert
!((mean
- 10.0).abs() > 0.4); // not 100% certain, but probable enough
93 fn test_cauchy_invalid_scale_zero() {
94 Cauchy
::new(0.0, 0.0);
99 fn test_cauchy_invalid_scale_neg() {
100 Cauchy
::new(0.0, -10.0);