Advertisement
gl0Ppy

Untitled

Sep 6th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.57 KB | None | 0 0
  1. %% price testing thing
  2. N = 3;
  3. T = 1;
  4. dT = T/N;
  5. u = exp(sigma*sqrt(dT)); %p85
  6. d = exp(-sigma*sqrt(dT)); %p85
  7. q = (exp_r^dT - d)/(u-d);
  8. pricemat = pricematfun(S0, u, d, N)
  9. derivpricematfun(pricemat, K, q)
  10.  
  11. %% price testing thing
  12. N = 4;
  13. T = 1;
  14. dT = T/N;
  15. K = 100;
  16. S0 = 90;
  17. exp_r = 1.05;
  18. sigma = 0.2;
  19.  
  20. u = exp(sigma*sqrt(dT)); %p85
  21. d = exp(-sigma*sqrt(dT)); %p85
  22. q = (exp_r^dT - d)/(u-d);
  23. pricemat = pricematfun(S0, u, d, N);
  24. derivpricemat = derivpricematfun(pricemat, K, q);
  25. derivpricemat(1,1)
  26.  
  27.  
  28. %%
  29. % 3.1
  30. % call = right to buy at price K
  31. % put = right to sell at price K
  32.  
  33. K = 100;
  34. S0 = 90;
  35. exp_r = 1.05;
  36. sigma = 0.2;
  37. N = 4;
  38. T = 1;
  39.  
  40. % dT = T/N;
  41. % u = exp(sigma*sqrt(dT)); %p85
  42. % d = exp(-sigma*sqrt(dT)); %p85
  43. % qu = (exp_r^dT - d)/(u-d);
  44. % qd = (u-exp_r^dT)/(u-d);
  45. pi0_eu = zeros(1,N);
  46. %pi0_eu = pi0_value_europecall(T, N, sigma, exp_r, S0, K)
  47. for n = 1:N
  48. pi0_eu(n) = pi0_value_europecall(T, n, sigma, exp_r, S0, K);
  49. end
  50. plot(pi0_eu)
  51. pi0_eu(end)
  52. %(St-k)+, eu call, St > k
  53.  
  54.  
  55.  
  56.  
  57. %% 3.2
  58. % K = 100;
  59. % S0 = 90;
  60. % exp_r = 1.05;
  61. % sigma = 0.2;
  62. % N = 3;
  63. % T = 1;
  64.  
  65. pi0_us = zeros(1,N);
  66. for n = 1:N
  67. pi0_us(n) = pi0_value_americanput(T, n, sigma, exp_r, S0, K);
  68. end
  69.  
  70. plot(pi0_us)
  71. pi0_us(end)
  72.  
  73. %% 3.3
  74.  
  75. %% forward contract at t=0
  76. % K = 100;
  77. % S0 = 90;
  78. % exp_r = 1.05;
  79. % sigma = 0.2;
  80. % N = 3;
  81. % T = 1;
  82.  
  83. pi0_forward = zeros(1,N);
  84. for n = 1:N
  85. pi0_forward(n) = pi0_value_forward(T, n, sigma, exp_r, S0, K);
  86. end
  87.  
  88. plot(pi0_forward)
  89. pi0_forward(end)
  90.  
  91. lower = S0-K
  92. mid = pi0_eu-pi0_us(end)
  93. upper = pi0_forward(end)
  94.  
  95.  
  96.  
  97. %%
  98. function y = europe_call(K, price)
  99. y = max(price - K, 0);
  100. end
  101.  
  102. function y = american_put(K, price_current)
  103. y = max(K - price_current, 0);
  104. end
  105.  
  106. function y = forward(S0, price_current)
  107. y = max(S0 - price_current, 0);
  108. end
  109.  
  110.  
  111. function pi0 = pi0_value_europecall(T, N, sigma, exp_r, S0, K)
  112. dT = T/N;
  113. u = exp(sigma*sqrt(dT)); %p85
  114. d = exp(-sigma*sqrt(dT)); %p85
  115.  
  116. qu = (exp_r^dT - d)/(u-d);
  117. qd = (u-exp_r^dT)/(u-d);
  118.  
  119. sum = 0;
  120. for k = 0:N
  121.     price = S0*u^k*d^(N-k);
  122.     sum = sum + nchoosek(N,k)*qu^k*(1-qu)^(N-k)*europe_call(K, price);
  123. end
  124. pi0 = exp_r^T*sum;
  125.  
  126. end
  127.  
  128. function pi0 = pi0_value_americanput(T, N, sigma, exp_r, S0, K)
  129. dT = T/N;
  130. u = exp(sigma*sqrt(dT)); %p85
  131. d = exp(-sigma*sqrt(dT)); %p85
  132.  
  133. qu = (exp_r^dT - d)/(u-d);
  134. qd = (u-exp_r^dT)/(u-d);
  135.  
  136. sum = 0;
  137. for k = 0:N
  138.     price = S0*u^k*d^(N-k);
  139.     sum = sum + nchoosek(N,k)*qu^k*(1-qu)^(N-k)*american_put(K, price);
  140. end
  141. pi0 = exp_r^T*sum;
  142.  
  143. end
  144.  
  145. function pi0 = pi0_value_forward(T, N, sigma, exp_r, S0, K)
  146. dT = T/N;
  147. u = exp(sigma*sqrt(dT)); %p85
  148. d = exp(-sigma*sqrt(dT)); %p85
  149.  
  150. qu = (exp_r^dT - d)/(u-d);
  151. qd = (u-exp_r^dT)/(u-d);
  152.  
  153. sum = 0;
  154. for k = 0:N
  155.     price = S0*u^k*d^(N-k);
  156.     sum = sum + nchoosek(N,k)*qu^k*(1-qu)^(N-k)*forward(S0, price);
  157. end
  158. pi0 = exp_r^T*sum;
  159.  
  160. end
  161.  
  162.  
  163.  
  164. function pricemat = pricematfun(S0, u, d, N)
  165.     pricemat = zeros(N+1,N+1);
  166.     for n = 1:N+1
  167.         for i = 1:n
  168.             pricemat(i,n) = S0*u^(i-1)*d^(n-i);
  169.             %S0*u^k*d^(N-k)
  170.         end
  171.     end
  172. end
  173.  
  174. function derivpricemat = derivpricematfun(mat, K, q)
  175.     derivpricemat = zeros(size(mat));
  176.     N = length(mat);
  177.     for n = 1:N
  178.         for i = 1:N-n+1
  179.             if n == 1
  180.                 derivpricemat(i,N-n+1) = max(mat(i,N-n+1)-K,0);
  181.             else
  182.                 derivpricemat(i,N-n+1) = derivpricemat(i,N-n+2)*(1-q)+derivpricemat(i+1,N-n+2)*(q);
  183.             end
  184.            
  185.         end
  186.     end
  187.    
  188. %     derivpricemat = derivpricemat(1:end-1,1:end-1);
  189. %     derivpricemat = flip(derivpricemat,2)';
  190. end
  191.  
  192.  
  193.  
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement