Tossing Three Fair Coins in R: A Deep Dive into Probability and Statistics

Introduction to Tossing 3 Fair Coins in R: A Deep Dive

===========================================================

In this blog post, we’ll delve into the world of probability and statistics using R. We’ll explore how to simulate tossing three fair coins and calculate the expected value (E(X)) and variance (P(X=1)). Our journey will cover various concepts, including conditional probabilities, discrete random variables, and simulation.

What is a Discrete Random Variable?


In probability theory, a discrete random variable is a variable that can take on only a finite number of distinct values. The values are usually represented by numbers or symbols. For example, the outcome of tossing a coin can be either Heads (H) or Tails (T), making it a discrete random variable.

Understanding Expected Value


The expected value (E(X)) is a measure of the average value that a discrete random variable can take on over many trials. It’s calculated by multiplying each possible value by its probability and summing them up.

For a fair coin toss, there are only two outcomes: H and T. The probability of getting heads is 1/2 (or 0.5), and the same applies to tails. To calculate E(X) for three fair coins, we can use the formula:

E(X) = ∑x * P(x)

where x represents each possible outcome (H or T), and P(x) is its corresponding probability.

Expected Value of Three Fair Coins


Let’s analyze the possible outcomes when tossing three fair coins. We’ll represent heads with H and tails with T. The sample space for three coin tosses includes 8 combinations: HHH, HHT, HTH, THH, HTT, THT, TTH, and TTT.

The probability of each outcome is equal (1/8), as the coins are independent and fair. To calculate E(X), we’ll sum up the products of each outcome’s value and its probability:

E(X) = (3 * 1/8) + (2 * 1/8) + (1 * 1/8) + (0 * 1/8) = 3/8 ≈ 0.375

So, the expected value of tossing three fair coins is approximately 0.375 or 37.5%.

Expected Value Using Simulation in R


Now that we understand the concept of expected value, let’s simulate it using R. We can use the sample() function to generate random outcomes for each coin toss.

# Set the number of experiments (trials)
noOfCoinTosses = 3;
noOfExperiments = 5;

# Initialize an empty vector to store the results
mySamples <- replicate(noOfExperiments,
                      { 
                        # Simulate a single coin toss
                        mySample <- sample(c("H", "T"), noOfCoinTosses, replace = T, prob=c(0.5, 0.5))
                        # Count the number of heads
                        headCount <- length(which(mySample == "H"))
                      }
                    )

# Calculate the probability (frequency / nr of experiments)
probOfCoinToss <- length(which(mySamples == 1)) / noOfExperiments   # 1.6
meanOfCoinToss <- length(which(headCount == 1))

Note that our simulation code has a small error. Instead of using mySamples directly, we should use the headCount variable to count the number of heads.

# Calculate the probability (frequency / nr of experiments)
probOfCoinToss <- length(which(headCount == 1)) / noOfExperiments   # 1.6
meanOfCoinToss <- mean(headCount, na.rm = TRUE)  # corrected mean calculation

Variance and Standard Deviation


The variance (P(X=1)) is a measure of the spread or dispersion of a discrete random variable’s values. It’s calculated by finding the average of the squared differences between each value and the expected value.

For our case, we’ll need to calculate the probability of getting exactly one head in three coin tosses using the binomial distribution formula:

P(X=k) = (n * C(k, n)) * p^k * q^(n-k)

where:

  • P(X=k) is the probability of k successes
  • n is the number of trials (coin tosses)
  • C(k, n) is the binomial coefficient (number of combinations)
  • p is the probability of success (getting heads)
  • q is the probability of failure (getting tails)

Variance Using Simulation in R


To estimate the variance using simulation, we can repeat the process many times and calculate the average squared difference between each outcome’s value and the expected value.

# Initialize an empty vector to store the results
varianceResults <- replicate(noOfExperiments,
                              {
                                # Simulate a single coin toss
                                mySample <- sample(c("H", "T"), noOfCoinTosses, replace = T, prob=c(0.5, 0.5))
                                # Count the number of heads
                                headCount <- length(which(mySample == "H"))
                                # Calculate squared difference from expected value (3/8)
                                varianceResult <- (headCount - 3/8)^2
                              }
                            )

# Calculate the average variance
varianceMean <- mean(varianceResults, na.rm = TRUE)

Note that this code estimates the variance by calculating the squared differences between each outcome’s value and the expected value. The expected value is used as a reference point to calculate these differences.

Conclusion


In this blog post, we explored how to toss three fair coins using R and calculate the expected value (E(X)) and variance of such a scenario. We used simulation to estimate the expected value and variance by repeating the process many times.


Last modified on 2025-03-16