Higher-Moment Partial Fit

The coskewness and the cokurtosis can take new observations one block at a time. partial_fit! adds a block of observations to the state of an estimator, and coskewness and cokurtosis compute the estimate from that state. Only the FullMoment form of each estimator has an incremental fit. SemiMoment keeps only the deviations below a centre, and each new observation moves that centre.

Functions

PortfolioOptimisers.port_opt_view — Method
port_opt_view(
    x::CoskewnessPartialFitState,
    i,
    args...
) -> CoskewnessPartialFitState

Slices a CoskewnessPartialFitState to the selected assets.

A third co-moment reads three assets' observations and no fourth, so the sub-tensor over a subset of assets is the state of that subset, entry for entry, and the count passes through. M3 is assets × assets², and its column for the pair $(p, q)$ is $(p - 1) N + q$, so the slice keeps the rows i and the columns of every pair drawn from i. fourth_moment_index_generator builds that column set, in the order the sliced universe numbers its own pairs.

The slice copies by index and does not view, so a later partial_fit! on the viewed estimator writes into arrays of its own. The copy is small: a cluster's sub-tensor is cubic in the cluster size, where the full one is cubic in the universe size.

Arguments

  • x: The state to slice.
  • i: Index or indices of the assets to keep.
  • args...: Additional positional arguments (ignored).

Returns

  • state::CoskewnessPartialFitState: The state of the same sample over the selected assets.

Related

source
PortfolioOptimisers.port_opt_view — Method
port_opt_view(
    x::CokurtosisPartialFitState,
    i,
    args...
) -> CokurtosisPartialFitState

Slices a CokurtosisPartialFitState to the selected assets.

A fourth co-moment reads four assets' observations and no fifth, so the sub-tensor over a subset of assets is the state of that subset, entry for entry, and the count passes through. M4 is assets² × assets², and both of its axes are numbered by the pair $(p, q)$ at $(p - 1) N + q$, so the slice keeps the columns of every pair drawn from i on both axes. M3 keeps the rows i and the same column set.

The slice copies by index and does not view, so a later partial_fit! on the viewed estimator writes into arrays of its own. The copy is small next to the state it comes from: a cluster's sub-tensor is quartic in the cluster size, so ten clusters of ten assets copy 80 KB each out of the 800 MB a hundred-asset state holds.

Arguments

  • x: The state to slice.
  • i: Index or indices of the assets to keep.
  • args...: Additional positional arguments (ignored).

Returns

  • state::CokurtosisPartialFitState: The state of the same sample over the selected assets.

Related

source
PortfolioOptimisers.partial_fit — Method
partial_fit(
    ske::Coskewness{<:Any, <:Any, <:FullMoment},
    args...;
    kwargs...
) -> Any

Coskewness method of partial_fit. Forwards to partial_fit!, because the fold already writes into no array of the state it was given.

This is the one family that overrides the generic method. The generic method copies the state before it folds, and a copy is what gives the verb its value semantics. This family's fold reaches merge_states, which reads the two states and builds a third, so the estimator handed over is already left as it was and the copy buys nothing. The override is kept for a measured loss: at 30 assets, one row takes 0.29 ms and 2.5 MiB through it, and 0.34 ms and 2.7 MiB through the generic method.

Algorithm

  1. Forward X and every keyword to partial_fit!, and return the estimator it gives.

Arguments

  • ske: Coskewness estimator with a FullMoment moment algorithm.
  • args...: The observations, forwarded to partial_fit!.
  • kwargs...: Additional keyword arguments, forwarded to partial_fit!.

Returns

  • ske::Coskewness: A new estimator carrying the folded state.

Related

source
PortfolioOptimisers.partial_fit — Method
partial_fit(
    kte::Cokurtosis{<:Any, <:Any, <:FullMoment},
    args...;
    kwargs...
) -> Any

Cokurtosis method of partial_fit. Forwards to partial_fit!, because the fold already writes into no array of the state it was given.

The companion of the Coskewness method, and the one that pays for the override. The generic method would copy M4 before every fold, which is assets² × assets²: 800 MB at a hundred assets, per call, for a copy no caller reads. The override is kept for that measured loss: at 30 assets, one row allocates 151 MiB through it, and 158 MiB through the generic method.

Algorithm

  1. Forward X and every keyword to partial_fit!, and return the estimator it gives.

Arguments

  • kte: Cokurtosis estimator with a FullMoment moment algorithm.
  • args...: The observations, forwarded to partial_fit!.
  • kwargs...: Additional keyword arguments, forwarded to partial_fit!.

Returns

  • kte::Cokurtosis: A new estimator carrying the folded state.

Related

source
Statistics.mean — Method
mean(me::AbstractExpectedReturnsEstimator) -> Any

Reads the expected returns out of the partial-fit state that an expected returns estimator carries.

This is the call with no data of an estimator that has no such call of its own. It reads the state out of the cache field with partial_fit_cache, and it gives the state to the method of Statistics.mean that takes one. A SampleBufferState, which Online seeds, runs the batch verb over the rows of the buffer. A family that folds exactly has a call with no data of its own, so this method serves the estimators that fold by buffering alone, such as WindowedExpectedReturns.

Arguments

  • me: Expected returns estimator.

Validation

  • me carries a state. An ArgumentError is thrown otherwise.

Returns

  • mu::ArrNum: Expected returns vector assets x 1 if the dims keyword does not exist or dims = 2, 1 x assets if dims = 1.

Related

source
PortfolioOptimisers.coskewness — Function
coskewness(ske::CoskewnessEstimator) -> Any
coskewness(
    ske::CoskewnessEstimator,
    state::SampleBufferState
) -> Any

Reads a coskewness tensor out of a SampleBufferState, by running the batch verb over the observations the buffer holds.

The method of coskewness that makes the estimate from a Sample Buffer. An estimator wrapped in Online carries a buffer rather than a family state, so it takes no exact fold, and its estimate is whatever a batch fit over the rows the buffer holds gives — every observation folded so far when the buffer is uncapped, and the last max_history of them when it is capped. It runs no assert_partial_fittable check of its own: the batch verb answers configurations an incremental fold cannot, and a wrapper is how a caller reaches them. The buffer also holds the per-observation masks it was folded with, so the batch verb is given the mask that explains the rows and a wrapped estimator under a CoveragePolicy answers what an unwrapped one answers.

The call with no data, coskewness(ske), fills state with the state that the estimator carries, which partial_fit_cache reads out of the cache field. The call then goes to the method of coskewness for the type of that state. This is the call with no data of an estimator that has no such call of its own. A family that folds exactly has a call with no data of its own, so the default serves the estimators that fold by buffering alone, such as WindowedCoskewness.

Arguments

  • ske: Coskewness estimator.
  • state: The buffer to read. The default is the state that ske carries.

Validation

  • With no state, the estimator carries a state. An ArgumentError is thrown otherwise.

Returns

  • (cskew, V)::Tuple: The coskewness tensor of the observations the buffer holds, and its negative spectral shape.

Related

source