Guest User

Untitled

a guest
Dec 4th, 2019
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. np.set_printoptions(formatter={'float_kind':lambda x:'%5.2f'%x})
  3. x0 = 50
  4. K = 50
  5. sigma = 0.4
  6. T = 5/12
  7. r = 0.1
  8. N = 5
  9. dt = T/N
  10. u = np.exp(sigma*np.sqrt(dt))
  11. d = 1 / u
  12. q = (np.exp(r*dt)-d) / (u - d)
  13. discount = np.exp(-r*dt)
  14. tree = np.empty((N+1,N+1))
  15. tree[:] = np.nan # for easier debugging
  16. def x(i,j):
  17.     return x0 * u**j * d**(i)
  18. temp = tree.copy()
  19. for i in range(N+1):
  20.     for j in range(N+1):
  21.         temp[i,j] = x(i,j)
  22. temp
  23. array([[50.00, 56.12, 62.99, 70.70, 79.35, 89.07],
  24.        [44.55, 50.00, 56.12, 62.99, 70.70, 79.35],
  25.        [39.69, 44.55, 50.00, 56.12, 62.99, 70.70],
  26.        [35.36, 39.69, 44.55, 50.00, 56.12, 62.99],
  27.        [31.50, 35.36, 39.69, 44.55, 50.00, 56.12],
  28.        [28.07, 31.50, 35.36, 39.69, 44.55, 50.00]])
  29. def prettyPrint(tree):
  30.     newtree = np.empty((tree.shape[0],2*N+1))
  31.     newtree[:]=np.nan
  32.     #print(newtree)
  33.     for i in range(tree.shape[0]):
  34.         for j in range(i+1):
  35.             newtree[i,N+(2*j-i)-i] = tree[i,j]
  36.     print(newtree)
  37. prettyPrint(temp)
  38. [[  nan   nan   nan   nan   nan 50.00   nan   nan   nan   nan   nan]
  39.  [  nan   nan   nan 44.55   nan 50.00   nan   nan   nan   nan   nan]
  40.  [  nan 39.69   nan 44.55   nan 50.00   nan   nan   nan   nan   nan]
  41.  [  nan 39.69   nan 44.55   nan 50.00   nan   nan   nan   nan 35.36]
  42.  [  nan 39.69   nan 44.55   nan 50.00   nan   nan 31.50   nan 35.36]
  43.  [  nan 39.69   nan 44.55   nan 50.00 28.07   nan 31.50   nan 35.36]]
RAW Paste Data