1

how to calculate mean and variance in online learning by matlab? suppose we have a stream of data that each time we receive only 40 of data. i want to update mean and variance of this data set by get each 40 data. I would like every time I get 40 data, I update mean and variance of the all data that received so far. please pay attention that I could not save all data and each time I can save only 40 data.

thanks a lot

lida
  • 137
  • 1
  • 9
  • You are looking for what is known as a rolling mean and rolling variance computation. The duplicate I've marked shows you how to compute both of these quantities. – rayryeng Dec 29 '15 at 08:21
  • yes, It is the same of [rolling-variance-algorithm](http://stackoverflow.com/questions/5147378/rolling-variance-algorithm) . thanks, I did not know that. – lida Dec 29 '15 at 11:49

1 Answers1

1

You might want to calculate a running mean and a running variance. There is a very good tutorial here:

http://www.johndcook.com/blog/standard_deviation/

With these algorithms you don't need to keep all values in memory.

Jorge Torres
  • 1,426
  • 12
  • 21
  • thanks a lot. but what solution is better: [blog/standard_deviation](http://www.johndcook.com/blog/standard_deviation/) or userOVER9000's response to this [rolling-variance-algorithm](http://stackoverflow.com/questions/5147378/rolling-variance-algorithm) ? Unfortunately, I understand only userOVER9000's response from [rolling-variance-algorithm](http://stackoverflow.com/questions/5147378/rolling-variance-algorithm). – lida Dec 29 '15 at 11:52
  • I think that both algorithms are similar. – Jorge Torres Dec 31 '15 at 06:58
  • @lida the one provided by userOVER9000 updates the mean and variance when there are N more observations available. The method above by John Cook updates the mean and variance when only one sample is present. You can loop through each new point and use John Cook's method to do adapt this for new points being available immediately. Use whichever method suits you best. – rayryeng Jan 01 '16 at 17:58