MATH 314 Homework 02
Due 2026-09-04 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 (and thus you) prefer to write the math a bit differently than the
Wikipedia page linked above, but to the same effect. Initialize
Update these variables with a new
observation
-
Write a Python class called
OnlineMoments, which implements the following API:om = OnlineMoments() om.update(1) # does the calculations above 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
OnlineMomentsclass. Generate a numpy array of random numbers. Loop through the array one number at a time, calling theupdatemethod 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.