Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Backward Calculation
#1
Hello All,

I am trying to code in Python the CRR Binomial Model for Option Pricing as part of a greater assignment.
I need to perform n-time a backward calculation on a matrix. Here is my code:
def CRR(S,K,r, vol, t, typ, n):
    
    dt = t/n
    up = np.exp(vol*np.sqrt(dt))
    down = np.exp(-vol*np.sqrt(dt))
    p = (np.exp(r*dt)-down)/(up-down)

#Stock price tree
    stock_price = np.zeros([n+1,n+1])
    stock_price[0,0] = S
    for i in range(1,n+1):
        stock_price[i,0] = stock_price[i-1,0]*up
        for j in range(1,i+1):
            stock_price[i,j] = stock_price[i-1,j-1]*down
    

#payoff tree of the option
    payoff = np.zeros([n+1,n+1])
    if typ == "Call":
        for i in range(0,n+1):
            payoff[i,:] =  np.maximum(stock_price[i,:] - K,0)
    elif typ == "Put":
        for i in range(0,n+1):
            payoff[i,:] =  np.maximum(np.minimum(K - stock_price[i,:], stock_price[i,:]),0)
    print(payoff) 
    

#Compute the expected value 
    expected_value = np.zeros([n+1,n+1])
    for k in range(n,0,-1):
        print("k=",k)           #this is for personal help
        print(expected_value)   #this is for personal help
        for i in range(k,0,-1):
            print("i=",i)       #this is for personal help
            print(expected_value) #this is for personal help
            for j in range(k,0,-1):
                print("j=",j) #this is for personal help
                expected_value[i-1,j-1] = (p*payoff[i,j-1]+(1-p)*payoff[i,j])*np.exp(-r*dt)
                print(expected_value)  #this is for personal help
    print(expected_value)   #this is for personal help
    
    

CRR(100,100,0.06,0.2,0.5,"Call", 3)
My problem is on #Compute the expected value part. I do the calculation through i and j for loop and i need to do it k time to get a matrix with only one value in it in [0,0] position.
I have no error, but the process is only working the first time.

Thanks a lot for your help, and please not that I am rather new a Python :)

Quentin
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020