Compute Common statistics of stream Data


You are working on a system that receives a continuous stream of numerical data. The total volume of incoming data is extremely large and cannot be stored entirely in memory. Your task is to compute common statistics - specifically, the mean and variance - in an online fashion, i.e., update the statistics as each new data point arrives.
You must design a class or function that:
  • Accepts new data points one at a time from the stream.
  • Efficiently updates and returns the current mean and variance after each new point.
  • Uses constant memory (i.e., no storing the entire stream).

Example :

Let's say there is a incoming stream of [4 , 7, 13, 6]

  • Once 4 comes in mean = 4, variance = 0
  • Once 7 comes in mean = 5.5, variance = 2.25
  • Once 13 comes in mean = 8, variance = 14
  • Once 13 comes in mean = 7.5, variance = 11.25

Code Output