I am trying to create a compiled version of this function:
cKalmanTimeLoop = Compile[{{Y, _Real, 2}, {X, _Real, 2}, {a, _Real, 2}, {b1, _Real,
2}, {Ve, _Real}, {Vw, _Real, 2}, {Covb1, _Real, 2}},
Module[{Yhat, b, , k, n, e, t, Q, K, Covb, Zscore
(************************************************************)
(************************************************************)
(**UPDATES THE KALMAN FILTER PARAMETERS FOR EACH TIMESTEP**)
(************************************************************)
(*X=k x n matrix of k prices data series for t=1,...,n;
note that X(t)[[1,All]]=1 for all t;
a=k x k State Transition matrix estimated in initialization;
b=k x n matrix of state vectors b(t)={b1,...,bk},Ve=
scalar price error variance,estimated in initialization;
Vw=k x k state noise covariance matrix,estimated in initialization;
Covb[[t]]=k x k state covariance matrix at time t=1,...,n;*)
(***********************************************************)
{k, n} = Dimensions@X;
K = 0.;
e = ConstantArray[0., n];
Q = ConstantArray[0., n];
b = ConstantArray[0., {k, n}];
Covb = ConstantArray[0., {n, k, k}];
b[[All, 1]] = b1;
Covb[[1]] = Covb1;
Do[
(*************************************************)
(*update state prediction and state covariance matrix*)
If[t > 1, b[[All, t]] = a.b[[All, t - 1]];
Covb[[t]] = a.Covb[[t - 1]].Transpose[a] + Vw;
Covb[[t]] = DiagonalMatrix@Diagonal@Covb[[t]];];
(*************************************************)
(*Measurement Prediction Equation*)
Yhat = X[[All, t]].b[[All, t]];
(*************************************************)
(*Measurement Prediction Error*)
e[[t]] = Y[[t]] - Yhat;
(*************************************************)
(*Measurement Covariance Prediction*)
Q[[t]] = X[[All, t]].Covb[[t]].X[[All, t]] + Ve;
(*************************************************)
(*Kalman Gain*)
K = Covb[[t]].X[[All, t]]/Q[[t]];
(*************************************************)
(*State Update*)
b[[All, t]] = b[[All, t]] + K*e[[t]];
(*************************************************)
(*State Covariance Update*)
Covb[[t]] = Covb[[t]] - DiagonalMatrix[K*X[[All, t]]].Covb[[t]];
, {t, 1, n}];
Zscore = e/Sqrt[Q];
{b, Covb, K, e, Q, Zscore}], {{Covb, _Real, 3}},
Parallelization -> True, CompilationTarget -> "C"]
But I cant seem to get past the following error message:
Compile::cplist: Compile`$28.Covb[[t]] should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function.
I assume that I am making some elementary syntax error, but I can't spot it.
Also, any suggestions for other possible speed-ups to the code would be gratefully received.