welch.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. const test = require('tap').test;
  3. const ttest = require('../../hypothesis.js');
  4. const equals = require('../equals.js');
  5. const summary = require('summary');
  6. test('testing not equal alternative', function (t) {
  7. const res = ttest([1, 2, 2, 2, 4], [0, 3, 3, 3, 2], {
  8. mu: 1,
  9. varEqual: false,
  10. alpha: 0.05,
  11. alternative: 'not equal'
  12. });
  13. equals(t, res, {
  14. valid: true,
  15. freedom: 7.769053117782910966582,
  16. pValue: 0.22661539838413605,
  17. testValue: -1.313064328597225660644,
  18. confidence: [
  19. -1.7653291692417925,
  20. 1.7653291692417925
  21. ]
  22. });
  23. t.end();
  24. });
  25. test('testing not equal alternative with summary', function (t) {
  26. const res = ttest(summary([1, 2, 2, 2, 4]), summary([0, 3, 3, 3, 2]), {
  27. mu: 1,
  28. varEqual: false,
  29. alpha: 0.05,
  30. alternative: 'not equal'
  31. });
  32. equals(t, res, {
  33. valid: true,
  34. freedom: 7.769053117782910966582,
  35. pValue: 0.22661539838413605,
  36. testValue: -1.313064328597225660644,
  37. confidence: [
  38. -1.7653291692417925,
  39. 1.7653291692417925
  40. ]
  41. });
  42. t.end();
  43. });
  44. test('testing less alternative', function (t) {
  45. const res = ttest([1, 2, 2, 2, 4], [0, 3, 3, 3, 2], {
  46. mu: 1,
  47. varEqual: false,
  48. alpha: 0.05,
  49. alternative: 'less'
  50. });
  51. equals(t, res, {
  52. valid: true,
  53. freedom: 7.769053117782910966582,
  54. pValue: 0.11330769919206803,
  55. testValue: -1.313064328597225660644,
  56. confidence: [
  57. -Infinity,
  58. 1.4216665293566955
  59. ]
  60. });
  61. t.end();
  62. });
  63. test('testing greater alternative', function (t) {
  64. const res = ttest([1, 2, 2, 2, 4], [0, 3, 3, 3, 2], {
  65. mu: 1,
  66. varEqual: false,
  67. alpha: 0.05,
  68. alternative: 'greater'
  69. });
  70. equals(t, res, {
  71. valid: true,
  72. freedom: 7.769053117782910966582,
  73. pValue: 0.8866923008079319,
  74. testValue: -1.313064328597225660644,
  75. confidence: [
  76. -1.4216665293566955,
  77. Infinity
  78. ]
  79. });
  80. t.end();
  81. });
  82. test('numerical error when p is close to zero', function (t) {
  83. const ttest = require('ttest');
  84. const ref = [
  85. 3.861147687082808,4.1833109086557325,3.973594179324881,
  86. 4.067660887263925,3.7661981850423203,3.8407050592346477,
  87. 3.983494353110395,3.791224198364107,3.7573201433330223,
  88. 3.6326953678695757,3.649353001081396,4.008367559943343,
  89. 3.642362926456249,3.9386159539971635,3.965380930294604,
  90. 3.720419087302582,4.019779785322333,3.916874280250766,
  91. 3.9826331513375517,3.8487184246955795,3.9777597010041306,
  92. 3.9301399596581796,3.3977074280624233,4.016225096648127,
  93. 3.9947182452600187,3.7147293468453944,3.9650116229578796,
  94. 4.15261985914954,4.072283338272716,4.018258042542346,
  95. 4.275900504663435,4.247052603538485,3.8632869646479966,
  96. 4.195181524801175,4.061338223974988,3.997470766890559,
  97. 4.151394128832368,3.956574144537081,4.102028418283781,
  98. 3.895513445852064,4.294429613680861,4.147884159109542,
  99. 4.324981618980995,3.9225663296782742,3.9036535245451747,
  100. 3.996488485132162,4.068017402803237,3.9086757816917657,
  101. 4.1985806684438485,4.215712687842988,4.141082407873602,
  102. 4.130592122446437,3.9844787517550766,4.055413115719177,
  103. 4.295688728970604,3.9295909891273317,4.018853727187908,
  104. 4.211687367710041,4.039532923487119,4.150267003760778,
  105. 3.9571008871840943,4.250207443005561,3.6432440226648395,
  106. 4.0926554219335065,3.9671228831029244,4.050496312050845,
  107. 4.08886200014234,3.9569368332194244,3.973713976092726,
  108. 4.078102228403546,3.8457644781798885,4.063690907132751,
  109. 4.072802425585134
  110. ];
  111. const other = [
  112. 4.196088504937897,4.297600006725063,4.380931627957188,
  113. 4.229470416380346,4.51424161894835,4.15374761736698,
  114. 4.185259220991428,4.1184978606339735,4.242710204234378,
  115. 4.357880244428704,4.2342678907235465,4.417689232509152,
  116. 4.391094295930451,4.270372284867759,4.404107881034074,
  117. 4.088192613114767,4.327923994691726,4.094632609126974,
  118. 4.169154407256733,4.291471956751135,3.9389016863511612,
  119. 4.236946427323764,4.429129606388344,4.362366516924023,
  120. 4.15712520715034,4.273894263027447,4.291340851721514,
  121. 4.156093971770384,4.18322608310349,4.242268731414564,
  122. 4.282951092770966,4.250751025033699,4.1722021750486595,
  123. 4.223035449341226,4.236514858116207,4.370006322215913,
  124. 4.2523040462607495,4.418104161273623,4.005054694044181,
  125. 4.332140449797302,4.114901342239478,4.235374982468168,
  126. 4.189091743846535,4.391354534957378,4.395337620628255,
  127. 4.331974041511989,4.406671154699146,4.399646144051766,
  128. 4.2813460855899175,4.289620122487038,4.390782160631226,
  129. 4.229368785039166,4.006587130212967,4.179324028691534,
  130. 4.126061175939888,4.2893865958789,4.26270828836601,
  131. 4.439895292474625,4.221064697480762,4.1670134964393455,
  132. 4.123864672737084,4.416479313108734,4.253537292991181,
  133. 4.141417410861353,4.239734666096584,4.263623754941469,
  134. 3.9763012135882425,4.251617630059521,4.292616064339593,
  135. 4.498105896751339,4.266088146494694,4.175256450633397,
  136. 4.375893778023997,4.2857149056812665,4.298162810306891,
  137. 4.40570741930156,4.332451872647763
  138. ];
  139. const res = ttest(ref, other, { varEqual: false });
  140. equals(t, res, {
  141. valid: false,
  142. freedom: 124.78698315673383,
  143. pValue: 9.192938947804553e-20,
  144. testValue: -10.865820339325207,
  145. confidence: [
  146. -0.31682232117864595,
  147. -0.21919020661869715
  148. ]
  149. });
  150. t.end();
  151. });