metatests
Version:
Simple to use test engine for Metarhia technology stack
111 lines (105 loc) • 5.23 kB
JavaScript
;
const { iter } = require('@metarhia/common');
const { sum } = require('./utils');
/**
* T-Distribution two-tailed critical values for 95% confidence.
* For more info see http://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm.
*/
// prettier-ignore
const studentsTTable = {
'0.90': [
3.078, 1.886, 1.638, 1.533, 1.476, 1.44, 1.415, 1.397, 1.383, 1.372, 1.363,
1.356, 1.35, 1.345, 1.341, 1.337, 1.333, 1.33, 1.328, 1.325, 1.323, 1.321,
1.319, 1.318, 1.316, 1.315, 1.314, 1.313, 1.311, 1.31, 1.309, 1.309, 1.308,
1.307, 1.306, 1.306, 1.305, 1.304, 1.304, 1.303, 1.303, 1.302, 1.302, 1.301,
1.301, 1.300, 1.300, 1.299, 1.299, 1.299, 1.298, 1.298, 1.298, 1.297, 1.297,
1.297, 1.297, 1.296, 1.296, 1.296, 1.296, 1.295, 1.295, 1.295, 1.295, 1.295,
1.294, 1.294, 1.294, 1.294, 1.294, 1.293, 1.293, 1.293, 1.293, 1.293, 1.293,
1.292, 1.292, 1.292, 1.292, 1.292, 1.292, 1.292, 1.292, 1.291, 1.291, 1.291,
1.291, 1.291, 1.291, 1.291, 1.291, 1.291, 1.291, 1.290, 1.290, 1.290, 1.290,
1.290,
],
'0.95': [
6.314, 2.92, 2.353, 2.132, 2.015, 1.943, 1.895, 1.86, 1.833, 1.812, 1.796,
1.782, 1.771, 1.761, 1.753, 1.746, 1.74, 1.734, 1.729, 1.725, 1.721, 1.717,
1.714, 1.711, 1.708, 1.706, 1.703, 1.701, 1.699, 1.697, 1.696, 1.694, 1.692,
1.691, 1.690, 1.688, 1.687, 1.686, 1.685, 1.684, 1.683, 1.682, 1.681, 1.680,
1.679, 1.679, 1.678, 1.677, 1.677, 1.676, 1.675, 1.675, 1.674, 1.674, 1.673,
1.673, 1.672, 1.672, 1.671, 1.671, 1.670, 1.670, 1.669, 1.669, 1.669, 1.668,
1.668, 1.668, 1.667, 1.667, 1.667, 1.666, 1.666, 1.666, 1.665, 1.665, 1.665,
1.665, 1.664, 1.664, 1.664, 1.664, 1.663, 1.663, 1.663, 1.663, 1.663, 1.662,
1.662, 1.662, 1.662, 1.662, 1.661, 1.661, 1.661, 1.661, 1.661, 1.661, 1.660,
1.660,
],
'0.975': [
12.706, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 2.262, 2.228,
2.201, 2.179, 2.16, 2.145, 2.131, 2.12, 2.11, 2.101, 2.093, 2.086, 2.08,
2.074, 2.069, 2.064, 2.06, 2.056, 2.052, 2.048, 2.045, 2.042, 2.040, 2.037,
2.035, 2.032, 2.030, 2.028, 2.026, 2.024, 2.023, 2.021, 2.020, 2.018, 2.017,
2.015, 2.014, 2.013, 2.012, 2.011, 2.010, 2.009, 2.008, 2.007, 2.006, 2.005,
2.004, 2.003, 2.002, 2.002, 2.001, 2.000, 2.000, 1.999, 1.998, 1.998, 1.997,
1.997, 1.996, 1.995, 1.995, 1.994, 1.994, 1.993, 1.993, 1.993, 1.992, 1.992,
1.991, 1.991, 1.990, 1.990, 1.990, 1.989, 1.989, 1.989, 1.988, 1.988, 1.988,
1.987, 1.987, 1.987, 1.986, 1.986, 1.986, 1.986, 1.985, 1.985, 1.985, 1.984,
1.984, 1.984,
],
'0.99': [
63.657, 9.925, 5.841, 4.604, 4.032, 3.707, 3.499, 3.355, 3.25, 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.75, 2.453, 2.449, 2.445,
2.441, 2.438, 2.434, 2.431, 2.429, 2.426, 2.423, 2.421, 2.418, 2.416, 2.414,
2.412, 2.410, 2.408, 2.407, 2.405, 2.403, 2.402, 2.400, 2.399, 2.397, 2.396,
2.395, 2.394, 2.392, 2.391, 2.390, 2.389, 2.388, 2.387, 2.386, 2.385, 2.384,
2.383, 2.382, 2.382, 2.381, 2.380, 2.379, 2.379, 2.378, 2.377, 2.376, 2.376,
2.375, 2.374, 2.374, 2.373, 2.373, 2.372, 2.372, 2.371, 2.370, 2.370, 2.369,
2.369, 2.368, 2.368, 2.368, 2.367, 2.367, 2.366, 2.366, 2.365, 2.365, 2.365,
2.364,
],
'0.995': [
318.313, 22.327, 10.215, 7.173, 5.893, 5.208, 4.782, 4.499, 4.296, 4.143,
4.024, 3.929, 3.852, 3.787, 3.733, 3.686, 3.646, 3.61, 3.579, 3.552, 3.527,
3.505, 3.485, 3.467, 3.45, 3.435, 3.421, 3.408, 3.396, 3.385, 2.744, 2.738,
2.733, 2.728, 2.724, 2.719, 2.715, 2.712, 2.708, 2.704, 2.701, 2.698, 2.695,
2.692, 2.690, 2.687, 2.685, 2.682, 2.680, 2.678, 2.676, 2.674, 2.672, 2.670,
2.668, 2.667, 2.665, 2.663, 2.662, 2.660, 2.659, 2.657, 2.656, 2.655, 2.654,
2.652, 2.651, 2.650, 2.649, 2.648, 2.647, 2.646, 2.645, 2.644, 2.643, 2.642,
2.641, 2.640, 2.640, 2.639, 2.638, 2.637, 2.636, 2.636, 2.635, 2.634, 2.634,
2.633, 2.632, 2.632, 2.631, 2.630, 2.630, 2.629, 2.629, 2.628, 2.627, 2.627,
2.626, 2.626,
],
};
studentsTTable['0.90'].infinity = 1.282;
studentsTTable['0.95'].infinity = 1.645;
studentsTTable['0.99'].infinity = 1.96;
studentsTTable['0.975'].infinity = 2.326;
studentsTTable['0.995'].infinity = 2.576;
const computeStats = (samples, tTable = studentsTTable['0.975']) => {
const mean = sum(samples) / samples.length;
const variance =
iter(samples)
.map((s) => (s - mean) ** 2)
.apply(sum) /
(samples.length - 1);
const standardDeviation = Math.sqrt(variance);
// Standard error of mean =
// the standard deviation of the sampling distribution of the sample mean.
const standardErrorMean = standardDeviation / Math.sqrt(samples.length);
const degreesOfFreedom = samples.length - 1;
const criticalValue = tTable[degreesOfFreedom] || tTable.infinity;
const marginOfError = standardErrorMean * criticalValue;
const relativeMarginOfError = (marginOfError / mean) * 100 || 0;
return {
mean,
variance,
standardDeviation,
standardErrorMean,
degreesOfFreedom,
criticalValue,
marginOfError,
relativeMarginOfError,
};
};
module.exports = {
studentsTTable,
computeStats,
};