MCS_in_R

Embed Size (px)

Citation preview

  • 8/13/2019 MCS_in_R

    1/4

    IE 603: Discrete Event System Simulation, IIT Bombay

    R is a free software environment for statistical computing and graphics. It is anintegrated suite of software for data manipulation, calculation and graphical display.

    Getting Started with R

    Open R. The white window you will see is the R console.

    Now let try some simple commands> x 1/x !Computes reciprocal of all values ofx. Can perform any

    such arithmetic operations> mean(x) !Computes mean ofx

    > sd(x) !Computes std dev ofx

    > sqrt(sum((x-mean(x))^2)/(length(x)-1)) !Same as sd(x)?

    Many more in built functions are available. You can access the HTML help files bytyping help.start() in R console and learn them on your own.

    Explanations on loops, basic outline etc can be found underAn Introduction to R Explanations on various statistical function can be found underPackages> Stats Explanations on histfunction can be found underPackages> Graphics

    Lets build our first simulation model to evaluate

    Click File >> New Script

    Input the code as shown below in the blank script window

    #My first MCS !Whatever follows # is a comment

    MCS1

  • 8/13/2019 MCS_in_R

    2/4

    IE 603: Discrete Event System Simulation, IIT Bombay

    Run the Script:

    With focus in the script editor window, click from menu, Edit > Runall

    Your script will run in the RConsole window on left. It will be in Red color.

    If there are any error messages, it will appear in Blue in the Console.

    Now, Go to R Console Window. At the prompt, type > MCS1(10) and press EnterResults of print statements will be displayed as above.

    Name MCS1 is the name given to the function you have written in script (.R) file.

    Compute the integral estimates using Monte Carlo Simulation, for above integral, for

    sample size = 10, 100, 1000, 10000, 100000. Observe the histograms obtained.

    The above mean values are just point estimates. We need to bound the error between

    this sample mean obtained and the true mean we are interested in !Lets use the

    measure of error: Confidence Interval (CI) of the MeanWe know that: Suppose X1,Xnare i.i.d samples from a Normal population, then

    the 100*(1 !)% CI of the true meanis:

    When population variance !2is known, When pop variance !

    2unknown,

    n

    zX !

    " 2/#

    n

    StX n 2/,1! "#

    Can we build a CI using the sample data generated in the above MCS1 function?Ans: NO !Recall discussions in the earlier classes

    Modify the above code to create batches, and then use the batch means to construct a

    valid CI, as shown below. Copy the code below in a new script window

    #My second MCSMCS2

  • 8/13/2019 MCS_in_R

    3/4

  • 8/13/2019 MCS_in_R

    4/4

    IE 603: Discrete Event System Simulation, IIT Bombay

    FC = 5000For i = 1!N

    Q(i) = UNIF(8000,12000)VC(i) = Sample from truncated normal(= 7, sd= 2, min= 3.5, max= 10)P(i) = Sample from truncated normal( = 10, sd = 3, min= 1)

    TP(i) = Q(i)*P(i) [(Q(i)*VC(i)) + FC(i)]Next i

    Exercises - Try the following and submit them via Moodle course page.

    1. Airline Optimization:Consider flight IEX 306 that flies daily from Mumbai to

    Chennai. Flight has 100 seats. Price of a ticket is on an average Rs. 6000. There is10% chance that a sold seat will remain vacant (i.e. passenger doesnt show up).

    Airline refunds 50% of the price of ticket to that passenger for such last minute

    cancellations. If the airline overbooks, then it pays an average of Rs.10000 for anypassenger who is bumped (i.e. denied seat in plane).

    Daily demand for seats is Poisson distributed with mean 120. Now, the airline can

    decide to sellx tickets, wherex can be more or less than the capacity of plane.

    What is the value ofx (the number of tickets to sell) that maximizes the expected

    profit?

    2. Finance: The following model is frequently used to mimic share price movement.Let Sidenote the price of share at time ih where i= 0, 1, 2, and his a positive time

    increment. Letand "denote growth rate and volatility respectively. Let

    u! 1

    2"e

    # $ h% e

    "$% &2

    'h'%

    1

    2("e

    # $h% e

    "$% &2

    ' h'2# 4,

    v! u# 1,

    p!e

    $ h# v

    u# v

    Then, Si=XiSi-1 whereXiare independent Bernoulli random variables with

    P{Xi=u} =pandP{Xi=v} = 1-p, for all i.

    (i) Simulate & plot the movement of the share price for each week during the next

    one year, h= 1/52. Let S0= 100,= 0.2 per annum and "= 0.3 per annum.(ii) Simulate 20 such paths (with plots), and compute the average price of stock at the

    end of one year.

    3. Area of Ellipse: Devise and implement a Monte Carlo Simulation procedure to

    calculate the area of the ellipse (x/a)

    2

    + (y/b)

    2

    = 1; a, b> 0.Does choice of a, baffect the accuracy of the results? Why or Why not?

    Note: a, b, to be inputs to the program