Random walks down Wall Street, Stochastic Processes in Python
James Bond is not a quant, but many famous quantitative fund managers enjoy playing poker in their spare time. Stochastic processes can be used to model the odds of such games. This article discusses some of the popular stochastic processes used by quants. This article was written with the help of a fellow quant and friend, Wilson Mongwe. I recommend reading his more detailed article on jump diffusion stochastic processes.
Learning about stochastic processes was quite challenging for me because I prefer to learn through experimentation and tend to think visually. So when learning about stochastic processes I found myself coding the stochastic processes, running them with different parameters, and plotting the results. Needless to say, this process was inefficient so I got in touch with a new innovative start-up company called Southern Ark and asked them to build an interactive educational web application for learning about stochastic processes.
Update - I got quite a few questions regarding the usage of this code, as per some of the suggestions from readers I have created an IPython Notebook. Hope that helps 🙂
A random event is any event which has a chance of happening. Probability is the measure of that chance. Random variables are functions which receive a random event and return a real number. Random variables may be discrete or continuous; discrete random variables are ones with a countable number of possible outcomes; continuous random variables are ones which have an infinite number of possible outcomes. In the context of finance, a stochastic process is a collection of random variables which describe the evolution of a system over time.
The beauty of random variables and stochastic processes is that they can be used to describe what is happening in the world around us. Often this is done by simulating multiple outcomes from a stochastic process in a Monte Carlo simulation. Example applications include the simulation of gambling games (Poker and Blackjack for sure), fluid and particle dynamics (which is often used in computer graphics for animations), in genetics to determine the likelihood of phylogenetic trees (how species relate), and even the evolution of stock prices and interest rates over time.
The rest of this article is structured as follows feel free to jump around,
- Stochastic Processes in Python
- The Brownian Motion Stochastic Process (Wiener Process)
- The Geometric Brownian Motion Stochastic Process
- The Merton Jump Diffusion Stochastic Process
- The Heston Stochastic Volatility Process (Heston Model)
- The Mean Reverting Cox Ingersoll Ross (CIR) Stochastic Process
- The Mean Reverting Ornstein-Uhlenbeck Stochastic Process
- Stochastic processes for derivatives pricing and hedging
- A philosophical digression
Stochastic Processes in Python
Stochastic processes are useful for many aspects of quantitative finance including, but not limited to, derivatives pricing, risk management, and investment management. These applications are discussed in further detail later in this article. This section presents some popular stochastic processes used in quantitative finance and their implementations in Python.
The model parameters class contains all of the parameters used by the following stochastic processes. The parameters have been prefixed with the name of the stochastic process they are used in for ease of understanding. Calibration of the stochastic processes would involve looking for the parameter values which bets fit some historical data. For those interested calibration will be discussed in later follow-up posts on my blog.
Some of the stochastic processes presented below produce a sequence of returns which have been sampled from the return distribution. In order to produce price levels from these return sequences the returns need to be exponentiated and then iteratively multiplied by the previous price level, starting with the starting asset value.
Graphing the results
The code below uses Matplotlib to graph a set of stochastic processes.
Brownian motion is the random motion exhibited by particles which have been suspended in a gas or liquid. This random motion is caused by the collision of the particles with the atoms or molecules in the liquid or gas. Brownian Motion is named after the Botanist Robert Brown who observed the random movements in 1827. The relationship between Brownian Motion and financial markets dates back to a paper written many years later, in 1900, by Louis Bachelier entitled The Theory of Speculation. His paper was the first to propose the use of Brownian Motion to evaluate stock options. The paper did not surface until later works in deriving the famous Black Scholes options pricing formula developed by Fisher Black and Myron Scholes in 1973. In the context of stochastic processes used in finance, Brownian Motion is often described as a Wiener process, denoted by . A Wiener process is described by the following properties,
- The function is continuous
- has independent normally distributed increments i.e. ~
In practice Brownian Motion is not used to model asset prices. I have included it because it is the foundation of every other stochastic process discussed in this article.
Here is an example of the output generated from this method. Notice that on average the generated paths do not drift upwards or downwards over time, in other words, the expected return is zero.
Geometric Brownian Motion (GBM) was popularized by Fisher Black and Myron Scholes when they used it in their 1973 paper, The Pricing of Options and Corporate Liabilities, to derive the Black Scholes equation. Geometric Brownian Motion is essentially Brownian Motion with a drift component and volatility component. The stochastic differential equation which describes the evolution of a Geometric Brownian Motion stochastic process is,
where is the change in the asset price, , at time ; is the percentage drift expected per annum, , represents time ( is used for daily changes), is the daily volatility expected in the asset prices, and is a Wiener process a.k.a Brownian Motion. As can be seen below the Brownian Motion code is used in the Geometric Brownian Motion method to construct a sequence for .
Here is an example of the output generated from this method. Notice that on average the generated paths drift upwards over time and have a larger variance of possible ending prices. In this example the paths grow at an average rate of 14% per annum as such the expected return is equal to 14% compounded over three and a bit years (800 days).
Robert C. Merton was one of the first academics to address some of the limitations in the Geometric Brownian Stochastic Process presented by Fisher Black and Myron Scholes. In 1997 Merton and Scholes won a Nobel Price in Economics for their work; Black had unfortunately passed away at that point in time. One of the stochastic processes proposed by Merton addressed the impossibility of a discontinuity occurring in the continuous Geometric Brownian Motion model.
Merton extended the original Geometric Brownian Motion process with a Jump Diffusion process in his 1976 paper, Option pricing when underlying stock returns are discontinuous. For an in-depth discussion on the mathematics underlying Jump Diffusion processes please see, an Introduction to Diffusion and Jump Diffusion Processes but in summary the stochastic process adds a jump process such as a compound Poisson process to an underlying diffusion process such as Geometric Brownian Motion. In the code below the jump component is called jump_sizes,
where is the jump component,
where is the Poisson process with rate and is a random variable which follows a log-normal distribution.
Notice that the assets have a slightly lower expected return on average because the jump diffusion processes has introduced downward discontinuities or jumps. Such discontinuities can represent market crashes. If the average jump size were positive, they could represent market spikes over very short periods of time which do can also happen.
The returns generated by the Geometric Brownian Motion stochastic process are normally distributed. We we add jumps we are essentially making the tail ends of the return distribution fatter, which is more realistic. Also, we know the market contains jumps e.g. in the S&P 500 there have been sixteen single day movements of more than -6% since 1929.
The original Geometric Brownian Motion stochastic process assumes that volatility over time is constant. In the early 1990's Steven Heston relaxed this assumption and extended the Geometric Brownian Motion model to include stochastic volatility. The resulting model is called the Heston model. In the Heston model volatility over time evolves over time according to the Cox Ingersoll Ross stochastic process. As such the model makes use to two Wiener processes, one for the Cox Ingersoll Ross process and another for the Geometric Brownian Motion process which uses the Cox Ingersoll Ross process as volatility. These two Wiener processes are correlated and can be constructed using a Cholesky Decomposition.
The stochastic differential equation for the Heston model is given as,
(Cox Ingersoll Ross), and
is the drift of the asset, and are two correlated Wiener processes where the correlation is given by , is the rate of mean-reversion of the Cox Ingersoll Ross process, is the mean value over time (in this case the mean volatility over time), and is the volatility of the Cox Ingersoll Ross process. The term, is also called the drift factor.
In order to correlate the two Wiener processes we matrix multiply uncorrelated Wiener processes with the Cholesky Decomposition of the correlation matrix we wish to produce. The Cholesky Decomposition technique decomposes a correlation matrix (positive definite matrix) into the product of its lower triangular matrix (or upper triangular matrix) and its conjugate transpose. Here is a simple example of how this works. If we are working with just two asset paths a correlated Wiener process can be constructed from another Wiener process as follows,
Given two uncorrelated Wiener processes and we can 'transform' such that it becomes correlated to where the correlation between and is equal to as follows,
The code for the Heston mode, the short-cut way of creating two correlated Wiener processes, and the traditional n-asset Cholesky decomposition are shown below,
Notice that the asset prices become more volatile as time goes by which results in a flaring out of potential asset prices towards the end of the projection. This phenomena occurs because I have set the long run average volatility to a number much higher than the starting volatility.
The Cox Ingersoll Ross (CIR) stochastic process is used to describe the evolution of interest rates over time. The CIR stochastic process was first introduced in 1985 by John Cox, Johnathan Ingersoll, and Stephen Ross. The CIR process is an extension of the Ornstein Uhlenbeck stochastic process. The stochastic process is often used in the valuation of interest rate derivatives and has been used in the Heston model to describe the evolution of volatility over time. One interesting characteristic of the CIR stochastic process is that it is mean-reverting. Mean reversion is the phenomena whereby a level tends to move towards the average level over time. In other words, if the average interest rate is 0.5%, and we simulate 500 independent CIR processes, we would expect the average interest rate at the end of the projection period to be approximately 0.5%. The stochastic differential equation for the CIR process is given by,
where is a Wiener process, is the rate at which the process mean reverts (a larger number results in a faster mean reverting process), is the long run average interest rate, and is the volatility of the process. The code for the CIR stochastic process is given below.
Notice that the when the interest rate has deviated to far from the long run average interest rate it tends to 'change directions' and approach the mean again. This is called mean-reversion and is specified by the term .
The Ornstein Uhlebneck stochastic process is another mean-reverting process which is sometimes used for modelling interest rates. The Ornstein Uhlenbeck process is named after Leonard Ornstein and George Eugene Uhlenbeck. The difference between the Ornstein Uhlenbeck stochastic process and the CIR process is that the CIR processes multiplies the stochastic component by the square root of the previous value for the interest rate. The stochastic differential equation for the Ornstein Uhlenbeck process is,
where is a Wiener process, is the rate at which the process mean reverts (a larger number results in a faster mean reverting process), is the long run average interest rate, and is the volatility of the process. The code for the Ornstein Uhlenbeck stochastic process is given below.
The biggest application of stochastic processes in quantitative finance is for derivatives pricing. This section will introduce the basic concepts behind derivatives and describe how stochastic processes can be used to price them numerically using closed form solutions such as the Black Scholes formula or using Monte Carlo methods.
A derivative is a contract which derives its value from the performance of some underlying security. Common underlyings include market indices, financial assets, interest rates, and volatility. Generally speaking derivatives are bought and sold either over the counter derivatives (OTC) or through an exchange or other intermediary. The price of an exchange traded derivatives is equal to the price they are being traded at in the market. Because over the counter derivatives are bought and sold directly between two parties, the fair price of the derivative must be computed using quantitative techniques.
When pricing over the counter derivatives most quants will use one of two approaches. Either they will solve (or find the solution to) the Black Scholes model for the derivative they are pricing or they will use a simulation method to approximate the value of the derivative. Both techniques rely heavily on the use of stochastic processes to model the underlying.
Derivative Pricing Approach One - Black Scholes
The Black Scholes model is used to price specific types of derivatives contracts under a set of assumptions. These assumptions include; (1) there exists a risk free rate, , at which any amount of money may be borrowed or lent, (2) the price of the underlying evolves according to the Geometric Brownian Motion stochastic process (discussed later on), (3) the underlying does not pay a dividend, (4) there are no arbitrage opportunities in the market, (5) the market is friction-less meaning that transaction costs are zero, and (6) it is possible to buy or short any amount of the underlying. For more information on crazy assumptions check out this article.
Under these assumptions the famous Black Scholes partial differential equation may be derived. For those interested in the step-by-step derivation of the Black Scholes partial differential equation please click here.
where is the price of the option as a function of the underlying price, , and time . is the risk-free interest rate, and is the volatility of the underlying. For those unfamiliar with the interpretation of partial differential functions, means the partial first order derivative (sensitivity) of with respect to and means the second order partial derivative of with respect to which is also the derivative of the first order partial derivative. We can break down each term as follows,
- is the partial derivative of the option price with respect to time a.k.a theta
- is the partial derivative of the option price with respect to the underlying a.k.a delta
- is the second order partial derivative of the option price with respect to the underlying a.k.a gamma
This formula asserts that the option price is twice differentiable with respect to the underlying, , and once with respect to time, . As such, if the payoff of the option is, for lack of a better word, weird, it may not be possible to use the Black Scholes partial differential equation. In which case you may have to resort to using approach two. The Black Scholes partial differential equation tells us is how the price of the derivative will evolve over time in relation to the underlying stochastic process. It doesn't yet tell us the price of the derivative. To arrive at the derivative pricing formula you would need to still solve the partial differential equation for a given derivative e.g. European call and put options.
From the Black Scholes partial differential equation the following formula for pricing European call options was derived. For those interested in the step-by-step derivation of the European call pricing formula click here.
The Black Scholes formula and the derivation of various closed form options pricing formulas from it are largely responsible for the massive growth in derivatives exchanges seen over the past three decades.
It is worth mentioning that 'partial derivative' may not be the best mathematical interpretation in the explanation of the Black Scholes formula because is a stochastic process and therefore non-differentiable by definition.
Derivative Pricing Approach Two - Simulation methods
Given the limitations and assumptions implied by the Black Scholes formula quants often resort to the use of Monte Carlo methods (simulations) in order to price more exotic derivatives or even vanilla derivatives under fewer simplifying assumptions. For example, you may wish to relax the simplifying assumption that the underlying follows a Geometric Brownian Motion in favour of an alternative stochastic process. In this example you have two available options,
- Either you derive the general partial differential equation for the alternative stochastic process and then still solve that partial differential equation for the derivative in question, or
- You model the derivatives payoff function and simulate it over multiple paths generated using the alternative stochastic process. The present value of the expected derivative payoff (as approximated using Monte Carlo methods) is equivalent to the discounted future value of the derivative. For more information see Monte Carlo methods for options pricing.
These two options present a trade off between computational complexity and time. It is more computationally complex to use simulation methods every time you want to price the derivative but it is more time consuming to derive the 'equivalent' of the Black Scholes partial differential equation for the alternative stochastic process and then still find the closed form derivatives pricing formula. For this reason most quants use simulation methods.
The reason why you would want to do this is illustrated in the graphs below. The fact of the matter is that how you choose and calibrate your stochastic process will have a big impact on the expected payoff of the option and therefore it's value.
The red oval shows where a jump in the market happened (mini-crash)
The pay-offs from the option deviate after the jump
The red oval shows how the price of the put is affected by the jump
Hedging using Derivatives
Hedging is a risk management strategy which aims to reduce the amount of hedgeable risk a portfolio is exposed to. Hedgeable risks include equity risk, interest rate risk, currency risk, credit risk, volatility risk, and commodity risk. Hedging is done by investing in assets which are negatively correlated to the underlyings in a portfolio. The simplest example of this is buying a put option on a stock. When the stock performs badly, the put performs well and the overall portfolio does not do as badly as it would have done had it not been hedged. The net effect is dampened returns up or down.
It is expensive and not always feasible to hedge every position in a portfolio. As such, companies and funds will try to identify the risk factors to which the portfolio is exposed and hedge against those. This is done by using quantitative risk models which look at the sensitivity of a portfolio to market changes. If those sensitivities are well understood it may be possible to buy derivatives to reduce the sensitivity of the portfolio. For example, given a macro balanced portfolio which is sensitive to volatility, interest rate changes, and currency fluctuations, a portfolio manager might be able to reduce the portfolios risk using swaps such as volatility swaps, interest rate swaps, and currency swaps.
That having been said, one must remember two things. Firstly, risk and return are inversely correlated meaning that the more risk the manager hedges away the smaller the returns should be. In fact, if all the risk factors the portfolio is exposed to he or she should expect the portfolio to return the "risk-free rate". For this reason, portfolio managers may choose to only hedge portions of their portfolio or hedge against specific risk factors. Secondly, this assumes that quantitative models are capable of accurately measuring the risks a portfolio is exposed to. The risk that the models are incorrect or incomplete is called model risk. Model risk can arise from uncertainty, time uncertainty, correlation uncertainty, market illiquidity, complexity and poor assumptions (like in the 2008 models used to price Collateralized Debt Obligations).
As you can see, hedging is certainly no silver bullet. In addition to the concerns I mentioned above there are a number of additional "real world" concerns. One example is the general cost and complexity of hedging programmes in general (hedging can be very expensive). For organization, the question becomes whether it is cheaper to hedge away risks or simply reserve more capital in the event of an adverse loss scenario. In recent years regulations such as Solvency II, and Basel III require banks, hedge funds, and insurers to reserve considerably more capital to back their portfolios. Reserves are typically kept in highly liquid securities with very low expected returns such as treasury bills.
Stochastic processes are useful for describing the random processes we find in the world around us. They are used in engineering, genetics, physics, and in quantitative finance. Quants use stochastic processes to project possible returns in markets and changes in interest rates over time. Stochastic processes are often used in conjunction with Monte Carlo methods to derive fair values for over the counter derivatives. Many stochastic processes exist which operate under different assumptions. Arguably the biggest challenge with stochastic processes is their calibration. Calibration is an optimization problem whereby the 'best' parameter values for a stochastic process are derived from historical returns. In the future I will write a follow up article about the different calibration techniques available to quants. If you have any questions or corrections please contact me.
Despite the fact that I use stochastic processes on an everyday basis I felt the need to clarify my somewhat confusing position as a non-supporter of both the random walk hypothesis and the efficient market hypothesis.
The random walk hypothesis is the financial theory which states that market prices evolve over time according to a random walk. This theory is consistent with the efficient market hypothesis which is widely accepted in academia. What many academic quants conclude from this theory is that because markets are random, prices are unpredictable and that beating the market is therefore a statistical impossibility.
My opinion is that even though prices are largely random this does not preclude the possibility that their randomness is impacted by external factors. In other words, certain factors may skew the distribution of returns towards favourable outcomes. Identifying these factors and their resulting edges is how investors are able to beat the market. Some factors used by quants include mean reversion, momentum, value, and macro factors.
For example, assume that a market drop of more than 3.5% is a factor which skews the resulting distribution of 52-week returns. In other words, if the markets drops by more than 3.5% we believe this will affect the probability of receiving a good return over the 52-weeks following the drop. If this hypothesis is supported one might use it to justify allocating a greater % of your portfolio into equities over the year than you would normally have done.
Once a hypothesis has been formed quantitative investors test it using statistical techniques. In our silly example we find that there have been 52 such market drops since 1950 in the S&P 500. Had we invested for 52 weeks after each drop our average return would have been 19.54% which is significantly above the average market return of 9.49%. In terms of quantitative asset allocation this means that following a 3.5% drop we should increase our equity exposure.
In all honesty this is a silly example, but it demonstrates that 'randomness' (the distribution of returns) is impacted by quantifiable factors. This has significant implications for investors. It means that if you can find factors which skew the distribution of returns in your favour for the markets, stocks, bonds, etc. that you are invested, you can beat the market. Quantitative investing is about finding statistically significant factors and betting on them.
In this way quantitative trading is similar to counting cards in a game of Blackjack (21). Even though the order in which pairs of cards are dealt from a shuffled deck is random and the odds are in favour of the house, if we count the number of high and the number of low cards we may identify times when we are at an advantage. At which point it makes sense to throw more chips down. In the market context that is the time to either invest more.
The key take-away from the this philosophical digression is that in the real world probabilities change and in the world of the random walk hypothesis they do not (usually they are quite normal - pun intended). So even though the market is still unpredictable, it is still possible to identify factors which skew the odds in your favour. This is the central tenet of quantitative investing. For strategies I recommend Quantocracy.com.
Lastly, I wanted to point out that all trading strategies fit into this model. Value investing asserts that companies which are trading at a discount to their intrinsic value have a higher probability of high returns. Momentum investing asserts that securities with positive momentum have a higher probability of high returns. Mean reversion investors assert that securities trading at a discount to their historical mean prices have a higher probability of high returns. All trading involves making some assertion about what factors drive returns.
The difference between quantitative investing and other forms of investing is that quants tend to make their hypotheses explicit and they focus on testing them more thoroughly than either fundamental, behavioral, or technical analysts. Anyway, this philosophical digression is my own personal opinion (which changes over time) so please feel free to agree or disagree in the comment section below.