开发者

How to represent my Matlab matrix values in the following lattice-tree form?

I have my values of the lattice in the matrix as shown in fig 1:

How to represent my Matlab matrix values in the following lattice-tree form?

Fig 1: Format of values as currently displayed in Matlab for my code

Now I would like to represent these values in form of lattice tree as shown in the figure 2 (Note the values in figure 2 are not same as in figure 1, and fig 2 is just for demonstration purpose). How could I modify my code in Matlab in order to have result that looks like the tree format shown in figure 2?:

How to represent my Matlab matrix values in the following lattice-tree form?

Fig 2: Format of values as I want to be displayed in my Matlab result

Following is my code:

function [price,BLOV_lattice]=BLOV_general(S0,K,sigma,r,T,nColumn)


%% Constant parameters
del_T=T./nColumn; % where n is the number of columns
u=exp(sigma.*sqrt(del_T));
d=1./u;
p=(exp(r.*del_T)-d)./(u-d);
a=exp(-r.*del_T);

%% Initializing the lattice
Stree=zeros(nColumn+1,nColumn+1);开发者_高级运维
BLOV_lattice=zeros(nColumn+1,nColumn+1);

%% Developing the lattice

for i=0:nColumn
    for j=0:i
        Stree(j+1,i+1)=S0.*(u.^j)*(d.^(i-j));
    end
end
for i=0:nColumn
    BLOV_lattice(i+1,nColumn+1)=max(Stree(i+1,nColumn+1)-K,0);
end
for i=nColumn:-1:1
    for j=0:i-1
        BLOV_lattice(j+1,i)=a.*(((1-p).*BLOV_lattice(j+1,i+1))+(p.*BLOV_lattice(j+2,i+1)));
    end
end
price=BLOV_lattice(1,1);


If your goal is to reformat an upper-triangular matrix (as shown in figure 1) into a matrix with the non-zero values arranged in a tree-like structure (as shown in figure 2), then you can accomplish this using the function SPDIAGS. Here's an example using a 5-by-5 matrix:

>> A = triu(reshape(1:25,5,5))  %# A sample upper-triangular matrix

A =

     1     6    11    16    21
     0     7    12    17    22
     0     0    13    18    23
     0     0     0    19    24
     0     0     0     0    25

>> N = size(A,1);  %# The size of the rows and columns in A
>> B = full(spdiags(spdiags(A),(1-N):2:(N-1),zeros(2*N-1,N)))

B =

     0     0     0     0    21
     0     0     0    16     0
     0     0    11     0    22
     0     6     0    17     0
     1     0    12     0    23
     0     7     0    18     0
     0     0    13     0    24
     0     0     0    19     0
     0     0     0     0    25


I see a solution using only one for loop..

function B = newShape(A)

n = size(A,1);
B = zeros(2*n-1,n);
idx0 = n:(2*n):(2*n^2 - n);
B(idx0(1):(2*n-2):(2*n^2-n-1)) = A(1,:);
for i=n:(2*n-2)
    B( idx0(i - n + 2):(2*n-2):(2*n^2-n) ) = A(i-(n-1)+1,i-(n-1)+1:end);
end


end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜