Factor attribution (b): private API

PortfolioOptimisers.attribution_key_align — Function
attribution_key_align(blk::AttributionBlock, T::Integer, key::Nothing)
attribution_key_align(blk::AttributionBlock, T::Integer, key::AttributionRowKey)

Return the lag-aligned history of a factor model block over the observations of a series that may name its rows.

The series of a cross-validation names its rows, and its folds usually cover fewer rows than a prior fitted on the whole sample. A key of nothing names no row, so the function aligns the block at the tail with attribution_align. A key with the fields idx and ts names the position and the timestamp of each observation of the series. attribution_key_pair picks the key that the block and the series both carry. The function then aligns the whole block, so each block row keeps the exposures of the row lag observations before it. Last, it cuts the aligned block with attribution_window to the rows that attribution_match_rows matches, and sets its rows to the matched observations of the series through rebuild_struct. The first observation of a fold that starts after the first block row therefore keeps its exposures, which a cut of the block before the alignment would lose. When the two carry no common key, the function aligns at the tail.

Arguments

  • blk: The block as bare arrays, from attribution_block_arrays or attribution_array_block.
  • T: The number of observations the caller's return series carries.
  • key: The position idx and the timestamp ts of each observation of the series, each nothing when the series does not carry it, or nothing.

Validation

  • With a common key, the key of the series has T entries and the key of the block has one entry for each observation of the block, else a DimensionMismatch is raised, and the rules of attribution_match_rows hold.
  • The rules of attribution_align.

Returns

  • al::AlignedAttributionBlock: The aligned history, as attribution_align states it. Its rows are the observations of the series that the block decomposes, and they are consecutive.

Related

source
PortfolioOptimisers.RowKeyPair — Type
struct RowKeyPair{__T_block, __T_series}

One kind of row key that a factor model block and a return series share: the key of the block and the key of the series.

This type is an implementation detail and is not intended for direct use. attribution_key_pair makes it, and attribution_key_align reads it. The two keys have one type, positions or timestamps, so a record names them.

Fields

  • block: block: The key of each block row.
  • series: series: The key of each observation of the series, of the same kind as block.

Related

source
PortfolioOptimisers.attribution_key_pair — Function
attribution_key_pair(bk::AttributionRowKey, key::AttributionRowKey)
attribution_key_pair(b::AbstractVector, s::AbstractVector)
attribution_key_pair(b::Option{<:AbstractVector}, s::Option{<:AbstractVector})

Return the row keys of a factor model block and of a return series that the two share, or nothing.

A timestamp names an observation in every returns data that holds it, so the function takes the timestamps when both carry them. A position names an observation in one returns data alone, so the function takes the positions only when one of the two carries no timestamps. The positions of the block are then positions in the returns data that the prior read, and those of the series are positions in the returns data that the cross-validation split. The two agree only when the prior read the returns data that the cross-validation split, and the function cannot check that.

Arguments

  • bk: The row key of the block, from attribution_row_key.
  • key: The position idx and the timestamp ts of each observation of the series, each nothing when the series does not carry it.
  • b, s: One kind of key of the block and of the series, each nothing when its side does not carry it. The two methods over them pair the keys when both are present, and answer nothing otherwise.

Returns

  • kp::Option{<:RowKeyPair}: The key of the block under block and that of the series under series, or nothing when the two share no key.

Related

source
PortfolioOptimisers.attribution_match_rows — Function
attribution_match_rows(bk::AbstractVector, sk::AbstractVector, lag::Integer)

Return the rows of a return series that a factor model block decomposes, and the aligned block row of each.

Block row t of a fit with exposure lag lag decomposes its returns with the exposures of block row t - lag, so the first lag block rows decompose nothing. Aligned row a is block row a + lag. The function finds the aligned row whose key equals the key of each observation of the series. The observations before the first match and after the last match fall outside the block, as the observations of a fold before the first fitted row do, and the attribution leaves them out, when their keys lie outside the range of the keys of the aligned rows. Every other observation must match, because a key inside that range with no row is a gap of the block, for example an observation that the fit dropped for a Shared Series gap. A gap at the head of the series is refused as a gap between two matches is.

Arguments

  • bk: The key of each block row, positions or timestamps.
  • sk: The key of each observation of the series, of the same kind as bk.
  • lag: The exposure lag of the block.

Validation

  • At least one observation of the series matches an aligned row. Raises a DimensionMismatch.
  • Every observation between the first match and the last match, and every observation whose key lies inside the range of the keys of the aligned rows, matches an aligned row. Raises an ArgumentError that names the first unmatched observations.
  • The matched aligned rows increase strictly, so the series repeats no observation and keeps the order of the block. Raises an ArgumentError.

Returns

The tuple (rows, arows):

  • rows::UnitRange{Int}: The observations of the series that the block decomposes.
  • arows::Vector{Int}: The aligned block row of each of them.

Related

source
PortfolioOptimisers.attribution_series_key — Function
attribution_series_key(pred::MultiPeriodPredictionResult)

Return the row key of the series that a cross-validation produced.

The series is the folds stacked in order, as attribution_prediction_history stacks them, so its key is the keys of the folds stacked in the same order. A fold records its positions in idx, and the timestamps of its rows in rd.ts. The series carries positions only when every fold carries them, and timestamps only when every fold carries them, as pred.mrd.ts holds them.

Arguments

  • pred: A multi-period prediction result.

Returns

  • key::AttributionRowKey: The position idx of each observation of the series in the returns data that the cross-validation split, or nothing, and the timestamp ts of each observation of the series, or nothing.

Related

source