11.5 Monte Carlo Integration of Multidimensional Integrals 373
fx=brute_force_MC(x);
int_mc += fx;
sum_sigma += fxfx;
}
int_mc = int_mc/((double) n );
sum_sigma = sum_sigma/((double) n );
variance=sum_sigma-int_mcint_mc;
// final output
cout << setiosflags(ios::showpoint | ios::uppercase);
cout <<" Monte carlo result= "<< setw(10) << setprecision(8) << jacobidetint_mc;
cout <<" Sigma= "<< setw(10) << setprecision(8) << volumesqrt(variance/((double) n
)) << endl;
return0;
}// end of main program
// this function defines the integrand to integrate
doublebrute_force_MC(doublex)
{
// evaluate the different terms of the exponential
doublexx=x[0]x[0]+x[1]x[1]+x[2]x[2];
doubleyy=x[3]x[3]+x[4]x[4]+x[5]x[5];
doublexy=pow((x[0]-x[3]),2)+pow((x[1]-x[4]),2)+pow((x[2]-x[5]),2);
returnexp(-xx-yy)xy;
}// end function for the integrand
11.5.2Importance Sampling
This code includes a call to the functionnormal_random, which produces random numbers
from a gaussian distribution.
http://folk.uio.no/mhjensen/compphys/programs/chapter11/cpp/program5.cpp
// importance sampling with gaussian deviates
#include
#include
#include
#include"lib.h"
using namespacestd;
doublegaussian_MC(double);
doublegaussian_deviate(long);
// Main function begins here
intmain()
{
intn;
doublex[6], y, fx;
cout <<"Read in the number of Monte-Carlo samples"<< endl;
cin >> n;
doubleint_mc = 0.;doublevariance = 0.;
doublesum_sigma= 0. ;longidum=-1 ;
doublejacobidet = pow(acos(-1.),3.);
doublesqrt2 = 1./sqrt(2.);
// 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] = gaussian_deviate(&idum)*sqrt2;