In [10]:
# One period binomial option pricing

from math import *
import numpy as np

u = 1.05
d = 0.98
r = 0.01 
T = 1
So = 100
K = 100

# produce the stock price vector at the nth period where the 0th period has one price So

def stock(So, u, d, n):
    s = np.zeros((n+1,1))
    for i in range(n+1):
            s[i] = So*(d**i)*(u**(n-i))
    return s

S = stock(So,u,d,1)
print("The stock price vector at period 1 is \n", S)    
print("...\n\n")
# produce the risk neutral probability vector at the nth period 

def risk_neutral_prob(u, d, r, T, n):
    delta_T = T/n
    q = (exp(r*delta_T) - d)/(u-d)
    Q = np.zeros((n+1,1))
    for i in range(n+1):
            Q[i] = ((1-q)**i)*(q**(n-i))
            if Q[i] < 0 or Q[i] > 1 :
                print("Arbitrage condition not satisfied !")
                return np.zeros((n+1,1))
    return Q

Q = risk_neutral_prob(u, d, r, T, 1)
# produce the call option payoff function with strike K at the nth period

def Euro_Call(S_o, u, d, K, n):
    S = stock(So, u,d, n)
    C = np.zeros((n+1,1))
    for i in range(n+1):
        C[i] = max(S[i]-K, 0)
    return C

# produce the put option payoff function with strike K at the nth period

def Euro_Put(S_o, u, d, K, n):
    S = stock(So, u,d, n)
    P = np.zeros((n+1,1))
    for i in range(n+1):
        P[i] = max(K - S[i], 0)
    return P

# produce the butterfly spread payoff function with strike K at the nth period

def Butterfly(S_o, u, d, K1, K2, K3, n):
    S = stock(So, u,d, n)
    B = np.zeros((n+1,1))
    for i in range(n+1):
        if K1 <= S[i] < K2:
            B[i]= S[i] - K1
        if K2 <= S[i] < K3:
            B[i] = K3 - S[i]
    return B

# produce the butterfly spread payoff function with strike K at the nth period

def Strangle(S_o, u, d, K1, K2, n):
    S = stock(So, u,d, n)
    Strangle = np.zeros((n+1,1))
    for i in range(n+1):
        if S[i] <= K1:
            Strangle[i]=  K1 - S[i]
        if S[i] >= K2:
            Strangle[i] = S[i] - K2
    return Strangle

# risk neutral pricing for an option with a given pay_off function with expiry T in a n period model

def risk_neutral_pricing(So, u, d, n, T, pay_off):
    v = 0
    S = stock(So, u,d, n)
    Q = risk_neutral_prob(u,d,r,T,n)
    delta_T = T/n
    for i in range(n+1):
        v += exp(-r*delta_T)* pay_off[i]*Q[i]
    return v

#Only produce output if no arbitrage condition is satisfied
if Q.any() :
    
    print("The risk neutral probability vector at period 1 is \n", Q)    
    print("...\n\n")

    call_pay_off = Euro_Call(So, u, d, K, 1)
    print("The payoff of Euro call in the one period model with strike ", K, " is \n", call_pay_off)   
    print("...\n\n")
    
    put_pay_off = Euro_Put(So, u, d, K, 1)
    print("The payoff of Euro put in the one period model with strike ", K, " is \n", put_pay_off)   
    print("...\n\n")
    
    butterfly_pay_off = Butterfly(So, u, d, 90, 100, 110, 1)
    print("The payoff of butterfly spread in the one period model with strikes 90, 100, 110 is \n", butterfly_pay_off)   
    print("...\n\n")
    
    strangle_pay_off = Strangle(So, u, d, 90, 110, 1)
    print("The payoff of the strangle in the one period model with strikes 90, 110 is \n", strangle_pay_off)   
    print("...\n\n")
    
    call_0 = risk_neutral_pricing(So, u, d, 1, 1, call_pay_off)
    print("The price of Euro call in the one period model with strike ", K, " is \n", call_0)  
    print("...\n\n")
    
    put_0 = risk_neutral_pricing(So, u, d, 1, 1, put_pay_off)
    print("The price of Euro put in the one period model with strike ", K, " is \n", put_0)  
    print("...\n\n")
    
    butterfly_0 = risk_neutral_pricing(So, u, d, 1, 1, butterfly_pay_off)
    print("The price of butterfly spread in the one period model with strikes 90, 100, 110 is \n", butterfly_0)   
    print("...\n\n")
    
    strangle_0 = risk_neutral_pricing(So, u, d, 1, 1, strangle_pay_off)
    print("The price of the strangle in the one period model with strikes 90, 110 is \n", strangle_0)   
    print("...\n\n")
The stock price vector at period 1 is 
 [[105.]
 [ 98.]]
...


The risk neutral probability vector at period 1 is 
 [[0.4292881]
 [0.5707119]]
...


The payoff of Euro call in the one period model with strike  100  is 
 [[5.]
 [0.]]
...


The payoff of Euro put in the one period model with strike  100  is 
 [[0.]
 [2.]]
...


The payoff of butterfly spread in the one period model with strikes 90, 100, 110 is 
 [[5.]
 [8.]]
...


The payoff of the strangle in the one period model with strikes 90, 110 is 
 [[0.]
 [0.]]
...


The price of Euro call in the one period model with strike  100  is 
 [2.12508307]
...


The price of Euro put in the one period model with strike  100  is 
 [1.13006644]
...


The price of butterfly spread in the one period model with strikes 90, 100, 110 is 
 [6.64534883]
...


The price of the strangle in the one period model with strikes 90, 110 is 
 [0.]
...


In [ ]:
 
In [ ]: