-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.php
1831 lines (1547 loc) · 60.3 KB
/
stat.php
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
<?php
class Kashi
{
protected $values = array();
protected $dataset = array();
protected $precision = 3;
public function __construct()
{
}
public function __destruct()
{
}
public function setPrecision($n)
{
$this->precision = (int) $n;
}
public function getPrecision()
{
return $this->precision;
}
/**
* Compute the arithmetic mean, it is calculated by adding a group of numbers
* and then dividing by the count of those numbers.
* For example, the mean of 2, 3, 3, 5, 7, and 10 is 30 divided by 6, which is 5.
*
* @param array $x List of float values for which you want to calculate the mean.
* @param string $type Mean type [arithmetic|geometric|harmonic], default is arithmetic.
*
* @return float Mean
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Mean
*/
public function mean($x, $type="arithmetic")
{
$type = strtolower($type);
if ($type == "arithmetic") {
$total = 0;
foreach ($x as $value) {
$total += $value;
}
$mean = $total/count($x);
} elseif ($type == "geometric") {
$total = 1;
foreach ($x as $value) {
$total *= $value;
}
$mean = pow($total, 1/count($x));
} elseif ($type == "harmonic") {
$total = 0;
foreach ($x as $value) {
$total += 1/$value;
}
$mean = count($x)/$total;
}
return $mean;
}
/**
* Compute the sample median which is the middle number of a group of numbers; that is,
* half the numbers have values that are greater than the median, and half the numbers
* have values that are less than the median.
* For example, the median of 2, 3, 3, 5, 7, and 10 is 4.
*
* @param array $x List of float values for which you want to calculate the median.
*
* @return float Sample median
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Median
*/
public function median($x)
{
sort($x);
$count = count($x);
if ($count % 2 == 0) {
$median = ($x[($count / 2) - 1] + $x[$count / 2]) / 2;
} else {
$median = $x[floor($count / 2)];
}
return $median;
}
/**
* Compute the mode which is the most frequently occurring number in a group of numbers.
* For example, the mode of 2, 3, 3, 5, 7, and 10 is 3.
*
* @param array $x List of float values for which you want to calculate the mode.
*
* @return array Returns the most frequently occurring or repetitive value
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Mode_(statistics)
*/
public function mode($x)
{
$counter = array();
foreach ($x as $value) {
if (isset($counter[$value])) {
$counter[$value]++;
} else {
$counter[$value] = 1;
}
}
return array_keys($counter, max($counter));
}
/**
* Estimates variance based on a sample, the variance is a measure of how far a set
* of numbers is spread out.
*
* @param array $x List of float values corresponding to a sample of a population.
*
* @return float Returns the sample variance
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Variance
*/
public function variance($x)
{
$mean = $this->mean($x);
$var = 0;
foreach ($x as $value) {
$var += ($value - $mean) * ($value - $mean);
}
$var = $var / (count($x) - 1);
return $var;
}
/**
* Compute the standard deviation based on a sample. The standard deviation is a measure of
* how widely values are dispersed from the average value (the mean).
*
* @param array $x List of float values for which you want to calculate the standard deviation.
*
* @return float Returns the standard deviation
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Standard_deviation
*/
public function sd($x)
{
$sd = sqrt($this->variance($x));
return $sd;
}
/**
* Compute the skewness of a distribution. Skewness characterizes the degree of asymmetry of
* a distribution around its mean. Positive skewness indicates a distribution with an asymmetric
* tail extending toward more positive values. Negative skewness indicates a distribution with
* an asymmetric tail extending toward more negative values.
*
* @param array $x List of float values for which you want to calculate the skewness.
*
* @return float Returns the skewness of a distribution
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Skewness
*/
public function skew($x)
{
$mean = $this->mean($x);
$sd = $this->sd($x);
$n = count($x);
$skew = 0;
foreach ($x as $value) {
$skew += pow(($value - $mean) / $sd, 3);
}
$skew = ($skew * $n) / (($n - 1) * ($n - 2));
return $skew;
}
/**
* Test skewness against 0
*
* @param array $x List of float values for which you want to test the skewness for.
*
* @return boolean Returns if skewness is significant
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Skewness
*/
public function isSkew($x)
{
$n = count($x);
$skew = $this->skew($x);
$skewSE = sqrt(6 / $n);
if (abs($skew) > 2 * $skewSE) {
$result = true;
} else {
$result = false;
}
return $result;
}
/**
* Compute the kurtosis of a distribution. Kurtosis characterizes the relative peakedness or
* flatness of a distribution compared with the normal distribution. Positive kurtosis indicates
* a relatively peaked distribution. Negative kurtosis indicates a relatively flat distribution.
*
* @param array $x List of float values for which you want to calculate the kurtosis.
*
* @return float Returns the kurtosis of a distribution
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Kurtosis
*/
public function kurt($x)
{
$mean = $this->mean($x);
$sd = $this->sd($x);
$n = count($x);
$kurt = 0;
foreach ($x as $value) {
$kurt += pow(($value - $mean) / $sd, 4);
}
$kurt = ($kurt * $n * ($n + 1)) / (($n - 1) * ($n - 2) * ($n - 3));
$kurt = $kurt - ((3 * ($n - 1) * ($n - 1)) / (($n - 2) * ($n - 3)));
return $kurt;
}
/**
* Test kurtosis against 0
*
* @param array $x List of float values for which you want to test the kurtosis for.
*
* @return boolean Returns if kurtosis is significant
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Kurtosis
*/
public function isKurt($x)
{
$n = count($x);
$kurt = $this->kurt($x);
$kurtSE = sqrt(24 / $n);
if (abs($kurt) > 2 * $kurtSE) {
$result = true;
} else {
$result = false;
}
return $result;
}
/**
* Compute the coefficients of variation, it shows the extent of variability in relation
* to mean of the population.
*
* @param array $x List of float values for which you want to calculate the coefficients of variation.
*
* @return float Returns the coefficients of variation
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Coefficient_of_variation
*/
public function cv($x)
{
$mean = $this->mean($x);
$sd = $this->sd($x);
$cv = ($sd / $mean) * 100;
return $cv;
}
/**
* Compute the covariance, the average of the products of deviations for each data point pair.
* Use covariance to determine the relationship between two data sets. For example, you can
* examine whether greater income accompanies greater levels of education.
*
* @param array $x First list of float values
* @param array $y Second list of float values
*
* @return float Returns the covariance
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Covariance
*/
public function cov($x, $y)
{
$meanX = $this->mean($x);
$meanY = $this->mean($y);
$count = count($x);
$total = 0;
for ($i=0; $i<$count; $i++) {
$total += ($x[$i] - $meanX) * ($y[$i] - $meanY);
}
$cov = (1 / ($count - 1)) * $total;
return $cov;
}
/**
* Compute the correlation coefficient. Use the correlation coefficient to determine the
* relationship between two properties. It uses different measures of association, all
* in the range [-1, 1] with 0 indicating no association.
*
* @param array $x First list of float values
* @param array $y Second list of float values
*
* @return float Returns the correlation coefficient
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Correlation
*/
public function cor($x, $y)
{
$cov = $this->cov($x, $y);
$sdX = $this->sd($x);
$sdY = $this->sd($y);
$cor = $cov / ($sdX * $sdY);
return $cor;
}
/**
* Test of the null hypothesis that true correlation is equal to 0
*
* @param float $r Correlation value
* @param integer $n Number of observations
*
* @return float Returns null hypothesis probability: true correlation is equal to 0
* @author Khaled Al-Sham'aa <[email protected]>
*/
public function corTest($r, $n)
{
$t = $r / sqrt((1 - ($r * $r)) / ($n - 2));
$result = $this->tDist($t, $n - 2);
return $result;
}
/**
* Compute the simple linear regression fits a linear model to represent
* the relationship between a response (or y-) variate, and an explanatory
* (or x-) variate.
*
* @param array $y List of float values of the response (or y-) variate.
* @param array $x1 List of float values of the first explanatory (or x1) variate.
* @param array $x2 List of float values of the second explanatory (or x2) variate (default is null).
* @param boolean $origin If TRUE then Intercept value set to 0 (default is FALSE)
*
* @return array Returns [intercept], [slope], [r-square], [adj-r-square] as float
* values in addition to standard error of regression model parameters
* [intercept-se] and [slope-se] as well as confidence intervals
* at level 95% [intercept-2.5%], [intercept-97.5%], [slope-2.5%],
* and [slope-97.5%]
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Regression_analysis
*/
public function lm($y, $x1, $x2=null, $origin=false)
{
if (is_null($x2)) {
$multiple = FALSE;
$k = 1;
} else {
$multiple = TRUE;
$k = 2;
}
$n = count($y);
$mx1 = $this->mean($x1);
$my = $this->mean($y);
if (!$multiple) {
$nominator = 0;
$denominator = 0;
$x1_2 = 0;
$y2 = 0;
$x1y = 0;
for ($i=0; $i<$n; $i++) {
$nominator += ($x1[$i] - $mx1) * ($y[$i] - $my);
$denominator += ($x1[$i] - $mx1) * ($x1[$i] - $mx1);
$x1_2 += $x1[$i] * $x1[$i];
$y2 += $y[$i] * $y[$i];
$x1y += $x1[$i] * $y[$i];
}
if ($origin) {
$b = $x1y / $x1_2;
$a = 0;
$df = $n - 1;
} else {
$b = $nominator / $denominator;
$a = $my - $b * $mx1;
$df = $n - 2;
}
$reg_df = 1;
} else {
$mx2 = $this->mean($x2);
$ysum = array_sum($y);
$x1sum = array_sum($x1);
$x2sum = array_sum($x2);
$x1_2 = 0;
$x2_2 = 0;
$x1x2 = 0;
$x1y = 0;
$x2y = 0;
for ($i=0; $i<$n; $i++) {
$x1_2 += $x1[$i] * $x1[$i];
$x2_2 += $x2[$i] * $x2[$i];
$x1x2 += $x1[$i] * $x2[$i];
$x1y += $x1[$i] * $y[$i];
$x2y += $x2[$i] * $y[$i];
}
$mx1_2 = $x1_2 - ($x1sum * $x1sum / $n);
$mx2_2 = $x2_2 - ($x2sum * $x2sum / $n);
$mx1x2 = $x1x2 - ($x1sum * $x2sum / $n);
$mx1y = $x1y - ($ysum * $x1sum / $n);
$mx2y = $x2y - ($ysum * $x2sum / $n);
$b1n = ($mx2_2 * $mx1y) - ($mx1x2 * $mx2y);
$b1d = ($mx1_2 * $mx2_2) - ($mx1x2 * $mx1x2);
$b1 = $b1n / $b1d;
$b2n = ($mx1_2 * $mx2y) - ($mx1x2 * $mx1y);
$b2d = ($mx1_2 * $mx2_2) - ($mx1x2 * $mx1x2);
$b2 = $b2n / $b2d;
$a = $my - ($b1 * $mx1) - ($b2 * $mx2);
$df = $n - 3;
$reg_df = 2;
}
// Total sum of squares (ss) and Residual sum of squares (rss)
$total_ss = 0;
$regression_ss = 0;
$residual_ss = 0;
for ($i=0; $i<$n; $i++) {
if ($multiple) {
$est = $a + ($b1 * $x1[$i]) + ($b2 * $x2[$i]);
} else {
$est = $a + ($b * $x1[$i]);
}
$total_ss += pow($y[$i] - $my, 2);
$residual_ss += ($y[$i] - $est) * ($y[$i] - $est);
if ($origin) {
$regression_ss += $est * $est;
} else {
$regression_ss += ($est - $my) * ($est - $my);
}
}
// R-square value and Standard error of regression intercept and slope
if (!$multiple) {
if ($origin) {
$r2 = $regression_ss / $y2;
$ase = 0;
$bse = sqrt($residual_ss/$df) / sqrt($x1_2);
} else {
$r2 = 1 - ($residual_ss/$total_ss);
$ase = sqrt($residual_ss/$df) * sqrt($x1_2/($n*$denominator));
$bse = sqrt($residual_ss/$df) / sqrt($denominator);
}
} else {
$r2 = 1 - ($residual_ss/$total_ss);
$ase = 0;
$b1se = 0;
$b2se = 0;
}
// Significance of regression
$regression_ms = $regression_ss / $reg_df;
$residual_ms = $residual_ss / $df;
$regression_f = $regression_ms / $residual_ms;
$regression_p = $this->fDist($regression_f, $reg_df, $df);
// Output
if (!$multiple) {
$result = array('intercept'=>$a, 'slope'=>$b);
} else {
$result = array('intercept'=>$a, 'b1'=>$b1, 'b2'=>$b2);
}
$residual_ms = $residual_ss / ($n - $k - 1);
$total_ms = $total_ss / ($n - 1);
$result['r-square'] = $r2;
$result['adj-r-square'] = 1 - ($residual_ms / $total_ms);
$result['intercept-se'] = $ase;
$result['intercept-2.5%'] = $a - $this->inverseTCDF(0.05, $df) * $ase;
$result['intercept-97.5%'] = $a + $this->inverseTCDF(0.05, $df) * $ase;
if (!$multiple) {
$result['slope-se'] = $bse;
$result['slope-2.5%'] = $b - $this->inverseTCDF(0.05, $df) * $bse;
$result['slope-97.5%'] = $b + $this->inverseTCDF(0.05, $df) * $bse;
} else {
$result['b1-se'] = $b1se;
$result['b1-2.5%'] = $b1 - $this->inverseTCDF(0.05, $df) * $b1se;
$result['b1-97.5%'] = $b1 + $this->inverseTCDF(0.05, $df) * $b1se;
$result['b2-se'] = $b2se;
$result['b2-2.5%'] = $b2 - $this->inverseTCDF(0.05, $df) * $b2se;
$result['b2-97.5%'] = $b2 + $this->inverseTCDF(0.05, $df) * $b2se;
}
$result['F-statistic'] = $regression_f;
$result['p-value'] = $regression_p;
return $result;
}
/**
* Compute the Student's t-Test value to determine whether two samples are likely
* to have come from the same two underlying populations that have the same mean.
*
* @param array $a First list of float values
* @param array $b Second list of float values
* @param boolean $paired Logical indicating whether you want a paired t-test, default
* value is FALSE for unpaired set of data
*
* @return float Returns the associated with a Student's t-Test
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/T-Test
*/
public function tTest ($a, $b, $paired=false)
{
if ($paired == true) {
$count = count($a);
$diff = array();
for ($i=0; $i<$count; $i++) {
$diff[$i] = $a[$i] - $b[$i];
}
$mean = $this->mean($diff);
$var = $this->variance($diff);
$t = $mean / sqrt($var / $count);
} else {
$meanA = $this->mean($a);
$meanB = $this->mean($b);
$varA = $this->variance($a);
$varB = $this->variance($b);
$countA = count($a);
$countB = count($b);
$t = ($meanA - $meanB) / sqrt(($varA / $countA) + ($varB / $countB));
}
return $t;
}
/**
* For use in significance testing, the distribution of the test statistic is approximated as an
* ordinary Student's t distribution with the degrees of freedom calculated using this function.
*
* @param array $a First list of float values
* @param array $b Second list of float values
* @param boolean $eqVar Logical indicates whether two population variances are assumed to be equal
* @param boolean $paired Logical indicates whether you want a paired t-test, default
* value is FALSE for unpaired set of data
*
* @return float Returns the degrees of freedom calculated for Student's t distribution
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/T-Test
*/
public function tTestDf ($a, $b, $eqVar=false, $paired=false)
{
if ($paired == true) {
$df = count($a) - 1;
} elseif ($eqVar == true) {
$df = count($a) + count($b) - 2;
} else {
$varA = $this->variance($a);
$varB = $this->variance($b);
$countA = count($a);
$countB = count($b);
$numerator = pow(($varA / $countA) + ($varB / $countB), 2);
$denominator = (pow($varA / $countA, 2) / ($countA - 1)) + (pow($varB / $countB, 2) / ($countB - 1));
$df = $numerator / $denominator;
}
return $df;
}
/**
* Returns the standard normal cumulative distribution function. The distribution
* has a mean of 0 (zero) and a standard deviation of one. Use this function in
* place of a table of standard normal curve areas.
*
* @param float $x Is the value for which you want the distribution.
* @param float $mean The distribution mean (default is zero).
* @param float $sd The distribution standard deviation (default is one).
*
* @return float the standard normal cumulative distribution function.
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/Normal_distribution
*/
public function norm ($x, $mean=0, $sd=1)
{
$y = (1 / sqrt(2 * pi())) * exp(-0.5 * pow($x, 2));
return $y;
}
private function _zip ($q, $i, $j, $b)
{
$zz = 1;
$z = $zz;
$k = $i;
while ($k <= $j) {
$zz *= $q * $k / ($k - $b);
$z += $zz;
$k += 2;
}
return $z;
}
/**
* Returns the Percentage Points (probability) for the Student t-distribution
* where a numeric value (t) is a calculated value of t for which the Percentage
* Points are to be computed.
*
* @param float $t Is the numeric value at which to evaluate the distribution.
* @param integer $n Is an integer indicating the number of degrees of freedom.
* @param integer $tail Specifies the number of distribution tails to return.
* If tail = 1, TDIST returns the one-tailed distribution.
* If tail = 2, TDIST returns the two-tailed distribution.
*
* @return float the Percentage Points (probability) for the Student t-distribution
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/T-distribution
*/
public function tDist ($t, $n, $tail=1)
{
$pj2 = pi() / 2;
$t = abs($t);
$rt = $t / sqrt($n);
$fk = atan($rt);
if ($n == 1) {
$result = 1 - $fk / $pj2;
} else {
$ek = sin($fk);
$dk = cos($fk);
if (($n % 2) == 1) {
$result = 1 - ($fk + $ek * $dk * $this->_zip($dk * $dk, 2, $n-3, -1)) / $pj2;
} else {
$result = 1 - $ek * $this->_zip($dk * $dk, 1, $n-3, -1);
}
}
return $result / $tail;
}
/**
* Returns the F probability distribution. You can use this function to determine
* whether two data sets have different degrees of diversity.
*
* @param float $f Is the value at which to evaluate the function.
* @param integer $df1 Is the numerator degrees of freedom.
* @param integer $df2 Is the denominator degrees of freedom.
*
* @return float the F probability distribution
* @author Khaled Al-Sham'aa <[email protected]>
* @url http://en.wikipedia.org/wiki/F-distribution
*/
public function fDist ($f, $df1, $df2)
{
$pj2 = pi() / 2;
$x = $df2 / ($df1 * $f + $df2);
if (($df1 % 2) == 0) {
return $this->_zip(1 - $x, $df2, $df1 + $df2 - 4, $df2 - 2) * pow($x, $df2 / 2);
}
if (($df2 % 2) == 0) {
return 1 - $this->_zip($x, $df1, $df1 + $df2 - 4, $df1 - 2) * pow(1 - $x, $df1 / 2);
}
$tan = atan(sqrt($df1 * $f / $df2));
$a = $tan / $pj2;
$sat = sin($tan);
$cot = cos($tan);
if ($df2 > 1) {
$a = $a + $sat * $cot * $this->_zip($cot * $cot, 2, $df2 - 3, -1) / $pj2;
}
if ($df1 == 1) {
return 1 - $a;
}
$c = 4 * $this->_zip($sat * $sat, $df2 + 1, $df1 + $df2 - 4, $df2 - 2) * $sat * pow($cot, $df2) / pi();
if ($df2 == 1) {
return 1 - $a + $c / 2;
}
$k = 2;
while ($k <= ($df2 - 1) / 2) {
$c *= $k / ($k - 0.5);
$k++;
}
return 1 - $a + $c;
}
/**
* Return the probability of normal z value
* Adapted from a polynomial approximation in:
* Ibbetson D, Algorithm 209
* Collected Algorithms of the CACM 1963 p. 616
*
* @param float $z Is the value at which you want to evaluate the probability.
*
* @return float the probability of normal z value
* @author Khaled Al-Sham'aa <[email protected]>
*/
public function normCDF ($z)
{
$max = 6;
if ($z == 0) {
$x = 0;
} else {
$y = abs($z) / 2;
if ($y >= ($max / 2)) {
$x = 1;
} elseif ($y < 1) {
$w = $y * $y;
$x = ((((((((0.000124818987 * $w
- 0.001075204047) * $w + 0.005198775019) * $w
- 0.019198292004) * $w + 0.059054035642) * $w
- 0.151968751364) * $w + 0.319152932694) * $w
- 0.531923007300) * $w + 0.797884560593) * $y * 2;
} else {
$y -= 2;
$x = (((((((((((((-0.000045255659 * $y
+ 0.000152529290) * $y - 0.000019538132) * $y
- 0.000676904986) * $y + 0.001390604284) * $y
- 0.000794620820) * $y - 0.002034254874) * $y
+ 0.006549791214) * $y - 0.010557625006) * $y
+ 0.011630447319) * $y - 0.009279453341) * $y
+ 0.005353579108) * $y - 0.002141268741) * $y
+ 0.000535310849) * $y + 0.999936657524;
}
}
if ($z > 0) {
$result = ($x + 1) / 2;
} else {
$result = (1 - $x) / 2;
}
return $result;
}
/**
* Returns the one-tailed probability of the chi-squared distribution. The chi-squared
* distribution is associated with a chi-squared test. Use the chi-squared test to
* compare observed and expected values.
* Adapted from:
* Hill, I. D. and Pike, M. C. Algorithm 299
* Collected Algorithms for the CACM 1967 p. 243
* Updated for rounding errors based on remark in
* ACM TOMS June 1985, page 185
*
* @param float $x Is the value at which you want to evaluate the distribution.
* @param integer $df Is the number of degrees of freedom.
*
* @return float the probability of the chi-squared distribution
* @author Khaled Al-Sham'aa <[email protected]>
*/
public function chiDist ($x, $df)
{
define(LOG_SQRT_PI, log(sqrt(pi())));
define(I_SQRT_PI, 1 / sqrt(pi()));
if ($x <= 0 || $df < 1) {
$result = 1;
}
$a = $x / 2;
if ($df % 2 == 0) {
$even = true;
} else {
$even = false;
}
if ($df > 1) {
$y = exp(-1 * $a);
}
if ($even) {
$s = $y;
} else {
$s = 2 * $this->normCDF(-1 * sqrt($x));
}
if ($df > 2) {
$x = ($df - 1) / 2;
if ($even) {
$z = 1;
} else {
$z = 0.5;
}
if ($a > 20) {
if ($even) {
$e = 0;
} else {
$e = LOG_SQRT_PI;
}
$c = log($a);
while ($z <= $x) {
$e += log($z);
$s += exp($c * $z - $a - $e);
$z += 1;
}
$result = $s;
} else {
if ($even) {
$e = 1;
} else {
$e = I_SQRT_PI / sqrt($a);
}
$c = 0;
while ($z <= $x) {
$e *= ($a / $z);
$c += $e;
$z += 1;
}
$result = $c * $y + $s;
}
} else {
$result = $s;
}
return $result;
}
/**
* Performs chi-squared contingency table tests and goodness-of-fit tests.
*
* Example:
* <code>
* $table['Automatic'] = array('4 Cylinders' => 3, '6 Cylinders' => 4, '8 Cylinders' => 12);
* $table['Manual'] = array('4 Cylinders' => 8, '6 Cylinders' => 3, '8 Cylinders' => 2);
*
* $results = $stats->chiTest($table);
* </code>
*
* @param array $table Specifies the two-way, n x m table containing the counts
*
* @return array [chi] => the value the chi-squared test statistic
* [df] => the degrees of freedom of the approximate chi-squared distribution of the test statistic.
* [expected] => the expected counts under the null hypothesis.
* @author Khaled Al-Sham'aa <[email protected]>
*/
public function chiTest ($table)
{
$total = 0;
$chi = 0;
foreach ($table as $category => $row) {
foreach ($row as $sample => $cell) {
if (isset($rows[$category])) {
$rows[$category] += $cell;
} else {
$rows[$category] = $cell;
}
if (isset($cols[$sample])) {
$cols[$sample] += $cell;
} else {
$cols[$sample] = $cell;
}
$total += $cell;
}
}
$r = count($rows);
$c = count($cols);
$df = ($r - 1) * ($c - 1);
$expected = array();
foreach ($table as $category => $row) {
foreach ($row as $sample => $cell) {
// fo frequency of the observed value
// fe frequency of the expected value
$fo = $cell;
$fe = ($rows[$category] * $cols[$sample]) / $total;
$chi += pow($fo - $fe, 2) / $fe;
$expected[$category][$sample] = $fe;
}
}
return array('chi'=>$chi, 'df'=>$df, 'expected'=>$expected);
}
/**
* Returns the inverse of the standard normal cumulative distribution.
* The distribution has a mean of zero and a standard deviation of one.
* This is an implementation of the algorithm published at:
* http://home.online.no/~pjacklam/notes/invnorm/
*
* @param float $p Is a probability corresponding to the normal distribution between 0 and 1.
*
* @return float Inverse of the standard normal cumulative distribution, with a probability of $p
* @author Khaled Al-Sham'aa <[email protected]>
*/
public function inverseNormCDF($p)
{
/* coefficients for the rational approximants for the normal probit: */
$a1 = -3.969683028665376e+01;
$a2 = 2.209460984245205e+02;
$a3 = -2.759285104469687e+02;
$a4 = 1.383577518672690e+02;
$a5 = -3.066479806614716e+01;
$a6 = 2.506628277459239e+00;
$b1 = -5.447609879822406e+01;
$b2 = 1.615858368580409e+02;
$b3 = -1.556989798598866e+02;
$b4 = 6.680131188771972e+01;
$b5 = -1.328068155288572e+01;
$c1 = -7.784894002430293e-03;
$c2 = -3.223964580411365e-01;
$c3 = -2.400758277161838e+00;
$c4 = -2.549732539343734e+00;
$c5 = 4.374664141464968e+00;
$c6 = 2.938163982698783e+00;
$d1 = 7.784695709041462e-03;
$d2 = 3.224671290700398e-01;
$d3 = 2.445134137142996e+00;
$d4 = 3.754408661907416e+00;
$p_low = 0.02425;