R Matrix Laboratory

Four browser-based base R laboratories for covariance matrices, AR recursions, linear prediction, likelihood, and state space.

R Matrix Laboratory — Theory You Can Execute

Laboratory purpose

These laboratories translate classical time-series derivations into base R. They do not begin with a large empirical dataset or automated model selection. Each starts from a mathematical object whose answer is known well enough to check.

Every code cell is:

  • self-contained and runnable in the browser with webR;
  • written with base R and the recommended stats package;
  • explicit about dimensions and parameter conventions;
  • followed by a numerical identity or error check.

Lab map

LabCentral equationMain R toolsOutput
1. Covariance geometryΓn=[γ(ij)]\Gamma_n=[\gamma(i-j)]toeplitz, eigen, cholvalid finite covariance block
2. AR recursionsst=Fst1+Gεt\mathbf s_t=F\mathbf s_{t-1}+G\varepsilon_tpolyroot, eigen, ARMAacfroots, impulse responses, Yule–Walker solution
3. Prediction and likelihoodΓa=g\Gamma\mathbf a=\mathbf g and (θ;Σθ)\ell(\theta;\Sigma_\theta)solve, chol, optimpredictor plus exact Gaussian estimate
4. State spaceKalman prediction and correction%*%, kronecker, loopsfiltered states and forecast covariance

Working conventions

  1. Vectors in the notes are columns. R vectors acquire column meaning when used with %*%.
  2. chol(Sigma) returns an upper-triangular UU satisfying UU=ΣU^\top U=\Sigma.
  3. ARMAacf follows R's model convention Xt=ϕ1Xt1++εt+θ1εt1+X_t=\phi_1X_{t-1}+\cdots+\varepsilon_t+\theta_1\varepsilon_{t-1}+\cdots.
  4. matrix(v, nrow=k) fills by columns, matching the usual vec\operatorname{vec} operator.
  5. Use solve(A, b) for Ax=bA\mathbf x=\mathbf b; reserve solve(A) for occasions when the full inverse is genuinely needed.

How to submit a lab

For each cell, report four items:

Equation:
Dimensions:
Numerical check:
One-parameter perturbation and predicted effect:

“The output looks right” is not a check. Examples of checks are

ΓΓ<1012,minλi(Γ)>0,Γag<1010.\|\Gamma-\Gamma^\top\|_\infty<10^{-12}, \qquad \min\lambda_i(\Gamma)>0, \qquad \|\Gamma\mathbf a-\mathbf g\|_\infty<10^{-10}.

Workload

Each lab is designed for 45–60 minutes:

  • 10 minutes: reproduce one derivation by hand;
  • 15 minutes: run and annotate the original cells;
  • 15 minutes: make one controlled parameter change;
  • 10–20 minutes: answer the exercises and write the mathematical conclusion.

Start

Open Lab 1. Before running code, predict what happens to the smallest eigenvalue and condition number when an AR(1) coefficient moves from 0.60.6 to 0.950.95.

Copyright © 2026