-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.js
212 lines (191 loc) · 4.81 KB
/
stat.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { newArray } from './utilMatrix.js';
export function sumByRow(matrix) {
let sum = newArray(matrix.rows);
for (let i = 0; i < matrix.rows; ++i) {
for (let j = 0; j < matrix.columns; ++j) {
sum[i] += matrix.get(i, j);
}
}
return sum;
}
export function sumByColumn(matrix) {
let sum = newArray(matrix.columns);
for (let i = 0; i < matrix.rows; ++i) {
for (let j = 0; j < matrix.columns; ++j) {
sum[j] += matrix.get(i, j);
}
}
return sum;
}
export function sumAll(matrix) {
let v = 0;
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
v += matrix.get(i, j);
}
}
return v;
}
export function productByRow(matrix) {
let sum = newArray(matrix.rows, 1);
for (let i = 0; i < matrix.rows; ++i) {
for (let j = 0; j < matrix.columns; ++j) {
sum[i] *= matrix.get(i, j);
}
}
return sum;
}
export function productByColumn(matrix) {
let sum = newArray(matrix.columns, 1);
for (let i = 0; i < matrix.rows; ++i) {
for (let j = 0; j < matrix.columns; ++j) {
sum[j] *= matrix.get(i, j);
}
}
return sum;
}
export function productAll(matrix) {
let v = 1;
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
v *= matrix.get(i, j);
}
}
return v;
}
export function varianceByRow(matrix, unbiased, mean) {
const rows = matrix.rows;
const cols = matrix.columns;
const variance = [];
for (let i = 0; i < rows; i++) {
let sum1 = 0;
let sum2 = 0;
let x = 0;
for (let j = 0; j < cols; j++) {
x = matrix.get(i, j) - mean[i];
sum1 += x;
sum2 += x * x;
}
if (unbiased) {
variance.push((sum2 - (sum1 * sum1) / cols) / (cols - 1));
} else {
variance.push((sum2 - (sum1 * sum1) / cols) / cols);
}
}
return variance;
}
export function varianceByColumn(matrix, unbiased, mean) {
const rows = matrix.rows;
const cols = matrix.columns;
const variance = [];
for (let j = 0; j < cols; j++) {
let sum1 = 0;
let sum2 = 0;
let x = 0;
for (let i = 0; i < rows; i++) {
x = matrix.get(i, j) - mean[j];
sum1 += x;
sum2 += x * x;
}
if (unbiased) {
variance.push((sum2 - (sum1 * sum1) / rows) / (rows - 1));
} else {
variance.push((sum2 - (sum1 * sum1) / rows) / rows);
}
}
return variance;
}
export function varianceAll(matrix, unbiased, mean) {
const rows = matrix.rows;
const cols = matrix.columns;
const size = rows * cols;
let sum1 = 0;
let sum2 = 0;
let x = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
x = matrix.get(i, j) - mean;
sum1 += x;
sum2 += x * x;
}
}
if (unbiased) {
return (sum2 - (sum1 * sum1) / size) / (size - 1);
} else {
return (sum2 - (sum1 * sum1) / size) / size;
}
}
export function centerByRow(matrix, mean) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) - mean[i]);
}
}
}
export function centerByColumn(matrix, mean) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) - mean[j]);
}
}
}
export function centerAll(matrix, mean) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) - mean);
}
}
}
export function getScaleByRow(matrix) {
const scale = [];
for (let i = 0; i < matrix.rows; i++) {
let sum = 0;
for (let j = 0; j < matrix.columns; j++) {
sum += Math.pow(matrix.get(i, j), 2) / (matrix.columns - 1);
}
scale.push(Math.sqrt(sum));
}
return scale;
}
export function scaleByRow(matrix, scale) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) / scale[i]);
}
}
}
export function getScaleByColumn(matrix) {
const scale = [];
for (let j = 0; j < matrix.columns; j++) {
let sum = 0;
for (let i = 0; i < matrix.rows; i++) {
sum += Math.pow(matrix.get(i, j), 2) / (matrix.rows - 1);
}
scale.push(Math.sqrt(sum));
}
return scale;
}
export function scaleByColumn(matrix, scale) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) / scale[j]);
}
}
}
export function getScaleAll(matrix) {
const divider = matrix.size - 1;
let sum = 0;
for (let j = 0; j < matrix.columns; j++) {
for (let i = 0; i < matrix.rows; i++) {
sum += Math.pow(matrix.get(i, j), 2) / divider;
}
}
return Math.sqrt(sum);
}
export function scaleAll(matrix, scale) {
for (let i = 0; i < matrix.rows; i++) {
for (let j = 0; j < matrix.columns; j++) {
matrix.set(i, j, matrix.get(i, j) / scale);
}
}
}