qmc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # -*- coding: utf-8 -*-
  2. r"""
  3. ====================================================
  4. Quasi-Monte Carlo submodule (:mod:`scipy.stats.qmc`)
  5. ====================================================
  6. .. currentmodule:: scipy.stats.qmc
  7. This module provides Quasi-Monte Carlo generators and associated helper
  8. functions.
  9. Quasi-Monte Carlo
  10. =================
  11. Engines
  12. -------
  13. .. autosummary::
  14. :toctree: generated/
  15. QMCEngine
  16. Sobol
  17. Halton
  18. LatinHypercube
  19. PoissonDisk
  20. MultinomialQMC
  21. MultivariateNormalQMC
  22. Helpers
  23. -------
  24. .. autosummary::
  25. :toctree: generated/
  26. discrepancy
  27. update_discrepancy
  28. scale
  29. Introduction to Quasi-Monte Carlo
  30. =================================
  31. Quasi-Monte Carlo (QMC) methods [1]_, [2]_, [3]_ provide an
  32. :math:`n \times d` array of numbers in :math:`[0,1]`. They can be used in
  33. place of :math:`n` points from the :math:`U[0,1]^{d}` distribution. Compared to
  34. random points, QMC points are designed to have fewer gaps and clumps. This is
  35. quantified by discrepancy measures [4]_. From the Koksma-Hlawka
  36. inequality [5]_ we know that low discrepancy reduces a bound on
  37. integration error. Averaging a function :math:`f` over :math:`n` QMC points
  38. can achieve an integration error close to :math:`O(n^{-1})` for well
  39. behaved functions [2]_.
  40. Most QMC constructions are designed for special values of :math:`n`
  41. such as powers of 2 or large primes. Changing the sample
  42. size by even one can degrade their performance, even their
  43. rate of convergence [6]_. For instance :math:`n=100` points may give less
  44. accuracy than :math:`n=64` if the method was designed for :math:`n=2^m`.
  45. Some QMC constructions are extensible in :math:`n`: we can find
  46. another special sample size :math:`n' > n` and often an infinite
  47. sequence of increasing special sample sizes. Some QMC
  48. constructions are extensible in :math:`d`: we can increase the dimension,
  49. possibly to some upper bound, and typically without requiring
  50. special values of :math:`d`. Some QMC methods are extensible in
  51. both :math:`n` and :math:`d`.
  52. QMC points are deterministic. That makes it hard to estimate the accuracy of
  53. integrals estimated by averages over QMC points. Randomized QMC (RQMC) [7]_
  54. points are constructed so that each point is individually :math:`U[0,1]^{d}`
  55. while collectively the :math:`n` points retain their low discrepancy.
  56. One can make :math:`R` independent replications of RQMC points to
  57. see how stable a computation is. From :math:`R` independent values,
  58. a t-test (or bootstrap t-test [8]_) then gives approximate confidence
  59. intervals on the mean value. Some RQMC methods produce a
  60. root mean squared error that is actually :math:`o(1/n)` and smaller than
  61. the rate seen in unrandomized QMC. An intuitive explanation is
  62. that the error is a sum of many small ones and random errors
  63. cancel in a way that deterministic ones do not. RQMC also
  64. has advantages on integrands that are singular or, for other
  65. reasons, fail to be Riemann integrable.
  66. (R)QMC cannot beat Bahkvalov's curse of dimension (see [9]_). For
  67. any random or deterministic method, there are worst case functions
  68. that will give it poor performance in high dimensions. A worst
  69. case function for QMC might be 0 at all n points but very
  70. large elsewhere. Worst case analyses get very pessimistic
  71. in high dimensions. (R)QMC can bring a great improvement over
  72. MC when the functions on which it is used are not worst case.
  73. For instance (R)QMC can be especially effective on integrands
  74. that are well approximated by sums of functions of
  75. some small number of their input variables at a time [10]_, [11]_.
  76. That property is often a surprising finding about those functions.
  77. Also, to see an improvement over IID MC, (R)QMC requires a bit of smoothness of
  78. the integrand, roughly the mixed first order derivative in each direction,
  79. :math:`\partial^d f/\partial x_1 \cdots \partial x_d`, must be integral.
  80. For instance, a function that is 1 inside the hypersphere and 0 outside of it
  81. has infinite variation in the sense of Hardy and Krause for any dimension
  82. :math:`d = 2`.
  83. Scrambled nets are a kind of RQMC that have some valuable robustness
  84. properties [12]_. If the integrand is square integrable, they give variance
  85. :math:`var_{SNET} = o(1/n)`. There is a finite upper bound on
  86. :math:`var_{SNET} / var_{MC}` that holds simultaneously for every square
  87. integrable integrand. Scrambled nets satisfy a strong law of large numbers
  88. for :math:`f` in :math:`L^p` when :math:`p>1`. In some
  89. special cases there is a central limit theorem [13]_. For smooth enough
  90. integrands they can achieve RMSE nearly :math:`O(n^{-3})`. See [12]_
  91. for references about these properties.
  92. The main kinds of QMC methods are lattice rules [14]_ and digital
  93. nets and sequences [2]_, [15]_. The theories meet up in polynomial
  94. lattice rules [16]_ which can produce digital nets. Lattice rules
  95. require some form of search for good constructions. For digital
  96. nets there are widely used default constructions.
  97. The most widely used QMC methods are Sobol' sequences [17]_.
  98. These are digital nets. They are extensible in both :math:`n` and :math:`d`.
  99. They can be scrambled. The special sample sizes are powers
  100. of 2. Another popular method are Halton sequences [18]_.
  101. The constructions resemble those of digital nets. The earlier
  102. dimensions have much better equidistribution properties than
  103. later ones. There are essentially no special sample sizes.
  104. They are not thought to be as accurate as Sobol' sequences.
  105. They can be scrambled. The nets of Faure [19]_ are also widely
  106. used. All dimensions are equally good, but the special sample
  107. sizes grow rapidly with dimension :math:`d`. They can be scrambled.
  108. The nets of Niederreiter and Xing [20]_ have the best asymptotic
  109. properties but have not shown good empirical performance [21]_.
  110. Higher order digital nets are formed by a digit interleaving process
  111. in the digits of the constructed points. They can achieve higher
  112. levels of asymptotic accuracy given higher smoothness conditions on :math:`f`
  113. and they can be scrambled [22]_. There is little or no empirical work
  114. showing the improved rate to be attained.
  115. Using QMC is like using the entire period of a small random
  116. number generator. The constructions are similar and so
  117. therefore are the computational costs [23]_.
  118. (R)QMC is sometimes improved by passing the points through
  119. a baker's transformation (tent function) prior to using them.
  120. That function has the form :math:`1-2|x-1/2|`. As :math:`x` goes from 0 to
  121. 1, this function goes from 0 to 1 and then back. It is very
  122. useful to produce a periodic function for lattice rules [14]_,
  123. and sometimes it improves the convergence rate [24]_.
  124. It is not straightforward to apply QMC methods to Markov
  125. chain Monte Carlo (MCMC). We can think of MCMC as using
  126. :math:`n=1` point in :math:`[0,1]^{d}` for very large :math:`d`, with
  127. ergodic results corresponding to :math:`d \to \infty`. One proposal is
  128. in [25]_ and under strong conditions an improved rate of convergence
  129. has been shown [26]_.
  130. Returning to Sobol' points: there are many versions depending
  131. on what are called direction numbers. Those are the result of
  132. searches and are tabulated. A very widely used set of direction
  133. numbers come from [27]_. It is extensible in dimension up to
  134. :math:`d=21201`.
  135. References
  136. ----------
  137. .. [1] Owen, Art B. "Monte Carlo Book: the Quasi-Monte Carlo parts." 2019.
  138. .. [2] Niederreiter, Harald. "Random number generation and quasi-Monte Carlo
  139. methods." Society for Industrial and Applied Mathematics, 1992.
  140. .. [3] Dick, Josef, Frances Y. Kuo, and Ian H. Sloan. "High-dimensional
  141. integration: the quasi-Monte Carlo way." Acta Numerica no. 22: 133, 2013.
  142. .. [4] Aho, A. V., C. Aistleitner, T. Anderson, K. Appel, V. Arnol'd, N.
  143. Aronszajn, D. Asotsky et al. "W. Chen et al.(eds.), "A Panorama of
  144. Discrepancy Theory", Sringer International Publishing,
  145. Switzerland: 679, 2014.
  146. .. [5] Hickernell, Fred J. "Koksma-Hlawka Inequality." Wiley StatsRef:
  147. Statistics Reference Online, 2014.
  148. .. [6] Owen, Art B. "On dropping the first Sobol' point." :arxiv:`2008.08051`,
  149. 2020.
  150. .. [7] L'Ecuyer, Pierre, and Christiane Lemieux. "Recent advances in randomized
  151. quasi-Monte Carlo methods." In Modeling uncertainty, pp. 419-474. Springer,
  152. New York, NY, 2002.
  153. .. [8] DiCiccio, Thomas J., and Bradley Efron. "Bootstrap confidence
  154. intervals." Statistical science: 189-212, 1996.
  155. .. [9] Dimov, Ivan T. "Monte Carlo methods for applied scientists." World
  156. Scientific, 2008.
  157. .. [10] Caflisch, Russel E., William J. Morokoff, and Art B. Owen. "Valuation
  158. of mortgage backed securities using Brownian bridges to reduce effective
  159. dimension." Journal of Computational Finance: no. 1 27-46, 1997.
  160. .. [11] Sloan, Ian H., and Henryk Wozniakowski. "When are quasi-Monte Carlo
  161. algorithms efficient for high dimensional integrals?." Journal of Complexity
  162. 14, no. 1 (1998): 1-33.
  163. .. [12] Owen, Art B., and Daniel Rudolf, "A strong law of large numbers for
  164. scrambled net integration." SIAM Review, to appear.
  165. .. [13] Loh, Wei-Liem. "On the asymptotic distribution of scrambled net
  166. quadrature." The Annals of Statistics 31, no. 4: 1282-1324, 2003.
  167. .. [14] Sloan, Ian H. and S. Joe. "Lattice methods for multiple integration."
  168. Oxford University Press, 1994.
  169. .. [15] Dick, Josef, and Friedrich Pillichshammer. "Digital nets and sequences:
  170. discrepancy theory and quasi-Monte Carlo integration." Cambridge University
  171. Press, 2010.
  172. .. [16] Dick, Josef, F. Kuo, Friedrich Pillichshammer, and I. Sloan.
  173. "Construction algorithms for polynomial lattice rules for multivariate
  174. integration." Mathematics of computation 74, no. 252: 1895-1921, 2005.
  175. .. [17] Sobol', Il'ya Meerovich. "On the distribution of points in a cube and
  176. the approximate evaluation of integrals." Zhurnal Vychislitel'noi Matematiki
  177. i Matematicheskoi Fiziki 7, no. 4: 784-802, 1967.
  178. .. [18] Halton, John H. "On the efficiency of certain quasi-random sequences of
  179. points in evaluating multi-dimensional integrals." Numerische Mathematik 2,
  180. no. 1: 84-90, 1960.
  181. .. [19] Faure, Henri. "Discrepance de suites associees a un systeme de
  182. numeration (en dimension s)." Acta arithmetica 41, no. 4: 337-351, 1982.
  183. .. [20] Niederreiter, Harold, and Chaoping Xing. "Low-discrepancy sequences and
  184. global function fields with many rational places." Finite Fields and their
  185. applications 2, no. 3: 241-273, 1996.
  186. .. [21] Hong, Hee Sun, and Fred J. Hickernell. "Algorithm 823: Implementing
  187. scrambled digital sequences." ACM Transactions on Mathematical Software
  188. (TOMS) 29, no. 2: 95-109, 2003.
  189. .. [22] Dick, Josef. "Higher order scrambled digital nets achieve the optimal
  190. rate of the root mean square error for smooth integrands." The Annals of
  191. Statistics 39, no. 3: 1372-1398, 2011.
  192. .. [23] Niederreiter, Harald. "Multidimensional numerical integration using
  193. pseudorandom numbers." In Stochastic Programming 84 Part I, pp. 17-38.
  194. Springer, Berlin, Heidelberg, 1986.
  195. .. [24] Hickernell, Fred J. "Obtaining O (N-2+e) Convergence for Lattice
  196. Quadrature Rules." In Monte Carlo and Quasi-Monte Carlo Methods 2000,
  197. pp. 274-289. Springer, Berlin, Heidelberg, 2002.
  198. .. [25] Owen, Art B., and Seth D. Tribble. "A quasi-Monte Carlo Metropolis
  199. algorithm." Proceedings of the National Academy of Sciences 102,
  200. no. 25: 8844-8849, 2005.
  201. .. [26] Chen, Su. "Consistency and convergence rate of Markov chain quasi Monte
  202. Carlo with examples." PhD diss., Stanford University, 2011.
  203. .. [27] Joe, Stephen, and Frances Y. Kuo. "Constructing Sobol sequences with
  204. better two-dimensional projections." SIAM Journal on Scientific Computing
  205. 30, no. 5: 2635-2654, 2008.
  206. """
  207. from ._qmc import *