-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.ts
146 lines (116 loc) · 3.81 KB
/
Stats.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
export default class Stats {
/**
* Return the sum of an array
* @param arr
*/
static sum = (arr: Array<number>): number => arr.reduce((a, b) => a + b, 0)
/**
* Calculate the mean of an array
* @param arr
*/
static mean = (arr: Array<number>): number => {
let sum = Stats.sum(arr)
let n = arr.length
return sum / n
}
/**
* Calculate the variance of a sample population
*
* s^2 | õ^2 = ∑(x - M)^2
*
* @param arr
*/
static variance = (arr: Array<number>): number => {
let m = Stats.mean(arr)
return Stats.sum(arr.map(i => Math.pow(i - m, 2)))
}
/**
* Calculate the pooled variance of multiple samples
* @param arr
*/
static variance_pooled = (arr: Array<Array<number>>): number => {
let k: number = arr.length // Number of sample groups
var number_of_samples: number = 0
var pooled_variance: number = 0
for(let i = 0; i < arr.length; i++) {
number_of_samples += arr[i].length
pooled_variance += Stats.variance(arr[i])
}
let dof: number = number_of_samples - k // Degrees of freedom
return pooled_variance / dof
}
/**
* Calculate the standard deviation of a population
*
* õ = √(1/N) * s^2
*
* õ = resulting standard deviation
* s^2 = variance of population
* N = number of samples in population
*
* @param arr
*/
static sd_population = (arr: Array<number>): number => {
let N = arr.length
let s = Stats.variance(arr)
return Math.sqrt(s / N)
}
/**
* Calculate pooled standard deviation of multiple samples
* (n1 - 1)s1^2 + (n2 - 1)s2^2 + ... (nk - 1)sk^2
* SD_pooled = √ (----------------------------------------------------)
* n1 + n2 + ... nk - k
*
* n = number of samples within sample group
* s = variance of sample group
* k = number of sample groups
* @param arr | 2D array
*/
static sd_pooled = (arr: Array<Array<number>>): number => {
// Get the number of sample groups
let k: number = arr.length
let var_pooled = 0
let n_pooled = 0
for(let i = 0; i < arr.length; i++) {
let sd: number = Stats.sd_sample(arr[i])
let n: number = arr[i].length
var_pooled += (n - 1) * Math.pow(sd, 2)
n_pooled += n
}
return Math.sqrt(var_pooled / (n_pooled - k))
}
/**
* Calculate the standard deviation of a sample
* @param arr
*/
static sd_sample = (arr: Array<number>): number => {
let n = arr.length - 1 // DoF corrected (Bessel's Correction)
let s = Stats.variance(arr)
return Math.sqrt(s / n)
}
/**
* Calculate the standard error of the mean of a sample
* @param arr
*/
static sem = (arr: Array<number>): number => {
let o: number = Stats.sd_sample(arr) // Standard Deviation
let n: number = arr.length // Number of samples
return o / Math.sqrt(n)
}
/**
* Independent samples ttest. Compares the means of two independent samples to give you a t value.
* @param sample_1
* @param sample_2
* @returns { t, dof }
*/
static ttest = (sample_1: Array<number>, sample_2: Array<number>): { t: number, dof: number } => {
let m1: number = Stats.mean(sample_1)
let m2: number = Stats.mean(sample_2)
let s: number = Stats.variance_pooled([sample_1, sample_2])
let n1: number = sample_1.length
let n2: number = sample_2.length
let t = (m1 - m2) / Math.sqrt((s / n1) + (s / n2))
let dof = n1 + n2 - 2
return { t, dof }
}
}