forked from simple-statistics/simple-statistics
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patht_test_two_sample.js
More file actions
66 lines (58 loc) · 2.3 KB
/
Copy patht_test_two_sample.js
File metadata and controls
66 lines (58 loc) · 2.3 KB
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
'use strict';
/* @flow */
var mean = require('./mean');
var sampleVariance = require('./sample_variance');
/**
* This is to compute [two sample t-test](http://en.wikipedia.org/wiki/Student's_t-test).
* Tests whether "mean(X)-mean(Y) = difference", (
* in the most common case, we often have `difference == 0` to test if two samples
* are likely to be taken from populations with the same mean value) with
* no prior knowledge on standard deviations of both samples
* other than the fact that they have the same standard deviation.
*
* Usually the results here are used to look up a
* [p-value](http://en.wikipedia.org/wiki/P-value), which, for
* a certain level of significance, will let you determine that the
* null hypothesis can or cannot be rejected.
*
* `diff` can be omitted if it equals 0.
*
* [This is used to confirm or deny](http://www.monarchlab.org/Lab/Research/Stats/2SampleT.aspx)
* a null hypothesis that the two populations that have been sampled into
* `sampleX` and `sampleY` are equal to each other.
*
* @param {Array<number>} sampleX a sample as an array of numbers
* @param {Array<number>} sampleY a sample as an array of numbers
* @param {number} [difference=0]
* @returns {number} test result
* @example
* ss.tTestTwoSample([1, 2, 3, 4], [3, 4, 5, 6], 0); //= -2.1908902300206643
*/
function tTestTwoSample(
sampleX/*: Array<number> */,
sampleY/*: Array<number> */,
difference/*: number */) {
var n = sampleX.length,
m = sampleY.length;
// If either sample doesn't actually have any values, we can't
// compute this at all, so we return `null`.
if (!n || !m) { return null; }
// default difference (mu) is zero
if (!difference) {
difference = 0;
}
var meanX = mean(sampleX),
meanY = mean(sampleY),
sampleVarianceX = sampleVariance(sampleX),
sampleVarianceY = sampleVariance(sampleY);
if (typeof meanX === 'number' &&
typeof meanY === 'number' &&
typeof sampleVarianceX === 'number' &&
typeof sampleVarianceY === 'number') {
var weightedVariance = ((n - 1) * sampleVarianceX +
(m - 1) * sampleVarianceY) / (n + m - 2);
return (meanX - meanY - difference) /
Math.sqrt(weightedVariance * (1 / n + 1 / m));
}
}
module.exports = tTestTwoSample;