Monte Carlo Methods Utilizing Mathematica®: Applications in Inverse Transform and Acceptance-Rejection Sampling 3031232933, 9783031232930

This book provides practical demonstrations of how to carry out definite integrals with Monte Carlo methods using Mathem

279 54 1MB

English Pages 150 Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
1
Preface
Contents
1 (1)
Part I Monte Carlo Methods Using Inverse Transform Sampling Utilizing Mathematica
978-3-031-23294-7_1
1 Introduction for Part I
1.1 Random Variable
1.2 Continuous Random Variable
1.3 Uniform Random Variable
1.4 Normal or Gaussian Random Variable
1.5 Generating Random Variates by Inverse Transform Method
1.6 Variance Reduction and Importance Sampling
978-3-031-23294-7_2
2 Evaluation of Definite Integrals Using Inverse Transform Sampling Utilizing Mathematica
2.1 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example I
2.2 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example II
2.3 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example III
2.4 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example IV
2.5 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example V
1 (2)
Part II Monte Carlo Methods Using Acceptance-Rejection Sampling Utilizing Mathematica
978-3-031-23294-7_3
3 Introduction for Part II
3.1 Random Variable
3.2 Continuous Random Variable
3.3 Uniform Random Variable
3.4 Normal or Gaussian Random Variable
3.5 Generating Random Variates by Acceptance-Rejection Method
3.6 Variance Reduction and Importance Sampling
978-3-031-23294-7_4
4 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling Utilizing Mathematica
4.1 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example I
4.2 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example II
4.3 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example III
4.4 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example IV
4.5 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example V
978-3-031-23294-7_5
5 Variational Monte Carlo Method Applied to Ground State of Simple Harmonic Oscillator Using Acceptance-Rejection Sampling Utilizing Mathematica
5.1 The Variational Method of Quantum Mechanics Applied to Ground State of Any Quantum Mechanical System
5.2 The Variational Method of Quantum Mechanics Applied to Ground State of Simple Harmonic Oscillator
5.3 Ground State Energy of Simple Harmonic Oscillator Using Variational Quantum Monte Carlo Method and Acceptance-Rejection Sampling
1 (3)
Concluding Remarks
Appendix: Handouts for Computational Lab and Automated Collection of Accepted Random Variates
A.1 Experiment I: Monte Carlo Integration Using Inverse Transform Sampling Using (a) Mathematica and (b) MS Excel
A.2 Experiment II: Monte Carlo Integration Using Acceptance-Rejection Sampling, Gaussian Pdf and Mathematica
A.3 Automated Collection of Accepted Random Variates
References
Recommend Papers

Monte Carlo Methods Utilizing Mathematica®: Applications in Inverse Transform and Acceptance-Rejection Sampling
 3031232933, 9783031232930

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Synthesis Lectures on Mathematics & Statistics Series Editor Steven G. Krantz, Department of Mathematics, Washington University, Saint Louis, MO, USA

This series includes titles in applied mathematics and statistics for cross-disciplinary STEM professionals, educators, researchers, and students. The series focuses on new and traditional techniques to develop mathematical knowledge and skills, an understanding of core mathematical reasoning, and the ability to utilize data in specific applications.

Sujaul Chowdhury

Monte Carlo Methods Utilizing Mathematica® Applications in Inverse Transform and Acceptance-Rejection Sampling

Sujaul Chowdhury Department of Physics Shahjalal University of Science and Technology Sylhet, Bangladesh

ISSN 1938-1743 ISSN 1938-1751 (electronic) Synthesis Lectures on Mathematics & Statistics ISBN 978-3-031-23293-0 ISBN 978-3-031-23294-7 (eBook) https://doi.org/10.1007/978-3-031-23294-7 © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland

Preface

This book on Monte Carlo Methods Using Inverse Transform and Acceptance-Rejection Sampling Utilizing Mathematica is intended for undergraduate students of Mathematics, Statistics, and Physics who wish to know how Monte Carlo methods work with calculations done using a user-friendly program Mathematica. All treatments have been done as less manually as practicable. All calculations except for gathering accepted values of random variate in acceptance-rejection sampling have been done using Mathematica. Definite integrals of a number of functions F(x) have been evaluated using uniform, linear, Gaussian and exponential probability density functions p(x). It is shown that results agree with known exact values better if p(x) is proportional to F(x). Deviation from the proportionality has been shown to result in worse agreement. All necessary background materials are covered. A separate chapter has been dedicated to variational Monte Carlo method applied to ground state of simple harmonic oscillator. Necessary background on variational method has been covered so that readers need not consult Quantum Mechanics textbooks. The book is on Monte Carlo methods. These are numerical methods for Computational Physics. These are parts of the syllabus for undergraduate students of Mathematics and Physics for the course titled “Computational Physics”. Besides the four referenced books, this is the only book to teach how basic Monte Carlo methods work. This is possibly the 1st Mathematica-based book on the topic. Pedagogical features: the book contains practical demonstrations of how to carry out definite integrals using Monte Carlo methods using Mathematica and using random variates sampled by inverse transform method and acceptance-rejection method. Moreover, the book contains a chapter on Variational Quantum Monte Carlo method applied to simple harmonic oscillator.

v

vi

Preface

Key words for the book are: Monte Carlo methods, Mathematica, basic Monte Carlo integration, Variational Quantum Monte Carlo method, simple harmonic oscillator, inverse transform sampling, and acceptance-rejection sampling.

Sylhet, Bangladesh 2022

Sujaul Chowdhury

Contents

Part I

Monte Carlo Methods Using Inverse Transform Sampling Utilizing Mathematica

1 Introduction for Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Continuous Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Uniform Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 Normal or Gaussian Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.5 Generating Random Variates by Inverse Transform Method . . . . . . . . . . . . 1.6 Variance Reduction and Importance Sampling . . . . . . . . . . . . . . . . . . . . . . . 2 Evaluation of Definite Integrals Using Inverse Transform Sampling Utilizing Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example IV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Evaluation of Definite Integrals Using Inverse Transform Sampling: Example V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part II

3 3 3 4 4 6 9 13 13 17 23 27 31

Monte Carlo Methods Using Acceptance-Rejection Sampling Utilizing Mathematica

3 Introduction for Part II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1 Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Continuous Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Uniform Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Normal or Gaussian Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45 45 45 46 46 vii

viii

Contents

3.5 Generating Random Variates by Acceptance-Rejection Method . . . . . . . . . 3.6 Variance Reduction and Importance Sampling . . . . . . . . . . . . . . . . . . . . . . . 4 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling Utilizing Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example IV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Variational Monte Carlo Method Applied to Ground State of Simple Harmonic Oscillator Using Acceptance-Rejection Sampling Utilizing Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 The Variational Method of Quantum Mechanics Applied to Ground State of Any Quantum Mechanical System . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 The Variational Method of Quantum Mechanics Applied to Ground State of Simple Harmonic Oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Ground State Energy of Simple Harmonic Oscillator Using Variational Quantum Monte Carlo Method and Acceptance-Rejection Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49 51 53 53 67 81 96 102

111 111 113

116

Concluding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

133

Appendix: Handouts for Computational Lab and Automated Collection of Accepted Random Variates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

135

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

149

Part I Monte Carlo Methods Using Inverse Transform Sampling Utilizing Mathematica

1

Introduction for Part I

This chapter provides background for Chap. 2. It introduces readers to continuous, uniform and Gaussian random variables. Inverse transform method of generating random variate for given probability distribution function, and concept of importance sampling are also introduced.

1.1

Random Variable

In the context of Monte Carlo methods, to define a random variable, we must indicate the values that the variable can assume and probabilities of occurrence of these values. We do not know value of the variable in any given case; but we do know the values that the variable can assume and the probabilities of occurrence of these values.

1.2

Continuous Random Variable

A random variable x is called continuous if it can assume any fractional value in a certain interval, say a to b. Besides specifying the interval containing all its possible values, we need to state a function p(x) called probability density function or probability distribution function. We have (1) (2) (3) (4)

p(x) ≥ 0 for a ≤ x ≤ b, the product p(x)dx is probability that x ≤ x ≤ x + dx,  b      p(x)d x is probability that a ≤ x ≤ b where a < a and b < b, ab a p(x)d x = 1 indicating the surety that x lies in the interval a to b.

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7_1

3

4

1 Introduction for Part I

As such, average or so called expectation value of x is given by A x = b and average value of any function of x, say f (x), is A f = a f (x) p(x)d x.

1.3

b a

x p(x)d x,

Uniform Random Variable

A random variable u in the interval 0 ≤ x ≤ 1 having constant, say C, probability density function p(x) = C is said to be a uniform random variable. The requirement that 1 1 0 p(x)d x = 1 gives 0 Cd x = 1 which gives C = 1. Thus probability density function of uniform random variable in the interval 0 to 1 is p(x) = 1. Program number 1.1 n=654321 SeedRandom[n] Table[{i=i+1,u[i] = RandomReal[]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]"}}] ListPlot[Table[{u[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"u [i]","1"}] Program number 1.1 in Mathematica 6.0 can provide us 100 uniform random numbers ui ’s in the interval 0–1 and the result is shown in Table 1.1 by rounding the numbers ui ’s to 4 decimal place in Microsoft Excel. In the program, value of n has been chosen by trial and error so that the random numbers ui ’s are almost uniformly distributed in the interval 0–1 as vindicated by Fig. 1.1 which displays the numbers ui ’s against a stagnant integer 1. This is a quality check of random numbers ui ’s. A benefit of using the command SeedRandom[n] in Program number 1.1 is that it enables us to get the values of ui ’s reproducibly.

1.4

Normal or Gaussian Random Variable

Probability density function p(x) for normal random variable x is given by     1 1 x −a 2 p(x) = √ exp − 2 σ σ 2π

(1.1)

defined for −∝ < x < +∝ . Average value of x is a and variance of x is σ 2 . According  a+3σ to so called rule of 3 sigma, a−3σ p(x)d x ≈ 0.997.

1.4

Normal or Gaussian Random Variable

5

Table 1.1 Showing 100 uniform random numbers ui ’s (in the interval 0–1) obtained using Program number 1.1 in Mathematica i

ui

i

ui

i

ui

i

ui

1

0.8618

26

0.9276

51

0.7833

76

0.8258

2

0.4287

27

0.0504

52

0.7877

77

0.4101

3

0.8596

28

0.3510

53

0.7012

78

0.6742

4

0.0342

29

0.1510

54

0.5937

79

0.2088

5

0.7597

30

0.1761

55

0.6616

80

0.8631

6

0.4609

31

0.3796

56

0.5202

81

0.2042

7

0.4189

32

0.5239

57

0.9024

82

0.3027

8

0.4746

33

0.3586

58

0.0050

83

0.0504

9

0.3799

34

0.4202

59

0.2183

84

0.8463

10

0.1687

35

0.5641

60

0.2361

85

0.4134

11

0.1899

36

0.1372

61

0.4477

86

0.2451

12

0.0364

37

0.0774

62

0.7308

87

0.5565

13

0.3335

38

0.8924

63

0.3429

88

0.0084

14

0.4257

39

0.9848

64

0.2260

89

0.3532

15

0.3860

40

0.4854

65

0.1079

90

0.7547

16

0.2445

41

0.3080

66

0.4879

91

0.1188

17

0.5881

42

0.4945

67

0.1655

92

0.5403

18

0.2322

43

0.9390

68

0.1218

93

0.5235

19

0.6743

44

0.5923

69

0.3544

94

0.8228

20

0.9089

45

0.1921

70

0.3523

95

0.5526

21

0.8759

46

0.3830

71

0.3895

96

0.8279

22

0.3658

47

0.6754

72

0.0911

97

0.6770

23

0.2945

48

0.2944

73

0.9631

98

0.7266

24

0.6719

49

0.9737

74

0.5184

99

0.4398

25

0.9104

50

0.4501

75

0.6328

100

0.2243

Program number 1.2 n=654321 SeedRandom[n] Table[{i=i+1,g[i] = RandomReal[NormalDistribution[0,1]]}, {i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","g[i]"}}]

6

1 Introduction for Part I

2.0

1

1.5

1.0

0.5

0.0 0.0

0.2

0.4

0.6

0.8

1.0

Fig. 1.1 Uniform random numbers ui ’s of Table 1.1 plotted against a stagnant integer 1 to vindicate that ui ’s are indeed almost uniformly distributed in the interval 0–1

ListPlot[Table[{g[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"g [i]","1"}] For a = 0, σ = 1, Program number 1.2 in Mathematica 6.0 can provide us 100 Gaussian random numbers gi ’s in the interval −3 to +3 and the result is shown in Table 1.2 by rounding the numbers gi ’s to 4 decimal place in Microsoft Excel. In the program, value of n has been chosen by trial and error so that the random numbers gi ’s are so distributed in the interval −3 to +3 that most of the numbers are close to the average value x = 0 as vindicated by Fig. 1.2 which displays the numbers gi ’s against a stagnant integer 1. This is a quality check of random numbers gi ’s. A benefit of using the command SeedRandom[n] in Program number 1.2 is that it enables us to get the values of gi ’s reproducibly.

1.5

Generating Random Variates by Inverse Transform Method

Values of random variable η corresponding to a given probability density function p(x) defined for a ≤ x ≤ b can be obtained by solving Eq. (1.2):  η p(x)d x = u (1.2) a

1.5

Generating Random Variates by Inverse Transform Method

7

Table 1.2 Showing 100 Gaussian random numbers gi ’s (in the interval − 3 to + 3) obtained using program number 1.2 in Mathematica. For a = 0, σ = 1 i

gi

i

gi

i

gi

i

1

−0.4915

26

0.1642

51

0.0250

76

0.2147

2

0.5374

27

−0.7006

52

0.0436

77

−0.1304

3

−0.7192

28

−0.9017

53

0.6939

78

1.2606

4

−1.3024

29

0.4529

54

−0.5178

79

1.8331

5

0.6802

30

0.1524

55

0.9525

80

1.2424

6

1.7751

31

−0.1529

56

−1.5620

81

−0.6542

7

−1.3234

32

0.2197

57

0.7447

82

−0.6587

8

0.0478

33

−2.1041

58

0.5326

83

−0.7827

9

0.1149

34

1.3676

59

0.3137

84

1.1034

10

0.7463

35

−0.8635

60

0.5035

85

−0.1103

11

−0.3424

36

1.1543

61

−1.0072

86

−0.1061

12

−0.7370

37

−0.2724

62

1.6373

87

−0.2752

13

0.3892

38

0.4385

63

−0.4160

88

0.6360

14

−1.4486

39

−0.6121

64

−0.2576

89

0.6611

15

0.8703

40

1.1545

65

2.3177

90

0.6861

16

−1.3761

41

−0.5795

66

0.0741

91

1.1652

17

−1.2559

42

1.3903

67

−0.5673

92

2.6803

18

0.6966

43

0.0406

68

−0.0757

93

−0.8103

19

1.7650

44

1.0811

69

0.1267

94

−1.3781

20

−0.1744

45

0.0427

70

−1.0962

95

−0.2635

21

−1.5339

46

−1.9984

71

−0.2686

96

−0.3182

22

−0.2967

47

0.5023

72

0.7321

97

0.7618

23

−1.3473

48

0.5118

73

2.0250

98

0.9291

24

−0.2439

49

−0.1296

74

−0.5563

99

−0.9932

25

−0.2195

50

0.2063

75

0.7393

100

gi

0.4787

where u is uniform random variable distributed in the interval 0 ≤ x ≤ 1, see Table 1.1. We now verify this. x Let

y(x) =

p(x)d x a

Since p(x) > 0, and

b a

p(x)d x = 1, we have

(1.3)

8

1 Introduction for Part I

2.0

1

1.5

1.0

0.5

0.0 2

1

0

1

2

g Fig. 1.2 Gaussian random numbers gi ’s of Table 1.2 plotted against a stagnant integer 1 to vindicate that gi ’s are so distributed in the interval −3 to +3 that most of the numbers are close to the average value x = 0. For a = 0, σ = 1

a y(a) =

p(x)d x = 0 a

b p(x)d x = 1 emphasizing that p(x) is normalized,

y(b) = a

and y  (x) = p(x) > 0. This means that y monotonically increases from 0 to 1 for the interval a < x < b. See Fig. 1.3. Straight line y = u where 0 < u < 1 intersects the curve y(x) at Q giving us the η value of η which satisfies Eq. (1.2): a p(x)d x = u. p(a  < η < b ) = p(y(a  ) < u < y(b )) = y(b ) − y(a  ) b

a  p(x)d x −

= a

b p(x)d x =

a

p(x)d x. a

1.6 Variance Reduction and Importance Sampling

9

Fig. 1.3 Figure to help explain inverse transform method of obtaining random variate

η Thus random variable η which obeys Eq. (1.2): a p(x)d x = u has the probability density function p(x). We need to evaluate this integral and obtain values of η for values of uniform random variable u.

1.6

Variance Reduction and Importance Sampling

Let us consider a definite integral b I =

F(x)d x

(1.4)

a

b Let us re-write Eq. (1.4) as I = a

F(x) p(x)d x = p(x)

b f (x) p(x)d x

(1.5)

a

where f (x) = F(x)/p(x). As discussed in Sect. 1.2, average value of any function of x, say f (x), is given by b A f = a f (x) p(x)d x where p(x) is a normalized probability density function. Thus according to Eq. (1.5), value I of the integral is average value of F/p. We can choose any functional form for p(x), but as proved below, choosing p(x) as proportional to F(x) ensures that the variance of f (x) = F/p is small. This will ensure a b better value of the average and hence a better value of the integral I = a F(x)d x.

10

1 Introduction for Part I

Variance of f (x) is square of standard deviation. Variance of f (x) is given by V [ f (x)] = A[ ( f − A f )2 ]

(1.6)

where A stands for average value. Thus   V f (x) = A[ f 2 − 2 f A f + A2f ] = A f 2 − 2 A f A f + A2f = A f 2 − A2f b or, V f (x) =

⎛ f 2 (x) p(x)d x − ⎝

a

b

⎞2 f (x) p(x)d x ⎠ =

b

a

a

⎞2 ⎛ b b 2  F = dx − ⎝ F dx⎠ p a

⎛ b ⎞2  F2 F pd x − ⎝ pd x ⎠ p2 p a

a

b

F2 dx − I2 p

V f (x) =

Thus the variance is

(1.7)

a

If p(x) is taken as

p(x) =

|F(x)| b

(1.8)

|F(x)|d x

a

the variance given by Eq. (1.7) becomes V f (x) = ⎛ or, V f (x) = ⎝

b a

⎞⎛ |F(x)|d x ⎠ ⎝

b a



b a

F2

|F(x)| b a |F(x)|d x



|F(x)|d x ⎠ − I 2 = ⎝

b

dx − I2 ⎞2 |F(x)|d x ⎠ − I 2

(1.9)

a

b Equation (1.9) in conjunction with Eq. (1.4): I = a F(x)d x reveals that the variance vanishes if the integrand F(x) does not change sign. If it does, the variance will be small if condition laid by Eq. (1.8) is met. Thus the probability density function p(x) should be proportional to the integrand |F(x)|. This is the so called importance sampling. Looking back at Eq. (1.5), we find that value of the integral is approximately deter N F(xi ) mined by the value of the average I = N1 i=1 p(xi ) . As recommended by importance sampling, we need to take p(x) = C F(x) where C is a constant; we need to normalb b . Thus p(x) = ize p(x) first; a p(x)d x = 1 gives a C F(x)d x = 1 or, C =  b 1 a F(x)d x    b N F(xi ) N F(xi ) F(x) 1 b . The sum N1 i=1 = a F(x)d x. Thus the F(xi ) i=1 p(xi ) becomes N a

F(x)d x

b a F(x)d x

1.6 Variance Reduction and Importance Sampling

11

 N F(xi ) sum N1 i=1 p(xi ) equals the integral if probability density function p(x) is taken proportional to the integrand |F(x)|. Slight variation of p(x) from proportionality with |F(x)|  N F(xi ) will result in slight difference of N1 i=1 p(xi ) from actual value of the integral I = b a F(x)d x.

2

Evaluation of Definite Integrals Using Inverse Transform Sampling Utilizing Mathematica

After getting required background in Chap. 1, this chapter provides practical demonstrations of how to carry out definite integrals using Monte Carlo method using random variates obtained by inverse transform method. Random variates have been obtained corresponding to probability distribution functions uniform, linear and exponential in the variable. Programs written in Mathematica have been used in this regard.

2.1

Evaluation of Definite Integrals Using Inverse Transform Sampling: Example I

As Example I, we now take up the integral b I =

π F(x)d x =

a

Sin(x)d x

(2.1)

0

where F(x) = Sin(x). We re-write Eq. (2.1) as b I =

F(x) p(x)d x p(x)

(2.2)

a

which, as discussed in Sect. 1.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi )

(2.3)

i=1

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7_2

13

14

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). Here p(x) is a suitable probability density function which, as demonstrated in Sect. 1.6, at least should follow F(x), if not be proportional to F(x). See Fig. 2.1 in which we have plotted F(x) = Sin(x) along with normalized probability density function of a uniform random variable in the interval 0 to π given by p(x) = 1/π. Let p(x) = C, a constant. π Normalization requires that 0 Cd x = 1 which gives C = 1/π. Uniform p(x) = 1/π is not too different from a slowly varying function of x like Sin(x). Hence p(x) = 1/π is a good choice. We now generate random variate η obeying p(x) = 1/π in the interval 0 < x < π using η inverse transform sampling. Values of η are obtainable from Eq. (1.2): a p(x)d x = u. As such η = π u where u is uniform random variable of Table 1.1 in the interval 0 < x < 1. Using Program number 2.1 and resulting Table 2.1, we now evaluate the integral with the result 2.127 rather than 2. The difference is attributable to the fact that F(x) and p(x)

1.0

0.8

0.6

0.4

0.2

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

x Fig. 2.1 Showing F(x) = Sin(x) as undashed curve and p(x) = 1/π as dashed curve obtained using the command Plot[{Sin[x],1/Pi},{x,0,Pi},Frame->True, FrameLabel->{"x","F(x) = Sin(x) or p(x) = 1/ \[Pi]" }, PlotStyle->{{Black},{Dashed,Black}}] in Mathematica 6.0.

2.1

Evaluation of Definite Integrals Using Inverse Transform …

15

used are not proportional to each other. That random variate η is uniformly distributed in the interval 0 to π is evidenced by Fig. 2.2 which displays values of η against stagnant integer 1. Program number 2.1 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=Pi*u[i], y[i]=Sin[eta[i]]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[Pi*y[i]/100,{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"\[Eta] [i]","1"}] We now take up a linear variation for p(x) given by p(x) = C x where C is a constant. π Normalization requires that 0 C x d x = 1 which gives C = 2/π2 . Thus normalized p(x) = 2x/π2 . See Fig. 2.3 in which we have plotted F(x) = Sin(x) along with the normalized linear probability density function p(x) = 2x/π2 . The linear probability density function p(x) = 2x/π2 is not too different from a slowly varying function of x like Sin(x). Hence it is a good choice. We now generate random variate η obeying p(x) = 2x/π2 in the interval 0 < x < π using η inverse transform sampling. Values of η are obtainable from Eq. (1.2): a p(x)d x = u. As such η = π u1/2 where u is uniform random variable of Table 1.1 in the interval 0 < x < 1. Using Program number 2.2 and resulting Table 2.2, we now evaluate the integral with the result 2.104 rather than 2. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. That random variate η is distributed as p(x) = 2x/π2 in the interval 0 to π is evidenced by Fig. 2.4 which displays values of η against stagnant integer 1. We find that indeed there are more variates for larger values of η. Program number 2.2 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=Pi*Sqrt[u[i]], y[i]=(Sin[eta[i]])/eta[i]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[Pi^2*y[i]/(2*100),{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"\[Eta] [i]","1"}]

16

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.1 Showing ui , ηi = π ui , yi = Sin(ηi ) from which we get I = N1 i=1 p(xi ) = 1  N F(ηi ) = π  N Sin(η ) as an approximate value of the integral I =  b F(x)d x = i i=1 a N Nπ i=1 1/π Sin(x)d x = 2.127 whereas exact value is 2. Using p(x) = 1/π. Using prog. 2.1 0 i

ui

η i = π ui

yi = Sin(ηi )

i

ui

η i = π ui

yi = Sin(ηi )

1

0.8618

2.7075

0.4206

26

0.9276

2.9141

0.2255

2

0.4287

1.3469

0.9750

27

0.0504

0.1583

0.1577

3

0.8596

2.7005

0.4269

28

0.3510

1.1026

0.8924

4

0.0342

0.1076

0.1074

29

0.1510

0.4745

0.4569

5

0.7597

2.3865

0.6853

30

0.1761

0.5534

0.5256

6

0.4609

1.4478

0.9924

31

0.3796

1.1926

0.9293

7

0.4189

1.3160

0.9677

32

0.5239

1.6460

0.9972

8

0.4746

1.4909

0.9968

33

0.3586

1.1266

0.9030

9

0.3799

1.1935

0.9297

34

0.4202

1.3201

0.9687

10

0.1687

0.5300

0.5055

35

0.5641

1.7723

0.9798

11

0.1899

0.5967

0.5619

36

0.1372

0.4309

0.4177

12

0.0364

0.1145

0.1142

37

0.0774

0.2431

0.2407

13

0.3335

1.0476

0.8662

38

0.8924

2.8037

0.3315

14

0.4257

1.3373

0.9729

39

0.9848

3.0938

0.0478

15

0.3860

1.2128

0.9366

40

0.4854

1.5248

0.9989

16

0.2445

0.7681

0.6947

41

0.3080

0.9675

0.8235

17

0.5881

1.8477

0.9619

42

0.4945

1.5535

0.9999

18

0.2322

0.7295

0.6665

43

0.9390

2.9501

0.1904

19

0.6743

2.1184

0.8538

44

0.5923

1.8607

0.9583

20

0.9089

2.8555

0.2822

45

0.1921

0.6035

0.5675

21

0.8759

2.7517

0.3801

46

0.3830

1.2032

0.9332

22

0.3658

1.1492

0.9125

47

0.6754

2.1218

0.8520

23

0.2945

0.9253

0.7988

48

0.2944

0.9248

0.7985

24

0.6719

2.1108

0.8577

49

0.9737

3.0590

0.0825

25

0.9104

2.8601

0.2778

50

0.4501

1.4139

0.9877

51

0.7833

2.4609

0.6293

76

0.8258

2.5943

0.5204

52

0.7877

2.4748

0.6185

77

0.4101

1.2882

0.9603

53

0.7012

2.2028

0.8068

78

0.6742

2.1181

0.8539

54

0.5937

1.8653

0.9569

79

0.2088

0.6560

0.6099

55

0.6616

2.0785

0.8739

80

0.8631

2.7115

0.4170 (continued)

2.2

Evaluation of Definite Integrals Using Inverse Transform …

17

Table 2.1 (continued) i

ui

η i = π ui

yi = Sin(ηi )

i

ui

η i = π ui

yi = Sin(ηi )

56

0.5202

1.6341

0.9980

81

0.2042

0.6416

0.5984

57

0.9024

2.8351

0.3017

82

0.3027

0.9510

0.8140

58

0.0050

0.0157

0.0157

83

0.0504

0.1582

0.1575

59

0.2183

0.6857

0.6332

84

0.8463

2.6586

0.4644

60

0.2361

0.7417

0.6755

85

0.4134

1.2986

0.9632

61

0.4477

1.4065

0.9865

86

0.2451

0.7701

0.6962

62

0.7308

2.2957

0.7485

87

0.5565

1.7483

0.9843

63

0.3429

1.0771

0.8806

88

0.0084

0.0265

0.0265

64

0.2260

0.7100

0.6519

89

0.3532

1.1096

0.8955

65

0.1079

0.3390

0.3326

90

0.7547

2.3710

0.6966

66

0.4879

1.5328

0.9993

91

0.1188

0.3732

0.3646

67

0.1655

0.5199

0.4968

92

0.5403

1.6973

0.9920

68

0.1218

0.3827

0.3735

93

0.5235

1.6447

0.9973

69

0.3544

1.1133

0.8972

94

0.8228

2.5849

0.5284

70

0.3523

1.1068

0.8943

95

0.5526

1.7361

0.9864

71

0.3895

1.2236

0.9403

96

0.8279

2.6008

0.5148

72

0.0911

0.2863

0.2824

97

0.6770

2.1269

0.8493

73

0.9631

3.0256

0.1157

98

0.7266

2.2826

0.7572

74

0.5184

1.6286

0.9983

99

0.4398

1.3816

0.9822

75

0.6328

1.9880

0.9142

100

0.2243

0.7046

0.6477

2.2

Evaluation of Definite Integrals Using Inverse Transform Sampling: Example II

As Example II, we now take up the integral +π/2 

b I =

F(x)d x =

Cos(x)d x

(2.4)

−π/2

a

where F(x) = Cos(x). We re-write Eq. (2.4) as b I = a

F(x) p(x)d x p(x)

(2.5)

18

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

Fig. 2.2 Showing that random variate η is uniformly distributed in the interval 0 to π

which, as discussed in Sect. 1.6, implies that average value of F/p is the value of the integral, i.e. N 1  F(xi ) I = N p(xi )

(2.6)

i=1

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). Here p(x) is a suitable probability density function which, as demonstrated in Sect. 1.6, at least should follow F(x), if not be proportional to F(x). See Fig. 2.5 in which we have plotted F(x) = Cos(x) along with normalized probability density function of a uniform random variable in the interval −π/2 to +π/2 given by p(x) = 1/π. Let p(x) = C, a  +π/2 constant. Normalization requires that −π/2 Cd x = 1 which gives C = 1/π. Uniform p(x) = 1/π is not too different from a slowly varying function of x like Cos(x). Hence p(x) = 1/π is a good choice. We now generate random variate η obeying p(x) = 1/π in the interval −π/2 < x < +π/2 using inverse transform sampling. Values of η are obtainable from Eq. (1.2): η a p(x)d x = u. As such η = πu −π/2 where u is uniform random variable of Table 1.1 in the interval 0 < x < 1.

2.2

Evaluation of Definite Integrals Using Inverse Transform …

19

1.0

0.8

0.6

0.4

0.2

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

x Fig. 2.3 Showing F(x) = Sin(x) as undashed curve and p(x) = 2x/π2 as dashed curve obtained using the command Plot[{Sin[x],(2*x)/(Pi^2)},{x,0,Pi},Frame->True, FrameLabel->{"x","F(x) = Sin(x) or p(x) "}, PlotStyle->{{Black},{Dashed,Black}}] in Mathematica 6.0.

Using Program number 2.3 and resulting Table 2.3, we now evaluate the integral with the result 2.127 rather than 2. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. That random variate η is uniformly distributed in the interval −π/2 < x < +π/2 is evidenced by Fig. 2.6 which displays values of η against stagnant integer 1. Program number 2.3 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=Pi*u[i]-Pi/2, y[i]=Cos[eta[i]]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[Pi*y[i]/100,{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}], Frame->True,FrameLabel->{"\[Eta] [i]","1"}]

20

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.2 Showing ui , ηi = π ui 1/2 , Sin(ηi )/ηi from which we get I = N1 i=1 p(xi ) = 1  N Sin(ηi ) = π 2  N Sin(ηi ) as an approximate value of the integral I =  b F(x)d x = i=1 2ηi /π 2 i=1 a ηi N 2N π 2 . Using prog. 2.2 Sin(x)d x = 2.104 whereas exact value is 2. Using p(x) = 2x/π 0 i

ui

ηi = π ui 1/2

yi = Sin(ηi )/ηi

i

ui

ηi = π ui 1/2

yi = Sin(ηi )/ηi

1

0.8618

2.9165

0.0765

26

0.9276

3.0257

0.0382

2

0.4287

2.0570

0.4298

27

0.0504

0.7053

0.9191

3

0.8596

2.9127

0.0779

28

0.3510

1.8611

0.5148

4

0.0342

0.5813

0.9446

29

0.1510

1.2209

0.7694

5

0.7597

2.7382

0.1434

30

0.1761

1.3185

0.7344

6

0.4609

2.1327

0.3968

31

0.3796

1.9357

0.4826

7

0.4189

2.0333

0.4401

32

0.5239

2.2740

0.3354

8

0.4746

2.1642

0.3831

33

0.3586

1.8813

0.5061

9

0.3799

1.9364

0.4823

34

0.4202

2.0365

0.4388

10

0.1687

1.2903

0.7447

35

0.5641

2.3596

0.2986

11

0.1899

1.3692

0.7156

36

0.1372

1.1635

0.7892

12

0.0364

0.5998

0.9411

37

0.0774

0.8740

0.8775

13

0.3335

1.8142

0.5350

38

0.8924

2.9678

0.0583

14

0.4257

2.0497

0.4330

39

0.9848

3.1176

0.0077

15

0.3860

1.9520

0.4755

40

0.4854

2.1887

0.3724

16

0.2445

1.5534

0.6437

41

0.3080

1.7434

0.5651

17

0.5881

2.4093

0.2775

42

0.4945

2.2092

0.3635

18

0.2322

1.5139

0.6595

43

0.9390

3.0443

0.0319

19

0.6743

2.5798

0.2065

44

0.5923

2.4178

0.2739

20

0.9089

2.9951

0.0487

45

0.1921

1.3770

0.7126

21

0.8759

2.9402

0.0680

46

0.3830

1.9442

0.4789

22

0.3658

1.9001

0.4980

47

0.6754

2.5819

0.2057

23

0.2945

1.7050

0.5812

48

0.2944

1.7045

0.5814

24

0.6719

2.5751

0.2084

49

0.9737

3.1000

0.0134

25

0.9104

2.9975

0.0479

50

0.4501

2.1076

0.4078

51

0.7833

2.7805

0.1271

76

0.8258

2.8549

0.0991

52

0.7877

2.7883

0.1241

77

0.4101

2.0117

0.4495

53

0.7012

2.6307

0.1859

78

0.6742

2.5796

0.2066

54

0.5937

2.4208

0.2726

79

0.2088

1.4356

0.6902 (continued)

2.2

Evaluation of Definite Integrals Using Inverse Transform …

21

Table 2.2 (continued) i

ui

ηi = π ui 1/2

yi = Sin(ηi )/ηi

i

ui

ηi = π ui 1/2

yi = Sin(ηi )/ηi

55

0.6616

2.5553

0.2165

80

0.8631

2.9186

0.0758

56

0.5202

2.2658

0.3390

81

0.2042

1.4197

0.6964

57

0.9024

2.9844

0.0525

82

0.3027

1.7284

0.5714

58

0.0050

0.2219

0.9918

83

0.0504

0.7050

0.9192

59

0.2183

1.4677

0.6777

84

0.8463

2.8900

0.0861

60

0.2361

1.5264

0.6545

85

0.4134

2.0198

0.4460

61

0.4477

2.1021

0.4101

86

0.2451

1.5555

0.6428

62

0.7308

2.6856

0.1640

87

0.5565

2.3436

0.3055

63

0.3429

1.8395

0.5241

88

0.0084

0.2885

0.9862

64

0.2260

1.4935

0.6676

89

0.3532

1.8670

0.5123

65

0.1079

1.0321

0.8317

90

0.7547

2.7292

0.1468

66

0.4879

2.1944

0.3699

91

0.1188

1.0828

0.8157

67

0.1655

1.2781

0.7492

92

0.5403

2.3092

0.3203

68

0.1218

1.0966

0.8113

93

0.5235

2.2731

0.3358

69

0.3544

1.8702

0.5109

94

0.8228

2.8497

0.1010

70

0.3523

1.8647

0.5133

95

0.5526

2.3354

0.3090

71

0.3895

1.9606

0.4718

96

0.8279

2.8584

0.0977

72

0.0911

0.9484

0.8567

97

0.6770

2.5849

0.2044

73

0.9631

3.0831

0.0190

98

0.7266

2.6779

0.1670

74

0.5184

2.2619

0.3407

99

0.4398

2.0834

0.4183

75

0.6328

2.4991

0.2398

100

0.2243

1.4878

0.6698

We now take up a linear variation for p(x) given by p(x) = C x + d where C and d are parameters such that slope C = intercept d on y axis/(π/2); hence d = Cπ /2. As such p(x)  +π/2 = C(x + π /2). Normalization of p(x) requires −π/2 p(x)d x = 1 or, C = 2/π2 . As such normalized probability density function is p(x) = 2x/π2 + 1/π. See Fig. 2.7 in which we have plotted F(x) = Cos(x) along with the normalized linear probability density function p(x) = 2x/π2 + 1/π. The linear probability density function p(x) = 2x/π2 +1/π is not too different from a slowly varying function of x like Cos(x). Hence it is a good choice. We now generate random variate η obeying p(x) = 2x/π2 + 1/π in the interval −π/2 < x < +π/2 using inverse transform sampling. Values of η are obtainable from Eq. (1.2): η η a p(x)d x = u or, −π/2 p(x)d x = u where u is uniform random variable of Table 1.1 in the interval 0 < x < 1. Thus we have η given by roots of the quadratic equation η2 + πη + (π2 /4−π2 u) = 0 which are η = −π/2 ± πu1/2 .

22

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

Fig. 2.4 Showing that random variate η is linearly distributed in the interval 0 to π. There are more variates for larger values of η

For η = −π/2 + πu1/2 , using Program number 2.4 and resulting Table 2.4, we now evaluate the integral with the result 2.104 rather than 2. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. That random variate η is distributed as p(x) = 2x/π2 + 1/π in the interval −π/2 to +π/2 is evidenced by Fig. 2.8 which displays values of η against stagnant integer 1. We find that indeed there are more variates for larger values of η. Program number 2.4 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=-Pi/2+Pi*Sqrt[u[i]], y[i]=(Cos[eta[i]])/(1/Pi+2*eta[i]/Pi^2)},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=(Sum[y[i],{i,1,100}])/100 ListPlot[Table[{eta[i],1},{i,0,99,1}],Frame->True,FrameLabel->{"\[Eta] [i]","1"}]

2.3

Evaluation of Definite Integrals Using Inverse Transform …

23

1.0

0.8

0.6

0.4

0.2

0.0 1.5

1.0

0.5

0.0

0.5

1.0

1.5

x Fig. 2.5 Showing F(x) = Cos(x) as undashed curve and p(x) = 1/π as dashed curve obtained using the command Plot[{Cos[x],1/Pi},{x,-Pi/2,Pi/2}, Frame->True,FrameLabel->{"x","F(x) = Cos(x) or p(x) "}, PlotStyle->{{Black},{Dashed,Black}}] in Mathematica 6.0.

2.3

Evaluation of Definite Integrals Using Inverse Transform Sampling: Example III

As Example III, we now take up the integral b I =

2 F(x)d x =

a

ex d x

(2.7)

0

where F(x) = ex . We re-write Eq. (2.7) as b I =

F(x) p(x)d x p(x)

(2.8)

a

which, as discussed in Sect. 1.6, implies that average value of F/p is the value of the integral, i.e.

24

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.3 Showing ui , ηi = π ui −π/2, Cos(ηi ) from which we get I = N1 i=1 p(xi ) = 1  N F(ηi ) = π  N Cos(η ) as an approximate value of the integral I =  b F(x)d x = i i=1 1/π i=1 a N N  +π/2 −π/2 Cos(x)d x = 2.127 whereas exact value is 2. Using p(x) = 1/π. Using prog. 2.3 i

ui

1

0.8618

2 3

ηi = π ui −π/2

ηi = πui −π/2

yi = Cos(ηi )

i

ui

yi = Cos(ηi )

1.1367

0.4206

26

0.9276

1.3433

0.2255

0.4287

−0.2239

0.9750

27

0.0504

−1.4125

0.1577

0.8596

1.1297

0.4269

28

0.3510

−0.4682

0.8924

4

0.0342

−1.4632

0.1074

29

0.1510

−1.0963

0.4569

5

0.7597

0.8157

0.6853

30

0.1761

−1.0174

0.5256

6

0.4609

−0.1230

0.9924

31

0.3796

−0.3782

0.9293

7

0.4189

−0.2548

0.9677

32

0.5239

0.0752

0.9972

8

0.4746

−0.0799

0.9968

33

0.3586

−0.4442

0.9030

9

0.3799

−0.3773

0.9297

34

0.4202

−0.2507

0.9687

10

0.1687

−1.0408

0.5055

35

0.5641

0.2015

0.9798

11

0.1899

−0.9741

0.5619

36

0.1372

−1.1399

0.4177

12

0.0364

−1.4563

0.1142

37

0.0774

−1.3277

0.2407

13

0.3335

−0.5232

0.8662

38

0.8924

1.2329

0.3315

14

0.4257

−0.2335

0.9729

39

0.9848

1.5230

0.0478

15

0.3860

−0.3580

0.9366

40

0.4854

−0.0460

0.9989

16

0.2445

−0.8027

0.6947

41

0.3080

−0.6033

0.8235

17

0.5881

0.2769

0.9619

42

0.4945

−0.0173

0.9999

18

0.2322

−0.8413

0.6665

43

0.9390

1.3793

0.1904

19

0.6743

0.5476

0.8538

44

0.5923

0.2899

0.9583

20

0.9089

1.2847

0.2822

45

0.1921

−0.9673

0.5675

21

0.8759

1.1809

0.3801

46

0.3830

−0.3676

0.9332

22

0.3658

−0.4216

0.9125

47

0.6754

0.5510

0.8520

23

0.2945

−0.6455

0.7988

48

0.2944

−0.6460

0.7985

24

0.6719

0.5400

0.8577

49

0.9737

1.4882

0.0825

25

0.9104

1.2893

0.2778

50

0.4501

−0.1569

0.9877

51

0.7833

0.8901

0.6293

76

0.8258

1.0235

0.5204

52

0.7877

0.9040

0.6185

77

0.4101

−0.2826

0.9603

53

0.7012

0.6320

0.8068

78

0.6742

0.5473

0.8539

54

0.5937

0.2945

0.9569

79

0.2088

−0.9148

0.6099 (continued)

2.3

Evaluation of Definite Integrals Using Inverse Transform …

25

Table 2.3 (continued) i

ui

55

0.6616

56 57

ηi = π ui −π/2

ηi = πui −π/2

yi = Cos(ηi )

i

ui

0.5077

0.8739

80

0.8631

1.1407

0.4170

0.5202

0.0633

0.9980

81

0.2042

−0.9292

0.5984

0.9024

1.2643

0.3017

82

0.3027

−0.6198

0.8140

58

0.0050

−1.5551

0.0157

83

0.0504

−1.4126

0.1575

59

0.2183

−0.8851

0.6332

84

0.8463

1.0878

0.4644

60

0.2361

−0.8291

0.6755

85

0.4134

−0.2722

0.9632

61

0.4477

−0.1643

0.9865

86

0.2451

−0.8007

0.6962

62

0.7308

0.7249

0.7485

87

0.5565

0.1775

0.9843

63

0.3429

−0.4937

0.8806

88

0.0084

−1.5443

0.0265

64

0.2260

−0.8608

0.6519

89

0.3532

−0.4612

0.8955

65

0.1079

−1.2318

0.3326

90

0.7547

0.8002

0.6966

66

0.4879

−0.0380

0.9993

91

0.1188

−1.1976

0.3646

67

0.1655

−1.0509

0.4968

92

0.5403

0.1265

0.9920

68

0.1218

−1.1881

0.3735

93

0.5235

0.0739

0.9973

69

0.3544

−0.4575

0.8972

94

0.8228

1.0141

0.5284

70

0.3523

−0.4640

0.8943

95

0.5526

0.1653

0.9864

71

0.3895

−0.3472

0.9403

96

0.8279

1.0300

0.5148

72

0.0911

−1.2845

0.2824

97

0.6770

0.5561

0.8493

73

0.9631

1.4548

0.1157

98

0.7266

0.7118

0.7572

74

0.5184

0.0578

0.9983

99

0.4398

−0.1892

0.9822

75

0.6328

0.4172

0.9142

100

0.2243

−0.8662

0.6477

N 1  F(xi ) I = N p(xi )

yi = Cos(ηi )

(2.9)

i=1

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). We now take up a linear variation for p(x) given by p(x) = C x where C is a constant. 2 Normalization of p(x) requires 0 p(x)d x = 1 or, C = 1/2. As such normalized probability density function is p(x) = x/2. See Fig. 2.9 in which we have plotted F(x) = ex along with the normalized linear probability density function p(x) = x/2. The linear probability density function p(x) = x/2 follows the function ex to some extent. Hence it is not a bad choice.

26

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 1.5

1.0

0.5

0.0

0.5

1.0

1.5

Fig. 2.6 Showing that random variate η is uniformly distributed in the interval −π/2 < x < +π/2

We now generate random variate η obeying p(x) = x/2 in the interval 0 < x < 2 using η inverse transform sampling. Values of η are obtainable from Eq. (1.2): a p(x)d x = u where u is random variable of Table 1.1. Thus we have η = 2 u1/2 . Using Program number 2.5 and resulting Table 2.5, we now evaluate the integral with the result 6.242 rather than 6.389. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. That random variate η is distributed as p(x) = x/2 in the interval 0 to 2 is evidenced by Fig. 2.10 which displays values of η against stagnant integer 1. We find that indeed there are more variates for larger values of η. Program number 2.5 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=2*Sqrt[u[i]], y[i]=(Exp[eta[i]])/eta[i]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[2*y[i]/100,{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"\[Eta] [i]","1"}]

2.4

Evaluation of Definite Integrals Using Inverse Transform …

27

Fig. 2.7 Showing F(x) = Cos(x) as undashed curve and p(x) = 2x/π2 + 1/π as dashed curve obtained using the command Plot[{Cos[x],2*x/Pi^2+1/Pi},{x,-Pi/2,Pi/2},Frame->True, FrameLabel->{"x","F(x) = Cos(x) or p(x) "}, PlotStyle->{{Black},{Dashed,Black}}] in Mathematica 6.0.

2.4

Evaluation of Definite Integrals Using Inverse Transform Sampling: Example IV

As Example IV, we now take up the integral b I =

5 F(x)d x =

a

loge xd x

(2.10)

1

where F(x) = loge x. We re-write Eq. (2.10) as b I =

F(x) p(x)d x p(x)

(2.11)

a

which, as discussed in Sect. 1.6, implies that average value of F/p is the value of the integral, i.e.

28

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.4 Showing ui , ηi = −π/2 + πui 1/2 , Cos(ηi )/pi from which we get I = N1 i=1 p(xi ) = b Cos(ηi ) 1  N Cos(ηi ) = 1  N i=1 i=1 2ηi /π 2 +1/π as an approximate value of the integral I = a F(x)d x = pi N N  π/2 2 −π/2 Cos(x)d x = 2.104 whereas exact value is 2. Using p(x) = 2x/π + 1/π. Using prog. 2.4 Cos(ηi )/ pi

i

ui

1.3457

0.3777

26

0.9276

1.4549

0.1886

0.4862

2.1210

27

0.0504

−0.8655

4.5357

0.8596

1.3419

0.3844

28

0.3510

0.2903

2.5406

4

0.0342

−0.9895

4.6615

29

0.1510

−0.3499

3.7970

5

0.7597

1.1674

0.7075

30

0.1761

−0.2523

3.6242

6

0.4609

0.5619

1.9581

31

0.3796

0.3649

2.3816

7

0.4189

0.4625

2.1719

32

0.5239

0.7032

1.6553

8

0.4746

0.5934

1.8904

33

0.3586

0.3105

2.4976

9

0.3799

0.3656

2.3801

34

0.4202

0.4657

2.1652

10

0.1687

−0.2805

3.6750

35

0.5641

0.7888

1.4738

11

0.1899

−0.2016

3.5312

36

0.1372

−0.4073

3.8943

12

0.0364

−0.9710

4.6442

37

0.0774

−0.6968

4.3301

13

0.3335

0.2434

2.6400

38

0.8924

1.3970

0.2875

14

0.4257

0.4789

2.1367

39

0.9848

1.5468

0.0380

15

0.3860

0.3812

2.3467

40

0.4854

0.6179

1.8378

16

0.2445

−0.0174

3.1764

41

0.3080

0.1726

2.7885

17

0.5881

0.8385

1.3694

42

0.4945

0.6384

1.7939

18

0.2322

−0.0569

3.2544

43

0.9390

1.4735

0.1574

19

0.6743

1.0090

1.0191

44

0.5923

0.8470

1.3517

20

0.9089

1.4243

0.2405

45

0.1921

−0.1938

3.5167

21

0.8759

1.3694

0.3358

46

0.3830

0.3734

2.3632

22

0.3658

0.3293

2.4575

47

0.6754

1.0111

1.0149

23

0.2945

0.1342

2.8684

48

0.2944

0.1337

2.8692

24

0.6719

1.0043

1.0284

49

0.9737

1.5292

0.0661

25

0.9104

1.4267

0.2363

50

0.4501

0.5368

2.0122

51

0.7833

1.2097

0.6270

76

0.8258

1.2841

0.4889

52

0.7877

1.2175

0.6123

77

0.4101

0.4409

2.2184

53

0.7012

1.0599

0.9173

78

0.6742

1.0088

1.0194

54

0.5937

0.8500

1.3455

79

0.2088

−0.1352

3.4062

i

ui

1

0.8618

2

0.4287

3

ηi = − π/2+πui 1/2

ηi = − π/2+πui 1/2

Cos(ηi )/ pi

(continued)

2.4

Evaluation of Definite Integrals Using Inverse Transform …

29

Table 2.4 (continued) Cos(ηi )/ pi

i

ui

0.9845

1.0684

80

0.8631

1.3478

0.3739

0.6950

1.6729

81

0.2042

−0.1511

3.4364

0.9024

1.4136

0.2589

82

0.3027

0.1576

2.8197

58

0.0050

−1.3489

4.8944

83

0.0504

−0.8658

4.5361

59

0.2183

−0.1031

3.3444

84

0.8463

1.3193

0.4250

60

0.2361

−0.0444

3.2297

85

0.4134

0.4490

2.2010

61

0.4477

0.5313

2.0240

86

0.2451

−0.0153

3.1722

62

0.7308

1.1148

0.8092

87

0.5565

0.7728

1.5075

63

0.3429

0.2687

2.5864

88

0.0084

−1.2823

4.8666

64

0.2260

−0.0773

3.2943

89

0.3532

0.2962

2.5280

65

0.1079

−0.5387

4.1043

90

0.7547

1.1584

0.7247

66

0.4879

0.6236

1.8255

91

0.1188

−0.4880

4.0255

67

0.1655

−0.2927

3.6969

92

0.5403

0.7384

1.5805

68

0.1218

−0.4742

4.0036

93

0.5235

0.7023

1.6572

69

0.3544

0.2994

2.5213

94

0.8228

1.2789

0.4984

70

0.3523

0.2939

2.5329

95

0.5526

0.7646

1.5249

71

0.3895

0.3898

2.3282

96

0.8279

1.2877

0.4823

72

0.0911

−0.6224

4.2276

97

0.6770

1.0142

1.0086

73

0.9631

1.5123

0.0936

98

0.7266

1.1071

0.8243

74

0.5184

0.6911

1.6811

99

0.4398

0.5126

2.0642

75

0.6328

0.9283

1.1832

100

0.2243

−0.0830

3.3055

i

ui

55

0.6616

56

0.5202

57

ηi = − π/2+πui 1/2

I =

N 1  F(xi ) N p(xi )

ηi = − π/2+πui 1/2

Cos(ηi )/ pi

(2.12)

i=1

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). We now take up an exponential variation for p(x) given by p(x) = C ex where C 5 is a constant. Normalization of p(x) requires 1 p(x)d x = 1 or, C = 1/145. As such normalized probability density function is p(x) = ex /145. See Fig. 2.11 in which we have plotted F(x) = loge x along with the normalized probability density function p(x) = ex /145. The probability density function p(x) = ex /145 follows the function loge x well. Hence it is a good choice.

30

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 1.0

0.5

0.0

0.5

1.0

1.5

Fig. 2.8 Showing that random variate η is linearly distributed in the interval −π/2 to +π/2. There are more variates for larger values of η

We now generate random variate η obeying p(x) = ex /145 in the interval 1 < x < 5 using η inverse transform sampling. Values of η are obtainable from Eq. (1.2): a p(x)d x = u η or, 1 p(x)d x = u. As such η = loge (145u + 2.72). Here u is uniform random variable of Table 1.1 in the interval 0–1. For ηi = loge (145ui + 2.72), using Program number 2.6 and resulting Table 2.6, we now evaluate the integral with the result 4.023 rather than 4.047. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. That random variate η is non-uniformly distributed like p(x) = ex /145 in the interval 1–5 is evidenced by Fig. 2.12 which displays values of η against stagnant integer 1. Program number 2.6 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=Log[145*u[i]+2.72], y[i]=(Log[eta[i]])/(Exp[eta[i]])},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2},

2.5

Evaluation of Definite Integrals Using Inverse Transform …

31

7 6 5 4 3 2 1 0 0.0

0.5

1.0

1.5

2.0

x Fig. 2.9 Showing F(x) = ex as undashed curve and p(x) = x/2 as dashed curve obtained using the command Plot[{Exp[x],x/2},{x,0,2},Frame->True, FrameLabel->{"x","F(x) = e^x or p(x)"}, PlotStyle->{{Black},{Dashed, Black}}] in Mathematica 6.0.

TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[145*y[i]/100,{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}], Frame->True,FrameLabel->{"\[Eta] [i]","1"}]

2.5

Evaluation of Definite Integrals Using Inverse Transform Sampling: Example V

As Example V, we now take up the integral b I =

3 F(x)d x =

a

0

1 dx 1 + x2

(2.13)

32

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.5 Showing tabulated values of ui , ηi = 2ui 1/2 , eηi /ηi from which we get I = N1 i=1 p(xi )     η η i i b 2 N N = N1 i=1 ηe /2 = N2 i=1 eηi as an approximate value of the integral I = a F(x)d x = 0 e x d x i

= 6.242 whereas exact value is 6.389. Using p(x) = x/2. Using prog. 2.5 i

ui

ηi = 2ui 1/2

eηi /ηi

i

ui

ηi = 2ui 1/2

eηi /ηi

1

0.8618

1.8567

3.4484

26

0.9276

1.9262

3.5632

2

0.4287

1.3095

2.8288

27

0.0504

0.4490

3.4894

3

0.8596

1.8543

3.4445

28

0.3510

1.1848

2.7600

4

0.0342

0.3701

3.9122

29

0.1510

0.7773

2.7989

5

0.7597

1.7432

3.2788

30

0.1761

0.8394

2.7579

6

0.4609

1.3577

2.8631

31

0.3796

1.2323

2.7827

7

0.4189

1.2945

2.8190

32

0.5239

1.4477

2.9380

8

0.4746

1.3778

2.8786

33

0.3586

1.1977

2.7657

9

0.3799

1.2327

2.7829

34

0.4202

1.2965

2.8202

10

0.1687

0.8215

2.7680

35

0.5641

1.5022

2.9900

11

0.1899

0.8716

2.7429

36

0.1372

0.7407

2.8316

12

0.0364

0.3818

3.8368

37

0.0774

0.5564

3.1352

13

0.3335

1.1549

2.7481

38

0.8924

1.8894

3.5013

14

0.4257

1.3049

2.8258

39

0.9848

1.9847

3.6665

15

0.3860

1.2427

2.7882

40

0.4854

1.3934

2.8911

16

0.2445

0.9889

2.7185

41

0.3080

1.1099

2.7336

17

0.5881

1.5338

3.0224

42

0.4945

1.4064

2.9019

18

0.2322

0.9638

2.7201

43

0.9390

1.9381

3.5836

19

0.6743

1.6423

3.1463

44

0.5923

1.5392

3.0281

20

0.9089

1.9068

3.5302

45

0.1921

0.8766

2.7410

21

0.8759

1.8718

3.4725

46

0.3830

1.2377

2.7856

22

0.3658

1.2097

2.7713

47

0.6754

1.6437

3.1479

23

0.2945

1.0854

2.7277

48

0.2944

1.0852

2.7276

24

0.6719

1.6394

3.1426

49

0.9737

1.9735

3.6463

25

0.9104

1.9083

3.5328

50

0.4501

1.3417

2.8513

51

0.7833

1.7701

3.3171

76

0.8258

1.8175

3.3872

52

0.7877

1.7751

3.3242

77

0.4101

1.2807

2.8103

53

0.7012

1.6747

3.1870

78

0.6742

1.6422

3.1461

54

0.5937

1.5411

3.0301

79

0.2088

0.9139

2.7290

55

0.6616

1.6268

3.1273

80

0.8631

1.8581

3.4505 (continued)

2.5

Evaluation of Definite Integrals Using Inverse Transform …

33

Table 2.5 (continued) i

ui

ηi = 2ui 1/2

eηi /ηi

i

ui

ηi = 2ui 1/2

eηi /ηi

56

0.5202

1.4424

2.9332

81

0.2042

0.9038

2.7318

57

0.9024

1.8999

3.5188

82

0.3027

1.1004

2.7312

58

0.0050

0.1413

8.1521

83

0.0504

0.4488

3.4903

59

0.2183

0.9344

2.7244

84

0.8463

1.8399

3.4218

60

0.2361

0.9718

2.7194

85

0.4134

1.2859

2.8135

61

0.4477

1.3382

2.8488

86

0.2451

0.9902

2.7184

62

0.7308

1.7097

3.2329

87

0.5565

1.4920

2.9799

63

0.3429

1.1711

2.7543

88

0.0084

0.1837

6.5418

64

0.2260

0.9508

2.7217

89

0.3532

1.1886

2.7616

65

0.1079

0.6570

2.9360

90

0.7547

1.7375

3.2708

66

0.4879

1.3970

2.8941

91

0.1188

0.6893

2.8903

67

0.1655

0.8136

2.7729

92

0.5403

1.4701

2.9587

68

0.1218

0.6981

2.8792

93

0.5235

1.4471

2.9374

69

0.3544

1.1906

2.7625

94

0.8228

1.8142

3.3822

70

0.3523

1.1871

2.7610

95

0.5526

1.4868

2.9748

71

0.3895

1.2482

2.7912

96

0.8279

1.8197

3.3907

72

0.0911

0.6038

3.0293

97

0.6770

1.6456

3.1503

73

0.9631

1.9627

3.6270

98

0.7266

1.7048

3.2263

74

0.5184

1.4400

2.9310

99

0.4398

1.3263

2.8403

75

0.6328

1.5910

3.0852

100

0.2243

0.9471

2.7222

where F(x) =

1 . 1+x 2

We re-write Eq. (2.13) as b I =

F(x) p(x)d x p(x)

(2.14)

a

which, as discussed in Sect. 1.6, implies that average value of F/p is the value of the integral, i.e. N 1  F(xi ) I = N p(xi )

(2.15)

i=1

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x).

34

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

Fig. 2.10 Showing that random variate η is linearly distributed in the interval 0–2. There are more variates for larger values of η

We now take up an exponential variation for p(x) given by p(x) = C e−x where C is a 3 constant. Normalization of p(x) requires 0 p(x)d x = 1 or, C = 1.05. As such normalized probability density function is p(x) = 1.05e−x . See Fig. 2.13 in which we have plotted 1 −x . The F(x) = 1+x 2 along with the normalized probability density function p(x) = 1.05 e

1 probability density function p(x) = 1.05e−x follows the function 1+x 2 quite well. Hence it is a very good choice. We now generate random variate η obeying p(x) = 1.05 e−x in the interval 0 < x < 3 η using inverse transform sampling. Values of η are obtainable from Eq. (1.2): a p(x)d x = η u or, 0 p(x)d x = u. As such η = −loge (1 − 0.95u) where u is uniform random variable of Table 1.1 in the interval 0 < x < 1. Using η = −loge (1 − 0.95u) and Program number 2.7 and resulting Table 2.7, we now evaluate the integral with the result 1.238 rather than 1.249. The absence of difference is attributable to the fact that F(x) and p(x) used are almost proportional to each other. That random variate η is non-uniformly distributed in the interval 0–3 is evidenced by Fig. 2.14 which displays values of η against stagnant integer 1.

2.5

Evaluation of Definite Integrals Using Inverse Transform …

35

1.5

1.0

0.5

0.0 1

2

3

4

5

x Fig. 2.11 Showing F(x) = loge x as undashed curve and p(x) = ex /145 as dashed curve obtained using the command a=1/145; p1=Plot[Log[E,x],{x,1,5},Frame->True, FrameLabel->{"x","F(x) = Log(x) or p(x)"}, PlotStyle->{Black}]; p2=Plot[a*Exp[x],{x,1,5},PlotStyle->{Dashed,Black}, Frame->True]; Show[p1,p2] in Mathematica 6.0.

Program number 2.7 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[],eta[i]=-Log[1-0.95*u[i]], y[i]=(Exp[eta[i]])/(1+eta[i]^2)},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]","eta[i]","y[i]"}}] Integral=Sum[0.95*y[i]/100,{i,1,100}] ListPlot[Table[{eta[i],1},{i,0,99,1}],Frame->True, FrameLabel->{"\[Eta] [i]","1"}]

36

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

 N F(xi ) Table 2.6 Showing ui , ηi = loge (145ui + 2.72), Loge ηi /eηi from which we get I = N1 i=1 p(xi )    η η Log Log b N N 145 e i e i = N1 i=1 as an approximate value of the integral I = a F(x)d x = i=1 eηi eηi /145 = N 5 x 1 Loge x d x = 4.023 whereas exact value is 4.047. Using p(x) = e /145. Using prog. 2.6 i

ui

ηi

Loge ηi eηi

i

ui

ηi

Loge ηi eηi

1

0.8618

4.8496

0.0124

26

0.9276

4.9216

0.0116

2

0.4287

4.1726

0.0220

27

0.0504

2.3054

0.0833

3

0.8596

4.8470

0.0124

28

0.3510

3.9817

0.0258

4

0.0342

2.0393

0.0927

29

0.1510

3.2036

0.0473

5

0.7597

4.7262

0.0138

30

0.1761

3.3415

0.0427

6

0.4609

4.2420

0.0208

31

0.3796

4.0564

0.0242

7

0.4189

4.1504

0.0224

32

0.5239

4.3655

0.0187

8

0.4746

4.2702

0.0203

33

0.3586

4.0022

0.0253

9

0.3799

4.0571

0.0242

34

0.4202

4.1534

0.0224

10

0.1687

3.3025

0.0440

35

0.5641

4.4370

0.0176

11

0.1899

3.4099

0.0405

36

0.1372

3.1184

0.0503

12

0.0364

2.0800

0.0915

37

0.0774

2.6349

0.0695

13

0.3335

3.9333

0.0268

38

0.8924

4.8837

0.0120

14

0.4257

4.1658

0.0221

39

0.9848

4.9803

0.0110

15

0.3860

4.0724

0.0239

40

0.4854

4.2918

0.0199

16

0.2445

3.6421

0.0339

41

0.3080

3.8581

0.0285

17

0.5881

4.4774

0.0170

42

0.4945

4.3097

0.0196

18

0.2322

3.5943

0.0352

43

0.9390

4.9336

0.0115

19

0.6743

4.6101

0.0152

44

0.5923

4.4842

0.0169

20

0.9089

4.9017

0.0118

45

0.1921

3.4202

0.0402

21

0.8759

4.8654

0.0122

46

0.3830

4.0648

0.0241

22

0.3658

4.0211

0.0250

47

0.6754

4.6117

0.0152

23

0.2945

3.8161

0.0295

48

0.2944

3.8156

0.0295

24

0.6719

4.6066

0.0153

49

0.9737

4.9692

0.0111 (continued)

2.5

Evaluation of Definite Integrals Using Inverse Transform …

37

Table 2.6 (continued) i

ui

ηi

Loge ηi eηi

i

ui

ηi

Loge ηi eηi

25

0.9104

4.9033

0.0118

50

0.4501

4.2192

0.0212

51

0.7833

4.7562

0.0134

76

0.8258

4.8078

0.0128

52

0.7877

4.7617

0.0133

77

0.4101

4.1300

0.0228

53

0.7012

4.6482

0.0147

78

0.6742

4.6100

0.0152

54

0.5937

4.4865

0.0169

79

0.2088

3.4964

0.0379

55

0.6616

4.5916

0.0155

80

0.8631

4.8510

0.0124

56

0.5202

4.3585

0.0188

81

0.2042

3.4760

0.0385

57

0.9024

4.8946

0.0119

82

0.3027

3.8418

0.0289

58

0.0050

1.2365

0.0616

83

0.0504

2.3047

0.0833

59

0.2183

3.5371

0.0368

84

0.8463

4.8317

0.0126

60

0.2361

3.6096

0.0347

85

0.4134

4.1377

0.0227

61

0.4477

4.2142

0.0213

86

0.2451

3.6446

0.0338

62

0.7308

4.6884

0.0142

87

0.5565

4.4238

0.0178

63

0.3429

3.9596

0.0262

88

0.0084

1.3720

0.0802

64

0.2260

3.5693

0.0359

89

0.3532

3.9877

0.0256

65

0.1079

2.9106

0.0582

90

0.7547

4.7199

0.0138

66

0.4879

4.2968

0.0198

91

0.1188

2.9930

0.0550

67

0.1655

3.2853

0.0445

92

0.5403

4.3952

0.0183

68

0.1218

3.0148

0.0541

93

0.5235

4.3648

0.0187

69

0.3544

3.9910

0.0256

94

0.8228

4.8042

0.0129

70

0.3523

3.9854

0.0257

95

0.5526

4.4170

0.0179

71

0.3895

4.0808

0.0238

96

0.8279

4.8102

0.0128

72

0.0911

2.7685

0.0639

97

0.6770

4.6140

0.0152

73

0.9631

4.9584

0.0112

98

0.7266

4.6828

0.0143

74

0.5184

4.3553

0.0189

99

0.4398

4.1970

0.0216

75

0.6328

4.5484

0.0160

100

0.2243

3.5621

0.0361

38

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

2.0

1

1.5

1.0

0.5

0.0 1.5

2.0

2.5

3.0

3.5

4.0

4.5

5.0

Fig. 2.12 Showing that random variate η is non-uniformly distributed like p(x) = ex /145 in the interval 1–5. There are more variates for larger values of η 1.0

0.8

0.6

0.4

0.2

0.0

0.0

0.5

1.0

1.5

2.0

2.5

3.0

x

Fig. 2.13 Showing F(x) = 1 2 as undashed curve and p(x) = 1.05 e−x as dashed curve obtained 1+x using the command p1=Plot[1/(1+x^2),{x,0,3},PlotRange->{0,1},Frame->True, FrameLabel->{"x","F(x) = 1/(1+x^2) or p(x)"}, PlotStyle->{Black}]; p2=Plot[1.05*Exp[-x],{x,0,3},PlotStyle->{Dashed,Black}]; Show[p1,p2] in Mathematica 6.0.

2.5

Evaluation of Definite Integrals Using Inverse Transform …

39

Table 2.7 Showing tabulated values of ui , ηi = −loge (1 − 0.95ui ), eηi /(1 + ηi2 ) from which we 2  N F(xi ) 1  N 1/(1+ηi ) = 0.95  N eηi get I = N1 i=1 i=1 1.05e−ηi i=1 (1+η2 ) as an approximate value of the N p(xi ) = N i   integral I = ab F(x)d x = 03 1 2 d x = 1.238 whereas exact value is 1.249. Using p(x) = 1.05

1+x

e−x . Using prog. 2.7 i

ui

ηi

eηi (1+ηi2 )

i

ui

ηi

eηi (1+ηi2 )

1

0.8618

1.7078

1.4086

26

0.9276

2.1304

1.5199

2

0.4287

0.5230

1.3247

27

0.0504

0.0491

1.0478

3

0.8596

1.6962

1.4065

28

0.3510

0.4056

1.2883

4

0.0342

0.0331

1.0325

29

0.1510

0.1549

1.1402

5

0.7597

1.2790

1.3632

30

0.1761

0.1831

1.1620

6

0.4609

0.5759

1.3357

31

0.3796

0.4473

1.3033

7

0.4189

0.5074

1.3209

32

0.5239

0.6887

1.3506

8

0.4746

0.5994

1.3397

33

0.3586

0.4166

1.2925

9

0.3799

0.4477

1.3035

34

0.4202

0.5095

1.3214

10

0.1687

0.1747

1.1556

35

0.5641

0.7677

1.3558

11

0.1899

0.1990

1.1737

36

0.1372

0.1396

1.1279

12

0.0364

0.0352

1.0346

37

0.0774

0.0764

1.0731

13

0.3335

0.3810

1.2782

38

0.8924

1.8827

1.4459

14

0.4257

0.5182

1.3236

39

0.9848

2.7418

1.8215

15

0.3860

0.4569

1.3064

40

0.4854

0.6182

1.3425

16

0.2445

0.2643

1.2175

41

0.3080

0.3461

1.2623

17

0.5881

0.8181

1.3576

42

0.4945

0.6344

1.3447

18

0.2322

0.2492

1.2080

43

0.9390

2.2264

1.5556

19

0.6743

1.0233

1.3591

44

0.5923

0.8271

1.3578

20

0.9089

1.9913

1.4753

45

0.1921

0.2015

1.1755

21

0.8759

1.7843

1.4235

46

0.3830

0.4523

1.3050

22

0.3658

0.4270

1.2963

47

0.6754

1.0262

1.3591

23

0.2945

0.3282

1.2535

48

0.2944

0.3280

1.2534

24

0.6719

1.0169

1.3591

49

0.9737

2.5907

1.7297 (continued)

40

2 Evaluation of Definite Integrals Using Inverse Transform Sampling …

Table 2.7 (continued) i

ui

ηi

eηi (1+ηi2 )

i

ui

ηi

eηi (1+ηi2 )

25

0.9104

2.0015

1.4783

50

0.4501

0.5578

1.3323

51

0.7833

1.3632

1.3675

76

0.8258

1.5348

1.3829

52

0.7877

1.3797

1.3686

77

0.4101

0.4936

1.3173

53

0.7012

1.0970

1.3593

78

0.6742

1.0231

1.3591

54

0.5937

0.8303

1.3579

79

0.2088

0.2211

1.1893

55

0.6616

0.9903

1.3591

80

0.8631

1.7145

1.4098

56

0.5202

0.6815

1.3499

81

0.2042

0.2157

1.1856

57

0.9024

1.9471

1.4627

82

0.3027

0.3391

1.2589

58

0.0050

0.0048

1.0047

83

0.0504

0.0490

1.0477

59

0.2183

0.2324

1.1970

84

0.8463

1.6294

1.3956

60

0.2361

0.2540

1.2110

85

0.4134

0.4987

1.3186

61

0.4477

0.5540

1.3315

86

0.2451

0.2651

1.2180

62

0.7308

1.1849

1.3604

87

0.5565

0.7522

1.3550

63

0.3429

0.3941

1.2837

88

0.0084

0.0080

1.0080

64

0.2260

0.2417

1.2031

89

0.3532

0.4088

1.2895

65

0.1079

0.1082

1.1014

90

0.7547

1.2622

1.3625

66

0.4879

0.6227

1.3431

91

0.1188

0.1197

1.1113

67

0.1655

0.1711

1.1528

92

0.5403

0.7200

1.3530

68

0.1218

0.1230

1.1140

93

0.5235

0.6879

1.3505

69

0.3544

0.4105

1.2902

94

0.8228

1.5216

1.3814

70

0.3523

0.4075

1.2890

95

0.5526

0.7444

1.3546

71

0.3895

0.4620

1.3081

96

0.8279

1.5440

1.3840

72

0.0911

0.0906

1.0859

97

0.6770

1.0305

1.3592

73

0.9631

2.4643

1.6620

98

0.7266

1.1719

1.3602

74

0.5184

0.6782

1.3496

99

0.4398

0.5409

1.3288

75

0.6328

0.9192

1.3590

100

0.2243

0.2396

1.2018

2.5

Evaluation of Definite Integrals Using Inverse Transform …

41

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

2.5

Fig. 2.14 Showing that random variate η is non-uniformly distributed in the interval 0 to 3. There are fewer variates for larger values of η as expected from p(x) = 1.05 e−x

Part II Monte Carlo Methods Using Acceptance-Rejection Sampling Utilizing Mathematica

3

Introduction for Part II

This chapter provides us with background for Chaps. 4 and 5. It introduces readers to continuous, uniform and Gaussian random variables. Acceptance-rejection method of generating random variate for given probability distribution function, and concept of importance sampling are also introduced. This chapter repeats much of Chap. 1; the purpose is to enable readers to read part I and part II of the book almost independently.

3.1

Random Variable

In the context of Monte Carlo methods, to define a random variable, we must indicate the values that the variable can assume and probabilities of occurrence of these values. We do not know value of the variable in any given case; but we do know the values that the variable can assume and the probabilities of occurrence of these values.

3.2

Continuous Random Variable

A random variable x is called continuous if it can assume any fractional value in a certain interval, say a to b. Besides specifying the interval containing all its possible values, we need to state a function p(x) called probability density function or probability distribution function. We have (1) p(x) ≥ 0 for a ≤ x ≤ b, (2) product p(x)dx is probability that x ≤ x ≤ x + dx,

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7_3

45

46

3 Introduction for Part II

 b (3) a  p(x)d x is probability that a ≤ x ≤ b where a < a and b < b, b (4) a p(x)d x = 1 indicating the surety that x lies in the interval a to b. As such, average or so called expectation value of x is given by A x = b and average value of any function of x, say f (x), is A f = a f (x) p(x)d x.

3.3

b a

x p(x)d x,

Uniform Random Variable

A random variable u in the interval 0 ≤ x ≤ 1 having constant, say C, probability density function p(x) = C is said to be a uniform random variable. The requirement that 1 1 0 p(x)d x = 1 gives 0 Cd x = 1 which gives C = 1. Thus probability density function of uniform random variable in the interval 0–1 is p(x) = 1. Program number 3.1 n=654321 SeedRandom[n] Table[{i=i+1,u[i]=RandomReal[]},{i,0,99,1}]; TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","u[i]"}}] ListPlot[Table[{u[i],1},{i,0,99,1}], Frame->True,FrameLabel->{"u [i]","1"}] Program number 3.1 in Mathematica 6.0 can provide us 100 uniform random numbers ui ’s in the interval 0–1 and the result is shown in Table 3.1 by rounding the numbers ui ’s to 4 decimal place in Microsoft Excel. In the program, value of n has been chosen by trial and error so that the random numbers ui ’s are almost uniformly distributed in the interval 0–1 as vindicated by Fig. 3.1 which displays the numbers ui ’s against a stagnant integer 1. This is a quality check of random numbers ui ’s. A benefit of using the command SeedRandom[n] in Program number 3.1 is that it enables us to get the values of ui ’s reproducibly.

3.4

Normal or Gaussian Random Variable

Probability density function p(x) for normal random variable x is given by     1 1 x −a 2 p(x) = √ exp − 2 σ σ 2π

(3.1)

3.4

Normal or Gaussian Random Variable

47

Table 3.1 Showing 100 uniform random numbers ui ’s (in the interval 0 to 1) obtained using program number 3.1 in Mathematica i

ui

i

ui

i

ui

i

ui

1

0.8618

26

0.9276

51

0.7833

76

0.8258

2

0.4287

27

0.0504

52

0.7877

77

0.4101

3

0.8596

28

0.3510

53

0.7012

78

0.6742

4

0.0342

29

0.1510

54

0.5937

79

0.2088

5

0.7597

30

0.1761

55

0.6616

80

0.8631

6

0.4609

31

0.3796

56

0.5202

81

0.2042

7

0.4189

32

0.5239

57

0.9024

82

0.3027

8

0.4746

33

0.3586

58

0.0050

83

0.0504

9

0.3799

34

0.4202

59

0.2183

84

0.8463

10

0.1687

35

0.5641

60

0.2361

85

0.4134

11

0.1899

36

0.1372

61

0.4477

86

0.2451

12

0.0364

37

0.0774

62

0.7308

87

0.5565

13

0.3335

38

0.8924

63

0.3429

88

0.0084

14

0.4257

39

0.9848

64

0.2260

89

0.3532

15

0.3860

40

0.4854

65

0.1079

90

0.7547

16

0.2445

41

0.3080

66

0.4879

91

0.1188

17

0.5881

42

0.4945

67

0.1655

92

0.5403

18

0.2322

43

0.9390

68

0.1218

93

0.5235

19

0.6743

44

0.5923

69

0.3544

94

0.8228

20

0.9089

45

0.1921

70

0.3523

95

0.5526

21

0.8759

46

0.3830

71

0.3895

96

0.8279

22

0.3658

47

0.6754

72

0.0911

97

0.6770

23

0.2945

48

0.2944

73

0.9631

98

0.7266

24

0.6719

49

0.9737

74

0.5184

99

0.4398

25

0.9104

50

0.4501

75

0.6328

100

0.2243

defined for −∝ < x < +∝ . Average value of x is a and variance of x is σ 2 . According  a+3σ to so called rule of 3 sigma, a−3σ p(x)d x ≈ 0.997. Program number 3.2 n=654321 SeedRandom[n] Table[{i=i+1,g[i] = RandomReal[NormalDistribution[0,1]]}, {i,0,99,1}];

48

3 Introduction for Part II

2.0

1

1.5

1.0

0.5

0.0 0.0

0.2

0.4

0.6

0.8

1.0

Fig. 3.1 Uniform random numbers ui ’s of Table 3.1 plotted against a stagnant integer 1 to vindicate that ui ’s are indeed almost uniformly distributed in the interval 0–1

TableForm[%,TableSpacing->{2,2}, TableHeadings->{None,{"i","g[i]"}}] ListPlot[Table[{g[i],1},{i,0,99,1}], Frame- >True,FrameLabel->{"g [i]","1"}] Program number 3.2 in Mathematica 6.0 can provide us 100 Gaussian random numbers gi ’s in the interval −3 to +3 and the result is shown in Table 3.2 by rounding the numbers gi ’s to 4 decimal place in Microsoft Excel. In the program, value of n has been chosen by trial and error so that the random numbers gi ’s are so distributed in the interval −3 to + 3 that most of the numbers are close to the average value x = 0 as vindicated by Fig. 3.2 which displays the numbers gi ’s against a stagnant integer 1. This is a quality check of random numbers gi ’s. A benefit of using the command SeedRandom[n] in Program number 3.2 is that it enables us to get the values of gi ’s reproducibly.

3.5

Generating Random Variates by Acceptance-Rejection Method

49

Table 3.2 Showing 100 Gaussian random numbers gi ’s (in the interval − 3 to + 3) obtained using program number 3.2 in Mathematica. For a = 0 and σ = 1 i

gi

1

−0.4915

26

0.1642

51

0.0250

76

0.2147

2

0.5374

27

−0.7006

52

0.0436

77

−0.1304

3

−0.7192

28

−0.9017

53

0.6939

78

1.2606

4

−1.3024

29

0.4529

54

−0.5178

79

1.8331

5

0.6802

30

0.1524

55

0.9525

80

1.2424

6

1.7751

31

−0.1529

56

−1.5620

81

−0.6542

7

−1.3234

32

0.2197

57

0.7447

82

−0.6587

8

0.0478

33

−2.1041

58

0.5326

83

−0.7827

9

0.1149

34

1.3676

59

0.3137

84

1.1034

10

0.7463

35

−0.8635

60

0.5035

85

−0.1103

11

−0.3424

36

1.1543

61

−1.0072

86

−0.1061

12

−0.7370

37

−0.2724

62

1.6373

87

−0.2752

13

0.3892

38

0.4385

63

−0.4160

88

0.6360

14

−1.4486

39

−0.6121

64

−0.2576

89

0.6611

15

0.8703

40

1.1545

65

2.3177

90

0.6861

16

−1.3761

41

−0.5795

66

0.0741

91

1.1652

17

−1.2559

42

1.3903

67

−0.5673

92

2.6803

18

0.6966

43

0.0406

68

−0.0757

93

−0.8103

19

1.7650

44

1.0811

69

0.1267

94

−1.3781

20

−0.1744

45

0.0427

70

−1.0962

95

−0.2635

21

−1.5339

46

−1.9984

71

−0.2686

96

−0.3182

22

−0.2967

47

0.5023

72

0.7321

97

0.7618

23

−1.3473

48

0.5118

73

2.0250

98

0.9291

24

−0.2439

49

−0.1296

74

−0.5563

99

−0.9932

25

−0.2195

50

0.2063

75

0.7393

3.5

i

gi

i

gi

i

100

gi

0.4787

Generating Random Variates by Acceptance-Rejection Method

See Fig. 3.3. Suppose, we wish to generate random variates x corresponding to a given probability distribution function p(x) defined for the interval x = a to b. Suppose, non-zero values of p(x) span from 0 to c. In acceptance-rejection method, 2 sets of uniform random numbers u1 ’s and u2 ’s in the interval 0–1 are first generated. Thereafter, linear transformations are carried out using

50

3 Introduction for Part II

2.0

1

1.5

1.0

0.5

0.0 2

1

0

1

2

Fig. 3.2 Gaussian random numbers gi ’s of Table 3.2 plotted against a stagnant integer 1 to vindicate that gi ’s are so distributed in the interval −3 to +3 that most of the numbers are close to the average value x = 0. For a = 0 and σ = 1 Fig. 3.3 Figure to help explain acceptance-rejection method of obtaining random variates for a given probability distribution function p(x)

p(x) c

(0, 0)

a

b

x

x

x + dx

U 1 = a + (b − a)u1 and U 2 = 0 + (c − 0)u2 known from sampling by inverse transform method, to get 2 sets of uniform random numbers U 1 ’s and U 2 ’s in the interval a to b and 0 to c respectively. The next steps are to calculate p(x = U 1 ) for each value of U 1 and if U 2 < p(x = U 1 ), that U 1 is accepted as a valid random variate for the probability distribution function p(x); otherwise that value of U 1 is rejected.

3.6 Variance Reduction and Importance Sampling

51

Validity of the method narrated above can be understood as follows. Since U 1 is a uniform random variable in the interval a to b, probability that U 1 falls in the interval x to x + dx is proportional to the width of the interval which is dx. Again, since U 2 is a uniform random variable in the interval 0 to c, probability that U 2 falls below p(x = U 1 ) is proportional to the interval p(x = U 1 ) − 0 = p (x = U 1 ). Thus the probability that U 1 is within x to x + dx and U 2 is below p(x = U 1 ) is the product of p(x = U 1 ) and dx which is p(x)dx.

3.6

Variance Reduction and Importance Sampling

Let us consider a definite integral b I =

F(x)d x

(3.2)

a

Let us re-write Eq. (3.2) as b I=

F(x) p(x)d x = p(x)

a

b f (x) p(x)d x

(3.3)

a

where f (x) = F(x)/ p(x). As discussed in Sect. 3.2, average value of any function of x, say f (x), is given by b A f = a f (x) p(x)d x where p(x) is a normalized probability density function. Thus according to Eq. (3.3), value I of the integral is average value of F/p. We can choose any functional form for p(x), but as proved below, choosing p(x) as proportional to F(x) ensures that variance of f (x) = F/p is small. This will ensure a b better value of the average and hence a better value of the integral I = a F(x)d x. Variance of f (x) is square of standard deviation. Variance of f (x) is given by V [ f (x)] = A[ ( f − A f )2 ]

(3.4)

where A stands for average value. Thus

or,

  V f (x) = A[ f 2 − 2 f A f + A2f ] = A f 2 − 2 A f A f + A2f = A f 2 − A2f ⎛ b ⎞2 b  V f (x) = f 2 (x) p(x)d x − ⎝ f (x) p(x)d x ⎠ a

b = a

⎛ F2 pd x − ⎝ p2

a

b a

⎞2 F p(x)d x ⎠ p

52

3 Introduction for Part II

b =

⎛ F2 p

a

dx − ⎝

b

⎞2 F dx⎠

a

Thus the variance is b V f (x) =

F2 dx − I2 p

(3.5)

a

If p(x) is taken as

p(x) =

|F(x)| b

(3.6)

|F(x)|d x

a

the variance given by Eq. (3.5) becomes V f (x) = ⎛ or, V f (x) = ⎝

b a

b a

F2

|F(x)| b a |F(x)|d x

dx − I2

⎛ b ⎞⎛ b ⎞ ⎞2   |F(x)|d x ⎠⎝ |F(x)|d x ⎠ − I 2 = ⎝ |F(x)|d x ⎠ − I 2 a

(3.7)

a

b Equation (3.7) in conjunction with Eq. (3.2): I = a F(x)d x reveals that the variance vanishes if the integrand F(x) does not change sign. If it does, the variance will be small if condition laid by Eq. (3.6) is met. Thus the probability density function p(x) should be proportional to the integrand |F(x)|. This is the so called importance sampling. Looking back at Eq. (3.3), we find that value of the integral is approximately  N F(xi ) determined by the value of the average I = N1 i=1 p(xi ) . As recommended by importance sampling, we need to take p(x) = C F(x) where C is a constant; we need to b b . Thus normalize p(x) first; a p(x)d x = 1 gives a C F(x)d x = 1 or, C =  b 1 F(x)d x  ba F(xi ) F(x) 1  N F(xi ) 1 N p(x) =  b . The sum N i=1 p(xi ) becomes N i=1 F(xi ) = a F(x)d x. Thus a

F(x)d x

N

F(xi ) i=1 p(xi )

b a F(x)d x

the sum N1 equals the integral if probability density function p(x) is taken proportional to the integrand |F(x)|. Slight variation of p(x) from proportionality with  N F(xi ) |F(x)| will result in slight difference of N1 i=1 p(xi ) from actual value of the integral b I = a F(x)d x.

4

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling Utilizing Mathematica

After getting required background in Chaps. 3, this chapter provides practical demonstrations of how to carry out definite integrals using Monte Carlo method using random variates obtained by acceptance-rejection method. Random variates have been obtained corresponding to probability distribution functions linear, Gaussian and exponential in the variable. Programs written in Mathematica have been used in this regard.

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example I

As example I, we now take up the integral b I =

π F(x)d x =

a

Sin(x)d x

(4.1)

0

where F(x) = Sin(x). We re-write Eq. (4.1) as b I =

F(x) p(x)d x p(x)

(4.2)

a

which, as discussed in Sect. 3.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi )

(4.3)

i=1

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7_4

53

54

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). We take up a linear variation for p(x) given by p(x) = C x where C is a constant. π Normalization requires that 0 C x d x = 1 which gives C = 2/π2 . Thus normalized p(x) = 2x/π2 . See Fig. 4.1 in which we have plotted F(x) = Sin(x) along with the normalized linear probability density function p(x) = 2x/π2 . The linear probability density function p(x) = 2x/π2 is not too different from a slowly varying function of x like Sin(x). Hence it is a good choice. We now generate random variate η obeying p(x) = 2x/π2 in the interval 0 < x < π using acceptance-rejection sampling, using Program number 4.1 and resulting Table 4.1. From Table 4.1, we manually gather accepted random variates ηa which we use in Program number 4.2 and resulting Table 4.2 to evaluate the integral with the result 2.095 rather than the exact value 2.

1.0

0.8

0.6

0.4

0.2

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

x Fig. 4.1 Showing F(x) = Sin(x) as undashed curve and p(x) = 2x/π2 as dashed curve obtained using the command Plot[{Sin[x],(2*x)/(Pi^2)},{x,0,Pi},Frame-> True, FrameLabel-> {"x", "F(x) = Sin(x) or p(x) "}, PlotStyle-> {{Black},{Dashed,Black}}] in Mathematica 6.0

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

55

Table 4.1 Showing values of u1[i], u2[i], U1[i], U2[i] and eta[i]. Using prog. 4.1 i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

1

0.8618

0.4287

2.7075

0.2787

2.7075

1

2

0.8596

0.0342

2.7005

0.0223

2.7005

2

3

0.7597

0.4609

2.3865

0.2996

2.3865

3

4

0.4189

0.4746

1.3160

0.3085

Null

4

5

0.3799

0.1687

1.1935

0.1097

1.1935

5

6

0.1899

0.0364

0.5967

0.0237

0.5967

6

7

0.3335

0.4257

1.0476

0.2767

Null

7

8

0.3860

0.2445

1.2128

0.1589

1.2128

8

9

0.5881

0.2322

1.8477

0.1509

1.8477

9

10

0.6743

0.9089

2.1184

0.5908

Null

10

11

0.8759

0.3658

2.7517

0.2378

2.7517

11

12

0.2945

0.6719

0.9253

0.4367

Null

12

13

0.9104

0.9276

2.8601

0.6029

Null

13

14

0.0504

0.3510

0.1583

0.2281

Null

14

15

0.1510

0.1761

0.4745

0.1145

Null

15

16

0.3796

0.5239

1.1926

0.3406

Null

16

17

0.3586

0.4202

1.1266

0.2731

Null

17

18

0.5641

0.1372

1.7723

0.0892

1.7723

18

19

0.0774

0.8924

0.2431

0.5801

Null

19

20

0.9848

0.4854

3.0938

0.3155

3.0938

20

21

0.3080

0.4945

0.9675

0.3214

Null

21

22

0.9390

0.5923

2.9501

0.3850

2.9501

22

23

0.1921

0.3830

0.6035

0.2490

Null

23

24

0.6754

0.2944

2.1218

0.1914

2.1218

24

25

0.9737

0.4501

3.0590

0.2925

3.0590

25

26

0.7833

0.7877

2.4609

0.5120

Null

26

27

0.7012

0.5937

2.2028

0.3859

2.2028

27

28

0.6616

0.5202

2.0785

0.3381

2.0785

28

29

0.9024

0.0050

2.8351

0.0032

2.8351

29

30

0.2183

0.2361

0.6857

0.1534

Null

30

31

0.4477

0.7308

1.4065

0.4750

Null

31

32

0.3429

0.2260

1.0771

0.1469

1.0771

32 (continued)

56

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.1 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

33

0.1079

0.4879

0.3390

0.3171

Null

33

34

0.1655

0.1218

0.5199

0.0792

0.5199

34

35

0.3544

0.3523

1.1133

0.2290

Null

35

36

0.3895

0.0911

1.2236

0.0592

1.2236

36

37

0.9631

0.5184

3.0256

0.3370

3.0256

37

38

0.6328

0.8258

1.9880

0.5368

Null

38

39

0.4101

0.6742

1.2882

0.4382

Null

39

40

0.2088

0.8631

0.6560

0.5610

Null

40

41

0.2042

0.3027

0.6416

0.1968

Null

41

42

0.0504

0.8463

0.1582

0.5501

Null

42

43

0.4134

0.2451

1.2986

0.1593

1.2986

43

44

0.5565

0.0084

1.7483

0.0055

1.7483

44

45

0.3532

0.7547

1.1096

0.4906

Null

45

46

0.1188

0.5403

0.3732

0.3512

Null

46

47

0.5235

0.8228

1.6447

0.5348

Null

47

48

0.5526

0.8279

1.7361

0.5381

Null

48

49

0.6770

0.7266

2.1269

0.4723

Null

49

50

0.4398

0.2243

1.3816

0.1458

1.3816

50

51

0.9524

0.7628

2.9922

0.4958

2.9922

51

52

0.9227

0.7673

2.8988

0.4988

2.8988

52

53

0.7744

0.0388

2.4329

0.0253

2.4329

53

54

0.4234

0.3146

1.3302

0.2045

1.3302

54

55

0.2849

0.8526

0.8952

0.5542

Null

55

56

0.2712

0.4589

0.8521

0.2983

Null

56

57

0.5662

0.1270

1.7787

0.0826

1.7787

57

58

0.3261

0.1921

1.0244

0.1249

1.0244

58

59

0.9294

0.0971

2.9197

0.0631

2.9197

59

60

0.8678

0.0527

2.7264

0.0342

2.7264

60

61

0.6010

0.5101

1.8880

0.3315

1.8880

61

62

0.0491

0.8662

0.1543

0.5630

Null

62

63

0.8311

0.3699

2.6111

0.2404

2.6111

63

64

0.6055

0.7086

1.9021

0.4606

Null

64 (continued)

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

57

Table 4.1 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

65

0.0621

0.0293

0.1951

0.0190

0.1951

65

66

0.9600

0.7918

3.0158

0.5146

3.0158

66

67

0.2790

0.3078

0.8764

0.2000

Null

67

68

0.9336

0.7173

2.9329

0.4662

2.9329

68

69

0.9645

0.1720

3.0300

0.1118

3.0300

69

70

0.1165

0.6613

0.3660

0.4299

Null

70

71

0.9249

0.6311

2.9056

0.4102

2.9056

71

72

0.6276

0.1130

1.9716

0.0734

1.9716

72

73

0.1215

0.9735

0.3816

0.6328

Null

73

74

0.0148

0.2807

0.0465

0.1824

Null

74

75

0.1744

0.8147

0.5479

0.5296

Null

75

76

0.9503

0.8674

2.9854

0.5638

2.9854

76

77

0.3100

0.7364

0.9738

0.4787

Null

77

78

0.2646

0.1094

0.8312

0.0711

0.8312

78

79

0.1454

0.9416

0.4569

0.6121

Null

79

80

0.3930

0.0684

1.2345

0.0445

1.2345

80

81

0.6325

0.3698

1.9870

0.2404

1.9870

81

82

0.3261

0.3225

1.0246

0.2096

Null

82

83

0.7350

0.4885

2.3089

0.3175

2.3089

83

84

0.4781

0.0687

1.5020

0.0447

1.5020

84

85

0.7332

0.7276

2.3035

0.4730

Null

85

86

0.2226

0.7403

0.6995

0.4812

Null

86

87

0.6044

0.2942

1.8987

0.1912

1.8987

87

88

0.3337

0.8206

1.0484

0.5334

Null

88

89

0.6196

0.1319

1.9464

0.0858

1.9464

89

90

0.4761

0.1548

1.4957

0.1006

1.4957

90

91

0.1495

0.1481

0.4696

0.0962

Null

91

92

0.0263

0.9820

0.0826

0.6383

Null

92

93

0.6774

0.4351

2.1281

0.2828

2.1281

93

94

0.3336

0.4401

1.0481

0.2861

Null

94

95

0.3045

0.2773

0.9565

0.1803

0.9565

95

96

0.9500

0.5186

2.9844

0.3371

2.9844

96 (continued)

58

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.1 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

97

0.7374

0.9651

2.3167

0.6273

Null

97

98

0.6385

0.9689

2.0060

0.6298

Null

98

99

0.5705

0.4434

1.7922

0.2882

1.7922

99

100

0.5668

0.8242

1.7808

0.5357

Null

100

Table 4.2 Showing etaa[i] and y[i] = (Sin(etaa[i]))/etaa[i]. Using prog. 4.2 i

etaa[i]

y[i]

i

etaa[i]

y[i]

1

2.7075

0.1553

26

2.4329

0.2675

2

2.7005

0.1581

27

1.3302

0.7301

3

2.3865

0.2872

28

1.7787

0.5501

4

1.1935

0.7789

29

1.0244

0.8341

5

0.5967

0.9417

30

2.9197

0.0754

6

1.2128

0.7723

31

2.7264

0.1480

7

1.8477

0.5206

32

1.8880

0.5032

8

2.7517

0.1381

33

2.6111

0.1938

9

1.7723

0.5528

34

0.1951

0.9937

10

3.0938

0.0154

35

3.0158

0.0416

11

2.9501

0.0645

36

2.9329

0.0706

12

2.1218

0.4015

37

3.0300

0.0368

13

3.0590

0.0270

38

2.9056

0.0805

14

2.2028

0.3663

39

1.9716

0.4670

15

2.0785

0.4204

40

2.9854

0.0521

16

2.8351

0.1064

41

0.8312

0.8888

17

1.0771

0.8176

42

1.2345

0.7647

18

0.5199

0.9556

43

1.9870

0.4603

19

1.2236

0.7685

44

2.3089

0.3204

20

3.0256

0.0382

45

1.5020

0.6642

21

1.2986

0.7417

46

1.8987

0.4986

22

1.7483

0.5630

47

1.9464

0.4779

23

1.3816

0.7109

48

1.4957

0.6667

24

2.9922

0.0497

49

2.1281

0.3988

25

2.8988

0.0829

50

0.9565

0.8544

51

2.9844

0.0524

52

1.7922

0.5443

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

Program number 4.1 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[], u2[i] = RandomReal[], U1[i] = 0 + Pi*u1[i],U2[i] = 0.65*u2[i], If[U2[i] < 2*U1[i]/Pi^2,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","eta[i]"}}] Program number 4.2 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + Pi*u1[i],U2[i] = 0.65*u2[i], If[U2[i] < 2*U1[i]/Pi^2,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","eta[i]"}}] etaa[1] = eta[1];etaa[2] = eta[2];etaa[3] = eta[3];etaa[4] = eta[5];etaa[5] = eta[6]; etaa[6] = eta[8];etaa[7] = eta[9];etaa[8] = eta[11];etaa[9] = eta[18]; etaa[10] = eta[20];etaa[11] = eta[22];etaa[12] = eta[24];etaa[13] = eta[25]; etaa[14] = eta[27];etaa[15] = eta[28];etaa[16] = eta[29];etaa[17] = eta[32]; etaa[18] = eta[34];etaa[19] = eta[36];etaa[20] = eta[37];etaa[21] = eta[43]; etaa[22] = eta[44];etaa[23] = eta[50];etaa[24] = eta[51];etaa[25] = eta[52]; etaa[26] = eta[53];etaa[27] = eta[54];etaa[28] = eta[57];etaa[29] = eta[58]; etaa[30] = eta[59];etaa[31] = eta[60];etaa[32] = eta[61];etaa[33] = eta[63]; etaa[34] = eta[65];etaa[35] = eta[66];etaa[36] = eta[68];etaa[37] = eta[69]; etaa[38] = eta[71];etaa[39] = eta[72];etaa[40] = eta[76];etaa[41] = eta[78]; etaa[42] = eta[80];etaa[43] = eta[81];etaa[44] = eta[83];etaa[45] = eta[84]; etaa[46] = eta[87];etaa[47] = eta[89];etaa[48] = eta[90];etaa[49] = eta[93]; etaa[50] = eta[95];etaa[51] = eta[96];etaa[52] = eta[99]; Table[{i = i + 1,etaa[i],y[i] = (Sin[etaa[i]])/etaa[i]},{i,0,51,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","etaa[i]","y[i]"}}] Integral = Sum[Pi^2*y[i]/(2*52),{i,1,52}] ListPlot[Table[{etaa[i],1},{i,0,51,1}], Frame-> True,FrameLabel-> {"\[Eta]a [i]","1"}]

59

60

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

Fig. 4.2 Showing that random variates ηa are linearly distributed in the interval 0 to π. There are more variates for larger values of ηa

That random variate ηa is distributed as p(x) = 2x/π2 in the interval 0 to π is evidenced by Fig. 4.2 which displays values of ηa against stagnant integer 1. We find that indeed there are more variates for larger values of ηa. We now up a normalized Gaussian variation for p(x) given by p(x) =  take  2 √1 exp − 1 x−a where a is average and σ is variance of x. See Fig. 4.3 in which 2 σ σ 2π we have plotted F(x) = Sin(x) along with the normalized Gaussian probability density function p(x). As Fig. 4.3 shows, the Gaussian probability density function p(x) with a = π/2 and σ = 0.5 is not too different from the function Sin(x). Hence it is ideal for use as p(x).   2  We now generate random variate g obeying p(g) = √1 exp − 21 g−a with a σ σ 2π = π/2 and σ = 0.5 in the interval 0 < g < π using acceptance-rejection sampling, using Program number 4.3 and resulting Table 4.3. From Table 4.3, we manually gather accepted random variates ga which we use in Program number 4.4 and resulting Table 4.4 to evaluate the integral with the result 2.132 rather than the exact value 2.

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

61

1.0

0.8

0.6

0.4

0.2

0.0 0.0

0.5

1.0

1.5

2.0

2.5

3.0

x   2  Fig. 4.3 Showing F(x) = Sin(x) as undashed curve and Gaussian p(x) = √1 exp − 21 x−a σ σ 2π as dashed curve obtained using the command sig = 0.5;a = Pi/2; p1 = Plot[Sin[x],{x,0,Pi},PlotStyle-> {Black},Frame-> True, FrameLabel-> {"x","F(x) = Sin (x) or Gaussian (x)"}]; p2 = Plot[(1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((x-a)/sig)^2],{x,0,Pi}, PlotStyle-> {Dashed,Black}];Show[p1,p2] in Mathematica 6.0

Program number 4.3 n = 654321 SeedRandom[n] sig = 0.5;a = Pi/2; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + Pi*u1[i],U2[i] = 0 + 0.8*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))* Exp[-0.5*((U1[i]-a)/sig)^2],g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","g[i]"}}]

62

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.3 Showing values of u1[i], u2[i], U1[i], U2[i] and g[i]. Using prog. 4.3 i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

1

0.8618

0.4287

2.7075

0.3430

Null

1

2

0.8596

0.0342

2.7005

0.0274

2.7005

2

3

0.7597

0.4609

2.3865

0.3687

Null

3

4

0.4189

0.4746

1.3160

0.3797

1.3160

4

5

0.3799

0.1687

1.1935

0.1350

1.1935

5

6

0.1899

0.0364

0.5967

0.0292

0.5967

6

7

0.3335

0.4257

1.0476

0.3406

1.0476

7

8

0.3860

0.2445

1.2128

0.1956

1.2128

8

9

0.5881

0.2322

1.8477

0.1858

1.8477

9

10

0.6743

0.9089

2.1184

0.7271

Null

10

11

0.8759

0.3658

2.7517

0.2927

Null

11

12

0.2945

0.6719

0.9253

0.5375

Null

12

13

0.9104

0.9276

2.8601

0.7421

Null

13

14

0.0504

0.3510

0.1583

0.2808

Null

14

15

0.1510

0.1761

0.4745

0.1409

Null

15

16

0.3796

0.5239

1.1926

0.4192

1.1926

16

17

0.3586

0.4202

1.1266

0.3362

1.1266

17

18

0.5641

0.1372

1.7723

0.1097

1.7723

18

19

0.0774

0.8924

0.2431

0.7140

Null

19

20

0.9848

0.4854

3.0938

0.3883

Null

20

21

0.3080

0.4945

0.9675

0.3956

Null

21

22

0.9390

0.5923

2.9501

0.4738

Null

22

23

0.1921

0.3830

0.6035

0.3064

Null

23

24

0.6754

0.2944

2.1218

0.2355

2.1218

24

25

0.9737

0.4501

3.0590

0.3600

Null

25

26

0.7833

0.7877

2.4609

0.6302

Null

26

27

0.7012

0.5937

2.2028

0.4750

Null

27

28

0.6616

0.5202

2.0785

0.4161

2.0785

28

29

0.9024

0.0050

2.8351

0.0040

2.8351

29

30

0.2183

0.2361

0.6857

0.1889

Null

30

31

0.4477

0.7308

1.4065

0.5846

1.4065

31

32

0.3429

0.2260

1.0771

0.1808

1.0771

32 (continued)

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

63

Table 4.3 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

33

0.1079

0.4879

0.3390

0.3903

Null

33

34

0.1655

0.1218

0.5199

0.0975

Null

34

35

0.3544

0.3523

1.1133

0.2819

1.1133

35

36

0.3895

0.0911

1.2236

0.0729

1.2236

36

37

0.9631

0.5184

3.0256

0.4147

Null

37

38

0.6328

0.8258

1.9880

0.6606

Null

38

39

0.4101

0.6742

1.2882

0.5394

1.2882

39

40

0.2088

0.8631

0.6560

0.6905

Null

40

41

0.2042

0.3027

0.6416

0.2422

Null

41

42

0.0504

0.8463

0.1582

0.6770

Null

42

43

0.4134

0.2451

1.2986

0.1961

1.2986

43

44

0.5565

0.0084

1.7483

0.0067

1.7483

44

45

0.3532

0.7547

1.1096

0.6038

Null

45

46

0.1188

0.5403

0.3732

0.4322

Null

46

47

0.5235

0.8228

1.6447

0.6582

1.6447

47

48

0.5526

0.8279

1.7361

0.6623

1.7361

48

49

0.6770

0.7266

2.1269

0.5812

Null

49

50

0.4398

0.2243

1.3816

0.1794

1.3816

50

51

0.9524

0.7628

2.9922

0.6102

Null

51

52

0.9227

0.7673

2.8988

0.6139

Null

52

53

0.7744

0.0388

2.4329

0.0311

2.4329

53

54

0.4234

0.3146

1.3302

0.2517

1.3302

54

55

0.2849

0.8526

0.8952

0.6821

Null

55

56

0.2712

0.4589

0.8521

0.3672

Null

56

57

0.5662

0.1270

1.7787

0.1016

1.7787

57

58

0.3261

0.1921

1.0244

0.1537

1.0244

58

59

0.9294

0.0971

2.9197

0.0777

Null

59

60

0.8678

0.0527

2.7264

0.0421

2.7264

60

61

0.6010

0.5101

1.8880

0.4081

1.8880

61

62

0.0491

0.8662

0.1543

0.6930

Null

62

63

0.8311

0.3699

2.6111

0.2959

Null

63

64

0.6055

0.7086

1.9021

0.5669

1.9021

64 (continued)

64

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.3 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

65

0.0621

0.0293

0.1951

0.0234

Null

65

66

0.9600

0.7918

3.0158

0.6334

Null

66

67

0.2790

0.3078

0.8764

0.2462

0.8764

67

68

0.9336

0.7173

2.9329

0.5738

Null

68

69

0.9645

0.1720

3.0300

0.1376

Null

69

70

0.1165

0.6613

0.3660

0.5291

Null

70

71

0.9249

0.6311

2.9056

0.5049

Null

71

72

0.6276

0.1130

1.9716

0.0904

1.9716

72

73

0.1215

0.9735

0.3816

0.7788

Null

73

74

0.0148

0.2807

0.0465

0.2246

Null

74

75

0.1744

0.8147

0.5479

0.6518

Null

75

76

0.9503

0.8674

2.9854

0.6939

Null

76

77

0.3100

0.7364

0.9738

0.5891

Null

77

78

0.2646

0.1094

0.8312

0.0875

0.8312

78

79

0.1454

0.9416

0.4569

0.7533

Null

79

80

0.3930

0.0684

1.2345

0.0547

1.2345

80

81

0.6325

0.3698

1.9870

0.2958

1.9870

81

82

0.3261

0.3225

1.0246

0.2580

1.0246

82

83

0.7350

0.4885

2.3089

0.3908

Null

83

84

0.4781

0.0687

1.5020

0.0550

1.5020

84

85

0.7332

0.7276

2.3035

0.5821

Null

85

86

0.2226

0.7403

0.6995

0.5922

Null

86

87

0.6044

0.2942

1.8987

0.2354

1.8987

87

88

0.3337

0.8206

1.0484

0.6565

Null

88

89

0.6196

0.1319

1.9464

0.1056

1.9464

89

90

0.4761

0.1548

1.4957

0.1238

1.4957

90

91

0.1495

0.1481

0.4696

0.1184

Null

91

92

0.0263

0.9820

0.0826

0.7856

Null

92

93

0.6774

0.4351

2.1281

0.3481

2.1281

93

94

0.3336

0.4401

1.0481

0.3521

1.0481

94

95

0.3045

0.2773

0.9565

0.2219

0.9565

95

96

0.9500

0.5186

2.9844

0.4149

Null

96 (continued)

4.1

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

65

Table 4.3 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

97

0.7374

0.9651

2.3167

0.7721

Null

97

98

0.6385

0.9689

2.0060

0.7751

Null

98

99

0.5705

0.4434

1.7922

0.3547

1.7922

99

100

0.5668

0.8242

1.7808

0.6593

1.7808

100

Table 4.4 Showing values of ga[i], p[i] and y[i] = (Sin(ga[i]))/p[i]. Using prog. 4.4 i

ga[i]

p[i]

y[i]

i

ga[i]

p[i]

y[i]

1

2.7005

0.0621

6.8699

26

1.7787

0.7318

1.3370

2

1.3160

0.7008

1.3810

27

1.0244

0.4391

1.9457

3

1.1935

0.6002

1.5489

28

2.7264

0.0552

7.3058

4

0.5967

0.1196

4.6977

29

1.8880

0.6525

1.4562

5

1.0476

0.4615

1.8769

30

1.9021

0.6406

1.4761

6

1.2128

0.6175

1.5168

31

0.8764

0.3041

2.5265

7

1.8477

0.6844

1.4054

32

1.9716

0.5786

1.5913

8

1.1926

0.5994

1.5504

33

0.8312

0.2672

2.7647

9

1.1266

0.5378

1.6791

34

1.2345

0.6364

1.4833

10

1.7723

0.7357

1.3318

35

1.9870

0.5642

1.6210

11

2.1218

0.4347

1.9599

36

1.0246

0.4393

1.9451

12

2.0785

0.4765

1.8339

37

1.5020

0.7904

1.2622

13

2.8351

0.0326

9.2479

38

1.8987

0.6435

1.4712

14

1.4065

0.7560

1.3050

39

1.9464

0.6017

1.5461

15

1.0771

0.4900

1.7970

40

1.4957

0.7889

1.2640

16

1.1133

0.5250

1.7089

41

2.1281

0.4287

1.9797

17

1.2236

0.6269

1.4999

42

1.0481

0.4620

1.8755

18

1.2882

0.6801

1.4120

43

0.9565

0.3751

2.1786

19

1.2986

0.6880

1.4000

44

1.7922

0.7234

1.3487

20

1.7483

0.7491

1.3139

45

1.7808

0.7306

1.3388

21

1.6447

0.7892

1.2636

22

1.7361

0.7554

1.3057

23

1.3816

0.7428

1.3223

24

2.4329

0.1805

3.6062

25

1.3302

0.7106

1.3667

66

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Program number 4.4 n = 654321 SeedRandom[n] sig = 0.5;a = Pi/2; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + Pi*u1[i],U2[i] = 0 + 0.8*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))* Exp[-0.5*((U1[i]-a)/sig)^2],g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}] ga[1] = g[2];ga[2] = g[4];ga[3] = g[5];ga[4] = g[6];ga[5] = g[7];ga[6] = g[8]; ga[7] = g[9];ga[8] = g[16];ga[9] = g[17];ga[10] = g[18];ga[11] = g[24]; ga[12] = g[28];ga[13] = g[29];ga[14] = g[31];ga[15] = g[32];ga[16] = g[35]; ga[17] = g[36];ga[18] = g[39];ga[19] = g[43];ga[20] = g[44];ga[21] = g[47]; ga[22] = g[48];ga[23] = g[50];ga[24] = g[53];ga[25] = g[54];ga[26] = g[57]; ga[27] = g[58];ga[28] = g[60];ga[29] = g[61];ga[30] = g[64];ga[31] = g[67]; ga[32] = g[72];ga[33] = g[78];ga[34] = g[80];ga[35] = g[81];ga[36] = g[82]; ga[37] = g[84];ga[38] = g[87];ga[39] = g[89];ga[40] = g[90];ga[41] = g[93]; ga[42] = g[94];ga[43] = g[95];ga[44] = g[99];ga[45] = g[100]; Table[{i = i + 1,ga[i], p[i] = (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((ga[i]-a)/sig)^2], y[i] = (Sin[ga[i]])/p[i]},{i,0,44,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","ga[i]", "p[i]","y[i]"}}] Integral = Sum[y[i]/(45),{i,1,45}] ListPlot[Table[{ga[i],1},{i,0,44,1}],Frame-> True, FrameLabel-> {"ga [i]","1"}]   2  That random variate ga is distributed as p(x) = √1 exp − 21 x−a in the interval σ σ 2π 0 to π is evidenced by Fig. 4.4 which displays values of ga against stagnant integer 1. We find that indeed there are more variates near ga = π /2.

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

67

2.0

1

1.5

1.0

0.5

0.0 1.0

1.5

2.0

2.5

ga Fig. 4.4 Showing random variates ga are distributed in the interval 0 to π as p(x)   that 2  1 1 x−a √ exp − 2 σ . There are more variates near ga = π/2

=

σ 2π

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example II

As example II, we now take up the integral +π/2 

b I =

F(x)d x =

Cos(x)d x

(4.4)

−π/2

a

where F(x) = Cos(x). We re-write Eq. (4.4) as b I =

F(x) p(x)d x p(x)

(4.5)

a

which, as discussed in Sect. 3.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi ) i=1

(4.6)

68

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). We now take up a linear variation for p(x) given by p(x) = C x + d where C and d are parameters such that slope C = intercept d on y axis /(π/2); hence d = Cπ /2. As  +π/2 such p(x) = C(x + π /2). Normalization of p(x) requires −π/2 p(x)d x = 1 or, C = 2/π2 . As such normalized probability density function is p(x) = 2x/π2 + 1/π. See Fig. 4.5 in which we have plotted F(x) = Cos(x) along with the normalized linear probability density function p(x) = 2x/π2 + 1/π. The linear probability density function p(x) = 2x/π2 + 1/π is not too different from a slowly varying function of x like Cos(x). Hence it is a good choice. We now generate random variate η obeying p(x) = 2x/π2 + 1/π in the interval −π/2 < x < +π/2 using acceptance-rejection sampling, using Program number 4.5 and resulting Table 4.5. From Table 4.5, we manually gather accepted random variates ηa which we

1.0

0.8

0.6

0.4

0.2

0.0 1.5

1.0

0.5

0.0

0.5

1.0

1.5

x Fig. 4.5 Showing F(x) = Cos(x) as undashed curve and p(x) = 2x/π2 + 1/π as dashed curve obtained using the command Plot[{Cos[x],2*x/Pi^2 + 1/Pi},{x,-Pi/2,Pi/2},Frame-> True, FrameLabel-> {"x","F(x) = Cos(x) or p(x)"}, PlotStyle-> {{Black},{Dashed,Black}}] in Mathematica 6.0

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

69

use in Program number 4.6 and resulting Table 4.6 to evaluate the integral with the result 2.095 rather than the exact value 2. Program number 4.5 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -Pi/2 + Pi*u1[i],U2[i] = 0 + 0.65*u2[i], If[U2[i] < 2*U1[i]/Pi^2 + 1/Pi,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","eta[i]"}}] Program number 4.6 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -Pi/2 + Pi*u1[i],U2[i] = 0 + 0.65*u2[i], If[U2[i] < 2*U1[i]/Pi^2 + 1/Pi,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","eta[i]"}}] etaa[1] = eta[1];etaa[2] = eta[2];etaa[3] = eta[3];etaa[4] = eta[5]; etaa[5] = eta[6];etaa[6] = eta[8];etaa[7] = eta[9];etaa[8] = eta[11]; etaa[9] = eta[18];etaa[10] = eta[20];etaa[11] = eta[22];etaa[12] = eta[24]; etaa[13] = eta[25];etaa[14] = eta[27];etaa[15] = eta[28];etaa[16] = eta[29]; etaa[17] = eta[32];etaa[18] = eta[34];etaa[19] = eta[36];etaa[20] = eta[37]; etaa[21] = eta[43];etaa[22] = eta[44];etaa[23] = eta[50];etaa[24] = eta[51]; etaa[25] = eta[52];etaa[26] = eta[53];etaa[27] = eta[54];etaa[28] = eta[57]; etaa[29] = eta[58];etaa[30] = eta[59];etaa[31] = eta[60];etaa[32] = eta[61]; etaa[33] = eta[63];etaa[34] = eta[65];etaa[35] = eta[66];etaa[36] = eta[68]; etaa[37] = eta[69];etaa[38] = eta[71];etaa[39] = eta[72];etaa[40] = eta[76]; etaa[41] = eta[78];etaa[42] = eta[80];etaa[43] = eta[81];etaa[44] = eta[83]; etaa[45] = eta[84];etaa[46] = eta[87];etaa[47] = eta[89];etaa[48] = eta[90]; etaa[49] = eta[93];etaa[50] = eta[95];etaa[51] = eta[96];etaa[52] = eta[99]; Table[{i = i + 1,etaa[i],p[i] = 2*etaa[i]/Pi^2 + 1/Pi, y[i] = (Cos[etaa[i]])/p[i]},{i,0,51,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","etaa[i]","p[i]", "(Cos[etaa[i]])/p[i]"}}] Integral = Sum[y[i]/(52),{i,1,52}]

70

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.5 Showing u1[i], u2[i], U1[i], U2[i] and eta[i]. Using prog. 4.5 i

u1[i]

u2[i]

1

0.8618

0.4287

2

0.8596

3

0.7597

4

U1[i]

U2[i]

eta[i]

i

1.1367

0.2787

1.1367

1

0.0342

1.1297

0.0223

1.1297

2

0.4609

0.8157

0.2996

0.8157

3

0.4189

0.4746

−0.2548

0.3085

Null

4

5

0.3799

0.1687

−0.3773

0.1097

−0.3773

5

6

0.1899

0.0364

−0.9741

0.0237

−0.9741

6

7

0.3335

0.4257

−0.5232

0.2767

Null

7

8

0.3860

0.2445

−0.3580

0.1589

−0.3580

8

9

0.5881

0.2322

0.2769

0.1509

0.2769

9

10

0.6743

0.9089

0.5476

0.5908

Null

10

11

0.8759

0.3658

1.1809

0.2378

1.1809

11

12

0.2945

0.6719

−0.6455

0.4367

Null

12

13

0.9104

0.9276

1.2893

0.6029

Null

13

14

0.0504

0.3510

−1.4125

0.2281

Null

14

15

0.1510

0.1761

−1.0963

0.1145

Null

15

16

0.3796

0.5239

−0.3782

0.3406

Null

16

17

0.3586

0.4202

−0.4442

0.2731

Null

17

18

0.5641

0.1372

0.2015

0.0892

0.2015

18

19

0.0774

0.8924

−1.3277

0.5801

Null

19

20

0.9848

0.4854

1.5230

0.3155

1.5230

20

21

0.3080

0.4945

−0.6033

0.3214

Null

21

22

0.9390

0.5923

1.3793

0.3850

1.3793

22

23

0.1921

0.3830

−0.9673

0.2490

Null

23

24

0.6754

0.2944

0.5510

0.1914

0.5510

24

25

0.9737

0.4501

1.4882

0.2925

1.4882

25

26

0.7833

0.7877

0.8901

0.5120

Null

26

27

0.7012

0.5937

0.6320

0.3859

0.6320

27

28

0.6616

0.5202

0.5077

0.3381

0.5077

28

29

0.9024

0.0050

1.2643

0.0032

1.2643

29

30

0.2183

0.2361

−0.8851

0.1534

Null

30

31

0.4477

0.7308

−0.1643

0.4750

Null

31

32

0.3429

0.2260

−0.4937

0.1469

−0.4937

32 (continued)

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

71

Table 4.5 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

33

0.1079

0.4879

−1.2318

0.3171

Null

33

34

0.1655

0.1218

−1.0509

0.0792

−1.0509

34

35

0.3544

0.3523

−0.4575

0.2290

Null

35

36

0.3895

0.0911

−0.3472

0.0592

−0.3472

36

37

0.9631

0.5184

1.4548

0.3370

1.4548

37

38

0.6328

0.8258

0.4172

0.5368

Null

38

39

0.4101

0.6742

−0.2826

0.4382

Null

39

40

0.2088

0.8631

−0.9148

0.5610

Null

40

41

0.2042

0.3027

−0.9292

0.1968

Null

41

42

0.0504

0.8463

−1.4126

0.5501

Null

42

43

0.4134

0.2451

−0.2722

0.1593

−0.2722

43

44

0.5565

0.0084

0.1775

0.0055

0.1775

44

45

0.3532

0.7547

−0.4612

0.4906

Null

45

46

0.1188

0.5403

−1.1976

0.3512

Null

46

47

0.5235

0.8228

0.0739

0.5348

Null

47

48

0.5526

0.8279

0.1653

0.5381

Null

48

49

0.6770

0.7266

0.5561

0.4723

Null

49

50

0.4398

0.2243

−0.1892

0.1458

−0.1892

50

51

0.9524

0.7628

1.4214

0.4958

1.4214

51

52

0.9227

0.7673

1.3281

0.4988

1.3281

52

53

0.7744

0.0388

0.8621

0.0253

0.8621

53

54

0.4234

0.3146

−0.2406

0.2045

−0.2406

54

55

0.2849

0.8526

−0.6756

0.5542

Null

55

56

0.2712

0.4589

−0.7187

0.2983

Null

56

57

0.5662

0.1270

0.2079

0.0826

0.2079

57

58

0.3261

0.1921

−0.5464

0.1249

−0.5464

58

59

0.9294

0.0971

1.3489

0.0631

1.3489

59

60

0.8678

0.0527

1.1556

0.0342

1.1556

60

61

0.6010

0.5101

0.3172

0.3315

0.3172

61

62

0.0491

0.8662

−1.4165

0.5630

Null

62

63

0.8311

0.3699

1.0403

0.2404

1.0403

63

64

0.6055

0.7086

0.3313

0.4606

Null

64 (continued)

72

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.5 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

65

0.0621

0.0293

−1.3757

0.0190

−1.3757

65

66

0.9600

0.7918

1.4450

0.5146

1.4450

66

67

0.2790

0.3078

−0.6944

0.2000

Null

67

68

0.9336

0.7173

1.3621

0.4662

1.3621

68

69

0.9645

0.1720

1.4592

0.1118

1.4592

69

70

0.1165

0.6613

−1.2048

0.4299

Null

70

71

0.9249

0.6311

1.3348

0.4102

1.3348

71

72

0.6276

0.1130

0.4008

0.0734

0.4008

72

73

0.1215

0.9735

−1.1892

0.6328

Null

73

74

0.0148

0.2807

−1.5243

0.1824

Null

74

75

0.1744

0.8147

−1.0229

0.5296

Null

75

76

0.9503

0.8674

1.4146

0.5638

1.4146

76

77

0.3100

0.7364

−0.5970

0.4787

Null

77

78

0.2646

0.1094

−0.7396

0.0711

−0.7396

78

79

0.1454

0.9416

−1.1139

0.6121

Null

79

80

0.3930

0.0684

−0.3363

0.0445

−0.3363

80

81

0.6325

0.3698

0.4162

0.2404

0.4162

81

82

0.3261

0.3225

−0.5462

0.2096

Null

82

83

0.7350

0.4885

0.7381

0.3175

0.7381

83

84

0.4781

0.0687

−0.0688

0.0447

−0.0688

84

85

0.7332

0.7276

0.7327

0.4730

Null

85

86

0.2226

0.7403

−0.8713

0.4812

Null

86

87

0.6044

0.2942

0.3279

0.1912

0.3279

87

88

0.3337

0.8206

−0.5224

0.5334

Null

88

89

0.6196

0.1319

0.3756

0.0858

0.3756

89

90

0.4761

0.1548

−0.0751

0.1006

−0.0751

90

91

0.1495

0.1481

−1.1012

0.0962

Null

91

92

0.0263

0.9820

−1.4882

0.6383

Null

92

93

0.6774

0.4351

0.5573

0.2828

0.5573

93

94

0.3336

0.4401

−0.5227

0.2861

Null

94

95

0.3045

0.2773

−0.6143

0.1803

−0.6143

95

96

0.9500

0.5186

1.4136

0.3371

1.4136

96 (continued)

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

73

Table 4.5 (continued) i

u1[i]

u2[i]

97

0.7374

0.9651

98

0.6385

99

0.5705

100

0.5668

U1[i]

U2[i]

eta[i]

i

0.7459

0.6273

Null

97

0.9689

0.4352

0.6298

Null

98

0.4434

0.2214

0.2882

0.2214

99

0.8242

0.2100

0.5357

Null

100

Table 4.6 Showing etaa[i] and y[i] = (Cos(etaa[i]))/p[i]. Using prog. 4.6 i

etaa[i]

p[i]

y[i]

i

etaa[i]

p[i]

y[i]

1

1.1367

0.5487

0.7665

26

0.8621

0.4930

1.3202

2

1.1297

0.5472

0.7801

27

−0.2406

0.2695

3.6030

3

0.8157

0.4836

1.4171

28

0.2079

0.3604

2.7147

4

−0.3773

0.2419

3.8439

29

−0.5464

0.2076

4.1160

5

−0.9741

0.1209

4.6471

30

1.3489

0.5917

0.3720

6

−0.3580

0.2458

3.8110

31

1.1556

0.5525

0.7301

7

0.2769

0.3744

2.5690

32

0.3172

0.3826

2.4834

8

1.1809

0.5576

0.6817

33

1.0403

0.5291

0.9563

9

0.2015

0.3591

2.7281

34

−1.3757

0.0395

4.9035

10

1.5230

0.6269

0.0762

35

1.4450

0.6111

0.2052

11

1.3793

0.5978

0.3185

36

1.3621

0.5943

0.3485

12

0.5510

0.4300

1.9815

37

1.4592

0.6140

0.1814

13

1.4882

0.6199

0.1331

38

1.3348

0.5888

0.3971

14

0.6320

0.4464

1.8075

39

0.4008

0.3995

2.3045

15

0.5077

0.4212

2.0748

40

1.4146

0.6050

0.2571

16

1.2643

0.5745

0.5252

41

−0.7396

0.1684

4.3858

17

−0.4937

0.2183

4.0345

42

−0.3363

0.2502

3.7734

18

−1.0509

0.1054

4.7155

43

0.4162

0.4027

2.2715

19

−0.3472

0.2479

3.7925

44

0.7381

0.4679

1.5810

20

1.4548

0.6131

0.1887

45

−0.0688

0.3044

3.2777

21

−0.2722

0.2632

3.6601

46

0.3279

0.3848

2.4606

22

0.1775

0.3543

2.7782

47

0.3756

0.3944

2.3586

23

−0.1892

0.2800

3.5080

48

−0.0751

0.3031

3.2901

24

1.4214

0.6063

0.2455

49

0.5573

0.4312

1.9680

25

1.3281

0.5874

0.4092

50

−0.6143

0.1938

4.2161

51

1.4136

0.6048

0.2588

52

0.2214

0.3632

2.6862

74

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

2.0

1

1.5

1.0

0.5

0.0 1.0

0.5

0.0

0.5

1.0

1.5

Fig. 4.6 Showing that random variates ηa are linearly distributed in the interval −π/2 to +π/2. There are more variates for larger values of ηa

ListPlot[Table[{etaa[i],1},{i,0,51,1}], Frame-> True,FrameLabel-> {"\[Eta]a [i]","1"}] That random variate ηa is distributed as p(x) = 2x/π2 + 1/π in the interval −π/2 to +π/2 is evidenced by Fig. 4.6 which displays values of ηa against stagnant integer 1. We find that indeed there are more variates for larger values of ηa. We now up a normalized Gaussian variation for p(x) given by p(x) =  take  2 √1 exp − 1 x−a where a is average and σ is variance of x. See Fig. 4.7 in which 2 σ σ 2π we have plotted F(x) = Cos(x) along with the normalized Gaussian probability density function p(x). As Fig. 4.7 shows, the Gaussian probability density function p(x) with a = 0 and σ = 0.5 is not too different from the function Cos(x). Hence it is ideal for use as p(x).   2  We now generate random variate g obeying p(g) = √1 exp − 21 g−a with a = σ σ 2π 0 and σ = 0.5 in the interval −π/2 < g < +π/2 using acceptance-rejection sampling, using Program number 4.7 and resulting Table 4.7. From Table 4.7, we manually gather accepted random variates ga which we use in Program number 4.8 and resulting Table 4.8 to evaluate the integral with the result 2.132 rather than the exact value 2.

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

75

1.0

0.8

0.6

0.4

0.2

0.0 1.5

1.0

0.5

0.0 x

0.5

1.0

1.5

  2  Fig. 4.7 Showing F(x) = Cos(x) as undashed curve and Gaussian p(x) = √1 exp − 21 x−a σ σ 2π as dashed curve obtained using the command sig = 0.5; a = 0; p1 = Plot[Cos[x],{x,-Pi/2,Pi/2},PlotStyle-> {Black},Frame-> True, FrameLabel-> {"x","F(x) = Cos (x) or Gaussian (x)"}]; p2 = Plot[(1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((x-a)/sig)^2], {x,-Pi/2,Pi/2},PlotStyle-> {Dashed,Black}]; Show[p1,p2] in Mathematica 6.0

Program number 4.7 n = 654321 SeedRandom[n] sig = 0.5;a = 0; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -Pi/2 + Pi*u1[i],U2[i] = 0 + 0.8*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((U1[i]-a)/sig)^2], g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","g[i]"}}]

76

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.7 Showing u1[i], u2[i], U1[i], U2[i] and g[i]. Using prog. 4.7 i

u1[i]

u2[i]

1

0.8618

0.4287

2

0.8596

3

0.7597

4

U1[i]

U2[i]

g[i]

i

1.1367

0.3430

Null

1

0.0342

1.1297

0.0274

1.1297

2

0.4609

0.8157

0.3687

Null

3

0.4189

0.4746

−0.2548

0.3797

−0.2548

4

5

0.3799

0.1687

−0.3773

0.1350

−0.3773

5

6

0.1899

0.0364

−0.9741

0.0292

−0.9741

6

7

0.3335

0.4257

−0.5232

0.3406

−0.5232

7

8

0.3860

0.2445

−0.3580

0.1956

−0.3580

8

9

0.5881

0.2322

0.2769

0.1858

0.2769

9

10

0.6743

0.9089

0.5476

0.7271

Null

10

11

0.8759

0.3658

1.1809

0.2927

Null

11

12

0.2945

0.6719

−0.6455

0.5375

Null

12

13

0.9104

0.9276

1.2893

0.7421

Null

13

14

0.0504

0.3510

−1.4125

0.2808

Null

14

15

0.1510

0.1761

−1.0963

0.1409

Null

15

16

0.3796

0.5239

−0.3782

0.4192

−0.3782

16

17

0.3586

0.4202

−0.4442

0.3362

−0.4442

17

18

0.5641

0.1372

0.2015

0.1097

0.2015

18

19

0.0774

0.8924

−1.3277

0.7140

Null

19

20

0.9848

0.4854

1.5230

0.3883

Null

20

21

0.3080

0.4945

−0.6033

0.3956

Null

21

22

0.9390

0.5923

1.3793

0.4738

Null

22

23

0.1921

0.3830

−0.9673

0.3064

Null

23

24

0.6754

0.2944

0.5510

0.2355

0.5510

24

25

0.9737

0.4501

1.4882

0.3600

Null

25

26

0.7833

0.7877

0.8901

0.6302

Null

26

27

0.7012

0.5937

0.6320

0.4750

Null

27

28

0.6616

0.5202

0.5077

0.4161

0.5077

28

29

0.9024

0.0050

1.2643

0.0040

1.2643

29

30

0.2183

0.2361

−0.8851

0.1889

Null

30

31

0.4477

0.7308

−0.1643

0.5846

−0.1643

31

32

0.3429

0.2260

−0.4937

0.1808

−0.4937

32 (continued)

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

77

Table 4.7 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

33

0.1079

0.4879

−1.2318

0.3903

Null

33

34

0.1655

0.1218

−1.0509

0.0975

Null

34

35

0.3544

0.3523

−0.4575

0.2819

−0.4575

35

36

0.3895

0.0911

−0.3472

0.0729

−0.3472

36

37

0.9631

0.5184

1.4548

0.4147

Null

37

38

0.6328

0.8258

0.4172

0.6606

Null

38

39

0.4101

0.6742

−0.2826

0.5394

−0.2826

39

40

0.2088

0.8631

−0.9148

0.6905

Null

40

41

0.2042

0.3027

−0.9292

0.2422

Null

41

42

0.0504

0.8463

−1.4126

0.6770

Null

42

43

0.4134

0.2451

−0.2722

0.1961

−0.2722

43

44

0.5565

0.0084

0.1775

0.0067

0.1775

44

45

0.3532

0.7547

−0.4612

0.6038

Null

45

46

0.1188

0.5403

−1.1976

0.4322

Null

46

47

0.5235

0.8228

0.0739

0.6582

0.0739

47

48

0.5526

0.8279

0.1653

0.6623

0.1653

48

49

0.6770

0.7266

0.5561

0.5812

Null

49

50

0.4398

0.2243

−0.1892

0.1794

−0.1892

50

51

0.9524

0.7628

1.4214

0.6102

Null

51

52

0.9227

0.7673

1.3281

0.6139

Null

52

53

0.7744

0.0388

0.8621

0.0311

0.8621

53

54

0.4234

0.3146

−0.2406

0.2517

−0.2406

54

55

0.2849

0.8526

−0.6756

0.6821

Null

55

56

0.2712

0.4589

−0.7187

0.3672

Null

56

57

0.5662

0.1270

0.2079

0.1016

0.2079

57

58

0.3261

0.1921

−0.5464

0.1537

−0.5464

58

59

0.9294

0.0971

1.3489

0.0777

Null

59

60

0.8678

0.0527

1.1556

0.0421

1.1556

60

61

0.6010

0.5101

0.3172

0.4081

0.3172

61

62

0.0491

0.8662

−1.4165

0.6930

Null

62

63

0.8311

0.3699

1.0403

0.2959

Null

63

64

0.6055

0.7086

0.3313

0.5669

0.3313

64 (continued)

78

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.7 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

65

0.0621

0.0293

−1.3757

0.0234

Null

65

66

0.9600

0.7918

1.4450

0.6334

Null

66

67

0.2790

0.3078

−0.6944

0.2462

−0.6944

67

68

0.9336

0.7173

1.3621

0.5738

Null

68

69

0.9645

0.1720

1.4592

0.1376

Null

69

70

0.1165

0.6613

−1.2048

0.5291

Null

70

71

0.9249

0.6311

1.3348

0.5049

Null

71

72

0.6276

0.1130

0.4008

0.0904

0.4008

72

73

0.1215

0.9735

−1.1892

0.7788

Null

73

74

0.0148

0.2807

−1.5243

0.2246

Null

74

75

0.1744

0.8147

−1.0229

0.6518

Null

75

76

0.9503

0.8674

1.4146

0.6939

Null

76

77

0.3100

0.7364

−0.5970

0.5891

Null

77

78

0.2646

0.1094

−0.7396

0.0875

−0.7396

78

79

0.1454

0.9416

−1.1139

0.7533

Null

79

80

0.3930

0.0684

−0.3363

0.0547

−0.3363

80

81

0.6325

0.3698

0.4162

0.2958

0.4162

81

82

0.3261

0.3225

−0.5462

0.2580

−0.5462

82

83

0.7350

0.4885

0.7381

0.3908

Null

83

84

0.4781

0.0687

−0.0688

0.0550

−0.0688

84

85

0.7332

0.7276

0.7327

0.5821

Null

85

86

0.2226

0.7403

−0.8713

0.5922

Null

86

87

0.6044

0.2942

0.3279

0.2354

0.3279

87

88

0.3337

0.8206

−0.5224

0.6565

Null

88

89

0.6196

0.1319

0.3756

0.1056

0.3756

89

90

0.4761

0.1548

−0.0751

0.1238

−0.0751

90

91

0.1495

0.1481

−1.1012

0.1184

Null

91

92

0.0263

0.9820

−1.4882

0.7856

Null

92

93

0.6774

0.4351

0.5573

0.3481

0.5573

93

94

0.3336

0.4401

−0.5227

0.3521

−0.5227

94

95

0.3045

0.2773

−0.6143

0.2219

−0.6143

95

96

0.9500

0.5186

1.4136

0.4149

Null

96 (continued)

4.2

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

79

Table 4.7 (continued) i

u1[i]

u2[i]

97

0.7374

0.9651

98

0.6385

99

0.5705

100

0.5668

U1[i]

U2[i]

g[i]

i

0.7459

0.7721

Null

97

0.9689

0.4352

0.7751

Null

98

0.4434

0.2214

0.3547

0.2214

99

0.8242

0.2100

0.6593

0.2100

100

Table 4.8 Showing ga[i], p[i] and y[i] = (Cos(ga[i]))/p[i]. Using prog. 4.8 i

ga[i]

p[i]

y[i]

i

ga[i]

p[i]

y[i]

1

1.1297

0.0621

6.8699

26

0.2079

0.7318

1.3370

2

−0.2548

0.7008

1.3810

27

−0.5464

0.4391

1.9457

3

−0.3773

0.6002

1.5489

28

1.1556

0.0552

7.3058

4

−0.9741

0.1196

4.6977

29

0.3172

0.6525

1.4562

5

−0.5232

0.4615

1.8769

30

0.3313

0.6406

1.4761

6

−0.3580

0.6175

1.5168

31

−0.6944

0.3041

2.5265

7

0.2769

0.6844

1.4054

32

0.4008

0.5786

1.5913

8

−0.3782

0.5994

1.5504

33

−0.7396

0.2672

2.7647

9

−0.4442

0.5378

1.6791

34

−0.3363

0.6364

1.4833

10

0.2015

0.7357

1.3318

35

0.4162

0.5642

1.6210

11

0.5510

0.4347

1.9599

36

−0.5462

0.4393

1.9451

12

0.5077

0.4765

1.8339

37

−0.0688

0.7904

1.2622

13

1.2643

0.0326

9.2479

38

0.3279

0.6435

1.4712

14

−0.1643

0.7560

1.3050

39

0.3756

0.6017

1.5461

15

−0.4937

0.4900

1.7970

40

−0.0751

0.7889

1.2640

16

−0.4575

0.5250

1.7089

41

0.5573

0.4287

1.9797

17

−0.3472

0.6269

1.4999

42

−0.5227

0.4620

1.8755

18

−0.2826

0.6801

1.4120

43

−0.6143

0.3751

2.1786

19

−0.2722

0.6880

1.4000

44

0.2214

0.7234

1.3487

20

0.1775

0.7491

1.3139

45

0.2100

0.7306

1.3388

21

0.0739

0.7892

1.2636

22

0.1653

0.7554

1.3057

23

−0.1892

0.7428

1.3223

24

0.8621

0.1805

3.6062

25

−0.2406

0.7106

1.3667

80

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Program number 4.8 n = 654321 SeedRandom[n] sig = 0.5;a = 0; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -Pi/2 + Pi*u1[i],U2[i] = 0 + 0.8*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((U1[i]-a)/sig)^2], g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}] ga[1] = g[2];ga[2] = g[4];ga[3] = g[5];ga[4] = g[6];ga[5] = g[7];ga[6] = g[8]; ga[7] = g[9];ga[8] = g[16];ga[9] = g[17];ga[10] = g[18];ga[11] = g[24]; ga[12] = g[28];ga[13] = g[29];ga[14] = g[31];ga[15] = g[32];ga[16] = g[35]; ga[17] = g[36];ga[18] = g[39];ga[19] = g[43];ga[20] = g[44];ga[21] = g[47]; ga[22] = g[48];ga[23] = g[50];ga[24] = g[53];ga[25] = g[54];ga[26] = g[57]; ga[27] = g[58];ga[28] = g[60];ga[29] = g[61];ga[30] = g[64];ga[31] = g[67]; ga[32] = g[72];ga[33] = g[78];ga[34] = g[80];ga[35] = g[81];ga[36] = g[82]; ga[37] = g[84];ga[38] = g[87];ga[39] = g[89];ga[40] = g[90];ga[41] = g[93]; ga[42] = g[94];ga[43] = g[95];ga[44] = g[99];ga[45] = g[100]; Table[{i = i + 1,ga[i], p[i] = (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((ga[i]-a)/sig)^2], y[i] = (Cos[ga[i]])/p[i]},{i,0,44,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","ga[i]", "p[i]","y[i]"}}] Integral = Sum[y[i]/(45),{i,1,45}] ListPlot[Table[{ga[i],1},{i,0,44,1}],Frame-> True, FrameLabel-> {"ga [i]","1"}]   2  That random variate ga is distributed as p(x) = √1 exp − 21 x−a in the interval σ σ 2π −π/2 to +π/2 is evidenced by Fig. 4.8 which displays values of ga against stagnant integer 1. We find that indeed there are more variates near ga = 0 than elsewhere.

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

81

2.0

1

1.5

1.0

0.5

0.0 1.0

0.5

0.0

0.5

1.0

Fig. 4.8 Showing random variates ga are distributed in the interval −π/2 to +π/2 as p(x) =   that 2  √1 exp − 1 x−a . There are more variates near ga = 0 2 σ σ 2π

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example III

As example III, we now take up the integral b I =

2 F(x)d x =

a

ex d x

(4.7)

0

where F(x) = ex . We re-write Eq. (4.7) as b I =

F(x) p(x)d x p(x)

(4.8)

a

which, as discussed in Sect. 3.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi ) i=1

(4.9)

82

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

where x i ’s are random values of x in the interval a < x < b obeying probability density function p(x). We now take up a linear variation for p(x) given by p(x) = C x where C is a constant. 2 Normalization of p(x) requires 0 p(x)d x = 1 or, C = 1/2. As such normalized probability density function is p(x) = x/2. See Fig. 4.9 in which we have plotted F(x) = ex along with the normalized linear probability density function p(x) = x/2. The linear probability density function p(x) = x/2 follows the function ex to some extent. Hence it is not a bad choice. We now generate random variate η obeying p(x) = x/2 in the interval 0 < x < 2 using acceptance-rejection sampling, using Program number 4.9 and resulting Table 4.9. From Table 4.9, we manually gather accepted random variates ηa which we use in Program number 4.10 and resulting Table 4.10 to evaluate the integral with the result 6.460 rather than the exact value 6.39.

7 6 5 4 3 2 1 0 0.0

0.5

1.0

1.5

2.0

x Fig. 4.9 Showing F(x) = ex as undashed curve and p(x) = x/2 as dashed curve obtained using the command Plot[{Exp[x],x/2},{x,0,2},Frame-> True, FrameLabel-> {"x","F(x) = e^x or p(x)"}, PlotStyle-> {{Black},{Dashed, Black}}] in Mathematica 6.0

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

83

Table 4.9 Showing u1[i], u2[i], U1[i], U2[i] and eta[i]. Using prog. 4.9 i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

1

0.8618

0.4287

1.7237

0.4287

1.7237

1

2

0.8596

0.0342

1.7192

0.0342

1.7192

2

3

0.7597

0.4609

1.5193

0.4609

1.5193

3

4

0.4189

0.4746

0.8378

0.4746

Null

4

5

0.3799

0.1687

0.7598

0.1687

0.7598

5

6

0.1899

0.0364

0.3799

0.0364

0.3799

6

7

0.3335

0.4257

0.6669

0.4257

Null

7

8

0.3860

0.2445

0.7721

0.2445

0.7721

8

9

0.5881

0.2322

1.1763

0.2322

1.1763

9

10

0.6743

0.9089

1.3486

0.9089

Null

10

11

0.8759

0.3658

1.7518

0.3658

1.7518

11

12

0.2945

0.6719

0.5891

0.6719

Null

12

13

0.9104

0.9276

1.8208

0.9276

Null

13

14

0.0504

0.3510

0.1008

0.3510

Null

14

15

0.1510

0.1761

0.3021

0.1761

Null

15

16

0.3796

0.5239

0.7593

0.5239

Null

16

17

0.3586

0.4202

0.7172

0.4202

Null

17

18

0.5641

0.1372

1.1283

0.1372

1.1283

18

19

0.0774

0.8924

0.1548

0.8924

Null

19

20

0.9848

0.4854

1.9696

0.4854

1.9696

20

21

0.3080

0.4945

0.6159

0.4945

Null

21

22

0.9390

0.5923

1.8781

0.5923

1.8781

22

23

0.1921

0.3830

0.3842

0.3830

Null

23

24

0.6754

0.2944

1.3508

0.2944

1.3508

24

25

0.9737

0.4501

1.9474

0.4501

1.9474

25

26

0.7833

0.7877

1.5667

0.7877

Null

26

27

0.7012

0.5937

1.4024

0.5937

1.4024

27

28

0.6616

0.5202

1.3232

0.5202

1.3232

28

29

0.9024

0.0050

1.8049

0.0050

1.8049

29

30

0.2183

0.2361

0.4365

0.2361

Null

30

31

0.4477

0.7308

0.8954

0.7308

Null

31

32

0.3429

0.2260

0.6857

0.2260

0.6857

32 (continued)

84

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.9 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

33

0.1079

0.4879

0.2158

0.4879

Null

33

34

0.1655

0.1218

0.3310

0.1218

0.3310

34

35

0.3544

0.3523

0.7088

0.3523

0.7088

35

36

0.3895

0.0911

0.7789

0.0911

0.7789

36

37

0.9631

0.5184

1.9262

0.5184

1.9262

37

38

0.6328

0.8258

1.2656

0.8258

Null

38

39

0.4101

0.6742

0.8201

0.6742

Null

39

40

0.2088

0.8631

0.4176

0.8631

Null

40

41

0.2042

0.3027

0.4084

0.3027

Null

41

42

0.0504

0.8463

0.1007

0.8463

Null

42

43

0.4134

0.2451

0.8267

0.2451

0.8267

43

44

0.5565

0.0084

1.1130

0.0084

1.1130

44

45

0.3532

0.7547

0.7064

0.7547

Null

45

46

0.1188

0.5403

0.2376

0.5403

Null

46

47

0.5235

0.8228

1.0471

0.8228

Null

47

48

0.5526

0.8279

1.1052

0.8279

Null

48

49

0.6770

0.7266

1.3540

0.7266

Null

49

50

0.4398

0.2243

0.8796

0.2243

0.8796

50

51

0.9524

0.7628

1.9049

0.7628

1.9049

51

52

0.9227

0.7673

1.8455

0.7673

1.8455

52

53

0.7744

0.0388

1.5488

0.0388

1.5488

53

54

0.4234

0.3146

0.8468

0.3146

0.8468

54

55

0.2849

0.8526

0.5699

0.8526

Null

55

56

0.2712

0.4589

0.5424

0.4589

Null

56

57

0.5662

0.1270

1.1323

0.1270

1.1323

57

58

0.3261

0.1921

0.6521

0.1921

0.6521

58

59

0.9294

0.0971

1.8587

0.0971

1.8587

59

60

0.8678

0.0527

1.7357

0.0527

1.7357

60

61

0.6010

0.5101

1.2019

0.5101

1.2019

61

62

0.0491

0.8662

0.0982

0.8662

Null

62

63

0.8311

0.3699

1.6623

0.3699

1.6623

63

64

0.6055

0.7086

1.2109

0.7086

Null

64 (continued)

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

85

Table 4.9 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

65

0.0621

0.0293

0.1242

0.0293

0.1242

65

66

0.9600

0.7918

1.9199

0.7918

1.9199

66

67

0.2790

0.3078

0.5579

0.3078

Null

67

68

0.9336

0.7173

1.8672

0.7173

1.8672

68

69

0.9645

0.1720

1.9289

0.1720

1.9289

69

70

0.1165

0.6613

0.2330

0.6613

Null

70

71

0.9249

0.6311

1.8498

0.6311

1.8498

71

72

0.6276

0.1130

1.2552

0.1130

1.2552

72

73

0.1215

0.9735

0.2430

0.9735

Null

73

74

0.0148

0.2807

0.0296

0.2807

Null

74

75

0.1744

0.8147

0.3488

0.8147

Null

75

76

0.9503

0.8674

1.9006

0.8674

1.9006

76

77

0.3100

0.7364

0.6200

0.7364

Null

77

78

0.2646

0.1094

0.5292

0.1094

0.5292

78

79

0.1454

0.9416

0.2909

0.9416

Null

79

80

0.3930

0.0684

0.7859

0.0684

0.7859

80

81

0.6325

0.3698

1.2650

0.3698

1.2650

81

82

0.3261

0.3225

0.6523

0.3225

0.6523

82

83

0.7350

0.4885

1.4699

0.4885

1.4699

83

84

0.4781

0.0687

0.9562

0.0687

0.9562

84

85

0.7332

0.7276

1.4665

0.7276

1.4665

85

86

0.2226

0.7403

0.4453

0.7403

Null

86

87

0.6044

0.2942

1.2087

0.2942

1.2087

87

88

0.3337

0.8206

0.6675

0.8206

Null

88

89

0.6196

0.1319

1.2391

0.1319

1.2391

89

90

0.4761

0.1548

0.9522

0.1548

0.9522

90

91

0.1495

0.1481

0.2990

0.1481

0.2990

91

92

0.0263

0.9820

0.0526

0.9820

Null

92

93

0.6774

0.4351

1.3548

0.4351

1.3548

93

94

0.3336

0.4401

0.6673

0.4401

Null

94

95

0.3045

0.2773

0.6089

0.2773

0.6089

95

96

0.9500

0.5186

1.9000

0.5186

1.9000

96 (continued)

86

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.9 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

97

0.7374

0.9651

1.4749

0.9651

Null

97

98

0.6385

0.9689

1.2771

0.9689

Null

98

99

0.5705

0.4434

1.1410

0.4434

1.1410

99

100

0.5668

0.8242

1.1337

0.8242

Null

100

That random variate η a is distributed as p(x) = x/2 in the interval 0 to 2 is evidenced by Fig. 4.10 which displays values of ηa against stagnant integer 1. We find that indeed there are more variates for larger values of ηa. Program number 4.9 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 2*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < U1[i]/2,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","eta[i]"}}] Program number 4.10 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 2*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < U1[i]/2,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","eta[i]"}}] etaa[1] = eta[1];etaa[2] = eta[2];etaa[3] = eta[3];etaa[4] = eta[5]; etaa[5] = eta[6];etaa[6] = eta[8];etaa[7] = eta[9];etaa[8] = eta[11]; etaa[9] = eta[18];etaa[10] = eta[20];etaa[11] = eta[22];etaa[12] = eta[24]; etaa[13] = eta[25];etaa[14] = eta[27];etaa[15] = eta[28];etaa[16] = eta[29]; etaa[17] = eta[32];etaa[18] = eta[34];etaa[19] = eta[35];etaa[20] = eta[36]; etaa[21] = eta[37];etaa[22] = eta[43];etaa[23] = eta[44];etaa[24] = eta[50]; etaa[25] = eta[51];etaa[26] = eta[52];etaa[27] = eta[53];etaa[28] = eta[54]; etaa[29] = eta[57];etaa[30] = eta[58];etaa[31] = eta[59];etaa[32] = eta[60]; etaa[33] = eta[61];etaa[34] = eta[63];etaa[35] = eta[65];etaa[36] = eta[66];

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

Table 4.10 Showing etaa[i] and y[i] = (Exp[etaa[i]))/etaa[i]. Using prog. 4.10 i

etaa[i]

y[i]

i

etaa[i]

y[i]

1

1.7237

3.2518

26

1.8455

3.4306

2

1.7192

3.2457

27

1.5488

3.0384

3

1.5193

3.0073

28

0.8468

2.7541

4

0.7598

2.8137

29

1.1323

2.7403

5

0.3799

3.8489

30

0.6521

2.9436

6

0.7721

2.8032

31

1.8587

3.4516

7

1.1763

2.7564

32

1.7357

3.2683

8

1.7518

3.2908

33

1.2019

2.7677

9

1.1283

2.7390

34

1.6623

3.1711

10

1.9696

3.6392

35

0.1242

9.1141

11

1.8781

3.4827

36

1.9199

3.5525

12

1.3508

2.8580

37

1.8672

3.4651

13

1.9474

3.6000

38

1.9289

3.5679

14

1.4024

2.8985

39

1.8498

3.4373

15

1.3232

2.8381

40

1.2552

2.7952

16

1.8049

3.3682

41

1.9006

3.5199

17

0.6857

2.8951

42

0.5292

3.2079

18

0.3310

4.2065

43

0.7859

2.7922

19

0.7088

2.8662

44

1.2650

2.8009

20

0.7789

2.7976

45

0.6523

2.9434

21

1.9262

3.5631

46

1.4699

2.9586

22

0.8267

2.7649

47

0.9562

2.7210

23

1.1130

2.7345

48

1.4665

2.9553

24

0.8796

2.7398

49

1.2087

2.7709

25

1.9049

3.5271

50

1.2391

2.7863

51

0.9522

2.7215

52

0.2990

4.5103

53

1.3548

2.8609

54

0.6089

3.0192

55

1.9000

3.5188

56

1.1410

2.7431

87

88

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

Fig. 4.10 Showing that random variates ηa are linearly distributed in the interval 0 to 2. There are more variates for larger values of ηa

etaa[37] = eta[68];etaa[38] = eta[69];etaa[39] = eta[71];etaa[40] = eta[72]; etaa[41] = eta[76];etaa[42] = eta[78];etaa[43] = eta[80];etaa[44] = eta[81]; etaa[45] = eta[82];etaa[46] = eta[83];etaa[47] = eta[84];etaa[48] = eta[85]; etaa[49] = eta[87];etaa[50] = eta[89];etaa[51] = eta[90];etaa[52] = eta[91]; etaa[53] = eta[93];etaa[54] = eta[95];etaa[55] = eta[96];etaa[56] = eta[99]; Table[{i = i + 1,etaa[i],y[i] = (Exp[etaa[i]])/etaa[i]},{i,0,55,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","etaa[i]","y[i]"}}] Integral = Sum[2*y[i]/(56),{i,1,56}] ListPlot[Table[{etaa[i],1},{i,0,55,1}], Frame-> True,FrameLabel-> {"\[Eta]a [i]","1"}] That random variate ηa is distributed as p(x) = x/2 in the interval 0 to 2 is evidenced by Fig. 4.10 which displays values of ηa against stagnant integer 1. We find that indeed there are more variates for larger values of ηa.

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

89

We now take up anormalized Gaussian variation for p(x) given by  2 p(x) = √1 exp − 21 x−a where a is average and σ is variance of x. See Fig. 4.11 σ σ 2π in which we have plotted F(x) = ex along with the normalized Gaussian probability density function p(x). As Fig. 4.11 shows, the Gaussian probability density function p(x) with a = 1 and σ = 0.3 is different from the function ex in that it does not follow ex for x > 1. Hence it is not expected to yield a good estimate of the value of the definite integral. Other values of σ yield worse results.   2  We now generate random variate g obeying p(g) = √1 exp − 21 g−a with a = 1 σ σ 2π and σ = 0.3 in the interval 0 < g < 2 using acceptance-rejection sampling, using Program number 4.11 and resulting Table 4.11. From Table 4.11, we manually gather accepted

7 6 5 4 3 2 1 0

0.0

0.5

1.0

1.5

2.0

x   2  Fig. 4.11 Showing F(x) = ex as undashed curve and Gaussian p(x) = √1 exp − 21 x−a as σ σ 2π dashed curve obtained using the command sig = 0.3; a = 1; p1 = Plot[Exp[x],{x,0,2},PlotStyle-> {Black},Frame-> True, FrameLabel-> {"x","F(x) = e^x or Gaussian (x)"}, PlotRange-> {0,7}]; p2 = Plot[(1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((x-a)/sig)^2], {x,0,2},PlotStyle-> {Dashed,Black},PlotRange-> {0,7}]; Show[p1,p2] in Mathematica 6.0

90

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

random variates ga which we use in Program number 4.12 and resulting Table 4.12 to evaluate the integral with the result 10.00 rather than the exact value 6.389. Program number 4.11 n = 654321 SeedRandom[n] sig = 0.3;a = 1; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 2*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))* Exp[-0.5*((U1[i]-a)/sig)^2],g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","g[i]"}}] Program number 4.12 n = 654321 SeedRandom[n] sig = 0.3;a = 1; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 2*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))* Exp[-0.5*((U1[i]-a)/sig)^2],g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}] ga[1] = g[2]; ga[2] = g[4]; ga[3] = g[5]; ga[4] = g[6]; ga[5] = g[7]; ga[6] = g[8]; ga[7] = g[9]; ga[8] = g[16]; ga[9] = g[17]; ga[10] = g[18]; ga[11] = g[21]; ga[12] = g[24]; ga[13] = g[28]; ga[14] = g[29]; ga[15] = g[31]; ga[16] = g[32]; ga[17] = g[35]; ga[18] = g[36]; ga[19] = g[38]; ga[20] = g[39]; ga[21] = g[43]; ga[22] = g[44]; ga[23] = g[45]; ga[24] = g[47]; ga[25] = g[48]; ga[26] = g[50]; ga[27] = g[53]; ga[28] = g[54]; ga[29] = g[57]; ga[30] = g[58]; ga[31] = g[60]; ga[32] = g[61]; ga[33] = g[64]; ga[34] = g[67]; ga[35] = g[72]; ga[36] = g[78]; ga[37] = g[80]; ga[38] = g[81]; ga[39] = g[82]; ga[40] = g[84]; ga[41] = g[87]; ga[42] = g[89];ga[43] = g[90];ga[44] = g[93];ga[45] = g[94];ga[46] = g[95]; ga[47] = g[99];ga[48] = g[100]; Table[{i = i + 1,ga[i], p[i] = (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((ga[i]-a)/sig)^2],

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

91

Table 4.11 Showing u1[i], u2[i], U1[i], U2[i] and g[i]. Using prog. 4.11 i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

1

0.8618

0.4287

1.7237

0.4287

Null

1

2

0.8596

0.0342

1.7192

0.0342

1.7192

2

3

0.7597

0.4609

1.5193

0.4609

Null

3

4

0.4189

0.4746

0.8378

0.4746

0.8378

4

5

0.3799

0.1687

0.7598

0.1687

0.7598

5

6

0.1899

0.0364

0.3799

0.0364

0.3799

6

7

0.3335

0.4257

0.6669

0.4257

0.6669

7

8

0.3860

0.2445

0.7721

0.2445

0.7721

8

9

0.5881

0.2322

1.1763

0.2322

1.1763

9

10

0.6743

0.9089

1.3486

0.9089

Null

10

11

0.8759

0.3658

1.7518

0.3658

Null

11

12

0.2945

0.6719

0.5891

0.6719

Null

12

13

0.9104

0.9276

1.8208

0.9276

Null

13

14

0.0504

0.3510

0.1008

0.3510

Null

14

15

0.1510

0.1761

0.3021

0.1761

Null

15

16

0.3796

0.5239

0.7593

0.5239

0.7593

16

17

0.3586

0.4202

0.7172

0.4202

0.7172

17

18

0.5641

0.1372

1.1283

0.1372

1.1283

18

19

0.0774

0.8924

0.1548

0.8924

Null

19

20

0.9848

0.4854

1.9696

0.4854

Null

20

21

0.3080

0.4945

0.6159

0.4945

0.6159

21

22

0.9390

0.5923

1.8781

0.5923

Null

22

23

0.1921

0.3830

0.3842

0.3830

Null

23

24

0.6754

0.2944

1.3508

0.2944

1.3508

24

25

0.9737

0.4501

1.9474

0.4501

Null

25

26

0.7833

0.7877

1.5667

0.7877

Null

26

27

0.7012

0.5937

1.4024

0.5937

Null

27

28

0.6616

0.5202

1.3232

0.5202

1.3232

28

29

0.9024

0.0050

1.8049

0.0050

1.8049

29

30

0.2183

0.2361

0.4365

0.2361

Null

30

31

0.4477

0.7308

0.8954

0.7308

0.8954

31

32

0.3429

0.2260

0.6857

0.2260

0.6857

32 (continued)

92

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.11 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

33

0.1079

0.4879

0.2158

0.4879

Null

33

34

0.1655

0.1218

0.3310

0.1218

Null

34

35

0.3544

0.3523

0.7088

0.3523

0.7088

35

36

0.3895

0.0911

0.7789

0.0911

0.7789

36

37

0.9631

0.5184

1.9262

0.5184

Null

37

38

0.6328

0.8258

1.2656

0.8258

1.2656

38

39

0.4101

0.6742

0.8201

0.6742

0.8201

39

40

0.2088

0.8631

0.4176

0.8631

Null

40

41

0.2042

0.3027

0.4084

0.3027

Null

41

42

0.0504

0.8463

0.1007

0.8463

Null

42

43

0.4134

0.2451

0.8267

0.2451

0.8267

43

44

0.5565

0.0084

1.1130

0.0084

1.1130

44

45

0.3532

0.7547

0.7064

0.7547

0.7064

45

46

0.1188

0.5403

0.2376

0.5403

Null

46

47

0.5235

0.8228

1.0471

0.8228

1.0471

47

48

0.5526

0.8279

1.1052

0.8279

1.1052

48

49

0.6770

0.7266

1.3540

0.7266

Null

49

50

0.4398

0.2243

0.8796

0.2243

0.8796

50

51

0.9524

0.7628

1.9049

0.7628

Null

51

52

0.9227

0.7673

1.8455

0.7673

Null

52

53

0.7744

0.0388

1.5488

0.0388

1.5488

53

54

0.4234

0.3146

0.8468

0.3146

0.8468

54

55

0.2849

0.8526

0.5699

0.8526

Null

55

56

0.2712

0.4589

0.5424

0.4589

Null

56

57

0.5662

0.1270

1.1323

0.1270

1.1323

57

58

0.3261

0.1921

0.6521

0.1921

0.6521

58

59

0.9294

0.0971

1.8587

0.0971

Null

59

60

0.8678

0.0527

1.7357

0.0527

1.7357

60

61

0.6010

0.5101

1.2019

0.5101

1.2019

61

62

0.0491

0.8662

0.0982

0.8662

Null

62

63

0.8311

0.3699

1.6623

0.3699

Null

63

64

0.6055

0.7086

1.2109

0.7086

1.2109

64 (continued)

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

93

Table 4.11 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

65

0.0621

0.0293

0.1242

0.0293

Null

65

66

0.9600

0.7918

1.9199

0.7918

Null

66

67

0.2790

0.3078

0.5579

0.3078

0.5579

67

68

0.9336

0.7173

1.8672

0.7173

Null

68

69

0.9645

0.1720

1.9289

0.1720

Null

69

70

0.1165

0.6613

0.2330

0.6613

Null

70

71

0.9249

0.6311

1.8498

0.6311

Null

71

72

0.6276

0.1130

1.2552

0.1130

1.2552

72

73

0.1215

0.9735

0.2430

0.9735

Null

73

74

0.0148

0.2807

0.0296

0.2807

Null

74

75

0.1744

0.8147

0.3488

0.8147

Null

75

76

0.9503

0.8674

1.9006

0.8674

Null

76

77

0.3100

0.7364

0.6200

0.7364

Null

77

78

0.2646

0.1094

0.5292

0.1094

0.5292

78

79

0.1454

0.9416

0.2909

0.9416

Null

79

80

0.3930

0.0684

0.7859

0.0684

0.7859

80

81

0.6325

0.3698

1.2650

0.3698

1.2650

81

82

0.3261

0.3225

0.6523

0.3225

0.6523

82

83

0.7350

0.4885

1.4699

0.4885

Null

83

84

0.4781

0.0687

0.9562

0.0687

0.9562

84

85

0.7332

0.7276

1.4665

0.7276

Null

85

86

0.2226

0.7403

0.4453

0.7403

Null

86

87

0.6044

0.2942

1.2087

0.2942

1.2087

87

88

0.3337

0.8206

0.6675

0.8206

Null

88

89

0.6196

0.1319

1.2391

0.1319

1.2391

89

90

0.4761

0.1548

0.9522

0.1548

0.9522

90

91

0.1495

0.1481

0.2990

0.1481

Null

91

92

0.0263

0.9820

0.0526

0.9820

Null

92

93

0.6774

0.4351

1.3548

0.4351

1.3548

93

94

0.3336

0.4401

0.6673

0.4401

0.6673

94

95

0.3045

0.2773

0.6089

0.2773

0.6089

95

96

0.9500

0.5186

1.9000

0.5186

Null

96 (continued)

94

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.11 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

97

0.7374

0.9651

1.4749

0.9651

Null

97

98

0.6385

0.9689

1.2771

0.9689

Null

98

99

0.5705

0.4434

1.1410

0.4434

1.1410

99

100

0.5668

0.8242

1.1337

0.8242

1.1337

100

Table 4.12 Showing ga[i], p[i] and y[i] = (Exp(ga[i]))/p[i]. Using prog. 4.12 i

ga[i]

p[i]

1

1.7192

0.0751

2

0.8378

1.1490

3

0.7598

4 5

y[i]

i

ga[i]

p[i]

y[i]

74.2764

26

0.8796

1.2269

1.9643

2.0116

27

1.5488

0.2495

18.8599

0.9652

2.2150

28

0.8468

1.1673

1.9980

0.3799

0.1570

9.3111

29

1.1323

1.2065

2.5717

0.6669

0.7180

2.7133

30

0.6521

0.6789

2.8276

6

0.7721

0.9965

2.1719

31

1.7357

0.0658

86.2567

7

1.1763

1.1189

2.8977

32

1.2019

1.0603

3.1374

8

0.7593

0.9637

2.2171

33

1.2109

1.0386

3.2319

9

0.7172

0.8529

2.4022

34

0.5579

0.4490

3.8910

10

1.1283

1.2137

2.5463

35

1.2552

0.9261

3.7884

11

0.6159

0.5860

3.1595

36

0.5292

0.3881

4.3739

12

1.3508

0.6712

5.7514

37

0.7859

1.0309

2.1286

13

1.3232

0.7443

5.0453

38

1.2650

0.9003

3.9354

14

1.8049

0.0364

167.1220

39

0.6523

0.6792

2.8265

15

0.8954

1.2514

1.9565

40

0.9562

1.3157

1.9775

16

0.6857

0.7682

2.5843

41

1.2087

1.0439

3.2084

17

0.7088

0.8301

2.4472

42

1.2391

0.9679

3.5672

18

0.7789

1.0136

2.1499

43

0.9522

1.3130

1.9736

19

1.2656

0.8986

3.9452

44

1.3548

0.6608

5.8658

20

0.8201

1.1110

2.0439

45

0.6673

0.7189

2.7110

21

0.8267

1.1255

2.0309

46

0.6089

0.5685

3.2336

22

1.1130

1.2387

2.4571

47

1.1410

1.1908

2.6283

23

0.7064

0.8237

2.4604

48

1.1337

1.2042

2.5802

24

1.0471

1.3136

2.1691

25

1.1052

1.2505

2.4151

4.3

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

95

y[i] = (Exp[ga[i]])/p[i]},{i,0,47,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","ga[i]", "p[i]","y[i]"}}] Integral = Sum[y[i]/(48),{i,1,48}] ListPlot[Table[{ga[i],1},{i,0,47,1}],Frame-> True, FrameLabel-> {"ga [i]","1"}]   2  That random variate ga is distributed as p(x) = √1 exp − 21 x−a with a = 1 σ σ 2π and sig = 0.3 in the interval 0 to 2 is evidenced by Fig. 4.12 which displays values of ga against stagnant integer 1. We find that indeed there are more variates near ga = 1. 2.0

1

1.5

1.0

0.5

0.0 0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

Fig. 4.12 Showing that random variates ga are distributed in the interval 0 to 2 as p(x) =  2  √1 exp − 1 x−a with a = 1 and sig = 0.3. There are more variates near ga = 1 2 σ σ 2π

96

4.4

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example IV

As example IV, we now take up the integral b I =

5 F(x)d x =

a

loge xd x

(4.10)

1

where F(x) = loge x. We re-write Eq. (4.10) as b I =

F(x) p(x)d x p(x)

(4.11)

a

which, as discussed in Sect. 3.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi )

(4.12)

i=1

where x i ’s are random values of x in the interval 1 < x < 5 obeying probability density function p(x). We now take up an exponential variation for p(x) given by p(x) = C ex where C 5 is a constant. Normalization of p(x) requires 1 p(x)d x = 1 or, C = 1/145. As such normalized probability density function is p(x) = ex /145. See Fig. 4.13 in which we have plotted F(x) = loge x along with the normalized probability density function p(x) = ex /145. The probability density function p(x) = ex /145 follows the function loge x well. Hence it is a good choice. We now generate random variate η obeying p(x) = ex /145 in the interval 1 < x < 5 using acceptance-rejection sampling, using Program number 4.13 and resulting Table 4.13. From Table 4.13, we manually gather accepted random variates ηa which we use in Program number 4.14 and resulting Table 4.14 to evaluate the integral with the result 3.953 rather than the exact value 4.045. The difference is attributable to the fact that F(x) and p(x) used are not proportional to each other. Program number 4.13 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 1 + 4*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < (Exp[U1[i]])/145,eta[i] = U1[i],Null]},{i,0,99,1}];

4.4

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example …

97

1.5

1.0

0.5

0.0 1

2

3 x

4

5

Fig. 4.13 Showing F(x) = loge x as undashed curve and p(x) = ex /145 as dashed curve obtained using the command a = 1/145; p1 = Plot[Log[E,x],{x,1,5},Frame-> True, FrameLabel-> {"x","F(x) = Log(x) or p(x)"},PlotStyle-> {Black}]; p2 = Plot[a*Exp[x],{x,1,5},PlotStyle-> {Dashed,Black}, Frame-> True]; Show[p1,p2] in Mathematica 6.0

TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","eta[i]"}}] Program number 4.14 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 1 + 4*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < (Exp[U1[i]])/145,eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2},

98

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.13 Showing u1[i], u2[i], U1[i], U2[i] and eta[i]. Using prog. 4.13 i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

1

0.8618

0.4287

4.4473

0.4287

4.4473

1

2

0.8596

0.0342

4.4384

0.0342

4.4384

2

3

0.7597

0.4609

4.0386

0.4609

Null

3

4

0.4189

0.4746

2.6756

0.4746

Null

4

5

0.3799

0.1687

2.5196

0.1687

Null

5

6

0.1899

0.0364

1.7598

0.0364

1.7598

6

7

0.3335

0.4257

2.3339

0.4257

Null

7

8

0.3860

0.2445

2.5442

0.2445

Null

8

9

0.5881

0.2322

3.3526

0.2322

Null

9

10

0.6743

0.9089

3.6973

0.9089

Null

10

11

0.8759

0.3658

4.5035

0.3658

4.5035

11

12

0.2945

0.6719

2.1781

0.6719

Null

12

13

0.9104

0.9276

4.6416

0.9276

Null

13

14

0.0504

0.3510

1.2016

0.3510

Null

14

15

0.1510

0.1761

1.6042

0.1761

Null

15

16

0.3796

0.5239

2.5185

0.5239

Null

16

17

0.3586

0.4202

2.4345

0.4202

Null

17

18

0.5641

0.1372

3.2565

0.1372

3.2565

18

19

0.0774

0.8924

1.3096

0.8924

Null

19

20

0.9848

0.4854

4.9391

0.4854

4.9391

20

21

0.3080

0.4945

2.2318

0.4945

Null

21

22

0.9390

0.5923

4.7561

0.5923

4.7561

22

23

0.1921

0.3830

1.7684

0.3830

Null

23

24

0.6754

0.2944

3.7016

0.2944

Null

24

25

0.9737

0.4501

4.8949

0.4501

4.8949

25

26

0.7833

0.7877

4.1333

0.7877

Null

26

27

0.7012

0.5937

3.8047

0.5937

Null

27

28

0.6616

0.5202

3.6464

0.5202

Null

28

29

0.9024

0.0050

4.6097

0.0050

4.6097

29

30

0.2183

0.2361

1.8731

0.2361

Null

30

31

0.4477

0.7308

2.7909

0.7308

Null

31

32

0.3429

0.2260

2.3714

0.2260

Null

32 (continued)

4.4

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example …

99

Table 4.13 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

33

0.1079

0.4879

1.4317

0.4879

Null

33

34

0.1655

0.1218

1.6620

0.1218

Null

34

35

0.3544

0.3523

2.4176

0.3523

Null

35

36

0.3895

0.0911

2.5579

0.0911

Null

36

37

0.9631

0.5184

4.8523

0.5184

4.8523

37

38

0.6328

0.8258

3.5312

0.8258

Null

38

39

0.4101

0.6742

2.6402

0.6742

Null

39

40

0.2088

0.8631

1.8352

0.8631

Null

40

41

0.2042

0.3027

1.8169

0.3027

Null

41

42

0.0504

0.8463

1.2014

0.8463

Null

42

43

0.4134

0.2451

2.6535

0.2451

Null

43

44

0.5565

0.0084

3.2261

0.0084

3.2261

44

45

0.3532

0.7547

2.4127

0.7547

Null

45

46

0.1188

0.5403

1.4752

0.5403

Null

46

47

0.5235

0.8228

3.0941

0.8228

Null

47

48

0.5526

0.8279

3.2105

0.8279

Null

48

49

0.6770

0.7266

3.7081

0.7266

Null

49

50

0.4398

0.2243

2.7592

0.2243

Null

50

51

0.9524

0.7628

4.8098

0.7628

4.8098

51

52

0.9227

0.7673

4.6909

0.7673

Null

52

53

0.7744

0.0388

4.0976

0.0388

4.0976

53

54

0.4234

0.3146

2.6936

0.3146

Null

54

55

0.2849

0.8526

2.1398

0.8526

Null

55

56

0.2712

0.4589

2.0849

0.4589

Null

56

57

0.5662

0.1270

3.2647

0.1270

3.2647

57

58

0.3261

0.1921

2.3043

0.1921

Null

58

59

0.9294

0.0971

4.7175

0.0971

4.7175

59

60

0.8678

0.0527

4.4713

0.0527

4.4713

60

61

0.6010

0.5101

3.4038

0.5101

Null

61

62

0.0491

0.8662

1.1964

0.8662

Null

62

63

0.8311

0.3699

4.3245

0.3699

4.3245

63

64

0.6055

0.7086

3.4219

0.7086

Null

64 (continued)

100

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.13 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

65

0.0621

0.0293

1.2485

0.0293

Null

65

66

0.9600

0.7918

4.8399

0.7918

4.8399

66

67

0.2790

0.3078

2.1158

0.3078

Null

67

68

0.9336

0.7173

4.7343

0.7173

4.7343

68

69

0.9645

0.1720

4.8579

0.1720

4.8579

69

70

0.1165

0.6613

1.4660

0.6613

Null

70

71

0.9249

0.6311

4.6995

0.6311

4.6995

71

72

0.6276

0.1130

3.5104

0.1130

3.5104

72

73

0.1215

0.9735

1.4859

0.9735

Null

73

74

0.0148

0.2807

1.0592

0.2807

Null

74

75

0.1744

0.8147

1.6976

0.8147

Null

75

76

0.9503

0.8674

4.8012

0.8674

Null

76

77

0.3100

0.7364

2.2399

0.7364

Null

77

78

0.2646

0.1094

2.0584

0.1094

Null

78

79

0.1454

0.9416

1.5817

0.9416

Null

79

80

0.3930

0.0684

2.5719

0.0684

2.5719

80

81

0.6325

0.3698

3.5300

0.3698

Null

81

82

0.3261

0.3225

2.3045

0.3225

Null

82

83

0.7350

0.4885

3.9398

0.4885

Null

83

84

0.4781

0.0687

2.9124

0.0687

2.9124

84

85

0.7332

0.7276

3.9329

0.7276

Null

85

86

0.2226

0.7403

1.8906

0.7403

Null

86

87

0.6044

0.2942

3.4175

0.2942

Null

87

88

0.3337

0.8206

2.3349

0.8206

Null

88

89

0.6196

0.1319

3.4783

0.1319

3.4783

89

90

0.4761

0.1548

2.9043

0.1548

Null

90

91

0.1495

0.1481

1.5980

0.1481

Null

91

92

0.0263

0.9820

1.1052

0.9820

Null

92

93

0.6774

0.4351

3.7096

0.4351

Null

93

94

0.3336

0.4401

2.3345

0.4401

Null

94

95

0.3045

0.2773

2.2178

0.2773

Null

95

96

0.9500

0.5186

4.7999

0.5186

4.7999

96 (continued)

4.4

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example … 101

Table 4.13 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

97

0.7374

0.9651

3.9497

0.9651

Null

97

98

0.6385

0.9689

3.5541

0.9689

Null

98

99

0.5705

0.4434

3.2819

0.4434

Null

99

100

0.5668

0.8242

3.2673

0.8242

Null

100

Table 4.14 Showing tabulated values of etaa[i] and y[i] = (Log[etaa[i]])/(Exp[etaa[i]]). Using prog. 4.14

i

etaa[i]

y[i]

1

4.4473

0.0175

2

4.4384

0.0176

3

1.7598

0.0973

4

4.5035

0.0167

5

3.2565

0.0455

6

4.9391

0.0114

7

4.7561

0.0134

8

4.8949

0.0119

9

4.6097

0.0152

10

4.8523

0.0123

11

3.2261

0.0465

12

4.8098

0.0128

13

4.0976

0.0234

14

3.2647

0.0452

15

4.7175

0.0139

16

4.4713

0.0171

17

4.3245

0.0194

18

4.8399

0.0125

19

4.7343

0.0137

20

4.8579

0.0123

21

4.6995

0.0141

22

3.5104

0.0375

23

2.5719

0.0722

24

2.9124

0.0581

25

3.4783

0.0385

26

4.7999

0.0129

102

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","eta[i]"}}] etaa[1] = eta[1]; etaa[2] = eta[2]; etaa[3] = eta[6]; etaa[4] = eta[11]; etaa[5] = eta[18]; etaa[6] = eta[20]; etaa[7] = eta[22]; etaa[8] = eta[25]; etaa[9] = eta[29]; etaa[10] = eta[37]; etaa[11] = eta[44]; etaa[12] = eta[51]; etaa[13] = eta[53]; etaa[14] = eta[57]; etaa[15] = eta[59]; etaa[16] = eta[60]; etaa[17] = eta[63]; etaa[18] = eta[66]; etaa[19] = eta[68]; etaa[20] = eta[69]; etaa[21] = eta[71]; etaa[22] = eta[72]; etaa[23] = eta[80]; etaa[24] = eta[84]; etaa[25] = eta[89]; etaa[26] = eta[96]; Table[{i = i + 1,etaa[i],y[i] = (Log[etaa[i]])/(Exp[etaa[i]])},{i,0,25,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","etaa[i]","y[i]"}}] Integral = Sum[145*y[i]/26,{i,1,26}] ListPlot[Table[{etaa[i],1},{i,0,25,1}],Frame-> True, FrameLabel-> {"\[Eta]a [i]","1"}] That random variate ηa is non-uniformly distributed like p(x) = ex /145 in the interval 1 to 5 is evidenced by Fig. 4.14 which displays values of ηa against stagnant integer 1. We find that indeed there are more variates for larger values of ηa.

4.5

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling: Example V

As example V, we now take up the integral b I =

3 F(x)d x =

a

where F(x) =

1 . 1+x 2

0

1 dx 1 + x2

(4.13)

We re-write Eq. (4.13) as b I =

F(x) p(x)d x p(x)

(4.14)

a

which, as discussed in Sect. 3.6, implies that average value of F/p is the value of the integral, i.e. I =

N 1  F(xi ) N p(xi ) i=1

(4.15)

4.5

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

103

2.0

1

1.5

1.0

0.5

0.0 2.0

2.5

3.0

3.5

4.0

4.5

5.0

Fig. 4.14 Showing that random variates ηa are non-uniformly distributed like p(x) = ex /145 in the interval 1 to 5. We find that indeed there are more variates for larger values of ηa

where x i ’s are random values of x in the interval 0 < x < 3 obeying probability density function p(x). We now take up an exponential variation for p(x) given by p(x) = C e−x where C is a 3 constant. Normalization of p(x) requires 0 p(x)d x = 1 or, C = 1.05. As such normalized probability density function is p(x) = 1.05e−x . See Fig. 4.15 in which we have plotted 1 −x . The F(x) = 1+x 2 along with the normalized probability density function p(x) = 1.05 e

1 probability density function p(x) = 1.05e−x follows the function 1+x 2 quite well. Hence it is a very good choice. We now generate random variate η obeying p(x) = 1.05 e−x in the interval 0 < x < 3 using acceptance-rejection sampling, using Program number 4.15 and resulting Table 4.15. From Table 4.15, we manually gather accepted random variates ηa which we use in Program number 4.16 and resulting Table 4.16 to evaluate the integral with the result 1.292 rather than the exact value 1.249. The absence of difference is attributable to the fact that F(x) and p(x) used are almost proportional to each other. That random variate ηa is non-uniformly distributed in the interval 0 to 3 is evidenced by Fig. 4.16 which displays values of ηa against stagnant integer 1.

104

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

1.0

0.8

0.6

0.4

0.2

0.0

0.0

0.5

1.0

1.5

2.0

2.5

3.0

x Fig. 4.15 Showing F(x) = 1 2 as undashed curve and p(x) = 1.05 e−x as dashed curve obtained 1+x using the command p1 = Plot[1/(1 + x^2),{x,0,3},PlotRange-> {0,1},Frame-> True, FrameLabel-> {"x","F(x) = 1/(1 + x^2) or p(x)"}, PlotStyle-> {Black}]; p2 = Plot[1.05*Exp[-x],{x,0,3},PlotStyle-> {Dashed,Black}]; Show[p1,p2] in Mathematica 6.0

Program number 4.15 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 3*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < 1.05*Exp[-U1[i]],eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]", "U1[i]","U2[i]","eta[i]"}}]

4.5

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

105

Table 4.15 Showing u1[i], u2[i], U1[i], U2[i] and eta[i]. Using prog. 4.15 i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

1

0.8618

0.4287

2.5855

0.4287

Null

1

2

0.8596

0.0342

2.5788

0.0342

2.5788

2

3

0.7597

0.4609

2.2790

0.4609

Null

3

4

0.4189

0.4746

1.2567

0.4746

Null

4

5

0.3799

0.1687

1.1397

0.1687

1.1397

5

6

0.1899

0.0364

0.5698

0.0364

0.5698

6

7

0.3335

0.4257

1.0004

0.4257

Null

7

8

0.3860

0.2445

1.1581

0.2445

1.1581

8

9

0.5881

0.2322

1.7644

0.2322

Null

9

10

0.6743

0.9089

2.0229

0.9089

Null

10

11

0.8759

0.3658

2.6277

0.3658

Null

11

12

0.2945

0.6719

0.8836

0.6719

Null

12

13

0.9104

0.9276

2.7312

0.9276

Null

13

14

0.0504

0.3510

0.1512

0.3510

0.1512

14

15

0.1510

0.1761

0.4531

0.1761

0.4531

15

16

0.3796

0.5239

1.1389

0.5239

Null

16

17

0.3586

0.4202

1.0759

0.4202

Null

17

18

0.5641

0.1372

1.6924

0.1372

1.6924

18

19

0.0774

0.8924

0.2322

0.8924

Null

19

20

0.9848

0.4854

2.9544

0.4854

Null

20

21

0.3080

0.4945

0.9239

0.4945

Null

21

22

0.9390

0.5923

2.8171

0.5923

Null

22

23

0.1921

0.3830

0.5763

0.3830

0.5763

23

24

0.6754

0.2944

2.0262

0.2944

Null

24

25

0.9737

0.4501

2.9212

0.4501

Null

25

26

0.7833

0.7877

2.3500

0.7877

Null

26

27

0.7012

0.5937

2.1036

0.5937

Null

27

28

0.6616

0.5202

1.9848

0.5202

Null

28

29

0.9024

0.0050

2.7073

0.0050

2.7073

29

30

0.2183

0.2361

0.6548

0.2361

0.6548

30

31

0.4477

0.7308

1.3431

0.7308

Null

31

32

0.3429

0.2260

1.0286

0.2260

1.0286

32 (continued)

106

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.15 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

33

0.1079

0.4879

0.3238

0.4879

0.3238

33

34

0.1655

0.1218

0.4965

0.1218

0.4965

34

35

0.3544

0.3523

1.0632

0.3523

1.0632

35

36

0.3895

0.0911

1.1684

0.0911

1.1684

36

37

0.9631

0.5184

2.8893

0.5184

Null

37

38

0.6328

0.8258

1.8984

0.8258

Null

38

39

0.4101

0.6742

1.2302

0.6742

Null

39

40

0.2088

0.8631

0.6264

0.8631

Null

40

41

0.2042

0.3027

0.6126

0.3027

0.6126

41

42

0.0504

0.8463

0.1511

0.8463

0.1511

42

43

0.4134

0.2451

1.2401

0.2451

1.2401

43

44

0.5565

0.0084

1.6695

0.0084

1.6695

44

45

0.3532

0.7547

1.0596

0.7547

Null

45

46

0.1188

0.5403

0.3564

0.5403

0.3564

46

47

0.5235

0.8228

1.5706

0.8228

Null

47

48

0.5526

0.8279

1.6579

0.8279

Null

48

49

0.6770

0.7266

2.0311

0.7266

Null

49

50

0.4398

0.2243

1.3194

0.2243

1.3194

50

51

0.9524

0.7628

2.8573

0.7628

Null

51

52

0.9227

0.7673

2.7682

0.7673

Null

52

53

0.7744

0.0388

2.3232

0.0388

2.3232

53

54

0.4234

0.3146

1.2702

0.3146

Null

54

55

0.2849

0.8526

0.8548

0.8526

Null

55

56

0.2712

0.4589

0.8137

0.4589

0.8137

56

57

0.5662

0.1270

1.6985

0.1270

1.6985

57

58

0.3261

0.1921

0.9782

0.1921

0.9782

58

59

0.9294

0.0971

2.7881

0.0971

Null

59

60

0.8678

0.0527

2.6035

0.0527

2.6035

60

61

0.6010

0.5101

1.8029

0.5101

Null

61

62

0.0491

0.8662

0.1473

0.8662

0.1473

62

63

0.8311

0.3699

2.4934

0.3699

Null

63

64

0.6055

0.7086

1.8164

0.7086

Null

64 (continued)

4.5

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

107

Table 4.15 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

65

0.0621

0.0293

0.1864

0.0293

0.1864

65

66

0.9600

0.7918

2.8799

0.7918

Null

66

67

0.2790

0.3078

0.8369

0.3078

0.8369

67

68

0.9336

0.7173

2.8007

0.7173

Null

68

69

0.9645

0.1720

2.8934

0.1720

Null

69

70

0.1165

0.6613

0.3495

0.6613

0.3495

70

71

0.9249

0.6311

2.7746

0.6311

Null

71

72

0.6276

0.1130

1.8828

0.1130

1.8828

72

73

0.1215

0.9735

0.3644

0.9735

Null

73

74

0.0148

0.2807

0.0444

0.2807

0.0444

74

75

0.1744

0.8147

0.5232

0.8147

Null

75

76

0.9503

0.8674

2.8509

0.8674

Null

76

77

0.3100

0.7364

0.9300

0.7364

Null

77

78

0.2646

0.1094

0.7938

0.1094

0.7938

78

79

0.1454

0.9416

0.4363

0.9416

Null

79

80

0.3930

0.0684

1.1789

0.0684

1.1789

80

81

0.6325

0.3698

1.8975

0.3698

Null

81

82

0.3261

0.3225

0.9784

0.3225

0.9784

82

83

0.7350

0.4885

2.2049

0.4885

Null

83

84

0.4781

0.0687

1.4343

0.0687

1.4343

84

85

0.7332

0.7276

2.1997

0.7276

Null

85

86

0.2226

0.7403

0.6679

0.7403

Null

86

87

0.6044

0.2942

1.8131

0.2942

Null

87

88

0.3337

0.8206

1.0012

0.8206

Null

88

89

0.6196

0.1319

1.8587

0.1319

1.8587

89

90

0.4761

0.1548

1.4282

0.1548

1.4282

90

91

0.1495

0.1481

0.4485

0.1481

0.4485

91

92

0.0263

0.9820

0.0789

0.9820

Null

92

93

0.6774

0.4351

2.0322

0.4351

Null

93

94

0.3336

0.4401

1.0009

0.4401

Null

94

95

0.3045

0.2773

0.9134

0.2773

0.9134

95

96

0.9500

0.5186

2.8499

0.5186

Null

96 (continued)

108

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table 4.15 (continued) i

u1[i]

u2[i]

U1[i]

U2[i]

eta[i]

i

97

0.7374

0.9651

2.2123

0.9651

Null

97

98

0.6385

0.9689

1.9156

0.9689

Null

98

99

0.5705

0.4434

1.7115

0.4434

Null

99

100

0.5668

0.8242

1.7005

0.8242

Null

100

Table 4.16 Showing etaa[i] and y[i] = (Exp[etaa[i]])/(1 + etaa[i]^2). Using prog. 4.16 i

etaa[i]

y[i]

i

etaa[i]

y[i]

1

2.5788

1.7230

26

2.6035

1.7370

2

1.1397

1.3597

27

0.1473

1.1341

3

0.5698

1.3346

28

0.1864

1.1644

4

1.1581

1.3599

29

0.8369

1.3580

5

0.1512

1.1372

30

0.3495

1.2640

6

0.4531

1.3052

31

1.8828

1.4460

7

1.6924

1.4059

32

0.0444

1.0433

8

0.5763

1.3358

33

0.7938

1.3568

9

2.7073

1.7995

34

1.1789

1.3603

10

0.6548

1.3472

35

0.9784

1.3591

11

1.0286

1.3592

36

1.4343

1.3727

12

0.3238

1.2512

37

1.8587

1.4401

13

0.4965

1.3181

38

1.4282

1.3722

14

1.0632

1.3592

39

0.4485

1.3037

15

1.1684

1.3601

40

0.9134

1.3590

16

0.6126

1.3417

17

0.1511

1.1371

18

1.2401

1.3618

19

1.6695

1.4020

20

0.3564

1.2672

21

1.3194

1.3650

22

2.3232

1.5957

23

0.8137

1.3575

24

1.6985

1.4069

25

0.9782

1.3591

4.5

Evaluation of Definite Integrals Using Acceptance-Rejection Sampling …

109

2.0

1

1.5

1.0

0.5

0.0 0.0

0.5

1.0

1.5

2.0

2.5

Fig. 4.16 Showing that random variates ηa are non-uniformly distributed like p(x) = 1.05 e−x in the interval 0 to 3. We find that indeed there are less number of variates for larger values of ηa

Program number 4.16 n = 654321 SeedRandom[n] Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = 0 + 3*u1[i],U2[i] = 0 + 1*u2[i], If[U2[i] < 1.05*Exp[-U1[i]],eta[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","eta[i]"}}] etaa[1] = eta[2];etaa[2] = eta[5];etaa[3] = eta[6];etaa[4] = eta[8]; etaa[5] = eta[14];etaa[6] = eta[15];etaa[7] = eta[18];etaa[8] = eta[23]; etaa[9] = eta[29];etaa[10] = eta[30];etaa[11] = eta[32];etaa[12] = eta[33]; etaa[13] = eta[34];etaa[14] = eta[35];etaa[15] = eta[36];etaa[16] = eta[41]; etaa[17] = eta[42];etaa[18] = eta[43];etaa[19] = eta[44];etaa[20] = eta[46]; etaa[21] = eta[50];etaa[22] = eta[53];etaa[23] = eta[56];etaa[24] = eta[57]; etaa[25] = eta[58];etaa[26] = eta[60];etaa[27] = eta[62];etaa[28] = eta[65]; etaa[29] = eta[67];etaa[30] = eta[70];etaa[31] = eta[72];etaa[32] = eta[74]; etaa[33] = eta[78];etaa[34] = eta[80];etaa[35] = eta[82];etaa[36] = eta[84]; etaa[37] = eta[89];etaa[38] = eta[90];etaa[39] = eta[91];etaa[40] = eta[95];

110

4 Evaluation of Definite Integrals Using Acceptance-Rejection …

Table[{i = i + 1,etaa[i],y[i] = (Exp[etaa[i]])/(1 + etaa[i]^2)},{i,0,39,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","etaa[i]","y[i]"}}] Integral = Sum[0.95*y[i]/40,{i,1,40}] ListPlot[Table[{etaa[i],1},{i,0,39,1}], Frame-> True,FrameLabel-> {"\[Eta]a [i]","1"}] That random variate ηa is non-uniformly distributed like p(x) = 1.05 e−x in the interval 0 to 3 is evidenced by Fig. 4.16 which displays values of ηa against stagnant integer 1. We find that indeed there are less number of variates for larger values of ηa.

5

Variational Monte Carlo Method Applied to Ground State of Simple Harmonic Oscillator Using Acceptance-Rejection Sampling Utilizing Mathematica

This chapter provides practical demonstration of obtaining ground state energy and eigenfunction of simple harmonic oscillator using variational Quantum Monte Carlo method. Necessary background on variational method has been covered so that readers need not consult Quantum Mechanics textbooks. Random variates have been obtained by acceptance-rejection method using Gaussian probability distribution function. Programs written in Mathematica have been used.

5.1

The Variational Method of Quantum Mechanics Applied to Ground State of Any Quantum Mechanical System

Let 0 , 1 , 2 , 3 ,. . . ,  N be exact, unknown, orthonormal eigenfunctions of known Hamiltonian H. Let E 0 < E 1 < E 2 < E 3 < · · · < E N be exact, unknown eigenvalues of energy respectively. That is, we have eigenvalue equation H n = E n n

(5.1)

which we cannot solve. Suppose we wish to estimate E0 and we wish to know approximate form of 0 . Let us choose by guess a suitable function 0 that is expected to resemble unknown 0 and write 0 =

N 

Cn(0) n

(5.2)

n=0

and let us evaluate the quantity E 0 given by

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7_5

111

112

5 Variational Monte Carlo Method Applied to Ground State …

 ∗  H 0 dτ (0 , H 0 ) E 0 = =  0∗ 0 0 dτ (0 , 0 )      (0)  (0)  (0)  (0) C m m , H C n n C m m , C n H n m=0 n=0 m=0 n=0   =  =   (0)  (0)  (0)  (0) C m m , C n n C m m , C n n m=0



n=0



(0)

C m m ,



m=0



(0)

C n E n n m=0 n=0  =   (0)  (0) C m m , C n n m=0

  =

(0)∗

 

(0)

m=0 n=0

 

n=0

using equation (5.1)

n=0

Cm Cn E n (m , n ) (0)∗ (0) Cm Cn (m , n )

=

m=0 n=0

(5.3)

(0)∗

  (0) 2 Cn  E n

(0)

Cm Cn E n δmn

m=0 n=0

 

(0)∗ (0) Cm Cn δmn

=

m=0 n=0

n=0

  (0) 2 Cn 

n=0

Thus   (0) 2 Cn  E n

E 0 − E 0 =

n=0

− E0   (0) 2 Cn 

  (0) 2 Cn  (E n − E 0 )

=

n=0

n=0

  (0) 2 Cn 

(5.4)

n=0

   (0) 2 In Eq. (5.4), E n ≥ E 0 . ∴ E n − E 0 ≥ 0; moreover, Cn  ≥ 0. Thus Eq. (5.4) shows that E0 − E0 ≥ 0 or, E 0 ≥ E 0 . We find that for arbitrary 0 , calculated value of E 0 ≥ E 0 i.e. E 0 is larger than or equal to ground state energy E 0 . E 0 gives upper bound of ground state energy E 0 . If chosen 0 is different from actual unknown  0 , calculated value of E 0 calculated using Eq. (5.3) will be larger than E 0 . If chosen 0 happens to be same as unknown  0 , value of E 0 calculated using Eq. (5.3) becomes equal to E 0 . Thus we can choose arbitrary 0 and calculate E 0 using Eq. (5.3) and get upper bound of E 0 . Usually we choose a suitable 0 known as trial function containing one or more (0) adjustable parameters αi (0) ’s; e.g. 0 = e−α1 x where α1 (0) is an adjustable parameter. Then we use this expression of 0 in Eq. (5.3) and calculated expression of E 0 will ∂ E 0 contain the parameters αi (0) ’s. We then stipulate (0) = 0 for each αi (0) separately. We ∂αi

thus get values of αi (0) ’s for which E 0 is minimum. We can put these values of αi (0) ’s in the expression of E 0 . We then get lowest value of E 0 for the chosen 0 . This upper bound to E 0 will be close to actual unknown value of E 0 if trial function 0 has a form closely resembling that of actual unknown  0 . The entire process is repeated for several different functions as 0 to see which function leads to much lower value of E 0 . If

5.2 The Variational Method of Quantum Mechanics Applied to Ground …

113

we are happy that a calculated value of E 0 is low enough and that it is likely to be close to actual unknown value of E 0 , we can take E 0 ≈ E 0 and  0 ≈ 0 within some approximation.

5.2

The Variational Method of Quantum Mechanics Applied to Ground State of Simple Harmonic Oscillator

 d Suppose, we know Hamiltonian operator H = − 2m + 21 mωc2 x 2 of a simple harmonic dx2 oscillator and we do not know eigenfunctions  n and eigenvalues En of H, because suppose we cannot solve eigenvalue equation H n = E n n of the operator H. Suppose, we wish to estimate ground state energy E0 and wish to know approximate form of corresponding eigenfunction  0 . 2 Let us choose 0 = e−αx by guess as an approximate form of  0 and let us evaluate the quantity 2

E 0 = Now (0 , 0 ) = (e−αx , e−αx ) = 2

1 = 2

2

+∝ 2 x −1 e−2αx 2xd x −∝

1 = 2

+∝ 2 = (x 2 )−1/ 2 e−2αx d x 2

−∝

(0 , H 0 ) (0 , 0 )

(5.5)

e−αx e−αx d x = 2

+∝ 

2

+∝ 2 x −1 e−2αx d x 2

−∝

e−2αx d x 2

+∝ 2 = x −1 e−2αx d x 2

−∝

0

+∝ 2 = (x 2 )1/ 2−1 e−2αx d x 2

0

= (2α)

+∝ 

2

= (2α)−1/ 2 (1 2)

0 −1/ 2 √

π

=

+∝ x n−1 e−ax d x = a −n n using

π 2α

0

where stands for well known Gamma function Thus

π (0 , 0 ) = 2α

(5.6)

We also note that

+∝ 1 π 2 e−2αx d x = 2 2α 0

(5.7)

114

5 Variational Monte Carlo Method Applied to Ground State …

We now turn to (0 , H 0 ). H = T + V where T is operator of kinetic energy and V is operator of potential energy.   2 d 2 0 1 2 2 (5.8) (0 , H0 ) = (0 , (T + V )0 ) = 0 , − + x  mω 0 c 2m d x 2 2  2 2 Now (0 , V 0 ) = 0 , 21 mωc2 x 2 0 = e−αx , 21 mωc2 x 2 e−αx

 1 1 2 2 = mωc2 e−αx , x 2 e−αx = mωc2 2 2

+∝ +∝ 2 2 −2αx 2 2 x e d x = mωc x 2 e−2αx d x

−∝

=

1 mωc2 2

+∝

xe−2αx 2xd x 2

0

+∝ 1 2 2 = mωc (x 2 )1/ 2 e−2αx d(x 2 ) 2

0

0

+∝ 1 1 2 2 = mωc (x 2 )3/ 2−1 e−2αx d(x 2 ) = mωc2 (2α)−3/ 2 (3 2) 2 2 0

=

√ 1 mωc2 1 mωc2 (2α)−3/ 2 (1 2) (1 2) = mωc2 (2α)−3/ 2 (1 2) π = 2 2 8α

 +∝ using 0 x n−1 e−ax d x = a −n n and (n + 1) = n n Thus

mωc2 π (0 , V 0 ) = 8α 2α



π 2α

(5.9)

We also note that

+∝ π 1 2 −2αx 2 x e dx = 8α 2α 0



2 d 2 0 Now (0 , T 0 ) = 0 , − 2m dx2

(5.10)

5.2 The Variational Method of Quantum Mechanics Applied to Ground …

115

    2 −αx 2 d 2 −αx 2 2 d 2 −αx 2 2 e = − = e−αx , − e , e 2m d x 2 2m dx2 +∝ +∝ 2 2 2 2 2 −αx 2 d −αx 2 −αx 2 d e e d x = − e e−αx d x =− 2 2 2m dx m dx =−

2

−∝ +∝

0

 2 d 2 −2αxe−αx d x e−αx dx

m 0

+∝

 2 2 2 2 =− e−αx −2αe−αx − 2αx(−2αx)e−αx d x m 0

+∝

 2 2 2 2 =− e−αx −2αe−αx + 4α 2 x 2 e−αx d x m 0 ⎡ ⎤ +∝ +∝ 2  ⎣ 2 2 −2α =− e−2αx d x + 4α 2 x 2 e−2αx d x ⎦ m 0 0





 1 π 2  2 1 π α π 2 α π 2 −2α =− =− + 4α −α + = m 2 2α 8α 2α m 2 2α 2m 2α using Eqs. (5.7) and (5.10) Thus (0 , T 0 ) =

2 α 2m



π 2α

(5.11)

From Eqs. (5.11) and (5.9) , we gather that  (0 , H 0 ) = (0 , (T + V )0 ) =

2 α mωc2 + 2m 8α



π 2α

Using Eqs. (5.12) and (5.6) in Eq. (5.5), we get 

2 mωc2  α π + 2m 8α 2α (0 , H 0 ) 2 α mωc2  E 0 = = = + π (0 , 0 ) 2m 8α

(5.12)

(5.13)



We now stipulate that E 0 for

∂ E 0 ∂α

= 0. Hence

α=

2 2m



mωc 2

mωc2 8α 2

= 0. Thus we get lowest value of

(5.14)

116

5 Variational Monte Carlo Method Applied to Ground State …

for the chosen 0 ( =e−αx ). Using Eq. (5.14) in Eq. (5.13), we get the lowest value of 2

E 0 =

2 α 2m

+

mωc2 8α

=

2 mωc 2m 2

+

mωc2 mω 8 2c

.

We take ground state energy as

E 0 ≈ E 0 =

1 ωc 2

(5.15)

This incidentally agrees exactly with known value of ground state energy E0 of simple 2 harmonic oscillator. The ground state eigenfunction 0 = e−αx becomes 1 mωc 

0 ≈ 0 = e− 2

x2

(5.16)

which incidentally agrees exactly with known ground state eigenfunction  0 of energy of simple harmonic oscillator. Thus, to some extent, we have got familiar with use of variational method.

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational Quantum Monte Carlo Method and Acceptance-Rejection Sampling

We consider an electron residing in a simple harmonic oscillator potential V (x) =

1 1 mωc2 x 2 = kx 2 2 2

(5.17)

We first gather exact results from Quantum Mechanics textbooks. Eigenvalue equation of energy is H = E or,   2 d 2 1 2 2 − (5.18) + mωc x  = E 2m d x 2 2 Eigenvalues of energy are En = (n + 1/2)ωc

(5.19)

where n = 0, 1, 2, 3, …. Ground state energy in unit of ωc is E 0 /(ωc ) = 0.5 and ground state eigenfunction is 1 mωc 

0 = e − 2

x2

1

= e− 2 L x

2

(5.20)

where L=

mωc 

(5.21)

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

117

We now gather results from variational method of Quantum Mechanics detailed in 2 2 Sect. 5.2: for trial wavefunction 0 = e−αx = e−β L x , ground state energy estimated or 0 ,H 0 ) evaluated using Eq. (5.5): E 0 = ( (0 ,0 ) is E0 /(ωc ) = 0.5 and value of β turns out to be 0.5 so that ground state eigenfunction is 1

0 = e−αx = e−β L x = e− 2 L x 2

2

2

(5.22)

We now move onto variational Monte Carlo method of evaluating ground state eigen2 d 2 + value and eigenfunction of energy. We have Hamiltonian operator H = − 2m dx2 1 2 2 2 mωc x or, 2 e−β L x lead

 d H = − 2m + 21 kx 2 which together with trial wavefunction 0 = e−αx = dx2 2

2

2

to

0 H 0 = e−αx H e−αx = − 2

2

2 1 2 2 (−2α + 4α 2 x 2 )e−2αx + k x 2 e−2αx 2m 2

and ground state energy in unit of ωc as E0 (0 , H 0 ) = ωc ωc (0 , 0 )    1 2 1 2 −2αx 2 2 2 −2αx 2  − dx + kx e = (−2α + 4α x )e π 2m 2 ωc 2α  = F(x)d x  = f (x) p(x)d x    2 1 2 1 −2αx 2 1 2 2 − dx = (−2α + 4α x ) + k x  e π ωc 2m 2

(5.23)



Here F(x) =

1 

ωc

 π 2α



f (x) =

2 1 2 2 (−2α + 4α 2 x 2 )e−2αx + k x 2 e−2αx 2m 2   2 1 1 − (−2α + 4α 2 x 2 ) + k x 2 ωc 2m 2

 (5.24)

(5.25)

and 1 2 p(x) =  e−2αx π 2α

(5.26)

118

5 Variational Monte Carlo Method Applied to Ground State …

60000 50000 40000 30000 20000 10000 0 0.00002

0.00001

0

0.00001

0.00002

x Fig. 5.1 Showing F(x) and p(x) using prog. 5.1

We find that average or expectation value of f is the ground state energy in unit of ωc of simple harmonic oscillator. But p(x) should look like F(x). We now plot the functions F(x) and p(x) together for k = 10−18 , β = 0.8, and using the corresponding values of √ mω ωc = (k / m), L =  c , α = β L. We use Program number 5.1 in this regard. See Fig. 5.1. We find that in the interval − 20 × 10−6 < x < + 20 × 10−6 shown, F(x) and p(x) look similar. Hence Eq. (5.23) will give a good estimate of E 0 /(ωc ). And the 2 p(x) = √1π e−2αx will be ideal for use as probability density function. 2α

We now generate random variate g obeying p(x) = √1π e−2αx in the interval − 20 × 2



10−6 < x < + 20 × 10−6 using acceptance-rejection sampling, using Program number 5.2 and resulting Table 5.1. From Table 5.1, we manually gather accepted random variates ga which we use in Program number 5.3 and resulting Table 5.2 to evaluate the integral with the result 0.563 for β = 0.8. Program number 5.1 beta = 0.8 k = 10^-18 m = 9.1*10^-31 hcut = (6.626*10^-34)/(2*3.1416) omegac = Sqrt[k/m] L = m*omegac/hcut

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

119

Table 5.1 Showing u1[i], u2[i], U1[i], U2[i] and g[i]. Using prog. 5.2 U1[i] × 106

U2[i]

g[i] × 106

i

30,010.2

Null

1

14.3841

2

Null

3

33,219.7

−3.2436

4

−4.8036

11,808.8

−4.8036

5

−12.4023

2551.2

−12.4023

6

29,798.2

−6.6611

7

17,113.7

−4.5581

8

3.5258

16,255.2

3.5258

9

0.9089

6.9726

63,625.1

Null

10

0.3658

15.0354

25,607.0

Null

11

0.2945

0.6719

−8.2187

47,031.6

Null

12

13

0.9104

0.9276

16.4157

64,931.4

Null

13

14

0.0504

0.3510

−17.9839

24,566.6

Null

14

15

0.1510

0.1761

−13.9585

12,330.0

Null

15

16

0.3796

0.5239

−4.8149

36,676.2

−4.8149

16

17

0.3586

0.4202

−5.6552

29,414.1

−5.6552

17

18

0.5641

0.1372

2.5654

9601.9

2.5654

18

19

0.0774

0.8924

−16.9043

62,470.6

Null

19

20

0.9848

0.4854

19.3914

33,975.0

Null

20

21

0.3080

0.4945

−7.6816

34,614.2

Null

21

22

0.9390

0.5923

17.5612

41,460.1

Null

22

23

0.1921

0.3830

−12.3158

26,810.1

Null

23

24

0.6754

0.2944

7.0160

20,607.0

7.0160

24

25

0.9737

0.4501

18.9486

31,503.6

Null

25

26

0.7833

0.7877

11.3334

55,142.0

Null

26

27

0.7012

0.5937

8.0473

41,562.5

Null

27

28

0.6616

0.5202

6.4639

36,410.6

6.4639

28

29

0.9024

0.0050

16.0972

349.3

16.0972

29

30

0.2183

0.2361

−11.2695

16,525.3

Null

30

31

0.4477

0.7308

−2.0914

51,152.8

−2.0914

31

32

0.3429

0.2260

−6.2859

15,820.6

−6.2859

i

u1[i]

u2[i]

1

0.8618

0.4287

14.4733

2

0.8596

0.0342

14.3841

2396.9

3

0.7597

0.4609

10.3863

32,259.9

4

0.4189

0.4746

−3.2436

5

0.3799

0.1687

6

0.1899

0.0364

7

0.3335

0.4257

−6.6611

8

0.3860

0.2445

−4.5581

9

0.5881

0.2322

10

0.6743

11

0.8759

12

32 (continued)

120

5 Variational Monte Carlo Method Applied to Ground State …

Table 5.1 (continued) i

u1[i]

u2[i]

U1[i] × 106

U2[i]

g[i] × 106

i

33

0.1079

0.4879

−15.6832

34,153.1

Null

33

34

0.1655

0.1218

−13.3800

8528.3

Null

34

35

0.3544

0.3523

−5.8246

24,661.9

−5.8246

35

36

0.3895

0.0911

−4.4213

6379.1

−4.4213

36

37

0.9631

0.5184

18.5234

36,287.1

Null

37

38

0.6328

0.8258

5.3122

57,804.8

Null

38

39

0.4101

0.6742

−3.5977

47,195.1

−3.5977

39

40

0.2088

0.8631

−11.6477

60,416.6

Null

40

41

0.2042

0.3027

−11.8314

21,188.8

Null

41

42

0.0504

0.8463

−17.9858

59,238.9

Null

42

43

0.4134

0.2451

−3.4653

17,159.9

−3.4653

43

44

0.5565

0.0084

2.2606

590.5

2.2606

44

45

0.3532

0.7547

−5.8727

52,829.8

Null

45

46

0.1188

0.5403

−15.2484

37,819.4

Null

46

47

0.5235

0.8228

0.9410

57,594.9

0.9410

47

48

0.5526

0.8279

2.1048

57,950.5

2.1048

48

49

0.6770

0.7266

7.0808

50,859.2

Null

49

50

0.4398

0.2243

−2.4085

15,698.6

−2.4085

50

51

0.9524

0.7628

18.0977

53,392.6

Null

51

52

0.9227

0.7673

16.9092

53,713.2

Null

52

53

0.7744

0.0388

10.9761

2719.4

10.9761

53

54

0.4234

0.3146

−3.0639

22,023.8

−3.0639

54

55

0.2849

0.8526

−8.6025

59,684.4

Null

55

56

0.2712

0.4589

−9.1511

32,126.5

Null

56

57

0.5662

0.1270

2.6466

8890.7

2.6466

57

58

0.3261

0.1921

−6.9575

13,447.8

−6.9575

58

59

0.9294

0.0971

17.1748

6794.7

Null

59

60

0.8678

0.0527

14.7133

3688.1

Null

60

61

0.6010

0.5101

4.0384

35,704.8

4.0384

61

62

0.0491

0.8662

−18.0358

60,633.2

Null

62

63

0.8311

0.3699

13.2452

25,891.2

Null

63

64

0.6055

0.7086

4.2186

49,602.9

4.2186

64 (continued)

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

121

Table 5.1 (continued) i

u1[i]

u2[i]

U1[i] × 106

g[i] × 106

i

65

0.0621

0.0293

−17.5153

2047.7

Null

65

66

0.9600

0.7918

67

0.2790

0.3078

18.3989

55,422.5

Null

66

−8.8417

21,543.3

−8.8417

67

68

0.9336

0.7173

17.3432

50,208.8

Null

68

69

0.9645

70

0.1165

0.1720

18.5788

12,036.8

Null

69

0.6613

−15.3398

46,294.1

Null

70

71 72

0.9249

0.6311

16.9949

44,175.3

Null

71

0.6276

0.1130

5.1038

7909.1

5.1038

72

73

0.1215

0.9735

−15.1410

68,146.4

Null

73

74

0.0148

0.2807

−19.4085

19,648.2

Null

74

75

0.1744

0.8147

−13.0242

57,031.1

Null

75

76

0.9503

0.8674

18.0118

60,715.6

Null

76

77

0.3100

0.7364

−7.6006

51,549.8

Null

77

78

0.2646

0.1094

−9.4165

7655.8

−9.4165

78

79

0.1454

0.9416

−14.1828

65,914.5

Null

79

80

0.3930

0.0684

−4.2813

4789.3

−4.2813

80

81

0.6325

0.3698

5.2995

25,883.9

5.2995

81

82

0.3261

0.3225

−6.9549

22,576.3

−6.9549

82

83

0.7350

0.4885

9.3983

34,196.5

Null

83

84

0.4781

0.0687

−0.8760

4809.9

−0.8760

84

85

0.7332

0.7276

9.3293

50,934.5

Null

85

86

0.2226

0.7403

−11.0940

51,817.5

Null

86

87

0.6044

0.2942

4.1747

20,595.0

4.1747

87

88

0.3337

0.8206

−6.6509

57,443.5

Null

88

89

0.6196

0.1319

4.7825

9235.9

4.7825

89

90

0.4761

0.1548

−0.9568

10,836.1

−0.9568

90

91

0.1495

0.1481

−14.0204

10,363.8

Null

91

92

0.0263

0.9820

−18.9479

68,741.9

Null

92

93

0.6774

0.4351

7.0961

30,459.3

7.0961

93

94

0.3336

0.4401

−6.6548

30,809.2

−6.6548

94

95

0.3045

0.2773

−7.8218

19,412.8

−7.8218

95

96

0.9500

0.5186

17.9990

36,302.8

Null

U2[i]

96 (continued)

122

5 Variational Monte Carlo Method Applied to Ground State …

Table 5.1 (continued) U2[i]

g[i] × 106

i

9.4971

67,554.9

Null

97

0.9689

5.5414

67,820.4

Null

98

0.4434

2.8194

31,038.3

2.8194

99

0.5668

0.8242

2.6732

57,691.2

2.6732

100

101

0.7161

0.5652

8.6434

39,563.4

Null

101

102

0.9431

0.7145

17.7231

50,014.2

Null

102

103

0.7682

0.5920

10.7287

41,436.6

Null

103

104

0.3412

0.8780

−6.3514

61,462.9

Null

104

105

0.5395

0.4653

1.5815

32,569.2

1.5815

105

106

0.4578

0.6364

−1.6861

44,549.1

−1.6861

106

107

0.4551

0.3154

−1.7949

22,074.6

−1.7949

107

108

0.2764

0.2152

−8.9455

15,063.4

−8.9455

108

109

0.8724

0.2879

14.8980

20,150.8

Null

109

110

0.5121

0.0132

0.4851

921.0

0.4851

110

111

0.2828

0.9446

−8.6884

66,123.1

Null

111

112

0.5885

0.0416

3.5382

2914.5

3.5382

112

113

0.1807

0.7244

−12.7704

50,709.7

Null

113

114

0.4751

0.8880

−0.9955

62,157.1

−0.9955

114

115

0.2541

0.6059

−9.8370

42,410.7

Null

115

116

0.0221

0.9858

−19.1146

69,007.1

Null

116

117

0.3934

0.1550

−4.2629

10,848.9

−4.2629

117

118

0.2128

0.9825

−11.4873

68,777.4

Null

118

119

0.2589

0.9184

−9.6452

64,285.6

Null

119

120

0.6655

0.6142

6.6212

42,995.5

Null

120

121

0.9326

0.1509

17.3032

10,565.1

Null

121

122

0.6705

0.5234

6.8218

36,637.7

Null

122

123

0.2799

0.7116

−8.8049

49,812.7

Null

123

124

0.3740

0.9556

−5.0383

66,894.7

Null

124

125

0.7299

0.8112

9.1966

56,781.8

Null

125

126

0.9289

0.0862

17.1551

6030.7

Null

126

127

0.4937

0.4432

−0.2525

31,024.0

−0.2525

127

128

0.4509

0.5649

−1.9629

39,542.1

−1.9629

i

u1[i]

u2[i]

97

0.7374

0.9651

98

0.6385

99

0.5705

100

U1[i] × 106

128 (continued)

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

123

Table 5.1 (continued) i

u1[i]

u2[i]

U1[i] × 106

U2[i]

g[i] × 106

i

129

0.0499

0.8580

−18.0045

60,060.7

Null

129

130

0.2225

0.0270

−11.1003

1889.6

−11.1003

130

131

0.8603

0.3734

14.4113

26,138.5

Null

131

132

0.6852

0.3196

7.4078

22,369.4

7.4078

132

133

0.0708

0.2959

−17.1688

20,713.4

Null

133

134

0.9198

0.6120

16.7904

42,837.3

Null

134

135

0.8673

0.5239

14.6908

36,672.0

Null

135

136

0.6730

0.2424

6.9202

16,967.0

6.9202

136

137

0.5179

0.8175

0.7168

57,226.1

0.7168

137

138

0.2558

0.6163

−9.7693

43,142.2

Null

138

139

0.0526

0.5841

−17.8964

40,889.3

Null

139

140

0.3158

0.1379

−7.3693

9649.5

−7.3693

140

141

0.4767

0.6441

−0.9309

45,087.6

−0.9309

141

142

0.6311

0.0228

5.2451

1595.1

5.2451

142

143

0.2654

0.8424

−9.3824

58,967.9

Null

143

144

0.8255

0.9899

13.0180

69,294.3

Null

144

145

0.5169

0.3570

0.6744

24,987.9

0.6744

145

146

0.9078

0.1674

16.3117

11,718.6

Null

146

147

0.7129

0.3596

8.5154

25,168.8

Null

147

148

0.8932

0.0561

15.7284

3928.0

Null

148

149

0.1703

0.5996

−13.1883

41,970.1

Null

149

150

0.9517

0.4264

18.0692

29,844.9

Null

150

151

0.6781

0.8373

7.1234

58,612.4

Null

151

152

0.6383

0.4100

5.5311

28,702.8

5.5311

152

153

0.8559

0.2554

14.2343

17,875.0

Null

153

154

0.3567

0.4564

−5.7337

31,949.7

−5.7337

154

155

0.5290

0.3290

1.1615

23,028.4

1.1615

155

156

0.5712

0.8274

2.8460

57,917.9

2.8460

156

157

0.6910

0.4119

7.6396

28,830.2

7.6396

157

158

0.2121

0.4267

−11.5169

29,871.9

Null

158

159

0.0268

0.4474

−18.9269

31,318.3

Null

159

160

0.1947

0.6496

−12.2108

45,474.1

Null

160 (continued)

124

5 Variational Monte Carlo Method Applied to Ground State …

Table 5.1 (continued) U2[i]

g[i] × 106

i

12.0035

36,955.0

Null

161

0.2609

−11.9901

18,266.0

Null

162

0.0161

−11.3802

1129.3

−11.3802

163

0.7723

0.9430

10.8902

66,009.0

Null

164

165

0.5086

0.7920

0.3448

55,443.0

0.3448

165

166

0.6609

0.2570

6.4366

17,987.2

6.4366

166

167

0.0900

0.1263

−16.4009

8841.2

Null

167

168

0.2807

0.7241

−8.7706

50,687.9

Null

168

169

0.0896

0.8471

−16.4170

59,298.1

Null

169

170

0.1760

0.6250

−12.9595

43,748.2

Null

170

171

0.5264

0.5202

1.0540

36,412.9

1.0540

171

172

0.8276

0.8793

13.1053

61,548.2

Null

172

173

0.2708

0.7876

−9.1699

55,133.3

Null

173

174

0.4623

0.2713

−1.5065

18,988.0

−1.5065

174

175

0.5351

0.6979

1.4051

48,853.8

1.4051

175

176

0.5660

0.4703

2.6405

32,922.7

2.6405

176

177

0.7876

0.3392

11.5042

23,742.9

Null

177

178

0.1836

0.9136

−12.6562

63,953.5

Null

178

179

0.3897

0.9718

−4.4112

68,028.7

Null

179

180

0.0294

0.0927

−18.8259

6487.0

Null

180

181

0.4954

0.3589

−0.1852

25,121.6

−0.1852

181

182

0.6253

0.0256

5.0138

1789.6

5.0138

182

183

0.9012

0.4223

16.0461

29,560.3

Null

183

184

0.0465

0.6019

−18.1392

42,131.6

Null

184

185

0.2193

0.4897

−11.2263

34,277.5

Null

185

186

0.1713

0.2252

−13.1469

15,766.8

Null

186

187

0.1002

0.6847

−15.9904

47,927.8

Null

187

188

0.4652

0.1787

−1.3928

12,512.2

−1.3928

188

189

0.9316

0.2757

17.2646

19,296.1

Null

189

190

0.7848

0.9415

11.3936

65,907.6

Null

190

191

0.3975

0.3792

−4.1004

26,543.6

−4.1004

191

192

0.2350

0.7681

−10.6004

53,769.5

Null

i

u1[i]

u2[i]

161

0.8001

0.5279

162

0.2002

163

0.2155

164

U1[i] × 106

192 (continued)

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

125

Table 5.1 (continued) i

u1[i]

u2[i]

U1[i] × 106

U2[i]

g[i] × 106

i

193

0.1693

0.2771

−13.2272

19,394.0

Null

193

194

0.3306

0.3072

−6.7750

21,504.7

−6.7750

194

195

0.8652

0.7051

14.6060

49,360.3

Null

195

196

0.7953

0.8995

11.8124

62,967.3

Null

196

197

0.4876

0.3045

−0.4979

21,313.5

−0.4979

197

198

0.5590

0.7461

2.3588

52,225.5

2.3588

198

199

0.1489

0.0499

−14.0447

3492.7

−14.0447

199

200

0.5615

0.6135

2.4603

42,946.1

2.4603

200

alpha = beta*L F = (1/(hcut*omegac*Sqrt[Pi/(2*alpha)]))* ((-hcut^2/(2*m))*(-2*alpha + 4*alpha^2*x^2)* Exp[-2*alpha*x^2] + (1/2)*k*x^2*Exp[-2*alpha*x^2]) p = (1/(Sqrt[Pi/(2*alpha)]))*Exp[-2*alpha*x^2] Plot[{F,p},{x,-20*10^-6,20*10^-6}, PlotStyle-> {{Black},{Dashed,Black}},Frame-> True, FrameLabel-> {"x", "F(x) or p(x)"}]

F(x) =

1 

ωc

 π 2α



2 1 2 2 (−2α + 4α 2 x 2 )e−2αx + k x 2 e−2αx 2m 2



and p(x) = √1π e−2αx as function of x for chosen value of spring constant k = 10−18 2



that corresponds to ωc = 1.048 MHz, L = 7.237 × 109 .

mωc 

= 9.046 × 109 , β = 0.8, α = βL =

126

5 Variational Monte Carlo Method Applied to Ground State …

  1 − 2 (−2α + 4α 2 ga[i]2 ) + 1 k ga[i]2 . Using prog. 5.3 Table 5.2 Showing ga[i] and y[i] = ω 2m 2 c i

ga[i] × 106

1 2

ga[i] × 106

y[i]

i

y[i]

14.3841

−0.6598

51

−0.9955

0.7930

−3.2436

0.7258

52

−4.2629

0.6718

3

−4.8036

0.6372

53

−0.2525

0.7996

4

−12.4023

−0.2853

54

−1.9629

0.7728

5

−6.6611

0.4869

55

−11.1003

−0.0694

6

−4.5581

0.6534

56

7.4078

0.4128

7

3.5258

0.7123

57

6.9202

0.4621

8

−4.8149

0.6364

58

0.7168

0.7964

9

−5.6552

0.5743

59

−7.3693

0.4168

10

2.5654

0.7536

60

−0.9309

0.7939

11

7.0160

0.4527

61

5.2451

0.6059

12

6.4639

0.5052

62

0.6744

0.7968

13

16.0972

−1.0283

63

5.5311

0.5841

14

−2.0914

0.7691

64

−5.7337

0.5680

15

−6.2859

0.5212

65

1.1615

0.7905

16

−5.8246

0.5606

66

2.8460

0.7429

17

−4.4213

0.6621

67

7.6396

0.3882

18

−3.5977

0.7087

68

−11.3802

−0.1138

19

−3.4653

0.7153

69

0.3448

0.7992

20

2.2606

0.7639

70

6.4366

0.5077

21

0.9410

0.7938

71

1.0540

0.7922

22

2.1048

0.7687

72

−1.5065

0.7840

23

−2.4085

0.7591

73

1.4051

0.7861

24

10.9761

−0.0500

74

2.6405

0.7508

25

−3.0639

0.7338

75

−0.1852

0.7998

26

2.6466

0.7506

76

5.0138

0.6226

27

−6.9575

0.4584

77

−1.3928

0.7863

28

4.0384

0.6849

78

−4.1004

0.6814

29

4.2186

0.6744

79

−6.7750

0.4761

30

−8.8417

0.2484

80

−0.4979

0.7983

31

5.1038

0.6162

81

2.3588

0.7607

32

−9.4165

0.1744

82

−14.0447

−0.5918 (continued)

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

127

Table 5.2 (continued) i

ga[i] × 106

y[i]

−4.2813

0.6707

34

5.2995

0.6018

35

−6.9549

0.4587

36

−0.8760

0.7946

37

4.1747

0.6770

38

4.7825

0.6386

39

−0.9568

0.7935

40

7.0961

0.4447

41

−6.6548

0.4875

42

−7.8218

0.3683

43

2.8194

0.7439

44

2.6732

0.7496

45

1.5815

0.7824

46

−1.6861

0.7799

47

−1.7949

0.7773

48

−8.9455

0.2354

49

0.4851

0.7983

50

3.5382

0.7117

33

i 83

ga[i] × 106 2.4603

Program number 5.2 n = 654321 SeedRandom[n] beta = 0.8 k = 10^-18 m = 9.1*10^-31 hcut = (6.626*10^-34)/(2*3.1416) omegac = Sqrt[k/m] L = m*omegac/hcut alpha = beta*L Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -20*10^-6 + (40*10^-6)*u1[i];10^6*U1[i], U2[i] = 0 + (70000)*u2[i], If[U2[i] < (1/(Sqrt[Pi/(2*alpha)]))*Exp[-2*alpha*(U1[i]^2)], g[i] = 10^6*U1[i],Null]},{i,0,199,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}]

y[i] 0.7573

128

5 Variational Monte Carlo Method Applied to Ground State …

Program number 5.3 n = 654321 SeedRandom[n] beta = 0.8 k = 10^-18 m = 9.1*10^-31 hcut = (6.626*10^-34)/(2*3.1416) omegac = Sqrt[k/m] L = m*omegac/hcut alpha = beta*L Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i] = -20*10^-6 + (40*10^-6)*u1[i];10^6*U1[i], U2[i] = 0 + (70000)*u2[i], If[U2[i] < (1/(Sqrt[Pi/(2*alpha)]))*Exp[-2*alpha*(U1[i]^2)], g[i] = (10^6)*U1[i],Null]},{i,0,199,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}] ga[1] = g[2];ga[2] = g[4];ga[3] = g[5];ga[4] = g[6];ga[5] = g[7];ga[6] = g[8]; ga[7] = g[9];ga[8] = g[16];ga[9] = g[17];ga[10] = g[18];ga[11] = g[24]; ga[12] = g[28];ga[13] = g[29];ga[14] = g[31];ga[15] = g[32];ga[16] = g[35]; ga[17] = g[36];ga[18] = g[39];ga[19] = g[43];ga[20] = g[44];ga[21] = g[47]; ga[22] = g[48];ga[23] = g[50];ga[24] = g[53];ga[25] = g[54];ga[26] = g[57]; ga[27] = g[58];ga[28] = g[61];ga[29] = g[64];ga[30] = g[67];ga[31] = g[72]; ga[32] = g[78];ga[33] = g[80];ga[34] = g[81];ga[35] = g[82];ga[36] = g[84]; ga[37] = g[87];ga[38] = g[89];ga[39] = g[90];ga[40] = g[93];ga[41] = g[94]; ga[42] = g[95];ga[43] = g[99];ga[44] = g[100];ga[45] = g[105];ga[46] = g[106]; ga[47] = g[107];ga[48] = g[108];ga[49] = g[110];ga[50] = g[112];ga[51] = g[114]; ga[52] = g[117];ga[53] = g[127];ga[54] = g[128];ga[55] = g[130];ga[56] = g[132]; ga[57] = g[136];ga[58] = g[137];ga[59] = g[140];ga[60] = g[141];ga[61] = g[142]; ga[62] = g[145];ga[63] = g[152];ga[64] = g[154];ga[65] = g[155];ga[66] = g[156]; ga[67] = g[157];ga[68] = g[163];ga[69] = g[165];ga[70] = g[166];ga[71] = g[171]; ga[72] = g[174];ga[73] = g[175];ga[74] = g[176];ga[75] = g[181];ga[76] = g[182]; ga[77] = g[188];ga[78] = g[191];ga[79] = g[194];ga[80] = g[197];ga[81] = g[198]; ga[82] = g[199];ga[83] = g[200];

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

129

Table[{i = i + 1,ga[i], y[i] = (1/(hcut*omegac))*((-hcut^2/(2*m))* (-2*alpha + (4*alpha^2)*((10^-6)*ga[i])^2) + 0.5*k*((10^-6)*ga[i])^2)},{i,0,82,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","ga[i]", "y[i]"}}] Integral = Sum[y[i]/(83),{i,1,83}] ListPlot[Table[{ga[i],1},{i,0,82,1}],Frame-> True, FrameLabel-> {"ga [i] × 10^6","1"}] That random variate ga is distributed as p(x) = √1π e−2αx is evidenced by Fig. 5.2 2



which displays values of ga against stagnant integer 1. We find that indeed there are more variates near ga = 0. We have repeated all the calculations for various values of β namely 0.3 to 2 and obtained values of E 0 /(ωc ) in each case. Table 5.3 shows the results which are plotted in Fig. 5.3. We find from Fig. 5.3 that minimum value of E 0 /(ωc ) = 0.5 occurs for β 1 mωc 2 1 2 2 = 0.5. Thus the ground state eigenfunction is 0 = e−β L x = e− 2 L x = e− 2  x while ground state eigenvalue of energy is E 0 = 0.5ωc .

2.0

1

1.5

1.0

0.5

0.0 10

5

0

5

10

15

2 Fig. 5.2 Showing that random variates ga are distributed as p(x) = 1π e−2αx . There are more

variates near ga = 0



130

5 Variational Monte Carlo Method Applied to Ground State …

Table 5.3 Showing values of ground state energy E 0 /(ωc ) of an electron in simple harmonic oscillator potential V (x) = 21 k x 2 with k = 10−18 , for various values of variational parameter β contained in trial wave function 0 = e−β L x = e−β 2

mωc 

β

E 0 /(ωc )

0.3

0.578

0.5

0.500

0.8

0.563

1.1

0.631

1.5

0.812

2

0.966

x2

1 0.9 0.8

E0 /( ωc )

0.7 0.6 0.5 0.4 0

0.5

1

β

1.5

2

Fig. 5.3 Showing values of ground state energy E 0 /(ωc ) of an electron in simple harmonic oscillator potential V (x) = 21 k x 2 with k = 10−18 , for various values of variational parameter β contained in trial wavefunction 0 = e−β L x = e−β

mωc 

x 2 . Minimum value of E /(ω ) = 0.5 occurs for β c 0 1 mωc 2 1 2 2 = 0.5. Thus the ground state eigenfunction is 0 = e−β L x = e− 2 L x = e− 2  x while ground state eigenvalue of energy is E 0 = 0.5ωc 2

Program number 5.3 rewritten (in Mathematica) n=654321; SeedRandom[n]; beta=0.8; a=-20*10^-6; b=20*10^-6; c=70000; k=10^-18; m=9.1*10^-31;

5.3

Ground State Energy of Simple Harmonic Oscillator Using Variational …

hcut=(6.626*10^-34)/(2*3.1416); omegac=Sqrt[k/m]; L=m*omegac/hcut; alpha=beta*L; Table[{i=i+1,u1[i]=RandomReal[],u2[i]=RandomReal[], U1[i]=a +(b-a)*u1[i];10^6*U1[i],U2[i]=0+(c-0)*u2[i], If[U2[i]{2,2}, TableHeadings->{None,{"i","u1[i]","u2[i]","U1[i] x 10^6","U2[i]", "g[i] x 10^6","i"}}] ga={}; i=0; While[i{None,{"i","ga[i] x 10^6","y[i]"}}] Integral=Sum[y[i]/(n1),{i,1,n1}] ListPlot[Table[{ga[[i]],1},{i,0,n1-1,1}], Frame->True,FrameLabel->{"ga [i] x 10^6","1"}]

131

Concluding Remarks

(1) The book is intended for undergraduate students of Mathematics, Statistics and Physics who wish to know how Monte Carlo Methods work with calculations done using a user friendly program Mathematica. (2) All treatments have been done as less manually as practicable. (3) All calculations except for gathering accepted values of random variate in acceptancerejection sampling have been done using Mathematica. (4) Definite integrals of a number of functions F(x) have been evaluated using uniform, linear, Gaussian and exponential probability density functions p(x). (5) It is shown that results agree with known exact values better if p(x) is proportional to F(x). (6) Deviation from the proportionality has been shown to result in worse agreement. (7) All necessary background materials are covered. (8) A separate chapter has been dedicated to variational Monte Carlo method applied to ground state of simple harmonic oscillator. Necessary background on variational method has been covered so that readers need not consult Quantum Mechanics textbooks. (9) The book is a good read; it is intended to turn the readers adept in using the method with Mathematica.

© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7

133

Appendix: Handouts for Computational Lab and Automated Collection of Accepted Random Variates

A.1

Experiment I: Monte Carlo Integration Using Inverse Transform Sampling Using (a) Mathematica and (b) MS Excel

Let us consider the definite integral 5 I =

5 F(x)d x =

3

[1 − (x − 4)2 ]d x 3

Let us rewrite the expression as 5 I =

F(x) p(x)d x = p(x)

3

5 f (x) p(x)d x 3

If p is considered or taken as a normalized probability density function, knowledge of elementary statistics tells us that average or expectation value of f is the value of the integral. Thus we can write I ≈

N 1  F(xi ) N p(xi ) i=1

. where x i ’s are random values of x in the interval 3 < x < 5 obeying probability density function p(x). N should be large and according to what is called importance sampling, p(x) at least should follow F(x), if not be proportional to F(x), to get a good value of the integral using the prescription

© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7

135

136

Appendix: Handouts for Computational Lab and Automated …

1.0

0.8

F, p

0.6

0.4

0.2

0.0 3.0

3.5

4.0

4.5

5.0

x Fig. A.1 Showing F(x) = 1 − (x − 4)2 as undashed curve and p(x) = 1/2 as dashed curve or line obtained using the following command in Mathematica F = 1-(x-4)^2; Plot[{F,1/2},{x,3,5},Frame-> True,PlotStyle-> {{Black},{Black,Dashed}}, FrameLabel-> {"x","F, p"}].

I ≈

N 1  F(xi ) N p(xi ) i=1

. See Fig. A.1 in which we have plotted F(x) = 1 − (x − 4)2 along with normalized probability density function of a uniform random variable in the interval 3–5 given by 5 p(x) = 1/2. Let p(x) = C, a constant. Normalization requires that 3 Cd x = 1 which gives C = 1/2. Uniform p(x) = 1/2 is not too different from a slowly varying function of x like F(x) = 1 − (x − 4)2 in the interval 3 < x < 5. Hence p(x) = 1/2 is a good choice. We now generate random variate η obeying p(x) = 1/2 in the interval 3 < x < 5 using what is called inverse transform sampling according to which values of η are obtainable η from the equation: 3 p(x)d x = u with p = 1/2. Here u stands for random numbers uniformly distributed in the interval 0–1. As such η = 2u + 3. Thus

Appendix: Handouts for Computational Lab and Automated …

I ≈

137

N N N N N 1  F(xi ) 2  2  1  F(ηi ) 2  F(ηi ) = [1 − (ηi − 4)2 ] = Yi = = N p(xi ) N 1/2 N N N i=1

i=1

i=1

i=1

i=1

Program A.1 (written in Mathematica) No = 100; n = 654321; SeedRandom[n]; Table[{i = i + 1,u[i] = RandomReal[],eta[i] = 2*u[i] + 3, Y[i] = 1-(eta[i]-4)^2},{i,0,No-1,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u[i]","eta[i]","Y[i]"}}] I1=Integral = Sum[2*Y[i]/No,{i,1,No}] ListPlot[Table[{eta[i],1},{i,0,No-1,1}],Frame-> True, FrameLabel-> {"\[Eta] [i]","1"}] Integrate[1-(x-4)^2,{x,3,5}]; I2 = N[%] PercentageError = (I1-I2)*100/I2 Using program A.1 and resulting Table A.1, we now evaluate the integral with the result 1.41127 rather than 1.33333. The 5.84% difference is attributable to the facts that F(x) and p(x) used are not proportional to each other and that we have used only 100 random variates. That random variate η is uniformly distributed in the interval 3–5 is evidenced by Fig. A.2 which displays values of η against stagnant integer 1. We have chosen n = 654321 by trial and error to get a set of values of ui and hence of ηi uniformly distributed in the interval 0 to 1 and 3 to 5 respectively. We used the command SeedRandom[n] to get ui ’s reproducibly. As a quality check of the random numbers u[i]’s, we can plot u[i]’s against a stagnant integer 1 to ascertain if u[i]’s are indeed nearly uniformly distributed in the interval 0–1. If not, we can run the spread-sheet as many times as necessary. In this regard, we have obtained Fig. A.3 using columns titled B and C of Table A.2.

138

Appendix: Handouts for Computational Lab and Automated …

Table A.1 Showing tabulated values of ui , ηi = 2ui + 3, Yi = 1 − (ηi − 4)2 from which we get  N F(xi ) 1  N F(ηi ) = 2  N [1 − (η − 4)2 ] = 2  N Y as an approximate I = N1 i=1 i i=1 1/2 i=1 i=1 i N N p(xi ) = N 5 5 value of the integral I = 3 F(x)d x = 3 [1 − (x − 4)2 ]d x = 1.41127 whereas exact value is 1.33333. Error is 5.84%. Using p(x) = 1/2. Using prog. A.1 i

u[i]

η[i]

Y [i]

1

0.86183

4.72366

0.47631

2

0.42872

3.85743

0.97968

3

0.85960

4.71920

0.48275

4

0.03424

3.06848

0.13227

5

0.75966

4.51932

0.73031

6

0.46086

3.92171

0.99387

7

0.41891

3.83782

0.97370

8

0.47457

3.94913

0.99741

9

0.37991

3.75982

0.94231

10

0.16870

3.33739

0.56095

11

0.18994

3.37988

0.61546

12

0.03645

3.07289

0.14047

13

0.33347

3.66694

0.88907

14

0.42569

3.85138

0.97791

15

0.38605

3.77209

0.94806

16

0.24448

3.48896

0.73884

17

0.58815

4.17629

0.96892

18

0.23222

3.46443

0.71317

19

0.67431

4.34863

0.87846

20

0.90893

4.81786

0.33111









81

0.20422

3.40843

0.65005

82

0.30270

3.60539

0.84429

83

0.05035

3.10071

0.19128

84

0.84627

4.69254

0.52039

85

0.41337

3.82673

0.96998

86

0.24514

3.49028

0.74019

87

0.55651

4.11303

0.98723 (continued)

Appendix: Handouts for Computational Lab and Automated …

139

Table A.1 (continued) i

u[i]

η[i]

Y [i]

88

0.00844

3.01687

0.03346

89

0.35318

3.70637

0.91378

90

0.75471

4.50942

0.74049

91

0.11879

3.23758

0.41872

92

0.54028

4.08055

0.99351

93

0.52352

4.04705

0.99779

94

0.82278

4.64557

0.58324

95

0.55262

4.10524

0.98892

96

0.82786

4.65573

0.57002

97

0.67702

4.35404

0.87465

98

0.72656

4.45312

0.79468

99

0.43979

3.87957

0.98550

100

0.22427

3.44853

0.69588

2.0

1

1.5

1.0

0.5

0.0 3.0

3.5

4.0

4.5

5.0

Fig. A.2 Demonstrating that random variates ηi ’s are uniformly distributed in the interval 3–5

140

Appendix: Handouts for Computational Lab and Automated …

Table A.2 Showing that the same calculations as above can be performed using MS Excel spreadsheet. (But the option of getting same values of u[i]’s every time reproducibly is not available.) To run the spread sheet, we need to copy the bold face quantities and paste in MS Excel spread sheet. Using prog. A.2 A

B

C

D

E

1

i

u[i]

1

eta[i]

Y[i]

2

1

= RAND()

1

= 2*B2 + 3

= 1-(D2-4)^2

3

2

1

4

3

1











… 1

No

integral = 2 × (sum of Y[i]’s) / No

1.2

1

1

0.8

0.6

0.4

0.2

0 0.0

0.2

0.4

0.6

0.8

1.0

u[i] Fig. A.3 Figure to ascertain that random variates ui ’s used are nearly uniformly distributed in the interval 0–1

Appendix: Handouts for Computational Lab and Automated …

A.2

141

Experiment II: Monte Carlo Integration Using Acceptance-Rejection Sampling, Gaussian Pdf and Mathematica

Let us consider the definite integral 5 I =

5 F(x)d x =

3

[1 − (x − 4)2 ]d x 3

Let us rewrite the expression as 5 I =

F(x) p(x)d x = p(x)

3

5 f (x) p(x)d x 3

If p is considered or taken as a normalized probability density function, knowledge of elementary statistics tells us that average or expectation value of f is the value of the integral. Thus we can write I ≈

N 1  F(xi ) N p(xi ) i=1

where x i ’s are random values of x in the interval 3 < x < 5 obeying probability density function p(x). N should be large and according to what is called importance sampling, p(x) at least should follow F(x), if not be proportional to F(x), to get a good value of the  N F(xi ) integral using the prescription I ≈ N1 i=1 p(xi ) . See Fig. A.4 in which we have plotted F(x) = 1 − (x − 4)2 along with normalized  x−a 2 1 1 Gaussian probability density function p(x) = √ exp − 2 σ of random variable σ 2π x in the interval 3–5 for variance σ = 1/3 and average a = 4. The Gaussian pdf is not too different from the function F(x) = 1 − (x − 4)2 in the interval 3 < x < 5. Hence the Gaussian pdf is a good choice.   2 We now generate values of random variate g obeying p(x) = √1 exp − 21 x−a σ σ 2π in the interval 3 < x < 5 using what is called acceptance-rejection sampling. According to the sampling method, we first generate 2 sets of uniform random numbers u1 and u2 in the interval 0–1. We then obtain 2 sets of uniform random numbers U1 and U2 in the interval 3 to 5 and 0 to 1.2 respectively, using 3 + (5 − 3)u1 and 0 + (1.2 − 0)u2 respectively according to what we know from inverse transform sampling method. Here 3–5 is the span of the integration and 0 to 1.2 is the span of the Gaussian pdf. Thereafter, for each pair (U1, U2), we calculate p(x = U1) i.e. we calculate p for the value of U1;

142

Appendix: Handouts for Computational Lab and Automated …

1.2 1.0

F, p

0.8 0.6 0.4 0.2 0.0

3.0

3.5

4.0

4.5

5.0

x   2 Fig. A.4 Showing F(x) = 1 − (x − 4)2 as undashed curve and p(x) = √1 exp − 21 x−a σ σ 2π as dashed curve obtained using the following command in Mathematica, and using variance sig = 1/3 and average avg = 4 p1 = Plot[1-(x-4)^2,{x,3,5},PlotStyle-> {Black},Frame-> True, FrameLabel-> {"x","F, p"},PlotRange-> {0,1.2}]; sig = 1/3;avg = 4; p2 = Plot[(1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((x-avg)/sig)^2],{x,3,5}, PlotStyle-> {Dashed,Black},Frame-> True]; Show[p1,p2]

and if the U2 < p(x = U1), we accept the value of U1 as a value of random variate g and call it ga. Otherwise we reject the value of U1 as g. Using program A.2 and resulting Table A.3, we gather ga[i]’s as. ga[1]=g[2];ga[2]=g[4];ga[3]=g[5];ga[4]=g[6];ga[5]=g[7]; ga[6]=g[8];ga[7]=g[9];ga[8]=g[16];ga[9]=g[17];ga[10]=g[18]; …, ga[82]=g[176];ga[83]=g[181];ga[84]=g[182];ga[85]=g[188]; ga[86]=g[191];ga[87]=g[194];ga[88]=g[197];ga[89]=g[198]; ga[90]=g[199];ga[91]=g[200];

Appendix: Handouts for Computational Lab and Automated …

143

Table A.3 Table to help obtain accepted values of random variate g as per the acceptance-rejection sampling method. Using prog. A.2 i

u1[i]

u2[i]

U1[i]

U2[i]

g[i]

i

1

0.8618

0.4287

4.7237

0.5145

Null

1

2

0.8596

0.0342

4.7192

0.0411

4.7192

2

3

0.7597

0.4609

4.5193

0.5530

Null

3

4

0.4189

0.4746

3.8378

0.5695

3.8378

4

5

0.3799

0.1687

3.7598

0.2024

3.7598

5

6

0.1899

0.0364

3.3799

0.0437

3.3799

6

7

0.3335

0.4257

3.6669

0.5108

3.6669

7

8

0.3860

0.2445

3.7721

0.2934

3.7721

8

9

0.5881

0.2322

4.1763

0.2787

4.1763

9

10

0.6743

0.9089

4.3486

1.0907

Null

10















191

0.3975

0.3792

3.7950

0.4550

3.7950

191

192

0.2350

0.7681

3.4700

0.9218

Null

192

193

0.1693

0.2771

3.3386

0.3325

Null

193

194

0.3306

0.3072

3.6613

0.3687

3.6613

194

195

0.8652

0.7051

4.7303

0.8462

Null

195

196

0.7953

0.8995

4.5906

1.0794

Null

196

197

0.4876

0.3045

3.9751

0.3654

3.9751

197

198

0.5590

0.7461

4.1179

0.8953

4.1179

198

199

0.1489

0.0499

3.2978

0.0599

3.2978

199

200

0.5615

0.6135

4.1230

0.7362

4.1230

200

Program A.2 (written in Mathematica) n = 654321; SeedRandom[n]; sig = 1/3;avg = 4; Table[{i = i + 1, u1[i] = RandomReal[], u2[i] = RandomReal[], U1[i]=3+(5-3)*u1[i], U2[i]=0+(1.2-0)*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((U1[i]-avg)/sig)^2],

144

Appendix: Handouts for Computational Lab and Automated …

g[i] = U1[i],Null],i}, {i,0,199,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]","i"}}] Program A.3 (written in Mathematica) F = 1-(x-4)^2; p1 = Plot[1-(x-4)^2,{x,3,5},PlotStyle-> {Black},Frame-> True, FrameLabel-> {"x","F, p"},PlotRange-> {0,1.2}]; sig = 1/3;avg = 4; p2 = Plot[(1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((x-avg)/sig)^2],{x,3,5}, PlotStyle-> {Dashed,Black},Frame-> True]; Show[p1,p2] n = 654321; SeedRandom[n]; Table[{i = i + 1, u1[i] = RandomReal[], u2[i] = RandomReal[], U1[i]=3+(5-3)*u1[i], U2[i]=0+(1.2-0)*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((U1[i]-avg)/sig)^2], g[i] = U1[i],Null],i},{i,0,199,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]", "i"}}] ga[1]=g[2];ga[2]=g[4];ga[3]=g[5];ga[4]=g[6];ga[5]=g[7];ga[6]=g[8]; ga[7] = g[9];ga[8] = g[16];ga[9] = g[17];ga[10] = g[18];ga[11] = g[21]; ga[12] = g[24];ga[13] = g[28];ga[14] = g[29];ga[15] = g[30];ga[16] = g[31]; ga[17] = g[32];ga[18] = g[34];ga[19] = g[35];ga[20] = g[36];ga[21] = g[39]; ga[22] = g[43];ga[23] = g[44];ga[24] = g[47];ga[25] = g[48];ga[26] = g[50]; ga[27] = g[53];ga[28] = g[54];ga[29] = g[57];ga[30] = g[58];ga[31] = g[60]; ga[32] = g[61];ga[33] = g[64];ga[34] = g[65];ga[35] = g[67];ga[36] = g[72]; ga[37] = g[78];ga[38] = g[80];ga[39] = g[81];ga[40] = g[82];ga[41] = g[84]; ga[42] = g[87];ga[43] = g[89];ga[44] = g[90];ga[45] = g[93];ga[46] = g[94]; ga[47] = g[95];ga[48] = g[99];ga[49] = g[100];ga[50] = g[105];ga[51] = g[106]; ga[52] = g[107];ga[53] = g[108];ga[54] = g[110];ga[55] = g[112];ga[56] = g[114]; ga[57] = g[117];ga[58] = g[122];ga[59] = g[127];ga[60] = g[128];ga[61] = g[130]; ga[62] = g[132];ga[63] = g[136];ga[64] = g[137];ga[65] = g[140];ga[66] = g[141]; ga[67] = g[142];ga[68] = g[145];ga[69] = g[147];ga[70] = g[148];ga[71] = g[152]; ga[72] = g[154];ga[73] = g[155];ga[74] = g[156];ga[75] = g[157];ga[76] = g[163]; ga[77] = g[165];ga[78] = g[166];ga[79] = g[171];ga[80] = g[174];ga[81] = g[175]; ga[82] = g[176];ga[83] = g[181];ga[84] = g[182];ga[85] = g[188];ga[86] = g[191];

Appendix: Handouts for Computational Lab and Automated …

145

ga[87] = g[194];ga[88] = g[197];ga[89] = g[198];ga[90] = g[199];ga[91] = g[200]; Table[{i = i + 1, ga[i], p[i] = (1/(sig*Sqrt[2*Pi]))*Exp[-0.5*((ga[i]-avg)/sig)^2], Y[i] = (1-(ga[i]-4)^2)/p[i]},{i,0,90,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","ga[i]","p[i]","Y[i]"}}] ListPlot[Table[{ga[i],1},{i,0,90,1}],Frame-> True, FrameLabel-> {"ga [i]","1"}] I1=Integral=Sum[Y[i]/(91),{i,1,91}] Integrate[1-(x-4)^2,{x,3,5}]; I2=N[%] PercentageError = (I1-I2)*100/I2 Program A.3 is the complete program to calculate the integral. In 200 attempts, we could get N = 91 accepted values ga of the Gaussian random variate g. Using program A.3 and resulting Table A.4, we now evaluate the integral. The 4.3% error is attributable to the facts that F(x) and p(x) used are not proportional to each other and that we have used only 91 values of the random variate. That random variate ga is distributed in the interval 3–5 as per Gaussian pdf is evidenced by Fig. A.5 which displays values of ga against stagnant integer 1. We have chosen n = 654321 by trial and error to get sets of values of u1 and u2 uniformly distributed in the interval 0–1. We used the command SeedRandom[n] to get u[i]’s reproducibly.

A.3

Automated Collection of Accepted Random Variates

Here we rewrite Program number 4.4 such that accepted random variates are collected by the program, rather than manually. Program number 4.4 rewritten (in Mathematica) n = 654321 SeedRandom[n] sig = 0.5;a = Pi/2; Table[{i = i + 1,u1[i] = RandomReal[],u2[i] = RandomReal[], U1[i]=0+Pi*u1[i],U2[i]=0+0.8*u2[i], If[U2[i] < (1/(sig*Sqrt[2*Pi]))* Exp[-0.5*((U1[i]-a)/sig)^2],g[i] = U1[i],Null]},{i,0,99,1}]; TableForm[%,TableSpacing-> {2,2}, TableHeadings-> {None,{"i","u1[i]","u2[i]","U1[i]","U2[i]","g[i]"}}] ga = {};

146

Appendix: Handouts for Computational Lab and Automated …

Table A.4 Showing tabulated values of ga[i], calculated values of p[i] =

2 √1 exp − 1 ga[i]−a and Y[i] = (1 − (ga[i] − 4)2 )/ p[i] from which we get σ 2 σ 2π

I = N1

N

F(xi ) 1  N F(ga[i]) = 1  N i=1 p(xi ) = N i=1 i=1 p[i] N

√1 σ 2π

2 1−(ga[i]−4) 1 N Y

2 = N i=1 i 1 ga[i]−a exp − 2 σ

  as an approximate value of the integral I = 35 F(x)d x = 35 [1 − (x − 4)2 ]d x = 1.39061 whereas exact value is 1.33333. Error is 4.3%. Using prog. A.3 i

ga[i]

p[i]

Y [i]

1

4.7192

0.1167

4.1359

2

3.8378

1.0632

0.9158

3

3.7598

0.9232

1.0207

4

3.3799

0.2121

2.9020

5

3.6669

0.7265

1.2238

6

3.7721

0.9474

1.0007

7

4.1763

1.0406

0.9311

8

3.7593

0.9221

1.0217

9

3.7172

0.8352

1.1016

10

4.1283

1.1114

0.8849









82

4.1320

1.1065

0.8880

83

3.9907

1.1964

0.8358

84

4.2507

0.9020

1.0390

85

3.9304

1.1710

0.8498

86

3.7950

0.9906

0.9671

87

3.6613

0.7141

1.2396

88

3.9751

1.1935

0.8374

89

4.1179

1.1242

0.8771

90

3.2978

0.1301

3.8959

91

4.1230

1.1180

0.8809

i = 0; While[i {2,2}, TableHeadings-> {None,{"i","ga[i]", "p[i]","y[i]"}}] Integral = Sum[y[i]/(n1),{i,1,n1}] ListPlot[Table[{ga[[i]],1},{i,0,n1-1,1}],Frame-> True, FrameLabel-> {"ga [i]","1"}]

References

1. A Primer for the Monte Carlo Method IIya M. Sobol’ CRC Press (1994) 2. The Monte Carlo Method I. M. Sobol’ The University of Chicago Press (1974) 3. The Monte Carlo Method: The Method of Statistical Trials Edited by Yu. A. Shreider, Pergamon Press (1966) 4. Monte Carlo Methods: A Hands-On Computational Introduction Utilizing Excel Sujaul Chowdhury Morgan & Claypool (2021) Springer

© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 S. Chowdhury, Monte Carlo Methods Utilizing Mathematica®, Synthesis Lectures on Mathematics & Statistics, https://doi.org/10.1007/978-3-031-23294-7

149