RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
InversePiProb.hpp
Go to the documentation of this file.
1 /**
2  * \file InversePiProb.hpp
3  * \brief Header for InversePiProb
4  *
5  * Return true with probabililty 1/π.
6  *
7  * Copyright (c) Charles Karney (2012) <charles@karney.com> and licensed
8  * under the MIT/X11 License. For more information, see
9  * http://randomlib.sourceforge.net/
10  **********************************************************************/
11 
12 #if !defined(RANDOMLIB_INVERSEPIPROB_HPP)
13 #define RANDOMLIB_INVERSEPIPROB_HPP 1
14 
15 #include <cstdlib> // for abs(int)
16 #include <RandomLib/Random.hpp>
17 
18 namespace RandomLib {
19 
20  /**
21  * \brief Return true with probability 1/&pi;.
22  *
23  * InversePiProb p; p(Random& r) returns true with prob 1/&pi; using the
24  * method of Flajolet et al. It consumes 9.6365 bits per call on average.
25  *
26  * The method is given in Section 3.3 of
27  * - P. Flajolet, M. Pelletier, and M. Soria,<br>
28  * On Buffon Machines and Numbers,<br> Proc. 22nd ACM-SIAM Symposium on
29  * Discrete Algorithms (SODA), Jan. 2011.<br>
30  * http://www.siam.org/proceedings/soda/2011/SODA11_015_flajoletp.pdf <br>
31  * .
32  * using the identity
33  * \f[ \frac 1\pi = \sum_{n=0}^\infty
34  * {{2n}\choose n}^3 \frac{6n+1}{2^{8n+2}} \f]
35  *
36  * It is based on the expression for 1/&pi; given by Eq. (28) of<br>
37  * - S. Ramanujan,<br>
38  * Modular Equations and Approximations to &pi;,<br>
39  * Quart. J. Pure App. Math. 45, 350--372 (1914);<br>
40  * In Collected Papers, edited by G. H. Hardy, P. V. Seshu Aiyar,
41  * B. M. Wilson (Cambridge Univ. Press, 1927; reprinted AMS, 2000).<br>
42  * http://books.google.com/books?id=oSioAM4wORMC&pg=PA36 <br>
43  * .
44  * \f[\frac4\pi = 1 + \frac74 \biggl(\frac 12 \biggr)^3
45  * + \frac{13}{4^2} \biggl(\frac {1\cdot3}{2\cdot4} \biggr)^3
46  * + \frac{19}{4^3} \biggl(\frac {1\cdot3\cdot5}{2\cdot4\cdot6} \biggr)^3
47  * + \ldots \f]
48  *
49  * The following is a description of how to carry out the algorithm "by hand"
50  * with a real coin, together with a worked example:
51  * -# Perform three coin tossing experiments in which you toss a coin until
52  * you get tails, e.g., <tt>HHHHT</tt>; <tt>HHHT</tt>; <tt>HHT</tt>. Let
53  * <i>h</i><sub>1</sub> = 4, <i>h</i><sub>2</sub> = 3,
54  * <i>h</i><sub>3</sub> = 2 be the numbers of heads tossed in each
55  * experiment.
56  * -# Compute <i>n</i> = &lfloor;<i>h</i><sub>1</sub>/2&rfloor; +
57  * &lfloor;<i>h</i><sub>2</sub>/2&rfloor; +
58  * mod(&lfloor;(<i>h</i><sub>3</sub> &minus; 1)/3&rfloor;, 2) = 2 + 1 + 0
59  * = 3. Here is a table of the 3 contributions to <i>n</i>:\verbatim
60  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 h
61  0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 floor(h1/2)
62  0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 floor(h2/2)
63  1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 mod(floor((h3-1)/3), 2)
64  \endverbatim
65  * -# Perform three additional coin tossing experiments in each of which you
66  * toss a coin 2<i>n</i> = 6 times, e.g., <tt>TTHHTH</tt>;
67  * <tt>HHTHH|H</tt>; <tt>THHHHH</tt>. Are the number of heads and tails
68  * equal in each experiment? <b>yes</b> and <b>no</b> and <b>no</b> &rarr;
69  * <b>false</b>. (Here, you can give up at the |.)
70  * .
71  * The final result in this example is <b>false</b>. The most common way a
72  * <b>true</b> result is obtained is with <i>n</i> = 0, in which case the
73  * last step vacuously returns <b>true</b>.
74  *
75  * Proof of the algorithm: Flajolet et al. rearrange Ramanujan's identity as
76  * \f[ \frac 1\pi = \sum_{n=0}^\infty
77  * \biggl[{2n\choose n} \frac1{2^{2n}} \biggr]^3
78  * \frac{6n+1}{2^{2n+2}}. \f]
79  * Noticing that
80  * \f[ \sum_{n=0}^\infty
81  * \frac{6n+1}{2^{2n+2}} = 1, \f]
82  * the algorithm becomes:
83  * -# pick <i>n</i> &ge; 0 with prob (6<i>n</i>+1) / 2<sup>2<i>n</i>+2</sup>
84  * (mean <i>n</i> = 11/9);
85  * -# return <b>true</b> with prob (binomial(2<i>n</i>, <i>n</i>) /
86  * 2<sup>2<i>n</i></sup>)<sup>3</sup>.
87  *
88  * Implement (1) as
89  * - geom4(r) + geom4(r) returns <i>n</i> with probability 9(<i>n</i> +
90  * 1) / 2<sup>2<i>n</i>+4</sup>;
91  * - geom4(r) + geom4(r) + 1 returns <i>n</i> with probability
92  * 36<i>n</i> / 2<sup>2<i>n</i>+4</sup>;
93  * - combine these with probabilities [4/9, 5/9] to yield (6<i>n</i> +
94  * 1) / 2<sup>2<i>n</i>+2</sup>, as required.
95  * .
96  * Implement (2) as the outcome of 3 coin tossing experiments of 2<i>n</i>
97  * tosses with success defined as equal numbers of heads and tails in each
98  * trial.
99  *
100  * This class illustrates how to return an exact result using coin tosses
101  * only. A more efficient implementation (which is still exact) would
102  * replace prob59 by r.Prob(5,9) and geom4 by LeadingZeros z; z(r)/2.
103  **********************************************************************/
105  private:
106  template<class Random> bool prob59(Random& r) {
107  // true with prob 5/9 = 0.1 000 111 000 111 000 111 ... (binary expansion)
108  if (r.Boolean()) return true;
109  for (bool res = false; ; res = !res)
110  for (int i = 3; i--; ) if (r.Boolean()) return res;
111  }
112 
113  template<class Random> int geom4(Random& r) { // Geom(1/4)
114  int sum = 0;
115  while (r.Boolean() && r.Boolean()) ++sum;
116  return sum;
117  }
118 
119  template<class Random> bool binom(Random& r, int n) {
120  // Probability of equal heads and tails on 2*n tosses
121  // = binomial(2*n, n) / 2^(2*n)
122  int d = 0;
123  for (int k = n; k--; ) d += r.Boolean() ? 1 : -1;
124  for (int k = n; k--; ) {
125  d += r.Boolean() ? 1 : -1;
126  // This optimization saves 0.1686 bit per call to operator() on average.
127  if (std::abs(d) > k) return false;
128  }
129  return true;
130  }
131 
132  public:
133  /**
134  * Return true with probability 1/&pi;.
135  *
136  * @tparam Random the type of the random generator.
137  * @param[in,out] r a random generator.
138  * @return true with probability 1/&pi;.
139  **********************************************************************/
140  template<class Random> bool operator()(Random& r) {
141  // Return true with prob 1/pi.
142  int n = geom4(r) + geom4(r) + (prob59(r) ? 1 : 0);
143  for (int j = 3; j--; ) if (!binom(r, n)) return false;
144  return true;
145  }
146  };
147 
148 } // namespace RandomLib
149 
150 #endif // RANDOMLIB_INVERSEPIPROB_HPP
bool operator()(Random &r)
Generate random integers, reals, and booleans.
Header for Random, RandomGenerator.
Return true with probability 1/π.