ExactPower.hpp
Go to the documentation of this file.
00001 /**
00002  * \file ExactPower.hpp
00003  * \brief Header for ExactPower
00004  *
00005  * Sample exactly from a power distribution.
00006  *
00007  * Written by Charles Karney <charles@karney.com> and licensed under the LGPL.
00008  * For more information, see http://randomlib.sourceforge.net/
00009  **********************************************************************/
00010 
00011 #if !defined(EXACTPOWER_HPP)
00012 #define EXACTPOWER_HPP "$Id: ExactPower.hpp 6723 2010-01-11 14:20:10Z ckarney $"
00013 
00014 #include "RandomLib/RandomNumber.hpp"
00015 
00016 namespace RandomLib {
00017   /**
00018    * \brief Sample exactly from a power distribution.
00019    *
00020    * Sample exactly from power distribution (<i>n</i> + 1)
00021    * <i>x</i><sup><i>n</i></sup> for \e x in (0,1) and integer \e n >= 0 using
00022    * infinite precision.  The template parameter \a bits specifies the number
00023    * of bits in the base used for RandomNumber (i.e., base =
00024    * 2<sup><i>bits</i></sup>).
00025    **********************************************************************/
00026   template<int bits = 1> class ExactPower {
00027   public:
00028     /**
00029      * Return the random deviate with a power distribution, (<i>n</i> + 1)
00030      * <i>x</i><sup><i>n</i></sup> for \e x in (0,1) and integer \e n >= 0.
00031      * Returned result is a RandomNumber with base 2<sup><i>bits</i></sup>.
00032      * For \e bits = 1, the number of random bits in the result and consumed
00033      * are as follows:\n
00034      \verbatim
00035       n    random bits
00036           result  consumed
00037       0   0        0
00038       1   2        4
00039       2   2.33     6.67
00040       3   2.67     9.24
00041       4   2.96    11.71
00042       5   3.20    14.11
00043       6   3.41    16.45
00044       7   3.59    18.75
00045       8   3.75    21.01
00046       9   3.89    23.25
00047      10   4.02    25.47
00048      \endverbatim
00049      * The relative frequency of the results with \a bits = 1 can be shown via
00050      * a histogram\n <img src="powerhist.png" width=580 height=750 alt="exact
00051      * binary sampling of power distribution">\n The base of each rectangle
00052      * gives the range represented by the corresponding binary number and the
00053      * area is proportional to its frequency.  A PDF version of this figure
00054      * <a href="powerhist.pdf">here</a>.  This allows the figure to be
00055      * magnified to show the rectangles for all binary numbers up to 9 bits.
00056      **********************************************************************/
00057     template<class Random>
00058     RandomNumber<bits> operator()(Random& r, unsigned n) const;
00059   };
00060 
00061   template<int bits> template<class Random> inline RandomNumber<bits>
00062   ExactPower<bits>::operator()(Random& r, unsigned n) const {
00063     // Return max(u_0, u_1, u_2, ..., u_n).  Equivalent to taking the
00064     // (n+1)th root of u_0.
00065     RandomNumber<bits> x;
00066     for (RandomNumber<bits> y; n--;) {
00067       y.Init();
00068       if (x.LessThan(r, y))
00069         x = y;
00070     }
00071     return x;
00072   }
00073 } // namespace RandomLib
00074 #endif  // EXACTPOWER_HPP