numerilib/stats/distr/
cauchy.rs

1/// A module containing functions to work with the Cauchy distribution.
2pub struct Cauchy;
3
4impl Cauchy {
5    /// Calculates the Probability Density Function (PDF) of the Cauchy distribution.
6    ///
7    /// The Cauchy distribution describes a symmetric distribution with heavy tails. It has no finite
8    /// mean or variance.
9    ///
10    /// # Parameters
11    ///
12    /// - `location`: The location parameter (median) of the distribution.
13    /// - `scale`: The scale parameter of the distribution.
14    /// - `x`: The value at which to evaluate the PDF.
15    ///
16    /// # Returns
17    ///
18    /// The calculated PDF.
19    ///
20    /// # Example
21    ///
22    /// ```rust
23    /// use numerilib::stats::distr::Cauchy;
24    ///
25    /// let location = 0.0;
26    /// let scale = 1.0;
27    /// let x = 2.5;
28    ///
29    /// let pdf = Cauchy::pdf(location, scale, x);
30    ///
31    /// println!("PDF at x = {}: {}", x, pdf);
32    /// ```
33    /// <hr/>
34    pub fn pdf(location: f64, scale: f64, x: f64) -> f64 {
35        1_f64 / (std::f64::consts::PI * scale * ((x - location) / scale).powi(2))
36    }
37
38    /// Calculates the Cumulative Density Function (CDF) of the Cauchy distribution.
39    ///
40    /// # Parameters
41    ///
42    /// - `location`: The location parameter (median) of the distribution.
43    /// - `scale`: The scale parameter of the distribution.
44    /// - `x`: The value at which to evaluate the CDF.
45    ///
46    /// # Returns
47    ///
48    /// The calculated CDF.
49    ///
50    /// # Example
51    ///
52    /// ```rust
53    /// use numerilib::stats::distr::Cauchy;
54    ///
55    /// let location = 0.0;
56    /// let scale = 1.0;
57    /// let x = 2.5;
58    ///
59    /// let cdf = Cauchy::cdf(location, scale, x);
60    ///
61    /// println!("CDF at x = {}: {}", x, cdf);
62    /// ```
63    /// <hr/>
64    pub fn cdf(location: f64, scale: f64, x: f64) -> f64 {
65        std::f64::consts::FRAC_1_PI * ((x - location) / scale).atan() + (1_f64 / 2_f64)
66    }
67
68    /// Calculates the Inverse Cauchy function.
69    ///
70    /// This function is used to find the value that corresponds to a given cumulative probability.
71    ///
72    /// # Parameters
73    ///
74    /// - `location`: The location parameter (median) of the distribution.
75    /// - `scale`: The scale parameter of the distribution.
76    /// - `probability`: The cumulative probability for which to find the quantile.
77    ///
78    /// # Returns
79    ///
80    /// The calculated quantile value.
81    ///
82    /// # Example
83    ///
84    /// ```rust
85    /// use numerilib::stats::distr::Cauchy;
86    ///
87    /// let location = 0.0;
88    /// let scale = 1.0;
89    /// let probability = 0.75;
90    ///
91    /// let quantile = Cauchy::inv(location, scale, probability);
92    ///
93    /// println!("Quantile for probability {}: {}", probability, quantile);
94    /// ```
95    /// <hr/>
96    pub fn inv(location: f64, scale: f64, probability: f64) -> f64 {
97        location + scale * (std::f64::consts::PI * (probability - (1_f64 / 2_f64))).tan()
98    }
99}