Aproximacia uhrnov skod

Momentova metoda pre fittovanie distribucii pre uhrny podliehajuce zaisteniu

ClearAll[momentovaMetodaZaistenie2p]
momentovaMetodaZaistenie2p[uhrnySkod_,dist_,parameter1_,parameter2_]:=Solve[Moment[uhrnySkod,1]==Moment[dist,1]&&Moment[uhrnySkod,2]==Moment[dist,2],
{parameter1,parameter2},Assumptions->{parameter1>0,parameter2>0}]//Flatten

ClearAll[momentovaMetodaZaistenie3p]
momentovaMetodaZaistenie3p[uhrnySkod_,dist_,parameter1_,parameter2_,parameter3_]:=Reduce[
Moment[uhrnySkod,1]==Moment[dist,1]&&Moment[uhrnySkod,2]==Moment[dist,2]&&Moment[uhrnySkod,3]==Moment[dist,3],
{parameter1,parameter2,parameter3}]

Posunute Gamma rozdelenie

ClearAll[translatedGammaDistribution]
translatedGammaDistribution[α_,β_,κ_]:=GammaDistribution[α,β,1,κ]

Vypocet uhrnov skod pre skody podliehajuce zaisteniu

ClearAll[uhrnySoZaistenim]
uhrnySoZaistenim[skody_,priorita_,vrstva_]:=Module[{rozdiel,max,min,uhrny},
rozdiel=Map[#-priorita&,skody,{2}];
max=Map[Max[#,0]&,rozdiel,{2}];
min=Map[Min[#,vrstva]&,max,{2}];
uhrny=Map[Total[#]&,min]
]

Percentile Matching Test - 80%-120% strednej hodnoty

ClearAll[percentileMatchingTest]
percentileMatchingTest[data_,dist_]:=Module[{survivalFvalues,meanPercentige={0.8,0.9,1,1.1,1.2},survivalFvaluesPercent},
survivalFvalues=Map[SurvivalFunction[dist,#*Mean[data]]&,meanPercentige];
survivalFvaluesPercent=Map[#*100&,survivalFvalues]
]

(*Tabulka na vypisanie*)

ClearAll[percentileTestTable]
percentileTestTable[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,meanPercentige,roundDif1,roundDif2,roundDif3,tabulka},
meanPercentige={"80%","90%","100%","110%","120%"};
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
roundDif1=Map[DecimalForm[#,{4,3}]&,dif1];
roundDif2=Map[DecimalForm[#,{4,3}]&,dif2];
roundDif3=Map[DecimalForm[#,{4,3}]&,dif3];
tabulka=TableForm[{meanPercentige,empTest,roundDif1,roundDif2,roundDif3},TableDirections->Row,TableHeadings->{{"Percentige of mean","Empirical","Gamma","Log N","Translated Gamma"},None},TableAlignments->Center,TableSpacing->{5,2}]
]

Plot pre Percentile Matching Test 80%-120%

ClearAll[fitTestPlot80]
fitTestPlot80[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,percentiges,plot,joinedGamma,joinedLog,joinedtransGamma},
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
percentiges={80,90,100,110,120};
joinedGamma=Transpose[{percentiges,dif1}];
joinedLog=Transpose[{percentiges,dif2}];
joinedtransGamma=Transpose[{percentiges,dif3}];
plot= Show[Plot[0,{x,75,125},PlotStyle->{Red,Thick},PlotRange->{{75,125},All},AxesOrigin->{75,-1},GridLines->Automatic,Background->GrayLevel[0.95],PlotLabel->"Test Percentilovej Zhody",LabelStyle->{Bold,Black},AxesLabel->{Style["% strednej hodnoty",Black],Style["Rozdiel v %",Black]}],
ListPlot[{joinedGamma,joinedLog,joinedtransGamma},PlotTheme->{"PastelColor"},PlotStyle->PointSize[0.025],Joined->True,Mesh->Full,PlotLegends->Placed[{"Gama","Log-Normalne","Posunute gama"},{0.8,0.8}]]
]]

Percentile Matching Test - 90%-110% strednej hodnoty

ClearAll[percentileMatchingTest90]
percentileMatchingTest90[data_,dist_]:=Module[{survivalFvalues,meanPercentige={0.9,0.95,1,1.05,1.1},survivalFvaluesPercent},
survivalFvalues=Map[SurvivalFunction[dist,#*Mean[data]]&,meanPercentige];
survivalFvaluesPercent=Map[#*100&,survivalFvalues]
]

(*Tabulka na vypisanie*)

ClearAll[percentileTestTable90]
percentileTestTable90[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,meanPercentige,roundDif1,roundDif2,roundDif3,tabulka},
meanPercentige={"90%","95%","100%","105%","110%"};
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
roundDif1=Map[DecimalForm[#,{4,3}]&,dif1];
roundDif2=Map[DecimalForm[#,{4,3}]&,dif2];
roundDif3=Map[DecimalForm[#,{4,3}]&,dif3];
tabulka=TableForm[{meanPercentige,empTest,roundDif1,roundDif2,roundDif3},TableDirections->Row,TableHeadings->{{"Percentige of mean","Empirical","Gamma","Log N","Translated Gamma"},None},TableAlignments->Center,TableSpacing->{5,2}]
]

Plot pre Percentile Matching Test 90%-110%

ClearAll[fitTestPlot90]
fitTestPlot90[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,percentiges,plot,joinedGamma,joinedLog,joinedtransGamma},
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
percentiges={90,95,100,105,110};
joinedGamma=Transpose[{percentiges,dif1}];
joinedLog=Transpose[{percentiges,dif2}];
joinedtransGamma=Transpose[{percentiges,dif3}];
plot= Show[Plot[0,{x,85,115},PlotStyle->{Red,Thick},PlotRange->{{85,115},All},AxesOrigin->{85,-0.5},GridLines->Automatic,Background->GrayLevel[0.95],PlotLabel->"Test Percentilovej Zhody",LabelStyle->{Bold,Black},AxesLabel->{Style["% strednej hodnoty",Black],Style["Rozdiel v %",Black]}],
ListPlot[{joinedGamma,joinedLog,joinedtransGamma},PlotTheme->{"PastelColor"},PlotStyle->PointSize[0.025],Joined->True,Mesh->Full,PlotLegends->Placed[{"Gama","Log-Normalne","Posunute gama"},{0.8,0.9}]]
]]

ClearAll[fitTest2Plot90]
fitTest2Plot90[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,percentiges,plot,joinedGamma,joinedLog,joinedtransGamma},
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
percentiges={90,95,100,105,110};
joinedGamma=Transpose[{percentiges,dif1}];
joinedLog=Transpose[{percentiges,dif2}];
joinedtransGamma=Transpose[{percentiges,dif3}];
plot= Show[Plot[0,{x,85,115},PlotStyle->{Red,Thick},PlotRange->{{85,115},All},AxesOrigin->{85,-0.3},GridLines->Automatic,Background->GrayLevel[0.95],PlotLabel->"Test Percentilovej Zhody",LabelStyle->{Bold,Black},AxesLabel->{Style["% strednej hodnoty",Black],Style["Rozdiel v %",Black]}],
ListPlot[{joinedGamma,joinedLog,joinedtransGamma},PlotTheme->{"PastelColor"},PlotStyle->PointSize[0.025],Joined->True,Mesh->Full,PlotLegends->Placed[{"Gama","Log-Normalne","Posunute gama"},{0.8,0.9}]]
]]

Percentile Matching Test - 30%-200% strednej hodnoty

ClearAll[percentileMatchingTest3]
percentileMatchingTest3[data_,dist_]:=Module[{survivalFvalues,meanPercentige={0.3,0.5,1,1.3,1.8,2},survivalFvaluesPercent},
survivalFvalues=Map[SurvivalFunction[dist,#*Mean[data]]&,meanPercentige];
survivalFvaluesPercent=Map[#*100&,survivalFvalues]
]

ClearAll[percentileTestTable3]
percentileTestTable3[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,meanPercentige,roundDif1,roundDif2,roundDif3,tabulka},
meanPercentige={"30%","50%","100%","130%","180%","200%"};
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
roundDif1=Map[DecimalForm[#,{4,3}]&,dif1];
roundDif2=Map[DecimalForm[#,{4,3}]&,dif2];
roundDif3=Map[DecimalForm[#,{4,3}]&,dif3];
tabulka=TableForm[{meanPercentige,empTest,roundDif1,roundDif2,roundDif3},TableDirections->Row,TableHeadings->{{"Percentige of mean","Empirical","Gamma","Log N","Translated Gamma"},None},TableAlignments->Center,TableSpacing->{5,2}]
]

Plot pre Percentile Matching Test - 30%-210%

ClearAll[fitTestPlot30]
fitTestPlot30[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,percentiges,plot,joinedGamma,joinedLog,joinedtransGamma},
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
percentiges={30,50,100,130,180,200};
joinedGamma=Transpose[{percentiges,dif1}];
joinedLog=Transpose[{percentiges,dif2}];
joinedtransGamma=Transpose[{percentiges,dif3}];
plot= Show[Plot[0,{x,25,215},PlotStyle->{Red,Thick},PlotRange->{{25,215},All},AxesOrigin->{25,-6},GridLines->Automatic,Background->GrayLevel[0.95],PlotLabel->"Test Percentilovej Zhody",LabelStyle->{Bold,Black},AxesLabel->{Style["% strednej hodnoty",Black],Style["Rozdiel v %",Black]}],
ListPlot[{joinedGamma,joinedLog,joinedtransGamma},PlotTheme->{"PastelColor"},PlotStyle->PointSize[0.025],Joined->True,Mesh->Full,PlotLegends->Placed[{"Gama","Log-Normalne","Posunute gama"},{0.8,0.9}]]
]]

ClearAll[fitTest2Plot30]
fitTest2Plot30[empTest_,gammaTest_,logNTest_,transGTest_]:=Module[{dif1,dif2,dif3,percentiges,plot,joinedGamma,joinedLog,joinedtransGamma},
dif1=empTest-gammaTest;
dif2=empTest-logNTest;
dif3=empTest-transGTest;
percentiges={30,50,100,130,180,200};
joinedGamma=Transpose[{percentiges,dif1}];
joinedLog=Transpose[{percentiges,dif2}];
joinedtransGamma=Transpose[{percentiges,dif3}];
plot= Show[Plot[0,{x,25,215},PlotStyle->{Red,Thick},PlotRange->{{25,215},All},AxesOrigin->{25,-15},GridLines->Automatic,Background->GrayLevel[0.95],PlotLabel->"Test Percentilovej Zhody",LabelStyle->{Bold,Black},AxesLabel->{Style["% strednej hodnoty",Black],Style["Rozdiel v %",Black]}],
ListPlot[{joinedGamma,joinedLog,joinedtransGamma},PlotTheme->{"PastelColor"},PlotStyle->PointSize[0.025],Joined->True,Mesh->Full,PlotLegends->Placed[{"Gama","Log-Normalne","Posunute gama"},{0.8,0.3}]]
]]

Percentile Matching Test pre Edgeworthovu aproximaciu

ClearAll[percentileMatchingTest2]
percentileMatchingTest2[dist_]:=Module[{survivalFvalues,matchingValues={-2,-1,-0.5,0.5,1,2},survivalFvaluesPercent},
survivalFvalues=Map[SurvivalFunction[dist,#]&,matchingValues];
survivalFvaluesPercent=Map[#*100&,survivalFvalues]
]

ClearAll[percentileTestTable2]
percentileTestTable2[empTest_,edgeworthAproxTest_]:=Module[{dif1,matchingValues,roundDif1,roundDif2,roundDif3,tabulka},
matchingValues={"x=-2","x=-1","x=-1/2","x=1/2","x=1","x=2"};
dif1=empTest-edgeworthAproxTest;
roundDif1=Map[DecimalForm[#,{4,3}]&,dif1];
tabulka=TableForm[{matchingValues,empTest,roundDif1},TableDirections->Row,TableHeadings->{{"Matching values","Empirical","Edgeworth Aproximation"},None},TableAlignments->Center,TableSpacing->{5,2}]
]

Edgeworthova aproximacia

ClearAll[edgeworthAproximation]
edgeworthAproximation[data_]:=Module[{Z,a3,a4,normalPdf,D3,D4,D6,aprox,dist},Z=Standardize[data];a3=Moment[Z,3]/6;a4=(Moment[Z,4]-3)/24;normalPdf=PDF[NormalDistribution[],x];
D3=D[normalPdf,{x,3}];
D4=D[normalPdf,{x,4}];
D6=D[normalPdf,{x,6}];
aprox=SetPrecision[normalPdf-a3*D3+a4*D4+((a3^2)/2)*D6,20]//Simplify;
dist=ProbabilityDistribution[aprox,{x,-Infinity,Infinity}]
]

Vysky skod s Gamma rozdelenim

Useknute gamma rozdelenie

ClearAll[truncatedGammaDistribution]
truncatedGammaDistribution[α_,β_]:=TruncatedDistribution[{0,30},GammaDistribution[α,β]]

Počty škôd  Poisson(100), Výšky škôd Gamma rozdelenie so strednou hodnotou 15k

Pocty skod:

SeedRandom[1032];
ClearAll[poctySkod100]
poctySkod100=RandomVariate[PoissonDistribution[100],5000];

Vysky skod:

(*strednu hodnotu volim 15, budem uvazovat v tisicoch*)

SeedRandom[1032];
ClearAll[vyskySkod100]
vyskySkod100=Map[RandomVariate[truncatedGammaDistribution[5,3],#]&,poctySkod100];

Prislusne uhrny skod:

ClearAll[uhrnySkod100]
uhrnySkod100=Map[Total[#]&,vyskySkod100];

empiricDist100=EmpiricalDistribution[uhrnySkod100];

Mean[uhrnySkod100]

Odhady parametrov

(*Odhad parametrov pre rozdelenie uhrnov skod uvazujuc 2 parametricke Gamma rozdelenie. 
Pouzijem aj MLE aj metodu momentov*)

ClearAll[gammaMLE100]
gammaMLE100=FindDistributionParameters[uhrnySkod100,GammaDistribution[α,β]]

ClearAll[gammaMM100]
gammaMM100=FindDistributionParameters[uhrnySkod100,GammaDistribution[α,β],ParameterEstimator->"MethodOfMoments"]

(*Odhady pre 2 parametricke Log-normal rozdelenie*)

ClearAll[logNorMLE100]
logNorMLE100=FindDistributionParameters[uhrnySkod100,LogNormalDistribution[μ,σ]]

ClearAll[logNorMM100]
logNorMM100=FindDistributionParameters[uhrnySkod100,LogNormalDistribution[μ,σ],ParameterEstimator->"MethodOfMoments"]

(*Odhady pre posunute gamma rozdelnie*)

ClearAll[transGammaMM100]
transGammaMM100=FindDistributionParameters[uhrnySkod100,translatedGammaDistribution[α,β,κ],ParameterEstimator->"MethodOfMoments"]

Percentile matching test

ClearAll[empTest100]
empTest100=percentileMatchingTest[uhrnySkod100,empiricDist100]

ClearAll[gammaTest100]
gammaTest100=percentileMatchingTest[uhrnySkod100,GammaDistribution[gammaMM100[[1,2]],gammaMM100[[2,2]]]]

ClearAll[logNTest100]
logNTest100=percentileMatchingTest[uhrnySkod100,LogNormalDistribution[logNorMM100[[1,2]],logNorMM100[[2,2]]]]

ClearAll[transGammaTest100]
transGammaTest100=percentileMatchingTest[uhrnySkod100,translatedGammaDistribution[transGammaMM100[[1,2]],transGammaMM100[[2,2]],transGammaMM100[[3,2]]]]

ClearAll[tabulkaTest100]
tabulkaTest100=percentileTestTable[empTest100,gammaTest100,logNTest100,transGammaTest100]

Plot vysledkov testu

ClearAll[testPlot100]
testPlot100=fitTestPlot80[empTest100,gammaTest100,logNTest100,transGammaTest100]

Edgeworthova aproximacia

ClearAll[aprox100,standardizedZ100]
aprox100=edgeworthAproximation[uhrnySkod100];
standardizedZ100=Standardize[uhrnySkod100];
standardizedEmpDist100=EmpiricalDistribution[standardizedZ100];

(*Percentile matching test*) -> (*nemoze dobre fungovat pretoze hocijake % of mean je 0, pretoze mean je 0*)->(*Treba pocitat pre ine porovnavane hodnoty*)

ClearAll[testAprox100]
testAprox100=percentileMatchingTest2[aprox100]

ClearAll[testStandardizedEmp100]
testStandardizedEmp100=percentileMatchingTest2[standardizedEmpDist100]

ClearAll[tabulkaEdgeworth100]
tabulkaEdgeworth100=percentileTestTable2[testStandardizedEmp100,testAprox100]

Počty škôd  Poisson(500), Výšky škôd Gamma rozdelenie so strednou hodnotou 10k

pocty skod:

SeedRandom[1035];
ClearAll[poctySkod500]
poctySkod500=RandomVariate[PoissonDistribution[500],5000];

vysky skod:

SeedRandom[1035];
ClearAll[vyskySkod500]
vyskySkod500=Map[RandomVariate[truncatedGammaDistribution[5,2],#]&,poctySkod500];

uhrny skod :

ClearAll[uhrnySkod500]
uhrnySkod500=Map[Total[#]&,vyskySkod500];

ClearAll[empiricDist500]
empiricDist500=EmpiricalDistribution[uhrnySkod500];

Mean[uhrnySkod500]

Odhady parametrov :

(*Dvojparametricke gamma,pouzivam uz len metodu momentov*)

ClearAll[gammaMM500]
gammaMM500=FindDistributionParameters[uhrnySkod500,GammaDistribution[α,β],ParameterEstimator->"MethodOfMoments"]

(*Dvojparametricke log-normalne*)

ClearAll[logNorMM500]
logNorMM500=FindDistributionParameters[uhrnySkod500,LogNormalDistribution[μ,σ],ParameterEstimator->"MethodOfMoments"]

(*Posunute gamma*)

ClearAll[transGammaMM500]
transGammaMM500=FindDistributionParameters[uhrnySkod500,translatedGammaDistribution[α,β,κ],ParameterEstimator->"MethodOfMoments"]

Percentile Matching Test

ClearAll[empTest500]
empTest500=percentileMatchingTest90[uhrnySkod500,empiricDist500]

ClearAll[gammaTest500]
gammaTest500=percentileMatchingTest90[uhrnySkod500,GammaDistribution[gammaMM500[[1,2]],gammaMM500[[2,2]]]]

ClearAll[logNTest500]
logNTest500=percentileMatchingTest90[uhrnySkod500,LogNormalDistribution[logNorMM500[[1,2]],logNorMM500[[2,2]]]]

ClearAll[transGammaTest500]
transGammaTest500=percentileMatchingTest90[uhrnySkod500,translatedGammaDistribution[transGammaMM500[[1,2]],transGammaMM500[[2,2]],transGammaMM500[[3,2]]]]

ClearAll[tabulkaTest500]
tabulkaTest500=percentileTestTable90[empTest500,gammaTest500,logNTest500,transGammaTest500]

Graf vysledkov testu

ClearAll[testPlot500]
testPlot500=fitTestPlot90[empTest500,gammaTest500,logNTest500,transGammaTest500]

Edgeworthova aproximacia

ClearAll[standardizedZ500,aprox500,standardizedEmpDist500]
standardizedZ500=Standardize[uhrnySkod500];
standardizedEmpDist500=EmpiricalDistribution[standardizedZ500];
aprox500=edgeworthAproximation[uhrnySkod500];

ClearAll[testStandardizedEmp500]
testStandardizedEmp500=percentileMatchingTest2[standardizedEmpDist500]

ClearAll[testAprox500]
testAprox500=percentileMatchingTest2[aprox500]

ClearAll[tabulkaEdgeworth500]
tabulkaEdgeworth500=percentileTestTable2[testStandardizedEmp500,testAprox500]

Graf

Show[
Histogram[standardizedZ500,25,"PDF",PlotRange->{{-4,4},{0,0.45}},ChartStyle->"Pastel",AxesOrigin->{0,0}],
Plot[PDF[aprox500,x],{x,-4,4},PlotStyle->{Red,Dashed,Thick}]
]

Počty škôd  Poisson(1000), Výšky škôd Gamma rozdelenie so strednou hodnotou 6k

Pocty skod

ClearAll[poctySkod1000]
SeedRandom[1030];
poctySkod1000=RandomVariate[PoissonDistribution[1000],5000];

Vysky skod

ClearAll[vyskySkod1000]
SeedRandom[1030];
vyskySkod1000=Map[RandomVariate[truncatedGammaDistribution[3,2],#]&,poctySkod1000];

Uhrny skod

ClearAll[uhrnySkod1000]
uhrnySkod1000=Map[Total[#]&,vyskySkod1000];

ClearAll[empiricDist1000]
empiricDist1000=EmpiricalDistribution[uhrnySkod1000];

Mean[uhrnySkod1000]

Odhady parametrov:

(*Gamma rozdelenie*)

ClearAll[gammaMM1000]
gammaMM1000=FindDistributionParameters[uhrnySkod1000,GammaDistribution[α,β],ParameterEstimator->"MethodOfMoments"]

(*Log-Normalne*)

ClearAll[logNorMM1000]
logNorMM1000=FindDistributionParameters[uhrnySkod1000,LogNormalDistribution[μ,σ],ParameterEstimator->"MethodOfMoments"]

(*Posunute Gamma*)

ClearAll[transGammaMM1000]
transGammaMM1000=FindDistributionParameters[uhrnySkod1000,translatedGammaDistribution[α,β,κ],ParameterEstimator->"MethodOfMoments"]

Percentile Matching Test

ClearAll[empTest1000]
empTest1000=percentileMatchingTest90[uhrnySkod1000,empiricDist1000];

ClearAll[gammaTest1000]
gammaTest1000=percentileMatchingTest90[uhrnySkod1000,GammaDistribution[gammaMM1000[[1,2]],gammaMM1000[[2,2]]]];

ClearAll[logNTest1000]
logNTest1000=percentileMatchingTest90[uhrnySkod1000,LogNormalDistribution[logNorMM1000[[1,2]],logNorMM1000[[2,2]]]];

ClearAll[transGammaTest1000]
transGammaTest1000=percentileMatchingTest90[uhrnySkod1000,translatedGammaDistribution[transGammaMM1000[[1,2]],transGammaMM1000[[2,2]],transGammaMM1000[[3,2]]]];

ClearAll[tabulkaTest1000]
tabulkaTest1000=percentileTestTable90[empTest1000,gammaTest1000,logNTest1000,transGammaTest1000]

Graf vysledkov testu

ClearAll[testPlot1000]
testPlot1000=fitTest2Plot90[empTest1000,gammaTest1000,logNTest1000,transGammaTest1000]

Edgeworthova aproximacia

ClearAll[standardizedZ1000,aprox1000,standardizedEmpDist1000]
standardizedZ1000=Standardize[uhrnySkod1000];
standardizedEmpDist1000=EmpiricalDistribution[standardizedZ1000];
aprox1000=edgeworthAproximation[uhrnySkod1000];

ClearAll[testStandardizedEmp1000]
testStandardizedEmp1000=percentileMatchingTest2[standardizedEmpDist1000]

ClearAll[testAprox1000]
testAprox1000=percentileMatchingTest2[aprox1000]

ClearAll[edgeworthTable1000]
edgeworthTable1000=percentileTestTable2[testStandardizedEmp1000,testAprox1000]

Vysky skod s Paretovym rozdelenim

Distribucie pre odhady

ClearAll[distribuciaZaistenie]
distribuciaZaistenie[uhrnySkod_,dist_]:=Module[{p0,konstantnaDist,zmiesanaDist},
p0=Probability[x==0,x\[Distributed]uhrnySkod];
konstantnaDist=ProbabilityDistribution[{"CDF",Piecewise[{{1,x>0}},0]},{x,0,Infinity}];
zmiesanaDist=MixtureDistribution[{p0,(1-p0)},{konstantnaDist,dist}]
]

Počty škôd  Poisson(50), Výšky škôd Paretovo rozdelenie so strednou hodnotou 350k, Vrstva 750k x 250k

pocty skod

ClearAll[poctySkodP50]
SeedRandom[1036];
poctySkodP50=RandomVariate[PoissonDistribution[50],50000];

Vysky skod:

ClearAll[vyskySkodP50]
SeedRandom[1036];
vyskySkodP50=Map[RandomVariate[ParetoDistribution[50,7/6],#]&,poctySkodP50];

Uhrny skod

Chceme uvazovat zaistnu vrstvu : 750k x 250k, chceme brat : Min( Max(Skoda-250,0), 750), tj Chcem zobrat jednotlive zoznamy vysok skod a z kazdeho zobrat len min

ClearAll[uhrnySkodP50]
uhrnySkodP50=uhrnySoZaistenim[vyskySkodP50,250,750];

Mean[uhrnySkodP50]

Odhady parametrov

(*2 parametricke gamma*)

ClearAll[gammaZaistenie,gammaMM50P]
gammaZaistenieP50[α_,β_]:=distribuciaZaistenie[uhrnySkodP50,GammaDistribution[α,β]]
gammaMM50P=momentovaMetodaZaistenie2p[uhrnySkodP50,gammaZaistenieP50[α,β],α,β]

(*Log-normalne*)

ClearAll[logNorZaistenie,logNorMM50P]
logNorZaistenieP50[μ_,σ_]:=distribuciaZaistenie[uhrnySkodP50,LogNormalDistribution[μ,σ]];
logNorMM50P=momentovaMetodaZaistenie2p[uhrnySkodP50,logNorZaistenieP50[μ,σ],μ,σ]

(*Posunute gamma*)

ClearAll[transgammaZaistenieP50,transGammaMM50P]
transgammaZaistenieP50[α_,β_,κ_]:=distribuciaZaistenie[uhrnySkodP50,translatedGammaDistribution[α,β,κ]];
transGammaMM50P=momentovaMetodaZaistenie3p[uhrnySkodP50,transgammaZaistenieP50[α,β,κ],α,β,κ]

Percentile matching test

ClearAll[empDistP50]
empDistP50=EmpiricalDistribution[uhrnySkodP50];

ClearAll[empTestP50]
empTestP50=percentileMatchingTest3[uhrnySkodP50,empDistP50]

ClearAll[gammaTestP50]
gammaTestP50=percentileMatchingTest3[uhrnySkodP50,gammaZaistenieP50[gammaMM50P[[1,2]],gammaMM50P[[2,2]]]]

ClearAll[logNTestP50]
logNTestP50=percentileMatchingTest3[uhrnySkodP50,logNorZaistenieP50[logNorMM50P[[1,2]],logNorMM50P[[2,2]]]]

ClearAll[transGammaTestP50]
transGammaTestP50=percentileMatchingTest3[uhrnySkodP50,transgammaZaistenieP50[transGammaMM50P[[1,2]],transGammaMM50P[[2,2]],transGammaMM50P[[3,2]]]]

ClearAll[tabTestP50]
tabTestP50=percentileTestTable3[empTestP50,gammaTestP50,logNTestP50,transGammaTestP50]

Plot vysledkov testu

ClearAll[testPlotP50]
testPlotP50=fitTestPlot30[empTestP50,gammaTestP50,logNTestP50,transGammaTestP50]

Edgeworthova aproximacia

ClearAll[aproxP50,standardizedZP50,standardizedEmpDistP50]
aproxP50=edgeworthAproximation[uhrnySkodP50];
standardizedZP50=Standardize[uhrnySkodP50];
standardizedEmpDistP50=EmpiricalDistribution[standardizedZP50];

ClearAll[testAproxP50]
testAproxP50=percentileMatchingTest2[aproxP50]

ClearAll[testStandardizedEmpP50]
testStandardizedEmpP50=percentileMatchingTest2[standardizedEmpDistP50]

ClearAll[tabulkaEdgeworthP50]
tabulkaEdgeworthP50=percentileTestTable2[testStandardizedEmpP50,testAproxP50]

Počty škôd  Poisson (10), Výšky škôd Paretovo rozdelenie so strednou hodnotou 2.2m,  Vrstva 4m x 1m

Pocty skod:

ClearAll[poctySkodP10]
SeedRandom[1037];
poctySkodP10=RandomVariate[PoissonDistribution[10],50000];

Vysky skod :

ClearAll[vyskySkodP10]
SeedRandom[1037];
vyskySkodP10=Map[RandomVariate[ParetoDistribution[300,22/19],#]&,poctySkodP10];

Uhrny skod:

ClearAll[uhrnySkodP10]
uhrnySkodP10=uhrnySoZaistenim[vyskySkodP10,1000,4000];

Mean[uhrnySkodP10]

Odhady parametrov:

(*2 parametricke Gamma*)

ClearAll[gammaZaistenieP10,gammaMM10P]
gammaZaistenieP10[α_,β_]:=distribuciaZaistenie[uhrnySkodP10,GammaDistribution[α,β]]
gammaMM10P=momentovaMetodaZaistenie2p[uhrnySkodP10,gammaZaistenieP10[α,β],α,β]

(*Log-Normalne*)

ClearAll[logNorZaistenieP10,logNorMM10P]
logNorZaistenieP10[μ_,σ_]:=distribuciaZaistenie[uhrnySkodP10,LogNormalDistribution[μ,σ]];
logNorMM10P=momentovaMetodaZaistenie2p[uhrnySkodP10,logNorZaistenieP10[μ,σ],μ,σ]

(*3 parametricke Gamma*)

ClearAll[transgammaZaistenieP10,transGammaMM10P]
transgammaZaistenieP10[α_,β_,κ_]:=distribuciaZaistenie[uhrnySkodP10,translatedGammaDistribution[α,β,κ]];
transGammaMM10P=momentovaMetodaZaistenie3p[uhrnySkodP10,transgammaZaistenieP10[α,β,κ],α,β,κ]

Percentile matching test

ClearAll[empDistP10]
empDistP10=EmpiricalDistribution[uhrnySkodP10];

ClearAll[empTestP10]
empTestP10=percentileMatchingTest3[uhrnySkodP10,empDistP10]

ClearAll[gammaTestP10]
gammaTestP10=percentileMatchingTest3[uhrnySkodP10,gammaZaistenieP10[gammaMM10P[[1,2]],gammaMM10P[[2,2]]]]

ClearAll[logNTestP10]
logNTestP10=percentileMatchingTest3[uhrnySkodP10,logNorZaistenieP10[logNorMM10P[[1,2]],logNorMM10P[[2,2]]]]

ClearAll[transGammaTestP10]
transGammaTestP10=percentileMatchingTest3[uhrnySkodP10,transgammaZaistenieP10[transGammaMM10P[[1,2]],transGammaMM10P[[2,2]],transGammaMM10P[[3,2]]]]

ClearAll[tabTestP10]
tabTestP10=percentileTestTable3[empTestP10,gammaTestP10,logNTestP10,transGammaTestP10]

Plot vysledkov testu

ClearAll[testPlotP10]
testPlotP10=fitTest2Plot30[empTestP10,gammaTestP10,logNTestP10,transGammaTestP10]

Edgeworthova aproximacia

ClearAll[aproxP10,standardizedZP10,standardizedEmpDistP10]
aproxP10=edgeworthAproximation[uhrnySkodP10];
standardizedZP10=Standardize[uhrnySkodP10];
standardizedEmpDistP10=EmpiricalDistribution[standardizedZP10];

ClearAll[testAproxP10]
testAproxP10=percentileMatchingTest2[aproxP10]

ClearAll[testStandardizedEmpP10]
testStandardizedEmpP10=percentileMatchingTest2[standardizedEmpDistP10]

ClearAll[tabulkaEdgeworthP10]
tabulkaEdgeworthP10=percentileTestTable2[testStandardizedEmpP10,testAproxP10]