Get startedGet started for free

Calculating the mean and variance of a sample

Now that you're familiar with working with coin flips using the binom object and calculating the mean and variance, let's try simulating a larger number of coin flips and calculating the sample mean and variance. Comparing this with the theoretical mean and variance will allow you to check if your simulated data follows the distribution you want.

We've preloaded the binom object and the describe() method from scipy.stats for you, as well as creating an empty list called averages to store the mean of the sample variable and a variable called variances to store the variance of the sample variable.

This exercise is part of the course

Foundations of Probability in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

for i in range(0, 1500):
    # 10 trials of 10 coin flips with 25% probability of heads
    sample = ____.rvs(____, ____, size=____)
    # Mean and variance of the values in the sample variable
    averages.append(describe(sample).mean)
    variances.append(describe(sample).variance)
Edit and Run Code