function [] = FPESeriesCoeffsBesselWithVariance04()
%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 +sigma x(t)^gamma dz(t)
%I have not directly simulated the SDE but simulated the transformed
%Besse1l process version of the SDE and then changed coordinates to retreive
%the SDE in original coo
%rdinates.
%The present program will analytically evolve only the Bessel Process version of the
%SDE in transformed coordinates.
dt=.125/2/2/2; % Simulation time interval.%Fodiffusions close to zero
%decrease dt for accuracy.
Tt=32*2*10;%16*2*4;%*4*4*1;%*4;%128*2*2*2; % Number of simulation levels. Terminal time= Tt*dt; //.125/32*32*16=2 year;
T=Tt*dt;
Ts=Tt;
ds(1:Ts)=dt;
%
% %Below I have done calculations for smaller step size at start.
%
% %ds(1:64)=dt/16;
% %ds(65:128)=dt/4;
% if(Tt<=4)
% Ts=(Tt*8);%+(64-16);
% ds(1:Ts)=dt/8;
% end
% if(Tt>4)&&(Tt<=20)
% Ts=(32)+((Tt-4)*2);
% ds(1:32)=dt/8;
% ds(33:Ts)=dt/2;
% end
% if(Tt>20)
% Ts=(32)+((20-4)*2)+(Tt-20);
% ds(1:32)=dt/8;
% ds(33:64)=dt/2;
% ds(65:Ts)=dt;
% end
% T
% sum(ds(1:Ts))
% Ts
%str=input('Check if time is right');
OrderA=4; %
OrderM=4; %
if(T>=1)
dtM=1/16;%.0625/1;
TtM=T/dtM;
else
dtM=T/16;%.0625/1;
TtM=T/dtM;
end
dtM=dt*8;
TtM=Tt/8;
dNn=.2/2; % Normal density subdivisions width. would change with number of subdivisions
Nn=47*2; % No of normal density subdivisions
NnMid=4.0;
x0=2.0; % starting value of SDE
beta1=0.65;
beta2=1.0; % Second drift term power.
%beta1=0.950;
%beta2=0.0; % Second drift term power.
gamma=.95;%50; % volatility power.
kappa=1.00;%.950; %mean reversion parameter.
theta=1.0;%mean reversion target
sigma0=.250;%Volatility value
%you can specify any general mu1 and mu2 and beta1 and beta2.
mu1=.10; %first drift coefficient.
mu2=0;%-1*kappa; % Second drift coefficient.
%mu1=theta*kappa;
%mu2=-1*kappa;
alpha=1;% x^alpha is being expanded. This is currently for monte carlo only.
alpha1=1-gamma;%This is for expansion of integrals for calculation of drift
%and volatility coefficients
%yy(1:Nn)=x0;
w0=x0^(1-gamma)/(1-gamma);
Z(1:Nn)=(((1:Nn)-7.5/1)*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);
ZProb(2:Nn-1)=normcdf(.5*Z(2:Nn-1)+.5*Z(3:Nn),0,1)-normcdf(.5*Z(2:Nn-1)+.5*Z(1:Nn-2),0,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:TtM+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.
%YCoeff0 and YCoeff are coefficents for original coordinates monte carlo.
%YqCoeff0 and YqCoeff are bessel/lamperti version monte carlo.
YCoeff0(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;
YqCoeff0(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;
%Pre-compute the time and power exponent values in small multi-dimensional arrays
YCoeff = ItoTaylorCoeffsNew(alpha,beta1,beta2,gamma); %expand y^alpha where alpha=1;
YqCoeff = ItoTaylorCoeffsNew(alpha1,beta1,beta2,gamma);%expand y^alpha1 where alpha1=(1-gamma)
YqCoeff=YqCoeff/(1-gamma); %Transformed coordinates coefficients have to be
%further divided by (1-gamma)
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;
%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));
YCoeff0(l1,l2,l3,l4) =YCoeff(l1,l2,l3,l4).*mu11(l1).*mu22(l2).*sigma22(l3).*sigma11(l4);
YqCoeff0(l1,l2,l3,l4) =YqCoeff(l1,l2,l3,l4).*mu11(l1).*mu22(l2).*sigma22(l3).*sigma11(l4);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ExpnOrder=5;
SeriesOrder=5;
wnStart=1;
tic
for tt=1:Ts
tt
if(tt==1)
[wMu0dt,dwMu0dtdw] = BesselDriftAndDerivativesH0(w0,YqCoeff0,Fp1,gamma,ds(tt),6);
c0=wMu0dt+w0;
c(1)=sigma0*sqrt(ds(tt));
c(2:10)=0.0;
w0=c0;
elseif(tt>=2)
[wMu0dt,dwMu0dtdw] = BesselDriftAndDerivativesH0(w0,YqCoeff0,Fp1,gamma,ds(tt),6);
%Below we get Z-series representation of drift term.
[b0,b] = CalculateDriftbCoeffs08A(wMu0dt,dwMu0dtdw,c,SeriesOrder);
%Below we convert Z_series of drift term to hermite representation
[bH0,bH] = ConvertZCoeffsToHCoeffs(b0,b,SeriesOrder);
[wVol0dt,dwVol0dtdw] = BesselVolAndDerivativesH1(w0,YqCoeff0,Fp1,gamma,ds(tt),SeriesOrder+1);
%Below we get Z-series representation of coefficient of first hermite polynomial in diffusion term.
[g10,g1] = CalculateDriftbCoeffs08A(wVol0dt,dwVol0dtdw,c,SeriesOrder);
g10=g10+sigma0*sqrt(dt);
%The Z_series(called g1) below is coefficient of first hermite polynomial
%g10+g1(1)*Z+g1(2)*Z^2+g1(3)*Z^3+g1(4)*Z^4+g1(5)*Z^5
[g1H0,g1H] = ConvertZCoeffsToHCoeffs(g10,g1,SeriesOrder);
%g1Sqr is variance of Z_series g1 that makes coefficient of first
%hermite polynomial
g1Sqr=(sign(g1H0).*g1H0^2+sign(g1H(1)).*g1H(1).^2+sign(g1H(2)).*g1H(2).^2*2+ ...
sign(g1H(3)).*g1H(3).^2*6+sign(g1H(4)).*g1H(4).^2*24+sign(g1H(5)).*g1H(5).^2*120);
g11=sign(g1Sqr).*sqrt(abs(g1Sqr));
[wVol2dt,dwVol2dtdw] = BesselVolAndDerivativesH2(w0,YqCoeff0,Fp1,gamma,ds(tt),SeriesOrder+1);
%Below we get Z-series representation of coefficient of second hermite polynomial in diffusion term.
[g20,g2] = CalculateDriftbCoeffs08A(wVol2dt,dwVol2dtdw,c,SeriesOrder);
%The Z_series(called g2) below is coefficient of second hermite polynomial
%g20+g2(1)*Z+g2(2)*Z^2+g2(3)*Z^3+g2(4)*Z^4+g2(5)*Z^5
[g2H0,g2H] = ConvertZCoeffsToHCoeffs(g20,g2,SeriesOrder);
%g2Sqr is variance of Z_series g2 that makes coefficient of second
%hermite polynomial
g2Sqr=(sign(g2H0).*g2H0^2+sign(g2H(1)).*g2H(1).^2+sign(g2H(2)).*g2H(2).^2*2+ ...
sign(g2H(3)).*g2H(3).^2*6+sign(g2H(4)).*g2H(4).^2*24+sign(g2H(5)).*g2H(5).^2*120);
g22=sign(g2Sqr).*sqrt(abs(g2Sqr));
%Below are derived hermite polynomial representation of SDE variable.
[cH0,cH] = ConvertZCoeffsToHCoeffs(c0,c,SeriesOrder);
%Drift hermite coefficients are added to SDE variable hermite
%coefficients variables linearly. This is done before adding variances
cH0=cH0+bH0;
cH=cH+bH;
%Since our diffusion term has only first and second hermites, we add
%them in a squared fashion to hermite coefficients of SDE variable.
%Other than first and second hermite, coefficients of other hermite
%polynomials representing the SDE variable remain the same.
wH0=cH0;
wH(1)=sign(cH(1)+g11).*sqrt(abs(sign(cH(1)).*cH(1).^2+g1Sqr));
wH(2)=sign(cH(2)+g22).*sqrt(abs(sign(cH(2)).*cH(2).^2+g2Sqr));
wH(3:SeriesOrder)=cH(3:SeriesOrder);
[c0,c] = ConvertHCoeffsToZCoeffs(wH0,wH,SeriesOrder);%
%Above we convert from hermite coefficients representation of SDE to
%simple Z-series representation.
w0=c0;
end
end
wnStart=1;
%yy0=((1-gamma).*w0).^(1/(1-gamma));
w(1:Nn)=c0;
for nn=1:ExpnOrder
w(1:Nn)=w(1:Nn)+c(nn)*Z(1:Nn).^nn;
end
%str=input('Look at numbers')
yy0(wnStart:Nn)=((1-gamma).*w(wnStart:Nn)).^(1/(1-gamma));
yy=yy0;
Dfyy(wnStart:Nn)=0;
%Dfyydt(wnStart:Nn)=0;
for nn=wnStart+1:Nn-1
Dfyy(nn) = (yy(nn + 1) - yy(nn - 1))/(Z(nn + 1) - Z(nn - 1));
% Dfyydt(nn) = (yydt(nn + 1) - yydt(nn - 1))/(Z(nn + 1) - Z(nn - 1));
%Change of variable derivative for densities
end
%Dfyy(Nn)=Dfyy(Nn-1);
%Dfyy(1)=Dfyy(2);
%Dfyydt(Nn)=Dfyydt(Nn-1);
%Dfyydt(1)=Dfyydt(2);
pyy(1:Nn)=0;
%pyydt(1:Nn)=0;
for nn = wnStart+1:Nn-1
pyy(nn) = (normpdf(Z(nn),0, 1))/abs(Dfyy(nn));
% pyydt(nn) = (normpdf(Z(nn),0, 1))/abs(Dfyydt(nn));
end
toc
ItoHermiteMean=sum(yy(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
disp('true Mean only applicable to standard SV mean reverting type models otherwise disregard');
TrueMean=theta+(x0-theta)*exp(-kappa*T)%Mean reverting SDE original variable true average
%yy0
theta1=1;
rng(29079137, 'twister')
paths=50000;
YY(1:paths)=x0; %Original process monte carlo.
%YYdt(1:paths)=0;
Random1(1:paths)=0;
for tt=1:TtM
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;
%YYdt(1:paths)=YYdt(1:paths)+.5*YY(1:paths)*dtM;
YY(1:paths)=YY(1:paths) + ...
(YCoeff0(1,1,2,1).*YY(1:paths).^Fp(1,1,2,1)+ ...
YCoeff0(1,2,1,1).*YY(1:paths).^Fp(1,2,1,1)+ ...
YCoeff0(2,1,1,1).*YY(1:paths).^Fp(2,1,1,1))*dtM + ...
(YCoeff0(1,1,3,1).*YY(1:paths).^Fp(1,1,3,1)+ ...
YCoeff0(1,2,2,1).*YY(1:paths).^Fp(1,2,2,1)+ ...
YCoeff0(2,1,2,1).*YY(1:paths).^Fp(2,1,2,1)+ ...
YCoeff0(1,3,1,1).*YY(1:paths).^Fp(1,3,1,1)+ ...
YCoeff0(2,2,1,1).*YY(1:paths).^Fp(2,2,1,1)+ ...
YCoeff0(3,1,1,1).*YY(1:paths).^Fp(3,1,1,1))*dtM^2 + ...
((YCoeff0(1,1,1,2).*YY(1:paths).^Fp(1,1,1,2).*sqrt(dtM))+ ...
(YCoeff0(1,1,2,2).*YY(1:paths).^Fp(1,1,2,2)+ ...
YCoeff0(1,2,1,2).*YY(1:paths).^Fp(1,2,1,2)+ ...
YCoeff0(2,1,1,2).*YY(1:paths).^Fp(2,1,1,2)).*dtM^1.5) .*HermiteP1(2,1:paths) + ...
((YCoeff0(1,1,1,3).*YY(1:paths).^Fp(1,1,1,3) *dtM) + ...
(YCoeff0(1,1,2,3).*YY(1:paths).^Fp(1,1,2,3)+ ...
YCoeff0(1,2,1,3).*YY(1:paths).^Fp(1,2,1,3)+ ...
YCoeff0(2,1,1,3).*YY(1:paths).^Fp(2,1,1,3)).*dtM^2).*HermiteP1(3,1:paths) + ...
((YCoeff0(1,1,1,4).*YY(1:paths).^Fp(1,1,1,4)*dtM^1.5 )).*HermiteP1(4,1:paths) + ...
(YCoeff0(1,1,1,5).*YY(1:paths).^Fp(1,1,1,5)*dtM^2.0).*HermiteP1(5,1:paths);
% YYdt(1:paths)=YYdt(1:paths)+.5*YY(1:paths)*dtM;
end
YY(YY<0)=0;
disp('Original process average from monte carlo');
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
disp('Original process average from our simulation');
ItoHermiteMean=sum(yy(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
%ItoHermiteMeanPI=sum(yydt(wnStart:Nn).*ZProb(wnStart:Nn))
%MCMeanPI=sum(YYdt(:))/paths
disp('true Mean only applicble to standard SV mean reverting type models otherwise disregard');
TrueMean=theta+(x0-theta)*exp(-kappa*T)%Mean reverting SDE original variable true average
MaxCutOff=50;
NoOfBins=round(8*500*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(yy(wnStart+1:Nn-1),pyy(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
%plot(y_w(wnStart+1:Nn-1),fy_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g',Z(wnStart+1:Nn-1),fy_w(wnStart+1:Nn-1),'b');
%title(sprintf('x0 = %.4f,theta=%.3f,kappa=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,TM=%.4f', x0,theta,kappa,gamma,sigma0,T,dt,ItoHermiteMean,TrueMean));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
title(sprintf('x0 = %.4f,mu1=%.3f,beta1=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,TM=%.4f', x0,mu1,beta1,gamma,sigma0,T,dt,ItoHermiteMean,TrueMean));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
legend({'Ito-Hermite Density','Monte Carlo Density'},'Location','northeast')
str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
%NoOfBins=round(1*500*gamma^2*4*sigma0/sqrt(MCMean)/(1+kappa))/2;
%[YdtDensity,IndexOutYdt,IndexMaxYdt] = MakeDensityFromSimulation_Infiniti_NEW(YYdt,paths,NoOfBins,MaxCutOff );
%plot(yydt(wnStart+1:Nn-1),pyydt(wnStart+1:Nn-1),'r',IndexOutYdt(1:IndexMaxYdt),YdtDensity(1:IndexMaxYdt),'g');
%title(sprintf('x0 = %.4f,theta=%.3f,kappa=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,MCM=%.4f', x0,theta,kappa,gamma,sigma0,T,dt,ItoHermiteMeanPI,MCMeanPI));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
%title(sprintf('x0 = %.4f,kappa=%.3f,theta=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,MCM=%.4f', x0,kappa,theta,gamma,sigma0,T,dt,ItoHermiteMeanPI,MCMeanPI));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
%legend({'Ito-Hermite Path Integral Density','Monte Carlo Path Integral Density'},'Location','northeast')
%str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
function [] = FPESeriesCoeffsBesselWithVariance04()
%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 +sigma x(t)^gamma dz(t)
%I have not directly simulated the SDE but simulated the transformed
%Besse1l process version of the SDE and then changed coordinates to retreive
%the SDE in original coo
%rdinates.
%The present program will analytically evolve only the Bessel Process version of the
%SDE in transformed coordinates.
dt=.125/2/2/2; % Simulation time interval.%Fodiffusions close to zero
%decrease dt for accuracy.
Tt=32*2*10;%16*2*4;%*4*4*1;%*4;%128*2*2*2; % Number of simulation levels. Terminal time= Tt*dt; //.125/32*32*16=2 year;
T=Tt*dt;
Ts=Tt;
ds(1:Ts)=dt;
%
% %Below I have done calculations for smaller step size at start.
%
% %ds(1:64)=dt/16;
% %ds(65:128)=dt/4;
% if(Tt<=4)
% Ts=(Tt*8);%+(64-16);
% ds(1:Ts)=dt/8;
% end
% if(Tt>4)&&(Tt<=20)
% Ts=(32)+((Tt-4)*2);
% ds(1:32)=dt/8;
% ds(33:Ts)=dt/2;
% end
% if(Tt>20)
% Ts=(32)+((20-4)*2)+(Tt-20);
% ds(1:32)=dt/8;
% ds(33:64)=dt/2;
% ds(65:Ts)=dt;
% end
% T
% sum(ds(1:Ts))
% Ts
%str=input('Check if time is right');
OrderA=4; %
OrderM=4; %
if(T>=1)
dtM=1/16;%.0625/1;
TtM=T/dtM;
else
dtM=T/16;%.0625/1;
TtM=T/dtM;
end
%dtM=dt*8;
%TtM=Tt/8;
dNn=.2/2; % Normal density subdivisions width. would change with number of subdivisions
Nn=47*2; % No of normal density subdivisions
NnMid=4.0;
x0=2.0; % starting value of SDE
beta1=0.65;
beta2=1.0; % Second drift term power.
%beta1=0.950;
%beta2=0.0; % Second drift term power.
gamma=.95;%50; % volatility power.
kappa=1.00;%.950; %mean reversion parameter.
theta=1.0;%mean reversion target
sigma0=.250;%Volatility value
%you can specify any general mu1 and mu2 and beta1 and beta2.
mu1=.10; %first drift coefficient.
mu2=0;%-1*kappa; % Second drift coefficient.
%mu1=theta*kappa;
%mu2=-1*kappa;
alpha=1;% x^alpha is being expanded. This is currently for monte carlo only.
alpha1=1-gamma;%This is for expansion of integrals for calculation of drift
%and volatility coefficients
%yy(1:Nn)=x0;
w0=x0^(1-gamma)/(1-gamma);
Z(1:Nn)=(((1:Nn)-7.5/1)*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);
ZProb(2:Nn-1)=normcdf(.5*Z(2:Nn-1)+.5*Z(3:Nn),0,1)-normcdf(.5*Z(2:Nn-1)+.5*Z(1:Nn-2),0,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:TtM+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.
%YCoeff0 and YCoeff are coefficents for original coordinates monte carlo.
%YqCoeff0 and YqCoeff are bessel/lamperti version monte carlo.
YCoeff0(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;
YqCoeff0(1:(OrderA+1),1:(OrderA+1),1:(OrderA+1),1:(OrderA+1))=0;
%Pre-compute the time and power exponent values in small multi-dimensional arrays
YCoeff = ItoTaylorCoeffsNew(alpha,beta1,beta2,gamma); %expand y^alpha where alpha=1;
YqCoeff = ItoTaylorCoeffsNew(alpha1,beta1,beta2,gamma);%expand y^alpha1 where alpha1=(1-gamma)
YqCoeff=YqCoeff/(1-gamma); %Transformed coordinates coefficients have to be
%further divided by (1-gamma)
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;
%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));
YCoeff0(l1,l2,l3,l4) =YCoeff(l1,l2,l3,l4).*mu11(l1).*mu22(l2).*sigma22(l3).*sigma11(l4);
YqCoeff0(l1,l2,l3,l4) =YqCoeff(l1,l2,l3,l4).*mu11(l1).*mu22(l2).*sigma22(l3).*sigma11(l4);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ExpnOrder=5;
SeriesOrder=5;
wnStart=1;
tic
for tt=1:Ts
tt
if(tt==1)
[wMu0dt,dwMu0dtdw] = BesselDriftAndDerivativesH0(w0,YqCoeff0,Fp1,gamma,ds(tt),6);
c0=wMu0dt+w0;
c(1)=sigma0*sqrt(ds(tt));
c(2:10)=0.0;
w0=c0;
elseif(tt>=2)
[wMu0dt,dwMu0dtdw] = BesselDriftAndDerivativesH0(w0,YqCoeff0,Fp1,gamma,ds(tt),6);
%Below we get Z-series representation of drift term.
[b0,b] = CalculateDriftbCoeffs08A(wMu0dt,dwMu0dtdw,c,SeriesOrder);
%Below we convert Z_series of drift term to hermite representation
[bH0,bH] = ConvertZCoeffsToHCoeffs(b0,b,SeriesOrder);
[wVol0dt,dwVol0dtdw] = BesselVolAndDerivativesH1(w0,YqCoeff0,Fp1,gamma,ds(tt),SeriesOrder+1);
%Below we get Z-series representation of coefficient of first hermite polynomial in diffusion term.
[g10,g1] = CalculateDriftbCoeffs08A(wVol0dt,dwVol0dtdw,c,SeriesOrder);
g10=g10+sigma0*sqrt(dt);
%The Z_series(called g1) below is coefficient of first hermite polynomial
%g10+g1(1)*Z+g1(2)*Z^2+g1(3)*Z^3+g1(4)*Z^4+g1(5)*Z^5
[g1H0,g1H] = ConvertZCoeffsToHCoeffs(g10,g1,SeriesOrder);
%g1Sqr is variance of Z_series g1 that makes coefficient of first
%hermite polynomial
g1Sqr=(sign(g1H0).*g1H0^2+sign(g1H(1)).*g1H(1).^2+sign(g1H(2)).*g1H(2).^2*2+ ...
sign(g1H(3)).*g1H(3).^2*6+sign(g1H(4)).*g1H(4).^2*24+sign(g1H(5)).*g1H(5).^2*120);
g11=sign(g1Sqr).*sqrt(abs(g1Sqr));
[wVol2dt,dwVol2dtdw] = BesselVolAndDerivativesH2(w0,YqCoeff0,Fp1,gamma,ds(tt),SeriesOrder+1);
%Below we get Z-series representation of coefficient of second hermite polynomial in diffusion term.
[g20,g2] = CalculateDriftbCoeffs08A(wVol2dt,dwVol2dtdw,c,SeriesOrder);
%The Z_series(called g2) below is coefficient of second hermite polynomial
%g20+g2(1)*Z+g2(2)*Z^2+g2(3)*Z^3+g2(4)*Z^4+g2(5)*Z^5
[g2H0,g2H] = ConvertZCoeffsToHCoeffs(g20,g2,SeriesOrder);
%g2Sqr is variance of Z_series g2 that makes coefficient of second
%hermite polynomial
g2Sqr=(sign(g2H0).*g2H0^2+sign(g2H(1)).*g2H(1).^2+sign(g2H(2)).*g2H(2).^2*2+ ...
sign(g2H(3)).*g2H(3).^2*6+sign(g2H(4)).*g2H(4).^2*24+sign(g2H(5)).*g2H(5).^2*120);
g22=sign(g2Sqr).*sqrt(abs(g2Sqr));
%Below are derived hermite polynomial representation of SDE variable.
[cH0,cH] = ConvertZCoeffsToHCoeffs(c0,c,SeriesOrder);
%Drift hermite coefficients are added to SDE variable hermite
%coefficients variables linearly. This is done before adding variances
cH0=cH0+bH0;
cH=cH+bH;
%Since our diffusion term has only first and second hermites, we add
%them in a squared fashion to hermite coefficients of SDE variable.
%Other than first and second hermite, coefficients of other hermite
%polynomials representing the SDE variable remain the same.
wH0=cH0;
wH(1)=sign(cH(1)+g11).*sqrt(abs(sign(cH(1)).*cH(1).^2+g1Sqr));
wH(2)=sign(cH(2)+g22).*sqrt(abs(sign(cH(2)).*cH(2).^2+g2Sqr));
wH(3:SeriesOrder)=cH(3:SeriesOrder);
[c0,c] = ConvertHCoeffsToZCoeffs(wH0,wH,SeriesOrder);%
%Above we convert from hermite coefficients representation of SDE to
%simple Z-series representation.
w0=c0;
end
end
wnStart=1;
%yy0=((1-gamma).*w0).^(1/(1-gamma));
w(1:Nn)=c0;
for nn=1:ExpnOrder
w(1:Nn)=w(1:Nn)+c(nn)*Z(1:Nn).^nn;
end
%str=input('Look at numbers')
yy0(wnStart:Nn)=((1-gamma).*w(wnStart:Nn)).^(1/(1-gamma));
yy=yy0;
Dfyy(wnStart:Nn)=0;
%Dfyydt(wnStart:Nn)=0;
for nn=wnStart+1:Nn-1
Dfyy(nn) = (yy(nn + 1) - yy(nn - 1))/(Z(nn + 1) - Z(nn - 1));
% Dfyydt(nn) = (yydt(nn + 1) - yydt(nn - 1))/(Z(nn + 1) - Z(nn - 1));
%Change of variable derivative for densities
end
%Dfyy(Nn)=Dfyy(Nn-1);
%Dfyy(1)=Dfyy(2);
%Dfyydt(Nn)=Dfyydt(Nn-1);
%Dfyydt(1)=Dfyydt(2);
pyy(1:Nn)=0;
%pyydt(1:Nn)=0;
for nn = wnStart+1:Nn-1
pyy(nn) = (normpdf(Z(nn),0, 1))/abs(Dfyy(nn));
% pyydt(nn) = (normpdf(Z(nn),0, 1))/abs(Dfyydt(nn));
end
toc
ItoHermiteMean=sum(yy(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
disp('true Mean only applicable to standard SV mean reverting type models otherwise disregard');
TrueMean=theta+(x0-theta)*exp(-kappa*T)%Mean reverting SDE original variable true average
%yy0
theta1=1;
rng(29079137, 'twister')
paths=50000;
YY(1:paths)=x0; %Original process monte carlo.
%YYdt(1:paths)=0;
Random1(1:paths)=0;
for tt=1:TtM
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;
%YYdt(1:paths)=YYdt(1:paths)+.5*YY(1:paths)*dtM;
YY(1:paths)=YY(1:paths) + ...
(YCoeff0(1,1,2,1).*YY(1:paths).^Fp(1,1,2,1)+ ...
YCoeff0(1,2,1,1).*YY(1:paths).^Fp(1,2,1,1)+ ...
YCoeff0(2,1,1,1).*YY(1:paths).^Fp(2,1,1,1))*dtM + ...
(YCoeff0(1,1,3,1).*YY(1:paths).^Fp(1,1,3,1)+ ...
YCoeff0(1,2,2,1).*YY(1:paths).^Fp(1,2,2,1)+ ...
YCoeff0(2,1,2,1).*YY(1:paths).^Fp(2,1,2,1)+ ...
YCoeff0(1,3,1,1).*YY(1:paths).^Fp(1,3,1,1)+ ...
YCoeff0(2,2,1,1).*YY(1:paths).^Fp(2,2,1,1)+ ...
YCoeff0(3,1,1,1).*YY(1:paths).^Fp(3,1,1,1))*dtM^2 + ...
((YCoeff0(1,1,1,2).*YY(1:paths).^Fp(1,1,1,2).*sqrt(dtM))+ ...
(YCoeff0(1,1,2,2).*YY(1:paths).^Fp(1,1,2,2)+ ...
YCoeff0(1,2,1,2).*YY(1:paths).^Fp(1,2,1,2)+ ...
YCoeff0(2,1,1,2).*YY(1:paths).^Fp(2,1,1,2)).*dtM^1.5) .*HermiteP1(2,1:paths) + ...
((YCoeff0(1,1,1,3).*YY(1:paths).^Fp(1,1,1,3) *dtM) + ...
(YCoeff0(1,1,2,3).*YY(1:paths).^Fp(1,1,2,3)+ ...
YCoeff0(1,2,1,3).*YY(1:paths).^Fp(1,2,1,3)+ ...
YCoeff0(2,1,1,3).*YY(1:paths).^Fp(2,1,1,3)).*dtM^2).*HermiteP1(3,1:paths) + ...
((YCoeff0(1,1,1,4).*YY(1:paths).^Fp(1,1,1,4)*dtM^1.5 )).*HermiteP1(4,1:paths) + ...
(YCoeff0(1,1,1,5).*YY(1:paths).^Fp(1,1,1,5)*dtM^2.0).*HermiteP1(5,1:paths);
% YYdt(1:paths)=YYdt(1:paths)+.5*YY(1:paths)*dtM;
end
YY(YY<0)=0;
disp('Original process average from monte carlo');
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
disp('Original process average from our simulation');
ItoHermiteMean=sum(yy(wnStart:Nn).*ZProb(wnStart:Nn)) %Original process average from coordinates
%ItoHermiteMeanPI=sum(yydt(wnStart:Nn).*ZProb(wnStart:Nn))
%MCMeanPI=sum(YYdt(:))/paths
disp('true Mean only applicble to standard SV mean reverting type models otherwise disregard');
TrueMean=theta+(x0-theta)*exp(-kappa*T)%Mean reverting SDE original variable true average
MaxCutOff=50;
NoOfBins=round(8*500*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(yy(wnStart+1:Nn-1),pyy(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');
%plot(y_w(wnStart+1:Nn-1),fy_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g',Z(wnStart+1:Nn-1),fy_w(wnStart+1:Nn-1),'b');
%title(sprintf('x0 = %.4f,theta=%.3f,kappa=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,TM=%.4f', x0,theta,kappa,gamma,sigma0,T,dt,ItoHermiteMean,TrueMean));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
title(sprintf('x0 = %.4f,mu1=%.3f,beta1=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,TM=%.4f', x0,mu1,beta1,gamma,sigma0,T,dt,ItoHermiteMean,TrueMean));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
legend({'Ito-Hermite Density','Monte Carlo Density'},'Location','northeast')
str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
%NoOfBins=round(1*500*gamma^2*4*sigma0/sqrt(MCMean)/(1+kappa))/2;
%[YdtDensity,IndexOutYdt,IndexMaxYdt] = MakeDensityFromSimulation_Infiniti_NEW(YYdt,paths,NoOfBins,MaxCutOff );
%plot(yydt(wnStart+1:Nn-1),pyydt(wnStart+1:Nn-1),'r',IndexOutYdt(1:IndexMaxYdt),YdtDensity(1:IndexMaxYdt),'g');
%title(sprintf('x0 = %.4f,theta=%.3f,kappa=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,MCM=%.4f', x0,theta,kappa,gamma,sigma0,T,dt,ItoHermiteMeanPI,MCMeanPI));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
%title(sprintf('x0 = %.4f,kappa=%.3f,theta=%.2f,gamma=%.3f,sigma=%.2f,T=%.2f,dt=%.5f,M=%.4f,MCM=%.4f', x0,kappa,theta,gamma,sigma0,T,dt,ItoHermiteMeanPI,MCMeanPI));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
%legend({'Ito-Hermite Path Integral Density','Monte Carlo Path Integral Density'},'Location','northeast')
%str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
.To find the solution of density of SDE along constant CDF (or Constant Z-lines) lines, we suppose that we can express the solution of w(t) where w is the SDE/PDE variable as a power series in Z. After all the research we have done in past few months, it seems very natural that we express w(t) as a series in Z for any given time along time evolution of the density. We find the coordinates of this series at median of the density where Z=0. If we can represent w(t) at any given time as a valid series in Z, we can find all its derivatives and other smooth functions and also represent them as a series in Z. For a given evolution equation of the constant CDF lines of the density, we can represent all the variables in right hand side as a series in Z. The products, reciprocals and integrals of theses series again result as answers in another series in Z. So we find power series in Z for all variables in the right hand side of evolution equation and then do all mathematical operations on them in the form of series. The resulting answer is based on our original series at time [$]t_0[$] but generally has different series coefficients than that of series at time [$]t_0[$]. This new series that follows the application of SDE evolution equation at series coefficients at time [$]t_0[$] represents the solution of constant CDF lines of SDE density [$]w(t_1)[$] at next time level [$]t_1[$] as a power series in Z again. Now we take this power series at time [$]t_1[$] and find the series representations of its derivatives and other smooth functions and substitute them again in SDE evolution equation to find the series representation at time [$]t_2[$].
I will write the proposed power series for w(t) and its derivatives and then explain how to find the power series of smooth functions of w when power series for w is given. The series for w(t) is given as
[$]w(t)=a_0 \,+ a_1 \, Z + a_2 \, Z^2 + a_3 \, Z^3 + a_4 \, Z^4 +\, ... [$]
The first, second and third derivatives of w(t) w.r.t Z are given as
[$]\frac{\partial w(t)}{\partial Z}= a_1 \, + 2 \,a_2 \, Z +3 \, a_3 \, Z^2 +4 \, a_4 \, Z^3 +\, ... [$]
[$]\frac{\partial^2 w(t)}{\partial Z^2}= 2 \,a_2 \, +6 \, a_3 \, Z +12 \, a_4 \, Z^2 +\, ... [$]
[$]\frac{\partial^3 w(t)}{\partial Z^3}= 6 \, a_3 \, +24 \, a_4 \, Z \,+60 \, a_5 \, Z^2 +\, ... [$]
We do all the calculations of series evolution equation at median where Z=0. here all the derivatives are given only by leading coefficient associated with zeroth power of Z. So at Z=0,
[$]w(t)=a_0 \,[$]
[$]\frac{\partial w(t)}{\partial Z}= a_1 \, [$]
[$]\frac{\partial^2 w(t)}{\partial Z^2}= 2 \,a_2 \, [$]
[$]\frac{\partial^3 w(t)}{\partial Z^3}= 6 \, a_3 \, [$]
In order to represent smooth functions of w(t) as a series in Z, we suppose a form of the series and equate its coefficients with analytic derivatives w.r.t Z and this calculation is done at median that is Z=0.
So let us take drift in SDE, [$]\mu(w)[$] as a smooth function of w and represent it as a power series in Z. In what follows [$]\mu(w)[$] is considered a function operating on w just like we use symbol [$]f(w)[$] to indicate that f is a function operating on w. We suppose that functional form of [$]\mu(w)[$] and its derivatives is known analytically.
Let us suppose we know the form of power series of [$]\mu(w)[$] with the coefficients at yet unknown. This power series is
[$]\mu(w(t))=b_0 \,+ b_1 \, Z + b_2 \, Z^2 + b_3 \, Z^3 + b_4 \, Z^4 +\, ... [$]
The first, second and third derivatives of [$]\mu(w(t))[$] w.r.t Z are given as
[$]\frac{\partial \mu(w(t))}{\partial Z}= b_1 \, + 2 \,b_2 \, Z +3 \, b_3 \, Z^2 +4 \, b_4 \, Z^3 +\, ... [$]
[$]\frac{\partial^2 \mu(w(t))}{\partial Z^2}= 2 \,b_2 \, +6 \, b_3 \, Z +12 \, b_4 \, Z^2 +\, ... [$]
[$]\frac{\partial^3 \mu(w(t))}{\partial Z^3}= 6 \, b_3 \, +24 \, b4 \, Z \,+60 \, b_5 \, Z^2 +\, ... [$]
We know that values of above drift and its derivatives are given by leading coefficients of the series when Z=0. So at Z=0,
[$]\mu(w(t))=b_0 \,[$]
[$]\frac{\partial \mu(w(t))}{\partial Z}= b_1 \, [$]
[$]\frac{\partial^2 \mu(w(t))}{\partial Z^2}= 2 \,b_2 \, [$]
[$]\frac{\partial^3 \mu(w(t))}{\partial Z^3}= 6 \, b_3 \, [$]
We also know the value of [$]w(t)[$] at median which is [$]a_0[$]
but [$]\mu(w(t))=b_0 \,[$] which means that [$]b_0=\mu(a_0) \,[$]
To find the second coefficient, we equate first derivative of drift w.r.t Z at [$]w_0=a_0[$] with its coefficient. We know that
[$]\frac{\partial \mu(w(t))}{\partial Z} \,= \, \frac{\partial \mu(w(t))}{\partial w} \,\frac{\partial w(t)}{\partial Z} \,[$] which equals
[$]=\, \frac{\partial \mu(w(t))}{\partial w} \, a_1[$]
but [$]\frac{\partial \mu(w(t))}{\partial Z}= b_1 \, [$]
so
[$]b_1=\, \frac{\partial \mu(w(t))}{\partial w} \, a_1=\, \frac{\partial \mu(a_0)}{\partial w} \, a_1[$]
Similarly
[$]\frac{\partial^2 \mu(w(t))}{\partial Z^2} \,= \, \frac{\partial^2 \mu(w(t))}{\partial w^2} \, {(\frac{\partial w(t)}{\partial Z} )}^2+ \, \, \frac{\partial \mu(w(t))}{\partial w} \,\frac{\partial^2 w(t)}{\partial Z^2} \,[$]
[$]= \, \frac{\partial^2 \mu(w(t))}{\partial w^2} {a_1}^2 \, + \, \, \, \frac{\partial \mu(w(t))}{\partial w} (2a_2)[$]
We equate this with [$]\frac{\partial^2 \mu(w(t))}{\partial Z^2}= 2 \,b_2 \, [$] to find out that
[$]b_2= 1/2 \, \Big[\, \frac{\partial^2 \mu(w(t))}{\partial w^2} {a_1}^2 \, + \, \, \, \frac{\partial \mu(w(t))}{\partial w} (2a_2) \Big][$]
since above values of derivatives of w are calculated at [$]w=a_0[$], we can write
[$]b_2= 1/2 \, \Big[\, \frac{\partial^2 \mu(a_0)}{\partial w^2} {a_1}^2 \, + \, \, \, \frac{\partial \mu(a_0)}{\partial w} (2a_2) \Big][$]
similarly equating third derivative we get
[$]6 b_3= \, \frac{\partial^3 \mu(w)}{\partial w^3} {(\frac{\partial w(t)}{\partial Z} )}^3 \, + \,3 \, \frac{\partial^2 \mu(w)}{\partial w^2} \frac{\partial w(t)}{\partial Z} \frac{\partial^2 w(t)}{\partial Z^2} \, + \, \frac{\partial \mu(w)}{\partial w} \frac{\partial^3 w(t)}{\partial Z^3} \, [$]
which means
[$] b_3= 1/6 \, \Big[\, \frac{\partial^3 \mu(a_0)}{\partial w^3} {a_1}^3 \, + \,6 \, \frac{\partial^2 \mu(a_0)}{\partial w^2}\, a_1 \, a_2 + \, \frac{\partial \mu(a_0)}{\partial w} \, 6a_3 \, \Big][$]
So this way we continue to find the power series of smooth functions of w by equating the series coefficients for derivatives at Z=0 with their functional form there and using chain rule of derivatives.
I am very sure that similar series method can be used for a lot of other partial differential equations where solution remains smooth and well-behaved and series representation converges well.
In next relatively non-technical post, I will just give an idea how to relate to variables I have used in my matlab implementation to equations in this and previous post. In another later post, I will explain some enhancements I have used to get the series solution method to get to work better closer to zero.