I have a task to use the Monte Carlo method to evaluate an unfair coin flip and determine the probability of obtaining n heads out of n flips within n simulations. The guide my professor provided (at bottom) isn't making sense for how to adjust it for an unfair coin. I've included what I started coding, but it's nowhere near complete. How can I adjust Monte Carlo to work for an unfair coin?
Problem:
Assume you have a coin that is not fair, where the probability of having a heads is p. We are interested in the probability of seeing exactly nheads heads out of a total of nflips flips. Write a function that takes these arguments to evaluate this probability over nsim simulations (a total of four arguments), and test it out on a few different values. Hint: use the prob argument in the sample function.
nsim <- 10000
nheads <-
nflips <-
p <- NULL
unfairfunc <- function(x){
for(nheads in 1:nsim)
nheads <-
nflips <-
}
Below is the base code my professor provided for Monte Carlo probabilities for a fair coin toss.
flip_function <- function(n) {
flips <- sample(c("heads", "tails"), n, replace=TRUE)
percent_heads <- length(which(flips== "heads")) / n
return(percent_heads)
}