FUNKCIE

(*Mathematica Raw Program*)MenoSuboru="";
data={};
SkusobnaVzorka={};
TestovaciaVzorka={};
gdata;
gSkusobnaVzorka;
gTestovaciaVzorka;
X={};
Y={};
Phi={};

<<"timeseri\\datasmoo.m";
<<"timeseri\\timeseri.m";
<<"timeseri\\userfunc.m";
<<"LinearRegression`";
<<"NonlinearRegression`";
<<"HypothesisTesting`";

NahratieBalikovNaAnalyzuCasovychRadov[]:=Module[{},Print["???"];];

NastaveniePracovnehoAdresara[]:=Module[{},SetDirectory[NotebookDirectory[]];];

NastaveniePodmienokPreListPlot[]:=Module[{},SetOptions[ListPlot,PlotRange->All,Joined->True,AxesOrigin->{0,0}];];

NacitanieDatPart1[]:=Module[{},MenoSuboru=NotebookDirectory[]<>"data\\hydrologicke\\boca.txt";
Print[FileNameSetter[Dynamic[MenoSuboru],Appearance->"Open"]];];

NacitanieDatPart2[]:=Module[{},Print[MenoSuboru];
data=ReadList[MenoSuboru];];

NacitanieDatPart3[]:=Module[{},If[Length[data]==854,{data=Drop[data,2];data=Table[Mean[data[[1+i;;12+i]]],{i,0,840,12}];}];
Print[data];];

VykreslitCasovyRad[]:=Module[{},gdata=ListPlot[data,PlotLabel->Style["Casovy Rad",Large,Bold],AxesLabel->{Style["time [-]"],Style["value []"]}];
Print[Show[gdata]];];

VypocetZakladnychCharakteristikCasovehoRadu[]:=Module[{},Print["Vyberova stredna hodnota: ",Mean[data]];
Print["Vyberova smerodajna odchylka: ",StandardDeviation[data]];
Print["Vyberovy rozptyl: ",Variance[data]];];

VytvoritSkusobnuVzorku[]:=Module[{},SkusobnaVzorka=Take[data,-5];
Print[SkusobnaVzorka];];

VytvoritTestovaciuVzorku[]:=Module[{},TestovaciaVzorka=Drop[data,-5];
Print[TestovaciaVzorka];];

VykreslitSkusobnuVzorku[]:=Module[{},gSkusobnaVzorka=ListPlot[SkusobnaVzorka,PlotLabel->Style["Skusobna Vzorka",Large,Bold],AxesLabel->{Style["time [-]"],Style["value []"]}];
Print[Show[gSkusobnaVzorka]];];

VykreslitTestovaciuVzorku[]:=Module[{},gTestovaciaVzorka=ListPlot[TestovaciaVzorka,PlotLabel->Style["Testovacia Vzorka",Large,Bold],AxesLabel->{Style["time [-]"],Style["value []"]}];
Print[Show[gTestovaciaVzorka]];];

VypocetZakladnychCharakteristikTestovacejVzorky[]:=Module[{},Print["Vyberova stredna hodnota: ",Mean[TestovaciaVzorka]];
Print["Vyberova smerodajna odchylka: ",StandardDeviation[TestovaciaVzorka]];
Print["Vyberovy rozptyl: ",Variance[TestovaciaVzorka]];];

VypocetACF[]:=Module[{cf,gcf},Print["WhatDoesItDo?"];
cf=CorrelationFunction[TestovaciaVzorka,Length[TestovaciaVzorka]-1];
gcf=myplotcorr1[cf,2/Sqrt[Length[TestovaciaVzorka]],AxesLabel->{k,Subscript[p,k]},PlotLabel->Style["Autocorrelation function",Large,Bold],PlotRange->All];
Print[Show[gcf]];];

VypocetPACF[]:=Module[{pcf,gpcf},pcf=PartialCorrelationFunction[TestovaciaVzorka,Length[TestovaciaVzorka]-1];
gpcf=myplotcorr1[pcf,2/Sqrt[Length[TestovaciaVzorka]],AxesLabel->{k,Subscript[p,k]},PlotLabel->Style["Partial Autocorrelation function",Large,Bold],PlotRange->All];
Print[Show[gpcf]];];

LevinsonDurbin[n_]:=Module[{ARModels,MinAIC,MinBIC,pAIC,pBIC},ARModels=LevinsonDurbinEstimate[TestovaciaVzorka,n];
(*Print[ARModels];*)MinAIC=ARModels[[Position[Table[AIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}],Min[Table[AIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}]]][[1,1]]]];
pAIC=Length[MinAIC[[1]]];
Print["Minimalna hodnota AIC je ",Min[Table[AIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}]]," modelu ",MinAIC];
Print["Vhodny rad pre kriterium AIC je p = ",pAIC];
MinBIC=ARModels[[Position[Table[BIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}],Min[Table[BIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}]]][[1,1]]]];
pBIC=Length[MinBIC[[1]]];
Print["Minimalna hodnota BIC je ",Min[Table[BIC[ARModels[[i]],Length[TestovaciaVzorka]],{i,1,n}]]," modelu ",MinBIC];
Print["Vhodny rad pre kriterium BIC je p = ",pBIC];];

UrcenieMinMaxParametraC[]:=Module[{tvSort,k1,k2,hmin,hmax},tvSort=Sort[TestovaciaVzorka];
k1=Round[0.075*Length[TestovaciaVzorka]];
k2=Round[0.925*Length[TestovaciaVzorka]];
hmin=tvSort[[k1]];
hmax=tvSort[[k2]];
Print["Minimum parametra c, hmin = ",hmin];
Print["Maximum parametra c, hmax = ",hmax];];

ParameterP[p1_,p2_]:=Module[{},Return[Max[p1,p2]];];

ParameterK[p1_,p2_,d_]:=Module[{},Return[Max[p1,p2,d]];];

FunkciaX[t_,p_,c_,d_,Phi_,sigma_]:=Module[{},(*Return[If[TestovaciaVzorka[[t-d]]≤c,Sum[Y[[i]]*Phi[[1,i]],{i,1,Length[Y]}],Sum[Y[[i]]*Phi[[2,i]],{i,1,Length[Y]}]]+RandomReal[NormalDistribution[0,sigma]]]*)Return[If[TestovaciaVzorka[[t-d]]≤c,Phi[[1,1]]+Sum[TestovaciaVzorka[[t-i]]*Phi[[1,i]],{i,2,p}],Phi[[2,1]]+Sum[TestovaciaVzorka[[t-i]]*Phi[[2,i]],{i,2,p}]]+RandomReal[NormalDistribution[0,sigma]]]];

FunkciaXTable[p_,c_,d_,Phi_,sigma_]:=Module[{n,Xt,p1,p2},(*Return[If[TestovaciaVzorka[[t-d]]≤c,Sum[Y[[i]]*Phi[[1,i]],{i,1,Length[Y]}],Sum[Y[[i]]*Phi[[2,i]],{i,1,Length[Y]}]]+RandomReal[NormalDistribution[0,sigma]]]*)n=Length[TestovaciaVzorka];
Xt=Table[0,{i,1,n}];
p1=0;
p2=0;
For[t=1+p,t≤n,t++,If[TestovaciaVzorka[[t-d]]≤c,Xt[[t]]=Phi[[1,1]]+Sum[TestovaciaVzorka[[t-i]]*Phi[[1,i]],{i,2,p}]+RandomReal[NormalDistribution[0,sigma]];p1++,Xt[[t]]=Phi[[2,1]]+Sum[TestovaciaVzorka[[t-i]]*Phi[[2,i]],{i,2,p}]+RandomReal[NormalDistribution[0,sigma]];p2++]];
Return[{Xt,p1,p2}];];

FunkciaY[t_,p_]:=Module[{},Return[Prepend[Table[{TestovaciaVzorka[[t-i]]},{i,1,p}],{1}]];];

FunkciaYc[t_,p_,c_,d_]:=Module[{Yt},Yt=FunkciaY[t,p];
(*Print["Yt"];
Print[Yt];*)If[TestovaciaVzorka[[t-d]]>c,Return[Flatten[{Yt*0,Yt*1},1]],Return[Flatten[{Yt*1,Yt*0},1]]];];

FunkciaPhic[p_,c_,d_]:=Module[{Ytc,n,k,part1,part2,spolu},n=Length[TestovaciaVzorka];
k=ParameterK[p,p,d];
part1=Inverse[Sum[FunkciaYc[t,p,c,d].Transpose[FunkciaYc[t,p,c,d]],{t,k+1,n}]];
part2=Sum[FunkciaYc[t,p,c,d]*TestovaciaVzorka[[t]],{t,k+1,n}];
spolu=part1.part2;
Return[spolu];];

FunkciaRez[t_,p_,c_,d_,Phic_]:=Module[{},Return[TestovaciaVzorka[[t]]-Transpose[Phic].FunkciaYc[t,p,c,d]];];

FunkciaRoz[p_,c_,d_,Phic_]:=Module[{n,k},n=Length[TestovaciaVzorka];
k=ParameterK[p,p,d];
Return[(1/(n-k))*Sum[FunkciaRez[t,p,c,d,Phic]^2,{t,k+1,n}]];];

FunkciaAIC[p1_,p2_,n1_,n2_,sigma1_,sigma2_]:=Module[{},Return[n1*Log[sigma1]+2*(p1+1)+n2*Log[sigma2]+2*(p2+1)];];

FunkciaBIC[p1_,p2_,n1_,n2_,sigma1_,sigma2_]:=Module[{},Return[n1*Log[sigma1]+(p1+1)*Log[n1]+n2*Log[sigma2]+(p2+1)*Log[n2]];];

ParametreSETAR[pmax_,hmin_,hmax_]:=Module[{n,p,d,c,Phic,Roz,Xt,sigma,n1,n2,ok,minRoz,i,k,t,Rez1,Rez2,Roz1,Roz2,minc,AIC,BIC,mincPhic},n=Length[TestovaciaVzorka];
For[p=1,p≤pmax,p++,{For[d=1,d≤p,d++,{k=ParameterK[p,p,d];
Roz={};
For[c=hmin,c≤hmax,c=c+((hmax-hmin)/10),{Phic=FunkciaPhic[p,c,d];
(*Print[Flatten[Phic]];*)(*Roz=FunkciaRoz[p,c,d,Phic];
Print[Flatten[Roz]];*)AppendTo[Roz,{c,FunkciaRoz[p,c,d,Phic][[1,1]]}];}];
minRoz=Min[Roz[[All,2]]];
minc=Roz[[Position[Roz,minRoz][[1,1]],1]];
(*Print[minc];
Print[minRoz];*)Rez1={};
Rez2={};
n1=0;
n2=0;
mincPhic=FunkciaPhic[p,minc,d];
For[t=k+1,t≤n,t++,{If[TestovaciaVzorka[[t-d]]≤c,{n1++;AppendTo[Rez1,FunkciaRez[t,p,minc,d,mincPhic]];},{n2++;AppendTo[Rez2,FunkciaRez[t,p,minc,d,mincPhic]];}];}];
(*Print[n1];
Print[n2];
Print[Flatten[Rez1,2]];
Print[Flatten[Rez2,2]];*)Roz1=Variance[Flatten[Rez1,2]];
Roz2=Variance[Flatten[Rez2,2]];
AIC=FunkciaAIC[p,p,n1,n2,Roz1,Roz2];
BIC=FunkciaBIC[p,p,n1,n2,Roz1,Roz2];
Print["p: ",p," d: ",d," c: ",minc," AIC: ",AIC," BIC: ",BIC];}];}];];

CAST_1.1

NahratieBalikovNaAnalyzuCasovychRadov[];

NastaveniePracovnehoAdresara[];

NastaveniePodmienokPreListPlot[];

NacitanieDatPart1[];

CAST_1.2

NacitanieDatPart2[];

NacitanieDatPart3[];

VykreslitCasovyRad[]

VypocetZakladnychCharakteristikCasovehoRadu[]

VytvoritSkusobnuVzorku[]

VytvoritTestovaciuVzorku[]

VykreslitSkusobnuVzorku[]

VykreslitTestovaciuVzorku[]

VypocetZakladnychCharakteristikTestovacejVzorky[]

VypocetACF[]

VypocetPACF[]

LevinsonDurbin[10]

CAST_2

UrcenieMinMaxParametraC[]

ParametreSETAR[3,12.5366,19.7076]