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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::extra::Extra;
use crate::special::Gamma;
use crate::Functions;
/// A module containing functions to work with the Beta Functions.
pub struct Beta;
impl Beta {
/// Calculates the natural logarithm of the Beta function using the definition of the Beta function.
///
/// The Beta function, B(z1, z2), is defined as B(z1, z2) = Γ(z1) * Γ(z2) / Γ(z1 + z2), where Γ represents the gamma function.
///
/// # Parameters
///
/// - `z1`: The first parameter of the Beta function.
/// - `z2`: The second parameter of the Beta function.
///
/// # Returns
///
/// The natural logarithm of the Beta function.
///
/// # Example
///
/// ```rust
/// use numerilib::special::Beta;
///
/// let z1 = 1_f64;
/// let z2 = 2_f64;
/// let lnbeta = Beta::lnbeta(z1, z2);
///
/// println!("Natural Logarithm of Beta({}, {}) is: {}", z1, z2, lnbeta);
/// ```
/// <hr/>
pub fn lnbeta(z1: f64, z2: f64) -> f64 {
Gamma::lanczosln(z1) + Gamma::lanczosln(z2) - Gamma::lanczosln(z1 + z2)
}
/// Calculates the Beta function using the natural logarithm of the Beta function.
///
/// The Beta function, B(z1, z2), is defined as B(z1, z2) = Γ(z1) * Γ(z2) / Γ(z1 + z2), where Γ represents the gamma function.
///
/// # Parameters
///
/// - `z1`: The first parameter of the Beta function.
/// - `z2`: The second parameter of the Beta function.
///
/// # Returns
///
/// The value of the Beta function.
///
/// # Example
///
/// ```rust
/// use numerilib::special::Beta;
///
/// let z1 = 8_f64;
/// let z2 = 7_f64;
/// let beta = Beta::beta(z1, z2);
///
/// println!("Beta({}, {}) is: {}", z1, z2, beta);
/// ```
/// <hr/>
pub fn beta(z1: f64, z2: f64) -> f64 {
Self::lnbeta(z1, z2).exp()
}
/// Calculates the Incomplete Beta function (I_x(z1, z2)).
///
/// # Parameters
///
/// - `x`: The upper limit of integration.
/// - `z1`: The first parameter of the Beta function.
/// - `z2`: The second parameter of the Beta function.
///
/// # Returns
///
/// The value of the regularized incomplete Beta function.
///
/// # Example
///
/// ```rust
/// use numerilib::special::Beta;
///
/// let x = 0.2_f64;
/// let z1 = 2.0_f64;
/// let z2 = 3.0_f64;
/// let incbeta = Beta::incbeta(x, z1, z2);
///
/// println!("Incomplete Beta({}, {}, {}) is: {}", x, z1, z2, incbeta);
/// ```
/// <hr/>
pub fn incbeta(z1: f64, z2: f64, x: f64) -> f64 {
let reg = Self::regincbeta(z1, z2, x);
let beta = Self::beta(z1, z2);
reg * beta
}
/// Calculates the regularized incomplete Beta function using a series definition.
///
/// The regularized incomplete Beta function, I_x(z1, z2), is defined as (1/B(z1, z2)) * ∫[0 to x] t^(z1-1) * (1-t)^(z2-1) dt, where B represents the Beta function.
///
/// # Parameters
///
/// - `z1`: The first parameter of the Beta function.
/// - `z2`: The second parameter of the Beta function.
/// - `x`: The upper limit of integration.
///
/// # Returns
///
/// The value of the regularized incomplete Beta function.
///
/// # Example
///
/// ```rust
/// use numerilib::special::Beta;
///
/// let x = 1_f64 / 7_f64;
/// let z1 = 1_f64 / 2_f64;
/// let z2 = 3_f64;
/// let regincbeta = Beta::regincbeta(z1, z2, x);
///
/// println!("Regularized Incomplete Beta({}, {}, {}) is: {}", z1, z2, x, regincbeta);
/// ```
/// <hr/>
pub fn regincbeta(z1: f64, z2: f64, x: f64) -> f64 {
if !(0_f64..=1_f64).contains(&x) {
return f64::INFINITY;
}
let y = (x.powf(z1) * (1_f64 - x).powf(z2)) / (z1 * Beta::beta(z1, z2));
let func = |i: f64| {
(Self::beta(z1 + 1_f64, i + 1_f64) / Self::beta(z1 + z2, i + 1_f64)) * x.powf(i + 1_f64)
};
let result = Functions::summation(0_f64, 99_f64, func);
(result + 1_f64) * y
}
/// Approximates the inverse of the regularized incomplete Beta function.
///
/// The regularized incomplete Beta function, I_x(z1, z2), is defined as (1/B(z1, z2)) * ∫[0 to x] t^(z1-1) * (1-t)^(z2-1) dt, where B represents the Beta function.
///
/// # Parameters
///
/// - `z1`: The first parameter of the Beta function.
/// - `z2`: The second parameter of the Beta function.
/// - `x`: The target probability, typically a value between 0 and 1.
///
/// # Returns
///
/// An approximation of the inverse of the regularized incomplete Beta function.
///
/// # Example
///
/// ```rust
/// use numerilib::special::Beta;
///
/// let z1 = 1_f64;
/// let z2 = 2_f64;
/// let x = 0.590401_f64;
/// let inverse = Beta::invregincbeta(z1, z2, x);
///
/// println!("Inverse Regularized Incomplete Beta({}, {}, {}) is: {}", z1, z2, x, inverse);
/// ```
/// <hr/>
pub fn invregincbeta(z1: f64, z2: f64, x: f64) -> f64 {
if !(0_f64..1_f64).contains(&x) {
return f64::NAN;
}
let target_prob = x;
let func = |t: f64| Beta::regincbeta(z1, z2, t) - target_prob;
let mut guess = 0.5;
if x - 1e-1 <= 1e-6 {
let mut low = 0.0;
let mut high = 1.0;
while high - low > Extra::EPSILON2 {
if func(guess) < 0.0 {
low = guess;
} else {
high = guess;
}
guess = (low + high) / 2.0;
}
}
Functions::newmet(guess, func)
}
}