Timelessness - 015 - Pure, Functional, Financial, Python Katas

The truth is, with all these exercises we are trying to build something timeless.

Pure functions, or Python's lambdas (for the most part) ensure our programs don't evolve, but remain timeless, like a set of mathematical equations.

This is important when studying systems.

Systems in equilibrium do not evolve over time, once frozen we can study them, we can experiment with them repeatedly, they become like coin flips, they lack any memory.

We can flip them over and over until we understand them completely.

Which is exactly how we study complicated derivatives, by casting a forgetfulness spell over our subject.


Let's pick up from the previous kata.

We had a stock which could go up or down. This time we will simplify it to jumping to 101 or 99 over a single period and is currently priced at 100 dollars.

U_T = lambda: 101.  
D_T = lambda: 99.  
S_0 = lambda: 100.  

Now, model a call option on this stock. The stock option has a strike price of 100.

The code looks something like this.

K = 100.  
C = lambda S_T, K, t: max(S_T - K, 0.)  

When the stock price jumps up, we see a call price of 1 ( max( 101-100, 0) ).

When it drops, our call will be worth 0 ( max( 1-100, 0) ).

But we want a timeless value, not just a value at expiry.

We want a portfolio which remains at the same value no matter what.

We will add a delta hedge which will magically adjust as the call option changes value, so that the portfolio's overallvalue will remain constant after an up or down jump.

In pseudo code, after our up-jump, our delta hedged portfolio is,

Portfolio_U = C_U - S_0 * U * delta

where U is the percentage change and C_U the value of the call option after the jump up, i.e. 1% and 1.

The same logic applies to the down-jump,

Portfolio_D = C_D - S_0 * D * delta

Delta is the unknown ingredient to our timeless spell.

We do know however, that delta equalises our outcomes,

Portfolio_U = Portfolio_D

In other words, through stock ups and downs our portfolio remains constant.

If we we solve for delta, it becomes,

DELTA = (C_U - C_D) / (S_0 * (U - D))

And upon taking a second look, we can see something interesting,

DELTA = (C_U - C_D) / (U_T - D_T)

which is a geometrical slope formula between call option and stock prices.

Delta is the door between both stock and call option worlds.

The door is not fully open yet however, we will need more 'greeks' to explain the interaction between both worlds - and keep our portfolio hedged and 'timeless'.

Today's kata is to code up the equations presented here, and fiddle with them.