Initialize

<< FGN.m

<< FitARMA.m

<< Statistics`

ARMA Simulation Example

m = 100; n = 200; ℕ = n + m; Muz = 0. ; 
    phi1 = 0.5; phi = {phi1}; theta = {};
maxLead = 7;
SeedRandom[72177791];
r = TacvfARMA[phi, theta, ℕ];
zTOT = 
        SimulateGaussianARMA[phi, 
          theta, ℕ];
z = Take[zTOT, n];
zFF = vFF = 
          Table[0, {maxLead}, {m + 1}];
ans = Array[0 &,  maxLead];

\(Do[
znew = Drop[zTOT, n]; 
zF = \(vF = Array[0 &, m + 1]\); 

      G = toe[Take[r, n]]; 
gk = Take[Drop[r, k], n]; 

      GI = TrenchInverse[G]; 

      zF[[1]] = 
        Muz + Reverse[gk] . GI . \((z - Muz)\); 

      vF[[1]] = 
        r[[1]] - gk . GI . gk; 

      zk = z; 
VarF = Total[Table[phi1\^\(2  i\), {i, 0, k - 1}]]; 

      Do[
gk = 
          Append[gk, 
            r[[n + j - 
                1]]]; 
zk = 
          Append[zk, 
            zTOT[[n + j - 
                1]]]; 
GI = 
          TrenchInverseUpdate[{r[[
                n + j]]}]; 
zF[[j]] = 
          Muz + Reverse[gk] . 
              GI . \((zk - Muz)\); 
vF[[
            j]] = 
          r[[1]] - 
            gk . GI . gk;, 
{j, 2, m + 1}]; 

      ans[[k]] = {Mean[
            errMMSE = \((Drop[znew, k - 1] - Drop[zF, \(-k\)])\)\^2], 
          MeanCI[errMMSE], VarF}; 

      zFF[[k]] = zF; 

      vFF[[k]] = 
        vF; 
Print[" ok done. lead time: ", 
        k];
, {k, maxLead}]\)

\(TableForm[ans, TableDepth -> 2, 
      TableHeadings -> {None, {"Emp MSE", "C.I.", "MSE"}}]\)

Faster Vectorized Computation

FastMatrixForecast = 
        Compile[{{GL,  _Real, 2}, {M, _Real, 2}, {z, _Real, 1}}, 
          With[{P = GL . M}, {P . z, 
              Table[P[[i]] . 
                  GL[[i]], {i, 
                  Dimensions[GL] // 
                    First}]}]
];

m = 100; n = 200; ℕ = n + m; Muz = 0. ; 
    phi1 = 0.5; phi = {phi1}; theta = {};
maxLead = 7;
SeedRandom[72177791];
r = 
        TacvfARMA[phi, theta, ℕ + maxLead];
zTOT = 
        SimulateGaussianARMA[phi, 
          theta, ℕ];
z = Take[zTOT, n];
zF = vF = Array[0 &, {maxLead + 1}];

znew = Drop[zTOT, n];
zF = vF = Array[0 &, m + 1];
G = toe[Take[r, n]];
GK = 
        Table[r[[n + i - j + 1]], {i, 
            maxLead}, {j, n}];
GI = TrenchInverse[G];
ans = 
        FastMatrixForecast[GK, GI, z - Muz];
F = First[ans];
V = Last[ans];
zF[[1]] = 
        F;
vF[[1]] = 
        V;
zk = z;
VarF = 
        Rest[FoldList[Plus, 0, 
            Table[(phi1^(2  i)), {i, 0, 
                maxLead - 1}]]];
Do[
GK = 
          Table[r[[n + 𝓂 + i - 
                j]], {i, maxLead}, {j, 
              n + 𝓂 - 1}]; 
zk = 
          Append[zk, 
            zTOT[[n + 𝓂 - 
                1]]]; 
GI = 
          TrenchInverseUpdate[{r[[
                n + 𝓂 - 
                  1]]}]; 
{F, V} = 
          FastMatrixForecast[GK, GI, 
            zk - Muz]; 
zF[[𝓂]] = 
          F; 
vF[[𝓂]] = V;
, {𝓂, 2, 
          m + 1}];

zF = Transpose[zF];
ans = 
        Table[{Mean[
              errMMSE = (((Drop[znew, k - 1] - Drop[zF[[k]], -k]))^2)], MeanCI[errMMSE], 
            VarF[[k]]}, {k, 
            maxLead}];

\(TableForm[ans, TableDepth -> 2, 
      TableHeadings -> {None, {"Emp MSE", "C.I.", "MSE"}}]\)

eFR = Abs[zFF - zF]/Abs[zFF];

eFR[[1]]

eFR[[2]]

eFR[[6]]

eFR2 = Abs[zFF - zF]/Abs[zF];

eFR2[[1]]