Could anyone help me add one more X into the equation

Could anyone please help me add one more X (variable) into State Space Model, there are currently 1 measurement equation and 1 transition equation. However, to verify 2 variables, I need to add one more X into this measurement equation that makes 2 transition equations.


/* Random.G -- To estimate stochastic beta for the regression of Rs (or Rfx) on X using

Mesaurement Eq: Rs(t) = b0 + Beta(t)*X(t) + e(t)
Transition Eq: beta (t) = beta(t-1) +w(t)
where e(t) ~ Normal(0,sig1) and w(t) ~ Normal(0,sig2). e(t) and w(t) are independent.

The source code has been designed to separate the code that users need to
write for their particular specification and the code that is generally
applicable to the univariate state space form. The generally applicable
code should never need to be altered.

The general State Space Form (SSF) can be written as:

Measurement equation:

yt = Zt * at + dt + et

yt is N by 1
Zt is N by K
at is K by 1
dt is N by 1
et is N by 1

E(et) = 0
Var(et) = h which is N by N

Transition Equation:

at = T * at-1 + ct + Rt * ut

T is K by K
ct is K by 1
Rt is K by L
ut is L by 1 where L <= K.

E(ut) = 0
Var(ut) = Q which is L by L.

*******************##########*********************/

@ Open the Required Library Files @
new; new;
library cml,pgraph;
cmlset; graphset;
tempry = 0; _Inf = 1e+5;
declare tt ?= 0; declare UUse ?= 0; UUse = 0;

load RS[116,1] = e:\liq\data\RS.txt;
load X[116,1] = e:\liq\data\X_IR.txt;
data = RS~X;

UUse = data;

OUTPUT file = e:\result\random.rst reset on; output off;
@-- starting values--@
b0 = .1;
sma1 = .12;

sma2 = 0.12; @-- variance of transition eq. --@
parms = b0|sma1|sma2;

@ Do the Estimation and output results @

_cml_Algorithm = 1;
_cml_Linesearch = 2;
_cml_CovPar = 2; @-- 1= inv of hessian, 2= cross-product of Jacobian, 3=Heteroskedasticity Consistent VCV --@
_cml_DirTol = 1e-5;

@ load parms; @
{parms,f,g,h,retcode} = CML(data,0,&LL,parms); save parms;
cls;

output on;
"II------------------ Parameter Estimates ------------------II";
format /m1 /rd 20,15;
"Starting values: " parms'; print;
para = parm(parms);
vpx = gradp(&parm,parms);
vpx = vpx*h*vpx';
vx =sqrt(diag(vpx));
ppp = para~(para./vx);
"b0: " ppp[1,.];
"SMA1: " ppp[2,.];
"SMA2: " ppp[3,.];

print;

@ Generate the Filtered Data using the Smoother and the natural parm @

{BKvec,VVec,FVec,PPVec,PuVec} = Kalman(parms,data); @ BKvec = FILTERED State Variables @
{BSvec,PSVec} = Smoother(parms,data); @ BSvec = SMOOTHED State Variables @

format /m1 /rd 8,6;

"II----------- Smoothed Beta (t) ----------------II"
BSVec; @--- BSvec = smoothed Beta, BKvec = filtered Beta --@

"II-----------------------------------------------------------------------II";

end;

/* NOW FOR THE PROCEDURES THAT USERS WILL HAVE TO CUSTOMISE FOR THEIR
MODEL*/

/*********************************************************************
INITIAL : Initialises the state vector and its covariance matrix

INPUT:
1. Parms

OUTPUT:
1. Initial State Vector at t0
2. Initial State Covariance Matrix at t0
**********************************************************************/
proc(2) = Initial(parms);
local Bu,Pu;
Bu = zeros(1,1);
Pu = eye(1) .* 1e+5;
retp(Bu,Pu);
endp;

/*********************************************************************
SystemM : Generate the System Matrices

INPUT:
1. Parameter Vector (K by 1)

OUTPUT:
1. System Matrices for the Kalman Filter as described by Harvey
1. yt and Zt
**********************************************************************/
proc(8) = SystemM(parms,UUsez);
local T,H,Q,R,d,c,y,Z;
local b0,a0,a1,sma1,sma2;

b0 = parms[1,1];
sma1 = (parms[2,.])^2;
sma2 = (parms[3,.])^2;

@ Transition Matrix @
T = 1;
@ Transition equation Covariance Matrix @
Q = sma2;
@ Measurement Equation Covariance Matrix @
H = sma1;
@ Transition Equation Error Coefficient Matrix @
R = 1;
@ Transition Equation Interventions @ @-- UUse = R(t)|X(t)--@
c = 0;
@ Measurment Equation Observations @
y = UUsez[1,1];
@ Measurment Equation Matrix@
Z = UUsez[2,1];
@ Measurement Equation Interventions @
d = b0;

retp(T,h,Q,R,d,c,y,Z);
endp;

/* NOW FOR THE PROCEDURES THAT NEVER HAVE TO BE ALTERED */

/*********************************************************************
*******
KALMAN : Generates the states vector using the Kalman Filter
INPUT:
1. Parameters
OUTPUT:
1. Bvec is the State Vector through time. It is a T by 1 vector.
2. Vvec is the T by 1 vector of prediction errors.
3. Fvec is the T by 1 vector of prediction error variances.
4. PpVec vectorised Pp matrices for each observation 1 through to T.
5. PuVec vectorised Pp matrices for each observation 1 through to T.
**********************************************************************
******/

proc(5) = Kalman(parms,data);
local Bp, @ State Vector Prediction @
Bu, @ Updated State Vector @
Bvec, @ stores the state vector for all time (T by K)
matrix @
Pp, @ state covariance prediction @
PpVec, @ Store vectorised Pp matrices for all time @
Pu, @ Updated state covariance estimate @
PuVec, @ Store Pu matrices @
yp, @ predicted measurement variable, Firm Return @
yu, @ Updated measurement variable estimate, Firm
Return @
y,@ Actual Dependent Variable @
Z,@ Actual Explanatory Variables @
V,@ Prediction error @
Vvec, @ Prediction Error vector @
F,@ Prediction error variance @
Prefix, @ Kind of Weighting term in the updating equation
@
Fvec, @ Prediction Error Variance matrix F @
DFvec, @ Determinant of F @
T,Q,H,R,d,c,@ System Matrices @
W,@ the matrix allowing for missing observations @
mask, @ The mask setting 1 for observations that occur @
vmask,Fmask,@ Templates for storing the v and F matrix output
@
i;@ Counter over time periods in the data @

local UUsez;

@ Get the i=0 State vector and Covariance Matrix @
{Bu,Pu} = Initial(parms);

@ Filter i=1..T, using index i cos T already used for a system matrix
@

i = 1;
do while i <= rows(Data);

@ Get the System Matrices and Data @

UUsez = data[i,1]|data[i,2]; UUse[i,.] = UUsez';

{T,H,Q,R,d,c,y,Z} = SystemM(parms,UUsez);

@ Predict the State Vector and Covariance Matrix @
Bp = T*Bu + c;
Pp = T*Pu*T' + R*Q*R';

vmask = miss(ones(rows(y),1),1);
Fmask = miss(ones(rows(y),rows(y)),1);

if not ismiss(packr(y)); @ Some new data exists @

@ Flag missing data. See Harvey FSK pp. 144-145 @
mask = missrv(y,-999999.99) ./= -999999.99;

W = diagrv(eye(rows(y)),mask);
W = delif(W,sumc(W) .== 0);

Z = W * Z;
d = W * d;

@ Predict the next observation on the firm's return @
yp = Z*Bp + d;

@ The prediction error @
v = packr(y) - yp;

@ The prediction error variance at t @
F = Z*Pp*Z' + selif(selif(H,mask)',mask)';

tempry = Z;

@ Update the state vector and Covariance Matrix where
possible @

Prefix = Pp*Z'invpd(F);
Bu = Bp + Prefix*v;
Pu = Pp - Prefix*Z*Pp;

vmask[1:rows(v)] = v;
v = vmask;

Fmask[1:rows(F),1:cols(F)] = F;
F = Fmask;

else;
v = vmask;
F = Fmask;
Bu = Bp;
Pu = Pp;
endif;

@ Store the Filtration Output @
if i /= 1;
Bvec = Bvec | Bu';
Vvec = VVec | v';
Fvec = FVec | F;
PpVec = PpVec | Pp;
PuVec = PuVec | Pu;
else;
Bvec = Bu';
Vvec = v';
Fvec = F;
PpVec = Pp;
PuVec = Pu;
endif;

i = i + 1;
endo;

retp(Bvec,Vvec,Fvec,PpVec,PuVec);
endp;

/*********************************************************************
*******
LL : Calculates the loglikelihood Function for the entire set of
observations

INPUT
1. Parameters.

OUTPUT
1. Loglikelihood function value.
**********************************************************************
******/
proc(1) = LL(Parms,Data);

local BVec, @ Kalman Filter Output @
VVec,
FVec,
PpVec,
PuVec,
F,
v,
n, @ Number of measurement equations @
i,
Logl; @ Log Likelihood function value @

@ Filter the data to extract the prediction error @
{Bvec,Vvec,Fvec,PpVec,PuVec} = Kalman(Parms,Data);

@ Get the number of the Measurement Variables @
n = cols(Vvec);

@ Iterate over Observations for Log Likelihood Computation @

LogL = 0; @ Initialise the vector of log likelihood values @
i = 1;
do while i <= rows(Vvec); @ Get v and F dumping missing observations @ v = packr(Vvec[i,.]'); if not ismiss(v); F = FVec[(i-1)*n+1:(i-1)*n+rows(v),1:rows(v)]; @ Construct the full Likelihood @ LogL = LogL | (n*ln(2*Pi*det(F)) + v'invpd(F)*v); endif; i = i + 1; endo; retp(-0.5*trimr(LogL,1,0)); endp; /********************************************************************* ******* Smoother : Generates the states vector using a Fixed Interval Smoother. INPUT: 1. Parameters. OUTPUT: 1. Bvec, the State Vector. 2. PuVec, the State Vector Covariance matrix. See Harvey's text on Structural time series models and the Kalman Filter (FSK), p 154 especially! ********************************************************************** ******/ proc(2) = Smoother(parms,data); local Pus, @ Smoothed Covariance Matrix @ Ps, @ PStar matrix in Harvey FSK text p. 154 @ Bvec, PpVec, PuVec, VVec, FVec, Pp, Pu, T,h,Q,R,d,c,y,Z, k, i; @ Call the Kalman Filter Procedure @ {BVec,VVec,FVec,PpVec,PuVec} = Kalman(parms,Data); @ Get the dimension of the state vector @ k = cols(Bvec); @ Retrieve the First Smoothed Covariance Matrix for observation T @ Pus = PuVec[rows(PuVec)-k+1:rows(PuVec),.]; @ Last State Vector in period T is already smoothed as such @ i = Rows(BVec) - 1; do while i >= 1;

@ Get the System Matrices and Data @
{T,H,Q,R,d,c,y,Z} = SystemM(parms,UUse[i,.]');

@ Get Pp for i+1/i which is row i+1 of PpVec @
Pp = PpVec[k*i+1:k*(i+1),.];

@ Get Pu for i which is row i of PuVec @
Pu = PuVec[k*(i-1)+1:k*i,.];

@ Construct the P Star Matrix @
Ps = Pu*T'invpd(Pp);

@ Construct Smooth State Vector for period i @
BVec[i,.] = (Bvec[i,.]' + Ps * (Bvec[i+1,.]' - T*BVec[i,.]'))';

Pus = Pu + Ps * (Pus - Pp) * Ps';
PuVec[k*(i-1)+1:k*i,.] = Pus;

i = i - 1;
endo;
retp(BVec,PuVec);
endp;

@------------------------------------------------------------------------------------------------@
proc parm(parms);
local b0,a0,a1,sma1,sma2;

b0 = parms[1,1];
sma1 = (parms[2,.])^2;
sma1 = sqrt(sma1);

sma2 = (parms[3,.])^2;
sma2 = sqrt(sma2);

para = b0|sma1|sma2;
retp(para);
endp;

@------------------------------------------------------------------------------------------------@

1 Answer



0



Attached is a version of your program running with the additional variable, X_Oil. The revision to this code was made on line 227 of the attached program file.

On line 227, the definition of UUsez was redefined to include all columns in the data matrix. Note that I did this by:

  1. Extract row i from the data matrix using i as the row index and the "." as a column index.
  2. Transposing the result to a column vector rather than a row vector.

As an alternative, the same results could be achieved using

UUsez = data[i,1]|data[i,2]|data[i,3]; 

Click to download MyRandom_EC program file

aptech

1,773

Your Answer

1 Answer

0

Attached is a version of your program running with the additional variable, X_Oil. The revision to this code was made on line 227 of the attached program file.

On line 227, the definition of UUsez was redefined to include all columns in the data matrix. Note that I did this by:

  1. Extract row i from the data matrix using i as the row index and the "." as a column index.
  2. Transposing the result to a column vector rather than a row vector.

As an alternative, the same results could be achieved using

UUsez = data[i,1]|data[i,2]|data[i,3]; 

Click to download MyRandom_EC program file


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.