forked from david-cortes/approxcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plackett.cpp
1265 lines (1139 loc) · 44.6 KB
/
plackett.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
#include "approxcdf.h"
/* Plackett, Robin L.
"A reduction formula for normal multivariate integrals."
Biometrika 41.3/4 (1954): 351-360.
Gassmann, H. I.
"Multivariate normal probabilities: implementing an old idea of Plackett's."
Journal of Computational and Graphical Statistics 12.3 (2003): 731-752.
(Particularly, matrices no. 2, 5, 7, with no.7 being Plackett's original)
IMPORTANT: something's very unclear in both of the papers when there is
more than one rho being corrected for:
Should the matrix in the gradients replace only the rho for the entry
that's being integrated for, keeping the rest as they were in the
original matrix, or should it replace all of them with the linear
combination for all the elements that are going to be corrected for?
According to the original paper, each correction should replace only one
entry at each matrix, but then the examples about PSD of the matrices mention
all of them being replaced at once. The second paper is ambiguous about it.
One can do the following thought experiment: set the reference matrix to be
the original rho with one arbitrary element zeroed-out, then calculate the
correction for it, and calculate the probability for this "reference"
through the same plackett function recursively, until one or two variables
become independent. In this way, it will end up integrating at each time
a single element, but each matrix used for them will be neither the original
with a different element, nor a linear combination of the corrections: it
will at each time be a matrix with one less zeroed-out element, but with
a remainder of elements that come before being the originals and the ones
that come after being zeros, which does not match with either paper.
The implementation was tested in the 3 possible ways:
- Ascending zeroing out (as if it were called recursively).
- Using the full linear combination as replacement.
- Replacing a single element.
Only the first one, even if it does not follow what's described in the papers
to the letter (but can be obtained by the same papers result by just switching
to a "bad" reference matrix which is evaluated through the same method), is
able to produce results that are within the ballpark for full correlation matrices,
while the other two can get *very* wrong.
The second paper says that this method, when using matrices 2 and 5 (the ones
implemented here) should give a maximum error of 5e-5, but from some tests
the *average* error is around 1e-3 and it can get as bad as 1e-1. It is
*particularly bad* when the matrices have a low determinant (probably
because their inverses as calculated here are too imprecise) - for example,
for a determinant of 1e-5, it might not be correct to even the first decimal.
The paper also mentions that matrix 2 gives slightly lower errors than
matrix 5, but some tests here show that matrix 5 has generally lower errors,
and in general, the error grows very badly with the amount of corrections
that need to be performed.
A similar conclusion about the errors being unsatisfactory was reached in:
Guillaume, Tristan.
"Computation of the quadrivariate and pentavariate
normal cumulative distribution functions."
Communications in Statistics-Simulation and Computation 47.3 (2018): 839-851.
Not recommended to use, unless there's 3 or 4 zero-valued correlations
(meaning a single correction) and a relatively large determinant, in which
case it ends up giving reasonable results with an error of around 1e-8,
but still very slow. */
/* https://stackoverflow.com/questions/2937702/i-want-to-find-determinant-of-4x4-matrix-in-c-sharp */
double determinant4by4tri(const double x_tri[6])
{
double x00 = x_tri[0] * x_tri[0];
double x01 = x_tri[0] * x_tri[1];
double x02 = x_tri[0] * x_tri[2];
double x03 = x_tri[0] * x_tri[3];
double x04 = x_tri[0] * x_tri[4];
double x14 = x_tri[1] * x_tri[4];
double x23 = x_tri[2] * x_tri[3];
return
x_tri[5] * (
x_tri[1] * (x_tri[2] - x04) +
x_tri[2] * (x_tri[1] - x03) +
x_tri[3] * (x_tri[4] - x02) +
x_tri[4] * (x_tri[3] - x01) +
x_tri[5] * (x00 - 1.)
) +
x_tri[1] * (x03 - x_tri[1]) +
x_tri[2] * (x04 - x_tri[2]) +
x_tri[3] * (x01 - x_tri[3]) +
x_tri[4] * (x02 - x_tri[4]) +
x14 * (x14 - x23) +
x23 * (x23 - x14) +
-x00 + 1.;
}
void schur01_4x4(const double x_tri[6], double invX[3])
{
const double rtilde = std::fma(x_tri[0], x_tri[0], -1.);
const double x00 = x_tri[0] * x_tri[0];
double reg = 0;
if (rtilde >= 0) {
reg = std::sqrt(std::fmax(rtilde, std::numeric_limits<double>::min()));
reg = std::fmax(reg, 1e-16);
}
reg = 1e-16;
if (!reg) {
invX[0] = (
x00
- x_tri[1]*std::fma(x_tri[0], x_tri[3], -x_tri[1])
- x_tri[3]*std::fma(x_tri[0], x_tri[1], -x_tri[3])
- 1.
);
invX[1] = (
x00
- x_tri[2]*std::fma(x_tri[0], x_tri[4], -x_tri[2])
- x_tri[4]*std::fma(x_tri[0], x_tri[2], -x_tri[4])
- 1.
);
invX[2] = (
x_tri[5]*rtilde
- x_tri[2]*std::fma(x_tri[0], x_tri[3], -x_tri[1])
- x_tri[4]*std::fma(x_tri[0], x_tri[1], -x_tri[3])
);
}
else {
const double adj = std::fma(reg, rtilde, -1.);
invX[0] = (
x00
- x_tri[1]*std::fma(x_tri[0], x_tri[3], x_tri[1]*adj)
- x_tri[3]*std::fma(x_tri[0], x_tri[1], x_tri[3]*adj)
- 1.
);
invX[1] = (
x00
- x_tri[2]*std::fma(x_tri[0], x_tri[4], x_tri[2]*adj)
- x_tri[4]*std::fma(x_tri[0], x_tri[2], x_tri[4]*adj)
- 1.
);
invX[2] = (
x_tri[5]*rtilde
- x_tri[2]*std::fma(x_tri[0], x_tri[3], x_tri[1]*adj)
- x_tri[4]*std::fma(x_tri[0], x_tri[1], x_tri[3]*adj)
);
}
for (int ix = 0; ix < 3; ix++) {
invX[ix] /= rtilde;
}
#ifdef REGULARIZE_PLACKETT
invX[0] = std::fmax(invX[0], 1e-8);
invX[1] = std::fmax(invX[1], 1e-8);
#endif
}
/* Assumes that rho_star takes the place of rho[5] */
void produce_placketts_singular_matrix_coefs(const double *restrict rho, double rho_star, double *restrict coefs)
{
/* The system of equations can be solved in 6 possible orders,
but not all of them provide the exact same solutions.
This computation requires very high precision so it will
try all possible combinations and take the best one.
After that, if the solution is not good enough, it will
refine it through conjugate gradient updates.
Perhaps it should do these calculations in long double precision. */
constexpr const int n = 4;
constexpr const int n1 = n - 1;
int permutation[] = {0, 1, 2};
double best_gap = HUGE_VAL;
double this_gap;
int best_permutation[3];
int ordering[4] = {0, 0, 0, 3};
double coefs_this[3];
double best_coefs[3];
double rho_copy[6];
do {
std::copy(permutation, permutation + n1, ordering);
std::copy(rho, rho + 5, rho_copy);
rho_copy[5] = rho_star;
rearrange_tri(rho_copy, ordering);
coefs_this[0] = (
- rho_copy[0]*rho_copy[3]*rho_copy[5]
+ rho_copy[0]*rho_copy[4]
- rho_copy[1]*rho_copy[3]*rho_copy[4]
+ rho_copy[1]*rho_copy[5]
+ rho_copy[2]*rho_copy[3]*rho_copy[3]
- rho_copy[2]
) / (
rho_copy[0]*rho_copy[0]
- 2.*rho_copy[0]*rho_copy[1]*rho_copy[3]
+ rho_copy[1]*rho_copy[1]
+ std::fma(rho_copy[3], rho_copy[3], -1.)
);
coefs_this[1] = (
coefs_this[0]*rho_copy[0]
- coefs_this[0]*rho_copy[1]*rho_copy[3]
+ rho_copy[3]*rho_copy[5]
- rho_copy[4]
) / (
std::fma(-rho_copy[3], rho_copy[3], 1.)
);
coefs_this[2] = rho_star - rho[1]*coefs_this[0] - rho[3]*coefs_this[1];
if (
std::isnan(coefs_this[0]) || std::isinf(coefs_this[0]) ||
std::isnan(coefs_this[1]) || std::isinf(coefs_this[1]) ||
std::isnan(coefs_this[2]) || std::isinf(coefs_this[2])
) {
continue;
}
this_gap = rho_copy[2]*coefs_this[0] + rho_copy[4]*coefs_this[1] + rho_copy[5]*coefs_this[2];
this_gap = std::fabs(this_gap - 1.);
if (this_gap < best_gap) {
best_gap = this_gap;
std::copy(permutation, permutation + n1, best_permutation);
std::copy(coefs_this, coefs_this + n1, best_coefs);
}
}
while (std::next_permutation(permutation, permutation + n1));
if (best_gap <= 0.1) {
for (int ix = 0; ix < n1; ix++) {
coefs[ix] = best_coefs[best_permutation[ix]];
}
}
else {
coefs[0] = 0; coefs[1] = 0, coefs[2] = 0;
}
if (best_gap >= 1e-4) {
const double cg_tol = 1e-15;
double reg = 1e-10;
int cg_counter = 0;
repeat_cg:
double r[] = {
std::fma(-rho[1], coefs[2], rho[2]) - std::fma(rho[0], coefs[1], coefs[0]) - reg*coefs[0],
std::fma(-rho[3], coefs[2], rho[4]) - std::fma(rho[0], coefs[0], coefs[1]) - reg*coefs[1],
std::fma(-rho[3], coefs[1], rho_star) - std::fma(rho[1], coefs[0], coefs[2]) - reg*coefs[2]
};
double rnorm = r[0]*r[0] + r[1]*r[1] + r[2]*r[2];
double p[] = {r[0], r[1], r[2]};
double Ap[3];
double rnorm_new;
double alpha;
double beta;
for (int cg_update = 0; cg_update < 5; cg_update++) {
if (rnorm <= cg_tol) return;
Ap[0] = std::fma(rho[0], p[1], p[0]) + rho[1]*p[2] + reg*p[0];
Ap[1] = std::fma(rho[0], p[0], p[1]) + rho[3]*p[2] + reg*p[1];
Ap[2] = rho[1]*p[0] + std::fma(rho[3], p[1], p[2]) + reg*p[2];
alpha = rnorm / (p[0]*Ap[0] + p[1]*Ap[1] + p[2]*Ap[2]);
coefs[0] = std::fma(alpha, p[0], coefs[0]);
coefs[1] = std::fma(alpha, p[1], coefs[1]);
coefs[2] = std::fma(alpha, p[2], coefs[2]);
r[0] = std::fma(-rho[1], coefs[2], rho[2]) - std::fma(rho[0], coefs[1], coefs[0]) - reg*coefs[0];
r[1] = std::fma(-rho[3], coefs[2], rho[4]) - std::fma(rho[0], coefs[0], coefs[1]) - reg*coefs[1];
r[2] = std::fma(-rho[3], coefs[1], rho_star) - std::fma(rho[1], coefs[0], coefs[2]) - reg*coefs[2];
rnorm_new = r[0]*r[0] + r[1]*r[1] + r[2]*r[2];
beta = rnorm_new / rnorm;
p[0] = std::fma(beta, p[0], r[0]);
p[1] = std::fma(beta, p[1], r[1]);
p[2] = std::fma(beta, p[2], r[2]);
rnorm = rnorm_new;
}
cg_counter++;
if (cg_counter < 15 && (std::isnan(rnorm) || rnorm > cg_tol)) {
reg *= 2.5;
if (std::fabs(coefs[0]) >= 25. || std::fabs(coefs[1]) >= 25. || std::fabs(coefs[2]) >= 25.) {
coefs[0] = 0; coefs[1] = 0, coefs[2] = 0;
}
goto repeat_cg;
}
}
}
/* assumes rho{2,3} (index 5 in triangular arrray) is the one that gets changed */
double produce_placketts_singular_matrix_rho(const double *restrict rho)
{
double one_mins_rho0sq = std::fma(-rho[0], rho[0], 1.);
double root = std::sqrt(
(one_mins_rho0sq - rho[1]*rho[1] - rho[3]*rho[3] + 2.*rho[0]*rho[1]*rho[3])
*
(one_mins_rho0sq - rho[2]*rho[2] - rho[4]*rho[4] + 2.*rho[0]*rho[2]*rho[4])
);
double c1 = rho[2] * std::fma(-rho[0], rho[3], rho[1]) +
rho[4] * std::fma(-rho[0], rho[1], rho[3]);
double sol1 = (c1 + root) / one_mins_rho0sq;
double sol2 = (c1 - root) / one_mins_rho0sq;
if (std::fabs(sol1) < 1 && std::fabs(sol2) > 1) {
return sol1;
}
else if (std::fabs(sol1) > 1 && std::fabs(sol2) < 1) {
return sol2;
}
else if (rho[5] >= 0) {
return (sol1 >= 0)? sol1 : sol2;
}
else {
return (sol1 <= 0)? sol1 : sol2;
}
}
/* Checks which rho would result in the smallest integration interval
and rearranges the data accordingly so as to put it in rho{2,3},
but taking also into consideration that large rho values are problematic. */
void find_best_plackett_integrand(double *restrict x, double *restrict rho, double &restrict rho_star)
{
double rho_ordered[6];
double new_rhos[6];
double diffs[6];
/* (0,1) */
rho_ordered[0] = rho[5]; rho_ordered[1] = rho[1]; rho_ordered[2] = rho[3];
rho_ordered[3] = rho[2]; rho_ordered[4] = rho[4];
rho_ordered[5] = rho[0];
new_rhos[0] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[0] = std::fabs(rho[0] - new_rhos[0]);
/* (0,2) */
rho_ordered[0] = rho[4]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[3];
rho_ordered[3] = rho[2]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[1];
new_rhos[1] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[1] = std::fabs(rho[1] - new_rhos[1]);
/* (0,3) */
rho_ordered[0] = rho[3]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[4];
rho_ordered[3] = rho[1]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[2];
new_rhos[2] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[2] = std::fabs(rho[2] - new_rhos[2]);
/* (1,2) */
rho_ordered[0] = rho[2]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[1];
rho_ordered[3] = rho[4]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[3];
new_rhos[3] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[3] = std::fabs(rho[3] - new_rhos[3]);
/* (1,3) */
rho_ordered[0] = rho[1]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[2];
rho_ordered[3] = rho[3]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[4];
new_rhos[4] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[4] = std::fabs(rho[4] - new_rhos[4]);
/* (2,3) */
new_rhos[5] = produce_placketts_singular_matrix_rho(rho_ordered);
diffs[5] = std::fabs(rho[5] - new_rhos[5]);
constexpr const double rho_too_high = 0.925;
double best_gap = HUGE_VAL;
int best_ix = 0;
for (int ix = 0; ix < 6; ix++) {
if (std::fmax(std::fabs(rho[ix]), std::fabs(new_rhos[ix])) < rho_too_high) {
if (diffs[ix] < best_gap) {
best_ix = ix;
best_gap = diffs[ix];
}
}
}
double crit;
if (best_gap > 1.) {
for (int ix = 0; ix < 6; ix++) {
crit = diffs[ix] +
std::fmax(
1. - rho_too_high,
1. - std::fmax(
std::fabs(rho[ix]),
std::fabs(new_rhos[ix])
)
);
if (crit < best_gap) {
best_gap = crit;
best_ix = ix;
}
}
}
double x_temp[4];
switch (best_ix) {
case 0: {
x_temp[0] = x[2];
x_temp[1] = x[3];
x_temp[2] = x[0];
x_temp[3] = x[1];
std::copy(x_temp, x_temp + 4, x);
rho_ordered[0] = rho[5]; rho_ordered[1] = rho[1]; rho_ordered[2] = rho[3];
rho_ordered[3] = rho[2]; rho_ordered[4] = rho[4];
rho_ordered[5] = rho[0];
std::copy(rho_ordered, rho_ordered + 6, rho);
break;
}
case 1: {
x_temp[0] = x[1];
x_temp[1] = x[3];
x_temp[2] = x[0];
x_temp[3] = x[2];
std::copy(x_temp, x_temp + 4, x);
rho_ordered[0] = rho[4]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[3];
rho_ordered[3] = rho[2]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[1];
std::copy(rho_ordered, rho_ordered + 6, rho);
break;
}
case 2: {
x_temp[0] = x[1];
x_temp[1] = x[2];
x_temp[2] = x[0];
x_temp[3] = x[3];
std::copy(x_temp, x_temp + 4, x);
rho_ordered[0] = rho[3]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[4];
rho_ordered[3] = rho[1]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[2];
std::copy(rho_ordered, rho_ordered + 6, rho);
break;
}
case 3: {
x_temp[0] = x[0];
x_temp[1] = x[3];
x_temp[2] = x[1];
x_temp[3] = x[2];
std::copy(x_temp, x_temp + 4, x);
rho_ordered[0] = rho[2]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[1];
rho_ordered[3] = rho[4]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[3];
std::copy(rho_ordered, rho_ordered + 6, rho);
break;
}
case 4: {
x_temp[0] = x[0];
x_temp[1] = x[2];
x_temp[2] = x[1];
x_temp[3] = x[3];
std::copy(x_temp, x_temp + 4, x);
rho_ordered[0] = rho[1]; rho_ordered[1] = rho[0]; rho_ordered[2] = rho[2];
rho_ordered[3] = rho[3]; rho_ordered[2] = rho[5];
rho_ordered[5] = rho[4];
std::copy(rho_ordered, rho_ordered + 6, rho);
break;
}
case 5: {
break;
}
default: {
assert(0);
}
}
rho_star = new_rhos[best_ix];
}
/* In Plackett's singular matrix, assume that x4 is expressed
as a linear combination of the other 3 variables, and assume
some inequality like the ones solved above would be implicit,
such as:
x1>y1 & x2<y2 & x3<y4 -> x4<y4
This implies that:
P(x1>y1 & x2<y2 & x3<y3 & x4>y4) = 0
Viewing it in terms of subspaces:
s1 : (-Inf,y1]
n1 : (y1, Inf)
s0 : (-Inf, Inf)
We need to find the subspace:
s1*s2*s3*s4
(Where s1*s2 is the intersection of the two subspaces)
We have from the inequality satisfaction that, if
e.g. x1>y1 & x2<y2 & x3<y3 -> x4<y4; then:
n1*s2*s3*s4 = 0
(s0-s1)*s2*s3*s4 = 0
s2*s3*s4 - s1*s2*s3*s4 = 0
s1*s2*s3*s4 = s2*s3*s4
Thus, assuming we sort the variables accordingly in
terms of their signs, one of the following will hold:
case 1:
n1*s2*s3*s4 = 0
->
s1*s2*s3*s4 = s2*s3*s4
case2:
n1*n2*s3*s4 = 0
->
s1*s2*s3*s4 = s1*s3*s4 + s2*s3*s4 - s3*s4
case3:
n1*n2*n3*s4 = 0
->
s1*s2*s3*s4 =
s1*s2*s4 + s1*s3*s4 + s2*s3*s4
- s1*s4 - s2*s4 - s3*s4
+ s4
case4:
n1*n2*n3*n4 = 0
->
s1*s2*s3*s4 =
s1*s2*s3 + s1*s2*s4 + s1*s3*s4 + s2*s3*s4
- s1*s2 - s1*s3 - s1*s4 - s2*s3 - s2*s4 - s3*s4
+ s1 + s2 + s3 + s4
- 1
Plackett's paper is extremely scant on details or examples,
and I wasn't sure about how the spaces are supposed to be found.
I thought of the following relationships:
Denote:
x1 = y1 - s1*e1;
e1 > 0
and s1 is either +1 or -1
if s1=+1, then x1<y1
if s1=-1, then x1>y1
From the coefficients:
x4 = c1*x1 + c2*x2 + c3*x3
x4 = c1*(y1-s1*e1) + c2*(y2-s2*e2) + c3*(y3-s3*e3)
x4 = c1*y1 + c2*y2 + c3*y3 - c1*s1*e1 - c2*s2*e2 - c3*s3*e3
y4 - s4*e4 = c1*y1 + c2*y2 + c3*y3 - c1*s1*e1 - c2*s2*e2 - c3*s3*e3
s4*e4 = c1*s1*e1 + c2*s2*e2 + c3*s3*e3 - c1*y1 - c2*y2 - c3*y3 + y4
Let:
K = -(c1*y1 + c2*y2 + c3*y3) + y4
Then:
s4*e4 = K + e1*(c1*s1) + e2*(c2*s2) + e3*(c3*s3)
If:
K > 0
c1*s1 > 0
c2*s2 > 0
c3*s3 > 0
Then it implies s4:+1, which means x4<y4
If
K < 0
c1*s1 < 0
c2*s2 < 0
c3*s3 < 0
Then it implies s4:-1, which means x4>y4
(If K = 0 then either would do)
If:
x4 < y4
s1*x1 < s1*y1
s2*x2 < s2*y2
s3*x3 < s3*y3
Then:
P(x4 < y4 | s1*x1 < s1*y1 & s2*x2 < s2*y2 & s3*x3 < s3*y3) = 1
P(x4 > y4 & s1*x1 < s1*y1 & s2*x2 < s2*y2 & s3*x3 < s3*y3) = 0
Here we want to find the empty set, so we flip the last sign.
*/
double singular_cdf4(const double *restrict x, const double *restrict rho)
{
double coefs[4];
produce_placketts_singular_matrix_coefs(rho, rho[5], coefs);
double K = -(coefs[0]*x[0] + coefs[1]*x[1] + coefs[2]*x[2]) + x[3];
double signs[4];
if (K >= 0.) {
signs[0] = (coefs[0] >= 0)? 1. : -1.;
signs[1] = (coefs[1] >= 0)? 1. : -1.;
signs[2] = (coefs[2] >= 0)? 1. : -1.;
signs[3] = -1.;
}
else {
signs[0] = (coefs[0] <= 0)? 1. : -1.;
signs[1] = (coefs[1] <= 0)? 1. : -1.;
signs[2] = (coefs[2] <= 0)? 1. : -1.;
signs[3] = +1.;
}
constexpr const int n = 4;
int argsort_signs[] = {0, 1, 2, 3};
std::sort(
argsort_signs,
argsort_signs + n,
[&signs](const int a, const int b){return signs[a] < signs[b];}
);
double temp[4];
for (int ix = 0; ix < n; ix++) {
temp[ix] = signs[argsort_signs[ix]];
}
std::copy(temp, temp + n, signs);
double x_ordered[4];
double rho_ordered[6];
for (int ix = 0; ix < n; ix++) {
x_ordered[ix] = x[argsort_signs[ix]];
}
std::copy(rho, rho + 6, rho_ordered);
rearrange_tri(rho_ordered, argsort_signs);
double out;
if (signs[0] > 0.) {
return 0.;
}
else if (signs[1] > 0.) {
return norm_cdf_3d(x_ordered[1], x_ordered[2], x_ordered[3], rho_ordered[3], rho_ordered[4], rho_ordered[5]);
}
else if (signs[2] > 0.) {
out =
+ norm_cdf_3d(x_ordered[0], x_ordered[2], x_ordered[3], rho_ordered[1], rho_ordered[2], rho_ordered[5])
+ norm_cdf_3d(x_ordered[1], x_ordered[2], x_ordered[3], rho_ordered[3], rho_ordered[4], rho_ordered[5])
- norm_cdf_2d(x_ordered[2], x_ordered[3], rho_ordered[5]);
}
else if (signs[3] > 0.) {
out =
+ norm_cdf_3d(x_ordered[0], x_ordered[1], x_ordered[3], rho_ordered[0], rho_ordered[2], rho_ordered[4])
+ norm_cdf_3d(x_ordered[0], x_ordered[2], x_ordered[3], rho_ordered[1], rho_ordered[2], rho_ordered[5])
+ norm_cdf_3d(x_ordered[1], x_ordered[2], x_ordered[3], rho_ordered[3], rho_ordered[4], rho_ordered[5])
- norm_cdf_2d(x_ordered[0], x_ordered[3], rho_ordered[2])
- norm_cdf_2d(x_ordered[1], x_ordered[3], rho_ordered[4])
- norm_cdf_2d(x_ordered[2], x_ordered[3], rho_ordered[5])
+ norm_cdf_1d(x_ordered[3]);
}
else {
out = -1.
+ norm_cdf_3d(x_ordered[0], x_ordered[1], x_ordered[2], rho_ordered[0], rho_ordered[1], rho_ordered[3])
+ norm_cdf_3d(x_ordered[0], x_ordered[1], x_ordered[3], rho_ordered[0], rho_ordered[2], rho_ordered[4])
+ norm_cdf_3d(x_ordered[0], x_ordered[2], x_ordered[3], rho_ordered[1], rho_ordered[2], rho_ordered[5])
+ norm_cdf_3d(x_ordered[1], x_ordered[2], x_ordered[3], rho_ordered[3], rho_ordered[4], rho_ordered[5])
- norm_cdf_2d(x_ordered[0], x_ordered[1], rho_ordered[0])
- norm_cdf_2d(x_ordered[0], x_ordered[2], rho_ordered[1])
- norm_cdf_2d(x_ordered[0], x_ordered[3], rho_ordered[2])
- norm_cdf_2d(x_ordered[1], x_ordered[2], rho_ordered[3])
- norm_cdf_2d(x_ordered[1], x_ordered[3], rho_ordered[4])
- norm_cdf_2d(x_ordered[2], x_ordered[3], rho_ordered[5])
+ norm_cdf_1d(x_ordered[0])
+ norm_cdf_1d(x_ordered[1])
+ norm_cdf_1d(x_ordered[2])
+ norm_cdf_1d(x_ordered[3]);
}
out = std::fmax(out, 0.);
out = std::fmin(out, 1.);
return out;
}
/*
[ 1, r0, r1, r2]
[r0, 1, r3, r4]
[r1, r3, 1, r5]
[r2, r4, r5, 1]
R11 = [ 1, r0] R12 = [r1, r2]
[r0, 1] [r3, r4]
R21 = [r1, r3] R22 = [ 1, r5]
[r2, r4] [r5, 1]
*/
/* diff(cdf4, rho{0,1}) * (2*pi) */
double grad_cdf4_rho0_mult2pi(const double x[4], const double rho[6])
{
double expr1 = std::fma(-rho[0], rho[0], 1.);
#ifdef REGULARIZE_PLACKETT
double reg = 1e-5;
double d = 1.;
while (expr1 <= 0.025) {
d += reg;
expr1 = std::fma(-rho[0], rho[0], d*d);
reg *= 1.5;
}
#endif
expr1 = 1. / expr1;
double f1 = std::sqrt(expr1);
double f2 = std::exp(-0.5 * expr1 * (x[0]*x[0] + x[1]*x[1] + 2.*x[0]*x[1]*rho[0]));
double b3 = expr1 * (
x[0] * std::fma(-rho[0], rho[3], rho[1]) +
x[1] * std::fma(-rho[0], rho[1], rho[3])
);
double b4 = expr1 * (
x[0] * std::fma(-rho[0], rho[4], rho[2]) +
x[1] * std::fma(-rho[0], rho[2], rho[4])
);
double iC22[3];
schur01_4x4(rho, iC22);
double f3 = norm_cdf_2d(
(x[2] - b3) / iC22[0],
(x[3] - b4) / iC22[1],
iC22[2] / (iC22[0] * iC22[1])
);
return f1 * f2 * f3;
}
[[gnu::flatten]]
double plackett_correction_rho2_mult2pi(const double x[4], const double rho[6], const double rho_ref)
{
const double x2[] = {x[0], x[3], x[1], x[2]};
double rho2[] = {rho[2], rho[0], rho[1], rho[4], rho[5], rho[3]};
double correction = 0;
double gradp, gradn;
const double rho_grad = rho2[0];
for (int ix = 0; ix < 8; ix++) {
rho2[0] = rho_grad * GL16_xp[ix] + rho_ref * GL16_xn[ix];
gradp = grad_cdf4_rho0_mult2pi(x2, rho2);
rho2[0] = rho_grad * GL16_xn[ix] + rho_ref * GL16_xp[ix];
gradn = grad_cdf4_rho0_mult2pi(x2, rho2);
correction = std::fma(GL16_w[ix], gradp + gradn, correction);
}
return correction * (rho[2] - rho_ref);
}
/* Plackett's original, decomposes as follows:
[ 1, r0, r1, r2]
[r0, 1, r3, r4]
[r1, r3, 1, r5]
[r2, r4, r5, 1]
->
Ref:
[ 1, r0, r1, r2]
[r0, 1, r3, r4]
[r1, r3, 1, r*]
[r2, r4, r*, 1]
Correction:
[r5 - r*]
With the reference matrix being singular (having a determinant of
zero), which is achieved by finding a rho that would make it singular.
As the matrix is singular, its CDF can be calculated in terms of
lower-dimensional CDFs without any corrections. */
double norm_cdf_4d_pg7(const double x[4], const double rho[6])
{
double rho_star;
double rho_ordered[6];
double x_ordered[4];
std::copy(rho, rho + 6, rho_ordered);
std::copy(x, x + 4, x_ordered);
find_best_plackett_integrand(x_ordered, rho_ordered, rho_star);
double refP = singular_cdf4(x_ordered, rho_ordered);
if (std::fabs(rho_star - rho_ordered[5]) <= 1e-2) {
return refP;
}
double rho5to2[] = {
rho_ordered[1], rho_ordered[3], rho_ordered[5],
rho_ordered[0], rho_ordered[2], rho_ordered[4]
};
double x5to2[] = {x_ordered[2], x_ordered[0], x_ordered[1], x_ordered[3]};
double correction = plackett_correction_rho2_mult2pi(x5to2, rho5to2, rho_star);
double out = std::fma(correction, inv_fourPI, refP);
out = std::fmax(out, 0.);
out = std::fmin(out, 1.);
return out;
}
/* This one decomposes as follows:
[ 1, r0, r1, r2]
[r0, 1, r3, r4]
[r1, r3, 1, r5]
[r2, r4, r5, 1]
->
Ref:
[ 1, r0, r1, 0]
[r0, 1, r3, 0]
[r1, r3, 1, 0]
[ 0, 0, 0, 1]
Corrections:
[r2, r4, r5]
Zeroes out the last variable, in descending order of its rhos.
The variable that is arranged in the last place is selected so
as to minimize the integration regions. */
double norm_cdf_4d_pg5_recursive(const double x[4], const double rho[6])
{
const bool rho_nonzero[] {
std::fabs(rho[0]) > LOW_RHO, std::fabs(rho[1]) > LOW_RHO, std::fabs(rho[2]) > LOW_RHO,
std::fabs(rho[3]) > LOW_RHO, std::fabs(rho[4]) > LOW_RHO, std::fabs(rho[5]) > LOW_RHO
};
if (!rho_nonzero[0] && !rho_nonzero[1] && !rho_nonzero[2]) {
return norm_cdf_1d(x[0]) * norm_cdf_3d(x[1], x[2], x[3], rho[3], rho[4], rho[5]);
}
else if (!rho_nonzero[0] && !rho_nonzero[3] && !rho_nonzero[4]) {
return norm_cdf_1d(x[1]) * norm_cdf_3d(x[0], x[2], x[3], rho[1], rho[2], rho[5]);
}
else if (!rho_nonzero[1] && !rho_nonzero[3] && !rho_nonzero[5]) {
return norm_cdf_1d(x[2]) * norm_cdf_3d(x[0], x[1], x[3], rho[0], rho[2], rho[4]);
}
else if (!rho_nonzero[2] && !rho_nonzero[4] && !rho_nonzero[5]) {
return norm_cdf_1d(x[3]) * norm_cdf_3d(x[0], x[1], x[2], rho[0], rho[1], rho[3]);
}
/* Should maintain desired order: r{2,4,5} */
if (!rho_nonzero[2]) {
if (rho_nonzero[4]) {
swap_rho4:
const double xpass[] = {x[1], x[0], x[2], x[3]};
const double rhopass[] = {rho[0], rho[3], rho[4], rho[1], rho[2], rho[5]};
return norm_cdf_4d_pg5_recursive(xpass, rhopass);
}
else if (rho_nonzero[5]) {
swap_rho5:
const double xpass[] = {x[2], x[0], x[1], x[3]};
const double rhopass[] = {rho[1], rho[3], rho[5], rho[0], rho[2], rho[4]};
return norm_cdf_4d_pg5_recursive(xpass, rhopass);
}
else {
assert(0);
return NAN;
}
}
/* Make the calculation in descending order of rhos */
if (rho_nonzero[4] && std::fabs(rho[4]) < std::fabs(rho[2])) {
goto swap_rho4;
}
else if (rho_nonzero[5] && std::fabs(rho[5]) < std::fabs(rho[2])) {
goto swap_rho5;
}
/* If the matrix is near-singular, see if it can approximate it more easily */
if (determinant4by4tri(rho) <= 1e-3) {
double rho2to5[] = {rho[3], rho[5], rho[1], rho[4], rho[0], rho[2]};
double singular_rho2 = produce_placketts_singular_matrix_rho(rho2to5);
if (std::fabs(singular_rho2 - rho[2]) <= 1e-2) {
double x_rho2to5[] = {x[2], x[1], x[3], x[0]};
rho2to5[5] = singular_rho2;
return singular_cdf4(x_rho2to5, rho2to5);
}
}
/* Will zero-out r2 and correct for it */
const double rhoref[] = {rho[0], rho[1], 0., rho[3], rho[4], rho[5]};
double refP = norm_cdf_4d_pg5_recursive(x, rhoref);
double correction = plackett_correction_rho2_mult2pi(x, rho, 0.);
double out = std::fma(correction, inv_fourPI, refP);
out = std::fmax(out, 0.);
out = std::fmin(out, 1.);
return out;
}
/* This one decomposes as follows:
[ 1, r0, r1, r2]
[r0, 1, r3, r4]
[r1, r3, 1, r5]
[r2, r4, r5, 1]
->
Ref:
[ 1, r0, 0, 0]
[r0, 1, 0, 0]
[ 0 , 0, 1, r5]
[ 0, 0, r5, 1]
Corrections:
[r1, r2]
[r3, r4]
Zeros out the upper-left corner, in descending order of its rhos.
The block that is placed there is selected so as to minimize
the integration regions. */
double norm_cdf_4d_pg2_recursive(const double x[4], const double rho[6])
{
const bool rho_nonzero[] {
std::fabs(rho[0]) > LOW_RHO, std::fabs(rho[1]) > LOW_RHO, std::fabs(rho[2]) > LOW_RHO,
std::fabs(rho[3]) > LOW_RHO, std::fabs(rho[4]) > LOW_RHO, std::fabs(rho[5]) > LOW_RHO
};
if (!rho_nonzero[1] && !rho_nonzero[2] && !rho_nonzero[3] && !rho_nonzero[4]) {
return norm_cdf_2d(x[0], x[1], rho[0]) * norm_cdf_2d(x[2], x[3], rho[5]);
}
else if (!rho_nonzero[0] && !rho_nonzero[1] &&!rho_nonzero[4] && !rho_nonzero[5]) {
return norm_cdf_2d(x[2], x[1], rho[3]) * norm_cdf_2d(x[0], x[3], rho[2]);
}
else if (!rho_nonzero[0] && !rho_nonzero[2] && !rho_nonzero[3] && !rho_nonzero[5]) {
return norm_cdf_2d(x[3], x[1], rho[4]) * norm_cdf_2d(x[2], x[0], rho[1]);
}
/* Should maintain desired order: r{2,4,3,1} */
if (!rho_nonzero[2]) {
if (rho_nonzero[4]) {
swap_rho4:
const double xpass[] = {x[1], x[0], x[2], x[3]};
const double rhopass[] = {rho[0], rho[3], rho[4], rho[1], rho[2], rho[5]};
return norm_cdf_4d_pg2_recursive(xpass, rhopass);
}
else if (rho_nonzero[3]) {
swap_rho3:
const double xpass[] = {x[1], x[0], x[3], x[2]};
const double rhopass[] = {rho[0], rho[4], rho[3], rho[2], rho[1], rho[5]};
return norm_cdf_4d_pg2_recursive(xpass, rhopass);
}
else if (rho_nonzero[1]) {
swap_rho1:
const double xpass[] = {x[0], x[1], x[3], x[2]};
const double rhopass[] = {rho[0], rho[2], rho[1], rho[4], rho[3], rho[5]};
return norm_cdf_4d_pg2_recursive(xpass, rhopass);
}
else {
assert(0);
return NAN;
}
}
/* Make the calculation in descending order of rhos */
if (rho_nonzero[4] && std::fabs(rho[4]) < std::fabs(rho[2])) {
goto swap_rho4;
}
else if (rho_nonzero[3] && std::fabs(rho[3]) < std::fabs(rho[2])) {
goto swap_rho3;
}
else if (rho_nonzero[1] && std::fabs(rho[1]) < std::fabs(rho[2])) {
goto swap_rho1;
}
/* If the matrix is near-singular, see if it can approximate it more easily */
if (determinant4by4tri(rho) <= 1e-3) {
double rho2to5[] = {rho[3], rho[5], rho[1], rho[4], rho[0], rho[2]};
double singular_rho2 = produce_placketts_singular_matrix_rho(rho2to5);
if (std::fabs(singular_rho2 - rho[2]) <= 1e-2) {
double x_rho2to5[] = {x[2], x[1], x[3], x[0]};
rho2to5[5] = singular_rho2;
return singular_cdf4(x_rho2to5, rho2to5);
}
}
/* Will zero-out r2 and correct for it */
const double rhoref[] = {rho[0], rho[1], 0., rho[3], rho[4], rho[5]};
double refP = norm_cdf_4d_pg2_recursive(x, rhoref);
double correction = plackett_correction_rho2_mult2pi(x, rho, 0.);
double out = std::fma(correction, inv_fourPI, refP);
out = std::fmax(out, 0.);
out = std::fmin(out, 1.);
return out;
}
double norm_cdf_4d_pg2or5(const double x[4], const double rho[6])
{
const double rho_sq[] = {
rho[0]*rho[0], rho[1]*rho[1], rho[2]*rho[2],
rho[3]*rho[3], rho[4]*rho[4], rho[5]*rho[5]
};
double norm_b1 = rho_sq[1] + rho_sq[2] + rho_sq[3] + rho_sq[4];
double norm_b2 = rho_sq[0] + rho_sq[1] + rho_sq[4] + rho_sq[5];
double norm_b3 = rho_sq[0] + rho_sq[2] + rho_sq[3] + rho_sq[5];
if (unlikely(norm_b1 < EPS_BLOCK)) {
double p2;
if ((x[0] < x[2] && x[0] < x[3]) || (x[1] < x[2] && x[1] < x[3])) {
p2 = norm_cdf_2d(x[0], x[1], rho[0]);
if (p2 <= 0.) {
return 0.;
}
return p2 * norm_cdf_2d(x[2], x[3], rho[5]);
}
else {
p2 = norm_cdf_2d(x[2], x[3], rho[5]);
if (p2 <= 0.) {
return 0.;
}
return p2 * norm_cdf_2d(x[0], x[1], rho[0]);
}
}
else if (unlikely(norm_b2 < EPS_BLOCK)) {
double p2;
if ((x[2] < x[0] && x[2] < x[3]) || (x[1] < x[0] && x[1] < x[3])) {
p2 = norm_cdf_2d(x[2], x[1], rho[3]);
if (p2 <= 0.) {
return 0.;
}
return p2 * norm_cdf_2d(x[0], x[3], rho[2]);
}
else {
p2 = norm_cdf_2d(x[0], x[3], rho[2]);
if (p2 <= 0.) {
return 0.;