Skip to content
5

Preprocessing

PortfolioOptimisers.ReturnsResult Type
julia
struct ReturnsResult{T1, T2, T3, T4, T5, T6, T7} <: AbstractReturnsResult
    nx::T1
    X::T2
    nf::T3
    F::T4
    ts::T5
    iv::T6
    ivpa::T7
end

A flexible container type for storing the results of asset and factor returns calculations in PortfolioOptimisers.jl.

ReturnsResult is the standard result type returned by returns-processing routines, such as prices_to_returns. It supports both asset and factor returns, as well as optional time series and implied volatility information, and is designed for downstream compatibility with optimization and analysis routines.

Fields

  • nx: Names or identifiers of asset columns (assets × 1).

  • X: Asset returns matrix (observations × assets).

  • nf: Names or identifiers of factor columns (factors × 1).

  • F: Factor returns matrix (observations × factors).

  • ts: Optional timestamps for each observation (observations × 1).

  • iv: Implied volatilities matrix (observations × assets).

  • ivpa: Implied volatility risk premium adjustment, if a vector (assets × 1).

Constructor

julia
ReturnsResult(; nx::Union{Nothing, <:AbstractVector} = nothing,
              X::Union{Nothing, <:AbstractMatrix} = nothing,
              nf::Union{Nothing, <:AbstractVector} = nothing,
              F::Union{Nothing, <:AbstractMatrix} = nothing,
              ts::Union{Nothing, <:AbstractVector} = nothing,
              iv::Union{Nothing, <:AbstractMatrix} = nothing,
              ivpa::Union{Nothing, <:Real, <:AbstractVector{<:Real}} = nothing)

Keyword arguments correspond to the fields above.

Validation

  • If nx or X is provided, !isempty(nx), !isempty(X), and length(nx) == size(X, 2).

  • If nf or F is provided, !isempty(nf), !isempty(F), and length(nf) == size(F, 2), and size(X, 1) == size(F, 1).

  • If ts is provided, !isempty(ts), and length(ts) == size(X, 1).

  • If iv is provided, !isempty(iv), all(x -> x >= 0, iv), and size(iv) == size(X).

  • If ivpa is provided, all(x -> x >= 0, ivpa), all(x -> isfinite(x), ivpa); if a vector, length(ivpa) == size(iv, 2).

Examples

julia
julia> ReturnsResult(; nx = ["A", "B"], X = [0.1 0.2; 0.3 0.4])
ReturnsResult
    nx ┼ Vector{String}: ["A", "B"]
     X ┼ 2×2 Matrix{Float64}
    nf ┼ nothing
     F ┼ nothing
    ts ┼ nothing
    iv ┼ nothing
  ivpa ┴ nothing

Related

source
PortfolioOptimisers.prices_to_returns Function
julia
prices_to_returns(X::TimeArray; F::TimeArray = TimeArray(TimeType[], []),
                  iv::Union{Nothing, <:TimeArray} = nothing,
                  ivpa::Union{Nothing, <:Real, <:AbstractVector{<:Real}} = nothing,
                  ret_method::Symbol = :simple, padding::Bool = false,
                  missing_col_percent::Real = 1.0,
                  missing_row_percent::Union{Nothing, <:Real} = 1.0,
                  collapse_args::Tuple = (), map_func::Union{Nothing, Function} = nothing,
                  join_method::Symbol = :outer,
                  impute_method::Union{Nothing, <:Impute.Imputor} = nothing)

Convert price data (and optionally factor data) in TimeArray format to returns, with flexible handling of missing data, imputation, and optional implied volatility information.

ReturnsResult a ReturnsResult containing asset and factor returns, time series, and optional implied volatility data, suitable for downstream portfolio optimization.

Arguments

  • X: Asset price data (timestamps × assets).

  • F: Optional Factor price data (timestamps × factors).

  • iv: Optional Implied volatility data.

  • ivpa: Optional Implied volatility risk premium adjustment.

  • ret_method: Return calculation method (:simple or :log).

  • padding: Whether to pad missing values in returns calculation.

  • missing_col_percent: Maximum allowed fraction (0, 1] of missing values per column (asset + factor).

  • missing_row_percent: Maximum allowed fraction (0, 1] of missing values per row (timestamp).

  • collapse_args: Arguments for collapsing the time series (e.g., to lower frequency).

  • map_func: Optional function to apply to the data before returns calculation.

  • join_method: How to join asset and factor data (:outer, :inner, etc.).

  • impute_method: Optional imputation method for missing data.

Returns

  • ReturnsResult: Struct containing asset/factor returns, names, time series, and optional implied volatility data.

Examples

julia
julia> using TimeSeries

julia> X = TimeArray(Date(2020, 1, 1):Day(1):Date(2020, 1, 3), [100 101; 102 103; 104 105],
                     ["A", "B"])
3×2 TimeSeries.TimeArray{Int64, 2, Dates.Date, Matrix{Int64}} 2020-01-01 to 2020-01-03
┌────────────┬─────┬─────┐
│            │ A   │ B   │
├────────────┼─────┼─────┤
2020-01-01100101
2020-01-02102103
2020-01-03104105
└────────────┴─────┴─────┘

julia> prices_to_returns(X)
ReturnsResult
    nx ┼ Vector{String}: ["A", "B"]
     X ┼ 2×2 Matrix{Float64}
    nf ┼ nothing
     F ┼ nothing
    ts ┼ Vector{Dates.Date}: [Dates.Date("2020-01-02"), Dates.Date("2020-01-03")]
    iv ┼ nothing
  ivpa ┴ nothing

Related

source