-
Notifications
You must be signed in to change notification settings - Fork 3
/
SpecialFunctions.cpp
5964 lines (5809 loc) · 191 KB
/
SpecialFunctions.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ALGLIB++
// Based on ALGLIB: Copyright (c) Sergey Bochkanov (ALGLIB project).
// Revisions Copyright (c) Lydia Marie Williamson, Mark Hopkins Consulting
// Source License:
// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation (www.fsf.org);
// either version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// A copy of the GNU General Public License is available at http://www.fsf.org/licensing/licenses
#define InAlgLib
#include "SpecialFunctions.h"
static const double Eul = 0.5772156649015328606065, HalfPi = 1.57079632679489661923, Root2 = 1.41421356237309504880;
// === GAMMAFUNC Package ===
namespace alglib_impl {
#if !defined ALGLIB_INTERCEPTS_SPECFUNCS
static double gammafunc_gammastirf(double x) {
double y;
double w;
double v;
double stir;
double result;
w = 1.0 / x;
stir = 7.87311395793093628397E-4;
stir = -2.29549961613378126380E-4 + w * stir;
stir = -2.68132617805781232825E-3 + w * stir;
stir = 3.47222221605458667310E-3 + w * stir;
stir = 8.33333333333482257126E-2 + w * stir;
w = 1.0 + w * stir;
y = exp(x);
if (x > 143.01608) {
v = pow(x, 0.5 * x - 0.25);
y = v * (v / y);
} else {
y = pow(x, x - 0.5) / y;
}
result = 2.50662827463100050242 * y * w;
return result;
}
#endif
// Gamma function
//
// Inputs:
// X - argument
//
// Domain:
// 0 < X < 171.6
// -170 < X < 0, X is not an integer.
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE -170,-33 20000 2.3e-15 3.3e-16
// IEEE -33, 33 20000 9.4e-16 2.2e-16
// IEEE 33, 171.6 20000 2.3e-15 3.2e-16
//
// Cephes Math Library Release 2.8: June, 2000
// Original copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
// Translated to AlgoPascal by Sergey Bochkanov (2005, 2006, 2007).
// API: double gammafunction(const double x);
double gammafunction(double x) {
#if defined ALGLIB_INTERCEPTS_SPECFUNCS
return _ialglib_i_gammafunction(x);
#else
double p;
double pp;
double q;
double qq;
double z;
ae_int_t i;
double sgngam;
double result;
sgngam = 1.0;
q = fabs(x);
if (q > 33.0) {
if (x < 0.0) {
p = floor(q);
i = iround(p);
if (i % 2 == 0) {
sgngam = -1.0;
}
z = q - p;
if (z > 0.5) {
p++;
z = q - p;
}
z = q * sin(pi * z);
z = fabs(z);
z = pi / (z * gammafunc_gammastirf(q));
} else {
z = gammafunc_gammastirf(x);
}
result = sgngam * z;
return result;
}
z = 1.0;
while (x >= 3.0) {
x--;
z *= x;
}
while (x < 0.0) {
if (x > -0.000000001) {
result = z / ((1.0 + Eul * x) * x);
return result;
}
z /= x;
x++;
}
while (x < 2.0) {
if (x < 0.000000001) {
result = z / ((1.0 + Eul * x) * x);
return result;
}
z /= x;
x++;
}
if (x == 2.0) {
result = z;
return result;
}
x -= 2.0;
pp = 1.60119522476751861407E-4;
pp = 1.19135147006586384913E-3 + x * pp;
pp = 1.04213797561761569935E-2 + x * pp;
pp = 4.76367800457137231464E-2 + x * pp;
pp = 2.07448227648435975150E-1 + x * pp;
pp = 4.94214826801497100753E-1 + x * pp;
pp = 9.99999999999999996796E-1 + x * pp;
qq = -2.31581873324120129819E-5;
qq = 5.39605580493303397842E-4 + x * qq;
qq = -4.45641913851797240494E-3 + x * qq;
qq = 1.18139785222060435552E-2 + x * qq;
qq = 3.58236398605498653373E-2 + x * qq;
qq = -2.34591795718243348568E-1 + x * qq;
qq = 7.14304917030273074085E-2 + x * qq;
qq = 1.00000000000000000320 + x * qq;
result = z * pp / qq;
return result;
#endif
}
// Natural logarithm of gamma function
//
// Inputs:
// X - argument
//
// Result:
// logarithm of the absolute value of the Gamma(X).
//
// Outputs:
// SgnGam - sign(Gamma(X))
//
// Domain:
// 0 < X < 2.55e305
// -2.55e305 < X < 0, X is not an integer.
//
// ACCURACY:
// arithmetic domain # trials peak rms
// IEEE 0, 3 28000 5.4e-16 1.1e-16
// IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
// The error criterion was relative when the function magnitude
// was greater than one but absolute when it was less than one.
//
// The following test used the relative error criterion, though
// at certain points the relative error could be much higher than
// indicated.
// IEEE -200, -4 10000 4.8e-16 1.3e-16
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
// Translated to AlgoPascal by Sergey Bochkanov (2005, 2006, 2007).
// API: double lngamma(const double x, double &sgngam);
double lngamma(double x, double *sgngam) {
#if defined ALGLIB_INTERCEPTS_SPECFUNCS
return _ialglib_i_lngamma(x, sgngam);
#else
double a;
double b;
double c;
double p;
double q;
double u;
double w;
double z;
ae_int_t i;
double logpi;
double ls2pi;
double tmp;
double result;
*sgngam = 0.0;
*sgngam = 1.0;
logpi = 1.14472988584940017414;
ls2pi = 0.91893853320467274178;
if (x < -34.0) {
q = -x;
w = lngamma(q, &tmp);
p = floor(q);
i = iround(p);
if (i % 2 == 0) {
*sgngam = -1.0;
} else {
*sgngam = 1.0;
}
z = q - p;
if (z > 0.5) {
p++;
z = p - q;
}
z = q * sin(pi * z);
result = logpi - log(z) - w;
return result;
}
if (x < 13.0) {
z = 1.0;
p = 0.0;
u = x;
while (u >= 3.0) {
p--;
u = x + p;
z *= u;
}
while (u < 2.0) {
z /= u;
p++;
u = x + p;
}
if (z < 0.0) {
*sgngam = -1.0;
z = -z;
} else {
*sgngam = 1.0;
}
if (u == 2.0) {
result = log(z);
return result;
}
p -= 2.0;
x += p;
b = -1378.25152569120859100;
b = -38801.6315134637840924 + x * b;
b = -331612.992738871184744 + x * b;
b = -1162370.97492762307383 + x * b;
b = -1721737.00820839662146 + x * b;
b = -853555.664245765465627 + x * b;
c = 1.0;
c = -351.815701436523470549 + x * c;
c = -17064.2106651881159223 + x * c;
c = -220528.590553854454839 + x * c;
c = -1139334.44367982507207 + x * c;
c = -2532523.07177582951285 + x * c;
c = -2018891.41433532773231 + x * c;
p = x * b / c;
result = log(z) + p;
return result;
}
q = (x - 0.5) * log(x) - x + ls2pi;
if (x > 100000000.0) {
result = q;
return result;
}
p = 1.0 / (x * x);
if (x >= 1000.0) {
q += ((7.9365079365079365079365 * 0.0001 * p - 2.7777777777777777777778 * 0.001) * p + 0.0833333333333333333333) / x;
} else {
a = 8.11614167470508450300 * 0.0001;
a = -5.95061904284301438324 * 0.0001 + p * a;
a = 7.93650340457716943945 * 0.0001 + p * a;
a = -2.77777777730099687205 * 0.001 + p * a;
a = 8.33333333333331927722 * 0.01 + p * a;
q += a / x;
}
result = q;
return result;
#endif
}
} // end of namespace alglib_impl
namespace alglib {
double gammafunction(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::gammafunction(x);
alglib_impl::ae_state_clear();
return D;
}
double lngamma(const double x, double &sgngam) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::lngamma(x, &sgngam);
alglib_impl::ae_state_clear();
return D;
}
} // end of namespace alglib
// === NORMALDISTR Package ===
// Depends on: (AlgLibMisc) HQRND
namespace alglib_impl {
// Rationally-based approximation of erf(x) over x in (0.0, 0.5).
static double erfr0(double x) {
double xx = x * x;
const double p13 = +0.007547728033418631287834, p11 = -0.288805137207594084924010;
const double p09 = +14.3383842191748205576712, p07 = +38.0140318123903008244444;
const double p05 = +3017.82788536507577809226, p03 = +7404.07142710151470082064, p01 = +80437.3630960840172832162;
double p = x * (p01 + xx * (p03 + xx * (p05 + xx * (p07 + xx * (p09 + xx * (p11 + xx * p13))))));
const double q8 = 38.0190713951939403753468, q6 = 658.070155459240506326937;
const double q4 = 6379.60017324428279487120, q2 = 34216.5257924628539769006, q0 = 80437.3630960840172826266;
double q = q0 + xx * (q2 + xx * (q4 + xx * (q6 + xx * (q8 + xx))));
return 1.1283791670955125738961589031 * p / q;
}
// Rationally-based approximation of erfc(x) over x in [0.5, 10.0).
static double erfr1(double x) {
const double p7 = 0.5641877825507397413087057563, p6 = 9.675807882987265400604202961;
const double p5 = 77.08161730368428609781633646, p4 = 368.5196154710010637133875746, p3 = 1143.262070703886173606073338;
const double p2 = 2320.439590251635247384768711, p1 = 2898.0293292167655611275846, p0 = 1826.3348842295112592168999;
double p = p0 + x * (p1 + x * (p2 + x * (p3 + x * (p4 + x * (p5 + x * (p6 + x * p7))))));
const double q7 = 17.14980943627607849376131193, q6 = 137.1255960500622202878443578;
const double q5 = 661.7361207107653469211984771, q4 = 2094.384367789539593790281779, q3 = 4429.612803883682726711528526;
const double q2 = 6089.5424232724435504633068, q1 = 4958.82756472114071495438422, q0 = 1826.3348842295112595576438;
double q = q0 + x * (q1 + x * (q2 + x * (q3 + x * (q4 + x * (q5 + x * (q6 + x * (q7 + x)))))));
return exp(-(x * x)) * p / q;
}
// Error function
//
// The integral is
//
// x
// -
// 2 | | 2
// erf(x) = -------- | exp( - t ) dt.
// sqrt(pi) | |
// -
// 0
//
// For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
// erf(x) = 1 - erfc(x).
//
// ACCURACY:
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE 0,1 30000 3.7e-16 1.0e-16
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double errorfunction(const double x);
double errorfunction(double x) {
if (x == 0.0) return 0.0;
else if (-0.5 < x && x < +0.5) return erfr0(x);
else if (-10.0 < x && x < +10.0) return x < 0.0 ? erfr1(-x) - 1.0 : 1.0 - erfr1(x);
else return x < 0.0 ? -1.0 : +1.0;
}
// Complementary error function
//
// 1 - erf(x) =
//
// inf.
// -
// 2 | | 2
// erfc(x) = -------- | exp( - t ) dt
// sqrt(pi) | |
// -
// x
//
// For small x, erfc(x) = 1 - erf(x); otherwise rational
// approximations are computed.
//
// ACCURACY:
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE 0,26.6417 30000 5.7e-14 1.5e-14
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double errorfunctionc(const double x);
double errorfunctionc(double x) {
if (x == 0.0) return 1.0;
else if (-0.5 < x && x < +0.5) return 1.0 - erfr0(x);
else if (-10.0 < x && x < +10.0) return x < 0.0 ? 2.0 - erfr1(-x) : erfr1(x);
else return x < 0.0 ? 2.0 : 0.0;
}
// Same as normalcdf(), obsolete name.
// API: double normaldistribution(const double x);
double normaldistribution(double x) {
return normalcdf(x);
}
// Normal distribution PDF
//
// Returns Gaussian probability density function:
//
// 1
// f(x) = --------- * exp(-x^2/2)
// sqrt(2pi)
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double normalpdf(const double x);
double normalpdf(double x) {
ae_assert(isfinite(x), "NormalPDF: X is infinite");
return exp(-x * x / 2.0) / sqrt(2.0 * pi);
}
// Normal distribution CDF
//
// Returns the area under the Gaussian probability density
// function, integrated from minus infinity to x:
//
// x
// -
// 1 | | 2
// ndtr(x) = --------- | exp( - t /2 ) dt
// sqrt(2pi) | |
// -
// -inf.
//
// = ( 1 + erf(z) ) / 2
// = erfc(z) / 2
//
// where z = x/sqrt(2). Computation is via the functions
// erf and erfc.
//
// ACCURACY:
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE -13,0 30000 3.4e-14 6.7e-15
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double normalcdf(const double x);
double normalcdf(double x) {
// return 0.5 * (errorfunction(x / Root2) + 1.0);
x /= Root2;
if (-0.5 < x && x < +0.5) return 0.5 * (erfr0(x) + 1.0);
else if (-10.0 < x && x < +10.0) return x < 0.0 ? 0.5 * erfr1(-x) : 1.0 - 0.5 * erfr1(x);
else return x < 0.0 ? 0.0 : +1.0;
}
static double inverfr0(double y) {
const double s2pi = 2.50662827463100050242;
double yy = y * y;
const double p11 = -59.9633501014107895267, p09 = +98.0010754185999661536;
const double p07 = -56.6762857469070293439, p05 = +13.9312609387279679503, p03 = -1.23916583867381258016;
double p = y * yy * (p03 + yy * (p05 + yy * (p07 + yy * (p09 + yy * p11))));
const double q14 = +1.95448858338141759834, q12 = +4.67627912898881538453;
const double q10 = +86.3602421390890590575, q08 = -225.462687854119370527, q06 = +200.260212380060660359;
const double q04 = -82.0372256168333339912, q02 = +15.9056225126211695515, q00 = -1.18331621121330003142;
double q = q00 + yy * (q02 + yy * (q04 + yy * (q06 + yy * (q08 + yy * (q10 + yy * (q12 + yy * (q14 + yy)))))));
return (y + p / q) * s2pi;
}
static double inverfr1(double z) {
const double p0 = +4.05544892305962419923, p1 = +31.5251094599893866154, p2 = +57.1628192246421288162;
const double p3 = +44.0805073893200834700, p4 = +14.6849561928858024014, p5 = +2.18663306850790267539;
const double p6 = -1.40256079171354495875 * 0.1, p7 = -3.50424626827848203418 * 0.01, p8 = -8.57456785154685413611 * 0.0001;
double p = z * (p8 + z * (p7 + z * (p6 + z * (p5 + z * (p4 + z * (p3 + z * (p2 + z * (p1 + z * p0))))))));
const double q2 = +15.7799883256466749731, q3 = +45.3907635128879210584, q4 = +41.3172038254672030440;
const double q5 = +15.0425385692907503408, q6 = +2.50464946208309415979, q7 = -1.42182922854787788574 * 0.1;
const double q8 = -3.80806407691578277194 * 0.01, q9 = -9.33259480895457427372 * 0.0001;
double q = q9 + z * (q8 + z * (q7 + z * (q6 + z * (q5 + z * (q4 + z * (q3 + z * (q2 + z)))))));
return p / q;
}
static double inverfr2(double z) {
const double p0 = 3.23774891776946035970, p1 = 6.91522889068984211695, p2 = 3.93881025292474443415;
const double p3 = 1.33303460815807542389, p4 = 2.01485389549179081538 * 0.1, p5 = 1.23716634817820021358 * 0.01;
const double p6 = 3.01581553508235416007 * 0.0001, p7 = 2.65806974686737550832 * 0.000001, p8 = 6.23974539184983293730 * 0.000000001;
double p = z * (p8 + z * (p7 + z * (p6 + z * (p5 + z * (p4 + z * (p3 + z * (p2 + z * (p1 + z * p0))))))));
const double q2 = 6.02427039364742014255, q3 = 3.67983563856160859403, q4 = 1.37702099489081330271;
const double q5 = 2.16236993594496635890 * 0.1, q6 = 1.34204006088543189037 * 0.01, q7 = 3.28014464682127739104 * 0.0001;
const double q8 = 2.89247864745380683936 * 0.000001, q9 = 6.79019408009981274425 * 0.000000001;
double q = q9 + z * (q8 + z * (q7 + z * (q6 + z * (q5 + z * (q4 + z * (q3 + z * (q2 + z)))))));
return p / q;
}
// Inverse of Normal CDF
//
// Returns the argument, x, for which the area under the
// Gaussian probability density function (integrated from
// minus infinity to x) is equal to y.
//
// For small arguments 0 < y < exp(-2), the program computes
// z = sqrt( -2.0 * log(y) ); then the approximation is
// x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
// There are two rational functions P/Q, one for 0 < y < exp(-32)
// and the other for y up to exp(-2). For larger arguments,
// w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
//
// ACCURACY:
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE 0.125, 1 20000 7.2e-16 1.3e-16
// IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double invnormalcdf(const double y0);
double invnormalcdf(double y0) {
const double expm2 = 0.13533528323661269189;
if (y0 <= 0.0) return -maxrealnumber;
else if (y0 >= 1.0) return +maxrealnumber;
ae_int_t sign = y0 <= expm2 ? -1 : y0 > 1.0 - expm2 ? +1 : 0;
if (sign == 0) return inverfr0(y0 - 0.5);
else if (sign > 0) y0 = 1.0 - y0;
double x = sqrt(-2.0 * log(y0));
return sign * (x - log(x) / x - (x < 8.0 ? inverfr1(1.0 / x) : inverfr2(1.0 / x)));
}
// Same as invnormalcdf(), deprecated name
// API: double invnormaldistribution(const double y0);
double invnormaldistribution(double y0) {
return invnormalcdf(y0);
}
// Inverse of the error function
//
// Cephes Math Library Release 2.8: June, 2000
// Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
// API: double inverf(const double e);
double inverf(double e) {
return invnormaldistribution(0.5 * (e + 1.0)) / Root2;
}
// Bivariate normal PDF
//
// Returns probability density function of the bivariate Gaussian with
// correlation parameter equal to Rho:
//
// 1 ( x^2 - 2*rho*x*y + y^2 )
// f(x,y,rho) = ----------------- * exp( - ----------------------- )
// 2pi*sqrt(1-rho^2) ( 2*(1-rho^2) )
//
// with -1 < rho < +1 and arbitrary x, y.
//
// This function won't fail as long as Rho is in (-1,+1) range.
// ALGLIB: Copyright 15.11.2019 by Sergey Bochkanov
// API: double bivariatenormalpdf(const double x, const double y, const double rho);
double bivariatenormalpdf(double x, double y, double rho) {
double onerho2;
double result;
ae_assert(isfinite(x), "BivariateNormalCDF: X is infinite");
ae_assert(isfinite(y), "BivariateNormalCDF: Y is infinite");
ae_assert(isfinite(rho), "BivariateNormalCDF: Rho is infinite");
ae_assert(-1.0 < rho && rho < 1.0, "BivariateNormalCDF: Rho is not in (-1,+1) range");
onerho2 = (1.0 - rho) * (1.0 + rho);
result = exp(-(x * x + y * y - 2.0 * rho * x * y) / (2.0 * onerho2)) / (2.0 * pi * sqrt(onerho2));
return result;
}
// Internal function which computes integrand of formula (3) by Alan
// Genz times Gaussian weights (passed by user).
// ALGLIB: Copyright 15.11.2019 by Sergey Bochkanov
static double normaldistr_bvnintegrate3(double rangea, double rangeb, double x, double y, double gw, double gx) {
double r;
double t2;
double dd;
double sinr;
double cosr;
double result;
r = (rangeb - rangea) * 0.5 * gx + (rangeb + rangea) * 0.5;
t2 = tan(0.5 * r);
dd = 1.0 / (1.0 + t2 * t2);
sinr = 2.0 * t2 * dd;
cosr = (1.0 - t2 * t2) * dd;
result = gw * exp(-(x * x + y * y - 2.0 * x * y * sinr) / (2.0 * cosr * cosr));
return result;
}
// Internal function which computes integrand of formula (6) by Alan
// Genz times Gaussian weights (passed by user).
// ALGLIB: Copyright 15.11.2019 by Sergey Bochkanov
static double normaldistr_bvnintegrate6(double rangea, double rangeb, double x, double y, double s, double gw, double gx) {
double r;
double exphsk22x2;
double exphsk2;
double sqrt1x2;
double exphsk1sqrt1x2;
double result;
r = (rangeb - rangea) * 0.5 * gx + (rangeb + rangea) * 0.5;
exphsk22x2 = exp(-(x - s * y) * (x - s * y) / (2.0 * r * r));
exphsk2 = exp(-x * s * y / 2.0);
sqrt1x2 = sqrt((1.0 - r) * (1.0 + r));
exphsk1sqrt1x2 = exp(-x * s * y / (1.0 + sqrt1x2));
result = gw * exphsk22x2 * (exphsk1sqrt1x2 / sqrt1x2 - exphsk2 * (1.0 + (4.0 - x * y * s) * r * r / 8.0));
return result;
}
// Bivariate normal CDF
//
// Returns the area under the bivariate Gaussian PDF with correlation
// parameter equal to Rho, integrated from minus infinity to (x,y):
//
// x y
// - -
// 1 | | | |
// bvn(x,y,rho) = ------------------- | | f(u,v,rho)*du*dv
// 2pi*sqrt(1-rho^2) | | | |
// - -
// -INF -INF
//
// where
//
// ( u^2 - 2*rho*u*v + v^2 )
// f(u,v,rho) = exp( - ----------------------- )
// ( 2*(1-rho^2) )
//
// with -1 < rho < +1 and arbitrary x, y.
//
// This subroutine uses high-precision approximation scheme proposed by
// Alan Genz in "Numerical Computation of Rectangular Bivariate and
// Trivariate Normal and t probabilities", which computes CDF with
// absolute error roughly equal to 1e-14.
//
// This function won't fail as long as Rho is in (-1,+1) range.
// ALGLIB: Copyright 15.11.2019 by Sergey Bochkanov
// API: double bivariatenormalcdf(const double x, const double y, const double rho);
double bivariatenormalcdf(double x, double y, double rho) {
double rangea;
double rangeb;
double s;
double v;
double v0;
double v1;
double fxys;
double ta;
double tb;
double tc;
double result;
ae_assert(isfinite(x), "BivariateNormalCDF: X is infinite");
ae_assert(isfinite(y), "BivariateNormalCDF: Y is infinite");
ae_assert(isfinite(rho), "BivariateNormalCDF: Rho is infinite");
ae_assert(-1.0 < rho && rho < 1.0, "BivariateNormalCDF: Rho is not in (-1,+1) range");
if (rho == 0.0) {
result = normalcdf(x) * normalcdf(y);
return result;
}
if (SmallAtR(rho, 0.8)) {
// Rho is small, compute integral using using formula (3) by Alan Genz, integrated
// by means of 10-point Gauss-Legendre quadrature
rangea = 0.0;
rangeb = asin(rho);
v = 0.0;
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2491470458134028, -0.1252334085114689);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2491470458134028, 0.1252334085114689);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2334925365383548, -0.3678314989981802);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2334925365383548, 0.3678314989981802);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2031674267230659, -0.5873179542866175);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2031674267230659, 0.5873179542866175);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1600783285433462, -0.7699026741943047);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1600783285433462, 0.7699026741943047);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1069393259953184, -0.9041172563704749);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1069393259953184, 0.9041172563704749);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.0471753363865118, -0.9815606342467192);
v += normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.0471753363865118, 0.9815606342467192);
v = v * 0.5 * (rangeb - rangea) / (2.0 * pi);
result = normalcdf(x) * normalcdf(y) + v;
} else {
// Rho is large, compute integral using using formula (6) by Alan Genz, integrated
// by means of 20-point Gauss-Legendre quadrature.
x = -x;
y = -y;
s = sign(rho);
if (s > 0.0) {
fxys = normalcdf(-rmax2(x, y));
} else {
fxys = rmax2(0.0, normalcdf(-x) - normalcdf(y));
}
rangea = 0.0;
rangeb = sqrt((1.0 - rho) * (1.0 + rho));
// Compute first term (analytic integral) from formula (6)
ta = rangeb;
tb = fabs(x - s * y);
tc = (4.0 - s * x * y) / 8.0;
v0 = ta * (1.0 - tc * (tb * tb - ta * ta) / 3) * exp(-tb * tb / (2.0 * ta * ta)) - tb * (1.0 - tc * tb * tb / 3) * sqrt(2.0 * pi) * normalcdf(-tb / ta);
v0 = v0 * exp(-s * x * y / 2.0) / (2.0 * pi);
// Compute second term (numerical integral, 20-point Gauss-Legendre rule) from formula (6)
v1 = 0.0;
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1527533871307258, -0.0765265211334973);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1527533871307258, 0.0765265211334973);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1491729864726037, -0.2277858511416451);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1491729864726037, 0.2277858511416451);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1420961093183820, -0.3737060887154195);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1420961093183820, 0.3737060887154195);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1316886384491766, -0.5108670019508271);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1316886384491766, 0.5108670019508271);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1181945319615184, -0.6360536807265150);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1181945319615184, 0.6360536807265150);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1019301198172404, -0.7463319064601508);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1019301198172404, 0.7463319064601508);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0832767415767048, -0.8391169718222188);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0832767415767048, 0.8391169718222188);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0626720483341091, -0.9122344282513259);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0626720483341091, 0.9122344282513259);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0406014298003869, -0.9639719272779138);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0406014298003869, 0.9639719272779138);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0176140071391521, -0.9931285991850949);
v1 += normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0176140071391521, 0.9931285991850949);
v1 = v1 * 0.5 * (rangeb - rangea) / (2.0 * pi);
result = fxys - s * (v0 + v1);
}
result = rmax2(result, 0.0);
result = rmin2(result, 1.0);
return result;
}
} // end of namespace alglib_impl
namespace alglib {
double errorfunction(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::errorfunction(x);
alglib_impl::ae_state_clear();
return D;
}
double errorfunctionc(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::errorfunctionc(x);
alglib_impl::ae_state_clear();
return D;
}
double normaldistribution(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::normaldistribution(x);
alglib_impl::ae_state_clear();
return D;
}
double normalpdf(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::normalpdf(x);
alglib_impl::ae_state_clear();
return D;
}
double normalcdf(const double x) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::normalcdf(x);
alglib_impl::ae_state_clear();
return D;
}
double invnormalcdf(const double y0) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::invnormalcdf(y0);
alglib_impl::ae_state_clear();
return D;
}
double invnormaldistribution(const double y0) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::invnormaldistribution(y0);
alglib_impl::ae_state_clear();
return D;
}
double inverf(const double e) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::inverf(e);
alglib_impl::ae_state_clear();
return D;
}
double bivariatenormalpdf(const double x, const double y, const double rho) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::bivariatenormalpdf(x, y, rho);
alglib_impl::ae_state_clear();
return D;
}
double bivariatenormalcdf(const double x, const double y, const double rho) {
alglib_impl::ae_state_init();
TryCatch(0.0)
double D = alglib_impl::bivariatenormalcdf(x, y, rho);
alglib_impl::ae_state_clear();
return D;
}
} // end of namespace alglib
// === IBETAF Package ===
// Depends on: GAMMAFUNC, NORMALDISTR
namespace alglib_impl {
// Continued fraction expansion #1 for incomplete beta integral
//
// Cephes Math Library, Release 2.8: June, 2000
// Copyright 1984, 1995, 2000 by Stephen L. Moshier
static double ibetaf_incompletebetafe(double a, double b, double x, double big, double biginv) {
double xk;
double pk;
double pkm1;
double pkm2;
double qk;
double qkm1;
double qkm2;
double k1;
double k2;
double k3;
double k4;
double k5;
double k6;
double k7;
double k8;
double r;
double t;
double ans;
double thresh;
ae_int_t n;
double result;
k1 = a;
k2 = a + b;
k3 = a;
k4 = a + 1.0;
k5 = 1.0;
k6 = b - 1.0;
k7 = k4;
k8 = a + 2.0;
pkm2 = 0.0;
qkm2 = 1.0;
pkm1 = 1.0;
qkm1 = 1.0;
ans = 1.0;
r = 1.0;
n = 0;
thresh = 3.0 * machineepsilon;
do {
xk = -x * k1 * k2 / (k3 * k4);
pk = pkm1 + pkm2 * xk;
qk = qkm1 + qkm2 * xk;
pkm2 = pkm1;
pkm1 = pk;
qkm2 = qkm1;
qkm1 = qk;
xk = x * k5 * k6 / (k7 * k8);
pk = pkm1 + pkm2 * xk;
qk = qkm1 + qkm2 * xk;
pkm2 = pkm1;
pkm1 = pk;
qkm2 = qkm1;
qkm1 = qk;
if (qk != 0.0) {
r = pk / qk;
}
if (r != 0.0) {
t = fabs((ans - r) / r);
ans = r;
} else {
t = 1.0;
}
if (t < thresh) {
break;
}
k1++;
k2++;
k3 += 2.0;
k4 += 2.0;
k5++;
k6--;
k7 += 2.0;
k8 += 2.0;
if (fabs(qk) + fabs(pk) > big) {
pkm2 *= biginv;
pkm1 *= biginv;
qkm2 *= biginv;
qkm1 *= biginv;
}
if (SmallR(qk, biginv) || SmallR(pk, biginv)) {
pkm2 *= big;
pkm1 *= big;
qkm2 *= big;
qkm1 *= big;
}
n++;
} while (n != 300);
result = ans;
return result;
}
// Continued fraction expansion #2
// for incomplete beta integral
//
// Cephes Math Library, Release 2.8: June, 2000
// Copyright 1984, 1995, 2000 by Stephen L. Moshier
static double ibetaf_incompletebetafe2(double a, double b, double x, double big, double biginv) {
double xk;
double pk;
double pkm1;
double pkm2;
double qk;
double qkm1;
double qkm2;
double k1;
double k2;
double k3;
double k4;
double k5;
double k6;
double k7;
double k8;
double r;
double t;
double ans;
double z;
double thresh;
ae_int_t n;
double result;
k1 = a;
k2 = b - 1.0;
k3 = a;
k4 = a + 1.0;
k5 = 1.0;
k6 = a + b;
k7 = a + 1.0;
k8 = a + 2.0;
pkm2 = 0.0;
qkm2 = 1.0;
pkm1 = 1.0;
qkm1 = 1.0;
z = x / (1.0 - x);
ans = 1.0;
r = 1.0;
n = 0;
thresh = 3.0 * machineepsilon;
do {
xk = -z * k1 * k2 / (k3 * k4);
pk = pkm1 + pkm2 * xk;
qk = qkm1 + qkm2 * xk;
pkm2 = pkm1;
pkm1 = pk;
qkm2 = qkm1;
qkm1 = qk;
xk = z * k5 * k6 / (k7 * k8);
pk = pkm1 + pkm2 * xk;
qk = qkm1 + qkm2 * xk;
pkm2 = pkm1;
pkm1 = pk;
qkm2 = qkm1;
qkm1 = qk;
if (qk != 0.0) {
r = pk / qk;
}
if (r != 0.0) {
t = fabs((ans - r) / r);
ans = r;
} else {
t = 1.0;
}
if (t < thresh) {
break;
}
k1++;
k2--;
k3 += 2.0;
k4 += 2.0;
k5++;
k6++;
k7 += 2.0;
k8 += 2.0;
if (fabs(qk) + fabs(pk) > big) {
pkm2 *= biginv;
pkm1 *= biginv;
qkm2 *= biginv;
qkm1 *= biginv;
}
if (SmallR(qk, biginv) || SmallR(pk, biginv)) {
pkm2 *= big;
pkm1 *= big;
qkm2 *= big;
qkm1 *= big;
}
n++;
} while (n != 300);
result = ans;
return result;
}
// Power series for incomplete beta integral.
// Use when b*x is small and x not too close to 1.
//
// Cephes Math Library, Release 2.8: June, 2000