-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSCStatistics.java
1425 lines (1190 loc) · 78.4 KB
/
SCStatistics.java
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
/**
* SCStatistics - utility methods of mathematical statistics. Various tools for extracting data patterns
* and making predictions about the vessels positions.
*/
public class SCStatistics {
// Variance: used for variance analysis of potentially steady-intervals
public static class Variance{
public Variance (int _index, int _f, double _m2) {
index = _index;
f = _f;
m2 = _m2;
}
int index;
int f; // degrees of freedom
double m2; // variance (sigma0 * sigma0)
}
// Used for sorting variances in ascending order
static class SortVariances implements Comparator<Variance>
{
@Override
public int compare(Variance a, Variance b) {
return Double.compare(a.m2, b.m2);
}
}
// Results from linear regression analysis
public static class RegrResults {
int istart;
int iend;
double taumax; // maximal normalized deviation
double sigma0; // standard deviation
// estimated parameters of linear model: Yi = a*Xi + b
double a;
double b;
// ... and their estimated standard error (i.e reciprocal accuracy)
double ma;
double mb;
// parameters needed for performance ehancement (i.e. to avoid recalculating of the same sums again)
double N11;
double N12;
double n1;
double n2;
double det;
// true/false if regression line is/isn't horizontal (default is false)
boolean isHorizontal = false;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of time values from an List of totes
* @param totes given List of Tote objects
* @return double[] - array of absolute times extracted from totes
*/
public static double[] getTimes(List<Tote> totes) {
return getTimes(totes, 0, totes.size());
}
//-------------------------------------------------------------------------
/**
* Creates a double array of absolute time values from an List of totes within the given indexes
* @param totes given List of Tote objects
* @param istart (inclusive) lower index of the totes-array segment extracted for calculation
* @param iend (exclusive) upper index of the totes-array segment extracted for calculation
* @return double[] - array of absolute times extracted from totes
*/
public static double[] getTimes(List<Tote> totes, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.getTimes - 'istart' index (" + istart + ") is less than 0.");
if (totes.size() < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.getTimes - 'iend' index (" + iend + ") is greater than array length.");
if (istart > iend)
throw new NegativeArraySizeException("Calling ScStatistics.getTimes - 'istart' (" + istart + ") is less than 'iend' (" + iend + ").");
int index = 0;
double[] da = new double[iend - istart];
for (int ii=istart; ii<iend; ii++)
da[index++] = totes.get(ii).dabsolute_time;
return da;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of relative time values from an List of totes within the given indexes. The values are relative to the first time in list i.e the elapsed times from totes[istart] are recorded.
* @param totes given List of Tote objects
* @return double[] - array of relative times extracted from totes
*/
public static double[] getRelativeTimes(List<Tote> totes) {
return getRelativeTimes(totes, 0, totes.size());
}
//-------------------------------------------------------------------------
/**
* Creates a double array of relative time values from an List of totes within the given indexes. The values are relative to the first time in list i.e the elapsed times from totes[istart] are recorded.
* @param totes given List of Tote objects
* @param istart (inclusive) lower index of the totes-array segment extracted for calculation
* @param iend (exclusive) upper index of the totes-array segment extracted for calculation
* @return double[] - array of relative times extracted from totes
*/
public static double[] getRelativeTimes(List<Tote> totes, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.getRelativeTimes - 'istart' index (" + istart + ") is less than 0.");
if (totes.size() < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.getRelativeTimes - 'iend' index (" + iend + ") is greater than array length.");
if (istart > iend)
throw new NegativeArraySizeException("Calling ScStatistics.getRelativeTimes - 'istart' (" + istart + ") is less than 'iend' (" + iend + ").");
int index = 0;
double dreduce = totes.get(istart).dabsolute_time;
double[] da = new double[iend - istart];
for (int ii=istart; ii<iend; ii++)
da[index++] = totes.get(ii).dabsolute_time - dreduce;
return da;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of speed values from an List of totes
* @param totes given List of Tote objects
* @return double[] - array of speeds extracted form totes
*/
public static double[] getSpeeds(List<Tote> totes) {
int index = 0;
double[] da = new double[totes.size()];
Iterator<Tote> iter = totes.iterator();
while (iter.hasNext())
da[index++] = iter.next().dspeed;
return da;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of speed values from an List of totes within the given indexes
* @param totes given List of Tote objects
* @param istart (inclusive) lower index of the totes-array segment extracted for calculation
* @param iend (exclusive) upper index of the totes-array segment extracted for calculation
* @return double[] - array of speeds extracted form totes
*/
public static double[] getSpeeds(List<Tote> totes, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.getSpeeds - 'istart' index (" + istart + ") is less than 0.");
if (totes.size() < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.getSpeeds - 'iend' index (" + iend + ") is greater than array length.");
if (istart > iend)
throw new NegativeArraySizeException("Calling ScStatistics.getSpeeds - 'istart' (" + istart + ") is less than 'iend' (" + iend + ").");
int index = 0;
double[] da = new double[iend - istart];
for (int ii=istart; ii<iend; ii++)
da[index++] = totes.get(ii).dspeed;
return da;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of heading values from an List of totes
* @param totes given List of Tote objects
* @return double[] - array of headings extracted form totes
*/
public static double[] getHeadings(List<Tote> totes) {
double[] dl = new double[totes.size()];
int index = 0;
Iterator<Tote> iter = totes.iterator();
while (iter.hasNext())
dl[index++] = iter.next().dheading;
return dl;
}
//-------------------------------------------------------------------------
/**
* Creates a double array of sheading values from an List of totes within the given indexes
* @param totes given List of Tote objects
* @param istart (inclusive) lower index of the totes-array segment extracted for calculation
* @param iend (exclusive) upper index of the totes-array segment extracted for calculation
* @return double[] - array of headings extracted form totes
*/
public static double[] getHeadings(List<Tote> totes, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.getHeadings - 'istart' index (" + istart + ") is less than 0.");
if (totes.size() < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.getHeadings - 'iend' index (" + iend + ") is greater than array length.");
if (istart > iend)
throw new NegativeArraySizeException("Calling ScStatistics.getHeadings - 'istart' (" + istart + ") is less than 'iend' (" + iend + ").");
int index = 0;
double[] da = new double[iend - istart];
for (int ii=istart; ii<iend; ii++)
da[index++] = totes.get(ii).dheading;
return da;
}
//-------------------------------------------------------------------------
/**
* Returns maximal value from an array of doubles
* @param ardoubles given array
* @return double - max value within the array
*/
public static double max(double[] ardoubles) {
double dmax = -Double.MAX_VALUE;
for (int ii=0; ii<ardoubles.length; ii++)
dmax = Double.max(dmax, ardoubles[ii]);
return dmax;
}
//-------------------------------------------------------------------------
/**
* Returns minimal value from an array of doubles
* @param ardoubles given array
* @return double - min value within the array
*/
public static double min(double[] ardoubles) {
double dmin = Double.MAX_VALUE;
for (int ii=0; ii<ardoubles.length; ii++)
dmin = Double.min(dmin, ardoubles[ii]);
return dmin;
}
//-------------------------------------------------------------------------
/**
* Returns maximal value from a sub-array within an array of doubles
* @param ardoubles given array
* @param istart (inclusive) lower index of the sub-array
* @param iend (exclusive) upper index of the sub-array
* @return double - max value within the array
*/
public static double max(double[] ardoubles, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.max - 'istart' index (" + istart + ") is less than 0.");
if (ardoubles.length < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.max - 'iend' index (" + iend + ") is greater than array length.");
double dmax = -Double.MAX_VALUE;
for (int ii=istart; ii<iend; ii++)
dmax = Double.max(dmax, ardoubles[ii]);
return dmax;
}
//-------------------------------------------------------------------------
/**
* Returns minimal value from a sub-array within an array of doubles
* @param ardoubles given array
* @param istart (inclusive) lower index of the sub-array
* @param iend (exclusive) upper index of the sub-array
* @return double - min value within the array
*/
public static double min(double[] ardoubles, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.min - 'istart' index (" + istart + ") is less than 0.");
if (ardoubles.length < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.min - 'iend' index (" + iend + ") is greater than array length.");
double dmin = Double.MAX_VALUE;
for (int ii=istart; ii<iend; ii++)
dmin = Double.min(dmin, ardoubles[ii]);
return dmin;
}
//-------------------------------------------------------------------------
/**
* Returns mean from an array of normally distributed values
* @param ardoubles given array
* @return double - mean value (calculated as simple arithmetic mean)
*/
public static double mean(double[] ardoubles) {
double dsum = 0.0;
for (int ii=0; ii<ardoubles.length; ii++)
dsum += ardoubles[ii];
return dsum / ardoubles.length;
}
//-------------------------------------------------------------------------
/**
* Returns mean of an sub-array from an array of normally distributed values
* @param ardoubles given array
* @param istart (inclusive) lower index of the sub-array
* @param iend (exclusive) upper index of the sub-array
* @return double - mean value (calculated as simple arithmetic mean)
*/
public static double mean(double[] ardoubles, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.mean - 'istart' index (" + istart + ") is less than 0.");
if (ardoubles.length < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.mean - 'iend' index (" + iend + ") is greater than array length.");
double dsum = 0.0;
for (int ii=istart; ii<iend; ii++)
dsum += ardoubles[ii];
return dsum / (iend - istart);
}
//-------------------------------------------------------------------------
/**
* Returns mean from an array of values with different 'weight' (e.g. probability)
* @param ardoubles given array of values
* @param arweights given array of weights
* @return double - mean value (calculated as weighted arithmetic mean)
*/
public static double weighted_mean(double[] ardoubles, double[] arweights){
if (ardoubles.length != arweights.length)
throw new IndexOutOfBoundsException("Calling ScStatistics.weighted_mean - arrays of different length");
double dsum = 0.0;
double dsumweights = 0.0;
for (int ii=0; ii<ardoubles.length; ii++) {
dsum += ardoubles[ii] * arweights[ii];
dsumweights += arweights[ii];
}
return dsum / dsumweights;
}
//-------------------------------------------------------------------------
/**
* Returns standard deviation an array of normally distributed values (expected mean value is unknown)
* @param ardoubles given array
* @return double - standard deviation (calculated as simple arithmetic mean)
*/
public static double stdev(double[] ardoubles) {
double dsum = 0.0;
double dsum_squares = 0.0;
for (int ii=0; ii<ardoubles.length; ii++) {
dsum += ardoubles[ii];
dsum_squares += ardoubles[ii]*ardoubles[ii];
}
double dmean = dsum / ardoubles.length;
double d1 = dsum_squares - dmean*dmean * ardoubles.length;
if(d1 < 1e-9) return 0.0;
double d2 = d1 / (ardoubles.length - 1);
double d3 = Math.sqrt(d2);
return d3;
}
//-------------------------------------------------------------------------
/**
* Returns standard deviation from a sub-array of normally distributed values (expected mean value is unknown)
* @param ardoubles given array
* @param istart (inclusive) lower index of the sub-array
* @param iend (exclusive) upper index of the sub-array
* @return double - standard deviation (calculated as simple arithmetic mean)
*/
public static double stdev(double[] ardoubles, int istart, int iend) {
if (0 > istart)
throw new IndexOutOfBoundsException("Calling ScStatistics.stdev - 'istart' index (" + istart + ") is less than 0.");
if (ardoubles.length < iend)
throw new IndexOutOfBoundsException("Calling ScStatistics.stdev - 'iend' index (" + iend + ") is greater than array length.");
int numelemets = iend - istart;
double dsum = 0.0;
double dsum_squares = 0.0;
for (int ii=istart; ii<iend; ii++) {
dsum += ardoubles[ii];
dsum_squares += ardoubles[ii]*ardoubles[ii];
}
double dmean = dsum / numelemets;
double d1 = dsum_squares - dmean*dmean * numelemets;
if(d1 < 1e-9) return 0.0;
double d2 = d1 / (numelemets - 1);
double d3 = Math.sqrt(d2);
return d3;
}
//-------------------------------------------------------------------------
/**
* Returns standard deviation an array of normally distributed values (please note: expected 'mean' value is known)
* @param ardoubles given array
* @param dmean the expected value of stochastic variable which is represented by array values
* @return double - standard deviation (calculated as simple arithmetic mean)
*/
public static double stdev_mn(double[] ardoubles, double dmean) {
double dsum_squares = 0.0;
for (int ii=0; ii<ardoubles.length; ii++)
dsum_squares += ardoubles[ii]*ardoubles[ii];
double d1 = dsum_squares - dmean*dmean * ardoubles.length;
if(d1 < 1e-9) return 0.0;
double d2 = d1 / ardoubles.length;
double d3 = Math.sqrt(d2);
return d3;
}
//-------------------------------------------------------------------------
/**
* Returns standard deviation from an array of values with different 'weight' (e.g. probability)
* @param ardoubles given array
* @param arweights given array of weights
* @return double - standard deviation (calculated as simple arithmetic mean)
*/
public static double weighted_stdev(double[] ardoubles, double[] arweights) {
if (ardoubles.length != arweights.length)
throw new IndexOutOfBoundsException("Calling ScStatistics.weighted_mean - arrays of different length");
double dsum = 0.0;
double dsum_squares = 0.0;
double dsumweights = 0.0;
for (int ii=0; ii<ardoubles.length; ii++) {
double dtemp = ardoubles[ii] * arweights[ii];
dsum += dtemp;
dtemp *= ardoubles[ii];
dsum_squares += dtemp;
dsumweights += arweights[ii];
}
double dmean = dsum / dsumweights;
double d1 = dsum_squares - dmean*dmean * dsumweights;
if(d1 < 1e-9) return 0.0;
double d2 = d1 / dsumweights;
double d3 = Math.sqrt(d2);
return d3;
}
//-------------------------------------------------------------------------
/**
* Returns two sided quantile of Student's distribution with the 95% confidence interval
* @param inum number of observed/measured data in dataset
* @return double - quantile of Student's distribution
* @see https://en.wikipedia.org/wiki/Student%27s_t-distribution
*/
static double get95StudentQuantil(int inum) {
if (inum < 1)
throw new IllegalArgumentException("Calling ScStatistics.get95StudentQuantil - number of elements leass than 1");
final double[] quantiles_1_30 = {
12.71, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 2.262, 2.228,
2.201, 2.179, 2.160, 2.145, 2.131, 2.120, 2.110, 2.101, 2.093, 2.086,
2.080, 2.074, 2.069, 2.064, 2.060, 2.056, 2.052, 2.048, 2.045, 2.042
};
final double[] quantiles_32_50 = { // increment = 2
2.037, 2.032, 2.028, 2.024, 2.021, 2.018, 2.015, 2.013, 2.011, 2.009
};
final double[] quantiles_55_70 = { // increment = 5
2.004, 2.000, 1.997, 1.994
};
final double[] quantiles_80_100 = { // increment = 10
1.990, 1.987, 1.984
};
final double quantil_120 = 1.980;
final double[] quantiles_150_300 = { // increment = 50
1.976, 1.972, 1.970, 1.968
};
final double[] quantiles_400_500 = { // increment = 100
1.966, 1.965
};
final double quantil_infinity = 1.960;
if (inum < 30) return quantiles_1_30[inum-1];
else if (inum < 32) return quantiles_1_30[29]; // 2.042
else if (inum <= 50) return quantiles_32_50[(inum-32) / 2];
else if (inum < 55) return quantiles_32_50[9]; // 2.009
else if (inum <= 70) return quantiles_55_70[(inum-55) / 5];
else if (inum < 80) return quantiles_55_70[3]; // 1.994
else if (inum <= 100) return quantiles_80_100[(inum-80) / 10];
else if (inum < 120) return quantiles_80_100[2]; // 1.984
else if (inum < 150) return quantil_120; // 1.980
else if (inum <= 300) return quantiles_150_300[(inum-150) / 50];
else if (inum < 400) return quantiles_150_300[3]; // 1.968
else if (inum < 500) return quantiles_400_500[0]; // 1.966
else if (inum < 700) return quantiles_400_500[1]; // 1.965
else
return quantil_infinity; // 1.96
}
//-------------------------------------------------------------------------
/**
* Returns two sided quantile of Student's distribution with the 99% confidence interval
* @param inum number of observed/measured data in dataset
* @return double - quantile of Student's distribution
*/
static double get99StudentQuantil(int inum) {
if (inum < 1)
throw new IllegalArgumentException("Calling ScStatistics.get99StudentQuantil - number of elements leass than 1");
final double[] quantiles_1_30 = {
63.66, 9.925, 5.841, 4.604, 4.032, 3.707, 3.499, 3.355, 3.250, 3.169,
3.106, 3.055, 3.012, 2.977, 2.947, 2.921, 2.898, 2.878, 2.861, 2.845,
2.831, 2.819, 2.807, 2.797, 2.787, 2.779, 2.771, 2.763, 2.756, 2.750
};
final double[] quantiles_32_50 = { // increment = 2
2.739, 2.728, 2.720, 2.712, 2.705, 2.698, 2.692, 2.687, 2.682, 2.678
};
final double[] quantiles_55_70 = { // increment = 5
2.668, 2.660, 2.654, 2.648
};
final double[] quantiles_80_100 = { // increment = 10
2.639, 2.632, 2.626
};
final double quantil_120 = 2.617;
final double[] quantiles_150_300 = { // increment = 50
2.609, 2.601, 2.596, 2.592
};
final double[] quantiles_400_500 = { // increment = 100
2.588, 2.586
};
final double quantil_infinity = 2.576;
if (inum < 30) return quantiles_1_30[inum-1];
else if (inum < 32) return quantiles_1_30[29]; // 2.042
else if (inum <= 50) return quantiles_32_50[(inum-32) / 2];
else if (inum < 55) return quantiles_32_50[9]; // 2.009
else if (inum <= 70) return quantiles_55_70[(inum-55) / 5];
else if (inum < 80) return quantiles_55_70[3]; // 1.994
else if (inum <= 100) return quantiles_80_100[(inum-80) / 10];
else if (inum < 120) return quantiles_80_100[2]; // 1.984
else if (inum < 150) return quantil_120; // 1.980
else if (inum <= 300) return quantiles_150_300[(inum-150) / 50];
else if (inum < 400) return quantiles_150_300[3]; // 1.968
else if (inum < 500) return quantiles_400_500[0]; // 1.966
else if (inum < 700) return quantiles_400_500[1]; // 1.965
else
return quantil_infinity; // 1.96
}
//-------------------------------------------------------------------------
/**
* Returns two sided quantile of Student's distribution with the 99.9% confidence interval
* @param inum number of observed/measured data in dataset
* @return double - quantile of Student's distribution
*/
static double get999StudentQuantil(int inum) {
if (inum < 1)
throw new IllegalArgumentException("Calling ScStatistics.get999StudentQuantil - number of elements leass than 1");
final double[] quantiles_1_30 = {
636.6, 31.96, 12.92, 8.610, 6.869, 5.959, 5.408, 5.041, 4.718, 4.587,
4.437, 4.318, 4.221, 4.140, 4.073, 4.015, 3.965, 3.922, 3.883, 3.850,
3.819, 3.792, 3.767, 3.745, 3.725, 3.707, 3.690, 3.674, 3.659, 3.646
};
final double[] quantiles_32_50 = { // increment = 2
3.622, 3.601, 3.582, 3.566, 3.551, 3.538, 3.526, 3.515, 3.505, 3.496
};
final double[] quantiles_55_70 = { // increment = 5
3.476, 3.460, 3.447, 3.435
};
final double[] quantiles_80_100 = { // increment = 10
3.416, 3.402, 3.390
};
final double quantil_120 = 3.374;
final double[] quantiles_150_300 = { // increment = 50
3.357, 3.340, 3.330, 3.323
};
final double[] quantiles_400_500 = { // increment = 100
3.315, 3.310
};
final double quantil_infinity = 3.291;
if (inum < 30) return quantiles_1_30[inum-1];
else if (inum < 32) return quantiles_1_30[29]; // 2.042
else if (inum <= 50) return quantiles_32_50[(inum-32) / 2];
else if (inum < 55) return quantiles_32_50[9]; // 2.009
else if (inum <= 70) return quantiles_55_70[(inum-55) / 5];
else if (inum < 80) return quantiles_55_70[3]; // 1.994
else if (inum <= 100) return quantiles_80_100[(inum-80) / 10];
else if (inum < 120) return quantiles_80_100[2]; // 1.984
else if (inum < 150) return quantil_120; // 1.980
else if (inum <= 300) return quantiles_150_300[(inum-150) / 50];
else if (inum < 400) return quantiles_150_300[3]; // 1.968
else if (inum < 500) return quantiles_400_500[0]; // 1.966
else if (inum < 700) return quantiles_400_500[1]; // 1.965
else
return quantil_infinity; // 1.96
}
//-------------------------------------------------------------------------
/**
* This function filters out an array of steady-values intervals (filtered out because of non-homogeneous variance). Equal range variance statistical test.
* @param intervals intervals of steady values (heading or speeds) for analyzing.
* @param values the array of input values (headings or speeds)
* @param steady_stdev interval will not be considered redundant if its standard deviation is within the limits defined
* by this parameter (this parameter is usually the decimal precision of written values)
* @return referent variance and degrees of freedom. (needed in sieve algorithms)
*/
public static Variance isolateNonHomogeneous(List<SCAlgorithms.SpanPair> intervals, double[] values, double steady_stdev) {
Variance retval = new Variance(0, 1, 1.0);
List<Variance> _variances = new ArrayList<>();
for(int ii=0; ii<intervals.size(); ii++) {
int index_start = intervals.get(ii).first;
int index_end = intervals.get(ii).second;
int numelements = index_end - index_start;
double dstdev = SCStatistics.stdev(values, index_start, index_end);
_variances.add(new SCStatistics.Variance(ii, numelements-1, dstdev*dstdev));
}
Collections.sort(_variances, new SortVariances());
// Remove wrong intervals (peaks, holes). They all have non-homogeneous variance.
// Find them...
int f0 = _variances.get(0).f;
double m20 = _variances.get(0).m2;
Variance disp = new Variance(_variances.size(), f0, m20);
List<Integer> for_remove = new ArrayList<>();
for(int ii = 1; ii < _variances.size(); ii++ ) {
Variance disp_i = _variances.get(ii);
double d1 = Math.sqrt(disp_i.m2);
double d2 = 3.0 * steady_stdev; //todo: 0.5 * SCConstants.*_STEADY_RANGE;
if(d1 < d2) {
disp.f = disp_i.f;
disp.m2 = disp_i.m2;
continue;
}
if(disp.m2 < 1e-6) disp.m2 = 3.0 * steady_stdev; // prevent F-test with zero-value and devzero error
double test = disp_i.m2 / disp.m2;
double quantile = get99FQuantile(disp_i.f, disp.f);
if(test <= quantile) {
disp.f = disp_i.f;
disp.m2 = disp_i.m2;
retval.index = disp_i.index;
retval.f = disp_i.f;
retval.m2 = disp_i.m2;
}
else {
// Take the last good variance (the biggist one) as reference variance for sieve algorithm.
Variance ref = _variances.get(ii-1);
retval.index = ref.index;
retval.f = ref.f;
retval.m2 = ref.m2;
for(int jj=ii; jj<_variances.size(); jj++)
for_remove.add(_variances.get(jj).index);
break;
}
}
// ... and remove them
Collections.sort(for_remove);
for(int jj = for_remove.size()-1; jj >= 0; jj--) {
Integer myint = for_remove.get(jj);
intervals.remove(myint.intValue());
}
return retval;
}
//-------------------------------------------------------------------------
/**
* Equal mean values statistical test. Returns true if two mean values
* can be consider stochastically equal (confidence interval - 95%).
* The assumption is that the both datasets are homogenous in the sense of
* the measurement accuracy. (e.g. measured with the same sensor)
* @param dmean1 first mean
* @param dstdev1 standard deviation of measurements (values) in the first dataset
* @param inum1 number of elements in the first dataset
* @param dmean2 second mean
* @param dstdev2 standard deviation of measurements (values) in the second dataset
* @param inum2 number of elements in the second dataset
* @return boolean - true if means can be considered equal (in the stochastic sense)
*/
public static boolean areMeansEqual(double dmean1, double dstdev1, int inum1,
double dmean2, double dstdev2, int inum2) {
final double quantil95 = get95StudentQuantil(Integer.min(inum1, inum2));
double dstdev = Math.sqrt(dstdev1*dstdev1 / inum1 + dstdev2*dstdev2 / inum2);
double ddiff = Math.abs(dmean1 - dmean2);
double dtest_value = ddiff / dstdev;
return dtest_value <= quantil95;
}
//-------------------------------------------------------------------------
/**
* Returns quantile of ranges (normal Gaussian distribution) with the 99% confidence interval
* @param num number of elements
* @return double - the resulting quantile
*/
public static double get99RangeQuantile(int num) {
if (num < 2)
throw new IllegalArgumentException("Calling ScStatistics.get99FQuantil - degrees of freedoms less than 1");
// The table has been created applying the Monte-Carlo method with 1e6 repeats. (The code can be exposed, if needed)
double[] rangeTable = {
// num = 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3.886, 4.346, 4.621, 4.815, 4.965, 5.087, 5.188, 5.277, 5.354, 5.422, 5.484, 5.540, 5.591, 5.638,
// num = 16 17 18 19 20 60 100 200 350 500
5.682, 5.723, 5.761, 5.797, 5.831, 6.509, 6.801, 7.178, 7.470, 7.650
};
// taking from the table directly
if(num <= 20)
return rangeTable[num-2];
// linear interpolation
else if(num <= 60)
return linterpolation(rangeTable[18], rangeTable[19], (num-20), (60-num));
else if(num <= 100)
return linterpolation(rangeTable[19], rangeTable[20], (num-60), (100-num));
else if(num <= 200)
return linterpolation(rangeTable[20], rangeTable[21], (num-100), (200-num));
else if(num <= 350)
return linterpolation(rangeTable[21], rangeTable[22], (num-200), (350-num));
else
return linterpolation(rangeTable[22], rangeTable[23], (num-350), (500-num));
}
//-------------------------------------------------------------------------
/**
* Returns quantile of F-distribution with the 95% confidence interval
* @param f1 numerator degrees of freedom
* @param f2 denominator degrees of freedom
* @return double - quantile of F-distribution
* @see https://en.wikipedia.org/wiki/F-distribution
* @see http://www.socr.ucla.edu/Applets.dir/F_Table.html#FTable0.05
* @todo There are more precise and bigger tables. Implement one of them.
*/
public static double get95FQuantile(int f1, int f2) {
if (f1 < 1 || f2 < 1)
throw new IllegalArgumentException("Calling ScStatistics.get95F_DistributionQuantil - degrees of freedoms less than 1");
double[][] F95table = {
// f1 = 1 2 3 4 5 6 7 8 9 10 12 15 20 24 30 40 60 120 ∞
/*f2=1 */ {161.4476, 199.5000, 215.7073, 224.5832, 230.1619, 233.9860, 236.7684, 238.8827, 240.5433, 241.8817, 243.9060, 245.9499, 248.0131, 249.0518, 250.0951, 251.1432, 252.1957, 253.2529, 254.3144},
/* 2 */ { 18.5128, 19.0000, 19.1643, 19.2468, 19.2964, 19.3295, 19.3532, 19.3710, 19.3848, 19.3959, 19.4125, 19.4291, 19.4458, 19.4541, 19.4624, 19.4707, 19.4791, 19.4874, 19.4957},
/* 3 */ { 10.1280, 9.5521, 9.2766, 9.1172, 9.0135, 8.9406, 8.8867, 8.8452, 8.8123, 8.7855, 8.7446, 8.7029, 8.6602, 8.6385, 8.6166, 8.5944, 8.5720, 8.5494, 8.5264},
/* 4 */ { 7.7086, 6.9443, 6.5914, 6.3882, 6.2561, 6.1631, 6.0942, 6.0410, 5.9988, 5.9644, 5.9117, 5.8578, 5.8025, 5.7744, 5.7459, 5.7170, 5.6877, 5.6581, 5.6281},
/* 5 */ { 6.6079, 5.7861, 5.4095, 5.1922, 5.0503, 4.9503, 4.8759, 4.8183, 4.7725, 4.7351, 4.6777, 4.6188, 4.5581, 4.5272, 4.4957, 4.4638, 4.4314, 4.3985, 4.3650},
/* 6 */ { 5.9874, 5.1433, 4.7571, 4.5337, 4.3874, 4.2839, 4.2067, 4.1468, 4.0990, 4.0600, 3.9999, 3.9381, 3.8742, 3.8415, 3.8082, 3.7743, 3.7398, 3.7047, 3.6689},
/* 7 */ { 5.5914, 4.7374, 4.3468, 4.1203, 3.9715, 3.8660, 3.7870, 3.7257, 3.6767, 3.6365, 3.5747, 3.5107, 3.4445, 3.4105, 3.3758, 3.3404, 3.3043, 3.2674, 3.2298},
/* 8 */ { 5.3177, 4.4590, 4.0662, 3.8379, 3.6875, 3.5806, 3.5005, 3.4381, 3.3881, 3.3472, 3.2839, 3.2184, 3.1503, 3.1152, 3.0794, 3.0428, 3.0053, 2.9669, 2.9276},
/* 9 */ { 5.1174, 4.2565, 3.8625, 3.6331, 3.4817, 3.3738, 3.2927, 3.2296, 3.1789, 3.1373, 3.0729, 3.0061, 2.9365, 2.9005, 2.8637, 2.8259, 2.7872, 2.7475, 2.7067},
/* 10 */ { 4.9646, 4.1028, 3.7083, 3.4780, 3.3258, 3.2172, 3.1355, 3.0717, 3.0204, 2.9782, 2.9130, 2.8450, 2.7740, 2.7372, 2.6996, 2.6609, 2.6211, 2.5801, 2.5379},
/* 11 */ { 4.8443, 3.9823, 3.5874, 3.3567, 3.2039, 3.0946, 3.0123, 2.9480, 2.8962, 2.8536, 2.7876, 2.7186, 2.6464, 2.6090, 2.5705, 2.5309, 2.4901, 2.4480, 2.4045},
/* 12 */ { 4.7472, 3.8853, 3.4903, 3.2592, 3.1059, 2.9961, 2.9134, 2.8486, 2.7964, 2.7534, 2.6866, 2.6169, 2.5436, 2.5055, 2.4663, 2.4259, 2.3842, 2.3410, 2.2962},
/* 13 */ { 4.6672, 3.8056, 3.4105, 3.1791, 3.0254, 2.9153, 2.8321, 2.7669, 2.7144, 2.6710, 2.6037, 2.5331, 2.4589, 2.4202, 2.3803, 2.3392, 2.2966, 2.2524, 2.2064},
/* 14 */ { 4.6001, 3.7389, 3.3439, 3.1122, 2.9582, 2.8477, 2.7642, 2.6987, 2.6458, 2.6022, 2.5342, 2.4630, 2.3879, 2.3487, 2.3082, 2.2664, 2.2229, 2.1778, 2.1307},
/* 15 */ { 4.5431, 3.6823, 3.2874, 3.0556, 2.9013, 2.7905, 2.7066, 2.6408, 2.5876, 2.5437, 2.4753, 2.4034, 2.3275, 2.2878, 2.2468, 2.2043, 2.1601, 2.1141, 2.0658},
/* 16 */ { 4.4940, 3.6337, 3.2389, 3.0069, 2.8524, 2.7413, 2.6572, 2.5911, 2.5377, 2.4935, 2.4247, 2.3522, 2.2756, 2.2354, 2.1938, 2.1507, 2.1058, 2.0589, 2.0096},
/* 17 */ { 4.4513, 3.5915, 3.1968, 2.9647, 2.8100, 2.6987, 2.6143, 2.5480, 2.4943, 2.4499, 2.3807, 2.3077, 2.2304, 2.1898, 2.1477, 2.1040, 2.0584, 2.0107, 1.9604},
/* 18 */ { 4.4139, 3.5546, 3.1599, 2.9277, 2.7729, 2.6613, 2.5767, 2.5102, 2.4563, 2.4117, 2.3421, 2.2686, 2.1906, 2.1497, 2.1071, 2.0629, 2.0166, 1.9681, 1.9168},
/* 19 */ { 4.3807, 3.5219, 3.1274, 2.8951, 2.7401, 2.6283, 2.5435, 2.4768, 2.4227, 2.3779, 2.3080, 2.2341, 2.1555, 2.1141, 2.0712, 2.0264, 1.9795, 1.9302, 1.8780},
/* 20 */ { 4.3512, 3.4928, 3.0984, 2.8661, 2.7109, 2.5990, 2.5140, 2.4471, 2.3928, 2.3479, 2.2776, 2.2033, 2.1242, 2.0825, 2.0391, 1.9938, 1.9464, 1.8963, 1.8432},
/* 21 */ { 4.3248, 3.4668, 3.0725, 2.8401, 2.6848, 2.5727, 2.4876, 2.4205, 2.3660, 2.3210, 2.2504, 2.1757, 2.0960, 2.0540, 2.0102, 1.9645, 1.9165, 1.8657, 1.8117},
/* 22 */ { 4.3009, 3.4434, 3.0491, 2.8167, 2.6613, 2.5491, 2.4638, 2.3965, 2.3419, 2.2967, 2.2258, 2.1508, 2.0707, 2.0283, 1.9842, 1.9380, 1.8894, 1.8380, 1.7831},
/* 23 */ { 4.2793, 3.4221, 3.0280, 2.7955, 2.6400, 2.5277, 2.4422, 2.3748, 2.3201, 2.2747, 2.2036, 2.1282, 2.0476, 2.0050, 1.9605, 1.9139, 1.8648, 1.8128, 1.7570},
/* 24 */ { 4.2597, 3.4028, 3.0088, 2.7763, 2.6207, 2.5082, 2.4226, 2.3551, 2.3002, 2.2547, 2.1834, 2.1077, 2.0267, 1.9838, 1.9390, 1.8920, 1.8424, 1.7896, 1.7330},
/* 25 */ { 4.2417, 3.3852, 2.9912, 2.7587, 2.6030, 2.4904, 2.4047, 2.3371, 2.2821, 2.2365, 2.1649, 2.0889, 2.0075, 1.9643, 1.9192, 1.8718, 1.8217, 1.7684, 1.7110},
/* 26 */ { 4.2252, 3.3690, 2.9752, 2.7426, 2.5868, 2.4741, 2.3883, 2.3205, 2.2655, 2.2197, 2.1479, 2.0716, 1.9898, 1.9464, 1.9010, 1.8533, 1.8027, 1.7488, 1.6906},
/* 27 */ { 4.2100, 3.3541, 2.9604, 2.7278, 2.5719, 2.4591, 2.3732, 2.3053, 2.2501, 2.2043, 2.1323, 2.0558, 1.9736, 1.9299, 1.8842, 1.8361, 1.7851, 1.7306, 1.6717},
/* 28 */ { 4.1960, 3.3404, 2.9467, 2.7141, 2.5581, 2.4453, 2.3593, 2.2913, 2.2360, 2.1900, 2.1179, 2.0411, 1.9586, 1.9147, 1.8687, 1.8203, 1.7689, 1.7138, 1.6541},
/* 29 */ { 4.1830, 3.3277, 2.9340, 2.7014, 2.5454, 2.4324, 2.3463, 2.2783, 2.2229, 2.1768, 2.1045, 2.0275, 1.9446, 1.9005, 1.8543, 1.8055, 1.7537, 1.6981, 1.6376},
/* 30 */ { 4.1709, 3.3158, 2.9223, 2.6896, 2.5336, 2.4205, 2.3343, 2.2662, 2.2107, 2.1646, 2.0921, 2.0148, 1.9317, 1.8874, 1.8409, 1.7918, 1.7396, 1.6835, 1.6223},
/* 40 */ { 4.0847, 3.2317, 2.8387, 2.6060, 2.4495, 2.3359, 2.2490, 2.1802, 2.1240, 2.0772, 2.0035, 1.9245, 1.8389, 1.7929, 1.7444, 1.6928, 1.6373, 1.5766, 1.5089},
/* 60 */ { 4.0012, 3.1504, 2.7581, 2.5252, 2.3683, 2.2541, 2.1665, 2.0970, 2.0401, 1.9926, 1.9174, 1.8364, 1.7480, 1.7001, 1.6491, 1.5943, 1.5343, 1.4673, 1.3893},
/* 120 */ { 3.9201, 3.0718, 2.6802, 2.4472, 2.2899, 2.1750, 2.0868, 2.0164, 1.9588, 1.9105, 1.8337, 1.7505, 1.6587, 1.6084, 1.5543, 1.4952, 1.4290, 1.3519, 1.2539},
/* Inf.*/ { 3.8415, 2.9957, 2.6049, 2.3719, 2.2141, 2.0986, 2.0096, 1.9384, 1.8799, 1.8307, 1.7522, 1.6664, 1.5705, 1.5173, 1.4591, 1.3940, 1.3180, 1.2214, 1.0000} };
// taking from the table directly
if(f2 <= 30 && f1 <= 10) return F95table[f2-1][f1-1];
// linear interpolation (of the table values)
else if(f2 <= 30) {
int f1index = f1 <= 12 ? 10 : f1 <= 15 ? 11 : f1 <= 20 ? 12 : f1 <= 24 ? 13 : f1 <= 30 ? 14 : f1 <= 40 ? 15 : f1 <= 60 ? 16 : f1 <= 120 ? 17 : 18;
double dx1 = f1 <= 12 ? (f1-10) : f1 <= 15 ? (f1-12) : f1 <= 20 ? (f1-15) : f1 <= 24 ? (f1-20) : f1 <= 30 ? (f1-24) : f1 <= 40 ? (f1-30) : f1 <= 60 ? (f1-40) : f1 <= 120 ? (f1-60) : (f1-120);
double dx2 = f1 <= 12 ? (12-f1) : f1 <= 15 ? (15-f1) : f1 <= 20 ? (20-f1) : f1 <= 24 ? (24-f1) : f1 <= 30 ? (30-f1) : f1 <= 40 ? (40-f1) : f1 <= 60 ? (60-f1) : f1 <= 120 ? (120-f1) : (Integer.MAX_VALUE-f1);
double z1 = F95table[f2-1][f1index-1];
double z2 = F95table[f2-1][f1index];
return linterpolation(z1, z2, dx1, dx2);
}
else if(f1 <= 10) {
int f2index = f2 <= 40 ? 30 : f2 <= 60 ? 31 : f2 <= 120 ? 32 : 33;
double dx1 = f2 <= 40 ? (f2-30) : f2 <= 60 ? (f2-40) : f2 <= 120 ? (f2-60) : (f2-120);
double dx2 = f2 <= 40 ? (40-f2) : f2 <= 60 ? (60-f2) : f2 <= 120 ? (120-f2) : (Integer.MAX_VALUE-f2);
double z1 = F95table[f2index-1][f1-1];
double z2 = F95table[f2index][f1-1];
return linterpolation(z1, z2, dx1, dx2);
}
// bilinear interpolation (of the table values)
else {
int f1index = f1 <= 12 ? 10 : f1 <= 15 ? 11 : f1 <= 20 ? 12 : f1 <= 24 ? 13 : f1 <= 30 ? 14 : f1 <= 40 ? 15 : f1 <= 60 ? 16 : f1 <= 120 ? 17 : 18;
int f2index = f2 <= 40 ? 30 : f2 <= 60 ? 31 : f2 <= 120 ? 32 : 33;
double dx1 = f1 <= 12 ? (f1-10) : f1 <= 15 ? (f1-12) : f1 <= 20 ? (f1-15) : f1 <= 24 ? (f1-20) : f1 <= 30 ? (f1-24) : f1 <= 40 ? (f1-30) : f1 <= 60 ? (f1-40) : f1 <= 120 ? (f1-60) : (f1-120);
double dx2 = f1 <= 12 ? (12-f1) : f1 <= 15 ? (15-f1) : f1 <= 20 ? (20-f1) : f1 <= 24 ? (24-f1) : f1 <= 30 ? (30-f1) : f1 <= 40 ? (40-f1) : f1 <= 60 ? (60-f1) : f1 <= 120 ? (120-f1) : (Integer.MAX_VALUE-f1);
double dy1 = f2 <= 40 ? (f2-30) : f2 <= 60 ? (f2-40) : f2 <= 120 ? (f2-60) : (f2-120);
double dy2 = f2 <= 40 ? (40-f2) : f2 <= 60 ? (60-f2) : f2 <= 120 ? (120-f2) : (Integer.MAX_VALUE-f2);
double z11 = F95table[f2index-1][f1index-1];
double z12 = F95table[f2index-1][f1index];
double z21 = F95table[f2index][f1index-1];
double z22 = F95table[f2index][f1index];
return bilinterpolation(z11, z12, z21, z22, dx1, dx2, dy1, dy2);
}
}
//-------------------------------------------------------------------------
/**
* Returns quantile of F-distribution with the 99% confidence interval
* @param f1 numerator degrees of freedom
* @param f2 denominator degrees of freedom
* @return double - quantile of F-distribution
* @see https://en.wikipedia.org/wiki/F-distribution
* @see http://www.socr.ucla.edu/Applets.dir/F_Table.html#FTable0.01
* @todo There are more precise and bigger tables. Implement one of them.
*/
public static double get99FQuantile(int f1, int f2) {
if (f1 < 1 || f2 < 1)
throw new IllegalArgumentException("Calling ScStatistics.get99F_DistributionQuantil - degrees of freedoms less than 1");
double[][] F99table = {
// f1 = 1 2 3 4 5 6 7 8 9 10 12 15 20 24 30 40 60 120 ∞
/*f2=1 */ {4052.181, 4999.500, 5403.352, 5624.583, 5763.650, 5858.986, 5928.356, 5981.070, 6022.473, 6055.847, 6106.321, 6157.285, 6208.730, 6234.631, 6260.649, 6286.782, 6313.030, 6339.391, 6365.864 },
/* 2 */ { 98.503, 99.000, 99.166, 99.249, 99.299, 99.333, 99.356, 99.374, 99.388, 99.399, 99.416, 99.433, 99.449, 99.458, 99.466, 99.474, 99.482, 99.491, 99.499 },
/* 3 */ { 34.116, 30.817, 29.457, 28.710, 28.237, 27.911, 27.672, 27.489, 27.345, 27.229, 27.052, 26.872, 26.690, 26.598, 26.505, 26.411, 26.316, 26.221, 26.125 },
/* 4 */ { 21.198, 18.000, 16.694, 15.977, 15.522, 15.207, 14.976, 14.799, 14.659, 14.546, 14.374, 14.198, 14.020, 13.929, 13.838, 13.745, 13.652, 13.558, 13.463 },
/* 5 */ { 16.258, 13.274, 12.060, 11.392, 10.967, 10.672, 10.456, 10.289, 10.158, 10.051, 9.888, 9.722, 9.553, 9.466, 9.379, 9.291, 9.202, 9.112, 9.020 },
/* 6 */ { 13.745, 10.925, 9.780, 9.148, 8.746, 8.466, 8.260, 8.102, 7.976, 7.874, 7.718, 7.559, 7.396, 7.313, 7.229, 7.143, 7.057, 6.969, 6.880 },
/* 7 */ { 12.246, 9.547, 8.451, 7.847, 7.460, 7.191, 6.993, 6.840, 6.719, 6.620, 6.469, 6.314, 6.155, 6.074, 5.992, 5.908, 5.824, 5.737, 5.650 },
/* 8 */ { 11.259, 8.649, 7.591, 7.006, 6.632, 6.371, 6.178, 6.029, 5.911, 5.814, 5.667, 5.515, 5.359, 5.279, 5.198, 5.116, 5.032, 4.946, 4.859 },
/* 9 */ { 10.561, 8.022, 6.992, 6.422, 6.057, 5.802, 5.613, 5.467, 5.351, 5.257, 5.111, 4.962, 4.808, 4.729, 4.649, 4.567, 4.483, 4.398, 4.311 },
/* 10*/ { 10.044, 7.559, 6.552, 5.994, 5.636, 5.386, 5.200, 5.057, 4.942, 4.849, 4.706, 4.558, 4.405, 4.327, 4.247, 4.165, 4.082, 3.996, 3.909 },
/* 11 */ { 9.646, 7.206, 6.217, 5.668, 5.316, 5.069, 4.886, 4.744, 4.632, 4.539, 4.397, 4.251, 4.099, 4.021, 3.941, 3.860, 3.776, 3.690, 3.602 },
/* 12 */ { 9.330, 6.927, 5.953, 5.412, 5.064, 4.821, 4.640, 4.499, 4.388, 4.296, 4.155, 4.010, 3.858, 3.780, 3.701, 3.619, 3.535, 3.449, 3.361 },
/* 13 */ { 9.074, 6.701, 5.739, 5.205, 4.862, 4.620, 4.441, 4.302, 4.191, 4.100, 3.960, 3.815, 3.665, 3.587, 3.507, 3.425, 3.341, 3.255, 3.165 },
/* 14 */ { 8.862, 6.515, 5.564, 5.035, 4.695, 4.456, 4.278, 4.140, 4.030, 3.939, 3.800, 3.656, 3.505, 3.427, 3.348, 3.266, 3.181, 3.094, 3.004 },
/* 15 */ { 8.683, 6.359, 5.417, 4.893, 4.556, 4.318, 4.142, 4.004, 3.895, 3.805, 3.666, 3.522, 3.372, 3.294, 3.214, 3.132, 3.047, 2.959, 2.868 },
//
/* 16 */ { 8.531, 6.226, 5.292, 4.773, 4.437, 4.202, 4.026, 3.890, 3.780, 3.691, 3.553, 3.409, 3.259, 3.181, 3.101, 3.018, 2.933, 2.845, 2.753 },
/* 17 */ { 8.400, 6.112, 5.185, 4.669, 4.336, 4.102, 3.927, 3.791, 3.682, 3.593, 3.455, 3.312, 3.162, 3.084, 3.003, 2.920, 2.835, 2.746, 2.653 },
/* 18 */ { 8.285, 6.013, 5.092, 4.579, 4.248, 4.015, 3.841, 3.705, 3.597, 3.508, 3.371, 3.227, 3.077, 2.999, 2.919, 2.835, 2.749, 2.660, 2.566 },
/* 19 */ { 8.185, 5.926, 5.010, 4.500, 4.171, 3.939, 3.765, 3.631, 3.523, 3.434, 3.297, 3.153, 3.003, 2.925, 2.844, 2.761, 2.674, 2.584, 2.489 },
/* 20 */ { 8.096, 5.849, 4.938, 4.431, 4.103, 3.871, 3.699, 3.564, 3.457, 3.368, 3.231, 3.088, 2.938, 2.859, 2.778, 2.695, 2.608, 2.517, 2.421 },
/* 21 */ { 8.017, 5.780, 4.874, 4.369, 4.042, 3.812, 3.640, 3.506, 3.398, 3.310, 3.173, 3.030, 2.880, 2.801, 2.720, 2.636, 2.548, 2.457, 2.360 },
/* 22 */ { 7.945, 5.719, 4.817, 4.313, 3.988, 3.758, 3.587, 3.453, 3.346, 3.258, 3.121, 2.978, 2.827, 2.749, 2.667, 2.583, 2.495, 2.403, 2.305 },
/* 23 */ { 7.881, 5.664, 4.765, 4.264, 3.939, 3.710, 3.539, 3.406, 3.299, 3.211, 3.074, 2.931, 2.781, 2.702, 2.620, 2.535, 2.447, 2.354, 2.256 },
/* 24 */ { 7.823, 5.614, 4.718, 4.218, 3.895, 3.667, 3.496, 3.363, 3.256, 3.168, 3.032, 2.889, 2.738, 2.659, 2.577, 2.492, 2.403, 2.310, 2.211 },
/* 25 */ { 7.770, 5.568, 4.675, 4.177, 3.855, 3.627, 3.457, 3.324, 3.217, 3.129, 2.993, 2.850, 2.699, 2.620, 2.538, 2.453, 2.364, 2.270, 2.169 },
/* 26 */ { 7.721, 5.526, 4.637, 4.140, 3.818, 3.591, 3.421, 3.288, 3.182, 3.094, 2.958, 2.815, 2.664, 2.585, 2.503, 2.417, 2.327, 2.233, 2.131 },
/* 27 */ { 7.677, 5.488, 4.601, 4.106, 3.785, 3.558, 3.388, 3.256, 3.149, 3.062, 2.926, 2.783, 2.632, 2.552, 2.470, 2.384, 2.294, 2.198, 2.097 },
/* 28 */ { 7.636, 5.453, 4.568, 4.074, 3.754, 3.528, 3.358, 3.226, 3.120, 3.032, 2.896, 2.753, 2.602, 2.522, 2.440, 2.354, 2.263, 2.167, 2.064 },
/* 29 */ { 7.598, 5.420, 4.538, 4.045, 3.725, 3.499, 3.330, 3.198, 3.092, 3.005, 2.868, 2.726, 2.574, 2.495, 2.412, 2.325, 2.234, 2.138, 2.034 },
/* 30 */ { 7.562, 5.390, 4.510, 4.018, 3.699, 3.473, 3.304, 3.173, 3.067, 2.979, 2.843, 2.700, 2.549, 2.469, 2.386, 2.299, 2.208, 2.111, 2.006 },
/* 40 */ { 7.314, 5.179, 4.313, 3.828, 3.514, 3.291, 3.124, 2.993, 2.888, 2.801, 2.665, 2.522, 2.369, 2.288, 2.203, 2.114, 2.019, 1.917, 1.805 },
/* 60 */ { 7.077, 4.977, 4.126, 3.649, 3.339, 3.119, 2.953, 2.823, 2.718, 2.632, 2.496, 2.352, 2.198, 2.115, 2.028, 1.936, 1.836, 1.726, 1.601 },
/* 120 */ { 6.851, 4.787, 3.949, 3.480, 3.174, 2.956, 2.792, 2.663, 2.559, 2.472, 2.336, 2.192, 2.035, 1.950, 1.860, 1.763, 1.656, 1.533, 1.381 },
/* Inf.*/ { 6.635, 4.605, 3.782, 3.319, 3.017, 2.802, 2.639, 2.511, 2.407, 2.321, 2.185, 2.039, 1.878, 1.791, 1.696, 1.592, 1.473, 1.325, 1.000 }
};
// taking from the table directly
if(f2 <= 30 && f1 <= 10) return F99table[f2-1][f1-1];
// linear interpolation (of the table values)
else if(f2 <= 30) {
int f1index = f1 <= 12 ? 10 : f1 <= 15 ? 11 : f1 <= 20 ? 12 : f1 <= 24 ? 13 : f1 <= 30 ? 14 : f1 <= 40 ? 15 : f1 <= 60 ? 16 : f1 <= 120 ? 17 : 18;
double dx1 = f1 <= 12 ? (f1-10) : f1 <= 15 ? (f1-12) : f1 <= 20 ? (f1-15) : f1 <= 24 ? (f1-20) : f1 <= 30 ? (f1-24) : f1 <= 40 ? (f1-30) : f1 <= 60 ? (f1-40) : f1 <= 120 ? (f1-60) : (f1-120);
double dx2 = f1 <= 12 ? (12-f1) : f1 <= 15 ? (15-f1) : f1 <= 20 ? (20-f1) : f1 <= 24 ? (24-f1) : f1 <= 30 ? (30-f1) : f1 <= 40 ? (40-f1) : f1 <= 60 ? (60-f1) : f1 <= 120 ? (120-f1) : (Integer.MAX_VALUE-f1);
double z1 = F99table[f2-1][f1index-1];
double z2 = F99table[f2-1][f1index];
return linterpolation(z1, z2, dx1, dx2);
}
else if(f1 <= 10) {
int f2index = f2 <= 40 ? 30 : f2 <= 60 ? 31 : f2 <= 120 ? 32 : 33;
double dx1 = f2 <= 40 ? (f2-30) : f2 <= 60 ? (f2-40) : f2 <= 120 ? (f2-60) : (f2-120);
double dx2 = f2 <= 40 ? (40-f2) : f2 <= 60 ? (60-f2) : f2 <= 120 ? (120-f2) : (Integer.MAX_VALUE-f2);
double z1 = F99table[f2index-1][f1-1];
double z2 = F99table[f2index][f1-1];
return linterpolation(z1, z2, dx1, dx2);
}
// bilinear interpolation (of the table values)
else {
int f1index = f1 <= 12 ? 10 : f1 <= 15 ? 11 : f1 <= 20 ? 12 : f1 <= 24 ? 13 : f1 <= 30 ? 14 : f1 <= 40 ? 15 : f1 <= 60 ? 16 : f1 <= 120 ? 17 : 18;
int f2index = f2 <= 40 ? 30 : f2 <= 60 ? 31 : f2 <= 120 ? 32 : 33;
double dx1 = f1 <= 12 ? (f1-10) : f1 <= 15 ? (f1-12) : f1 <= 20 ? (f1-15) : f1 <= 24 ? (f1-20) : f1 <= 30 ? (f1-24) : f1 <= 40 ? (f1-30) : f1 <= 60 ? (f1-40) : f1 <= 120 ? (f1-60) : (f1-120);
double dx2 = f1 <= 12 ? (12-f1) : f1 <= 15 ? (15-f1) : f1 <= 20 ? (20-f1) : f1 <= 24 ? (24-f1) : f1 <= 30 ? (30-f1) : f1 <= 40 ? (40-f1) : f1 <= 60 ? (60-f1) : f1 <= 120 ? (120-f1) : (Integer.MAX_VALUE-f1);
double dy1 = f2 <= 40 ? (f2-30) : f2 <= 60 ? (f2-40) : f2 <= 120 ? (f2-60) : (f2-120);
double dy2 = f2 <= 40 ? (40-f2) : f2 <= 60 ? (60-f2) : f2 <= 120 ? (120-f2) : (Integer.MAX_VALUE-f2);
double z11 = F99table[f2index-1][f1index-1];
double z12 = F99table[f2index-1][f1index];
double z21 = F99table[f2index][f1index-1];
double z22 = F99table[f2index][f1index];
return bilinterpolation(z11, z12, z21, z22, dx1, dx2, dy1, dy2);
}
}
//-------------------------------------------------------------------------
/**
* Returns quantile of F-distribution with the 99.9% confidence interval
* @param f1 numerator degrees of freedom
* @param f2 denominator degrees of freedom
* @return double - quantile of F-distribution
* @see https://en.wikipedia.org/wiki/F-distribution
* @see http://www.socr.ucla.edu/Applets.dir/F_Table.html#FTable0.01
* @todo There are more precise and bigger tables. Implement one of them.
*/
public static double get999FQuantile(int f1, int f2) {
if (f1 < 1 || f2 < 1)
throw new IllegalArgumentException("Calling ScStatistics.get99F_DistributionQuantil - degrees of freedoms less than 1");