Pi evlution y 1 1 x Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (311) Monte Crlo integrtion we wnt to evlute the following integrl: F = f (x) dx remember tht the expecttion vlue of the function f (x) for x distributed ccording to PDF p(x) f = f (x) p(x) dx with: p(x) dx = 1 choosing x to be uniformly distributed in the intervl [, b], one hs: p(x) = 1 b f = 1 b f (x) p(x) dx = b f (x) dx MC integrtion F = f (x) dx = (b ) f = (b ) i=1 f (x i) x i is rndom vrible uniformly distributed in the intervl [, b] error estimtion σ F = (b )σ f σ 2 f = f 2 f 2 σ 2 f = σ2 f σ F = (b ) σ f 1 1 = (b ) ( f (xi ) ) 2 i=1 1 2 f (x i ) i=1 Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (312)
MC integrtion (cont.) Let s compute the integrls of the functions: dx 2 = 0.471239 cos(x) dx = 0.412215 (x + b) dx = 0.622035 Throwing 100 rndom vrible uniformly distributed we obtin the following results: dx 2 = 0.471239 ± 0.000000 cos(x) dx = 0.413671 ± 0.007098 (x + b) dx = 0.622280 ± 0.001037 Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (313) MC integrtion (cont.) double xmin=tmth::pi()*0.2; double xmx=tmth::pi()*0.5; int = 1000; lgorithm TF1 *f1 = new TF1( f1, TMth::Abs(cos(x)),xmin,xmx); TF1 *f2 = new TF1( f2, 0.5,xmin,xmx); TF1 *f3 = new TF1( f3, -0.4/TMth::Pi()*x+0.8,xmin,xmx);... for (int i=0; i<; i++) { double x = xmin + (xmx-xmin)*grndom->uniform(); double func1 = f1->evl(x); double func2 = f2->evl(x); double func3 = f3->evl(x); F1 += func1; F2 += func2; F3 += func3; f1s += func1*func1; f2s += func2*func2; f3s += func3*func3; } double f1m = F1/; //men double f2m = F2/; double f3m = F3/; lgorithm // integrls double I1 = f1m*(xmx-xmin); double I2 = f2m*(xmx-xmin); double I3 = f3m*(xmx-xmin); // vrinces double Vr1 = f1s/ - f1m*f1m; double Vr2 = f2s/ - f2m*f2m; double Vr3 = f3s/ - f3m*f3m; // errors double E1 = (xmx-xmin)/sqrt()*sqrt(vr1); double E2 = (xmx-xmin)/sqrt()*sqrt(vr2); double E3 = (xmx-xmin)/sqrt()*sqrt(vr3); Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (314)
MC integrtion (cont.) Let s check the integrl vlue s function of the number of rndom vribles generted F = cos(x) dx F = dx 2 F = (x + b) dx Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (315) Empty Slide Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (316)
Reduction vrince techniques The cos(x) function vries much more in the intervl of integrtion tht the others Its integrl vlue evlution presents the lrgest vrince. Why? Becuse we re smpling uniformly nd the regions close to zero where the function is more importnt re smpled with the sme importnce s others where the function is smller! In the frmework of the importnce smpling technique n dditionl pdf p(x) cn be used to rend the integrnd smooth! Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (317) Importnce smpling Rend smooth our integrnd by pplying pdf p(x) F = f (x) dx = f (x) p(x) p(x) dx If the pdf is normlized in the integrl intervl [, b] p(x) dx = 1 nd x is vrible distributed ccording to p(x), then f b = p f (x) p(x) p(x) dx Let s mke vrible chnge f (x) p(x) p(x) dx } {{ } p(y)dy p(x)dx = p(y)dy if y is distributed uniformly in [0, 1] then 1 0 p(y)dy = 1 p(y) = 1 The trnsformtion between x nd Y cn be obtined by: x p(x )dx = y 0 dy y = x p(x )dx Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (318)
Importnce smpling (cont.) From the trnsformtion of vribles we hve reltion between x nd y y = x p(x )dx x(y) Generting rndom vrible y uniformly between [0, 1] nd pplying the trnsformtion reltion x(y) we get rndom vribles x distributed ccording to p(x) F = f (x) dx = f (x) p(x) p(x) dx = 1 0 f [ x(y) ] f p [ x(y) ] dy = = 1 p y i=1 f [ x(y i ) ] p [ x(y i ) ] Exercise: mke the following integrl cos(x) dx expected = 0.412215 MC = 0.432225 +/- 0.025083 (100 devites generted) Wht bout using importnce smpling with pdf: p(x) e x? Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (319) Importnce smpling (cont.) The function PDF shpe mtters? Let s study the vrition of the integrl error with the prmeter of the exponentil Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (320)
Importnce smpling (cont.) Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (321) Simultion Simultion is very importnt for understnding rel situtions or for modelling the behviour of system it is lrgely used on prticle nd stroprticle physics for designing the instruments used for prticles detection the vrious rel conditions the system hs cn be introduced esily in simulted process Suppose you hd to design detector system for detecting photons coming from Compton scttering on mteril? I ssume my gmm source emits bem very colimted long n xis (x for instnce) nd in between I hve block of mteril where Compton is going to hppen... Wht we need to know? Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (322)
Buffon s needle problem Buffon s needle problem is question first posed in the 18th century by Georges-Louis Leclerc where simultion cn help us lot! A needle of length l is thrown rndomly onto grid of prllel lines, seprted by distnce d, with d > l Wht is the probbility tht needle intersects line? Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (323) Buffon s needle problem The probbility of crossing the line it s the rtio between the needle "perpendiculr distnce", l sin θ 2 nd the lines seprtion distnce, d P(θ) = l sin θ 2 d All θ re likely, so we need to clculte n verge probbility for θ rnge: 2 [0, π] Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (324)
Generting rndom vribles The common problem is to hve vrible x distributed ccording given distribution function p(x) we re going to mke chnge of vrible such tht the number of events (rndoms) generted is independent of the used vrible d = p(x)dx = p(y)dy Suppose tht y is rndom vrible distributed in [0, 1] nd x strts t x 0 p(y) = 1 y 0 dy = x x p(x )dx y = p(x )dx x 0 x 0 For generting rndom vrible x in intervl [x 0, x 1 ] we just mke sure tht: x1 x 0 p(x)dx = 1 nd we invert the reltion bove (boxed) giving us x(y). Provided rndom y in [0, 1] we generte rndom x in [x 0, x 1 ]. Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (325) uniform [0,1] uniform [,b] Let s trnsform vrible y uniformly distributed in [0, 1] into vrible x uniformly distributed in [, b] ormliztion of p(x): p(x) = 1 b Trnsformtion: y = x 1 b dx = x b x = (b )y x = + (b ) y Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (326)
uniform [0,1] exponentil [0, ] Let s trnsform vrible y uniformly distributed in [0, 1] into vrible x distributed ccording to p(x) e x in [0, ] ormliztion of p(x): k + e x dx = 1 0 k [ e x] 0 = 1 k = 1 p(x) = e x Trnsformtion: y = x 0 e x dx = [ e x ] x = 1 0 e x x = ln(1 y) Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (327) rndoms gen: cceptnce-rejection For pplying the method before sid we need to integrte the pdf nd invert it In cse it is not possible we cn use the more generic but less efficient method of cceptnce-rejection For generting x vrible in the intervl [, b] distributed ccording to pdf p(x) wo do the following: generte uniform rndom x R between [, b] compute p(x R ) nd the rtio p(x R) p mx generte second rndom u R from U(0, 1) if u R p(x R) p mx ccept the vrible x R Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (328)
MC integrtion: cc-rej method introduced by von eumnn for integrting the function we define n envelope with n re A = (x mx x min ) f mx generte two rndom vribles x R in in rnge [x min, x mx ] f R in in rnge [0, f mx ] count the number of events R tht f R f (x R ) the integrl I = (x mx x min ) f mx R the integrl error σ I = (x mx x min ) f mx R ( 1 R ) Using 100 rndoms: cos(x) dx = 0.435 ± 0.037 Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (329) rndom gen: f (x) = 3x 2 The cceptnce-rejection method is inefficient if the function vries quickly The frction of rndoms ccepted (efficiency) is given by: ε = f (x) dx (b ) f mx The efficiency cn be improved using n uxilir function S (x) tht hs shpe close to the one we wnt to smple ε S = f (x) dx q(x) dx where q(x) = C S (x) f (x) for x in [, b] efficiencies: ε = R 0.535 ε S = R 0.91 Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (330)
cc-rej with ux function For generting rndom vrible x distributed ccording to f (x) in the intervl [, b] 1. Find uxilir function S (x) with shpe close to the function f (x) we wnt to smple integrble, invertible from S (x) we define pdf p(x) nd we pply the trnsformtion y U[0, 1] x U(p(x)) invert trnsformtion eqution: x(y) using x(y) nd generting y uniformly between [0, 1] we obtin the rndom vrible x R distributed ccording to p(x) 2. Define function q(x) = C S (x) such tht q(x) f (x) in the intervl [, b] 3. Generte rndom vrible x R ccording to p(x) nd compute f (x R) q(x R ) 4. Generte rndom u R U[0, 1] nd ccept x R if: u R f (x R) q(x R ) Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (331) rndom gen: f (x) = 3x 2 We wnt to generte vrible ccording to function f (x) = 3x 2 1. Define the uxilir function S (x) to improve cceptnce-rejection efficiency 2. define q(x) which in this cse is = S (x) nd generte x R ccording to p(x) 3.,4. generte u R u[0, 1] nd ccept x R if u R f (x R) q(x R ) S (x) = 30x 63 generte rndom in [3,7] intervl ccording to uxilir function get pdf(x): S (x) p(x) with 7 3 p(x)dx = 1 k 7 1 S (x) dx = 1 k = 3 348 p(x) = 1 348 (30x 63) mke trnsformtion: y[0, 1] x ccording to p(x) y = x 3 p(x ) dx = x 1 3 348 (30x 63) dx Derive x(y) from: 15x 2 63x + 54 348y = 0 Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (332)
C++ clsses clss Func1D { public: Func1D(TF1 *fp=ull); // other constructors? ~Func1D(); void Drw(); double Evlute(); (...) protected: TF1 *p; //integrnd function }; clss IntegrtorMC: public Func1D { public: Integrtor(double fx0, double fx1, TF1 *fp=ull) : x0(fx0), x1(fx1), Func1D(fp) {;} ~Integrtor(); // set function void SetIntegrndFunction(TF1*); // simple integrtion void IntegrlMC(double xmin, double xmx, int, double& result, double& error); // importnce smpling void IntegrlMCIS(double xmin, double xmx, int, double& result, double& error, TF1* pdf); // cceptnce-rejection void IntegrlMCIS(double xmin, double xmx, int, double& result, double& error, TF1* pdf); // other methods (...) protected: double x0, x1; // integrnd limits }; Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (341) Computtionl Physics Universe prticle ccelertor Fernndo Bro, Phys Deprtment IST (Lisbon) Computtionl Physics 2018-19 (Phys Dep IST, Lisbon) Fernndo Bro (342)