Monte Carlo simulation with TM1

A bit about Vangelis:

Hi, I am Vangelis, I am from Greece and now I live in Sydney Australia.

Currently, I’m working as a consultant at Infocube Consulting. I have expertise in IBM Cognos products, BI and TM1 and also in mathematics and statistics.

My academic background consists of a BSc in Mathematics, MSc in Finance & Banking, MSc in Information & Telecommunications Technologies and a couple of certifications on IBM Cognos products and KXEN data mining software.
I can also speak 2 languages – Greek and English.

My interests are sports and movies and I like open source software like linux, along with many others.

In this article we are going to demonstrate how to represent the results of a Monte Carlo simulation within  TM1 via Cognos Insight. We also include both a Cube Rule and a Turbo Integrator Process that will manipulate the results for the best format for analysis. For this demonstration we used Cognos Insight version 10.2.1 which supports scatter plots.

Monte Carlo simulation is a mathematical problem solving technique used to approximate the probability of certain outcomes by using a stochastic process to perform repeated random sampling on the problem.

The accuracy of the result depends on the number of samples used and increases as the number of samples increase.

A very common example of demonstrating a Monte Carlo Method is by estimating the value of Pi.

Let us consider a circle, with a radius of one which is within a square, with edges of length two as shown in the below diagram.

 

Now, let us consider drawing random points in the square.  You can imagine this as if this was a dart board and someone is throwing darts. Then we can determine if a point is within the circle or not.

The area of a circle with a radius r is πr2.

The area of the square with an edge x is x2. In our example x = 2r thus the area is 4r2.

We can calculate the ratio of the random points inside the circle to the random points within the square by

Thus     

Therefore if we can calculate the ratio with a Monte Carlo simulation we can calculate the Pi. We will demonstrate how to do this in TM1, with rules and also with a Turbo Integrator (TI) script.

There are a number of different ways to do it, we just show 1. We created 2 cubes. 1 called “Monte Carlo” with 2 dimensions. Dimension “lines” which has the number of simulations 1 to 1000 as members and can be easily repopulated with a TI process to increase the number of simulations. Dimension “measures” which has members like “x”, ”y”, ”distance”,” x in circle”, “y in rectangle”, “area in circle”, “area in rect”.

And a second cube called “Pi” with 2 dimensions with members like ”area in circle”, “area in rectangle” and “pi” and another dimension “measures” with only one member “value”.

First we generate random values for x and y. TM1 has the rand() function which generates random values from 0 to 1. But because we want to generate values from -1 to 1, we write the formulas as

[‘x’]=N:2*rand()1;

[‘y’]=N:2*rand()1;

If we create a scatter plot for 1000 or more of calculated points we will end up with a square, much like the one in the figure below

The distance “d” between 2 points A = (x1, y1) and B = (x2, y2) is given by the formula (which is an application of the Pythagoras’ theorem for right triangles):

The point (x1, y1) is always(0,0) which is the centre of the circle. Therefore the distance between each point and the centre of the circle is

and in TM1 is written as

[‘Distance’]=N:SQRT([‘x’]^2+[‘y’]^2);

 

Then we need to calculate the points inside the circle. All points within the circle must have a distance less or equal to 1, so the code is:

[‘x in circle’]=N:IF([‘Distance’]<=1,[‘x’],0);

[‘y in circle’]=N:IF([‘Distance’]<=1,[‘y’],0);

 

If create a scatter plot with this points we will end up with a circle much like the one in the figure below

Now that we have the points inside the circle we need to calculate how many are within the circle as:

[‘area in circle’]=N:IF([‘Distance’]<=1,1,0);

And the total number of points is:

[‘area in rect’] =N:1;

Finally we calculate Pi as

[‘Pi’, ‘Value’]=4*[‘Area in Circle’, ‘Value’]\[ ‘Area in Rectangle’, ‘Value’];

The above example is done with TM1 rules but you can calculate pi by using a TI script instead of rules. In this case all you need to do Monte Carlo Simulation is the cube “pi” and to run the following script.

Create a parameter pTrials which is the number of simulations to do.

Then in prolog type the following script:

inCircle=0;

index=1;

while(index<pTrials);

x=2*rand()1;

y=2*rand()1;

dist=sqrt(x^2+y^2);

IF(dist<=1);

inCircle=inCircle+1;

endIF;

index=index+1;

end;

pi=4*inCircle\pTrials;

CellPutN(pi,’New Cube 1′,’Pi’,’Member 1′);

 

Monte Carlo Simulation Cognos Insight file

 

 

 

 

 

If you like this post, please spread the love…
This entry was posted in Cognos Express, Cognos Insight, TM1 10.1 and tagged Cognos Insight, Monte Carlo simulation, TM1. Bookmark the permalink.

2 Responses to "Monte Carlo simulation with TM1"

Log in to post a comment.