Forecast rules: private API
PortfolioOptimisers.assert_forecaster — Function
assert_forecaster(me::AbstractExpectedReturnsEstimator)
Refuses an Online wrapper in the forecaster slot of a rule.
The rows buffer of the head already holds the rows that the forecaster reads. A second buffer would hold them twice.
Validation
meis not anOnline, and holds none at any depth. AnArgumentErroris thrown otherwise, and it names the path to the wrapper.
Returns
nothing.
Related
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
ArgumentErroris 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
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_momentdecides it. - $n_{\mathrm{fit}}$: Least number of rows that the estimator states through
fit_min_rows.
Related
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
- When
estis a covariance, variance or prior estimator, or aStatsBase.CovarianceEstimator, returntrue. Stop. - When
estis another estimator, call the function on each field thatestimator_fieldsnames, and returntruewhen any call returnstrue. - Return
falsefor a value that is not an estimator.
Related
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
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
PortfolioOptimisers.last_active_mask — Function
last_active_mask(pnl::Nothing)
last_active_mask(pnl::AssetPanel)Returns the active mask of the last row of an Asset Panel, or nothing when there is no panel or the panel is static.
The fold arm of forecast_relative folds the current row under this mask.
Related
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
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