Naldi Carrion - Fab Lab ESAN - Fab Futures - Data Science
Home About

< Home

Week 3 - 1st Class: Probability¶

The class begins with the basic idea: Probability tells us how likely something is to happen.

Typical examples:

  • Flipping a coin → there's a 50% chance of getting heads.
  • Drawing a card from the deck.
  • It will rain tomorrow.

What I should learn: Probability is a measure of uncertainty, It ranges from 0 (impossible) to 1 (certain)

Sample space and events¶

Before discussing formulas, we must identify:

  • Sample space (S): everything that can happen.
  • Event (E): what I want to happen.

Example:

S = {1, 2, 3, 4, 5, 6} E = “even number” = {2, 4, 6}

Always define what we are measuring. Errors in probability almost always occur due to not properly defining the sample space.

Classic Probability¶

favorable results / possible results Example: rolling a 6 on a die → 1/6 (probability).

Simulations with code (Python + Numpy)¶

In [1]:
import numpy as np
rolls = np.random.randint(1,7,10000)

We can draw a histogram: how many times each number appeared.

In [2]:
import matplotlib.pyplot as plt
plt.hist(rolls, bins=6)
Out[2]:
(array([1660., 1728., 1646., 1675., 1627., 1664.]),
 array([1.        , 1.83333333, 2.66666667, 3.5       , 4.33333333,
        5.16666667, 6.        ]),
 <BarContainer object of 6 artists>)
No description has been provided for this image

Probability Distribution¶

We can show:

  • Uniform distribution
  • Normal distribution
  • Other distributions commonly used in data science

Generates 10,000 simulated data points of a normal distribution with mean 0 and standard deviation 1.

In [3]:
x = np.random.normal(0, 1, 10000)
plt.hist(x, bins=50)
Out[3]:
(array([  1.,   4.,   1.,   5.,   8.,  12.,  23.,  23.,  37.,  49.,  86.,
         94., 152., 192., 213., 237., 305., 373., 414., 470., 544., 567.,
        598., 561., 611., 575., 549., 526., 460., 429., 373., 300., 259.,
        220., 185., 153., 104.,  99.,  53.,  51.,  23.,  21.,  16.,  10.,
          6.,   3.,   1.,   2.,   0.,   2.]),
 array([-3.5635679 , -3.41539945, -3.26723101, -3.11906256, -2.97089412,
        -2.82272567, -2.67455723, -2.52638878, -2.37822034, -2.23005189,
        -2.08188345, -1.93371501, -1.78554656, -1.63737812, -1.48920967,
        -1.34104123, -1.19287278, -1.04470434, -0.89653589, -0.74836745,
        -0.60019901, -0.45203056, -0.30386212, -0.15569367, -0.00752523,
         0.14064322,  0.28881166,  0.43698011,  0.58514855,  0.733317  ,
         0.88148544,  1.02965388,  1.17782233,  1.32599077,  1.47415922,
         1.62232766,  1.77049611,  1.91866455,  2.066833  ,  2.21500144,
         2.36316988,  2.51133833,  2.65950677,  2.80767522,  2.95584366,
         3.10401211,  3.25218055,  3.400349  ,  3.54851744,  3.69668589,
         3.84485433]),
 <BarContainer object of 50 artists>)
No description has been provided for this image

Joint events (compound probabilities)¶

Probability of A and B (intersection)¶

P(A∩B)=P(A)P(B) If one event does NOT affect the other, they multiply.

Assignment¶

P(A∪B)=P(A)+P(B)−P(A∩B)

Compound event simulations¶

Experiment: "Probability of obtaining a sum ≥ 10 when rolling two dice" What is the probability that when rolling two dice, the sum of both will be greater than or equal to 10? Three possible outcomes meet this condition:

  • 4 + 6
  • 5 + 5
  • 6 + 4
  • 6 + 5
  • 5 + 6
  • 6 + 6

In total: 6 favorable combinations out of 36 possible combinations.

Theoretical probability:

P(suma≥10) = 36/6 = 0.1666... ≈ 16.7%

In [4]:
import numpy as np
import matplotlib.pyplot as plt

# 1. Número de simulaciones
N = 10000  # puedes subirlo si quieres más precisión

# 2. Simular los dos dados
dado1 = np.random.randint(1, 7, N)
dado2 = np.random.randint(1, 7, N)

# 3. Calcular suma
suma = dado1 + dado2

# 4. Calcular probabilidad experimental
eventos_favorables = np.sum(suma >= 10)
prob_experimental = eventos_favorables / N

prob_experimental
Out[4]:
np.float64(0.1613)
In [5]:
prob_teorica = 6/36

print("Probabilidad teórica:", prob_teorica)
print("Probabilidad experimental:", prob_experimental)
print("Diferencia absoluta:", abs(prob_teorica - prob_experimental))
Probabilidad teórica: 0.16666666666666666
Probabilidad experimental: 0.1613
Diferencia absoluta: 0.0053666666666666585
In [ ]: