-
Notifications
You must be signed in to change notification settings - Fork 3
/
icremental-online-statistics-002.nb
4230 lines (4040 loc) · 171 KB
/
icremental-online-statistics-002.nb
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Content-type: application/vnd.wolfram.mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 10.4' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 158, 7]
NotebookDataLength[ 174735, 4222]
NotebookOptionsPosition[ 158854, 3848]
NotebookOutlinePosition[ 160823, 3898]
CellTagsIndexPosition[ 160780, 3895]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[CellGroupData[{
Cell["Incremental Online Statistics", "Title",
CellChangeTimes->{{3.6698529556858463`*^9, 3.669852964981929*^9}, {
3.703627058983081*^9,
3.70362706623743*^9}},ExpressionUUID->"129ac8a2-6e51-4716-a998-\
200490363395"],
Cell["Extracting Models from Data, One Observation at a Time", "Subtitle",
CellChangeTimes->{{3.7036272558616753`*^9, 3.7036272754374037`*^9}},
FontFamily->"Candara",
FontWeight->"Bold",ExpressionUUID->"fc738cd6-52e4-4fb6-b4bf-376ce021dda9"],
Cell["Brian Beckman", "Author",
CellChangeTimes->{{3.669852973717079*^9,
3.6698529760933933`*^9}},ExpressionUUID->"5f758b53-4e60-404a-ad8d-\
b3126b7197ea"],
Cell["12 May 2017", "Date",
CellChangeTimes->{{3.6699160402711906`*^9, 3.669916050814829*^9}, {
3.670568322040224*^9, 3.67056832212545*^9}, {3.703627074282936*^9,
3.703627081067047*^9}},ExpressionUUID->"e1a5fe2c-f46d-4db8-a304-\
735fd43f88cc"],
Cell[TextData[{
"We calculate some descriptive statistics --- mean, unbiased variance, \
windowed versions of the same --- of\.7f sequences of numerical data. We use\
\.7f ",
StyleBox["fold",
FontSlant->"Italic"],
" expressions that decouple iteration and accumulation from the means of \
accessing the observations. The descriptive statistics are computed by \
stateless binary functions that update prior estimates given new data, one at \
a time. Such functions can be tested independently of the source of the data, \
say over known test cases with known true answers. The same functions can be \
deployed without even being recompiled in harsh asynchronous environments."
}], "Text",
CellChangeTimes->{{3.6704315337480507`*^9, 3.670431538810305*^9}, {
3.670433011092613*^9, 3.670433012117442*^9}, {3.670517974613204*^9,
3.670518132702087*^9}, {3.703257038903595*^9, 3.70325709498783*^9}, {
3.703521036238616*^9, 3.7035211007149363`*^9}, {3.7036271493714*^9,
3.703627240750091*^9}, {3.7036273107413282`*^9,
3.703627493470467*^9}},ExpressionUUID->"0fd4ee09-07c8-4df1-915a-\
ab5bcee610c9"],
Cell[CellGroupData[{
Cell["Prelude: Running Count", "Chapter",
CellChangeTimes->{{3.6704315063231487`*^9, 3.6704315092341843`*^9}, {
3.7036275120129557`*^9,
3.703627514556757*^9}},ExpressionUUID->"b9e934fb-0c0b-47d7-b1a5-\
5462ccedf011"],
Cell["\<\
Consider the following sample of length ten from a standard normal \
distribution, mean zero, standard deviation one:\
\>", "Text",
CellChangeTimes->{{3.703627518396331*^9, 3.703627549592085*^9}, {
3.703627640211589*^9,
3.703627644841185*^9}},ExpressionUUID->"e76ed38f-03cf-4d0d-ba23-\
35c9c310ae48"],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "zs", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{"zs", "=",
RowBox[{"RandomVariate", "[",
RowBox[{
RowBox[{"NormalDistribution", "[", "]"}], ",", "10"}], "]"}]}]}], "Input",\
CellChangeTimes->{{3.670431546140057*^9,
3.670431550834399*^9}},ExpressionUUID->"c7ee03cf-c7f7-4918-85de-\
ae1d4d8a1ef1"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"-", "0.23610520493481874`"}], ",",
RowBox[{"-", "1.0261868459977768`"}], ",",
RowBox[{"-", "0.949993488947774`"}], ",", "1.0746680959299657`", ",",
"0.13480102467335983`", ",", "0.6651168208236736`", ",",
"0.9273742108284604`", ",",
RowBox[{"-", "1.0338796485387276`"}], ",", "0.280724783428447`", ",",
RowBox[{"-", "0.7799693845055969`"}]}], "}"}]], "Output",
CellChangeTimes->CompressedData["
1:eJxTTMoPSmVkYGCwBeKyP5+en4p47bil0OcriLbqkHM4A6TNJk+f+xFI32Le
txVEp3ie+PYZSDvVelv+AdKXHJUOgmiGyCOZf4F02CrjbKbI146MnM0beYH0
6Uifm3JA+sz+pbeKgHRfjmT5PCB9a56y5SIgvahuV/FpIG0nwCR1BUgfUK5O
uQZSn7tn6QcgvUxq4WoQLbV912YQzTbR6vxnkD7fBcZfgfTkUJ+fILrGXMqT
Ieq1oxdDsgknkOa7GurtBqSvhUsu8wDS68pP7fUC0scevhPwBtJVYZekZgHp
VapJB44AaZdS1Z8XgfRWlxtX3wJpxQz3us9A+kL7iqT/QDrtcLuuQjRQ/6+j
ASpAOmzX+g5VIB33wvAhiK5w2vF4IpD+vqfPeTWQLsl53boHSCeJzk0RiXnt
6FYTxWcKpJ8VH7WMA9J1b64nxAPpT4593xOA9Bqn1VyJQHreA/b2jUD6WC/H
9fjY1479SWtvtwLpELVZf/bEvXZsu36yOTjlteMNu/9G7tNfOyb/2/cpFkg/
qNDmjgPSESGeyUlAmu2x9q3nQDpWjOUViGZYs7RcesZrRwCm7P2y
"],ExpressionUUID->"3f4d3990-df5d-4af0-88bc-16a0fa52cd1c"]
}, Open ]],
Cell["Running count, as a fold, is trivial. ", "Text",
CellChangeTimes->{{3.6704330187743607`*^9, 3.670433023108251*^9}, {
3.670518142496415*^9, 3.670518147005398*^9}, {3.7035211086483927`*^9,
3.703521119744053*^9}},ExpressionUUID->"cdf88e23-4d7e-4a4a-9dfd-\
004c8eb3b68c"],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "cume", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"cume", "[",
RowBox[{"n_", ",", "z_"}], "]"}], ":=",
RowBox[{"n", "+", "1"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{"Fold", "[",
RowBox[{"cume", ",", "0", ",", "zs"}], "]"}]}], "Input",
CellChangeTimes->{{3.670362262676239*^9, 3.670362403112179*^9}, {
3.6703624919674664`*^9, 3.670362500108472*^9}, {3.670382335403171*^9,
3.670382392568015*^9}, {3.670431573529311*^9,
3.67043161176023*^9}},ExpressionUUID->"c5c4eeae-4e0a-47d4-b73f-\
17b36399817b"],
Cell[BoxData["10"], "Output",
CellChangeTimes->{{3.670362386860121*^9, 3.670362403451788*^9},
3.6703625007501383`*^9, {3.6703823807018337`*^9, 3.670382393646204*^9}, {
3.6704316052181597`*^9, 3.670431612698892*^9}, 3.6704316598530817`*^9,
3.670434305008449*^9, 3.670510828814732*^9, 3.670511021991407*^9,
3.6705156383222437`*^9, 3.670532554390843*^9, 3.670533641128706*^9,
3.670534990204936*^9, 3.670545241683096*^9, 3.670568332167439*^9,
3.670603468588195*^9, 3.670775509237636*^9, 3.6708648250455*^9,
3.670872521140728*^9, 3.670956957876609*^9, 3.6709746721071672`*^9,
3.670979363897111*^9, 3.671033133935173*^9, 3.671033181090272*^9,
3.671033248379916*^9, 3.671039609902426*^9, 3.671042461077897*^9,
3.6710440424634867`*^9, 3.671065160885859*^9, 3.6710834272121267`*^9,
3.671208538711453*^9, 3.671213360851935*^9, 3.67121764635679*^9,
3.671218311528186*^9, 3.671380182639037*^9, 3.671467523124352*^9,
3.67149460139983*^9, 3.6715516628216953`*^9, 3.6715632502793922`*^9,
3.671587605321035*^9, 3.671654764447816*^9, 3.671663238240183*^9,
3.6716657335167418`*^9, 3.671666441628186*^9, 3.671887645821486*^9,
3.6719396125308027`*^9, 3.6719749594219503`*^9, 3.672154916974106*^9,
3.67222181089568*^9, 3.6723061262429323`*^9, 3.672308486802026*^9,
3.672311740498646*^9, 3.672311893426462*^9, 3.672476728306038*^9,
3.672833720310568*^9, 3.672911581556663*^9, 3.673548773060635*^9,
3.676478494593018*^9, 3.703191966360875*^9, 3.703238552830134*^9,
3.703238745418429*^9, 3.703247642341167*^9, 3.7035209774920683`*^9,
3.70352111141931*^9,
3.703626685236958*^9},ExpressionUUID->"8de8bd56-2088-4f4c-b4a4-\
277e9b3603a1"]
}, Open ]],
Cell["\<\
Wolfram\[CloseCurlyQuote]s documentation for Fold includes an example that \
explains this operator symbolically:\
\>", "Text",
CellChangeTimes->{{3.703627654696693*^9, 3.703627655361246*^9}, {
3.7036277306386957`*^9,
3.703627778906796*^9}},ExpressionUUID->"a568f3e8-6216-47b7-97fd-\
e4b312b793e6"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Fold", "[",
RowBox[{"f", ",", "x", ",",
RowBox[{"{",
RowBox[{"a", ",", "b", ",", "c", ",", "d"}], "}"}]}], "]"}]], "Input",
CellID->12834,ExpressionUUID->"e0bb0789-4704-44c9-989d-73fa1df944ac"],
Cell[BoxData[
RowBox[{"f", "[",
RowBox[{
RowBox[{"f", "[",
RowBox[{
RowBox[{"f", "[",
RowBox[{
RowBox[{"f", "[",
RowBox[{"x", ",", "a"}], "]"}], ",", "b"}], "]"}], ",", "c"}], "]"}],
",", "d"}], "]"}]], "Output",
CellChangeTimes->{3.703627785817709*^9},
ImageSize->{167, 16},
ImageMargins->{{0, 0}, {0, 0}},
ImageRegion->{{0, 1}, {0,
1}},ExpressionUUID->"94264381-0f9d-4a9e-a8b3-8c269d1d0c3d"]
}, Open ]],
Cell[BoxData["Fold"], "Input",
CellChangeTimes->{{3.7036277976045513`*^9,
3.703627798479802*^9}},ExpressionUUID->"6fd208fa-da06-4634-877d-\
078c68c80f8c"],
Cell[CellGroupData[{
Cell["Running Mean", "Subchapter",
CellChangeTimes->{{3.670382281644032*^9, 3.6703822840358477`*^9}, {
3.703417921227977*^9,
3.703417921748693*^9}},ExpressionUUID->"2e907cf8-02e7-4eb1-97b9-\
8045ac871b0c"],
Cell[TextData[{
"As explicit ratio of new sum and new count, we write ",
StyleBox["x", "Code"],
" for the mean-so-far in the code, and use the symbol ",
Cell[BoxData[
FormBox[
OverscriptBox["z", "_"], TraditionalForm]],ExpressionUUID->
"9e5a45e6-65d4-45df-a01d-13daa2422f24"],
" for it in mathematical expressions."
}], "Text",
CellChangeTimes->{{3.670431465148507*^9, 3.67043148791577*^9}, {
3.670518308010725*^9, 3.670518363776095*^9}, 3.6714946268792133`*^9, {
3.7032571068989573`*^9,
3.70325710732666*^9}},ExpressionUUID->"487e0667-44b9-4f04-8636-\
162ffad41841"],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "cume", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"cume", "[",
RowBox[{
RowBox[{"{",
RowBox[{"x_", ",", "n_", ",", "sum_"}], "}"}], ",", "z_"}], "]"}], ":=",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
FractionBox[
RowBox[{"sum", "+", "z"}],
RowBox[{"n", "+", "1"}]], ",",
RowBox[{"n", "+", "1"}], ",",
RowBox[{"sum", "+", "z"}]}], "}"}]}], ";"}], "\[IndentingNewLine]",
RowBox[{"Fold", "[",
RowBox[{"cume", ",",
RowBox[{"{",
RowBox[{"0", ",", "0", ",", "0"}], "}"}], ",", "zs"}], "]"}]}], "Input",
CellChangeTimes->{{3.670362262676239*^9, 3.670362403112179*^9}, {
3.6703624919674664`*^9, 3.670362500108472*^9}, {3.670382335403171*^9,
3.670382392568015*^9}, {3.6704316503726187`*^9, 3.670431652756393*^9},
3.671494635109106*^9},ExpressionUUID->"3cc7ecca-c8a0-4a97-aae2-\
e2df788d10fd"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"-", "0.09434496372407875`"}], ",", "10", ",",
RowBox[{"-", "0.9434496372407875`"}]}], "}"}]], "Output",
CellChangeTimes->CompressedData["
1:eJxTTMoPSmVkYGCwAuI4j7NfT0W8djyVqORwBkgzzZo99yOQXhN8cCuI9io6
8+0zkFaY6Wf5B0i/mqB6EERbbDqR+RdIN/8yz2aKfO1o4NG+kRdIN0ysuS0H
pHUMVt4qAtJX/v2omAeizdUsFwHpz3P3Fp8G0ncYWaWugOiyupRrQPrw6v1L
PwDp1YuXrAbR0trRW0H0ziN25z8D6RNcS4y/Auk9T/x/gmhWIzlPhqjXjgn2
mSacQFphUYS3G5DOvSazzANIG9ac3esFpE+8/CTgDaTbtK9JzQLSMbtTDxwB
0l7cq/9eBNIfFO9cfQukhS54130G61+d9B9Iv/rRr6sQ/drRw/5kgAqQvmG6
qUMVSP9bZ/oQRO913/N4IpBm+DjJeTWQXpvzvnUPkD4kvjBFJOa144ZFcXym
QLp04gnLOCC9cv3thHggfb9r4vcEIJ2XvY4rEUgf+MHdvhFIt2/muh4f+9qR
JXPz7VYgve7Yoj974l478uy40hyc8tpRqkDN3H36a8dj592/xAJpja963HFA
mqXRJzkJSEsKriyXnvHaEQCdKfNj
"],ExpressionUUID->"1068d583-205b-462c-b96f-e7e89250d257"]
}, Open ]],
Cell[TextData[{
"Check against ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" built-in."
}], "Text",
CellChangeTimes->{{3.670431675634911*^9,
3.670431683354504*^9}},ExpressionUUID->"68ed2278-ce54-4449-916c-\
7228ebecc711"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Mean", "[", "zs", "]"}]], "Input",
CellChangeTimes->{{3.67038269040097*^9,
3.67038269418116*^9}},ExpressionUUID->"f4212f36-9443-4840-a179-\
2b10b7a0baa6"],
Cell[BoxData[
RowBox[{"-", "0.09434496372407875`"}]], "Output",
CellChangeTimes->CompressedData["
1:eJxTTMoPSmVkYGCwAmKbXxd4Nke8dtw44/O3U0D633tlhzNAmstq7tyPQHrF
m0NbQXQby7lvn4G06v4Ayz9A+kqg+kEQfeDUqcy/QLrhj2U2U+RrxyePOzby
Aukm64o7ckC64OWqW0VAWurV74p5QFpnn7rlIiDdYr2/+DSQXrmITeoKkA5m
b0i5BqTrnA4u/QCkD1suWw2iH/PEbQXRF447nP8MpG8sW2r8FUhznQ/8CaLV
Dsl7MkS9drR/km3CCaTf6UZ5uwHpSwFyyzyA9IGZ5/d6AWmn6i8C3kDa5ex1
qVlAWn1/+oEjQLrhcvi/i0D6yaa7V98C6S/xfnWfgfTc9LVJ/4H0h4qJugrR
rx0nXzkVoAKkc09u7lAF0iHm5g9BtNPNvY8nAuljUlOdVwPpbz8/tO4B0snb
F6WIxLx2VLJI4DMF0ttNTlnGAeky57sJ8UDaQnby9wQgXemzgSsRSDuI8bVv
BNI2IjzX42OB+v5uud0KpJ32LfmzJw7ob9drzcEpQPoE06/Y6a8d5ScYcMcB
ae80/+QkIC21clW59IzXjgBCGPVO
"],ExpressionUUID->"1d8b1bd3-a9b9-47e3-af3f-fa2da7cdae68"]
}, Open ]],
Cell[TextData[{
"A preferable form: one fewer quantity to track; new value expressed as sum \
of old value and a correction that depends only on old values. The right-hand \
side of the recurrence ",
Cell[BoxData[
FormBox[
RowBox[{
OverscriptBox["z", "_"], "\[LeftArrow]",
RowBox[{
OverscriptBox["z", "_"], "+",
RowBox[{"K", "\[Times]",
RowBox[{"(",
RowBox[{"z", "-",
OverscriptBox["z", "_"]}], ")"}]}]}]}], TraditionalForm]],
ExpressionUUID->"ce3b72ef-5e59-4e92-a7ba-e328c23c94a4"],
" depends only on old values except for the new datum ",
Cell[BoxData[
FormBox["z", TraditionalForm]],ExpressionUUID->
"c3fd6865-129e-4ca1-b035-593fab73677a"],
". The recurrence is not an equation, but it means the following equation"
}], "Text",
CellChangeTimes->{{3.6704316924172087`*^9, 3.670431753150216*^9}, {
3.6704330453125343`*^9, 3.6704331176664963`*^9}, {3.67043363375209*^9,
3.6704336988423653`*^9}, {3.670459737889914*^9, 3.670459769456072*^9}, {
3.670459896870551*^9, 3.670459939759371*^9}, {3.670515581200055*^9,
3.6705156053270893`*^9}, {3.6705159594739017`*^9, 3.670516034850823*^9}, {
3.670516930257908*^9, 3.670517000661891*^9}, {3.6705181671335*^9,
3.67051830083702*^9}, {3.703257150897099*^9, 3.703257340578795*^9}, {
3.703258830996993*^9,
3.703258895832912*^9}},ExpressionUUID->"b205f9bd-e470-4e88-8ed7-\
64f4cd96ccb6"],
Cell[BoxData[
FormBox[
RowBox[{
SubscriptBox[
OverscriptBox["z", "_"],
RowBox[{"n", "+", "1"}]], "=",
RowBox[{
RowBox[{
SubscriptBox[
OverscriptBox["z", "_"], "n"], "+",
RowBox[{"K", "\[Times]",
RowBox[{"(",
RowBox[{
SubscriptBox["z",
RowBox[{"n", "+", "1"}]], "-",
SubscriptBox[
OverscriptBox["z", "_"], "n"]}], ")"}]}]}], "=",
RowBox[{"x", "+",
RowBox[{"K", "\[Times]",
RowBox[{
RowBox[{"(",
RowBox[{
SubscriptBox["z",
RowBox[{"n", "+", "1"}]], "-", "x"}], ")"}], "."}]}]}]}]}],
TraditionalForm]], "DisplayFormulaNumbered",
CellChangeTimes->{{3.6704316924172087`*^9, 3.670431753150216*^9}, {
3.6704330453125343`*^9, 3.6704331176664963`*^9}, {3.67043363375209*^9,
3.6704336988423653`*^9}, {3.670459737889914*^9, 3.670459769456072*^9}, {
3.670459896870551*^9, 3.670459939759371*^9}, {3.670515581200055*^9,
3.6705156053270893`*^9}, {3.6705159594739017`*^9, 3.670516034850823*^9}, {
3.670516930257908*^9, 3.670517000661891*^9}, {3.6705181671335*^9,
3.67051830083702*^9}, {3.703257150897099*^9, 3.703257340578795*^9}, {
3.703258830996993*^9,
3.703258926808292*^9}},ExpressionUUID->"10e1690b-0ae8-42de-950b-\
f3c9123d926a"],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "cume", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"cume", "[",
RowBox[{
RowBox[{"{",
RowBox[{"x_", ",", "n_"}], "}"}], ",", "z_"}], "]"}], ":=",
"\[IndentingNewLine]",
RowBox[{"With", "[",
RowBox[{
RowBox[{"{",
RowBox[{"K", "=",
FractionBox["1",
RowBox[{"n", "+", "1"}]]}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"x", "+",
RowBox[{"K",
RowBox[{"(",
RowBox[{"z", "-", "x"}], ")"}]}]}], ",",
RowBox[{"n", "+", "1"}]}], "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Fold", "[",
RowBox[{"cume", ",",
RowBox[{"{",
RowBox[{"0", ",", "0"}], "}"}], ",", "zs"}], "]"}]}], "Input",
CellChangeTimes->{{3.670362262676239*^9, 3.670362403112179*^9}, {
3.670362444550591*^9, 3.670362471870253*^9}, {3.6703625074206047`*^9,
3.670362513220016*^9}, {3.67037783476613*^9, 3.67037784933753*^9}, {
3.670378041717937*^9, 3.670378067296968*^9}, {3.6703824020637703`*^9,
3.670382402334654*^9}, {3.670431768319312*^9, 3.6704317784315243`*^9}, {
3.670515854519878*^9, 3.670515855081403*^9}, {3.671494664327977*^9,
3.671494669399404*^9}},ExpressionUUID->"f405aaa1-41f2-45c1-9b4e-\
099ceab5bce1"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"-", "0.09434496372407876`"}], ",", "10"}], "}"}]], "Output",
CellChangeTimes->{
3.671380182794004*^9, 3.67146752330802*^9, 3.6714946702176313`*^9,
3.671551662979211*^9, 3.671563250532096*^9, 3.671587605469666*^9,
3.671654764595467*^9, 3.671663238388459*^9, 3.671665733669017*^9,
3.671666441776033*^9, 3.6718876459843616`*^9, 3.67193961272646*^9,
3.671974959583703*^9, 3.672154917135109*^9, 3.672221811052742*^9,
3.6723061263722897`*^9, 3.672308486964917*^9, 3.672311740646706*^9,
3.672311893558407*^9, 3.672476728505651*^9, 3.6728337204119377`*^9,
3.672911581705785*^9, 3.673548773210943*^9, 3.67647849474658*^9,
3.7031920412240143`*^9, {3.703238727507362*^9, 3.70323874556763*^9},
3.7032476425421677`*^9,
3.703626685383574*^9},ExpressionUUID->"18357440-36f3-4d2e-a996-\
8abaf0a45fec"]
}, Open ]],
Cell[TextData[{
"We prefer this form because (1) it separates old quantities on the right \
(except for the new datum, ",
Cell[BoxData[
FormBox["z", TraditionalForm]],ExpressionUUID->
"b0eff3a7-7b89-4d0a-92b9-a1b8a08202be"],
") from new quantities on the left, and (2) it is easy to memorize because \
it is \.7f\.7fan affine update: the old estimate plus a gain factor times a \
correction, namely the residual of the new observation from\.7f the old \
mean-so-far. "
}], "Text",
CellChangeTimes->{{3.670517004197152*^9, 3.670517035862468*^9}, {
3.670517077032709*^9, 3.67051707851892*^9}, {3.670518388275486*^9,
3.670518405641315*^9}, {3.670518522885989*^9, 3.6705185551517487`*^9}, {
3.670603643386138*^9, 3.670603773061327*^9}, {3.670630445011032*^9,
3.670630450887945*^9}, {3.67063225971425*^9, 3.670632260359024*^9}, {
3.671494686782105*^9, 3.6714947015967073`*^9}, {3.7032573591932373`*^9,
3.703257370310305*^9}, {3.7032589438421297`*^9, 3.703259017330193*^9}, {
3.703259645970358*^9,
3.703259721601274*^9}},ExpressionUUID->"1e223bfb-38fa-4bd1-a604-\
4a64e43d8859"],
Cell["\<\
Equation 1 is formally identical\.7f\.7f to the Kalman update. This paper \
builds\.7f up to the Kalman filter through more elementary calculations that \
have similar forms.\
\>", "Text",
CellChangeTimes->{{3.6704316924172087`*^9, 3.670431753150216*^9}, {
3.6704330453125343`*^9, 3.6704331176664963`*^9}, {3.67043363375209*^9,
3.6704336988423653`*^9}, {3.670459737889914*^9, 3.670459769456072*^9}, {
3.670459896870551*^9, 3.670459939759371*^9}, {3.670515581200055*^9,
3.6705156053270893`*^9}, {3.6705159594739017`*^9, 3.670516077348494*^9}, {
3.670516113480064*^9, 3.670516143854025*^9}, {3.670517063936718*^9,
3.67051706403942*^9}, {3.670518573059667*^9, 3.6705185804182663`*^9}, {
3.670518619165094*^9, 3.670518628451119*^9}, {3.670603785703046*^9,
3.670603826763315*^9}, {3.7032387919021473`*^9, 3.703238792522904*^9}, {
3.703259734146041*^9,
3.70325980488206*^9}},ExpressionUUID->"74e72d77-bdcb-45ac-ae45-\
2f0a5505820b"],
Cell["\<\
The following numerical check raises confidence that we have a correct \
formula.\
\>", "Text",
CellChangeTimes->{{3.670515538020369*^9, 3.670515571242523*^9}, {
3.703267353274686*^9,
3.703267354560576*^9}},ExpressionUUID->"0ed2b7a6-efd7-4382-b583-\
fddfc9c01c4f"],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "cume", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"cume", "[",
RowBox[{
RowBox[{"{",
RowBox[{"x_", ",", "n_"}], "}"}], ",", "z_"}], "]"}], ":=",
"\[IndentingNewLine]",
RowBox[{"With", "[",
RowBox[{
RowBox[{"{",
RowBox[{"K", "=",
FractionBox["1",
RowBox[{"n", "+", "1"}]]}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"K",
RowBox[{"(",
RowBox[{"z", "+",
RowBox[{"n", " ", "x"}]}], ")"}]}], ",",
RowBox[{"n", "+", "1"}]}], "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"Fold", "[",
RowBox[{"cume", ",",
RowBox[{"{",
RowBox[{"0", ",", "0"}], "}"}], ",", "zs"}], "]"}]}], "Input",
CellChangeTimes->{{3.671287837306099*^9, 3.671287843472137*^9}, {
3.671288077997649*^9,
3.671288231024335*^9}},ExpressionUUID->"a5b30c0b-b814-42f9-9316-\
231dcf3b45ab"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"-", "0.09434496372407875`"}], ",", "10"}], "}"}]], "Output",
CellChangeTimes->{{3.67128820891545*^9, 3.67128823198494*^9},
3.671380182892873*^9, 3.671467523603307*^9, 3.6714947198016567`*^9,
3.671551663029353*^9, 3.671563250771865*^9, 3.671587605520295*^9,
3.671654764645953*^9, 3.671663238439508*^9, 3.671665733718739*^9,
3.6716664418264112`*^9, 3.671887646033708*^9, 3.671939612798139*^9,
3.6719749596345053`*^9, 3.6721549171881657`*^9, 3.672221811102048*^9,
3.6723061264220667`*^9, 3.672308487014928*^9, 3.672311740697731*^9,
3.672311893607265*^9, 3.67247672855307*^9, 3.672833720444697*^9,
3.6729115817557096`*^9, 3.673548773258769*^9, 3.676478494795781*^9,
3.7031920537790337`*^9, 3.7032388110075417`*^9, 3.703247642592477*^9,
3.7036266854322042`*^9},ExpressionUUID->"225e1574-b4cf-4741-97ec-\
948200afeea1"]
}, Open ]],
Cell[TextData[{
"Symbolically, the Kalman-like form ",
Cell[BoxData[
FormBox[
RowBox[{"x", "+",
RowBox[{"K", "(",
RowBox[{"z", "-", "x"}], ")"}]}], TraditionalForm]],ExpressionUUID->
"8beb7f8a-3702-4454-88cb-07a3715c5b53"],
" is equal to the more obvious form ",
Cell[BoxData[
FormBox[
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"n", " ", "x"}], "+", "z"}], ")"}], "/",
RowBox[{"(",
RowBox[{"n", "+", "1"}], ")"}]}], TraditionalForm]],ExpressionUUID->
"80cfb143-ac90-4eaf-b028-aba4eba766fd"],
" when ",
Cell[BoxData[
FormBox[
RowBox[{
RowBox[{"n", "+", "1"}], "\[NotEqual]", " ", "0"}], TraditionalForm]],
ExpressionUUID->"6130502a-e848-4d44-9318-d1d81e429d71"],
"."
}], "Text",
CellChangeTimes->{{3.703238817945127*^9, 3.7032388955480337`*^9}, {
3.7032598156015577`*^9,
3.703259842487281*^9}},ExpressionUUID->"83e10233-a7d3-4614-8a30-\
fee46a5eba9e"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Solve", "[",
RowBox[{
RowBox[{
RowBox[{"x", "+",
RowBox[{"K",
RowBox[{"(",
RowBox[{"z", "-", "x"}], ")"}]}]}], "\[Equal]",
FractionBox[
RowBox[{
RowBox[{"n", " ", "x"}], "+", "z"}],
RowBox[{"n", "+", "1"}]]}], ",", "K"}], "]"}]], "Input",
CellChangeTimes->{{3.6712884079266863`*^9, 3.6712884562672377`*^9}, {
3.671288503604869*^9,
3.6712885043628893`*^9}},ExpressionUUID->"6f581829-1095-45cd-86c6-\
e0fff1f957f1"],
Cell[BoxData[
RowBox[{"{",
RowBox[{"{",
RowBox[{"K", "\[Rule]",
FractionBox["1",
RowBox[{"1", "+", "n"}]]}], "}"}], "}"}]], "Output",
CellChangeTimes->{3.671288458081163*^9, 3.6712885056894503`*^9,
3.6713801831560717`*^9, 3.6714675236574497`*^9, 3.671494744314947*^9,
3.671551663079789*^9, 3.6715632508217688`*^9, 3.6715876055693903`*^9,
3.671654764695631*^9, 3.671663238489139*^9, 3.67166573376818*^9,
3.671666441876202*^9, 3.671887646083633*^9, 3.6719396128599157`*^9,
3.6719749596851797`*^9, 3.672154917235499*^9, 3.672221811152903*^9,
3.672306126468524*^9, 3.672308487069242*^9, 3.672311740746661*^9,
3.672311893658123*^9, 3.672476728604815*^9, 3.6728337204823427`*^9,
3.67291158180684*^9, 3.673548773310336*^9, 3.676478494846999*^9,
3.703247642641045*^9,
3.703626685484098*^9},ExpressionUUID->"a407b647-5571-4984-9a25-\
e1054d2dbb27"]
}, Open ]]
}, Open ]],
Cell[CellGroupData[{
Cell["Running Variance", "Subchapter",
CellChangeTimes->{{3.670382309964444*^9, 3.670382314266637*^9}, {
3.670395292654766*^9, 3.6703952975310907`*^9}, {3.6703953494267073`*^9,
3.67039535540079*^9}, {3.670431797991806*^9,
3.670431803519627*^9}},ExpressionUUID->"98eaf97d-c181-45f7-b379-\
7b39102fdc4e"],
Cell[CellGroupData[{
Cell["Definitions", "Subsection",
CellChangeTimes->{{3.703255624184164*^9,
3.703255626655767*^9}},ExpressionUUID->"5a88b1e0-4efa-4335-954e-\
f55c7e5f3a30"],
Cell[TextData[{
"Variance is a measure of volatility or dispersion. It is the sum of squared \
residuals of data with respect to running mean divided by the count less one. \
A \.7fresidual is the difference between an observation ",
Cell[BoxData[
FormBox["z", TraditionalForm]],ExpressionUUID->
"c940438a-f105-4030-83de-822e2022b45a"],
" and the old mean ",
Cell[BoxData[
FormBox["x", TraditionalForm]],ExpressionUUID->
"0bb8aeb1-647e-46fa-bf29-3347628ed8c6"],
". The \[OpenCurlyDoubleQuote]count less one\[CloseCurlyDoubleQuote] is \
Bessel\[CloseCurlyQuote]s correction. "
}], "Text",
CellChangeTimes->{{3.670431823046796*^9, 3.670431859941572*^9}, {
3.670433713354093*^9, 3.670433722114554*^9}, {3.6705161644664516`*^9,
3.670516219692975*^9}, {3.67053362227778*^9, 3.670533629483571*^9}, {
3.7032471240364017`*^9, 3.703247146686647*^9}, {3.7032574763652563`*^9,
3.7032574824031963`*^9}, {3.70325751378489*^9, 3.703257514184754*^9}, {
3.7032598668148813`*^9,
3.7032598816459103`*^9}},ExpressionUUID->"7f05ea6a-5ee0-4848-9294-\
4a4e1b6cda7a"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Variance", "[", "zs", "]"}]], "Input",
CellChangeTimes->{{3.670382669592613*^9, 3.670382674246121*^9}, {
3.6703829206029*^9, 3.670382938545705*^9}, {3.670459965363146*^9,
3.670459966610405*^9}},ExpressionUUID->"5a587299-2251-4a3e-85ae-\
5e42f5064541"],
Cell[BoxData["0.6837594372569936`"], "Output",
CellChangeTimes->{
3.670382674618895*^9, {3.670382921838168*^9, 3.67038293941509*^9},
3.6703954855841722`*^9, 3.670431972098316*^9, 3.670434305457439*^9,
3.670459967798513*^9, 3.6705108290471373`*^9, 3.670511022188486*^9,
3.670515638555119*^9, 3.67053255482953*^9, 3.670533722642839*^9,
3.670534990454266*^9, 3.6705452419116898`*^9, 3.670568332377552*^9,
3.670603843743137*^9, 3.6706304978358097`*^9, 3.670775509436372*^9,
3.670872521345413*^9, 3.6709569580752087`*^9, 3.6709746723030777`*^9,
3.670979364100161*^9, 3.671033134135478*^9, 3.6710331812935963`*^9,
3.671033259015767*^9, 3.671039610140358*^9, 3.6710424612770853`*^9,
3.671044042672844*^9, 3.671065161092989*^9, 3.671083427711821*^9,
3.671208538918159*^9, 3.671213361048538*^9, 3.6712176466373262`*^9,
3.671218311739625*^9, 3.671380183659974*^9, 3.671467524536325*^9,
3.6715516631294327`*^9, 3.6715632509024687`*^9, 3.671587605794986*^9,
3.671654764745378*^9, 3.671663238539227*^9, 3.671665733818348*^9,
3.67166644192665*^9, 3.671887646133072*^9, 3.671939613038643*^9,
3.671974959900478*^9, 3.672154917458267*^9, 3.672221811203701*^9,
3.672306126523012*^9, 3.672308487273673*^9, 3.672311740796954*^9,
3.6723118937074347`*^9, 3.672476728652503*^9, 3.672833720514945*^9,
3.672911581857141*^9, 3.673548773359325*^9, 3.6764784948970547`*^9,
3.703192105102125*^9, 3.703238928137909*^9,
3.703626685556292*^9},ExpressionUUID->"f4900f80-e664-4549-af07-\
c6c5051daaef"]
}, Open ]],
Cell["Variance is also standard deviation squared.", "Text",
CellChangeTimes->{{3.6704320048744307`*^9,
3.6704320221877337`*^9}},ExpressionUUID->"69ddb04b-bd34-4617-aaf7-\
ce64b9e92738"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
SuperscriptBox[
RowBox[{"StandardDeviation", "[", "zs", "]"}], "2"], "\[Equal]",
RowBox[{"Variance", "[", "zs", "]"}]}]], "Input",
CellChangeTimes->{{3.670431982689183*^9,
3.6704319993084106`*^9}},ExpressionUUID->"1e67c627-436a-43b3-a7c6-\
13e1697b763d"],
Cell[BoxData["True"], "Output",
CellChangeTimes->CompressedData["
1:eJwV0Gkow3EAxvG/RTlGFF5syZ1oSplmXuCfzJmEcs1ma144sqIcb/YCc7yU
F65Wjnhhjq3mDSbXmpoi11xpJqX9Uhhjrnn+L54+b5++0XJlWYMXRVECjGXy
RFqqCF2RnpJ9AH0tXfsXUDo3q3mBXxrzKmOK+MTlhJutYuEP3Loc2GGM0R81
/kKNM7OZVU3oaItHHwiTDCF+kXBzbeWqDfYv84QzUOTYbbfAEmUA5xRGWPsU
53CHmOaeIdtHq2Vc5MpXGc1C0aET9nQspL5DVWKlm/HbGFdA1RB6StDN94OK
L0mRCGbmxM7nQ1vYjbEQshvcwUVQNu7iTMCm4JGtPfhoeDh7gkOtUpUTutrW
5R7IGxtPjqoldOHNcWkcdGRsD8bDN13uHWOx3XQ/DO3Tszla+N7+p96A4hGd
IlRMaO+KlqA0yH89E0rgqMZRL4VpnMmPethvW/eXQbc6fEAPBQkhVmkdoYMo
47UaluYt/WxI8D/rtrdcQejPpzpR3ih6emQxEsieNnRyxwj9D9DZ51w=
"],ExpressionUUID->"66b81d2d-2f69-4487-b3e7-2390d9858e5e"]
}, Open ]],
Cell["The sum of squared residuals, ", "Text",
CellChangeTimes->{{3.670432048862856*^9, 3.6704320868380537`*^9},
3.67046000286861*^9, {3.703257530821227*^9, 3.7032575581033907`*^9},
3.703257766040704*^9},ExpressionUUID->"4bda312c-357c-4436-87be-\
a0e9842b9223"],
Cell[BoxData[
FormBox[
RowBox[{
SubscriptBox["\[CapitalSigma]", "n"],
OverscriptBox["=", "def"],
RowBox[{
UnderoverscriptBox["\[Sum]",
RowBox[{"i", "=", "1"}], "n"],
SuperscriptBox[
RowBox[{"(",
RowBox[{
SubscriptBox["z", "i"], "-",
SubscriptBox[
OverscriptBox["z", "_"], "n"]}], ")"}], "2"]}]}],
TraditionalForm]], "DisplayFormulaNumbered",
CellChangeTimes->{{3.670432048862856*^9, 3.6704320868380537`*^9},
3.67046000286861*^9, {3.703257530821227*^9, 3.7032575581033907`*^9}, {
3.703257692174491*^9,
3.703257735513049*^9}},ExpressionUUID->"251901b2-0841-43d0-b783-\
e521c2bba640"],
Cell["is the variance times the length-less-one:", "Text",
CellChangeTimes->{{3.670432048862856*^9, 3.6704320868380537`*^9},
3.67046000286861*^9, {3.703257530821227*^9, 3.7032575581033907`*^9}, {
3.703257757111902*^9, 3.70325776030017*^9}, {3.7032577988054743`*^9,
3.703257806650758*^9}},ExpressionUUID->"65650b20-a582-4a27-aebb-\
b186464f3d8d"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"Variance", "[", "zs", "]"}], "*",
RowBox[{"(",
RowBox[{
RowBox[{"Length", "[", "zs", "]"}], "-", "1"}], ")"}]}]], "Input",
CellChangeTimes->{{3.670382907632208*^9, 3.670382947009407*^9}, {
3.670383269166387*^9, 3.670383275524267*^9}, {3.670432027377913*^9,
3.670432035537219*^9}},ExpressionUUID->"bffe9cd2-79cf-4ab5-836c-\
ae752dea31bd"],
Cell[BoxData["6.1538349353129425`"], "Output",
CellChangeTimes->{
3.670382913059112*^9, 3.670382947400587*^9, {3.670383272026621*^9,
3.6703832763587027`*^9}, 3.67039548680044*^9, 3.670431969625476*^9,
3.670432035825199*^9, 3.670434305558316*^9, 3.670459974344613*^9,
3.6704600139723186`*^9, 3.670510829181871*^9, 3.67051102230578*^9,
3.6705156386581993`*^9, 3.6705325549532547`*^9, 3.670533655682394*^9,
3.670534990585311*^9, 3.6705452420296707`*^9, 3.670568337919689*^9,
3.6706038653803387`*^9, 3.6707755095403643`*^9, 3.6708725214421453`*^9,
3.6709569581758337`*^9, 3.670974672407144*^9, 3.670979364202217*^9,
3.671033134236342*^9, 3.6710331813939133`*^9, 3.671033259120215*^9,
3.671039610257135*^9, 3.67104246137722*^9, 3.671044042793754*^9,
3.6710651612116137`*^9, 3.671083427872038*^9, 3.6712085392267942`*^9,
3.671213361152192*^9, 3.671217646936729*^9, 3.671218311857286*^9,
3.671380183760858*^9, 3.6714675246581373`*^9, 3.67155166323343*^9,
3.671563251065629*^9, 3.671587605920619*^9, 3.671654764861909*^9,
3.6716632387889357`*^9, 3.671665734107753*^9, 3.6716664422269917`*^9,
3.67188764635574*^9, 3.6719396131768227`*^9, 3.671974960015381*^9,
3.672154917571679*^9, 3.672221811435433*^9, 3.6723061267348557`*^9,
3.67230848738134*^9, 3.672311741032843*^9, 3.67231189392535*^9,
3.672476728758039*^9, 3.6728337205802317`*^9, 3.6729115819563847`*^9,
3.673548773477803*^9, 3.676478494995984*^9, 3.703192126600795*^9,
3.703238936549418*^9,
3.7036266857045507`*^9},ExpressionUUID->"dc9828f0-da00-4530-9e4d-\
416809ee0562"]
}, Open ]],
Cell["\<\
The covariance, for scalar data, is the same as the Variance. For vector \
data, the covariance is a matrix.\
\>", "Text",
CellChangeTimes->{{3.703238946912305*^9,
3.7032389776943417`*^9}},ExpressionUUID->"7aed2afd-b552-46b6-a81d-\
6145e68f6acb"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Covariance", "[", "zs", "]"}]], "Input",
CellChangeTimes->{{3.670630484085153*^9,
3.670630489935699*^9}},ExpressionUUID->"d967e2e5-dd62-4697-afe2-\
f2af2cd14258"],
Cell[BoxData["0.6837594372569936`"], "Output",
CellChangeTimes->CompressedData["
1:eJxTTMoPSmVkYGDQAeIQF09uncjXjm1HNt0qAtIxD/UtFwHpXKvjxaeBdKi9
qNQVIM0m2JFyDUhfjTm/9AOQvmm0bjWIFhFP2wqiD3X6n/8MpNXWrjX+CqRv
TEj4CaJfP9H1ZIgC6p9dZ8IJpI+pZnq7AeklJWrLPIB0gOqDvV5A+g07k6A3
kM74/FNqFpAuMJl+4AiQLhF5efUtkNY+mFL3GUjf0tif9B9Iq+nO1VWIfu1o
dudWgAqQvtx6tEMVxL/l8RBEFxifeTwRSEueXO68GkgvcGNq2wOkf+zfnCIS
89rxWXs+nymQXm130zIOSL/89DYhHkjfn7boewKQTtHfz5UIpDl2SbZvBNI/
Holfj4997fjU9MDtViC94+qGP3viXjvGJjxsDk4B+u+fs4f79NeOr/wWKMcB
6aLOHeXSM147AgBz1bUy
"],ExpressionUUID->"997e2ad0-77ca-43f1-b15f-63e5b9e67036"]
}, Open ]],
Cell["\<\
We require a running variance that does not refer to future observations and \
does not store the entire past, only a summary of constant size. \
\>", "Text",
CellChangeTimes->{{3.670604020011711*^9, 3.670604024479059*^9}, {
3.703257883604397*^9, 3.703257883770673*^9}, {3.703259901741646*^9,
3.703259908835472*^9}},ExpressionUUID->"39080b1f-bd6d-40f4-a6fb-\
666ac4d08441"]
}, Open ]],
Cell[CellGroupData[{
Cell["Brute-Force Variance", "Subsection",
CellChangeTimes->{{3.6705336697324743`*^9,
3.670533675772894*^9}},ExpressionUUID->"ca0888ee-e33e-41ce-a9f4-\
1736ca09db56"],
Cell[TextData[{
"The following accumulates the variance directly from the definition. This \
is useful for numerical checks. However, it assumes we know the length and \
the mean of the entire sequence ",
StyleBox["zs", "Input"],
" of observations ahead of time, so it does not meet our requirement of \
being incremental. It also needs computer memory for the entire sequence ",
StyleBox["zs", "Input"],
", which is of variable or unknown length, so does not meet our requirement \
of constant memory. "
}], "Text",
CellChangeTimes->{{3.670432100808134*^9, 3.6704322064258137`*^9}, {
3.670432649658374*^9, 3.670432667175961*^9}, {3.670515648597191*^9,
3.670515704555501*^9}, {3.6705162522000637`*^9, 3.6705163243713713`*^9}, {
3.67051642781385*^9, 3.6705164987871428`*^9}, {3.670603885781478*^9,
3.67060397961869*^9}, 3.670604012657071*^9, {3.7032476873151207`*^9,
3.703247733464987*^9}, {3.703257610890315*^9, 3.70325761787906*^9}, {
3.703259918398096*^9,
3.70325993478627*^9}},ExpressionUUID->"71714606-c700-43ce-a0a3-\
2bc3a7c6bf54"],
Cell[BoxData[
RowBox[{
RowBox[{"ClearAll", "[", "zs", "]"}], ";",
RowBox[{"zs", "=",
RowBox[{"{",
RowBox[{"55", ",", "89", ",", "144"}], "}"}]}], ";"}]], "Input",
CellChangeTimes->{{3.6706330406927958`*^9, 3.670633053631119*^9},
3.670633688373909*^9},ExpressionUUID->"7f434747-5782-4f94-bbfc-\
d3a684dd0d67"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"Variance", "[", "zs", "]"}], "//", "N"}]], "Input",
CellChangeTimes->{{3.670633089512288*^9,
3.6706330968373747`*^9}},ExpressionUUID->"24018859-f0d6-4914-87e4-\
7e3438650682"],
Cell[BoxData["2017.`"], "Output",
CellChangeTimes->{{3.670633093456658*^9, 3.670633097174734*^9},
3.670633690725699*^9, 3.670775509661017*^9, 3.670872521748149*^9,
3.670956958300746*^9, 3.670974672753292*^9, 3.670979364485127*^9,
3.6710331345690928`*^9, 3.671033181524482*^9, 3.6710332592727947`*^9,
3.671039610574546*^9, 3.671042461680066*^9, 3.6710440430852222`*^9,
3.671065161517785*^9, 3.671083428079273*^9, 3.671208539364647*^9,
3.671213361253249*^9, 3.671217647110773*^9, 3.671218312123784*^9,
3.671380183894141*^9, 3.671467524827323*^9, 3.671494785039616*^9,
3.6715516633693666`*^9, 3.671563251255499*^9, 3.671587606060575*^9,
3.671654764985839*^9, 3.6716632389085093`*^9, 3.671665734259427*^9,
3.67166644236049*^9, 3.671887646468121*^9, 3.6719396133329077`*^9,
3.671974960157995*^9, 3.672154917691401*^9, 3.6722218115594597`*^9,
3.6723061268590317`*^9, 3.672308487518257*^9, 3.672311741159417*^9,
3.672311894044874*^9, 3.6724767288837547`*^9, 3.672833720783937*^9,
3.672911582086088*^9, 3.6735487735928497`*^9, 3.676478495146277*^9,
3.703192163424919*^9, 3.7032390294009533`*^9, 3.703239087790371*^9,
3.7036266858402643`*^9},ExpressionUUID->"952c73a9-3009-47a2-a400-\
b9f1a2794999"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[{
RowBox[{
RowBox[{"ClearAll", "[", "cume", "]"}], ";"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"cume", "[",
RowBox[{"var_", ",", "z_"}], "]"}], ":=",
RowBox[{"var", "+",
FractionBox[
SuperscriptBox[
RowBox[{"(",
RowBox[{"z", "-",
RowBox[{"Mean", "[", "zs", "]"}]}], ")"}], "2"],
RowBox[{
RowBox[{"Length", "[", "zs", "]"}], "-", "1"}]]}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"FoldList", "[",
RowBox[{"cume", ",", "0.", ",", "zs"}], "]"}]}], "Input",
CellChangeTimes->{{3.6704318784414883`*^9, 3.67043196611762*^9}, {
3.670432532993738*^9, 3.670432537354947*^9}, {3.670432609940963*^9,
3.670432641355281*^9}, 3.670633063383287*^9, {3.703239120069359*^9,
3.703239121060824*^9}},ExpressionUUID->"f56889e6-3cb8-4430-940b-\
61b73af80ec6"],
Cell[BoxData[
RowBox[{"{",
RowBox[{"0.`", ",", "840.5`", ",", "865.`", ",", "2017.`"}],
"}"}]], "Output",
CellChangeTimes->{{3.6704319573089523`*^9, 3.6704319667670937`*^9},
3.6704326420142603`*^9, 3.670434305670938*^9, 3.670460032828039*^9,
3.670510829243762*^9, 3.670511022354578*^9, 3.6705156387093897`*^9,
3.670532555004262*^9, 3.670533709693441*^9, 3.670534990651474*^9,
3.670545242096092*^9, 3.670568348584985*^9, {3.670633057017488*^9,
3.670633063753138*^9}, 3.670633692940551*^9, 3.6707755096874647`*^9,
3.670872521796035*^9, 3.670956958355507*^9, 3.6709746728028383`*^9,
3.6709793645344133`*^9, 3.6710331346184196`*^9, 3.671033181577526*^9,
3.671033259332559*^9, 3.671039610627647*^9, 3.671042461727104*^9,
3.671044043144093*^9, 3.6710651615772457`*^9, 3.671083428155424*^9,
3.671208539418248*^9, 3.6712133613009453`*^9, 3.671217647171185*^9,
3.6712183121730947`*^9, 3.6713801839443493`*^9, 3.671467524891789*^9,
3.6714947850972443`*^9, 3.671551663393964*^9, 3.671563251305648*^9,
3.671587606087097*^9, 3.67165476504527*^9, 3.671663238934552*^9,
3.671665734285263*^9, 3.671666442410055*^9, 3.671887646516275*^9,
3.671939613369315*^9, 3.671974960217766*^9, 3.672154917717157*^9,
3.672221811584959*^9, 3.672306126883164*^9, 3.672308487544251*^9,
3.672311741214449*^9, 3.6723118940696363`*^9, 3.67247672895965*^9,
3.67283372080483*^9, 3.672911582139677*^9, 3.673548773617812*^9,
3.6764784951966343`*^9, 3.703192166000389*^9, 3.703239087839944*^9,
3.703239121669447*^9,
3.703626685904263*^9},ExpressionUUID->"da83ffb1-f28a-4853-ac8b-\
b7f014d98bbd"]
}, Open ]],
Cell["\<\
Notice that the intermediate variances are not valid because they do not \
refer to the \[OpenCurlyDoubleQuote]mean-so-far,\[CloseCurlyDoubleQuote] but \
to the final mean.\
\>", "Text",
CellChangeTimes->{{3.7032585213756847`*^9,
3.703258550420412*^9}},ExpressionUUID->"0c52136b-d218-40cb-8105-\
1b667af864f4"]
}, Open ]],
Cell[CellGroupData[{
Cell["School Variance", "Subsection",
CellChangeTimes->{{3.670533688721724*^9, 3.670533692209456*^9}, {
3.670533849344036*^9, 3.670533850096394*^9},
3.67053480141547*^9},ExpressionUUID->"0adf919e-896d-456b-acd1-\
62acf4cc0549"],
Cell[TextData[{
"The following is much better, exploiting the \[OpenCurlyDoubleQuote]school \
formula,\[CloseCurlyDoubleQuote] which is easy to prove by expanding the \
square. Letting ",
Cell[BoxData[
FormBox[
RowBox[{
SubscriptBox[
OverscriptBox["z", "_"], "n"], "=", "x"}], TraditionalForm]],
ExpressionUUID->"94d392ea-fbdf-42ba-be96-dfdb54648cff"],
", our incremental running mean, the following yields the sum of squared \
residuals, ",
StyleBox["ssq", "Input"],
":"
}], "Text",
CellChangeTimes->{{3.670432227885901*^9, 3.6704322415912027`*^9}, {
3.670432290397142*^9, 3.670432347981297*^9}, {3.670432678931017*^9,
3.670432685303421*^9}, {3.670533861267909*^9, 3.670533862540182*^9}, {
3.703247773192071*^9, 3.7032477851577673`*^9}, {3.703255853882349*^9,
3.70325586014079*^9}, {3.703255904510639*^9, 3.703255933394547*^9}, {