Factor Risk Models

Building a Barra-style fundamental factor risk model from scratch, entirely in the browser. DuckDB WASM for the data, TypeScript for the math.

5 posts

  1. 1

    Building a Factor Risk Model: The Data

    Setting up a client-side data pipeline for a Barra-style fundamental factor risk model. We load parquet files into DuckDB WASM and explore our security universe and financial data directly in the browser.

  2. 2

    Building a Factor Risk Model: The Exposure Matrix

    Turning raw financial data into the factor exposure matrix in DuckDB SQL: cross-sectional standardization, composite factors, rolling-window factors, and an OLS trend slope built out of nothing but rolling sums.

  3. 3

    Building a Factor Risk Model: The Cross-Sectional Regression

    Estimating factor returns from the exposure matrix. Sector dummies make the regression exactly singular, the constraint that repairs it decides what every factor return means, and the solver is fifty lines of Cholesky and back-substitution.

  4. 4

    Building a Factor Risk Model: The Covariance Matrix

    Estimating the factor covariance matrix with a decaying memory, repairing thin specific risk estimates with shrinkage, and assembling the model. Any portfolio's predicted risk then costs one pass over the holdings and a 20 × 20 sandwich.

  5. 5

    Building a Factor Risk Model: The Optimizer

    Rolling the model back to a formation date two years before the end of the sample, drawing a random 30-stock portfolio, and reshaping it three ways with a small active-set QP. Every prediction is then graded on the 104 weeks the model never saw.