-
Notifications
You must be signed in to change notification settings - Fork 3
/
SpecFuncs.pas
289 lines (250 loc) · 8.05 KB
/
SpecFuncs.pas
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
{
Copyright (c) Peter Karpov 2010 - 2017.
Usage of the works is permitted provided that this instrument is retained with
the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
}
{$IFDEF FPC} {$MODE DELPHI} {$ENDIF}
unit SpecFuncs; /////////////////////////////////////////////////////////////////////
{
>> Version: 0.7.1
>> Description
Approximations of some special mathematical functions. Part of InvLibs unit
collection.
>> Author
Peter Karpov
Email : [email protected]
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> ToDo
- Add lower branch of LambertW
- Add more functions
>> References
[Lambert W]
On the Lambert W Function.
R. M. Corless, G. H. Gonnet, D. E. G. Hare, D. J. Jeffrey, D. E. Knuth.
[Winitzki]
Uniform approximations for transcendental functions.
Serge Winitzki.
[Bagby]
Calculating Normal Probabilities.
Bagby, R. J.
[NDF]
Normal Distribution Function.
http://mathworld.wolfram.com/NormalDistributionFunction.html
[Nemes]
New asymptotic expansion for the Gamma(z) function.
Gergo Nemes.
[Luschny]
Approximation Formulas for the Factorial Function n!
http://www.luschny.de/math/factorial/approx/SimpleCases.html
Peter Luschny.
>> Changelog
0.7.1 : 2017.11.25 ~ FreePascal compatibility
0.7 : 2014.01.23 - Gamma function, use RealFac instead
- Error function, use normal integral instead
~ Replaced InvErf with InvNormalInt
~ Slightly improved Accuracy of InvNormalInt
~ Switched to new math constants convention
0.6.1 : 2013.03.31 * LambertW(0)
0.6 : 2013.03.18 + Normalized Sinc function
0.5 : 2013.01.30 ~ Renamed RealFactorial to RealFac
0.3 : 2012.04.26 + Real factorial and gamma functions
0.2 : 2011.11.01 + Different forms of normal distribution integrals
~ More precise approximation of Erf and related functions
0.0 : 2011.07.29 + Initial version
Notation: + added, - removed, * fixed, ~ changed
}
interface ///////////////////////////////////////////////////////////////////////////
{-----------------------<< Assorted functions >>------------------------------------}
// Upper branch of Lambert's W function aka product logarithm
function LambertW(
x : Real // [-1 / e .. +inf)
) : Real;
// Normalized Sinc function
function Sinc(
x : Real
) : Real;
// Factorial of real x. Max relative error = 1.3e-8.
function RealFac(
x : Real
) : Real;
{-----------------------<< Normal CDF >>--------------------------------------------}
// Different forms of integrals of normal distribution. Accuracy of employed
// approximations is moderate, but usually should be sufficient.
// Integral [0, x] of standard normal distribution.
// Max absolute error = 3e-5, max relative error = 3e-4.
function NormalInt(
x : Real
) : Real;
// Inverse normal integral. Max relative error = 1.7e-3.
function InvNormalInt(
x : Real
) : Real;
// Normal cumulative distribution function =
// Integral (-inf, x] of standard normal distribution
function NormalCDF(
x : Real
) : Real;
// Normal complimentary cumulative distribution function =
// Integral [x, +inf) of standard normal distribution
function NormalCCDF(
x : Real
) : Real;
// Inverse normal cumulative distribution function
function InvNormalCDF(
x : Real
) : Real;
// Inverse normal complimentary cumulative distribution function
function InvNormalCCDF(
x : Real
) : Real;
implementation //////////////////////////////////////////////////////////////////////
uses
Math, // Used: Sign
ExtraMath;
const
// Determines the accuracy of numeric calculations
Precision = DoublePrecision;
{-----------------------<< Assorted functions >>------------------------------------}
// Upper branch of Lambert's W function aka product logarithm
function LambertW(
x : Real // [-1 / e .. +inf)
) : Real;
var
y, t, s, u,
LnX1, OldY,
Epsilon : Real;
begin
// Initial approximation
Assert(x >= -1 / mE);
if x < 0 then
y := (mE * x) /
(1 + 1 /
( 1 / Sqrt(2 * mE * x + 2)
+ 1 / (mE - 1)
- 1 / Sqrt(2)
)
)
else
// y := Ln(x) - Ln( Ln(x) ) + Ln( Ln(x) ) / Ln(x) is a better approximation for
// x > 95, use it as a third piece if a faster version is required.
begin
LnX1 := Ln(x + 1);
y := LnX1 * ( 1 - Ln(1 + LnX1) / (2 + LnX1) );
end;
// Halley's method
Epsilon := y * Precision;
repeat
t := y * Exp(y) - x;
s := (y + 2) / ( 2 * (y + 1) );
u := (y + 1) * Exp(y);
OldY := y;
y := y + t / (t * s - u);
until Abs(OldY - y) <= Epsilon;
Result := y;
end;
// Normalized Sinc function
function Sinc(
x : Real
) : Real;
var
t : Real;
begin
t := mTau * x / 2;
if x = 0 then
Result := 1 else
Result := Sin(t) / (t);
end;
// Factorial of real x. Max relative error = 1.3e-8.
// Stieltjes's approximation from [Luschny] is used.
function RealFac(
x : Real
) : Real;
var
y, K,
R, p : Real;
const
Shift = 3;
begin
if x < 0 then
Result := 1 / (RealFac(-x) * Sinc(x))
else
begin
y := x + 1;
p := 1;
while y < Shift do
begin
p := p * y;
y := y + 1;
end;
K := Sqrt(mTau / y) * Power(y / mE, y);
R := (1 / 12) / (y + (1 / 30) / (y + (53 / 210) / (y + (195 / 371) / y)));
Result := K * Exp(R) / p;
end;
end;
{-----------------------<< Normal CDF >>--------------------------------------------}
// Different forms of integrals of normal distribution. Accuracy of employed
// approximations is moderate, but usually should be sufficient.
// Integral [0, x] of standard normal distribution.
// Max absolute error = 3e-5, max relative error = 3e-4.
function NormalInt(
x : Real
) : Real;
var
t : Real;
begin
t := x * x;
Result := Sign(x) * Sqrt
( 1 -
( 7 * Exp( -t / 2 ) +
16 * Exp( -t * ( 2 - Sqrt(2) ) ) +
Exp( -t ) * (7 + mTau * t / 8)
) / 30
) / 2;
end;
// Inverse normal integral. Max relative error = 1.7e-3.
function InvNormalInt(
x : Real
) : Real;
var
t, u : Real;
const
a = 27;
begin
t := Ln(1 - Sqr(2 * x));
u := 2 * a / mTau + t;
Result := Sign(x) * Sqrt(Sqrt(Sqr(u) - a * t) - u);
end;
// Normal cumulative distribution function =
// Integral (-inf, x] of standard normal distribution
function NormalCDF(
x : Real
) : Real;
begin
Result := 1 / 2 + NormalInt(x);
end;
// Normal complimentary cumulative distribution function =
// Integral [x, +inf) of standard normal distribution
function NormalCCDF(
x : Real
) : Real;
begin
Result := 1 / 2 - NormalInt(x);
end;
// Inverse normal cumulative distribution function
function InvNormalCDF(
x : Real
) : Real;
begin
Result := InvNormalInt(x - 1 / 2);
end;
// Inverse normal complimentary cumulative distribution function
function InvNormalCCDF(
x : Real
) : Real;
begin
Result := InvNormalInt(1 / 2 - x);
end;
end.