Serving the Quantitative Finance Community

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

I am writing this post to let friends know that mind control torture is not decreasing at all. They use a huge amount of sickening gas in my bath room  and in my room. Bottled water in the city is all drugged but water in public supply was good almost everywhere when I arrived from Islamabad about ten days ago. But they are slowly drugging public supply water in a lot of areas and if it continues it will become extremely difficult to get water in the city again. They still continue to use all sort of ugly tricks for example they would focus low intensity microwave on my forehead and it would cause light intensity but unsettling pain that would not let me concentrate on anything. food in home is also not very good since it would almost always make me lethargic though it would not usually shock my brain as happened in the past several times.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Friends, I went ahead and added the effect of second hermite polynomial on top of the previous analytics. I was earlier able to get my self to sit and try to work but could not do much due to effect of the injection. But I was able to work out things on computer today and I did not do very advanced analytics like integrating the effect on the originating grid cell. Though that can easily be done, I thought that it would be an overkill at least as a start. Adding second hermite polynomial does improve the fit of shape of the density to monte carlo density.
The idea behind my improvisation is that if we want to solve an equation for Z given the final points and starting points while two hermite polynomials are involved, we really have to solve a quadratic equation. But once we know what the particular Z is which solves an equation given the final point and initial point with just one first hermite polynomial, we could simply adjust the final point after adding the effect of second hermite polynomial with the same Z which solved the first order equation and since Z remains the same, all CDF, PDF and derivatives with respect to Z will be carried forward to the adjusted final point(which we have found after adding the effect of second hermite polynomial for the particular Z that solved the "first final" and initial points with just one hermite polynomial). Only final point is adjusted and since all derivatives of drift and volatility are calculated with respect to initial point grid which does not change and is not adjusted so we do not need to alter those analytics at all. So if we have the liberty of altering the actual grid, we can in essence, using this way, turn our implicit problem into a simpler problem with an explicit solution.
Even though there is a great improvement due to addition of second hermite polynomial effect, I think even for step size I used, we will also need third hermite when parameters are extreme. But that would follow just as simply.
Though I want to make further changes, I will still post the new program with this improvement most probably later today.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Post in progress.

Here are new intermediate version of programs without comments.
function [] = SDETransProb08WmtGrid200b53Drift()

%Copyright Ahsan Amin. Infiniti derivatives Technologies.
%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)

%I have not directly simulated the SDE but simulated the transformed
%Besse1 process version of the SDE and then changed coordinates to retreive
%the SDE in original coordinates.
%The present program will analytically evolve only the Bessel Process version of the
%SDE in transformed coordinates.

%In this program, I have simulated the density for first sixteen time
%intervals using a simple method. After first sixteen intervals, I start
%simulating with probability mass transfer method. I am presenting a
%simpler version which is easier to understand. I have advanced the density
%here for only two steps with mass transfer method. 17 to 18 intervals.
%My extrapolations at the boundaries are not very good and I suggest that
%you use your favorite method for that. Same for caclualtion of higher
%order derivatives. With above improvements, you can easily continue the
%method for whatever number of steps. Please experiment with step size and
%you will find the method robust enough. This is just an early version and
%I will be coming with better versions soon.

dt=.125/16/2/2/2;   % Regular Simulation time interval to advance the density before large step by CDF method.
%decrease dt for accuracy.
dt2=1/16;% One step simulation step size for new method with CDF taylor expansion.
%T=Tt*dt;
Tt1=32;
Tt=Tt1+1;%Total time intervlas including small steps and large step.
T=(Tt-1)*dt+dt2;%Total time elapsed.
OrderA=4;  %
dtM=dt;%.125/2/4;%Monte carlo time interval size dtM.
TtM=Tt;%T*2*8*4;%Monte carlo number of simulation intervals.

dNn=.05;   % Normal density subdivisions width. would change with number of subdivisions
Nn=200;  % No of normal density subdivisions
NnMidl=100;%One half density Subdivision left from mid of normal density(low)
NnMidh=101;%One half density subdivision right from the mid of normal density(high)
NnMid=4.0;

Z(1:Nn)=(((1:Nn)-20.5)*dNn-NnMid)

%There are two indices. One goes to Nn=200. This is used for calculation of
%SDE simulation at next time level t+1. This covers -5 to +5 SDs of
%underlying Z.
%Second index Nn1 below goes from 1 to 240. This covers -6 to +6 SD of
%underlying Z. In probability mass transfer methos, we simulate the SDE
%from -5 SD to +5 SD and then extrapolate on both sides so that we have a
%new grid from -6 SD to +6 SD(1:Nn1). This extrapolated grid(which is assigned time t)
%is used to calculate the time t+1 grid from -5 SD to +5 SD(1:Nn).
%Grid spacing is the same on both grids.
Nn1=240;
%dNn1=.05;
Nn1Midl=120;
Nn1Midh=121;
Nn1Mid=6.025;

Z1(1:Nn1)=((1:Nn1)*dNn-Nn1Mid)
Z1Prob(1)=normcdf(-5.95)-normcdf(-6.0);
Z1Prob(Nn1)=normcdf(6.0)-normcdf(5.95);
Z1Prob(2:Nn1-1)=normcdf(.5*Z1(2:Nn1-1)+.5*Z1(3:Nn1),0,1)-normcdf(.5*Z1(2:Nn1-1)+.5*Z1(1:Nn1-2),0,1);

ZProb(1:Nn)=Z1Prob(21:Nn1-20);
Z(1:Nn)=Z1(21:Nn1-20);

Z1Prob
sum(Z1Prob(1:Nn1))
sum(ZProb(1:Nn))
str=input('Look at Zs');

x0=.1;   % starting value of SDE
beta1=0.0;
beta2=1.00;   % Second drift term power.
gamma=.65;%50;   % volatility power.
kappa=2.0;%.950;   %mean reversion parameter.
theta=.250;%mean reversion target
sigma0=.850;%Volatility value

%you can specify any general mu1 and mu2 and beta1 and beta2.
mu1=+1*theta*kappa;   %first drift coefficient.
mu2=-1*kappa;    % Second drift coefficient.
%mu1=0;
%mu2=0;

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

w(1:Nn)=x0^(1-gamma)/(1-gamma);

Z
Z1
str=input('Look at Zs');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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;

mu33(1)=1;
mu44(1)=1;
mu33(2:OrderA+1)=0;
mu44(2:OrderA+1)=0;

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);
YqzCoeff0(l1,l2,l3,l4) =YqCoeff(l1,l2,l3,l4).*mu33(l1).*mu44(l2).*sigma22(l3).*sigma11(l4);
end
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wnStart=1;%
yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
%The above yy(wnStart:Nn)=x0;

tic

for tt=1:Tt
t2=tt*dt;
t1=(tt-1)*dt;
if(tt==1)

[wMu0dt,c1,c22] = CalculateDriftAndVolA8Trans(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);
w(1:Nn)=x0^(1-gamma)/(1-gamma)+wMu0dt(1)+c1(1).*Z(1:Nn);

end
if((tt>1) && (tt<=Tt1))
%The first sixteen steps are simulated using a simple and slightly
%altered version of the method I have used for simple CEV noises
%and brownian motions. I used it only for starting few time steps
%since density remains pretty much very close to linear here. If
%that is not the case, you would have to alter this.
[wMu0dt,wMu1dt,dwMu0dtdw,d2wMu0dtdw2,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);
%dw(wnStart:Nn)=c1(wnStart:Nn);%.*Z(wnStart:Nn) ;% ...
dw(wnStart:Nn)=c1(wnStart:Nn).*Z(wnStart:Nn) ;% ...
dw2(wnStart:Nn)=dw(wnStart:Nn).^2;
[wMid] = InterpolateOrderN8(8,0,Z(NnMidl-3),Z(NnMidl-2),Z(NnMidl-1),Z(NnMidl),Z(NnMidh),Z(NnMidh+1),Z(NnMidh+2),Z(NnMidh+3),w(NnMidl-3),w(NnMidl-2),w(NnMidl-1),w(NnMidl),w(NnMidh),w(NnMidh+1),w(NnMidh+2),w(NnMidh+3));
B(1:Nn)=w(1:Nn)-wMid;
[dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,B,Z);
B(wnStart:Nn)=sign(B(wnStart:Nn)+dw(wnStart:Nn)).* ...
sqrt(abs(sign(B(wnStart:Nn)).*(B(wnStart:Nn)).^2+ ...
sign(dw(wnStart:Nn)).*dw2(wnStart:Nn)));

w(wnStart:Nn)=wMid+wMu0dt(wnStart:Nn)+B(wnStart:Nn);

end
%Now starts the calculation for SDE simulation using probability mass
%transfer.
if(tt>Tt1)
if(tt>Tt1)
dt=dt2;
end

[wMu0dt,wMu1dt,dwMu0dtdw,d2wMu0dtdw2,c1,c2] = CalculateDriftAndVolA404B(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);

dw(wnStart:Nn)=c1(wnStart:Nn) ;% ...
dw2(wnStart:Nn)=dw(wnStart:Nn).^2;
%[wMid] = InterpolateOrderN6(6,0,Z(NnMidl-2),Z(NnMidl-1),Z(NnMidl),Z(NnMidh),Z(NnMidh+1),Z(NnMidh+2),w(NnMidl-2),w(NnMidl-1),w(NnMidl),w(NnMidh),w(NnMidh+1),w(NnMidh+2));
[wMid] = InterpolateOrderN8(8,0,Z(NnMidl-3),Z(NnMidl-2),Z(NnMidl-1),Z(NnMidl),Z(NnMidh),Z(NnMidh+1),Z(NnMidh+2),Z(NnMidh+3),w(NnMidl-3),w(NnMidl-2),w(NnMidl-1),w(NnMidl),w(NnMidh),w(NnMidh+1),w(NnMidh+2),w(NnMidh+3));
B(1:Nn)=w(1:Nn)-wMid;
B1=B;
[dBdZ_0,d2BdZ2_0,d3BdZ3_0] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,B,Z);
%Above are derivative names with an appended zero "_0". These are derivatives on the
%Nn grid (time t+1 grid). When there are no appended zeros, those are
%derivatives on the Nn1 grid i.e time t grid.

if(tt<=64)
for nn=1:20
xx0(nn)=Z(1)-nn*dNn;  %Z values corresponding to points to be extrapolated.
xx1(nn)=Z(nn);        %Z Values corresponding to boundary points used to extrapolate.%We use only first ten out of 20.
yy1(nn)=B1(nn);       %resultant values for points used to extrapolate.%We use only first ten out of 20.
end
%%[yy0] = InterpolateOrderN10(10,xx0,xx1(1),xx1(2),xx1(3),xx1(4),xx1(5),xx1(6),xx1(7),xx1(8),xx1(9),xx1(10),yy1(1),yy1(2),yy1(3),yy1(4),yy1(5),yy1(6),yy1(7),yy1(8),yy1(9),yy1(10));
[yy0] = InterpolateOrderN10(10,xx0,xx1(1),xx1(2),xx1(3),xx1(4),xx1(5),xx1(6),xx1(7),xx1(8),xx1(9),xx1(10),yy1(1),yy1(2),yy1(3),yy1(4),yy1(5),yy1(6),yy1(7),yy1(8),yy1(9),yy1(10));
%In the loop below, the interpolated values are assigned to
%variable BB1 which extends form 1:Nn1. We would use this
%variable BB1(Bm(t) in the notes) later as values at time t to simulate the values
%of Bn at time t+1.
for nn=1:20
BB1(21-nn)=yy0(nn);
%%BB1(21-nn)=Btemp-yy0(nn)*dNn;
%%Btemp=BB1(21-nn);
end
%In the loop below main body of the variable is allocated to
%the array BB1 in notes "Bm(t)"
for nn=1:Nn
BB1(nn+20)=B1(nn);
end

%Below we do extrapolation on right boundary from 5SD to 6 SD
%and then assign it to BB1 which is Bm(t).
for nn=1:20
xx0(nn)=Z(Nn)+nn*dNn;
xx1(nn)=Z(Nn-nn+1);
%%yy1(nn)=dBdZ_0(Nn-nn+1);
yy1(nn)=B1(Nn-nn+1);
end
[yy0] = InterpolateOrderN10(10,xx0,xx1(1),xx1(2),xx1(3),xx1(4),xx1(5),xx1(6),xx1(7),xx1(8),xx1(9),xx1(10),yy1(1),yy1(2),yy1(3),yy1(4),yy1(5),yy1(6),yy1(7),yy1(8),yy1(9),yy1(10));

for nn=1:20
BB1(Nn+20+nn)=yy0(nn);

end

%The function below calculates grid cell boundaries from mid
%points of the grids using interpolation and relevant Z. The
%grid is equidistant and symmetric with respect to underlying Z
%but not with respect to the Bessel variable. We use the value
%of Z at boundaries of each cell to interpolate the value of
%bessel variable there.
[BB1a,BB1b] = CalculateGridStartsAndEndsBoundedEnds(BB1,Z1,1,Nn1,dNn);

%Below,I later assigned Z1 to ZZ1 and N1 to NN1 ans similarly for
%Z1Prob since earlier I had several(three different) cases of
%extrapolation ranging from 5 to 6 SD, and 5 to 5.5 SD and 5 to 5.25 SD.
%I Was thinking when time would advance and grid would have
%expanded and if dt would stay the same, the user might be
%better off choosing a smaller grid with better extrapolation.

ZZ1=Z1;
NN1=Nn1;
ZZProb(1:NN1)=Z1Prob(1:Nn1);
end
%plot(ZZ1(1:NN1),BB1(1:NN1),'b',ZZ1(1:NN1),BB1a(1:NN1),'y',ZZ1(1:NN1),BB1b(1:NN1),'k',Z(1:Nn),B1(1:Nn),'r')
%str=input('Look at B1 and BB1, original and extrapolated version and boundaries');

%robust function.
[dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(1,NN1,dNn,BB1,Z1);
[dwdZAA,d2wdZ2AA,d3wdZ3AA,d4BdZ4] = First4Derivatives2ndOrderEqSpaced(1,NN1,dNn,BB1,Z1);

%plot((1:NN1),dBdZ(1:NN1),'r')
%str=input('Look at 1st derivative');
%plot((1:NN1),d2BdZ2(1:NN1),'r')
%str=input('Look at 2nd derivative');
%plot((1:NN1),d3BdZ3(1:NN1),'r')
%str=input('Look at 3rd derivative');

%Below calculate hermite polynomials of Z1 underlying the
%Grid BB1 which is "Bm(t)" grid. We will use these hermite
%polynomials and later derivatives to calculate the derivatives of
%density of Bm with respect to Bm. dpdBm calcualted at centre
%of the grid cells.

He1(1:NN1)=Z1(1:NN1);
He2(1:NN1)=Z1(1:NN1).^2-1;
He3(1:NN1)=Z1(1:NN1).^3-3*Z1(1:NN1);
He4(1:NN1)=Z1(1:NN1).^4-6*Z1(1:NN1).^2+3;
He5(1:NN1)=Z1(1:NN1).^5-10*Z1(1:NN1).^3+15;
He6(1:NN1)=Z1(1:NN1).^6-15*Z1(1:NN1).^4+45*Z1(1:NN1).^2-15;
He7(1:NN1)=Z1(1:NN1).^7-21*Z1(1:NN1).^5+105*Z1(1:NN1).^3-105*Z1(1:NN1);
He8(1:NN1)=Z1(1:NN1).^8-28*Z1(1:NN1).^6+210*Z1(1:NN1).^4-420*Z1(1:NN1).^2+105;
He9(1:NN1)=Z1(1:NN1).^9-36*Z1(1:NN1).^7+378*Z1(1:NN1).^5-1260*Z1(1:NN1).^3+945*Z1(1:NN1);

Zpdf(1:NN1)=normpdf(Z1(1:NN1),0,1);
dZpdf(1:NN1)=-He1(1:NN1).*Zpdf(1:NN1);
d2Zpdf(1:NN1)=He2(1:NN1).*Zpdf(1:NN1);
d3Zpdf(1:NN1)=-He3(1:NN1).*Zpdf(1:NN1);
d4Zpdf(1:NN1)=He4(1:NN1).*Zpdf(1:NN1);
d5Zpdf(1:NN1)=-He5(1:NN1).*Zpdf(1:NN1);
d6Zpdf(1:NN1)=He6(1:NN1).*Zpdf(1:NN1);
d7Zpdf(1:NN1)=-He7(1:NN1).*Zpdf(1:NN1);
d8Zpdf(1:NN1)=He8(1:NN1).*Zpdf(1:NN1);
d9Zpdf(1:NN1)=-He9(1:NN1).*Zpdf(1:NN1);

%Please note that in calculations below, we are using derivatives without
%appended zeros since they are on BB1 "Bm(t)" grid that ranges from 1:Nn1 or equivalently 1:NN1.
dZdB(1:NN1)=1.0./dBdZ(1:NN1);
d2ZdB2(1:NN1)=-d2BdZ2(1:NN1).*dZdB(1:NN1).^3;
d3ZdB3(1:NN1)=-d3BdZ3(1:NN1).*dZdB(1:NN1).^4+3*d2BdZ2(1:NN1).^2.*dZdB(1:NN1).^5;
d4ZdB4(1:NN1)=-d4BdZ4(1:NN1).*dZdB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1:NN1).* d6ZdB6(1:NN1))+7.*dZpdf(1:NN1) .*dZdB(1:NN1).* d7ZdB7(1:NN1)+ ...
dZdB(1:NN1).* (105 .*d2ZdB2(1:NN1).^2.* d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+70.* dZdB(1:NN1) .*d3Zpdf(1:NN1) .*d3ZdB3(1:NN1).^2+ ...
105 .*dZdB(1:NN1).* d2ZdB2(1:NN1).^3.* d4Zpdf(1:NN1)+210.* dZdB(1:NN1).^2.* d2ZdB2(1:NN1).* d3ZdB3(1:NN1).* d4Zpdf(1:NN1)+ ...
105.* dZdB(1:NN1).* d2ZdB2(1:NN1).* d3Zpdf(1:NN1).* d4ZdB4(1:NN1)+35.*d2Zpdf(1:NN1).* d3ZdB3(1:NN1).* d4ZdB4(1:NN1)+ ...
35 * dZdB(1:NN1).^3.* d4Zpdf(1:NN1).* d4ZdB4(1:NN1)+105.* dZdB(1:NN1).^3 .*d2ZdB2(1:NN1).^2 .*d5Zpdf(1:NN1)+ ...
35.* dZdB(1:NN1).^4.* d3ZdB3(1:NN1).* d5Zpdf(1:NN1)+21.*d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d5ZdB5(1:NN1)+ ...
21.* dZdB(1:NN1).^2.* d3Zpdf(1:NN1).* d5ZdB5(1:NN1)+21 .*dZdB(1:NN1).^5.* d2ZdB2(1:NN1) .* d6Zpdf(1:NN1)+ ...
7.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d6ZdB6(1:NN1)+dZdB(1:NN1).^7.* d7Zpdf(1:NN1)+dZpdf(1:NN1) .*d7ZdB7(1:NN1))+ ...
Zpdf(1:NN1).* d8ZdB8(1:NN1);
%str=input('Look at derivatives of initial probability').*.*.*;
%Below I reconstruct the new variable ww1(1:Nn1)=w(1:NN1) which is an
%extrapolated version of w(1:Nn). I have used a first order
%improved version that is still locally gaussian but has
%slightly different variance across the grid. I might change it
%in new later versions and use a gaussian grid with dt variance on
%which even non-bessel coordinate variables in original coordinates would
%be possibly simulated. ww1 is reconstructed by adding the mean

ww1(1:NN1)=BB1(1:NN1)+wMid;
%cc1 below is volatility associated with each grid cell at time
%t Bm(t) or BB1.
[wMu0dtA,dwMu0dtdwA,d2wMu0dtdw2A,d3wMu0dtdw3A,d4wMu0dtdw4A,d5wMu0dtdw5A,d6wMu0dtdw6A,d7wMu0dtdw7A,d8wMu0dtdw8A,SigmawA,dSigmawdwA,d2Sigmawdw2A,Sigma2wA] = CalculateDriftAndVolA404B4Drift(ww1,wnStart,NN1,YqCoeff0,Fp1,gamma,dt)

plot(ZZ1(1:NN1),ww1(1:NN1),'b',Z(1:Nn),w(1:Nn),'r')
str=input('Look at comparison of interpolated and original w-0');
plot(ZZ1(1:NN1),SigmawA(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
str=input('Look at comparison of original and interpolated vol coefficient-1');
%plot(ZZ1(1:NN1),cc1(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
%str=input('Look at comparison of original and interpolated vol coefficient-1A');
plot(ZZ1(1:NN1),wMu0dtA(1:NN1),'b',Z(1:Nn),wMu0dt(1:Nn),'r')
str=input('Look at drifts origianl and interpolated-2');
%plot(ZZ1(1:NN1),ZZProb(1:NN1),'b',Z(1:Nn),ZProb(1:Nn),'r')
%str=input('Look at Zprobss -3');
%plot((1:Nn),Z(1:Nn),'r',(1:NN1)-20,ZZ1(1:NN1),'b')
%str=input('Look at Zs -4');

%Below are calculations of CDF and its derivatives to calculate
%the mid point of the grid at time t+1. This mid point should
%lie exactly on median which means CDF there would be exactly
%equal to .5 or CDF=.5.

c1Guess(1:Nn)=sigma0*sqrt(dt);
SigmaB(1:Nn)=sqrt(dBdZ_0(1:Nn).^2+ c1Guess(1:Nn).^2);

Bn(1:Nn)=w(1:Nn)+wMu0dt(1:Nn)+(SigmaB(1:Nn)-dBdZ_0(1:Nn)).*Z(1:Nn);

Bm0(1:NN1)=BB1(1:NN1)+wMid;
Bm1(1:NN1)=BB1a(1:NN1)+wMid;
Bm2(1:NN1)=BB1b(1:NN1)+wMid;

%     plot((1:Nn),c1(1:Nn),'r');
%     str=input('Look at Bn plot---0');
%     plot((1:Nn),SigmaB(1:Nn),'r',(1:Nn),dBdZ_0(1:Nn),'b');
%     str=input('Look at Bn plot---1');
%     plot((1:Nn),Bn(1:Nn));
%     str=input('Look at Bn plot');

Pn(1:Nn)=0.0;
pn(1:Nn)=0.0;
ESecondHeTotal(1:Nn)=0.0;
for mm=1:NN1

[Pmn,pmn,ESecondHe] = CalculateCDFArraySignIndexDrift(Bn,Nn,wMu0dtA(mm),dwMu0dtdwA(mm),d2wMu0dtdw2A(mm),d3wMu0dtdw3A(mm),d4wMu0dtdw4A(mm),d5wMu0dtdw5A(mm),d6wMu0dtdw6A(mm),d7wMu0dtdw7A(mm),d8wMu0dtdw8A(mm),SigmawA(mm),dSigmawdwA(mm),d2Sigmawdw2A(mm),Sigma2wA(mm),Bm0(mm),Bm1(mm),Bm2(mm),ZZProb(mm),pm0(mm),dpm0(mm),d2pm0(mm),d3pm0(mm),d4pm0(mm),d5pm0(mm),d6pm0(mm),d7pm0(mm));
%Pn is total CDF at the point Bn while Pmn is contribution
%to CDF from mth grid cell at previous time point.
Pmn(isnan(Pmn)==1)=0.0;
pmn(isnan(pmn)==1)=0.0;
ESecondHe(isnan(ESecondHe)==1)=0.0;
Pn(1:Nn)=Pn(1:Nn)+Pmn(1:Nn);
pn(1:Nn)=pn(1:Nn)+pmn(1:Nn);
ESecondHeTotal(1:Nn)=ESecondHeTotal(1:Nn)+ESecondHe(1:Nn);

end
%0.037397306908424
%Calculate Znew which is Znew-grid that corresponds to our Guess Bn where CDF is now available.
Znew(1:Nn)=norminv(Pn(1:Nn));
Bn2(1:Nn)=Bn(1:Nn)+ESecondHeTotal(1:Nn)./pn(1:Nn);

plot((1:Nn),Bn(1:Nn),'g',(1:Nn),Bn2(1:Nn),'r');
str=input('Look at Bn graphs');
%Below we Interpolate values of w at Z-grid (on variable Z)from our
%calculated values of Znew that correspond to CDF at Bn.
%[w] = InterpolateNewGrid(Bn,Znew,wnStart,Nn,dNn,Z)
[w] = InterpolateNewGrid(Bn2,Znew,wnStart,Nn,dNn,Z)

%Below is the grsph of newly calculated value of w.
plot((1:Nn),w(1:Nn),'r')
str=input('Calculated Value of w');
end
end

%below D's (the names of variables starting with D) are
%change of probability derivatives.

y_w(1:Nn)=0;
y_w(wnStart:Nn) = ((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
Dfy_w(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));
%Change of variable derivative for densities
end
py_w(1:Nn)=0;
for nn = wnStart:Nn-1
py_w(nn) = (normpdf(Z(nn),0, 1))/abs(Dfy_w(nn));%Origianl coordinates density
end

toc

ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %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

rng(29079137, 'twister')
paths=200000;
YY(1:paths)=x0;  %Original process monte carlo.
Random1(1:paths)=0;
for tt=1:(Tt-1)+8
if(tt>Tt1)
dtM=dt2/8;
end
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;

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);

end

YY(YY<0)=0;
disp('Original process average from monte carlo');
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
MCVar=sum((YY(:)-MCMean).^2)/paths
disp('Original process average from our simulation');
ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %Original process average from coordinates
ItoHermiteVar=sum((y_w(wnStart+1:Nn-1)-ItoHermiteMean).^2.*ZProb(wnStart+1:Nn-1))
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=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(y_w(wnStart+1:Nn-1),py_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');

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));

legend({'New Model Density','Monte Carlo Density'},'Location','northeast')

str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
.
First Sub Function.
.
function [Pmn,pmn,ESecondHe] = CalculateCDFArraySignIndexDrift(Bn,Nn,Muw,dMuwdBm,d2MuwdBm2,d3MuwdBm3,d4MuwdBm4,d5MuwdBm5,d6MuwdBm6,d7MuwdBm7,d8MuwdBm8,Sigmaw,dSigmawdBm,d2SigmawdBm2,Sigma2wA,Bm0,Bm1,Bm2,dPm,pm0,dpm0,d2pm0,d3pm0,d4pm0,d5pm0,d6pm0,d7pm0)

%This function calculates CDF Pmn, PDF pmn, first derivative of Pdf dpmn,
%second derivative of PDF d2pmn, and third derivative of PDF d3pmn at an
%arbitrary point Bn on next time level grid. All these CDF, PDF and their
%derivatives are calculated from an entire subdivision Bm at time t grid.
%Bn is the array of target points at time t+1 and Nn is the fianl index of
%array Bn starting from one.
%Bm1 and Bm2 are boundaries of grid cell Bm and Bm0 is center of grid cell
%Bm.
% sigma0 is the volatility associated with transition gaussian originating
% from the center of grid Bm given as Bm0.
%dPm is the integrate probability mass in mth originating subdivision.
%pm0 is the value of probability distribution at the center of mth cell at
%Bm0.
%dpm0 is the value of first derivative of probability distribution at the
%center of mth subdivision at Bm0;
%and so on for d2pm0(second derivative of pdf at Bm0), d3pm0, d4pm0, d5pm0.
%pdf and all its derivatives at center of mth subdivsion Bm0 are input to
%the function.
%This program is not written with regard to efficiency at all and I will
%optimize the final version for efficiency.

%The following two loops assume that Bn array is arranged in an ascending
%order as it usually is. The lower two arrays find the lower and upper
%limit of indices such that target array index in between is withing -7 SD
%to + 7SD of the transition normal from the originating cell under question
%(change SDs appropriately if you wish).
nn1=1;
for nn=1:Nn
if( (Bn(nn)-Bm0) <-7*abs(Sigmaw))
nn1=nn;
end
end

nn2=Nn;
for nn=Nn:-1:1
if( (Bn(nn)-Bm0) >7*abs(Sigmaw))
nn2=nn;
end
end
%size(Bn)
%size(Bm0)

%nn1
%nn2
Zt(1:Nn)=0.0;
%Bt(mm1:mm2)=Bn(mm1:mm2)-Bm0;
Zt(nn1:nn2)=(Bn(nn1:nn2)-Bm0-Muw)/Sigmaw;
Zt2(nn1:nn2)=Zt(nn1:nn2).*Zt(nn1:nn2);
Zt3(nn1:nn2)=Zt2(nn1:nn2).*Zt(nn1:nn2);
Zt4(nn1:nn2)=Zt3(nn1:nn2).*Zt(nn1:nn2);
Zt5(nn1:nn2)=Zt4(nn1:nn2).*Zt(nn1:nn2);
Zt6(nn1:nn2)=Zt5(nn1:nn2).*Zt(nn1:nn2);
Zt7(nn1:nn2)=Zt6(nn1:nn2).*Zt(nn1:nn2);
Zt8(nn1:nn2)=Zt7(nn1:nn2).*Zt(nn1:nn2);

Pm0n(nn1:nn2)=normcdf(Zt(nn1:nn2),0,1);
d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
d7ZtdBm7(nn1:nn2)=-d7MuwdBm7/Sigmaw;
d8ZtdBm8(nn1:nn2)=-d8MuwdBm8/Sigmaw;
% d9ZtdBm9(nn1:nn2)=-d9MuwdBm9/Sigmaw;
% d10ZtdBm10(nn1:nn2)=-d10MuwdBm10/Sigmaw;

%  dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((Zt)* dSigmawdBm)/Sigmaw;
%
%
%  d2ZtdBm2=-(((-1-dMuwdBm).* dSigmawdBm)/Sigmaw^2)+(Zt.* dSigmawdBm^2)/Sigmaw^2-( dSigmawdBm.* dZtdBm)/Sigmaw- ...
%      d2MuwdBm2/Sigmaw-(Zt .* d2SigmawdBm2)/Sigmaw;
%
%   d3ZtdBm3=(2.* (-1-dMuwdBm).* dSigmawdBm.^2)/Sigmaw^3-(2.* Zt.* dSigmawdBm^3)/Sigmaw^3+(2 .*dSigmawdBm.^2 .* ...
%       dZtdBm)/Sigmaw.^2+(2.* dSigmawdBm.* d2MuwdBm2)/Sigmaw.^2-((-1-dMuwdBm).* d2SigmawdBm2)/Sigmaw.^2+ ...
%       (3.* Zt.* dSigmawdBm .*d2SigmawdBm2)/Sigmaw.^2-(2.* dZtdBm.* d2SigmawdBm2)/Sigmaw- ...
%       (dSigmawdBm.* d2ZtdBm2)/Sigmaw-d3MuwdBm3/Sigmaw;
%
%
%  d4ZtdBm4=-6.* d2MuwdBm2.* ((2.* dSigmawdBm.^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2)+(4.* dSigmawdBm .* d3MuwdBm3)/Sigmaw.^2+ ...
%       4.* (-1-dMuwdBm) .*(-((6 .*dSigmawdBm.^3)/Sigmaw^4)+(6.* dSigmawdBm .* d2SigmawdBm2)/Sigmaw^3)- ...
%       d4MuwdBm4/Sigmaw+(-Bm0+Bn-Muw).* ((24.* dSigmawdBm.^4)/Sigmaw^5-(36.* dSigmawdBm.^2 .*d2SigmawdBm2)/Sigmaw^4+ ...
%       (6.* d2SigmawdBm2.^2)/Sigmaw^3);
%
%
%
%  d5ZtdBm5=-10.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d3MuwdBm3-10.* d2MuwdBm2.* ...
%      (-((6.* dSigmawdBm^3)/Sigmaw^4)+(6 .*dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3)+ ...
%      (5.* dSigmawdBm.* d4MuwdBm4)/Sigmaw^2+5.* (-1-dMuwdBm).* ((24.* dSigmawdBm^4)/Sigmaw^5- ...
%      (36.* dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3)-d5MuwdBm5/Sigmaw+(-Bm0+Bn-Muw).* ...
%      (-((120.* dSigmawdBm^5)/Sigmaw^6)+(240.* dSigmawdBm^3 .*d2SigmawdBm2)/Sigmaw^5- ...
%      (90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4);
%
%
%
%
%   d6ZtdBm6=-20.* d3MuwdBm3.* (-((6 .*dSigmawdBm.^3)/Sigmaw^4)+(6 .* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3)- ...
%       15.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2) .*d4MuwdBm4-15  .* ...
%       d2MuwdBm2 .*((24.* dSigmawdBm.^4)/Sigmaw^5-(36.* dSigmawdBm^2 .* d2SigmawdBm2)/Sigmaw^4+ ...
%       (6.* d2SigmawdBm2.^2)/Sigmaw^3)+ ...
%       (6.* dSigmawdBm .* d5MuwdBm5)/Sigmaw^2+6 .*(-1-dMuwdBm) .* (-((120.* dSigmawdBm.^5)/Sigmaw^6)+ ...
%       (240.* dSigmawdBm.^3.* d2SigmawdBm2)/Sigmaw^5-(90 .* dSigmawdBm .* d2SigmawdBm2.^2)/Sigmaw^4)-d6MuwdBm6/Sigmaw+(-Bm0+Bn-Muw).* ...
%       ((720.* dSigmawdBm.^6)/Sigmaw^7-(1800.* dSigmawdBm.^4.* d2SigmawdBm2)/S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nn1:nn2)+ ...
dZtdBm(nn1:nn2).^8.* d8Pm0ndZt8(nn1:nn2)+ ...
dPm0ndZt(nn1:nn2).* d8ZtdBm8(nn1:nn2);

dBm=Bm2-Bm1;
dBm1=1/2* ((Bm2 - Bm0)^2 - (Bm1 - Bm0)^2);
dBm2=1/3* ((Bm2 - Bm0)^3 - (Bm1 - Bm0)^3)/2;
dBm3=1/4* ((Bm2 - Bm0)^4 - (Bm1 - Bm0)^4)/6;
dBm4=1/5* ((Bm2 - Bm0)^5 - (Bm1 - Bm0)^5)/24;
dBm5=1/6* ((Bm2 - Bm0)^6 - (Bm1 - Bm0)^6)/120;
dBm6=1/7* ((Bm2 - Bm0)^7 - (Bm1 - Bm0)^7)/720;
dBm7=1/8* ((Bm2 - Bm0)^8 - (Bm1 - Bm0)^8)/(720*7);
dBm8=1/9* ((Bm2 - Bm0)^9 - (Bm1 - Bm0)^9)/(720*7*8);

Integral0(nn1:nn2)=dPm.*Pm0n(nn1:nn2); % G1 group of terms in wilmott explanation.

IntegralCDF(nn1:nn2)=Integral0(nn1:nn2)+ ...  %Integral0 is Group1(G1) in the explanation on wilmott.
+pm0 * dPm0ndBm(nn1:nn2) *dBm1 + ...
+ (2* dpm0 * dPm0ndBm(nn1:nn2)+pm0 * d2Pm0ndBm2(nn1:nn2))  *dBm2 + ...
+ (3*d2pm0*dPm0ndBm(nn1:nn2) + 3*dpm0*d2Pm0ndBm2(nn1:nn2)+pm0 * d3Pm0ndBm3(nn1:nn2))*dBm3 + ...
+ (4*d3pm0*dPm0ndBm(nn1:nn2) + 6* d2pm0*d2Pm0ndBm2(nn1:nn2) + 4* dpm0 * d3Pm0ndBm3(nn1:nn2)+pm0 * d4Pm0ndBm4(nn1:nn2))*dBm4 + ...
+ (5*d4pm0*dPm0ndBm(nn1:nn2) + 10* d3pm0*d2Pm0ndBm2(nn1:nn2)+ 10* d2pm0*d3Pm0ndBm3(nn1:nn2) + ...
5* dpm0 * d4Pm0ndBm4(nn1:nn2) +p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
end
if (nn2<Nn)
Pmn(nn2+1:Nn)=dPm;
pmn(nn2+1:Nn)=0;
ESecondHe(nn2+1:Nn)=0;

end
%Pmn
%str=input('Look at Pmn');
end

.
.
Here are some graphs. The following graph shows Bn VS Zn with second hermite polynomial added(in Red) and without second hermite added i.e just with first hremite polynomial added(in green.)
So Green $B_n$ line is defined as $B_n=B_m + \mu(B_m) + \sigma(B_m) Z_t$
while red $B_n$ line is defined as $B_n=B_m + \mu(B_m) + \sigma(B_m) Z_t+ \sigma_2(B_m) ({Z_t}^2-1)$

Here is the final out put graph of the given program

While final output of the program should be

ItoHermiteMean =
0.125777842090532
true Mean only applicable to standard SV mean reverting type models otherwise disregard
TrueMean =
0.125645632272940
Original process average from monte carlo
MCMean =
0.125565310438908
MCVar =
0.003453688619556
Original process average from our simulation
ItoHermiteMean =
0.125777842090532
ItoHermiteVar =
0.003518899352026
true Mean only applicble to standard SV mean reverting type models otherwise disregard
TrueMean =
0.125645632272940
IndexMax =
406
red line is density of SDE from Ito-Hermite method, green is monte carlo.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here is another sub function you will need. I have altered this one from before. Rest of the functions should be the same ones as posted earlier in previous posts.
.
function [wMu0dt,dwMu0dtdw,d2wMu0dtdw2,d3wMu0dtdw3,d4wMu0dtdw4,d5wMu0dtdw5,d6wMu0dtdw6,d7wMu0dtdw7,d8wMu0dtdw8,c1,dc1dw,d2c1dw2,c2] = CalculateDriftAndVolA404B4Drift(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt)

yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));

Fp2=Fp1/(1-gamma);

wMu0dt(wnStart:Nn)=(YqCoeff0(1,1,2,1).*yy(wnStart:Nn).^Fp1(1,1,2,1)+ ...
YqCoeff0(1,2,1,1).*yy(wnStart:Nn).^Fp1(1,2,1,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1))*dt + ...
(YqCoeff0(1,1,3,1).*yy(wnStart:Nn).^Fp1(1,1,3,1)+ ...
YqCoeff0(1,2,2,1).*yy(wnStart:Nn).^Fp1(1,2,2,1)+ ...
YqCoeff0(2,1,2,1).*yy(wnStart:Nn).^Fp1(2,1,2,1)+ ...
YqCoeff0(1,3,1,1).*yy(wnStart:Nn).^Fp1(1,3,1,1)+ ...
YqCoeff0(2,2,1,1).*yy(wnStart:Nn).^Fp1(2,2,1,1)+ ...
YqCoeff0(3,1,1,1).*yy(wnStart:Nn).^Fp1(3,1,1,1))*dt^2 + ...
(YqCoeff0(1,1,4,1).*yy(wnStart:Nn).^Fp1(1,1,4,1)+ ...
YqCoeff0(1,2,3,1).*yy(wnStart:Nn).^Fp1(1,2,3,1)+ ...
YqCoeff0(2,1,3,1).*yy(wnStart:Nn).^Fp1(2,1,3,1)+ ...
YqCoeff0(1,3,2,1).*yy(wnStart:Nn).^Fp1(1,3,2,1)+ ...
YqCoeff0(2,2,2,1).*yy(wnStart:Nn).^Fp1(2,2,2,1)+ ...
YqCoeff0(3,1,2,1).*yy(wnStart:Nn).^Fp1(3,1,2,1)+ ...
YqCoeff0(1,4,1,1).*yy(wnStart:Nn).^Fp1(1,4,1,1)+ ...
YqCoeff0(2,3,1,1).*yy(wnStart:Nn).^Fp1(2,3,1,1)+ ...
YqCoeff0(3,2,1,1).*yy(wnStart:Nn).^Fp1(3,2,1,1)+ ...
YqCoeff0(4,1,1,1).*yy(wnStart:Nn).^Fp1(4,1,1,1))*dt^3+ ...
(YqCoeff0(1,1,5,1).*yy(wnStart:Nn).^Fp1(1,1,5,1)+ ...
YqCoeff0(1,2,4,1).*yy(wnStart:Nn).^Fp1(1,2,4,1)+ ...
YqCoeff0(2,1,4,1).*yy(wnStart:Nn).^Fp1(2,1,4,1)+ ...
YqCoeff0(2,2,3,1).*yy(wnStart:Nn).^Fp1(2,2,3,1)+ ...
YqCoeff0(3,1,3,1).*yy(wnStart:Nn).^Fp1(3,1,3,1)+ ...
YqCoeff0(1,4,2,1).*yy(wnStart:Nn).^Fp1(1,4,2,1)+ ...
YqCoeff0(2,3,2,1).*yy(wnStart:Nn).^Fp1(2,3,2,1)+ ...
YqCoeff0(3,2,2,1).*yy(wnStart:Nn).^Fp1(3,2,2,1)+ ...
YqCoeff0(4,1,2,1).*yy(wnStart:Nn).^Fp1(4,1,2,1)+ ...
YqCoeff0(1,5,1,1).*yy(wnStart:Nn).^Fp1(1,5,1,1)+ ...
YqCoeff0(2,4,1,1).*yy(wnStart:Nn).^Fp1(2,4,1,1)+ ...
YqCoeff0(3,3,1,1).*yy(wnStart:Nn).^Fp1(3,3,1,1)+  ...
YqCoeff0(4,2,1,1).*yy(wnStart:Nn).^Fp1(4,2,1,1)+ ...
YqCoeff0(5,1,1,1).*yy(wnStart:Nn).^Fp1(5,1,1,1))*dt^4;

%       (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,4,1).*YY(1:paths).^Fp(1,1,4,1)+YCoeff0(1,2,3,1).*YY(1:paths).^Fp(1,2,3,1)+ ...
%       YCoeff0(2,1,3,1).*YY(1:paths).^Fp(2,1,3,1)+YCoeff0(1,3,2,1).*YY(1:paths).^Fp(1,3,2,1)+ ...
%       YCoeff0(2,2,2,1).*YY(1:paths).^Fp(2,2,2,1)+YCoeff0(3,1,2,1).*YY(1:paths).^Fp(3,1,2,1)+ ...
%       YCoeff0(1,4,1,1).*YY(1:paths).^Fp(1,4,1,1)+YCoeff0(2,3,1,1).*YY(1:paths).^Fp(2,3,1,1)+ ...
%       YCoeff0(3,2,1,1).*YY(1:paths).^Fp(3,2,1,1)+YCoeff0(4,1,1,1).*YY(1:paths).^Fp(4,1,1,1))*dtM^3 + ...
%        (YCoeff0(1,1,5,1).*YY(1:paths).^Fp(1,1,5,1)+YCoeff0(1,2,4,1).*YY(1:paths).^Fp(1,2,4,1)+ ...
%     YCoeff0(2,1,4,1).*YY(1:paths).^Fp(2,1,4,1)+YCoeff0(2,1,1,1).*YY(1:paths).^Fp(2,1,1,1)+ ...
%      YCoeff0(2,2,3,1).*YY(1:paths).^Fp(2,2,3,1)+ ...
%      YCoeff0(3,1,3,1).*YY(1:paths).^Fp(3,1,3,1)+YCoeff0(1,4,2,1).*YY(1:paths).^Fp(1,4,2,1)+ ...
%      YCoeff0(2,3,2,1).*YY(1:paths).^Fp(2,3,2,1)+YCoeff0(3,2,2,1).*YY(1:paths).^Fp(3,2,2,1)+ ...
%      YCoeff0(4,1,2,1).*YY(1:paths).^Fp(4,1,2,1)+YCoeff0(1,5,1,1).*YY(1:paths).^Fp(1,5,1,1)+ ...
%      YCoeff0(2,4,1,1).*YY(1:paths).^Fp(2,4,1,1)+ ...
%      YCoeff0(3,3,1,1).*YY(1:paths).^Fp(3,3,1,1)+YCoeff0(4,2,1,1).*YY(1:paths).^Fp(4,2,1,1)+ ...
%      YCoeff0(5,1,1,1).*YY(1:paths).^Fp(5,1,1,1))*dtM^4

dwMu0dtdw(wnStart:Nn)=(YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,2,1))+ ...
YqCoeff0(1,2,1,1).*Fp1(1,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,1,1))+ ...
YqCoeff0(2,1,1,1).*Fp1(2,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,1,1)))*dt + ...
(YqCoeff0(1,1,3,1).*Fp1(1,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,3,1))+ ...
YqCoeff0(1,2,2,1).*Fp1(1,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,2,1))+ ...
YqCoeff0(2,1,2,1).*Fp1(2,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,2,1))+ ...
YqCoeff0(1,3,1,1).*Fp1(1,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,1,1))+ ...
YqCoeff0(2,2,1,1).*Fp1(2,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,1,1))+ ...
YqCoeff0(3,1,1,1).*Fp1(3,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,1,1)))*dt^2 + ...
(YqCoeff0(1,1,4,1).*Fp1(1,1,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,4,1))+ ...
YqCoeff0(1,2,3,1).*Fp1(1,2,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,3,1))+ ...
YqCoeff0(2,1,3,1).*Fp1(2,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,3,1))+ ...
YqCoeff0(1,3,2,1).*Fp1(1,3,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,2,1))+ ...
YqCoeff0(2,2,2,1).*Fp1(2,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,2,1))+ ...
YqCoeff0(3,1,2,1).*Fp1(3,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,2,1))+ ...
YqCoeff0(1,4,1,1).*Fp1(1,4,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,1,1))+ ...
YqCoeff0(2,3,1,1).*Fp1(2,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,1,1))+ ...
YqCoeff0(3,2,1,1).*Fp1(3,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,1,1))+ ...
YqCoeff0(4,1,1,1).*Fp1(4,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,1,1)))*dt^3 + ...
(YqCoeff0(1,1,5,1).*Fp1(1,1,5,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,5,1))+ ...
YqCoeff0(1,2,4,1).*Fp1(1,2,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,4,1))+ ...
YqCoeff0(2,1,4,1).*Fp1(2,1,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,4,1))+ ...
YqCoeff0(2,2,3,1).*Fp1(2,2,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,3,1))+ ...
YqCoeff0(3,1,3,1).*Fp1(3,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,3,1))+ ...
YqCoeff0(1,4,2,1).*Fp1(1,4,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,2,1))+ ...
YqCoeff0(2,3,2,1).*Fp1(2,3,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,2,1))+ ...
YqCoeff0(3,2,2,1).*Fp1(3,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,2,1))+ ...
YqCoeff0(4,1,2,1).*Fp1(4,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,2,1))+ ...
YqCoeff0(1,5,1,1).*Fp1(1,5,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,5,1,1))+ ...
YqCoeff0(2,4,1,1).*Fp1(2,4,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,4,1,1))+ ...
YqCoeff0(3,3,1,1).*Fp1(3,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,3,1,1))+  ...
YqCoeff0(4,2,1,1).*Fp1(4,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,2,1,1))+ ...
YqCoeff0(5,1,1,1).*Fp1(5,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(5,1,1,1)))*dt^4;

d2wMu0dtdw2(wnStart:Nn)=(1-gamma).*((YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*(-1+Fp2(1,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,2,1))+ ...
YqCoeff0(1,2,1,1).*Fp1(1,2,1,1).*(-1+Fp2(1,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,1,1))+ ...
YqCoeff0(2,1,1,1).*Fp1(2,1,1,1).*(-1+Fp2(2,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,1,1)))*dt + ...
(YqCoeff0(1,1,3,1).*Fp1(1,1,3,1).*(-1+Fp2(1,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,3,1))+ ...
YqCoeff0(1,2,2,1).*Fp1(1,2,2,1).*(-1+Fp2(1,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,2,1))+ ...
YqCoeff0(2,1,2,1).*Fp1(2,1,2,1).*(-1+Fp2(2,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,2,1))+ ...
YqCoeff0(1,3,1,1).*Fp1(1,3,1,1).*(-1+Fp2(1,3,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,1,1))+ ...
YqCoeff0(2,2,1,1).*Fp1(2,2,1,1).*(-1+Fp2(2,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,1,1))+ ...
YqCoeff0(3,1,1,1).*Fp1(3,1,1,1).*(-1+Fp2(3,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,1,1)))*dt^2+ ...
(YqCoeff0(1,1,4,1).*Fp1(1,1,4,1).*(-1+Fp2(1,1,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,4,1))+ ...
YqCoeff0(1,2,3,1).*Fp1(1,2,3,1).*(-1+Fp2(1,2,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,3,1))+ ...
YqCoeff0(2,1,3,1).*Fp1(2,1,3,1).*(-1+Fp2(2,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,3,1))+ ...
YqCoeff0(1,3,2,1).*Fp1(1,3,2,1).*(-1+Fp2(1,3,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,2,1))+ ...
YqCoeff0(2,2,2,1).*Fp1(2,2,2,1).*(-1+Fp2(2,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,2,1))+ ...
YqCoeff0(3,1,2,1).*Fp1(3,1,2,1).*(-1+Fp2(3,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,2,1))+ ...
YqCoeff0(1,4,1,1).*Fp1(1,4,1,1).*(-1+Fp2(1,4,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,1,1))+ ...
YqCoeff0(2,3,1,1).*Fp1(2,3,1,1).*(-1+Fp2(2,3,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,1,1))+ ...
YqCoeff0(3,2,1,1).*Fp1(3,2,1,1).*(-1+Fp2(3,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,1,1))+ ...
YqCoeff0(4,1,1,1).*Fp1(4,1,1,1).*(-1+Fp2(4,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,1,1)))*dt^3 + ...
(YqCoeff0(1,1,5,1).*Fp1(1,1,5,1).*(-1+Fp2(1,1,5,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,5,1))+ ...
YqCoeff0(1,2,4,1).*Fp1(1,2,4,1).*(-1+Fp2(1,2,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,4,1))+ ...
YqCoeff0(2,1,4,1).*Fp1(2,1,4,1).*(-1+Fp2(2,1,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,4,1))+ ...
YqCoeff0(2,2,3,1).*Fp1(2,2,3,1).*(-1+Fp2(2,2,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,3,1))+ ...
YqCoeff0(3,1,3,1).*Fp1(3,1,3,1).*(-1+Fp2(3,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,3,1))+ ...
YqCoeff0(1,4,2,1).*Fp1(1,4,2,1).*(-1+Fp2(1,4,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,2,1))+ ...
YqCoeff0(2,3,2,1).*Fp1(2,3,2,1).*(-1+Fp2(2,3,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,2,1))+ ...
YqCoeff0(3,2,2,1).*Fp1(3,2,2,1).*(-1+Fp2(3,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,2,1))+ ...
YqCoeff0(4,1,2,1).*Fp1(4,1,2,1).*(-1+Fp2(4,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,2,1))+ ...
YqCoeff0(1,5,1,1).*Fp1(1,5,1,1).*(-1+Fp2(1,5,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,5,1,1))+ ...
YqCoeff0(2,4,1,1).*Fp1(2,4,1,1).*(-1+Fp2(2,4,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,4,1,1))+ ...
YqCoeff0(3,3,1,1).*Fp1(3,3,1,1).*(-1+Fp2(3,3,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,3,1,1))+  ...
YqCoeff0(4,2,1,1).*Fp1(4,2,1,1).*(-1+Fp2(4,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,2,1,1))+ ...
YqCoeff0(5,1,1,1).*Fp1(5,1,1,1).*(-1+Fp2(5,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(5,1,1,1)))*dt^4);

d3wMu0dtdw3(wnStart:Nn)=(1-gamma)^2.* ...
((YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*(-1+Fp2(1,1,2,1)).*(-2+Fp2(1,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(1,1,2,1))+ ...
YqCoeff0(1,2,1,1).*Fp1(1,2,1,1).*(-1+Fp2(1,2,1,1)).*(-2+Fp2(1,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(1,2,1,1))+ ...
YqCoeff0(2,1,1,1).*Fp1(2,1,1,1).*(-1+Fp2(2,1,1,1)).*(-2+Fp2(2,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(2,1,1,1)))*dt + ...
(YqCoeff0(1,1,3,1).*Fp1(1,1,3,1).*(-1+Fp2(1,1,3,1)).*(-2+Fp2(1,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(1,1,3,1))+ ...
YqCoeff0(1,2,2,1).*Fp1(1,2,2,1).*(-1+Fp2(1,2,2,1)).*(-2+Fp2(1,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(1,2,2,1))+ ...
YqCoeff0(2,1,2,1).*Fp1(2,1,2,1).*(-1+Fp2(2,1,2,1)).*(-2+Fp2(2,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-3+Fp2(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2).*yy(wnStart:Nn).^Fp1(1,1,4,2)+YqCoeff0(1,2,3,2).*yy(wnStart:Nn).^Fp1(1,2,3,2)+ ...
YqCoeff0(2,1,3,2).*yy(wnStart:Nn).^Fp1(2,1,3,2)+YqCoeff0(1,3,2,2).*yy(wnStart:Nn).^Fp1(1,3,2,2)+ ...
YqCoeff0(2,2,2,2).*yy(wnStart:Nn).^Fp1(2,2,2,2)+ YqCoeff0(3,1,2,2).*yy(wnStart:Nn).^Fp1(3,1,2,2)+ ...
YqCoeff0(1,4,1,2).*yy(wnStart:Nn).^Fp1(1,4,1,2)+YqCoeff0(2,3,1,2).*yy(wnStart:Nn).^Fp1(2,3,1,2)+ ...
YqCoeff0(3,2,1,2).*yy(wnStart:Nn).^Fp1(3,2,1,2)+YqCoeff0(4,1,1,2).*yy(wnStart:Nn).^Fp1(4,1,1,2)).*dt^3.5);

dc1dw(wnStart:Nn)=((YqCoeff0(1,1,1,2).*Fp1(1,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,1,2)).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*Fp1(1,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,2,2))+ ...
YqCoeff0(1,2,1,2).*Fp1(1,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,1,2))+ ...
YqCoeff0(2,1,1,2).*Fp1(2,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,1,2))).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*Fp1(1,1,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,3,2))+ ...
YqCoeff0(1,2,2,2).*Fp1(1,2,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,2,2))+ ...
YqCoeff0(2,1,2,2).*Fp1(2,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,2,2))+ ...
YqCoeff0(1,3,1,2).*Fp1(1,3,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,1,2))+ ...
YqCoeff0(2,2,1,2).*Fp1(2,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,1,2))+ ...
YqCoeff0(3,1,1,2).*Fp1(3,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,1,2))).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*Fp1(1,1,4,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,4,2))+ ...
YqCoeff0(1,2,3,2).*Fp1(1,2,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,3,2))+ ...
YqCoeff0(2,1,3,2).*Fp1(2,1,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,3,2))+ ...
YqCoeff0(1,3,2,2).*Fp1(1,3,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,2,2))+ ...
YqCoeff0(2,2,2,2).*Fp1(2,2,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,2,2))+ ...
YqCoeff0(3,1,2,2).*Fp1(3,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,2,2))+ ...
YqCoeff0(1,4,1,2).*Fp1(1,4,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,1,2))+ ...
YqCoeff0(2,3,1,2).*Fp1(2,3,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,1,2))+ ...
YqCoeff0(3,2,1,2).*Fp1(3,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,1,2))+ ...
YqCoeff0(4,1,1,2).*Fp1(4,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,1,2))).*dt^3.5);

d2c1dw2(wnStart:Nn)=(1-gamma).*((YqCoeff0(1,1,1,2).*Fp1(1,1,1,2).*(-1+Fp2(1,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,1,2)).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*Fp1(1,1,2,2).*(-1+Fp2(1,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,2,2))+ ...
YqCoeff0(1,2,1,2).*Fp1(1,2,1,2).*(-1+Fp2(1,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,1,2))+ ...
YqCoeff0(2,1,1,2).*Fp1(2,1,1,2).*(-1+Fp2(2,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,1,2))).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*Fp1(1,1,3,2).*(-1+Fp2(1,1,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,3,2))+ ...
YqCoeff0(1,2,2,2).*Fp1(1,2,2,2).*(-1+Fp2(1,2,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,2,2))+ ...
YqCoeff0(2,1,2,2).*Fp1(2,1,2,2).*(-1+Fp2(2,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,2,2))+ ...
YqCoeff0(1,3,1,2).*Fp1(1,3,1,2).*(-1+Fp2(1,3,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,1,2))+ ...
YqCoeff0(2,2,1,2).*Fp1(2,2,1,2).*(-1+Fp2(2,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,1,2))+ ...
YqCoeff0(3,1,1,2).*Fp1(3,1,1,2).*(-1+Fp2(3,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,1,2))).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*Fp1(1,1,4,2).*(-1+Fp2(1,1,4,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,4,2))+ ...
YqCoeff0(1,2,3,2).*Fp1(1,2,3,2).*(-1+Fp2(1,2,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,3,2))+ ...
YqCoeff0(2,1,3,2).*Fp1(2,1,3,2).*(-1+Fp2(2,1,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,3,2))+ ...
YqCoeff0(1,3,2,2).*Fp1(1,3,2,2).*(-1+Fp2(1,3,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,2,2))+ ...
YqCoeff0(2,2,2,2).*Fp1(2,2,2,2).*(-1+Fp2(2,2,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,2,2))+ ...
YqCoeff0(3,1,2,2).*Fp1(3,1,2,2).*(-1+Fp2(3,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,2,2))+ ...
YqCoeff0(1,4,1,2).*Fp1(1,4,1,2).*(-1+Fp2(1,4,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,1,2))+ ...
YqCoeff0(2,3,1,2).*Fp1(2,3,1,2).*(-1+Fp2(2,3,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,1,2))+ ...
YqCoeff0(3,2,1,2).*Fp1(3,2,1,2).*(-1+Fp2(3,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,1,2))+ ...
YqCoeff0(4,1,1,2).*Fp1(4,1,1,2).*(-1+Fp2(4,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,1,2))).*dt^3.5);

c2(wnStart:Nn)=((YqCoeff0(1,1,1,3).*yy(wnStart:Nn).^Fp1(1,1,1,3) *dt) + ...
(YqCoeff0(1,1,2,3).*yy(wnStart:Nn).^Fp1(1,1,2,3)+ ...
YqCoeff0(1,2,1,3).*yy(wnStart:Nn).^Fp1(1,2,1,3)+ ...
YqCoeff0(2,1,1,3).*yy(wnStart:Nn).^Fp1(2,1,1,3)).*dt^2+ ...
(YqCoeff0(1,1,3,3).*yy(wnStart:Nn).^Fp1(1,1,3,3)+ ...
YqCoeff0(1,2,2,3).*yy(wnStart:Nn).^Fp1(1,2,2,3)+ ...
YqCoeff0(2,1,2,3).*yy(wnStart:Nn).^Fp1(2,1,2,3) + ...
YqCoeff0(1,3,1,3).*yy(wnStart:Nn).^Fp1(1,3,1,3)+ ...
YqCoeff0(2,2,1,3).*yy(wnStart:Nn).^Fp1(2,2,1,3)+ ...
YqCoeff0(3,1,1,3).*yy(wnStart:Nn).^Fp1(3,1,1,3)).*dt^3);

end
.

I was thinking if volatility changes slowly with w i.e  it does not have substantial higher derivatives, may be we can use just first two derivatives of volatility with respect to Bm (used in the program as dSigmawdBm and as its higher derivatives.) in calculation of dBmdz and its higher derivatives.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Friends,  $B_n$ in the previous program  is actually  defined as $B_n=B_m + \mu(B_m) + \sigma(B_m) Z_t+ \sigma_2(B_m) E[({Z_t}^2-1)]$
where the last term is an expectation over all $({Z_t}^2-1)$ coming to a particular Bn from all of the Bm's. The true value would not be an expectation like this. I have decided to solve this with quadratic formula(which is really not very expensive) using exact analytics to find out how much the error is and when to use exact formulas and when to use any approximations. Hopefully, I will be doing it today and would also post the program here.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Dear friends, I am so excited to break the good news that multistep version of my program is working well in a very stable fashion for densities that do not go to zero. I wanted a basis point precision but I noticed that for mean-reverting SDEs with kappa=2 - 4 , the error till one year is always within 2-15 bp with a step size of 1/32. I did not post a program with exact analytics with quadratic equation for second hermite because (for one step), it had results very similar to what I had earlier calculated with expected value but I will still post a program later with quadratic equation analytics for friends but only a few days later after I complete this part. I have changed the extrapolation at the ends that was very unstable with something better that is extremely stable so the program works great for multistep. I will be posting graphs and also the program in another post later today. I will also cover SDEs that go to zero but I want to do everything step by step.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here are a few graphs with the multistep version of the earlier method. Currently the program  is very sensitive for zero since if any value goes below zero and some resulting calculations becomes complex, the program blows up but I will fix that in next version.
I have taken extremely small 32 steps at start with small step size method and then took another 32 larger steps with this new Taylor CDF method using step size dt=.03092 which is given in the graph.
Here are the graphs.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here are the files used to create the output and make above graphs.
.
function [] = SDETransProb08WmtGrid200b71Drift()

%Copyright Ahsan Amin. Infiniti derivatives Technologies.
%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)

%I have not directly simulated the SDE but simulated the transformed
%Besse1 process version of the SDE and then changed coordinates to retreive
%the SDE in original coordinates.
%The present program will analytically evolve only the Bessel Process version of the
%SDE in transformed coordinates.

%In this program, I have simulated the density for first few very small time
%intervals using a simple method. After first few intervals, I start
%simulating with probability mass transfer method.  Please experiment with step size and
%you will find the method robust enough. This is just an early version and
%I will be coming with better versions soon.

%dt=.125/16/2/2/2;   % Regular Simulation time interval to advance the density before large step by CDF method.
dt=1/48*1/2*1/32;             %decrease dt for accuracy.

Tt1=32;
Tt2=32;

T=1;
dt2=(T-dt*Tt1)/Tt2;
Tt=Tt1+Tt2;
T=dt*Tt1+dt2*Tt2;
OrderA=4;  %
%dtM=dt/8;%.125/2/4;%Monte carlo time interval size dtM.
%TtM=Tt*8;%T*2*8*4;%Monte carlo number of simulation intervals.
dtM=T/32;%.125/2/4;%Monte carlo time interval size dtM.
TtM=32;

dNn=.05;   % Normal density subdivisions width. would change with number of subdivisions
Nn=200;  % No of normal density subdivisions
NnMidl=100;%One half density Subdivision left from mid of normal density(low)
NnMidh=101;%One half density subdivision right from the mid of normal density(high)
NnMid=4.0;

Z(1:Nn)=(((1:Nn)-20.5)*dNn-NnMid)

%There are two indices. One goes to Nn=200. This is used for calculation of
%SDE simulation at next time level t+1. This covers -5 to +5 SDs of
%underlying Z.
%Second index Nn1 below goes from 1 to 240. This covers -6 to +6 SD of
%underlying Z. In probability mass transfer methos, we simulate the SDE
%from -5 SD to +5 SD and then extrapolate on both sides so that we have a
%new grid from -6 SD to +6 SD(1:Nn1). This extrapolated grid(which is assigned time t)
%is used to calculate the time t+1 grid from -5 SD to +5 SD(1:Nn).
%Grid spacing is the same on both grids.
Nn1=240;
%dNn1=.05;
Nn1Midl=120;
Nn1Midh=121;
Nn1Mid=6.025;

Z1(1:Nn1)=((1:Nn1)*dNn-Nn1Mid)
Z1Prob(1)=normcdf(-5.95)-normcdf(-6.0);
Z1Prob(Nn1)=normcdf(6.0)-normcdf(5.95);
Z1Prob(2:Nn1-1)=normcdf(.5*Z1(2:Nn1-1)+.5*Z1(3:Nn1),0,1)-normcdf(.5*Z1(2:Nn1-1)+.5*Z1(1:Nn1-2),0,1);

ZProb(1:Nn)=Z1Prob(21:Nn1-20);
Z(1:Nn)=Z1(21:Nn1-20);

Z1Prob
sum(Z1Prob(1:Nn1))
sum(ZProb(1:Nn))
str=input('Look at Zs');

x0=1.0;   % starting value of SDE
beta1=0.0;
beta2=1.00;   % Second drift term power.
gamma=.65;%50;   % volatility power.
kappa=4.0;%.950;   %mean reversion parameter.
theta=1.0;%mean reversion target
sigma0=1.0;%Volatility value

%you can specify any general mu1 and mu2 and beta1 and beta2.
mu1=+1*theta*kappa;   %first drift coefficient.
mu2=-1*kappa;    % Second drift coefficient.
%mu1=0;
%mu2=0;

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

w(1:Nn)=x0^(1-gamma)/(1-gamma);

Z
Z1
str=input('Look at Zs');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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;

mu33(1)=1;
mu44(1)=1;
mu33(2:OrderA+1)=0;
mu44(2:OrderA+1)=0;

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);
YqzCoeff0(l1,l2,l3,l4) =YqCoeff(l1,l2,l3,l4).*mu33(l1).*mu44(l2).*sigma22(l3).*sigma11(l4);
end
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wnStart=1;%
yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
%The above yy(wnStart:Nn)=x0;

tic

for tt=1:Tt

if(tt==1)

w_00=x0^(1-gamma)/(1-gamma);
%[wMu0dt,c1,c2,c3] = CalculateDriftAndVolA404B4Drift02(w_00,1,1,YqCoeff0,Fp1,gamma,dt);
[wMu0dt,c1] = CalculateDriftAndVolA404(w_00,1,1,YqCoeff0,Fp1,gamma,dt);
w(1:Nn)=x0^(1-gamma)/(1-gamma)+wMu0dt(1)+c1(1).*Z(1:Nn);%+c2(1).*(Z(1:Nn).^2-1)+c3(1).*(Z(1:Nn).^3-3*Z(1:Nn));

end
if((tt>1) && (tt<=Tt1))
%The first Tt1 steps are simulated using a simple and slightly
%altered version of the method I have used for simple CEV noises
%and brownian motions. I used it only for starting few time steps
%since density remains pretty much very close to linear here. If
%that is not the case, you would have to alter this.
[wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);
%dw(wnStart:Nn)=c1(wnStart:Nn);%.*Z(wnStart:Nn) ;% ...
dw(wnStart:Nn)=c1(wnStart:Nn).*Z(wnStart:Nn) ;% ...
dw2(wnStart:Nn)=dw(wnStart:Nn).^2;
[wMid] = InterpolateOrderN8(8,0,Z(NnMidl-3),Z(NnMidl-2),Z(NnMidl-1),Z(NnMidl),Z(NnMidh),Z(NnMidh+1),Z(NnMidh+2),Z(NnMidh+3),w(NnMidl-3),w(NnMidl-2),w(NnMidl-1),w(NnMidl),w(NnMidh),w(NnMidh+1),w(NnMidh+2),w(NnMidh+3));
B(1:Nn)=w(1:Nn)-wMid;
%[dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,B,Z);
B(wnStart:Nn)=sign(B(wnStart:Nn)+dw(wnStart:Nn)).* ...
sqrt(abs(sign(B(wnStart:Nn)).*(B(wnStart:Nn)).^2+ ...
sign(dw(wnStart:Nn)).*dw2(wnStart:Nn)));

w(wnStart:Nn)=wMid+wMu0dt(wnStart:Nn)+B(wnStart:Nn);

end
%Now starts the calculation for SDE simulation using probability mass
%transfer.
if(tt>Tt1)

dt=dt2;

[wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);

[dwdZ_0,d2wdZ2_0,d3wdZ3_0] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,w,Z);
%Above are derivative names with an appended zero "_0". These are derivatives on the
%Nn grid (time t+1 grid). When there are no appended zeros, those are
%derivatives on the Nn1 grid i.e time t grid.

BB1(21:Nn+20)=w(1:Nn);
BB1(1:20)=BB1(21)-(20:-1:1).*dwdZ_0(1)*dNn;
%Below we do extrapolation on right boundary from 5SD to 6 SD
%and then assign it to BB1 which is Bm(t).

BB1(Nn+20+1:Nn+20+20)=BB1(Nn+20)+(1:20).*dwdZ_0(Nn)*dNn;

%Below is the loop version of above extrapolations
%        for nn=1:Nn
%            BB1(nn+20)=w(nn);
%        end

%        for nn=20:-1:1
%            %BB1(nn)=BB1(nn+1)-dwdZ_0(1)*dNn ;
%        end
%
%        for nn=Nn+20+1:Nn+20+20
%            BB1(nn)=BB1(nn-1)+ dwdZ_0(Nn)*dNn ;
%        end
%
%The function below calculates grid cell boundaries from mid
%points of the grids using interpolation and relevant Z. The
%grid is equidistant and symmetric with respect to underlying Z
%but not with respect to the Bessel variable. We use the value
%of Z at boundaries of each cell to interpolate the value of
%bessel variable there.
[BB1a,BB1b] = CalculateGridStartsAndEndsBoundedEnds(BB1,Z1,1,Nn1,dNn);

%Below,I later assigned Z1 to ZZ1 and N1 to NN1 ans similarly for
%Z1Prob since earlier I had several(three different) cases of
%extrapolation ranging from 5 to 6 SD, and 5 to 5.5 SD and 5 to 5.25 SD.
%I Was thinking when time would advance and grid would have
%expanded and if dt would stay the same, the user might be
%better off choosing a smaller grid with better extrapolation.

ZZ1=Z1;
NN1=Nn1;
ZZProb(1:NN1)=Z1Prob(1:Nn1);

%plot(ZZ1(1:NN1),BB1(1:NN1),'b',ZZ1(1:NN1),BB1a(1:NN1),'y',ZZ1(1:NN1),BB1b(1:NN1),'k',Z(1:Nn),B1(1:Nn),'r')
%str=input('Look at B1 and BB1, original and extrapolated version and boundaries');

%robust function.
[dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(1,NN1,dNn,BB1,Z1);
[dwdZAA,d2wdZ2AA,d3wdZ3AA,d4BdZ4] = First4Derivatives2ndOrderEqSpaced(1,NN1,dNn,BB1,Z1);

%plot((1:NN1),dBdZ(1:NN1),'r')
%str=input('Look at 1st derivative');
%plot((1:NN1),d2BdZ2(1:NN1),'r')
%str=input('Look at 2nd derivative');
%plot((1:NN1),d3BdZ3(1:NN1),'r')
%str=input('Look at 3rd derivative');

%Below calculate hermite polynomials of Z1 underlying the
%Grid BB1 which is "Bm(t)" grid. We will use these hermite
%polynomials and later derivatives to calculate the derivatives of
%density of Bm with respect to Bm. dpdBm calcualted at centre
%of the grid cells.

He1(1:NN1)=Z1(1:NN1);
He2(1:NN1)=Z1(1:NN1).^2-1;
He3(1:NN1)=Z1(1:NN1).^3-3*Z1(1:NN1);
He4(1:NN1)=Z1(1:NN1).^4-6*Z1(1:NN1).^2+3;
He5(1:NN1)=Z1(1:NN1).^5-10*Z1(1:NN1).^3+15;
He6(1:NN1)=Z1(1:NN1).^6-15*Z1(1:NN1).^4+45*Z1(1:NN1).^2-15;
He7(1:NN1)=Z1(1:NN1).^7-21*Z1(1:NN1).^5+105*Z1(1:NN1).^3-105*Z1(1:NN1);
He8(1:NN1)=Z1(1:NN1).^8-28*Z1(1:NN1).^6+210*Z1(1:NN1).^4-420*Z1(1:NN1).^2+105;
He9(1:NN1)=Z1(1:NN1).^9-36*Z1(1:NN1).^7+378*Z1(1:NN1).^5-1260*Z1(1:NN1).^3+945*Z1(1:NN1);

Zpdf(1:NN1)=normpdf(Z1(1:NN1),0,1);
dZpdf(1:NN1)=-He1(1:NN1).*Zpdf(1:NN1);
d2Zpdf(1:NN1)=He2(1:NN1).*Zpdf(1:NN1);
d3Zpdf(1:NN1)=-He3(1:NN1).*Zpdf(1:NN1);
d4Zpdf(1:NN1)=He4(1:NN1).*Zpdf(1:NN1);
d5Zpdf(1:NN1)=-He5(1:NN1).*Zpdf(1:NN1);
d6Zpdf(1:NN1)=He6(1:NN1).*Zpdf(1:NN1);
d7Zpdf(1:NN1)=-He7(1:NN1).*Zpdf(1:NN1);
d8Zpdf(1:NN1)=He8(1:NN1).*Zpdf(1:NN1);
d9Zpdf(1:NN1)=-He9(1:NN1).*Zpdf(1:NN1);

%Please note that in calculations below, we are using derivatives without
%appended zeros since they are on BB1 "Bm(t)" grid that ranges from 1:Nn1 or equivalently 1:NN1.
dZdB(1:NN1)=1.0./dBd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dB5(1:NN1)+21 .*dZdB(1:NN1).^5.* d2ZdB2(1:NN1) .* d6Zpdf(1:NN1)+ ...
7.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d6ZdB6(1:NN1)+dZdB(1:NN1).^7.* d7Zpdf(1:NN1)+dZpdf(1:NN1) .*d7ZdB7(1:NN1))+ ...
Zpdf(1:NN1).* d8ZdB8(1:NN1);

ww1(1:NN1)=BB1(1:NN1);%+wMid;
ww1(ww1<0)=0.000000000001;
%I will improve the above improvisation in a day.
%SigmawA below is volatility associated with each grid cell at time
%t Bm(t) or BB1.
[wMu0dtA,dwMu0dtdwA,d2wMu0dtdw2A,d3wMu0dtdw3A,d4wMu0dtdw4A,d5wMu0dtdw5A,d6wMu0dtdw6A,d7wMu0dtdw7A,d8wMu0dtdw8A,SigmawA,dSigmawdwA,d2Sigmawdw2A,Sigma2wA,Sigma3wA] = CalculateDriftAndVolA404B4Drift(ww1,wnStart,NN1,YqCoeff0,Fp1,gamma,dt);

%            plot(ZZ1(1:NN1),ww1(1:NN1),'b',Z(1:Nn),w(1:Nn),'r')
%            str=input('Look at comparison of interpolated and original w-0');
%            plot(ZZ1(1:NN1),SigmawA(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
%            str=input('Look at comparison of original and interpolated vol coefficient-1');
%plot(ZZ1(1:NN1),cc1(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
%str=input('Look at comparison of original and interpolated vol coefficient-1A');
%            plot(ZZ1(1:NN1),wMu0dtA(1:NN1),'b',Z(1:Nn),wMu0dt(1:Nn),'r')
%            str=input('Look at drifts origianl and interpolated-2');
%plot(ZZ1(1:NN1),ZZProb(1:NN1),'b',Z(1:Nn),ZProb(1:Nn),'r')
%str=input('Look at Zprobss -3');
%plot((1:Nn),Z(1:Nn),'r',(1:NN1)-20,ZZ1(1:NN1),'b')
%str=input('Look at Zs -4');

%Below are calculations of CDF and its derivatives to calculate
%the mid point of the grid at time t+1. This mid point should
%lie exactly on median which means CDF there would be exactly
%equal to .5 or CDF=.5.

%c1Guess(1:Nn)=sigma0*sqrt(dt);
Sigmaw_0(1:Nn)=sqrt(dwdZ_0(1:Nn).^2+ c1(1:Nn).^2);

Bn(1:Nn)=w(1:Nn)+wMu0dt(1:Nn)+(Sigmaw_0(1:Nn)-dwdZ_0(1:Nn)).*Z(1:Nn);
Bn(Bn<0)=0.0000000000001;
%I will improve the above improvisation in a day.

Bm0(1:NN1)=BB1(1:NN1);%
Bm1(1:NN1)=BB1a(1:NN1);%
Bm2(1:NN1)=BB1b(1:NN1);%
%Bm0(Bm0<0)=.000000000001;

%     plot((1:Nn),c1(1:Nn),'r');
%     str=input('Look at Bn plot---0');
%     plot((1:Nn),SigmaB(1:Nn),'r',(1:Nn),dBdZ_0(1:Nn),'b');
%     str=input('Look at Bn plot---1');
%     plot((1:Nn),Bn(1:Nn));
%     str=input('Look at Bn plot');

Pn(1:Nn)=0.0;
pn(1:Nn)=0.0;
ESecondHeTotal(1:Nn)=0.0;
%EThirdHeTotal(1:Nn)=0.0;
for mm=1:NN1

[Pmn,pmn,ESecondHe] = CalculateCDFArraySignIndexDrift(Bn,Nn,wMu0dtA(mm),dwMu0dtdwA(mm),d2wMu0dtdw2A(mm),d3wMu0dtdw3A(mm),d4wMu0dtdw4A(mm),d5wMu0dtdw5A(mm),d6wMu0dtdw6A(mm),d7wMu0dtdw7A(mm),d8wMu0dtdw8A(mm),SigmawA(mm),dSigmawdwA(mm),d2Sigmawdw2A(mm),Sigma2wA(mm),Bm0(mm),Bm1(mm),Bm2(mm),ZZProb(mm),pm0(mm),dpm0(mm),d2pm0(mm),d3pm0(mm),d4pm0(mm),d5pm0(mm),d6pm0(mm),d7pm0(mm));
%Pn is total CDF at the point Bn while Pmn is contribution
%to CDF from mth grid cell at previous time point.
Pmn(isnan(Pmn)==1)=0.0;
pmn(isnan(pmn)==1)=0.0;
ESecondHe(isnan(ESecondHe)==1)=0.0;

Pn(1:Nn)=Pn(1:Nn)+Pmn(1:Nn);
pn(1:Nn)=pn(1:Nn)+pmn(1:Nn);
ESecondHeTotal(1:Nn)=ESecondHeTotal(1:Nn)+ESecondHe(1:Nn);

end

%Calculate Znew which is Znew-grid that corresponds to our Guess Bn where CDF is now available.
Znew(1:Nn)=norminv(Pn(1:Nn));
Bn2(1:Nn)=Bn(1:Nn)+ESecondHeTotal(1:Nn)./pn(1:Nn);%

%            plot((1:Nn),Bn(1:Nn),'g',(1:Nn),Bn2(1:Nn),'r');
%            str=input('Look at Bn graphs');
%Below we Interpolate values of w at Z-grid (on variable Z)from our
%calculated values of Znew that correspond to CDF at Bn.
%[w] = InterpolateNewGrid(Bn,Znew,wnStart,Nn,dNn,Z)
[w] = InterpolateNewGrid(Bn2,Znew,wnStart,Nn,dNn,Z);

%Below is the grsph of newly calculated value of w.
%       plot((1:Nn),w(1:Nn),'r')
%       str=input('Calculated Value of w');
end
end

%below D's (the names of variables starting with D) are
%change of probability derivatives.

y_w(1:Nn)=0;
y_w(wnStart:Nn) = ((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
Dfy_w(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));
%Change of variable derivative for densities
end
py_w(1:Nn)=0;
for nn = wnStart:Nn-1
py_w(nn) = (normpdf(Z(nn),0, 1))/abs(Dfy_w(nn));%Origianl coordinates density
end

toc

ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %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

rng(29079137, 'twister')
paths=200000;
YY(1:paths)=x0;  %Original process monte carlo.
Random1(1:paths)=0;
%for tt=1:(Tt-1)+8
for tt=1:TtM
if(tt>Tt1)
%dtM=dt2/8;
end
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;

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);

end

YY(YY<0)=0;
disp('Original process average from monte carlo');
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
MCVar=sum((YY(:)-MCMean).^2)/paths
disp('Original process average from our simulation');
ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %Original process average from coordinates
ItoHermiteVar=sum((y_w(wnStart+1:Nn-1)-ItoHermiteMean).^2.*ZProb(wnStart+1:Nn-1))
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=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(y_w(wnStart+1:Nn-1),py_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');

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));

legend({'New Model Density','Monte Carlo Density'},'Location','northeast')

str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
.
First Sub Function.
.
function [Pmn,pmn,ESecondHe] = CalculateCDFArraySignIndexDrift(Bn,Nn,Muw,dMuwdBm,d2MuwdBm2,d3MuwdBm3,d4MuwdBm4,d5MuwdBm5,d6MuwdBm6,d7MuwdBm7,d8MuwdBm8,Sigmaw,dSigmawdBm,d2SigmawdBm2,Sigma2w,Bm0,Bm1,Bm2,dPm,pm0,dpm0,d2pm0,d3pm0,d4pm0,d5pm0,d6pm0,d7pm0)

%This function calculates CDF Pmn, PDF pmn, first derivative of Pdf dpmn,
%second derivative of PDF d2pmn, and third derivative of PDF d3pmn at an
%arbitrary point Bn on next time level grid. All these CDF, PDF and their
%derivatives are calculated from an entire subdivision Bm at time t grid.
%Bn is the array of target points at time t+1 and Nn is the fianl index of
%array Bn starting from one.
%Bm1 and Bm2 are boundaries of grid cell Bm and Bm0 is center of grid cell
%Bm.
% sigma0 is the volatility associated with transition gaussian originating
% from the center of grid Bm given as Bm0.
%dPm is the integrate probability mass in mth originating subdivision.
%pm0 is the value of probability distribution at the center of mth cell at
%Bm0.
%dpm0 is the value of first derivative of probability distribution at the
%center of mth subdivision at Bm0;
%and so on for d2pm0(second derivative of pdf at Bm0), d3pm0, d4pm0, d5pm0.
%pdf and all its derivatives at center of mth subdivsion Bm0 are input to
%the function.
%This program is not written with regard to efficiency at all and I will
%optimize the final version for efficiency.

%The following two loops assume that Bn array is arranged in an ascending
%order as it usually is. The lower two arrays find the lower and upper
%limit of indices such that target array index in between is withing -7 SD
%to + 7SD of the transition normal from the originating cell under question
%(change SDs appropriately if you wish).
nn1=1;
for nn=1:Nn
if( (Bn(nn)-Bm0) <-7*abs(Sigmaw))
nn1=nn;
end
end

nn2=Nn;
for nn=Nn:-1:1
if( (Bn(nn)-Bm0) >7*abs(Sigmaw))
nn2=nn;
end
end
%size(Bn)
%size(Bm0)

%nn1
%nn2

%W2=W1+ mu* dt+ sigma *Z + sigma2 *(Z^2-1)

%Here mu*dt=wMu0dt; sigma=dw02; sigma2 =c22;

Zt(1:Nn)=0.0;
%Bt(mm1:mm2)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sigmaw^2)+(Zt.* dSigmawdBm^2)/Sigmaw^2-( dSigmawdBm.* dZtdBm)/Sigmaw- ...
%       d2MuwdBm2/Sigmaw-(Zt .* d2SigmawdBm2)/Sigmaw;
% %
%    d3ZtdBm3=(2.* (-1-dMuwdBm).* dSigmawdBm.^2)/Sigmaw^3-(2.* Zt.* dSigmawdBm^3)/Sigmaw^3+(2 .*dSigmawdBm.^2 .* ...
%        dZtdBm)/Sigmaw.^2+(2.* dSigmawdBm.* d2MuwdBm2)/Sigmaw.^2-((-1-dMuwdBm).* d2SigmawdBm2)/Sigmaw.^2+ ...
%        (3.* Zt.* dSigmawdBm .*d2SigmawdBm2)/Sigmaw.^2-(2.* dZtdBm.* d2SigmawdBm2)/Sigmaw- ...
%        (dSigmawdBm.* d2ZtdBm2)/Sigmaw-d3MuwdBm3/Sigmaw;
% %
% %
%   d4ZtdBm4=-6.* d2MuwdBm2.* ((2.* dSigmawdBm.^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2)+(4.* dSigmawdBm .* d3MuwdBm3)/Sigmaw.^2+ ...
%        4.* (-1-dMuwdBm) .*(-((6 .*dSigmawdBm.^3)/Sigmaw^4)+(6.* dSigmawdBm .* d2SigmawdBm2)/Sigmaw^3)- ...
%        d4MuwdBm4/Sigmaw+(-Bm0+Bn-Muw).* ((24.* dSigmawdBm.^4)/Sigmaw^5-(36.* dSigmawdBm.^2 .*d2SigmawdBm2)/Sigmaw^4+ ...
%        (6.* d2SigmawdBm2.^2)/Sigmaw^3);
% %
% %
% %
%   d5ZtdBm5=-10.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d3MuwdBm3-10.* d2MuwdBm2.* ...
%       (-((6.* dSigmawdBm^3)/Sigmaw^4)+(6 .*dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3)+ ...
%       (5.* dSigmawdBm.* d4MuwdBm4)/Sigmaw^2+5.* (-1-dMuwdBm).* ((24.* dSigmawdBm^4)/Sigmaw^5- ...
%       (36.* dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3)-d5MuwdBm5/Sigmaw+(-Bm0+Bn-Muw).* ...
%       (-((120.* dSigmawdBm^5)/Sigmaw^6)+(240.* dSigmawdBm^3 .*d2SigmawdBm2)/Sigmaw^5- ...
%       (90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4);
% %
% %
% %
% %
%    d6ZtdBm6=-20.* d3MuwdBm3.* (-((6 .*dSigmawdBm.^3)/Sigmaw^4)+(6 .* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3)- ...
%        15.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2) .*d4MuwdBm4-15  .* ...
%        d2MuwdBm2 .*((24.* dSigmawdBm.^4)/Sigmaw^5-(36.* dSigmawdBm^2 .* d2SigmawdBm2)/Sigmaw^4+ ...
%        (6.* d2SigmawdBm2.^2)/Sigmaw^3)+ ...
%        (6.* dSigmawdBm .* d5MuwdBm5)/Sigmaw^2+6 .*(-1-dMuwdBm) .* (-((120.* dSigmawdBm.^5)/Sigmaw^6)+ ...
%        (240.* dSigmawdBm.^3.* d2SigmawdBm2)/Sigmaw^5-(90 .* dSigmawdBm .* d2SigmawdBm2.^2)/Sigmaw^4)-d6MuwdBm6/Sigmaw+(-Bm0+Bn-Muw).* ...
%        ((720.* dSigmawdBm.^6)/Sigmaw^7-(1800.* dSigmawdBm.^4.* d2SigmawdBm2)/Sigmaw^6+(1080 .*dSigmawdBm.^2 .* d2SigmawdBm2.^2)/Sigmaw^5- ...
%        (90.* d2SigmawdBm2^3)/Sigmaw^4);
%

dPm0ndBm(nn1:nn2)=dPm0ndZt(nn1:nn2).*dZtdBm(nn1:nn2);
d2Pm0ndBm2(nn1:nn2)=d2Pm0ndZt2(nn1:nn2).*dZtdBm(nn1:nn2).^2+dPm0ndZt(nn1:nn2).*d2ZtdBm2(nn1:nn2);
d3Pm0nd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dBm4=1/5* ((Bm2 - Bm0)^5 - (Bm1 - Bm0)^5)/24;
dBm5=1/6* ((Bm2 - Bm0)^6 - (Bm1 - Bm0)^6)/120;
dBm6=1/7* ((Bm2 - Bm0)^7 - (Bm1 - Bm0)^7)/720;
dBm7=1/8* ((Bm2 - Bm0)^8 - (Bm1 - Bm0)^8)/(720*7);
dBm8=1/9* ((Bm2 - Bm0)^9 - (Bm1 - Bm0)^9)/(720*7*8);

Integral0(nn1:nn2)=dPm.*Pm0n(nn1:nn2); % G1 group of terms in wilmott explanation.

IntegralCDF(nn1:nn2)=Integral0(nn1:nn2)+ ...  %Integral0 is Group1(G1) in the explanation on wilmott.
+pm0 * dPm0ndBm(nn1:nn2) *dBm1 + ...
+ (2* dpm0 * dPm0ndBm(nn1:nn2)+pm0 * d2Pm0ndBm2(nn1:nn2))  *dBm2 + ...
+ (3*d2pm0*dPm0ndBm(nn1:nn2) + 3*dpm0*d2Pm0ndBm2(nn1:nn2)+pm0 * d3Pm0ndBm3(nn1:nn2))*dBm3 + ...
+ (4*d3pm0*dPm0ndBm(nn1:nn2) + 6* d2pm0*d2Pm0ndBm2(nn1:nn2) + 4* dpm0 * d3Pm0ndBm3(nn1:nn2)+pm0 * d4Pm0ndBm4(nn1:nn2))*dBm4 + ...
+ (5*d4pm0*dPm0ndBm(nn1:nn2) + 10* d3pm0*d2Pm0ndBm2(nn1:nn2)+ 10* d2pm0*d3Pm0ndBm3(nn1:nn2) + ...
5* dpm0 * d4Pm0ndBm4(nn1:nn2) +pm0 * d5Pm0ndBm5(nn1:nn2))*dBm5 + ...
+ (6*d5pm0*dPm0ndBm(nn1:nn2) + 15* d4pm0*d2Pm0ndBm2(nn1:nn2)+ 20* d3pm0*d3Pm0ndBm3(nn1:nn2) + 15* d2pm0 * d4Pm0ndBm4(nn1:nn2)+ ...
6* dpm0 * d5Pm0ndBm5(nn1:nn2)+pm0 * d6Pm0ndBm6(nn1:nn2)) * dBm6+ ...
+ (7*d6pm0*dPm0nd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at Pmn');
end
.
Second Sub Function.
.
function [wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt)

yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));

Fp2=Fp1/(1-gamma);

wMu0dt(wnStart:Nn)=(YqCoeff0(1,1,2,1).*yy(wnStart:Nn).^Fp1(1,1,2,1)+ ...
YqCoeff0(1,2,1,1).*yy(wnStart:Nn).^Fp1(1,2,1,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1))*dt + ...
(YqCoeff0(1,1,3,1).*yy(wnStart:Nn).^Fp1(1,1,3,1)+ ...
YqCoeff0(1,2,2,1).*yy(wnStart:Nn).^Fp1(1,2,2,1)+ ...
YqCoeff0(2,1,2,1).*yy(wnStart:Nn).^Fp1(2,1,2,1)+ ...
YqCoeff0(1,3,1,1).*yy(wnStart:Nn).^Fp1(1,3,1,1)+ ...
YqCoeff0(2,2,1,1).*yy(wnStart:Nn).^Fp1(2,2,1,1)+ ...
YqCoeff0(3,1,1,1).*yy(wnStart:Nn).^Fp1(3,1,1,1))*dt^2 + ...
(YqCoeff0(1,1,4,1).*yy(wnStart:Nn).^Fp1(1,1,4,1)+ ...
YqCoeff0(1,2,3,1).*yy(wnStart:Nn).^Fp1(1,2,3,1)+ ...
YqCoeff0(2,1,3,1).*yy(wnStart:Nn).^Fp1(2,1,3,1)+ ...
YqCoeff0(1,3,2,1).*yy(wnStart:Nn).^Fp1(1,3,2,1)+ ...
YqCoeff0(2,2,2,1).*yy(wnStart:Nn).^Fp1(2,2,2,1)+ ...
YqCoeff0(3,1,2,1).*yy(wnStart:Nn).^Fp1(3,1,2,1)+ ...
YqCoeff0(1,4,1,1).*yy(wnStart:Nn).^Fp1(1,4,1,1)+ ...
YqCoeff0(2,3,1,1).*yy(wnStart:Nn).^Fp1(2,3,1,1)+ ...
YqCoeff0(3,2,1,1).*yy(wnStart:Nn).^Fp1(3,2,1,1)+ ...
YqCoeff0(4,1,1,1).*yy(wnStart:Nn).^Fp1(4,1,1,1))*dt^3+ ...
(YqCoeff0(1,1,5,1).*yy(wnStart:Nn).^Fp1(1,1,5,1)+ ...
YqCoeff0(1,2,4,1).*yy(wnStart:Nn).^Fp1(1,2,4,1)+ ...
YqCoeff0(2,1,4,1).*yy(wnStart:Nn).^Fp1(2,1,4,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1)+ ...
YqCoeff0(2,2,3,1).*yy(wnStart:Nn).^Fp1(2,2,3,1)+ ...
YqCoeff0(3,1,3,1).*yy(wnStart:Nn).^Fp1(3,1,3,1)+ ...
YqCoeff0(1,4,2,1).*yy(wnStart:Nn).^Fp1(1,4,2,1)+ ...
YqCoeff0(2,3,2,1).*yy(wnStart:Nn).^Fp1(2,3,2,1)+ ...
YqCoeff0(3,2,2,1).*yy(wnStart:Nn).^Fp1(3,2,2,1)+ ...
YqCoeff0(4,1,2,1).*yy(wnStart:Nn).^Fp1(4,1,2,1)+ ...
YqCoeff0(1,5,1,1).*yy(wnStart:Nn).^Fp1(1,5,1,1)+ ...
YqCoeff0(2,4,1,1).*yy(wnStart:Nn).^Fp1(2,4,1,1)+ ...
YqCoeff0(3,3,1,1).*yy(wnStart:Nn).^Fp1(3,3,1,1)+  ...
YqCoeff0(4,2,1,1).*yy(wnStart:Nn).^Fp1(4,2,1,1)+ ...
YqCoeff0(5,1,1,1).*yy(wnStart:Nn).^Fp1(5,1,1,1))*dt^4;

c1(wnStart:Nn)=((YqCoeff0(1,1,1,2).*yy(wnStart:Nn).^Fp1(1,1,1,2).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*yy(wnStart:Nn).^Fp1(1,1,2,2)+YqCoeff0(1,2,1,2).*yy(wnStart:Nn).^Fp1(1,2,1,2)+ ...
YqCoeff0(2,1,1,2).*yy(wnStart:Nn).^Fp1(2,1,1,2)).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*yy(wnStart:Nn).^Fp1(1,1,3,2)+YqCoeff0(1,2,2,2).*yy(wnStart:Nn).^Fp1(1,2,2,2)+ ...
YqCoeff0(2,1,2,2).*yy(wnStart:Nn).^Fp1(2,1,2,2)+YqCoeff0(1,3,1,2).*yy(wnStart:Nn).^Fp1(1,3,1,2)+ ...
YqCoeff0(2,2,1,2).*yy(wnStart:Nn).^Fp1(2,2,1,2)+YqCoeff0(3,1,1,2).*yy(wnStart:Nn).^Fp1(3,1,1,2)).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*yy(wnStart:Nn).^Fp1(1,1,4,2)+YqCoeff0(1,2,3,2).*yy(wnStart:Nn).^Fp1(1,2,3,2)+ ...
YqCoeff0(2,1,3,2).*yy(wnStart:Nn).^Fp1(2,1,3,2)+YqCoeff0(1,3,2,2).*yy(wnStart:Nn).^Fp1(1,3,2,2)+ ...
YqCoeff0(2,2,2,2).*yy(wnStart:Nn).^Fp1(2,2,2,2)+ YqCoeff0(3,1,2,2).*yy(wnStart:Nn).^Fp1(3,1,2,2)+ ...
YqCoeff0(1,4,1,2).*yy(wnStart:Nn).^Fp1(1,4,1,2)+YqCoeff0(2,3,1,2).*yy(wnStart:Nn).^Fp1(2,3,1,2)+ ...
YqCoeff0(3,2,1,2).*yy(wnStart:Nn).^Fp1(3,2,1,2)+YqCoeff0(4,1,1,2).*yy(wnStart:Nn).^Fp1(4,1,1,2)).*dt^3.5);

end
.
.
function [w] = InterpolateNewGrid(Bn,Znew,wnStart,Nn,dNn,Z)
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here

for nn=wnStart+3:Nn-4
x0(nn-wnStart-3+1)=Z(nn);
%x0b(nn-wnStart-3+1)=Z(nn)-1/3*dNn;

x1(nn-wnStart-3+1)=Znew(nn-3);
x2(nn-wnStart-3+1)=Znew(nn-2);
x3(nn-wnStart-3+1)=Znew(nn-1);
x4(nn-wnStart-3+1)=Znew(nn);
x5(nn-wnStart-3+1)=Znew(nn+1);
x6(nn-wnStart-3+1)=Znew(nn+2);
x7(nn-wnStart-3+1)=Znew(nn+3);

y1(nn-wnStart-3+1)=Bn(nn-3);
y2(nn-wnStart-3+1)=Bn(nn-2);
y3(nn-wnStart-3+1)=Bn(nn-1);
y4(nn-wnStart-3+1)=Bn(nn);
y5(nn-wnStart-3+1)=Bn(nn+1);
y6(nn-wnStart-3+1)=Bn(nn+2);
y7(nn-wnStart-3+1)=Bn(nn+3);
end

N=7;
[y0] = InterpolateOrderN8(N,x0,x1,x2,x3,x4,x5,x6,x7,0,y1,y2,y3,y4,y5,y6,y7,0);
w(wnStart+3:Nn-4)=y0(1:Nn-4-wnStart-3+1);

x0a=Z(wnStart+2);
x0b=Z(wnStart+1);

x1=Znew(wnStart);
x2=Znew(wnStart+1);
x3=Znew(wnStart+2);
x4=Znew(wnStart+3);
x5=Znew(wnStart+4);
x6=0;

y1=Bn(wnStart);
y2=Bn(wnStart+1);
y3=Bn(wnStart+2);
y4=Bn(wnStart+3);
y5=Bn(wnStart+4);
y6=0;
N=5;
[y0a] = InterpolateOrderN6(N,x0a,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);

w(wnStart+2)=y0a;

[y0b] = InterpolateOrderN6(N,x0b,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);

w(wnStart+1)=y0b;

x0=Z(wnStart);

x1=Znew(wnStart);
x2=Znew(wnStart+1);
x3=Znew(wnStart+2);
x4=Znew(wnStart+3);

y1=Bn(wnStart);
y2=Bn(wnStart+1);
y3=Bn(wnStart+2);
y4=Bn(wnStart+3);

N=4;
[y0] = InterpolateOrderN6(N,x0,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);
w(wnStart)=y0;

x0a=Z(Nn-3);
x0b=Z(Nn-2);
x1=Znew(Nn-4);
x2=Znew(Nn-3);
x3=Znew(Nn-2);
x4=Znew(Nn-1);
x5=Znew(Nn);
x6=0;

y1=Bn(Nn-4);
y2=Bn(Nn-3);
y3=Bn(Nn-2);
y4=Bn(Nn-1);
y5=Bn(Nn);
y6=0;
N=5;
[y0a] = InterpolateOrderN6(N,x0a,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);
[y0b] = InterpolateOrderN6(N,x0b,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);

w(Nn-3)=y0a;
w(Nn-2)=y0b;
%--------------------------------------------------
x0a=Z(Nn-1);
x0b=Z(Nn);

x1=Znew(Nn-3);
x2=Znew(Nn-2);
x3=Znew(Nn-1);
x4=Znew(Nn);
x5=0;
x6=0;

y1=Bn(Nn-3);
y2=Bn(Nn-2);
y3=Bn(Nn-1);
y4=Bn(Nn);
y5=0;
y6=0;
N=4;
[y0a] = InterpolateOrderN6(N,x0a,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);
[y0b] = InterpolateOrderN6(N,x0b,x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6);

w(Nn-1)=y0a;
w(Nn)=y0b;
end



Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here is another sub function.
.
function [wMu0dt,dwMu0dtdw,d2wMu0dtdw2,d3wMu0dtdw3,d4wMu0dtdw4,d5wMu0dtdw5,d6wMu0dtdw6,d7wMu0dtdw7,d8wMu0dtdw8,c1,dc1dw,d2c1dw2,c2,c3] = CalculateDriftAndVolA404B4Drift(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt)

yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));

Fp2=Fp1/(1-gamma);

wMu0dt(wnStart:Nn)=(YqCoeff0(1,1,2,1).*yy(wnStart:Nn).^Fp1(1,1,2,1)+ ...
YqCoeff0(1,2,1,1).*yy(wnStart:Nn).^Fp1(1,2,1,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1))*dt + ...
(YqCoeff0(1,1,3,1).*yy(wnStart:Nn).^Fp1(1,1,3,1)+ ...
YqCoeff0(1,2,2,1).*yy(wnStart:Nn).^Fp1(1,2,2,1)+ ...
YqCoeff0(2,1,2,1).*yy(wnStart:Nn).^Fp1(2,1,2,1)+ ...
YqCoeff0(1,3,1,1).*yy(wnStart:Nn).^Fp1(1,3,1,1)+ ...
YqCoeff0(2,2,1,1).*yy(wnStart:Nn).^Fp1(2,2,1,1)+ ...
YqCoeff0(3,1,1,1).*yy(wnStart:Nn).^Fp1(3,1,1,1))*dt^2 + ...
(YqCoeff0(1,1,4,1).*yy(wnStart:Nn).^Fp1(1,1,4,1)+ ...
YqCoeff0(1,2,3,1).*yy(wnStart:Nn).^Fp1(1,2,3,1)+ ...
YqCoeff0(2,1,3,1).*yy(wnStart:Nn).^Fp1(2,1,3,1)+ ...
YqCoeff0(1,3,2,1).*yy(wnStart:Nn).^Fp1(1,3,2,1)+ ...
YqCoeff0(2,2,2,1).*yy(wnStart:Nn).^Fp1(2,2,2,1)+ ...
YqCoeff0(3,1,2,1).*yy(wnStart:Nn).^Fp1(3,1,2,1)+ ...
YqCoeff0(1,4,1,1).*yy(wnStart:Nn).^Fp1(1,4,1,1)+ ...
YqCoeff0(2,3,1,1).*yy(wnStart:Nn).^Fp1(2,3,1,1)+ ...
YqCoeff0(3,2,1,1).*yy(wnStart:Nn).^Fp1(3,2,1,1)+ ...
YqCoeff0(4,1,1,1).*yy(wnStart:Nn).^Fp1(4,1,1,1))*dt^3+ ...
(YqCoeff0(1,1,5,1).*yy(wnStart:Nn).^Fp1(1,1,5,1)+ ...
YqCoeff0(1,2,4,1).*yy(wnStart:Nn).^Fp1(1,2,4,1)+ ...
YqCoeff0(2,1,4,1).*yy(wnStart:Nn).^Fp1(2,1,4,1)+ ...
YqCoeff0(2,2,3,1).*yy(wnStart:Nn).^Fp1(2,2,3,1)+ ...
YqCoeff0(3,1,3,1).*yy(wnStart:Nn).^Fp1(3,1,3,1)+ ...
YqCoeff0(1,4,2,1).*yy(wnStart:Nn).^Fp1(1,4,2,1)+ ...
YqCoeff0(2,3,2,1).*yy(wnStart:Nn).^Fp1(2,3,2,1)+ ...
YqCoeff0(3,2,2,1).*yy(wnStart:Nn).^Fp1(3,2,2,1)+ ...
YqCoeff0(4,1,2,1).*yy(wnStart:Nn).^Fp1(4,1,2,1)+ ...
YqCoeff0(1,5,1,1).*yy(wnStart:Nn).^Fp1(1,5,1,1)+ ...
YqCoeff0(2,4,1,1).*yy(wnStart:Nn).^Fp1(2,4,1,1)+ ...
YqCoeff0(3,3,1,1).*yy(wnStart:Nn).^Fp1(3,3,1,1)+  ...
YqCoeff0(4,2,1,1).*yy(wnStart:Nn).^Fp1(4,2,1,1)+ ...
YqCoeff0(5,1,1,1).*yy(wnStart:Nn).^Fp1(5,1,1,1))*dt^4;

%       (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,4,1).*YY(1:paths).^Fp(1,1,4,1)+YCoeff0(1,2,3,1).*YY(1:paths).^Fp(1,2,3,1)+ ...
%       YCoeff0(2,1,3,1).*YY(1:paths).^Fp(2,1,3,1)+YCoeff0(1,3,2,1).*YY(1:paths).^Fp(1,3,2,1)+ ...
%       YCoeff0(2,2,2,1).*YY(1:paths).^Fp(2,2,2,1)+YCoeff0(3,1,2,1).*YY(1:paths).^Fp(3,1,2,1)+ ...
%       YCoeff0(1,4,1,1).*YY(1:paths).^Fp(1,4,1,1)+YCoeff0(2,3,1,1).*YY(1:paths).^Fp(2,3,1,1)+ ...
%       YCoeff0(3,2,1,1).*YY(1:paths).^Fp(3,2,1,1)+YCoeff0(4,1,1,1).*YY(1:paths).^Fp(4,1,1,1))*dtM^3 + ...
%        (YCoeff0(1,1,5,1).*YY(1:paths).^Fp(1,1,5,1)+YCoeff0(1,2,4,1).*YY(1:paths).^Fp(1,2,4,1)+ ...
%     YCoeff0(2,1,4,1).*YY(1:paths).^Fp(2,1,4,1)+YCoeff0(2,1,1,1).*YY(1:paths).^Fp(2,1,1,1)+ ...
%      YCoeff0(2,2,3,1).*YY(1:paths).^Fp(2,2,3,1)+ ...
%      YCoeff0(3,1,3,1).*YY(1:paths).^Fp(3,1,3,1)+YCoeff0(1,4,2,1).*YY(1:paths).^Fp(1,4,2,1)+ ...
%      YCoeff0(2,3,2,1).*YY(1:paths).^Fp(2,3,2,1)+YCoeff0(3,2,2,1).*YY(1:paths).^Fp(3,2,2,1)+ ...
%      YCoeff0(4,1,2,1).*YY(1:paths).^Fp(4,1,2,1)+YCoeff0(1,5,1,1).*YY(1:paths).^Fp(1,5,1,1)+ ...
%      YCoeff0(2,4,1,1).*YY(1:paths).^Fp(2,4,1,1)+ ...
%      YCoeff0(3,3,1,1).*YY(1:paths).^Fp(3,3,1,1)+YCoeff0(4,2,1,1).*YY(1:paths).^Fp(4,2,1,1)+ ...
%      YCoeff0(5,1,1,1).*YY(1:paths).^Fp(5,1,1,1))*dtM^4

dwMu0dtdw(wnStart:Nn)=(YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,2,1))+ ...
YqCoeff0(1,2,1,1).*Fp1(1,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,1,1))+ ...
YqCoeff0(2,1,1,1).*Fp1(2,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,1,1)))*dt + ...
(YqCoeff0(1,1,3,1).*Fp1(1,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,3,1))+ ...
YqCoeff0(1,2,2,1).*Fp1(1,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,2,1))+ ...
YqCoeff0(2,1,2,1).*Fp1(2,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,2,1))+ ...
YqCoeff0(1,3,1,1).*Fp1(1,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,1,1))+ ...
YqCoeff0(2,2,1,1).*Fp1(2,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,1,1))+ ...
YqCoeff0(3,1,1,1).*Fp1(3,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,1,1)))*dt^2 + ...
(YqCoeff0(1,1,4,1).*Fp1(1,1,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,4,1))+ ...
YqCoeff0(1,2,3,1).*Fp1(1,2,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,3,1))+ ...
YqCoeff0(2,1,3,1).*Fp1(2,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,3,1))+ ...
YqCoeff0(1,3,2,1).*Fp1(1,3,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,2,1))+ ...
YqCoeff0(2,2,2,1).*Fp1(2,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,2,1))+ ...
YqCoeff0(3,1,2,1).*Fp1(3,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,2,1))+ ...
YqCoeff0(1,4,1,1).*Fp1(1,4,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,1,1))+ ...
YqCoeff0(2,3,1,1).*Fp1(2,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,1,1))+ ...
YqCoeff0(3,2,1,1).*Fp1(3,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,1,1))+ ...
YqCoeff0(4,1,1,1).*Fp1(4,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,1,1)))*dt^3 + ...
(YqCoeff0(1,1,5,1).*Fp1(1,1,5,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,5,1))+ ...
YqCoeff0(1,2,4,1).*Fp1(1,2,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,4,1))+ ...
YqCoeff0(2,1,4,1).*Fp1(2,1,4,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,4,1))+ ...
YqCoeff0(2,2,3,1).*Fp1(2,2,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,3,1))+ ...
YqCoeff0(3,1,3,1).*Fp1(3,1,3,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,3,1))+ ...
YqCoeff0(1,4,2,1).*Fp1(1,4,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,2,1))+ ...
YqCoeff0(2,3,2,1).*Fp1(2,3,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,2,1))+ ...
YqCoeff0(3,2,2,1).*Fp1(3,2,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,2,1))+ ...
YqCoeff0(4,1,2,1).*Fp1(4,1,2,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,2,1))+ ...
YqCoeff0(1,5,1,1).*Fp1(1,5,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,5,1,1))+ ...
YqCoeff0(2,4,1,1).*Fp1(2,4,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,4,1,1))+ ...
YqCoeff0(3,3,1,1).*Fp1(3,3,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,3,1,1))+  ...
YqCoeff0(4,2,1,1).*Fp1(4,2,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,2,1,1))+ ...
YqCoeff0(5,1,1,1).*Fp1(5,1,1,1).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(5,1,1,1)))*dt^4;

d2wMu0dtdw2(wnStart:Nn)=(1-gamma).*...
((YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*(-1+Fp2(1,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,2,1))+ ...
YqCoeff0(1,2,1,1).*Fp1(1,2,1,1).*(-1+Fp2(1,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,1,1))+ ...
YqCoeff0(2,1,1,1).*Fp1(2,1,1,1).*(-1+Fp2(2,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,1,1)))*dt + ...
(YqCoeff0(1,1,3,1).*Fp1(1,1,3,1).*(-1+Fp2(1,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,3,1))+ ...
YqCoeff0(1,2,2,1).*Fp1(1,2,2,1).*(-1+Fp2(1,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,2,1))+ ...
YqCoeff0(2,1,2,1).*Fp1(2,1,2,1).*(-1+Fp2(2,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,2,1))+ ...
YqCoeff0(1,3,1,1).*Fp1(1,3,1,1).*(-1+Fp2(1,3,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,1,1))+ ...
YqCoeff0(2,2,1,1).*Fp1(2,2,1,1).*(-1+Fp2(2,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,1,1))+ ...
YqCoeff0(3,1,1,1).*Fp1(3,1,1,1).*(-1+Fp2(3,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,1,1)))*dt^2+ ...
(YqCoeff0(1,1,4,1).*Fp1(1,1,4,1).*(-1+Fp2(1,1,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,4,1))+ ...
YqCoeff0(1,2,3,1).*Fp1(1,2,3,1).*(-1+Fp2(1,2,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,3,1))+ ...
YqCoeff0(2,1,3,1).*Fp1(2,1,3,1).*(-1+Fp2(2,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,3,1))+ ...
YqCoeff0(1,3,2,1).*Fp1(1,3,2,1).*(-1+Fp2(1,3,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,2,1))+ ...
YqCoeff0(2,2,2,1).*Fp1(2,2,2,1).*(-1+Fp2(2,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,2,1))+ ...
YqCoeff0(3,1,2,1).*Fp1(3,1,2,1).*(-1+Fp2(3,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,2,1))+ ...
YqCoeff0(1,4,1,1).*Fp1(1,4,1,1).*(-1+Fp2(1,4,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,1,1))+ ...
YqCoeff0(2,3,1,1).*Fp1(2,3,1,1).*(-1+Fp2(2,3,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,1,1))+ ...
YqCoeff0(3,2,1,1).*Fp1(3,2,1,1).*(-1+Fp2(3,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,1,1))+ ...
YqCoeff0(4,1,1,1).*Fp1(4,1,1,1).*(-1+Fp2(4,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,1,1)))*dt^3 + ...
(YqCoeff0(1,1,5,1).*Fp1(1,1,5,1).*(-1+Fp2(1,1,5,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,5,1))+ ...
YqCoeff0(1,2,4,1).*Fp1(1,2,4,1).*(-1+Fp2(1,2,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,4,1))+ ...
YqCoeff0(2,1,4,1).*Fp1(2,1,4,1).*(-1+Fp2(2,1,4,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,4,1))+ ...
YqCoeff0(2,2,3,1).*Fp1(2,2,3,1).*(-1+Fp2(2,2,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,3,1))+ ...
YqCoeff0(3,1,3,1).*Fp1(3,1,3,1).*(-1+Fp2(3,1,3,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,3,1))+ ...
YqCoeff0(1,4,2,1).*Fp1(1,4,2,1).*(-1+Fp2(1,4,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,2,1))+ ...
YqCoeff0(2,3,2,1).*Fp1(2,3,2,1).*(-1+Fp2(2,3,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,2,1))+ ...
YqCoeff0(3,2,2,1).*Fp1(3,2,2,1).*(-1+Fp2(3,2,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,2,1))+ ...
YqCoeff0(4,1,2,1).*Fp1(4,1,2,1).*(-1+Fp2(4,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,2,1))+ ...
YqCoeff0(1,5,1,1).*Fp1(1,5,1,1).*(-1+Fp2(1,5,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,5,1,1))+ ...
YqCoeff0(2,4,1,1).*Fp1(2,4,1,1).*(-1+Fp2(2,4,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,4,1,1))+ ...
YqCoeff0(3,3,1,1).*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^Fp1(1,2,3,2)+ ...
YqCoeff0(2,1,3,2).*yy(wnStart:Nn).^Fp1(2,1,3,2)+YqCoeff0(1,3,2,2).*yy(wnStart:Nn).^Fp1(1,3,2,2)+ ...
YqCoeff0(2,2,2,2).*yy(wnStart:Nn).^Fp1(2,2,2,2)+ YqCoeff0(3,1,2,2).*yy(wnStart:Nn).^Fp1(3,1,2,2)+ ...
YqCoeff0(1,4,1,2).*yy(wnStart:Nn).^Fp1(1,4,1,2)+YqCoeff0(2,3,1,2).*yy(wnStart:Nn).^Fp1(2,3,1,2)+ ...
YqCoeff0(3,2,1,2).*yy(wnStart:Nn).^Fp1(3,2,1,2)+YqCoeff0(4,1,1,2).*yy(wnStart:Nn).^Fp1(4,1,1,2)).*dt^3.5);

dc1dw(wnStart:Nn)=((YqCoeff0(1,1,1,2).*Fp1(1,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,1,2)).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*Fp1(1,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,2,2))+ ...
YqCoeff0(1,2,1,2).*Fp1(1,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,1,2))+ ...
YqCoeff0(2,1,1,2).*Fp1(2,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,1,2))).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*Fp1(1,1,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,3,2))+ ...
YqCoeff0(1,2,2,2).*Fp1(1,2,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,2,2))+ ...
YqCoeff0(2,1,2,2).*Fp1(2,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,2,2))+ ...
YqCoeff0(1,3,1,2).*Fp1(1,3,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,1,2))+ ...
YqCoeff0(2,2,1,2).*Fp1(2,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,1,2))+ ...
YqCoeff0(3,1,1,2).*Fp1(3,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,1,2))).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*Fp1(1,1,4,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,1,4,2))+ ...
YqCoeff0(1,2,3,2).*Fp1(1,2,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,2,3,2))+ ...
YqCoeff0(2,1,3,2).*Fp1(2,1,3,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,1,3,2))+ ...
YqCoeff0(1,3,2,2).*Fp1(1,3,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,3,2,2))+ ...
YqCoeff0(2,2,2,2).*Fp1(2,2,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,2,2,2))+ ...
YqCoeff0(3,1,2,2).*Fp1(3,1,2,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,1,2,2))+ ...
YqCoeff0(1,4,1,2).*Fp1(1,4,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(1,4,1,2))+ ...
YqCoeff0(2,3,1,2).*Fp1(2,3,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(2,3,1,2))+ ...
YqCoeff0(3,2,1,2).*Fp1(3,2,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(3,2,1,2))+ ...
YqCoeff0(4,1,1,2).*Fp1(4,1,1,2).*((1-gamma).*w(wnStart:Nn)).^(-1+Fp2(4,1,1,2))).*dt^3.5);

d2c1dw2(wnStart:Nn)=(1-gamma).*((YqCoeff0(1,1,1,2).*Fp1(1,1,1,2).*(-1+Fp2(1,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,1,2)).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*Fp1(1,1,2,2).*(-1+Fp2(1,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,2,2))+ ...
YqCoeff0(1,2,1,2).*Fp1(1,2,1,2).*(-1+Fp2(1,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,1,2))+ ...
YqCoeff0(2,1,1,2).*Fp1(2,1,1,2).*(-1+Fp2(2,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,1,2))).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*Fp1(1,1,3,2).*(-1+Fp2(1,1,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,3,2))+ ...
YqCoeff0(1,2,2,2).*Fp1(1,2,2,2).*(-1+Fp2(1,2,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,2,2))+ ...
YqCoeff0(2,1,2,2).*Fp1(2,1,2,2).*(-1+Fp2(2,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,2,2))+ ...
YqCoeff0(1,3,1,2).*Fp1(1,3,1,2).*(-1+Fp2(1,3,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,1,2))+ ...
YqCoeff0(2,2,1,2).*Fp1(2,2,1,2).*(-1+Fp2(2,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,1,2))+ ...
YqCoeff0(3,1,1,2).*Fp1(3,1,1,2).*(-1+Fp2(3,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,1,2))).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*Fp1(1,1,4,2).*(-1+Fp2(1,1,4,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,1,4,2))+ ...
YqCoeff0(1,2,3,2).*Fp1(1,2,3,2).*(-1+Fp2(1,2,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,2,3,2))+ ...
YqCoeff0(2,1,3,2).*Fp1(2,1,3,2).*(-1+Fp2(2,1,3,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,1,3,2))+ ...
YqCoeff0(1,3,2,2).*Fp1(1,3,2,2).*(-1+Fp2(1,3,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,3,2,2))+ ...
YqCoeff0(2,2,2,2).*Fp1(2,2,2,2).*(-1+Fp2(2,2,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,2,2,2))+ ...
YqCoeff0(3,1,2,2).*Fp1(3,1,2,2).*(-1+Fp2(3,1,2,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,1,2,2))+ ...
YqCoeff0(1,4,1,2).*Fp1(1,4,1,2).*(-1+Fp2(1,4,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(1,4,1,2))+ ...
YqCoeff0(2,3,1,2).*Fp1(2,3,1,2).*(-1+Fp2(2,3,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(2,3,1,2))+ ...
YqCoeff0(3,2,1,2).*Fp1(3,2,1,2).*(-1+Fp2(3,2,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(3,2,1,2))+ ...
YqCoeff0(4,1,1,2).*Fp1(4,1,1,2).*(-1+Fp2(4,1,1,2)).*((1-gamma).*w(wnStart:Nn)).^(-2+Fp2(4,1,1,2))).*dt^3.5);

c2(wnStart:Nn)=((YqCoeff0(1,1,1,3).*yy(wnStart:Nn).^Fp1(1,1,1,3) *dt) + ...
(YqCoeff0(1,1,2,3).*yy(wnStart:Nn).^Fp1(1,1,2,3)+ ...
YqCoeff0(1,2,1,3).*yy(wnStart:Nn).^Fp1(1,2,1,3)+ ...
YqCoeff0(2,1,1,3).*yy(wnStart:Nn).^Fp1(2,1,1,3)).*dt^2+ ...
(YqCoeff0(1,1,3,3).*yy(wnStart:Nn).^Fp1(1,1,3,3)+ ...
YqCoeff0(1,2,2,3).*yy(wnStart:Nn).^Fp1(1,2,2,3)+ ...
YqCoeff0(2,1,2,3).*yy(wnStart:Nn).^Fp1(2,1,2,3) + ...
YqCoeff0(1,3,1,3).*yy(wnStart:Nn).^Fp1(1,3,1,3)+ ...
YqCoeff0(2,2,1,3).*yy(wnStart:Nn).^Fp1(2,2,1,3)+ ...
YqCoeff0(3,1,1,3).*yy(wnStart:Nn).^Fp1(3,1,1,3)).*dt^3);

c3(wnStart:Nn)=((YqCoeff0(1,1,1,4).*yy(wnStart:Nn).^Fp1(1,1,1,4)*dt^1.5 )+ ...
(YqCoeff0(1,1,2,4).*yy(wnStart:Nn).^Fp1(1,1,2,4)+ ...
YqCoeff0(1,2,1,4).*yy(wnStart:Nn).^Fp1(1,2,1,4)+ ...
YqCoeff0(2,1,1,4).*yy(wnStart:Nn).^Fp1(2,1,1,4))*dt^2.5);

end

.
Another sub function.
.
function [dwdZ,d2wdZ2,d3wdZ3] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,w,Z)

[wS] = InterpolateOrderN6(6,Z(wnStart)-dNn,Z(wnStart),Z(wnStart+1),Z(wnStart+2),Z(wnStart+3),Z(wnStart+4),Z(wnStart+5),w(wnStart),w(wnStart+1),w(wnStart+2),w(wnStart+3),w(wnStart+4),w(wnStart+5));
[wE] = InterpolateOrderN6(6,Z(Nn)+dNn,Z(Nn),Z(Nn-1),Z(Nn-2),Z(Nn-3),Z(Nn-4),Z(Nn-5),w(Nn),w(Nn-1),w(Nn-2),w(Nn-3),w(Nn-4),w(Nn-5));

[wS2] = InterpolateOrderN6(6,Z(wnStart)-2*dNn,Z(wnStart),Z(wnStart+1),Z(wnStart+2),Z(wnStart+3),Z(wnStart+4),Z(wnStart+5),w(wnStart),w(wnStart+1),w(wnStart+2),w(wnStart+3),w(wnStart+4),w(wnStart+5));
[wE2] = InterpolateOrderN6(6,Z(Nn)+2*dNn,Z(Nn),Z(Nn-1),Z(Nn-2),Z(Nn-3),Z(Nn-4),Z(Nn-5),w(Nn),w(Nn-1),w(Nn-2),w(Nn-3),w(Nn-4),w(Nn-5));

d2wdZ2(wnStart)=(wS-2*w(wnStart)+w(wnStart+1))/(dNn.^2);
d2wdZ2(wnStart+1:Nn-1)=(w(wnStart:Nn-2)-2*w(wnStart+1:Nn-1)+1*w(wnStart+2:Nn))/(dNn.^2);
d2wdZ2(Nn)=(wE-2*w(Nn)+w(Nn-1))/(dNn.^2);

dwdZ(wnStart)=(-1*wS+1*w(wnStart+1))/(2*dNn);
dwdZ(wnStart+1:Nn-1)=(-1*w(wnStart:Nn-2)+1*w(wnStart+2:Nn))/(2*dNn);
dwdZ(Nn)=(wE-w(Nn-1))/(2*dNn);

dwdZ(wnStart+3:Nn-3) = (-1*w(wnStart:Nn-6)+9*w(wnStart+1:Nn-5)-45*w(wnStart+2:Nn-4)+0*w(wnStart+3:Nn-3)+45*w(wnStart+4:Nn-2)- ...
9*w(wnStart+5:Nn-1)+1*w(wnStart+6:Nn))/(60*dNn);

[dwdZ(wnStart)] = InterpolateOrderN6(5,Z(wnStart),Z(wnStart+1),Z(wnStart+2),Z(wnStart+3),Z(wnStart+4),Z(wnStart+5),0,dwdZ(wnStart+1),dwdZ(wnStart+2),dwdZ(wnStart+3),dwdZ(wnStart+4),dwdZ(wnStart+5),0);
[dwdZ(Nn)] = InterpolateOrderN6(5,Z(Nn),Z(Nn-1),Z(Nn-2),Z(Nn-3),Z(Nn-4),Z(Nn-5),0,dwdZ(Nn-1),dwdZ(Nn-2),dwdZ(Nn-3),dwdZ(Nn-4),dwdZ(Nn-5),0);

d2wdZ2(wnStart+3:Nn-3) = (2*w(wnStart:Nn-6)-27*w(wnStart+1:Nn-5)+270*w(wnStart+2:Nn-4)-490*w(wnStart+3:Nn-3)+270*w(wnStart+4:Nn-2)- ...
27*w(wnStart+5:Nn-1)+2*w(wnStart+6:Nn))/(180*dNn.^2);

% (2*f[i-3]-27*f[i-2]+270*f[i-1]-490*f[i+0]+270*f[i+1]-27*f[i+2]+2*f[i+3])/(180*1.0*h**2)

[d2wdZ2(wnStart)] = InterpolateOrderN6(5,Z(wnStart),Z(wnStart+1),Z(wnStart+2),Z(wnStart+3),Z(wnStart+4),Z(wnStart+5),0,d2wdZ2(wnStart+1),d2wdZ2(wnStart+2),d2wdZ2(wnStart+3),d2wdZ2(wnStart+4),d2wdZ2(wnStart+5),0);
[d2wdZ2(Nn)] = InterpolateOrderN6(5,Z(Nn),Z(Nn-1),Z(Nn-2),Z(Nn-3),Z(Nn-4),Z(Nn-5),0,d2wdZ2(Nn-1),d2wdZ2(Nn-2),d2wdZ2(Nn-3),d2wdZ2(Nn-4),d2wdZ2(Nn-5),0);

d3wdZ3(wnStart+2:Nn-2)=(-1*w(wnStart:Nn-4)+2*w(wnStart+1:Nn-3)+0*w(wnStart+2:Nn-2)-2*w(wnStart+3:Nn-1)+w(wnStart+4:Nn))/(2*dNn^3);
d3wdZ3(wnStart+1)=(-1*wS+2*w(wnStart)+0*w(wnStart+1)-2*w(wnStart+2)+w(wnStart+3))/(2*dNn^3);
d3wdZ3(wnStart)=(-1*wS2+2*wS+0*w(wnStart)-2*w(wnStart+1)+w(wnStart+2))/(2*dNn^3);
d3wdZ3(Nn-1)=(-1*w(Nn-3)+2*w(Nn-2)+0*w(Nn-1)-2*w(Nn)+wE)/(2*dNn^3);
d3wdZ3(Nn)=(-1*w(Nn-2)+2*w(Nn-1)+0*w(Nn)-2*wE+wE2)/(2*dNn^3);

end
.
Another sub function.
.
function [dwdZ,d2wdZ2,d3wdZ3,d4wdZ4] = First4Derivatives2ndOrderEqSpaced(wnStart,Nn,dNn,w,Z)

d4wdZ4(wnStart)=(3*w(wnStart)-14*w(wnStart+1)+26*w(wnStart+2)-24*w(wnStart+3)+11*w(wnStart+4)-2*w(wnStart+5))/(dNn.^4);
d4wdZ4(wnStart+1)=(2*w(wnStart)-9*w(wnStart+1)+16*w(wnStart+2)-14*w(wnStart+3)+6*w(wnStart+4)-1*w(wnStart+5))/(dNn.^4);
d4wdZ4(wnStart+2:Nn-2)=(1*w(wnStart:Nn-4)-4*w(wnStart+1:Nn-3)+6*w(wnStart+2:Nn-2)-4*w(wnStart+3:Nn-1)+1*w(wnStart+4:Nn))/(dNn.^4);
d4wdZ4(Nn-1)=(2*w(Nn)-9*w(Nn-1)+16*w(Nn-2)-14*w(Nn-3)+6*w(Nn-4)-1*w(Nn-5))/(dNn.^4);
d4wdZ4(Nn)=(3*w(Nn)-14*w(Nn-1)+26*w(Nn-2)-24*w(Nn-3)+11*w(Nn-4)-2*w(Nn-5))/(dNn.^4);

d3wdZ3(wnStart)=(-5*w(wnStart)+18*w(wnStart+1)-24*w(wnStart+2)+14*w(wnStart+3)-3*w(wnStart+4))/(2*dNn.^3);
d3wdZ3(wnStart+1)=(-3*w(wnStart)+10*w(wnStart+1)-12*w(wnStart+2)+6*w(wnStart+3)-1*w(wnStart+4))/(2*dNn.^3);
d3wdZ3(wnStart+2:Nn-2)=(-1*w(wnStart:Nn-4)+2*w(wnStart+1:Nn-3)+0*w(wnStart+2:Nn-2)-2*w(wnStart+3:Nn-1)+1*w(wnStart+4:Nn))/(2*dNn.^3);
d3wdZ3(Nn-1)=(3*w(Nn)-10*w(Nn-1)+12*w(Nn-2)-6*w(Nn-3)+1*w(Nn-4))/(2*dNn.^3);
d3wdZ3(Nn)=(5*w(Nn)-18*w(Nn-1)+24*w(Nn-2)-14*w(Nn-3)+3*w(Nn-4))/(2*dNn.^3);

%d4wdZ4(wnStart:Nn)=0;
%d3wdZ3(wnStart:Nn)=0;

[wS] = InterpolateOrderN6(5,Z(wnStart)-dNn,Z(wnStart),Z(wnStart+1),Z(wnStart+2),Z(wnStart+3),Z(wnStart+4),Z(wnStart+5),w(wnStart),w(wnStart+1),w(wnStart+2),w(wnStart+3),w(wnStart+4),w(wnStart+5));
[wE] = InterpolateOrderN6(5,Z(Nn)+dNn,Z(Nn),Z(Nn-1),Z(Nn-2),Z(Nn-3),Z(Nn-4),Z(Nn-5),w(Nn),w(Nn-1),w(Nn-2),w(Nn-3),w(Nn-4),w(Nn-5));

d2wdZ2(wnStart)=(wS-2*w(wnStart)+w(wnStart+1))/(dNn.^2);
d2wdZ2(wnStart+1:Nn-1)=(w(wnStart:Nn-2)-2*w(wnStart+1:Nn-1)+1*w(wnStart+2:Nn))/(dNn.^2);
d2wdZ2(Nn)=(wE-2*w(Nn)+w(Nn-1))/(dNn.^2);

dwdZ(wnStart)=(-1*wS+1*w(wnStart+1))/(2*dNn);
dwdZ(wnStart+1:Nn-1)=(-1*w(wnStart:Nn-2)+1*w(wnStart+2:Nn))/(2*dNn);
dwdZ(Nn)=(wE-w(Nn-1))/(2*dNn);

%dwdZ(wnStart)=(-3*w(wnStart)+4*w(wnStart+1)-1*w(wnStart+2))/(2*dNn);
%dwdZ(wnStart+1:Nn-1)=(-1*w(wnStart:Nn-2)+1*w(wnStart+2:Nn))/(2*dNn);
%dwdZ(Nn)=(3*w(Nn)-4*w(Nn-1)+1*w(Nn-2))/(2*dNn);

end


.
.
When you run the first main program with the values in it, you will get the following results
ItoHermiteMean =
1.001330407913867
true Mean only applicable to standard SV mean reverting type models otherwise disregard
TrueMean =
1
Original process average from monte carlo
MCMean =
1.000349829748880
MCVar =
0.130692067168452
Original process average from our simulation
ItoHermiteMean =
1.001330407913867
ItoHermiteVar =
0.130365031698457
true Mean only applicble to standard SV mean reverting type models otherwise disregard
TrueMean =
1
IndexMax =
102
red line is density of SDE from Ito-Hermite method, green is monte carlo.
.
And you will get the last graph posted in the first of the three posts with graphs.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

I was playing with the program and I tried expansion order zero i.e just the first principal term in Taylor expansion with $P_{m,n}=P_{m0,n} dP_{m}$(which is Integral0 in my program) and results were quite close to my eighth order expansion. I noticed that for the kind of SDEs I have been working with, the accuracy decreased by 2-15 bp at most on top of inaccuracy in eighth order expansion while graph was still indistinguishable from monte carlo or 8th order Taylor expansion. Eighth order expansion was taking 25 secs on my very slow computer while zero order was taking slightly more than two secs. When I increased order of taylor expansion from six to eight, I noticed no improvement but I continued to carry the same order in my later programs to be on the safe side but now I think unless SDE is very difficult, we will rarely need order beyond 4th order of Taylor expansion which can probably take 8-10 seconds on my computer. All of the above numbers for accuracy are using a density simulation step size very close to 1/32.
I will soon be posting more programs.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here is the new program with Order zero Taylor expansion.
.
function [] = SDETransProb08WmtGrid200b74DriftO0()

%Copyright Ahsan Amin. Infiniti derivatives Technologies.
%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)

%I have not directly simulated the SDE but simulated the transformed
%Besse1 process version of the SDE and then changed coordinates to retreive
%the SDE in original coordinates.
%The present program will analytically evolve only the Bessel Process version of the
%SDE in transformed coordinates.

%In this program, I have simulated the density for first few very small time
%intervals using a simple method. After first few intervals, I start
%simulating with probability mass transfer method.  Please experiment with step size and
%you will find the method robust enough. This is just an early version and
%I will be coming with better versions soon.

%dt=.125/16/2/2/2;   % Regular Simulation time interval to advance the density before large step by CDF method.
dt=1/48*1/2*1/32;             %decrease dt for accuracy.

Tt1=32;
Tt2=32;

T=1;
dt2=(T-dt*Tt1)/Tt2;
Tt=Tt1+Tt2;
T=dt*Tt1+dt2*Tt2;
OrderA=4;  %
%dtM=dt/8;%.125/2/4;%Monte carlo time interval size dtM.
%TtM=Tt*8;%T*2*8*4;%Monte carlo number of simulation intervals.
dtM=T/32;%.125/2/4;%Monte carlo time interval size dtM.
TtM=32;

dNn=.05;   % Normal density subdivisions width. would change with number of subdivisions
Nn=200;  % No of normal density subdivisions
NnMidl=100;%One half density Subdivision left from mid of normal density(low)
NnMidh=101;%One half density subdivision right from the mid of normal density(high)
NnMid=4.0;

Z(1:Nn)=(((1:Nn)-20.5)*dNn-NnMid)

%There are two indices. One goes to Nn=200. This is used for calculation of
%SDE simulation at next time level t+1. This covers -5 to +5 SDs of
%underlying Z.
%Second index Nn1 below goes from 1 to 240. This covers -6 to +6 SD of
%underlying Z. In probability mass transfer methos, we simulate the SDE
%from -5 SD to +5 SD and then extrapolate on both sides so that we have a
%new grid from -6 SD to +6 SD(1:Nn1). This extrapolated grid(which is assigned time t)
%is used to calculate the time t+1 grid from -5 SD to +5 SD(1:Nn).
%Grid spacing is the same on both grids.
Nn1=240;
%dNn1=.05;
Nn1Midl=120;
Nn1Midh=121;
Nn1Mid=6.025;

Z1(1:Nn1)=((1:Nn1)*dNn-Nn1Mid)
Z1Prob(1)=normcdf(-5.95)-normcdf(-6.0);
Z1Prob(Nn1)=normcdf(6.0)-normcdf(5.95);
Z1Prob(2:Nn1-1)=normcdf(.5*Z1(2:Nn1-1)+.5*Z1(3:Nn1),0,1)-normcdf(.5*Z1(2:Nn1-1)+.5*Z1(1:Nn1-2),0,1);

ZProb(1:Nn)=Z1Prob(21:Nn1-20);
Z(1:Nn)=Z1(21:Nn1-20);

Z1Prob
sum(Z1Prob(1:Nn1))
sum(ZProb(1:Nn))
str=input('Look at Zs');

x0=.25;   % starting value of SDE
beta1=0.0;
beta2=1.00;   % Second drift term power.
gamma=.75;%50;   % volatility power.
kappa=1.0;%.950;   %mean reversion parameter.
theta=.040;%mean reversion target
sigma0=.650;%Volatility value

%you can specify any general mu1 and mu2 and beta1 and beta2.
mu1=+1*theta*kappa;   %first drift coefficient.
mu2=-1*kappa;    % Second drift coefficient.
%mu1=0;
%mu2=0;

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

w(1:Nn)=x0^(1-gamma)/(1-gamma);

Z
Z1
str=input('Look at Zs');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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;

mu33(1)=1;
mu44(1)=1;
mu33(2:OrderA+1)=0;
mu44(2:OrderA+1)=0;

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);
YqzCoeff0(l1,l2,l3,l4) =YqCoeff(l1,l2,l3,l4).*mu33(l1).*mu44(l2).*sigma22(l3).*sigma11(l4);
end
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wnStart=1;%
yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
%The above yy(wnStart:Nn)=x0;

tic

for tt=1:Tt

if(tt==1)

w_00=x0^(1-gamma)/(1-gamma);
%[wMu0dt,c1,c2,c3] = CalculateDriftAndVolA404B4Drift02(w_00,1,1,YqCoeff0,Fp1,gamma,dt);
[wMu0dt,c1] = CalculateDriftAndVolA404(w_00,1,1,YqCoeff0,Fp1,gamma,dt);
w(1:Nn)=x0^(1-gamma)/(1-gamma)+wMu0dt(1)+c1(1).*Z(1:Nn);%+c2(1).*(Z(1:Nn).^2-1)+c3(1).*(Z(1:Nn).^3-3*Z(1:Nn));

end
if((tt>1) && (tt<=Tt1))
%The first Tt1 steps are simulated using a simple and slightly
%altered version of the method I have used for simple CEV noises
%and brownian motions. I used it only for starting few time steps
%since density remains pretty much very close to linear here. If
%that is not the case, you would have to alter this.
[wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);
%dw(wnStart:Nn)=c1(wnStart:Nn);%.*Z(wnStart:Nn) ;% ...
dw(wnStart:Nn)=c1(wnStart:Nn).*Z(wnStart:Nn) ;% ...
dw2(wnStart:Nn)=dw(wnStart:Nn).^2;
[wMid] = InterpolateOrderN8(8,0,Z(NnMidl-3),Z(NnMidl-2),Z(NnMidl-1),Z(NnMidl),Z(NnMidh),Z(NnMidh+1),Z(NnMidh+2),Z(NnMidh+3),w(NnMidl-3),w(NnMidl-2),w(NnMidl-1),w(NnMidl),w(NnMidh),w(NnMidh+1),w(NnMidh+2),w(NnMidh+3));
B(1:Nn)=w(1:Nn)-wMid;
%[dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,B,Z);
B(wnStart:Nn)=sign(B(wnStart:Nn)+dw(wnStart:Nn)).* ...
sqrt(abs(sign(B(wnStart:Nn)).*(B(wnStart:Nn)).^2+ ...
sign(dw(wnStart:Nn)).*dw2(wnStart:Nn)));

w(wnStart:Nn)=wMid+wMu0dt(wnStart:Nn)+B(wnStart:Nn);

end
%Now starts the calculation for SDE simulation using probability mass
%transfer.
if(tt>Tt1)

dt=dt2;

[wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);

%size(w)
%w
[dwdZ_0,d2wdZ2_0,d3wdZ3_0] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,w,Z);
%dwdZ_0(wnStart)=(w(wnStart+1)-w(wnStart))/dNn;

%Above are derivative names with an appended zero "_0". These are derivatives on the
%Nn grid (time t+1 grid). When there are no appended zeros, those are
%derivatives on the Nn1 grid i.e time t grid.
%       if(wnStart==1)
BB1(21:Nn+20)=w(1:Nn);
BB1(1:20)=BB1(21)-(20:-1:1).*dwdZ_0(1)*dNn;
%Below we do extrapolation on right boundary from 5SD to 6 SD
%and then assign it to BB1 which is Bm(t).

%BB1(Nn+20+1:Nn+20+20)=BB1(Nn+20)+(1:20).*dwdZ_0(Nn)*dNn;
BB1(Nn-10+20+1:Nn+20+20)=BB1(Nn-10+20)+(1:30).*dwdZ_0(Nn-10)*dNn;

wnStart1=1;
% %       else
%            BB1(1:20+wnStart)=0.0;
%             BB1(wnStart+20:Nn-10+20)=w(wnStart:Nn-10);
%             BB1(Nn-10+20+1:Nn+20+20)=BB1(Nn-10+20)+(1:30).*dwdZ_0(Nn-10)*dNn;
%         wnStart1=20+wnStart;
%        end
%Below is the loop version of above extrapolations
%        for nn=1:Nn
%            BB1(nn+20)=w(nn);
%        end

%        for nn=20:-1:1
%            %BB1(nn)=BB1(nn+1)-dwdZ_0(1)*dNn ;
%        end
%
%        for nn=Nn+20+1:Nn+20+20
%            BB1(nn)=BB1(nn-1)+ dwdZ_0(Nn)*dNn ;
%        end
%
%The function below calculates grid cell boundaries from mid
%points of the grids using interpolation and relevant Z. The
%grid is equidistant and symmetric with respect to underlying Z
%but not with respect to the Bessel variable. We use the value
%of Z at boundaries of each cell to interpolate the value of
%bessel variable there.
%       BB1(BB1<=0)=0.00000000000001;
%       [BB1a,BB1b] = CalculateGridStartsAndEndsBoundedEnds(BB1,Z1,wnStart1,Nn1,dNn);

%Below,I later assigned Z1 to ZZ1 and N1 to NN1 ans similarly for
%Z1Prob since earlier I had several(three different) cases of
%extrapolation ranging from 5 to 6 SD, and 5 to 5.5 SD and 5 to 5.25 SD.
%I Was thinking when time would advance and grid would have
%expanded and if dt would stay the same, the user might be
%better off choosing a smaller grid with better extrapolation.

ZZ1=Z1;
NN1=Nn1;
ZZProb(1:NN1)=Z1Prob(1:Nn1);

%plot(ZZ1(1:NN1),BB1(1:NN1),'b',ZZ1(1:NN1),BB1a(1:NN1),'y',ZZ1(1:NN1),BB1b(1:NN1),'k',Z(1:Nn),B1(1:Nn),'r')
%str=input('Look at B1 and BB1, original and extrapolated version and boundaries');

%robust function.
%      [dBdZ,d2BdZ2,d3BdZ3] = First3Derivatives2ndOrderEqSpacedA(1,NN1,dNn,BB1,Z1);
%      [dwdZAA,d2wdZ2AA,d3wdZ3AA,d4BdZ4] = First4Derivatives2ndOrderEqSpaced(1,NN1,dNn,BB1,Z1);

%plot((1:NN1),dBdZ(1:NN1),'r')
%str=input('Look at 1st derivative');
%plot((1:NN1),d2BdZ2(1:NN1),'r')
%str=input('Look at 2nd derivative');
%plot((1:NN1),d3BdZ3(1:NN1),'r')
%str=input('Look at 3rd derivative');

%Below calculate hermite polynomials of Z1 underlying the
%Grid BB1 which is "Bm(t)" grid. We will use these hermite
%polynomials and later derivatives to calculate the derivatives of
%density of Bm with respect to Bm. dpdBm calcualted at centre
%of the grid cells.

%             He1(1:NN1)=Z1(1:NN1);
%             He2(1:NN1)=Z1(1:NN1).^2-1;
%             He3(1:NN1)=Z1(1:NN1).^3-3*Z1(1:NN1);
%             He4(1:NN1)=Z1(1:NN1).^4-6*Z1(1:NN1).^2+3;
%             He5(1:NN1)=Z1(1:NN1).^5-10*Z1(1:NN1).^3+15;
%             He6(1:NN1)=Z1(1:NN1).^6-15*Z1(1:NN1).^4+45*Z1(1:NN1).^2-15;
%             He7(1:NN1)=Z1(1:NN1).^7-21*Z1(1:NN1).^5+105*Z1(1:NN1).^3-105*Z1(1:NN1);
%             He8(1:NN1)=Z1(1:NN1).^8-28*Z1(1:NN1).^6+210*Z1(1:NN1).^4-420*Z1(1:NN1).^2+105;
%             He9(1:NN1)=Z1(1:NN1).^9-36*Z1(1:NN1).^7+378*Z1(1:NN1).^5-1260*Z1(1:NN1).^3+945*Z1(1:NN1);
%
%
%             Zpdf(1:NN1)=normpdf(Z1(1:NN1),0,1);
%             dZpdf(1:NN1)=-He1(1:NN1).*Zpdf(1:NN1);
%             d2Zpdf(1:NN1)=He2(1:NN1).*Zpdf(1:NN1);
%             d3Zpdf(1:NN1)=-He3(1:NN1).*Zpdf(1:NN1);
%             d4Zpdf(1:NN1)=He4(1:NN1).*Zpdf(1:NN1);
%             d5Zpdf(1:NN1)=-He5(1:NN1).*Zpdf(1:NN1);
%             d6Zpdf(1:NN1)=He6(1:NN1).*Zpdf(1:NN1);
%             d7Zpdf(1:NN1)=-He7(1:NN1).*Zpdf(1:NN1);
%             d8Zpdf(1:NN1)=He8(1:NN1).*Zpdf(1:NN1);
%             d9Zpdf(1:NN1)=-He9(1:NN1).*Zpdf(1:NN1);
%
%
%
%
%
%
%
%
%
%             %Please note that in calculations below, we are using derivatives without
%             %appended zeros since they are on BB1 "Bm(t)" grid that ranges from 1:Nn1 or equivalently 1:NN1.
%             dZdB(1:NN1)=1.0./dBdZ(1:NN1);
%             d2ZdB2(1:NN1)=-d2BdZ2(1:NN1).*dZdB(1:NN1).^3;
%             d3ZdB3(1:NN1)=-d3BdZ3(1:NN1).*dZdB(1:NN1).^4+3*d2BdZ2(1:NN1).^2.*dZdB(1:NN1).^5;
%             %d4ZdB4(1:NN1)=-d4BdZ4(1:NN1).*dZdB(1:NN1).^5-4*d3BdZ3(1:NN1).*d2ZdB2(1:NN1).*dZdB(1:NN1).^3+ ...
%             %6*d2BdZ2(1:NN1).*d3BdZ3(1:NN1).*dZdB(1:NN1).^6+15*d2BdZ2(1:NN1).^2.*d2ZdB2(1:NN1).*dZdB(1:NN1).^6 ;
%             d4ZdB4(1:NN1)=-d4BdZ4(1:NN1).*dZdB(1:NN1).^5+4*d3BdZ3(1:NN1).*d2ZdB2(1:NN1).*dZdB(1:NN1).^6+ ...
%             6*d2BdZ2(1:NN1).*d3BdZ3(1:NN1).*dZdB(1:NN1).^6-15*d2BdZ2(1:NN1).^2.*d2ZdB2(1:NN1).*dZdB(1:NN1).^7 ;
%             d5ZdB5(1:NN1)=0.0;
%             d6ZdB6(1:NN1)=0.0;
%             d7ZdB7(1:NN1)=0.0;
%             d8ZdB8(1:NN1)=0.0;
%
%
%
%             pm0(1:NN1)=Zpdf(1:NN1).*dZdB(1:NN1);
%             dpm0(1:NN1)=dZpdf(1:NN1).*dZdB(1:NN1).^2+Zpdf(1:NN1).*d2ZdB2(1:NN1);
%             d2pm0(1:NN1)=d2Zpdf(1:NN1).*dZdB(1:NN1).^3+3*dZpdf(1:NN1).*dZdB(1:NN1).*d2ZdB2(1:NN1)+Zpdf(1:NN1).*d3ZdB3(1:NN1);
%             d3pm0(1:NN1)=d3Zpdf(1:NN1).*dZdB(1:NN1).^4+6*d2Zpdf(1:NN1).*dZdB(1:NN1).^2.*d2ZdB2(1:NN1) + ...
%                 4*dZpdf(1:NN1).*dZdB(1:NN1).*d3ZdB3(1:NN1) + 3*dZpdf(1:NN1).*d2ZdB2(1:NN1).^2+ Zpdf(1:NN1).*d4ZdB4(1:NN1);
%
%             d4pm0(1:NN1)=d4Zpdf(1:NN1).*dZdB(1:NN1).^5+10*d3Zpdf(1:NN1).*dZdB(1:NN1).^3.*d2ZdB2(1:NN1) + ...
%                 15*d2Zpdf(1:NN1).*dZdB(1:NN1).*d2ZdB2(1:NN1).^2 + ...
%                 10*d2Zpdf(1:NN1).*dZdB(1:NN1).^2.*d3ZdB3(1:NN1) + ...
%                 10*dZpdf(1:NN1).*d2ZdB2(1:NN1).*d3ZdB3(1:NN1) + ...
%                 5*dZpdf(1:NN1).*dZdB(1:NN1).*d4ZdB4(1:NN1)+ Zpdf(1:NN1).*d5ZdB5(1:NN1);
%
%             d5pm0(1:NN1)=d5Zpdf(1:NN1).*dZdB(1:NN1).^6+15*d4Zpdf(1:NN1).*dZdB(1:NN1).^4.*d2ZdB2(1:NN1) + ...
%                 45*d3Zpdf(1:NN1).*dZdB(1:NN1).^2.*d2ZdB2(1:NN1).^2 + ...
%                 20*d3Zpdf(1:NN1).*dZdB(1:NN1).^3.*d3ZdB3(1:NN1) + ...
%                 15*d2Zpdf(1:NN1).*d2ZdB2(1:NN1).^3 + ...
%                 60*d2Zpdf(1:NN1).*dZdB(1:NN1).*d2ZdB2(1:NN1).*d3ZdB3(1:NN1) + ...
%                 15*d2Zpdf(1:NN1).*d2ZdB2(1:NN1).^2.*d4ZdB4(1:NN1)+ ...
%                 10*dZpdf(1:NN1).*d3ZdB3(1:NN1).^2+ ...
%                 15*dZpdf(1:NN1).*d2ZdB2(1:NN1).*d4ZdB4(1:NN1)+ ...
%                 6*dZpdf(1:NN1).*dZdB(1:NN1).*d5ZdB5(1:NN1)+ ...
%                 Zpdf(1:NN1).*d6ZdB6(1:NN1);
%
%             d6pm0(1:NN1)=20.* (3.* dZdB(1:NN1).* d2Zpdf(1:NN1).* d2ZdB2(1:NN1)+dZdB(1:NN1).^3.* d3Zpdf(1:NN1)+ ...
%                 dZpdf(1:NN1).* d3ZdB3(1:NN1)).* d4ZdB4(1:NN1)+15.* d3ZdB3(1:NN1).* (3.* d2Zpdf(1:NN1).* d2ZdB2(1:NN1).^2+ ...
%                 6 .*dZdB(1:NN1).^2.* d2ZdB2(1:NN1).* d3Zpdf(1:NN1)+4.* dZdB(1:NN1).* d2Zpdf(1:NN1).* d3ZdB3(1:NN1)+ ...
%                 dZdB(1:NN1).^4.* d4Zpdf(1:NN1)+dZpdf(1:NN1).* d4ZdB4(1:NN1))+15 .*(dZdB(1:NN1).^2.* d2Zpdf(1:NN1)+ ...
%                 dZpdf(1:NN1).* d2ZdB2(1:NN1)).* d5ZdB5(1:NN1)+6.* d2ZdB2(1:NN1).* (15.* dZdB(1:NN1).* d2ZdB2(1:NN1).^2.* d3Zpdf(1:NN1)+ ...
%                 10.* d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d3ZdB3(1:NN1)+10.* dZdB(1:NN1).^2.* d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+ ...
%                 10.* dZdB(1:NN1).^3.* d2ZdB2(1:NN1).* d4Zpdf(1:NN1)+5 .*dZdB(1:NN1).* d2Zpdf(1:NN1) .*d4ZdB4(1:NN1)+ ...
%                 dZdB(1:NN1).^5.* d5Zpdf(1:NN1)+dZpdf(1:NN1).* d5ZdB5(1:NN1))+6.*dZpdf(1:NN1).* dZdB(1:NN1) .*d6ZdB6(1:NN1)+ ...
%                 dZdB(1:NN1).* (15 .*d2ZdB2(1:NN1).^3.* d3Zpdf(1:NN1)+60.* dZdB(1:NN1).* d2ZdB2(1:NN1).* d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+ ...
%                 10.* d2Zpdf(1:NN1).* d3ZdB3(1:NN1).^2+45.* dZdB(1:NN1).^2.* d2ZdB2(1:NN1).^2.* d4Zpdf(1:NN1)+ ...
%                 20 .*dZdB(1:NN1).^3.* d3ZdB3(1:NN1).* d4Zpdf(1:NN1)+15 .*d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d4ZdB4(1:NN1)+ ...
%                 15 .*dZdB(1:NN1).^2.* d3Zpdf(1:NN1).* d4ZdB4(1:NN1)+15.* dZdB(1:NN1).^4.* d2ZdB2(1:NN1).* d5Zpdf(1:NN1)+ ...
%                 6 .*dZdB(1:NN1).* d2Zpdf(1:NN1).* d5ZdB5(1:NN1)+dZdB(1:NN1).^6 .*d6Zpdf(1:NN1)+dZpdf(1:NN1).* d6ZdB6(1:NN1))+ ...
%                 Zpdf(1:NN1).* d7ZdB7(1:NN1);
%
%             d7pm0(1:NN1)=35 .* d4ZdB4(1:NN1).* (3.*d2Zpdf(1:NN1) .*d2ZdB2(1:NN1).^2+6.* dZdB(1:NN1).^2.* d2ZdB2(1:NN1).* d3Zpdf(1:NN1)+ ...
%                 4.* dZdB(1:NN1).*d2Zpdf(1:NN1) .*d3ZdB3(1:NN1)+dZdB(1:NN1).^4.* d4Zpdf(1:NN1)+dZpdf(1:NN1).* d4ZdB4(1:NN1))+ ...
%                 35.* (3.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d2ZdB2(1:NN1)+dZdB(1:NN1).^3 .*d3Zpdf(1:NN1)+ ...
%                 dZpdf(1:NN1) .*d3ZdB3(1:NN1)).* d5ZdB5(1:NN1)+21.* d3ZdB3(1:NN1).* (15.* dZdB(1:NN1).* d2ZdB2(1:NN1).^2.* d3Zpdf(1:NN1)+ ...
%                 10.*d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d3ZdB3(1:NN1)+  10.* dZdB(1:NN1).^2 .*d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+ ...
%                 10 .*dZdB(1:NN1).^3.* d2ZdB2(1:NN1) .*d4Zpdf(1:NN1)+5.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d4ZdB4(1:NN1)+ ...
%                 dZdB(1:NN1).^5.* d5Zpdf(1:NN1)+dZpdf(1:NN1) .*d5ZdB5(1:NN1))+21 .*(dZdB(1:NN1).^2.*d2Zpdf(1:NN1)+ ...
%                 dZpdf(1:NN1).* d2ZdB2(1:NN1)).* d6ZdB6(1:NN1)+7 .*d2ZdB2(1:NN1).* (15.* d2ZdB2(1:NN1).^3.* d3Zpdf(1:NN1)+ ...
%                 60.* dZdB(1:NN1).* d2ZdB2(1:NN1).* d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+10.*d2Zpdf(1:NN1).* d3ZdB3(1:NN1).^2+ ...
%                 45.* dZdB(1:NN1).^2.* d2ZdB2(1:NN1).^2.* d4Zpdf(1:NN1)+20 .*dZdB(1:NN1).^3 .*d3ZdB3(1:NN1).* d4Zpdf(1:NN1)+ ...
%                 15.*d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d4ZdB4(1:NN1)+15.* dZdB(1:NN1).^2 .*d3Zpdf(1:NN1).* d4ZdB4(1:NN1)+ ...
%                 15.* dZdB(1:NN1).^4.* d2ZdB2(1:NN1).* d5Zpdf(1:NN1)+60.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d5ZdB5(1:NN1)+ ...
%                 dZdB(1:NN1).^6 .*d6Zpdf(1:NN1)+dZpdf(1:NN1).* d6ZdB6(1:NN1))+7.*dZpdf(1:NN1) .*dZdB(1:NN1).* d7ZdB7(1:NN1)+ ...
%                 dZdB(1:NN1).* (105 .*d2ZdB2(1:NN1).^2.* d3Zpdf(1:NN1).* d3ZdB3(1:NN1)+70.* dZdB(1:NN1) .*d3Zpdf(1:NN1) .*d3ZdB3(1:NN1).^2+ ...
%                 105 .*dZdB(1:NN1).* d2ZdB2(1:NN1).^3.* d4Zpdf(1:NN1)+210.* dZdB(1:NN1).^2.* d2ZdB2(1:NN1).* d3ZdB3(1:NN1).* d4Zpdf(1:NN1)+ ...
%                 105.* dZdB(1:NN1).* d2ZdB2(1:NN1).* d3Zpdf(1:NN1).* d4ZdB4(1:NN1)+35.*d2Zpdf(1:NN1).* d3ZdB3(1:NN1).* d4ZdB4(1:NN1)+ ...
%                 35 * dZdB(1:NN1).^3.* d4Zpdf(1:NN1).* d4ZdB4(1:NN1)+105.* dZdB(1:NN1).^3 .*d2ZdB2(1:NN1).^2 .*d5Zpdf(1:NN1)+ ...
%                 35.* dZdB(1:NN1).^4.* d3ZdB3(1:NN1).* d5Zpdf(1:NN1)+21.*d2Zpdf(1:NN1).* d2ZdB2(1:NN1).* d5ZdB5(1:NN1)+ ...
%                 21.* dZdB(1:NN1).^2.* d3Zpdf(1:NN1).* d5ZdB5(1:NN1)+21 .*dZdB(1:NN1).^5.* d2ZdB2(1:NN1) .* d6Zpdf(1:NN1)+ ...
%                 7.* dZdB(1:NN1).*d2Zpdf(1:NN1).* d6ZdB6(1:NN1)+dZdB(1:NN1).^7.* d7Zpdf(1:NN1)+dZpdf(1:NN1) .*d7ZdB7(1:NN1))+ ...
%                 Zpdf(1:NN1).* d8ZdB8(1:NN1);

ww1(1:NN1)=BB1(1:NN1);%+wMid;
ww1(ww1<=0)=0.000000000001;
%I will improve the above improvisation in a day.
%SigmawA below is volatility associated with each grid cell at time
%t Bm(t) or BB1.
%   [wMu0dtA,dwMu0dtdwA,d2wMu0dtdw2A,d3wMu0dtdw3A,d4wMu0dtdw4A,d5wMu0dtdw5A,d6wMu0dtdw6A,d7wMu0dtdw7A,d8wMu0dtdw8A,SigmawA,dSigmawdwA,d2Sigmawdw2A,Sigma2wA,Sigma3wA] = CalculateDriftAndVolA404B4Drift(ww1,wnStart,NN1,YqCoeff0,Fp1,gamma,dt);

[Muw,Sigmaw,Sigma2w] = CalculateDriftAndVolA404B4DriftO0(ww1,wnStart1,NN1,YqCoeff0,Fp1,gamma,dt);

%            plot(ZZ1(1:NN1),ww1(1:NN1),'b',Z(1:Nn),w(1:Nn),'r')
%            str=input('Look at comparison of interpolated and original w-0');
%            plot(ZZ1(1:NN1),SigmawA(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
%            str=input('Look at comparison of original and interpolated vol coefficient-1');
%plot(ZZ1(1:NN1),cc1(1:NN1),'b',Z(1:Nn),c1(1:Nn),'r')
%str=input('Look at comparison of original and interpolated vol coefficient-1A');
%            plot(ZZ1(1:NN1),wMu0dtA(1:NN1),'b',Z(1:Nn),wMu0dt(1:Nn),'r')
%            str=input('Look at drifts origianl and interpolated-2');
%plot(ZZ1(1:NN1),ZZProb(1:NN1),'b',Z(1:Nn),ZProb(1:Nn),'r')
%str=input('Look at Zprobss -3');
%plot((1:Nn),Z(1:Nn),'r',(1:NN1)-20,ZZ1(1:NN1),'b')
%str=input('Look at Zs -4');

%Below are calculations of CDF and its derivatives to calculate
%the mid point of the grid at time t+1. This mid point should
%lie exactly on median which means CDF there would be exactly
%equal to .5 or CDF=.5.

c1Guess(1:Nn)=sigma0*sqrt(dt);
%Sigmaw_0(wnStart:Nn)=sqrt(dwdZ_0(wnStart:Nn).^2+ c1(wnStart:Nn).^2);
Sigmaw_0(wnStart:Nn)=sqrt(dwdZ_0(wnStart:Nn).^2+ c1Guess(wnStart:Nn).^2);

Bn(wnStart:Nn)=w(wnStart:Nn)+wMu0dt(wnStart:Nn)+(Sigmaw_0(wnStart:Nn)-dwdZ_0(wnStart:Nn)).*Z(wnStart:Nn);
Bn(Bn<=0)=0.0000000000001;
%I will improve the above improvisation in a day.

Bm0(1:NN1)=BB1(1:NN1);%
%      Bm1(1:NN1)=BB1a(1:NN1);%
%      Bm2(1:NN1)=BB1b(1:NN1);%
%Bm0(Bm0<0)=.000000000001;

%     plot((1:Nn),c1(1:Nn),'r');
%     str=input('Look at Bn plot---0');
%     plot((1:Nn),SigmaB(1:Nn),'r',(1:Nn),dBdZ_0(1:Nn),'b');
%     str=input('Look at Bn plot---1');
%     plot((1:Nn),Bn(1:Nn));
%     str=input('Look at Bn plot');

Pn(1:Nn)=0.0;
pn(1:Nn)=0.0;
ESecondHeTotal(1:Nn)=0.0;
%EThirdHeTotal(1:Nn)=0.0;
for mm=wnStart1:NN1

nn1=wnStart;
for nn=wnStart:Nn
if( (Bn(nn)-Bm0(mm)-Muw(mm)) <-6*abs(Sigmaw(mm)))
nn1=nn;
end
end

nn2=Nn;
for nn=Nn:-1:wnStart
if( (Bn(nn)-Bm0(mm)-Muw(mm)) >6*abs(Sigmaw(mm)))
nn2=nn;
end
end
Zt(1:Nn)=0.0;
Pmn(1:Nn)=0.0;
ESecondHe(1:Nn)=0.0;
%Bt(mm1:mm2)=Bn(mm1:mm2)-Bm0;
Zt(nn1:nn2)=(Bn(nn1:nn2)-Bm0(mm)-Muw(mm))/Sigmaw(mm);

Zt(nn1:nn2)=real(Zt(nn1:nn2));
Pm0n(nn1:nn2)=normcdf(Zt(nn1:nn2),0,1);
pm0n(nn1:nn2)=normpdf(Zt(nn1:nn2),0,1);

Pmn(nn1:nn2)=Pm0n(nn1:nn2).*ZZProb(mm);
pmn(nn1:nn2)=pm0n(nn1:nn2).*ZZProb(mm);
%[Pmn,pmn,ESecondHe] = CalculateCDFArraySignIndexDrift(Bn,Nn,wMu0dtA(mm),dwMu0dtdwA(mm),d2wMu0dtdw2A(mm),d3wMu0dtdw3A(mm),d4wMu0dtdw4A(mm),d5wMu0dtdw5A(mm),d6wMu0dtdw6A(mm),d7wMu0dtdw7A(mm),d8wMu0dtdw8A(mm),SigmawA(mm),dSigmawdwA(mm),d2Sigmawdw2A(mm),Sigma2wA(mm),Bm0(mm),Bm1(mm),Bm2(mm),ZZProb(mm),pm0(mm),dpm0(mm),d2pm0(mm),d3pm0(mm),d4pm0(mm),d5pm0(mm),d6pm0(mm),d7pm0(mm));

%ESecondHe(nn1:nn2)=(Sigma2w(mm).*(Zt(nn1:nn2).^2-1)).*ZZProb(mm);
ESecondHe(nn1:nn2)=(Sigma2w(mm).*(Zt(nn1:nn2).^2-1)).*pmn(nn1:nn2);

if(nn1>1)
Pmn(1:nn1-1)=0;
pmn(1:nn1-1)=0;
ESecondHe(1:nn1-1)=0;
%  EThirdHe(1:nn1-1)=0;
end
if (nn2<Nn)
Pmn(nn2+1:Nn)=ZZProb(mm);
pmn(nn2+1:Nn)=0;
ESecondHe(nn2+1:Nn)=0;
%   EThirdHe(nn2+1:Nn)=0;

end

%Pn is total CDF at the point Bn while Pmn is contribution
%to CDF from mth grid cell at previous time point.
Pmn(isnan(Pmn)==1)=0.0;
pmn(isnan(pmn)==1)=0.0;
ESecondHe(isnan(ESecondHe)==1)=0.0;

Pn(wnStart:Nn)=Pn(wnStart:Nn)+Pmn(wnStart:Nn);
pn(wnStart:Nn)=pn(wnStart:Nn)+pmn(wnStart:Nn);
ESecondHeTotal(wnStart:Nn)=ESecondHeTotal(wnStart:Nn)+ESecondHe(wnStart:Nn);

end

%Calculate Znew which is Znew-grid that corresponds to our Guess Bn where CDF is now available.
Znew(wnStart:Nn)=norminv(Pn(wnStart:Nn));
Bn2(wnStart:Nn)=Bn(wnStart:Nn)+ESecondHeTotal(wnStart:Nn)./pn(wnStart:Nn);%
%Bn2(1:Nn)=Bn(1:Nn)+ESecondHeTotal(1:Nn);%./pn(1:Nn);%

%            plot((1:Nn),Bn(1:Nn),'g',(1:Nn),Bn2(1:Nn),'r');
%            str=input('Look at Bn graphs');
%Below we Interpolate values of w at Z-grid (on variable Z)from our
%calculated values of Znew that correspond to CDF at Bn.
%[w] = InterpolateNewGrid(Bn,Znew,wnStart,Nn,dNn,Z)
[w] = InterpolateNewGrid(Bn2,Znew,wnStart,Nn,dNn,Z);

[dwdZ_1,d2wdZ2_1,d3wdZ3_1] = First3Derivatives2ndOrderEqSpacedA(wnStart,Nn,dNn,w,Z);
w(Nn-20+1:Nn)=w(Nn-20)+(1:20).*dwdZ_1(Nn-20)*dNn;

%
%         w1(1:NnMid-1)=w(1:NnMid-1);
%         w1(NnMid)=w(NnMid);
%         w2(1:NnMid-1)=w(2:NnMid);
%         w2(NnMid)=w(NnMid+1);%+w1(Nn)-w1(Nn-1);
%         w(w1(:)>w2(:))=0;%Be careful;might not universally hold;
%      %Change 3:7/25/2020: I have improved zero correction in above.
%         for nn=1:NnMid
%             if(w(nn+1)<w(nn))
%                 w(1:nn+1)=0.0;
%             end
%         end

w(isnan(w)==1)=0.0;

w(w<0)=0.0;
for nn=wnStart:Nn
if(w(nn)<=0)
wnStart=nn+1;
w(1:wnStart-1)=0.0;
end
end

%Below is the grsph of newly calculated value of w.
%       plot((1:Nn),w(1:Nn),'r')
%       str=input('Calculated Value of w');
end
end

%below D's (the names of variables starting with D) are
%change of probability derivatives.

y_w(1:Nn)=0;
y_w(wnStart:Nn) = ((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));
Dfy_w(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));
%Change of variable derivative for densities
end
py_w(1:Nn)=0;
for nn = wnStart:Nn-1
py_w(nn) = (normpdf(Z(nn),0, 1))/abs(Dfy_w(nn));%Origianl coordinates density
end

toc

ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %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

rng(29079137, 'twister')
paths=200000;
YY(1:paths)=x0;  %Original process monte carlo.
Random1(1:paths)=0;
%for tt=1:(Tt-1)+8
for tt=1:TtM
if(tt>Tt1)
%dtM=dt2/8;
end
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;

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);

end

YY(YY<0)=0;
disp('Original process average from monte carlo');
MCMean=sum(YY(:))/paths %origianl coordinates monte carlo average.
MCVar=sum((YY(:)-MCMean).^2)/paths
disp('Original process average from our simulation');
ItoHermiteMean=sum(y_w(wnStart+1:Nn-1).*ZProb(wnStart+1:Nn-1)) %Original process average from coordinates
ItoHermiteVar=sum((y_w(wnStart+1:Nn-1)-ItoHermiteMean).^2.*ZProb(wnStart+1:Nn-1))
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=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(y_w(wnStart+1:Nn-1),py_w(wnStart+1:Nn-1),'r',IndexOutY(1:IndexMaxY),YDensity(1:IndexMaxY),'g');

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));

legend({'New Model Density','Monte Carlo Density'},'Location','northeast')

str=input('red line is density of SDE from Ito-Hermite method, green is monte carlo.');
end
.
One sub function.
.
function [wMu0dt,c1] = CalculateDriftAndVolA404(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt)

yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));

Fp2=Fp1/(1-gamma);

wMu0dt(wnStart:Nn)=(YqCoeff0(1,1,2,1).*yy(wnStart:Nn).^Fp1(1,1,2,1)+ ...
YqCoeff0(1,2,1,1).*yy(wnStart:Nn).^Fp1(1,2,1,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1))*dt + ...
(YqCoeff0(1,1,3,1).*yy(wnStart:Nn).^Fp1(1,1,3,1)+ ...
YqCoeff0(1,2,2,1).*yy(wnStart:Nn).^Fp1(1,2,2,1)+ ...
YqCoeff0(2,1,2,1).*yy(wnStart:Nn).^Fp1(2,1,2,1)+ ...
YqCoeff0(1,3,1,1).*yy(wnStart:Nn).^Fp1(1,3,1,1)+ ...
YqCoeff0(2,2,1,1).*yy(wnStart:Nn).^Fp1(2,2,1,1)+ ...
YqCoeff0(3,1,1,1).*yy(wnStart:Nn).^Fp1(3,1,1,1))*dt^2 + ...
(YqCoeff0(1,1,4,1).*yy(wnStart:Nn).^Fp1(1,1,4,1)+ ...
YqCoeff0(1,2,3,1).*yy(wnStart:Nn).^Fp1(1,2,3,1)+ ...
YqCoeff0(2,1,3,1).*yy(wnStart:Nn).^Fp1(2,1,3,1)+ ...
YqCoeff0(1,3,2,1).*yy(wnStart:Nn).^Fp1(1,3,2,1)+ ...
YqCoeff0(2,2,2,1).*yy(wnStart:Nn).^Fp1(2,2,2,1)+ ...
YqCoeff0(3,1,2,1).*yy(wnStart:Nn).^Fp1(3,1,2,1)+ ...
YqCoeff0(1,4,1,1).*yy(wnStart:Nn).^Fp1(1,4,1,1)+ ...
YqCoeff0(2,3,1,1).*yy(wnStart:Nn).^Fp1(2,3,1,1)+ ...
YqCoeff0(3,2,1,1).*yy(wnStart:Nn).^Fp1(3,2,1,1)+ ...
YqCoeff0(4,1,1,1).*yy(wnStart:Nn).^Fp1(4,1,1,1))*dt^3+ ...
(YqCoeff0(1,1,5,1).*yy(wnStart:Nn).^Fp1(1,1,5,1)+ ...
YqCoeff0(1,2,4,1).*yy(wnStart:Nn).^Fp1(1,2,4,1)+ ...
YqCoeff0(2,1,4,1).*yy(wnStart:Nn).^Fp1(2,1,4,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1)+ ...
YqCoeff0(2,2,3,1).*yy(wnStart:Nn).^Fp1(2,2,3,1)+ ...
YqCoeff0(3,1,3,1).*yy(wnStart:Nn).^Fp1(3,1,3,1)+ ...
YqCoeff0(1,4,2,1).*yy(wnStart:Nn).^Fp1(1,4,2,1)+ ...
YqCoeff0(2,3,2,1).*yy(wnStart:Nn).^Fp1(2,3,2,1)+ ...
YqCoeff0(3,2,2,1).*yy(wnStart:Nn).^Fp1(3,2,2,1)+ ...
YqCoeff0(4,1,2,1).*yy(wnStart:Nn).^Fp1(4,1,2,1)+ ...
YqCoeff0(1,5,1,1).*yy(wnStart:Nn).^Fp1(1,5,1,1)+ ...
YqCoeff0(2,4,1,1).*yy(wnStart:Nn).^Fp1(2,4,1,1)+ ...
YqCoeff0(3,3,1,1).*yy(wnStart:Nn).^Fp1(3,3,1,1)+  ...
YqCoeff0(4,2,1,1).*yy(wnStart:Nn).^Fp1(4,2,1,1)+ ...
YqCoeff0(5,1,1,1).*yy(wnStart:Nn).^Fp1(5,1,1,1))*dt^4;

c1(wnStart:Nn)=((YqCoeff0(1,1,1,2).*yy(wnStart:Nn).^Fp1(1,1,1,2).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*yy(wnStart:Nn).^Fp1(1,1,2,2)+YqCoeff0(1,2,1,2).*yy(wnStart:Nn).^Fp1(1,2,1,2)+ ...
YqCoeff0(2,1,1,2).*yy(wnStart:Nn).^Fp1(2,1,1,2)).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*yy(wnStart:Nn).^Fp1(1,1,3,2)+YqCoeff0(1,2,2,2).*yy(wnStart:Nn).^Fp1(1,2,2,2)+ ...
YqCoeff0(2,1,2,2).*yy(wnStart:Nn).^Fp1(2,1,2,2)+YqCoeff0(1,3,1,2).*yy(wnStart:Nn).^Fp1(1,3,1,2)+ ...
YqCoeff0(2,2,1,2).*yy(wnStart:Nn).^Fp1(2,2,1,2)+YqCoeff0(3,1,1,2).*yy(wnStart:Nn).^Fp1(3,1,1,2)).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*yy(wnStart:Nn).^Fp1(1,1,4,2)+YqCoeff0(1,2,3,2).*yy(wnStart:Nn).^Fp1(1,2,3,2)+ ...
YqCoeff0(2,1,3,2).*yy(wnStart:Nn).^Fp1(2,1,3,2)+YqCoeff0(1,3,2,2).*yy(wnStart:Nn).^Fp1(1,3,2,2)+ ...
YqCoeff0(2,2,2,2).*yy(wnStart:Nn).^Fp1(2,2,2,2)+ YqCoeff0(3,1,2,2).*yy(wnStart:Nn).^Fp1(3,1,2,2)+ ...
YqCoeff0(1,4,1,2).*yy(wnStart:Nn).^Fp1(1,4,1,2)+YqCoeff0(2,3,1,2).*yy(wnStart:Nn).^Fp1(2,3,1,2)+ ...
YqCoeff0(3,2,1,2).*yy(wnStart:Nn).^Fp1(3,2,1,2)+YqCoeff0(4,1,1,2).*yy(wnStart:Nn).^Fp1(4,1,1,2)).*dt^3.5);

end
.
.
Second Sub function.
.
function [wMu0dt,c1,c2] = CalculateDriftAndVolA404B4DriftO0(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt)

yy(wnStart:Nn)=((1-gamma)*w(wnStart:Nn)).^(1/(1-gamma));

Fp2=Fp1/(1-gamma);

wMu0dt(wnStart:Nn)=(YqCoeff0(1,1,2,1).*yy(wnStart:Nn).^Fp1(1,1,2,1)+ ...
YqCoeff0(1,2,1,1).*yy(wnStart:Nn).^Fp1(1,2,1,1)+ ...
YqCoeff0(2,1,1,1).*yy(wnStart:Nn).^Fp1(2,1,1,1))*dt + ...
(YqCoeff0(1,1,3,1).*yy(wnStart:Nn).^Fp1(1,1,3,1)+ ...
YqCoeff0(1,2,2,1).*yy(wnStart:Nn).^Fp1(1,2,2,1)+ ...
YqCoeff0(2,1,2,1).*yy(wnStart:Nn).^Fp1(2,1,2,1)+ ...
YqCoeff0(1,3,1,1).*yy(wnStart:Nn).^Fp1(1,3,1,1)+ ...
YqCoeff0(2,2,1,1).*yy(wnStart:Nn).^Fp1(2,2,1,1)+ ...
YqCoeff0(3,1,1,1).*yy(wnStart:Nn).^Fp1(3,1,1,1))*dt^2 + ...
(YqCoeff0(1,1,4,1).*yy(wnStart:Nn).^Fp1(1,1,4,1)+ ...
YqCoeff0(1,2,3,1).*yy(wnStart:Nn).^Fp1(1,2,3,1)+ ...
YqCoeff0(2,1,3,1).*yy(wnStart:Nn).^Fp1(2,1,3,1)+ ...
YqCoeff0(1,3,2,1).*yy(wnStart:Nn).^Fp1(1,3,2,1)+ ...
YqCoeff0(2,2,2,1).*yy(wnStart:Nn).^Fp1(2,2,2,1)+ ...
YqCoeff0(3,1,2,1).*yy(wnStart:Nn).^Fp1(3,1,2,1)+ ...
YqCoeff0(1,4,1,1).*yy(wnStart:Nn).^Fp1(1,4,1,1)+ ...
YqCoeff0(2,3,1,1).*yy(wnStart:Nn).^Fp1(2,3,1,1)+ ...
YqCoeff0(3,2,1,1).*yy(wnStart:Nn).^Fp1(3,2,1,1)+ ...
YqCoeff0(4,1,1,1).*yy(wnStart:Nn).^Fp1(4,1,1,1))*dt^3+ ...
(YqCoeff0(1,1,5,1).*yy(wnStart:Nn).^Fp1(1,1,5,1)+ ...
YqCoeff0(1,2,4,1).*yy(wnStart:Nn).^Fp1(1,2,4,1)+ ...
YqCoeff0(2,1,4,1).*yy(wnStart:Nn).^Fp1(2,1,4,1)+ ...
YqCoeff0(2,2,3,1).*yy(wnStart:Nn).^Fp1(2,2,3,1)+ ...
YqCoeff0(3,1,3,1).*yy(wnStart:Nn).^Fp1(3,1,3,1)+ ...
YqCoeff0(1,4,2,1).*yy(wnStart:Nn).^Fp1(1,4,2,1)+ ...
YqCoeff0(2,3,2,1).*yy(wnStart:Nn).^Fp1(2,3,2,1)+ ...
YqCoeff0(3,2,2,1).*yy(wnStart:Nn).^Fp1(3,2,2,1)+ ...
YqCoeff0(4,1,2,1).*yy(wnStart:Nn).^Fp1(4,1,2,1)+ ...
YqCoeff0(1,5,1,1).*yy(wnStart:Nn).^Fp1(1,5,1,1)+ ...
YqCoeff0(2,4,1,1).*yy(wnStart:Nn).^Fp1(2,4,1,1)+ ...
YqCoeff0(3,3,1,1).*yy(wnStart:Nn).^Fp1(3,3,1,1)+  ...
YqCoeff0(4,2,1,1).*yy(wnStart:Nn).^Fp1(4,2,1,1)+ ...
YqCoeff0(5,1,1,1).*yy(wnStart:Nn).^Fp1(5,1,1,1))*dt^4;

c1(wnStart:Nn)=((YqCoeff0(1,1,1,2).*yy(wnStart:Nn).^Fp1(1,1,1,2).*sqrt(dt))+ ...
(YqCoeff0(1,1,2,2).*yy(wnStart:Nn).^Fp1(1,1,2,2)+YqCoeff0(1,2,1,2).*yy(wnStart:Nn).^Fp1(1,2,1,2)+ ...
YqCoeff0(2,1,1,2).*yy(wnStart:Nn).^Fp1(2,1,1,2)).*dt^1.5+ ...
(YqCoeff0(1,1,3,2).*yy(wnStart:Nn).^Fp1(1,1,3,2)+YqCoeff0(1,2,2,2).*yy(wnStart:Nn).^Fp1(1,2,2,2)+ ...
YqCoeff0(2,1,2,2).*yy(wnStart:Nn).^Fp1(2,1,2,2)+YqCoeff0(1,3,1,2).*yy(wnStart:Nn).^Fp1(1,3,1,2)+ ...
YqCoeff0(2,2,1,2).*yy(wnStart:Nn).^Fp1(2,2,1,2)+YqCoeff0(3,1,1,2).*yy(wnStart:Nn).^Fp1(3,1,1,2)).*dt^2.5+ ...
(YqCoeff0(1,1,4,2).*yy(wnStart:Nn).^Fp1(1,1,4,2)+YqCoeff0(1,2,3,2).*yy(wnStart:Nn).^Fp1(1,2,3,2)+ ...
YqCoeff0(2,1,3,2).*yy(wnStart:Nn).^Fp1(2,1,3,2)+YqCoeff0(1,3,2,2).*yy(wnStart:Nn).^Fp1(1,3,2,2)+ ...
YqCoeff0(2,2,2,2).*yy(wnStart:Nn).^Fp1(2,2,2,2)+ YqCoeff0(3,1,2,2).*yy(wnStart:Nn).^Fp1(3,1,2,2)+ ...
YqCoeff0(1,4,1,2).*yy(wnStart:Nn).^Fp1(1,4,1,2)+YqCoeff0(2,3,1,2).*yy(wnStart:Nn).^Fp1(2,3,1,2)+ ...
YqCoeff0(3,2,1,2).*yy(wnStart:Nn).^Fp1(3,2,1,2)+YqCoeff0(4,1,1,2).*yy(wnStart:Nn).^Fp1(4,1,1,2)).*dt^3.5);

c2(wnStart:Nn)=((YqCoeff0(1,1,1,3).*yy(wnStart:Nn).^Fp1(1,1,1,3) *dt) + ...
(YqCoeff0(1,1,2,3).*yy(wnStart:Nn).^Fp1(1,1,2,3)+ ...
YqCoeff0(1,2,1,3).*yy(wnStart:Nn).^Fp1(1,2,1,3)+ ...
YqCoeff0(2,1,1,3).*yy(wnStart:Nn).^Fp1(2,1,1,3)).*dt^2+ ...
(YqCoeff0(1,1,3,3).*yy(wnStart:Nn).^Fp1(1,1,3,3)+ ...
YqCoeff0(1,2,2,3).*yy(wnStart:Nn).^Fp1(1,2,2,3)+ ...
YqCoeff0(2,1,2,3).*yy(wnStart:Nn).^Fp1(2,1,2,3) + ...
YqCoeff0(1,3,1,3).*yy(wnStart:Nn).^Fp1(1,3,1,3)+ ...
YqCoeff0(2,2,1,3).*yy(wnStart:Nn).^Fp1(2,2,1,3)+ ...
YqCoeff0(3,1,1,3).*yy(wnStart:Nn).^Fp1(3,1,1,3)).*dt^3);

c3(wnStart:Nn)=((YqCoeff0(1,1,1,4).*yy(wnStart:Nn).^Fp1(1,1,1,4)*dt^1.5 )+ ...
(YqCoeff0(1,1,2,4).*yy(wnStart:Nn).^Fp1(1,1,2,4)+ ...
YqCoeff0(1,2,1,4).*yy(wnStart:Nn).^Fp1(1,2,1,4)+ ...
YqCoeff0(2,1,1,4).*yy(wnStart:Nn).^Fp1(2,1,1,4))*dt^2.5);

end
.
.
Any Other sub functions are the same as old.
.
Here is the output of the program.
ItoHermiteMean =
0.117442814511675
true Mean only applicable to standard SV mean reverting type models otherwise disregard
TrueMean =
0.117254682646003
Original process average from monte carlo
MCMean =
0.117364976282220
MCVar =
0.013274708996826
Original process average from our simulation
ItoHermiteMean =
0.117442814511675
ItoHermiteVar =
0.013177477018960
true Mean only applicble to standard SV mean reverting type models otherwise disregard
TrueMean =
0.117254682646003
IndexMax =
641
red line is density of SDE from Ito-Hermite method, green is monte carlo.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Here are some of the graphs that I made with the above program without any elaborate Taylor expansion.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Friends, I am working on tackling stochastic differential equations that go to zero. As a start, I am getting encouraging results. But I noticed when a density has inaccuracy in left tail, there also arises some inaccuracy in right tail since we are working with CDF. I am posting the following graph where density goes to zero but main body of the density is well-captured by the new method however there is some inaccuracy in far right tail after +3 SDs that throw the mean off even though body of the density is very well captured by the method. But I am working on it. Here is the graph.

It may take a few days but I will be posting new programs here on the forum.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Friends, I have been able to tackle the particular case I was working on with density of SDE going to zero perfectly well. Now both extremes of the density when density of SDE goes to zero seem to be well captured by the new method. Only thing I did was to turn the drift to zero at grid points that were very close to zero boundary where drift was seemingly becoming too large or unstable. Here is the new graph with both the body of density and its mean perfectly captured. In the density, 2.5 SDs to the left of zero from -5 to -2.5 SDs have their mass absorbed in zero.

I will now work with cases where even more of the density goes to zero. Let us see how that goes.

Amin
Topic Author
Posts: 2728
Joined: July 14th, 2002, 3:00 am

### Re: Breakthrough in the theory of stochastic differential equations and their simulation

Sorry, double.
Last edited by Amin on April 20th, 2021, 12:59 pm, edited 1 time in total.