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

SetDirectory[NotebookDirectory[]];

SetOptions[ListPlot,PlotRange->All,BaseStyle->{11,FontFamily->"Arial"}];
popis=Table[{60+i*120,1936+i*10},{i,0,6}];

data=ReadList["PLN.txt"];
data=Flatten[data];
n=Length[data];

gdata=ListPlot[data,Ticks->{popis,Automatic}, AxesOrigin->{0,0}, AxesLabel->{"time [month]","data [(m^3)/s]"},PlotLabel->"PLN",Joined->True]

μ=Mean[data]
σ=StandardDeviation[data]
σ2=Variance[data]

skus=Take[data,-5]

test=Take[data,n-5]

gskus=ListPlot[skus,PlotLabel->"priemerny kurz euro/skk",AxesLabel->{"time[month]","priemerny kurz"},Joined->True,AxesOrigin->{0,0}]

gtest=ListPlot[test,PlotLabel->"priemerny kurz euro/skk",AxesLabel->{"time[month]","priemerny kurz"},Joined->True]

Save["skus.def",skus]
Save["test.def",test]

μ = Mean[test]
σ = StandardDeviation[test]
σ2 = Variance[test]

corrtest=CorrelationFunction[test,20];
myplotcorr1[corrtest,2/Sqrt[n],AxesLabel->{k,(Subscript[ρ, k])},PlotLabel->"Autocorrelation Function"]

parcorrtest=PartialCorrelationFunction[test,20];
myplotcorr1[parcorrtest,2/Sqrt[n],AxesLabel->{k,(Subscript[ρ, k])},PlotLabel->"Partial Correlation Function"]

armodels=LevinsonDurbinEstimate[test,10];

dis=Map[#[[-1]]&,armodels];

ListPlot[dis,Joined->True,AxesLabel->{"p","sigma2"}]

mod=LevinsonDurbinEstimate[test,20];
Do[Print["p = ", p,"   AIC= ",AIC[mod[[p]],n],"   BIC= ",BIC[mod[[p]],n]],{p,20}]

a1=Flatten[Position[Table[AIC[mod[[i]],n],{i,1,20}],Min[Table[AIC[mod[[i]],n],{i,1,20}]]]];
a2=Flatten[Position[Table[BIC[mod[[i]],n],{i,1,20}],Min[Table[BIC[mod[[i]],n],{i,1,20}]]]];
If[a1==a2,Print[mod[[a1]]]]

Print["Vysledny model je AR2"]