Prices result
Types
PortfolioOptimisers.PricesResult — Type
struct PricesResult{__T_X, __T_F, __T_B, __T_iv, __T_ivpa, __T_pnl, __T_span} <: AbstractPricesResultA container for aligned, time-indexed price-level data.
PricesResult is the prices-level mirror of ReturnsResult: it bundles asset prices with optional factor, benchmark, and implied volatility series, all as TimeSeries.TimeArrays. It is the input to price-level preprocessing estimators and prices-to-returns conversion, and the type that defines timestamp-window slicing for pipeline cross-validation via port_opt_view.
The asset price series X is the master clock: port_opt_view selects observation windows on X and aligns the other series to the selected timestamps.
The AssetPanel pnl and the Listing Span span are the exceptions to that alignment. Neither is a TimeArray — a Panel Field may be a 3-dimensional array, a static panel has no clock at all, and a span is two integers per asset — so they cannot be aligned by timestamp, only indexed positionally. Their axes are therefore held parallel to X: the asset axis to TimeSeries.colnames(X), and, for the time-varying shape, the observation axis to TimeSeries.timestamp(X) row for row. Every routine that drops an asset or an observation from X must drop it from both in the same step, which is what port_opt_view, MissingDataFilter and prices_to_returns do, through panel_carrier_view and span_carrier_view.
Fields
X: Asset price data (observations × assets). The master clock for timestamp-window slicing.
F: Optional factor price data (observations × factors).
B: Optional benchmark price data (observations × 1) or (observations × assets).
iv: Optional implied volatility data (observations × assets). An absent value is carried asNaN, and the estimator that reads the series excludes the asset from its fit.
ivpa: Implied volatility risk premium adjustment, if a vector (assets × 1).
pnl: OptionalAssetPanel: the Panel Fields of the universe, and its two universe masks. Not aTimeArray: its axes are held positionally parallel toX.
span: Optional Listing Span: which assets are listed at each observation of the price clock,observations × assets, held positionally parallel toX. APortfolioOptimisers.ListingSpanwhenPriceIngestionderived it by the Span Rule, and any otherAbstractMatrix{Bool}when a caller declared their own listing calendar.nothingsays the carrier was not built by the ingestion layer.
Constructors
PricesResult(; X::TimeSeries.TimeArray, F::Option{<:TimeSeries.TimeArray} = nothing, B::Option{<:TimeSeries.TimeArray} = nothing, iv::Option{<:TimeSeries.TimeArray} = nothing, ivpa::Option{<:Num_VecNum} = nothing, pnl::Option{<:AssetPanel} = nothing, span::Option{<:AbstractMatrix{Bool}} = nothing,) -> PricesResultKeywords correspond to the struct's fields.
Validation
!isempty(X).- If
Fis notnothing:!isempty(F). - If
Bis notnothing:!isempty(B), andsize(values(B), 2) in (1, size(values(X), 2)). - If
ivis notnothing:!isempty(iv),size(values(iv), 2) == size(values(X), 2), and every value is finite and non-negative where it is present (an absent one isNaN, ormissingon a carrier built by hand; seeassert_nonneg_where_present). - If
ivpais notnothing:all(x -> x > 0, ivpa),all(x -> isfinite(x), ivpa); if a vector,length(ivpa) == size(values(X), 2). pnl's asset axis issize(values(X), 2), and its observation axis issize(values(X), 1)when it is time-varying. Seecheck_asset_panel.- If
spanis notnothing:size(span) == size(values(X)). Raises aDimensionMismatch.
Examples
julia> X = TimeArray(Date(2020, 1, 1):Day(1):Date(2020, 1, 3), [100.0 101.0; 102.0 103.0; 104.0 105.0], ["A", "B"]);julia> pr = PricesResult(; X = X);julia> size(values(pr.X))(3, 2)Related
Functions
PortfolioOptimisers.port_opt_view — Method
port_opt_view(
pr::PricesResult,
_::Colon,
_::Colon
) -> PricesResult
Return a view of the PricesResult for the observation window i and the assets j of the asset price series X.
The asset price series is the master clock: i selects rows of X, and the factor, benchmark, and implied volatility series are aligned to the selected timestamps (rows whose timestamps are absent from a series are dropped from that series). j selects asset columns and defaults to :, so a call giving only i is an observation window over the whole universe.
Algorithm
The method that Julia selects is the algorithm. The timestamp methods do the work, and the integer method routes into them.
iandjare bothColon: returnpritself. No view is built.iis a vector of timestamps andjis aColon: indexX,F,Bandivby the timestampsi. Recover the rows of a time-varying Asset Panel from the surviving timestamps withfeature_row_indices, and view the panel on that observation axis withpanel_carrier_view. A static panel has no observation axis and ignores the row index. View the Listing Span on the same surviving timestamps withspan_carrier_view. Carryivpathrough untouched, because the asset index does not reach it. Rebuild thePricesResult.iis a vector of timestamps andjis a vector of asset indices:- Index
Xby the timestampsi, then keep the asset columnsj. - Index
Fby the timestampsialone.jis an asset index, and the factors are a separate axis, so every factor column is kept. - Index
Bby the timestampsi. Keep its columnsjwhenBholds one column per asset, and keep its single column otherwise. The test isB's own width, because a shared benchmark has one column to give whateverjasks for. - Index
ivby the timestampsiand the asset columnsj, and viewivpaatj. - Recover the rows of a time-varying Asset Panel with
feature_row_indices, and view the panel at those rows and the assetsjwithpanel_carrier_view, handing it the asset names so that a tensor Panel Field whose labels are the asset names (features_are_assets) is cut on its label axis too. - View the Listing Span at the surviving timestamps and the assets
jwithspan_carrier_view. - Rebuild the
PricesResult.
- Index
iandjare integer indices, ranges orColons: read the timestampsTimeSeries.timestamp(pr.X)[i], and call step 2 or step 3 with them. This is the method a caller reaches withport_opt_view(pr, 2:3).
Arguments
pr: APricesResultobject.i: Observation window into the rows ofpr.X. Either integer indices (AbstractVector{<:Integer},AbstractRange, orColon) or a vector of timestamps (AbstractVector{<:Dates.AbstractTime}).j: Asset window into the columns ofpr.X. Integer indices, anAbstractRange, orColonfor the whole universe. AColonleavesX,B,ivandivpaalone, which is whyivpapasses through untouched on the observation-only arity and is viewed atjon the other.
Returns
new_pr::PricesResult: A newPricesResultcontaining only the data for the selected window.
Examples
julia> X = TimeArray(Date(2020, 1, 1):Day(1):Date(2020, 1, 3), [100.0 101.0; 102.0 103.0; 104.0 105.0], ["A", "B"]);julia> pr = PricesResult(; X = X);julia> pv = PortfolioOptimisers.port_opt_view(pr, 2:3);julia> first(timestamp(pv.X))2020-01-02julia> size(values(pv.X))(2, 2)Related