2025-09-20

Introduction to Probability and Visualizations using R

Probability is simply the likelihood that an event will occur given certain parameters.

Foundational Concepts

  • Experiment – is the action or process with an observable outcome.

    • Examples: flipping a coin, rolling dice, removing a colored ball from a box, etc.
  • Sample space(S) – The set of all possible outcomes of an experiment.

    • Example: Head or Tails, 1, 2, 3, 4, 5, 6 for the roll of a die, removing the blue or red ball from the box.
  • Event(E) – an instance of the sample space.

    • Example: flipping the coin once,rolling the dice once and receivinge a given number, or removing the blue ball from the box.
  • Probability of an Event P(E) – The likelihood that a specified event occurs.

Probability Formula

  • P(E)=Number of desired outcomes/Total number of possible outcomes

    • Example: Flipping a coin and getting heads

      • P(heads)=\(1/2\)=0.5

      • Number of desired outcomes = 1. The only outcome we are looking for is heads.

      • Total number of possible outcomes = 2. The options are heads and tails.

Types of Probability

  • Theoretical Probability: Based on reasoning or calculation

    • Example: Roll an even number on a dice. \(\rightarrow\) P(even) = 3/6 = 0.5
  • Experimental Probability: Based on actual experiments.

    • Example: Roll a dice 10 times, get the number 6 three times \(\rightarrow\) P(6)=3/10
  • Subjective Probability: Based on belief or experience.

    • Example: I think there is a 70% our football team will win tonight’s game.

Rules of Probability

  • Rule 1: \(0 \le P(E) \le 1\)
    • Probability can neither be negative or greater than 1.
  • Rule 2: Sum of probabilities of all outcomes =1.
    • Example: Coin \(\rightarrow\) \(1/2+1/2=1\)

    • Example: Dice \(\rightarrow\) \(1/6+1/6+1/6+1/6+1/6+1/6=1\)

  • Complement Rule: \(P(E')=1-P(E)\)
    • The probability that a desired event does not happen.

    • Example: Probability of not rolling a 1 or 2 \(\rightarrow\) \(1-1/6-1/6=4/6\)

Rules of Probability

  • Addition Rule (for mutually exclusive events):
    • \(P(A \text{ or } B) = P(A) + P(B)\)
  • Multiplication Rule (for independent events):
    • \(P(A \text{ and } B)=P(A) \cdot P(B)\)
  • Conditional Probability
    • The probability of A happening given B has occurred.

    • \(P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}\)

  • Probability Visualization

    • There are a number of ways to visualize probability.

    • To compare probabilities of discrete events, use a bar or pie chart.

    • To show step-by-step possibilities for multiple events, use a tree-diagram.

    • To show overlapping events (union/intersection), use a Venn diagram.

    Example 1

    • What is the probability of rolling a 6 when rolling a die?

      library(ggplot2)
      library(formatR)
      
      df <- data.frame( 
        roll = 1:6, probability = rep(1/6,6)
      )
      ggplot(df, aes(x=factor(roll), y=probability, 
                     fill = roll ==6)) +
        geom_bar(stat = "identity") +
        scale_fill_manual(
          values = c("#48D1CC","#98FF98")) +
        labs(x = "Rolled Value",  y = "Probability", 
             title = "Probability of Rolling a 6") +
        theme_get()

    Example 1 Continued…

    Example 2:

    • What is the probability of rolling a dice 100 times? How many times did you roll a 6?

      [1] 18
      [1] 0.18

    Example 3:

    If you flip a coin 150 times how many times will you get the observation of Tails?