# Modifying a distribution by adding in samples incrementally

I would like to calculate the distribution (e.g., Gaussian) of a set of samples. However, I would also like to see how the distribution changes as I fit the samples into the distribution incrementally.

One way to do this would be to compute the distribution over all relevant samples every increment (e.g., first increment: calculate distribution of 2 samples, second increment: calculate distribution of 3 samples). However, this is computationally intensive.

Would I be able to calculate the distribution of 3 samples from solely the 3rd sample and the properties of the distribution of 2 previous samples?

For example, say I have 5 ordered samples.

• I start off by calculating the mean and standard deviation of the first 2 samples This is the 1st Gaussian distribution.
• I then look at the third sample, and fit it into the first Gaussian distribution (knowing the mean, std, number of samples). This is the 2nd Gaussian distribution.
• I then look at the fourth sample, and fit it into the 2nd Gaussian distribution (knowing the mean, std, number of samples). This is the 3rd Gaussian distribution.

Calculation of standard deviation on the fly is possible (turn to our brothers at math.stackexchange):

https://math.stackexchange.com/questions/198336/how-to-calculate-standard-deviation-with-streaming-inputs

It is easier to keep track of the variance and only take the square root to calculate the stdev when you really need it.

And the mean is even easier, don't overthink it. If you have 4 samples with a mean of 4 and 3 samples with a mean of 3, this totals up to 25 in 7 samples.

So

$$\mu = \frac{\mu_1 N_1 + \mu_2 N_2 + \mu_3 N_3}{N_1 + N_2 + N_3}$$

and

$$\sigma^2 = \frac{\sigma_1^2 N_1 + \sigma_2^2 N_2 + \sigma_3^2 N_3}{(N_1 + N_2 + N_3)^2}$$

• Thank you! Great answer and link. – Arnold Jan 17 at 22:00