Friends here is the program for SDE series expansion order 4-12. I have given some instructions at the start how to change order of the program.
.
function [] = NewHermiteExpansionO8()
%Copyright Ahsan Amin. Infiniti derivatives Technologies.
%Please fell free to connect on linkedin: linkedin.com/in/ahsan-amin-0a53334
%or skype ahsan.amin2999
%In this program, I am simulating the SDE given as
%dy(t)=mu1 x(t)^beta1 dt + mu2 x(t)^beta2 dt +sigma x(t)^gamma dz(t)
%where mu1=kappa*theta and mu2=-kappa with
%beta1=0 and beta2=1;
%Instructions for changing the Expansion Order of the Program
%1. OrderA has to be given appropriate order value for the program
%This can be anywhere between 2-12%
%2. For Order four use the function (Look for the appropriate block below
%where these functions appear and uncomment the ones with right order and
%comment the other ones.)
%ItoTaylorCoeffsNew(alpha,beta1,beta2,gamma); for original coordinates
%%ItoTaylorCoeffsNew(alpha1,beta1,beta2,gamma); for Bessel coordinates
%For Order eight use the function
%ItoTaylorCoeffsNewOrder8(alpha,beta1,beta2,gamma); for original
%coordinates
%ItoTaylorCoeffsNewOrder8(alpha1,beta1,beta2,gamma); for Bessel coordinates
% For Order twelve use the function
%ItoTaylorCoeffsNewOrder12(alpha,beta1,beta2,gamma); for original
%coordinates
%ItoTaylorCoeffsNewOrder12(alpha1,beta1,beta2,gamma); for Bessel coordinates
%Both OrderA has to be given the right order value and functions for
%appropriate order have to be chosen as I previously explained.
dt=.5; % Simulation time interval.%Fodiffusions close to zero
%decrease dt for accuracy.
Tt=1; % Number of simulation levels. Terminal time= Tt*dt; //.125/32*32*16=2 year;
OrderA=8; %Keep at appropriate order.
OrderM=2; %Keep at two.
T=Tt*dt;
TtM=8*2;%Monte carlo number of simulation intervals.
dtM=T/TtM;%Monte carlo time interval size dtM.
dNn=.2/1; % Normal density subdivisions width. would change with number of subdivisions
Nn=50; % No of normal density subdivisions
NnMidl=25;%One half left from mid of normal density(low)
NnMidh=26;%One half right from the mid of normal density(high)
NnMid=4.0;
x0=.150; % starting value of SDE
beta1=0.0; % the first drift term power.
beta2=1.0; % Second drift term power.
gamma=.60; % volatility power.
kappa=1.0; %mean reversion parameter.
theta=.04; %mean reversion target
%MonteTp5dtp03125x0p1Thp1Ka1Gap75Sip8
mu1=1*theta*kappa; %first drift coefficient.
mu2=-1*kappa; % Second drift coefficient.
sigma0=.60;%Volatility value
alpha=1;% x^alpha is being expanded. This is currently for monte carlo only.
alpha1=1-gamma;
w(1:Nn)=x0^(1-gamma)/(1-gamma);
Z(1:Nn)=(((1:Nn)-5.5)*dNn-NnMid);
Z
str=input('Look at Z');
ZProb(1)=normcdf(.5*Z(1)+.5*Z(2),0,1)-normcdf(.5*Z(1)+.5*Z(2)-dNn,0,1);
ZProb(Nn)=normcdf(.5*Z(Nn)+.5*Z(Nn-1)+dNn,0,1)-normcdf(.5*Z(Nn)+.5*Z(Nn-1),0,1);
for nn=2:Nn-1
ZProb(nn)=normcdf(.5*Z(nn)+.5*Z(nn+1),0,1)-normcdf(.5*Z(nn)+.5*Z(nn-1),0,1);
%Above calculate probability mass in each probability subdivision.
end
tic
wnStart=1;%\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\1;
sigma11(1:OrderA+1)=0;
mu11(1:OrderA+1)=0;
mu22(1:OrderA+1)=0;
sigma22(1:OrderA+1)=0;
% index 1 correponds to zero level since matlab indexing starts at one.
sigma11(1)=1;
mu11(1)=1;
mu22(1)=1;
sigma22(1)=1;
for k=1:(OrderA+1)
if sigma0~=0
sigma11(k)=sigma0^(k-1);
end
if mu1 ~= 0
mu11(k)=mu1^(k-1);
end
if mu2 ~= 0
mu22(k)=mu2^(k-1);
end
if sigma0~=0
sigma22(k)=sigma0^(2*(k-1));
end
end
Ft(1:Tt+1,1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0; %General time powers on hermite polynomials
Fp(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;%General x powers on coefficients of hermite polynomials.
Fp1(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;%General x powers for bessel transformed coordinates.
%Pre-compute the time and power exponent values in small multi-dimensional arrays
for k = 0 : (OrderA+1)
for m = 0:k
l4 = k - m + 1;
for n = 0 : m
l3 = m - n + 1;
for j = 0:n
l2 = n - j + 1;
l1 = j + 1;
Ft(l1,l2,l3,l4) = dt^((l1-1) + (l2-1) + (l3-1) + .5* (l4-1));
Fp(l1,l2,l3,l4) = (alpha + (l1-1) * beta1 + (l2-1) * beta2 + (l3-1) * 2* gamma + (l4-1) * gamma ...
- (l1-1) - (l2-1) - 2* (l3-1) - (l4-1));
Fp1(l1,l2,l3,l4) = (alpha1 + (l1-1) * beta1 + (l2-1) * beta2 + (l3-1) * 2* gamma + (l4-1) * gamma ...
- (l1-1) - (l2-1) - 2* (l3-1) - (l4-1));
end
end
end
end
% %Below call the program that calculates the Ito-Taylor coeffs to 12th order of expansion.
% YCoeff = ItoTaylorCoeffsNewOrder12(alpha,beta1,beta2,gamma);
% Yq=ItoTaylorCoeffsNewOrder12(alpha1,beta1,beta2,gamma);
% %Below call the program that calculates the Ito-Taylor coeffs to 8th order of expansion.
YCoeff = ItoTaylorCoeffsNewOrder8(alpha,beta1,beta2,gamma);
Yq=ItoTaylorCoeffsNewOrder8(alpha1,beta1,beta2,gamma);
%Below call the program that calculates the Ito-Taylor coeffs to 4th order of expansion.
%YCoeff = ItoTaylorCoeffsNew(alpha,beta1,beta2,gamma);
%Yq=ItoTaylorCoeffsNew(alpha1,beta1,beta2,gamma);
YqCoeff=Yq/(1-gamma);%These Bessel coordinates are divided by (1-gamma)
%Calculate the density with monte carlo below.
HermiteP(1,1:Nn)=1;
HermiteP(2,1:Nn)=Z(1:Nn);
HermiteP(3,1:Nn)=Z(1:Nn).^2-1;
HermiteP(4,1:Nn)=Z(1:Nn).^3-3*Z(1:Nn);
HermiteP(5,1:Nn)=Z(1:Nn).^4-6*Z(1:Nn).^2+3;
HermiteP(6,1:Nn)=Z(1:Nn).^5-10*Z(1:Nn).^3+15*Z(1:Nn);
HermiteP(7,1:Nn)=Z(1:Nn).^6-15*Z(1:Nn).^4+45*Z(1:Nn).^2-15;
HermiteP(8,1:Nn)=Z(1:Nn).^7-21*Z(1:Nn).^5+105*Z(1:Nn).^3-105*Z(1:Nn);
HermiteP(9,1:Nn)=Z(1:Nn).^8-28*Z(1:Nn).^6+210*Z(1:Nn).^4-420*Z(1:Nn).^2+105;
HermiteP(10,1:Nn)=Z(1:Nn).^9-36*Z(1:Nn).^7+378*Z(1:Nn).^5-1260*Z(1:Nn).^3+945*Z(1:Nn);
HermiteP(11,1:Nn)=Z(1:Nn).^10-45*Z(1:Nn).^8+630*Z(1:Nn).^6-3150*Z(1:Nn).^4+4725*Z(1:Nn).^2-945;
HermiteP(12,1:Nn)=Z(1:Nn).*HermiteP(11,1:Nn)-10*HermiteP(10,1:Nn);
HermiteP(13,1:Nn)=Z(1:Nn).*HermiteP(12,1:Nn)-11*HermiteP(11,1:Nn);
yy0=x0;
ww0=x0^(1-gamma)/(1-gamma);
yy(1:Nn)=x0; %Original process monte carlo.
ww(1:Nn)=ww0;
for k = 0 : OrderA
for m = 0:k
l4 = k - m + 1;
for n = 0 : m
l3 = m - n + 1;
for j = 0:n
l2 = n - j + 1;
l1 = j + 1;
if(l4<=5)
ww(1:Nn)=ww(1:Nn) + YqCoeff(l1,l2,l3,l4) * ...
mu11(l1)*mu22(l2)*sigma22(l3)*sigma11(l4)* ...
((1-gamma)*ww0).^(Fp1(l1,l2,l3,l4)/(1-gamma)) .* ...
HermiteP(l4,1:Nn) * Ft(l1,l2,l3,l4);
yy(1:Nn)=yy(1:Nn) + YCoeff(l1,l2,l3,l4) * ...
mu11(l1)*mu22(l2)*sigma22(l3)*sigma11(l4)* ...
yy0.^Fp(l1,l2,l3,l4) .* HermiteP(l4,1:Nn) * Ft(l1,l2,l3,l4);
%Above is original coordinates monte carlo equation.
end
end
end
end
end
y_w(1:Nn)=0;
y_w(wnStart:Nn) = ((1-gamma)*ww(1:Nn)).^(1/(1-gamma));
Dfy_w(wnStart:Nn)=0;
Dfy(wnStart:Nn)=0;
for nn=wnStart+1:Nn-1
Dfy_w(nn) = (y_w(nn + 1) - y_w(nn - 1))/(Z(nn + 1) - Z(nn - 1));
Dfy(nn) = (yy(nn + 1) - yy(nn - 1))/(Z(nn + 1) - Z(nn - 1));
%Change of variable derivative for densities
end
fy_w(1:Nn)=0;
fy(1:Nn)=0;
for nn = wnStart:Nn-1
fy_w(nn) = (normpdf(Z(nn),0, 1))/abs(Dfy_w(nn));%Origianl coordinates density
fy(nn) = (normpdf(Z(nn),0, 1))/abs(Dfy(nn));
end
toc
%str=input('Analytic Values calculated; Press a key to start monte carlo');
sigma11(1:OrderM+1)=0;
mu11(1:OrderM+1)=0;
mu22(1:OrderM+1)=0;
sigma22(1:OrderM+1)=0;
% index 1 correponds to zero level since matlab indexing starts at one.
sigma11(1)=1;
mu11(1)=1;
mu22(1)=1;
sigma22(1)=1;
for k=1:(OrderM+1)
if sigma0~=0
sigma11(k)=sigma0^(k-1);
end
if mu1 ~= 0
mu11(k)=mu1^(k-1);
end
if mu2 ~= 0
mu22(k)=mu2^(k-1);
end
if sigma0~=0
sigma22(k)=sigma0^(2*(k-1));
end
end
Ft(1:TtM+1,1:(OrderM+1),1:(OrderM+1),1:(OrderM+1),1:(OrderM+1))=0; %General time powers on hermite polynomials
Fp(1:(OrderM+1),1:(OrderM+1),1:(OrderM+1),1:(OrderM+1))=0;%General x powers on coefficients of hermite polynomials.
Fp1(1:(OrderM+1),1:(OrderM+1),1:(OrderM+1),1:(OrderM+1))=0;%General x powers for bessel transformed coordinates.
%Pre-compute the time and power exponent values in small multi-dimensional arrays
for k = 0 : (OrderM+1)
for m = 0:k
l4 = k - m + 1;
for n = 0 : m
l3 = m - n + 1;
for j = 0:n
l2 = n - j + 1;
l1 = j + 1;
Ft(l1,l2,l3,l4) = dtM^((l1-1) + (l2-1) + (l3-1) + .5* (l4-1));
Fp(l1,l2,l3,l4) = (alpha + (l1-1) * beta1 + (l2-1) * beta2 + (l3-1) * 2* gamma + (l4-1) * gamma ...
- (l1-1) - (l2-1) - 2* (l3-1) - (l4-1));
Fp1(l1,l2,l3,l4) = (alpha1 + (l1-1) * beta1 + (l2-1) * beta2 + (l3-1) * 2* gamma + (l4-1) * gamma ...
- (l1-1) - (l2-1) - 2* (l3-1) - (l4-1));
end
end
end
end
%Below call the program that calculates the Ito-Taylor coeffs.
YCoeff = ItoTaylorCoeffsNew(alpha,beta1,beta2,gamma);
%Yq=ItoTaylorCoeffsNew(alpha1,beta1,beta2,gamma);
%YqCoeff=Yq/(1-gamma);%These coordinates are divided by (1-gamma)
%Calculate the density with monte carlo below.
rng(85109634, 'twister')
paths=250000;
YY(1:paths)=x0; %Original process monte carlo.
% YY2dt(1:paths)=0.0;
%YY4dt(1:paths)=0.0;
X(1:paths)=x0^(1-gamma)/(1-gamma);
Random1(1:paths)=0;
for tt=1:TtM
% t1=tt*dtM;
% t0=(tt-1)*dtM;
Random1=randn(size(Random1));
HermiteP1(1,1:paths)=1;
HermiteP1(2,1:paths)=Random1(1:paths);
HermiteP1(3,1:paths)=Random1(1:paths).^2-1;
HermiteP1(4,1:paths)=Random1(1:paths).^3-3*Random1(1:paths);
HermiteP1(5,1:paths)=Random1(1:paths).^4-6*Random1(1:paths).^2+3;
YY0=YY;
% YYPrev=YY;
% XX0=X;
for k = 0 : OrderM
for m = 0:k
l4 = k - m + 1;
for n = 0 : m
l3 = m - n + 1;
for j = 0:n
l2 = n - j + 1;
l1 = j + 1;
if(l4<=5)
% X(1:paths)=X(1:paths) + YqCoeff(l1,l2,l3,l4) * ...
% mu11(l1)*mu22(l2)*sigma22(l3)*sigma11(l4)* ...
% ((1-gamma)*XX0(1:paths)).^(Fp1(l1,l2,l3,l4)/(1-gamma)) .* ...
% HermiteP1(l4,1:paths) * Ft(l1,l2,l3,l4);
YY(1:paths)=YY(1:paths) + YCoeff(l1,l2,l3,l4) * ...
mu11(l1)*mu22(l2)*sigma22(l3)*sigma11(l4)* ...
YY0(1:paths).^Fp(l1,l2,l3,l4) .* HermiteP1(l4,1:paths) * Ft(l1,l2,l3,l4);
%Above is original coordinates monte carlo equation.
end
end
end
end
end
end
YY(YY<0)=0;
% sum(YY(:))/paths %origianl coordinates monte carlo average.
% sum(y_w(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
% sum(yy(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
TrueMean= theta+(x0-theta)*exp(-kappa*dt*Tt)%Mean reverting SDE original variable true average
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
MCVar=sum((YY(:)-MCMean).^2)/paths
disp('Original process average from our simulation');
ItoHermiteMeanBes=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %Original process average from coordinates
ItoHermiteMeanOrig=sum(yy(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %Original process average from coordinates
ItoHermiteVarBes=sum((y_w(wnStart+1:Nn-1)-ItoHermiteMeanBes).^2.*ZProb(wnStart+1:Nn-1))
ItoHermiteVarOrig=sum((yy(wnStart+1:Nn-1)-ItoHermiteMeanOrig).^2.*ZProb(wnStart+1:Nn-1))
disp('true Mean only applicble to standard SV mean reverting type models otherwise disregard');
MaxCutOff=30;
NoOfBins=round(300*gamma^2*4*sigma0/sqrt(MCMean)/(1+kappa));%Decrease the number of bins if the graph is too
[YDensity,IndexOutY,IndexMaxY] = MakeDensityFromSimulation_Infiniti_NEW(YY,paths,NoOfBins,MaxCutOff );
%plot(y_w1(wnStart+1:Nn-1),py_w1(wnStart+1:Nn-1),'b',y_w(wnStart+1:Nn-1),py_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
%plot(y_w(wnStart+1:Nn-1),py_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
plot(yy(wnStart+1:Nn-1),fy(wnStart+1:Nn-1),'r',y_w(wnStart+1:Nn-1),fy_w(wnStart+1:Nn-1),'b',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
%plot(yy(wnStart+1:Nn-1),fy(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
title(sprintf('x0 = %.4f,theta=%.4f,kappa=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.3f,MOrig=%.5f,MBes=%.5f,TM=%.5f', x0,theta,kappa,gamma,sigma0,T,dt,ItoHermiteMeanOrig,ItoHermiteMeanBes,TrueMean));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
legend({'OriginalCoordinates Density','Bessel Coordinates Density','Monte Carlo Density'},'Location','northeast')
str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
.
.
.
function [IntegralCoeff] = ComputeIntegralCoeffsOrder12()
IntegralCoeff(1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3)=0;
%IntegralCoeffdt(1:3,1:3,1:3,1:3)=0;
%IntegralCoeffdz(1:3,1:3,1:3,1:3)=0;
IntegralCoeff0(1:3,1:3,1:3,1:3)=0;
IntegralCoeff0(1,1,1,2)=1;
IntegralCoeff0(1,1,1,3)=1;
IntegralCoeff0(1,1,2,2)=1/2;
IntegralCoeff0(1,1,3,2)=1-1/sqrt(3);
IntegralCoeff0(1,1,2,3)=1/sqrt(3);
IntegralCoeff0(1,1,3,3)=1/2;
IntegralCoeff0(1,2,2,2)=1/6;
IntegralCoeff0(1,3,2,2)=(1-1/sqrt(3))*1/2*(1-1/sqrt(5));
IntegralCoeff0(1,2,3,2)=1/sqrt(3)/2*(1-1/sqrt(5));
IntegralCoeff0(1,3,3,2)=1/2*(1-sqrt(2)/2);
IntegralCoeff0(1,2,2,3)=1/2*1/sqrt(5);
IntegralCoeff0(1,3,2,3)=(1-1/sqrt(3))*1/sqrt(2)*1/2;
IntegralCoeff0(1,2,3,3)=1/sqrt(3)/sqrt(2)*1/(2);
IntegralCoeff0(1,3,3,3)=1/6;
IntegralCoeff0(2,2,2,2)=1/24;
IntegralCoeff0(2,2,3,2)=1/2*1/sqrt(5)*1/3*(1-1/sqrt(7));
IntegralCoeff0(2,3,2,2)=1/sqrt(3)*1/2*(1-1/sqrt(5))*1/3*(1-1/sqrt(7));
IntegralCoeff0(3,2,2,2)=(1-1/sqrt(3))*1/2*(1-1/sqrt(5))*1/3*(1-1/sqrt(7));
IntegralCoeff0(3,3,2,2)=1/2*(1-sqrt(2)/2)*1/2*(1-sqrt(2)/sqrt(6));
IntegralCoeff0(2,3,3,2)=1/sqrt(3)*1/sqrt(2)*1/2*1/2*(1-sqrt(2)/sqrt(6));
IntegralCoeff0(3,2,3,2)=(1-1/sqrt(3))*1/sqrt(2)*1/2*1/2*(1-sqrt(2)/sqrt(6));
IntegralCoeff0(3,3,3,2)=1/6*(1-sqrt(3)/sqrt(5));
IntegralCoeff0(2,2,2,3)=1/6*1/sqrt(7);
IntegralCoeff0(2,2,3,3)=1/2*1/sqrt(5)*1/sqrt(2)*1/sqrt(6);
IntegralCoeff0(2,3,2,3)=1/sqrt(3)*1/2*(1-1/sqrt(5))*1/sqrt(2)*1/sqrt(6);
IntegralCoeff0(3,2,2,3)=(1-1/sqrt(3))*1/2*(1-1/sqrt(5))*1/sqrt(2)*1/sqrt(6);
IntegralCoeff0(3,3,2,3)=1/2*(1-sqrt(2)/2)*1/sqrt(3)*1/sqrt(5);
IntegralCoeff0(2,3,3,3)=1/sqrt(3)*1/sqrt(2)*1/sqrt(4)*1/sqrt(3)*(1/sqrt(5));
IntegralCoeff0(3,2,3,3)=(1-1/sqrt(3))*1/sqrt(2)*1/2*1/sqrt(3)*1/sqrt(5);
IntegralCoeff0(3,3,3,3)=1/24;
%Can also be calculated with algorithm below.
%here IntegralCoeffdt indicates the coefficients of a dt-integral.
%This dt-integral means that you are calculating the Ito-hermite expansion
%of Integral X(t)^alpha dt with X(t) dynamics given by the SDE
%here IntegralCoeffdz indicates the coefficients of a dz-integral.
%This dz-integral means that you are calculating the Ito-hermite expansion
%of Integral X(t)^alpha dz(t) with X(t) dynamics given by the SDE
%IntegralCoeff is is associated with expansion of X(t)^alpha.
%IntegralCoeff below is not returned and IntegralCoeff0 manually calculated
%above is returned but both are the same.
l0(1:2)=1;
for m12=1:2
l0(1)=1;
l0(2)=1;
%IntegralCoeff4(m4,1,1,1)=1;
%IntegralCoeff4(m4,1,1,1)=1;
%1 is added to m8 since index 1 stands for zero, 2 for one and three
%for two.
IntegralCoeff(1,1,1,1,1,1,1,1,1,1,1,m12+1)=1;
for m11=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
if(m11==1)
IntegralCoeff(1,1,1,1,1,1,1,1,1,1,m12+1,m11+1)=1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m11==2)
IntegralCoeff(1,1,1,1,1,1,1,1,1,1,m12+1,m11+1)= 1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m10=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
if(m10==1)
IntegralCoeff(1,1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1)=IntegralCoeff(1,1,1,1,1,1,1,1,1,1,m12+1,m11+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m10==2)
IntegralCoeff(1,1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1)=IntegralCoeff(1,1,1,1,1,1,1,1,1,1,m12+1,m11+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m9=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
if(m9==1)
IntegralCoeff(1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1)=IntegralCoeff(1,1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m9==2)
IntegralCoeff(1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1)=IntegralCoeff(1,1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m8=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
if(m8==1)
IntegralCoeff(1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1)=IntegralCoeff(1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m8==2)
IntegralCoeff(1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1)=IntegralCoeff(1,1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m7=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
if(m7==1)
IntegralCoeff(1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1)=IntegralCoeff(1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m7==2)
IntegralCoeff(1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1)=IntegralCoeff(1,1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m6=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
if(m6==1)
IntegralCoeff(1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1)=IntegralCoeff(1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m6==2)
IntegralCoeff(1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1)=IntegralCoeff(1,1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m5=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
l0(m6)=l0(m6)+1;
if(m5==1)
IntegralCoeff(1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1)=IntegralCoeff(1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m5==2)
IntegralCoeff(1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1)=IntegralCoeff(1,1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m4=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
l0(m6)=l0(m6)+1;
l0(m5)=l0(m5)+1;
if(m4==1)
IntegralCoeff(1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1)=IntegralCoeff(1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m4==2)
IntegralCoeff(1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1)=IntegralCoeff(1,1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m3=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
l0(m6)=l0(m6)+1;
l0(m5)=l0(m5)+1;
l0(m4)=l0(m4)+1;
if(m3==1)
IntegralCoeff(1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1)=IntegralCoeff(1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m3==2)
IntegralCoeff(1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1)=IntegralCoeff(1,1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m2=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
l0(m6)=l0(m6)+1;
l0(m5)=l0(m5)+1;
l0(m4)=l0(m4)+1;
l0(m3)=l0(m3)+1;
if(m2==1)
IntegralCoeff(1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1)=IntegralCoeff(1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m2==2)
IntegralCoeff(1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1)=IntegralCoeff(1,1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
for m1=1:2
l0(1)=1;
l0(2)=1;
l0(m12)=l0(m12)+1;
l0(m11)=l0(m11)+1;
l0(m10)=l0(m10)+1;
l0(m9)=l0(m9)+1;
l0(m8)=l0(m8)+1;
l0(m7)=l0(m7)+1;
l0(m6)=l0(m6)+1;
l0(m5)=l0(m5)+1;
l0(m4)=l0(m4)+1;
l0(m3)=l0(m3)+1;
l0(m2)=l0(m2)+1;
if(m1==1)
IntegralCoeff(m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1,m1+1)=IntegralCoeff(1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1)*1/(l0(1)-1+1)*(1-sqrt(l0(2)-1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+2));
end
if(m1==2)
IntegralCoeff(m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1,m1+1)=IntegralCoeff(1,m12+1,m11+1,m10+1,m9+1,m8+1,m7+1,m6+1,m5+1,m4+1,m3+1,m2+1)*1/sqrt(l0(2)-1+1)/sqrt(2*(l0(1)-1)+(l0(2)-1)+1);
end
end
end
end
end
end
end
end
end
end
end
end
end
end
.
.
function [Y] = ItoTaylorCoeffsNewOrder12(alpha,beta1,beta2,gamma)
%In the coefficient calculation program which calculates Y(l1,l2,l3,l4),
%I have used four levels of looping each for relevant expansion order.
%The first loop takes four values and second loop takes 16 values and
%third loop takes 64 values and so on. And then each coefficient
%term can be individually calculated while carefully accounting
%for path dependence.
%So for example in a nested loop structure
%m1= 1:mDim
% m2=1:mDim
% m3=1:mDim
% l(m1)=l(m1)+1;
% l(m2)=l(m2)+1;
% l(m3)=l(m3)+1;
%in the above looping loop takes values from one to four with one
%indicating the first drift term, two indicating the second drift term
%and three indicating quadratic variation term and
%four indicating the volatility term. And with this looping structure
%we can So in the above looping m1=1 would mean that all terms are
%descendent of first drift term and m2=4 would mean that all terms are
%descendent of first drift term on first expansion order and descendent
%of volatility term on the second order and so we can keep track of path
%dependence perfectly.
%And on each level, we individually calculate the descendent terms. While
%keeping track of path dependence and calculating the coefficients with
%careful path dependence consideration, we update the appropriate element
%in our polynomial like expansion coefficient array
%explaining the part of code
%m1= 1:mDim
% m2=1:mDim
% m3=1:mDim
% l(m1)=l(m1)+1;
% l(m2)=l(m2)+1;
% l(m3)=l(m3)+1;
%Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,2);
%Here l(1) denotes l1 but written as l(1) so it can be conveniently
%updated with the loop variable when the loop variable takes value one
%indicating first drift term . And l(2) could be conveniently updated when
%the loop variable takes value equal to two indicating second
%drift term and so on.
%Here is the part of code snippet for that
%for m1=1:mDim
% l(1)=1;
% l(2)=1;
% l(3)=1;
% l(4)=1;
% l(m1)=l(m1)+1;
%CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma - (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
% CoeffDX2 = CoeffDX1 - 1;
% ArrIndex0=m1;
% ArrIndex=(m1-1)*mDim;
% Coeff1st=Y1(ArrIndex0)*CoeffDX1;
% Coeff2nd=Y1(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
% Y2(1+ArrIndex)=Coeff1st;
% Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,2,n1(m1));
% Y2(2+ArrIndex)=Coeff1st;
% Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,2,n1(m1));
% Y2(3+ArrIndex)=Coeff2nd;
% Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,2,n1(m1));
% Y2(4+ArrIndex)=Coeff1st;
% Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,3,n1(m1));
%The first four lines update the array indices according to the parent term.
%And then CoeffDX1 and CoeffDX2 are calculated according to algebraic exponents on parent terms.
%ArrIndex0=m1; calculates the array index of the parent term
%And ArrIndex=(m1-1)*mDim; calculates the array index of the descendent terms
%And coefficient of the drift and volatility descendent terms is
%calculated by multiplying the coefficient of the parent term by
%Coeff1st=Y1(ArrIndex0)*CoeffDX1;
%And coefficient of the quadratic variation descendent terms is
%calculated by multiplying the coefficient of the parent term by
%Coeff2nd=Y1(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
%And then each of the four descendent terms are updated with Coeff1st
%if they are drift or volatility descendent terms or Coeff2nd if
%they are quadratic variation descendent terms.
%Here Y1 indicates the temporary coefficient array with parent terms on
%first level. And Y2 denotes temporary coefficient array with parent terms
%on second level and Y3 indicates temporary coefficient array with parent terms on third level.
[IntegralCoeff] = ComputeIntegralCoeffsOrder12();
%[IntegralCoeff1] = ComputeIntegralCoeffsOrder8();
%IntegralCoeff(1,1,1,1,:,:,:,:,:,:,:,:)=IntegralCoeff1(:,:,:,:,:,:,:,:);
%IntegralCoeff2(1,1,1,1,:,:,:,:,:,:,:,:)-IntegralCoeff(1,1,1,1,:,:,:,:,:,:,:,:)
% for m1=1:3
% for m2=1:3
% for m3=1:3
% for m4=1:3
% for m5=1:3
% for m6=1:3
% for m7=1:3
% for m8=1:3
% if(IntegralCoeff2(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1) ~= IntegralCoeff(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1))
% IntegralCoeff2(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1)- IntegralCoeff(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1)
% IntegralCoeff2(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1)
% IntegralCoeff(1,1,1,1,m8,m7,m6,m5,m4,m3,m2,m1)
% m8
% m7
% m6
% m5
% m4
% m3
% m2
% m1
% str=input('Look at difference of integrals');
% end
% end
% end
% end
% end
% end
% end
% end
% end
%str=input('Look at difference of integrals');
n1(1)=2;
n1(2)=2;
n1(3)=2;
n1(4)=3;
%n1(1), n1(2), n1(3) are drift and quadratic variation term variables
%and take a value equal to 2 indicating a dt integral.
%n1(4) is volatility term variable and indicates a dz-integral by taking a
%value of 3.
mDim=4; % four descendent terms in each expansion
Y(1:13,1:13,1:13,1:13)=0;
Y1(1:mDim)=0;
Y2(1:mDim*mDim)=0;
Y3(1:mDim*mDim*mDim)=0;
Y4(1:mDim*mDim*mDim*mDim)=0;
Y5(1:mDim*mDim*mDim*mDim*mDim)=0;
Y6(1:mDim*mDim*mDim*mDim*mDim*mDim)=0;
Y7(1:mDim*mDim*mDim*mDim*mDim*mDim*mDim)=0;
Y8(1:mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim)=0;
Y9(1:mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim)=0;
Y10(1:mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim)=0;
Y11(1:mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim*mDim)=0;
%First Ito-hermite expansion level starts here. No loops but four
%descendent terms.
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
CoeffDX1 = alpha;
CoeffDX2 = CoeffDX1 - 1;
Coeff1st=CoeffDX1;
Coeff2nd=.5*CoeffDX1*CoeffDX2;
Y1(1)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,1,2);
Y1(2)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,1,2);
Y1(3)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,1,2);
Y1(4)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,1,3);
%Second Ito-hermite expansion level starts. It has a loop over four parent
%terms and there are four descendent terms for each parent term.
%The coefficient terms are then lumped in a polynomial-like expansion
%array of coefficients.
for m1=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2 = CoeffDX1 - 1;
ArrIndex0=m1;
ArrIndex=(m1-1)*mDim;
Coeff1st=Y1(ArrIndex0)*CoeffDX1;
Coeff2nd=Y1(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y2(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,2,n1(m1));
Y2(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,2,n1(m1));
Y2(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,2,n1(m1));
Y2(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,1,3,n1(m1));
%Third Ito-hermite expansion level starts and it is a nested loop with
%a total of sixteen parents and each parent takes four descendent
%terms.
for m2=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=(m1-1)*mDim+m2;
ArrIndex=((m1-1)*mDim+(m2-1))*mDim;
Coeff1st=Y2(ArrIndex0)*CoeffDX1;
Coeff2nd=Y2(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y3(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,2,n1(m2),n1(m1));
Y3(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,2,n1(m2),n1(m1));
Y3(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,1,1,1,2,n1(m2),n1(m1));
Y3(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,1,3,n1(m2),n1(m1));
%fourht Ito-hermite expansion level starts and it is a triply-nested loop with
%a total of sixteen parents and each parent takes four descendent
%terms. We then lump the terms in a relatively sparse polynomial
%like expansion coefficient array that has smaller number of
%non-zero terms.
for m3=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=((m1-1)*mDim+(m2-1))*mDim+m3;
ArrIndex=(((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim;
Coeff1st=Y3(ArrIndex0)*CoeffDX1;
Coeff2nd=Y3(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y4(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,2,n1(m3),n1(m2),n1(m1));
Y4(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,2,n1(m3),n1(m2),n1(m1));
Y4(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,1,1,2,n1(m3),n1(m2),n1(m1));
Y4(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,1,3,n1(m3),n1(m2),n1(m1));
for m4=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=(((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+m4;
ArrIndex=((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim;
Coeff1st=Y4(ArrIndex0)*CoeffDX1;
Coeff2nd=Y4(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y5(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,2,n1(m4),n1(m3),n1(m2),n1(m1));
Y5(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,2,n1(m4),n1(m3),n1(m2),n1(m1));
Y5(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,1,2,n1(m4),n1(m3),n1(m2),n1(m1));
Y5(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,1,3,n1(m4),n1(m3),n1(m2),n1(m1));
for m5=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+m5;
ArrIndex=(((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim;
Coeff1st=Y5(ArrIndex0)*CoeffDX1;
Coeff2nd=Y5(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y6(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,2,n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y6(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,1,2,n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y6(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,1,2,n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y6(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,1,3,n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m6=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=(((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+m6;
ArrIndex=((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim;
Coeff1st=Y6(ArrIndex0)*CoeffDX1;
Coeff2nd=Y6(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y7(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,2,n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y7(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,1,2,n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y7(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,1,2,n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y7(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,1,3,n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m7=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
l(m7)=l(m7)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+m7;
ArrIndex=(((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim;
Coeff1st=Y7(ArrIndex0)*CoeffDX1;
Coeff2nd=Y7(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y8(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,2,n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y8(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,1,2,n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y8(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,1,2,n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y8(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,1,3,n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m8=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
l(m7)=l(m7)+1;
l(m8)=l(m8)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=(((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+m8;
ArrIndex=((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim;
Coeff1st=Y8(ArrIndex0)*CoeffDX1;
Coeff2nd=Y8(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y9(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,2,n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y9(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,1,2,n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y9(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,1,2,n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y9(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,1,3,n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m9=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
l(m7)=l(m7)+1;
l(m8)=l(m8)+1;
l(m9)=l(m9)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+m9;
ArrIndex=(((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+(m9-1))*mDim;
Coeff1st=Y9(ArrIndex0)*CoeffDX1;
Coeff2nd=Y9(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y10(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,1,2,n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y10(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,1,2,n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y10(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,1,2,n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y10(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,1,3,n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m10=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
l(m7)=l(m7)+1;
l(m8)=l(m8)+1;
l(m9)=l(m9)+1;
l(m10)=l(m10)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=(((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+(m9-1))*mDim+m10;
ArrIndex=((((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+(m9-1))*mDim+(m10-1))*mDim;
Coeff1st=Y10(ArrIndex0)*CoeffDX1;
Coeff2nd=Y10(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
Y11(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(1,2,n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y11(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(1,2,n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y11(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(1,2,n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
Y11(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(1,3,n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
for m11=1:mDim
l(1)=1;
l(2)=1;
l(3)=1;
l(4)=1;
l(m1)=l(m1)+1;
l(m2)=l(m2)+1;
l(m3)=l(m3)+1;
l(m4)=l(m4)+1;
l(m5)=l(m5)+1;
l(m6)=l(m6)+1;
l(m7)=l(m7)+1;
l(m8)=l(m8)+1;
l(m9)=l(m9)+1;
l(m10)=l(m10)+1;
l(m11)=l(m11)+1;
CoeffDX1 = alpha + (l(1)-1) *beta1 + (l(2)-1) *beta2 + (l(3)-1) *2*gamma + (l(4)-1)*gamma ...
- (l(1)-1) - (l(2)-1) - 2*(l(3)-1) - (l(4)-1);
CoeffDX2=CoeffDX1-1;
ArrIndex0=((((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+(m9-1))*mDim+(m10-1))*mDim+m11;
ArrIndex=(((((((((((m1-1)*mDim+(m2-1))*mDim+(m3-1))*mDim+(m4-1))*mDim+(m5-1))*mDim+(m6-1))*mDim+(m7-1))*mDim+(m8-1))*mDim+(m9-1))*mDim+(m10-1))*mDim+(m11-1))*mDim;
Coeff1st=Y11(ArrIndex0)*CoeffDX1;
Coeff2nd=Y11(ArrIndex0)*.5*CoeffDX1*CoeffDX2;
%Y12(1+ArrIndex)=Coeff1st;
Y(l(1)+1,l(2),l(3),l(4))=Y(l(1)+1,l(2),l(3),l(4))+Coeff1st*IntegralCoeff(2,n1(m11),n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
%Y12(2+ArrIndex)=Coeff1st;
Y(l(1),l(2)+1,l(3),l(4))=Y(l(1),l(2)+1,l(3),l(4))+Coeff1st*IntegralCoeff(2,n1(m11),n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
%Y12(3+ArrIndex)=Coeff2nd;
Y(l(1),l(2),l(3)+1,l(4))=Y(l(1),l(2),l(3)+1,l(4))+Coeff2nd*IntegralCoeff(2,n1(m11),n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
%Y12(4+ArrIndex)=Coeff1st;
Y(l(1),l(2),l(3),l(4)+1)=Y(l(1),l(2),l(3),l(4)+1)+Coeff1st*IntegralCoeff(3,n1(m11),n1(m10),n1(m9),n1(m8),n1(m7),n1(m6),n1(m5),n1(m4),n1(m3),n1(m2),n1(m1));
end
end
end
end
end
end
end
end
end
end
end
end
.
.