Base Prior: private API

PortfolioOptimisers.AbstractLowOrderPriorEstimator — Type
abstract type AbstractLowOrderPriorEstimator <: AbstractPriorEstimator

Abstract supertype for low order prior estimators.

A low order prior estimator computes the mean and the covariance from asset returns, from factor returns, or from both. A member of this family returns a LowOrderPrior, not a tuple of moments, so every consumer reads one result type. A member does not subtype this type directly. It subtypes the one of AbstractLowOrderPriorEstimator_A, AbstractLowOrderPriorEstimator_F and AbstractLowOrderPriorEstimator_AF that names the returns it reads.

Related

source
PortfolioOptimisers.AbstractLowOrderPriorEstimator_A_AF — Type
const AbstractLowOrderPriorEstimator_A_AF = Union{AbstractLowOrderPriorEstimator_A,
                                                  AbstractLowOrderPriorEstimator_AF}

Union type for asset-only and asset-and-factor low order prior estimators.

A field typed AbstractLowOrderPriorEstimator_A_AF admits the asset-only shape and the optional-factor shape, and excludes the shape that requires factor returns. A wrapper uses this bound when it fits its nested estimator on one returns matrix that it gives itself. FactorPrior and FactorBlackLittermanPrior fit pe on the factor returns alone. AugmentedBlackLittermanPrior fits a_pe on the assets and f_pe on the factors. The wrapper has no second matrix to give, so the bound refuses a nested estimator that requires one. The refusal happens at construction, not at the call.

Related

source
PortfolioOptimisers.AbstractLowOrderPriorEstimator_F_AF — Type
const AbstractLowOrderPriorEstimator_F_AF = Union{AbstractLowOrderPriorEstimator_F,
                                                  AbstractLowOrderPriorEstimator_AF}

Union type for factor-only and asset-and-factor low order prior estimators.

A field typed AbstractLowOrderPriorEstimator_F_AF admits the factor-requiring shape and the optional-factor shape, and excludes the asset-only shape. A wrapper uses this bound when it passes both returns matrices to its nested estimator, and needs a result that can carry a factor block. HighOrderFactorPriorEstimator is such a wrapper. The bound limits the returns that the nested estimator reads. It does not limit the result that it returns. A member of the optional-factor shape can return a result with rr === nothing, so a consumer that reads the loadings checks them first with assert_prior_regression.

Related

source
PortfolioOptimisers.AbstractLowOrderPriorEstimator_A_F_AF — Type
const AbstractLowOrderPriorEstimator_A_F_AF = Union{AbstractLowOrderPriorEstimator_A,
                                                    AbstractLowOrderPriorEstimator_F,
                                                    AbstractLowOrderPriorEstimator_AF}

Union type for asset-only, factor-only, and asset-and-factor low order prior estimators.

A field typed AbstractLowOrderPriorEstimator_A_F_AF admits all three source shapes, and excludes no subtype of the low order root. A wrapper uses this bound when it passes the returns matrices of the caller to its nested estimator unchanged. The nested estimator then receives the arguments of the caller, and its shape decides what it reads. EntropyPoolingPrior bounds pe this way. The union names the three shapes in place of AbstractLowOrderPriorEstimator for two reasons. Each field that admits the shapes names them, and a fourth shape joins this bound only when someone edits the union.

Related

source
PortfolioOptimisers.Pr_RR — Type
const Pr_RR = Union{AbstractPriorResult, ReturnsResult}

Groups the two result types that hold an asset returns matrix X.

The clustering, phylogeny and centrality forwarders of src/10_Prior/20_PriorReturnsSource.jl dispatch on Pr_RR. Each forwarder reads X from the result that it receives, and calls the method on the asset returns. With one forwarder, an estimator can fit from a prior result or from the returns data. Each forwarder passes dims = 1 after kwargs, because each result type holds its observations along the rows. A dims in kwargs has no effect. When the caller gives both results, returns_matrix_picker picks one of them. The forwarder passes both to the estimator tree as pr and rd, so a FeatureDistance finds its Asset Panel from them.

Related

source
PortfolioOptimisers.PrE_Pr — Type
const PrE_Pr = Union{AbstractPriorEstimator, AbstractPriorResult}

Groups a prior estimator with a fitted prior result.

PrE_Pr is the bound of the pe field of every optimiser. A caller can thus give an optimiser a fitted prior result in place of a prior estimator. prior has a method for each of the two. The estimator method fits, and the result method returns its argument unchanged. The optimiser calls prior once, and has no branch for the kind of value that it holds.

Related

source
PortfolioOptimisers.assert_prior_regression — Function
assert_prior_regression(pr::AbstractPriorResult; ...)
assert_prior_regression(
    pr::AbstractPriorResult,
    sym::Union{AbstractString, Symbol};
    lead
)

Check that a prior result carries a factor block, so that a consumer can read its loadings.

An estimator whose pe field is typed AbstractLowOrderPriorEstimator_F_AF admits the AbstractLowOrderPriorEstimator_AF part of that union. A member of that part reads factor returns only when the caller gives them. The type limits the returns that the estimator reads, and does not tell whether its result carries a regression. An estimator that projects factor moments through the loadings needs the regression, and must check for it.

A prior has pr.rr === nothing for one cause: no estimator in the chain computed a regression, as in EntropyPoolingPrior(; pe = EmpiricalPrior()). No estimator removes a regression, because every wrapper estimator passes on rr and the factor block fpr. The order of the estimators in the chain has no effect. A check of rr covers the whole factor block, because LowOrderPrior requires rr and fpr together or not at all. For this reason, a plotting function that reads fpr.mu or fpr.sigma calls this check on rr before it reads them.

The plotting functions in the factor space also read the factor block, and they get the same message. Only the first sentence is different. lead holds it, and prior_regression_remedy holds the rest.

Arguments

  • pr: Prior result that the consumer received.
  • sym: Name of the field or argument through which the result came, for the error message.
  • lead: First sentence of the message. It names what needed the loadings and what it found. The default is for a wrapper estimator. A consumer that is not an estimator gives its own sentence, because the default says that the type of an estimator field does not make sure of a regression.

Validation

Returns

  • nothing.

Related

source
PortfolioOptimisers.prior_regression_remedy — Constant
prior_regression_remedy

The cause and the remedy of every error that says that a prior carries no factor block.

The string is written once, so the two kinds of consumer give the same cause and remedy. The consumers need the loadings for different purposes. Some project factor moments through the loadings, and some plot them. Each consumer gives its own first sentence in the lead of assert_prior_regression. No consumer writes the cause again. A prior has rr === nothing for one cause, and one remedy corrects it. Both follow from the rule that every wrapper estimator passes on rr and the factor block fpr.

Which errors carry it

Two errors carry it. They are the two ways in which a caller can ask for loadings that no estimator computed.

  • The IsNothingError that assert_prior_regression raises. An estimator raises it when its pe field returns a prior with rr === nothing. A plotting function in the factor space raises it when it receives such a prior. Each caller gives its own lead, and adds this string unchanged after it.
  • The IsNothingError that constraint_space_basis raises when a factor exposure constraint has no basis for its loadings. The space states no basis, and the prior carries no loadings. That message starts with its own sentences about the space, and then adds this string, because the remedy for the prior is the same.

Related

source
PortfolioOptimisers.prior_field_values — Function
prior_field_values(pr::AbstractPriorResult) -> NamedTuple

Return the fields of a prior result as a named tuple, keyed in declaration order.

The function reads with getfield, so it reads only the values that pr stores. It does not read a name that a @forward_properties block adds. forward_prior needs this difference. HighOrderPrior forwards all of its pr, so mu and sigma are properties of it but not fields, and a patch can change only a field. The function reads the field list from the type, so a new field of a prior result type needs no change here.

Algorithm

  1. Read the field names of typeof(pr) into fnames, in declaration order.
  2. Read each of those fields off pr with getfield, and return them as a NamedTuple keyed by fnames.

Arguments

  • pr: Prior result.

Returns

  • vals::NamedTuple: The fields of pr, keyed by name in declaration order.

Related

source
PortfolioOptimisers.prior_result_property_pool — Function
prior_result_property_pool() -> Vector{Symbol}

Return the field names of the two prior result types, with no duplicates.

propagatable_contract_violations checks an @pprop field name against this pool. The generated factory(x, pr::AbstractPriorResult, args...) reads getproperty(pr, :field), and the declaration does not know which prior result type arrives.

The function names the two prior result types, as reconstruct_prior does, and reads their fields from the types. So a result type that gains a field needs no change here. HighOrderPrior forwards all of the pr that it wraps, so the low order names are properties of it but not fields. For this reason, the field names of one result type are not the pool.

The pool holds field names only. A computed property, such as f_mu, f_kt or original_X, is not in it, so an @pprop field cannot name one.

This file defines the function after both prior result types, because it names the concrete types.

Algorithm

  1. Concatenate the field names of LowOrderPrior and of HighOrderPrior into one vector of Symbol.
  2. Remove the duplicates in place, and return the vector. fpr is a field of both types, so the concatenation has a duplicate.

Returns

Related

source
PortfolioOptimisers.bound_field_is_stale — Function
bound_field_is_stale(
    pr::AbstractPriorResult,
    sym::Symbol
) -> Bool

Return true when field sym of pr holds a value that becomes stale if its bound field changes without it.

A field that pr does not have, or that holds nothing, cannot become stale. The function reads with getfield, so a forwarded property with the same name cannot answer for a field that pr does not have.

Algorithm

  1. Check whether typeof(pr) declares a field named sym. When it does not, the binding has no effect on pr, so answer false and read nothing.
  2. Read that field with getfield, and answer true when its value is not nothing.

Arguments

  • pr: Prior result.
  • sym: Name of the bound field to test.

Returns

  • stale::Bool: true when pr holds a value under sym that a change to the field it is bound to would make stale.

Related

source
PortfolioOptimisers.field_states_nothing — Function
field_states_nothing(v) -> Bool

Return true when a field value of a prior result states nothing.

A field states nothing when it holds nothing, or a tuple whose every entry is nothing. The tuple is the form of eoc, which holds one count for each moment, and (nothing, nothing) is its default.

Arguments

  • v: The value of the field.

Returns

  • flag::Bool: true when v states nothing.

Related

source
PortfolioOptimisers.moment_observation_count — Function
moment_observation_count(pr::AbstractPriorResult, k::Integer) -> Real

Return the effective number of observations of moment k of a prior result.

The index follows pr.eoc: 1 is mu, 2 is sigma, 3 is the coskewness and 4 is the cokurtosis. An entry of nothing means size(pr.X, 1), so a result that states no count gives the number of its rows. A consumer that needs the sample size of one moment reads this function, and not size(pr.X, 1).

Arguments

  • pr: Prior result.
  • k: The index of the moment.

Returns

  • T_e::Real: The effective number of observations of the moment.

Related

source
PortfolioOptimisers.moment_count_entry — Function
moment_count_entry(T_e::Real, T::Integer) -> Option{<:Real}

Return the entry of eoc for a moment that is worth T_e observations, in a prior result that carries T rows.

An entry of nothing means the number of rows of the result, so a count equal to T is written as nothing. An unweighted fit with no Scenario Cap then states no count, as it did before the field existed.

Arguments

  • T_e: The effective number of observations of the moment.
  • T: The number of rows that the prior result carries in X.

Returns

  • nothing when T_e == T, and T_e otherwise.

Related

source
PortfolioOptimisers.investable_mask — Function
investable_mask(pr::AbstractPriorResult)::Option{BitVector}
investable_mask(pr::Nothing)

Derive the Investable Mask of a prior result, which is true at every asset whose moments are finite.

A Prior Estimator fits on the coverage universe and returns a result on the full asset universe. An asset that it could not estimate carries NaN in mu and on the diagonal of sigma. This function derives the mask, and no prior result stores it in a field. A caller that needs the mask calls this function. A caller that reduces a result keeps the mask that it received. A reduced result cannot give the mask again, because its mu is finite at every asset.

When every asset is investable, the function returns nothing, and not a mask of true values. With nothing, a universe with no asset to exclude takes the same path as before the mask existed. That path has no reduction, no expansion and no allocation.

The function is a query, so it owns nothing, the answer "none". No prior result, such as the prior of an Allocation Set that reads no rows, gives no mask, and a nothing mask already means every asset.

The function does not read the entries of sigma outside the diagonal. A non-investable asset can carry NaN in all of its row and its column. The diagonal alone decides, so the mask costs one pass over two vectors.

Algorithm

  1. Take the elementwise conjunction of isfinite.(pr.mu) and isfinite.(diag(pr.sigma)).
  2. Raise an IsEmptyError when the conjunction holds no true. An optimisation over no asset has no answer. A problem with no asset that goes further fails at a point far from its cause.
  3. Return nothing when the conjunction holds no false.
  4. Otherwise, return the conjunction.
  5. On nothing in place of a prior result, return nothing.

Arguments

  • pr: Prior result.

Validation

  • At least one asset must be investable.

Returns

  • imsk::Option{BitVector}: true at every investable asset, or nothing when every asset is investable or there is no prior result.

Examples

julia> pr = prior(EmpiricalPrior(),                  ReturnsResult(; nx = ["a", "b"], X = [0.1 -0.2; -0.1 0.2; 0.05 0.1]));julia> isnothing(PortfolioOptimisers.investable_mask(pr))true

Related

source
investable_mask(
    pr::HighOrderPrior
) -> Union{Nothing, BitVector}

Derive the Investable Mask of a HighOrderPrior from every order of moment that the prior result holds.

The AbstractPriorResult method reads mu and the diagonal of sigma. Under a CoveragePolicy, an asset with a finite mu and a finite variance can have a co-moment that is not finite. Each estimator holds its own policy, so pe.pe can hold one while pe.ske and pe.kte do not. This method also reads the diagonal entry of each asset in sk and kt, with comoment_investable. An asset that the co-moments did not estimate leaves the problem. Without this, the asset gets to a spectral step that throws.

This method does not warn when the mask removes such an asset. Each entry to an optimiser derives the mask again, and the derivation must have no side effect. assert_matched_coverage warns once, at the fit.

Arguments

  • pr: Prior result.

Validation

  • At least one asset must be investable.

Returns

  • imsk::Option{BitVector}: true at every investable asset, or nothing when every asset is investable.

Related

source
PortfolioOptimisers.investable_views — Function
investable_views(pr::AbstractPriorResult, sets::Nothing) -> Tuple
investable_views(pr::AbstractPriorResult, sets::UniverseSets) -> Tuple

Derive the Investable Mask of a fitted prior, and give a view builder the universe on which it can write rows.

A view is a dense linear form over the asset axis. A departed asset carries NaN in mu and on the diagonal of sigma. A[i] == 0 does not remove the NaN from a row, because 0 * NaN is NaN. A view that names only active assets gets a NaN row, the same as a view that names the departed asset. The solver receives a row of NaN, and the fit fails with an error that names a wrong cause. The fix is to build the row on the investable columns only. Every optimiser takes the same reduction at its entry, port_opt_view of the prior result at findall(imsk). That reduction gives the same bits as a prior reduced by hand.

The two prior families that take views both reduce here. Each does its own work after the reduction. A row of entropy pooling is over the observations, so its solved probabilities have no asset axis, and nothing expands back. The moments come from the wrapped prior after its new fit, which already holds the NaN values of the full universe. A Black-Litterman posterior is a pair of moments over the reduced assets. The estimator must write it back into a NaN frame of the full width with expand_moment before it returns. For this difference, this function returns the index to the caller.

This function also adds the Non-Investable Axis to the sets that it gives the builders, with non_investable_sets after port_opt_view. The order matters, because the view drops the axis, so that a subproblem does not get the departures of its parent. With the axis, a builder can tell a departed name from a wrong name. It drops the row of a departed name with no message. For a wrong name, the strict_diagnostic rule applies.

The method selects on sets by dispatch, and on the mask by a condition, for these reasons:

  • sets is a field of a @concrete estimator. Whether it is nothing is a fact of its type, fixed for each instance. Two methods let the compiler select at no cost, and keep the returned sets a concrete UniverseSets. One method over Option{<:UniverseSets} returns a Union of values. The view builders declare sets::UniverseSets, so JET reports a missing method for the Nothing part at every builder call site, though no call reaches it.
  • investable_mask returns a Union{Nothing, BitVector} that the data decides. Julia splits a Union of two types and compiles the two cases in this branch. A dispatch on it gives nothing more, and adds a unit to the file.

When sets is nothing, the method returns at once, whatever the mask. There is nothing to build and nothing to reduce, because the constructor of an estimator that takes views refuses nothing sets when it holds a view. When every asset is investable, the method returns its arguments unchanged. A fit with no gap then reads two vectors once and allocates nothing.

Algorithm

  1. When sets is nothing, return nothing, sets and no departed names. The field type of the estimator selects this method.
  2. Otherwise, derive the Investable Mask from the fitted prior with investable_mask. When it is nothing, return nothing, sets and no departed names.
  3. Read the asset universe from sets.dict[sets.xkey], and check it against the mask.
  4. Read the departed names with non_investable_names.
  5. Take a port_opt_view of sets at findall(imsk), and add the Non-Investable Axis to it with non_investable_sets. Return the mask, the new sets and the departed names.

Arguments

  • pr: Prior result.
  • sets: The UniverseSets of the estimator, or nothing.

Validation

  • length(sets.dict[sets.xkey]) == length(imsk). Otherwise, raises a DimensionMismatch that names both counts, in place of the BoundsError that the complement raises.

Returns

  • (imsk, sets, ni): The Investable Mask or nothing, the reduced sets that carry the Non-Investable Axis, and the departed names. investable_prior takes its view at the mask, and expand_moment writes back through it. A caller that reduces and expands needs nothing more.

Related

source
PortfolioOptimisers.investable_prior — Function
investable_prior(imsk::Nothing, pr::AbstractPriorResult) -> AbstractPriorResult
investable_prior(imsk::BitVector, pr::AbstractPriorResult) -> AbstractPriorResult

View a fitted prior at the Investable Mask that investable_views derived.

This function is separate from investable_views because a staged entropy pooling fit fits its wrapped prior again between stages, once for each solve. Each new fit needs a new view before the builders of the next stage read it. The mask does not change, because no new weights of the observations can estimate a column that had no estimate. So the caller derives the mask once and applies this function many times.

A mask of nothing means that every asset is investable, and the function returns the prior unchanged. A fit with no gap thus allocates nothing. Here two methods select on the mask, and investable_views uses a condition. The two agree. The mask comes from a local variable whose Union Julia already split at the call site, so each branch calls this function with a concrete argument. One method over Option{BitVector} would bring the Union back, so the function must not become one.

Arguments

  • imsk: The Investable Mask, or nothing when every asset is investable.
  • pr: Prior result.

Returns

  • pr::AbstractPriorResult: The prior over the investable assets, or the prior unchanged.

Related

source
PortfolioOptimisers.investable_universe_names — Function
investable_universe_names(sets::Nothing, imsk) -> VecStr
investable_universe_names(sets::UniverseSets, imsk::Nothing) -> VecStr
investable_universe_names(sets::UniverseSets, imsk::BitVector) -> VecStr

Name the departed assets for an estimator that reduces its asset axis, and whose views are on a different axis.

Every estimator whose views resolve against xkey calls investable_views. That function reduces the sets, adds the Non-Investable Axis to them, and finds the names. BayesianBlackLittermanPrior and FactorBlackLittermanPrior write their views on the factor axis. They reduce their assets, and do not change a view axis. They have no axis to add. A call of investable_views would make them require an asset universe that they do not use for any other purpose. They still report departed assets, and this function gives them only the names that they need.

When the caller states no sets, the function returns an empty list and raises no error. announce_non_investable prints nothing for an empty list. This is the correct result for a valid configuration. Both estimators admit sets of nothing, because a precomputed BlackLittermanViews resolves no name and needs no universe. A stated universe that does not match this fit also returns an empty list. Neither estimator reads an asset name for any other purpose, so no other check reads the length of the universe.

Arguments

  • sets: The estimator's UniverseSets, or nothing.
  • imsk: The Investable Mask, or nothing when every asset is investable.

Returns

  • ni::VecStr: The names the mask left out, or an empty vector.

Related

source
PortfolioOptimisers.held_non_investable — Function
held_non_investable(imsk::BitVector, w::VecNum)
held_non_investable(imsk::BitVector, w::VecVecNum)
held_non_investable(imsk::BitVector, W::MatNum)

Find the assets a portfolio holds and the Investable Mask excludes.

A non-investable asset carries NaN in mu and on the diagonal of sigma, so it has no moment. The reduction of a portfolio that holds no such asset loses nothing. A portfolio that holds such an asset needs a report to the caller, and this function finds those assets. A weight path holds an asset when one of its rows holds it. A population holds an asset when one of its members holds it.

Arguments

  • imsk: The Investable Mask, true at every asset whose prior moments were finite.
  • w: Portfolio weights, a population of them, or a weight path (observations × assets).

Returns

  • held::VecInt: Indices of the held non-investable assets, empty when there are none.

Related

source
PortfolioOptimisers.investable_weights_view — Function
investable_weights_view(imsk::Nothing, w)
investable_weights_view(imsk::BitVector, w::Nothing)
investable_weights_view(imsk::BitVector, w::VecNum)
investable_weights_view(imsk::BitVector, w::VecVecNum)
investable_weights_view(imsk::BitVector, w::MatNum)

Take the view of the weights at the Investable Mask.

A weight vector is one cross-section, so the mask selects its entries. A weight path has one row of weights for each observation. The mask selects its columns, and every row keeps its own observation. The function reduces a population one member at a time. The asset return history of a fold has the shape of a path, observations × assets. The consumers of a HeldWeightsResult that take a fold use the same view of it, so this one column selection serves both.

When either argument is nothing, the function returns the argument w that it received. An imsk of nothing means that every asset is investable, so the function selects nothing and returns the weights unchanged. A w of nothing means that the caller gave no weights. equilibrium_mu then uses equal weights over the axis that it receives. There is nothing to reduce, and a reduction would need a length that does not exist. Dispatch selects both cases, so they cost nothing.

The w of a prior estimator is a per-asset configuration over the full universe of the caller. It meets a reduced axis for the same reason as a per-asset bound. The function was first written for the weights that an optimisation returns. A prior also reduces its own weights with it, because the two reductions are one operation.

Arguments

  • imsk: The Investable Mask, true at every asset whose prior moments were finite, or nothing when every asset is investable.
  • w: Portfolio weights, a population of them, a weight path (observations × assets), or nothing.

Returns

  • The view of w at the investable assets, or w itself when either argument is nothing.

Related

source
PortfolioOptimisers.reads_factor_returns — Function
reads_factor_returns(pe::Union{AbstractPriorEstimator, Online}) -> Bool
reads_factor_returns(pe::Union{Nothing, AbstractPriorResult, AbstractEstimator}) -> Bool

Answer whether a fit through pe reads the factor returns. When it does, a gap in the factor returns drops the observation, see shared_series_view.

A tree reads them when needs_factor_returns answers true. A tree whose factor arguments are all optional answers nothing, and it reads no factor return of its own. When pe is nothing, or a prior result that a caller computed before, the fit reads no factor return. Any other estimator in the place of a prior is a schedule, and the nearest fold loop resolves it to a prior. That prior drops its own gaps at its entry, so the answer is false.

Arguments

  • pe: The prior estimator, a prior result, or nothing.

Returns

  • flag::Bool: true when the fit reads the factor returns.

Related

source
PortfolioOptimisers.shared_series_record — Function
shared_series_record(pr::AbstractPriorResult, rows::Nothing, T::Integer)
shared_series_record(pr::LowOrderPrior, rows::VecInt, T::Integer)
shared_series_record(pr::HighOrderPrior, rows::VecInt, T::Integer)
shared_series_record(pr::AbstractPriorResult, state::SampleBufferState)

Record on a prior the observations of the returns data that its fit kept after a Shared Series cut.

A fit drops each observation with a Shared Series gap before it starts, see shared_series_view and shared_series_buffer. After the cut, no part of the prior states which observation of the data each of its rows is. The function writes two records:

  • ss_gaps, the marks of the dropped observations, one for each of the T observations of the data. A reader that holds the data pairs the rows of X with its own rows through the marks, see shared_series_kept_rows.
  • The row key of the factor model block. The batch verb counts the position of each row among the observations that it kept, and kept row k is observation rows[k] of the data. The function replaces each position with its observation, so that a realised factor_attribution of a cross-validation over the same data matches the correct rows. A key of positions matches each row after a gap to the next row, and raises no error. The timestamps come from the kept rows, so they need no change.

prior calls it after it fits on the returns data, and after it reads out a sample buffer. An optimiser calls it on the prior that it fits after its own cut, see shared_series_solve.

Algorithm

Julia selects the method by the types of the arguments. With rows, the method does these steps:

  1. Mark every one of the T observations as dropped. Call the marks ss_gaps.
  2. Remove the mark from the observations that the fit kept. A prior that an optimiser fits on its cut data can already hold marks of the length(rows) kept observations. Then remove the mark only from the rows at the positions that those marks keep, so that the two cuts combine.
  3. Map the row key of the factor model block through rows with map_row_positions. Forward the key and ss_gaps onto pr with forward_prior. A HighOrderPrior forwards the record of the LowOrderPrior that it wraps.

Arguments

  • pr: The prior that the fit returned.
  • rows: The observations that the fit kept, from shared_series_rows, or nothing when it kept every observation.
  • T: The number of observations of the data that the fit received.
  • state: The sample buffer that a new fit read. Its kept rows are those of buffer_kept_rows, and its observations are its n rows.

Validation

Returns

  • pr::AbstractPriorResult: pr itself when the fit kept every observation, and otherwise pr with the marks and with the positions of its block mapped through rows.

Related

source
PortfolioOptimisers.assert_shared_series_gaps — Function
assert_shared_series_gaps(ss_gaps::Nothing, n::Integer)
assert_shared_series_gaps(ss_gaps::AbstractVector{Bool}, n::Integer)

Check the Shared Series marks ss_gaps of a LowOrderPrior against the number of rows n of its X.

Validation

  • At least one mark is true. Otherwise, raises an ArgumentError. A prior that dropped no observation records nothing. So the returns data after the cut always holds fewer observations than the marks, and shared_series_kept_rows tells the two apart by their length.
  • The marks keep at least n observations, because the rows of X are the last observations that the marks keep. Otherwise, raises a DimensionMismatch.

Returns

  • nothing.

Related

source
PortfolioOptimisers.assert_pool_cache — Function
assert_pool_cache(pc::Nothing, T::Integer)
assert_pool_cache(pc::NamedTuple, T::Integer)

Check the cache of an opinion pool against the scenarios of the LowOrderPrior that carries it.

The pooled weights of the cache weigh the scenarios of the result, so the weights and the rows of the cache match the rows of the result.

Arguments

  • pc: The cache of the pool, (; X, w, pe2) with the cache of pe2 in pe2, or nothing.
  • T: The number of scenarios of the result.

Validation

  • length(pc.w) == T. Raises a DimensionMismatch otherwise.
  • size(pc.X, 1) == T when pc.X is a matrix. Raises a DimensionMismatch otherwise.

Returns

  • nothing.

Related

source
PortfolioOptimisers.shared_series_kept_rows — Function
shared_series_kept_rows(pr, T::Integer) -> Option{Vector{Int}}

Return the observations that a prior kept after a Shared Series cut, when the returns data of a reader is the data that the prior read.

A reader holds T observations of the returns data. When T equals the number of marks in pr.ss_gaps, the reader holds the data that the prior read. The rows of pr.X are then the last of the observations that the marks keep. Otherwise, the reader holds the data after the same cut, or data with no record. The rows of pr.X are then its last observations, which was the rule of every reader before the record existed. The marks are nothing when the prior dropped no observation, so the cut data never has as many observations as the marks.

Arguments

  • pr: The prior result, the returns data, a prior estimator, or nothing. Only a prior result holds marks: a LowOrderPrior in its field ss_gaps, and a HighOrderPrior in the low order prior that it wraps.
  • T: The number of observations of the returns data that the reader holds.

Returns

  • rows::Option{Vector{Int}}: The positions of the kept observations in the data of the reader, or nothing when the reader pairs the last rows.

Related

source
PortfolioOptimisers.prior_data_rows — Function
prior_data_rows(pr, T::Integer, n::Integer) -> AbstractVector{Int}

Return the observations of the returns data of a reader that pair the last n rows of the X of a prior.

A fit drops rows at the start, for a Scenario Cap and for the warm-up of a CrossSectionalFactorPrior. It also drops the observations of a Shared Series gap. When the reader holds the data that the prior read, the rows of X are the last of the observations that the marks pr.ss_gaps keep, see shared_series_kept_rows. Otherwise, they are the last n observations of the data of the reader.

Arguments

  • pr: The prior result, or the returns data that a reader reads in its place.
  • T: The number of observations of the returns data that the reader holds.
  • n: The number of rows to pair, at most the number of rows of pr.X.

Returns

  • rows::AbstractVector{Int}: The positions of the paired observations in the data of the reader, in order.

Related

source
PortfolioOptimisers.map_row_positions — Function
map_row_positions(rr::Option{<:AbstractLoadingsRegressionResult}, rows::VecInt)
map_row_positions(rr::Regression, rows::VecInt)
map_row_positions(rr::CrossSectionalFactorModel, rows::VecInt)

Return a factor model block whose positions are mapped through rows, and every other field unchanged.

Position k becomes rows[k], with kept_row_positions. The function returns unchanged a block that records no row key, and returns nothing when there is no block.

Arguments

  • rr: The factor model block, or nothing.
  • rows: The observation of each position.

Returns

  • rr: The block with its positions mapped, or rr itself.

Related

source
PortfolioOptimisers.kept_row_positions — Function
kept_row_positions(rows::Nothing, pos::Option{<:VecInt})
kept_row_positions(rows::VecInt, pos::Nothing)
kept_row_positions(rows::VecInt, pos::VecInt)

Return the observation of each position among the kept rows.

Kept row k is observation rows[k]. When rows is nothing, the fit kept every observation, so the positions are the observations. When pos is nothing, there is nothing to map.

Arguments

  • rows: The kept observations, or nothing when the fit kept every observation.
  • pos: The positions among the kept rows, or nothing.

Returns

  • idx::Option{<:VecInt}: rows[pos], pos itself when rows is nothing, or nothing when pos is nothing.

Related

source