# Matlab : Stuck in implementing Least Squares estimator for this model

The formula to estimate $\mathbf{h}$ is then $$\hat{\mathbf{h}} = (X^T X)^{-1} X^T \vec{y}\tag{2}$$

I think this can be implemented in Matlab using hat_h = pinv(X)*X*y

Question 1 : What is the lag of the model?

Question 2: I don't know how to create the design matrix X in order to estimate h. Can somebody please provide the complete implementation? Thank you

The equation you're trying to solve is $$\mathbf{y}=\mathbf{X}\mathbf{h},$$ where $\mathbf{h}$ is your unknown. The matrix $\mathbf{X}$ is going to have a time-shifted structure that reflects the convolution operator. If we assume that the $\mathbf{y}$ vector starts with y(3) i.e. ignores the first two zeroed out elements of y, then the corresponding $\mathbf{X}$ matrix is given by:

$$\mathbf{X}=\left[\begin{matrix} x(3) & x(2) & x(1)\\ x(4) & x(3) & x(2) \\ x(5) & x(4) & x(3) \\ ... & ... & ... \end{matrix} \right]$$

You can add as many rows as you are have observations to support them. You can then solve for $\mathbf{h}$ by $$\mathbf{h}_{est}=\text{pinv}(\mathbf{X})\mathbf{y}$$

• Thank you for your reply. The structure of X starts from the third element and there are 3 coefficients. So, is this an MA model of order 3 or order 2? Can you please let me know? – SKM May 1 '17 at 13:51
• Your system is represented by a quadratic polynomial - thus having 2 roots, so it's a 2nd order system. – David May 1 '17 at 14:45

If X is your design matrix then the matlab implementation of Ordinary Least Squares is:

    h_hat = X'*X\(X'*y);


I attempted to answer your other question here: How to apply Least Squares estimation for sparse coefficient estimation? which explains how to create the design matrix.

As mentioned this is a second order Moving Average model, lag of 2. https://onlinecourses.science.psu.edu/stat510/node/48