Problem 2.2

Consider flipping 20 fair coins.  The Mathematica function Tuples[] will generate all permutations of the set {“H”, “T”}.

a.  Microstates

microstates = Tuples[{"H", "T"}, 20];

TableForm[microstates[[Range[10]]]] (* Here are the first 10 elements *)

nstates = Length[microstates] (* Note this is the same as (2^20) *)

macrostates (not part of this problem)

Count macrostates with the ‘Select’ function.  For example, to find all states with 3 “H”, we simply Select[ ] all microstates that have 3 “H”s.  Then the Length[ ] function can be used to count the length of that list, i.e. 4 microstates.

Select[microstates, Count[#, "H"] == 2 &];

Length[Select[microstates, Count[#, "H"] == 2 &]]

macrostates = Table[{n, multiplicity = Length[Select[microstates, Count[#, "H"] == n &]], N[multiplicity/nstates]}, {n, 0, 20}];

TableForm[macrostates, TableHeadings->{None, {"Number of Heads", "Multiplicity", "Probability"}}]

This is the same result we get from the combinatoric function (Binomial[n, m] in Mathematica).

TableForm[Table[{m, multiplicity = Binomial[20, m], N[multiplicity/(2^20)]}, {m, 0, 20}],
TableHeadings->{None, {"Number of Heads", "Multiplicity", "Probability"}}]

ListPlot[macrostates[[All, {1, 3}]], AxesLabel->{"Number of Heads", "Probability"}, LabelStyle->Larger, ImageSize-> Scaled[0.8]]

b.  Probability of getting a specific microstate

target =StringSplit["HTHHTTTHTHHHTHHHHTHT", ""]

Select[microstates, # == target &]

Since there is just one such state, the probability of getting it is

N[Length[%]/nstates]

c.  Probability of getting a particular macrostate with 12 Heads

n12H = Length[Select[microstates, Count[#, "H"] == 12 &]]

p = N[n12H/nstates]

Calculating the probability more directly using the Binomial coefficient:

N[Binomial[20, 12]/nstates]