Created
February 6, 2015 19:13
-
-
Save valtih1978/c7e6dbc64226b10fc77c to your computer and use it in GitHub Desktop.
Comparison of ANOVA F-Test with Student's T-Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Rumor says that they must match. They do indeed (I believe less than 1% mistmach is because | |
| // tables are not 100% complete rather than invalid input data) | |
| import math.pow | |
| object student_vs_anova { | |
| def log(msg: String) { | |
| //println(msg) | |
| } //> log: (msg: String)Unit | |
| def fTest(alpha: Double, var1: Double, freedoms1: Int, var2: Double, freedoms2: Int) = { | |
| val (observedF, hiFreedoms, loFreedoms) = if (var1 > var2) (var1/var2, freedoms1, freedoms2) else (var2/var1, freedoms2, freedoms1) | |
| def report(tails: Int): Any = { | |
| val a2 = alpha * tails / 2 | |
| val alternative = (if (tails == 1) "one of the variances is larger" else "variances differ") | |
| try { | |
| val criticalF = fisherTable(a2, hiFreedoms, loFreedoms) | |
| val result = Math.abs(observedF) < criticalF | |
| log("at confidence (1-alpha)=" + (1-alpha) + f", $tails-tailed (alternative H1 = $alternative)" + ", observed:critical F=" + f"$observedF:$criticalF, " | |
| + (if (result) "observed < critical: difference is insignificant" else "observed > critical, difference is significant") | |
| ) | |
| !result | |
| } catch { | |
| case e: Exception => println(tails + f"-tailed F-Test (alpha = $a2) is not available.") ; return null | |
| } | |
| } | |
| (1 to 2) map report | |
| } //> fTest: (alpha: Double, var1: Double, freedoms1: Int, var2: Double, freedoms | |
| //| 2: Int)scala.collection.immutable.IndexedSeq[Any] | |
| def fisherTable(singleSideAlpha: Double, largerVarianceFreedom: Int, lowerVarianceFreedom: Int) = { | |
| // Here are two-sided critical values (vairances are just different). For single-sided | |
| // alphas must be halved. | |
| // first line contains freedoms of larger variance, first column denotes freedoms of smaller variance | |
| // q stands for infinity (Int.MaxValue) | |
| val tables = List(0.1 -> """1 2 3 4 5 7 10 15 20 30 60 120 500 1000 | |
| 1 39.864 49.500 53.593 55.833 57.240 58.906 60.195 61.220 61.740 62.265 62.794 63.061 63.264 63.296 | |
| 2 8.5264 8.9999 9.1618 9.2434 9.2926 9.3491 9.3915 9.4248 9.4413 9.4580 9.4745 9.4829 9.4893 9.4902 | |
| 3 5.5384 5.4624 5.3907 5.3426 5.3092 5.2661 5.2304 5.2003 5.1845 5.1681 5.1513 5.1425 5.1358 5.1347 | |
| 4 4.5448 4.3245 4.1909 4.1073 4.0505 3.9790 3.9198 3.8704 3.8443 3.8175 3.7896 3.7753 3.7643 3.7625 | |
| 5 4.0605 3.7798 3.6194 3.5202 3.4530 3.3679 3.2974 3.2379 3.2067 3.1740 3.1402 3.1228 3.1094 3.1071 | |
| 7 3.5895 3.2575 3.0740 2.9605 2.8833 2.7850 2.7025 2.6322 2.5947 2.5555 2.5142 2.4927 2.4761 2.4735 | |
| 10 3.2850 2.9244 2.7277 2.6054 2.5216 2.4139 2.3226 2.2434 2.2007 2.1554 2.1071 2.0818 2.0618 2.0587 | |
| 15 3.0731 2.6951 2.4898 2.3615 2.2729 2.1582 2.0593 1.9722 1.9243 1.8727 1.8168 1.7867 1.7629 1.7590 | |
| 20 2.9746 2.5893 2.3801 2.2490 2.1582 2.0397 1.9368 1.8450 1.7939 1.7383 1.6768 1.6432 1.6163 1.6118 | |
| 30 2.8808 2.4887 2.2761 2.1423 2.0493 1.9269 1.8195 1.7222 1.6674 1.6064 1.5376 1.4990 1.4669 1.4617 | |
| 60 2.7911 2.3932 2.1774 2.0409 1.9457 1.8194 1.7070 1.6034 1.5435 1.4756 1.3953 1.3476 1.3060 1.2989 | |
| 120 2.7478 2.3473 2.1300 1.9924 1.8959 1.7675 1.6523 1.5450 1.4821 1.4094 1.3203 1.2646 1.2123 1.2026 | |
| 500 2.7157 2.3132 2.0947 1.9561 1.8588 1.7288 1.6115 1.5009 1.4354 1.3583 1.2600 1.1937 1.1215 1.1057 | |
| 1000 2.7106 2.3080 2.0892 1.9505 1.8530 1.7228 1.6051 1.4941 1.4281 1.3501 1.2500 1.1813 1.1031 1.0844 | |
| """, | |
| 0.05 -> """1 2 3 4 5 6 8 12 24 inf | |
| 1 161.4 199.5 215.7 224.6 230.2 234.0 238.9 243.9 249.0 254.3 | |
| 2 18.51 19.00 19.16 19.25 19.30 19.33 19.37 19.41 19.45 19.50 | |
| 3 10.13 9.55 9.28 9.12 9.01 8.94 8.84 8.74 8.64 8.53 | |
| 4 7.71 6.94 6.59 6.39 6.26 6.16 6.04 5.91 5.77 5.63 | |
| 5 6.61 5.79 5.41 5.19 5.05 4.95 4.82 4.68 4.53 4.36 | |
| 6 5.99 5.14 4.76 4.53 4.39 4.28 4.15 4.00 3.84 3.67 | |
| 7 5.59 4.74 4.35 4.12 3.97 3.87 3.73 3.57 3.41 3.23 | |
| 8 5.32 4.46 4.07 3.84 3.69 3.58 3.44 3.28 3.12 2.93 | |
| 9 5.12 4.26 3.86 3.63 3.48 3.37 3.23 3.07 2.90 2.71 | |
| 10 4.96 4.10 3.71 3.48 3.33 3.22 3.07 2.91 2.74 2.54 | |
| 11 4.84 3.98 3.59 3.36 3.20 3.09 2.95 2.79 2.61 2.40 | |
| 12 4.75 3.88 3.49 3.26 3.11 3.00 2.85 2.69 2.50 2.30 | |
| 13 4.67 3.80 3.41 3.18 3.02 2.92 2.77 2.60 2.42 2.21 | |
| 14 4.60 3.74 3.34 3.11 2.96 2.85 2.70 2.53 2.35 2.13 | |
| 15 4.54 3.68 3.29 3.06 2.90 2.79 2.64 2.48 2.29 2.07 | |
| 16 4.49 3.63 3.24 3.01 2.85 2.74 2.59 2.42 2.24 2.01 | |
| 17 4.45 3.59 3.20 2.96 2.81 2.70 2.55 2.38 2.19 1.96 | |
| 18 4.41 3.55 3.16 2.93 2.77 2.66 2.51 2.34 2.15 1.92 | |
| 19 4.38 3.52 3.13 2.90 2.74 2.63 2.48 2.31 2.11 1.88 | |
| 20 4.35 3.49 3.10 2.87 2.71 2.60 2.45 2.28 2.08 1.84 | |
| 21 4.32 3.47 3.07 2.84 2.68 2.57 2.42 2.25 2.05 1.81 | |
| 22 4.30 3.44 3.05 2.82 2.66 2.55 2.40 2.23 2.03 1.78 | |
| 23 4.28 3.42 3.03 2.80 2.64 2.53 2.38 2.20 2.00 1.76 | |
| 24 4.26 3.40 3.01 2.78 2.62 2.51 2.36 2.18 1.98 1.73 | |
| 25 4.24 3.38 2.99 2.76 2.60 2.49 2.34 2.16 1.96 1.71 | |
| 26 4.22 3.37 2.98 2.74 2.59 2.47 2.32 2.15 1.95 1.69 | |
| 27 4.21 3.35 2.96 2.73 2.57 2.46 2.30 2.13 1.93 1.67 | |
| 28 4.20 3.34 2.95 2.71 2.56 2.44 2.29 2.12 1.91 1.65 | |
| 29 4.18 3.33 2.93 2.70 2.54 2.43 2.28 2.10 1.90 1.64 | |
| 30 4.17 3.32 2.92 2.69 2.53 2.42 2.27 2.09 1.89 1.62 | |
| 40 4.08 3.23 2.84 2.61 2.45 2.34 2.18 2.00 1.79 1.51 | |
| 60 4.00 3.15 2.76 2.52 2.37 2.25 2.10 1.92 1.70 1.39 | |
| 120 3.92 3.07 2.68 2.45 2.29 2.17 2.02 1.83 1.61 1.25 | |
| inf 3.84 2.99 2.60 2.37 2.21 2.09 1.94 1.75 1.52 1.00 | |
| """, | |
| 0.01 -> """1 2 3 4 5 7 10 15 20 30 60 120 500 1000 | |
| 1 4052.2 4999.5 5403.4 5624.6 5763.6 5928.4 6055.8 6157.3 6208.7 6260.6 6313.0 6339.4 6359.5 6362.7 | |
| 2 98.503 99.000 99.166 99.249 99.299 99.356 99.399 99.433 99.449 99.466 99.482 99.491 99.497 99.498 | |
| 3 34.116 30.817 29.457 28.710 28.237 27.672 27.229 26.872 26.690 26.504 26.316 26.221 26.148 26.137 | |
| 4 21.198 18.000 16.694 15.977 15.522 14.976 14.546 14.198 14.020 13.838 13.652 13.558 13.486 13.474 | |
| 5 16.258 13.274 12.060 11.392 10.967 10.455 10.051 9.7222 9.5526 9.3793 9.2020 9.1118 9.0424 9.0314 | |
| 7 12.246 9.5467 8.4513 7.8466 7.4605 6.9929 6.6201 6.3143 6.1554 5.9920 5.8236 5.7373 5.6707 5.6601 | |
| 10 10.044 7.5594 6.5523 5.9944 5.6363 5.2001 4.8492 4.5582 4.4055 4.2469 4.0818 3.9964 3.9303 3.9195 | |
| 15 8.6831 6.3588 5.4169 4.8932 4.5557 4.1416 3.8049 3.5223 3.3719 3.2141 3.0471 2.9594 2.8906 2.8796 | |
| 20 8.0960 5.8489 4.9382 4.4306 4.1027 3.6987 3.3682 3.0880 2.9377 2.7785 2.6078 2.5167 2.4446 2.4330 | |
| 30 7.5624 5.3903 4.5098 4.0179 3.6990 3.3046 2.9791 2.7002 2.5486 2.3859 2.2078 2.1108 2.0321 2.0192 | |
| 60 7.0771 4.9774 4.1259 3.6491 3.3388 2.9530 2.6318 2.3522 2.1978 2.0284 1.8362 1.7264 1.6328 1.6169 | |
| 120 6.8509 4.7865 3.9490 3.4795 3.1736 2.7918 2.4720 2.1914 2.0345 1.8600 1.6557 1.5330 1.4215 1.4015 | |
| 500 6.6858 4.6479 3.8210 3.3569 3.0539 2.6751 2.3564 2.0746 1.9152 1.7353 1.5175 1.3774 1.2317 1.2007 | |
| 1000 6.6603 4.6264 3.8012 3.3379 3.0356 2.6571 2.3387 2.0564 1.8967 1.7158 1.4953 1.3513 1.1947 1.1586""" | |
| ).toMap | |
| val lines = tables(singleSideAlpha).stripMargin.split('\n'). map{_.replace("inf", Int.MaxValue.toString).split('\t')} | |
| //println ("freedoms (larger, smaller) = " + f"($largerVarianceFreedom, $lowerVarianceFreedom)") | |
| //println("k1 = " + lines(0).mkString(",")) | |
| val closestCol = lines(0).takeWhile{_.toDouble < largerVarianceFreedom}.length | |
| var closestLine = lines.takeWhile(_(0).toDouble < lowerVarianceFreedom).length | |
| val k1 = lines(0)(closestCol) ; val k2 = lines(closestLine)(0) ; | |
| val fCritical = lines(closestLine)(Math.min(lines(closestLine).length-1, closestCol+1)) | |
| //println("k1/k2 = " + f"$k1/$k2, F_crit = " + fCritical) | |
| log(f"Fisher table ($singleSideAlpha, $largerVarianceFreedom, $lowerVarianceFreedom) = " | |
| + f"Fisher table ($singleSideAlpha, $k1, $k2) = " + fCritical) | |
| fCritical.toDouble // observed > critical: difference is significant | |
| } //> fisherTable: (singleSideAlpha: Double, largerVarianceFreedom: Int, lowerVar | |
| //| ianceFreedom: Int)Double | |
| def ANOVA3(alpha: Double, pairs: List[(Int, Int, Int)]) = { | |
| val l2 = pairs.map {pair => List(pair._1, pair._2, pair._3).map(_.toDouble)} | |
| ANOVA(alpha, l2) | |
| } //> ANOVA3: (alpha: Double, pairs: List[(Int, Int, Int)])scala.collection.immut | |
| //| able.IndexedSeq[Any] | |
| def ANOVAList(alpha: Double, pairs: List[List[Int]]) = { | |
| ANOVA(alpha, pairs.map {_.map(_.toDouble)}) | |
| } //> ANOVAList: (alpha: Double, pairs: List[List[Int]])scala.collection.immutabl | |
| //| e.IndexedSeq[Any] | |
| def ANOVA(alpha: Double, rows: List[List[Double]]) = { | |
| val cols: Int = rows(0).length | |
| def accumulate(rowReduce: List[Double] => Double) = { | |
| rows.foldLeft(0.0) { _ + rowReduce(_)} | |
| } | |
| val avg = accumulate(_.sum)/cols / rows.length | |
| val S_total = accumulate{_.map(e => pow(e - avg,2)).sum } | |
| val groupAvg = (1 to cols) map {group => accumulate(row => row.apply(group-1))/rows.length } | |
| val S_between = (1 to cols).map {group => | |
| val groupAvg = accumulate(row => row.apply(group-1))/rows.length | |
| rows.length * pow(avg - groupAvg, 2) | |
| }.sum | |
| val S_within = S_total - S_between | |
| val betweenFreedoms = cols-1 ; val withinFreedoms = cols*(rows.length-1) | |
| val between = S_between/betweenFreedoms ; val within = S_within/withinFreedoms | |
| log("avg = " + avg + ", sgen = " + S_total + ", group averages = " + groupAvg | |
| + ", S_beteen " + S_between + ", S_within = " + S_within | |
| +", normalized square: beteen " + between + ", within = " + within | |
| ) | |
| fTest(alpha, between, betweenFreedoms, within, withinFreedoms) | |
| } //> ANOVA: (alpha: Double, rows: List[List[Double]])scala.collection.immutable. | |
| //| IndexedSeq[Any] | |
| def ANOVA2(alpha: Double, sample1: List[Double], sample2: List[Double]) = { | |
| ANOVA(alpha, sample1.zip(sample2) map {pair => List(pair._1, pair._2)}) | |
| } //> ANOVA2: (alpha: Double, sample1: List[Double], sample2: List[Double])scala. | |
| //| collection.immutable.IndexedSeq[Any] | |
| def variance(samples: List[Double]): Double = { | |
| var avg = samples.sum / samples.length | |
| samples.foldLeft(0.0) {(acc, x) => acc + pow(x-avg,2)} / (samples.length-1) | |
| } //> variance: (samples: List[Double])Double | |
| def tTest(alpha: Double, averages: List[Double], variances: List[Double], df: Tuple2[Int, Int]) = df match { case(n,m) => | |
| val df = n+m-2 | |
| val tObserved = (averages(0)-averages(1)) * pow(n*m*df/ (variances(0)*(n-1) + variances(1)*(m-1))/(n+m),1.0/2) | |
| log("at confidence (1-alpha)=" + (1-alpha) + ", T observed = " + tObserved) | |
| //println("t-test: averages = " + f"$averages, variances = " + f"$variances, df=$df, fObserved=$tObserved") | |
| var table = List( | |
| // source: https://www.easycalculation.com/statistics/t-distribution-critical-value-table.php | |
| // this is a double-tailed list. For single-tail, use halved alpha | |
| 0.1 -> List(Double.NaN, 6.3138,2.9200,2.3534,2.1319,2.0150,1.9432,1.8946,1.8595,1.8331,1.8124,1.7959,1.7823,1.7709,1.7613,1.7530,1.7459,1.7396,1.7341,1.7291,1.7247,1.7207,1.7172,1.7139,1.7109,1.7081,1.7056,1.7033,1.7011,1.6991,1.6973,1.6955,1.6939,1.6924,1.6909,1.6896,1.6883,1.6871,1.6859,1.6849,1.6839,1.6829,1.6820,1.6811,1.6802,1.6794,1.6787,1.6779,1.6772,1.6766,1.6759,1.6753,1.6747,1.6741,1.6736,1.6730,1.6725,1.6720,1.6715,1.6711,1.6706,1.6702,1.6698,1.6694,1.6690,1.6686,1.6683,1.6679,1.6676,1.6673,1.6669,1.6666,1.6663,1.6660,1.6657,1.6654,1.6652,1.6649,1.6646,1.6644,1.6641,1.6639,1.6636,1.6634,1.6632,1.6630,1.6628,1.6626,1.6623,1.6622,1.6620,1.6618,1.6616,1.6614,1.6612,1.6610,1.6609,1.6607,1.6606,1.6604,1.6602,1.6601,1.6599,1.6598,1.6596,1.6595,1.6593,1.6592,1.6591,1.6589,1.6588,1.6587,1.6586,1.6585,1.6583,1.6582,1.6581,1.6580,1.6579,1.6578,1.6577,1.6575,1.6574,1.6573,1.6572,1.6571,1.6570,1.6570,1.6568,1.6568,1.6567,1.6566,1.6565,1.6564,1.6563,1.6562,1.6561,1.6561,1.6560,1.6559,1.6558,1.6557,1.6557,1.6556,1.6555,1.6554,1.6554,1.6553,1.6552,1.6551,1.6551,1.6550,1.6549,1.6549,1.6548,1.6547,1.6547,1.6546,1.6546,1.6545,1.6544,1.6544,1.6543,1.6543,1.6542,1.6542,1.6541,1.6540,1.6540,1.6539,1.6539,1.6538,1.6537,1.6537,1.6537,1.6536,1.6536,1.6535,1.6535,1.6534,1.6534,1.6533,1.6533,1.6532,1.6532,1.6531,1.6531,1.6531,1.6530,1.6529,1.6529,1.6529,1.6528,1.6528,1.6528,1.6527,1.6527,1.6526,1.6526,1.6525,1.6525), | |
| 0.05 -> List(Double.NaN, 12.7065,4.3026,3.1824,2.7764,2.5706,2.4469,2.3646,2.3060,2.2621,2.2282,2.2010,2.1788,2.1604,2.1448,2.1314,2.1199,2.1098,2.1009,2.0930,2.0860,2.0796,2.0739,2.0686,2.0639,2.0596,2.0555,2.0518,2.0484,2.0452,2.0423,2.0395,2.0369,2.0345,2.0322,2.0301,2.0281,2.0262,2.0244,2.0227,2.0211,2.0196,2.0181,2.0167,2.0154,2.0141,2.0129,2.0117,2.0106,2.0096,2.0086,2.0076,2.0066,2.0057,2.0049,2.0041,2.0032,2.0025,2.0017,2.0010,2.0003,1.9996,1.9990,1.9983,1.9977,1.9971,1.9966,1.9960,1.9955,1.9950,1.9944,1.9939,1.9935,1.9930,1.9925,1.9921,1.9917,1.9913,1.9909,1.9904,1.9901,1.9897,1.9893,1.9889,1.9886,1.9883,1.9879,1.9876,1.9873,1.9870,1.9867,1.9864,1.9861,1.9858,1.9855,1.9852,1.9850,1.9847,1.9845,1.9842,1.9840,1.9837,1.9835,1.9833,1.9830,1.9828,1.9826,1.9824,1.9822,1.9820,1.9818,1.9816,1.9814,1.9812,1.9810,1.9808,1.9806,1.9805,1.9803,1.9801,1.9799,1.9798,1.9796,1.9794,1.9793,1.9791,1.9790,1.9788,1.9787,1.9785,1.9784,1.9782,1.9781,1.9779,1.9778,1.9777,1.9776,1.9774,1.9773,1.9772,1.9771,1.9769,1.9768,1.9767,1.9766,1.9765,1.9764,1.9762,1.9761,1.9760,1.9759,1.9758,1.9757,1.9756,1.9755,1.9754,1.9753,1.9752,1.9751,1.9750,1.9749,1.9748,1.9747,1.9746,1.9745,1.9744,1.9744,1.9743,1.9742,1.9741,1.9740,1.9739,1.9739,1.9738,1.9737,1.9736,1.9735,1.9735,1.9734,1.9733,1.9732,1.9731,1.9731,1.9730,1.9729,1.9729,1.9728,1.9727,1.9727,1.9726,1.9725,1.9725,1.9724,1.9723,1.9723,1.9722,1.9721,1.9721,1.9720,1.9720,1.9719), | |
| 0.02 -> List(Double.NaN, 31.819,6.9646,4.5407,3.7470,3.3650,3.1426,2.9980,2.8965,2.8214,2.7638,2.7181,2.6810,2.6503,2.6245,2.6025,2.5835,2.5669,2.5524,2.5395,2.5280,2.5176,2.5083,2.4998,2.4922,2.4851,2.4786,2.4727,2.4671,2.4620,2.4572,2.4528,2.4487,2.4448,2.4411,2.4377,2.4345,2.4315,2.4286,2.4258,2.4233,2.4208,2.4185,2.4162,2.4142,2.4121,2.4102,2.4083,2.4066,2.4049,2.4033,2.4017,2.4002,2.3988,2.3974,2.3961,2.3948,2.3936,2.3924,2.3912,2.3901,2.3890,2.3880,2.3870,2.3860,2.3851,2.3842,2.3833,2.3824,2.3816,2.3808,2.3800,2.3793,2.3785,2.3778,2.3771,2.3764,2.3758,2.3751,2.3745,2.3739,2.3733,2.3727,2.3721,2.3716,2.3710,2.3705,2.3700,2.3695,2.3690,2.3685,2.3680,2.3676,2.3671,2.3667,2.3662,2.3658,2.3654,2.3650,2.3646,2.3642,2.3638,2.3635,2.3631,2.3627,2.3624,2.3620,2.3617,2.3614,2.3611,2.3607,2.3604,2.3601,2.3598,2.3595,2.3592,2.3589,2.3586,2.3583,2.3581,2.3578,2.3576,2.3573,2.3571,2.3568,2.3565,2.3563,2.3561,2.3559,2.3556,2.3554,2.3552,2.3549,2.3547,2.3545,2.3543,2.3541,2.3539,2.3537,2.3535,2.3533,2.3531,2.3529,2.3527,2.3525,2.3523,2.3522,2.3520,2.3518,2.3516,2.3515,2.3513,2.3511,2.3510,2.3508,2.3507,2.3505,2.3503,2.3502,2.3500,2.3499,2.3497,2.3496,2.3495,2.3493,2.3492,2.3490,2.3489,2.3487,2.3486,2.3485,2.3484,2.3482,2.3481,2.3480,2.3478,2.3477,2.3476,2.3475,2.3474,2.3472,2.3471,2.3470,2.3469,2.3468,2.3467,2.3466,2.3465,2.3463,2.3463,2.3461,2.3460,2.3459,2.3458,2.3457,2.3456,2.3455,2.3454,2.3453,2.3452,2.3451), | |
| 0.01 -> List(Double.NaN, 63.655,9.9247,5.8408,4.6041,4.0322,3.7074,3.4995,3.3554,3.2498,3.1693,3.1058,3.0545,3.0123,2.9768,2.9467,2.9208,2.8983,2.8784,2.8609,2.8454,2.8314,2.8188,2.8073,2.7970,2.7874,2.7787,2.7707,2.7633,2.7564,2.7500,2.7440,2.7385,2.7333,2.7284,2.7238,2.7195,2.7154,2.7115,2.7079,2.7045,2.7012,2.6981,2.6951,2.6923,2.6896,2.6870,2.6846,2.6822,2.6800,2.6778,2.6757,2.6737,2.6718,2.6700,2.6682,2.6665,2.6649,2.6633,2.6618,2.6603,2.6589,2.6575,2.6561,2.6549,2.6536,2.6524,2.6512,2.6501,2.6490,2.6479,2.6468,2.6459,2.6449,2.6439,2.6430,2.6421,2.6412,2.6404,2.6395,2.6387,2.6379,2.6371,2.6364,2.6356,2.6349,2.6342,2.6335,2.6328,2.6322,2.6316,2.6309,2.6303,2.6297,2.6292,2.6286,2.6280,2.6275,2.6269,2.6264,2.6259,2.6254,2.6249,2.6244,2.6240,2.6235,2.6230,2.6225,2.6221,2.6217,2.6212,2.6208,2.6204,2.6200,2.6196,2.6192,2.6189,2.6185,2.6181,2.6178,2.6174,2.6171,2.6168,2.6164,2.6161,2.6158,2.6154,2.6151,2.6148,2.6145,2.6142,2.6139,2.6136,2.6133,2.6130,2.6127,2.6125,2.6122,2.6119,2.6117,2.6114,2.6112,2.6109,2.6106,2.6104,2.6102,2.6099,2.6097,2.6094,2.6092,2.6090,2.6088,2.6085,2.6083,2.6081,2.6079,2.6077,2.6075,2.6073,2.6071,2.6069,2.6067,2.6065,2.6063,2.6062,2.6060,2.6058,2.6056,2.6054,2.6052,2.6051,2.6049,2.6047,2.6046,2.6044,2.6042,2.6041,2.6039,2.6037,2.6036,2.6034,2.6033,2.6031,2.6030,2.6028,2.6027,2.6025,2.6024,2.6022,2.6021,2.6019,2.6018,2.6017,2.6015,2.6014,2.6013,2.6012,2.6010,2.6009,2.6008,2.6007), | |
| 0.005 -> List(Double.NaN, 127.34,14.088,7.4534,5.5976,4.7734,4.3168,4.0294,3.8325,3.6896,3.5814,3.4966,3.4284,3.3725,3.3257,3.2860,3.2520,3.2224,3.1966,3.1737,3.1534,3.1352,3.1188,3.1040,3.0905,3.0782,3.0669,3.0565,3.0469,3.0380,3.0298,3.0221,3.0150,3.0082,3.0019,2.9961,2.9905,2.9853,2.9803,2.9756,2.9712,2.9670,2.9630,2.9591,2.9555,2.9521,2.9488,2.9456,2.9426,2.9397,2.9370,2.9343,2.9318,2.9293,2.9270,2.9247,2.9225,2.9204,2.9184,2.9164,2.9146,2.9127,2.9110,2.9092,2.9076,2.9060,2.9045,2.9030,2.9015,2.9001,2.8987,2.8974,2.8961,2.8948,2.8936,2.8925,2.8913,2.8902,2.8891,2.8880,2.8870,2.8859,2.8850,2.8840,2.8831,2.8821,2.8813,2.8804,2.8795,2.8787,2.8779,2.8771,2.8763,2.8755,2.8748,2.8741,2.8734,2.8727,2.8720,2.8713,2.8706,2.8700,2.8694,2.8687,2.8682,2.8675,2.8670,2.8664,2.8658,2.8653,2.8647,2.8642,2.8637,2.8632,2.8627,2.8622,2.8617,2.8612,2.8608,2.8603,2.8599,2.8594,2.8590,2.8585,2.8582,2.8577,2.8573,2.8569,2.8565,2.8561,2.8557,2.8554,2.8550,2.8546,2.8542,2.8539,2.8536,2.8532,2.8529,2.8525,2.8522,2.8519,2.8516,2.8512,2.8510,2.8506,2.8503,2.8500,2.8497,2.8494,2.8491,2.8489,2.8486,2.8483,2.8481,2.8478,2.8475,2.8472,2.8470,2.8467,2.8465,2.8463,2.8460,2.8458,2.8455,2.8452,2.8450,2.8448,2.8446,2.8443,2.8441,2.8439,2.8437,2.8435,2.8433,2.8430,2.8429,2.8427,2.8424,2.8423,2.8420,2.8419,2.8416,2.8415,2.8413,2.8411,2.8409,2.8407,2.8406,2.8403,2.8402,2.8400,2.8398,2.8397,2.8395,2.8393,2.8392,2.8390,2.8388,2.8387,2.8385), | |
| 0.002 -> List(Double.NaN, 318.49,22.327,10.214,7.1732,5.8934,5.2076,4.7852,4.5008,4.2969,4.1437,4.0247,3.9296,3.8520,3.7874,3.7328,3.6861,3.6458,3.6105,3.5794,3.5518,3.5272,3.5050,3.4850,3.4668,3.4502,3.4350,3.4211,3.4082,3.3962,3.3852,3.3749,3.3653,3.3563,3.3479,3.3400,3.3326,3.3256,3.3190,3.3128,3.3069,3.3013,3.2959,3.2909,3.2861,3.2815,3.2771,3.2729,3.2689,3.2651,3.2614,3.2579,3.2545,3.2513,3.2482,3.2451,3.2423,3.2394,3.2368,3.2342,3.2317,3.2293,3.2269,3.2247,3.2225,3.2204,3.2184,3.2164,3.2144,3.2126,3.2108,3.2090,3.2073,3.2056,3.2040,3.2025,3.2010,3.1995,3.1980,3.1966,3.1953,3.1939,3.1926,3.1913,3.1901,3.1889,3.1877,3.1866,3.1854,3.1844,3.1833,3.1822,3.1812,3.1802,3.1792,3.1782,3.1773,3.1764,3.1755,3.1746,3.1738,3.1729,3.1720,3.1712,3.1704,3.1697,3.1689,3.1681,3.1674,3.1667,3.1660,3.1653,3.1646,3.1640,3.1633,3.1626,3.1620,3.1614,3.1607,3.1601,3.1595,3.1589,3.1584,3.1578,3.1573,3.1567,3.1562,3.1556,3.1551,3.1546,3.1541,3.1536,3.1531,3.1526,3.1522,3.1517,3.1512,3.1508,3.1503,3.1499,3.1495,3.1491,3.1486,3.1482,3.1478,3.1474,3.1470,3.1466,3.1462,3.1458,3.1455,3.1451,3.1447,3.1443,3.1440,3.1436,3.1433,3.1430,3.1426,3.1423,3.1419,3.1417,3.1413,3.1410,3.1407,3.1403,3.1400,3.1398,3.1394,3.1392,3.1388,3.1386,3.1383,3.1380,3.1377,3.1375,3.1372,3.1369,3.1366,3.1364,3.1361,3.1358,3.1356,3.1354,3.1351,3.1349,3.1346,3.1344,3.1341,3.1339,3.1337,3.1334,3.1332,3.1330,3.1328,3.1326,3.1323,3.1321,3.1319,3.1317,3.1315), | |
| 0.001 -> List(Double.NaN, 636.04,31.598,12.924,8.6103,6.8688,5.9589,5.4079,5.0414,4.7809,4.5869,4.4369,4.3178,4.2208,4.1404,4.0728,4.0150,3.9651,3.9216,3.8834,3.8495,3.8193,3.7921,3.7676,3.7454,3.7251,3.7067,3.6896,3.6739,3.6594,3.6459,3.6334,3.6218,3.6109,3.6008,3.5912,3.5822,3.5737,3.5657,3.5581,3.5510,3.5442,3.5378,3.5316,3.5258,3.5202,3.5149,3.5099,3.5051,3.5004,3.4960,3.4917,3.4877,3.4838,3.4800,3.4764,3.4730,3.4696,3.4663,3.4632,3.4602,3.4573,3.4545,3.4518,3.4491,3.4466,3.4441,3.4417,3.4395,3.4372,3.4350,3.4329,3.4308,3.4288,3.4269,3.4250,3.4232,3.4214,3.4197,3.4180,3.4164,3.4147,3.4132,3.4117,3.4101,3.4087,3.4073,3.4059,3.4046,3.4032,3.4020,3.4006,3.3995,3.3982,3.3970,3.3959,3.3947,3.3936,3.3926,3.3915,3.3905,3.3894,3.3885,3.3875,3.3866,3.3856,3.3847,3.3838,3.3829,3.3820,3.3812,3.3803,3.3795,3.3787,3.3779,3.3771,3.3764,3.3756,3.3749,3.3741,3.3735,3.3727,3.3721,3.3714,3.3707,3.3700,3.3694,3.3688,3.3682,3.3676,3.3669,3.3663,3.3658,3.3652,3.3646,3.3641,3.3635,3.3630,3.3624,3.3619,3.3614,3.3609,3.3604,3.3599,3.3594,3.3589,3.3584,3.3579,3.3575,3.3570,3.3565,3.3561,3.3557,3.3552,3.3548,3.3544,3.3540,3.3536,3.3531,3.3528,3.3523,3.3520,3.3516,3.3512,3.3508,3.3505,3.3501,3.3497,3.3494,3.3490,3.3487,3.3483,3.3480,3.3477,3.3473,3.3470,3.3466,3.3464,3.3460,3.3457,3.3454,3.3451,3.3448,3.3445,3.3442,3.3439,3.3436,3.3433,3.3430,3.3428,3.3425,3.3422,3.3419,3.3417,3.3414,3.3411,3.3409,3.3406,3.3403,3.3401,3.3398) | |
| ) toMap | |
| def report(tails: Int): Any = { | |
| val a2 = alpha * tails / 2 | |
| if (!table.contains(a2)) {println(tails + "-tailed T-Test is not available"); return null} | |
| val tCrit = table(a2)(df) | |
| val alternative = if (tails == 1) "larger mean is larger" else "a_mean != b_mean" | |
| val result = Math.abs(tObserved) < tCrit | |
| log(f"$tails-tailed (alternative H1 = $alternative), " | |
| + (if (result) f"observed < critical $tCrit : difference is insignificant" else f"observed > critical $tCrit : difference is significant") | |
| ) | |
| return !result | |
| } | |
| (1 to 2) map report | |
| } //> tTest: (alpha: Double, averages: List[Double], variances: List[Double], df | |
| //| : (Int, Int))scala.collection.immutable.IndexedSeq[Any] | |
| //tTest(0.1, List(31.2, 29.2), List(0.84, 0.40), (12, 18)) | |
| /*Student's test*/ | |
| def student(alpha: Double, sample1: List[Double], sample2: List[Double]) = { | |
| val samples = List(sample1, sample2) | |
| def accumulate(sample: List[Double], func: Double => Double) = { | |
| sample.foldLeft(0.0){_ + func(_)} | |
| } | |
| def avg(sample: List[Double]) = accumulate(sample, (x: Double) => x) / sample.length | |
| val averages = samples map avg | |
| def variances = List(0, 1) map {i => | |
| accumulate(samples(i), (x: Double) => pow(x - averages(i), 2))/ (samples(i).length-1)} // no need to divide because we need to muliply in the factor | |
| val (n,m) = (samples(0).length, samples(1).length) | |
| tTest(alpha, averages, variances, (n,m)) | |
| } //> student: (alpha: Double, sample1: List[Double], sample2: List[Double])scal | |
| //| a.collection.immutable.IndexedSeq[Any] | |
| def fTestSamples(alpha: Double, list1: List[Double], list2: List[Double]) = { | |
| //val f = fTest(alpha, variance(list1), list1.length-1, variance(list2), list2.length-1) | |
| val f = ANOVA2(alpha, list1, list2) | |
| val t = student(alpha, list1, list2) | |
| val res = List(f, t) map {_.toList} | |
| res | |
| } //> fTestSamples: (alpha: Double, list1: List[Double], list2: List[Double])Lis | |
| //| t[List[Any]] | |
| //fTest(0.05, 0.84, 13, 2.52, 9) | |
| //student(0.1, List(3.4, 3.4, 3.5, 3.5, 3.5, 3.7, 3.7, 3.7, 3.7, 3.9), List(3.2, 3.2, 3.4, 3.4, 3.6, 3.6, 3.6, 3.6, 3.6, 3.6, 3.6, 3.6)) | |
| fTestSamples(0.1, List(9.6, 10, 9.8, 10.2, 10.6), List(10.4, 9.7, 10, 10.3)) | |
| //> res0: List[List[Any]] = List(List(false, false), List(false, false)) | |
| ANOVA3(0.05, List((38,20,21), (36,24,22), (35,26,31), (31,30,34))) | |
| //> 1-tailed F-Test (alpha = 0.025) is not available. | |
| //| res1: scala.collection.immutable.IndexedSeq[Any] = Vector(null, true) | |
| val N = 2000 //> N : Int = 2000 | |
| val failures = (1 to N) map {_ => | |
| def gen() = { | |
| val avg = Math.random() * 100 | |
| val list = List.tabulate(50){_ => Math.random()*100 + avg} map {_.toInt} | |
| (avg, list) | |
| } | |
| val s1 = gen() ; val s2 = gen() | |
| val result = fTestSamples(0.1, s1._2 map {_.toDouble}, s2._2 map {_.toDouble}) | |
| //(s1._1 + ", " + s2._1 + ": " + result + | |
| //(if (result(0) != result(1)) s1._1 + ", " + s2._1 + ": " + result + s1._2 + ", " + s2._2 else "")} filter(_.length > 0 ) mkString("\n") | |
| result} filter (r => r(0) != r(1))//> failures : scala.collection.immutable.IndexedSeq[List[List[Any]]] = Vecto | |
| //| r(List(List(false, true), List(false, false)), List(List(false, true), Lis | |
| //| t(false, false)), List(List(false, true), List(false, false)), List(List(f | |
| //| alse, true), List(false, false)), List(List(true, true), List(false, true) | |
| //| ), List(List(true, true), List(false, true)), List(List(false, true), List | |
| //| (false, false)), List(List(false, true), List(false, false)), List(List(fa | |
| //| lse, true), List(false, false))) | |
| println ("t-test mismatches f-test with as " + failures.length + " / "+ N + " = " + (failures.length.toDouble/N*100) + '%') | |
| //> t-test mismatches f-test with as 9 / 2000 = 0.44999999999999996% | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment