Wednesday, November 28, 2012

Monday, November 26, 2012

How to derive the Black-Scholes PDE

The Black Scholes equation of course is one of the most important equations for financial engineering. However it is not so straight forward, so memorizing it is a bit difficult. So today I decide to derive it from scratch.

The equation goes like this:

$$ \frac{\partial V}{\partial t} + \frac12 \sigma^2 S^2 \frac{\partial^2 V }{\partial S^2} + (r - d) S \frac{\partial V}{\partial S} - rV =  0 $$

Let's get started.

1. the stochastic differential equation that drives stock price: $ dS = S (r dt + \sigma dW) $
It's a bit not precise to say 'drive' since the mechanism behind stock price is like a black box. But anyway this is not the point. Start from here we have:
$$ dS^2 = S^2 ( r dt + \sigma dW)^2 = S^2 ( \sigma^2 dt + 2 r \sigma dt dW + r^2 dt^2) $$
By Ito's lemma, both $ dt dW $ term and $ dt^2 $ terms goes small compared to $ dt $.
So : $ d S^2 = S^2 \sigma^2 dt^2 $

2. Using Taylor's expansion, $ d F = \frac{dF}{dS} dS + \frac12 \frac{d^2 F}{dS^2} dS^2 $
So for an option whose price is a function of stock price, we have:
$$ d V = \frac{dV}{dS} dS + \frac12 \frac{d^2 V}{dS^2} dS^2 $$
Simplify it:
$$ d V = \Delta ( r dt + \sigma dW) + \frac12 \Gamma (S^2 \sigma^2 dt^2) $$
Where $\Delta$ and $\Gamma$ are the hedge ratio and the greeks relatively.

3. A portfolio of one option and $\Delta$ shares of stock. $\Pi = V - \Delta S $.
Since there is no arbitrage ( what a fantasy ),
 $$d \Pi = \Pi r dt $$
Expand this:
$$ dV - \Delta dS = (V - \Delta S) r dt $$
Collect the terms and make some replacement:

$$ \frac{\partial V}{\partial t} + \frac12 \sigma^2 S^2 \frac{\partial^2 V }{\partial S^2} + (r ) S \frac{\partial V}{\partial S} - rV =  0 $$

Yeah, that's it.

Sunday, October 21, 2012

Memo from computational finance assignment 4

Aim: to price European style Asian call option. The code must be able to do these few stuffs:

  1. Sampling dimension = 1, which makes the option back to vanilla.
  2. Sampling dimension = 12, $ 0 = t_{0}<t_{1}<t_{2}<...<t_{d-1}<t_{d}=T $, $ t_{i+1} - t_{i} = \frac{T}{12} $.
  3. Geometric or Arithmetic.

To make things easy, I will split up a single class to three classes.

The main class: EuroCallOption


  • This class deals with basic properties of the option.
    • Spot
    • Strike
    • InterestRate
    • DividendRate
    • Volatility
    • ExpirationDate
  • And some settings
    • nSAMPLE---- how many samples to generate when pricing this option in Monte Carlo manner
    • nSTEP---- how many steps are there in a single random walk. ( This is not the sampling dimension, we can have 1,000 steps in a sample path while we only take the end point of this random walk into account.)
    • Dimension---- this is the sampling dimension. Increasing nSTEP could lower the bias from discretization of the stochastic process. Increasing Dimension would only change the nature of this option.
    • Geometric or Arithmetic---- To decide which method to use when calculating the payoff.
And two assisting class:

PayOff

  • This class deals with the calculation of payoff given the random walk paths
    • price
  • And it will rely on another class's output----BrownianMotion
  • Settings that PayOff reads from EuroCallOption
    • Dimension
    • Geo or Arith

BrownianMotion

  • This class would generate sample paths according to settings.
    • Allow stratified sampling along $W(t_{d})$.
    • Allow use of Park-Miller random number generator
    • Allow use of Sobol sequence generator
  • And the settings that should be forwarded to this class when calling its functions include:
    • nSAMPLE
    • nSTEP
In short, I will split the pricing process into this 'SOP'
1, make an object call EuroCallOption
2, make a BrownianMotion based on EuroCallOption's setting
    use this BM to generate random path according to requirements
3, make a PayOff class to read BM's sample paths, and calculate the price of this option according to requirements.

OK, let't get down to details:
1, how to stratify the sampling along the ending points $W(t_{d})$
2, how to generate the Brownian bridge based on known $W(t_{d})$

The first question:
Stratified sampling is to sample proportionally to the probability density function. Say if we draw 1,000 samples, of course we don't want all 1,000 ones to be extreme values. The most ideal scenario should be that about 600 ones are located within $[-1\sigma, 1\sigma]$, and 999 located inside $[-3\sigma, 3\sigma]$. And with stratified sampling, we are actually controlling how many samples to sample from each part of the whole distribution. So the sample distribution is much like the theoretical distribution.

1, generate stratified ending point. (I accomplished this by writing a STRATIFY.m)
2, generate intermediate values of each path based on conditional distribution ( working on it)

It's best that if we pack $2^{m}$ steps in a path. But if not, doesn't matter...

Relax and have some life

Spent a sunny afternoon in the central park wandering with my camera. Wish you all love my pics~








Saturday, October 13, 2012

Computational Finance review note

OK, I will be updating this post as if it's a live blog

For vanilla call option pricing using simple MC method

    the stochastic process that drives stock price:

$$ \frac{dS_{t}}{S_{t}} = r dt + \sigma dW_{t}$$

    If we are to produce the final stock price with one step:

$$ \log S_{T} =\log S_{0}+(1+r-\frac{1}{2}\sigma^{2})T + \sigma W_{T}$$

where  $ W_{T} = \sqrt{T}*N(0,1)$

For each S(T), the payoff function is  $(S_{T}-K)^{+}$

So use a random generator to draw a big-enough set of normal random numbers and derive the final stock prices, the MonteCarlo estimator for the call option is:

$$ \frac{1}{N} \sum_{i=1}^{N} (S_{T(i)}-K)^{+} $$

And this is the most simple one. Convergence is slow. Variance is high. And one key point: if the option is path-dependent, say a knock-out or an Asian, generating final stock prices this way will surely fail the calculation.

    So we want something more finer tuned.It's a good idea to generate the random path step-by-step. Say to generate a one-year stock price random path by 252 steps, or even more.There are some ways to do this kind of finite difference :
For the SDE:
$$ dS(t) = a(t, S(t)) dt + b(t, S(t)) dW(t), t\geq 0 $$
Euler scheme: 
$$ S(t+dt) = S(t)*((1+a*dt)+ b*(W(t+dt)-W(t))) $$
This would only simulate the stochastic process up to order 1 of the taylor expansion. The effect should not be good.
Milstein scheme:
$ S(t+dt) = S(t)*((1+a*dt) + b*(W(t+dt)-W(t))) + \frac{1}{2}*b*\frac{\partial b}{\partial S(t)} *[(W(t+dt)-W(t))^{2}-(dt)] $

Looks nicer, but there is a major disadvantage, if S(t) is a $ R^{d} $ valued process with $d\geq 2$, the $b(t,S)$ could be hard to be differentiable with respect to S.

However, the Milstein scheme has a higher order of convergence compared to Euler scheme.

Sorry I have to update this post later this week given I have another two mid-terms coming up....

See you guys



Friday, October 12, 2012

Taking my first exam in America

3 hours ago I just completed my first exam since arriving in USA.
IEOR 4721, Global Capital Market.It wasn't quite hard, but required tremendous calculation.

Not only couldn't I keep any paper including question sheet or 'blue book'(answer sheet in Columbia style), but also had to leave my cheat sheet to instructors... OK, so I have no souvenir now.

Oh no. As I am typing this blog I realized I made a mistake....T^T

 
 

Wednesday, October 10, 2012

A brief introduction on how to write a simple MATLAB OOP script

Few days ago I completed a programming assignment on Monte Carlo simulation. Because I was required to compare time consumptions of different variance reduction methods, and by that time I wasn’t sure if I could write equivalently efficient codes for 4 methods, I chose my old friend MATLAB. Here is an easy guide for how to write an OOP script in MATLAB.
OOP is short for Object-Oriented programming. Instead of defining separate functions and processes, we define objects that both store properties(data) to compute and functions(methods) to deal with the data, or to realize certain functionalities. Say we want to define an object for Up-and-out barrier call options, let’s list all of them first:
Option:
properties: (things that define the option)
spot price, strike price, upper barrier, interest rate, dividend rate, volatility, expiration date.

methods:
price the option by Closed-form formula
price the option by simple Monte Carlo simulation
price the option with Monte Carlo simulation * antithetic variance reduction
price the option with MC simulation * control variates

So this is the structure we would like to define. For each of the methods, we would like MATLAB to look inside the *option* object and take out what is needed, compute the price and display the result.
First of all:
use this template to build up a basic framework:


classdef <objectName> < handle
properties (SetAccess = public)
        <anything you would like to add>
yes, anything you would like to be included in the *class*. it can be double, integer, vector or anything. Keep in mind that you just need to put a name there first and don’t specify the form.
end

methods
        function obj = <objectName>(external parameters)
        the first function usually shares the same name with the object, and it’s exactly the constructor function. So at least one external parameter is need.
        function A(obj)
        end
        function B(obj, external parameter)
        end
        function disp(obj)
        and yes, you can overload some frequently-used functions specifically for your object
        end
end
end
---------------------------------------------------------------------
That’s it. Replace the <objectName> with exact and meaningful string, save the file as <objectName>.m. And you can call the functions or do anything you like with the object.
Let’s do a simple example. Define a class with one 5*5 matrix, and one double. There is one method (besides the constructor) inside the class, each time we call this method, 5*5 matrix would be filled with random number, and the double will be the biggest one of the matrix entries.

classdef example < handle
properties
        matrix5_5
        double1
end
methods
        function obj = example(void)
                obj.matrix5_5 = zeros(5,5);
                obj.double1 = 0;   % see how we cite the value inside?
        end
        function randomAndPick(obj)
                obj.matrix5_5 = randn(5,5)
                obj.double1 = max(max(randn)
        end
end
end
---------------------------------------------------
save it with filename = ‘example.m’
run in main window:
test1 = example();
test1.randomAndPick

Here I attach one of my code. It’s not that well written, but works.
BarrierOption2.m

Jiaming

Test drive

Since I am gonna post some mathematical things up here. I am now trying the MathJax...

Please ignore this post..Sorry for the inconvenience.


OK, it works now

$$\theta$$
$$dS_{t} = rS_{t}dt + \sigma S_{t}dW_{t}$$


Thanks! Andy!
Irreducible representation


——————————————————————————————
And now I am adding some other stuffs. For example, the code highlighting using prettyPrint

<pre class="prettyprint">
 int i;
</pre>
 



Starting a new blog

Hi, this is Jiaming in New York city.

I can't really recall when I first started writing a blog when I was in middle school and paused blogging after myOpera was down. Posting something publicly, it's like a natural filter to find someone who has commons with you.

So I would like to do that again. Not on social network services like Facebook or Renren, they're just way too noisy for words and articles. Not on twitter and weibo since I can't even complete a sentence sometimes. I am back in the traditional and old-school way of blogging, on Blogspot.

Visiting blogspot seems difficult for most of my friends now, but it's OK and will get better.


Welcome to my blog.


Best regards

Jiaming Kong @ the Butler library