Cross-Sectional Regression Diagnostics (b)

Functions

PortfolioOptimisers.cross_sectional_r2 — Function
cross_sectional_r2(csr::CrossSectionalRegression, Z::Arr3Num, X::MatNum,
                   W::MatNum) -> VecNum

Return the weighted coefficient of determination of every observation.

An observation whose weighted total sum of squares is zero has no defined ratio, and its entry is NaN. mean_cross_sectional_r2 is the scalar summary that skips those entries.

Mathematical definition

\[\begin{align} R^{2}_{t} &= 1 - \frac{\sum_{i} w_{t,i} \left(x_{t,i} - \hat{x}_{t,i}\right)^{2}}{\sum_{i} w_{t,i} \left(x_{t,i} - \bar{x}_{t}\right)^{2}} \\ \bar{x}_{t} &= \frac{\sum_{i} w_{t,i} x_{t,i}}{\sum_{i} w_{t,i}}\,. \end{align}\]

Where:

  • $R^{2}_{t}$: Weighted coefficient of determination of observation $t$.
  • $x_{t,i}$: Return of asset $i$ at observation $t$.
  • $\hat{x}_{t,i}$: Systematic return of asset $i$ at observation $t$.
  • $\bar{x}_{t}$: Weighted mean return of observation $t$.
  • $w_{t,i} \geq 0$: Cross-sectional weight of asset $i$ at observation $t$. Both sums run over the eligible assets alone.

Algorithm

  1. Take the eligibility mask through cross_sectional_design_mask.
  2. Take the systematic returns through StatsAPI.predict(csr::CrossSectionalRegression, Z::Arr3Num).
  3. For each observation, sum the weighted squared residuals and the weighted squared deviations from the weighted mean, over the eligible assets alone.
  4. Return 1 - rss / tss per observation, and NaN where tss is not positive.

Arguments

  • csr: A cross-sectional regression result.
  • Z::Arr3Num: Exposure tensor assets × factors × observations.
  • X::MatNum: Asset returns matrix observations × assets.
  • W::MatNum: Cross-sectional weights matrix observations × assets.

Validation

Returns

  • r2::VecNum: Coefficient of determination of every observation, of length observations, in the type a division of the inputs lands in.

Examples

julia> Z = reshape([1.0, 0.0, 0.5, 0.0, 1.0, 0.5], 3, 2, 1);julia> csr = cross_sectional_regression(CrossSectionalLinearRegression(), Z, [1.0 2.0 1.5],                                        ones(1, 3));julia> cross_sectional_r2(csr, Z, [1.0 2.0 1.5], ones(1, 3))1-element Vector{Float64}: 1.0

Related

source
PortfolioOptimisers.mean_cross_sectional_r2 — Function
mean_cross_sectional_r2(csr::CrossSectionalRegression, Z::Arr3Num, X::MatNum,
                        W::MatNum) -> Number

Return the mean weighted coefficient of determination across the observations.

The mean skips every observation whose ratio is undefined, and it is NaN when no observation defines one.

Algorithm

  1. Take the per-observation vector through cross_sectional_r2.
  2. Return the mean of its finite entries, and NaN when it holds none.

Arguments

  • csr: A cross-sectional regression result.
  • Z::Arr3Num: Exposure tensor assets × factors × observations.
  • X::MatNum: Asset returns matrix observations × assets.
  • W::MatNum: Cross-sectional weights matrix observations × assets.

Validation

Returns

  • r2::Number: Mean coefficient of determination across the observations.

Examples

julia> Z = reshape([1.0, 0.0, 0.5, 0.0, 1.0, 0.5], 3, 2, 1);julia> csr = cross_sectional_regression(CrossSectionalLinearRegression(), Z, [1.0 2.0 1.5],                                        ones(1, 3));julia> mean_cross_sectional_r2(csr, Z, [1.0 2.0 1.5], ones(1, 3))1.0

Related

source