OLS in Mathematica

These are some notes for myself on econometrics in Mathematica.

Set up the data


n = 100;
B0 = 1;
B1 = 2;
x = Table[Random[NormalDistribution[0, 1]], {n}];
\[Epsilon] = Table[Random[NormalDistribution[0, 1]], {n}];
y = B0 + B1*x + \[Epsilon];
ListPlot[Transpose[{x, y}]]

Screen Shot 2017-04-23 at 8.52.46 AM

Create the model matrix


iota = Table[1, {n}];
X = Transpose[{iota, x}];
k = Dimensions[X][[2]];

Estimate coefficients


Bhat = Inverse[Transpose[X].X].Transpose[X].y

Make predictions


yhat = X.Bhat;
ListPlot[{Transpose[{x, y}], Transpose[{x, yhat}]}]

Screen Shot 2017-04-23 at 8.55.34 AM

Compute the variance/covariance matrix


error = y - yhat;
sigma = Sqrt[Variance[error]]
sigma^2* Inverse[Transpose[X].X] // MatrixForm

Screen Shot 2017-04-23 at 8.57.34 AM

Regression statistics

R squared


rsq = Variance[yhat]/Variance[y]