# Using MATLAB to work out coin-flip style probabilities

1. Mar 12, 2013

### Flucky

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.

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?

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.

#### Attached Files:

File size:
60.4 KB
Views:
507
File size:
42.4 KB
Views:
162
• ###### GamblerMan3.jpg
File size:
43.4 KB
Views:
536
Last edited: Mar 12, 2013
2. Mar 13, 2013

### BruceW

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)

3. Mar 13, 2013

### Flucky

EDIT

Hi Bruce, realised 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.

#### Attached Files:

• ###### the100pundman.jpg
File size:
29.6 KB
Views:
442
Last edited: Mar 13, 2013
4. Mar 14, 2013

### Flucky

Bump :(

5. Mar 16, 2013

### BruceW

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 realised that I had just declared one of my variables as the wrong type.