# 3.4 Comparing Theoretical and Empirical Probabilities

Last updated: March 8th, 2020

# Comparing Theoretical and Empirical Probabilities¶

In the first section we introduced the concept of empirical probability as the relative frecuency of a given outcome.

Now that we know how to calculate some theoretical probabilities, let's perform some experiments and compare them!

Remember the last experiment of throwing a dice $5$ times. We calculated the probability of getting all ones as

In :
p_all_ones = (1/6) ** 5
p_all_ones

Out:
0.00012860082304526745

Now, let's model this experiment, and see what the relative frequency is:

In :
import random

In :
def throw_dice():
return [random.randint(1,6) for _ in range(5)]

In :
throw_dice()

Out:
[1, 3, 5, 1, 5]

Given $n$ throws, let's count how many are all ones.

In :
def count_all_ones(n):
amount_found = 0
for _ in range(n):
dice = throw_dice()
if dice == [1,1,1,1,1]:
amount_found +=  1
return amount_found

In :
def prob(n):
return count_all_ones(n)/n

In :
prob(1_000_000)

Out:
0.000145

We can see that the empirical probability gets closer to the theoretical probability as we throw the dice more times.

In :
abs(prob(1_000_000)-p_all_ones)

Out:
5.600823045267438e-06