Delta - 二项式期权定价Matlab

时间:2015-02-16 11:39:50

标签: matlab delta quantitative-finance

我试图通过二项式模型计算期权的增量。不过,运行代码时出现以下错误:

Subscripted assignment dimension mismatch.

Error in Prova (line 57)
Delta(1:idx, idx)= ...

错误必须在代码的最后一行中。但是,我无法解决问题。 代码如下:

 %Data
 X= 8;
 S0= 8;
 r= 0.01;
 sigma= 0.27;
 dt=91/252;
 steps=4;
 oType='CALL';
 earlyExercise=false;
 N=10000000;

 % Calculate the Cox Ross Rubinstein model parameters
 R = exp(r*dt);
 u = exp(sigma*sqrt(dt));
 d = 1/u;
 p = (R-d)/(u-d);

 % Loop over each node and calculate the Cox Ross Rubinstein underlying    price tree
 priceTree = nan(steps+1,steps+1); 
 priceTree(1,1) = S0;
 for idx = 2:steps+1
 priceTree(1:idx-1,idx) = priceTree(1:idx-1,idx-1)*u;
 priceTree(idx,idx) = priceTree(idx-1,idx-1)*d;
 end
 priceTree

 % Calculate the value at expiry
 valueTree = nan(size(priceTree));
 switch oType
 case 'PUT'
    valueTree(:,end) = N*max(X-priceTree(:,end),0);
 case 'CALL'
    valueTree(:,end) = N*max(priceTree(:,end)-X,0);
 end

 % Loop backwards to get values at the earlier times
 %steps - Gives the "number" of the step (either 1st, 2nd, 3rd...) by  calculating the size of the second dimension of priceTree-1
 %N.B: priceTree(dim1,dim2) - dim1: position of the node in the step
 %                          - dim2: the step
 steps = size(priceTree,2)-1; 
 for idx = steps:-1:1
 valueTree(1:idx,idx) = ...
    exp(-r*dt)*(p*valueTree(1:idx,idx+1) ...
    + (1-p)*valueTree(2:idx+1,idx+1));
 end
 valueTree
 % Calculate Delta at expiry-1
 Delta=nan(steps,steps);
 for idx=1:steps
 Delta(idx, steps)= ...
    (valueTree(idx,end)-valueTree(idx+1,end))...
    /(priceTree(idx,end)-priceTree(idx+1,end));
 end

 %Calculate Delta
 deltasteps=size(priceTree,2)-2;
 for idx=deltasteps:-1:1
 Delta(1:idx, idx)= ...
    (valueTree(1:idx,idx+1)-valueTree(2:idx+1,idx+1))...
    /(priceTree(1:idx,idx+1)-priceTree(2:idx+1,idx+1));
 end
 Delta

有人可以帮助我吗? 在此先感谢您的回答!

1 个答案:

答案 0 :(得分:0)

这意味着Delta(1:idx, idx)(valueTree(1:idx,idx+1)-valueTree(2:idx+1,idx+1))/(priceTree(1:idx,idx+1)-priceTree(2:idx+1,idx+1));的大小不一样有问题。 这可能是因为您希望使用./元素除法而不是/

错误之前没有显示几行,因为您使用了标量,因此.//是相同的,但是,第57行您正在向量上操作,因此您需要使用元素分割./

Delta(1:idx, idx)= ...
(valueTree(1:idx,idx+1)-valueTree(2:idx+1,idx+1))...
./(priceTree(1:idx,idx+1)-priceTree(2:idx+1,idx+1));