MATH 314 Homework 03
Due 2025-09-19 by 11:59pm
Welford's online algorithm for computing the mean and variance/standard deviation work with just one data point at a time, as if the data were streaming in and you didn't have the memory or ability to store past data.
I prefer to write the math a bit differently than the Wikipedia page linked above, but to the same effect. Initialize
-
Write a Python class called
OnlineMoments
, which implements the following API:om = OnlineMoments() om.update(1) om.update(2) om.update(3) om.mean() om.var() om.count() om.size()
Note that the mathematics above track the biased variance, where as I'm asking for unbiased variance. The class method
var()
needs to make the appropriate conversion:The constructor should accept a size argument, which defaults to 1, that sets the number of means and variances to be tracked.
-
Test your code. Create an instance of the
OnlineMoments
class. Generate a numpy array of random numbers. Loop through the array one number at a time, calling the method update on each element of your array. Print the mean and variance you calculated. -
Compare the answers to what
np.mean()
andnp.var(..., ddof=1)
produce.