# Function to simulate coin tosses
<- function(n) {
simulate_tosses set.seed(42) # Set seed for reproducibility
<- sample(c("Heads", "Tails"), n, replace = TRUE)
flips return(flips)
}
# Simulate coin flips for different scenarios
<- simulate_tosses(100)
flips_100 <- simulate_tosses(1000)
flips_1000 <- simulate_tosses(10000)
flips_10000 <- simulate_tosses(100000) flips_100000
Dive into the world of coin flip sequences using R simulations. Objective: Understand the probabilities of consecutive sequences of heads or tails. Simulating Coin Flips
Utilize R to simulate coin flips with varying numbers (100, 1000, 10000, 100000) for experimentation. simulate_tosses function generates sequences of “Heads” and “Tails.” Analyzing Consecutive Sequences
Define count_consecutive_sequences function to analyze lengths of consecutive sequences. Obtain counts for different scenarios: 100 tosses, 1000 tosses, 10000 tosses, and 100000 tosses. Visualizing the Results
Use ggplot2 to create line plots (create_plot function) illustrating consecutive sequence counts. Arrange plots with gridExtra to compare scenarios side by side. Results and Insights
Longer consecutive sequences are more likely with a larger number of tosses. Simulation provides valuable insights into the probability distribution of sequences. Conclusion
Probability exploration through simulation is a powerful tool. Whether a statistician, data scientist, or curious individual, simulations offer insights into chance. Encouragement to experiment with parameters and observe changes in results.
Introduction
In this blog post, we will dive into the fascinating world of coin flip sequences. Leveraging the capabilities of R, we will conduct simulations to gain insights into the probabilities of consecutive sequences of heads or tails.
Simulating Coin Flips
Let’s kick things off by simulating coin flips using R. We’ll conduct experiments with varying numbers of flips to observe how the results evolve.
Analyzing Consecutive Sequences
Moving on, we’ll analyze the consecutive sequences of heads or tails in each simulation. We’ll count the length of these sequences to gain insights into their distribution.
# Function to count consecutive sequences
<- function(flips) {
count_consecutive_sequences <- rep(0, length(flips))
count <- 1
counter
for (i in 2:length(flips)) {
if (flips[i] == flips[i-1]) {
<- counter + 1
counter else {
} <- 1
counter
}<- counter
count[i]
}
return(count)
}
# Function to create a plot for consecutive sequences
<- function(consecutive_counts, n) {
create_plot <- data.frame(Launch = 1:length(consecutive_counts), Consecutive = consecutive_counts)
data
library(ggplot2)
<- ggplot(data, aes(x = Launch, y = Consecutive)) +
plot geom_line() +
labs(title = paste("Consecutive Sequences -", n, "Tosses"),
x = "Toss Number",
y = "Consecutive Count") +
theme_minimal()
return(plot)
}
# Analyze consecutive sequences for different scenarios
<- count_consecutive_sequences(flips_100)
consecutive_counts_100 <- count_consecutive_sequences(flips_1000)
consecutive_counts_1000 <- count_consecutive_sequences(flips_10000)
consecutive_counts_10000 <- count_consecutive_sequences(flips_100000) consecutive_counts_100000
Visualizing the Results
Now, let’s create visualizations to better understand the distribution of consecutive sequences. We’ll use the ggplot2 library to craft insightful graphs and gridExtra to arrange them together.
# Create plots
<- create_plot(consecutive_counts_100, 100) plot_100
Warning: il pacchetto 'ggplot2' è stato creato con R versione 4.3.2
<- create_plot(consecutive_counts_1000, 1000)
plot_1000 <- create_plot(consecutive_counts_10000, 10000)
plot_10000 <- create_plot(consecutive_counts_100000, 100000)
plot_100000
# Use gridExtra to arrange the plots
library(gridExtra)
grid.arrange(plot_100, plot_1000, plot_10000, plot_100000, ncol = 2)
# Function to simulate coin tosses and plot the maximum consecutive sequence
<- function(n) {
simulate_and_plot_max_sequence # Simulate coin tosses
<- simulate_tosses(n)
flips
# Analyze consecutive sequences
<- count_consecutive_sequences(flips)
consecutive_counts
# Return the maximum consecutive sequence
return(max(consecutive_counts))
}
# Vector to store results
<- c()
max_sequences
# Perform simulations with increasing numbers of tosses
for (n_tosses in c(100, 2000, 10000, 100000)) {
<- simulate_and_plot_max_sequence(n_tosses)
max_sequence <- c(max_sequences, max_sequence)
max_sequences
}
# Plot the results
library(ggplot2)
<- data.frame(Num_Tosses = c(100, 2000, 10000, 100000), Max_Sequence = max_sequences)
data
<- ggplot(data, aes(x = Num_Tosses, y = Max_Sequence)) +
plot geom_line() +
labs(title = "Maximum Consecutive Sequence vs. Number of Tosses",
x = "Number of Tosses",
y = "Max Consecutive Sequence") +
theme_minimal()
print(plot)
Results and Insights
After simulating coin flips and analyzing consecutive sequences, we’ve gained valuable insights into the probability of encountering various sequences. As expected, with a larger number of flips, the likelihood of encountering longer consecutive sequences increases.
Conclusion Exploring probability through simulation is a powerful way to grasp the nuances of random processes. Whether you’re a statistician, data scientist, or simply curious, these simulations provide a fascinating glimpse into the world of chance.
Feel free to experiment with different parameters, such as the number of flips or the probability of getting heads or tails, and observe how the results change. Happy exploring!