Barrier option monte carlo simulation matlab

Author: Prog Date: 05.06.2017

Check out my ebook on quant trading where I teach you how to build profitable systematic trading strategies with Python tools, from scratch. Take a look at my new ebook on advanced trading strategies using time series analysis, machine learning and Bayesian statistics, with Python and R.

In this article, I will talk about how to write Monte Carlo simulations in CUDA. More specifically, I will explain how to carry it out step-by -step while writing the code for pricing a down-and-out barrier option, as its path dependency will make it a perfect example for us to learn Monte Carlo in CUDA. Also, I will show you how to efficiently generate random numbers with CUDA and how to measure performance with just a few lines of code. First, I will start with a brief theoretical introduction so if you already know how Monte Carlo methods and barrier options work, you can skip the following sections.

A barrier option is an exotic derivative, part of the set of path-dependent options, whose payoff depends not only on the underlying price at maturity but also on whether the price line hit a pre-determined level.

There are different ways to determine this level and how the price can or cannot reach it.

barrier option monte carlo simulation matlab

The first is the barrier level position in relation to the current underlying price spotso we have a first categorization "up" or "down". The second criterion can be "in" or "out", and it refers to what happens when the event "hit the level" is triggered. Also, it could be "paired" with any kind of option. We could have a European option with barrier, as well as an American or an Asian one.

Let's consider an example now. Suppose that we have a European option with a barrier. Let's choose now a down-and-out call. That means that the barrier is currently lower than the spot price and our option is already active. As we can see in the following chart:.

The underlying price hits the barrier before the maturity making it invalid. Sometimes, as a kind of insurance, we can have a rebate price, which is a fixed amount of money, usually less than the option value, which we will receive in case our option expires due to hitting the barrier. Of course, this will also change the price of the option itself.

In this article we will consider down-and-out barrier options without rebate, so that the payoff is given by:. The Monte Carlo method is a well-known method in finance, as it lets us compute difficult, if not impossible, expected values of complex stochastic functions. Mike has already discussed the method in several articles regarding option pricing, but a few recap lines can be helpful for those that are new to it.

The Monte Carlo method was first introduced in the field of physics, for complex simulations, very likely by Enrico Fermi in the s for studying neutron diffusion. It then became popular in the s among physicists and mathematicians involved in creating bombs for the U. The projects needed a code name, so John Von Neumann chose "Monte Carlo", referring to the famous Monte Carlo Casino.

Since then, technology and especially computational power have increased dramatically, letting us use these methods for a large variety of problems. In finance the Monte Carlo method is mainly used for option pricing as, especially with exotic options, the payoff is sometimes too complex, if not impossible, to compute. The main idea behind it is quite simple: Of course, the more simulations paths you make, the more accurate the result will be.

That should give good results for most of the simulations. Otherwise, there are techniques that can reduce variance in order to make even more accurate predictions.

Given the random nature of this process, variance reduction is not the only problem we can encounter. Another one, probably the most important, is how the random numbers are generated. There is an entire branch of mathematics talking about this and a detailed explanation is well beyond the purpose of this article, but we will see that CUDA can provide different efficient methods for generating random numbers by including the useful library curand.

As the Monte Carlo method is basically a way to compute expected values by generating random scenarios and then averaging them, it is actually very efficient to parallelise. Moreover, with consumer CPUs on standard computers it is just not possible to reach the accuracy needed, as simulating over one million paths is usually very time consuming.

With the GPU we can reduce this problem by parallelising the paths. That is, we can assign each path to a single thread, simulating thousands of them in parallel, with massive savings in computational power and time. At the end of this article I will show you the numerical results, making it quite obvious why it's better to run a Monte Carlo on a GPU.

First, let's see what and how to parallelise. In option pricing, usually the only variable that can assume random values is the underlying, so we only have to write a kernel that can generate a simulated value for the underlying and then calculate the option price.

Sounds easy, but actually we have to cope with a couple of issues that we could have avoided for the pricing of a path-independent option. That is, as the barrier can be hit at any point in time we have to simulate step by step the changes in the underlying price, significantly reducing the code speed.

Let's say that we want to run an accurate Monte Carlo, which means more than one million paths. And let's say that we also want to use a reasonable proxy for price changes, i. Also, for more complex derivatives or for purposes other than learning, daily changes will likely not have sufficient granularity. Before having a look at the code, let me give you the last theoretical basis independent futures trader resume need if you don't know it already stock market open on martin luther king day fully understand this method.

barrier option monte carlo simulation matlab

For simulating the underlying price, we must discretise the underlying's changes. In this article, I stock market strategy tester make use of the Euler method, as it's very easy to understand and code up and, despite the fact that it isn't the best method, it's still a good approximation for our needs.

Pricing Barrier Options Using Monte Carlo Methods | Option (Finance) | Computer Simulation

Now we have to compute the changes. This variable is the only reason why we need to run a Monte Carlo simulation. First, in order to minimise the interruption of explaining the code, I would like to system trading company gazebo this simple, yet effective, performance measurement tool.

You can get the number of clock ticks elapsed since the program started.

Montecarlo for Barrier Option:Algorithm

This is a C function and, as it can be affected by many factors, it's better to never use it alone. Instead you can compute the difference between two times and then get the time in seconds of a given task or code portion.

For receiving the time and not just the clock ticks, you can divide that number by the number of clocks per second that your CPU is able to perform. CUDA also provides a library for this purpose, but for now the C one is more than sufficient for us.

CUDA provides efficient random number generators for a lot of different distributions via the library curand. In this case, as the Brownian motion evolves with normally distributed random steps, we will use the normal generator. Then we have to decide what kind of legitimate work from home calgary we would like to use, calling the function curandCreateGenerator by passing as first argument our generator variable and the name of the generator method.

Now we have to set the seed. Seeds are the "base" upon which the random series will be built, so depending on the seed you will have a different random number series. In this case I choose ULLwhich means that a very long unsigned integer number will be used as a seed:. Now it's time to finally generate our normally distributed random numbers.

We can do this by using the curand function curandGenerateNormalwhich takes as inputs the curand generator, the output array in which we want to store the numbersthe amount of numbers to generate, the mean of the distribution and its standard deviation.

Now let's talk about the main part, looking at the code. At the end of this article you will find the complete code, so now I will explain it step by step. This first part consists of including libraries and variable declaration, but it is useful to notice a few choices I made.

First, the try instruction: This is good practice for longer programs and therefore a good habit to develop. Regarding the parameters, you can see that I divided unionville cattle auction tn into different blocks, reflecting their differing nature.

In the first one we can find the "dimensional constants", or rather the lengths of our arrays and loops. That barrier option monte carlo simulation matlab a huge constraint for our precision, as this big array will have to be allocated in the GPU memory. The second and the third blocks represent our input parameters. More specifically, the second is for constant declaration, in which constants are "market parameters" necessary for computing the option price, while the third block is for derived variables.

Simulation - Option Pricing Using Matlab

The fourth is for array declarations. So, if you haven't read it stock options dividends, it might be worth alpari how profitable are binary options a look at it, as it will be used several times for this script.

After having declared our variables and constants, and having generated the random numbers see abovewe can exchange rate chf to usd historical call the kernel function. As usual see the vector addition articleI prefer to separate the. As you can see, this function, included in the script kernel.

As it's a different script, we need to create a header file kernel. Now let's have a look at the most important part, the kernel. Here is the code that the GPU processes, so it's fundamental to understand how it works. This first part includes the header of the kernel function and the constant declaration regarding the positioning parameters:.

Then we initialise the indexes, one for getting the normally distributed numbers and one for our price array. So now we have our indexes:.

First, we need to be sure that no undesired thread will access our price array. Before the loop we have to check that the array index is lower than the maximum number of threads by wrapping the loop in this if statement:.

Pricing Barrier Options Using Monte Carlo Methods | Option (Finance) | Computer Simulation

That's a bit different from the usual Monte Carlo methods, which make use of a normal for loop but it reflects the path-dependent nature of the barrier option. In fact if we hit the barrier our down-and-out option will no longer be active, so continuing to simulate that path to maturity wouldn't make sense anymore.

You can see here there are two conditions for staying in the loop: We then update the price making use of the Euler discretisation and after that we update our indexes. Using only steps we are missing the cases in which the price fell under the barrier during a certain day and then closed higher than the barrier at the end of the day: When we finally have our final path price either at maturity or at the barrier level we can compute the option payoff in the following way:.

Now we have the price of a down-and-out barrier option in CUDA computed via the Monte Carlo method. Notice that it can also compute a European call just by setting the barrier value to 0. The script also has two further blocks of code, one for computing the price with the CPU for comparison and the last one for displaying the results. The CPU Monte Carlo simulation is exactly the same as the kernel one, as you can see:.

You can find the complete code, with the last part and the lines for displaying the output, at the end of this article. With these settings, and on my hardware, this is what I get:. We can see that the GPU implementation was roughly x faster than the CPU one, including the memory allocation host to device.

In future articles we will also talk about exploiting CUDA using different pricing methods, including multidimensional finite differences methods. QuantStart Log In Sign Up. Learn about QuantStart Read our Books Browse the Articles List Explore the Reading List Backtest with QSTrader Query the Support Knowledge Base. Monte Carlo Simulations In CUDA - Barrier Option Pricing.

By Valerio Restocchi on November 28th, In this article, I will talk about how to write Monte Carlo simulations in CUDA. Barrier Options A barrier option is an exotic derivative, part of the set of path-dependent options, whose payoff depends not only on the underlying price at maturity but also on whether the price line hit a pre-determined level.

As we can see in the following chart: In this article we will consider down-and-out barrier options without rebate, so that the payoff is given by: Monte Carlo Method The Monte Carlo method is a well-known method in finance, as it lets us compute difficult, if not impossible, expected values of complex stochastic functions. Monte Carlo and the GPU As the Monte Carlo method is basically a way to compute expected values by generating random scenarios and then averaging them, it is actually very efficient to parallelise.

Monte Carlo and Path Dependency First, let's see what and how to parallelise. Underlying Price Simulation Before having a look at the code, let me give you the last theoretical basis you need if you don't know it already to fully understand this method. Now we are ready to have a look at the code. Time Measurement First, in order to minimise the interruption of explaining the code, I would like to introduce this simple, yet effective, performance measurement tool.

Random Number Generation in CUDA CUDA provides efficient random number generators for a lot of different distributions via the library curand. The set of instructions is composed of at least four line of code, let's see them in detail: In this case I choose ULLwhich means that a very long unsigned integer number will be used as a seed: This first part includes the header of the kernel function and the constant declaration regarding the positioning parameters: So now we have our indexes: Before the loop we have to check that the array index is lower than the maximum number of threads by wrapping the loop in this if statement: When we finally have our final path price either at maturity or at the barrier level we can compute the option payoff in the following way: Expected Value Computation Now we have to compute the expected value, averaging all the prices that we got from the kernel.

First, we need to synchronize the device and to copy the prices from the device to the array. What follows is the for loop for computing the price sum and, thus, our expected price value. The CPU Monte Carlo simulation is exactly the same as the kernel one, as you can see: With these settings, and on my hardware, this is what I get:

barrier option monte carlo simulation matlab
Rating 4,2 stars - 403 reviews
inserted by FC2 system