sf_quant.data.construct_covariance_matrix#
- sf_quant.data.construct_covariance_matrix(date_: date, barrids: list[str]) DataFrame#
Construct the asset covariance matrix from a factor model.
This function builds a covariance matrix for the given assets by combining factor exposures, factor covariances, and specific (idiosyncratic) risks. The resulting covariance matrix can be used as an input to mean-variance optimization.
Parameters#
- date_datetime.date
The date for which the covariance matrix is computed.
- barridslist of str
List of Barrid identifiers for the assets.
Returns#
- pl.DataFrame
A square covariance matrix stored in a Polars DataFrame.
Rows and columns are indexed by
barrid.Column
barridlists the asset identifiers.Each subsequent column corresponds to the covariance of the row asset with the column asset.
Notes#
The input factor covariance matrix is assumed to be positive semidefinite (PSD).
Examples#
>>> import sf_quant.data as sfd >>> import datetime as dt >>> date_ = dt.date(2024, 1, 3) >>> barrids = ['USA06Z1', 'USA0771'] >>> covariance_matrix = sfd.construct_covariance_matrix( ... date_=date_, ... barrids=barrids ... ) >>> covariance_matrix shape: (2, 3) ┌─────────┬─────────────┬──────────────┐ │ barrid ┆ USA06Z1 ┆ USA0771 │ │ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ f64 │ ╞═════════╪═════════════╪══════════════╡ │ USA06Z1 ┆ 3224.338938 ┆ 697.641425 │ │ USA0771 ┆ 697.641425 ┆ 11158.366868 │ └─────────┴─────────────┴──────────────┘