Cross-Sectional Regression Diagnostics (a)

Functions

PortfolioOptimisers.cs_gram — Function
cs_gram(B::Arr3Num, w::Option{<:MatNum} = nothing) -> Array{<:Real, 3}

Return the weighted Gram history of a cross-sectional regression, one slice per observation.

The slice of observation $t$ is the normal matrix of the weighted design. The variance inflation factors, the condition number and the standard errors of the factor returns all read this one history. The kernel applies the mask first, so an asset whose exposures are not all finite contributes nothing, and neither does an asset whose weight is zero.

Mathematical definition

\[\mathbf{G}_{t} = \mathbf{B}_{t}^{\intercal} \mathbf{W}_{t} \mathbf{B}_{t}\]

Where:

  • $\mathbf{B}_{t}$: Exposure matrix of observation $t$, $N \times K$, whose row of an ineligible asset is zero.
  • $\mathbf{W}_{t}$: Diagonal weight matrix of observation $t$, whose entry of an ineligible asset is zero.
  • $N$: Number of assets.
  • $K$: Number of factors.

Algorithm

  1. Take the mask and the weights with cs_estimation_mask_weights.
  2. For each observation and each pair of factors $(k, l)$ with $k \le l$, sum $u_{t,i} \, B_{t,i,k} \, B_{t,i,l}$ over the assets inside the mask.
  3. Copy each sum to the entry $(l, k)$, so the slice is exactly symmetric. The kernel takes no square root, so a Rational history gives an exact Gram history.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights. A weight of zero excludes the pair.

Validation

  • !isempty(B).
  • size(w) == (size(B, 3), size(B, 1)), when w is present.

Returns

  • G::Array{<:Real, 3}: Gram history factors × factors × observations.

Examples

julia> B = reshape([1.0, 1.0, 0.0, 1.0], 2, 2, 1);julia> cs_gram(B)2×2×1 Array{Float64, 3}:[:, :, 1] = 2.0  1.0 1.0  1.0

Related

source
PortfolioOptimisers.cs_regression_t_stats — Function
cs_regression_t_stats(
    B::Arr3Num,
    f::MatNum,
    eps::MatNum,
    w::Option{<:MatNum} = nothing;
    G::Option{<:Arr3Num} = nothing,
    P = ()
) -> Matrix{<:Real}
cs_regression_t_stats(csfm::CrossSectionalFactorModel) -> FactorDiagnosticResult

Return the t-statistic of every factor return, one row per observation.

The statistic says how many standard errors a factor return of one observation sits from zero, so a rule of thumb reads an absolute value above two as significant at about the five per cent level. The fit stores no standard error, so this verb recomputes it from the Gram matrix and the residuals, and the fit result gains no field.

Mathematical definition

\[\begin{align} t_{t,k} &= \frac{f_{t,k}}{\mathrm{SE}_{t,k}}\,,\\ \mathrm{SE}_{t,k} &= \sqrt{\hat{\sigma}^{2}_{t} \, (\mathbf{G}_{t}^{-1})_{kk}}\,,\\ \hat{\sigma}^{2}_{t} &= \frac{\mathrm{RSS}_{t}}{n_{t} - r_{t}}\,,\\ \mathrm{RSS}_{t} &= \sum_{i} u_{t,i} \, \varepsilon_{t,i}^{2}\,. \end{align}\]

Where:

  • $f_{t,k}$: Factor return of factor $k$ at observation $t$, which is the coefficient of the cross-sectional fit.
  • $\mathbf{G}_{t}$: Gram matrix of observation $t$, which cs_gram defines.
  • $(\mathbf{G}_{t}^{-1})_{kk}$: Diagonal of the inverse of $\mathbf{G}_{t}$, which cs_gram_inverse_diagonal defines. On a collinear slice it is the diagonal of the pseudo-inverse for a coefficient the design identifies, and Inf for another. At an observation with an Unseen Member it is the diagonal of $\mathbf{P}_{t} \mathbf{G}_{t}^{+} \mathbf{P}_{t}^{\intercal}$, which cs_inverse_diagonal! states, because the factor returns are $\mathbf{P}_{t}$ times the coefficients of the changed design.
  • $r_{t}$: Numeric rank of the weighted design of observation $t$, which cross_sectional_rank reads as the fit reads it. It is $K$ on a full-rank design. The residuals of a collinear fit span $n_{t} - r_{t}$ dimensions, so the unbiased variance divides by that count.
  • $u_{t,i}$: Resolved regression weight of asset $i$ at observation $t$, zero outside the mask.
  • $\varepsilon_{t,i}$: Residual of asset $i$ at observation $t$.
  • $n_{t}$: Number of eligible assets at observation $t$.
  • $K$: Number of factors.

Algorithm

  1. Resolve the mask and the weights with cs_diagnostic_mask_weights, which also excludes a pair whose residual is not finite.
  2. Take the diagonal of the inverse Gram matrix and the rank of every observation with cs_slice_inverse_diagonal!, mapped by the change of the observation in P when it has one. Without G it reads the weighted design of the observation, so the rank and the inverse are those of the fit. With G it reads the slice of G.
  3. Take the residual sum of squares of every observation, and divide it by the degrees of freedom.
  4. Scale the diagonal by that variance, and take the square root, which is the standard error.
  5. Divide the factor returns by the standard errors. Answer NaN where the degrees of freedom are not positive, where a factor return of that observation is not finite, and where the standard error is zero or Inf. A coefficient that a collinear design does not identify has no t-statistic, because the fit states no value of it. An Unseen Member has a standard error of zero, so it has no t-statistic either, because the rule states its return and the data do not estimate it.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • f: Factor return matrix observations × factors, already lagged.
  • eps: Residual matrix observations × assets, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights.
  • G: Gram history factors × factors × observations the caller already holds, or nothing to read the design. A caller that supplies one states that it built the history from the same mask. The verb does not check this. A Gram history formed in floating point squares the condition number of the design, so on a design whose condition number exceeds about $10^{8}$ it drops a direction that the fit kept, and the t-statistics of the near-dependent factors are NaN. Leave G unset to read the design.
  • P: The t => P_t pairs of unseen_member_design on the rows of B, where B is the changed design and f holds the factor returns. The block method passes the pairs of cs_regression_data. It is () for a design that no rule changed.
  • csfm: A cross-sectional factor model block. The answer is on the reduced factor axis when the block carries a family re-basis. When the fit states a dropped-column mask, the block method reads the design after cs_dropped_design, so a factor that DependentColumnDrop dropped has no t-statistic, and the kept factors of its dependent group have the t-statistics of the kept columns.

Validation

  • !isempty(B), and every history agrees with B on the axes it shares.
  • On the block method, csfm.Ms and csfm.csr are not nothing.

Returns

  • t::Matrix{<:Real}: observations × factors, on the method over histories.
  • r::FactorDiagnosticResult: On the block method, the same matrix in X, with the names and the family labels of the design axis. The block method returned the bare matrix in earlier releases, so a caller that indexed it now reads r.X.

Examples

julia> B = reshape([1.0, 1.0, 1.0, -1.0, 1.0, 0.0], 3, 2, 1);julia> round.(cs_regression_t_stats(B, [2.0 1.0], [0.1 -0.1 0.05]); digits = 4)1×2 Matrix{Float64}: 23.094  9.4281

Related

source
PortfolioOptimisers.cs_regression_t_stat_exceedance_rate — Function
cs_regression_t_stat_exceedance_rate(t::MatNum; threshold::Number = 2) -> Vector{<:Real}
cs_regression_t_stat_exceedance_rate(
    t::FactorDiagnosticResult;
    threshold::Number = 2
) -> FactorDiagnosticResult
cs_regression_t_stat_exceedance_rate(
    csfm::CrossSectionalFactorModel;
    threshold::Number = 2
) -> FactorDiagnosticResult

Return the fraction of observations at which a factor's t-statistic exceeds a threshold.

A factor whose true cross-sectional coefficient is zero, and whose t-statistics are about Gaussian, exceeds a threshold of two at about five per cent of the observations. A rate above that reference level says that the factor is repeatedly significant rather than significant once. An observation whose t-statistic is NaN counts in neither the numerator nor the denominator.

Mathematical definition

\[\mathrm{rate}_{k} = \frac{\#\{t : |t_{t,k}| > \tau\}}{\#\{t : t_{t,k} \text{ is finite}\}}\]

Where:

  • $t_{t,k}$: T-statistic of factor $k$ at observation $t$, which cs_regression_t_stats returns.
  • $\tau$: Absolute threshold.

Arguments

  • t: T-statistic matrix observations × factors, or the FactorDiagnosticResult of cs_regression_t_stats, whose labels the answer keeps.
  • csfm: A cross-sectional factor model block. The answer is on the reduced factor axis when the block carries a family re-basis.
  • threshold: Absolute t-statistic above which an observation counts as significant.

Validation

  • !isempty(t).

Returns

  • rate::Vector{<:Real}: One entry per factor, on the method over a matrix. A factor with no finite t-statistic answers zero.
  • r::FactorDiagnosticResult: On the other two methods, the same vector in X, with the names and the family labels of the factor axis of t. The block method returned the bare vector in earlier releases, so a caller that indexed it now reads r.X.

Examples

julia> cs_regression_t_stat_exceedance_rate([3.0 1.0; 1.0 1.0; NaN 1.0])2-element Vector{Float64}: 0.5 0.0

Related

source
PortfolioOptimisers.cs_regression_r2 — Function
cs_regression_r2(
    B::Arr3Num,
    f::MatNum,
    eps::MatNum,
    w::Option{<:MatNum} = nothing
) -> Vector{<:Real}
cs_regression_r2(csfm::CrossSectionalFactorModel) -> Vector{<:Real}

Return the weighted cross-sectional coefficient of determination, one entry per observation.

The score says what share of the weighted cross-sectional variance of the returns the factors of that observation explain. The verb normalises the weights to sum to one, so an observation with many assets and an observation with few are on one scale.

Mathematical definition

\[R^{2}_{t} = 1 - \frac{\sum_{i} q_{t,i} \, \varepsilon_{t,i}^{2}} {\sum_{i} q_{t,i} \, (r_{t,i} - \bar{r}_{t})^{2}}\]

Where:

  • $q_{t,i}$: Regression weight of asset $i$ at observation $t$, normalised over the eligible assets to sum to one.
  • $\varepsilon_{t,i}$: Residual of asset $i$ at observation $t$.
  • $r_{t,i}$: Return of asset $i$ at observation $t$, reconstructed as the systematic part plus the residual.
  • $\bar{r}_{t}$: Weighted mean return of observation $t$.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • f: Factor return matrix observations × factors, already lagged.
  • eps: Residual matrix observations × assets, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights.
  • csfm: A cross-sectional factor model block.

Validation

  • !isempty(B), and every history agrees with B on the axes it shares.
  • On the block method, csfm.Ms and csfm.csr are not nothing.

Returns

  • r2::Vector{<:Real}: One entry per observation, NaN where the weighted total sum of squares is zero, as it is for a cross-section of equal returns.

Examples

julia> B = reshape([1.0, 1.0, -1.0, 1.0], 2, 2, 1);julia> cs_regression_r2(B, [1.0 1.0], [0.0 0.0])1-element Vector{Float64}: 1.0

Related

source
PortfolioOptimisers.cs_regression_adjusted_r2 — Function
cs_regression_adjusted_r2(
    B::Arr3Num,
    f::MatNum,
    eps::MatNum,
    w::Option{<:MatNum} = nothing;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}
cs_regression_adjusted_r2(
    csfm::CrossSectionalFactorModel;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}

Return the cross-sectional coefficient of determination adjusted for the regressor count, one entry per observation.

The adjustment charges the score for every regressor, so a factor that explains nothing lowers it rather than leaving it flat. The adjusted score therefore never exceeds cs_regression_r2, and it is the score to read when two designs of different sizes are compared.

Mathematical definition

\[\bar{R}^{2}_{t} = 1 - (1 - R^{2}_{t}) \, \frac{n_{t} - 1}{n_{t} - k_{t} - 1}\]

Where:

  • $R^{2}_{t}$: Coefficient of determination of observation $t$, which cs_regression_r2 defines.
  • $n_{t}$: Number of eligible assets at observation $t$.
  • $k_{t}$: Effective number of regressors of observation $t$.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • f: Factor return matrix observations × factors, already lagged.
  • eps: Residual matrix observations × assets, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights.
  • k: Effective number of regressors, or nothing for the rank of the design of each observation, which cs_score_regressors takes. The rank is what the fit spent: the factor count of the reduced axis on a full-rank observation, and less on a collinear one.
  • csfm: A cross-sectional factor model block.

Validation

  • !isempty(B), and every history agrees with B on the axes it shares.
  • On the block method, csfm.Ms and csfm.csr are not nothing.

Returns

  • adj::Vector{<:Real}: One entry per observation, NaN where the eligible asset count does not exceed k_t + 1.

Examples

julia> B = reshape([1.0, 1.0, 1.0, -1.0, 1.0, 0.0], 3, 2, 1);julia> cs_regression_adjusted_r2(B, [1.0 0.0], [0.0 0.0 0.0])1-element Vector{Float64}: NaN

Related

source
PortfolioOptimisers.cs_regression_aic — Function
cs_regression_aic(
    B::Arr3Num,
    f::MatNum,
    eps::MatNum,
    w::Option{<:MatNum} = nothing;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}
cs_regression_aic(
    csfm::CrossSectionalFactorModel;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}

Return the Akaike information criterion of every cross-sectional fit, one entry per observation.

The criterion trades the fit of an observation against the size of its design, and a lower value is the better trade. It shares its residual term with cs_regression_bic and differs only in the penalty, which is flat in the asset count here and grows with it there.

Mathematical definition

\[\mathrm{AIC}_{t} = n_{t} \ln (\mathrm{RSS}_{t}) + 2 k_{t}\]

Where:

  • $\mathrm{RSS}_{t}$: Weight-normalised residual sum of squares of observation $t$.
  • $n_{t}$: Number of eligible assets at observation $t$.
  • $k_{t}$: Effective number of regressors of observation $t$.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • f: Factor return matrix observations × factors, already lagged.
  • eps: Residual matrix observations × assets, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights.
  • k: Effective number of regressors, or nothing for the rank of the design of each observation, which cs_score_regressors takes. The rank is what the fit spent: the factor count of the reduced axis on a full-rank observation, and less on a collinear one.
  • csfm: A cross-sectional factor model block.

Validation

  • !isempty(B), and every history agrees with B on the axes it shares.
  • On the block method, csfm.Ms and csfm.csr are not nothing.

Returns

  • aic::Vector{<:Real}: One entry per observation, NaN where the eligible asset count does not exceed k_t, and -Inf where the residual sum of squares is zero.

Examples

julia> B = reshape([1.0, 1.0, 1.0, -1.0, 1.0, 0.0], 3, 2, 1);julia> cs_regression_aic(B, [1.0 0.0], [0.1 0.1 0.1]; k = 1)1-element Vector{Float64}: -11.815510557964274

Related

source
PortfolioOptimisers.cs_regression_bic — Function
cs_regression_bic(
    B::Arr3Num,
    f::MatNum,
    eps::MatNum,
    w::Option{<:MatNum} = nothing;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}
cs_regression_bic(
    csfm::CrossSectionalFactorModel;
    k::Option{<:Integer} = nothing
) -> Vector{<:Real}

Return the Bayesian information criterion of every cross-sectional fit, one entry per observation.

The criterion trades the fit of an observation against the size of its design, and a lower value is the better trade. Its penalty grows with the logarithm of the eligible asset count, so it charges a large cross-section more for a regressor than cs_regression_aic does.

Mathematical definition

\[\mathrm{BIC}_{t} = n_{t} \ln (\mathrm{RSS}_{t}) + k_{t} \ln (n_{t})\]

Where:

  • $\mathrm{RSS}_{t}$: Weight-normalised residual sum of squares of observation $t$.
  • $n_{t}$: Number of eligible assets at observation $t$.
  • $k_{t}$: Effective number of regressors of observation $t$.

Arguments

  • B: Exposure history assets × factors × observations, already lagged.
  • f: Factor return matrix observations × factors, already lagged.
  • eps: Residual matrix observations × assets, already lagged.
  • w: Regression weight history observations × assets, or nothing for equal weights.
  • k: Effective number of regressors, or nothing for the rank of the design of each observation, which cs_score_regressors takes. The rank is what the fit spent: the factor count of the reduced axis on a full-rank observation, and less on a collinear one.
  • csfm: A cross-sectional factor model block.

Validation

  • !isempty(B), and every history agrees with B on the axes it shares.
  • On the block method, csfm.Ms and csfm.csr are not nothing.

Returns

  • bic::Vector{<:Real}: One entry per observation, NaN where the eligible asset count does not exceed k_t, and -Inf where the residual sum of squares is zero.

Examples

julia> B = reshape([1.0, 1.0, 1.0, -1.0, 1.0, 0.0], 3, 2, 1);julia> cs_regression_bic(B, [1.0 0.0], [0.1 0.1 0.1]; k = 1)1-element Vector{Float64}: -12.716898269296165

Related

source