0% found this document useful (0 votes)
33 views10 pages

Term Paper On Monte Carlo Method

Uploaded by

shreyansh.shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

Term Paper On Monte Carlo Method

Uploaded by

shreyansh.shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Monte Carlo Techniques and Random Numbers

Aditya Tripathi
November 25, 2020

1 Introduction:
In this paper random numbers and Monte Carlo techniques are implemented to solve
various aplication based problems. Stainslaw Ulam and John Von Neumann can be re-
garded as developer of this method. They developed this method while working at the
Los Alamos project.Being secret, the work of von Neumann and Ulam required a code
name. A colleague of von Neumann and Ulam, Nicholas Metropolis, suggested using the
name Monte Carlo, which refers to the Monte Carlo Casino in Monaco where Ulam’s
uncle would borrow money from relatives to gamble1 . In this term paper I will first talk
about how to derive some fundamental constants of nature stochastically (using MC).
Then show an algorithm to derive euler’s constant. After that we will see how we can
do integration using MC technique. Then use it to solve ODE’s. After solving ODE’s we
will look into solving dynamical system using MC.

2 Constant Conundrum:
In class we have seen that we can get the value of pi using pseudo random numbers. We
were able to do this because there is a certain geometry associated with pi. The question
becomes more intriguing when we want to find the value of some other fundamental con-
stant. Which does not has an explixit geometry associated with it. Then we look for some
explicit geometry associated with it for example the golden ratio. Suppose we are not able
to find any kind of geometry, then we use stochastic definitions of that constant that are
derived from numerical analysis. Here for example we take euler’s constant as an example.

ˆ We iteratively start generating random variables xi ∈ (0, 1) and stop when the sum
of all those variables becomes greater than 1.
ˆ We store the count value of the number of variables generated as n.
ˆ For example: x1 = 0.5, x2 = 0.6 therefore n=2
ˆ We repeat this experiment N times, the expected value(average value) of n is e2 .
ˆ
N
X ni
lim =e
N →∞
i=1
N

1
Next we will implement this algorithm in a code using the inbuilt random number
generation function, Below is the code,

In the code the value of N is set to be 10000, when we choose N=100, we get a variance
of ≈ 5 × 10−2 , which gives us a standard deviation of 0.2.

2
Below is a plot of Value of e vs N. f(x)=2.718281828 (value of e uptill 9 decimal places).

Below is the plot uptill 10000 points.

Here the standard deviation turns out to be ≈ 0.03 (assuming the mean of all observa-
tion to be e uptill 9 decimal places), therefore its clear both from the plot as well as the
standard deviation, that more the number of iterations, more closer we get to e.

Remarks: Although its very pleasing to see e popping up from randomness, this stochas-
tic method is not a very efficient one, my computer crashes if I set N=a million. Also
even after 10000 iterations the standard deviation is still pretty high. Using fortran we
can have other methods and techniques to calculate these fudamental constants, more
efficiently. Its better to think of calculating these constants as an excercise for peo-
ple(undergrads) new to MC simulations, rather than actually using MC again and again
to calculate them.

3
3 Solving Differential Equations:
MC simulation are great for modelling and simulating real life phenomenon. I mean they
were used in various projects during Los Alamos testing of the atomic bomb. So if they
can be trusted to make an atomic bomb than they can be trusted. It happens so that
various real life situations are governed by differential equations. For example you want
to know at what time a person will reach there house then if based on existing data I can
get there average speed then one can tell the time also. This is a very crude example.
The speed of the ride can be described as a differential equation and on solving it we get
the answer. Ususally if the differential equations is solvable then we can easily get the
answer by manually integrating it. The end equation will depend upon the initial values
(boundary conditions). Differential equations are more diverse than this, for example in
fluid dynamics we can describe the motion using the Navier Stoke’s equation but we can
still lack a formal solution, which gives us an output for an input. We can have non-
linear dynamical differential equations which are extremely senstitive to initail value,
these types of equations help us simulate and predict weather.
As stated earlier if these equations are easily solvanble, then the just boil down to an
integral. So whenever we can do that, we do that. Then we can use MC to solve that
integral.

3.1 Integration using MC:3


We have seen how we can calculate π using MC. We did that by taking the ratio of random
points in a quarter circle and a square. We can use the same technique to calculate Area
under a curve by using a known area of a rectangle and ratio of the random points. Thus
we can integrate a function on a particular interval.
Theory: Now there are 2 ways to actually calculate the area under curve.
Way 1: We make a rectangle bounding the curve. Then throw random stones(random
points generated by fortran) inside this rectangle. Then we find the ratio of points inside
the curve to the points all over the rectangle. Multiplying this ratio with the area of
ractangle will give us the area under curve. Let S be the points inside and N be the total
number of points. Let the function in interval [a,b], be bounded by y=U, y=L
b
S × (U − L)(b − a)
Z
f (x)dx =
a N

Now we implement this program on f (x) = sin(x), In place of that we can have any other
function, the next step while solving differential equations will be to plot the integral,
which can be trivially done by calculating integral at various points forming a data set
and then plotting it using gnuplot.

4
The U and L are set to be -2 and 2 respectively, then xr and yr produce random num-
ber ∈ [0, 1] then we scale them so that they lie in our rectangle. After finding the
area under the curve we subtract the area of the rectangle below x-axis, so that the
area below x axis of the curve becomes negative. The common region becomes zero.

5
3.2 Predator-Prey Model:4
Now we look into a real life example of differential equations. Suppose we have a certain
population of wolves and sheeps(we have a prey and a predator). In the jungle lets say at
start the population of sheeps is more. What will happen is that wolves will eat sheep and
will increase in population, simultaneously decreasing the population of sheeps. When
the population of sheeps is low then the resources of food available to wolves will be
less thus causing there population to decrease. We can see that we have two variables
whose rate of changes are dependent on each other. We can see that we will have a set
of differential equations. Before getting into that we will first solve a simple differential
dy
equation of dx = 2x (which has the solution y = x2 + C), to understand the algorithm
better. The code is named ”XsquarebyMC.f95”, The algorithm of the code is:

ˆ First we take the differential equation of the form dy/dx=f(x,y) as input.

ˆ Then we find the upper limit and lower limit of dy/dx

ˆ Then we sprinkle 2 sets of N random numbers between 0 to U and 0 to L(L is lower


bound and is negative).

ˆ
dy
Y =Y + ∆X
dx
ˆ Using the above equation iteratively will give us the value of Y required at a point.

ˆ The problem is to get dy/dx accurately

ˆ MC comes into picture, Let count be the random numbers(those which were sprin-
kled earlier) less then JF(judgement factor: dy/dx at a particular point), now dy/dx
at any point is given by U*(S/N).

ˆ
Yn+1 = Yn + U ∗ (S/N )dx

ˆ for value less than zero of JF we take L in place of U

Below is the plot of simple deq (dy/dx = 2x), (xo, yo) = (0, 0)), f (x) = x2 and N=100

6
Now using the same algorithm we solve the differential equations, for the predator prey
model, which are given by,
dx
= αx − βxy
dt
dy
= δxy − γy
dt
These are known as Lotka volterra equations, x is the population of prey(example rabbit)
and y is the population of predator (foxes). More is the population of y lesser the growth
of rabbits. More is the population of rabbit more is the growth of foxes. The code works
on the same algorithm as before (attached code ”PreyPredatorCode.f95”). We gave the
input of initial conditions and constants.

Green one is predator and purple one is prey.


Using MC simulation we can see that as the prey population falls the predator popula-
tion increases. But it reaches a point where they have no food left. Then again their
population starts to fall again. Lets look at some more plots with same constants but
different initial value(purple is prey, green is predator).

One has to scale these populations(Y axis) to the actual population (I mean there cannot
be 0.5 rabbits in their niche).

3.3 Into Chaos:5


Now we have a hand on tool to solve differential equations. Using the random numbers can
we extend this idea to a point where the solution of the equations is random in itself?.
We did an example of 2 coupled equations earlier. What if we have three variables

7
and corresponding them 3 differential equations. Such types of system are termed as
dynamical systems. They are extremely sensitive to initial value. And for the past three
pages we have dealt with initial value problems. A slight change in initial conditions will
result in extremely different values at the end. This is somewhat similar to the case of
three body problem. Lets look into 3 differential equations for Lorentz Attractor;
dx
= σ(y − x)
dt
dy
= ρx − zx − y
dt
dz
= xy − βz
dt
We can implement these functions as code,

Here is the plot x,y and z with given initial conditions.

This was one example of a Lorentz Attractor, these types of machines are used to predict
the weather models, which are extremely sensitive to initial conditions, therefore we need
a robust way to predict them. This leads to what is known as the ”Butterfly Effect”

8
which states that a Butterfly flapping wings can cause a tornado. This was coined by
Edward Lorenz who was a meteorologist and a mathematician. While he was modelling
and simulating weather systems on computer he found that a slight change to initial
conditions resulted in very big changes in the final value.

4 Remarks:
So we just saw how useful MC can be. We solved to dependent differential equations
without actually finding their integral. Now the question is why use this when we can
just itereatively multiply the derivative with the step size and it to Y? Or more specif-
ically what are the pros and cons of this method over more basic techniques of solving
differential equations, like Runge Kutta methods and euler back/forward sub?
So this method is almost as accurate as any other method(as one can see from the initial
X square example, we only took 100 random points). One advantage of this method is
to increase the accuracy one needs to increase only the random points between U and L,
while in the case of normal substitution we would have to decrease the dx value also.

The MC method never fails to predict a solution (what possibly can stop the progam
from creating random numbers?).

The problem while using this method is that it is really expensive complexity-wise().
When we run the program not only our algorithm runs but also the inbuilt random num-
ber generation algorithm runs. Therefore its not such a good idea to use this method
where more memory is required and outputs are needed in less time.

At last we conclude that the MC simulations are a really powerful tool to model and
simulate various physical situations (people have modelled Rogue Star Earth capture
theories using MC6 ). We saw how totally non stochastic quantities like e can be calcu-
lated using MC. Then we saw how we can solve integral and differential equations using
MC.

9
5 References:
1. http://library.lanl.gov/cgi-bin/getfile?15-12.pdf
2. The Theory of Probability-Boris Vladimirovich Gnedenko
3. Bevington Numerical Analysis
4. Solving Initial value Ordinary Differential Equations by MC Method, Akhtar et al,
Proceeding of IAM
5. https://en.wikipedia.org/wiki/Chaos theory
6.M onte Carlo Simulations of Stellar P lanet Capture- Alan J. Scott

10

You might also like