Monte Carlo Simulation of Bernoulli Trials in R
Last Updated :
16 Mar, 2023
A Monte Carlo simulation is a statistical method used to generate random sample sets to study and model the behavior of a system. The Bernoulli trial is a type of discrete probability experiment where the outcome can only be two possible results, either success or failure.
- Probability of success (p): This is the probability of obtaining a “success” outcome in a single trial of a Bernoulli experiment. It can range from 0 to 1, where 0 represents impossible and 1 represents certain success. For example, if a coin has a 50% chance of landing on heads, the probability of success is 0.5.
- The number of trials (n): This is the number of times the Bernoulli experiment will be performed. For example, if you flip a coin 10 times, the number of trials is 10.
- Bernoulli distribution: The Bernoulli distribution is a discrete probability distribution that models the outcome of a single trial of a Bernoulli experiment. It is a two-point distribution with a probability of success (p) for the “success” outcome and a probability of 1-p for the “failure” outcome. The distribution models the number of successes in n trials, which follows a binomial distribution.
Simulating the outcome of flipping a fair coin 10 times. A histogram showing the frequency of the number of heads in 10 coin flips. The distribution should be symmetrical and centered around 5 since each flip has a 50-50 chance of being heads or tails.
- Define the probability of success (p).
- Define the number of trials (n).
- Simulate the Bernoulli trials using rbinom() function in R Programming Language.
- Plot the results using hist() or barplot() functions.
R
p <- 0.5
n <- 10
results <- rbinom (1000, size = n, prob = p)
hist (results, main = "Coin Flip Simulation" ,
xlab = "Number of Heads" , col = "blue" )
|
Output :
Simulating the outcome of 10 people taking a test with a 70% passing rate. A histogram showing the frequency of the number of people passing the test in a group of 10. The distribution should be skewed to the right with more values closer to 7 (70% passing rate) and fewer values closer to 0 or 10.
R
p <- 0.7
n <- 10
results <- rbinom (1000, size = n, prob = p)
hist (results, main = "Test Passing Simulation" ,
xlab = "Number of Passes" , col = "blue" )
|
Output:
Simulating the outcome of 100 patients receiving a new medication with a success rate of 80%. A histogram showing the frequency of the number of successful treatments in 100 patients. The distribution should be skewed to the right with more values closer to 80 (80% success rate) and fewer values closer to 0 or 100.
R
p <- 0.8
n <- 100
results <- rbinom (1000, size = n, prob = p)
hist (results, main = "Medication Success Simulation" ,
xlab = "Number of Successful Treatments" ,
col = "blue" )
|
Output :
Monte Carlo Simulations for unbiased coin toss
Simulating the outcome of a biased coin flip with a 60% chance of landing on. A histogram showing the frequency of the number of heads in 10 coin flips. The distribution should be skewed to the right with more values closer to 6 (60% chance of heads) and fewer values closer to 0 or 10.
R
p <- 0.6
n <- 10
results <- rbinom (1000, size = n, prob = p)
hist (results, main = "Biased Coin Flip Simulation" ,
xlab = "Number of Heads" ,
col = "blue" )
|
Output :
Monte Carlo Simulations for Biased coin toss
Share your thoughts in the comments
Please Login to comment...