Coding towards CFA (8) – Options Pricing with Multi-period Binomial Model

Coding towards CFA (8) – Options Pricing with Multi-period Binomial Model

*More articles can be found in my blog site: https://dataninjago.com

With the foundational concepts introduced in the previous blog post on the one-period binomial model, along with the path tracing and backward induction covered in the post on the two-period binomial model, we can now move to the multi-period binomial model, which is applicable to real-world scenarios.

Once you understand how the one-period and two-period binomial models function, grasping the multi-period model becomes straightforward. The application of the multi-period model will then be a programming challenge: writing code that can efficiently handle a configurable number of periods. In this blog post, we will guide you through the code implementation step by step.

Similar to the one-period and two-period models, the multi-period model follows the same steps for implementation. However, unlike the one-period and two-period models, where the periods are fixed and the calculations for each period are hard-coded, the multi-period model requires a different data structure and approach to generalise the processing.

  • Calculate the up and down factors
  • Calculate the up and down probabilities
  • Calculate underlying prices at expiration
  • Calculate the option payoffs at expiration
  • Backward induction of option value at the initiation

Step 1 & 2 – Calculate the up and down factors and probabilities

Same with the one-period binomial model. Please refer to the blog post?for one period binomial model.

Step 3 – Calculate underlying prices at expiration

In the one-period and two-period model implementations, we hard-coded the paths and calculations of the underlying prices. To support a variable number of periods, which results in an uncertain number of possible underlying prices at expiration, we can define an array or list to store the calculated underlying prices at expiration. These prices are determined by multiplying the initial underlying price, S(0), by the price-evolving steps along the paths that lead to these prices.

Step 4 – Calculate the option payoffs at expiration

Once we have the underlying prices at expiration, we can calculate the payoff related to each price and store the results in a list.

Step 5 – Backward induction of option value at the initiation

Once we have the option payoffs at expiration, we can then work backwards to calculate the option price at each period by taking the discounted expected payoffs of the option at the next period.

The discounting algorithm of the multi-period model is exactly the same as that of the one-period and two-period models.

However, since the periods are unfixed, the backward induction process is implemented with a nested loop, where the inner loop discounts option payoffs for one period, and the outer loop repeats the process for all the periods.

Full Code – Python

import math
 
def calculate_up_down_factor(time_per_period, sigma):
 
    u = math.exp(sigma * math.sqrt(time_per_period))
    d = math.exp(-sigma * math.sqrt(time_per_period))
 
    return u, d
 
 
def calculate_up_down_probability(u, d, r, time_per_period):
     
    Pi = (math.exp(r*time_per_period)-d)/(u-d)
 
    return Pi, 1-Pi
 
 
def binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="call"):
 
    time_per_period = T/number_of_periods
     
    # Calculate the up and down factors
    u, d = calculate_up_down_factor(time_per_period, sigma)
     
    # Calculate the up and down probabilities
    Pi_up, Pi_down = calculate_up_down_probability(u, d, r, time_per_period)
     
    # calculate underlying prices at expiration
    stock_prices = [0] * (number_of_periods+1)
    for i in range(number_of_periods+1):
        stock_prices[i] = s0*(u**i)*(d**(number_of_periods-i))
 
    # calculate the option payoff at expiration
    payoffs = [0] * (number_of_periods+1)
    for i in range(number_of_periods+1):
        if option_type == "call":
            payoffs[i] = max(stock_prices[i] - k, 0)
        else:
            payoffs[i] = max(k - stock_prices[i], 0)
     
    # Backward induction of option value at the initiation
    discount_factor = 1/math.exp(r*time_per_period)
    for i in range(number_of_periods-1, -1, -1):
        for j in range(i+1):
            payoffs[j] = (Pi_up*payoffs[j+1]+Pi_down*payoffs[j])*discount_factor
     
    option_price = payoffs[0]
 
    return option_price
 
 
s0 = 100
k = 100
T = 1
r = 0.05
sigma = 0.2
number_of_periods = 100
 
call_price = binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="call")
print("the call option price is: " + str(call_price))
 
put_price = binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="put")
print("the put option price is: " + str(put_price))        

Full Code – DolphinDB

def calculate_up_down_factor(time_per_period, sigma){
 
    u = exp(sigma * sqrt(time_per_period))
    d = exp(-sigma * sqrt(time_per_period))
 
    return u, d
}
 
def calculate_up_down_probability(u, d, r, time_per_period){
     
    Pi = (exp(r*time_per_period)-d)/(u-d)
 
    return Pi, 1-Pi
}
 
def binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="call"){
 
    time_per_period = T/number_of_periods
     
    // Calculate the up and down factors
    u, d = calculate_up_down_factor(time_per_period, sigma)
     
    // calculate the up and down probabilities
    Pi_up, Pi_down = calculate_up_down_probability(u, d, r, time_per_period)
     
    // calculate underlying prices at expiratoin
    stock_prices = each(def(i): s0*pow(u, i)*pow(d, (number_of_periods-i)),
                0..number_of_periods)
 
    // calculate the option payoff at expiration
    if (option_type == "call"){
        payoffs= each(def(i): max(stock_prices[i] - k, 0), 0..number_of_periods)
    } else {
        payoffs= each(def(i): max(k - stock_prices[i], 0), 0..number_of_periods)
    }
     
    // Backward induction of option value at the initiation
    discount_factor = 1/exp(r*time_per_period)
    for (i in (number_of_periods-1)..0 ){
        for (j in 0..i){
            payoffs[j] = (Pi_up*payoffs[j+1]+Pi_down*payoffs[j])*discount_factor
        }
    }
     
    option_price = payoffs[0]
 
    return option_price
}
 
s0 = 100.0
k = 100.0
T = 1.0
r = 0.05
sigma = 0.2
number_of_periods = 100
 
call_price = binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="call")
print("the option price is: " + call_price)
 
put_price = binomial_tree_m_period(s0, k, T, r, sigma, number_of_periods, option_type="put")
print("the option price is: " + put_price)        

要查看或添加评论,请登录

Linxiao Ma的更多文章

社区洞察

其他会员也浏览了