File: Monte-Carlo-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
  • links: PTS
  • area: non-free
  • in suites: bullseye, buster, sid
  • size: 6,876 kB
  • ctags: 4,574
  • sloc: makefile: 35
file content (271 lines) | stat: -rw-r--r-- 10,331 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Free Software
Needs Free Documentation", the Front-Cover text being "A GNU Manual",
and with the Back-Cover Text being (a) (see below). A copy of the
license is included in the section entitled "GNU Free Documentation
License".

(a) The Back-Cover Text is: "You have the freedom to copy and modify this
GNU Manual." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Scientific Library &ndash; Reference Manual: Monte Carlo Examples</title>

<meta name="description" content="GNU Scientific Library &ndash; Reference Manual: Monte Carlo Examples">
<meta name="keywords" content="GNU Scientific Library &ndash; Reference Manual: Monte Carlo Examples">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Function-Index.html#Function-Index" rel="index" title="Function Index">
<link href="Monte-Carlo-Integration.html#Monte-Carlo-Integration" rel="up" title="Monte Carlo Integration">
<link href="Monte-Carlo-Integration-References-and-Further-Reading.html#Monte-Carlo-Integration-References-and-Further-Reading" rel="next" title="Monte Carlo Integration References and Further Reading">
<link href="VEGAS.html#VEGAS" rel="previous" title="VEGAS">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Monte-Carlo-Examples"></a>
<div class="header">
<p>
Next: <a href="Monte-Carlo-Integration-References-and-Further-Reading.html#Monte-Carlo-Integration-References-and-Further-Reading" accesskey="n" rel="next">Monte Carlo Integration References and Further Reading</a>, Previous: <a href="VEGAS.html#VEGAS" accesskey="p" rel="previous">VEGAS</a>, Up: <a href="Monte-Carlo-Integration.html#Monte-Carlo-Integration" accesskey="u" rel="up">Monte Carlo Integration</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Examples-17"></a>
<h3 class="section">25.5 Examples</h3>

<p>The example program below uses the Monte Carlo routines to estimate the
value of the following 3-dimensional integral from the theory of random
walks,
</p>
<div class="example">
<pre class="example">I = \int_{-pi}^{+pi} {dk_x/(2 pi)} 
    \int_{-pi}^{+pi} {dk_y/(2 pi)} 
    \int_{-pi}^{+pi} {dk_z/(2 pi)} 
     1 / (1 - cos(k_x)cos(k_y)cos(k_z)).
</pre></div>

<p>The analytic value of this integral can be shown to be <em>I =
\Gamma(1/4)^4/(4 \pi^3) = 1.393203929685676859...</em>.  The integral gives
the mean time spent at the origin by a random walk on a body-centered
cubic lattice in three dimensions.
</p>
<p>For simplicity we will compute the integral over the region
<em>(0,0,0)</em> to <em>(\pi,\pi,\pi)</em> and multiply by 8 to obtain the
full result.  The integral is slowly varying in the middle of the region
but has integrable singularities at the corners <em>(0,0,0)</em>,
<em>(0,\pi,\pi)</em>, <em>(\pi,0,\pi)</em> and <em>(\pi,\pi,0)</em>.  The
Monte Carlo routines only select points which are strictly within the
integration region and so no special measures are needed to avoid these
singularities.
</p>
<div class="smallexample">
<pre class="verbatim">#include &lt;stdlib.h&gt;
#include &lt;gsl/gsl_math.h&gt;
#include &lt;gsl/gsl_monte.h&gt;
#include &lt;gsl/gsl_monte_plain.h&gt;
#include &lt;gsl/gsl_monte_miser.h&gt;
#include &lt;gsl/gsl_monte_vegas.h&gt;

/* Computation of the integral,

      I = int (dx dy dz)/(2pi)^3  1/(1-cos(x)cos(y)cos(z))

   over (-pi,-pi,-pi) to (+pi, +pi, +pi).  The exact answer
   is Gamma(1/4)^4/(4 pi^3).  This example is taken from
   C.Itzykson, J.M.Drouffe, &quot;Statistical Field Theory -
   Volume 1&quot;, Section 1.1, p21, which cites the original
   paper M.L.Glasser, I.J.Zucker, Proc.Natl.Acad.Sci.USA 74
   1800 (1977) */

/* For simplicity we compute the integral over the region 
   (0,0,0) -&gt; (pi,pi,pi) and multiply by 8 */

double exact = 1.3932039296856768591842462603255;

double
g (double *k, size_t dim, void *params)
{
  (void)(dim); /* avoid unused parameter warnings */
  (void)(params);
  double A = 1.0 / (M_PI * M_PI * M_PI);
  return A / (1.0 - cos (k[0]) * cos (k[1]) * cos (k[2]));
}

void
display_results (char *title, double result, double error)
{
  printf (&quot;%s ==================\n&quot;, title);
  printf (&quot;result = % .6f\n&quot;, result);
  printf (&quot;sigma  = % .6f\n&quot;, error);
  printf (&quot;exact  = % .6f\n&quot;, exact);
  printf (&quot;error  = % .6f = %.2g sigma\n&quot;, result - exact,
          fabs (result - exact) / error);
}

int
main (void)
{
  double res, err;

  double xl[3] = { 0, 0, 0 };
  double xu[3] = { M_PI, M_PI, M_PI };

  const gsl_rng_type *T;
  gsl_rng *r;

  gsl_monte_function G = { &amp;g, 3, 0 };

  size_t calls = 500000;

  gsl_rng_env_setup ();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  {
    gsl_monte_plain_state *s = gsl_monte_plain_alloc (3);
    gsl_monte_plain_integrate (&amp;G, xl, xu, 3, calls, r, s, 
                               &amp;res, &amp;err);
    gsl_monte_plain_free (s);

    display_results (&quot;plain&quot;, res, err);
  }

  {
    gsl_monte_miser_state *s = gsl_monte_miser_alloc (3);
    gsl_monte_miser_integrate (&amp;G, xl, xu, 3, calls, r, s,
                               &amp;res, &amp;err);
    gsl_monte_miser_free (s);

    display_results (&quot;miser&quot;, res, err);
  }

  {
    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (3);

    gsl_monte_vegas_integrate (&amp;G, xl, xu, 3, 10000, r, s,
                               &amp;res, &amp;err);
    display_results (&quot;vegas warm-up&quot;, res, err);

    printf (&quot;converging...\n&quot;);

    do
      {
        gsl_monte_vegas_integrate (&amp;G, xl, xu, 3, calls/5, r, s,
                                   &amp;res, &amp;err);
        printf (&quot;result = % .6f sigma = % .6f &quot;
                &quot;chisq/dof = %.1f\n&quot;, res, err, gsl_monte_vegas_chisq (s));
      }
    while (fabs (gsl_monte_vegas_chisq (s) - 1.0) &gt; 0.5);

    display_results (&quot;vegas final&quot;, res, err);

    gsl_monte_vegas_free (s);
  }

  gsl_rng_free (r);

  return 0;
}
</pre></div>

<p>With 500,000 function calls the plain Monte Carlo algorithm achieves a
fractional error of 1%.  The estimated error <code>sigma</code> is roughly
consistent with the actual error&ndash;the computed result differs from
the true result by about 1.4 standard deviations,
</p>
<div class="example">
<pre class="example">plain ==================
result =  1.412209
sigma  =  0.013436
exact  =  1.393204
error  =  0.019005 = 1.4 sigma
</pre></div>

<p>The <small>MISER</small> algorithm reduces the error by a factor of four, and also
correctly estimates the error,
</p>
<div class="example">
<pre class="example">miser ==================
result =  1.391322
sigma  =  0.003461
exact  =  1.393204
error  = -0.001882 = 0.54 sigma
</pre></div>

<p>In the case of the <small>VEGAS</small> algorithm the program uses an initial
warm-up run of 10,000 function calls to prepare, or &ldquo;warm up&rdquo;, the grid.
This is followed by a main run with five iterations of 100,000 function
calls. The chi-squared per degree of freedom for the five iterations are
checked for consistency with 1, and the run is repeated if the results
have not converged. In this case the estimates are consistent on the
first pass.
</p>
<div class="example">
<pre class="example">vegas warm-up ==================
result =  1.392673
sigma  =  0.003410
exact  =  1.393204
error  = -0.000531 = 0.16 sigma
converging...
result =  1.393281 sigma =  0.000362 chisq/dof = 1.5
vegas final ==================
result =  1.393281
sigma  =  0.000362
exact  =  1.393204
error  =  0.000077 = 0.21 sigma
</pre></div>

<p>If the value of <code>chisq</code> had differed significantly from 1 it would
indicate inconsistent results, with a correspondingly underestimated
error.  The final estimate from <small>VEGAS</small> (using a similar number of
function calls) is significantly more accurate than the other two
algorithms.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Monte-Carlo-Integration-References-and-Further-Reading.html#Monte-Carlo-Integration-References-and-Further-Reading" accesskey="n" rel="next">Monte Carlo Integration References and Further Reading</a>, Previous: <a href="VEGAS.html#VEGAS" accesskey="p" rel="previous">VEGAS</a>, Up: <a href="Monte-Carlo-Integration.html#Monte-Carlo-Integration" accesskey="u" rel="up">Monte Carlo Integration</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>