Forecast rules: private API

PortfolioOptimisers.forecaster_seed — Function
forecaster_seed(
    me::AbstractExpectedReturnsEstimator
) -> Union{Nothing, ForecasterState{__T_me} where __T_me<:AbstractExpectedReturnsEstimator}

Returns the state that a forecast-reading rule seeds.

The state is a ForecasterState over a forecaster that folds, and nothing for a forecaster that the rule fits again from the rows of the head.

Validation

  • A forecaster that folds carries no partial-fit state at the seed. An ArgumentError is thrown otherwise. The head starts cold, and a state folded elsewhere would make the forecast of the first row read rows that the head never saw.

Related

source
PortfolioOptimisers.forecast_min_rows — Function
forecast_min_rows(
    me::AbstractExpectedReturnsEstimator
) -> Any

Returns the number of rows that a forecaster needs before the refit arm of forecast_relative reads it.

Below this number the refit arm gives a flat forecast.

Mathematical definition

\[\begin{align} n_{\min} &= \max\left(n_2,\, n_{\mathrm{fit}}\right)\,. \end{align}\]

Where:

  • $n_{\min}$: Least number of rows of the refit arm.
  • $n_2$: Two when the estimator tree holds a covariance, variance or prior estimator, whose second moment is undefined on one row, and one otherwise. holds_second_moment decides it.
  • $n_{\mathrm{fit}}$: Least number of rows that the estimator states through fit_min_rows.

Related

source
PortfolioOptimisers.holds_second_moment — Function
holds_second_moment(est)

Returns whether an estimator tree holds a covariance, variance or prior estimator at any depth.

Algorithm

  1. When est is a covariance, variance or prior estimator, or a StatsBase.CovarianceEstimator, return true. Stop.
  2. When est is another estimator, call the function on each field that estimator_fields names, and return true when any call returns true.
  3. Return false for a value that is not an estimator.

Related

source
PortfolioOptimisers.forecast_where_defined — Function
forecast_where_defined(xhat::AbstractVector) -> Any

Reads each non-finite entry of a Price Relative Forecast as NaN, the mark of an asset that the forecaster cannot price.

The step of the rule leaves such an asset out with investable_mask, and gives it a zero weight, as a batch optimiser does with an asset that its prior cannot price. A forecaster that prices no asset states no universe. The forecast is then flat, so the step holds. This happens over the first rows of a forecaster whose estimate is undefined on them, such as a variance over one observation.

Related

source
PortfolioOptimisers.refit_rows — Function
refit_rows(rows::ReturnsResult, x::AbstractVector)
refit_rows(rows::Nothing, x::AbstractVector)

Returns the returns that the refit arm fits a forecaster on, the Asset Panel that explains them, and the implied volatilities of the rows as keywords of mean.

When rows is a ReturnsResult, these are its returns in the head's frame, which head_frame_returns gives, its pnl, and its iv and ivpa. The frame is X - B when the head's x_src is ExcessSource, and X otherwise. Without a Returns Result, they are the current row as a matrix of one row, no panel and no keywords. The current row is in the head's frame already, because the head forms x from it.

The keywords go to every forecaster, as prior over a ReturnsResult gives them to every member. A PriorExpectedReturns gives them to its prior, so a member of the prior that reads them, such as an ImpliedVolatility covariance, reads the surface over the same rows. A WindowedExpectedReturns cuts iv to its window first. Every other forecaster ignores them.

Related

source
PortfolioOptimisers.scale_relative — Function
scale_relative(scale::Nothing, x::AbstractVector, rows)
scale_relative(scale::AbstractPriceLevelStatistic, x::AbstractVector, rows)

Returns the diagonal of the preconditioner of a ForecastReversion step, or nothing for the identity.

The diagonal is the Price Relative Forecast of the scale statistic, fit on the rows that the head holds, or on the current row alone when rows is nothing. An entry that the statistic cannot give is one, through flat_where_undefined, so the step on that asset is not scaled.

Related

source
PortfolioOptimisers.forecast_step — Function
forecast_step(alg::ForecastReversion, w::AbstractVector, what::AbstractVector, xhat::AbstractVector, imsk::Option{BitVector}, x::AbstractVector, rows)
forecast_step(alg::ForecastTracking, w::AbstractVector, what::AbstractVector, xhat::AbstractVector, imsk, x::AbstractVector, rows)
forecast_step(alg::KernelTrendTracking, w::AbstractVector, what::AbstractVector, xhat::AbstractVector, imsk, x::AbstractVector, rows)
forecast_step(alg::TransactionCostOptimisation, w::AbstractVector, what::AbstractVector, xhat::AbstractVector, imsk, x::AbstractVector, rows)
forecast_step(alg::ShortTermSparsePortfolio, w::AbstractVector, what::AbstractVector, xhat::AbstractVector, imsk, x::AbstractVector, rows)

Returns the raw step of a rule that reads a Price Relative Forecast, over the assets that the forecaster prices.

The docstring of each rule states its step. online_update! reduces the three vectors to the Investable Mask of the forecast, and projects the step at that mask. A forecast that prices every asset has no mask, so the vectors are the full ones.

Arguments

  • alg: The rule.
  • w: The allocation of the step, reduced to the mask.
  • what: The Price-Adjusted Allocation of the step, reduced to the mask.
  • xhat: The Price Relative Forecast, reduced to the mask.
  • imsk: The Investable Mask of the forecast, or nothing when the forecaster prices every asset. ForecastReversion reduces its preconditioner to it.
  • x: The price relative of the row over the full universe.
  • rows: The rows that the head holds through the row, a ReturnsResult, or nothing.

Returns

  • q::AbstractVector: The raw step, reduced to the mask.

Related

source
PortfolioOptimisers.ForecastReadingRule — Type
const ForecastReadingRule = Union{ForecastReversion, ForecastTracking, KernelTrendTracking,
                                  TransactionCostOptimisation, ShortTermSparsePortfolio}

Union type for the Online Selection Rules that read a Price Relative Forecast on their me slot.

Each rule holds its forecaster on me. So the rule views the forecaster at an asset subset, holds the rows that the forecaster reads, seeds its state from the forecaster, and takes its update, through one method of port_opt_view, of rows_needed, of rule_state_seed and of online_update! on this union. forecast_step gives the raw step of each rule. A forecaster can price a part of the universe alone, with NaN at every other asset, as forecast_relative states. The update then reduces the allocation to the priced assets with renormalised_view, takes the step and projects it at them, and gives a zero weight to every other asset, as a batch optimiser does with an asset that its prior cannot price. So on a panel with an asset that never lists, the book is the book of the same rule over the other assets. ForecastReversion also reads the rows of its scale, so it has a method of rows_needed of its own.

Related

source