📄 montecarlo.texi
字号:
@end example@end ifinfo@noindentwith a corresponding variance,@tex\beforedisplay$$Var_g(f; N) = Var(f/g; N)$$\afterdisplay@end tex@ifinfo@exampleVar_g(f; N) = Var(f/g; N)@end example@end ifinfo@noindentIf the probability distribution is chosen as @math{g = |f|/I(|f|)} thenit can be shown that the variance @math{V_g(f; N)} vanishes, and theerror in the estimate will be zero. In practice it is not possible tosample from the exact distribution @math{g} for an arbitrary function, soimportance sampling algorithms aim to produce efficient approximationsto the desired distribution.The @sc{vegas} algorithm approximates the exact distribution by making anumber of passes over the integration region while histogramming thefunction @math{f}. Each histogram is used to define a samplingdistribution for the next pass. Asymptotically this procedure convergesto the desired distribution. In orderto avoid the number of histogram bins growing like @math{K^d} theprobability distribution is approximated by a separable function:@c{$g(x_1, x_2,\ldots) = g_1(x_1) g_2(x_2)\ldots$} @math{g(x_1, x_2, ...) = g_1(x_1) g_2(x_2) ...} so that the number of bins required is only @math{Kd}. This is equivalent to locating the peaks of the function from theprojections of the integrand onto the coordinate axes. The efficiencyof @sc{vegas} depends on the validity of this assumption. It is mostefficient when the peaks of the integrand are well-localized. If anintegrand can be rewritten in a form which is approximately separablethis will increase the efficiency of integration with @sc{vegas}.@sc{vegas} incorporates a number of additional features, and combines bothstratified sampling and importance sampling. The integration region isdivided into a number of ``boxes'', with each box getting in fixednumber of points (the goal is 2). Each box can then have a fractionalnumber of bins, but if bins/box is less than two, Vegas switches to akind variance reduction (rather than importance sampling).@deftypefun {gsl_monte_vegas_state *} gsl_monte_vegas_alloc (size_t @var{dim})This function allocates and initializes a workspace for Monte Carlointegration in @var{dim} dimensions. The workspace is used to maintainthe state of the integration.@end deftypefun@deftypefun int gsl_monte_vegas_init (gsl_monte_vegas_state* @var{s})This function initializes a previously allocated integration state.This allows an existing workspace to be reused for differentintegrations.@end deftypefun@deftypefun int gsl_monte_vegas_integrate (gsl_monte_function * @var{f}, double * @var{xl}, double * @var{xu}, size_t @var{dim}, size_t @var{calls}, gsl_rng * @var{r}, gsl_monte_vegas_state * @var{s}, double * @var{result}, double * @var{abserr})This routines uses the @sc{vegas} Monte Carlo algorithm to integrate thefunction @var{f} over the @var{dim}-dimensional hypercubic regiondefined by the lower and upper limits in the arrays @var{xl} and@var{xu}, each of size @var{dim}. The integration uses a fixed numberof function calls @var{calls}, and obtains random sampling points usingthe random number generator @var{r}. A previously allocated workspace@var{s} must be supplied. The result of the integration is returned in@var{result}, with an estimated absolute error @var{abserr}. The resultand its error estimate are based on a weighted average of independentsamples. The chi-squared per degree of freedom for the weighted averageis returned via the state struct component, @var{s->chisq}, and must beconsistent with 1 for the weighted average to be reliable.@end deftypefun@deftypefun void gsl_monte_vegas_free (gsl_monte_vegas_state* @var{s}), This function frees the memory associated with the integrator state@var{s}.@end deftypefunThe @sc{vegas} algorithm computes a number of independent estimates of theintegral internally, according to the @code{iterations} parameterdescribed below, and returns their weighted average. Random sampling ofthe integrand can occasionally produce an estimate where the error iszero, particularly if the function is constant in some regions. Anestimate with zero error causes the weighted average to break down andmust be handled separately. In the original Fortran implementations of@sc{vegas} the error estimate is made non-zero by substituting a smallvalue (typically @code{1e-30}). The implementation in GSL differs fromthis and avoids the use of an arbitrary constant -- it either assignsthe value a weight which is the average weight of the precedingestimates or discards it according to the following procedure,@table @asis@item current estimate has zero error, weighted average has finite errorThe current estimate is assigned a weight which is the average weight ofthe preceding estimates.@item current estimate has finite error, previous estimates had zero errorThe previous estimates are discarded and the weighted averagingprocedure begins with the current estimate.@item current estimate has zero error, previous estimates had zero errorThe estimates are averaged using the arithmetic mean, but no error is computed.@end tableThe @sc{vegas} algorithm is highly configurable. The following variablescan be accessed through the @code{gsl_monte_vegas_state} struct,@deftypevar double result@deftypevarx double sigmaThese parameters contain the raw value of the integral @var{result} andits error @var{sigma} from the last iteration of the algorithm.@end deftypevar@deftypevar double chisqThis parameter gives the chi-squared per degree of freedom for theweighted estimate of the integral. The value of @var{chisq} should beclose to 1. A value of @var{chisq} which differs significantly from 1indicates that the values from different iterations are inconsistent.In this case the weighted error will be under-estimated, and furtheriterations of the algorithm are needed to obtain reliable results.@end deftypevar@deftypevar double alphaThe parameter @code{alpha} controls the stiffness of the rebinningalgorithm. It is typically set between one and two. A value of zeroprevents rebinning of the grid. The default value is 1.5.@end deftypevar@deftypevar size_t iterationsThe number of iterations to perform for each call to the routine. Thedefault value is 5 iterations.@end deftypevar@deftypevar int stageSetting this determines the @dfn{stage} of the calculation. Normally,@code{stage = 0} which begins with a new uniform grid and empty weightedaverage. Calling vegas with @code{stage = 1} retains the grid from theprevious run but discards the weighted average, so that one can ``tune''the grid using a relatively small number of points and then do a largerun with @code{stage = 1} on the optimized grid. Setting @code{stage =2} keeps the grid and the weighted average from the previous run, butmay increase (or decrease) the number of histogram bins in the griddepending on the number of calls available. Choosing @code{stage = 3}enters at the main loop, so that nothing is changed, and is equivalentto performing additional iterations in a previous call.@end deftypevar@deftypevar int modeThe possible choices are @code{GSL_VEGAS_MODE_IMPORTANCE},@code{GSL_VEGAS_MODE_STRATIFIED}, @code{GSL_VEGAS_MODE_IMPORTANCE_ONLY}.This determines whether @sc{vegas} will use importance sampling orstratified sampling, or whether it can pick on its own. In lowdimensions @sc{vegas} uses strict stratified sampling (more precisely,stratified sampling is chosen if there are fewer than 2 bins per box).@end deftypevar@deftypevar int verbose@deftypevarx {FILE *} ostreamThese parameters set the level of information printed by @sc{vegas}. Allinformation is written to the stream @var{ostream}. The default settingof @var{verbose} is @code{-1}, which turns off all output. A@var{verbose} value of @code{0} prints summary information about theweighted average and final result, while a value of @code{1} alsodisplays the grid coordinates. A value of @code{2} prints informationfrom the rebinning procedure for each iteration.@end deftypevar@node Monte Carlo Examples@section ExamplesThe example program below uses the Monte Carlo routines to estimate thevalue of the following 3-dimensional integral from the theory of randomwalks,@tex\beforedisplay$$I = \int_{-\pi}^{+\pi} {dk_x \over 2\pi} \int_{-\pi}^{+\pi} {dk_y \over 2\pi} \int_{-\pi}^{+\pi} {dk_z \over 2\pi} { 1 \over (1 - \cos(k_x)\cos(k_y)\cos(k_z))}$$\afterdisplay@end tex@ifinfo@exampleI = \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))@end example@end ifinfo@noindentThe analytic value of this integral can be shown to be @math{I =\Gamma(1/4)^4/(4 \pi^3) = 1.393203929685676859...}. The integral givesthe mean time spent at the origin by a random walk on a body-centeredcubic lattice in three dimensions.For simplicity we will compute the integral over the region@math{(0,0,0)} to @math{(\pi,\pi,\pi)} and multiply by 8 to obtain thefull result. The integral is slowly varying in the middle of the regionbut has integrable singularities at the corners @math{(0,0,0)},@math{(0,\pi,\pi)}, @math{(\pi,0,\pi)} and @math{(\pi,\pi,0)}. TheMonte Carlo routines only select points which are strictly within theintegration region and so no special measures are needed to avoid thesesingularities.@smallexample#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;doubleg (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]));@}voiddisplay_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);@}intmain (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); @} return 0;@}@end smallexample@noindentWith 500,000 function calls the plain Monte Carlo algorithm achieves afractional error of 0.6%. The estimated error @code{sigma} isconsistent with the actual error, and the computed result differs fromthe true result by about one standard deviation,@exampleplain ==================result = 1.385867sigma = 0.007938exact = 1.393204error = -0.007337 = 0.9 sigma@end example@noindentThe @sc{miser} algorithm reduces the error by a factor of two, and alsocorrectly estimates the error,@examplemiser ==================result = 1.390656sigma = 0.003743exact = 1.393204error = -0.002548 = 0.7 sigma@end example@noindentIn the case of the @sc{vegas} algorithm the program uses an initialwarm-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 functioncalls. The chi-squared per degree of freedom for the five iterations arechecked for consistency with 1, and the run is repeated if the resultshave not converged. In this case the estimates are consistent on thefirst pass.@examplevegas warm-up ==================result = 1.386925sigma = 0.002651exact = 1.393204error = -0.006278 = 2 sigmaconverging...result = 1.392957 sigma = 0.000452 chisq/dof = 1.1vegas final ==================result = 1.392957sigma = 0.000452exact = 1.393204error = -0.000247 = 0.5 sigma@end example@noindentIf the value of @code{chisq} had differed significantly from 1 it wouldindicate inconsistent results, with a correspondingly underestimatederror. The final estimate from @sc{vegas} (using a similar number offunction calls) is significantly more accurate than the other twoalgorithms.@node Monte Carlo Integration References and Further Reading@section References and Further Reading@noindentThe @sc{miser} algorithm is described in the following article,@itemize @asis@itemW.H. Press, G.R. Farrar, @cite{Recursive Stratified Sampling forMultidimensional Monte Carlo Integration},Computers in Physics, v4 (1990), pp190-195.@end itemize@noindentThe @sc{vegas} algorithm is described in the following papers,@itemize @asis@itemG.P. Lepage,@cite{A New Algorithm for Adaptive Multidimensional Integration},Journal of Computational Physics 27, 192-203, (1978)@itemG.P. Lepage,@cite{VEGAS: An Adaptive Multi-dimensional Integration Program},Cornell preprint CLNS 80-447, March 1980@end itemize
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -