<s>
Algorithms	B-Algorithm
for	I-Algorithm
calculating	I-Algorithm
variance	I-Algorithm
play	O
a	O
major	O
role	O
in	O
computational	B-Algorithm
statistics	I-Algorithm
.	O
</s>
<s>
A	O
key	O
difficulty	O
in	O
the	O
design	O
of	O
good	O
algorithms	O
for	O
this	O
problem	O
is	O
that	O
formulas	O
for	O
the	O
variance	O
may	O
involve	O
sums	O
of	O
squares	O
,	O
which	O
can	O
lead	O
to	O
numerical	B-Algorithm
instability	I-Algorithm
as	O
well	O
as	O
to	O
arithmetic	B-Algorithm
overflow	I-Algorithm
when	O
dealing	O
with	O
large	O
values	O
.	O
</s>
<s>
Using	O
Bessel	B-General_Concept
's	I-General_Concept
correction	I-General_Concept
to	O
calculate	O
an	O
unbiased	O
estimate	O
of	O
the	O
population	O
variance	O
from	O
a	O
finite	O
sample	O
of	O
n	O
observations	O
,	O
the	O
formula	O
is	O
:	O
</s>
<s>
Because	O
and	O
can	O
be	O
very	O
similar	O
numbers	O
,	O
cancellation	B-Algorithm
can	O
lead	O
to	O
the	O
precision	B-Architecture
of	O
the	O
result	O
to	O
be	O
much	O
less	O
than	O
the	O
inherent	O
precision	B-Architecture
of	O
the	O
floating-point	B-Algorithm
arithmetic	I-Algorithm
used	O
to	O
perform	O
the	O
computation	O
.	O
</s>
<s>
Thus	O
this	O
algorithm	O
should	O
not	O
be	O
used	O
in	O
practice	O
,	O
and	O
several	O
alternate	O
,	O
numerically	B-Algorithm
stable	I-Algorithm
,	O
algorithms	O
have	O
been	O
proposed	O
.	O
</s>
<s>
The	O
variance	O
is	O
invariant	O
with	O
respect	O
to	O
changes	O
in	O
a	O
location	O
parameter	O
,	O
a	O
property	O
which	O
can	O
be	O
used	O
to	O
avoid	O
the	O
catastrophic	B-Algorithm
cancellation	I-Algorithm
in	O
this	O
formula	O
.	O
</s>
<s>
In	O
any	O
case	O
the	O
second	O
term	O
in	O
the	O
formula	O
is	O
always	O
smaller	O
than	O
the	O
first	O
one	O
therefore	O
no	O
cancellation	B-Algorithm
may	O
occur	O
.	O
</s>
<s>
This	O
algorithm	O
is	O
numerically	B-Algorithm
stable	I-Algorithm
if	O
n	O
is	O
small	O
.	O
</s>
<s>
Techniques	O
such	O
as	O
compensated	B-Algorithm
summation	I-Algorithm
can	O
be	O
used	O
to	O
combat	O
this	O
error	O
to	O
a	O
degree	O
.	O
</s>
<s>
It	O
is	O
often	O
useful	O
to	O
be	O
able	O
to	O
compute	O
the	O
variance	O
in	O
a	O
single	B-General_Concept
pass	I-General_Concept
,	O
inspecting	O
each	O
value	O
only	O
once	O
;	O
for	O
example	O
,	O
when	O
the	O
data	O
is	O
being	O
collected	O
without	O
enough	O
storage	O
to	O
keep	O
all	O
the	O
values	O
,	O
or	O
when	O
costs	O
of	O
memory	O
access	O
dominate	O
those	O
of	O
computation	O
.	O
</s>
<s>
For	O
such	O
an	O
online	B-Algorithm
algorithm	I-Algorithm
,	O
a	O
recurrence	O
relation	O
is	O
required	O
between	O
quantities	O
from	O
which	O
the	O
required	O
statistics	O
can	O
be	O
calculated	O
in	O
a	O
numerically	B-Algorithm
stable	I-Algorithm
fashion	O
.	O
</s>
<s>
These	O
formulas	O
suffer	O
from	O
numerical	B-Algorithm
instability	I-Algorithm
,	O
as	O
they	O
repeatedly	O
subtract	O
a	O
small	O
number	O
from	O
a	O
big	O
number	O
which	O
scales	O
with	O
n	O
.	O
A	O
better	O
quantity	O
for	O
updating	O
is	O
the	O
sum	O
of	O
squares	O
of	O
differences	O
from	O
the	O
current	O
mean	O
,	O
,	O
here	O
denoted	O
:	O
</s>
<s>
An	O
example	O
Python	B-Language
implementation	O
for	O
Welford	O
's	O
algorithm	O
is	O
given	O
below	O
.	O
</s>
<s>
This	O
algorithm	O
is	O
much	O
less	O
prone	O
to	O
loss	O
of	O
precision	B-Architecture
due	O
to	O
catastrophic	B-Algorithm
cancellation	I-Algorithm
,	O
but	O
might	O
not	O
be	O
as	O
efficient	O
because	O
of	O
the	O
division	O
operation	O
inside	O
the	O
loop	O
.	O
</s>
<s>
note	O
that	O
Welford	O
's	O
online	B-Algorithm
algorithm	I-Algorithm
detailed	O
above	O
is	O
a	O
special	O
case	O
of	O
an	O
algorithm	O
that	O
works	O
for	O
combining	O
arbitrary	O
sets	O
and	O
:	O
</s>
<s>
Chan	O
's	O
method	O
for	O
estimating	O
the	O
mean	O
is	O
numerically	B-Algorithm
unstable	I-Algorithm
when	O
and	O
both	O
are	O
large	O
,	O
because	O
the	O
numerical	O
error	O
in	O
is	O
not	O
scaled	O
down	O
in	O
the	O
way	O
that	O
it	O
is	O
in	O
the	O
case	O
.	O
</s>
<s>
This	O
can	O
be	O
generalized	O
to	O
allow	O
parallelization	O
with	O
AVX	B-General_Concept
,	O
with	O
GPUs	B-Architecture
,	O
and	O
computer	B-Architecture
clusters	I-Architecture
,	O
and	O
to	O
covariance	O
.	O
</s>
<s>
Assume	O
that	O
all	O
floating	B-Algorithm
point	I-Algorithm
operations	O
use	O
standard	O
IEEE	O
754	O
double-precision	O
arithmetic	O
.	O
</s>
<s>
While	O
this	O
loss	O
of	O
precision	B-Architecture
may	O
be	O
tolerable	O
and	O
viewed	O
as	O
a	O
minor	O
flaw	O
of	O
the	O
naïve	O
algorithm	O
,	O
further	O
increasing	O
the	O
offset	O
makes	O
the	O
error	O
catastrophic	O
.	O
</s>
<s>
This	O
is	O
a	O
serious	O
problem	O
with	O
naïve	O
algorithm	O
and	O
is	O
due	O
to	O
catastrophic	B-Algorithm
cancellation	I-Algorithm
in	O
the	O
subtraction	O
of	O
two	O
similar	O
numbers	O
at	O
the	O
final	O
stage	O
of	O
the	O
algorithm	O
.	O
</s>
<s>
Terriberry	O
extends	O
Chan	O
's	O
formulae	O
to	O
calculating	O
the	O
third	O
and	O
fourth	O
central	B-General_Concept
moments	I-General_Concept
,	O
needed	O
for	O
example	O
when	O
estimating	O
skewness	B-General_Concept
and	O
kurtosis	B-Error_Name
:	O
</s>
<s>
An	O
example	O
of	O
the	O
online	B-Algorithm
algorithm	I-Algorithm
for	O
kurtosis	B-Error_Name
implemented	O
as	O
described	O
is	O
:	O
</s>
<s>
further	O
extends	O
these	O
results	O
to	O
arbitrary-order	O
central	B-General_Concept
moments	I-General_Concept
,	O
for	O
the	O
incremental	O
and	O
the	O
pairwise	O
cases	O
,	O
and	O
subsequently	O
Pébaÿ	O
et	O
al	O
.	O
</s>
<s>
offer	O
two	O
alternative	O
methods	O
to	O
compute	O
the	O
skewness	B-General_Concept
and	O
kurtosis	B-Error_Name
,	O
each	O
of	O
which	O
can	O
save	O
substantial	O
computer	O
memory	O
requirements	O
and	O
CPU	O
time	O
in	O
certain	O
applications	O
.	O
</s>
<s>
The	O
first	O
approach	O
is	O
to	O
compute	O
the	O
statistical	O
moments	O
by	O
separating	O
the	O
data	O
into	O
bins	O
and	O
then	O
computing	O
the	O
moments	O
from	O
the	O
geometry	O
of	O
the	O
resulting	O
histogram	O
,	O
which	O
effectively	O
becomes	O
a	O
one-pass	B-General_Concept
algorithm	I-General_Concept
for	O
higher	O
moments	O
.	O
</s>
<s>
One	O
benefit	O
is	O
that	O
the	O
statistical	O
moment	O
calculations	O
can	O
be	O
carried	O
out	O
to	O
arbitrary	O
accuracy	O
such	O
that	O
the	O
computations	O
can	O
be	O
tuned	O
to	O
the	O
precision	B-Architecture
of	O
,	O
e.g.	O
,	O
the	O
data	O
storage	O
format	O
or	O
the	O
original	O
measurement	O
hardware	O
.	O
</s>
<s>
After	O
this	O
normalization	O
,	O
the	O
raw	O
moments	O
and	O
central	B-General_Concept
moments	I-General_Concept
of	O
can	O
be	O
calculated	O
from	O
the	O
relative	O
histogram	O
:	O
</s>
<s>
are	O
then	O
used	O
to	O
compute	O
the	O
central	B-General_Concept
moments	I-General_Concept
of	O
the	O
concatenated	O
time-history	O
.	O
</s>
<s>
Finally	O
,	O
the	O
statistical	O
moments	O
of	O
the	O
concatenated	O
history	O
are	O
computed	O
from	O
the	O
central	B-General_Concept
moments	I-General_Concept
:	O
</s>
<s>
For	O
the	O
algorithm	O
above	O
,	O
one	O
could	O
use	O
the	O
following	O
Python	B-Language
code	I-Language
:	O
</s>
<s>
and	O
again	O
choosing	O
a	O
value	O
inside	O
the	O
range	O
of	O
values	O
will	O
stabilize	O
the	O
formula	O
against	O
catastrophic	B-Algorithm
cancellation	I-Algorithm
as	O
well	O
as	O
make	O
it	O
more	O
robust	O
against	O
big	O
sums	O
.	O
</s>
<s>
A	O
stable	O
one-pass	B-General_Concept
algorithm	I-General_Concept
exists	O
,	O
similar	O
to	O
the	O
online	B-Algorithm
algorithm	I-Algorithm
for	O
computing	O
the	O
variance	O
,	O
that	O
computes	O
co-moment	O
:	O
</s>
<s>
Even	O
greater	O
accuracy	O
can	O
be	O
achieved	O
by	O
first	O
computing	O
the	O
means	O
,	O
then	O
using	O
the	O
stable	O
one-pass	B-General_Concept
algorithm	I-General_Concept
on	O
the	O
residuals	O
.	O
</s>
