You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -65,37 +63,13 @@ var format = require( '@stdlib/string/format' );
65
63
* var accumulator = incrnanmvariance( 3, -2.0 );
66
64
*/
67
65
functionincrnanmvariance(W,mean){
68
-
vardelta;
69
-
varbuf;
70
-
vartmp;
71
-
varM2;
72
-
varmu;
73
-
vard1;
74
-
vard2;
75
-
varN;
76
-
varn;
77
-
vari;
78
-
79
-
if(!isPositiveInteger(W)){
80
-
thrownewTypeError(format('invalid argument. Must provide a positive integer. Value: `%s`.',W));
81
-
}
82
-
83
-
buf=[];
84
-
n=W-1;
85
-
M2=0.0;
86
-
i=-1;
87
-
N=0;
88
-
66
+
varacc;
89
67
if(arguments.length>1){
90
-
if(!isNumber(mean)){
91
-
thrownewTypeError(format('invalid argument. Must provide a number. Value: `%s`.',mean));
92
-
}
93
-
mu=mean;
94
-
returnaccumulator2;
68
+
acc=incrmvariance(W,mean);
69
+
}else{
70
+
acc=incrmvariance(W);
95
71
}
96
-
97
-
mu=0.0;
98
-
returnaccumulator1;
72
+
returnaccumulator;
99
73
100
74
/**
101
75
* If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance.
@@ -104,145 +78,14 @@ function incrnanmvariance( W, mean ) {
104
78
* @param {number} [x] - input value
105
79
* @returns {(number|null)} unbiased sample variance or null
106
80
*/
107
-
functionaccumulator1(x){
81
+
functionaccumulator(x){
108
82
if(arguments.length===0){
109
-
if(N===0){
110
-
returnnull;
111
-
}
112
-
if(N===1){
113
-
return0.0;
114
-
}
115
-
if(N<W){
116
-
returnM2/(N-1);
117
-
}
118
-
returnM2/n;
83
+
returnacc();
119
84
}
120
-
121
85
if(isnan(x)){
122
-
if(N===0){
123
-
returnnull;
124
-
}
125
-
if(N===1){
126
-
return0.0;
127
-
}
128
-
if(N<W){
129
-
returnM2/(N-1);
130
-
}
131
-
returnM2/n;
86
+
returnacc();
132
87
}
133
-
134
-
// Update the index for managing the circular buffer:
135
-
i=(i+1)%W;
136
-
137
-
// Case: initial window...
138
-
if(N<W){
139
-
if(N<W){
140
-
buf.push(x);
141
-
}else{
142
-
buf[i]=x;
143
-
}
144
-
N+=1;
145
-
delta=x-mu;
146
-
mu+=delta/N;
147
-
M2+=delta*(x-mu);
148
-
if(N===1){
149
-
return0.0;
150
-
}
151
-
returnM2/(N-1);
152
-
}
153
-
154
-
// Case: N = W = 1
155
-
if(N===1){
156
-
M2=0.0;
157
-
returnM2;
158
-
}
159
-
160
-
// Case: outgoing value is NaN, which we need to simply replace without affecting the variance
161
-
if(isnan(buf[i])){
162
-
buf[i]=x;
163
-
164
-
// When replacing NaN with a value, update M2 with the squared difference from the mean
165
-
M2+=(x-mu)*(x-mu);
166
-
returnM2/n;
167
-
}
168
-
169
-
// Case: standard update when neither incoming nor outgoing values are NaN
170
-
tmp=buf[i];
171
-
delta=x-tmp;
172
-
d1=tmp-mu;
173
-
mu+=delta/N;
174
-
d2=x-mu;
175
-
M2+=delta*(d1+d2);
176
-
177
-
buf[i]=x;
178
-
returnM2/n;
179
-
}
180
-
181
-
/**
182
-
* If provided a value, the accumulator function returns an updated unbiased sample variance, ignoring NaN values. If not provided a value, the accumulator function returns the current unbiased sample variance.
183
-
*
184
-
* @private
185
-
* @param {number} [x] - input value
186
-
* @returns {(number|null)} unbiased sample variance or null
187
-
*/
188
-
functionaccumulator2(x){
189
-
if(arguments.length===0){
190
-
if(N===0){
191
-
returnnull;
192
-
}
193
-
if(N<W){
194
-
returnM2/N;
195
-
}
196
-
returnM2/W;
197
-
}
198
-
199
-
// If incoming value is NaN, ignore it:
200
-
if(isnan(x)){
201
-
if(N===0){
202
-
returnnull;
203
-
}
204
-
if(N<W){
205
-
returnM2/N;
206
-
}
207
-
returnM2/W;
208
-
}
209
-
210
-
// Update the index for managing the circular buffer:
211
-
i=(i+1)%W;
212
-
213
-
// Case: initial window...
214
-
if(N<W){
215
-
if(N<W){
216
-
buf.push(x);
217
-
}else{
218
-
buf[i]=x;
219
-
}
220
-
N+=1;
221
-
delta=x-mu;
222
-
M2+=delta*delta;
223
-
returnM2/N;
224
-
}
225
-
226
-
// Case: outgoing value is NaN, which we need to simply replace without affecting the variance
227
-
if(isnan(buf[i])){
228
-
buf[i]=x;
229
-
230
-
// Add the squared difference between the new value and mean
231
-
M2+=(x-mu)*(x-mu);
232
-
returnM2/W;
233
-
}
234
-
235
-
// Case: standard update when neither incoming nor outgoing values are NaN
0 commit comments