Skip to content

Commit

Permalink
Use critical values helpers with t-value
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Oct 31, 2021
1 parent 9a88e1d commit 2a0cf49
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/stats/critical_values/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const normalizeKey = function ([key, value]) {

// Retrieve the last entry which can used as `preciseMap`
const isLastPreciseKey = function ([key], index, entries) {
const [nextKey] = entries[index + 1]
return key !== nextKey - 1
const nextEntry = entries[index + 1]
return nextEntry === undefined || key !== nextEntry[0] - 1
}

// Retrieve a critical value given a specific degrees of freedom and
Expand Down
259 changes: 224 additions & 35 deletions src/stats/critical_values/student_t.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,229 @@
// Retrieve t-value, using t-distribution, two-tailed, with 95% significance
// level.
export const getStudentTValue = function (degreesOfFreedom) {
if (degreesOfFreedom >= T_VALUES.length) {
return MIN_T_VALUE
}
/* eslint-disable max-lines */
import { getCriticalValue, getCriticalValuesMap } from './map.js'

return T_VALUES[degreesOfFreedom - 1]
// Retrieve t-value, using t-distribution, two-tailed
export const getStudentTValue = function (degreesOfFreedom) {
const singleSignificanteRate = (1 + SIGNIFICANCE_RATE) / 2
return getCriticalValue(
STUDENT_T_MAP,
degreesOfFreedom,
singleSignificanteRate,
)
}

const MIN_T_VALUE = 1.96
const SIGNIFICANCE_RATE = 0.95

// We hardcode first t-values because computing them is quite complex.
// Beyond a specific size, they converge to `MIN_T_VALUE` with a very minor
// difference.
/* eslint-disable no-magic-numbers */
const T_VALUES = [
12.7065, 4.3026, 3.1824, 2.7764, 2.5706, 2.4469, 2.3646, 2.306, 2.2621,
2.2282, 2.201, 2.1788, 2.1604, 2.1448, 2.1314, 2.1199, 2.1098, 2.1009, 2.093,
2.086, 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.001, 2.0003, 1.9996, 1.999, 1.9983, 1.9977, 1.9971, 1.9966,
1.996, 1.9955, 1.995, 1.9944, 1.9939, 1.9935, 1.993, 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.987, 1.9867, 1.9864, 1.9861, 1.9858, 1.9855, 1.9852,
1.985, 1.9847, 1.9845, 1.9842, 1.984, 1.9837, 1.9835, 1.9833, 1.983, 1.9828,
1.9826, 1.9824, 1.9822, 1.982, 1.9818, 1.9816, 1.9814, 1.9812, 1.981, 1.9808,
1.9806, 1.9805, 1.9803, 1.9801, 1.9799, 1.9798, 1.9796, 1.9794, 1.9793,
1.9791, 1.979, 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.976, 1.9759, 1.9758, 1.9757, 1.9756,
1.9755, 1.9754, 1.9753, 1.9752, 1.9751, 1.975, 1.9749, 1.9748, 1.9747, 1.9746,
1.9745, 1.9744, 1.9744, 1.9743, 1.9742, 1.9741, 1.974, 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.973,
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.972, 1.972, 1.9719,
const STUDENT_T_RAW = [
{
significanceRate: 0.975,
getMaxValue() {
return MAX_T_VALUE
},
criticalValues: {
1: 12.7065,
2: 4.3026,
3: 3.1824,
4: 2.7764,
5: 2.5706,
6: 2.4469,
7: 2.3646,
8: 2.306,
9: 2.2621,
10: 2.2282,
11: 2.201,
12: 2.1788,
13: 2.1604,
14: 2.1448,
15: 2.1314,
16: 2.1199,
17: 2.1098,
18: 2.1009,
19: 2.093,
20: 2.086,
21: 2.0796,
22: 2.0739,
23: 2.0686,
24: 2.0639,
25: 2.0596,
26: 2.0555,
27: 2.0518,
28: 2.0484,
29: 2.0452,
30: 2.0423,
31: 2.0395,
32: 2.0369,
33: 2.0345,
34: 2.0322,
35: 2.0301,
36: 2.0281,
37: 2.0262,
38: 2.0244,
39: 2.0227,
40: 2.0211,
41: 2.0196,
42: 2.0181,
43: 2.0167,
44: 2.0154,
45: 2.0141,
46: 2.0129,
47: 2.0117,
48: 2.0106,
49: 2.0096,
50: 2.0086,
51: 2.0076,
52: 2.0066,
53: 2.0057,
54: 2.0049,
55: 2.0041,
56: 2.0032,
57: 2.0025,
58: 2.0017,
59: 2.001,
60: 2.0003,
61: 1.9996,
62: 1.999,
63: 1.9983,
64: 1.9977,
65: 1.9971,
66: 1.9966,
67: 1.996,
68: 1.9955,
69: 1.995,
70: 1.9944,
71: 1.9939,
72: 1.9935,
73: 1.993,
74: 1.9925,
75: 1.9921,
76: 1.9917,
77: 1.9913,
78: 1.9909,
79: 1.9904,
80: 1.9901,
81: 1.9897,
82: 1.9893,
83: 1.9889,
84: 1.9886,
85: 1.9883,
86: 1.9879,
87: 1.9876,
88: 1.9873,
89: 1.987,
90: 1.9867,
91: 1.9864,
92: 1.9861,
93: 1.9858,
94: 1.9855,
95: 1.9852,
96: 1.985,
97: 1.9847,
98: 1.9845,
99: 1.9842,
100: 1.984,
101: 1.9837,
102: 1.9835,
103: 1.9833,
104: 1.983,
105: 1.9828,
106: 1.9826,
107: 1.9824,
108: 1.9822,
109: 1.982,
110: 1.9818,
111: 1.9816,
112: 1.9814,
113: 1.9812,
114: 1.981,
115: 1.9808,
116: 1.9806,
117: 1.9805,
118: 1.9803,
119: 1.9801,
120: 1.9799,
121: 1.9798,
122: 1.9796,
123: 1.9794,
124: 1.9793,
125: 1.9791,
126: 1.979,
127: 1.9788,
128: 1.9787,
129: 1.9785,
130: 1.9784,
131: 1.9782,
132: 1.9781,
133: 1.9779,
134: 1.9778,
135: 1.9777,
136: 1.9776,
137: 1.9774,
138: 1.9773,
139: 1.9772,
140: 1.9771,
141: 1.9769,
142: 1.9768,
143: 1.9767,
144: 1.9766,
145: 1.9765,
146: 1.9764,
147: 1.9762,
148: 1.9761,
149: 1.976,
150: 1.9759,
151: 1.9758,
152: 1.9757,
153: 1.9756,
154: 1.9755,
155: 1.9754,
156: 1.9753,
157: 1.9752,
158: 1.9751,
159: 1.975,
160: 1.9749,
161: 1.9748,
162: 1.9747,
163: 1.9746,
164: 1.9745,
165: 1.9744,
166: 1.9744,
167: 1.9743,
168: 1.9742,
169: 1.9741,
170: 1.974,
171: 1.9739,
172: 1.9739,
173: 1.9738,
174: 1.9737,
175: 1.9736,
176: 1.9735,
177: 1.9735,
178: 1.9734,
179: 1.9733,
180: 1.9732,
181: 1.9731,
182: 1.9731,
183: 1.973,
184: 1.9729,
185: 1.9729,
186: 1.9728,
187: 1.9727,
188: 1.9727,
189: 1.9726,
190: 1.9725,
191: 1.9725,
192: 1.9724,
193: 1.9723,
194: 1.9723,
195: 1.9722,
196: 1.9721,
197: 1.9721,
198: 1.972,
199: 1.972,
200: 1.9719,
},
},
]
/* eslint-enable no-magic-numbers */

const MAX_T_VALUE = 1.96
const STUDENT_T_MAP = getCriticalValuesMap(STUDENT_T_RAW)
/* eslint-enable max-lines */

0 comments on commit 2a0cf49

Please sign in to comment.