implied_vol/
special_function.rs

1mod erf_cody;
2pub mod normal_distribution;
3
4use crate::special_function::erf_cody::{erf_cody, erfc_cody, erfcx_cody};
5use crate::special_function::normal_distribution::{erfinv, inverse_norm_cdf, norm_pdf};
6
7/// The `SpecialFn` trait provides a collection of special mathematical functions that are commonly
8/// used in statistics, probability, and scientific computations. These methods are implemented
9/// to calculate specific special functions such as the error function (`erf`) and its complement,
10/// inverse error functions, the normal probability density function, and the inverse cumulative
11/// distribution function for a standard normal distribution.
12///
13/// # Required Methods
14///
15/// ### `erf`
16///
17/// Computes the error function `erf(x)`. The error function gives the probability of a random variable
18/// from a normal distribution falling within a certain range of standard deviations.
19///
20/// ### `erfc`
21///
22/// Computes the complementary error function `erfc(x)` which is defined as `1 - erf(x)`.
23/// It is often used to simplify formulas for survival or tail probability distributions.
24///
25/// ### `erfcx`
26///
27/// Computes the scaled complementary error function `erfcx(x)` defined as `exp(x^2) * erfc(x)`.
28/// It is often used to avoid numerical instability in calculations involving the complementary error function.
29///
30/// ### `erfinv`
31///
32/// Computes the inverse error function `erfinv(x)`. This function determines the value of the argument
33/// that produces the given value for the error function, i.e., it satisfies `erf(erfinv(x)) = x`.
34///
35/// ### `inverse_norm_cdf`
36///
37/// Computes the inverse of the cumulative distribution function (quantile function) for a standard
38/// normal distribution. That is, for a given probability `x` in the range `[0, 1]`, it returns
39/// the value `z` such that the probability of a standard normal random variable `N(0, 1)` being
40/// less than `z` equals `x`.
41///
42/// ### `norm_pdf`
43///
44/// Computes the probability density function of the standard normal distribution `N(0, 1)`.
45/// This function evaluates the PDF at a given input `x`.
46///
47/// ## 7. `norm_cdf`
48/// Computes the Cumulative Distribution Function (CDF) of the standard normal distribution at a
49/// given `x`.
50/// - This method is only available if the `"normal-distribution"` feature is enabled.
51/// - The CDF quantifies the probability that a normally distributed random variable will be
52///   less than or equal to `x`.
53/// - This function delegates to `normal_distribution::norm_cdf`.
54///
55/// # Feature Flags
56/// - The method `norm_cdf` is available only when the `"normal-distribution"` feature flag is
57///   enabled.
58pub trait SpecialFn {
59    #[inline(always)]
60    fn erf(x: f64) -> f64 {
61        erf_cody(x)
62    }
63    #[inline(always)]
64    fn erfc(x: f64) -> f64 {
65        erfc_cody(x)
66    }
67    #[inline(always)]
68    fn erfcx(x: f64) -> f64 {
69        erfcx_cody(x)
70    }
71    #[inline(always)]
72    fn erfinv(x: f64) -> f64 {
73        erfinv(x)
74    }
75    #[inline(always)]
76    fn inverse_norm_cdf(x: f64) -> f64 {
77        inverse_norm_cdf(x)
78    }
79    #[inline(always)]
80    fn norm_pdf(x: f64) -> f64 {
81        norm_pdf(x)
82    }
83    #[cfg(feature = "normal-distribution")]
84    fn norm_cdf(x: f64) -> f64 {
85        normal_distribution::norm_cdf::<Self>(x)
86    }
87}
88
89/// A struct representing the default implementation of a special function.
90pub struct DefaultSpecialFn;
91impl SpecialFn for DefaultSpecialFn {}