Returns buffer state
The fold context of the online step
An optimiser's online step forwards each observation to its prior and records the rest of the carrier in a PortfolioOptimisers.ReturnsBufferState: the factor, benchmark and timestamp columns as buffers of their own, and the names and the static Asset Panel pinned by the first step. The returns are owned once, by the prior, and this state holds them only where no prior sits beneath the optimiser. PortfolioOptimisers.returns_result rebuilds the ReturnsResult a batch fit over the same observations would have read.
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
state::ReturnsBufferState,
rd::ReturnsResult;
own_returns,
own_factors
) -> ReturnsBufferState{__T_nx, __T_X, __T_nf, __T_F, __T_nb, __T_B, __T_ts, __T_pnl, __T_max_history} where {__T_nx<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_X<:Union{Nothing, SampleBufferState}, __T_nf<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_F<:Union{Nothing, SampleBufferState}, __T_nb<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_B<:Union{Nothing, SampleBufferState, AbstractVector}, __T_ts<:Union{Nothing, AbstractVector}, __T_pnl<:Union{Nothing, AssetPanel}, __T_max_history<:Union{Nothing, Integer}}
Folds the observations of a ReturnsResult into a ReturnsBufferState.
The state's own step: it pins the context on the first observation, checks it on every later one, and appends each column the carrier holds to the buffer of that column. The returns are appended only when the state owns them, which own_returns decides once per host; a state whose prior carries the rows appends nothing for X and nothing for the masks, because both travel to the prior. The factor column is appended only when the state owns it, which own_factors decides once per host from the prior's tree: a prior that reads F records it in its own buffer, whose first-append rule then refuses a column that comes and goes, so this state neither keeps it nor checks its presence. The panel is either pinned, when it is static, or left to the returns buffer, when it is time-varying.
Algorithm
- On the first observation, pin
nx,nf,nband a staticpnl. On every other, refuse a carrier whose pinned context differs, and one that adds or drops a column the state owns. - Append
Xwhere the state owns the rows, with the active mask of a time-varying panel. - Append
Fwhere the state owns the factor column, andBandtswhere the carrier holds them.
Arguments
state: The state to fold into.rd: The carrier holding one or more observations,observations × assets.own_returns: Whether this state keeps the returns, which istruefor a head that holds no prior andfalseotherwise.own_factors: Whether this state keeps the factor column, which istruefor a head that holds no prior and for a host whose prior's tree never reads it, andfalseotherwise.
Validation
rd.Xis notnothing. AnIsNothingErroris thrown otherwise.- The pinned context and the column presence agree with the first step. An
ArgumentErroris thrown otherwise.
Returns
state::ReturnsBufferState: The state after the last observation.
Related
PortfolioOptimisers.port_opt_view — Method
port_opt_view(
x::ReturnsBufferState,
i,
args...
) -> ReturnsBufferState{__T_nx, __T_X, __T_nf, __T_F, __T_nb, __T_B, __T_ts, __T_pnl, __T_max_history} where {__T_nx<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_X<:Union{Nothing, SampleBufferState}, __T_nf<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_F<:Union{Nothing, SampleBufferState}, __T_nb<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_B<:Union{Nothing, SampleBufferState, AbstractVector}, __T_ts<:Union{Nothing, AbstractVector}, __T_pnl<:Union{Nothing, AssetPanel}, __T_max_history<:Union{Nothing, Integer}}
Slices a ReturnsBufferState to the selected assets.
The asset axis runs through nx, X, a matrix benchmark and its names, and the panel; the factors, the timestamps and a single-column benchmark are not indexed by asset and are copied unchanged, for the reason port_opt_view on a SampleBufferState copies its factor rows — a later fold on the viewed estimator must not write through into the estimator the view was taken from.
Arguments
x: The state to slice.i: Index or indices of the assets to keep.args...: Additional positional arguments, forwarded to the returns buffer.
Returns
state::ReturnsBufferState: The state of the same observations over the selected assets.
Related
PortfolioOptimisers.merge_states — Method
merge_states(
a::ReturnsBufferState,
b::ReturnsBufferState
) -> ReturnsBufferState{__T_nx, __T_X, __T_nf, __T_F, __T_nb, __T_B, __T_ts, __T_pnl, __T_max_history} where {__T_nx<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_X<:Union{Nothing, SampleBufferState}, __T_nf<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_F<:Union{Nothing, SampleBufferState}, __T_nb<:Union{Nothing, AbstractVector{<:AbstractString}}, __T_B<:Union{Nothing, SampleBufferState, AbstractVector}, __T_ts<:Union{Nothing, AbstractVector}, __T_pnl<:Union{Nothing, AssetPanel}, __T_max_history<:Union{Nothing, Integer}}
Folds two ReturnsBufferState fitted on disjoint blocks into the state of the concatenated block.
Each column merges as its buffer merges — concatenation — and the pinned context must agree, because two runs over different universes describe no single run.
Arguments
a: The state of the first block of observations.b: The state of the second block of observations.
Validation
- The pinned context of
aandbagree. AnArgumentErroris thrown otherwise.
Returns
state::ReturnsBufferState: The state the two blocks give when they are folded as one block.
Related