[Finance] Arbitrage Free Initial Price of a European option (revised)

written by jjycjn   2014. 10. 15. 11:02

지난번 코드에서 출력값을 matrix로 받다보니 for문을 반복할때 index가 지나치게 복잡하기도 하고, matrix의 사이즈를 맞추기 위해서 불필요하게 0이 추가되가 보니까 출력값을 보기가 불편한 느낌이 들어 코드를 전체적으로 수정을 하였다. 이를 위해서 matrix로 받던 출력값을 cell array로 바꾸어, 시간 t에 따라 각각 다른 cell에 출력값이 저장되도록 수정하였다. 수정된 코드는 다음과 같다.

function [V0,V,AB] = EuropeanOption(S0,u,d,T,K,r,type)
% Compute the arbitrage free initial price of the European call option
% by using V0 = E^{p*}[X*]

% Make the binomial tree with S_0, S_1, S_2, ..., S_T
S = cell(1,T+1);
S{1} = S0;
for t = 2:T+1
    S{t} = zeros(2^(t-1),1);
    for j = 1:2:2^(t-1)
        S{t}(j) = S{t-1}((j+1)/2)*u;
        S{t}(j+1) = S{t-1}((j+1)/2)*d;
    end
end

% Compute the contingent claim X
X = zeros(2^T,1);
for j = 1:2^T
    if strcmp(type,'call')
        X(j,1) = max(0, S{T+1}(j)-K);
    elseif strcmp(type,'put')
        X(j,1) = max(0, K-S{T+1}(j));
    end
end

% Calculate the probability p*
p = (1+r-d)/(u-d);

% Compute the arbitrage free initial price V0 of the claim X
% and the hedging strategy (a_t,b_t) where t=1,2,...,T
V = cell(1, T+1);
AB = cell(1, T);
V{T+1} = X;
for t = T:-1:1
    V{t} = zeros(2^(t-1),1);
    AB{t} = zeros(2^(t-1),2);
    for j = 1:2^(t-1)
        % Compute the value of the portfolio V_t
        V{t}(j) = (p*V{t+1}(2*j-1)+(1-p)*V{t+1}(2*j))/(1+r);
        % Compute the number of shares of stock a_t
        AB{t}(j,1) = (V{t+1}(2*j-1)-V{t+1}(2*j))/((u-d)*S{t}(j));
        % Compute the number of unit of bond b_t
        AB{t}(j,2) = 1/(1+r)^t*(u*V{t+1}(2*j)-d*V{t+1}(2*j-1))/(u-d);
    end
end

% Find the arbitrage free initial price of the claim X
V0 = V{1};

end


위의 코드를 보면 알수 있듯이, 저번 코드에 비해 우선 코드의 길이가 다소 짧아졌고, for문의 index도 좀 더 직관적으로 이해하기 쉽게 바뀌었음을 알 수 있다. 또한 입력값 중에 type를 추가하여 type이 'call'일 경우에는 call option을, 'put'일 경우에는 put option을 가정하여 코드를 돌리게끔 수정하였다. 이제 이 코드를 이용하여 저번과 동일한 입력값 (S0=100, u=2, d=0.5, T=4, K=80, r=0.1, type='call') 을 놓고 프로그램을 돌려보자. 역시 initial price는 64.8699로 동일한 결과를 출력함을 확인 할 수 있다.

S0=100; u=2; d=0.5; T=4; K=80; r=0.1; type='call';
>> [V0,V,AB] = EuropeanOption(S0,u,d,T,K,r,type)

V0 =
   64.8699

V = 
    [64.8699]    [2x1 double]    [4x1 double]    [8x1 double]    [16x1 double]

AB = 
    [1x2 double]    [2x2 double]    [4x2 double]    [8x2 double]


  ::  
  • 공유하기  ::