# Probability for a weighted coin flip

I have a problem I need to do for school. The class is an advanced course in R at my high school. Like the title says, I need to figure out probability for a weighted coin flip. I need to land on heads 3 times or more out of 6, in 80% of all trials. I think the best way to attack the problem is to run a simulation of millions of trials, and then give an approximate answer based on the number of times in those trials that the coin landed on heads. My teacher was not able to explain the problem in a way I could understand, so I got advice from a friend to try using the coin.flip function and simply adjust the weights until I get the right percentage, like so:

``````total = 1
flip = 0
right = 0
for (total in 1:1000000000){
flip = coin.flip(coins = 6, flips = 10000000, weights = c(0.5, 0.5), getExact)
if ((sum(flip)) <= 3) {
right = right + 1
}
total = total + 1
}
right
``````

...But, I think there must be an easier way. Can someone help me out?

Also, please take a look at our homework policy

Hi andresrcs. Thank you for replying. I have edited my question to help it fit into the guidelines set in these articles. I hate to ask, but how long do you think it will be before I get an answer? Obviously I am under time pressure as the assignment is due on Monday, and I won't have much time to work before then.

This site is not here for answering your homework, but if you generate a good enough google query for simulations in R you'll find plenty of examples along the lines of your example, coin flips being a very common example.

Alternatively you could try and solve the probability analytically. It's not clear if that is an acceptable solution or not here.

I have already tried solving the problem analytically. I am allowed to, just haven't been able to do it so far.

Ok, good to know you have already investigated solving it analytically. I have to admit it's been too many years for me to remember whether this could be done easily by hand.

However, there is a simple way to solve this without simulating the process millions of times. You just need to think about the probability distribution associated with coin flips (hint: it's the most simple one of all). This will then lead you to the set of functions in R (or Excel for that matter) which do the probability calculations for you.

1 Like

Yes, but the problem is that it's a weighted coin flip. If I knew the probability of heads, this would be an easy problem. Here's what I thought the problem was: 80% (success rate of trials) = 50% (success rate within each trial) * weight of heads. But that doesn't sound right since the answer would only be 40%. (In fact, now that I think about it, that might be right.)

You might be getting there yourself (which is the intention), but there are two problems:

1. Calculate the probability of weighted coin flips for which I have given you a hint in the previous post. Don't worry about the weighted part yet. If you can do this for any arbitrary p, then you can move on to the second stage.
2. Finding the correct parameter can be done by trial and error or you can optimise the function. Again, this can easily be searched for.

OK, so I tried to optimize the probability. I found an optimization function at https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/optimize:

``````prob = {0:1}
optimize(prob, lower = min(prob), upper = max(prob),
maximum = FALSE,
tol = 0.8)
``````

This command is meant to optimize a specific function (which I called prob). But I got an error:

``````Error in f(arg, ...) : could not find function "f"
``````

I know that function "f" is in place of where I have function "prob". I'm not sure what exactly I was supposed to type there.

You are getting closer. `optimise()` deals with the second part of the problem, but the more critical part is the first.

You first need to define your probability function (prob), which is based on the probability of getting >=3 successes out of 6 weighted coin flips for an input weight. Once you have that you have nearly solved the task. (This part you can do by hand assuming you have had any probability or stats classes, which would allow you to confirm the computational answer).

Next tweak the function so that it can be minimised, i.e. get very close to a target (in this case 0.8).

Once you have that you can look at using the `optimise` function. Hint: `tol` should either be left as the default or made a very small number. The 0.8 should be part of your prob function.

Good luck.

Working on the first part now. I should be able to get the number of possible combinations from an nCr function. I tried the one listed at https://www.rdocumentation.org/packages/rtemis/versions/0.79/topics/nCr:

`prob = (nCr(6,3)) / (2^6)`

And I got the error:

`Error in nCr(6, 3) : could not find function "nCr"`

I then checked to see if I needed a specific package for it, since that error happened before. I didn't find one. Any idea what went wrong?

Thank you for your help today. I really appreciate it and I'm not sure I would've figured this out otherwise.

Never mind, I figured it out. The function is called

`choose(6,3)` not `ncr(6,3)`

But I'm still having trouble with the optimization function. Can you help?

You are making progress with the probability function. What you have is the formula for exactly 3 out 6 successes of a fair coin, i.e. one with p(H) = 0.5.

You need to extend this for >=3 successes and for arbitrary p(H).

All of the above is available in a single built-in probability distribution function, but there is no harm in calculating it in long form.

Once you have that sorted you will be very close to the final solution. I'd urge you to concentrate on that before optimising.

OK. I was able to contact my teacher. He said to use the pbinom() function and approximate to 4 decimal places, so I used trial and error to get a probability of .7314. The code I used was:
`p = 1 - pbinom(3, size=6, prob=P(H))`
with P(H) as the unknown that I kept adjusting. I started at .5, then went up and down as necessary.

Sorry about this whole thing. It could have been easily avoided. Thank you again for all your help. You are much more helpful than the people on Stack Overflow

You are sooo close, but beware of what the first parameter of `pbinom(3, 6, prob)` means: it gives you the probability of <=3 successes, so `1 - pbinom(3, 6, prob)` gives you the probability of >3 successes (not the >= desired).

Therefore you need to change the formula to either
`1 - pbinom(2, 6, prob)` or
`sum(dbinom(3:6, 6, prob))` which you may find more intuitive.

Or if you really did want a simple simulation solution you could use this:
`mean(rbinom(1000000, 6, prob) >= 3)` (run it multiple times to get slightly different estimates)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.