Commit f154c6e
committed
ENH: Use Welford's method in stats.moments.rolling_var
This PR implements a modified version of Welford's method to compute
the rolling variance. Instead of keeping track of the sum and sum of
the squares of the items in the window, it tracks the mean and the sum
of squared differences from the mean. This turns out to be (much) more
numerically stable.
The formulas to update these two variables when adding or removing an
item from the sequence are well known, see e.g.
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
The formulas used when both adding one and removing one item I have
not seen explicitly worked out anywhere, but are not too hard to come
up with if you put pen to (a lot of) paper.1 parent 4a37102 commit f154c6e
File tree
4 files changed
+67
-30
lines changed- doc/source
- pandas
- stats
- tests
4 files changed
+67
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| 279 | + | |
279 | 280 | | |
280 | 281 | | |
281 | 282 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1122 | 1122 | | |
1123 | 1123 | | |
1124 | 1124 | | |
1125 | | - | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
1126 | 1129 | | |
1127 | 1130 | | |
1128 | 1131 | | |
1129 | 1132 | | |
1130 | 1133 | | |
1131 | 1134 | | |
1132 | 1135 | | |
1133 | | - | |
| 1136 | + | |
1134 | 1137 | | |
1135 | 1138 | | |
1136 | 1139 | | |
1137 | 1140 | | |
1138 | 1141 | | |
1139 | | - | |
1140 | | - | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
1141 | 1145 | | |
1142 | | - | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
1143 | 1156 | | |
1144 | | - | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
1145 | 1160 | | |
| 1161 | + | |
1146 | 1162 | | |
1147 | 1163 | | |
1148 | | - | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | | - | |
1153 | | - | |
1154 | 1164 | | |
1155 | | - | |
1156 | | - | |
1157 | | - | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
1158 | 1184 | | |
1159 | 1185 | | |
1160 | | - | |
| 1186 | + | |
1161 | 1187 | | |
1162 | | - | |
1163 | | - | |
1164 | | - | |
1165 | | - | |
1166 | | - | |
1167 | 1188 | | |
1168 | | - | |
1169 | | - | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
1170 | 1193 | | |
1171 | | - | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
1172 | 1197 | | |
1173 | 1198 | | |
1174 | 1199 | | |
| 1200 | + | |
1175 | 1201 | | |
1176 | 1202 | | |
1177 | 1203 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
751 | 751 | | |
752 | 752 | | |
753 | 753 | | |
754 | | - | |
| 754 | + | |
755 | 755 | | |
756 | 756 | | |
757 | 757 | | |
| |||
978 | 978 | | |
979 | 979 | | |
980 | 980 | | |
981 | | - | |
| 981 | + | |
982 | 982 | | |
983 | 983 | | |
984 | 984 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
298 | | - | |
| 298 | + | |
| 299 | + | |
299 | 300 | | |
300 | 301 | | |
301 | 302 | | |
| |||
349 | 350 | | |
350 | 351 | | |
351 | 352 | | |
352 | | - | |
| 353 | + | |
| 354 | + | |
353 | 355 | | |
354 | 356 | | |
355 | 357 | | |
356 | 358 | | |
357 | 359 | | |
358 | | - | |
| 360 | + | |
| 361 | + | |
359 | 362 | | |
360 | 363 | | |
361 | 364 | | |
| |||
367 | 370 | | |
368 | 371 | | |
369 | 372 | | |
370 | | - | |
| 373 | + | |
| 374 | + | |
371 | 375 | | |
372 | 376 | | |
373 | 377 | | |
| |||
425 | 429 | | |
426 | 430 | | |
427 | 431 | | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
428 | 438 | | |
429 | 439 | | |
430 | 440 | | |
| |||
0 commit comments