Agenda

• First, a little more on C# Interfaces • Then today’s main topic: an introduction to Monte Carlo simulation • I strongly encourage you to read the chapters in Implementing Derivative Models that have to do with simulation • Also, if you have the Brandimarte book I encourage you to read the relevant chapters regarding Monte Carlo – You are going to use simulation elsewhere in the MFM program, so you’ll see this stuff again

C# Interfaces

• I’d like to cover an example of interfaces using a common .NET interface: IConvertible • We have seen IConvertible at work when we’ve used the Convert class • IConvertible forces the implementing class to implement a number of methods to convert data to a new desired type • We are going to create our own type and implement IConvertible to make our type compatible with the Convert class • Recall the syntax for implementing interfaces: class OurNewType : IConvertible

• I’m going to create a class that contains the Spot AUD/USD FX rate

C# Interfaces

• I can right-click on the IConvertible interface to implement the necessary methods in my class

The bottom choice, to implement explicitly, is useful if two interfaces are being implemented and they share a method name. For now, we can choose the top selection, “Implement Interface”.

• Notice the code generated by Visual Studio

C# Interfaces

Notice that these methods feature parameters that must implement the IFormatProvider interface.

• We must now implement code to deal with making a type conversion from our SpotAUDUSD data to any given format – Some type conversions are not supportable

• Here I added a field to contain the FX value SpotFX field with basic getter and setter definition.

C# Interfaces

Does it make sense to be able to convert a Spot FX price to a boolean?

• For type conversions that don’t make sense, we can throw a relevant exception This same exception is thrown if you try to convert a DateTime object to a boolean.

C# Interfaces

• For all other methods, implement relevant code to perform the conversion

• Now our new class can be used with Convert Because we are converting to an integer, rounding will take place and data will be lost.

C# Interfaces

• Don’t be afraid to try implementing .NET interfaces in your projects • Using .NET resources (like Convert) is much easier if you make your code conform to .NET using interfaces • Later in the semester I will introduce a feature of the C# language called LINQ • LINQ is a powerful way to organize and query collections of objects in your code • Understanding interfaces will allow you to make classes that work smoothly within the LINQ query structure

• So far in this class we have discussed three methods of pricing derivatives

Monte Carlo Simulation

– Black-Scholes (closed formula) – Binomial tree – Trinomial tree

• These methods, properly applied, provide ways to price many types of options (European, American, Asian, etc) • For a number of reasons, these methods are sometimes intractable • Monte Carlo simulation is a numerical technique that can be used to price almost anything

Monte Carlo Simulation

• There are a number of complex derivatives for which formulas, such as B-S, are unrealistic • Monte Carlo simulation is a process of creating a simulated path of an underlying (such as a stock) many times, and using those paths to price some type of financial instrument • Simulation is very useful in cases of options on multiple assets, spreads, and stochastic volatility or rates • Simulation can also create a more realistic picture of price evolution, including jumps in asset prices from daily close/open

Monte Carlo Simulation

• Monte Carlo simulation is often based on geometric Brownian motion • Recall from last semester one of the assumptions of the Black-Scholes model: The price of the underlying instrument follows a geometric Brownian motion with constant drift μ and volatility σ, and the price changes are log-normally distributed. • Recall also that Black-Scholes prices European options • If we use Monte Carlo simulation to price a European option, will the result be the same as Black-Scholes?

Monte Carlo Simulation

• Simulation, although highly flexible and useful, has substantial downsides • The foremost downside is required computation • Simulation by nature is computationally inefficient • In contrast, the Black-Scholes model or even a tree model requires much less computation • Simulation is based on numbers generated randomly, which generates a variance value relating to the group of random values • This variance is present in the price of the derivatives priced using the simulation, which can lead to inconsistent pricing

Monte Carlo Simulation

• All of the downsides can be addressed (although not eliminated) through a handful of tricks involving variance reduction and computational efficiency • We define a standard one-dimensional Brownian motion on [0,T] as a stochastic process {W(t),0≤t≤T} with the following properties – W(0)=0 – the mapping t → W(t) is, with probability one, a continuous mapping on [0,T] – the increments {W(t1)-W(t0),W(t2)-W(t1),…,W(tk)-W(tk1)} are independent for any k and any 0≤t0

• A little background on simulation…

Monte Carlo Simulation

– We are simulating the path of a security, which is modeled using Brownian motion – Brownian motion is described in continuous time using a stochastic differential equation, given below

– In this expression, μ and σ represent drift and volatility, respectively – For practical purposes, we define drift in terms of the risk-free interest rate – Volatility maintains its definition from Black-Scholes – This SDE describes the behavior of St

Monte Carlo Simulation

• More details… – dW represents the “random” component – Although only S and dW have t subscripts, μ and σ need not be constant as long as they are deterministic – like a yield curve – We apply Ito’s Lemma to make the problem tractable in discrete time; solution given below

– Here, epsilon represents independent standard normal random values at different times t – We can model this process easily using MATLAB

Monte Carlo Simulation

By inspection, why is this an inappropriate model to use for stock prices?

Monte Carlo Simulation

• More details… – Brownian motion is unsuitable for modeling most financial instruments because values may be negative – What type of financial instrument makes sense to model using Brownian motion? – We need a process that better represents the actual evolution of financial instrument prices – Geometric Brownian motion disallows negative asset prices – We define a geometric Brownian motion as a stochastic process for which log(S(t)) is a Brownian motion with initial value log(S(0)); a geometric Brownian motion is nothing more than an exponentiated Brownian motion

• More details…

Monte Carlo Simulation

– Geometric Brownian motion is described by the following SDE

Here, St is a coefficient of this side of the expression.

– Again we apply Ito’s Lemma, bringing us to the following expression

– On the class website there are two documents that show the process for these computations; look for stochastic_calc.pdf and ito.pdf

• In its most simple form, Monte Carlo simulation is simply the repetition of the following formula

Monte Carlo Simulation

Could recursion apply here?

• The inputs in this formula are things that should be familiar – σ = Volatility – μ = Drift (risk-free rate) – S0 = Simulation starting price (underlying price) – S(t+Δt) = Next step in the simulation – Δt = Time step (option tenor divided by steps) – ε = Normally distributed random number

• 5 Minute Challenge

Monte Carlo Simulation

Determine the standard deviation of W(50).

Monte Carlo Simulation

• Solution 7.0710678

Monte Carlo Simulation

• Notice that among the inputs, strike price of the option is not present • The details of the option will come later; right now only the underlying is being modeled • Consider the illustration below This graphic shows three generated Monte Carlo paths given S0=50, σ=0.5, μ=0.05, t=1, and steps=252. In other words, this shows three possible paths of daily prices for one year.

Monte Carlo Simulation

• For the purposes of illustration, assume that our simulation consists of only these three trials • Once the simulation is complete, the paths can be evaluated in the context of the option • The orange line represents the strike price

Assuming a call option, the two paths shown with green circles end in-the-money. Assuming a call option, this path shown in red would end out-of-themoney.

Monte Carlo Simulation

• In this example we are trying to price a European option and we have modeled a daily path • Since we’re pricing a European option, the path information isn’t necessary The path taken by the option isn’t useful information in pricing a European option.

Assuming a call option, the two paths shown with green circles end in-the-money. Assuming a call option, this path shown in red would end out-of-themoney.

Monte Carlo Simulation

• Once the paths have been established, the intrinsic value of the option is relatively easy to calculate – Intrinsic call value = S – K – Intrinsic put value = K – S

• The value of the European option is calculated by aggregating the intrinsic value at the end of each path and applying a discount formula • Expressed mathematically: Where j=1 is the first trial and there are M trials total. r is the risk-free rate and T is the tenor of the option. Vj is the intrinsic value at each terminal point in the simulation.

Monte Carlo Simulation

• In our example only three paths were generated • Three paths is inadequate to arrive at an accurate option value, regardless of option type • Today we will implement a very simple Monte Carlo simulator in class in MATLAB and explore its limitations • The first implementation will be shabby, but over the course of the semester keep in mind a few principles as we improve the algorithm – Variance in the option value should be minimized – Trials required to arrive at an accurate value should be minimized – Computational resources should be optimized

Monte Carlo Simulation

• In this first implementation, we will ignore virtually all of the principles just presented; we just want it to work for now • With no optimization whatsoever, how many trials will be required to arrive at an accurate value and how long will it take to process? • The algorithm is straightforward: – Create a matrix with x rows and y columns, where x is the number of trials and y is the number of steps – Create a nested loop structure where the GBM formula is used by the inner loop to calculate each step in a trial and the outer loop counts through the trials – Find the average “moneyness” and use the discount formula to price the option

Monte Carlo Simulation

• For the purposes of example, assume that we are pricing an option with the following characteristics: – – – – –

Underlying: $50 Strike price: $60 Risk-free rate: 5% Volatility: 50% Tenor: 1 year

• Furthermore, assume that our simulation will model daily prices for one year, as in the example Here I assume S is known. • Therefore, Δt = 1/251 • The only remaining variable is the number of trials 0

• The MATLAB code to generate simulations is straightforward • Create an m-file with the following code: Monte Carlo Simulation

function [result]=mcsim(trials) allsims=zeros(trials,252); allsims(:,1)=50;

This is the implementation of the GBM formula

for a=1:trials for b=2:252 allsims(a,b)=allsims(a,b-1)*exp((0.05(0.5)^2/2)*(1/251)+(0.5)*sqrt(1/251)*randn()); end end [TO BE CONTINUED…]

• This code generates a matrix of simulations

• If we were clever MATLAB programmers we could cut computational time in half… • Create an m-file with the following code: Monte Carlo Simulation

function [result]=mcsim(trials) allsims=zeros(trials,252); allsims(:,1)=50;

For the purposes of readability I’ll stick with the

for a=1:trials previous implementation for the remainder of this for b=2:252 lecture… allsims(:,b)=allsims(:,b-1).*exp((0.05(0.5)^2/2).*(1/251)+(0.5).*sqrt(1/251).* randn(:,b)); end end [TO BE CONTINUED…]

Monte Carlo Simulation

• 5 Minute Challenge Finish the code from the previous slide such that the simulations are used to price the option described. You may only use one line of code.

• Solution function [result]=mcsim(trials)

Monte Carlo Simulation

allsims=zeros(trials,252); allsims(:,1)=50; for a=1:trials for b=2:252 allsims(a,b)=allsims(a,b-1)*exp((0.05(0.5)^2/2)*(1/251)+(0.5)*sqrt(1/251)*randn()); end end

result=(sum(max(allsims(:,252)-60,0))/trials)*exp(-0.05*1);

Monte Carlo Simulation

• The Black-Scholes price of the option described is $7.39 • Earlier, the question was posed: If we use Monte Carlo simulation to price a European option, will the result be the same as BlackScholes? • The answer is yes…sort of • Remember that variance is an artifact of values generated through simulation • As the number of trials increases, the value generated by the simulation converges on the Black-Scholes value, assuming that we’re using Monte Carlo to price European options

Monte Carlo Simulation

• Below is a table of ten different runs with 100, 1000, and 10,000 simulations Trial #

100 sims

1000 sims

10,000 sims

1

$9.4000

$6.3775

$7.4696

2

$4.1774

$6.9350

$7.4296

3

$6.5287

$7.7842

$7.4218

4

$9.4627

$7.1347

$7.2206

5

$7.9928

$7.7716

$7.3001

6

$7.2718

$7.8633

$7.0033

7

$7.4671

$6.6468

$7.2221

8

$5.7568

$6.7356

$7.3259

9

$5.8364

$6.5518

$7.2541

10

$8.9725

$7.4592

$7.3360

As the number of sims increases, the value converges ($7.39 is the true value).

• I ran many simulations using my favorite Black-Scholes inputs (50,50,5%,50%,1 year) • I know that the Black-Scholes price is 10.89 Monte Carlo Simulation

1500

1000

500

0 10

10.2

10.4

10.6

10.8

11

11.2

11.4

11.6

11.8

This is a distribution of prices using 10,000 sims

12

• I ran many simulations using my favorite Black-Scholes inputs (50,50,5%,50%,1 year) • I know that the Black-Scholes price is 10.89 Monte Carlo Simulation

1500

1000

500

0 10.5

10.6

10.7

10.8

10.9

11

11.1

11.2

11.3

This is a distribution of prices using 50,000 sims

11.4

• I ran many simulations using my favorite Black-Scholes inputs (50,50,5%,50%,1 year) • I know that the Black-Scholes price is 10.89 Monte Carlo Simulation

1400

1200

1000

Notice the distribution is getting tighter…

800

600

400

200

0 10.65

10.7

10.75

10.8

10.85

10.9

10.95

11

11.05

11.1

11.15

This is a distribution of prices using 100,000 sims

Monte Carlo Simulation

• A more scientific way to determine the accuracy of simulation results is to calculate standard error • Chapter 4 in Implementing Derivative Models defines standard error, expressed below

• Here, C0 hat is the estimate of the option value, M represents the number of trials, and SD(C0,j) is the standard deviation of each path discounted • MATLAB has a built-in standard deviation function; C# does not

Monte Carlo Simulation

• Syntax (MATLAB): std(vector) • The std function in MATLAB will return the standard deviation of a vector • In C#, a standard deviation function needs to be created • Chapter 4 defines standard deviation as shown in this formula

Monte Carlo Simulation

• Standard error is a way to communicate to the user of the simulator the reliability of the output • It is also a handy way to compare variancereduction methods (we’ll talk about variancereduction in future lectures) • The simulator being implemented today is the least-sophisticated simulator possible • All else equal, standard error in results generated by this simulator should be highest All of your projects involving simulation should include a standard error measure

• With a slight modification to the code discounting the option, a put can be priced instead of a call Call

result=(sum(max(allsims(:,252)-60,0))/trials)*exp(-0.05*1);

Monte Carlo Simulation

S–K Put

result=(sum(max(60-allsims(:,252),0))/trials)*exp(-0.05*1); K–S

• We’ve created a rudimentary Monte Carlo simulator in only nine lines of code • Unfortunately this simulator isn’t very useful because the values generated are inaccurate • Also, this simulator is extremely slow, even to produce inaccurate values • We’ll address those issues in coming weeks…

Monte Carlo Simulation

• There is an analogy between this slide from last semester and the current simulation process

Monte Carlo Simulation

• Just as the underlying data in the binomial tree useless when pricing an option that was not pathdependent, the Monte Carlo evolution data is useless when not pricing path-dependent options • Efficiency can be improved with modest changes

If only the terminal values were calculated, only three values would need to be generated and stored. If the daily data is generated, 756 values need to be calculated and stored (3 x 252 = 756).

Monte Carlo Simulation

• Only Δt values need to be changed in the GBM formula to generate the terminal values

• Δt = 1; changes below are in red function [result]=mcsim(trials) allsims=zeros(trials,2); allsims(:,1)=50; for a=1:trials for b=2:2 allsims(a,b)=allsims(a,b-1)*exp((0.05(0.5)^2/2)*(1)+(0.5)*sqrt(1)*randn()); end end

What would be the value of Δt if the simulation aimed to produce monthly values?

result=(sum(max(allsims(:,2)-60,0))/trials)*exp(-0.05*1);

Monte Carlo Simulation

• With the preceding improvement to efficiency, many more simulations can be run, which should lead to better convergence Trial #

1,000,000 sims 2,000,000 sims

1

$7.4019

$7.3848

2

$7.3707

$7.3926

3

$7.3818

$7.4009

4

$7.3995

$7.4209

5

$7.4034

$7.3888

6

$7.3987

$7.3882

7

$7.4060

$7.4231

8

$7.3846

$7.4036

9

$7.3665

$7.3769

10

$7.3941

$7.3946

$7.39 is the Black-Scholes price of the option. Clearly, more sims leads to better values.

Monte Carlo Simulation

• Chapter 4 indicates that the total number of trials to approximate an option value without variance reduction is greater than 1,000,000 • The average of the ten trials in the previous slide where M=1,000,000 was $7.39 – the correct value • It is crucial to understand that Monte Carlo simulation operates on the principle of convergence • As M → ∞, SE → 0 You should understand that outcomes converge as the number of Monte Carlo simulations increases

Monte Carlo Simulation

• Let’s take a few minutes to tinker with the simulator and make some observations – Change the code to reduce volatility to 5% and increase drift to 95% – Change the code to output the matrix with all the simulation values instead of an option price – Define a matrix in the workspace and set it equal to 1000 simulations – Define a vector with 252 scalars, counting 1 to 252 – Plot the results of the simulation using the following syntax: plot(vector252, simmatrix)

• Notice the effect of a high drift and low volatility

Monte Carlo Simulation

Simulating using a high drift and a low variance creates a time series with a strong upward bias but relatively little variance in the individual paths.

Notice that there remains some variance in each individual path.

Monte Carlo Simulation

• Making several other changes should provide further intuition on simulation – Change the drift factor back to 5% and change the volatility to 25% – Run a simulation and plot the results – Leave the drift factor at 5% and change the volatility to 95% – Run a simulation and plot the results

• Just as in the Black-Scholes model, changes to these inputs will change the price of options You should understand the impact of changes in drift and volatility on simulation

Monte Carlo Simulation

This simulation depicts 25% volatility. Notice the high and low values.

Monte Carlo Simulation

This simulation depicts 95% volatility. Notice the minimum and maximum values plotted.

Monte Carlo Simulation

• Knowing the price of an option is only part of required information in option pricing • Methods have been developed to calculate Greek values like those in Black-Scholes • All of the Greeks can be approximated using finite difference ratios • Delta is calculated using the following formula

Where C(S+ΔS) is the Monte Carlo option value estimate using an initial value of S+ΔS. ΔS should be a small fraction of S, such as ΔS=0.001S.

Monte Carlo Simulation

• Gamma is calculated using the following formula

• Vega is calculated using the following formula

• Theta is calculated using the following formula

Monte Carlo Simulation

• Rho is calculated using the following formula

• The finite difference method of calculating Greek values comes with a few caveats – Each one effectively requires simulation on its own, which is computationally inefficient – Since they too rely on simulation, they are only as accurate as the option price tied to them – The same set of random values generated for the price simulation must be used to create simulations to determine Greek values

Each one effectively requires simulation on its own, which is computationally inefficient

Monte Carlo Simulation

• Consider the formula to calculate theta

• C(t+Δt) requires that M trials be run with all inputs stationary except t, which should be replaced with (t+Δt) • This same algorithm is repeated for each of the Greek formulas, holding four of five inputs static and adjusting a single input by a fraction

Each one effectively requires simulation on its own, which is computationally inefficient

Monte Carlo Simulation

• Consider the formula to calculate vega

• In the case of vega, both C(σ+Δσ) and C(σ-Δσ) must be run, which necessitates M trials for each • Assuming that M is a non-trivial number, computation time can rapidly become intractable, especially when calculating all Greeks • Improvements in efficiency are crucial

Monte Carlo Simulation

Since they too rely on simulation, they are only as accurate as the option price tied to them

• As was shown with the tables of option values generated, a simple Monte Carlo simulator produces fairly poor values with fewer than 1,000,000 simulations • The accuracy of Greeks calculated using the same number of trials, M, follows the same logic • Inaccurate sensitivity values (Greeks) are every bit as useless as inaccurate price values • Not only is calculating Greek values generally inefficient, but M must be very large to do it right

The same set of random values generated for the price simulation must be used to create simulations to determine Greek values

Monte Carlo Simulation

• Using the same set of random values to calculate simulations for Greeks makes intuitive sense – The sample of random values used to calculate the option price will have a given variance – That variance is transmitted to the resulting option price; the same variance should be transmitted to the Greek values

• This requirement is actually very good news! – Generating random numbers accounts for roughly 30% of simulation time – Recycling our random values is an efficiency gain

Monte Carlo Simulation

The same set of random values generated for the price simulation must be used to create simulations to determine Greek values

• Recycling random values requires that the simulation code presented earlier be amended

• 5 Minute Challenge Add to the previous code such that the simulation uses a set of random values that can be accessed later to calculate Greek values.

• Solution function [result]=mcsim(trials)

Monte Carlo Simulation

allsims=zeros(trials,252); allsims(:,1)=50; r=zeros(trials,252);

Since the random values generated will be needed later, define a matrix to hold all the random values. Generate a normal random value and store it in a matrix corresponding to the simulation and step value.

for a=1:trials for b=2:252 r(a,b)=randn(); allsims(a,b)=allsims(a,b-1)*exp((0.05(0.5)^2/2)*(1/251)+(0.5)*sqrt(1/251)*r(a,b)); end end

Pull the random value stored in the matrix.

result=(sum(max(allsims(:,252)-60,0))/trials)*exp(-0.05*1);

Note that we could make this code more efficient by defining a matrix of normal random numbers at the start, instead of defining a matrix of zeros and populating it with random numbers in the loop. The method here was used for clarity of exposition.

Monte Carlo Simulation

• Throughout the semester we will endeavor to improve simulation efficiency • There is an immediate opportunity to do so in the context of Greek calculation • Another approach, known as discounted expectation, is expressed below

If ST is greater than K, this expression evaluates to 1. Otherwise, it evaluates to 0.

Monte Carlo Simulation

• From an algorithm standpoint, expected value is simply a sum of all trials, M, divided by M • Note that this expression is only good for calculating the delta value of a call option

Monte Carlo Simulation

• Gamma cannot be calculated using discounted expectation directly, but can be calculated using finite difference in the context of delta, expressed here

• It is up to you to adapt these formulas to calculate Greek values for put options • Refer to page 105 in Implementing Derivative Models for a deeper discussion on computing hedge sensitivities and pseudo-code

Monte Carlo Simulation

• The second delta algorithm claimed to improve computational efficiency; let’s consider

In the case of the finite difference method, two new simulations must be run (C(S+ΔS) and C(S-ΔS)) to calculate delta.

The discounted expectation method requires a single full simulation to calculate the expected value. Intuitively, the discounted expectation method appears computationally superior.

Monte Carlo Simulation

• The second delta algorithm appears to indeed be an improvement; consider gamma

Using finite difference to calculate gamma presents the same inefficiency present in the delta calculation. The C(S) value could be considered inevitable, since the option value is likely to be calculated along with the Greek values.

Discounted expectation appears to be roughly as efficient in calculating gamma as finite difference. Discounted expectation does not require a C(S) value, which is slightly more efficient.

Monte Carlo Simulation

• Today Monte Carlo simulation has been introduced in the context of options and we will use it in that context throughout the semester • It is important to understand that simulation is used throughout finance and other disciplines – Actuaries use simulation to model mortality (life insurance) and various other events that affect an insurance company – Quants use simulation to model various financial instruments, such as interest rates (see Vasicek model and Ornstein-Ulenbeck process) – Physicists use simulation to model the behavior of particles (let’s not forget that Brownian motion is based on a physically observed principle!)

Monte Carlo Simulation

• Your assignment, due two weeks from now, is to create a Monte Carlo simulator in C# • The simulator must: – – – – –

Calculate the price of European calls and puts Calculate the Greeks associated with these options Calculate standard error of the simulation Use a GUI Permit the user a choice in number of simulations and number of steps per simulation

• Creating a C# version presents some challenges not found in MATLAB – Generating random numbers (refer to last semester) – Calculating standard deviation – Creating a GUI

Monte Carlo Simulation

• The next few projects in this class will be based on improving this simulator • As harped upon ad nauseam, your life will be much easier if you take the time to create a robust program now • Throughout the semester we will make continuous improvements our simulator and use it to price new things – Reduce variance (lower standard error) – Improve speed through variance reduction – Improve speed using multithreading – Price exotic options using simulation

Monte Carlo Simulation

• I suggest that you: – Try to define an object hierarchy for your Instruments (Euro options for now) – Try to engineer a way to separate the generation of Monte Carlo paths from the pricing of an option – Do not put all of your “business logic” in your GUI forms – Try using WPF at least once before you give up and use WinForms – Consider what data you’d want to save to a database for the user and what that data model would look like