DS Probability Basics

Probability measures how likely an event is to happen. The scale runs from 0 to 1. A value of 0 means the event cannot happen. A value of 1 means the event always happens. Data science uses probability to handle uncertainty, from spam filters to weather forecasts.

The Core Formula

                    Number of outcomes you want
Probability  =  ------------------------------------
                    Total number of possible outcomes

A fair die has six faces. The chance of rolling a 4 is 1 out of 6, or about 0.17. The chance of rolling an even number is 3 out of 6, or 0.5.

Key Terms in Plain Words

TermMeaningExample
ExperimentAn action with an uncertain resultRolling a die
OutcomeOne possible resultGetting a 5
Sample spaceThe list of all outcomes1, 2, 3, 4, 5, 6
EventA group of outcomes you care aboutGetting an even number

Rules You Will Use Often

Complement rule

The chance that an event does not happen equals 1 minus the chance that it happens. If rain has a 0.3 chance, no rain has a 0.7 chance.

Addition rule

For events that cannot occur together, add their chances. Rolling a 1 or a 2 has a chance of 1/6 plus 1/6, which equals 2/6.

Multiplication rule

For independent events, multiply their chances. Independent events do not affect each other. Two coin flips are independent. The chance of two heads is 0.5 times 0.5, which equals 0.25.

Diagram: Two Coin Flips

                 Start
              /          \
          Heads           Tails
          (0.5)           (0.5)
         /     \         /     \
     Heads    Tails   Heads    Tails
     (0.5)    (0.5)   (0.5)    (0.5)

 Outcomes:   HH 0.25    HT 0.25    TH 0.25    TT 0.25

Every path from the start to the bottom is one outcome. Multiply the numbers along a path to find its chance. The four chances add up to 1.

Conditional Probability

Conditional probability asks for the chance of an event after you learn something new. Write it as P(A given B).

A bag holds 3 red and 2 blue marbles. You draw one red marble and keep it out. The bag now holds 2 red and 2 blue marbles. The chance that the second draw is red equals 2 out of 4, or 0.5. The first draw changed the second draw, so the events are dependent.

Bayes Idea Made Simple

Bayes theorem updates a belief when new evidence arrives. Picture an email filter. Suppose 20 percent of all emails are spam. Suppose 90 percent of spam emails contain the word winner, and 5 percent of normal emails contain it. An email with the word winner arrives.

Out of 1000 emails:
  200 are spam    --> 180 contain "winner"
  800 are normal  -->  40 contain "winner"

Emails with "winner": 180 + 40 = 220
Chance of spam given "winner": 180 / 220 = about 0.82

The word raises the spam chance from 0.20 to 0.82. Naive Bayes classifiers use this exact idea at a large scale.

Expected Value

Expected value is the long-run average of a random result. Multiply each value by its chance and add the results. A game pays 10 points with a 0.2 chance and 0 points with a 0.8 chance. The expected value equals 10 times 0.2 plus 0 times 0.8, which equals 2 points per play.

Simulate Chance with Python

import random

trials = 100000
heads = 0
for i in range(trials):
    if random.random() < 0.5:
        heads += 1

print(heads / trials)

The printed value lands close to 0.5. More trials bring the result closer to the true chance. This behavior is called the law of large numbers.

Probability in Data Science

  • Classifiers output probabilities, such as 0.87 chance of fraud.
  • A/B tests rely on probability to judge whether a difference is real.
  • Forecasts express uncertainty with ranges instead of single guesses.
  • Random sampling lets small groups represent large populations.

Key Points

  • Probability runs from 0 to 1 and equals wanted outcomes divided by all outcomes.
  • Multiply chances for independent events, and adjust chances when events depend on each other.
  • Bayes theorem updates beliefs when new evidence appears.