Serving the Quantitative Finance Community

 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

March 27th, 2021, 2:00 am

It continues to seem that there is no plan to end my detention anytime soon.
Yesterday, a few hours after writing of my previous post, my sister called and emphatically told me that she will send my mother the next day(Saturday) and the hospital will be asked to discharge me. Probably my sister wanted me to declare false victory here on this forum because when I called her in the evening she was again making all sort of excuses about ending my detention. Let us see what happens today.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

March 27th, 2021, 2:57 am

And of course, they bitterly want to add daily oral medication to my drug regimen. Please protest.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

March 30th, 2021, 12:16 am

Friends, Since my detention ended, I have been working on generalizing the previous program to variable drift and variable volatility. I have been able to match all integrals for variable drift and constant volatility but there are still some integrals yet to be matched in the most general case of variable volatility. I am planning to go back to Lahore today or tomorrow and as soon as I get there, I will start working in full steam to solve the problem.
Today, I will be visiting the psychiatrist again and I hope my meeting goes well. If we are free from the psychiatrist early, I might drive to Lahore with mother today otherwise I will go to Lahore with her tomorrow. Meeting with the psychiatrist is crucial. 
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

March 30th, 2021, 4:38 pm

For SDE's with variable drift and variable volatility, I have re-done some analytics and the new solution for integral of CDF is simpler, faster and more elegant but I want to test it on computer and match analytic integrals with numerical versions before posting it on the forum. Tomorrow, I will be driving from Islamabad to Lahore so it will possibly take another day after tomorrow. 
My meeting with the psychiatrist today went well and he did not prescribe any more antipsychotics which was a relief for the time being.
In Lahore, I look forward to doing interesting work and will continue to share it with friends.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 2nd, 2021, 4:40 pm

I had to redo the analytics again for SDEs with variable drift and volatility. Finally I decided to expand the unweighted transition CDF integral as a Taylor series without any closed form integrals. For quite reasonably large grid cell width (.25) and also reasonably large time horizon (.25) and very high volatility and a large drift, with order of expansion to order four, I am getting an accuracy of 1 X 10-6. I am going to take the order to sixth order at least and hope that accuracy improves  to somewhere between 1 X 10-7 to 1X 10-8. I will be extending the order tomorrow.
For normal density, I decided to not normalize it and used two parameters Zt and Sigma (vol). For the unweighted CDF integral calculations, I took derivatives of CDF with respect to Zt and Sigma and also their cross derivatives and then converted them into derivatives with respect to Bm. 
I hope that a fully working program would be ready in less than a week. I am very reasonably sure that there should be no unforeseen problems. I hope that friends would like this new program.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 3rd, 2021, 8:13 am

Friends, here is the update about progress on the program. As I had told friends that I had a low accuracy working Taylor expansion in which I had converted derivatives of Zt and sigma and their cross derivatives into derivatives of Bm. I was previously working with a standardized normal form and it did not seem to work well. But when I had good numbers from an alternate two parameter method, I compared different terms and I was able to find some errors in the standardized normal expansion that I had made. When I corrected for errors, I found that standardized normal expansion had more accuracy and fourth order expansion had accuracy of 2 X 10-7 as opposed to 1 X 10-6 with the other alternate method that I mentioned yesterday. And when I expanded (the standardized normal method) to sixth order, I was able to get an accuracy of 2 X 10-10 for the CDF integral part which seems pretty good now. I hope that I will complete the whole program in next three days and then I will take an extra day or two to make any more changes and to write comments and then post the program here. There does not seem to be any more technical problem and I am sure things would go well. I will keep friends posted about progress on my program.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 4th, 2021, 10:20 am

I am posting the program that I used to check the accuracy of my Taylor expansion of unweighted Integral of CDF of a normal that with variable vol and variable drift (representing values for an SDE) from an entire grid cell to a particular point. You can safely run this function.
I am posting two other functions, the first of which is to be used for expansion of (actual meaning weighted with probability of original cell) CDF and this is going to be the main function. The second function calculates first six derivatives of drift and volatility in bessel coordinates(yet - but straightforward to extend to original coordinates- I will also be posting a program for original coordinates later). Both of these last two functions are yet untested and I am posting them for friends only because I decided to post this material if things go wrong again in some worst case scenario (I get drugged/detained etc). I will be posting perfectly working and tested version in another 2-3 days as promised if things go well.
You can try the first function used to check accuracy of the integral. I am posting it with the same values that had a difference of 2 X 10-10 that I mentioned but for other values/parameters, I have seen accuracy decrease sometimes to 5 X 10-9. For difficult scenarios, we could decrease the grid cell size to get better results. I was using a subdivision width of .25  with the parameters I will post after the program. I believe, working with Bessel coordinates even for variable volatility(that remains variable even after lamperti transform) will usually have better results but of course many times it would be inconvenient to use bessel/lamperti form and we may want to stick to the original coordinates(which should also work reasonably well). 
.
function [] = CheckCDFfromSubdivDriftVol04(Bn,Bm0,dB)
%This function integrates gaussian transition CDF(to target point Bn) over 
%a subdivision with centre Bm0 and symmetric boundaries Bm1=Bm0-.5*dB; and 
%Bm2=Bm0+.5*dB;
%And compares it with integration by parts expression

delta_t=.25;
sigma0=1.5*sqrt(delta_t);
mu1=.50*delta_t;
beta=1.25;
gamma=0.650;%1.65;
%variable vol is defined as Sigmaw=Sigma0*Bm.^gamma;
%variable drift is defined as Muw=mu1*Bm.^beta;
%real drift and vol and their derivatives would be in terms of a sum of large number of terms
%that come out of Ito-hermite expansion.


Bm1=Bm0-.5*dB;
Bm2=Bm0+.5*dB;
dBm=(Bm2-Bm1)/100000;
Integral0=0;
for n=1:100001
    if(n==1)
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw);
        dSigmawdBm=sigma0*gamma.*BmI.^(gamma-1);
        dMuwdBm=mu1*beta.*BmI.^(beta-1);
        dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((-BmI + Bn - Muw)* dSigmawdBm)/Sigmaw^2;
        Integral0=Integral0+.5*normcdf(Zt,0,Sigmaw)*dBm;
    end
    if((n>1)&&(n<100001))
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw);
        Integral0=Integral0+normcdf(Zt,0,Sigmaw)*dBm;
    end
    if((n==100001))
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw);
        Integral0=Integral0+.5*normcdf(Zt,0,Sigmaw)*dBm;
    end
end
        
%Analytic Integration of CDF over a subdivision using by parts integration 
%as we solved for terms in Group G2.
Muw=mu1*Bm0.^beta;
Sigmaw=sigma0*Bm0.^gamma;


Zt=(Bn-Bm0-Muw)/Sigmaw;

Zt2=Zt^2;
Zt3=Zt^3;
Zt4=Zt^4;
Zt5=Zt^5;
Zt6=Zt^6;


Pm0n=normcdf(Zt,0,1);
dPm0ndZt=normpdf(Zt,0,1);
d2Pm0ndZt2=-dPm0ndZt*(Zt);
d3Pm0ndZt3=dPm0ndZt*(Zt2-1);
d4Pm0ndZt4=-dPm0ndZt.*(Zt3-3*Zt);
d5Pm0ndZt5=dPm0ndZt.*(Zt4-6*Zt2+3);
d6Pm0ndZt6=-dPm0ndZt.*(Zt5-10*Zt3+15*Zt);




Zt=(-Bm0 + Bn - Muw);





Sigmaw=sigma0.*Bm0.^(gamma);
dSigmawdBm=sigma0*gamma.*Bm0.^(gamma-1);
d2SigmawdBm2=sigma0*gamma.*(gamma-1).*Bm0.^(gamma-2);
d3SigmawdBm3=sigma0*gamma.*(gamma-1).*(gamma-2).*Bm0.^(gamma-3);
d4SigmawdBm4=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*Bm0.^(gamma-4);
d5SigmawdBm5=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*Bm0.^(gamma-5);
d6SigmawdBm6=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*Bm0.^(gamma-6);


Muw=mu1*Bm0.^(beta);
dMuwdBm=mu1*beta.*Bm0.^(beta-1);
d2MuwdBm2=mu1*beta.*(beta-1).*Bm0.^(beta-2);
d3MuwdBm3=mu1*beta.*(beta-1).*(beta-2).*Bm0.^(beta-3);
d4MuwdBm4=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*Bm0.^(beta-4);
d5MuwdBm5=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*Bm0.^(beta-5);
d6MuwdBm6=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*Bm0.^(beta-6);





Zt=(-Bm0 + Bn - Muw)/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-(Zt.* d3SigmawdBm3)/Sigmaw;
 
  d4ZtdBm4=-((6.* (-1-dMuwdBm).* dSigmawdBm^3)/Sigmaw^4)+(6.* Zt.* dSigmawdBm^4)/Sigmaw^4- ...
      (6.* dSigmawdBm.^3 .*dZtdBm)/Sigmaw^3-(6 .*dSigmawdBm.^2 .* d2MuwdBm2)/Sigmaw^3+ ...
      (6.* (-1-dMuwdBm).* dSigmawdBm .*d2SigmawdBm2)/Sigmaw^3-(12.* Zt.* dSigmawdBm^2 .*d2SigmawdBm2)/Sigmaw^3+  ...
      (9.* dSigmawdBm .* dZtdBm .* d2SigmawdBm2)/Sigmaw^2+(3  .*d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^2+ ...
      (3.* Zt.* d2SigmawdBm2^2)/Sigmaw^2+(3 .* dSigmawdBm^2 .* d2ZtdBm2)/Sigmaw^2- ...
      (3.* d2SigmawdBm2 .* d2ZtdBm2)/Sigmaw+(3 .* dSigmawdBm .* d3MuwdBm3)/Sigmaw.^2- ...
      ((-1-dMuwdBm) .* d3SigmawdBm3)/Sigmaw.^2+(4.* Zt .* dSigmawdBm .* d3SigmawdBm3)/Sigmaw.^2- ...
      (3.* dZtdBm .*d3SigmawdBm3)/Sigmaw-(dSigmawdBm.* d3ZtdBm3)/Sigmaw- ...
      d4MuwdBm4/Sigmaw-(Zt.* d4SigmawdBm4)/Sigmaw;
 
 
  
  d5ZtdBm5=(24.* (-1-dMuwdBm).* dSigmawdBm.^4)/Sigmaw^5-(24.* Zt.* dSigmawdBm^5)/Sigmaw.^5+ ...
      (24 .*dSigmawdBm^4.* dZtdBm)/Sigmaw^4+(24.* dSigmawdBm.^3 .* d2MuwdBm2)/Sigmaw.^4-(36 .* (-1-dMuwdBm) .* dSigmawdBm^2 .* ...
      d2SigmawdBm2)/Sigmaw^4+(60.* Zt.* dSigmawdBm.^3.* d2SigmawdBm2)/Sigmaw^4-(48 .*dSigmawdBm^2.* dZtdBm .* d2SigmawdBm2)/Sigmaw^3- ...
      (24 .* dSigmawdBm .* d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^3+(6.* (-1-dMuwdBm).* d2SigmawdBm2.^2)/Sigmaw^3- ...
      (30.* Zt .* dSigmawdBm.* d2SigmawdBm2.^2)/Sigmaw^3+(12.* dZtdBm.* d2SigmawdBm2.^2)/Sigmaw^2- ...
      (12.* dSigmawdBm.^3 .* d2ZtdBm2)/Sigmaw^3+(18.8 .*dSigmawdBm .* d2SigmawdBm2 .* d2ZtdBm2)/Sigmaw^2- ...
      (12.* dSigmawdBm.^2 .* d3MuwdBm3)/Sigmaw^3+(6 .*d2SigmawdBm2 .*d3MuwdBm3)/Sigmaw^2+ ...
      (8.* (-1-dMuwdBm).* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-(20 .* Zt.* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^3+ ...
      (16.* dSigmawdBm.* dZtdBm.* d3SigmawdBm3)/Sigmaw^2+(4.* d2MuwdBm2  .*d3SigmawdBm3)/Sigmaw^2+ ...
      (10.* Zt.* d2SigmawdBm2  .*d3SigmawdBm3)/Sigmaw^2-(6.* d2ZtdBm2  .*d3SigmawdBm3)/Sigmaw+ ...
      (4 .*dSigmawdBm^2.* d3ZtdBm3)/Sigmaw^2-(4.* d2SigmawdBm2 .* d3ZtdBm3)/Sigmaw+ ...
      (4.* dSigmawdBm .*d4MuwdBm4)/Sigmaw^2-((-1-dMuwdBm) .* d4SigmawdBm4)/Sigmaw^2+ ...
      (5.* Zt .* dSigmawdBm .* d4SigmawdBm4)/Sigmaw^2-(4 .* dZtdBm.* d4SigmawdBm4)/Sigmaw- ...
      (dSigmawdBm.* d4ZtdBm4)/Sigmaw-d5MuwdBm5/Sigmaw-(Zt .* d5SigmawdBm5)/Sigmaw;
  
 
  
  
  d6ZtdBm6=-((120.* (-1-dMuwdBm) .* dSigmawdBm.^5)/Sigmaw^6)+(120.* Zt.* dSigmawdBm.^6)/Sigmaw^6- ...
      (120.* dSigmawdBm.^5.* dZtdBm)/Sigmaw^5-(120.* dSigmawdBm.^4 .* d2MuwdBm2)/Sigmaw^5+ ...\
      (240 .*(-1-dMuwdBm).* dSigmawdBm.^3 .* d2SigmawdBm2)/Sigmaw^5-(360 .* Zt.* dSigmawdBm^4 .* d2SigmawdBm2)/Sigmaw^5+ ...
      (300.* dSigmawdBm^3 .*dZtdBm.* d2SigmawdBm2)/Sigmaw^4+(180 .* dSigmawdBm.^2 .* d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^4- ...
      (90.* (-1-dMuwdBm).* dSigmawdBm .* d2SigmawdBm2.^2)/Sigmaw^4+(270.* Zt .* dSigmawdBm.^2 .* d2SigmawdBm2.^2)/Sigmaw^4- ...
      (150.* dSigmawdBm .* dZtdBm .* d2SigmawdBm2.^2)/Sigmaw^3-(30 .* d2MuwdBm2 .* d2SigmawdBm2^2)/Sigmaw^3- ...
      (30 .* Zt.* d2SigmawdBm2.^3)/Sigmaw^3+(60 .* dSigmawdBm.^4 .* d2ZtdBm2)/Sigmaw^4- ...
      (120 .*dSigmawdBm.^2  .*d2SigmawdBm2 .*d2ZtdBm2)/Sigmaw^3+(30 .* d2SigmawdBm2.^2 .* d2ZtdBm2)/Sigmaw^2+ ...
      (60 .*dSigmawdBm.^3.* d3MuwdBm3)/Sigmaw^4-(60 .* dSigmawdBm .* d2SigmawdBm2 .*d3MuwdBm3)/Sigmaw^3- ...
      (60.* (-1-dMuwdBm) .* dSigmawdBm.^2 .* d3SigmawdBm3)/Sigmaw^4+(120.* Zt.* dSigmawdBm.^3 .*d3SigmawdBm3)/Sigmaw^4- ...
      (100.* dSigmawdBm^2 .*dZtdBm.* d3SigmawdBm3)/Sigmaw^3-(40.* dSigmawdBm.* d2MuwdBm2 .*d3SigmawdBm3)/Sigmaw^3+ ...
      (20 .*(-1-dMuwdBm) .* d2SigmawdBm2 .* d3SigmawdBm3)/Sigmaw^3-(120.* Zt.* dSigmawdBm .* d2SigmawdBm2 .* d3SigmawdBm3)/Sigmaw^3+ ...
      (50 .*dZtdBm .* d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^2+(40.* dSigmawdBm .*d2ZtdBm2 .*d3SigmawdBm3)/Sigmaw^2+ ...
      (10.* d3MuwdBm3 .* d3SigmawdBm3)/Sigmaw^2+(10.* Zt.* d3SigmawdBm3.^2)/Sigmaw^2- ...
      (20.* dSigmawdBm.^3 .* d3ZtdBm3)/Sigmaw^3+(30 .*dSigmawdBm.* d2SigmawdBm2 .* d3ZtdBm3)/Sigmaw^2- ...
      (10.* d3SigmawdBm3 .* d3ZtdBm3)/Sigmaw-(20 .* dSigmawdBm.^2 .*d4MuwdBm4)/Sigmaw^3+(10 .*d2SigmawdBm2.* d4MuwdBm4)/Sigmaw^2+ ...
      (10.* (-1-dMuwdBm).* dSigmawdBm .* d4SigmawdBm4)/Sigmaw ^3-(30 .* Zt.* dSigmawdBm.^2 .* d4SigmawdBm4)/Sigmaw^3+ ...
      (25.* dSigmawdBm .* dZtdBm .*d4SigmawdBm4)/Sigmaw ^2+(5.* d2MuwdBm2 .* d4SigmawdBm4)/Sigmaw^2+ ...
      (15.* Zt.* d2SigmawdBm2 .* d4SigmawdBm4)/Sigmaw^2-(10.* d2ZtdBm2 .* d4SigmawdBm4)/Sigmaw+ ...
      (5.* dSigmawdBm.^2 .* d4ZtdBm4)/Sigmaw.^2-(5 .*d2SigmawdBm2 .* d4ZtdBm4)/Sigmaw+ ...
      (5.* dSigmawdBm .* d5MuwdBm5)/Sigmaw^2-((-1-dMuwdBm).* d5SigmawdBm5)/Sigmaw^2+(6 .* Zt.* dSigmawdBm.* d5SigmawdBm5)/Sigmaw^2- ...
      (5.* dZtdBm.* d5SigmawdBm5)/Sigmaw-(dSigmawdBm .*d5ZtdBm5)/Sigmaw-d6MuwdBm6/Sigmaw-(Zt.* d6SigmawdBm6)/Sigmaw;
dPm0ndBm=dPm0ndZt.*dZtdBm;
d2Pm0ndBm2=d2Pm0ndZt2.*dZtdBm.^2+dPm0ndZt.*d2ZtdBm2;
d3Pm0ndBm3=d3Pm0ndZt3.*dZtdBm.^3+3*d2Pm0ndZt2.*dZtdBm.*d2ZtdBm2+dPm0ndZt.*d3ZtdBm3;
d4Pm0ndBm4=d4Pm0ndZt4.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=1/7* ((Bm2 - Bm0)^7 - (Bm1 - Bm0)^7)/720;




IntegralAnalytic=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
    +(d2Pm0ndBm2) *dBm2 + ...  %This line and lines below are G3 in explanation.
    +(d3Pm0ndBm3) *dBm3 + ...
    +(d4Pm0ndBm4)*dBm4+ ...
    +(d5Pm0ndBm5)*dBm5+ ...
    +(d6Pm0ndBm6)*dBm6;


Integral0
IntegralAnalytic

end

.
When you run the above program with parameters
CheckCDFfromSubdivDriftVol04(1,.75,.25)
you will get the result
>> CheckCDFfromSubdivDriftVol04(1,.75,.25)
Integral0 =
   0.151414529218717
IntegralAnalytic =
   0.151414529000360


Symmetric boundaries in above example are .625 and .875 around the center of .75.
But in many other cases accuracy slightly decreases but is still of order of 10-9.

Here is the main function to calculate the CDF that I will be using. As I earlier mentioned, this is still untested and posted only for reference(in case anything goes wrong, you would easily be able to play with it and make it run if you understand earlier parts of my thread).
.
function [Pmn] = CalculateCDFArraySignIndexDriftVol(Bn,Nn,Muw,dMuwdBm,d2MuwdBm2,d3MuwdBm3,d4MuwdBm4,d5MuwdBm5,d6MuwdBm6,Sigmaw,dSigmawdBm,d2SigmawdBm2,d3SigmawdBm3,d4SigmawdBm4,d5SigmawdBm5,d6SigmawdBm6,Bm0,Bm1,Bm2,dPm,pm0,dpm0,d2pm0,d3pm0,d4pm0,d5pm0)

%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*sigma0)
        nn1=nn;
    end
end

nn2=Nn;
for nn=Nn:-1:1
    if( (Bn(nn)-Bm0) >7*sigma0)
        nn2=nn;
    end
end


%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)=Zt05(nn1:nn2).*Zt(nn1:nn2);




Pm0n(nn1:nn2)=normcdf(Zt(nn1:nn2),0,1);
dPm0ndZt(nn1:nn2)=normpdf(Zt(nn1:nn2),0,1);
pm0n(nn1:nn2)=normpdf(Zt(nn1:nn2),0,1);
d2Pm0ndZt2(nn1:nn2)=-pm0n(nn1:nn2)*(Zt(nn1:nn2));
d3Pm0ndZt3(nn1:nn2)=pm0n(nn1:nn2)*(Zt2(nn1:nn2)-1);
d4Pm0ndZt4(nn1:nn2)=-pm0n(nn1:nn2)*(Zt3(nn1:nn2)-3*Zt(nn1:nn2));
d5Pm0ndZt5(nn1:nn2)=pm0n(nn1:nn2)*(Zt4(nn1:nn2)-6*Zt2(nn1:nn2)+3);
d6Pm0ndZt6(nn1:nn2)=-pm0n(nn1:nn2)*(Zt5(nn1:nn2)-10*Zt3(nn1:nn2)+15*Zt(nn1:nn2));

    
dZtdBm(nn1:nn2)=(-1 - dMuwdBm )/ Sigmaw - ((Zt(nn1:nn2))* dSigmawdBm)/Sigmaw;
 
 d2ZtdBm2(nn1:nn2)=-(((-1-dMuwdBm).* dSigmawdBm)/Sigmaw^2)+(Zt(nn1:nn2).* dSigmawdBm^2)/Sigmaw^2-( dSigmawdBm.* dZtdBm(nn1:nn2))/Sigmaw- ...
     d2MuwdBm2/Sigmaw-(Zt(nn1:nn2) .* d2SigmawdBm2)/Sigmaw;
 
  d3ZtdBm3(nn1:nn2)=(2.* (-1-dMuwdBm).* dSigmawdBm.^2)/Sigmaw^3-(2.* Zt(nn1:nn2).* dSigmawdBm^3)/Sigmaw^3+(2 .*dSigmawdBm.^2 .* ...
      dZtdBm(nn1:nn2))/Sigmaw.^2+(2.* dSigmawdBm.* d2MuwdBm2)/Sigmaw.^2-((-1-dMuwdBm).* d2SigmawdBm2)/Sigmaw.^2+ ...
      (3.* Zt(nn1:nn2).* dSigmawdBm .*d2SigmawdBm2)/Sigmaw.^2-(2.* dZtdBm(nn1:nn2).* d2SigmawdBm2)/Sigmaw- ...
      (dSigmawdBm.* d2ZtdBm2(nn1:nn2))/Sigmaw-d3MuwdBm3/Sigmaw-(Zt(nn1:nn2).* d3SigmawdBm3)/Sigmaw;
 
  d4ZtdBm4(nn1:nn2)=-((6.* (-1-dMuwdBm).* dSigmawdBm^3)/Sigmaw^4)+(6.* Zt(nn1:nn2).* dSigmawdBm^4)/Sigmaw^4- ...
      (6.* dSigmawdBm.^3 .*dZtdBm(nn1:nn2))/Sigmaw^3-(6 .*dSigmawdBm.^2 .* d2MuwdBm2)/Sigmaw^3+ ...
      (6.* (-1-dMuwdBm).* dSigmawdBm .*d2SigmawdBm2)/Sigmaw^3-(12.* Zt(nn1:nn2).* dSigmawdBm^2 .*d2SigmawdBm2)/Sigmaw^3+  ...
      (9.* dSigmawdBm .* dZtdBm(nn1:nn2) .* d2SigmawdBm2)/Sigmaw^2+(3  .*d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^2+ ...
      (3.* Zt(nn1:nn2).* d2SigmawdBm2^2)/Sigmaw^2+(3 .* dSigmawdBm^2 .* d2ZtdBm2(nn1:nn2))/Sigmaw^2- ...
      (3.* d2SigmawdBm2 .* d2ZtdBm2(nn1:nn2))/Sigmaw+(3 .* dSigmawdBm .* d3MuwdBm3)/Sigmaw.^2- ...
      ((-1-dMuwdBm) .* d3SigmawdBm3)/Sigmaw.^2+(4.* Zt(nn1:nn2) .* dSigmawdBm .* d3SigmawdBm3)/Sigmaw.^2- ...
      (3.* dZtdBm(nn1:nn2) .*d3SigmawdBm3)/Sigmaw-(dSigmawdBm.* d3ZtdBm3(nn1:nn2))/Sigmaw- ...
      d4MuwdBm4/Sigmaw-(Zt(nn1:nn2).* d4SigmawdBm4)/Sigmaw;
 
 
  
  d5ZtdBm5(nn1:nn2)=(24.* (-1-dMuwdBm).* dSigmawdBm.^4)/Sigmaw^5-(24.* Zt(nn1:nn2).* dSigmawdBm^5)/Sigmaw.^5+ ...
      (24 .*dSigmawdBm^4.* dZtdBm(nn1:nn2))/Sigmaw^4+(24.* dSigmawdBm.^3 .* d2MuwdBm2)/Sigmaw.^4-(36 .* (-1-dMuwdBm) .* dSigmawdBm^2 .* ...
      d2SigmawdBm2)/Sigmaw^4+(60.* Zt(nn1:nn2).* dSigmawdBm.^3.* d2SigmawdBm2)/Sigmaw^4-(48 .*dSigmawdBm^2.* dZtdBm(nn1:nn2) .* d2SigmawdBm2)/Sigmaw^3- ...
      (24 .* dSigmawdBm .* d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^3+(6.* (-1-dMuwdBm).* d2SigmawdBm2.^2)/Sigmaw^3- ...
      (30.* Zt(nn1:nn2) .* dSigmawdBm.* d2SigmawdBm2.^2)/Sigmaw^3+(12.* dZtdBm(nn1:nn2).* d2SigmawdBm2.^2)/Sigmaw^2- ...
      (12.* dSigmawdBm.^3 .* d2ZtdBm2(nn1:nn2))/Sigmaw^3+(18.8 .*dSigmawdBm .* d2SigmawdBm2 .* d2ZtdBm2(nn1:nn2))/Sigmaw^2- ...
      (12.* dSigmawdBm.^2 .* d3MuwdBm3)/Sigmaw^3+(6 .*d2SigmawdBm2 .*d3MuwdBm3)/Sigmaw^2+ ...
      (8.* (-1-dMuwdBm).* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-(20 .* Zt(nn1:nn2).* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^3+ ...
      (16.* dSigmawdBm.* dZtdBm(nn1:nn2).* d3SigmawdBm3)/Sigmaw^2+(4.* d2MuwdBm2  .*d3SigmawdBm3)/Sigmaw^2+ ...
      (10.* Zt(nn1:nn2).* d2SigmawdBm2  .*d3SigmawdBm3)/Sigmaw^2-(6.* d2ZtdBm2(nn1:nn2)  .*d3SigmawdBm3)/Sigmaw+ ...
      (4 .*dSigmawdBm^2.* d3ZtdBm3(nn1:nn2))/Sigmaw^2-(4.* d2SigmawdBm2 .* d3ZtdBm3(nn1:nn2))/Sigmaw+ ...
      (4.* dSigmawdBm .*d4MuwdBm4)/Sigmaw^2-((-1-dMuwdBm) .* d4SigmawdBm4)/Sigmaw^2+ ...
      (5.* Zt(nn1:nn2) .* dSigmawdBm .* d4SigmawdBm4)/Sigmaw^2-(4 .* dZtdBm(nn1:nn2).* d4SigmawdBm4)/Sigmaw- ...
      (dSigmawdBm.* d4Zt0dBm4(nn1:nn2))/Sigmaw-d5MuwdBm5/Sigmaw-(Zt(nn1:nn2) .* d5SigmawdBm5)/Sigmaw;
  
 
  
  
  d6ZtdBm6(nn1:nn2)=-((120.* (-1-dMuwdBm) .* dSigmawdBm.^5)/Sigmaw^6)+(120.* Zt(nn1:nn2).* dSigmawdBm.^6)/Sigmaw^6- ...
      (120.* dSigmawdBm.^5.* dZtdBm(nn1:nn2))/Sigmaw^5-(120.* dSigmawdBm.^4 .* d2MuwdBm2)/Sigmaw^5+ ...\
      (240 .*(-1-dMuwdBm).* dSigmawdBm.^3 .* d2SigmawdBm2)/Sigmaw^5-(360 .* Zt(nn1:nn2).* dSigmawdBm^4 .* d2SigmawdBm2)/Sigmaw^5+ ...
      (300.* dSigmawdBm^3 .*dZtdBm(nn1:nn2).* d2SigmawdBm2)/Sigmaw^4+(180 .* dSigmawdBm.^2 .* d2MuwdBm2 .* d2SigmawdBm2)/Sigmaw^4- ...
      (90.* (-1-dMuwdBm).* dSigmawdBm .* d2SigmawdBm2.^2)/Sigmaw^4+(270.* Zt(nn1:nn2) .* dSigmawdBm.^2 .* d2SigmawdBm2.^2)/Sigmaw^4- ...
      (150.* dSigmawdBm .* dZtdBm(nn1:nn2) .* d2SigmawdBm2.^2)/Sigmaw^3-(30 .* d2MuwdBm2 .* d2SigmawdBm2^2)/Sigmaw^3- ...
      (30 .* Zt(nn1:nn2).* d2SigmawdBm2.^3)/Sigmaw^3+(60 .* dSigmawdBm.^4 .* d2ZtdBm2(nn1:nn2))/Sigmaw^4- ...
      (120 .*dSigmawdBm.^2  .*d2SigmawdBm2 .*d2ZtdBm2(nn1:nn2))/Sigmaw^3+(30 .* d2SigmawdBm2.^2 .* d2ZtdBm2(nn1:nn2))/Sigmaw^2+ ...
      (60 .*dSigmawdBm.^3.* d3MuwdBm3)/Sigmaw^4-(60 .* dSigmawdBm .* d2SigmawdBm2 .*d3MuwdBm3)/Sigmaw^3- ...
      (60.* (-1-dMuwdBm) .* dSigmawdBm.^2 .* d3SigmawdBm3)/Sigmaw^4+(120.* Zt(nn1:nn2).* dSigmawdBm.^3 .*d3SigmawdBm3)/Sigmaw^4- ...
      (100.* dSigmawdBm^2 .*dZtdBm(nn1:nn2).* d3SigmawdBm3)/Sigmaw^3-(40.* dSigmawdBm.* d2MuwdBm2 .*d3SigmawdBm3)/Sigmaw^3+ ...
      (20 .*(-1-dMuwdBm) .* d2SigmawdBm2 .* d3SigmawdBm3)/Sigmaw^3-(120.* Zt(nn1:nn2).* dSigmawdBm .* d2SigmawdBm2 .* d3SigmawdBm3)/Sigmaw^3+ ...
      (50 .*dZtdBm(nn1:nn2) .* d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^2+(40.* dSigmawdBm .*d2ZtdBm2(nn1:nn2) .*d3SigmawdBm3)/Sigmaw^2+ ...
      (10.* d3MuwdBm3 .* d3SigmawdBm3)/Sigmaw^2+(10.* Zt(nn1:nn2).* d3SigmawdBm3.^2)/Sigmaw^2- ...
      (20.* dSigmawdBm.^3 .* d3ZtdBm3)/Sigmaw^3+(30 .*dSigmawdBm.* d2SigmawdBm2 .* d3ZtdBm3(nn1:nn2))/Sigmaw^2- ...
      (10.* d3SigmawdBm3 .* d3ZtdBm3)/Sigmaw-(20 .* dSigmawdBm.^2 .*d4MuwdBm4)/Sigmaw^3+(10 .*d2SigmawdBm2.* d4MuwdBm4)/Sigmaw^2+ ...
      (10.* (-1-dMuwdBm).* dSigmawdBm .* d4SigmawdBm4)/Sigmaw ^3-(30 .* Zt(nn1:nn2).* dSigmawdBm.^2 .* d4SigmawdBm4)/Sigmaw^3+ ...
      (25.* dSigmawdBm .* dZtdBm(nn1:nn2) .*d4SigmawdBm4)/Sigmaw ^2+(5.* d2MuwdBm2 .* d4SigmawdBm4)/Sigmaw^2+ ...
      (15.* Zt(nn1:nn2).* d2SigmawdBm2 .* d4SigmawdBm4)/Sigmaw^2-(10.* d2ZtdBm2(nn1:nn2) .* d4SigmawdBm4)/Sigmaw+ ...
      (5.* dSigmawdBm.^2 .* d4ZtdBm4(nn1:nn2))/Sigmaw.^2-(5 .*d2SigmawdBm2 .* d4ZtdBm4(nn1:nn2))/Sigmaw+ ...
      (5.* dSigmawdBm .* d5MuwdBm5)/Sigmaw^2-((-1-dMuwdBm).* d5SigmawdBm5)/Sigmaw^2+(6 .* Zt(nn1:nn2).* dSigmawdBm.* d5SigmawdBm5)/Sigmaw^2- ...
      (5.* dZtdBm(nn1:nn2).* d5SigmawdBm5)/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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;

Pmn(nn1:nn2)=IntegralCDF(nn1:nn2);
if(nn1>1)
Pmn(1:nn1-1)=0;
end
if (nn2<Nn)
Pmn(nn2+1:Nn)=1;
end
end

.
Last edited by Amin on April 4th, 2021, 10:34 am, edited 2 times in total.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 4th, 2021, 10:26 am

This is the second part of my previous post since it exceeded maximum characters allowed.
Here is the function that calculates the derivatives of drift and volatility (as yet in bessel form only) and this is also untested. I will be posting final working and tested versions in anohter 2-3 days as I hope. The function below can be written more efficiently by calculating higher derivatives of each term in drift and vol at the same time when we calculate the original term but I will come to that only later when a working program is perfectly ready. I have calculated drift and first hermite(dZ part of vol) to 4th order which might be unnecessary a lot of times but would be helpful when friends like to take large steps.  
I will later also be adding the program that will calculate the contribution of second hermite part of volatility (and similarly for 3rd and 4th hermite) in original coordinates to keep things accurate when you take a large step.  
.
function [wMu0dt,dwMu0dtdw,d2wMu0dtdw2,d3wMu0dtdw3,d4wMu0dtdw4,d5wMu0dtdw5,d6wMu0dtdw6,c1,dc1dw,d2c1dw2,d3c1dw3,d4c1dw4,d5c1dw5,d6c1dw6] = CalculateDriftAndVolA404B4(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;

   
   


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

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

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

 


end
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 4th, 2021, 3:57 pm

Though I would be posting a complete worked out code in another 2-3 days, since many friends would like to play on their own with the code, here is a very brief explanation with the functions I earlier posted about how to advance the density for one step.

Suppose we are given a grid at time t and we denote a general cell of this grid with subscript m. We first want to find drift, volatility and first six derivatives of both at center of all grid cells on this grid. This is done by the function(I earlier posted)  (wMu0dt is drift and c1 is volatility and rest are their derivatives on grid) given below as

[wMu0dt,dwMu0dtdw,d2wMu0dtdw2,d3wMu0dtdw3,d4wMu0dtdw4,d5wMu0dtdw5,d6wMu0dtdw6,c1,dc1dw,d2c1dw2,d3c1dw3,d4c1dw4,d5c1dw5,d6c1dw6] = CalculateDriftAndVolA404B4(w,wnStart,Nn,YqCoeff0,Fp1,gamma,dt);

%Now we want to calculate guess target points on time t+1 grid as possibly close to our actual target points. (These target points could be based on desired %CDF or equivalently on a desired Z-grid.) I have done that very cheaply and very approximately in next two lines as (feel free to use your own guess algorithm).This is done in next two lines of code as

SigmaB(nn)=sqrt(dwdZ(nn).^2+ c1(nn).^2);
wGuess(nn)=w(nn)+wMu0dt(nn)+(SigmaB(nn)-dwdZ(nn)).*Z(nn);

%Now loop over all Mm points on initial grid at time t and calculate the contribution of mth grid cell to CDF at guess points. Loop is given below as

for mm=1:Mm

%below assign t+1 target guess to Bn.        
Bn(1:Nn)=w2(1:Nn);
%Call the function (below) I posted to calculate the share of CDF from mth grid cell to all points Bn(1:Nn) on next time grid.             [Pmn]=CalculateCDFArraySignIndexDriftVol(Bn,Nn,wMu0dt(mm),dwMu0dtdw(mm),d2wMu0dtdw2(mm),d3wMu0dtdw3(mm),d4wMu0dtdw4(mm),d5wMu0dtdw5(mm),d6wMu0dtdw6(mm),c1(mm),dc1dw(mm),d2c1dw2(mm),d3c1dw3(mm),d4c1dw4(mm),d5c1dw5(mm),d6c1dw6(mm),Bm0(mm),Bm1(mm),Bm2(mm),dPm(mm),pm0(mm),dpm0(mm),d2pm0(mm),d3pm0(mm),d4pm0(mm),d5pm0(mm));
%Line of code below. continue to calculate total CDF over the loop at each point Bn(indicated by Pn) by adding share of CDF coming from mth grid cell.
Pn(1:Nn)=Pn(1:Nn)+Pmn(1:Nn);
    
end

%Now retrieve the Z associated with the CDF calculated on guess points as given below
Zn(1:Nn)=norminv(Pn(1:Nn));
 
%Now you can interpolate the required CDF or equivalently desired underlying CDF by simply interpolating the desired Z between calculated Znew and %associated values of Bn we had used. Basically we have the pairs (Zn,Bn/wn) calculated and we would have to interpolate the value of wn/Bn against the desired values of underlying Z (or equivalently the desired values of CDF since each Z is uniquely mapped on its own CDF).
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 4th, 2021, 10:21 pm

Dear friends, In this post, I will try to explain with a bit more clarity how to calculate the transition CDF.PDF, and their derivatives between a whole grid cell at time t to an arbitrary point at time t+1. I will use the previous notation but with some slight changes, write and explain equations and later(in the same post) post the function code to calculate those CDF, PDF and its derivatives. I will also post some(simple) code that I used to compare results of some analytic integrals in above formulas with numerical integration. Today, I will only post the above function and related code and post the whole working program with explanatory comments tomorrow. And yes, I will post the simplified version of solution to quartic equation today(that will be used by the main code that I will post tomorrow).
I am copying my old post for reference but will try to explain things again.
Amin:
Post in progress.
I tried a bit to see if I could further improve on the formula for probability mass transfer CDF I wrote a few posts ago. I am copying equation(E) from the relevant earlier post. 
We write the above results of Equation(D) and substitute them in equation (C) and take constants out of the integrals to write the equation(E) as 
[$]\int_{B_{m1}}^{B_{m2}} p_m(B) P_{m,n}(B) dB[$]
[$]=\Delta P_m P_{m0,n}[$]
[$]+ ( p_{m0} p_{m0,n})  \int_{B_{m1}}^{B_{m2}}  (B - B_{m0}) dB[$]
[$]+ (2 \frac{dp_{m0}}{dB} p_{m0,n}+  p_{m0} \frac{dp_{m0,n}}{dB}) \int_{B_{m1}}^{B_{m2}}  \frac{ {(B - B_{m0})}^2}{2} dB[$]
[$]+(3 \frac{d^2p_{m0}}{d{B}^2} p_{m0,n}+3 \frac{dp_{m0}}{dB} \frac{dp_{m0,n}}{dB}+  p_{m0} \frac{d^2p_{m0,n}}{dB^3})  \int_{B_{m1}}^{B_{m2}}   \frac{{(B - B_{m0})}^3}{6}  dB[$]  Equation(E)

We have earlier simplified the equation by collecting some terms and recognizing their series and finding the term that describes the series. We want to try doing that with some other terms as well but it is a bit more difficult now.
Let us put separate some more terms and try to simplify them as

[$] ( p_{m0} p_{m0,n})  \int_{B_{m1}}^{B_{m2}}  (B - B_{m0}) dB[$]
[$]+ (p_{m0} \frac{dp_{m0,n}}{dB}) \int_{B_{m1}}^{B_{m2}}  \frac{ {(B - B_{m0})}^2}{2} dB[$]
[$]+(p_{m0} \frac{d^2p_{m0,n}}{dB^3})  \int_{B_{m1}}^{B_{m2}}   \frac{{(B - B_{m0})}^3}{6}  dB[$]  equation(a)

The above three terms could  be recognized as series for integral of CDF if we could add first order Taylor term that it is missing. So we could write as
[$]\int_{B_{m1}}^{B_{m2}} p_{m0} P_{m,n}(B) dB[$]
[$] -  \int_{B_{m1}}^{B_{m2}} ( p_{m0} P_{m0,n})  dB[$]
[$] =  \int_{B_{m1}}^{B_{m2}}  ( p_{m0} p_{m0,n})(B - B_{m0}) dB[$]
[$]+  \int_{B_{m1}}^{B_{m2}} (p_{m0} \frac{dp_{m0,n}}{dB}) \frac{ {(B - B_{m0})}^2}{2} dB[$]
[$]+ \int_{B_{m1}}^{B_{m2}} (p_{m0} \frac{d^2p_{m0,n}}{dB^3})   \frac{{(B - B_{m0})}^3}{6}  dB[$] equation(b)

So we can basically replace the series continuation of the following terms  
[$] ( p_{m0} p_{m0,n})  \int_{B_{m1}}^{B_{m2}}  (B - B_{m0}) dB[$]
[$]+ (p_{m0} \frac{dp_{m0,n}}{dB}) \int_{B_{m1}}^{B_{m2}}  \frac{ {(B - B_{m0})}^2}{2} dB[$]
[$]+(p_{m0} \frac{d^2p_{m0,n}}{dB^3})  \int_{B_{m1}}^{B_{m2}}   \frac{{(B - B_{m0})}^3}{6}  dB[$] Equation(a)

with the following two terms

[$] p_{m0} \int_{B_{m1}}^{B_{m2}} P_{m,n}(B) dB[$]
[$] - ( p_{m0} P_{m0,n}) \int_{B_{m1}}^{B_{m2}}   dB[$] Equation(c)

The first analytic integral might be hard to solve analytically but we can try to integrate it by parts as 
[$]  \int_{B_{m1}}^{B_{m2}} P_{m,n} dB[$]
[$]=B_{m2} P_{m2,n} - B_{m1} P_{m1,n}  - \int_{B_{m1}}^{B_{m2}}  B p_{m,n}(B) dB[$]  Equation(d)

Substituting Equation(d) in Equation(c), we get
[$] p_{m0} \int_{B_{m1}}^{B_{m2}} P_{m,n}(B) dB[$]
[$] - ( p_{m0} P_{m0,n}) \int_{B_{m1}}^{B_{m2}}   dB[$]
[$]=p_{m0} B_{m2} P_{m2,n} -p_{m0} B_{m1} P_{m1,n} [$]
[$]- p_{m0} \int_{B_{m1}}^{B_{m2}}B p_{m,n}(B) dB[$]
[$] - ( p_{m0} P_{m0,n}) \int_{B_{m1}}^{B_{m2}}   dB[$] Equation(e)

Now we can substitute equation(e) in the original equa.tion(E) and write as
[$]\int_{B_{m1}}^{B_{m2}} p_m(B) P_{m,n}(B) dB[$]
[$]=\Delta P_m P_{m0,n}[$]
[$]+p_{m0} B_{m2} P_{m2,n} -p_{m0} B_{m1} P_{m1,n} [$]
[$]- p_{m0} \int_{B_{m1}}^{B_{m2}}B p_{m,n}(B) dB[$]
[$] - ( p_{m0} P_{m0,n}) \int_{B_{m1}}^{B_{m2}}   dB[$]
[$]+ (2 \frac{dp_{m0}}{dB} p_{m0,n}) \int_{B_{m1}}^{B_{m2}}  \frac{ {(B - B_{m0})}^2}{2} dB[$]
[$]+(3 \frac{d^2p_{m0}}{d{B}^2} p_{m0,n}+3 \frac{dp_{m0}}{dB} \frac{dp_{m0,n}}{dB})  \int_{B_{m1}}^{B_{m2}}   \frac{{(B - B_{m0})}^3}{6}  dB[$] 
[$] + higher order terms [$] 
EDIT: In some of the integrals below I have used dB and mostly I have used [$]dB_m[$] but both are the same. I forgot the subscript m in some integrals. But I hope that friends would still perfectly understand as I have given reasonable orientation explanation for most of the integrals.
 
In this post, I want to write how to calculate exact Gaussian transition CDF, Gaussian transition probability and derivatives of Gaussian transition probability from mth Bessel grid cell  [$]B_m[$] at time t, to any point on the new grid at time t+1. 

Let us fix the notation first. We have a Bessel grid and we indicate points in an arbitrary cell by [$]B_m[$](this is notation, we use when we integrate over the originating cell etc) and the center of this cell is given by [$]B_{m0}[$]. 
This center [$]B_{m0}[$] will correspond to point [$]Z_m[$] on Z-grid and in general both boundaries of the cell(that will correspond to [$]Z_m+\Delta Z_m/2[$] and [$]Z_m-\Delta Z_m/2[$]) will not be equidistant from this center of the cell [$]B_{m0}[$]. We will represent the left boundary of cell [$]B_m[$] corresponding to [$]Z_m-\Delta Z_m/2[$] as  [$]B_{m1}[$] and right boundary corresponding to  [$]Z_m+\Delta Z_m/2[$]  as [$]B_{m2}[$]
Please note that gaussian [$]Z_m[$] that underlies the originating bessel grid is different from the transition gaussian between any point on the originating grid cell [$]B_m[$] to some target point [$]B_n[$] on grid at time t+1 which is indicated with [$]Z_t[$]. By definition [$]Z_t=B_n(t+1)-B_m(t)[$]. Since in the following analyticss, we have to calculate derivatives of gaussian [$]Z_t[$] with respect to both [$]B_n[$] and [$]B_m[$], we denote the derivatives with respect to [$]B_n[$] with a simplified notation using apostrophe on the original variable and derivatives with respect to [$]B_m[$] with a fraction notation. Please note that
[$]\frac{\partial B_n}{\partial Z_t}=1[$] and
[$]\frac{\partial B_m}{\partial Z_t}=-1[$]

The transition probability between arbitrary point on mth cell [$]B_{m}[$] to arbitary point [$]B_n[$] on next grid is denoted as [$] p_{m,n}=N(B_n-B_{m},0,\sigma_m)[$].Its CDF is defined by uppercase of PDF as [$] P_{m,n}=\int_{-\infty}^{(B_n-B_{m})} N(B_n-B_{m},0,\sigma_m) dN [$] 
When we expand around center of mth grid cell [$]B_{m0}[$], the above quantities change notation and the transition probability between center of mth cell [$]B_{m0}[$] to arbitary point [$]B_n[$] on next grid is denoted as [$] p_{m0,n}=N(B_n-B_{m0},0,\sigma_m)[$].Its CDF is defined by uppercase of PDF as [$] P_{m0,n}=\int_{-\infty}^{(B_n-B_{m0})} N(B_n-B_{m0},0,\sigma_m) dN [$]
When we calculate kth derivative of CDF or its other derivatives (with respect to target [$]B_n[$]) further with respect to originating cell [$]B_m[$], we use the simple formula [$]\frac{\partial^k P_{m,n}}{\partial {B_m}^k}[$][$]=\frac{\partial^k P_{m,n}}{\partial {Z_t}^k} {(\frac{\partial Z_t}{\partial B_m})}^k[$][$]={P_{m,n}}^{(k)}(Zt) * {(-1)}^{k}[$] since  [$]\frac{\partial Z_t}{\partial B_m}=-1[$]

Now we come to derivatives of PDF in the originating cell [$]B_m[$] with underlying unit gaussian [$]Z_m[$]. Please note that this gaussian [$]Z_m[$] underlying originating cell [$]B_m[$] is different from transition gaussian [$]Z_t[$] between originating cell [$]B_m(t)[$] and target cell [$]B_n(t+1)[$]. In the following paragraph, we are only concerned with probability distribution and its derivatives in the originating cell [$]B_m[$]. In general, This probability distribution in originating cell is not a gaussian probability but this PDF and its derivatives in mth cell are related to  underlying gaussian probability [$]Z_m[$] by change of variable formula for distributions.
We define the probability density function at center of mth cell in Bessel variable [$]B_m[$] as lowercase [$]p_{m0}=p(Z_m) \frac{dZ}{dB_m}[$] while uppercase will denote its CDF given as [$]P_{m0}=P(Z_m)[$]
The probability mass in mth cell which is the difference of CDF at boundaries is denoted as [$]\Delta P_{m}[$] 
The derivatives of  probability density function at center of mth cell in Bessel variable [$]B_m[$] are given as[$]\frac{dp_{m0}}{dB}=\frac{dp(Z_m)}{dZ} {(\frac{dZ}{dB_m})}^2+p(Z_m) \frac{d^2Z}{d{B_m}^2} [$]
We can similarly calculate higher derivatives [$]\frac{d^2p_{m0}}{d{B}^2}(B_{m0})[$] and [$]\frac{d^3p_{m0}}{d{B}^3}[$] by further differentiating the above function. (I calculated these derivatives in the main body of the program since they have to be calculated just once for every transition to target points and calculating them in the function that calculates CDF and its derivatives from originating cell to target points was not appropriate. They are simply input to the function that calculates CDF and its derivatives to target points at t+1 from a particular cell at time t.)
Again please note the difference that  [$]P_{m0}[$] and [$]p_{m0}[$] are CDF and its PDF in the originating cell(calculated at its center) while [$]P_{m0,n}[$] and [$]p_{m0,n}[$] are transition CDF and PDF respectively between center of originating cell and the target point [$]B_n[$] on next time grid.
We want to calculate the transition CDF, transition probability, and its derivatives from a cell m at time t, to an arbitrary point n on the grid at time t+1.  
We first calculate the CDF from entire mth cell to nth point on grid at time t+1 and write it as an integral as
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n}(B_m) dB_m[$] Equation(A)
Here [$]p_m(B_m)[$] indicates the probability density function at arbitrary point [$]B_m[$] within the mth subdivision(not necessarily at center).
Here [$]P_{m,n}(B_m)[$] indicates the CDF of transition probability from an arbitrary point [$]B_m[$] within the mth subdivision(not necessarily from its center) to arbitrary point [$]B_n[$] on next (time level) grid such that [$]Z_t=B_n - B_m[$].
Our strategy to solve the above integral is to take derivatives of integrand at center of the grid cell [$]B_{m0}[$] and expand it as Taylor series and then solve it. Our expansion is basically constant values calculated at center of the mth subdivision(which are handily available) multiplied by an integral of the powers of difference of distance of two boundaries from the center. I have already indicated previously how to calculate these derivatives at center to any higher order.

So we have (in the following equation B indicates [$]B_m[$]. I have omitted m in subscript in the expansion after the first line.) 
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n} dB_m[$]
[$]=\int_{B_{m1}}^{B_{m2}} p_{m0} P_{m0,n} dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{dp_{m0}}{dB} P_{m0,n}+ p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^2p_{m0}}{d{B}^2} P_{m0,n}+2 \frac{dp_{m0}}{dB} \frac{dP_{m0,n}}{dB}+  p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^3p_{m0}}{d{B}^3} P_{m0,n}+3 \frac{d^2p_{m0}}{d{B}^2} \frac{dP_{m0,n}}{dB}+3 \frac{dp_{m0}}{dB} \frac{d^2P_{m0,n}}{dB^2}+  p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]  Equation(B)
[$]+ higher order terms in Taylor series[$]
Please note again that in the above equation odd derivatives of transition CDF with respect to [$]B_m[$] or B (abbreviated notation) will take a negative sign with respect to regular(plain) transition CDF and its derivatives(with respect to [$]Z_t[$] as [$]\frac{dB_m}{dZ_t}=-1[$].
We rearrange the above equation in further three groups of terms. Then We find analytic approximation to two of those three groups and add simply add the third group as such.
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n}(B_m) dB_m[$]
[$]=G1 + G2 + G3[$]
where

[$]G1=\int_{B_{m1}}^{B_{m2}} p_{m0} P_{m0,n} dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{dp_{m0}}{dB} P_{m0,n})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^2p_{m0}}{d{B}^2} P_{m0,n})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^3p_{m0}}{d{B}^3} P_{m0,n})  \frac{{(B - B_{m0})}^3}{6}   dB[$]
and
[$]G2=\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]

[$]G3=\int_{B_{m1}}^{B_{m2}}  (2 \frac{dp_{m0}}{dB} \frac{dP_{m0,n}}{dB})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (3 \frac{d^2p_{m0}}{d{B}^2} \frac{dP_{m0,n}}{dB}+3 \frac{dp_{m0}}{dB} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^3}{6}   dB[$] Equation(C)

We notice that after taking [$]P_{m0,n}[$] common out of terms in G!, the terms in G1(and their taylor series continuation) are integral of a simple Taylor expansion of probability density function in the originating mth Grid at time t and hence represent the integral of probability mass in mth subdivision. Hence we can write this group G1 as  
[$]G1= P_{m0,n} \int_{B_{m1}}^{B_{m2}} p_{m}(B_m) dB_m= P_{m0,n} \Delta P_m[$]

where [$]\Delta P_m[$] indicates total (integral of) probability mass in the mth subdivision.  

Now we take the second group of terms.
[$]G2=\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]
We notice that if we take [$] p_{m0}[$] common, this group of terms (and their taylor series continuation) would equal the (non-probability weighted) integral of transition CDF over the mth subdivision but in order to do that we will have to add one first term in the series that would equal [$]T2= \int_{B_{m1}}^{B_{m2}}   p_{m0} P_{m0,n} dB[$]. Only if we could add T2 to this G2 group of terms, it would be equal to (non-probability weighted) integral of transition CDF over mth subdivision. So we can write 

[$]G2= p_{m0} \int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m-\int_{B_{m1}}^{B_{m2}}   p_{m0} P_{m0,n} dB [$]
The term T2 we added over G2 is very simple but we would still have to see how to analytically solve (non-probability weighted) integral of CDF over mth subdivision given as [$]\int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m[$]. In order to solve this integral by parts as
[$]\int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m=\int_{B_{m1}}^{B_{m2}} d[B_m P_{m,n}(B_m)] - \int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
[$]=B_{m2} P_{m2,n} - B_{m1} P_{m1,n} - \int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
In order to solve the second term in last line, we write as
[$]\int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
We notice that [$]B_m=B_n-Z_t[$]  , [$]\frac{d P_{m,n}}{dB_m}= -p_{m,n}(Z_t)[$] and [$]dB_m=- dZ_t[$], [$]Z_{Bm1}=B_n - B_{m1}[$] and [$]Z_{Bm2}=B_n - B_{m2}[$]
so the integral in question becomes  
[$]\int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m =\int_{Z_{Bm1}}^{Z_{Bm2}} ( B_n-Z_t) p_{m,n}(Z_t) dZ_t[$]
dropping the subscript t in the notation of transition gaussian, we write the above equation as
[$]=\int_{Z_{Bm1}}^{Z_{Bm2}} ( B_n-Z) p_{m,n}(Z) dZ= B_n \int_{Z_{Bm1}}^{Z_{Bm2}} p(Z) dZ - \int_{Z_{Bm1}}^{Z_{Bm2}}  Z p(Z) dZ[$]
Both of the above integrals can be solved analytically. I checked the whole of this by parts analytic solution by comparing it with numerical integration of the(non-probability weighted)  CDF over different subdivisions and found it to be the right formula. In the end today, I will also give some (informal) programs I used to compare this by parts integral with numerical integration.
With the above calculations, the terms in group G2 equal to the expression as
[$]G2= p_{m0} \int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m-\int_{B_{m1}}^{B_{m2}}   p_{m0} P_{m0,n} dB [$]
[$]= p_{m0} (B_{m2} P_{m2,n} - B_{m1} P_{m1,n} ) - p_{m0} B_n \int_{Z_{Bm1}}^{Z_{Bm2}} p(Z) dZ + p_{m0} \int_{Z_{Bm1}}^{Z_{Bm2}}  Z p(Z) dZ[$]
[$]- p_{m0} P_{m0,n} \int_{B_{m1}}^{B_{m2}}  dB [$]
Please note that gaussian Z in above is not a unit gaussian, and it takes variance associated with the transition probability.
We add the terms in group G3 individually without any special analytical treatment.

I am pasting the code in next post.
I have re-written the earlier note so that it accounts for variable drift and variable volatility over the originating cell for calculation of CDF from originating grid cell to a target point at time t+1 when the dynamics are governed by an SDE. I will first outline three differences with respect to previous exposition.
1. I have changed the definition of transition gaussian and defined it to be standard as opposed to previous exposition in which it was not defined to be standard.
[$]Z_t=\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{\sigma(B_m(t))}[$]
2. Earlier the derivatives of transition gaussian with respect to originating cell [$]B_m[$] were very simple due to different defintion, constant volatility and due to lack of drift but now it has changed to
[$]\frac{\partial Z_t}{\partial B_m}=\frac{-1-\frac{\partial\mu(B_m}{\partial B_m}}{\sigma(B_m(t))}-\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{{\sigma(B_m(t))}^2} \frac{\partial\sigma(B_m)}{\partial B_m}[$]
and so on for higher derivatives.
3. Later in the previous exposition, I divided the main Taylor expansion into three groups G1, G2 nd G3. In this new note, Math for groups G1 and G3 remains almost identical (except that [$]\frac{\partial Z_t}{\partial B_m}[$] has changed) but I have re-done math for Group G2 that required more care with variable drift and variable volatility.  

I want to emphasize that [$]\mu(B_m(t))[$] and [$]\sigma(B_m(t))[$] both are expressions with anywhere between 5 to 25 terms and each term is algebraic in [$]B_m[$] multiplied by some coefficient so both of these can be differentiated many times at the center and both [$]\mu(B_m(t))[$] and [$]\sigma(B_m(t))[$] can be easily integrated over the originating grid cell due to their algebraic(multiplied by a coefficient depending on transition time and SDE parameters) nature.

Please note that in the note below, I have used subdivision and grid cell interchangeably and they mean the same thing.

In this post, I want to write how to calculate exact Gaussian transition CDF, Gaussian transition probability and derivatives of Gaussian transition probability from mth Bessel grid cell  [$]B_m[$] at time t, to any point on the new grid at time t+1. 

Let us fix the notation first. We have an originating grid at time [$]t_1[$] and we indicate points in an arbitrary cell on this grid by [$]B_m[$](this is notation, we use when we integrate over the originating cell etc) and the center of this cell is given by [$]B_{m0}[$]. 
This center [$]B_{m0}[$] will correspond to point [$]Z_m[$] on Z-grid and in general both boundaries of the cell(that will correspond to [$]Z_m+\Delta Z_m/2[$] and [$]Z_m-\Delta Z_m/2[$]) will not be equidistant from this center of the cell [$]B_{m0}[$]. We will represent the left boundary of cell [$]B_m[$] corresponding to [$]Z_m-\Delta Z_m/2[$] as  [$]B_{m1}[$] and right boundary corresponding to  [$]Z_m+\Delta Z_m/2[$]  as [$]B_{m2}[$]
Please note that gaussian [$]Z_m[$] that underlies the originating bessel grid is different from the transition gaussian between any point on the originating grid cell [$]B_m[$] to some target point [$]B_n[$] on grid at time t+1 which is indicated with [$]Z_t[$]. By definition [$]Z_t=\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{\sigma(B_m(t))}[$]. Please notice that in previous program, the transition gaussian was not standard by definition but now we take it as standard Gaussian by definition. 
Since in the following analytics, we have to calculate derivatives of gaussian [$]Z_t[$] with respect to both [$]B_n[$] and [$]B_m[$], we denote the derivatives with respect to [$]B_n[$] with a simplified notation using apostrophe on the original variable and derivatives with respect to [$]B_m[$] with a fraction notation. Please note that
since 
[$]Z_t=\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{\sigma(B_m(t))}[$]
[$]\frac{\partial Z_t}{\partial B_n}=\frac{1}{\sigma(B_m(t))}[$] 
[$]\frac{\partial Z_t}{\partial B_m}=\frac{-1-\frac{\partial\mu(B_m}{\partial B_m}}{\sigma(B_m(t))}-\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{{\sigma(B_m(t))}^2} \frac{\partial\sigma(B_m)}{\partial B_m}[$]
We will also need higher drivatives [$]\frac{\partial^2 Z_t}{\partial {B_m}^2}[$] and so on that we will easily find with repeated differentiation. We will need these derivatives expressions at the center of the originating grid cell and since expressions for [$]\mu(B_m(t))[$] and [$]\sigma(B_m(t))[$] both can be easily differentiated, this is not difficult.
We will calculate the derivatives of transition probability or transition CDF with respect to [$]B_m[$] by using the equations
[$]\frac{dP_{m,n}}{dB_m}=\frac{dP_{m,n}}{dZ_t} \frac{\partial Z_t}{\partial B_m}[$]
[$]\frac{d^2P_{m,n}}{{dB_m}^2}=\frac{d^2P_{m,n}}{d{Z_t}^2} {(\frac{\partial Z_t}{\partial B_m})}^2+\frac{dP_{m,n}}{dZ_t} \frac{\partial^2 Z_t}{\partial {B_m}^2}[$]
and so on for higher order

The transition probability between arbitrary point on mth cell [$]B_{m}[$] to arbitary point [$]B_n[$] on next grid is denoted as [$] p_{m,n}=\frac{1}{\sigma(B_m)\sqrt{2 \pi}}Exp(-.5 {(\frac{(B_n(t+1)-B_m(t)-\mu(B_m(t)))}{\sigma(B_m(t))})}^2)[$].Its CDF is defined by uppercase of PDF as integral of above probability density of transition Zt from -infinity to Zt.
When we expand around center of mth grid cell [$]B_{m0}[$], the above quantities change notation and the transition probability between center of mth cell [$]B_{m0}[$] to arbitary point [$]B_n[$] on next grid is denoted as [$] p_{m0,n}=\frac{1}{\sigma(B_m)\sqrt{2 \pi}}Exp(-.5 {(\frac{(B_n(t+1)-B_m0(t)-\mu(B_m0(t)))}{\sigma(B_m0(t))})}^2)[$].Its CDF is denoted by uppercase of PDF as [$] P_{m0,n} [$]

Now we come to derivatives of PDF in the originating cell [$]B_m[$] with underlying unit gaussian [$]Z_m[$]. Please note that this gaussian [$]Z_m[$] underlying originating cell [$]B_m[$] is different from transition gaussian [$]Z_t[$] between originating cell [$]B_m(t)[$] and target cell [$]B_n(t+1)[$]. In the following paragraph, we are only concerned with probability distribution and its derivatives in the originating cell [$]B_m[$]. In general, This probability distribution in originating cell is not a gaussian probability but this PDF and its derivatives in mth cell are related to  underlying gaussian probability [$]Z_m[$] by change of variable formula for distributions.
We define the probability density function at center of mth cell in Bessel variable [$]B_m[$] as lowercase [$]p_{m0}=p(Z_m) \frac{dZ}{dB_m}[$] while uppercase will denote its CDF given as [$]P_{m0}=P(Z_m)[$]
The probability mass in mth cell which is the difference of CDF at boundaries is denoted as [$]\Delta P_{m}[$] 
The derivatives of  probability density function at center of mth cell in Bessel variable [$]B_m[$] are given as[$]\frac{dp_{m0}}{dB}=\frac{dp(Z_m)}{dZ} {(\frac{dZ}{dB_m})}^2+p(Z_m) \frac{d^2Z}{d{B_m}^2} [$]
We can similarly calculate higher derivatives [$]\frac{d^2p_{m0}}{d{B}^2}(B_{m0})[$] and [$]\frac{d^3p_{m0}}{d{B}^3}[$] by further differentiating the above function. (I calculated these derivatives in the main body of the program since they have to be calculated just once for every transition to target points and calculating them in the function that calculates CDF and its derivatives from originating cell to target points was not appropriate. They are simply input to the function that calculates CDF and its derivatives to target points at t+1 from a particular cell at time t.)
Again please note the difference that  [$]P_{m0}[$] and [$]p_{m0}[$] are CDF and its PDF in the originating cell(calculated at its center) while [$]P_{m0,n}[$] and [$]p_{m0,n}[$] are transition CDF and PDF respectively between center of originating cell and the target point [$]B_n[$] on next time grid.
We want to calculate the transition CDF, transition probability, and its derivatives from a cell m at time t, to an arbitrary point n on the grid at time t+1.  
We first calculate the CDF from entire mth cell to nth point on grid at time t+1 and write it as an integral as
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n}(B_m) dB_m[$] Equation(A)
Here [$]p_m(B_m)[$] indicates the probability density function at arbitrary point [$]B_m[$] within the mth subdivision(not necessarily at center).
Here [$]P_{m,n}(B_m)[$] indicates the CDF of transition probability from an arbitrary point [$]B_m[$] within the mth subdivision(not necessarily from its center) to arbitrary point [$]B_n[$] on next (time level) grid.
Our strategy to solve the above integral is to take derivatives of integrand at center of the grid cell [$]B_{m0}[$] and expand it as Taylor series and then solve it. Our expansion is basically constant values calculated at center of the mth subdivision(which are handily available) multiplied by an integral of the powers of difference of distance of two boundaries from the center. I have already indicated previously how to calculate these derivatives at center to any higher order.

So we have (in the following equation B indicates [$]B_m[$]. I have omitted m in subscript in the expansion after the first line.) 
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n} dB_m[$]
[$]=\int_{B_{m1}}^{B_{m2}} p_{m0} P_{m0,n} dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{dp_{m0}}{dB} P_{m0,n}+ p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^2p_{m0}}{d{B}^2} P_{m0,n}+2 \frac{dp_{m0}}{dB} \frac{dP_{m0,n}}{dB}+  p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^3p_{m0}}{d{B}^3} P_{m0,n}+3 \frac{d^2p_{m0}}{d{B}^2} \frac{dP_{m0,n}}{dB}+3 \frac{dp_{m0}}{dB} \frac{d^2P_{m0,n}}{dB^2}+  p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]  Equation(B)
[$]+ higher order terms in Taylor series[$]

We rearrange the above equation in further three groups of terms. Then We find analytic approximation to two of those three groups and add simply add the third group as such.
[$]\int_{B_{m1}}^{B_{m2}} p_m(B_m) P_{m,n}(B_m) dB_m[$]
[$]=G1 + G2 + G3[$]
where
[$]G1=\int_{B_{m1}}^{B_{m2}} p_{m0} P_{m0,n} dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{dp_{m0}}{dB} P_{m0,n})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^2p_{m0}}{d{B}^2} P_{m0,n})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (\frac{d^3p_{m0}}{d{B}^3} P_{m0,n})  \frac{{(B - B_{m0})}^3}{6}   dB[$]
and
[$]G2=\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]

[$]G3=\int_{B_{m1}}^{B_{m2}}  (2 \frac{dp_{m0}}{dB} \frac{dP_{m0,n}}{dB})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (3 \frac{d^2p_{m0}}{d{B}^2} \frac{dP_{m0,n}}{dB}+3 \frac{dp_{m0}}{dB} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^3}{6}   dB[$] Equation(C)

We notice that after taking [$]P_{m0,n}[$] common out of terms in G1, the terms in G1(and their taylor series continuation) are integral of a simple Taylor expansion of probability density function in the originating mth Grid at time t and hence represent the integral of probability mass in mth subdivision. Hence we can write this group G1 as  
[$]G1= P_{m0,n} \int_{B_{m1}}^{B_{m2}} p_{m}(B_m) dB_m= P_{m0,n} \Delta P_m[$]
where [$]\Delta P_m[$] indicates total (integral of) probability mass in the mth subdivision.  

Now we take the second group of terms.
[$]G2=\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{dP_{m0,n}}{dB})  (B - B_{m0}) dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  ( p_{m0} \frac{d^2P_{m0,n}}{dB^2})  \frac{{(B - B_{m0})}^2}{2}  dB[$]
[$]+\int_{B_{m1}}^{B_{m2}}  (p_{m0} \frac{d^3P_{m0,n}}{dB^3})  \frac{{(B - B_{m0})}^3}{6}   dB[$]
We notice that if we take [$] p_{m0}[$] common, this group of terms (and their taylor series continuation) would equal the (non-probability weighted) integral of transition CDF over the mth subdivision but in order to do that we will have to add one first term in the series that would equal [$]T2= \int_{B_{m1}}^{B_{m2}}   p_{m0} P_{m0,n} dB[$]. Only if we could add T2 to this G2 group of terms, it would be equal to (non-probability weighted) integral of transition CDF over mth subdivision. So we can write 
[$]G2= p_{m0} \int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m-\int_{B_{m1}}^{B_{m2}}   p_{m0} P_{m0,n} dB [$]
The term T2 we added over G2 is very simple but we would still have to see how to analytically solve (non-probability weighted) integral of CDF over mth subdivision given as [$]\int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m[$]. In order to solve this integral by parts as
[$]\int_{B_{m1}}^{B_{m2}} P_{m,n}(B_m) dB_m=\int_{B_{m1}}^{B_{m2}} d[B_m P_{m,n}(B_m)] - \int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
[$]=B_{m2} P_{m2,n} - B_{m1} P_{m1,n} - \int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
****In order to solve the second term in last line, we write as
[$]\int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m  [$]
We notice that [$]B_m=B_n-\sigma(B_m) Z_t -\mu(B_m)[$]  
so the integral in question becomes  
[$]\int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m =\int_{B_{m1}}^{B_{m2}} (B_n-\sigma(B_m) Z_t -\mu(B_m) \frac{d P_{m,n}}{dB_m} dB_m[$]
so we have 
[$]\int_{B_{m1}}^{B_{m2}} B_m \frac{d P_{m,n}}{dB_m} dB_m = I_1 + I_2 +I_3[$]
where 
[$]I_1=\int_{B_{m1}}^{B_{m2}} B_n \frac{d P_{m,n}}{dB_m} dB_m[$]
[$]I_2=-\int_{B_{m1}}^{B_{m2}} \sigma(B_m) Z_t \frac{d P_{m,n}}{dB_m} dB_m[$]
[$]I_3=-\int_{B_{m1}}^{B_{m2}} \mu(B_m) \frac{d P_{m,n}}{dB_m} dB_m[$]

integral [$]I_1[$] is straightforward since [$]B_n[$] is a constant so it can be solved as
[$]I_1=B_n (P_{m2,n}-P_{m1,n})[$]
We want to carefully solve integral [$]I_2[$]
[$]I_2=-\int_{B_{m1}}^{B_{m2}} \sigma(B_m) Z_t \frac{d P_{m,n}}{dB_m} dB_m[$]
[$]=\int_{B_{m1}}^{B_{m2}} \sigma(B_m) \frac{d p_{m,n}}{dB_m} dB_m[$]
where I have used [$]Z_t \frac{d P_{m,n}}{dB_m}=Z_t \frac{d P_{m,n}}{dZ_t}\frac{d Z_t}{dB_m}[$]
[$]=-\frac{d p_{m,n}}{dZ_t}\frac{d Z_t}{dB_m}=\frac{d p_{m,n}}{dB_m}[$]
so 
[$]I_2=\int_{B_{m1}}^{B_{m2}} \sigma(B_m) \frac{d p_{m,n}}{dB_m} dB_m[$]

Just like the main original integral, we expand this integral again into Taylor series at the centre of grid cell and then among the expansion, we identify two Taylor expansions at the ends both of which can be solved analytically just like we have done for the main integral.
So we have
[$]I_2=\int_{B_{m1}}^{B_{m2}} \sigma(B_m) \frac{d p_{m,n}}{dB_m} dB_m[$]
[$]=\sigma(B_{m0}) \frac{d p_{m0,n}}{dB_m} (B_{m1}-B_{m2})[$]
[$]+(\frac{d\sigma(B_{m0})}{dB_m} \frac{d p_{m0,n}}{dB_m} + \sigma(B_{m0}) \frac{d^2 p_{m0,n}}{d{B_m}^2}) \int_{B_{m1}}^{B_{m2}} (B-B_{m0}) dB_m[$]
[$]+(\frac{d^2\sigma(B_{m0})}{d{B_m}^2} \frac{d p_{m0,n}}{dB_m} +2 \frac{d\sigma(B_{m0})}{dB_m} \frac{d^2 p_{m0,n}}{d{B_m}^2}+ \sigma(B_{m0}) \frac{d^3 p_{m0,n}}{d{B_m}^3}) \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^2}{2} dB_m[$]

We again divide the above expansion into three groups of terms [$]H1[$], [$]H2[$], [$]H3[$] where
[$]H1=\sigma(B_{m0}) \frac{d p_{m0,n}}{dB_m} (B_{m1}-B_{m2})[$]
[$]+(\frac{d\sigma(B_{m0})}{dB_m} \frac{d p_{m0,n}}{dB_m}) \int_{B_{m1}}^{B_{m2}} (B-B_{m0}) dB_m[$]
[$]+(\frac{d^2\sigma(B_{m0})}{d{B_m}^2} \frac{d p_{m0,n}}{dB_m}) \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^2}{2} dB_m[$]
[$]+(\frac{d^3\sigma(B_{m0})}{d{B_m}^3} \frac{d p_{m0,n}}{dB_m}) \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^3}{6} dB_m[$]

We immediately identify that
[$]H1=\frac{d p_{m0,n}}{dB_m} \int_{B_{m1}}^{B_{m2}} \sigma(B_m)dB_m[$]
where [$]\frac{d p_{m0,n}}{dB_m}[$] is a constant term and we had already indicated that in our set up [$]\sigma(B_m)[$] means a set of anywhere between 5 to 20 algebraic terms(with constant coefficient multipliers) that can easily be integrated over the grid cell analytically.
Now we identify the second group of terms
[$]H2=( \sigma(B_{m0}) \frac{d^2 p_{m0,n}}{d{B_m}^2}) \int_{B_{m1}}^{B_{m2}} (B-B_{m0}) dB_m[$]
[$]+ \sigma(B_{m0}) \frac{d^3 p_{m0,n}}{d{B_m}^3} \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^2}{2} dB_m[$]
[$]+ \sigma(B_{m0}) \frac{d^4 p_{m0,n}}{d{B_m}^4} \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^3}{6} dB_m[$]
We notice that if we can take [$]\sigma(B_m0)[$] constant and add a first order term in taylor expansion, we can again identify it as a Taylor series of [$]\frac{d p_{m,n}}{dB_m}[$] which can again be analytically integrated since its values(of its integral) at both ends are known. So we have
[$]H2=\sigma(B_{m0}) \int_{B_{m1}}^{B_{m2}} \frac{d p_{m,n}}{dB_m} dB_m- ( \sigma(B_{m0}) \frac{d p_{m0,n}}{dB_m}) \int_{B_{m1}}^{B_{m2}} dB_m[$]
[$]=\sigma(B_{m0}) (p_{m2,n}-p_{m1,n})- ( \sigma(B_{m0}) \frac{d p_{m0,n}}{dB_m}) (B_{m2}- B_{m1})[$]

We identify the third group of terms as
[$]H3=2 \frac{d\sigma(B_{m0})}{dB_m} \frac{d^2 p_{m0,n}}{d{B_m}^2} \int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^2}{2} dB_m[$]
[$]+(3 \frac{d^2\sigma(B_{m0})}{d{B_m}^2} \frac{d^2 p_{m0,n}}{d{B_m}^2} +3 \frac{d\sigma(B_{m0})}{dB_m} \frac{d^3 p_{m0,n}}{d{B_m}^3})\int_{B_{m1}}^{B_{m2}} \frac{(B-B_{m0})^3}{6} dB_m[$]
+Taylor continuation.
We would have to add this group of terms as such one by one and there is no analytic solution for them so We add the terms in group [$]H3[$] individually without any special analytical treatment.
So we can easily solve integral [$]I_2=H1+H2+H3[$]
The integral [$]I_3[$] is very similar to integral [$]I_2[$] so I am not writing how to solve it again.

Coming back to group G3 in the main Taylor expansion, We add the terms in group [$]G3[$] individually without any special analytical treatment.
In a few days, I will be modifying the program to add the above changes.
I will again try the above method and its various variants. It may work since earlier when I was trying this, there were errors in my program elsewhere.  I am trying this since I have seen accuracy of Taylor seriously decrease for very small time delta_t and for small volatility coefficients. I hope to get back later today in the evening with some results.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 5th, 2021, 2:55 pm

I tried some analytic-like methods I had copied in my previous post but accuracy remains comparable to what we obtained with straight Taylor expansion. I have increased the order of expansion from six to eight and that is helpful for another 2-3 digit accuracy but (unweighted) integral of CDF with very small volatility still remains problematic with relatively large grid cells. I am sure the method will work well and would be very useful but some considerations regarding the grid cell size would have to be taken into account in special cases of low volatility. 
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 6th, 2021, 3:03 pm

Those friends who are unfamiliar with mathematica might be daunted by large number of terms in expression for various derivatives. This is very easy in mathematica. For example, you can use the following command in mathematica to calculate various derivatives of Zt with respect to Bm as given in my program.(since Zt=(Bn-Bm-Muw)/Sigmaw) in our setup)

D[(-Bm+Bn-Muw[Bm])/Sigmaw[Bm],{Bm,5}]
(here 5 in parantheses brackets means that you want to calculate fifth derivative given as d5ZtdBm5 in the program.) and mathematica comes up with the expression
-10 ((2 (Sigmaw^\[Prime])[Bm]^2)/Sigmaw[Bm]^3-(Sigmaw^\[Prime]\[Prime])[Bm]/Sigmaw[Bm]^2) (Muw^(3))[Bm]-10 (Muw^\[Prime]\[Prime])[Bm] (-((6 (Sigmaw^\[Prime])[Bm]^3)/Sigmaw[Bm]^4)+(6 (Sigmaw^\[Prime])[Bm] (Sigmaw^\[Prime]\[Prime])[Bm])/Sigmaw[Bm]^3-(Sigmaw^(3))[Bm]/Sigmaw[Bm]^2)+(5 (Sigmaw^\[Prime])[Bm] (Muw^(4))[Bm])/Sigmaw[Bm]^2+5 (-1-(Muw^\[Prime])[Bm]) ((24 (Sigmaw^\[Prime])[Bm]^4)/Sigmaw[Bm]^5-(36 (Sigmaw^\[Prime])[Bm]^2 (Sigmaw^\[Prime]\[Prime])[Bm])/Sigmaw[Bm]^4+(6 (Sigmaw^\[Prime]\[Prime])[Bm]^2)/Sigmaw[Bm]^3+(8 (Sigmaw^\[Prime])[Bm] (Sigmaw^(3))[Bm])/Sigmaw[Bm]^3-(Sigmaw^(4))[Bm]/Sigmaw[Bm]^2)-(Muw^(5))[Bm]/Sigmaw[Bm]+(-Bm+Bn-Muw[Bm]) (-((120 (Sigmaw^\[Prime])[Bm]^5)/Sigmaw[Bm]^6)+(240 (Sigmaw^\[Prime])[Bm]^3 (Sigmaw^\[Prime]\[Prime])[Bm])/Sigmaw[Bm]^5-(90 (Sigmaw^\[Prime])[Bm] (Sigmaw^\[Prime]\[Prime])[Bm]^2)/Sigmaw[Bm]^4-(60 (Sigmaw^\[Prime])[Bm]^2 (Sigmaw^(3))[Bm])/Sigmaw[Bm]^4+(20 (Sigmaw^\[Prime]\[Prime])[Bm] (Sigmaw^(3))[Bm])/Sigmaw[Bm]^3+(10 (Sigmaw^\[Prime])[Bm] (Sigmaw^(4))[Bm])/Sigmaw[Bm]^3-(Sigmaw^(5))[Bm]/Sigmaw[Bm]^2)
You can use "copy as plain text" from mathematica and paste the expression in matlab file. Then you can use "find/replace" command in matlab to convert mathematica symbolic names to variable names used in matlab. For example choose  "(Sigmaw^\[Prime]\[Prime])[Bm]" in find and select "d2SigmawdBm2" in replace and replace in all of the expression. Giving another example, you can replace "(Sigmaw^(3))[Bm]" in above expression to "d3SigmawdBm3" in matlab.
Similarly to find the partial derivatives of CDF of normal density with respect to Bm, you can use the following command in mathematica. For the expression of 4th derivative of CDF Pm0n with respect to Bm, you will use the following command
D[Pm0n[Zt[Bm]], {Bm, 4}]
and it will give the result
3 (Pm0n^\[Prime]\[Prime])[Zt[Bm]] (Zt^\[Prime]\[Prime])[Bm]^2+6 (Zt^\[Prime])[Bm]^2 (Zt^\[Prime]\[Prime])[Bm] (Pm0n^(3))[Zt[Bm]]+4 (Zt^\[Prime])[Bm] (Pm0n^\[Prime]\[Prime])[Zt[Bm]] (Zt^(3))[Bm]+(Zt^\[Prime])[Bm]^4 (Pm0n^(4))[Zt[Bm]]+(Pm0n^\[Prime])[Zt[Bm]] (Zt^(4))[Bm]
Now you choose "copy as plain text" from mathematica and paste it in matlab editor where you will use find/replace command to give your own matlab programming names to mathematica symbolic names. For example you could select "(Pm0n^(4))[Zt[Bm]]" in find and replace it with "d4Pm0ndZt4" throughout the expression. Similarly you could replace "(Zt^\[Prime]\[Prime])[Bm]" with "d2ZtdBm2" which is the programming name I used in matlab for second derivative of Zt with respect to Bm.
To make it intuitively easy to understand the first derivative of CDF with respect to Bm, you use the command
D[Pm0n[Zt[Bm]], {Bm, 1}]
which will give the result
(Pm0n^\[Prime])[Zt[Bm]] (Zt^\[Prime])[Bm]
In matlab, I changed the above mathematica symbolic equation  to  dPm0ndBm= dPm0ndZt * dZtdBm

I am posting another working program that calculates the "unweighted" CDF of normal with variable drift and variable volatility. I wanted to check how the convergence of Taylor series increases as the order of expansion increases, so I calculated the CDF Taylor expansion to 10th order and then compared numbers of zero order, 2nd order, 4th order, 6th order, 8th order, and 10th order. It seems that for difficult integrals even with small cell size of .03125 (which means 32 cells as Bm changes by one) , every second order expansion increases the convergence to true result by one decimal point. You can throw numbers on this program and see how it goes.
function [] = CheckCDFfromSubdivDriftVol08(Bn,Bm0,dB)
%This function integrates gaussian transition CDF(to target point Bn) over 
%a subdivision with centre Bm0 and symmetric boundaries Bm1=Bm0-.5*dB; and 
%Bm2=Bm0+.5*dB;
%And compares it with integration by parts expression

delta_t=.1;
sigma0=.15*sqrt(delta_t);
mu1=.50*delta_t;
beta=.95;
gamma=0.650;%1.65;
%variable vol is defined as Sigmaw=Sigma0*Bm.^gamma;
%variable drift is defined as Muw=mu1*Bm.^beta;
%real drift and vol and their derivatives would be in terms of a sum of large number of terms
%that come out of Ito-hermite expansion.


Bm1=Bm0-.5*dB;
Bm2=Bm0+.5*dB;
dBm=(Bm2-Bm1)/100000;
Integral0=0;
Integral1=0;
Integral2=0;
IntegralVol0=0;
IntegralMu0=0;
for n=1:100001
    if(n==1)
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw)/Sigmaw;
        dSigmawdBm=sigma0*gamma.*BmI.^(gamma-1);
        dMuwdBm=mu1*beta.*BmI.^(beta-1);
        %dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((-BmI + Bn - Muw)* dSigmawdBm)/Sigmaw^2;
        dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((Zt)* dSigmawdBm)/Sigmaw;
        Integral0=Integral0+.5*normcdf(Zt,0,1)*dBm;
    end
    if((n>1)&&(n<100001))
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw)/Sigmaw;
        dSigmawdBm=sigma0*gamma.*BmI.^(gamma-1);
        dMuwdBm=mu1*beta.*BmI.^(beta-1);
        %dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((-BmI + Bn - Muw)* dSigmawdBm)/Sigmaw^2;
        dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((Zt)* dSigmawdBm)/Sigmaw;
        Integral0=Integral0+normcdf(Zt,0,1)*dBm;
    end
    if((n==100001))
        BmI=Bm1+(n-1)*dBm;
        Muw=mu1*BmI.^beta;
        Sigmaw=sigma0*BmI.^gamma;
        Zt=(Bn-BmI-Muw)/Sigmaw;
        dSigmawdBm=sigma0*gamma.*BmI.^(gamma-1);
        dMuwdBm=mu1*beta.*BmI.^(beta-1);
        %dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((-BmI + Bn - Muw)* dSigmawdBm)/Sigmaw^2;
        dZtdBm=(-1 - dMuwdBm )/ Sigmaw - ((Zt)* dSigmawdBm)/Sigmaw;
        Integral0=Integral0+.5*normcdf(Zt,0,1)*dBm;
    end
end



Muw=mu1*Bm0.^beta;
Sigmaw=sigma0*Bm0.^gamma;


Zt=(Bn-Bm0-Muw)/Sigmaw;

Zt2=Zt.*Zt;
Zt3=Zt2.*Zt;
Zt4=Zt3.*Zt;
Zt5=Zt4.*Zt;
Zt6=Zt5.*Zt;
Zt7=Zt6.*Zt;
Zt8=Zt7.*Zt;
Zt9=Zt8.*Zt;
Zt10=Zt9.*Zt;


Pm0n=normcdf(Zt,0,1);
dPm0ndZt=normpdf(Zt,0,1);
d2Pm0ndZt2=-dPm0ndZt*(Zt);
d3Pm0ndZt3=dPm0ndZt*(Zt2-1);
d4Pm0ndZt4=-dPm0ndZt.*(Zt3-3*Zt);
d5Pm0ndZt5=dPm0ndZt.*(Zt4-6*Zt2+3);
d6Pm0ndZt6=-dPm0ndZt.*(Zt5-10*Zt3+15*Zt);
d7Pm0ndZt7=dPm0ndZt.*(Zt6-15*Zt4+45*Zt2-15);
d8Pm0ndZt8=-dPm0ndZt.*(Zt7-21*Zt5+105*Zt3-105*Zt);
d9Pm0ndZt9=dPm0ndZt.*(Zt8-28*Zt6+210*Zt4-420*Zt2+105);
d10Pm0ndZt10=-dPm0ndZt.*(Zt9-36*Zt7+378*Zt5-1260*Zt3+945*Zt);

Sigmaw=sigma0.*Bm0.^(gamma);
dSigmawdBm=sigma0*gamma.*Bm0.^(gamma-1);
d2SigmawdBm2=sigma0*gamma.*(gamma-1).*Bm0.^(gamma-2);
d3SigmawdBm3=sigma0*gamma.*(gamma-1).*(gamma-2).*Bm0.^(gamma-3);
d4SigmawdBm4=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*Bm0.^(gamma-4);
d5SigmawdBm5=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*Bm0.^(gamma-5);
d6SigmawdBm6=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*Bm0.^(gamma-6);
d7SigmawdBm7=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*(gamma-6).*Bm0.^(gamma-7);
d8SigmawdBm8=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*(gamma-6).*(gamma-7).*Bm0.^(gamma-8);
d9SigmawdBm9=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*(gamma-6).*(gamma-7).*(gamma-8).*Bm0.^(gamma-9);
d10SigmawdBm10=sigma0*gamma.*(gamma-1).*(gamma-2).*(gamma-3).*(gamma-4).*(gamma-5).*(gamma-6).*(gamma-7).*(gamma-8).*(gamma-9).*Bm0.^(gamma-10);

Muw=mu1*Bm0.^(beta);
dMuwdBm=mu1*beta.*Bm0.^(beta-1);
d2MuwdBm2=mu1*beta.*(beta-1).*Bm0.^(beta-2);
d3MuwdBm3=mu1*beta.*(beta-1).*(beta-2).*Bm0.^(beta-3);
d4MuwdBm4=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*Bm0.^(beta-4);
d5MuwdBm5=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*Bm0.^(beta-5);
d6MuwdBm6=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*Bm0.^(beta-6);
d7MuwdBm7=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*(beta-6).*Bm0.^(beta-7);
d8MuwdBm8=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*(beta-6).*(beta-7).*Bm0.^(beta-8);
d9MuwdBm9=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*(beta-6).*(beta-7).*(beta-8).*Bm0.^(beta-9);
d10MuwdBm10=mu1*beta.*(beta-1).*(beta-2).*(beta-3).*(beta-4).*(beta-5).*(beta-6).*(beta-7).*(beta-8).*(beta-9).*Bm0.^(beta-10);





Zt=(-Bm0 + Bn - Muw)/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-(Zt.* d3SigmawdBm3)/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- d3SigmawdBm3/Sigmaw^2)- ...
      d4MuwdBm4/Sigmaw+(-Bm0+Bn-Muw).* ((24.* dSigmawdBm.^4)/Sigmaw^5-(36.* dSigmawdBm.^2 .*d2SigmawdBm2)/Sigmaw^4+ ...
      (6.* d2SigmawdBm2.^2)/Sigmaw^3+(8 .*dSigmawdBm .* d3SigmawdBm3)/Sigmaw.^3-d4SigmawdBm4/Sigmaw^2);
  
  
 
 d5ZtdBm5=-10.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d3MuwdBm3-10.* d2MuwdBm2.* ...
     (-((6.* dSigmawdBm^3)/Sigmaw^4)+(6 .*dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3-d3SigmawdBm3/Sigmaw^2)+ ...
     (5.* dSigmawdBm.* d4MuwdBm4)/Sigmaw^2+5.* (-1-dMuwdBm).* ((24.* dSigmawdBm^4)/Sigmaw^5- ...
     (36.* dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3+ ...
     (8.* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-d4SigmawdBm4/Sigmaw^2)-d5MuwdBm5/Sigmaw+(-Bm0+Bn-Muw).* ...
     (-((120.* dSigmawdBm^5)/Sigmaw^6)+(240.* dSigmawdBm^3 .*d2SigmawdBm2)/Sigmaw^5- ...
     (90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4-(60.* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^4+ ...
     (20 .*d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^3+(10.* dSigmawdBm.* d4SigmawdBm4)/Sigmaw^3-d5SigmawdBm5/Sigmaw^2); 
  
  
  
  
  d6ZtdBm6=-20.* d3MuwdBm3.* (-((6 .*dSigmawdBm.^3)/Sigmaw^4)+(6 .* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3- ...
      d3SigmawdBm3/Sigmaw.^2)-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+(8 .*dSigmawdBm .* d3SigmawdBm3)/Sigmaw^3-d4SigmawdBm4/Sigmaw^2)+ ...
      (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- ...
      (60 .*dSigmawdBm.^2.* d3SigmawdBm3)/Sigmaw^4+(20 .* d2SigmawdBm2 .* d3SigmawdBm3)/Sigmaw^3+ ...
      (10.* dSigmawdBm .* d4SigmawdBm4)/Sigmaw^3-d5SigmawdBm5/Sigmaw^2)-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+(480.* dSigmawdBm.^3.* d3SigmawdBm3)/Sigmaw^5- ...
      (360.* dSigmawdBm .*  d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^4+(20 .* d3SigmawdBm3.^2)/Sigmaw^3- ...
      (90.* dSigmawdBm.^2 .* d4SigmawdBm4)/Sigmaw^4+(30.* d2SigmawdBm2 .* d4SigmawdBm4)/Sigmaw^3+ ...
      (12 .* dSigmawdBm .* d5SigmawdBm5)/Sigmaw^3-d6SigmawdBm6/Sigmaw^2);
  
  d7ZtdBm7=-35.* (-((6.* dSigmawdBm^3)/Sigmaw^4)+(6.* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3-d3SigmawdBm3/Sigmaw^2).* d4MuwdBm4- ...
      35.* d3MuwdBm3.* ((24.* dSigmawdBm^4)/Sigmaw^5-(36.* dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3+ ...
      (8.* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-d4SigmawdBm4/Sigmaw^2)-21.* ((2.* dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d5MuwdBm5- ...
      21.* d2MuwdBm2 .*(-((120.* dSigmawdBm^5)/Sigmaw^6)+(240.* dSigmawdBm^3 .*d2SigmawdBm2)/Sigmaw^5-(90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4- ...
      (60.* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^4+(20.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^3+(10.* dSigmawdBm.* d4SigmawdBm4)/Sigmaw^3- ...
      d5SigmawdBm5/Sigmaw^2)+(7.* dSigmawdBm.* d6MuwdBm6)/Sigmaw^2+7 .*(-1-dMuwdBm).* ((720.* dSigmawdBm^6)/Sigmaw^7- ...
      (1800.* dSigmawdBm^4 .*d2SigmawdBm2)/Sigmaw^6+(1080.* dSigmawdBm^2.* d2SigmawdBm2^2)/Sigmaw^5-(90.* d2SigmawdBm2^3)/Sigmaw^4+ ...
      (480.* dSigmawdBm^3 .*d3SigmawdBm3)/Sigmaw^5-(360 .*dSigmawdBm.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^4+ ...
      (20.* d3SigmawdBm3^2)/Sigmaw^3-(90.* dSigmawdBm^2.* d4SigmawdBm4)/Sigmaw^4+(30.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^3+ ...
      (12.* dSigmawdBm.* d5SigmawdBm5)/Sigmaw^3-d6SigmawdBm6/Sigmaw^2)-d7MuwdBm7/Sigmaw+(-Bm0+Bn-Muw) .*(-((5040.* dSigmawdBm^7)/Sigmaw^8)+ ...
      (15120.* dSigmawdBm^5.* d2SigmawdBm2)/Sigmaw^7-(12600 .*dSigmawdBm^3.* d2SigmawdBm2^2)/Sigmaw^6+ ...
      (2520.* dSigmawdBm.* d2SigmawdBm2^3)/Sigmaw^5-(4200 .*dSigmawdBm^4.* d3SigmawdBm3)/Sigmaw^6+ ...
      (5040.* dSigmawdBm^2.* d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^5-(630.* d2SigmawdBm2^2 .*d3SigmawdBm3)/Sigmaw^4- ...
      (420.* dSigmawdBm.* d3SigmawdBm3^2)/Sigmaw^4+(840.* dSigmawdBm^3 .*d4SigmawdBm4)/Sigmaw^5- ...
      (630.* dSigmawdBm.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^4+(70.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^3- ...
      (126.* dSigmawdBm^2.* d5SigmawdBm5)/Sigmaw^4+(42.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^3+ ...
      (14 .*dSigmawdBm .*d6SigmawdBm6)/Sigmaw^3-d7SigmawdBm7/Sigmaw^2);
  
  d8ZtdBm8=-70.* d4MuwdBm4.* ((24.* dSigmawdBm^4)./Sigmaw^5-(36.* dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+ ...
      (6.* d2SigmawdBm2^2)/Sigmaw^3+(8.* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-d4SigmawdBm4/Sigmaw^2)-56.* (-((6.* dSigmawdBm^3)/Sigmaw^4)+ ...
      (6.* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3-d3SigmawdBm3/Sigmaw^2).* d5MuwdBm5-56.* d3MuwdBm3 .*(-((120.* dSigmawdBm^5)/Sigmaw^6)+ ...
      (240.* dSigmawdBm^3.* d2SigmawdBm2)/Sigmaw^5-(90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4-(60.* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^4+ ...
      (20.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^3+(10.* dSigmawdBm.* d4SigmawdBm4)/Sigmaw^3-d5SigmawdBm5/Sigmaw^2)- ...
      28.* ((2 .*dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d6MuwdBm6-28.* d2MuwdBm2.* ((720 .*dSigmawdBm^6)/Sigmaw^7- ...
      (1800.* dSigmawdBm^4.* d2SigmawdBm2)/Sigmaw^6+(1080.* dSigmawdBm^2.* d2SigmawdBm2^2)/Sigmaw^5-(90.* d2SigmawdBm2^3)/Sigmaw^4+ ...
      (480.* dSigmawdBm^3.* d3SigmawdBm3)/Sigmaw^5-(360.* dSigmawdBm .*d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^4+ ...
      (20.* d3SigmawdBm3^2)/Sigmaw^3-(90.* dSigmawdBm^2 .*d4SigmawdBm4)/Sigmaw^4+(30 .*d2SigmawdBm2 .*d4SigmawdBm4)/Sigmaw^3+ ...
      (12.* dSigmawdBm.* d5SigmawdBm5)/Sigmaw^3-d6SigmawdBm6/Sigmaw^2)+(8.* dSigmawdBm .*d7MuwdBm7)/Sigmaw^2+8 .*(-1-dMuwdBm).*...
      (-((5040 .*dSigmawdBm^7)/Sigmaw^8)+(15120.* dSigmawdBm^5.* d2SigmawdBm2)/Sigmaw^7-(12600.* dSigmawdBm^3.* d2SigmawdBm2^2)/Sigmaw^6+ ...
      (2520.* dSigmawdBm.* d2SigmawdBm2^3)/Sigmaw^5-(4200.* dSigmawdBm^4.* d3SigmawdBm3)/Sigmaw^6+ ...
      (5040.* dSigmawdBm^2 .*d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^5-(630.* d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^4- ...
      (420.* dSigmawdBm.* d3SigmawdBm3^2)/Sigmaw^4+(840.* dSigmawdBm^3.* d4SigmawdBm4)/Sigmaw^5- ...
      (630.* dSigmawdBm.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^4+(70.* d3SigmawdBm3 .*d4SigmawdBm4)/Sigmaw^3- ...
      (126 .*dSigmawdBm^2 .*d5SigmawdBm5)/Sigmaw^4+(42.* d2SigmawdBm2 .*d5SigmawdBm5)/Sigmaw^3+ ...
      (14 .*dSigmawdBm .*d6SigmawdBm6)/Sigmaw^3-d7SigmawdBm7/Sigmaw^2)-d8MuwdBm8/Sigmaw+ ...
      (-Bm0+Bn-Muw).* ((40320.* dSigmawdBm^8)/Sigmaw^9-(141120.* dSigmawdBm^6.* d2SigmawdBm2)/Sigmaw^8+ ...
      (151200.* dSigmawdBm^4.* d2SigmawdBm2^2)/Sigmaw^7-(50400.* dSigmawdBm^2 .*d2SigmawdBm2^3)/Sigmaw^6+ ...
      (2520.* d2SigmawdBm2^4)/Sigmaw^5+(40320.* dSigmawdBm^5.* d3SigmawdBm3)/Sigmaw^7-...
      (67200.* dSigmawdBm^3.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^6+(20160 .*dSigmawdBm.* d2SigmawdBm2^2 .*d3SigmawdBm3)/Sigmaw^5+ ...
      (6720.* dSigmawdBm^2.* d3SigmawdBm3^2)/Sigmaw^5-(1680.* d2SigmawdBm2.* d3SigmawdBm3^2)/Sigmaw^4- ...
      (8400.* dSigmawdBm^4.* d4SigmawdBm4)/Sigmaw^6+(10080.* dSigmawdBm^2.* d2SigmawdBm2 .*d4SigmawdBm4)/Sigmaw^5- ...
      (1260 .*d2SigmawdBm2^2.* d4SigmawdBm4)/Sigmaw^4-(1680.* dSigmawdBm .*d3SigmawdBm3 .*d4SigmawdBm4)/Sigmaw^4+ ...
      (70 .*d4SigmawdBm4^2)/Sigmaw^3+(1344 .*dSigmawdBm^3.* d5SigmawdBm5)/Sigmaw^5-(1008 .*dSigmawdBm.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^4+ ...
      (112.* d3SigmawdBm3.* d5SigmawdBm5)/Sigmaw^3-(168.* dSigmawdBm^2.* d6SigmawdBm6)/Sigmaw^4+(56 .*d2SigmawdBm2.* d6SigmawdBm6)/Sigmaw^3+ ...
      (16.* dSigmawdBm.* d7SigmawdBm7)/Sigmaw^3-d8SigmawdBm8/Sigmaw^2);
  
  
  
  
  
  
  d9ZtdBm9=-126.* ((24.* dSigmawdBm^4)/Sigmaw^5-(36 .*dSigmawdBm^2.* d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3+ ...
      (8.* dSigmawdBm.* d3SigmawdBm3)/Sigmaw^3-d4SigmawdBm4/Sigmaw^2).* d5MuwdBm5-126.* d4MuwdBm4.* (-((120 .*dSigmawdBm^5)/Sigmaw^6)+ ...
      (240.* dSigmawdBm^3.* d2SigmawdBm2)/Sigmaw^5-(90.* dSigmawdBm .*d2SigmawdBm2^2)/Sigmaw^4-(60.* dSigmawdBm^2 .*d3SigmawdBm3)/Sigmaw^4+ ...
      (20.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^3+(10.* dSigmawdBm.* d4SigmawdBm4)/Sigmaw^3-d5SigmawdBm5/Sigmaw^2)- ...
      84.* (-((6 .*dSigmawdBm^3)/Sigmaw^4)+(6.* dSigmawdBm .*d2SigmawdBm2)/Sigmaw^3-d3SigmawdBm3/Sigmaw^2).* d6MuwdBm6- ...
      84 .*d3MuwdBm3.* ((720.* dSigmawdBm^6)/Sigmaw^7-(1800.* dSigmawdBm^4 .*d2SigmawdBm2)/Sigmaw^6+ ...
      (1080.* dSigmawdBm^2.* d2SigmawdBm2^2)/Sigmaw^5-(90.* d2SigmawdBm2^3)/Sigmaw^4+(480.* dSigmawdBm^3.* d3SigmawdBm3)/Sigmaw^5- ...
      (360.* dSigmawdBm.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^4+(20.* d3SigmawdBm3^2)/Sigmaw^3-(90.* dSigmawdBm^2.* d4SigmawdBm4)/Sigmaw^4+ ...
      (30.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^3+(12.* dSigmawdBm .*d5SigmawdBm5)/Sigmaw^3-d6SigmawdBm6/Sigmaw^2)- ...
      36.* ((2 .*dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2) .*d7MuwdBm7-36.* d2MuwdBm2 .*(-((5040.* dSigmawdBm^7)/Sigmaw^8)+ ...
      (15120 .*dSigmawdBm^5.* d2SigmawdBm2)/Sigmaw^7-(12600.* dSigmawdBm^3.* d2SigmawdBm2^2)/Sigmaw^6+ ...
      (2520.* dSigmawdBm.* d2SigmawdBm2^3)/Sigmaw^5-(4200.* dSigmawdBm^4 .*d3SigmawdBm3)/Sigmaw^6+ ...
      (5040.* dSigmawdBm^2.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^5-(630 .*d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^4- ...
      (420.* dSigmawdBm.* d3SigmawdBm3^2)/Sigmaw^4+(840.* dSigmawdBm^3.* d4SigmawdBm4)/Sigmaw^5- ...
      (630.* dSigmawdBm.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^4+(70 .*d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^3- ...
      (126.* dSigmawdBm^2.* d5SigmawdBm5)/Sigmaw^4+(42.* d2SigmawdBm2 .*d5SigmawdBm5)/Sigmaw^3+ ...
      (14.* dSigmawdBm.* d6SigmawdBm6)/Sigmaw^3-d7SigmawdBm7/Sigmaw^2)+(9.* dSigmawdBm .*d8MuwdBm8)/Sigmaw^2+ ...
      9 .*(-1-dMuwdBm).* ((40320.* dSigmawdBm^8)/Sigmaw^9-(141120.* dSigmawdBm^6.* d2SigmawdBm2)/Sigmaw^8+ ...
      (151200.* dSigmawdBm^4.* d2SigmawdBm2^2)/Sigmaw^7-(50400.* dSigmawdBm^2 .*d2SigmawdBm2^3)/Sigmaw^6+ ...
      (2520.* d2SigmawdBm2^4)/Sigmaw^5+(40320.* dSigmawdBm^5.* d3SigmawdBm3)/Sigmaw^7- ...
      (67200 .*dSigmawdBm^3.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^6+(20160.* dSigmawdBm .*d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^5+ ...
      (6720.* dSigmawdBm^2.* d3SigmawdBm3^2)/Sigmaw^5-(1680.* d2SigmawdBm2.* d3SigmawdBm3^2)/Sigmaw^4- ...
      (8400 .*dSigmawdBm^4.* d4SigmawdBm4)/Sigmaw^6+(10080.* dSigmawdBm^2.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^5- ...
      (1260.* d2SigmawdBm2^2.* d4SigmawdBm4)/Sigmaw^4-(1680 .*dSigmawdBm .*d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^4+ ...
      (70.* d4SigmawdBm4^2)/Sigmaw^3+(1344.* dSigmawdBm^3.* d5SigmawdBm5)/Sigmaw^5-(1008.* dSigmawdBm.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^4+ ...
      (112 .*d3SigmawdBm3.* d5SigmawdBm5)/Sigmaw^3-(168 .*dSigmawdBm^2.* d6SigmawdBm6)/Sigmaw^4+(56.* d2SigmawdBm2 .*d6SigmawdBm6)/Sigmaw^3+ ...
      (16.* dSigmawdBm .*d7SigmawdBm7)/Sigmaw^3-d8SigmawdBm8/Sigmaw^2)-d9MuwdBm9/Sigmaw+(-Bm0+Bn-Muw).* (-((362880.* dSigmawdBm^9)/Sigmaw^10)+ ...
      (1451520.* dSigmawdBm^7 .*d2SigmawdBm2)/Sigmaw^9-(1905120.* dSigmawdBm^5 .*d2SigmawdBm2^2)/Sigmaw^8+ ...
      (907200 .*dSigmawdBm^3.* d2SigmawdBm2^3)/Sigmaw^7-(113400.* dSigmawdBm.* d2SigmawdBm2^4)/Sigmaw^6- ...
      (423360.* dSigmawdBm^6.* d3SigmawdBm3)/Sigmaw^8+(907200.* dSigmawdBm^4.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^7- ...
      (453600.* dSigmawdBm^2 .*d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^6+(30240.* d2SigmawdBm2^3 .*d3SigmawdBm3)/Sigmaw^5- ...
      (100800.* dSigmawdBm^3.* d3SigmawdBm3^2)/Sigmaw^6+(60480.* dSigmawdBm .*d2SigmawdBm2 .*d3SigmawdBm3^2)/Sigmaw^5- ...
      (1680.* d3SigmawdBm3^3)/Sigmaw^4+(90720.* dSigmawdBm^5 .*d4SigmawdBm4)/Sigmaw^7- ...
      (151200.* dSigmawdBm^3 .*d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^6+(45360.* dSigmawdBm.* d2SigmawdBm2^2.* d4SigmawdBm4)/Sigmaw^5+ ...
      (30240.* dSigmawdBm^2.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^5-(7560.* d2SigmawdBm2.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^4- ...
      (1890 .*dSigmawdBm.* d4SigmawdBm4^2)/Sigmaw^4-(15120.* dSigmawdBm^4.* d5SigmawdBm5)/Sigmaw^6+ ...
      (18144.* dSigmawdBm^2.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^5-(2268.* d2SigmawdBm2^2.* d5SigmawdBm5)/Sigmaw^4- ...
      (3024.* dSigmawdBm .*d3SigmawdBm3.* d5SigmawdBm5)/Sigmaw^4+(252.* d4SigmawdBm4.* d5SigmawdBm5)/Sigmaw^3+ ...
      (2016 .*dSigmawdBm^3.* d6SigmawdBm6)/Sigmaw^5-(1512.* dSigmawdBm.* d2SigmawdBm2.* d6SigmawdBm6)/Sigmaw^4+ ...
      (168 .*d3SigmawdBm3.* d6SigmawdBm6)/Sigmaw^3-(216.* dSigmawdBm^2.* d7SigmawdBm7)/Sigmaw^4+ ...
      (72.* d2SigmawdBm2.* d7SigmawdBm7)/Sigmaw^3+(18.* dSigmawdBm.* d8SigmawdBm8)/Sigmaw^3-d9SigmawdBm9/Sigmaw^2);
  
  d10ZtdBm10=-252.* d5MuwdBm5 .*(-((120.* dSigmawdBm^5)/Sigmaw^6)+(240 .*dSigmawdBm^3.* d2SigmawdBm2)/Sigmaw^5- ...
      (90.* dSigmawdBm.* d2SigmawdBm2^2)/Sigmaw^4-(60.* dSigmawdBm^2.* d3SigmawdBm3)/Sigmaw^4+(20 .*d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^3+ ...
      (10.* dSigmawdBm.* d4SigmawdBm4)/Sigmaw^3-d5SigmawdBm5/Sigmaw^2)-210.* ((24.* dSigmawdBm^4)/Sigmaw^5- ...
      (36.* dSigmawdBm^2 .*d2SigmawdBm2)/Sigmaw^4+(6.* d2SigmawdBm2^2)/Sigmaw^3+(8 .*dSigmawdBm .*d3SigmawdBm3)/Sigmaw^3- ...
      d4SigmawdBm4/Sigmaw^2).* d6MuwdBm6-210.* d4MuwdBm4.* ((720 .*dSigmawdBm^6)/Sigmaw^7-(1800.* dSigmawdBm^4 .*d2SigmawdBm2)/Sigmaw^6+ ...
      (1080.* dSigmawdBm^2.* d2SigmawdBm2^2)/Sigmaw^5-(90.* d2SigmawdBm2^3)/Sigmaw^4+(480.* dSigmawdBm^3 .*d3SigmawdBm3)/Sigmaw^5- ...
      (360.* dSigmawdBm.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^4+(20.* d3SigmawdBm3^2)/Sigmaw^3-(90.* dSigmawdBm^2 .*d4SigmawdBm4)/Sigmaw^4+ ...
      (30.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^3+(12.* dSigmawdBm.* d5SigmawdBm5)/Sigmaw^3-d6SigmawdBm6/Sigmaw^2)- ...
      120.* (-((6.* dSigmawdBm^3)/Sigmaw^4)+(6.* dSigmawdBm.* d2SigmawdBm2)/Sigmaw^3-d3SigmawdBm3/Sigmaw^2) .*d7MuwdBm7- ...
      120.* d3MuwdBm3.* (-((5040 .*dSigmawdBm^7)/Sigmaw^8)+(15120.* dSigmawdBm^5.* d2SigmawdBm2)/Sigmaw^7- ...
      (12600.* dSigmawdBm^3.* d2SigmawdBm2^2)/Sigmaw^6+(2520.* dSigmawdBm.* d2SigmawdBm2^3)/Sigmaw^5- ...
      (4200.* dSigmawdBm^4.* d3SigmawdBm3)/Sigmaw^6+(5040 .*dSigmawdBm^2.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^5- ...
      (630.* d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^4-(420.* dSigmawdBm.* d3SigmawdBm3^2)/Sigmaw^4+ ...
      (840.* dSigmawdBm^3.* d4SigmawdBm4)/Sigmaw^5-(630.* dSigmawdBm.* d2SigmawdBm2 .*d4SigmawdBm4)/Sigmaw^4+ ...
      (70.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^3-(126 .*dSigmawdBm^2.* d5SigmawdBm5)/Sigmaw^4+(42.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^3+ ...
      (14.* dSigmawdBm.* d6SigmawdBm6)/Sigmaw^3-d7SigmawdBm7/Sigmaw^2)-45.* ((2 .*dSigmawdBm^2)/Sigmaw^3-d2SigmawdBm2/Sigmaw^2).* d8MuwdBm8- ...
      45.* d2MuwdBm2.* ((40320 .*dSigmawdBm^8)/Sigmaw^9-(141120.* dSigmawdBm^6 .*d2SigmawdBm2)/Sigmaw^8+ ...
      (151200.* dSigmawdBm^4.* d2SigmawdBm2^2)/Sigmaw^7-(50400 .*dSigmawdBm^2 .*d2SigmawdBm2^3)/Sigmaw^6+(2520.* d2SigmawdBm2^4)/Sigmaw^5+ ...
      (40320.* dSigmawdBm^5.* d3SigmawdBm3)/Sigmaw^7-(67200 .*dSigmawdBm^3.* d2SigmawdBm2.* d3SigmawdBm3)/Sigmaw^6+ ...
      (20160.* dSigmawdBm.* d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^5+(6720.* dSigmawdBm^2.* d3SigmawdBm3^2)/Sigmaw^5- ...
      (1680 .*d2SigmawdBm2.* d3SigmawdBm3^2)/Sigmaw^4-(8400.* dSigmawdBm^4 .*d4SigmawdBm4)/Sigmaw^6+ ...
      (10080 .*dSigmawdBm^2.* d2SigmawdBm2.* d4SigmawdBm4)/Sigmaw^5-(1260.* d2SigmawdBm2^2 .*d4SigmawdBm4)/Sigmaw^4- ...
      (1680 .*dSigmawdBm.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^4+(70 .*d4SigmawdBm4^2)/Sigmaw^3+(1344.* dSigmawdBm^3.* d5SigmawdBm5)/Sigmaw^5- ...
      (1008.* dSigmawdBm.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^4+(112.* d3SigmawdBm3 .*d5SigmawdBm5)/Sigmaw^3- ...
      (168.* dSigmawdBm^2.* d6SigmawdBm6)/Sigmaw^4+(56.* d2SigmawdBm2.* d6SigmawdBm6)/Sigmaw^3+(16.* dSigmawdBm .*d7SigmawdBm7)/Sigmaw^3-d8SigmawdBm8/Sigmaw^2)+ ...
      (10.* dSigmawdBm.* d9MuwdBm9)/Sigmaw^2+10 .*(-1-dMuwdBm).* (-((362880 .*dSigmawdBm^9)/Sigmaw^10)+(1451520.* dSigmawdBm^7 .*d2SigmawdBm2)/Sigmaw^9- ...
      (1905120.* dSigmawdBm^5.* d2SigmawdBm2^2)/Sigmaw^8+(907200.* dSigmawdBm^3.* d2SigmawdBm2^3)/Sigmaw^7- ...
      (113400.* dSigmawdBm.* d2SigmawdBm2^4)/Sigmaw^6-(423360.* dSigmawdBm^6 .*d3SigmawdBm3)/Sigmaw^8+ ...
      (907200.* dSigmawdBm^4.* d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^7-(453600 .*dSigmawdBm^2.* d2SigmawdBm2^2.* d3SigmawdBm3)/Sigmaw^6+ ...
      (30240.* d2SigmawdBm2^3.* d3SigmawdBm3)/Sigmaw^5-(100800.* dSigmawdBm^3.* d3SigmawdBm3^2)/Sigmaw^6+ ...
      (60480.* dSigmawdBm.* d2SigmawdBm2.* d3SigmawdBm3^2)/Sigmaw^5-(1680.* d3SigmawdBm3^3)/Sigmaw^4+ ...
      (90720 .*dSigmawdBm^5 .*d4SigmawdBm4)/Sigmaw^7-(151200.* dSigmawdBm^3.* d2SigmawdBm2 .*d4SigmawdBm4)/Sigmaw^6+ ...
      (45360.* dSigmawdBm.* d2SigmawdBm2^2.* d4SigmawdBm4)/Sigmaw^5+(30240.* dSigmawdBm^2 .*d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^5- ...
      (7560.* d2SigmawdBm2.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^4-(1890.* dSigmawdBm.* d4SigmawdBm4^2)/Sigmaw^4- ...
      (15120 .*dSigmawdBm^4.* d5SigmawdBm5)/Sigmaw^6+(18144.* dSigmawdBm^2.* d2SigmawdBm2 .*d5SigmawdBm5)/Sigmaw^5- ...
      (2268 .*d2SigmawdBm2^2.* d5SigmawdBm5)/Sigmaw^4-(3024.* dSigmawdBm.* d3SigmawdBm3.* d5SigmawdBm5)/Sigmaw^4+ ...
      (252.* d4SigmawdBm4.* d5SigmawdBm5)/Sigmaw^3+(2016 .*dSigmawdBm^3.* d6SigmawdBm6)/Sigmaw^5- ...
      (1512.* dSigmawdBm.* d2SigmawdBm2.* d6SigmawdBm6)/Sigmaw^4+(168.* d3SigmawdBm3.* d6SigmawdBm6)/Sigmaw^3- ...
      (216 .*dSigmawdBm^2.* d7SigmawdBm7)/Sigmaw^4+(72.* d2SigmawdBm2.* d7SigmawdBm7)/Sigmaw^3+(18.* dSigmawdBm.* d8SigmawdBm8)/Sigmaw^3- ...
      d9SigmawdBm9/Sigmaw^2)-d10MuwdBm10/Sigmaw+(-Bm0+Bn-Muw) .*((3628800.* dSigmawdBm^10)/Sigmaw^11- ...
      (16329600.* dSigmawdBm^8.* d2SigmawdBm2)/Sigmaw^10+(25401600.* dSigmawdBm^6 .*d2SigmawdBm2^2)/Sigmaw^9- ...
      (15876000 .*dSigmawdBm^4.* d2SigmawdBm2^3)/Sigmaw^8+(3402000.* dSigmawdBm^2 .*d2SigmawdBm2^4)/Sigmaw^7- ...
      (113400.* d2SigmawdBm2^5)/Sigmaw^6+(4838400.* dSigmawdBm^7.* d3SigmawdBm3)/Sigmaw^9- ...
      (12700800.* dSigmawdBm^5.* d2SigmawdBm2 .*d3SigmawdBm3)/Sigmaw^8+(9072000.* dSigmawdBm^3 .*d2SigmawdBm2^2 .*d3SigmawdBm3)/Sigmaw^7- ...
      (1512000.* dSigmawdBm.* d2SigmawdBm2^3.* d3SigmawdBm3)/Sigmaw^6+(1512000 .*dSigmawdBm^4.* d3SigmawdBm3^2)/Sigmaw^7- ...
      (1512000 .*dSigmawdBm^2.* d2SigmawdBm2.* d3SigmawdBm3^2)/Sigmaw^6+(151200.* d2SigmawdBm2^2 .*d3SigmawdBm3^2)/Sigmaw^5+ ...
      (67200.* dSigmawdBm.* d3SigmawdBm3^3)/Sigmaw^5-(1058400 .*dSigmawdBm^6.* d4SigmawdBm4)/Sigmaw^8+ ...
      (2268000.* dSigmawdBm^4.* d2SigmawdBm2 .*d4SigmawdBm4)/Sigmaw^7-(1134000.* dSigmawdBm^2.* d2SigmawdBm2^2.* d4SigmawdBm4)/Sigmaw^6+ ...
      (75600 .*d2SigmawdBm2^3.* d4SigmawdBm4)/Sigmaw^5-(504000.* dSigmawdBm^3.* d3SigmawdBm3 .*d4SigmawdBm4)/Sigmaw^6+ ...
      (302400 .*dSigmawdBm.* d2SigmawdBm2.* d3SigmawdBm3.* d4SigmawdBm4)/Sigmaw^5- ...
      (12600.* d3SigmawdBm3^2.* d4SigmawdBm4)/Sigmaw^4+(37800.* dSigmawdBm^2.* d4SigmawdBm4^2)/Sigmaw^5- ...
      (9450.* d2SigmawdBm2.* d4SigmawdBm4^2)/Sigmaw^4+(181440.* dSigmawdBm^5 .*d5SigmawdBm5)/Sigmaw^7- ...
      (302400.* dSigmawdBm^3.* d2SigmawdBm2.* d5SigmawdBm5)/Sigmaw^6+(90720.* dSigmawdBm.* d2SigmawdBm2^2.* d5SigmawdBm5)/Sigmaw^5+ ...
      (60480.* dSigmawdBm^2.* d3SigmawdBm3.* d5SigmawdBm5)/Sigmaw^5-(15120 .*d2SigmawdBm2 .*d3SigmawdBm3 .*d5SigmawdBm5)/Sigmaw^4- ...
      (7560.* dSigmawdBm.* d4SigmawdBm4 .*d5SigmawdBm5)/Sigmaw^4+(252 .*d5SigmawdBm5^2)/Sigmaw^3- ...
      (25200.* dSigmawdBm^4.* d6SigmawdBm6)/Sigmaw^6+(30240.* dSigmawdBm^2 .*d2SigmawdBm2.* d6SigmawdBm6)/Sigmaw^5- ...
      (3780.* d2SigmawdBm2^2.* d6SigmawdBm6)/Sigmaw^4-(5040.* dSigmawdBm.* d3SigmawdBm3.* d6SigmawdBm6)/Sigmaw^4+ ...
      (420.* d4SigmawdBm4.* d6SigmawdBm6)/Sigmaw^3+(2880 .*dSigmawdBm^3.* d7SigmawdBm7)/Sigmaw^5- ...
      (2160 .*dSigmawdBm .*d2SigmawdBm2.* d7SigmawdBm7)/Sigmaw^4+(240.* d3SigmawdBm3.* d7SigmawdBm7)/Sigmaw^3- ...
      (270.* dSigmawdBm^2.* d8SigmawdBm8)/Sigmaw^4+(90.* d2SigmawdBm2.* d8SigmawdBm8)/Sigmaw^3+(20 .*dSigmawdBm.* d9SigmawdBm9)/Sigmaw^3- ...
      d10SigmawdBm10/Sigmaw^2);
  
dPm0ndBm=dPm0ndZt.*dZtdBm;
d2Pm0ndBm2=d2Pm0ndZt2.*dZtdBm.^2+dPm0ndZt.*d2ZtdBm2;
d3Pm0ndBm3=d3Pm0ndZt3.*dZtdBm.^3+3*d2Pm0ndZt2.*dZtdBm.*d2ZtdBm2+dPm0ndZt.*d3ZtdBm3;
d4Pm0ndBm4=d4Pm0ndZt4.*dZtdBm.^4+6*d3Pm0ndZt3.*dZtdBm.^2.*d2ZtdBm2+4*d2Pm0ndZt2.*dZtdBm.*d3ZtdBm3+ ...
    3*d2Pm0ndZt2.*d2ZtdBm2.^2+dPm0ndZt.*d4ZtdBm4;
d5Pm0ndBm5=d5Pm0ndZt5.*dZtdBm.^5+10*d4Pm0ndZt4.*dZtdBm.^3.*d2ZtdBm2+15*d3Pm0ndZt3.*dZtdBm.*d2ZtdBm2.^2+ ...
    10*d3Pm0ndZt3.*dZtdBm.^2.*d3ZtdBm3+10*d2Pm0ndZt2.*d2ZtdBm2.*d3ZtdBm3+ ...
    5*d2Pm0ndZt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(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);
dBm9=1/10* ((Bm2 - Bm0)^10 - (Bm1 - Bm0)^10)/(720*7*8*9);
dBm10=1/11* ((Bm2 - Bm0)^11 - (Bm1 - Bm0)^11)/(720*7*8*9*10);




% IntegralAnalytic=Pm0n.*dBm + ...
%     +(dPm0ndBm)*dBm1+ ...
%     +(d2Pm0ndBm2) *dBm2 + ...  
%     +(d3Pm0ndBm3) *dBm3 + ...
%     +(d4Pm0ndBm4)*dBm4+ ...
%     +(d5Pm0ndBm5)*dBm5+ ...
%     +(d6Pm0ndBm6)*dBm6 + ...
%     (d7Pm0ndBm7)*dBm7 + ...
%     (d8Pm0ndBm8)*dBm8 + ...
%     (d9Pm0ndBm9)*dBm9 + ...
%     (d10Pm0ndBm10)*dBm10;
IntegralAnalyticO0=Pm0n.*dBm;

IntegralAnalyticO2=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
    +(d2Pm0ndBm2) *dBm2;   

IntegralAnalyticO4=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
    +(d2Pm0ndBm2) *dBm2 + ...  
    +(d3Pm0ndBm3) *dBm3 + ...
    +(d4Pm0ndBm4)*dBm4;

IntegralAnalyticO6=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
    +(d2Pm0ndBm2) *dBm2 + ...  
    +(d3Pm0ndBm3) *dBm3 + ...
    +(d4Pm0ndBm4)*dBm4+ ...
    +(d5Pm0ndBm5)*dBm5+ ...
    +(d6Pm0ndBm6)*dBm6;

IntegralAnalyticO8=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
  +(d2Pm0ndBm2) *dBm2 + ...  
    +(d3Pm0ndBm3) *dBm3 + ...
    +(d4Pm0ndBm4)*dBm4+ ...
    +(d5Pm0ndBm5)*dBm5+ ...
    +(d6Pm0ndBm6)*dBm6 + ...
    (d7Pm0ndBm7)*dBm7 + ...
    (d8Pm0ndBm8)*dBm8;

IntegralAnalyticO10=Pm0n.*dBm + ...
    +(dPm0ndBm)*dBm1+ ...
    +(d2Pm0ndBm2) *dBm2 + ...  
    +(d3Pm0ndBm3) *dBm3 + ...
    +(d4Pm0ndBm4)*dBm4+ ...
    +(d5Pm0ndBm5)*dBm5+ ...
    +(d6Pm0ndBm6)*dBm6 + ...
    (d7Pm0ndBm7)*dBm7 + ...
    (d8Pm0ndBm8)*dBm8 + ...
    (d9Pm0ndBm9)*dBm9 + ...
    (d10Pm0ndBm10)*dBm10;


 

IntegralAnalyticO0
IntegralAnalyticO2
IntegralAnalyticO4
IntegralAnalyticO6
IntegralAnalyticO8
IntegralAnalyticO10
Integral0


end
Here is the result of this program when run by command>> CheckCDFfromSubdivDriftVol08(.8,.75,.03125)
IntegralAnalyticO0 =
   0.019356181958274
IntegralAnalyticO2 =
   0.019272312682345
IntegralAnalyticO4 =
   0.019273921209310
IntegralAnalyticO6 =
   0.019273899789850
IntegralAnalyticO8 =
   0.019273899942692
IntegralAnalyticO10 =
   0.019273899943666
Integral0 =
   0.019273899943599
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 8th, 2021, 4:21 am

This morning, I went ahead and wrote the program for simulation of SDEs in Bessel form with only variable drift. This was very simple since a lot of derivatives became extremely easy. In my program, I have advanced the density by 32 steps with a very small step and later did a large step of dt2=1/16=.0625; In coming days, I will be adding many more things to the program. It will become multistep and I will add second hermite polynomial for the sake of accuracy in volatility. It is also very simple to increase the order of expansion since higher derivatives are very inexpensive as compared to the case when both drift and volatility are variable. The current expansion order is six. 
These are a few graphs I quickly made with my program. I want to make many changes in the program especially making it perfectly workable close to zero which is still not the case. I hope to post another improved program within next 3 days but I will get an injection today so I am still afraid that I may not be able to work properly for next few days but I will try.
Here are the graphs and I am posting the program in next post.
Image

Image

Image

Image

Image

Image

Image

Image

Image

Image
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 8th, 2021, 4:30 am

The main program.
.
function [] = SDETransProb08WmtGrid200b53Drift()

%Copyright Ahsan Amin. Infiniti derivatives Technologies.
%Please fell free to connect on linkedin: linkedin.com/in/ahsan-amin-0a53334 
%or skype ahsan.amin2999
%In this program, I am simulating the SDE given as
%dy(t)=mu1 x(t)^beta1 dt + mu2 x(t)^beta2 dt +sigma x(t)^gamma dz(t)

%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=.5;%50;   % volatility power.                                                                                                                                                                                                                                                                     
kappa=8.0;%.950;   %mean reversion parameter.
theta=.25;%mean reversion target
sigma0=1.250;%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');
            
            %Below calculate first four derivatives. Please replace with your favorite
            %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;
            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);

            %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 ;
            d5ZdB5(1:NN1)=0.0;
            d6ZdB6(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);
   
    
            %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
            %we had subtracted.
            
            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,SigmawA] = 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),cc1(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),wwMu0dt(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.
            
            c1(1:Nn)=sigma0*sqrt(dt);
            SigmaB(1:Nn)=sqrt(dBdZ_0(1:Nn).^2+ c1(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;
            for mm=1:NN1
                
                [Pmn] = CalculateCDFArraySignIndexDrift(Bn,Nn,wMu0dtA(mm),dwMu0dtdwA(mm),d2wMu0dtdw2A(mm),d3wMu0dtdw3A(mm),d4wMu0dtdw4A(mm),d5wMu0dtdw5A(mm),d6wMu0dtdw6A(mm),SigmawA(mm),Bm0(mm),Bm1(mm),Bm2(mm),ZZProb(mm),pm0(mm),dpm0(mm),d2pm0(mm),d3pm0(mm),d4pm0(mm),d5pm0(mm));
                %Pn is total CDF at the point Bn while Pmn is contribution
                %to CDF from mth grid cell at previous time point.
                Pn(1:Nn)=Pn(1:Nn)+Pmn(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));    
%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)
       

      
%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
.
Sub Function one.
.
function [Pmn] = CalculateCDFArraySignIndexDrift(Bn,Nn,Muw,dMuwdBm,d2MuwdBm2,d3MuwdBm3,d4MuwdBm4,d5MuwdBm5,d6MuwdBm6,Sigmaw,Bm0,Bm1,Bm2,dPm,pm0,dpm0,d2pm0,d3pm0,d4pm0,d5pm0)

%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:nn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tdBm2(nn1:nn2).^3+ ...
    +60*d3Pm0ndZt3(nn1:nn2).*dZtdBm(nn1:nn2).*d2ZtdBm2(nn1:nn2).*d3ZtdBm3(nn1:nn2)+15*d3Pm0ndZt3(nn1:nn2).*dZtdBm(nn1:nn2).^2.*d4ZtdBm4(nn1:nn2)+ ...
    +10*d2Pm0ndZt2(nn1:nn2).*d3ZtdBm3(nn1:nn2).^2+15*d2Pm0ndZt2(nn1:nn2).*d2ZtdBm2(nn1:nn2).*d4ZtdBm4(nn1:nn2)+ ...
    6*d2Pm0ndZt2(nn1:nn2).*dZtdBm(nn1:nn2).*d5ZtdBm5(nn1:nn2)+dPm0ndZt(nn1:nn2).*d6ZtdBm6(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;


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;

Pmn(nn1:nn2)=IntegralCDF(nn1:nn2);
if(nn1>1)
Pmn(1:nn1-1)=0;
end
if (nn2<Nn)
Pmn(nn2+1:Nn)=dPm;
end
Pmn
%str=input('Look at Pmn');
end

.
 
User avatar
Amin
Topic Author
Posts: 2695
Joined: July 14th, 2002, 3:00 am

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

April 8th, 2021, 4:35 am

Sub Function two.
.
function [wMu0dt,dwMu0dtdw,d2wMu0dtdw2,d3wMu0dtdw3,d4wMu0dtdw4,d5wMu0dtdw5,d6wMu0dtdw6,c1] = 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,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;

   
   


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

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


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

   
   d6wMu0dtdw6(wnStart:Nn)=(1-gamma)^5.* ...
    ((YqCoeff0(1,1,2,1).*Fp1(1,1,2,1).*(-1+Fp2(1,1,2,1)).*(-2+Fp2(1,1,2,1)).*(-3+Fp2(1,1,2,1)).*(-4+Fp2(1,1,2,1)).*(-5+Fp2(1,1,2,1)).*((1-gamma).*w(wnStart:Nn)).^(-6+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)).*(-3+Fp2(1,2,1,1)).*(-4+Fp2(1,2,1,1)).*(-5+Fp2(1,2,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-6+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)).*(-3+Fp2(2,1,1,1)).*(-4+Fp2(2,1,1,1)).*(-5+Fp2(2,1,1,1)).*((1-gamma).*w(wnStart:Nn)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-6+Fp2(4,2,1,1))+ ...
       YqCoeff0(5,1,1,1).*Fp1(5,1,1,1).*(-1+Fp2(5,1,1,1)).*(-2+Fp2(5,1,1,1)).*(-3+Fp2(5,1,1,1)).*(-4+Fp2(5,1,1,1)).*(-5+Fp2(5,1,1,1)).*((1-gamma).*w(wnStart:Nn)).^(-6+Fp2(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
.
Sub Function 3.
.
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

.

Here is the output of the program.  You should see exactly the last graph two posts ago.
MCMean =
   0.604049346265947
MCVar =
   0.055687000767541
Original process average from our simulation
ItoHermiteMean =
   0.605016573179303
ItoHermiteVar =
   0.056959957487800
true Mean only applicble to standard SV mean reverting type models otherwise disregard
TrueMean =
   0.604274914555761
IndexMax =
    55
red line is density of SDE from Ito-Hermite method, green is monte carlo.>> 



Again, I posted this program relatively prematurely since I wanted to make a few further changes and write comments before posting it but I have to get an injection today so I quickly posted everything. If things go well, I will make another post with a far better progam with proper comments in another 3-4 days. I am looking to make it multi-step, possibly increase order of expansion and add the effect of second hermite polynomial for more accuracy, make it better at zero and also improve structure of the program while removing uncecessary things I carried forward from previous program.