Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Monte Carlo Simulation For Traders
#2

Calculate PI with Monte Carlo Simulation
Let's do a simple Monte Carlo experiment. We know PI is an irrational number. PI was first discovered when mathematicians tried to measure the circumference of a circle and then calculate its area. In this experiment we measure PI by throwing a needle on a board with parallel lines on it. When the needle lands on a line, we call it a hit. When it lands on space without any line we call is a miss. We calculate the probability of hit. We don't have to do it physically. We will simulated the whole experiment on a computer. Below is the MATLAB code that does the Monte Carlo computer simulation for us:

% we will throw the needle 10,000 times.
>> trials=10000;
% we generate 10000 random variables for the position
>> x=rand(1,trials);
% we also generate 10000 random variable for the angle
>> theta=rand(1,trials);
>> theta=0.5*pi*theta;
>> hits=x<=0.5*sin(theta);
>> sum(hits);
% Estimated value of PI
>> piEst=trials/sum(hits);

>> piEst

piEst = 3.1328

So in 10,000 throws of the needle, the estimate value of PI is 3.1328. Now let's refine our computer simulation. We want to calculate the standard deviation of the sample so that we know how wide the variance is in the calculated value of PI. Below is the MATLAB code that does that:

% we reduce each trial to 1000
>> trials = 1000;
%however we replicate the trails 20,000 times
>> repeat=20000;
% the for loop that does the job
>> for i=1:repeat
       x=rand(1,trials);
       theta=0.5*pi*rand(1,trials);
       hits= x<=0.5*sin(theta);
       y(i)=sum(hits)/trials;
end
>> hist(y)
>> recipEst=sum(y)/repeat

reciprocalEst =

    0.3183

>> vVec=(y-reciprocalEst).*(y-reciprocalEst);
>> v=sum(vVec)/(repeat-1);
>> stddev=sqrt(v)

stddeviation =

    0.0148

>> piEst=1/reciprocalEst

piEst =

    3.1413

So this time the estimated value of PI is 3.1413 with the standard deviation of 0.0148. So you can see we can do 20,000 repetitions of a random experiment in a few seconds on our computer. This is something powerful that can help us simulate random experiments on our computers.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Messages In This Thread
Monte Carlo Simulation For Traders - by Hassam - 04-11-2018, 06:48 AM
RE: Monte Carlo Simulation For Traders - by Hassam - 04-12-2018, 05:47 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)