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
// Update the index for managing the circular buffer:
130
-
i=(i+1)%W;
136
+
i=(i+1)%W;
131
137
132
138
// Case: initial window...
133
139
if(N<W){
134
-
buf[i]=x;// update buffer
140
+
if(N<W){
141
+
buf.push(x);
142
+
}else{
143
+
buf[i]=x;
144
+
}
135
145
N+=1;
136
146
delta=x-mu;
137
147
mu+=delta/N;
138
-
M2+=delta*(x-mu);
148
+
M2+=delta*(x-mu);
139
149
if(N===1){
140
150
return0.0;
141
151
}
142
-
returnM2/(N-1);
152
+
returnM2/(N-1);
143
153
}
144
154
145
155
// Case: N = W = 1
146
-
elseif(N===1){
156
+
if(N===1){
147
157
M2=0.0;
148
158
returnM2;
149
159
}
150
160
151
161
// Case: outgoing value is NaN, which we need to simply replace without affecting the variance
152
-
elseif(isnan(buf[i])){
153
-
buf[i]=x;
154
-
// When replacing NaN with a value, we need to update M2 with the squared difference from mean
162
+
if(isnan(buf[i])){
163
+
buf[i]=x;
164
+
165
+
// When replacing NaN with a value, update M2 with the squared difference from the mean
155
166
M2+=(x-mu)*(x-mu);
156
167
returnM2/n;
157
168
}
158
169
159
170
// Case: standard update when neither incoming nor outgoing values are NaN
160
-
else{
161
-
tmp=buf[i];
162
-
delta=x-tmp;
163
-
d1=tmp-mu;
164
-
mu+=delta/N;
165
-
d2=x-mu;
166
-
M2+=delta*(d1+d2);
167
-
}
168
-
169
-
buf[i]=x;
171
+
tmp=buf[i];
172
+
delta=x-tmp;
173
+
d1=tmp-mu;
174
+
mu+=delta/N;
175
+
d2=x-mu;
176
+
M2+=delta*(d1+d2);
177
+
178
+
buf[i]=x;
170
179
returnM2/n;
171
180
}
172
-
181
+
173
182
/**
174
183
* 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.
175
184
*
@@ -178,8 +187,6 @@ function incrnanmvariance( W, mean ) {
178
187
* @returns {(number|null)} unbiased sample variance or null
179
188
*/
180
189
functionaccumulator2(x){
181
-
vark;
182
-
183
190
if(arguments.length===0){
184
191
if(N===0){
185
192
returnnull;
@@ -200,7 +207,11 @@ function incrnanmvariance( W, mean ) {
200
207
201
208
// Case: initial window...
202
209
if(N<W){
203
-
buf[i]=x;// update buffer
210
+
if(N<W){
211
+
buf.push(x);
212
+
}else{
213
+
buf[i]=x;
214
+
}
204
215
N+=1;
205
216
delta=x-mu;
206
217
M2+=delta*delta;
@@ -210,6 +221,7 @@ function incrnanmvariance( W, mean ) {
210
221
// Case: outgoing value is NaN, which we need to simply replace without affecting the variance
211
222
elseif(isnan(buf[i])){
212
223
buf[i]=x;
224
+
213
225
// Add the squared difference between the new value and mean
214
226
M2+=(x-mu)*(x-mu);
215
227
returnM2/W;
@@ -218,8 +230,10 @@ function incrnanmvariance( W, mean ) {
218
230
// Case: standard update when neither incoming nor outgoing values are NaN
0 commit comments