Monte Carlo Methods: Direct Sampling and Integration, Slides of Stochastic Processes

The monte carlo method for finding integrals through direct sampling. It includes examples with matlab code and visualizations of the results. The method involves generating random numbers and using their cumulative distribution to approximate the integral.

Typology: Slides

2011/2012

Uploaded on 08/12/2012

ranganath
ranganath 🇮🇳

4.7

(3)

37 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2
Lecture Two: Direct Sampling -
II
Monte Carlo Methods
Monte Carlo Methods
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Monte Carlo Methods: Direct Sampling and Integration and more Slides Stochastic Processes in PDF only on Docsity!

Lecture Two: Direct Sampling -

II

Monte Carlo Methods^ Monte Carlo Methods

docsity.com

Direct Sampling : Part –II

b a

dx

x

g

x

f

I^

Suppose that

we have to find following integral:

Then let us first sample x using f(x) (say by direct method).

First we find its

normalization constant:

∞^ ∫

0

(^
A

dx x f

So, normalized pdf is given as

x a

dx
x
Af
x
F

Then the Cumulative distribution is given as

(^

x

Af

x

h

docsity.com

Example 1: As an illustration of this we will try first to find the following integral:

1 0 4

1 0

3

x

dx x

I

1 0

2 dx

x

x

I

2

1

2

) (

1 0 2

1 0

1 0

=

=

=^

∫^

A

x A

dx x A

dx x Af

Let us consider f(x) = x and g(x) = x

The exact answer is

Let us first find the normalization constant for f(x): Then comulative pdf is

ξ^
=^

∫^

x

x

dx x

xdx

x

x

x

0 2

0

0

and the find the area as

. /) (

1 2

N x g

I^

i^

i

∑ ≈

So, we use this equationto generate x

docsity.com

MATLAB

Program for the

example

% Program to find integral (f(x)g(x) type) % by Monte Carlo method % as a function of N N = 1000 ; rand('state', 0) % initialize area(1)= 0.25; xn(1)= N; exact_area(1)= 0.25; er(1) =0.0;^ for i = 2:

i sum = 0; for k = 1: N

exi = rand; x(k) = sqrt(exi);

end^ for kk = 1: N

sum = sum + x(kk)*x(kk);

end^ area(i) = sum/(2.*N); xn(i)= N;^ N = N + 1000;^ exact_area(i) = 0.25;^ error(i) = (area(i) - exact_area(i)); end plot(xn, area, xn, exact_area)

Example 1:

docsity.com

Let us try to find the following integral:

π

π

=

=

∫^0

sin

xdx

x

I

1 0

)

(sin

dx x

x

I^2

0 2

0

0

/ 2 1 2 ) ( π

π

π

π

=

=

=^

∫^

A

x A

dx x A

dx x Af

Let us consider f(x) = x and g(x) = sinx.

The exact answer is

Let us first find the normalization constant for f(x): Then comulative pdf is

ξ π

ξ

=

=

=

=^

x

x A

dx x A

Axdx

x

x

x

0 2

0

0

2

So, we use the above equation to generate x and the find the area as

. /)

(

(^22)

N

x g

I^

i^

i

π

Example 2:

docsity.com

The MATLAB program

for Example 2

% Program to find integral (f(x)g(x) type) % by Monte Carlo method % as a function of number of histories N = 1000 ; rand('state', 0) % initialize area(1)= pi; xn(1)= N; exact_area(1)= pi; er(1) =0.0;^ for i = 2:

i sum = 0; for k = 1: N

exi = rand; x(k) = pi*sqrt(exi);

end

for kk = 1: N

sum = sum + sin(x(kk)); end area(i) = pipisum/(2.*N); xn(i)= N; N = N + 1000; exact_area(i) = pi; error(i) = (area(i) - exact_area(i));

Example 2:^ end^ plot(xn, area, xn, exact_area)

docsity.com

RESULTS

Example 2:^ Figure above shows the area versus the number of histories (N). Increasingthe N we see a decrease in the error. The error bars below are indicating thateffect.

Error in Area

Number of Trails in a sample

x 1000

Error in Area from MC method Zero Error line

docsity.com