<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Puzzler: Coin game</title>
	<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/</link>
	<description>&#60;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=982174&#38;objectType=author"&#62;Doug Hull&#60;/a&#62; is an Application Engineer at &#60;a href="http://www.mathworks.com"&#62;The MathWorks&#60;/a&#62;. This blog is dedicated to promoting the &#60;a href="http://www.mathworks.com/matlabcentral/fileexchange/"&#62;File Exchange&#60;/a&#62; by highlighting files and original video content.&#60;/p&#62;&#60;a href="/images/pick/doug-full.jpg"&#62;&#60;img src="/images/pick/doug.jpg" width="164" height="282" border="0"&#62;&#60;/a&#62;&#60;br /&#62;&#60;br /&#62; &#60;a href="http://blogs.mathworks.com/pick/how-to-get-help/"&#62; Get help here &#60;/a&#62;</description>
	<pubDate>Mon, 08 Sep 2008 03:43:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: craig</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-12478</link>
		<dc:creator>craig</dc:creator>
		<pubDate>Tue, 19 Aug 2008 05:46:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-12478</guid>
		<description>Output: x

p = [.25 .25 .5 .75 ; .5 0 0 .25 ; .25 .5 0 0 ; 0 .25 .5 0 ];
[P D] = eig(p);
D=(abs(D)&#62;.99).*D;
x = real(P * D * P^(-1) * [1 ; 0 ; 0 ; 0]);

The idea here is to use the eigenvalue decomposition, noting that the eigenvalues with norm less than 1 will vanish.  Remove those eigenvalues and reassemble the matrix.  Multiply by the starting vector and you have the result.  This is accurate to machine precision.  The real is to remove the roundoff imaginary components.</description>
		<content:encoded><![CDATA[<p>Output: x</p>
<p>p = [.25 .25 .5 .75 ; .5 0 0 .25 ; .25 .5 0 0 ; 0 .25 .5 0 ];<br />
[P D] = eig(p);<br />
D=(abs(D)&gt;.99).*D;<br />
x = real(P * D * P^(-1) * [1 ; 0 ; 0 ; 0]);</p>
<p>The idea here is to use the eigenvalue decomposition, noting that the eigenvalues with norm less than 1 will vanish.  Remove those eigenvalues and reassemble the matrix.  Multiply by the starting vector and you have the result.  This is accurate to machine precision.  The real is to remove the roundoff imaginary components.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Urs (us) Schwarz</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10404</link>
		<dc:creator>Urs (us) Schwarz</dc:creator>
		<pubDate>Tue, 01 Apr 2008 16:25:58 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10404</guid>
		<description>as with last weeks puzzler, when i had to second guess the task from a few examples (with consecutive bad proposals, of course), i'm again NOT able to run the videos from behind our two firewalls (the flash player [9,0,115,0] works fine, though, for other stuff): it shows about one 3rd of the first and kills the browser for the second...
it's just a bit frustrating...

best from zurich
urs

ps: maybe i'm not meant to contribute... or it's just the date...</description>
		<content:encoded><![CDATA[<p>as with last weeks puzzler, when i had to second guess the task from a few examples (with consecutive bad proposals, of course), i&#8217;m again NOT able to run the videos from behind our two firewalls (the flash player [9,0,115,0] works fine, though, for other stuff): it shows about one 3rd of the first and kills the browser for the second&#8230;<br />
it&#8217;s just a bit frustrating&#8230;</p>
<p>best from zurich<br />
urs</p>
<p>ps: maybe i&#8217;m not meant to contribute&#8230; or it&#8217;s just the date&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10402</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Tue, 01 Apr 2008 15:17:42 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10402</guid>
		<description>Daniel,

About the comments.  Great idea.  I will make it a best practice.

Thanks,
Doug</description>
		<content:encoded><![CDATA[<p>Daniel,</p>
<p>About the comments.  Great idea.  I will make it a best practice.</p>
<p>Thanks,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10401</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Tue, 01 Apr 2008 15:17:12 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10401</guid>
		<description>Daniel, Richard

I am surprised that so many people jumped no the Markov chain method, I expected that the exhaustive search or monte carlo methods would dominate.  Maybe that is because I worked with distributed computing clusters for MATLAB for a while but never really studied Markov Chains.

Thanks for playing!
Doug</description>
		<content:encoded><![CDATA[<p>Daniel, Richard</p>
<p>I am surprised that so many people jumped no the Markov chain method, I expected that the exhaustive search or monte carlo methods would dominate.  Maybe that is because I worked with distributed computing clusters for MATLAB for a while but never really studied Markov Chains.</p>
<p>Thanks for playing!<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10400</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Tue, 01 Apr 2008 15:11:35 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10400</guid>
		<description>Francois,

A beautiful solution.  I like how you derived the M matrix rather than it being a "Magic Number Matrix" (which is what I did).  

When I was designing this challenge, I was going to have an add-on where the number of coins and number of squares was variable.  I thought that simpler was better.  I see that your code would have been able to handle such a complication nicely.

Thanks for a great solution that works in a different way than the earlier one.

Doug</description>
		<content:encoded><![CDATA[<p>Francois,</p>
<p>A beautiful solution.  I like how you derived the M matrix rather than it being a &#8220;Magic Number Matrix&#8221; (which is what I did).  </p>
<p>When I was designing this challenge, I was going to have an add-on where the number of coins and number of squares was variable.  I thought that simpler was better.  I see that your code would have been able to handle such a complication nicely.</p>
<p>Thanks for a great solution that works in a different way than the earlier one.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Armyr</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10393</link>
		<dc:creator>Daniel Armyr</dc:creator>
		<pubDate>Tue, 01 Apr 2008 09:06:29 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10393</guid>
		<description>Hi.
One general comment about how you write your blog. I noticed that Loren allways puts a link to the comments section in the text of her entries. I read these blogs with a feed reader so I don't get the automatic footer with links to the comments section as I get when I visit the homepage. If there is a link in the text, I can go directly to the comments section which is nice.

Sincerely
Daniel Armyr</description>
		<content:encoded><![CDATA[<p>Hi.<br />
One general comment about how you write your blog. I noticed that Loren allways puts a link to the comments section in the text of her entries. I read these blogs with a feed reader so I don&#8217;t get the automatic footer with links to the comments section as I get when I visit the homepage. If there is a link in the text, I can go directly to the comments section which is nice.</p>
<p>Sincerely<br />
Daniel Armyr</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Armyr</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10391</link>
		<dc:creator>Daniel Armyr</dc:creator>
		<pubDate>Tue, 01 Apr 2008 07:22:27 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10391</guid>
		<description>Yeah, the markov chain is of couse the obvious method here, assuming you are familiar with statistics. Richard here beat me to it though. My solution is identical to his, although quite a bit more verbose:

startPos = [1; 0; 0; 0];
numberOfMoves = 20;

probabilityMatrix = ...
    [ 1/4 1/2 1/4 0; ...
    1/4 0 1/2 1/4; ...
    1/2 0 0 1/2; ...
    3/4 1/4 0 0]';

probabilityAfter20Turns = probabilityMatrix^numberOfMoves*startPos</description>
		<content:encoded><![CDATA[<p>Yeah, the markov chain is of couse the obvious method here, assuming you are familiar with statistics. Richard here beat me to it though. My solution is identical to his, although quite a bit more verbose:</p>
<p>startPos = [1; 0; 0; 0];<br />
numberOfMoves = 20;</p>
<p>probabilityMatrix = &#8230;<br />
    [ 1/4 1/2 1/4 0; &#8230;<br />
    1/4 0 1/2 1/4; &#8230;<br />
    1/2 0 0 1/2; &#8230;<br />
    3/4 1/4 0 0]&#8217;;</p>
<p>probabilityAfter20Turns = probabilityMatrix^numberOfMoves*startPos</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Brown</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10379</link>
		<dc:creator>Richard Brown</dc:creator>
		<pubDate>Mon, 31 Mar 2008 21:30:41 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10379</guid>
		<description>Alternatively, as a finite time-homogeneous Markov chain

p20 = (0.25 * [1 2 1 0; 1 0 2 1; 2 0 0 2; 3 1 0 0])^20;
disp(p20(1, :))

cheers,

Richard</description>
		<content:encoded><![CDATA[<p>Alternatively, as a finite time-homogeneous Markov chain</p>
<p>p20 = (0.25 * [1 2 1 0; 1 0 2 1; 2 0 0 2; 3 1 0 0])^20;<br />
disp(p20(1, :))</p>
<p>cheers,</p>
<p>Richard</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Francois</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10377</link>
		<dc:creator>Francois</dc:creator>
		<pubDate>Mon, 31 Mar 2008 21:03:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10377</guid>
		<description>Hello Doug,

Here is a function working for a slightly more general situation:

&lt;pre&gt;
function probs = game(steps)
N = 4; % number of game states

% Basic transition matrices
% move(k) corresponds to "move k squares"
move = @(k)circshift(eye(N),[0 k]);
% go(k) corresponds to "go to square k"
go = @(k)ones(N,1)*((1:N)==k);

% Build the actual transition matrix
M = 1/2 * move(1) + 1/4 * move(2) + 1/4 * go(1);

% Compute probabilities 
if steps &#60; inf
   % Finite number of steps: intermediate distributions are computed 
   % using successive matrix-vector products
   state = (1:N)==1;
   for i = 1:steps
       state = state*M;
       probs(i, 1:N) = state;
   end
else 
   % Infinite number of steps: stationary distribution is computed 
   % (assuming it exists) by solving a linear system
   probs = [zeros(1,N) 1] /[M-eye(N) ones(N,1)];
end    
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hello Doug,</p>
<p>Here is a function working for a slightly more general situation:</p>
<pre>
function probs = game(steps)
N = 4; % number of game states

% Basic transition matrices
% move(k) corresponds to "move k squares"
move = @(k)circshift(eye(N),[0 k]);
% go(k) corresponds to "go to square k"
go = @(k)ones(N,1)*((1:N)==k);

% Build the actual transition matrix
M = 1/2 * move(1) + 1/4 * move(2) + 1/4 * go(1);

% Compute probabilities
if steps &lt; inf
   % Finite number of steps: intermediate distributions are computed
   % using successive matrix-vector products
   state = (1:N)==1;
   for i = 1:steps
       state = state*M;
       probs(i, 1:N) = state;
   end
else
   % Infinite number of steps: stationary distribution is computed
   % (assuming it exists) by solving a linear system
   probs = [zeros(1,N) 1] /[M-eye(N) ones(N,1)];
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10375</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Mon, 31 Mar 2008 19:30:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2008/03/31/puzzler-coin-game/#comment-10375</guid>
		<description>Dan,

I like this solution.  It follows exactly the idea I put in the last wrap-up where you make your code as readable as possible, and then modify it for speed when needed.  

Your implementation is very similar to my solution (video one above), but yours is a lot faster.  The reason yours is faster is the use of BSXFUN.  In this case, a little less clarity is worth the performance increase.  I have to admit, I needed to read the doc for this function, as I have never used it!

I would have placed the definition of twoTails immediately before the line where it was used, so it is easier to follow, but this is pretty clear code with some amount of commenting where needed.

While this code is fast and pretty accurate, I am curious if anyone will come up with other methods that are exactly accurate and even faster like my second video.

Thanks!
Doug</description>
		<content:encoded><![CDATA[<p>Dan,</p>
<p>I like this solution.  It follows exactly the idea I put in the last wrap-up where you make your code as readable as possible, and then modify it for speed when needed.  </p>
<p>Your implementation is very similar to my solution (video one above), but yours is a lot faster.  The reason yours is faster is the use of BSXFUN.  In this case, a little less clarity is worth the performance increase.  I have to admit, I needed to read the doc for this function, as I have never used it!</p>
<p>I would have placed the definition of twoTails immediately before the line where it was used, so it is easier to follow, but this is pretty clear code with some amount of commenting where needed.</p>
<p>While this code is fast and pretty accurate, I am curious if anyone will come up with other methods that are exactly accurate and even faster like my second video.</p>
<p>Thanks!<br />
Doug</p>
]]></content:encoded>
	</item>
</channel>
</rss>
