# MeanAndVariance

### From charlesreid1

## Contents

## Summary

This page covers __mean__ and __variance__ definitions for continuous random variables (with prescribed probability density function) and discrete random variables (with prescribed probability mass function).

## Mean

### Continuous Random Variables

If we have a __continuous__ random variable with a probability __density__ function , the mean and variance are given by:

(where the integral is over the range of x values)

### Discrete Random Variable

The mean of a __discrete__ random variable with discrete values and a probability __mass__ function is given by the expression:

Note that by definition, the probability mass function must sum to 1:

If we assume a uniform probability for each value, then the probability mass function of component i is just:

## Variance

### Continuous Random Variable

The variance of a continuous random variable is given by:

Note that this can be expanded and simplified,

or just

which is equivalent to saying:

### Discrete Random Variable

If we have a discrete random variable with a probability mass function, the variance is given by:

This can be simplified to:

where the last term is equal to the mean,

## Updating On The Fly

### Updating Discrete Mean

Let's suppose we have n data values, , and we are adding one additional data value and want to compute the effect it has on the mean. For the sake of simplicity we assume that the probability mass function is uniform, so that or . Then old mean is given by:

and the new mean is given by:

We can re-express the new mean in terms of the old mean by the following relationship:

### Updating Discrete Variance

Supposing the same situation - that we have n discrete data values , and we are adding one additional data value and want to compute the effect it has on the variance. As with updating the mean, we presume a uniform mass density function for the sake of simplicity, or . Then the old variance is given by:

or,

(as before, the last term is the old mean; because the probability mass function must sum to 1, the sum of 1/n is the same as the sum of 1/n^2, so the mismatch in the two terms is not a problem). Grouping the first term as SS (sum of squares) for convenience,

Now we can write out the new expression for the variance as:

which can be written in terms of the old sum of squares term as:

Thus, if we want to start with a set of n points, and quickly evaluate the effect that adding one additional point would have (say, from a pool of possible "next choices"), we can implement the above formulas for a one-shot update.

Computer SciencePart of the 2017 CS Study Plan.
Builtin features of Java: Java/Exceptions Java/Generics Java/Iterators Java/Comparators Java/Numeric Documentation: Javadocs Tools and functionality: Java/URLs External libraries: Apache Commons OOP: OOP Checklist
· Template:CSFlag · e |

See also: