Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StatMath.cs
1using System;
2using System.Numerics;
3
5{
9 public static class StatMath
10 {
11 #region erf
12
18 public static double Erf(double x)
19 {
20 double mz2 = -x * x;
21 double Sum = x;
22 double Product = 1;
23 double Term;
24 int n = 0;
25
26 do
27 {
28 n++;
29 Product *= mz2 / n;
30 Term = x * Product / (2 * n + 1);
31 Sum += Term;
32 }
33 while (Math.Abs(Term) > 1e-10);
34
35 Sum *= erfC;
36
37 return Sum;
38 }
39
45 public static Complex Erf(Complex z)
46 {
47 Complex mz2 = -z * z;
48 Complex Sum = z;
49 Complex Product = 1;
50 Complex Term;
51 int n = 0;
52
53 do
54 {
55 n++;
56 Product *= mz2 / n;
57 Term = z * Product / (2 * n + 1);
58 Sum += Term;
59 }
60 while (Complex.Abs(Term) > 1e-10);
61
62 Sum *= erfC;
63
64 return Sum;
65 }
66
67 private static readonly double erfC = 2 / Math.Sqrt(Math.PI);
68
69 #endregion
70
71 #region Γ
72
78 public static double Γ(double x)
79 {
80 // References:
81 // https://rosettacode.org/wiki/Gamma_function#C.23
82
83 if (x < 0.5)
84 return Math.PI / (Math.Sin(Math.PI * x) * Γ(1 - x)); // 5.5.3: https://dlmf.nist.gov/5.5
85 else
86 {
87 // 5.11.3: https://dlmf.nist.gov/5.11
88
89 double v = x + 6.5;
90 double w = Math.Pow(v, x - 0.5);
91 double u = 0.99999999999980993;
92
93 u += 676.5203681218851 / x++;
94 u += -1259.1392167224028 / x++;
95 u += 771.32342877765313 / x++;
96 u += -176.61502916214059 / x++;
97 u += 12.507343278686905 / x++;
98 u += -0.13857109526572012 / x++;
99 u += 9.9843695780195716e-6 / x++;
100 u += 1.5056327351493116e-7 / x++;
101
102 return gammaC * w * Math.Exp(-v) * u;
103 }
104 }
105
111 public static Complex Γ(Complex z)
112 {
113 // References:
114 // https://rosettacode.org/wiki/Gamma_function#C.23
115
116 if (z.Real < 0.5)
117 return Math.PI / (Complex.Sin(Math.PI * z) * Γ(1 - z)); // 5.5.3: https://dlmf.nist.gov/5.5
118 else
119 {
120 // 5.11.3: https://dlmf.nist.gov/5.11
121
122 Complex v = z + 6.5;
123 Complex w = Complex.Pow(v, z - 0.5);
124 Complex u = 0.99999999999980993;
125
126 u += 676.5203681218851 / z;
127 u += -1259.1392167224028 / (z + 1);
128 u += 771.32342877765313 / (z + 2);
129 u += -176.61502916214059 / (z + 3);
130 u += 12.507343278686905 / (z + 4);
131 u += -0.13857109526572012 / (z + 5);
132 u += 9.9843695780195716e-6 / (z + 6);
133 u += 1.5056327351493116e-7 / (z + 7);
134
135 return gammaC * w * Complex.Exp(-v) * u;
136 }
137 }
138
139 private static readonly double gammaC = Math.Sqrt(2 * Math.PI);
140
147 public static double γ(double a, double x)
148 {
149 if (x == 0)
150 return 0;
151
152 double c = Math.Abs(a);
153 if (c > 1.1 && Math.Abs(x) > c)
154 return Γ(a) - Γ(a, x);
155
156 return γ(a, x, 1e-10);
157 }
158
159 private static double γ(double a, double x, double eps)
160 {
161 double c = Math.Pow(x, a) * Math.Exp(-x);
162 double n = 1;
163 double d = a++;
164 double Term = n / d;
165 double Sum = Term;
166
167 do
168 {
169 n *= x;
170 d *= a++;
171 Term = n / d;
172 Sum += Term;
173 }
174 while (Math.Abs(Term) > eps);
175
176 return c * Sum;
177 }
178
185 public static Complex γ(Complex a, Complex z)
186 {
187 if (z == Complex.Zero)
188 return Complex.Zero;
189
190 double c = Complex.Abs(a);
191 if (c > 1.1 && Complex.Abs(z) > c)
192 return Γ(a) - Γ(a, z);
193
194 return γ(a, z, 1e-10);
195 }
196
197 private static Complex γ(Complex a, Complex z, double eps)
198 {
199 Complex c = Complex.Pow(z, a) * Complex.Exp(-z);
200 Complex n = 1;
201 Complex d = a;
202 Complex Term = n / d;
203 Complex Sum = Term;
204
205 a += 1;
206 do
207 {
208 n *= z;
209 d *= a;
210 a += 1;
211 Term = n / d;
212 Sum += Term;
213 }
214 while (Complex.Abs(Term) > eps);
215
216 return c * Sum;
217 }
218
225 public static double Γ(double a, double x)
226 {
227 if (x == 0)
228 return Γ(a);
229
230 double c = Math.Abs(a);
231 if (c <= 1.1 || Math.Abs(x) <= c)
232 return Γ(a) - γ(a, x);
233
234 return Γ(a, x, 60);
235 }
236
237 private static double Γ(double a, double x, int N)
238 {
239 double n, d, q;
240 int i;
241
242 q = 0;
243 for (i = N; i > 0; i--)
244 {
245 d = q + 1 + 2 * i + x - a;
246 n = i * (a - i);
247 q = n / d;
248 }
249
250 n = Math.Pow(x, a) * Math.Exp(-x);
251 d = 1 + x - a + q;
252 return n / d;
253 }
254
261 public static Complex Γ(Complex a, Complex z)
262 {
263 if (z == Complex.Zero)
264 return Γ(a);
265
266 double c = Complex.Abs(a);
267 if (c <= 1.1 || Complex.Abs(z) <= c)
268 return Γ(a) - γ(a, z);
269
270 return Γ(a, z, 60);
271 }
272
273 private static Complex Γ(Complex a, Complex z, int N)
274 {
275 Complex n, d, q;
276 int i;
277
278 q = 0;
279 for (i = N; i > 0; i--)
280 {
281 d = q + 1 + 2 * i + z - a;
282 n = i * (a - i);
283 q = n / d;
284 }
285
286 n = Complex.Pow(z, a) * Complex.Exp(-z);
287 d = 1 + z - a + q;
288 return n / d;
289 }
290
291 #endregion
292
293 #region Β
294
301 public static double Β(double a, double b)
302 {
303 return Γ(a) * Γ(b) / Γ(a + b); // 5.12.1: https://dlmf.nist.gov/5.12
304 }
305
312 public static Complex Β(Complex a, Complex b)
313 {
314 return Γ(a) * Γ(b) / Γ(a + b); // 5.12.1: https://dlmf.nist.gov/5.12
315 }
316
317 #endregion
318 }
319}
Contains Numerical Methods to compute mathematical functions needed for probabilistic computations.
Definition: StatMath.cs:10
static Complex Β(Complex a, Complex b)
Beta-function Β(a,b)
Definition: StatMath.cs:312
static double Β(double a, double b)
Beta-function Β(a,b)
Definition: StatMath.cs:301
static double Γ(double x)
Gamma function Γ(x), for real-valued arguments.
Definition: StatMath.cs:78
static double Erf(double x)
Error function erf(x)
Definition: StatMath.cs:18
static Complex Γ(Complex a, Complex z)
Incomplete gamma function Γ(a,z), γ(a,z)+Γ(a,z)=Γ(a)
Definition: StatMath.cs:261
static Complex γ(Complex a, Complex z)
Incomplete gamma function γ(a,z)→Γ(a),z→∞
Definition: StatMath.cs:185
static Complex Γ(Complex z)
Gamma function Γ(x), for real-valued arguments.
Definition: StatMath.cs:111
static double γ(double a, double x)
Incomplete gamma function γ(a,x)→Γ(a),x→∞
Definition: StatMath.cs:147
static double Γ(double a, double x)
Incomplete gamma function Γ(a,x), γ(a,x)+Γ(a,x)=Γ(a)
Definition: StatMath.cs:225
static Complex Erf(Complex z)
Error function erf(z)
Definition: StatMath.cs:45