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新手,需要帮助。谢谢