Using MATLAB to work out coin-flip style probabilities

4 replies · 5K views · last reply
Flucky
Messages
93
Reaction score
1
I've basically been set the task below however I've not really been taught how to go about it. I think I know the general theory of how to go about it but the actual commands and putting it all together bamboozles me. The question is attatched.

Text problem: a gambler starts with one pound, wins or loses one pound per game with equal probability, stops at zero or at a goal of 100 pounds. Asks the probability of winning.

Just seen the typo, it should be 'loses' not 'looses'. Rookie mistake!

My thinking is that you use the random number feature to get a value between 0 and 1. If it comes out greater than 0.5 it adds a pound (£) to the pot and if it comes out less than 0.5 it removes a £ from the pot (starting off with only £1). You then use the loop feature to continue doing this until/if he reaches £100. However I've not the slightest clue how to measure the probability or even run multiple loops to get an average probability.

The furthest I've got with this problem is:

a=rand % where a is the 50/50 flip
x=1 % where x is the current total amount of money he has


if a<0.5
x=x+1

if a>0.5
x=x-1
end
end


but even when I run that simple script, if the random number comes out less than 0.5 it doesn't remove a £ it just remains at 1.

Any insight on how to solve this would be greatly appreciated as my MATLAB knowledge is poor.

-----------EDIT-------------------------------------------------------------------------------------------

I think I'm getting closer, does this look promising?

Screenshot of the MATLAB Editor showing a script with while loop and if/else logic for a coin-flip money simulation, with a rand call and counters x and n.


Here it stops the script if he runs out of money and also stops when he reaches his target of £100. Now I've hit a wall and don't have a clue how to continue with the question, I haven't even addressed the probability side of it.
 
Attachments
  • Text problem: a gambler starts with one pound, wins or loses a pound each play with equal probability, stops at zero or at a goal of £100; asks for the probability of winning.
    GamblerMan.jpg
    30.8 KB · Views: 1,181
  • Screenshot of the MATLAB editor showing a script named the100poundman.m: a=rand, x=1, n=1 initialised, then a while x<100 loop with an if a<0.5 branch adding 1 to x and n, else subtracting 1 from x and adding 1 to n.
    GamblerMan2.jpg
    39.8 KB · Views: 777
  • Screenshot of a MATLAB editor script with line numbers 1 to 24: a=rand, x=1, n=1, then a while x<100 loop with an if a<0.5 branch incrementing x and n, else decrementing x and n, and a nested if x<=0 resetting n and x to 1.
    GamblerMan3.jpg
    43.4 KB · Views: 1,260
Last edited:
hmm. Well really it resets n and x when he hits zero pounds. It doesn't exit the loop. But, that's not necessarily a bad thing. You can make the algorithm this way, you do need to add some more things to it though.

For the probability side of the question: think, for each 'experiment', the gambler either goes bust ('loses'), or reaches his target ('wins'). And you observe a number of these 'experiments'. Now, if you recorded the outcome of each experiment, and how many experiments happened in total, then what would you 'predict' is the probability of the next experiment being a 'success' or 'loss' ? (The answer is intuitive).

hint: think of the extreme case. If he won 99 times out of 100, you'd assume the next experiment is very likely to be a win, right? So exactly what probability should we predict? And then how does this generalise for any given number of wins (w) and total experiments (T)
 
brucew said:
hmm. Well really it resets n and x when he hits zero pounds. It doesn't exit the loop. But, that's not necessarily a bad thing. You can make the algorithm this way, you do need to add some more things to it though.

For the probability side of the question: Think, for each 'experiment', the gambler either goes bust ('loses'), or reaches his target ('wins'). And you observe a number of these 'experiments'. Now, if you recorded the outcome of each experiment, and how many experiments happened in total, then what would you 'predict' is the probability of the next experiment being a 'success' or 'loss' ? (the answer is intuitive).

Hint: Think of the extreme case. If he won 99 times out of 100, you'd assume the next experiment is very likely to be a win, right? So exactly what probability should we predict? And then how does this generalise for any given number of wins (w) and total experiments (t)

EDIT

Hi Bruce, realized what you meant earlier today so decided to change my script a little. For some reason when I used 'while 0<x<100' it would still give me negative x values and so I ditched that effort and used two 'for' loops. I'm planning on running 1000 "sessions" ie run the experiment 1000 times recording whether he wins or loses. From that it will give me however many wins vs however many losses and like you said, from that I can work out the probability. I think the final hurdle for this is getting the whole running 1000 loops thing. I'm not sure what to put in, in order to get a total amount of wins/losses out of 1000.

I've tried adding 's=s+1' (where s stands for session) in various places within the loops but can't get it to work. What is needed in the following script?

Apologies for the lousy wording.

Screenshot of a MATLAB editor showing script the100poundman.m with commented variables a, x, n, win and loss, a nested for loop simulating coin flips, and break conditions when x reaches 100 or 0.
 
Attachments
  • Screenshot of a MATLAB editor script simulating a coin flip gambler starting with £1, with comments on each variable and nested for and if loops.
    the100pundman.jpg
    29.6 KB · Views: 982
Last edited:
Nice work. It is pretty much correct except for a few mistakes. For the 's loop', you have put "for s=1000" but I think you meant to type "for s=1:1000". Also, when you check for x==100 and x==0, they are both inside the else part. This means your program will only do the check when a>0.5.

Another thing is that in your program, if x gets to 100 (or zero), then the next session will start with that value, which is not what you want the program to do. I'll leave it to you to think of fixes for these problems. p.s. I always make mistakes the first time I write code. The other day I was puzzled for ages about why my program was doing something strange, then I realized that I had just declared one of my variables as the wrong type.