Question

The statement rbinom(n,1, p) makes a sample of size n from a Bernoulli distribu- tion with success probability p. If you wanted to simulate n independent tosses of a fair coin with success probability p, you could use this command to do it. The successes would appear as 1s, and the failures as 0s. Suppose that you have a gambling game which pays off 2 when a coin is flipped and comes up heads and in which each toss costs 1 (regadless of whether you win or not) The coin is fair if the probability of getting a head is p0.5, but it could be unfair if p took on some other value. Write a function that simulates playing this game which starts with the player having 10 and runs until they become bankrupt (i.e. their total winnings become 0). The program should tel you when the bankruptcy happened (on which toss) and also plot out the path of winnings from 10 ui bankruptcy. Run the program and produce output for p- 0.5, p 17/35, and p 1/3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

RQUIRED R CODE:

x<-10
y<-c(10)
game<-function(p)
{
set.seed(20*p)
while(x>0)
{
z<-runif(1)
if(z<=p)
{
x<-x+1
y<-c(y,x)
}
else
{
x<-x-1
y<-c(y,x)
}
}
print(length(y)-1)
plot(y)
lines(y)
}
set.seed(100)
game(.5)
game(17/35)
game(1/3)

________________________________

OUTPUT FOR TOSS AT WHICH BANKRUPTCY HAPPEN IS

FOR P 0.5 IS 522

FOR P 17/35 IS 322

FOR P 1/3 IS 22

_________________________

PLOTS ARE

FOR P 0.50 100 300 400 500 600 Index

FOR P 17/35 IS

FOR P 1/3 IS

____________________________________________

DON'T FORGET TO RATE THE ANSWER BY THUMBS UP TO SUPPORT MY WORK.

THANKS FOR ASKING THE PROBLEM .

CHEERS!

HAPPY CHEGGING!

Add a comment
Know the answer?
Add Answer to:
The statement rbinom(n,1, p) makes a sample of size n from a Bernoulli distribu- tion with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT