Jim Killingsworth

More Coin Toss Per­for­mance En­hance­ments

This post is an ex­ten­sion of the pre­vi­ous post in which I ex­plored some tech­niques for speed­ing up the cal­cu­la­tions used to find ap­prox­i­mate so­lu­tions to the coin toss prob­lem. Here I want to ex­am­ine a cou­ple of en­hance­ments to these ideas. First, I de­scribe an en­hanced com­pu­ta­tion method that cuts the num­ber of float­ing-point op­er­a­tions re­quired al­most in half. Sec­ond, I in­tro­duce a pro­gres­sive poly­no­mi­al ap­prox­i­ma­tion tech­nique that can re­duce the num­ber of it­er­a­tions need­ed to find a so­lu­tion.

En­hanced Com­pu­ta­tion Method

The op­ti­mized com­pu­ta­tion method out­lined in the pre­vi­ous post had a much bet­ter per­for­mance pro­file than the pre­vi­ous­ly used method. How­ev­er, it still in­clud­ed a bunch of re­dun­dant com­pu­ta­tion­s. For even-num­bered coin toss­es, the val­ue com­put­ed for the odd­-num­bered states is al­ways ze­ro. Like­wise, for odd­-num­bered coin toss­es, the val­ue com­put­ed for the even-num­bered states is al­ways ze­ro. Since we know these al­ter­nat­ing val­ues are al­ways ze­ro, we re­al­ly don’t need to com­pute them. We can use an en­hanced com­pu­ta­tion method that just elim­i­nates the un­nec­es­sary com­pu­ta­tion­s. Like be­fore, the best way to de­scribe how it works is with an ex­am­ple. Sup­pose we have a mod­el of the coin toss game with five coin toss events per round:

Figure 1

Since we’re us­ing the same sym­met­ri­cal coin toss mod­el we’ve been us­ing in the past few post­s, we can as­sume that the coin in the ini­tial state is al­ways a fair coin:

Figure 2

For this ex­am­ple, let’s choose some ar­bi­trary val­ues for the re­main­ing state tran­si­tion­s:

Figure 3

We want to cre­ate some lookup ta­bles for our state tran­si­tion val­ues like we did for the op­ti­mized com­pu­ta­tion method pre­sent­ed in the pre­vi­ous post. But this time around, we want to cre­ate two dif­fer­ent sets of lookup ta­bles, one for odd­-num­bered coin toss­es and one for even-num­bered coin toss­es. Here is the for­mu­la to pop­u­late the lookup ta­bles for odd­-num­bered coin toss­es:

Figure 4

This for­mu­la is based on the even-num­bered state tran­si­tion val­ues. Here is what the pop­u­lat­ed ar­rays wind up look­ing like:

Figure 5

We need to align the ar­rays in a way that makes it easy to per­form the cal­cu­la­tion­s. In this case, we on­ly need to shift one of them to the left:

Figure 6

These are the lookup ta­bles we’ll use for odd­-num­bered coin toss­es. For even-num­bered coin toss­es, we need to do some­thing sim­i­lar. But we need to do it with the al­ter­nate set of state tran­si­tion val­ues. Here is the for­mu­la to pop­u­late the lookup ta­bles for even-num­bered coin toss­es:

Figure 7

This for­mu­la is based on the odd­-num­bered state tran­si­tion val­ues. Here is what the pop­u­lat­ed ar­rays wind up look­ing like:

Figure 8

We need to align the ar­rays in a way that makes it easy to per­form the cal­cu­la­tion­s. In this case, we on­ly need to shift one of them to the right:

Figure 9

These are the lookup ta­bles we’ll use for even-num­bered coin toss­es. No­tice the padded ze­ros in the front and back of the ar­rays. This padding guar­an­tees that we can shift the ar­rays left or right with­out vi­o­lat­ing any mem­o­ry al­lo­cat­ed for some­thing else. For this method, we al­so need to shift the state vec­tor ar­rays left and right in a sim­i­lar fash­ion when com­put­ing the re­sults for the even and odd coin toss­es. Here is the al­go­rithm for the en­hanced com­pu­ta­tion method:

Figure 10

The ex­e­cu­tion path of the in­ner loop de­pends on whether we are cal­cu­lat­ing the re­sult for an even-num­bered coin toss or an odd­-num­bered coin toss. No­tice that we dou­ble the val­ue in the ze­ro off­set just like we did for the op­ti­mized com­pu­ta­tion method in the pre­vi­ous post. But in this case, we on­ly do it for even-num­bered coin toss­es.

Here are the com­put­ed val­ues af­ter out­er loop it­er­a­tion #1:

Figure 11

Here are the com­put­ed val­ues af­ter out­er loop it­er­a­tion #2:

Figure 12

Here are the com­put­ed val­ues af­ter out­er loop it­er­a­tion #3:

Figure 13

Here are the com­put­ed val­ues af­ter out­er loop it­er­a­tion #4:

Figure 14

Here are the com­put­ed val­ues af­ter out­er loop it­er­a­tion #5:

Figure 15

Af­ter the loop ter­mi­nates, we have the prob­a­bil­i­ties of land­ing on each state af­ter the fifth and fi­nal toss of the coin. This re­sult on­ly in­cludes the ter­mi­nal states since we nev­er both­ered to com­pute the non-ter­mi­nal states. Since this ex­am­ple mod­els an odd num­ber of coin toss events, the ter­mi­nal states are al­ways odd­-num­bered states. Here are the re­sult­ing val­ues:

Figure 16

Us­ing this en­hanced com­pu­ta­tion method, we can com­pute the fi­nal re­sult with less mem­o­ry and few­er float­ing-point op­er­a­tions than we could with the op­ti­mized com­pu­ta­tion method pre­sent­ed in the last post.

Count­ing the Float­ing-Point Op­er­a­tions

How does the num­ber of float­ing-point op­er­a­tions re­quired for the en­hanced com­pu­ta­tion method pre­sent­ed above com­pare to the num­ber of float­ing-point op­er­a­tions re­quired for the op­ti­mized com­pu­ta­tion method pre­sent­ed in the pre­vi­ous post? Let’s count the num­ber of op­er­a­tions need­ed for each it­er­a­tion of the out­er loop in the above ex­am­ple:

Figure 17

From the ta­ble above, we can de­duce the fol­low­ing gen­er­al­iza­tion for each it­er­a­tion, re­gard­less of the size of the coin toss mod­el:

Figure 18

We can sum the num­ber of op­er­a­tions for each it­er­a­tion to get the to­tal num­ber of op­er­a­tions for a coin toss mod­el of any size. Here is the for­mu­la:

Figure 19

We can re­place the sum­ma­tion like we did in the last post and present the so­lu­tion in the fol­low­ing al­ge­bra­ic for­m:

Figure 20

Us­ing the op­ti­mized com­pu­ta­tion method from the last post as a base­line, we can see how well the en­hanced com­pu­ta­tion method stacks up in com­par­ison:

Figure 21

Both meth­ods have qua­drat­ic time com­plex­i­ty. But as you can see in the chart above, the en­hanced com­pu­ta­tion method re­quires few­er float­ing-point op­er­a­tions in all cas­es. The en­hanced com­pu­ta­tion method re­quires rough­ly half the num­ber of float­ing-point op­er­a­tions.

Pro­gres­sive Poly­no­mi­al Ap­prox­i­ma­tion

In the last post, we looked at an ex­am­ple of us­ing the cu­bic poly­no­mi­al ap­prox­i­ma­tion tech­nique to find an ap­prox­i­mate so­lu­tion for a mod­el with fifty coin toss events. The method took 1,746 it­er­a­tions to com­plete. Us­ing a pro­gres­sive poly­no­mi­al ap­prox­i­ma­tion tech­nique as an al­ter­na­tive, we can ar­rive at a so­lu­tion in less than a tenth of the num­ber of it­er­a­tions. To see how it work­s, let’s work through the ex­am­ple us­ing the al­ter­na­tive ap­proach. We’ll use the same tar­get dis­tri­b­u­tion that we did be­fore:

Figure 22

Like we did be­fore, we’ll start with a set of fair coins for the ini­tial guess. And like be­fore, we’ll use the hill climb­ing method to find an op­ti­mal set of weights that can be de­scribed by a poly­no­mi­al. But in­stead of fit­ting a cu­bic poly­no­mi­al, we’ll start by fit­ting a con­stant poly­no­mi­al. Here is the re­sult af­ter 10 it­er­a­tions:

Figure 23

This re­sult can now be used as the start­ing point for the next step in the process. We’ll fol­low the same pro­ce­dure us­ing a lin­ear poly­no­mi­al in­stead of a con­stant poly­no­mi­al. Here is the re­sult af­ter an ad­di­tion­al 49 it­er­a­tions:

Figure 24

Re­peat­ing the pro­ce­dure once again, we can use the re­sult above as the start­ing point for find­ing the most op­ti­mal so­lu­tion that can be de­scribed by a qua­drat­ic poly­no­mi­al. Here is the re­sult af­ter an­oth­er 26 it­er­a­tions:

Figure 25

And re­peat­ing this pro­ce­dure one more time, we can use the re­sult above as the start­ing point for the fi­nal stage in the process—find­ing the best re­sult that can be de­scribed by a cu­bic poly­no­mi­al. Here is the re­sult af­ter 28 more it­er­a­tions:

Figure 26

This is the fi­nal so­lu­tion af­ter a to­tal of 113 it­er­a­tions. In this case, us­ing the pro­gres­sive poly­no­mi­al ap­prox­i­ma­tion tech­nique al­lows us to find a so­lu­tion that fits a cu­bic poly­no­mi­al in far few­er it­er­a­tions than the reg­u­lar cu­bic poly­no­mi­al ap­prox­i­ma­tion method. In ad­di­tion, it is worth not­ing that it­er­a­tions in­volv­ing low­er-order poly­no­mi­als are less com­pu­ta­tion­al­ly ex­pen­sive.

You might no­tice that this is not the same so­lu­tion that was found us­ing the reg­u­lar cu­bic poly­no­mi­al ap­prox­i­ma­tion tech­nique in the pre­vi­ous post. And it is not the most op­ti­mal so­lu­tion that can be found with a cu­bic poly­no­mi­al. Like the ex­am­ple in the last post, the hill climb­ing process gets stuck in what seems to be a lo­cal min­i­mum. But fur­ther analy­sis in­di­cates that it might be stuck in a nar­row ridge or val­ley. Ap­ply­ing the hill climb­ing al­go­rithm with small­er step sizes may lead to a more ac­cu­rate so­lu­tion, but there is no guar­an­tee. Be­cause of this, I am a bit wary of us­ing the hill climb­ing al­go­rith­m. I might ex­per­i­ment with us­ing a dif­fer­ent ap­proach to avoid this pit­fal­l.

Ac­com­pa­ny­ing source code is avail­able on GitHub.

Com­ments

Show comments