<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import warnings
import matplotlib.pyplot as plt


from google.colab import drive
drive.mount('/content/drive')
df = pd.read_csv("/content/drive/MyDrive/Test/BinomialOptions.csv")
df.head()
df.columns
# Initialise parameters
S0 = UnderlyingPrice # initial stock price /1/
K = StrikePrice # strike price /2/
T = ExpiryDate - Date # time to maturity in years
r = RiskFreeInterestRate # annual risk-free rate
N = 5 # number of timesteps
# u =  # up-factor in binomial models
# d = 1/u # ensure recombining tree
sigma = ImpliedVolatility #Annualised stock price volatility
opttype = PullCall # Option Type 'C' or 'P'<jupyter_output><empty_output><jupyter_text>Binomial Tree Slow<jupyter_code>def CRR_method(K,T,S0,r,N,sigma,opttype='PullCall'):
    #precomute constants
    dt = T/N
    u = np.exp(sigma*np.sqrt(dt))
    d = 1/u
    q = (np.exp(r*dt) - d) / (u-d)
    disc = np.exp(-r*dt)

    #initials asset prices at maturity - Time spend N
    S = np.zeros(N+1)
    S[0] = S0*d**N
    for j in range(1,N+1):
      S[j] = S[j-1]*u/d

    # initialise option values at maturity///
    C = np.zeros(N+1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    for j in range(0,N+1):
      C[j] = max(0,S[j]-K)

    # step backwars thourgh tree
    for i in np.arange(N,0,-1):
      for j in range(0,i):
        C[j] = disc * ( q*C[j+1] + (1-q)*C[j] )

    return C[0]

CRR_method(K,T,S0,r,N,sigma,opttype='C')<jupyter_output><empty_output><jupyter_text>Binomial Tree Fast<jupyter_code>def binomial_tree_fast(K,T,S0,r,N,u,d,opttype='C'):
    #precomute constants
    dt = T/N
    q = (np.exp(r*dt) - d) / (u-d)
    disc = np.exp(-r*dt)

    #initials asset prices at maturity - Time spend N
    C = S0 * d ** (np.arange(N,-1,-1)) * u ** (np.arange(0,N+1,1))

    # initialise option values at maturity
    C = np.maximum( C - K, np.zeros(N+1) )

    # step backwars thourgh tree
    for i in np.arange(N,0,-1):
        C = disc * ( q * C[1:i+1] + (1-q) * C[0:i] )

    return C[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09046664758087604, 'fraction_numerical': 0.011165187517892928, 'mean_word_length': 0.8047520661157025, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '199165', 'license': 'MIT', 'n_tokens_mistral': 870, 'n_tokens_neox': 830, 'n_words': 197, 'path': 'Europeancalulator.ipynb', 'repo_name': 'Kamohelo01/github-slideshow', 'size': '3493'}