cis541 08 montecarlo - computer science and...

12
Monte-Carlo Techniques Roger Crawfis August 12, 2005 OSU/CIS 541 2 Monte-Carlo Integration Overview 1. Generating Psuedo-Random Numbers 2. Multidimensional Integration a) Handling complex boundaries. b) Handling complex integrands. August 12, 2005 OSU/CIS 541 3 Pseudo-Random Numbers Definition of random from Merriam-Webster: Main Entry: random Function: adjective Date: 1565 1 a : lacking a definite plan, purpose, or pattern b: made, done, or chosen at random <read random passages from the book> 2 a : relating to, having, or being elements or events with definite probability of occurrence <random processes> b: being or relating to a set or to an element of a set each of whose elements has equal probability of occurrence <a random sample>; also : characterized by procedures designed to obtain such sets or elements <random sampling> August 12, 2005 OSU/CIS 541 4 Random Computer Calculations? Compare this to the definition of an algorithm (dictionary.com): algorithm n : a precise rule (or set of rules) specifying how to solve some problem.

Upload: tranxuyen

Post on 28-Mar-2018

215 views

Category:

Documents


2 download

TRANSCRIPT

Mon

te-C

arlo

Tec

hniq

ues

Rog

er C

raw

fis

Aug

ust 1

2, 2

005

OSU

/CIS

541

2

Mon

te-C

arlo

Inte

grat

ion

•O

verv

iew

1.G

ener

atin

g Ps

uedo

-Ran

dom

Num

bers

2.M

ultid

imen

sion

al In

tegr

atio

na)

Han

dlin

g co

mpl

ex b

ound

arie

s.b)

Han

dlin

g co

mpl

ex in

tegr

ands

.

Aug

ust 1

2, 2

005

OSU

/CIS

541

3

Pseu

do-R

ando

m N

umbe

rs

•D

efin

ition

of r

ando

m fr

om M

erria

m-W

ebst

er:

•M

ain

Entry

: ran

dom

Func

tion:

adj

ectiv

eD

ate:

156

51

a:l

acki

ng a

def

inite

pla

n, p

urpo

se, o

r pat

tern

b:m

ade,

don

e, o

r ch

osen

at r

ando

m <

read

rand

ompa

ssag

es fr

om th

e bo

ok>

2 a

:rel

atin

g to

, hav

ing,

or b

eing

ele

men

ts o

r eve

nts w

ith d

efin

ite

prob

abili

ty o

f occ

urre

nce

<ran

dom

proc

esse

s> b

:bei

ng o

r rel

atin

g to

a

set o

r to

an e

lem

ent o

f a se

t eac

h of

who

se e

lem

ents

has

equ

alpr

obab

ility

of o

ccur

renc

e <a

rand

omsa

mpl

e>; a

lso

:cha

ract

eriz

ed b

y pr

oced

ures

des

igne

d to

obt

ain

such

sets

or e

lem

ents

<ra

ndom

sam

plin

g>

Aug

ust 1

2, 2

005

OSU

/CIS

541

4

Rand

om C

ompu

ter C

alcu

latio

ns?

•C

ompa

re th

is to

the

defin

ition

of a

n al

gorit

hm (d

ictio

nary

.com

):–

algo

rith

m•

n : a

pre

cise

rule

(or s

et o

f rul

es) s

peci

fyin

g ho

w to

so

lve

som

e pr

oble

m.

Aug

ust 1

2, 2

005

OSU

/CIS

541

5

Rand

om N

umbe

r

•W

hat i

s ran

dom

num

ber ?

Is 3

?–

Ther

e is

no

such

thin

g as

sing

le ra

ndom

num

ber

•R

ando

m n

umbe

r –

A se

t of n

umbe

rs th

at h

ave

noth

ing

to d

o w

ith th

e ot

her

num

bers

in th

e se

quen

ce

•In

a u

nifo

rm d

istri

butio

n of

rand

om n

umbe

rs in

th

e ra

nge

[0,1

] , e

very

num

ber h

as th

e sa

me

chan

ce o

f tur

ning

up.

–0.

0000

1 is

just

as l

ikel

y as

0.5

000

Aug

ust 1

2, 2

005

OSU

/CIS

541

6

Rand

om v

. Pse

udo-

rand

om

•R

ando

m n

umbe

rsha

ve n

o de

fined

sequ

ence

or

form

ulat

ion.

Thu

s, fo

r any

nra

ndom

num

bers

, ea

ch a

ppea

rs w

ith e

qual

pro

babi

lity.

•If

we

rest

rict o

urse

lves

to th

e se

t of 3

2-bi

t int

eger

s, th

en o

ur n

umbe

rs w

ill st

art t

o re

peat

afte

r som

e ve

ry la

rge

n. T

he n

umbe

rs th

us c

lum

p w

ithin

this

ra

nge

and

arou

nd th

ese

inte

gers

. •

Due

to th

is li

mita

tion,

com

pute

r alg

orith

ms a

re

rest

ricte

d to

gen

erat

ing

wha

t we

call

pseu

do-

rand

om n

umbe

rs.

Aug

ust 1

2, 2

005

OSU

/CIS

541

7

Mon

te-C

arlo

Met

hods

•19

53, N

icol

ausM

etro

polis

Mon

te C

arlo

met

hod

refe

rs to

any

met

hod

that

mak

es u

se o

f ran

dom

num

bers

–Si

mul

atio

n of

nat

ural

phe

nom

ena

–Si

mul

atio

n of

exp

erim

enta

l app

arat

us

–N

umer

ical

ana

lysi

s

Aug

ust 1

2, 2

005

OSU

/CIS

541

8

How

to g

ener

ate

rand

om

num

bers

?

•U

se so

me

chao

tic sy

stem

(B

alls

in a

bar

rel –

Lotto

) •

Use

a p

roce

ss th

at is

inhe

rent

ly ra

ndom

Rad

ioac

tive

deca

y–

Ther

mal

noi

se–

Cos

mic

ray

arriv

al

•Ta

bles

of a

few

mill

ion

rand

om n

umbe

rs

•H

ooki

ng u

p a

rand

om m

achi

ne to

a c

ompu

ter.

Aug

ust 1

2, 2

005

OSU

/CIS

541

9

Pseu

do R

ando

m n

umbe

r ge

nera

tors

•Th

e cl

oses

t ran

dom

num

ber g

ener

ator

that

can

be

obta

ined

by

com

pute

r alg

orith

m.

•U

sual

ly a

uni

form

dis

tribu

tion

in th

e ra

nge

[0,1

] •

Mos

t pse

udo

rand

om n

umbe

r gen

erat

ors h

ave

two

thin

gs in

com

mon

–Th

e us

e of

larg

e pr

ime

num

bers

The

use

of m

odul

o ar

ithm

etic

•A

lgor

ithm

gen

erat

es in

tege

rs b

etw

een

0 an

d M

, map

to

zer

o an

d on

e.

MI

Xn

n/

=A

ugus

t 12,

200

5O

SU/C

IS 5

4110

An e

arly

exa

mpl

e (J

ohn

Von

Neu

man

n,19

46)

•To

gen

erat

e 10

dig

its o

f int

eger

–St

art w

ith o

ne o

f 10

digi

ts in

tege

rs

–Sq

uare

it a

nd ta

ke m

iddl

e 10

dig

its fr

om a

nsw

er–

Exam

ple:

5

7721

5664

92=

3331

7792

3805

9490

9291

•Th

e se

quen

ce a

ppea

rs to

be

rand

om, b

ut e

ach

num

ber i

s det

erm

ined

from

the

prev

ious

no

t ran

dom

.•

Serio

us p

robl

em :

Smal

l num

bers

(0 o

r 1) a

re lu

mpe

d to

geth

er, i

tcan

ge

t its

elf t

o a

shor

t loo

p. F

or e

xam

ple:

•61

002

= 37

2100

00•

2100

2=

0441

0000

•41

002

= 16

8100

00•

5100

2=

6561

0000

Aug

ust 1

2, 2

005

OSU

/CIS

541

11

Line

ar C

ongr

uent

ialM

etho

d

•Le

hmer

, 194

8•

Mos

t typ

ical

so-c

alle

dra

ndom

num

ber g

ener

ator

•A

lgor

ithm

:–

a,c

>=

0 , m

> I 0

,a,c

Adva

ntag

e :

–Ve

ry fa

st•

Prob

lem

: –

Poor

cho

ice

of th

e co

nsta

nts c

an le

ad to

ver

y po

or se

quen

ce–

The

rela

tions

hip

will

repe

at w

ith a

per

iod

no g

reat

er th

an m

(a

roun

d m

/4)

•C

com

plie

r RAN

D_M

AX :

m =

327

67

)m

od(

)(

1m

caI

In

n+

=+

Aug

ust 1

2, 2

005

OSU

/CIS

541

12

RAN

DU

Gen

erat

or

•19

60’s

IB

M•

Alg

orith

m

•Th

is g

ener

ator

was

late

r fou

nd to

hav

e a

serio

us p

robl

em

)2

mod

()

6553

9(

311

nn

II

×=

+

Aug

ust 1

2, 2

005

OSU

/CIS

541

13

1D a

nd 2

D D

istr

ibut

ion

of

RAN

DU

Aug

ust 1

2, 2

005

OSU

/CIS

541

14

Rand

om N

umbe

r Alg

orith

ms

•M

ost a

lgor

ithm

s ope

rate

by

calc

ulat

ing

som

e nu

mbe

r and

th

en ta

king

the

low

er-o

rder

bits

. For

exa

mpl

e: th

e cl

ass o

f m

ultip

licat

ive

cong

ruen

tialr

ando

m-n

umbe

r gen

erat

ors h

as

the

form

: . T

he c

hoic

e of

the

coef

ficie

nts i

s crit

ical

. Ex

ampl

e in

boo

k:

()

()

()

()

31

531

1

05

11

1031

102

2

1531

33

21

7m

od2

1

1 70.

0000

0782

6369

2594

2561

0890

3445

3541

5221

3e-6

7m

od2

17

0.13

1537

7881

4316

6242

2340

2060

6723

62

7m

od2

116

2265

0073

0.75

5605

3221

9503

3227

1843

3720

3942

4

nn n

n

lx l

l

l lx

lx

lx

+

=−

=−

= =⇒

=

=−

=⇒

=

=−

=⇒

=

()

()

531

44

5

7*1

6226

5007

3m

od2

198

4943

658

0.45

8650

1319

2344

9287

1553

8665

9854

74

0.53

2767

2374

1216

9220

5835

9217

8571

78

lx

x

=−

=⇒

=

=

Aug

ust 1

2, 2

005

OSU

/CIS

541

15

Use

of P

rim

e N

umbe

rs

•Th

e nu

mbe

r 231

–1

is a

prim

e nu

mbe

r, so

the

rem

aind

er w

hen

a nu

mbe

r is d

ivid

ed b

y a

prim

e is

ra

ther

, wel

l ran

dom

.•

Not

es o

n th

e pr

evio

us a

lgor

ithm

:–

The

l’s c

an re

ach

a m

axim

um v

alue

of t

he p

rime

num

ber.

–D

ivid

ing

by th

is n

umbe

r map

s the

inte

gers

into

real

sin

with

in th

e op

en in

terv

al (0

,1.0

).•

Why

ope

n in

terv

al?

–l 0

is c

alle

d th

e se

edof

the

rand

om p

roce

ss. W

e ca

n us

e an

ythi

ng h

ere.

Aug

ust 1

2, 2

005

OSU

/CIS

541

16

Oth

er A

lgor

ithm

s

•M

ultip

ly b

y a

larg

e pr

ime

and

take

the

low

er-o

rder

bits

. •

Her

e, w

e us

e hi

gher

-bi

t int

eger

s to

gene

rate

48

-bit

rand

om

num

bers

.•

Dra

nd48

()

()

481

0

1

2

3

4 5

2736

7316

3155

813

8m

od2 1

2736

7316

3169

621

6915

2289

5421

844

6648

5884

4294

1232

7642

4030

766

1624

1526

4731

678

2996

1701

4593

9051

8927

4149

3630

2517

1568

5692

926

3710

8576

9044

4616

3500

6476

2854

2

nn

xx

xx

x x x x

+=

+

==

= = = =

Aug

ust 1

2, 2

005

OSU

/CIS

541

17

Oth

er A

lgor

ithm

s

•M

any

mor

e su

ch a

lgor

ithm

s.

•So

me

do n

ot u

se in

tege

rs. I

nteg

ers w

ere

just

m

ore

effic

ient

on

old

com

pute

rs.

()5

1m

od1

nn

xx

π+=

+

()

18

3

2

nn

nn

q

ut

uu

x+=

=

t is a

ny la

rge

num

ber

Wha

t is t

his o

pera

tion?

Aug

ust 1

2, 2

005

OSU

/CIS

541

18

Oth

er A

lgor

ithm

s

•O

ne w

ay to

impr

ove

the

beha

vior

of

rand

om n

umbe

r gen

erat

or

)m

od(

)(

21

mI

bI

aI

nn

n−

−×

=H

as tw

o in

itial

see

d an

d ca

n ha

ve a

per

iod

grea

ter

than

m

Aug

ust 1

2, 2

005

OSU

/CIS

541

19

The

RAN

MAR

gen

erat

or

•A

vaila

ble

in th

e C

ERN

Lib

rary

Req

uire

s 103

initi

al se

ed–

Perio

d : a

bout

1043

–Th

is se

ems t

o be

the

ultim

ate

rand

om n

umbe

r ge

nera

tor

Aug

ust 1

2, 2

005

OSU

/CIS

541

20

Prop

ertie

s of P

seud

o-Ra

ndom

N

umbe

rs

•Th

ree

key

prop

ertie

s tha

t you

shou

ld

rem

embe

r:1.

Thes

e al

gorit

hms g

ener

ate

perio

dic

sequ

ence

s (h

ence

not

rand

om).

To se

e th

is, c

onsi

der

wha

t hap

pens

whe

n a

rand

om n

umbe

r is

gene

rate

d th

at m

atch

es o

ur in

itial

seed

.

Aug

ust 1

2, 2

005

OSU

/CIS

541

21

Prop

ertie

s of P

seud

o-Ra

ndom

N

umbe

rs

2.Th

e re

stric

tion

to q

uant

ized

num

bers

(a fi

nite

-se

t), le

ads t

o pr

oble

ms i

n hi

gh-d

imen

sion

al

spac

e. M

any

poin

ts e

nd u

p to

be

co-p

lana

r. Fo

r ten

-dim

ensi

ons,

and

32-b

it ra

ndom

nu

mbe

rs, t

his l

eads

to o

nly

126

hype

r-pl

anes

in

10-

dim

ensi

onal

spac

e.

Aug

ust 1

2, 2

005

OSU

/CIS

541

22

3D D

istr

ibut

ion

from

RAN

DU

Prob

lem

s se

en w

hen

obse

rved

at t

he ri

ght

angl

e

Aug

ust 1

2, 2

005

OSU

/CIS

541

23

The

Mar

sagl

iaef

fect

•19

68, M

arsa

glia

•R

ando

nnu

mbe

rs fa

ll m

ainl

y in

the

plan

es

•Th

e re

plac

emen

t of t

he m

ultip

lier f

rom

65

539

to 6

9069

impr

oves

per

form

ance

si

gnifi

cant

ly

Aug

ust 1

2, 2

005

OSU

/CIS

541

24

Prop

ertie

s of P

seud

o-Ra

ndom

N

umbe

rs

3.Th

e in

divi

dual

dig

its in

the

rand

om n

umbe

r m

ay n

ot b

e in

depe

nden

t. Th

ere

may

be

a hi

gher

pro

babi

lity

that

a 3

will

follo

w a

5.

Aug

ust 1

2, 2

005

OSU

/CIS

541

25

Avai

labl

e fu

nctio

ns

•St

anda

rd C

Lib

rary

–Ty

pe in

“m

an ra

nd”

on y

our C

IS U

nix

envi

ronm

ent.

•R

athe

r poo

r pse

udo-

rand

om n

umbe

r gen

erat

or.

•O

nly

resu

lts in

16-

bit i

nteg

ers.

•H

as a

per

iodi

city

of 2

**31

thou

gh.

–Ty

pe in

“m

an ra

ndom

” on

you

r CIS

Uni

x en

viro

nmen

t.•

Slig

htly

bet

ter p

seud

o-ra

ndom

num

ber g

ener

ator

.•

Res

ults

in 3

2-bi

t int

eger

s.•

Use

d ra

nd()

to b

uild

an

initi

al ta

ble.

•H

as a

per

iodi

city

of a

roun

d 2*

*69.

–#i

nclu

de <

stdl

ib.h

>

Aug

ust 1

2, 2

005

OSU

/CIS

541

26

Avai

labl

e fu

nctio

ns

•D

rand

48()

–re

turn

s a p

seud

o-ra

ndom

nu

mbe

r in

the

rang

e fr

om z

ero

to o

ne, u

sing

do

uble

pre

cisi

on.

–Pr

etty

goo

d ro

utin

e.–

May

not

be

as p

orta

ble.

Aug

ust 1

2, 2

005

OSU

/CIS

541

27

Initi

aliz

ing

with

See

ds

•M

ost o

f the

alg

orith

ms h

ave

som

e so

rt st

ate

that

can

be

initi

aliz

ed. M

any

times

this

is

the

last

gen

erat

ed n

umbe

r (no

t thr

ead

safe

).•

You

can

set t

his s

tate

usi

ng th

e ro

utin

es

initi

aliz

atio

n m

etho

ds (s

rand

, sra

ndom

or

sran

d48)

.–

Why

wou

ld y

ou w

ant t

o do

this

?

Aug

ust 1

2, 2

005

OSU

/CIS

541

28

Initi

aliz

ing

with

See

ds

•Tw

o re

ason

s to

initi

aliz

e th

e se

ed:

1.Th

e de

faul

t sta

te a

lway

s gen

erat

es th

e sa

me

sequ

ence

of r

ando

m n

umbe

rs. N

ot re

ally

ra

ndom

at a

ll, p

artic

ular

ly fo

r a sm

all s

et o

f ca

lls. S

olut

ion:

Cal

l the

seed

met

hod

with

the

low

er-o

rder

bits

of t

he sy

stem

clo

ck.

2.Y

ou n

eed

a de

term

inis

tic p

roce

ss th

at is

re

peat

able

.

Aug

ust 1

2, 2

005

OSU

/CIS

541

29

Initi

aliz

ing

with

See

ds

•W

e do

not

wan

t the

mou

ntai

n to

cha

nge

as

the

cam

era

mov

es.

Aug

ust 1

2, 2

005

OSU

/CIS

541

30

Map

ping

rand

om n

umbe

rs

•M

ost c

ompu

ter l

ibra

ry su

ppor

t for

rand

om

num

bers

onl

y pr

ovid

es ra

ndom

num

bers

ov

er a

fixe

d ra

nge.

•Y

ou n

eed

to m

ap th

is to

you

r des

ired

rang

e.•

Two

com

mon

cas

es:

–R

ando

m in

tege

rs fr

om z

ero

to so

me

max

imum

.–

Ran

dom

floa

ting-

poin

t or d

oubl

e-pr

ecis

ion

num

bers

map

ped

to th

e ra

nge

zero

to o

ne.

Aug

ust 1

2, 2

005

OSU

/CIS

541

31

Non

-rec

tang

ular

Are

as

•In

2D

, we

may

wan

t poi

ntsr

ando

mly

di

strib

uted

ove

r som

e re

gion

.–

Squa

re –

inde

pend

ently

det

erm

ine

xan

d y.

–R

ecta

ngle

-??

?–

Circ

le -

???

•W

rong

way

–in

depe

nden

tly d

eter

min

e r a

ndθ.

Aug

ust 1

2, 2

005

OSU

/CIS

541

32

Mon

te-C

arlo

Tec

hniq

ues

•Pr

oble

m: W

hat i

s the

pro

babi

lity

that

10

dice

thro

ws a

dd

up e

xact

ly to

32?

•E

xact

Way

. Cal

cula

te th

is e

xact

ly b

y co

untin

g al

l pos

sibl

e w

ays o

f mak

ing

32 fr

om 1

0 di

ce.

•A

ppro

xim

ate

(Laz

y) W

ay. S

imul

ate

thro

win

g th

e di

ce

(say

500

tim

es),

coun

t the

num

ber o

f tim

es th

e re

sults

add

up

to 3

2, a

nd d

ivid

e th

is b

y 50

0.

•L

azy

Way

can

get

qui

te c

lose

to th

e co

rrec

t ans

wer

qu

ite q

uick

ly.

Aug

ust 1

2, 2

005

OSU

/CIS

541

33

Mon

te-C

arlo

Tec

hniq

ues

•Sa

mpl

e A

pplic

atio

ns–

Inte

grat

ion

–Sy

stem

sim

ulat

ion

–C

ompu

ter g

raph

ics -

Ren

derin

g.–

Phys

ical

phe

nom

ena

-rad

iatio

n tra

nspo

rt –

Sim

ulat

ion

of B

ingo

gam

e–

Com

mun

icat

ions

-bi

t err

or ra

tes

–V

LSI d

esig

ns -

tole

ranc

e an

alys

is

Aug

ust 1

2, 2

005

OSU

/CIS

541

34

∫b adx

xp

)(

P(x)

ab

x

P(x)

ab

Sim

ple

Exam

ple:

.

•M

etho

d 1:

Ana

lytic

al In

tegr

atio

n•

Met

hod

2: Q

uadr

atur

e•

Met

hod

3: M

C --

rand

om sa

mpl

ing

the

area

enc

lose

d by

a<x

<b a

nd

0<y<

max

(p(x

))

⎟⎟ ⎠⎞⎜⎜ ⎝⎛

+−

≈∫

##

#)

))(

(m

ax(

)(

ab

xp

dxx

pb a

Aug

ust 1

2, 2

005

OSU

/CIS

541

35

Sim

ple

Exam

ple:

.

•In

tuiti

vely

:

⎟⎟ ⎠⎞⎜⎜ ⎝⎛

+−

≈∫

##

#)

))(

(m

ax(

)(

ab

xp

dxx

pb a

()

Prob

abili

tyBo

xAr

eay

fx

⇒•

∫b adx

xp

)(

Aug

ust 1

2, 2

005

OSU

/CIS

541

36

Shap

e of

Hig

h D

imen

sion

al

Regi

on

•H

ighe

r dim

ensi

onal

shap

es c

an b

e co

mpl

ex.

•H

ow to

con

stru

ct w

eigh

ted

poin

ts in

a g

rid

that

cov

ers t

he re

gion

R ?

Prob

lem

: m

ean-

squa

re d

ista

nce

from

the

orig

in

∫∫∫∫

+>=

<dx

dy

dxdy

yx

r)

(2

22

Aug

ust 1

2, 2

005

OSU

/CIS

541

37

Inte

grat

ion

over

sim

ple

shap

e ?

0.5

0.5

22

20.

50.

50.

50.

5

0.5

0.5(

)(

,)

(,

)

dxdy

xy

sx

yr

dxdy

sx

y

++

−−

++

−−

+=∫

∫∫

1

insi

de R

0 o

utsi

de R

s=⎧ ⎨ ⎩

Grid

mus

t be

fine

enou

gh !

Aug

ust 1

2, 2

005

OSU

/CIS

541

38

•In

tegr

ate

a fu

nctio

n ov

er a

co

mpl

icat

ed d

omai

n–

D: c

ompl

icat

ed d

omai

n.–

D’:

Sim

ple

dom

ain,

supe

rset

of D

.•

Pick

ran

dom

poi

nts o

ver

D’:

•C

ount

ing:

N: p

oint

s ove

r D

•N

’: po

ints

ove

r D

D

D’: rectangular

D

D’: circle

Mon

te-C

arlo

Inte

grat

ion

D D

Volu

me

NVo

lum

eN

≈′

Aug

ust 1

2, 2

005

OSU

/CIS

541

39

•T

he p

roba

bilit

y of

a r

ando

m p

oint

ly

ing

insi

de th

e un

it ci

rcle

:

•If

pic

k a

rand

om p

oint

Ntim

es a

nd

Mof

thos

e tim

es th

e po

int l

ies i

nsid

e th

e un

it ci

rcle

:

•If

Nbe

com

es v

ery

larg

e,

P=

P0→

N

M

(x,y)

Estim

atin

g π

usin

g M

onte

Car

lo

Aug

ust 1

2, 2

005

OSU

/CIS

541

40

Estim

atin

g π

usin

g M

onte

Car

lo

•R

esul

ts:

–N

=

10,0

00Pi

= 3.

1388

–N

=

100,

000

Pi=

3.14

52–

N =

1,

000,

000

Pi=

3.14

164

–N

= 1

0,00

0,00

0Pi

= 3.

1422

784

–…

Aug

ust 1

2, 2

005

OSU

/CIS

541

41

Estim

atin

g π

usin

g M

onte

Car

lo

doub

le x

, y, p

i;co

nst i

ntm

_nM

axSa

mpl

es=

1000

0000

0;in

tcou

nt=0

;fo

r (in

tk=0

; k<m

_nM

axSa

mpl

es; k

++)

x=2.

0*dr

and4

8() –

1.0;

// M

ap to

the

rang

e [-1

,1]

y=2.

0*dr

and4

8() –

1.0;

if (x

*x+y

*y<=

1.0)

cou

nt++

; pi

=4.0

* (d

oubl

e) s

/ (do

uble

)m

_nM

axSa

mpl

es;

Aug

ust 1

2, 2

005

OSU

/CIS

541

42

Stan

dard

Qua

drat

ure

•W

e ca

n fin

d nu

mer

ical

val

ue o

f a d

efin

ite

inte

gral

by

the

defin

ition

:

whe

re p

oint

s xia

re u

nifo

rmly

spac

ed.

1f(

)lim

f()

bN

ix

ia

xdx

xx

∆→∞

=

=∆

∑∫

Aug

ust 1

2, 2

005

OSU

/CIS

541

43

Erro

r in

Qua

drat

ure

•C

onsi

der i

nteg

ral i

n d

dim

ensi

ons:

•Th

e er

ror w

ith N

sam

plin

g po

ints

is

12

f()

f()

di

dX

dxdx

dxX

≈∆

∑∫

L

1/f(

)f(

)|

|d

dX

dXX

xN

−−

∆∝

∑∫

Aug

ust 1

2, 2

005

OSU

/CIS

541

44

Mon

te C

arlo

Err

or

•Fr

om p

roba

bilit

y th

eory

one

can

show

that

th

e M

onte

Car

lo e

rror

dec

reas

es w

ith

sam

ple

size

Nas

inde

pend

ent o

f dim

ensi

on d

.

1 Nε∝

Aug

ust 1

2, 2

005

OSU

/CIS

541

45

Gen

eral

Mon

te C

arlo

•If

the

sam

ples

are

not

dra

wn

unifo

rmly

but

w

ith so

me

prob

abili

ty d

istri

butio

n P(

X), w

e ca

n co

mpu

te b

y M

onte

Car

lo:

1

1f(

)P(

)df(

)N

ii

XX

XX

N=

=∑

Whe

re P

(X) i

s no

rmal

ized

, P(

)d1

XX

=∫