⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 montecarlo.texi

📁 开放gsl矩阵运算
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@cindex Monte Carlo integrationThis chapter describes routines for multidimensional Monte Carlointegration.  These include the traditional Monte Carlo method andadaptive algorithms such as @sc{vegas} and @sc{miser} which useimportance sampling and stratified sampling techniques. Each algorithmcomputes an estimate of a multidimensional definite integral of theform,@tex\beforedisplay$$I = \int_{x_l}^{x_u} dx\,\int_{y_l}^{y_u}dy\,... f(x,y,...)$$\afterdisplay@end tex@ifinfo@exampleI = \int_xl^xu dx \int_yl^yu  dy ...  f(x, y, ...)@end example@end ifinfo@noindentover a hypercubic region @math{((x_l,x_u)}, @math{(y_l,y_u), ...)} usinga fixed number of function calls.  The routines also provide astatistical estimate of the error on the result.  This error estimateshould be taken as a guide rather than as a strict error bound ---random sampling of the region may not uncover all the important featuresof the function, resulting in an underestimate of the error.The functions are defined in separate header files for each routine,@code{gsl_monte_plain.h}, @file{gsl_monte_miser.h} and@file{gsl_monte_vegas.h}.@menu* Monte Carlo Interface::       * PLAIN Monte Carlo::  * MISER::                       * VEGAS::                       * Monte Carlo Examples::        * Monte Carlo Integration References and Further Reading::  @end menu@node Monte Carlo Interface@section InterfaceAll of the Monte Carlo integration routines use the same interface.There is an allocator to allocate memory for control variables andworkspace, a routine to initialize those control variables, theintegrator itself, and a function to free the space when done.Each integration function requires a random number generator to besupplied, and returns an estimate of the integral and its standarddeviation.  The accuracy of the result is determined by the number offunction calls specified by the user.  If a known level of accuracy isrequired this can be achieved by calling the integrator several timesand averaging the individual results until the desired accuracy isobtained.  Random sample points used within the Monte Carlo routines are alwayschosen strictly within the integration region, so that endpointsingularities are automatically avoided.The function to be integrated has its own datatype, defined in theheader file @file{gsl_monte.h}.@deftp {Data Type} gsl_monte_function This data type defines a general function with parameters for MonteCarlo integration.@table @code@item double (* @var{function}) (double * @var{x}, size_t @var{dim}, void * @var{params})this function should return the value@c{$f(x,\hbox{\it params})$}@math{f(x,params)} for argument @var{x} and parameters @var{params},where @var{x} is an array of size @var{dim} giving the coordinates ofthe point where the function is to be evaluated.@item size_t @var{dim}the number of dimensions for @var{x}@item void * @var{params}a pointer to the parameters of the function@end table@end deftp@noindentHere is an example for a quadratic function in two dimensions,@tex\beforedisplay$$f(x,y) = a x^2 + b x y + c y^2$$\afterdisplay@end tex@ifinfo@examplef(x,y) = a x^2 + b x y + c y^2@end example@end ifinfo@noindentwith @math{a = 3}, @math{b = 2}, @math{c = 1}.  The following codedefines a @code{gsl_monte_function} @code{F} which you could pass to anintegrator:@examplestruct my_f_params @{ double a; double b; double c; @};doublemy_f (double x, size_t dim, void * p) @{   struct my_f_params * fp = (struct my_f_params *)p;   if (dim != 2)      @{        fprintf(stderr, "error: dim != 2");        abort();      @}   return  fp->a * x[0] * x[0]              + fp->b * x[0] * x[1]                + fp->c * x[1] * x[1];@}gsl_monte_function F;struct my_f_params params = @{ 3.0, 2.0, 1.0 @};F.function = &my_f;F.dim = 2;F.params = &params;@end example@noindentThe function @math{f(x)} can be evaluated using the following macro,@example#define GSL_MONTE_FN_EVAL(F,x)     (*((F)->function))(x,(F)->dim,(F)->params)@end example@node PLAIN Monte Carlo@section PLAIN Monte Carlo@cindex plain monte carloThe plain Monte Carlo algorithm samples points randomly from theintegration region to estimate the integral and its error.  Using thisalgorithm the estimate of the integral @math{E(f; N)} for @math{N}randomly distributed points @math{x_i} is given by,@tex\beforedisplay$$E(f; N) =  V \langle f \rangle = {V \over N} \sum_i^N f(x_i).$$\afterdisplay@end tex@ifinfo@exampleE(f; N) = =  V <f> = (V / N) \sum_i^N f(x_i).@end example@end ifinfo@noindentwhere @math{V} is the volume of the integration region.  The error onthis estimate @math{\sigma(E;N)} is calculated from the estimatedvariance of the mean,@tex\beforedisplay$$\sigma^2 (E; N) = {V \over N } \sum_i^N (f(x_i) - \langle f \rangle)^2$$\afterdisplay@end tex@ifinfo@example\sigma^2 (E; N) = (V / N) \sum_i^N (f(x_i) -  <f>)^2@end example@end ifinfo@noindentFor large @math{N} this variance decreases asymptotically as@math{var(f)/N}, where @math{var(f)} is the true variance of thefunction over the integration region.  The error estimate itself shoulddecrease as @c{$\sigma(f)/\sqrt{N}$}@math{\sigma(f)/\sqrt@{N@}}.  The familiar law of errorsdecreasing as @c{$1/\sqrt{N}$}@math{1/\sqrt@{N@}} applies --- to reduce the error by afactor of 10 requires a 100-fold increase in the number of samplepoints.The functions described in this section are declared in the header file@file{gsl_monte_plain.h}.@deftypefun {gsl_monte_plain_state *} gsl_monte_plain_alloc (size_t @var{dim})This function allocates and initializes a workspace for Monte Carlointegration in @var{dim} dimensions.  @end deftypefun@deftypefun int gsl_monte_plain_init (gsl_monte_plain_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_plain_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_plain_state * @var{s}, double * @var{result}, double * @var{abserr})This routines uses the plain 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}.@end deftypefun@deftypefun void gsl_monte_plain_free (gsl_monte_plain_state* @var{s}), This function frees the memory associated with the integrator state@var{s}.@end deftypefun@node MISER@section MISER@cindex MISER monte carlo integration@cindex recursive stratified sampling, MISERThe @sc{miser} algorithm of Press and Farrar is based on recursivestratified sampling.  This technique aims to reduce the overallintegration error by concentrating integration points in the regions ofhighest variance.The idea of stratified sampling begins with the observation that for twodisjoint regions @math{a} and @math{b} with Monte Carlo estimates of theintegral @math{E_a(f)} and @math{E_b(f)} and variances@math{\sigma_a^2(f)} and @math{\sigma_b^2(f)}, the variance@math{Var(f)} of the combined estimate @c{$E(f) = {1\over 2} (E_a(f) + E_b(f))$} @math{E(f) = (1/2) (E_a(f) + E_b(f))} is given by,@tex\beforedisplay$$Var(f) = {\sigma_a^2(f) \over 4 N_a} + {\sigma_b^2(f) \over 4 N_b}$$\afterdisplay@end tex@ifinfo@exampleVar(f) = (\sigma_a^2(f) / 4 N_a) + (\sigma_b^2(f) / 4 N_b)@end example@end ifinfo@noindentIt can be shown that this variance is minimized by distributing thepoints such that,@tex\beforedisplay$${N_a \over N_a+N_b} = {\sigma_a \over \sigma_a + \sigma_b}$$\afterdisplay@end tex@ifinfo@exampleN_a / (N_a + N_b) = \sigma_a / (\sigma_a + \sigma_b)@end example@end ifinfo@noindentHence the smallest error estimate is obtained by allocating samplepoints in proportion to the standard deviation of the function in eachsub-region.The @sc{miser} algorithm proceeds by bisecting the integration regionalong one coordinate axis to give two sub-regions at each step.  Thedirection is chosen by examining all @math{d} possible bisections andselecting the one which will minimize the combined variance of the twosub-regions.  The variance in the sub-regions is estimated by samplingwith a fraction of the total number of points available to the currentstep.  The same procedure is then repeated recursively for each of thetwo half-spaces from the best bisection. The remaining sample points areallocated to the sub-regions using the formula for @math{N_a} and@math{N_b}.  This recursive allocation of integration points continuesdown to a user-specified depth where each sub-region is integrated usinga plain Monte Carlo estimate.  These individual values and their errorestimates are then combined upwards to give an overall result and anestimate of its error.The functions described in this section are declared in the header file@file{gsl_monte_miser.h}.@deftypefun {gsl_monte_miser_state *} gsl_monte_miser_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_miser_init (gsl_monte_miser_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_miser_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_miser_state * @var{s}, double * @var{result}, double * @var{abserr})This routines uses the @sc{miser} 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}.@end deftypefun@deftypefun void gsl_monte_miser_free (gsl_monte_miser_state* @var{s}), This function frees the memory associated with the integrator state@var{s}.@end deftypefunThe @sc{miser} algorithm has several configurable parameters. Thefollowing variables can be accessed through the@code{gsl_monte_miser_state} struct,@deftypevar double estimate_fracThis parameter specifies the fraction of the currently available number offunction calls which are allocated to estimating the variance at eachrecursive step. The default value is 0.1.@end deftypevar@deftypevar size_t min_callsThis parameter specifies the minimum number of function calls requiredfor each estimate of the variance. If the number of function callsallocated to the estimate using @var{estimate_frac} falls below@var{min_calls} then @var{min_calls} are used instead.  This ensuresthat each estimate maintains a reasonable level of accuracy.  Thedefault value of @var{min_calls} is @code{16 * dim}.@end deftypevar@deftypevar size_t min_calls_per_bisectionThis parameter specifies the minimum number of function calls requiredto proceed with a bisection step.  When a recursive step has fewer callsavailable than @var{min_calls_per_bisection} it performs a plain MonteCarlo estimate of the current sub-region and terminates its branch ofthe recursion.  The default value of this parameter is @code{32 *min_calls}.@end deftypevar@deftypevar double alphaThis parameter controls how the estimated variances for the twosub-regions of a bisection are combined when allocating points.  Withrecursive sampling the overall variance should scale better than@math{1/N}, since the values from the sub-regions will be obtained usinga procedure which explicitly minimizes their variance.  To accommodatethis behavior the @sc{miser} algorithm allows the total variance todepend on a scaling parameter @math{\alpha},@tex\beforedisplay$$Var(f) = {\sigma_a \over N_a^\alpha} + {\sigma_b \over N_b^\alpha}$$\afterdisplay@end tex@ifinfo@exampleVar(f) = @{\sigma_a \over N_a^\alpha@} + @{\sigma_b \over N_b^\alpha@}@end example@end ifinfo@noindentThe authors of the original paper describing @sc{miser} recommend thevalue @math{\alpha = 2} as a good choice, obtained from numericalexperiments, and this is used as the default value in thisimplementation.@end deftypevar@deftypevar double ditherThis parameter introduces a random fractional variation of size@var{dither} into each bisection, which can be used to break thesymmetry of integrands which are concentrated near the exact center ofthe hypercubic integration region.  The default value of dither is zero,so no variation is introduced. If needed, a typical value of@var{dither} is around 0.1.@end deftypevar@node VEGAS@section VEGAS@cindex VEGAS monte carlo integration@cindex importance sampling, VEGASThe @sc{vegas} algorithm of Lepage is based on importance sampling.  Itsamples points from the probability distribution described by thefunction @math{|f|}, so that the points are concentrated in the regionsthat make the largest contribution to the integral.In general, if the Monte Carlo integral of @math{f} is sampled withpoints distributed according to a probability distribution described bythe function @math{g}, we obtain an estimate @math{E_g(f; N)},@tex\beforedisplay$$E_g(f; N) = E(f/g; N)$$\afterdisplay@end tex@ifinfo@exampleE_g(f; N) = E(f/g; N)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -