Cross-Sectional Regression Diagnostics (b)
Functions
PortfolioOptimisers.cross_sectional_r2 — Function
cross_sectional_r2(csr::CrossSectionalRegression, Z::Arr3Num, X::MatNum,
W::MatNum) -> VecNumReturn 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
- Take the eligibility mask through
cross_sectional_design_mask. - Take the systematic returns through
StatsAPI.predict(csr::CrossSectionalRegression, Z::Arr3Num). - For each observation, sum the weighted squared residuals and the weighted squared deviations from the weighted mean, over the eligible assets alone.
- Return
1 - rss / tssper observation, andNaNwheretssis not positive.
Arguments
csr: A cross-sectional regression result.Z::Arr3Num: Exposure tensorassets × factors × observations.X::MatNum: Asset returns matrixobservations × assets.W::MatNum: Cross-sectional weights matrixobservations × assets.
Validation
- The rules of
cross_sectional_design_mask.
Returns
r2::VecNum: Coefficient of determination of every observation, of lengthobservations, 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.0Related
PortfolioOptimisers.mean_cross_sectional_r2 — Function
mean_cross_sectional_r2(csr::CrossSectionalRegression, Z::Arr3Num, X::MatNum,
W::MatNum) -> NumberReturn 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
- Take the per-observation vector through
cross_sectional_r2. - Return the mean of its finite entries, and
NaNwhen it holds none.
Arguments
csr: A cross-sectional regression result.Z::Arr3Num: Exposure tensorassets × factors × observations.X::MatNum: Asset returns matrixobservations × assets.W::MatNum: Cross-sectional weights matrixobservations × assets.
Validation
- The rules of
cross_sectional_design_mask.
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.0Related