I am posting most of the functions required to run the program. Functions recently posted might not be included unless I changed them. If you cannot find any function, please search for it on the thread using search functionality.
.
.
.
function []= ConditionalDensityHermitesBivariateNewtonIters02()
%SV SDE is
%dV(t)=mu1 V(t)^beta1 dt+ mu2 V(t)^beta2 dt + sigma0 V(t)^gamma dZ(t)
%In mean reverting SDEs we ususally have
%mu1= kappaV * thetaV
%beta1=0
%mu2=-kappa
%beta2=0
%NDim=4;%Three assets and one SV.
%%%%%%%%%%%%%%%%%%%5
V0=1.00;%.32;
V00=V0;
thetaV=1.250;%.045;%1;%.04;
kappaV=1.05;%1.5;%1.5;
mu1=kappaV*thetaV;
mu2=-kappaV;
beta1=0;
beta2=1;
gamma=.9;%.950;
sigma0=.5%.45;%
dt=.03125/2;
Tt=64*2;
T=Tt*dt;
seed0=52130649;
rng(seed0, 'twister')
paths=25000;
V(1:paths,1)=V0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
TT1=30; %Transition distribution start
TT2=124; %Transition distribution end
Random2(1:paths/2,1)=0;
for tt=1:Tt
Random2(1:paths/2)=randn(paths/2,1);
Random2(paths/2+1:paths)=-Random2(1:paths/2);
V(1:paths,1)=V(1:paths,1)+ ...
(mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2)*dt + ...
sigma0*V(1:paths,1).^gamma .*Random2(1:paths,1)*sqrt(dt) + ...
(mu1.*beta1*V(1:paths,1).^(beta1-1) + mu2.*beta2.*V(1:paths,1).^(beta2-1)).* ...
((mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2)*dt^2/2 + ...
sigma0*V(1:paths,1).^gamma .*Random2(1:paths,1)*(1-1/sqrt(3))*dt^1.5) + ...
.5*(mu1.*beta1.*(beta1-1).*V(1:paths,1).^(beta1-2) + mu2.*beta2.*(beta2-1).*V(1:paths,1).^(beta2-2)).* ...
sigma0^2.*V(1:paths,1).^(2*gamma).*dt^2/2 + ...
sigma0*gamma*V(1:paths,1).^(gamma-1) .* ...
((mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2).*Random2(1:paths,1).*1/sqrt(3)*dt^1.5 + ...
sigma0.*V(1:paths,1).^gamma .*(Random2(1:paths,1).^2-1)*dt/2) + ...
.5*sigma0*gamma*(gamma-1).*V(1:paths,1).^(gamma-2) .* ...
sigma0^2.*V(1:paths,1).^(2*gamma) .*Random2(1:paths,1).*1/sqrt(3)*dt^1.5;
V(V<0)=.00001;
if(tt==TT1)
Xin(1:paths,1)=V(1:paths,1);
end
if(tt==TT2)
Yin(1:paths,1)=V(1:paths,1);
end
end
str=input("I have reached stone 1");
Order=5;
%The function below does initial analytics of caclulation of 2D conditional
%and joint densities. We want to use the result from this function as a
%Starting guess/seed to Newton derivatives based optimization method.
[YCoeffHH,XYCoeffHH,XCoeffH,Coeffyx,YCoeffH] =CalculateBivariateHermiteSeries01(Yin,Xin,Order);
for qq1=1:Order+1
XMoments(qq1)=sum(Xin(1:paths).^qq1)/paths;
end
[XCoeffZ(1:Order+1)] = ConvertHermiteSeriesToZSeries01(XCoeffH(1:Order+1),Order);
[XCoeffZ(1),XCoeffZ(2:6)] = CalculateZSeriesDensityFromRawMomentsM6(XMoments,XCoeffZ(2:6));
YCoeffHH(1:Order+1,Order:Order+1)=YCoeffHH(1:Order+1,Order:Order+1)/100000;
YCoeffHH(Order:Order+1,1:Order+1)=YCoeffHH(Order:Order+1,1:Order+1)/100000;
%The above 2D Conditional density calculated from regression is used as an
%input seed to Newton method. YCoeffHH is input as a seed to Newton method
%after converting it to a 2D Z-series.
str=input("I have reached stone 2");
%Below, we calculate Target cross-moments of Y and X from data.
for pp1=1:Order+1
for qq1=1:Order+1
MomentsYX0((pp1-1)*(Order+1)+qq1,1)=sum(Yin(1:paths).^pp1.*Xin(1:paths).^qq1)/paths;
end
end
% for pp1=1:Order+1
% pp4=6;
% pp3=5;
% pp2=4;
% pp1=3;
% if((MomentsYX0((pp4-1)*(Order+1)+qq1,1)/MomentsYX0((pp3-1)*(Order+1)+qq1,1))<(MomentsYX0((pp2-1)*(Order+1)+qq1,1)/MomentsYX0((pp1-1)*(Order+1)+qq1,1)))
% MomentsYX0((pp4-1)*(Order+1)+qq1,1)=MomentsYX0((pp3-1)*(Order+1)+qq1,1)*(MomentsYX0((pp2-1)*(Order+1)+qq1,1)/MomentsYX0((pp1-1)*(Order+1)+qq1,1));
%
% str=input("Moments are being altered")
% end
% %for qq1=1:Order+1
% % MomentsYX0((pp1-1)*(Order+1)+qq1,1)=sum(Yin(1:paths).^pp1.*Xin(1:paths).^qq1)/paths;
% %end
% end
%Before we enter Newton optimization function, we want to convert 2D and 1D hermite
%Series to Z-series.
[XCoeffZ(1:Order+1)] = ConvertHermiteSeriesToZSeries01(XCoeffH(1:Order+1),Order);
[YCoeffZ(1:Order+1)] = ConvertHermiteSeriesToZSeries01(YCoeffH(1:Order+1),Order);
[YCoeffZZ] = Convert2DHermitesInto2DSeriesNew(YCoeffHH,Order,Order);
OrderY=Order;
OrderX=Order;
[Y1DMoments]=CalculateMomentsOfZSeries(YCoeffZ(1),YCoeffZ(2:6),5,6)
MaxIter0=30;
YCoeffZZ=CalculateSmoothingGuessDensity2D(YCoeffZZ,XCoeffZ,MomentsYX0,Y1DMoments,OrderY,OrderX,MaxIter0)
str=input("Look at smoothing results")
MaxIter=3000;
[YCoeffZZ,AMat] = DoNewtonIterationsOn2DZSeries(YCoeffZZ,XCoeffZ,Order,MomentsYX0,Y1DMoments,MaxIter);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
% Order=7
% YCoeffHH(1:Order+1,Order:Order+1)=0.0;%YCoeffHH(1:Order+1,Order:Order+1)/100000;
% YCoeffHH(Order:Order+1,1:Order+1)=0.0;%YCoeffHH(Order:Order+1,1:Order+1)/100000;
%
% for pp1=1:Order+1
% for qq1=1:Order+1
% MomentsYX0((pp1-1)*(Order+1)+qq1,1)=sum(Yin(1:paths).^pp1.*Xin(1:paths).^qq1)/paths;
% end
% end
%
%MaxIter0=30;
%YCoeffZZ=CalculateSmoothingGuessDensity2D(YCoeffZZ,XCoeffZ,MomentsYX0,Y1DMoments,OrderY,OrderX,MaxIter0);
%str=input("Look at smoothing results")
%MaxIter=200;
%[YCoeffZZ,AMat] = DoNewtonIterationsOn2DZSeries(YCoeffZZ,XCoeffZ,Order,MomentsYX0,Y1DMoments,MaxIter);
%YCoeffZZ is the 2D conditional Z-series of Y given X [Y|X](Zy,Zx)
%that is the result of optimization method and is calibrated to Cross-moments of Y and X.
%MaxIter0=30;
%YCoeffZZ=CalculateSmoothingGuessDensity2D(YCoeffZZ,XCoeffZ,MomentsYX0,Y1DMoments,OrderY,OrderX,MaxIter0);
%str=input("Look at smoothing---3 results")
OrderY=Order;
OrderX=Order;
NMoments=6;
[MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZ,OrderY,OrderX)
%The name of above function is slightly misleading. It calculates mean and
%second moment
[Y1DMoments]=CalculateMomentsOfZSeries(YCoeffZ(1),YCoeffZ(2:6),5,6)
[YMoments] = CalculateMomentsOf2DZSeriesY(YCoeffZZ,OrderY,OrderX,NMoments);
Y1DMoments
YMoments
%Y1DMoments(2)-Y1DMoments(1).^2
%VarZZ
str=input("Look at comparison of second moments/moments");
YCoeffZZ
%AMat
%MomentsYX0
%Below Check the difference between model moments and target moments.
%AMat-MomentsYX0
%Below, we convert the 2D Conditional Z-series of [Y|X](Zy,Zx) into 2D
%Conditional Hermite Seiries of [Y|X](Zy,Zx).
[YCoeffHH] = ConvertZSeriesToHermiteSeries2D(YCoeffZZ,Order+1,Order+1)
% MMul1=sqrt(YCoeffHH(1,1).^2+YCoeffHH(1,2).^2+YCoeffHH(1,3).^2*2+YCoeffHH(1,4).^2*6+YCoeffHH(1,5).^2*24+YCoeffHH(1,6).^2*120)
% YCoeffH(1)
%
% %YCoeffHH(1,:)=YCoeffHH(1,:)*abs(YCoeffH(1))./MMul1;
%
%
% MMul2=sqrt(YCoeffHH(2,1).^2+YCoeffHH(2,2).^2+YCoeffHH(2,3).^2*2+YCoeffHH(2,4).^2*6+YCoeffHH(2,5).^2*24+YCoeffHH(2,6).^2*120)
% YCoeffH(2)
%
% MMul3=sqrt(YCoeffHH(3,1).^2+YCoeffHH(3,2).^2+YCoeffHH(3,3).^2*2+YCoeffHH(3,4).^2*6+YCoeffHH(3,5).^2*24+YCoeffHH(3,6).^2*120)
% YCoeffH(3)
%
%
% MMul4=sqrt(YCoeffHH(4,1).^2+YCoeffHH(4,2).^2+YCoeffHH(4,3).^2*2+YCoeffHH(4,4).^2*6+YCoeffHH(4,5).^2*24+YCoeffHH(4,6).^2*120)
% YCoeffH(4)
%
% MMul5=sqrt(YCoeffHH(5,1).^2+YCoeffHH(5,2).^2+YCoeffHH(5,3).^2*2+YCoeffHH(5,4).^2*6+YCoeffHH(5,5).^2*24+YCoeffHH(5,6).^2*120)
% YCoeffH(5)
%
%
% MMul6=sqrt(YCoeffHH(6,1).^2+YCoeffHH(6,2).^2+YCoeffHH(6,3).^2*2+YCoeffHH(6,4).^2*6+YCoeffHH(6,5).^2*24+YCoeffHH(6,6).^2*120)
% YCoeffH(6)
%
% % YCoeffHH(1,:)=YCoeffHH(1,:)*abs(YCoeffH(1))./MMul1;
% % YCoeffHH(2,:)=YCoeffHH(2,:)*abs(YCoeffH(2))./MMul2;
% % YCoeffHH(3,:)=YCoeffHH(3,:)*abs(YCoeffH(3))./MMul3;
% % YCoeffHH(4,:)=YCoeffHH(4,:)*abs(YCoeffH(4))./MMul4;
% % YCoeffHH(5,:)=YCoeffHH(5,:)*abs(YCoeffH(5))./MMul5;
% % YCoeffHH(6,:)=YCoeffHH(6,:)*abs(YCoeffH(6))./MMul6;
%
%
%
%
str=input("Look at comparison of coefficients")
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%In this block, we do conditional Monte Carlo of SDE which is the true
%numerical 1D conditional density of Y conditional on X having a given
%value. This value of X on which we are conditionin is defined by V10 below.
%We can alter V10 below and it will take out a 1D slice from the 2D density
%of Y conditional on X taking a specific value (here V10).
V10=1.70;
V(1:paths)=V10;
Random2(1:paths,1)=0;
for tt=TT1+1:TT2
Random2(1:paths/2)=randn(paths/2,1);
Random2(paths/2+1:paths)=-Random2(1:paths/2);
V(1:paths,1)=V(1:paths,1)+ ...
(mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2)*dt + ...
sigma0*V(1:paths,1).^gamma .*Random2(1:paths,1)*sqrt(dt) + ...
(mu1.*beta1*V(1:paths,1).^(beta1-1) + mu2.*beta2.*V(1:paths,1).^(beta2-1)).* ...
((mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2)*dt^2/2 + ...
sigma0*V(1:paths,1).^gamma .*Random2(1:paths,1)*(1-1/sqrt(3))*dt^1.5) + ...
.5*(mu1.*beta1.*(beta1-1).*V(1:paths,1).^(beta1-2) + mu2.*beta2.*(beta2-1).*V(1:paths,1).^(beta2-2)).* ...
sigma0^2.*V(1:paths,1).^(2*gamma).*dt^2/2 + ...
sigma0*gamma*V(1:paths,1).^(gamma-1) .* ...
((mu1.*V(1:paths,1).^beta1 + mu2.*V(1:paths,1).^beta2).*Random2(1:paths,1).*1/sqrt(3)*dt^1.5 + ...
sigma0.*V(1:paths,1).^gamma .*(Random2(1:paths,1).^2-1)*dt/2) + ...
.5*sigma0*gamma*(gamma-1).*V(1:paths,1).^(gamma-2) .* ...
sigma0^2.*V(1:paths,1).^(2*gamma) .*Random2(1:paths,1).*1/sqrt(3)*dt^1.5;
V(V<0)=.00001;
end
%Below
NoOfBins=200;
MaxCutOff=10;
[XDensity,IndexOut,IndexMax] = MakeDensityFromSimulation_Infiniti_NEW(V,paths,NoOfBins,MaxCutOff );
clf;
plot(IndexOut(1:IndexMax),XDensity(1:IndexMax),'r');
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%Below, we calculate the value of Zx0 corresponding to X0 (here V10). This
%comes from inverting the Z-series of X so that X=V10;
xOrder=Order;
yOrder=Order;
[Zx0] = CalculateZgivenXAndHSeriesCoeffs(V10,XCoeffH,xOrder);
%Below, we collapse two dimensional pdf of Y|X to one dimensional pdf given
%Zx i.e. this Y|X=X(Zx)
%Here we take out a 1D slice from the 2D density
%of Y conditional on X taking a specific value (here V10).
He(1)=1;
He(2)=Zx0;
He(3)=Zx0^2-1;
He(4)=Zx0^3-3*Zx0;
He(5)=Zx0^4-6*Zx0.^2+3;
He(6)=Zx0^5-10*Zx0.^3+15*Zx0;
He(7)=Zx0^6-15*Zx0.^4+45*Zx0.^2-15;
He(8)=Zx0^7-21*Zx0.^5+105*Zx0.^3-105*Zx0;
YCoeffh(1:Order+1)=0;
for hh=1:yOrder+1
for hh2=1:xOrder+1
YCoeffh(hh)=YCoeffh(hh)+YCoeffHH(hh,hh2).*He(hh2);
end
end
[YCoeffz(1:Order+1)] = ConvertHermiteSeriesToZSeries01(YCoeffh(1:Order+1),Order);
%
%
% %[IsValidFlag] = CheckIsValidDensityPN(YCoeffz(1),YCoeffz(2:Order+1));
% [PIsValidFlag,NIsValidFlag] = CheckIsValidDensityPN02(YCoeffz(1),YCoeffz(2:Order+1));
% PIsValidFlag
% NIsValidFlag
%
%
% for mm=1:100
% [PIsValidFlag,NIsValidFlag] = CheckIsValidDensityPN02(YCoeffz(1),YCoeffz(2:Order+1));
% if((NIsValidFlag==0)&&(PIsValidFlag==1))
% YCoeffz(5)=YCoeffz(5)-.01*abs(YCoeffz(5));
% YCoeffz(6)=YCoeffz(6)+.001*abs(YCoeffz(6));
% %YCoeffz(3)=YCoeffz(3)-.001*abs(YCoeffz(3));
% end
%
% if((PIsValidFlag==0)&&(NIsValidFlag==1))
% YCoeffz(5)=YCoeffz(5)+.01*abs(YCoeffz(5));
% YCoeffz(6)=YCoeffz(6)+.001*abs(YCoeffz(6));
% %YCoeffz(3)=YCoeffz(3)-.001*abs(YCoeffz(3));
% end
%
% if((NIsValidFlag==0)&&(PIsValidFlag==0))
% YCoeffz(6)=YCoeffz(6)+.01*abs(YCoeffz(6));
% %YCoeffz(3)=YCoeffz(3)-.001*abs(YCoeffz(3));
% end
%
% end
%
% [YCoeffh] = ConvertZSeriesToHermiteSeriesNew(YCoeffz,Order)
%
PlotHermiteSeriesDensityAndRvGraph(YCoeffh(1),YCoeffh(2:yOrder+1),'b')
%hold on
%PlotHermiteSeriesDensityAndRvGraph(YCoeffh(1),YCoeffh1(2:yOrder+1),'g')
title(sprintf('V00 = %.3f; kappa=%.3f, theta=%.3f,gamma=%.3f,sigma=%.3f;V10=%.3f at TT1=%.3f;TT2=%.3f;Paths=%d ',V00, kappaV, thetaV,gamma,sigma0, V10,(TT1*dt),(TT2*dt),paths));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
legend({'Numerical Density','Analytic Density1'}, ...
'Location','northeast')
str=input("This is the first version without altering the moments of conditional density")
% %[IsValidFlag] = CheckIsValidDensityPN(YCoeffz(1),YCoeffz(2:Order+1));
[PIsValidFlag,NIsValidFlag] = CheckIsValidDensityPN02(YCoeffz(1),YCoeffz(2:Order+1));
PIsValidFlag
NIsValidFlag
LoopIter=0;
Mul6(1)=.66;
Mul6(2)=.8;
Mul6(3)=.9;
Mul6(4)=1.0;
Mul6(5)=1.1;
while(((PIsValidFlag==0)||(NIsValidFlag==0))&&(LoopIter<5))
LoopIter=LoopIter+1;
YCoeffh
Y2Coeffz=YCoeffz;
Mean=Y2Coeffz(1)+(Y2Coeffz(3)+3*Y2Coeffz(5));
Y2Coeffz(1)=-(Y2Coeffz(3)+3*Y2Coeffz(5));
[YMoments]=CalculateMomentsOfZSeries(Y2Coeffz(1),Y2Coeffz(2:6),5,6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
abs(YMoments(3:6))./abs(YMoments(2:5))
YMoments(2:6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
if(abs(YMoments(6)/YMoments(5))<abs(YMoments(4)/YMoments(3))*Mul6(LoopIter))
YMoments(6)=abs(YMoments(5).*YMoments(4)./YMoments(3)*Mul6(LoopIter))
end
[Y3Coeffz(1),Y3Coeffz(2:6)] = CalculateZSeriesDensityFromRawMomentsM6(YMoments,Y2Coeffz(2:6));
Y3Coeffz(1)=Mean-(Y2Coeffz(3)+3*Y2Coeffz(5));
[PIsValidFlag,NIsValidFlag] = CheckIsValidDensityPN02(Y3Coeffz(1),Y3Coeffz(2:Order+1));
PIsValidFlag
NIsValidFlag
end
if(LoopIter==0)
Y3Coeffz=YCoeffz;
end
clf;
plot(IndexOut(1:IndexMax),XDensity(1:IndexMax),'r');
hold on
PlotZSeriesDensity(Y3Coeffz(1),Y3Coeffz(2:yOrder+1),'b')
title(sprintf('V00 = %.3f; kappa=%.3f, theta=%.3f,gamma=%.3f,sigma=%.3f;V10=%.3f at TT1=%.3f;TT2=%.3f;Paths=%d ',V00, kappaV, thetaV,gamma,sigma0, V10,(TT1*dt),(TT2*dt),paths));%,sprintf('theta= %f', theta), sprintf('kappa = %f', kappa),sprintf('sigma = %f', sigma0),sprintf('T = %f', T));
legend({'Numerical Density','Analytic Density1'}, ...
'Location','northeast')
str=input("This is the Second version after possibly altering the moments of conditional density when needed")
end
.
.
.
.
function [YCoeffZZ,AMat] = DoNewtonIterationsOn2DZSeries(YCoeffZZ,XCoeffZ,Order,MomentsYX0,MomentsY,MaxIter)
%YCoeffZZ is 2D Z-series conditional on X denoted in literature as [Y|X](Zy,Zx)
%The way to construct Z-series from YCoeffZZ is sum of terms YCoeffZZ(n,m) Zy^{n-1)Zx^{m-1}.
%YCoeffZZ(1:Order+1,1:Order+1) is a 2D array containing coefficients of 2D
%Z-Series of Conditional distribution of Y given X (or Z_x).
%XCoeffZ(1:Order+1) is a one dimensional Z-series of X. The way to construct the Z-series is
%sum of terms XCoeffZ(m) Zx^{m-1}. It has dimensiona YCoeffZ(1:Order+1).
% The way to calculate the cross-product/cross-moments: 2D product of Z-series
% ([Y|X](Zy,Zx))^(pp1-1) *(X(Zx))^qq2/E[([Y|X](Zy,Zx))^(pp1-1)*(X(Zx))^qq2]
%MomentsYX0 is a 1D array containing all the target cross-moments ((Order+1)*(Order+1))stacked in an array.
%%AMat gives us all the expected model cross-moments ((Order+1)*(Order+1))stacked in an array.
%DMat contains Jacobian that calculates ((Order+1)*(Order+1)) derivatives
%for each cross-moment hence its size is ((Order+1)*(Order+1)) *,((Order+1)*(Order+1))
%Below, we calculate AMat and DMat.
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
%CalculateMomentsAndDerivativesFor2DNewton_0_BumpCheck
mul=.00625*4;
mulCount=0;
mulCount0=0;
%Below is initial value of Objective function calculated from input array
%target and model sum of squared differences of cross-moments.
ObjMax=sum((AMat-MomentsYX0).^2);
FailCount=0;
%Iterations=MaxIter;
Mul_Gauss=1;
iter=0;
while((sum((AMat-MomentsYX0).^2)>.001)&&(iter<MaxIter))
iter=iter+1;
%Below we stack all coefficients of YCoeffZZ to be optimized in a
%single Newton work-horse array da.
for pp2=1:Order+1
for qq2=1:Order+1
da((pp2-1)*(Order+1)+qq2,1)=YCoeffZZ(pp2,qq2);
end
end
for pp2=1:Order+1
for qq2=1:Order+1
F((pp2-1)*(Order+1)+qq2,1)=(AMat((pp2-1)*(Order+1)+qq2,1)-MomentsYX0((pp2-1)*(Order+1)+qq2,1));
end
end
%DMat0 is matrix with diagonal weights that have to be applied to Jacobian matrix in order to turn
%the Newton method closer to Steepest descent.
for pp1=1:Order+1
for qq1=1:Order+1
for pp2=1:Order+1
for qq2=1:Order+1
if((pp1-1)*(Order+1)+qq1==(pp2-1)*(Order+1)+qq2)
DMat0((pp1-1)*(Order+1)+qq1,(pp2-1)*(Order+1)+qq2)=Mul_Gauss*.0001;
end
end
end
end
end
%mul and mul_Gauss are two different multipliers that work in the algorithm.
da=da-mul*((DMat0+DMat)\F);
%Assign the new Newton values of Coefficients from Newton work-horse
%array to a temporary array YCoeffZZnew. If the objective function with
%this new array increases, we will declare the iteration a success and
%assign this YCoeffZZnew to original array YCoeffZZ.
for pp2=1:Order+1
for qq2=1:Order+1
YCoeffZZnew(pp2,qq2)=da((pp2-1)*(Order+1)+qq2,1);
end
end
if(rem(iter,10)==0)
[MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZnew,Order,Order);
YCoeffZZnew=YCoeffZZnew*sqrt(MomentsY(2))./sqrt(VarZZ);
[MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZnew,Order,Order);
YCoeffZZnew(1,1)=YCoeffZZnew(1,1)+MomentsY(1)-MeanZZ;
end
if(rem(iter,50)==0)
MaxIter0=1;
[YCoeffZZnew] = CalculateSmoothingGuessDensity2D(YCoeffZZnew,XCoeffZ,MomentsYX0,MomentsY,Order,Order,MaxIter0);
end
%Calculate model cross-moments with new values to find the objective
%function to decide whether to accept the iteration value of reject it.
[AMat] = CalculateCrossMomentsofTwo2Dand1DZSeries(YCoeffZZnew,XCoeffZ,Order,Order);
ObjNew=sum((AMat-MomentsYX0).^2);
ObjNew
ObjMax
iter
%str=input("Look at values");
MulGaussHigh=10^8;
MulGaussLow=1;
if(Mul_Gauss>MulGaussHigh)
Mul_Gauss=MulGaussLow;
end
if(ObjNew<ObjMax)
%If objective function decreases assign YCoeffZZnew to YCoeffZZ
ObjMax=ObjNew;
YCoeffZZ=YCoeffZZnew;
mulCount0=mulCount0+1;
if(mulCount0>=4)
Mul_Gauss=Mul_Gauss*.75;
if(Mul_Gauss<1)
Mul_Gauss=1;
end
mulCount0=0;
end
mul=mul*2; %On success increase the step size.
if(mul>1)
mul=1;
end
FailCount=0;
else
mul=mul*.5; %On failure, decrease the step size.
if(mul<.00625/8)
mul=.00625*2;
mulCount=mulCount+1;
end
if(mulCount>=4)
Mul_Gauss=Mul_Gauss*1.5;
mul_Count=0;
end
FailCount=FailCount+1;
end
%Calculate model cross-moments and Jacobian derivatives for Next Newton
%type iteration.
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
end
% YCoeffZZ
% AMat
% MomentsYX0
% AMat-MomentsYX0
end
.
.
.
.
function [YCoeffZZBest] = CalculateSmoothingGuessDensity2D(YCoeffZZ,XCoeffZ,MomentsYX0,MomentsY,OrderY,OrderX,MaxIter)
Order=OrderY;
%[AMat] = CalculateCrossMomentsofTwo2Dand1DZSeries(YCoeffZZ,XCoeffZ,OrderY,OrderX)
Iterations=40;
Iterations2=2;
YCoeffZZBest=YCoeffZZ;
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
ObjMax=sum((AMat-MomentsYX0).^2);
for iter=1:MaxIter
for pp1=1:Order+1
for qq1=1:Order+1
for iter2=1:Iterations2
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
DiffMoment=AMat((pp1-1)*(Order+1)+qq1)-MomentsYX0((pp1-1)*(Order+1)+qq1);
DDiff=DMat((pp1-1)*(Order+1)+qq1,(pp1-1)*(Order+1)+qq1);
%YCoeffZZ=YCoeffZZ;
YCoeffZZ(pp1,qq1)=YCoeffZZ(pp1,qq1)-DiffMoment/DDiff;
end
[MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZ,Order,Order);
YCoeffZZ=YCoeffZZ*sqrt(MomentsY(2))./sqrt(VarZZ);
[MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZ,Order,Order);
YCoeffZZ(1,1)=YCoeffZZ(1,1)+MomentsY(1)-MeanZZ;
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
ObjNew=sum((AMat-MomentsYX0).^2);
if(ObjNew<ObjMax)
%If objective function decreases assign YCoeffZZnew to YCoeffZZ
ObjMax=ObjNew;
YCoeffZZBest=YCoeffZZ;
end
end
end
[AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
ObjNew=sum((AMat-MomentsYX0).^2);
if(ObjNew<ObjMax)
%If objective function decreases assign YCoeffZZnew to YCoeffZZ
ObjMax=ObjNew;
YCoeffZZBest=YCoeffZZ;
end
if(ObjNew>2*ObjMax)
% YCoeffZZ=YCoeffZZBest;
end
ObjNew
ObjMax
iter
end
end
% for iter=1:Iterations
%
% for pp1=1:Order+1
% for qq1=1:Order+1
%
% for iter2=1:Iterations2
% [AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
%
% DiffMoment=AMat((pp1-1)*(Order+1)+qq1)-MomentsYX0((pp1-1)*(Order+1)+qq1);
% DDiff=DMat((pp1-1)*(Order+1)+qq1,(pp1-1)*(Order+1)+qq1);
% YCoeffZZnew=YCoeffZZ;
% YCoeffZZnew(pp1,qq1)=YCoeffZZ(pp1,qq1)-DiffMoment/DDiff;
% end
%
% [MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZnew,Order,Order);
% YCoeffZZnew=YCoeffZZnew*sqrt(MomentsY(2))./sqrt(VarZZ);
% [MeanZZ,VarZZ] = CalculateMeanAndVarOf2DZSeriesY(YCoeffZZnew,Order,Order);
% YCoeffZZnew(1,1)=YCoeffZZnew(1,1)+MomentsY(1)-MeanZZ;
% [AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZnew,XCoeffZ,Order);
% ObjNew=sum((AMat-MomentsYX0).^2);
% if(ObjNew<ObjMax)
% %If objective function decreases assign YCoeffZZnew to YCoeffZZ
% ObjMax=ObjNew;
% YCoeffZZ=YCoeffZZnew;
% end
% end
%
% end
% [AMat,DMat] = CalculateMomentsAndDerivativesFor2DNewton_0(YCoeffZZ,XCoeffZ,Order);
% ObjNew=sum((AMat-MomentsYX0).^2);
% if(ObjNew<ObjMax)
% %If objective function decreases assign YCoeffZZnew to YCoeffZZ
% ObjMax=ObjNew;
% YCoeffZZBest=YCoeffZZ;
% end
%
% ObjNew
% ObjMax
% iter
%
%
% end
.
.
.
.
function [PIsValidFlag,NIsValidFlag] = CheckIsValidDensityPN02(cc0,cc)
%Roughly checks if the density is valid. It is not a definitive check but
%just a rough one but mostly works.
if(cc(1)>0)
Z(1)=2.0;
Z(2)=3.0;
Z(3)=3.9;
Z(4)=4.2;
Z(5)=-2.3;
Z(6)=-2.7;
Z(7)=-3.3;
Z(8)=-3.9;
Z(9)=-4.3;
if(length(cc)==7)
X=cc0 + cc(1) * Z+ cc(2) * Z.^2+ cc(3) * Z.^3+ cc(4) * Z.^4+ cc(5) * Z.^5+ cc(6) * Z.^6+ cc(7) * Z.^7;
end
if(length(cc)==5)
X=cc0 + cc(1) * Z+ cc(2) * Z.^2+ cc(3) * Z.^3+ cc(4) * Z.^4+ cc(5) * Z.^5;
end
PIsValidFlag=0;
if ((X(4)>=X(3)) && (X(3)>X(2)) && (X(2)>X(1))&&(X(1)>X(5)) )
PIsValidFlag=1;
end
NIsValidFlag=0;
if ((X(5)>X(6))&&(X(6)>X(7)) && (X(7)>X(8))&& (X(8)>X(9)))
NIsValidFlag=1;
end
else
Z(1)=2.0;
Z(2)=3.0;
Z(3)=3.9;
Z(4)=4.2;
Z(5)=-2.3;
Z(6)=-2.7;
Z(7)=-3.3;
Z(8)=-3.9;
Z(9)=-4.3;
if(length(cc)==7)
X=cc0 + cc(1) * Z+ cc(2) * Z.^2+ cc(3) * Z.^3+ cc(4) * Z.^4+ cc(5) * Z.^5+ cc(6) * Z.^6+ cc(7) * Z.^7;
end
if(length(cc)==5)
X=cc0 + cc(1) * Z+ cc(2) * Z.^2+ cc(3) * Z.^3+ cc(4) * Z.^4+ cc(5) * Z.^5;
end
PIsValidFlag=0;
if ((X(4)>=X(3)) && (X(3)>X(2)) && (X(2)>X(1))&&(X(1)>X(5)) )
PIsValidFlag=1;
end
NIsValidFlag=0;
if ((X(5)>X(6))&&(X(6)>X(7)) && (X(7)>X(8))&& (X(8)>X(9)))
NIsValidFlag=1;
end
end
end
.
.
.
.
function [c0,c] = CalculateZSeriesDensityFromRawMomentsM6(rMu,bGuess)
mOrder=6;
[Mu1,cMu] = ConvertRawMomentsToCentralMoments(rMu,mOrder);
sMu(1)=0;
sMu(2)=1;
sMu(3)=cMu(3)/cMu(2).^1.5;
sMu(4)=cMu(4)/cMu(2).^2.0;
sMu(5)=cMu(5)/cMu(2).^2.5;
sMu(6)=cMu(6)/cMu(2).^3.0;
%sMu(7)=cMu(7)/cMu(2).^3.5;
%sMu(8)=cMu(8)/cMu(2).^4.0;
w(mOrder)=1;
for nn=mOrder-1:-1:1
w(nn)=w(nn+1).*(nn+1)*(nn);
end
%w(2)=w(2)*1024;
%w(8)=w(8)*2;
%w(10)=w(10)*sqrt(2);
iter=2000;
%bGuess(1:5)=0;
%bGuess(1)=1;
[c0,c] = PreSmoothingGuessAdvancedFromGuessBestNewC5(sMu,bGuess,iter);
SeriesOrder=5;
NMoments=6;
[F,dF] = CalculateMomentsAndDerivatives_0(sMu,c0,c,SeriesOrder,SeriesOrder,NMoments);
da(1,1)=c0;
da(2:SeriesOrder+1,1)=c(1:SeriesOrder);
[Moments] = CalculateMomentsOfZSeries(c0,c,SeriesOrder,NMoments);
Moments
sMu
%str=input('Look at comparison of moments');
%Replace with your own more intelligent objective function if you like.
%ObjBest=100000*(abs(sMu(1)-Moments(1)))+abs(sMu(2)-Moments(2))+abs((sMu(3)-Moments(3))^(1/1.5))+abs((sMu(4)-Moments(4))^(1/2.0)) + ...
% abs((sMu(5)-Moments(5))^(1/2.0))+abs((sMu(6)-Moments(6))^(1/2.0));
[ObjBest] = CalculateObjMoment(sMu,Moments,w,mOrder)
b0Best=c0;
bBest(1:SeriesOrder)=c(1:SeriesOrder);
nn=0;
while((nn<1000)&&((abs(F(1,1))>.000000000001) || (abs(F(2,1))>.000000000001) || (abs(F(3,1))>.000000000001) || (abs(F(4,1))>.00000000001)|| (abs(F(5,1))>.00000000001)|| (abs(F(6,1))>.00000000001) ))
nn=nn+1;
%Below Newton matrix equation to improve the Z-series coefficients guess at previous step.
if(nn<10)
da=da-10*dF\F;
elseif(nn<20)
da=da-5*dF\F;
elseif(nn<40)
da=da-2*dF\F;
else
da=da-dF\F;
end
%b0=median
b0=da(1,1);
b(1:SeriesOrder)=da(2:SeriesOrder+1,1);
%[F,dF] = CalculateCumulantsAndDerivativesFromMoments_0(C,b0,b,SeriesOrder,SeriesOrder,NoOfCumulants);
[F,dF] = CalculateMomentsAndDerivatives_0(sMu,b0,b,SeriesOrder,SeriesOrder,NMoments);
[IsValidFlag] =1;% CheckIsValidDensity(b0,b);
[Moments] = CalculateMomentsOfZSeries(b0,b,SeriesOrder,NMoments);
[ObjNew] = CalculateObjMoment(sMu,Moments,w,mOrder)
%ObjNew=100000*(abs(sMu(1)-Moments(1)))+abs(sMu(2)-Moments(2))+abs((sMu(3)-Moments(3))^(1/1.5))+abs((sMu(4)-Moments(4))^(1/2.0)) + ...
%abs((sMu(5)-Moments(5))^(1/2.0))+abs((sMu(6)-Moments(6))^(1/2.0));
if((ObjBest>ObjNew) &&( IsValidFlag))
ObjBest=ObjNew;
b0Best=b0;
bBest(1:SeriesOrder)=b(1:SeriesOrder);
end
da(1,1)=b0;
da(2:SeriesOrder+1,1)=b(1:SeriesOrder);
end
c0=b0Best;%Best;
c(1:SeriesOrder)=bBest;%Best(1:SeriesOrder);
[Moments] = CalculateMomentsOfZSeries(c0,c,SeriesOrder,NMoments);
b0Best
bBest
Moments
sMu
cMu(2)
%str=input('Look at fit of density----0000 ')
c0=c0*sqrt(cMu(2));
c=c*sqrt(cMu(2));
c0=c0+Mu1;
end
.
.
.
function [c0,c] = PreSmoothingGuessAdvancedFromGuessBestNewC5(cmu,cin,iter)
%Mul=1.0;
SeriesOrder=5;
NMoments=6;
EZ(1)=0;
EZ(2)=1;
for nn=3:NMoments*SeriesOrder+SeriesOrder+2
if rem(nn,2)==1
EZ(nn)=0;
else
EZ(nn)=EZ(nn-2)*(nn-1);
EZ(nn);
end
end
c0=-(cin(2)+3*cin(4)); %This is for zero mean condition.
c=cin;
MaxIter=100;
for nn=1:iter %increase the iterations over coefficients if neeeded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
tolerance=.0000001*cmu(3);
mm=0;
while ((abs(M3-cmu(3)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(2)=c(2)-(M3-cmu(3))/dc2;
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[c0,c] = IterateSmoothingGuessOverPreviousMomentsC5(cmu,c,3,2,EZ);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
tolerance=.0000001*cmu(4);
mm=0;
while ((abs(M4-cmu(4)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(3)=c(3)-(M4-cmu(4))/dc3;
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[c0,c] = IterateSmoothingGuessOverPreviousMomentsC5(cmu,c,4,2,EZ);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
tolerance=.0000001*cmu(5);
mm=0;
while ((abs(M5-cmu(5)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(4)=c(4)-(M5-cmu(5))/dc4;
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[c0,c] = IterateSmoothingGuessOverPreviousMomentsC5(cmu,c,5,2,EZ);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M6,dc5] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,6,EZ);
tolerance=.0000001*cmu(6);
mm=0;
while ((abs(M6-cmu(6)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(5)=c(5)-(M6-cmu(6))/dc5;
[M6,dc5] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,6,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[c0,c] = IterateSmoothingGuessOverPreviousMomentsC5(cmu,c,6,2,EZ);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
.
.
.
.
function [EnMoment,dEnMomentdCoeff] = CalculateParticularMomentAndDerivativeOfItsCoeff(a0,a,SeriesOrder,nMoment,EZ)
% if(SeriesOrder>=8)
% a0=-(a(2)+3*a(4)+15*a(6)+105*a(8));% ---1
% end
% if(SeriesOrder<8)
% a0=-(a(2)+3*a(4)+15*a(6));% ---1
% end
%
% EZ(1)=0;
% EZ(2)=1;
% for nn=3:NMoments*SeriesOrder+NZterms+2
% if rem(nn,2)==1
% EZ(nn)=0;
% else
% EZ(nn)=EZ(nn-2)*(nn-1);
% EZ(nn);
% end
% end
% EZ;
%EXZ(1,1)=1;
%for pp1=1:NZterms
% EXZ(1,pp1+1)=EZ(pp1);
%end
a(SeriesOrder+1:nMoment*SeriesOrder+1+SeriesOrder)=0;
b0=a0;
b=a;
for mm=2:nMoment
[b0,b] =SeriesProduct(a0,a,b0,b,SeriesOrder*mm+SeriesOrder+1);
b(SeriesOrder*mm+1:nMoment*SeriesOrder+SeriesOrder+1)=0;
if(mm==nMoment-1)
dEnMomentdCoeff=b0.*EZ(nMoment-1);
for pp2=1:SeriesOrder*nMoment-1
dEnMomentdCoeff=dEnMomentdCoeff+b(pp2).*EZ(pp2+nMoment-1);
end
dEnMomentdCoeff=dEnMomentdCoeff*nMoment;
end
if(mm==nMoment)
EnMoment=b0;
for pp2=1:SeriesOrder*nMoment
EnMoment=EnMoment+b(pp2).*EZ(pp2);
end
end
end
end
.
.
.
function [c0,c] = IterateSmoothingGuessOverPreviousMomentsC5(cmu,c,nMoment,iter,EZ)
MaxIter=7;
SeriesOrder=5;
c0=-(c(2)+3*c(4));
if(nMoment==3)
for nn=1:iter %increase the iterations over coefficients if neeeded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
tolerance=.0000001*cmu(3);
mm=0;
while ((abs(M3-cmu(3)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(2)=c(2)-(M3-cmu(3))/dc2;
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
if(nMoment==4)
for nn=1:iter %increase the iterations over coefficients if neeeded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
tolerance=.0000001*cmu(3);
mm=0;
while ((abs(M3-cmu(3)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(2)=c(2)-(M3-cmu(3))/dc2;
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
tolerance=.0000001*cmu(4);
mm=0;
while ((abs(M4-cmu(4)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(3)=c(3)-(M4-cmu(4))/dc3;
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(nMoment==5)
for nn=1:iter %increase the iterations over coefficients if neeeded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
tolerance=.0000001*cmu(3);
mm=0;
while ((abs(M3-cmu(3)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(2)=c(2)-(M3-cmu(3))/dc2;
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
tolerance=.0000001*cmu(4);
mm=0;
while ((abs(M4-cmu(4)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(3)=c(3)-(M4-cmu(4))/dc3;
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
tolerance=.0000001*cmu(5);
mm=0;
while ((abs(M5-cmu(5)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(4)=c(4)-(M5-cmu(5))/dc4;
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(nMoment==6)
for nn=1:iter %increase the iterations over coefficients if neeeded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
tolerance=.0000001*cmu(5);
mm=0;
while ((abs(M3-cmu(3)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(2)=c(2)-(M3-cmu(3))/dc2;
[M3,dc2] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,3,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
tolerance=.0000001*cmu(5);
mm=0;
while ((abs(M4-cmu(4)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(3)=c(3)-(M4-cmu(4))/dc3;
[M4,dc3] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,4,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
tolerance=.0000001*cmu(5);
mm=0;
while ((abs(M5-cmu(5)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(4)=c(4)-(M5-cmu(5))/dc4;
[M5,dc4] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,5,EZ);
end
c0=-(c(2)+3*c(4));
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M6,dc5] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,6,EZ);
tolerance=.0000001*cmu(6);
mm=0;
while ((abs(M6-cmu(6)) >tolerance ) && (mm<MaxIter))
mm=mm+1;
c(5)=c(5)-(M6-cmu(6))/dc5;
[M6,dc5] = CalculateParticularMomentAndDerivativeOfItsCoeff(c0,c,SeriesOrder,6,EZ);
end
[SecondMoment] = CalculateSecondMomentC5(c0,c);
c=c/sqrt(SecondMoment);
c0=c0/sqrt(SecondMoment);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
end
end
.
.
.
.
function [SecondMoment] = CalculateSecondMomentC5(a0,a)
SecondMoment=a0^2+a(1)^2 +2* a0* a(2) +3*(a(2).^2+2* a(1).* a(3) +2* a0* a(4))+ ...
15*(a(3).^2 +2 *a(2).* a(4) +2 *a(1).* a(5))+105*(a(4).^2 +2* a(3).* a(5) )+ ...
945*(a(5).^2 );
%a0^2+a1^2+2 a0 a2+3 (a2^2+2 a1 a3+2 a0 a4)+945 a5^2+15 (a3^2+2 a2 a4+2 a1 a5)+105 (a4^2+2 a3 a5)
end
.
.
.
.
function [Moments] = CalculateMomentsOfZSeries(a0,a,SeriesOrder,NMoments)
%aa0=a0;
%a0=0;% ---1
EZ(1)=0;
EZ(2)=1;
%LogEZ(2)=0;
for nn=3:NMoments*SeriesOrder
if rem(nn,2)==1
EZ(nn)=0;
else
EZ(nn)=EZ(nn-2)*(nn-1);
%LogEZ(nn)=log(EZ(nn-2))+log(nn-1);
%EZ(nn);
end
end
%EZ
%EZ(1:30)
%str=input('Look at numbers')
a(SeriesOrder+1:SeriesOrder*NMoments+1)=0;
b0=a0;
b=a;
for mm=1:NMoments
if(mm>1)
%[b0,b] =SeriesProductLogarithmic(a0,a,b0,b,SeriesOrder*mm);
[b0,b] =SeriesProduct(a0,a,b0,b,SeriesOrder*mm);
%b0-bb0
%b-bb
%str=input('Look at two products')
b(SeriesOrder*mm+1:SeriesOrder*NMoments+1)=0;
end
% Logb=log(abs(b));
% Signb=sign(b);
EXZ(mm,1)=b0;
for pp2=1:SeriesOrder*mm
if rem(pp2,2)==0
%EXZ(mm,1)=EXZ(mm,1)+exp(Logb(pp2)+LogEZ(pp2)).*Signb(pp2);
EXZ(mm,1)=EXZ(mm,1)+b(pp2).*EZ(pp2);
% b(pp2).*EZ(pp2)
% exp(Logb(pp2)+LogEZ(pp2)).*Signb(pp2)
% mm
% pp2
%str=input('Look at moment values')
end
end
end
for nn=1:NMoments
Moments(nn)=EXZ(nn,1);
end
end
.
.
.
function [F,dF] = CalculateMomentsAndDerivatives_0(Ms,a0,a,SeriesOrder,NZterms,NMoments)
%[F,dF] = CalculateCumulantsAndDerivativesFromMoments(C,a0,a,SeriesOrder,SeriesOrder,NoOfCumulants);
%a(1)=a1;
%a(2)=a2;
%a(3)=a3;
%a(4)=a4;
%a(5)=a5;
%a(6)=a6;
%a(7)=a7;
%aa0=a0;
if(NMoments>8)
a0=-(a(2)+3*a(4)+15*a(6)+105*a(8));% ---1
end
if((NMoments==7)||(NMoments==8))
a0=-(a(2)+3*a(4)+15*a(6));% ---1
end
% if(NMoments==6)
% a0=-(a(2)+3*a(4));% ---1
% end
if((NMoments==6)||(NMoments==7))
a0=-(a(2)+3*a(4));% ---1
end
if((NMoments==3)||(NMoments==4))
a0=-(a(2));% ---1
end
EZ(1)=0;
EZ(2)=1;
for nn=3:NMoments*SeriesOrder+NZterms+2
if rem(nn,2)==1
EZ(nn)=0;
else
EZ(nn)=EZ(nn-2)*(nn-1);
EZ(nn);
end
end
EZ;
EXZ(1,1)=1;
for pp1=1:NZterms
EXZ(1,pp1+1)=EZ(pp1);
end
a(SeriesOrder+1:NMoments*SeriesOrder+1)=0;
b0=a0;
b=a;
for mm=1:NMoments
if(mm>1)
[b0,b] =SeriesProduct(a0,a,b0,b,SeriesOrder*mm);
b(SeriesOrder*mm+1:NMoments*SeriesOrder+1)=0;
end
% b0
% b
%str=input('Look at numbers')
EXZ(mm+1,1)=b0;
for pp2=1:SeriesOrder*mm
EXZ(mm+1,1)=EXZ(mm+1,1)+b(pp2).*EZ(pp2);
end
for pp1=1:NZterms
EXZ(mm+1,pp1+1)=b0.*EZ(pp1);
for pp2=1:SeriesOrder*mm
EXZ(mm+1,pp1+1)=EXZ(mm+1,pp1+1)+b(pp2).*EZ(pp2+pp1);
end
end
end
%u1=EXZ(2,1);
u2=EXZ(3,1);
if(NMoments>=3)
u3=EXZ(4,1);
end
%NMoments
if(NMoments>=4)
u4=EXZ(5,1);
end
u1=a0+a(2);
if(SeriesOrder>=4)
u1=a0+a(2)+3*a(4);
end
if(SeriesOrder>=6)
u1=u1+15*a(6);
end
if(SeriesOrder>=8)
u1=u1+105*a(8);
end
%k2=u2-u1^2;
%k3=u3-3*u2*u1+2*u1^3;
%k4=u4-4*u3*u1-3*u2^2+12*u2*u1^2-6*u1^4;
du1(1)=1;%----2
% du1(2)=0;%----2
% du1(3)=1;%----2
% du1(4)=0;%----2
% du1(5)=1;%----2
% du1(6)=0;%----2
% du1(7)=15;%----2
% du1(8)=0;%----2
% du1(9)=105;%----2
% du1(10)=0;%----2
du2(1)=2*EXZ(2,1);
if(NMoments>=3)
du3(1)=3*EXZ(3,1);
end
if(NMoments>=4)
du4(1)=4*EXZ(4,1);
end
%du1(1)=1;%----2
%du2(1)=0;
%du3(1)=0;
%du4(1)=0;
for mm=2:SeriesOrder+1
du1(mm)=EXZ(1,mm);
du2(mm)=2*EXZ(2,mm);
if(NMoments>=3)
du3(mm)=3*EXZ(3,mm);
end
if(NMoments>=4)
du4(mm)=4*EXZ(4,mm);
end
end
if(NMoments>=5)
u5=EXZ(6,1);
%du5(1)=5*EXZ(5,1);
for mm=1:SeriesOrder+1
du5(mm)=5*EXZ(5,mm);
end
end
if(NMoments>=6)
u6=EXZ(7,1);
%du6(1)=0;
for mm=1:SeriesOrder+1
du6(mm)=6*EXZ(6,mm);
end
end
if(NMoments>=7)
u7=EXZ(8,1);
%str=input('Look at k7 and k71')
%du7(1)=0;
for mm=1:SeriesOrder+1
du7(mm)=7*EXZ(7,mm);
end
end
if(NMoments>=8)
u8=EXZ(9,1);
for mm=1:SeriesOrder+1
du8(mm)=8*EXZ(8,mm);
end
end
if(NMoments>=9)
u9=EXZ(10,1);
for mm=1:SeriesOrder+1
du9(mm)=9*EXZ(9,mm);
end
end
if(NMoments>=10)
u10=EXZ(11,1);
for mm=1:SeriesOrder+1
du10(mm)=10*EXZ(10,mm);
end
end
F(1,1)=u1-Ms(1);
F(2,1)=u2-Ms(2);
F(3,1)=u3-Ms(3);
if(NMoments>=4)
F(4,1)=u4-Ms(4);
end
if(NMoments>=5)
F(5,1)=u5-Ms(5);
end
if(NMoments>=6)
F(6,1)=u6-Ms(6);
end
if(NMoments>=7)
F(7,1)=u7-Ms(7);
end
if(NMoments>=8)
F(8,1)=u8-Ms(8);
end
if(NMoments>=9)
F(9,1)=u9-Ms(9);
end
if(NMoments>=10)
F(10,1)=u10-Ms(10);
end
for mm=1:SeriesOrder+1
dF(1,mm)=du1(mm);
dF(2,mm)=du2(mm);
dF(3,mm)=du3(mm);
if(NMoments>=4)
dF(4,mm)=du4(mm);
end
if(NMoments>=5)
dF(5,mm)=du5(mm);
end
if(NMoments>=6)
dF(6,mm)=du6(mm);
end
if(NMoments>=7)
dF(7,mm)=du7(mm);
end
if(NMoments>=8)
dF(8,mm)=du8(mm);
end
if(NMoments>=9)
dF(9,mm)=du9(mm);
end
if(NMoments>=10)
dF(10,mm)=du10(mm);
end
end
end
.
.
.
function [ObjMomentOut] = CalculateObjMoment(Moment0,Moment,w,MOrder)
ObjMomentOut=0;
for nn=1:MOrder
ObjMomentOut=ObjMomentOut+(Moment(nn)-Moment0(nn)).^2.*w(nn);
end
end
.
.
.
.
function [mu1,cMu] = ConvertRawMomentsToCentralMoments(Mu,mOrder)
%cu
%mu
% %mOrder
%str=input('Look at numbers');
mu1=Mu(1);
for mm=2:mOrder
cMu(mm)=0;
for jj=0:mm
if(jj==0)
cMu(mm)=cMu(mm)+ (-1)^(mm-jj).*factorial(mm)/factorial(jj)/factorial(mm-jj)*1*mu1.^(mm-jj);
else
cMu(mm)=cMu(mm)+ (-1)^(mm-jj).*factorial(mm)/factorial(jj)/factorial(mm-jj)*Mu(jj)*mu1.^(mm-jj);
end
end
end
.
.
.
.
You think life is a secret, Life is only love of flying, It has seen many ups and downs, But it likes travel more than the destination. Allama Iqbal