372 11 Outline of the Monte Carlo Strategy
where
g(x,y) =exp(−x^2 −y^2 )(x−y)^2
withd= 6.
We can solve this integral by employing our brute force scheme, or using importance sam-
pling and random variables distributed according to a gaussian PDF. For the latter, if we set
the mean valueμ= 0 and the standard deviationσ= 1 /
√
2 , we have
√^1
π
exp(−x^2 ),
and using this normal distribution we rewrite our integral as
π^3
∫ 6
∏
i= 1
(
1
√
π
exp(−x^2 i)
)
(x−y)^2 dx 1 ....dx 6 ,
which is rewritten in a more compact form as
∫
f(x 1 ,...,xd)F(x 1 ,...,xd)
6
∏
i= 1
dxi,
wherefis the above normal distribution and
F(x 1 ,...,x 6 ) =F(x,y) = (x−y)^2 ,
Below we list two codes, one for the brute force integration and the other employing im-
portance sampling with a gaussian distribution.
11.5.1Brute Force Integration
http://folk.uio.no/mhjensen/compphys/programs/chapter11/cpp/program4.cpp
#include
#include
#include
#include"lib.h"
using namespacestd;
doublebrute_force_MC(double);
// Main function begins here
intmain()
{
intn;
doublex[6], y, fx;
doubleint_mc = 0.;doublevariance = 0.;
doublesum_sigma= 0. ;longidum=-1 ;
doublelength = 5.;// we fix the max size of the box to L=5
doublejacobidet = pow((2length),6);
cout <<"Read in the number of Monte-Carlo samples"<< endl;
cin >> n;
// evaluate the integral with importance sampling
for(inti = 1; i <= n; i++){
// x[] contains the random numbers for all dimensions
for(intj = 0; j< 6; j++){
x[j]=-length+2lengthran0(&idum);
}