http://www.mathworks.com/matlabcentral/newsreader/view_thread/294580
MATLAB Central Newsreader  Help with Monte Carlo Simulation
Feed for thread: Help with Monte Carlo Simulation
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 22 Oct 2010 18:09:04 +0000
Help with Monte Carlo Simulation
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294580#789942
Rami
Hi, I'm pretty new to matlab and I need to program a Monte Carlo simulation where 3 players have a number of coins. They decide to play a game where they flip coins and if the result ends up as 2 heads/tails and 1 tails/heads, the person with the 1 tails/heads result takes one coin from the other 2 player and keeps his or her coin. If the result is 3 heads or 3 tails then there is no change in any players coins. I need to figure out the average number of coin flips it takes to make one player go to zero. So far this is the code and I haven't tried to compute the average yet but my problem is that if I make n>1 then sometimes the code won't compute and it gets stuck at either "if X(3,1)==X(2,1)~=X(1,1)" which is line 29 or at line 50 which is just an "end" statement. If someone could help me out with what is wrong and any other advice it would be appreciated greatly. Thank you.<br>
<br>
function avgflips = going_broke(initP1, initP2, initP3, p, n)<br>
Y=zeros(3,1);<br>
Y(1,1)=initP1;<br>
Y(2,1)=initP2;<br>
Y(3,1)=initP3;<br>
X=zeros(3,1);<br>
for i=1:n<br>
c=1;<br>
j=1;<br>
while j>0<br>
X(1,1)=rand;<br>
if X(1,1)>p<br>
X(1,1)=1;<br>
elseif X(1,1)<p<br>
X(1,1)=0;<br>
end<br>
X(2,1)=rand;<br>
if X(2,1)>p<br>
X(2,1)=1;<br>
elseif X(2,1)<p<br>
X(2,1)=0;<br>
end<br>
X(3,1)=rand;<br>
if X(3,1)>p<br>
X(3,1)=1;<br>
elseif X(3,1)<p<br>
X(3,1)=0;<br>
end<br>
if X(3,1)==X(2,1)~=X(1,1)<br>
Y(1,1)=Y(1,1)+2;<br>
Y(2,1)=Y(2,1)1;<br>
Y(3,1)=Y(3,1)1;<br>
elseif X(1,1)==X(3,1)~=X(2,1)<br>
Y(1,1)=Y(1,1)1;<br>
Y(2,1)=Y(2,1)+2;<br>
Y(3,1)=Y(3,1)1;<br>
elseif X(1,1)==X(2,1)~=X(3,1)<br>
Y(1,1)=Y(1,1)1;<br>
Y(2,1)=Y(2,1)1;<br>
Y(3,1)=Y(3,1)+2;<br>
elseif X(1,1)==X(2,1)==X(3,1)<br>
Y(1,1)=Y(1,1);<br>
Y(2,1)=Y(2,1);<br>
Y(3,1)=Y(3,1);<br>
end<br>
if Y(1,1)==0<br>
j=0;<br>
elseif Y(2,1)==0<br>
j=0;<br>
elseif Y(3,1)==0<br>
j=0;<br>
end<br>
c=c+1;<br>
end<br>
end

Fri, 22 Oct 2010 21:41:03 +0000
Re: Help with Monte Carlo Simulation
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294580#790011
Roger Stafford
"Rami " <akatsukiX2@gmail.com> wrote in message <i9sk00$ghf$1@fred.mathworks.com>...<br>
> ..... If someone could help me out with what is wrong and any other advice it would be appreciated greatly. Thank you.<br>
> ......<br>
          <br>
I see three serious mistakes in your coding. The first one is that the expressions "X(3,1)==X(2,1)~=X(1,1)", "X(1,1)==X(3,1)~=X(2,1)", "X(1,1)==X(2,1)~=X(3,1)", and "X(1,1)==X(2,1)==X(3,1)" are not acting the way you are intending them to act. The first three are equivalent and are true whenever there is an even number of ones (trues) in the three X's. The last one is true whenever there is an odd number of ones. This means that half the time player Y1 wins two coins and the other half noone wins, a decidedly unfair game.<br>
<br>
If a, b, and c are logical quantities, the expression a==b~=c is interpreted as (a==b)~=c and this has the effect of saying that an even number of them are true  either all are false, or else two of them true and the other one false. Try all eight cases on your computer and see. What I think you want for Y1 to win is (X(2)==X(3)) & (x(1)~=X(2)), and analogously for Y2 and Y3. Also it is unnecessary to test for the case when noone wins since the Y's will not change unless you tell them to change.<br>
<br>
The second error is that after the whileloop exits you don't store the count in 'c' anywhere. Store it in an array indexed by the 'i' value in your forloop. You need this to compute the mean (average) value when you're ready to exit.<br>
<br>
The third error is that you don't restore the coin counts in the Y's when you begin a new run in the game. You should move the Y initialization down into the beginning of the forloop where you are already resetting c and j. Otherwise on subsequent passes you will be starting with one player having no coins. You could get negative coin counts this way.<br>
<br>
You will eventually learn that you can accomplish many things more efficiently by dealing with entire vectors. For example in simulating a coin toss using the X's you could have done this:<br>
<br>
X = (rand(3,1)>p);<br>
<br>
and it would accomplish what you have taken 18 lines to do. (By the way, if p is not equal to 1/2, those must be rather strange coins!)<br>
<br>
Roger Stafford

Sat, 23 Oct 2010 04:28:03 +0000
Re: Help with Monte Carlo Simulation
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294580#790050
Rami
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i9t0df$ato$1@fred.mathworks.com>...<br>
> "Rami " <akatsukiX2@gmail.com> wrote in message <i9sk00$ghf$1@fred.mathworks.com>...<br>
> > ..... If someone could help me out with what is wrong and any other advice it would be appreciated greatly. Thank you.<br>
> > ......<br>
>           <br>
> I see three serious mistakes in your coding. The first one is that the expressions "X(3,1)==X(2,1)~=X(1,1)", "X(1,1)==X(3,1)~=X(2,1)", "X(1,1)==X(2,1)~=X(3,1)", and "X(1,1)==X(2,1)==X(3,1)" are not acting the way you are intending them to act. The first three are equivalent and are true whenever there is an even number of ones (trues) in the three X's. The last one is true whenever there is an odd number of ones. This means that half the time player Y1 wins two coins and the other half noone wins, a decidedly unfair game.<br>
> <br>
> If a, b, and c are logical quantities, the expression a==b~=c is interpreted as (a==b)~=c and this has the effect of saying that an even number of them are true  either all are false, or else two of them true and the other one false. Try all eight cases on your computer and see. What I think you want for Y1 to win is (X(2)==X(3)) & (x(1)~=X(2)), and analogously for Y2 and Y3. Also it is unnecessary to test for the case when noone wins since the Y's will not change unless you tell them to change.<br>
> <br>
> The second error is that after the whileloop exits you don't store the count in 'c' anywhere. Store it in an array indexed by the 'i' value in your forloop. You need this to compute the mean (average) value when you're ready to exit.<br>
> <br>
> The third error is that you don't restore the coin counts in the Y's when you begin a new run in the game. You should move the Y initialization down into the beginning of the forloop where you are already resetting c and j. Otherwise on subsequent passes you will be starting with one player having no coins. You could get negative coin counts this way.<br>
> <br>
> You will eventually learn that you can accomplish many things more efficiently by dealing with entire vectors. For example in simulating a coin toss using the X's you could have done this:<br>
> <br>
> X = (rand(3,1)>p);<br>
> <br>
> and it would accomplish what you have taken 18 lines to do. (By the way, if p is not equal to 1/2, those must be rather strange coins!)<br>
> <br>
> Roger Stafford<br>
<br>
Ya I realized the problem about both not restoring the Y's and the fact that I made p not equal to 1/2! Thank you for the help, especially the part about how I needed to separate the conditions for each player to win I don't think I would have figured that part out by myself.