Quantophile

Financial programming for Quants

Author: Quasar Chunawala

Cubic Splines Interpolation

cubic-splines-interpolation

A quadratic polynomial $p(x)=ax^2+bx+c$ has only three degrees of freedom \((a,b,c)\). If you want your quadratic function to run through two points, you already have only one degree of freedom left. If the the slope of the curve at one of the end-points is fixed, this uses up the last degree of freedom, leaving no choice of slope at the other end. A cubic polynomial \(ax^3+bx^2+cx+d\) has four degrees of freedom, thus allowing to prescribe four conditions – passing through two points and having specific slopes at the two end points.

Quadratic functions are more ringed. It’s because, a quadratic has a fixed degree of bentness. If we glue together a few quadratics, to interpolate a set of points, a passenger roller-coaster travelling on this curve would experience long ascents and descents. Cubics are more relaxed. Unlike cubics, quadratics can’t have a point of inflexion. Intuitively, this is why, cubic polynomials are used for interpolating a set of points.

However, quadratic splines also have many applications, for example, they are used while designing true type fonts.

Cubic splines are also use to construct cubic Bezier curves used in car designing.

I have put together the intuition, the math behind cubic splines and a python code snippet implementing the algorithm in this notebook.

The Vanna-Volga Method

This is part 1 of 4 posts that I would like to share on FX Volatility surfaces. The posts assume that the reader is familiar with the concept of volatility skew. There are many excellent references out there and I don’t want to repeat what you can already find in texts.

This part will focus on how FX traders build a volatility surface. Beginning with three data points \(25\Delta\) put(\(75\Delta\) call) volatility, at-the-money volatility and \(25\Delta\) call(\(75\Delta\) put) volatilities, we are to interpolate the curve. The curve should be arb free. Traders use a thumb-rule called the Vanna-Volga method.

VV is extremely intuitive. The idea is to construct a portfolio of \(1\) long call, short \(\Delta\) units of the stock and short the three calls (\(25\Delta\), \(50\Delta\) and \(75\Delta\)) in the proportion \((x_{1},x_{2},x_{3})\). For the portfolio to be arb-free, we set vega, volga and vanna to zero. This yields \((x_{1},x_{2},x_{3})\). The weights can be used to compute the vanna-volga adjustment. The market price of the option is simply the sum of the Black Scholes Price and the vanna-volga adjustment.

I could write a first order approximation of the adjustment as :

\(\text{Adjustment}= \text{Vega}\times(\text{IV} – \text{Flat volatility }\sigma_{atm})\)

And a second order one as :

\(\begin{aligned}
\text{Adjustment} &= \text{Vega}\times(\text{IV} – \text{Flat volatility }\sigma_{atm}) \\
&+ \text{Volga}\times(IV – \text{Flat volatility }\sigma_{atm})^{2}
\end{aligned}\)

Solving the above for IV, it’s easy to extract a first-order and second-order approximation.

This document outlines the Vanna-Volga method and a sample code snippet in Python. In my next post, I plan to write on other stochastic volatility models and their C++ implementation.

Gaussian Elimination

A system of linear equations can be solved by using Gaussian elimination. For example, I have three equations:

\(
\begin{matrix}
2u&+v&+w&=5\\
4u&-6v& &=-2 \\
-2u&+7v&+2w&=9
\end{matrix}\)

The problem is to find the unknown values of \(u\), \(v\) and \(w\) that satisfies the three equations.

Gaussian Elimination

I start by subtracting multiples of the first equation from the other equations.

The coefficient \(2\) is the first pivot. Elimination is constantly finding the right multiplier \(l\) by dividing the pivot into the members below it, so that one of the variables is eliminated from the equation. To eliminate \(u\) from \(4u-6v =-2\), I must subtract \(2(2u+v+w=5)\) from it. The multiplier \(l=4/2\). Similarly, to eliminate \(u\) from \(-2u+7v+2w=9\), the multiplier is \(l=-2/2=-1\).

So, the operations

1. Subtract \(2\) times equation 1 from equation 2.
2. Subtract \(-1\) times equation 1 from equation 3.

result in :

\(
\begin{matrix}
2u&+v&+w&=5\\
&-8v&-2w &=-12 \\
&+8v&+3w&=14
\end{matrix}\)

Continue reading

Conditional probability

Conditional probability has numerous applications in scientific, medical and legal reasoning, statistical genetics, cryptography etc. Whenever we observe new evidence or information, the odds of an event must be updated.

A motivating example.

Example. The Monty Hall problem.

On the game show Let’s Make a Deal, hosted by Monty Hall, there are three doors, randomly one of the doors has a car behind them, the other two doors have goats behind them. You as the contestant have no idea, which one has the car, they are all equally likely. But Monty Hall knows which one has the car.

You as a contestant, pick a door, say door \(1\). Then, what happens is, Monty opens up either of the remaining doors \(2\) or \(3\), revealing a goat. The door he opens always has a goat behind it (he never reveals the car!), for example, Monty opens goat door \(3\). So, then you know, that the car is behind the door you initially picked, door \(1\) or the other unopened door \(2\).

Monty then offers the contestant the option of switching to the other unopened door, or keeping your original choice. The question is, should you stay with with your initial choice or should you switch?

Assumption. Monty always opens a goat door. If he has a choice, of which door to open, he picks with equal probabilities.

For example, if you initially guessed right, so the car is behind the door \(1\), doors \(2\) and \(3\) both have goats. Monty could open either door \(2\) or door \(3\). Assume these are equally likely.

Should you stay with your initial choice or switch?

Many people, upon seeing the problem for the first time, argue that there is no advantage to switching : “There are two doors remaining, and one of them has the car, so the chances are 50-50”. Controversy has raged about this problem for years and years.

Solution.
Under the standard assumptions above, the answer is, you should switch. Let’s label the doors \(1\) through \(3\). We know that, any of the \(3\) doors are equally likely to be the car-door.

\(P(C_{1})=P(C_{2})=P(C_{3})=1/3\)

Without the loss of generality, we can assume that the contestant picked the door \(1\). Suppose that Monty opens door \(3\). Suppose we believe the car is behind door \(2\), denoted by event \(C_{2}\). The prior probability that the car is behind door \(2\), \(P(C_{2})=1/3\) can be updated in light of the new information about Monty opening door \(3\).

Bayes’s rule is used to update prior probabilities by incorporating new information.

\(\begin{align}
P(C_{2}|M_{3})&=P(C_{2})\cdot\frac{P(M_{3}|C_{2})}{P(M_{3})}\\
&=(1/3)\cdot\frac{1}{(1/2)}\\
&=2/3
\end{align}\)

And, the probability that the car is behind door \(1\) is,

\(P(C_{1}|M_{3})=1-P(C_{2}|M_{3})=1/3\)

It would be an abuse of the naive definition of probability, if you just immediately say, that the two doors are equally likely to have a car behind them. There is a \(1/3\)rd chance that the car is behind the first door, and a \(2/3\)rds chance it is behind the second door.

Thus, the observation that Monty opened door \(3\) makes us more sure of our belief that the car is behind door \(2\).

Building correct intuition

Let’s consider an extreme case. Suppose that there are a thousand doors, \(999\) of which contain goats and \(1\) of which has a car. After the contestant’s initial pick, Monty opens opens \(998\) doors with goats behind them. Let’s update our prior probabilities as Monty opens doors with goats behind them.

Assume, Monty opens door \(1000\).

\(
P(C_{2}|M_{1000})=P(C_{2})\frac{P(M_{1000}|C_{2})}{P(M_{1000})}=\frac{999}{998}\cdot\frac{1}{1000}
\)

Next, Monty opens door \(999\).

\(
P(C_{2}|M_{999}M_{1000})=P(C_{2}|M_{1000})\frac{P(M_{999}|C_{2}M_{1000})}{P(M_{999}|M_{1000})}=\frac{999}{997}\cdot\frac{1}{1000}
\)

Continuing in this fashion,

\(P(C_{2}|M_{3}\ldots{M_{1000}})=\frac{999}{1000}\)

In this extreme case, it becomes clear that the probabilities are not \(50-50\). As Monty eliminates \(998\) of the \(999\) doors, we are extremely confident of the belief that the car is behind the remaining unopened door.

R Simulation

A simple R simulation demonstrates that the strategy to always switch has a success probability of \(2/3\).

n <- 3

trial <- function(n,game_result_flag){
  
  doors <- 1:n
    
  # Contestant's initial choice
  choice <- sample(doors,1,replace=TRUE)
  
  # The car is behind one of n doors
  car_door <- sample(doors,1,replace=TRUE)
  
  # Monty knows the car door. He randomly opens the goat doors,
  # but it can't be the goat door or the player's door
  if(choice != car_door)
    remaining_doors <- doors[-c(choice,car_door)]
  else
    remaining_doors <- doors[-c(choice)]
  
  # After Monty has opened all of the goat doors, he offers the contestant
  # the option of switching to the other unopened door.
  switch_flag <- TRUE
  
  if(switch_flag == TRUE)
  {
    if(choice != car_door)
      game_result_flag <- TRUE
    else
      game_result_flag <- FALSE
  }else{
    if(choice == car_door)
      game_result_flag <- TRUE
    else
      game_result_flag <- FALSE
  }
}
 

The experiment is repeated \(1\) millon times.

nSim <- 10^6
list_of_results <- replicate(nSim,trial(n=3,game_result_flag = NULL))
games_won <- sum(list_of_results == TRUE)
games_lost <- nSim - games_won

The number of games won and lost are :

> games_won
[1] 667414
> games_lost
[1] 332586

The links to my notes and the solved problems are given below.

Conditioning and independence
Sequential Sampling
Solved examples

Probability and Counting methods

In a typical \(6/49\) lotto, where \(6\) numbers are drawn from a range of \(49\) and if the six numbers drawn match on the ticket, the ticket holder is a jackpot winner. The odds of this event are \(1\) in \(13,983,816\).

The study of counting methods dates at least to Gottfried Wilhelm Leibniz’s Dissertatio de Arte Combinatoria in the seventeenth century. Combinatorics has applications in many fields of study. Applications of combinatorics arise, for example in chemistry, in studying the arrangements of atoms in molecules and crystals; biology in questions about the structure of genes and proteins; physics, in problems in statistical mechanics; communication, in the design of codes for encryption, compression, and especially in computer science, for instance in problems of scheduling and allocating resources.

The links to my notes on how to count and the solved problems are given below.

Theorems and notes
Exercises and examples
More solved problems

Binomial trees


The random behavior of financial variables such as stock prices, interest rates, FX rates, credit spreads can be approximated using two-state lattices known as Binomial trees. Binomial trees are useful for a variety of European-style and American-style derivatives.

Introduction

Consider an asset with an initial value \(S_{0}\) at time \(t=0\). During a time period \(\Delta{t}\), the asset price can go up to \(Su\) with probability \(p\) or down to \(Sd\) with probability \(q=1-p\). The asset price \(S\) is a Bernoulli random variable.

Continue reading

Monte Carlo Methods

A beautiful rendering of the Bugatti Veyron with 2 million polygons worth of information using ray-tracing! The science behind ray-tracing is to use the Monte-carlo methods to simulate the random paths of light through any given pixel and create a 3D-image.

Simulations are central to finance and risk management. They allow us to price complex financial instruments e.g. European style options for which no analytic pricing formula is available. They allow risk managers to simulate a portfolio’s profit and loss performance for a specified time horizon. Repeated trials within the simulation produce a frequency distribution for the changes in the portfolio value. The cutoff point beyond which there is very low probability of greater losses is an estimate of VAR.

Continue reading

© 2019 Quantophile

Theme by Anders NorenUp ↑