0
我目前正在尋找二項式期權定價。我已經編寫了下面的代碼,當你一次輸入一個變量時,它可以正常工作。但是,輸入每組數值非常單調乏味,我需要能夠分析大量數據。我爲每個變量創建了數組。但是,我不斷收到錯誤; A(I)= B,B中元素的數量必須等於I.該函數如下所示。Matlab錯誤A(I)= B
function C = BinC(S0,K,r,sig,T,N);
% PURPOSE:
% To return the value of a European call option using the Binomial method
%-------------------------------------------------------------------------
% INPUTS:
% S0 - The initial price of the underlying asset
% K - The strike price
% r - The risk free rate of return, expressed as a decimal
% sig - The volatility of the underlying asset, expressed as a decimal
% T - The time to maturity, expressed as a decimal
% N - The number of steps
%-------------------------------------------------------------------------
dt = T/N;
u = exp(sig*sqrt(dt));
d = 1/u;
p = (exp(r*dt) - d)/(u - d);
S = zeros(N+1,1);
% Price of underlying asset at time T
for n = 1:N+1
S(n) = S0*(d^(N+1-n))*(u^(n-1));
end
% Price of Option at time T
for n = 1:N+1
C(n) = max(S(n)- K, 0);
end
% Backtrack to get option price at time 0
for i = N:-1:1
for n = 1:i
C(n) = exp(-r*dt)*(p*C(n+1) + (1-p)*C(n));
end
end
disp(C(1))
導入我的數據後,我將它輸入到命令窗口中。
for i=1:20
w(i)= BinC(S0(i),K(i),r(i),sig(i),T(i),N(i));
end
當我輸入w時,我回到的是w = []。我不知道如何讓A(I)= B。我很抱歉,如果這是一個非常愚蠢的問題,但我是Matlab新手,需要幫助。謝謝