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
|
<html lang="en">
<head>
<title>Monte Carlo Examples - GNU Scientific Library -- Reference Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Scientific Library -- Reference Manual">
<meta name="generator" content="makeinfo 4.8">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Monte-Carlo-Integration.html" title="Monte Carlo Integration">
<link rel="prev" href="VEGAS.html" title="VEGAS">
<link rel="next" href="Monte-Carlo-Integration-References-and-Further-Reading.html" title="Monte Carlo Integration References and Further Reading">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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.2 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 freedom to copy and modify this
GNU Manual, like GNU software.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Monte-Carlo-Examples"></a>
Next: <a rel="next" accesskey="n" href="Monte-Carlo-Integration-References-and-Further-Reading.html">Monte Carlo Integration References and Further Reading</a>,
Previous: <a rel="previous" accesskey="p" href="VEGAS.html">VEGAS</a>,
Up: <a rel="up" accesskey="u" href="Monte-Carlo-Integration.html">Monte Carlo Integration</a>
<hr>
</div>
<h3 class="section">23.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,
<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>
<p class="noindent">The analytic value of this integral can be shown to be I =
\Gamma(1/4)^4/(4 \pi^3) = 1.393203929685676859.... The integral gives
the mean time spent at the origin by a random walk on a body-centered
cubic lattice in three dimensions.
<p>For simplicity we will compute the integral over the region
(0,0,0) to (\pi,\pi,\pi) 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 (0,0,0),
(0,\pi,\pi), (\pi,0,\pi) and (\pi,\pi,0). 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.
<pre class="smallexample"><pre class="verbatim"> #include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_plain.h>
#include <gsl/gsl_monte_miser.h>
#include <gsl/gsl_monte_vegas.h>
/* 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, "Statistical Field Theory -
Volume 1", 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) -> (pi,pi,pi) and multiply by 8 */
double exact = 1.3932039296856768591842462603255;
double
g (double *k, size_t dim, 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 ("%s ==================\n", title);
printf ("result = % .6f\n", result);
printf ("sigma = % .6f\n", error);
printf ("exact = % .6f\n", exact);
printf ("error = % .6f = %.1g sigma\n", 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 = { &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 (&G, xl, xu, 3, calls, r, s,
&res, &err);
gsl_monte_plain_free (s);
display_results ("plain", res, err);
}
{
gsl_monte_miser_state *s = gsl_monte_miser_alloc (3);
gsl_monte_miser_integrate (&G, xl, xu, 3, calls, r, s,
&res, &err);
gsl_monte_miser_free (s);
display_results ("miser", res, err);
}
{
gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (3);
gsl_monte_vegas_integrate (&G, xl, xu, 3, 10000, r, s,
&res, &err);
display_results ("vegas warm-up", res, err);
printf ("converging...\n");
do
{
gsl_monte_vegas_integrate (&G, xl, xu, 3, calls/5, r, s,
&res, &err);
printf ("result = % .6f sigma = % .6f "
"chisq/dof = %.1f\n", res, err, s->chisq);
}
while (fabs (s->chisq - 1.0) > 0.5);
display_results ("vegas final", res, err);
gsl_monte_vegas_free (s);
}
gsl_rng_free (r);
return 0;
}
</pre></pre>
<p class="noindent">With 500,000 function calls the plain Monte Carlo algorithm achieves a
fractional error of 0.6%. The estimated error <code>sigma</code> is
consistent with the actual error, and the computed result differs from
the true result by about one standard deviation,
<pre class="example"> plain ==================
result = 1.385867
sigma = 0.007938
exact = 1.393204
error = -0.007337 = 0.9 sigma
</pre>
<p class="noindent">The <span class="sc">miser</span> algorithm reduces the error by a factor of two, and also
correctly estimates the error,
<pre class="example"> miser ==================
result = 1.390656
sigma = 0.003743
exact = 1.393204
error = -0.002548 = 0.7 sigma
</pre>
<p class="noindent">In the case of the <span class="sc">vegas</span> algorithm the program uses an initial
warm-up run of 10,000 function calls to prepare, or “warm up”, 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.
<pre class="example"> vegas warm-up ==================
result = 1.386925
sigma = 0.002651
exact = 1.393204
error = -0.006278 = 2 sigma
converging...
result = 1.392957 sigma = 0.000452 chisq/dof = 1.1
vegas final ==================
result = 1.392957
sigma = 0.000452
exact = 1.393204
error = -0.000247 = 0.5 sigma
</pre>
<p class="noindent">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 <span class="sc">vegas</span> (using a similar number of
function calls) is significantly more accurate than the other two
algorithms.
<hr>The GNU Scientific Library - a free numerical library licensed under the GNU GPL<br>Back to the <a href="/software/gsl/">GNU Scientific Library Homepage</a></body></html>
|