implied_vol/lets_be_rational/
special_function.rs

1mod error_function;
2pub mod normal_distribution;
3
4use crate::lets_be_rational::special_function::error_function::{erf_cody, erfc_cody, erfcx_cody};
5use crate::lets_be_rational::special_function::normal_distribution::inverse_norm_cdf;
6use error_function::erfinv;
7
8/// The `SpecialFn` trait provides a collection of special mathematical functions.
9///
10/// # Required Methods
11///
12/// ## `erf`
13///
14/// Computes the error function `erf(x)`. The error function gives the probability of a random variable
15/// from a normal distribution falling within a certain range of standard deviations.
16///
17/// ## `erfc`
18///
19/// Computes the complementary error function `erfc(x)` which is defined as `1 - erf(x)`.
20/// It is often used to simplify formulas for survival or tail probability distributions.
21///
22/// ## `erfcx`
23///
24/// Computes the scaled complementary error function `erfcx(x)` defined as `exp(x^2) * erfc(x)`.
25/// It is often used to avoid numerical instability in calculations involving the complementary error function.
26///
27/// ## `erfinv`
28///
29/// Computes the inverse error function `erfinv(x)`. This function determines the value of the argument
30/// that produces the given value for the error function, i.e., it satisfies `erf(erfinv(x)) = x`.
31///
32/// ## `inverse_norm_cdf`
33///
34/// Computes the inverse of the cumulative distribution function (quantile function) for a standard
35/// normal distribution. That is, for a given probability `x` in the range `[0, 1]`, it returns
36/// the value `z` such that the probability of a standard normal random variable `N(0, 1)` being
37/// less than `z` equals `x`.
38///
39/// ## `norm_cdf`
40/// Computes the Cumulative Distribution Function (CDF) of the standard normal distribution at a
41/// given `x`.
42pub trait SpecialFn {
43    #[inline(always)]
44    #[must_use]
45    fn erf(x: f64) -> f64 {
46        DefaultSpecialFn::erf(x)
47    }
48    #[inline(always)]
49    #[must_use]
50    fn erfc(x: f64) -> f64 {
51        DefaultSpecialFn::erfc(x)
52    }
53    #[inline(always)]
54    #[must_use]
55    fn erfcx(x: f64) -> f64 {
56        DefaultSpecialFn::erfcx(x)
57    }
58    #[inline(always)]
59    #[must_use]
60    fn erfinv(x: f64) -> f64 {
61        DefaultSpecialFn::erfinv(x)
62    }
63    #[inline(always)]
64    #[must_use]
65    fn inverse_norm_cdf(x: f64) -> f64 {
66        DefaultSpecialFn::inverse_norm_cdf(x)
67    }
68    #[inline(always)]
69    #[must_use]
70    fn norm_cdf(x: f64) -> f64 {
71        DefaultSpecialFn::norm_cdf(x)
72    }
73    #[inline(always)]
74    #[must_use]
75    fn one_minus_erfcx(x: f64) -> f64 {
76        DefaultSpecialFn::one_minus_erfcx(x)
77    }
78}
79
80/// A struct representing the default implementation of a special function.
81pub struct DefaultSpecialFn;
82impl SpecialFn for DefaultSpecialFn {
83    #[inline(always)]
84    fn erf(x: f64) -> f64 {
85        erf_cody(x)
86    }
87    #[inline(always)]
88    fn erfc(x: f64) -> f64 {
89        erfc_cody(x)
90    }
91    #[inline(always)]
92    fn erfcx(x: f64) -> f64 {
93        erfcx_cody(x)
94    }
95    #[inline(always)]
96    fn erfinv(x: f64) -> f64 {
97        erfinv(x)
98    }
99    #[inline(always)]
100    fn inverse_norm_cdf(x: f64) -> f64 {
101        inverse_norm_cdf(x)
102    }
103    #[inline(always)]
104    fn norm_cdf(x: f64) -> f64 {
105        normal_distribution::norm_cdf::<Self>(x)
106    }
107    #[inline(always)]
108    fn one_minus_erfcx(x: f64) -> f64 {
109        error_function::one_minus_erfcx::<Self>(x)
110    }
111}