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