# # A function to calculate ruin probabilities. # Function illusttrate the ifelse flow control structure. # It considers the case of the fair coin toss separately # from the biased coin toss. # It contains no error checking, say to test that # p is between 0 and 1, but this you can add with more "if statements." # # win(p, A, B) # # The probability of winning A dollars before losing B dollars given that you # have a probability of winning of p on each round of betting. win<-function(p, A, B){ if (p==0.5) return(B/(A+B)) else { q=1-p; N=(q/p)^(B)-1; D=(q/p)^(A+B)-1; return(N/D)} } # Incidentally there are some things you might want to explore # using this code. For example, what happned if you let p be very close to 0.5 # do you recover the simpler formula for ruin in a fair game?