The Princess Revisited

March 30, 2015 Leave a comment

I enjoyed the Princess puzzle but lost the code I wrote. As an exercise, I approached it again…feeling in a code sharing mood,

sa = SparseArray[{{1, 2} -> 1, {17, 16} -> 1, Band[{2, 1}] -> 1/2,
Band[{2, 3}, {16, 17}] -> 1/2}, {17, 17}];
dm[p_] :=
DiscreteMarkovProcess[ReplacePart[ConstantArray[0, 17], p -> 1], sa]
sol = Thread[{Range[0, 29], Join[Range[2, 16], Reverse@Range[2, 16]]}];
func[n_] := Module[{rp = RandomFunction[dm[n], {0, 29}], pt},
pt = Intersection[rp["Paths"][[1]], sol];
ListPlot[{rp, sol}, PlotStyle -> {Black, Red}, Joined -> True,
Frame -> True,
FrameTicks -> {Table[{j, j + 1}, {j, 0, 29, 2}], Automatic},
PlotLabel ->
Row[Style[#, Bold] & /@ {"Princess starts in room: ", n}],
FrameLabel -> {"Day", "Room"}, Background -> LightOrange,
Epilog -> {Yellow, EdgeForm[Black], Disk[#, 0.5] & /@ pt}]]

and for simulation:

sim[num_] := Transpose[Table[func[#], {num}] & /@ Range[17]]

100 simulations:


Of course better code welcome.

Categories: Mathematica

Rock, Paper, Scissors

March 30, 2015 Leave a comment

I have been reading “Magical Mathematics”. I admit that I am ignorant of magic and cannot juggle (but would like to learn). I am enjoying getting insights into magic.

This post relates to a rock paper scissors trick on page 185 of the book. In the trick, there is a deck of 27 card with all the triples of choosing (with replacement)  from the {rock, paper, scissors} with object arrange vertically. The trick involves shuffling the cards, putting aside the top card, then taking pairs of the remaining 26 cards and playing rock, paper scissors looking at first, second, the third rows then predicting the removed card. The process is much better explained in the book.

I decided I’d like to implement in Mathematica:
The rock, paper, scissors game:

rps[{"rock", "paper"}] := {"paper", "rock"};
rps[{"paper", "scissors"}] := {"scissors", "paper"}
rps[{"scissors", "rock"}] := {"rock", "scissors"}
rps[{x_, y_}] := rps[{y, x}]
rps[{x_, x_}] := Sequence[];

The round process:

rsub[m_, n_] := Module[{dat, lab = {"rock", "paper", "scissors"}, tl},
dat = rps /@ ((Transpose[#])[[n]] & /@ m);
tl = {#, Count[dat[[All, 2]], #]} & /@ lab;
rps[Select[GatherBy[tl, Mod[#[[2]], 2] &], Length@# == 2 &][[1, All,
trick[m_] := {rsub[Partition[Rest@m, 2], #] & /@ Range[3], First@m}

Generating the “cards”:

tup = Tuples[{"rock", "paper", "scissors"}, 3];

Generating 1000 random samples and using thumbnails:

rck = Thumbnail@
ppr = Thumbnail@
scs = Thumbnail@
rule = Thread[{"rock", "paper", "scissors"} -> {rck, ppr, scs}];
tab = Table[Table[trick[RandomSample[tup, 27]], {10}], {100}];
anim = Grid[
Prepend[{Row[#1 /. rule],
Row[#2 /. rule]} & @@@ #, {"Prediction", "First Card"}],
Frame -> All] & /@ tab;

Exported as animated gif:


Categories: Uncategorized

Here’s Looking at Euclid

March 13, 2015 Leave a comment

I enjoyed this book.  It was an enjoyable journey through multiple areas of mathematics.  Numbers, euclidean  geometry, recreational mathematics, probability and statistics and finally non-euclidean geometry are presented with motivating current everyday examples being enriched by the historical developments that underpinned them. I particularly enjoyed the chapters on mathematical devices, e.g. Curta, recreational mathematics, the chapters on chance and statistics.  The mathematical discussions are interspersed with interesting personalities and personal anecdotes such as weighing baguettes ‘in search of’ the normal distribution, only to be thwarted by the heat…in the footsteps of Poincare.

Categories: books, Mathematics

A matter of luck

February 16, 2015 Leave a comment

“Regression to the mean: what it is and how to deal with it” (International Journal of Epidemiology 2005;34:215–220 [ doi:10.1093/ije/dyh299])is a very instructive paper.


I have taken the example and illustrated to demonstrate effect of between and within group variance and  number of sample measurements.






Categories: Mathematics

The Improbability Principle

February 13, 2015 Leave a comment

This is an excellent book. It is extremely well written. It fits well with “Black Swan” and “Thinking fast and slow” in importantly making us aware of underlying statistical considerations in interpreting coincidence, clusters,  extreme value or apparently extremely low probability events. The author gathers these considerations under the “improbability principle” and through real examples and instructive toy examples beautifully explains very important biases and limitations. He provides helpful labels such: “law of truly large numbers”, “laws of selection”, :law of probability leverage”  covering topics such as: hindsight bias, lead time bias, base rate fallacy, prosecutots fallacy, the effects of fat tailed distributions.

I enjoyed this book and sadly (but wonderfully) it exposed some of my own hidden biases and misconceptions. It is always good to be shaken out of complacent lazy and convenient thinking.


Categories: books, Mathematics


November 8, 2014 1 comment

ubpdqnIt has been a challenging time of late…the evanescence of life, I am a speck of dust floating aimlessly on a turbulent sea.   Using some code from the talented MSE users( @RahulNarain and @MichaelE2) I digitized my handwritten ambigram and with Mathematica produced the animated gif…

Categories: Mathematica

Introduction To Probability

October 24, 2014 Leave a comment

This is a wonderful textbook that complements Professor Blitzstein’s Harvard STAT10 course.


I was very much looking forward to this textbook. I was not disappointed. The book follows closely the course. The concepts are clearly explained and the use of story and the examples work very well. The concepts accumulate gradually but steadily to increasingly complex subjects.

The book affirms Professor Blitzstein’s aphorism: “conditioning is the soul of statistics” and helps to use this principle in tackling a broad and deep swathe of problems.


Categories: books, Mathematics

Get every new post delivered to your Inbox.

Join 37 other followers