simulation in finance

Upload: ravi

Post on 02-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Simulation in Finance

    1/62

    Ben Van Vliet

    October 12, 2010

    Financial ApplicationsSimulation

    with

  • 8/9/2019 Simulation in Finance

    2/62

    2010 Ben Van Vliet 2

    I. VBA for EXCEL..................................................... .................................................... 3A. CODE DEFINITIONS....................... ............................................................. ............ 4B. RECORDING A MACRO....................................... ................................................... 8II. INTRODUCTION TO SIMULATION .......................................................... .......... 13

    III. CONTINUOUS DISTIRBUTIONS ............................................... ...................... 18A. UNIFORM DISTRIBUTION........................................................ ........................... 15B. INVERSE TRANSFORM METHOD ................................................. ..................... 18C. EXPONENTIAL DISTRIBUTION........................ .................................................. 19D. WEIBULL DISTRIBUTION............................................................................. ....... 21E. TRIANGULAR DISTRIBUTION................................................................... ......... 22F. NORMAL DISTRIBUTION ..................................................... ............................... 24G. LOGNORMAL DISTRIBUTION................................................................ ............ 28H. GENERALIZED INVERSE TRANSFORM METHOD ......................................... 29IV. DISCRETE DISTRIBUTIONS .................................................. .......................... 30A. BERNOULLI TRIALS...................................................... ....................................... 30

    B. BINOMIAL DISTRIBUTION....................................................................... ........... 31C. TRINOMIAL DISTRIBUTION..................................................... .......................... 32D. POISSON DISTRIBUTION................................................................... .................. 33E. EMPIRICAL DISTRIBUTIONS..................... ......................................................... 34F. LINEAR INTERPOLATION .......................................................... ......................... 35V. GENERATING CORRELATED RANDOM NUMBERS ...................................... 35A. BIVARIATE NORMAL............................ ............................................................. .. 36B. MULTIVARIATE NORMAL.................................................. ................................ 37VI. MODELING REAL TIME FINANCIAL DATA ................................................ 39A. FINANCIAL MARKET DATA................................................ ............................... 42B. MODELING TICK DATA.......... ....................................................... ...................... 44C. MODELING TIME SERIES DATA.................................................. ...................... 511. FAT TAILS.................................. ............................................................. ................ 532. STOCHASTIC VOLATILITY MODELS ....................................................... ........ 54a. ARCH(1)..................... ....................................................... ....................................... 54

    b. GARCH(1,1)..................................................... ........................................................ 54VII. MODELING OPTIONS .................................................... ................................... 56A. THE SIMULATION WAY ........................................................ .............................. 56B. THE COX-ROSS-RUBENSTEIN or BINOMIAL WAY........................................ 57C. THE BLACK-SCHOLES WAY...................................................................... ......... 58PROJECT I .................................................. ........................................................ ............. 59PROJECT II ................................................... ....................................................... ............ 60PROJECT III.......................................... ........................................................ ................... 61PROJECT IV ................................................. ....................................................... ............ 62

  • 8/9/2019 Simulation in Finance

    3/62

    2010 Ben Van Vliet 3

    I. VBA for EXCEL

    We use the Visual Basic Editor create and modify Visual Basic for Applications (VBA) procedures and modules in our Excel applications. Every Excel file can contain a VBA project to which you can add modules and forms. Here is a brief overview of the VBA

    Editor environment.

    Project Explorer: Displays the list of projects and project items in thedocument.

    Properties Window: Displays the list of the properties for the selected items

    Code Window: In the code window you can write, display and edit thecode in new procedures or in existing event procedures.You can open any number of code windows on differentmodules, class modules, user forms, so that the code iseasily visible (and cut, copy and paste can be done).

    The Side Bar: This is used to place the breakpoints when you debug the program.

  • 8/9/2019 Simulation in Finance

    4/62

    2010 Ben Van Vliet 4

    Object Browser: This provides quick access to list the commands, functionsthat are in the development environment.

    A. SYNTAX DEFINITIONS

    Now here are some definitions of a few terms:

    Procedure: A procedure is a block of code enclosed in Sub and End Sub statements orin Function and End Function statements. The difference between a Suband a Function is that a Sub procedure (or Sub-routine) has no returnvalue. Both Sub routines and Functions may have input parameters withtheir types defined in the parentheses. A Function must also declare areturn type after the parentheses.

    Sub MyPr ocedur e( a As Doubl e )

    End Sub

    Funct i on MyPr ocedur e( a As Doubl e ) As Doubl e

    The r et ur n val ue i s set as f ol l ows:MyPr ocedur e = a + 5

    End Funct i on

    Module: A module is a code window where we write procedures.

    Macro: An Excel macro is a Sub routine that contains instructions. This Subroutine can be triggered to run via button in your spreadsheet. We usemacros to eliminate the need to repeat steps of common performed tasks.

    Variables: Variables are physical memory locations inside the computer. VBA hasseveral variable types to hold different kinds of data. The most commonlyused are: Boolean (true/false), Char (character), Date (Date and Time),Double (floating point number), Integer , Object (any type), String (series of characters), Variant (any type).

    What about converting from one type to another?

    To convert the data type from one to another type conversions are used. VBAs built-infunctions:

    Function DescriptionCDbl Convert to Double

  • 8/9/2019 Simulation in Finance

    5/62

    2010 Ben Van Vliet 5

    Cint Convert to IntegerCStr Convert to StringCLng Convert to LongCDate Convert to DateChr Convert value to ascii character

    Asc Convert from ascii to value

    Here is some simple code to show type conversions:

    Opt i on Expl i ci t

    Sub t est ( )

    Di m a as I nt egerDi m b as St r i ngDi m c as Doubl e

    a = 5b = Hel l oc = 3. 14159

    Di m d as St r i ngd = CSt r ( a )

    Di m e as Doubl eE = CDbl ( a )

    End Sub

    Constant:

    A constant is a variable whose value cannot change. A Constant is declared using thekeyword Const instead of Dim.

    Const Exchange As St r i ng = CMEConst St ar t Dat e As Dat e = 05/ 05/ 2010

    When declaring a constant, you must assign its value.

    What is the scope and lifetime of variables and constants?

    As soon as a variable or constant goes out of scope it can no longer be accessed and itloses its value. Variables and constants can be given different scopes based upon how wedeclare them.

    1. Procedure-level scope . Variables are declared using Dim or Const inside a procedure.

  • 8/9/2019 Simulation in Finance

    6/62

    2010 Ben Van Vliet 6

    Publ i c Sub MyPr ocedur e( )

    Di m a As I nt egera = 7

    End Sub

    When the procedure is done running, the variable or constant goes out of scopedand is destroyed.

    2. Module-level scope . Variables that are declared above the Procedure definitionsstay within scope after the procedure is done running.

    Di m a As I nt eger

    Publ i c Sub MyPr ocedur e( )

    a = 7

    End Sub

    All variables with this level of scope are available to all procedures that are withinthe module.

    3. Project-Level, Workbook-Level, or Public Module-Level. These variables are

    declared at the top of any standard public module and are available to all procedures in all modules.

    If...Then...Else:

    Often we need to test for equality or inequality. We do this with an If...Then statement.The general syntax is this: if the condition to validate is true, then execute some code.

    Publ i c Sub MyPr ocedur e( )

    Di m a as I nt eger = 2

    Di m b as I nt eger = 3I f a < b Then

    MsgBox( Tr ue )End I f

    End Sub

  • 8/9/2019 Simulation in Finance

    7/62

    2010 Ben Van Vliet 7

    If need to add lines of code in the case where the expression evaluates to false, we useIf...Then...Else. This syntax is:

    I f a > b ThenMsgBox( Tr ue )

    El se MsgBox( Fal se )End i f

    Select...Case:

    If multiple evaluation conditions exist, we can use a Select...Case statement. You canthink of a SelectCase statement as a series of if statements. The syntax is:

    Sel ect Case a

    Case I s < 0 Case 1 To 5

    Case I s > 5

    Case El se

    End Sel ect

    DoLoop, While and Until:

    For repeated execution of a block of code, we can use one of several repletion or iterationstructures. The general syntax is of a Do While loop is:

    Do Whi l e a < 10a = a + 1

    Loop

    This line of code inside the Do While loop will executed repetitively until the conditionevaluates to false. The syntax of the DoLoop While is:

    Doa = a + 1

    Loop Whi l e a < 10

  • 8/9/2019 Simulation in Finance

    8/62

    2010 Ben Van Vliet 8

    ForNext:

    The most commonly used repetition structure is the For loop. The syntax for a For loopis:

    For a = 1 t o 10 St ep 1

    Next a

    In this case, the Step 1 is optional because the For loop will by default increment a by 1each time through the loop. However, any other incrementation would require the Stepclause.

    For a = 50 t o 0 St ep - 2

    Next a

    B. RECORDING A MACRO

    Here is an example showing how to record a VBA macro to calculate the average of fivenumbers.

    There are several ways of doing the above problem.

    1. General Way . Computing the sum of the five numbers and dividing by the totalnumber of numbers.

  • 8/9/2019 Simulation in Finance

    9/62

    2010 Ben Van Vliet 9

    2. Use the built-in Excel function for Average . Get the list of built-in Excelfunctions and select AVERAGE function.

    3. Write or Record a simple macro.

  • 8/9/2019 Simulation in Finance

    10/62

    2010 Ben Van Vliet 10

    Click OK, and then do the calculations your way (either by Step a/b fromabove). And Click on the STOP button as shown.

    This way you have a recorded macro, the code generated in this process isgenerated in the Visual Basic Editor.

  • 8/9/2019 Simulation in Finance

    11/62

    2010 Ben Van Vliet 11

    You can open the Visual Basic Editor by either pressing Alt + F11 key, or by goingthrough the following steps shown here:

    The following is the Visual Basic Editor which has the generated macro.

  • 8/9/2019 Simulation in Finance

    12/62

  • 8/9/2019 Simulation in Finance

    13/62

    2010 Ben Van Vliet 13

    II. INTRODUCTION TO SIMULATION

    A model is a representation of reality. Traditionally, models are mathematical equations,which are attempts at analytical or closed form, solutions to problems of representation.

    All models are wrong. Some models are useful. -Edwards Deming

    These equations enable estimation or prediction of the future behavior of the systemfrom a set of input parameters, or initial conditions. However, many problems are toocomplex for closed form equations.

    Simulation methods are used when it is unfeasible or impossible to develop aclosed form model. Simulation as a field of study is a set of algorithms that depend uponthe iterative generation of random numbers to build a distribution of probable outcomes.Because of their reliance on iteration, sometimes millions of them, simulation isaccomplished only through the use of computer programs.

    Simulation is especially useful when applied to problems with a large number of

    input distributions, or when considerable uncertainty exists about the value of inputs.Simulation is a widely-used method in financial risk analysis, and is especially successfulwhen compared with closed-form models which produce single-point estimates or humanintuition. Where simulation has been applied in finance, it is usually referred to as MonteCarlo simulation.

    Monte Carlo methods in finance are often used to calculate the value ofcompanies, to evaluate investments in projects, to evaluate financial derivatives, or tounderstand portfolio sensitivities to uncertain, external processes such as market risk,interest rate risk, and credit risk. Monte Carlo methods are used to value and analyzecomplex portfolios by simulating the various sources of market uncertainty that mayaffect the values of instruments in the portfolio.

    Monte Carlo methods used in these cases allow the construction of probabilisticmodels, by enhancing the treatment of risk or uncertainty inherent in the inputs. Whenvarious combinations of each uncertain input are chosen, the results are a distribution ofthousands, maybe millions, of what-if scenarios. In this way Monte Carlo simulationconsiders random sampling of probability distribution functions as model inputs to

    produce probable outcomes.Central to the concept of simulation is the generation of random numbers. A

    random number generator is a computer algorithm designed to generate a sequence ofnumbers that lack any apparent pattern. A series of numbers is said to be (sufficiently)random if it is statistically indistinguishable from random, even if the series was created

    by a deterministic algorithm, such as a computer program. The first tests for randomnesswere published by Kendall and Smith in the Journal of the Royal Statistical Society in1938.

    These frequency tests are built on the Pearson's chi-squared test, in order to testthe hypothesis that experimental data corresponded with its theoretical probabilities.Kendall and Smith's null hypotheses were that each outcome had an equal probability andthen from that other patterns in random data would also be likely to occur according toderived probabilities.

  • 8/9/2019 Simulation in Finance

    14/62

    2010 Ben Van Vliet 14

    For example, a serial test compares the outcome that one random number isfollowed by another with the hypothetical probabilities which predict independence. Aruns test compares how often sequences of numbers occur, say five 1s in a row. A gaptest compares the distances between occurrences of an outcome. If a data sequence isable to pass all of these tests, then it is said to be random.

    As generation of random numbers became of more interest, more sophisticatedtests have been developed. Some tests plot random numbers on a graph, where hidden patterns can be visible. Some of these new tests are: the monobit test which is afrequency test; the WaldWolfowitz test; the information entropy test; the autocorrelationtest; the K-S test; and, Maurer's universal statistical test.

  • 8/9/2019 Simulation in Finance

    15/62

    2010 Ben Van Vliet 15

    A. UNIFORM DISTRIBUTION

    Parameters a and b, the lower and upper bounds.

    Probability density:

    ab x f = 1)(

    Cumulative distribution function F(x):

    aba x

    x F =)(

    Expected value of x:

    2)(

    ba x E

    +=

    Variance of x:

    12

    )()(

    2ab xV

    =

    The Linear Congruential Generator (LCG) will generate uniformly distributed integersover the interval 0 to m - 1:

    ))(mod( 1 k d cuu ii += The generator is defined by the recurrence relation, where ui is the sequence of

    pseudorandom values, and 0 < m, the modulus, 0 < c < k, the multiplier, and 0 < d < m,the increment. u0 is called the seed value.

    Publ i c Funct i on LCG( c As Doubl e, d As Doubl e, k As Doubl e, _

    u0 As Doubl e ) As Doubl eLCG = ( c * u0 + d) Mod kEnd Funct i on

    A B C D1 c 6578 =LCG(B1,B2,B3,B4) =C1/($B$3-1)2 d 1159 =LCG($B$1,$B$2,$B$3,C1) =C2/($B$3-1)3 k 7825 4 u0 5684

    TABLE 1

    EXCEL: =MOD( c * u0 + d, k )

    The real problem is to generate uniformly distributed random numbers over the interval 0to 1, what we call the standard uniform distribution, where the parameters a = 0 and b =1. A standard uniform random number, u s, can be accomplished by dividing the LCGrandom integer by k 1 as in Table 1. However, Excel and VBA already have functionsthat return standard uniform random numbers:

  • 8/9/2019 Simulation in Finance

    16/62

    2010 Ben Van Vliet 16

    EXCEL: =RAND()

    VBA: =Rnd()

    Publ i c Funct i on Uni f or mRand( ) As Doubl eUni f or mRand = Rnd( )

    End Funct i on

    Generating Uniformly Distributed Random Numbers:

    Sub Gener at e( )Di m i as I nt egerFor i = 0 To Range( " A1" ) . Val ue

    Range( "A2") . Of f set ( i ) . Val ue = Rnd( )Next i

    End Sub

    In any case, the state of the art in uniform random number generation is the MersenneTwister algorithm. Most statistical packages, including MatLab, use this algorithm forsimulation.

    Turning a standard uniform random number, u s, into a uniformly distributed randomnumber, u, over the interval a to b.

    EXCEL: = a + RAND() * ( b - a )

    VBA:

    Publ i c Funct i on Uni f or m( a As Doubl e, b As Doubl e ) As Doubl eUni f or m = a + Rnd( ) * ( b a )

    End Funct i on

    Generating Uniformly Distributed Random Integers:

    Turning a standard uniform random number, u s, into a uniformly distributed randominteger of the interval a to b:

    EXCEL: = FLOOR( a + RAND() * ( b a + 1 ), 1 )

    VBA:

    Publ i c Funct i on Uni f or m( a As Doubl e, b As Doubl e ) As Doubl eUni f orm = I nt ( a + Rnd( ) * ( b - a + 1) )

    End Funct i on

  • 8/9/2019 Simulation in Finance

    17/62

    2010 Ben Van Vliet 17

  • 8/9/2019 Simulation in Finance

    18/62

    2010 Ben Van Vliet 18

    III. CONTINUOUS DISTIRBUTIONS

    A. INVERSE TRANSFORM METHOD

    The inverse transform method generates random numbers from any probability

    distribution given its cumulative distribution function (cdf). Assuming the distribution iscontinuous, and that its probability density is actually integratable, the inverse transformmethod is generally computationally efficient.

    The inverse transform methods states that if f(x) is a continuous function withcumulative distribution function F(x) , then F(x) has a uniform distribution over theinterval a to b. The inverse transform is just the inverse of the cdf evaluated at u:

    )(1 u F x =

    The inverse transform method works as follows:1. Generate a random number from the standard uniform distribution, u s.

    2. Compute the value x such that F ( x) = u. That is, solve for x so that F -1

    (u) = x. 3. x is random number drawn from the distribution f .

  • 8/9/2019 Simulation in Finance

    19/62

    2010 Ben Van Vliet 19

    C. EXPONENTIAL DISTRIBUTION

    Parameter , the scale parameter. The exponential distribution arises when describing theinter-arrival times in a (discrete) Poisson process.

    Probability density:

    xe x f = 1)(

    Derivation of the cumulative distribution function F(x):

    = x

    dx x f x F 0

    )()(

    dxe x F x x

    =

    0

    1)(

    dxe x F x x

    =

    0

    1)(

    0)(

    xe x F x =

    0)( += ee x F x xe x F = 1)(

    Expected value of x: =)( x E

    Variance of x:2)( = xV

    To generate a random number from an exponential distribution:)( x F u s =

    So that:)(1 su F x

    = Solve for x:

    x s eu

    =1 x

    s eu =1

    xu s = )1ln(

    )1ln( su x =

    Notice that if u s is a uniformly distributed random number between 0 and 1, then 1 u s isalso a uniformly distributed random number between 0 and 1. Thus,

    )ln( su x = is equivalent to the prior solution.

  • 8/9/2019 Simulation in Finance

    20/62

    2010 Ben Van Vliet 20

    EXCEL: = -$A$4 * LN( 1 - RAND() )

    VBA:

    Funct i on Random_Exp( bet a As Doubl e ) As Doubl eRandom_Exp = - bet a * Log( 1 - Rnd( ) )

    End Funct i on

  • 8/9/2019 Simulation in Finance

    21/62

    2010 Ben Van Vliet 21

    D. WEIBULL DISTRIBUTION

    Parameters and , the location and scale, where x 0.

    Probability density:

    )(1)( xe x x f =

    Cumulative distribution function F(x):

    )(1)( xe x F = Expected value of x:

    =

    1)( x E

    Variance of x:

    )1()21()( 121 ++= aa xV

    To generate a random number from a Weibull distribution:

    )( x F u s = So that:

    )(1 su F x =

    Solve for x: 1))1ln(( su x =

    EXCEL: = $B$2 * ( -LN( 1 - RAND() ) ) ^ ( 1 / $B$1 )

    VBA:

    Funct i on Random_Wei bul l ( al pha As Doubl e, bet a As Doubl e ) As Doubl eRandom_Wei bul l = bet a * ( - Log( 1 - Rnd( ) ) ) ^ ( 1 / al pha )

    End Funct i on

  • 8/9/2019 Simulation in Finance

    22/62

    2010 Ben Van Vliet 22

    E. TRIANGULAR DISTRIBUTION

    Parameters a, b, and m, the lower and upper bounds and the mode or most likely value, sothat a m b.

    Probability density:

    =b xmif

    mbab xb

    m xaif amab

    a x

    x f

    ))(()(2

    ))(()(2

    )(

    Cumulative distribution function F(x):

    =b xmif

    mbab

    xb

    m xaif amab

    a x

    x F

    ))((

    )(1

    ))(()(

    )( 2

    2

    Expected value of x:

    3)(

    mba x E

    ++=

    Variance of x:

    18)(

    222 bmamabmba xV

    ++=

    To generate a random number from a triangular distribution:)( x F u =

    So that:)(1 u F x =

    Solve for x s is standard triangular, where a = 0 , b = 1 , and where:

    abam

    m s =

    And, therefore:

    >

    = s s s s

    s s s s s muif um

    muif um x

    )1)(1(1

    So that x is triangular( a , b, m ):)( ab xa x s +=

    EXCEL:

    VBA:

  • 8/9/2019 Simulation in Finance

    23/62

    2010 Ben Van Vliet 23

    Funct i on STri angul ar( m As Doubl e ) As Doubl e

    Di m us As Doubl eus = Rnd( )

    I f us < m Then

    El se

    End I f

    End Funct i on

    Funct i on Tr i angul ar ( a As Doubl e, b As Doubl e, m As Doubl e ) As Doubl e

    Di m ms As Doubl ems = ( m - a) / ( b - a)

    Tr i angul ar = a + STr i angul ar ( ms ) * ( b - a)

    End Funct i on

  • 8/9/2019 Simulation in Finance

    24/62

  • 8/9/2019 Simulation in Finance

    25/62

    2010 Ben Van Vliet 25

    )( z F u =

    So that:)(1 u F z =

    Solve for x:= z approximation?

    Generating Random Numbers from the Standard Normal Distribution:

    To generate a z s, a random number drawn from the standard normal distribution, = 0and = 1.

    EXCEL: = NORMSINV( RAND() )

    VBA:

    There are three ways to generate standard normal random numbers. Here is the first way:

    Funct i on Random_SNor m1( ) As Doubl eDi m u1 As Doubl eDi m u2 As Doubl eu1 = Rnd( )u2 = Rnd( )Random_SNor m1 = Sqr ( - 2 * Ln( u1) ) * Cos( 2 * 3. 1415927 * u2)

    End Funct i on

    Here is the second way using an approximation to the Normal Inverse CDF:

    Funct i on SNor m_I nver seCDF( p As Doubl e ) As Doubl e Di ms ar e l ef t out f or br evi t y.a1 = - 39. 6968303a2 = 220. 9460984a3 = - 275. 9285104a4 = 138. 3577519a5 = - 30. 6647981a6 = 2. 5066283

    b1 = - 54. 4760988b2 = 161. 5858369b3 = - 155. 6989799b4 = 66. 8013119b5 = - 13. 2806816

    c1 = - 0. 0077849c2 = - 0. 3223965c3 = - 2. 4007583c4 = - 2. 5497325c5 = 4. 3746641

  • 8/9/2019 Simulation in Finance

    26/62

    2010 Ben Van Vliet 26

    c6 = 2. 938164

    d1 = 0. 0077847d2 = 0. 3224671d3 = 2. 4451341d4 = 3. 7544087

    p_l ow = 0. 02425p_hi gh = 1 - p_l ow

    q = 0#r = 0#

    Sel ect Case p

    Case I s < p_l owq = Sqr ( - 2 * Log( p) )SNor m_I nver seCDF = ( ( ( ( ( c1 * q + c2) * q + c3) * q + c4) _

    * q + c5) * q + c6) / ( ( ( ( d1 * q + d2) _* q + d3) * q + d4) * q + 1)

    Case I s < p_hi ghq = p - 0. 5r = q * qSNor m_I nver seCDF = ( ( ( ( ( a1 * r + a2) * r + a3) * r + a4) _

    * r + a5) * r + a6) * q / ( ( ( ( ( b1 * r _+ b2) * r + b3) * r + b4) * r + b5) * _r + 1)

    Case I s < 1q = Sqr ( - 2 * Log( 1 - p) )SNor m_I nver seCDF = - ( ( ( ( ( c1 * q + c2) * q + c3) * q + c4) _

    * q + c5) * q + c6) / ( ( ( ( d1 * q + d2) ) _* q + d3) * q + d4) * q + 1)

    End Sel ectEnd Funct i on

    Funct i on Random_SNor m2( ) As Doubl eRandom_SNor m2 = SNor m_I nver seCDF( Rnd( ) )

    End Funct i on

    Here is the third way which works because of the central limit theorem. However, the previous two ways should be preferred.

    Funct i on Random_SNor m3( ) As Doubl eRandom_SNor m3 = Rnd + Rnd + Rnd + Rnd + Rnd + Rnd + Rnd + Rnd + _

    Rnd + Rnd + Rnd + Rnd - 6End Funct i on

  • 8/9/2019 Simulation in Finance

    27/62

    2010 Ben Van Vliet 27

    Generating Random Numbers from a Normal Distribution:

    To generate a random number drawn from a normal distribution, with parameters and:

    s z z +=

    Funct i on Random_Nor m( mu As Doubl e, si gma As Doubl e ) As Doubl eRandom_Nor m = mu + Random_SNor m3( ) * si gma

    End Funct i on

  • 8/9/2019 Simulation in Finance

    28/62

  • 8/9/2019 Simulation in Finance

    29/62

    2010 Ben Van Vliet 29

    H. GENERALIZED INVERSE TRANSFORM METHOD

    Probability density:100003.)(:.. 2 = x x x f g E

    Cumulative distribution function F(x):

    == x

    xdx x x F 0

    32 001.003.)(

    Notice that this is a probability density because the total area under the curve from 0 to10 is 1. That is:

    1003.)(10

    0

    2 == dx x x F For the inverse transform method, we must solve for F -1. We set u = F ( x ) and solve foru.

    3

    001.)( x x F u ==

    31

    1 )1000()( uu F x ==

    To generate a random number from this probability density, if u s = .701, then:

    88.8)701.1000( 31

    == x

    Should we, on the other hand, wish to truncate this distribution and, say, generate arandom number only between 2 and 5, then we must scale u s over the range F (2) to F (5)thusly:

    ))()(()( a F b F ua F u s +=

    ))2()5(()2( F F u F u s +=

    )008.125(.008. += suu

    Again, if u s = .701, then u = .090. The random number x drawn from f(x) over the range 2< x < 5 is:

    48.4)090.1000( 31 == x

    Thus, we can generalize the inverse transform method as:

    )))()(()(()( 11 a F b F ua F F u F x s +==

  • 8/9/2019 Simulation in Finance

    30/62

  • 8/9/2019 Simulation in Finance

    31/62

  • 8/9/2019 Simulation in Finance

    32/62

    2010 Ben Van Vliet 32

    C. TRINOMIAL DISTRIBUTION

    Parameters p, q and n.

    The obvious way to extend the Bernoulli trials concept is to add ties. Thus, each de

    Moivre trial yields success ( x = 1 ), failure ( x = -1 ), or a tie ( x = 0 ). The probability ofsuccess is p, of failure q, and of a tie is 1 p q.

    Probability: y xn y x q pq p

    y xn y xn

    yY x X P === )1(

    )!(!!!

    ),(

    Expected value of x and y:nq ynp x E == )()(

    Variance of x:)1()()1()( qnq yV pnp xV ==

    To generate a random number from a de Moivre trial:

    ++

    =

    1uq pif 1q pu pif 0

    puif 1

    s

    s

    s

    x

    EXCEL: Series of de Moivre trials.

    VBA:

    Series of de Moivre Trials

  • 8/9/2019 Simulation in Finance

    33/62

    2010 Ben Van Vliet 33

    D. POISSON DISTRIBUTION

    Parameter .

    Number of events that occur in an interval of time when events are occurring at a

    constant rate, say exponentially.

    Probability:

    ,...}1,0{!

    )( =

    x for x

    e x P

    x

    Cumulative Distribution Function:

    0!

    )(0

    = =

    xif i

    e x F x

    i

    i

    Expected value of x and y: =)( x E

    Variance of x: =)( xV

    To generate a random number from a Poisson distribution:

    Step 1 : Let a = e - , b = 1, and i = 0.Step 2 : Generate u s and let b = bu s. If b < a, then return x = 1.

    Otherwise, continue to Step 3.Step 3 : Set i = i + 1. Go back to Step 2.

    EXCEL: NA

    VBA:

  • 8/9/2019 Simulation in Finance

    34/62

    2010 Ben Van Vliet 34

    E. EMPIRICAL DISTRIBUTIONS

    A cumulative distribution function gives the probability than a random variable X is lessthat a given value x. So,

    )()( x X P X F =

    An empirical distribution uses actual data rather than a theoretical distribution function.In the table below, n = 1000 observations are made with i = 4 outcomes, xi = { 100, 200,1000, 5000 } . The outcomes are sorted from low to high. Then calculated is the

    probability of each outcome, where the empirical probability, P(x i ), is the ratio of thecount of each outcome, n x, to the total number of trials.

    i xi n x P(x i) F(x i) If u s = .71,1 100 500 .50 .502 200 100 .10 .603 1000 250 .25 .85 x = 10004 5000 150 .15 1.0

    = 1000 = 1.0

    VBA:

    Funct i on Empi r i cal ( ) As Doubl e

    Di m us As Doubl eus = Rnd( )

    Di m x As Doubl e

    Sel ect Case usCase I s < 0. 5

    x = 100Case 0. 5 To 0. 6

    x = 200Case 0. 6 To 0. 85

    x = 1000Case 0. 85 To 1

    x = 5000End Sel ect

    Empi r i cal = x

    End Funct i on

  • 8/9/2019 Simulation in Finance

    35/62

    2010 Ben Van Vliet 35

    F. LINEAR INTERPOLATION

    Linear interpolation is a method of curve fitting using linear polynomials. If the coordinates oftwo points are known, the linear interpolant is the straight line between these points. For a pointin the interval, the coordinate values, x and y, along the straight line are given by:

    01

    0

    01

    0 x x x x

    y y y y

    =

    V. GENERATING CORRELATED RANDOM NUMBERS

    What does correlated mean? Correlation is the tendency for two series of randomnumbers to diverge in tandem, above or below, from their respective means.

    21

    21

    2

    21

    2

    1,2,1

    ,,

    )))(((

    x x

    n

    i xi xi

    x x

    x x x x

    x x

    i

    i

    =

    ==

  • 8/9/2019 Simulation in Finance

    36/62

  • 8/9/2019 Simulation in Finance

    37/62

    2010 Ben Van Vliet 37

    B. MULTIVARIATE NORMAL

    Random multivariate normal numbers (i.e. correlated random numbers for normaldistributions) can be generated by multiplying a vector of standard normal randomnumbers, Z s, by the Cholesky decomposition, L, of the correlation matrix, C, as follows:

    s s LZ Z =*

    The Cholesky decomposition is in the lower left triangle and main diagonal of a squarematrix. The elements in the upper right triangle are 0.

    Funct i on Chol esky( mat As Range ) As Doubl e( )Di m A As Var i ant , L( ) As Doubl e, S As Doubl eDi m n As Doubl e, m As Doubl eA = matn = mat . Rows. Count

    m = mat . Col umns. CountI f n m ThenChol esky = " ?"Exi t Funct i on

    End I f

    ReDi m L( 1 To n, 1 To n)For j = 1 To n

    S = 0For K = 1 To j - 1

    S = S + L( j , K) ^ 2Next K

    L( j , j ) = A( j , j ) S

    I f L(j , j )

  • 8/9/2019 Simulation in Finance

    38/62

    2010 Ben Van Vliet 38

    =

    1803.183.803.1503.

    183.503.1C

    The Cholesky decomposition matrix of C is:

    =

    538.823.183.0864.503.00000.1

    L

    Given a vector of standard normal random numbers (i.e. z s):

    =

    151.516.1517.

    s Z

    The vector of correlated standard normal random variables, Z s*, is:

    ==

    234.1050.1

    517.

    * s s LZ Z

    To convert the individual z s*s in Z s* to z is for each z i ~ N( i, i ):

    iiii z z *+=

  • 8/9/2019 Simulation in Finance

    39/62

  • 8/9/2019 Simulation in Finance

    40/62

    2010 Ben Van Vliet 40

    Brownian motion is another continuous-time stochastic process useful formodeling the prices of stocks and other financial instruments. In Brownian motion, theexpected value of the next price is equal to the last price. Geometric Brownian motion isa continuous-time stochastic process where the logarithm of the random variable followsBrownian motion. It is also called a Wiener process. A stochastic process S t is to be

    geometric Brownian motion if it satisfies the following stochastic differential equation:t t t t dW S dt S dS +=

    where W t is a Wiener process and is the drift rate and the volatility. For an initialvalue S 0 the equation we can find a random value at some time t in the future:

    t W t t eS S

    += )2/(02

    Where S t is a log-normally distributed with expected value:

    t t eS S

    0)( =

    and variance:

    )1(222

    0

    2 = t t S

    eeS t

    This conclusion can be verified using It 's lemma, where the continuous rate of return r =ln(S t /S 0 ) is normally distributed. A Wiener process W t has independent, normallydistributed changes such that:

    ),0(~0 t N W W W t t = (1)

    That is, the expected value and variance of a Wiener process is:

    0)( =t W E and t W V t =)( (2)

    This variance is important because is shows that the standard deviation is the square rootof t, and so it is that stock volatility scales with the square root of time.

    The following proof shows the connection to between dt from geometricBrownian motion and the normally distributed, continuously compounding drift term ( - 2 ) t. Given the assumption that stock price follows geometric Brownian motion,with Wiener process W:

    )(0 t t t t dW dt S dS S S +== (3)

    This says that the change in the price of the stock is equal to the price of the stock times amean drift rate times the change in time plus the standard deviation times some randomvariable.

    The reason we use geometric Brownian motion to model stock price paths is becauseit encapsulates two widely observed phenomena in financial markets:

    1. The long term trends of markets, represented by the mean term.

  • 8/9/2019 Simulation in Finance

    41/62

    2010 Ben Van Vliet 41

    2. The white noise or random price movements or volatility around the trend,represented by the standard deviation term, which scales with the squareroot of time.

  • 8/9/2019 Simulation in Finance

    42/62

    2010 Ben Van Vliet 42

    A. FINANCIAL MARKET DATA

    Exchanges provide for continuous quoting of bids and offers in shares or contracts listed on theexchange. From an automated trading perspective, a full quote consists of seven things: thesymbol, the quantity on the highest bid price, the highest bid price, the lowest ask price, thequantity on the lowest ask price, the last price traded, and the quantity of the last trade (or the sumof the quantities of the consecutive trades on that price).

    Symbol Bid Qty Bid Price Ask Price Ask Qty Last Price Last QtyES 195 125025 125050 456 125025 33

    Quote 1

    TICK

    The term tick has different meanings in finance. The tick size is the minimum priceincrementthe minimum bid-ask spreadthat is the difference between the highest bidand the lowest offer. In the example above the inside market (i.e. the highest bid and

    lowest ask) is 1250.25 to 1250.50, where the tick size is .25. The value of the minimumtick increment is not, in fact 25 cents; the contract size is much larger. In this sense, if atrader makes a tick profit on a trade, he has made .25 of a point. For the S&P 500 E-minicontract the value of a tick is $12.50. The whole-point value is often referred to as thehandle. In the above example, 1250 is the handle. If the contract increases to 1254, wesay the price is up four handles. In the case of this contract, a tick is a quarter handle.

    A tickas in tick data or uptick or downtickrefers to trade that has occurred onan exchange as been broadcasted to market particpants. In Quote 1 above, a tick would

    be reflected by a change in the Last Price and Last Qty fields. A days worth of tick datawould contain all the information about all the trades that occurred that day. A tick inthis sense contains at least four things:

    Ticker symbol Price Volume or quantity Time

    Now, some of these data elements may be represented by a value, by a change from the previous value, or by a predefined increment. For example, given data representing atrade as follows:

    Ticker Symbol Price Quantity TimeES 125025 33 13:43:12.100

    Trade 1

    Then, to save bandwidth, the next tick may show the trade data in an abbreviated format:

    Ticker Symbol Price Quantity TimeES -1 10 12

    Trade 2

  • 8/9/2019 Simulation in Finance

    43/62

    2010 Ben Van Vliet 43

    The new price of the new trade is 125000, because -1 means subtract 1 increment (e.g..25) from the previous price. The time of the trade is 13:43:12.112, the time of the

    previous trade plus 12 milliseconds.

  • 8/9/2019 Simulation in Finance

    44/62

    2010 Ben Van Vliet 44

    B. MODELING TICK DATA

    The goal is to simulate the arrival of tick data. Clearly, there are 3 stochastic processes atwork: the price, the quantity, and the arrival time interval. To do this, we will use theexponential distribution to simulate the inter-arrival times of new ticks. We will use a

    trinomial distribution to simulate price movementsup, down, or sideways. And, wewill use an empirical distribution to simulate quantities.

    Funct i on deMoi vr e( p As Doubl e, q As Doubl e ) As Doubl e

    Di m us As Doubl eus = Rnd( )

    Di m v As Doubl e

    Sel ect Case us

    Case I s < pv = 1Case p To p + q

    v = - 1Case p + q To 1. 0

    v = 0End Sel ect

    deMoi vr e = v

    End Funct i on

    Funct i on Exponent i al ( bet a As Doubl e ) As Doubl e

    Exponent i al = I nt ( - bet a * Log( 1 - Rnd( ) ) * 1000 + 1)

    End Funct i on

    Funct i on Empi r i cal ( ) As Doubl e

    Di m us As Doubl eus = Rnd( )

    Di m v As Doubl e

    Sel ect Case usCase I s < 0. 4

    v = 100Case 0. 4 To 0. 5

    v = 200Case 0. 5 To 0. 6

    v = 300

  • 8/9/2019 Simulation in Finance

    45/62

    2010 Ben Van Vliet 45

    Case 0. 6 To 0. 7v = 400

    Case 0. 7 To 0. 8v = 500

    Case 0. 8 To 0. 9v = 1000

    Case 0. 9 To 0. 925v = 1500

    Case 0. 925 To 0. 95v = 2000

    Case 0. 95 To 0. 975v = 5000

    Case 0. 975 To 1v = 10000

    End Sel ect

    Empi r i cal = v

    End Funct i on

    The initial time is 0, the initial price 50.00, and the initial quantity is 0. The Excelformula to generate millisecond arrival intervals is:

    =Exponential( 0.5 )

    The Excel formula to generate price movements is:

    =B3 + deMoivre( 0.333, 0.333 ) * 0.01

    The Excel formula to generate random quantity:

    =Empirical()

    These formulae generated the following random tick data:

    Times Price Qty0 50.00 0

    667 50.00 3001018 49.99 100

    38 49.99 100084 49.98 100

    651 49.98 20001599 49.98 100

    47 49.98 300100 49.97 100861 49.96 100287 49.97 100

    68 49.98 50095 49.97 100

    503 49.96 200

  • 8/9/2019 Simulation in Finance

    46/62

    2010 Ben Van Vliet 46

    The tick data produces the following chart:

    49.70

    49.75

    49.80

    49.85

    49.90

    49.95

    50.00

    50.05

    50.10

    50.15

    50.20

    1 61 121 181 241 301 361 421 481 541 601 661 721 781 841 901 961

    Random Tick Data

    Now, what we would like to do is convert the random tick data (with random inter-arrivaltimes) into a time series of bars (with constant time intervals), where a bar contains fourdata elements over the time interval:

    Open Price High Price Low Price Closing Price

    Using the tick data, there are a random number of ticks that occur over each one minuteinterval. (The number of ticks per interval follows a Poisson distribution.) By summingthe inter-arrival times until the minute is over, i.e. Times > 60000, we can find theExcel range of the ticks that occurred in that minute. The bar data is then:

    Open Price = first price in the range High Price = MAX( range ) Low Price = MIN( range ) Closing Price = last price of the range

    The tick data generated the following bars:

    Open High Low Close50.00 50.09 50.05 50.0750.06 50.29 50.01 50.2450.23 50.23 50.06 50.1050.11 50.13 50.03 50.0950.08 50.23 50.08 50.2150.21 50.27 50.14 50.2050.20 50.24 50.10 50.1150.10 50.24 50.08 50.22

  • 8/9/2019 Simulation in Finance

    47/62

    2010 Ben Van Vliet 47

    50.21 50.35 50.20 50.35 The bar data produces the following chart:

    49.80

    49.90

    50.00

    50.10

    50.20

    50.30

    50.40

    1 2 3 4 5 6 7 8 9

    Bar Data

    This data can be used to find the continuous returns for each minute interval as per:

    )ln( 1= iii P P r

    Where P i equals the closing price for minute i.

    Close Returns50.0750.24 0.003450.10 -0.002850.09 -0.000250.21 0.002450.20 -0.000250.11 -0.001850.22 0.002250.35 0.0026

    These rates of return, r, are approximately normally distributed:

    ),(~ 2 N r

    To demonstrate the approximation of returns to normality, we need to generate a lot morethan 10 returns. The following code will generate around 250 bars.

  • 8/9/2019 Simulation in Finance

    48/62

    2010 Ben Van Vliet 48

    Opt i on Expl i ci tOpt i on Base 1

    Publ i c Type Ti ck Ti me As Doubl ePr i ce As Doubl eQuant i t y As Doubl e

    End Type

    Sub Run_Si mul at i on( )

    Appl i cat i on. Scr eenUpdat i ng = Fal se

    Randomi ze

    Di m t i cks( 30000) As Ti ckDi m p As Doubl ep = 50

    Di m i As I nt egerDi m j As I nt eger

    ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '' ' Cr eat e r andom t i cks ' '' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

    For i = 1 To 30000t i cks( i ) . Ti me = Exponent i al ( 0. 5)t i cks( i ) . Pri ce = p + deMoi vre( 0. 333, 0. 333) * 0. 01t i cks( i ) . Quant i t y = Empi r i cal ( )p = t i cks ( i ) . Pr i ce

    Next i

    ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '' ' Cr eat e bar s f r om t i cks ' '' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

    Range( " A1") . val ue = " Open"Range( " B1" ) . val ue = " Hi gh"Range( " C1") . val ue = " Low"Range( " D1" ) . val ue = " Cl ose"

    Di m count As Doubl eDi m cur sor As Doubl eDi m t i me_sum As Doubl e

    cur sor = 1

    For j = 1 To 30000

    t i me_sum = t i me_sum + t i cks(j ) . Ti me

    I f ( t i me_sum > 60000) ThenRange( "A2") . Of f set ( count ) . val ue = t i cks( cur sor ) . Pr i ceRange( "B2") . Of f set ( count ) . val ue = Max( t i cks, cur sor , j - 1)Range( "C2") . Of f set ( count ) . val ue = Mi n( t i cks, cur sor , j - 1)

  • 8/9/2019 Simulation in Finance

    49/62

  • 8/9/2019 Simulation in Finance

    50/62

    2010 Ben Van Vliet 50

    48.5

    49

    49.5

    50

    50.5

    51

    1 13 25 37 49 61 73 85 97 109 121 133 145 157 169 181 193 205 217 229 241

    Randomly generated bar data

    The histogram of the log returns of the one minute time series appear to be approximatelynormally distributed:

    Histogram

    0

    5

    10

    15

    20

    25

    30

    35

    - 0 . 0 0 5

    0

    - 0 . 0 0 4

    0

    - 0 . 0 0 3

    0

    - 0 . 0 0 2

    0

    - 0 . 0 0 1

    0

    0 . 0 0

    0 0

    0 . 0 0

    1 0

    0 . 0 0

    2 0

    0 . 0 0

    3 0

    0 . 0 0

    4 0

    0 . 0 0

    5 0

    Bin

    F r e q u e n c y

    0.00%

    20.00%

    40.00%

    60.00%

    80.00%

    100.00%

    120.00%

    Distribution of log returns

    The mean return was very close to 0 at .000005, and the standard deviation was .0017.Given more bar data, we would see the normal distribution even more clearly.

  • 8/9/2019 Simulation in Finance

    51/62

    2010 Ben Van Vliet 51

    C. MODELING TIME SERIES DATA

    Here is an actual chart using real daily closing price data of Boeing stock (symbol BA)from 10-02-2008 to 10-02-2009.

    0

    10

    20

    30

    40

    50

    60

    1 13 25 37 49 61 73 85 97 109 121 1 33 145 157 169 181 193 205 217 229 241

    Price path of BA stock

    The histogram for the daily log returns for BA over the year also appears to beapproximately normally distributed:

    Histogram

    0

    5

    10

    15

    20

    25

    30

    35

    40

    45

    - 0 . 1

    - 0 . 0 8

    - 0 . 0 6

    - 0 . 0

    4 - 0

    . 0 2 0

    0 . 0 2

    0 . 0 4

    0 . 0 6

    0 . 0 8 0 .

    1

    Bin

    F r e q u e n c y

    0.00%

    20.00%

    40.00%

    60.00%

    80.00%

    100.00%

    120.00%

    Distribution of BA log returns

    The mean log return is .00011 with a standard deviation .0335.

  • 8/9/2019 Simulation in Finance

    52/62

  • 8/9/2019 Simulation in Finance

    53/62

    2010 Ben Van Vliet 53

    1. FAT TAILS

    The assumption in the previous model is that the standard deviation remains constantover the price path, which is almost certainly not representative of the real world. Stockstypically exhibit periods of low volatility and periods of high volatility, usually for

    exogenous or fundamental reasonsmarket shocks, earnings reports, etc. Fat tails are a property of financial data distributions relative to normal distributions which have thintails. In an attempt incorporate extreme events with greater probability than a normaldistribution would imply, mixture models that mix distributions together have beendeveloped. As a simple example, the code below implements a jump process, where 1%of the time the width of the distribution generating the random returns increases from onestandard deviation, or volatility, to another.

    Now, for simplicity, lets set 0, so that: s z

    t t eS S 1= VBA:

    Funct i on Mi xt ur e_of _Nor mal s( pr i ce As Doubl e, vol 1 As Doubl e, _vol 2 As Doubl e ) As Doubl e

    Di m us As Doubl eus = Rnd( )

    I f us < 0. 99 ThenMi xt ur e_of _Normal s = Random_Pr i ce( pr i ce, vol 1)

    El seMi xt ur e_of _Normal s = Random_Pr i ce( pr i ce, vol 2)

    End I f

    End Funct i on

    Funct i on Random_Pr i ce( pr i ce As Doubl e, vol As Doubl e ) As Doubl e

    Random_Pr i ce = pr i ce * Exp( mu + Appl i cat i on. Nor mSI nv( Rnd( ) ) * vol )

    End Funct i on

    A B C D1 Mean 0 502 StDev 1 0.01 49.507343 StDev 2 0.10 49.438784 49.74915 49.68668

    In cell D2 and copied down is the following formula:

    EXCEL: = Mixture_Of_Normals( D1, $B$2, $B$3 )

    This code produced the following sample price path:

  • 8/9/2019 Simulation in Finance

    54/62

    2010 Ben Van Vliet 54

    0

    10

    20

    30

    40

    50

    60

    1 60 119 178 237 296 355 414 473 532 591 650 709 768 827 886 945

    Random price path with jumps

    A histogram of the log returns will show a normal-like distribution but with fatter tails.

    2. STOCHASTIC VOLATILITY MODELS

    Stochastic volatility models treat volatility as a random process, governed by statevariables, and may include a serial correlation and a tendency to revert to a long-runmean value. Stochastic volatility models are one way to resolve the shortcoming of manyfinancial models that assume constant volatility over some time horizon, which iscertainly a contradiction of widely observed phenomena. Stochastic volatility models areused to value and manage risk associated with derivative instruments.

    a. ARCH(1)

    An autoregressive conditional heteroscedasticity (ARCH) model considers the varianceof the current period return, (i.e. the error term) to be a function of the prior periodsquared errors. We use it finance, to account for volatility clustering, i.e. periods of highvolatility tending to be followed by periods of low volatility. The ARCH(1) equation is:

    221 t t r +=+

    b. GARCH(1,1)

    An generalized autoregressive conditional heteroscedasticity (GARCH) model considersthe variance of the current period return, (i.e. the error term) to be a function of the prior

    period squared errors and the prior period estimated of variance. We use it finance, toaccount for volatility clustering, i.e. periods of high volatility tending to be followed by

    periods of low volatility. The GARCH(1,1) equation is:

  • 8/9/2019 Simulation in Finance

    55/62

    2010 Ben Van Vliet 55

    2221 t t t r ++=+

    Here is an Excel implementation of the GARCH(1,1) formula:

    A B C D E F

    1 gamma 0.0001 Price Return GARCH Vol2 alpha 0.4 503 beta 0.6 51 0.0198 0.01 0.14 53.44 0.0467 0.0062 0.07885 53.97 0.0098 0.0044 0.06696 50.57 -0.0651 0.0028 0.0531

    Where, cells C2 and C32 contain initial prices, cell D3 (and copied down) contains theformula = LN( C3 / C2 ), cell E3 is an initial value for the GARCH forecast and columnF is the square root of column E. The GARCH equation in E4 and copied down is:

    EXCEL: = $B$1 + $B$2 * D3 ^ 2 + $B$3 * E3The following chart, of the data in column F, shows the kind of volatility clusteringcommonly observed in markets.

    0

    0.01

    0.02

    0.03

    0.04

    0.05

    0.06

    0.07

    0.08

    0.09

    1 5 9 13 1 7 21 25 2 9 33 37 4 1 45 4 9 5 3 57 6 1 65 6 9 73 77 8 1 85 8 9 93 9 7

    Volatility Clustering

  • 8/9/2019 Simulation in Finance

    56/62

    2010 Ben Van Vliet 56

    VII. MODELING OPTIONS

    Before we begin to discuss option pricing, lets quickly review an importantrelationshipput-call parity. Put-call parity states that the value of a call at a given strikeimplies a certain value for the corresponding put. If there is divergence from parity, then

    an arbitrage opportunity would exist and trading could take risk-free profits until thedivergence from parity is eliminated.Put-call parity states that the price of the stock, the price of a call at a given strike

    and expiration, and the price of a put with that same strike and expiration, are allinterrelated and simultaneously solved according to:

    0S P XeC rt +=+

    Where, C is the call price, P the put price, X the strike, S the stock price, r the interestrate and t the time till expiration.

    A. THE SIMULATION WAY

    Recall that the t time ahead price can be simulated in the following way:t z t

    t seS S += 0

    Also, the call option payoff at time t can be expressed as:

    )0,max( X S C t =

    Where X is the strike price of the option. Using VBA we generate many simulations of

    the stock price to generate many call option payoffs.

    VBA:

    Funct i on Si m_Eur _Cal l ( S As Doubl e, X As Doubl e, r f As Doubl e, _t As Doubl e, si gma As Doubl e, si ms As Doubl e ) _As Doubl e

    r f = ( r f - 0. 5 * si gma ^ 2) * t

    sum_payof f s = 0#

    For n = 1 To si msST = s * Exp( r f + Appl i cat i on. NormSI nv( Rnd) * s i gma * Sqr ( t ) )sum_payof f s = sum_payof f s + Max( ST - X, 0#)

    Next n

    Si m_Eur _Cal l = Exp( - r f * t ) * ( sum_payof f s / si ms)End Funct i on

  • 8/9/2019 Simulation in Finance

    57/62

    2010 Ben Van Vliet 57

    B. THE COX-ROSS-RUBENSTEIN or BINOMIAL WAY

    VBA:

    Funct i on Bi n_Am_Cal l ( S As Doubl e, X As Doubl e, r f As Doubl e, _t As Doubl e, si gma As Doubl e, n As Doubl e ) _As Doubl e

    dt = t / n

    u = Exp( si gma * Sqr ( dt ) )d = Exp( - si gma * Sqr ( dt ) )

    r = Exp( r f * dt )qu = ( r - d) / ( r * ( u - d) )qd = 1 / r - qu

    Di m Opt i onVal uesEnd( ) As Doubl e

    ReDi m Opt i onVal uesEnd( n)For i = 0 To n

    Opt i onVal uesEnd( i ) = Max( s * u ^ i * d ^ ( n - i ) - X, 0)Next i

    For i = n - 1 To 0 St ep - 1

    ReDi m Opt i onVal uesPr i or( i - 1) As Doubl e

    For j = 0 To iOpt i onVal uesMi ddl e( j ) = Max( s * u ^ j * _

    d ^ ( i - j ) - X, _qd * Opt i onVal uesEnd( j ) + _qu * Opt i onVal uesEnd( j + 1) )

    Next j

    ReDi m Opt i onVal uesEnd( i - 1)

    For j = 0 To iOpt i onVal uesEnd( j ) = Opt i onVal uesMi ddl e( j )

    Next j

    Next i

    Bi n_Am_Cal l = Opt i onVal uesMi ddl e( 0)End Funct i on

  • 8/9/2019 Simulation in Finance

    58/62

  • 8/9/2019 Simulation in Finance

    59/62

    2010 Ben Van Vliet 59

    PROJECT I

    1. Create a VBA function that generates random numbers from a triangulardistribution. In Excel, generate 1000 random numbers using this function and show thedistribution of outcomes in a histogram.

    2. Create a VBA function that generates random integers greater than 0 using anexponential distribution.

    3. Here is a probability density:

    500064.)( 3 = x x x f

    Create a VBA function that generates random numbers from this distribution using theinverse transform method. Also, create a VBA function that generates random numbersfrom a to b, a truncation (or sub-range) of the density.

  • 8/9/2019 Simulation in Finance

    60/62

    2010 Ben Van Vliet 60

    PROJECT II

    1. Generate 100 x 1s and x 2s using a bivariate normal distribution (BVN) and VBA.

    2. Generate 100 xs using empirical data in bins: 100, 500, 1000, 5000, 10000. The probabilities are .2, .3, .1, .2, and .2 respectively. Use linear interpolation in VBA tocalculate values of the xs from within these bins.

    3. Generate 100 x 1s to x ns using a multivariate normal distribution (MVN).

  • 8/9/2019 Simulation in Finance

    61/62

    2010 Ben Van Vliet 61

    PROJECT III

    1. Generate a random stock price path for 1000 days using ARCH(1) and jumps inVBA. For the coefficients, use:

    0.9 0.0001

    Create a histogram of the log returns.

    2. Generate a random stock price path for 1000 days using GARCH(1,1) and jumpsin VBA. For the coefficients, use:

    0.5 0.4

    0.0001 Create a histogram of the log returns.

  • 8/9/2019 Simulation in Finance

    62/62

    PROJECT IV

    1. Create an Excel application with functions in VBA for Black Scholes puts andcalls, plus functions for the Greeks: Delta, Gamma, Rho, Theta and Vega. Be sure toshow that these functions work properly in by calling them in Excel.