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.flat_where_undefined — Function
flat_where_undefined(xhat::AbstractVector) -> Any

Reads each non-finite entry of a Price Relative Forecast as one, so the step holds that asset.

A forecaster whose estimate is undefined over the first rows, such as a variance over one observation, therefore holds until the estimate is defined.

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.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, and seeds its state from the forecaster, through one method of port_opt_view, of rows_needed and of rule_state_seed on this union. ForecastReversion also reads the rows of its scale, so it has a method of rows_needed of its own.

Related

source