Implied Volatility: private API

PortfolioOptimisers.realised_vol — Function
realised_vol(ce::AbstractVarianceEstimator, X::MatNum, ws::Integer,
             chunk::Option{<:Integer} = nothing, T::Option{<:Integer} = nothing,
             N::Option{<:Integer} = nothing)

Returns the realised volatility of each asset over consecutive blocks of ws rows.

The function splits the last chunk * ws rows of X into chunk blocks of ws rows that do not overlap. The estimator ce gives the standard deviation of each asset in each block. The result is a chunk × N matrix.

@propagatable makes an obs_weights_view method for each estimator that it declares, from the @wprop tag of the weights field. So both variance estimators of the library slice their weights with no method written by hand. An estimator can keep its weights in a field with no @wprop tag, and have no method of its own. Such an estimator receives a block of ws rows and a vector of T weights, and the call raises an error.

Mathematical definition

Write $C$ for chunk and $w_s$ for ws. The function counts the blocks back from the last observation, so the first

\[\begin{align} o &= T - C w_s \end{align}\]

rows are in no block. Block $c$ holds the rows

\[\begin{align} \mathcal{R}_c &= \{o + (c - 1) w_s + 1,\, \ldots,\, o + c w_s\}\,, \qquad c = 1, \ldots, C\,, \end{align}\]

and the realised volatility of asset $i$ over that block is

\[\begin{align} \mathrm{rv}_{c,\,i} &= \operatorname{std}\left(\{x_{t,\,i} : t \in \mathcal{R}_c\}\right)\,. \end{align}\]

Where:

  • $T$: Number of observations.
  • $x_{t,\,i}$: Return of asset $i$ at observation $t$.
  • $o$: Number of rows at the start of the sample that no block covers.
  • $\mathcal{R}_c$: Rows of block $c$.
  • $\mathrm{rv}_{c,\,i}$: Realised volatility of asset $i$ over block $c$.

The last row of block $c$ is $o + c w_s$. implied_vol samples the same row for the same window. When $w_s$ does not divide $T$, the function drops the oldest rows and keeps the newest.

Algorithm

  1. When chunk, T or N is nothing, read T and N from size(X) and set chunk to div(T, ws).
  2. Set offset to T - chunk * ws, the number of rows at the start of X that no block covers.
  3. For each block c in 1:chunk, set rows to the ws row indices of that block.
  4. Slice ce to rows with obs_weights_view. The result is the estimator for this block. obs_weights_view indexes each weights field on its own. So an estimator with a weighted mean and an unweighted dispersion keeps that shape, and an unweighted ce stays unchanged.
  5. Call Statistics.std with that estimator on the rows of the block of X, with dims = 1. The result is one row of standard deviations. The function calls the estimator once for each block, so the estimator reads only a matrix.
  6. Stack the chunk rows with vcat to make rv.

Arguments

  • ce: Variance estimator that gives the standard deviations in each block. Its observation weights describe the whole sample, and step 4 slices them to each block.
  • X: Asset returns matrix observations × assets.
  • ws: Window size, the number of rows in each block.
  • chunk: Number of blocks, or nothing. Step 1 states the rule when one of chunk, T and N is nothing.
  • T: Number of observations, or nothing.
  • N: Number of assets, or nothing.

Returns

  • rv::Matrix{<:Number}: Realised volatility matrix chunk × assets.

Examples

julia> PortfolioOptimisers.realised_vol(SimpleVariance(),                                        [0.1 0.2; 0.3 0.1; 0.2 0.4; 0.1 0.1; 0.4 0.2;                                         0.2 0.3], 2)3×2 Matrix{Float64}: 0.141421   0.0707107 0.0707107  0.212132 0.141421   0.0707107

Related

source
PortfolioOptimisers.implied_vol — Function
implied_vol(X::MatNum, ws::Integer, chunk::Option{<:Integer} = nothing,
            T::Option{<:Integer} = nothing, N::Option{<:Integer} = nothing)

Returns the row of X at the end of each window of ws rows.

The function selects the last row of each window, and returns a chunk × N view of X.

Mathematical definition

Write $C$ for chunk and $w_s$ for ws. The sampled rows are

\[\begin{align} \mathcal{S} &= \{T - (C - 1) w_s,\, T - (C - 2) w_s,\, \ldots,\, T\}\,, \end{align}\]

whose $c$-th entry is

\[\begin{align} \mathcal{S}_c &= o + c w_s\,, \qquad o = T - C w_s\,, \qquad c = 1, \ldots, C\,. \end{align}\]

Where:

  • $T$: Number of observations.
  • $\mathcal{S}$: Sampled rows, one per window.
  • $o$: Number of rows at the start of the sample that no window covers.

$\mathcal{S}_c$ is the last row of block $c$ of realised_vol, so the two functions read the same windows. The last sampled row is $T$ for each $w_s$. So when $w_s$ does not divide $T$, the function drops the oldest rows and keeps the newest.

Algorithm

  1. When chunk, T or N is nothing, read T and N from size(X) and set chunk to div(T, ws).
  2. Return the view of X over the rows (T - (chunk - 1) * ws):ws:T. The view shares its memory with X.

Arguments

  • X: Implied volatility matrix observations × assets.
  • ws: Window size, the number of rows in each window.
  • chunk: Number of windows, or nothing. Step 1 states the rule when one of chunk, T and N is nothing.
  • T: Number of observations, or nothing.
  • N: Number of assets, or nothing.

Returns

  • iv::SubArray: The implied volatility at the end of each window, chunk × assets.

Examples

julia> PortfolioOptimisers.implied_vol([0.1 0.2; 0.3 0.1; 0.2 0.4; 0.1 0.1; 0.4 0.2;                                        0.2 0.3], 2)3×2 view(::Matrix{Float64}, 2:2:6, :) with eltype Float64: 0.3  0.1 0.1  0.1 0.2  0.3

Related

source
PortfolioOptimisers.coverage_reduced_ivpa — Function
coverage_reduced_ivpa(ivpa::Option{<:Number}, cmsk) -> Option{<:Number}
coverage_reduced_ivpa(ivpa::VecNum, cmsk::BitVector) -> VecNum

Reduces a per-asset implied volatility premium to the Coverage Universe.

A scalar premium applies to each asset, so the function returns it unchanged. A vector premium has one entry for each asset, so the function slices it as the caller slices X.

Arguments

  • ivpa: The implied volatility premium adjustment factor, as a scalar, a vector, or nothing.
  • cmsk: The Coverage Universe as a mask of the assets, or nothing.

Returns

  • ivpa: The premium on the Coverage Universe.

Related

source
PortfolioOptimisers.assert_implied_volatility — Function
assert_implied_volatility(
    iv::Union{Nothing, AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}}
)

Refuses a fit of an ImpliedVolatility estimator that receives no implied volatilities.

The estimator scales a correlation by the realised volatilities that it predicts from iv, so it cannot compute a result without them. A prior over a ReturnsResult gives rd.iv to each estimator that it holds. rd.iv is nothing when the returns have no implied volatilities. So the iv keyword of each cov and cor method accepts nothing. This function then refuses nothing with an error that names the input and the remedy.

Arguments

  • iv: The implied volatilities, observations × assets, or nothing.

Validation

  • iv is not nothing. Otherwise the function throws an IsNothingError.

Returns

  • nothing.

Related

source
PortfolioOptimisers.show_fields — Method
show_fields(
    _::ImpliedVolatility
) -> Tuple{Symbol, Symbol, Symbol}

Renders every field of an ImpliedVolatility except cache.

A cache holds the state of an incremental fit. That state is not part of the configuration, and it prints under the estimator at each site that renders the estimator. Set set_show_nothing_fields!(:ImpliedVolatility, true) to render it.

Arguments

  • ::ImpliedVolatility: The implied volatility covariance estimator. The method reads only its type.

Returns

  • fields::Tuple: The field names to render, which is (:ce, :alg, :ppy).

Related

source
PortfolioOptimisers.reads_implied_volatility — Function
reads_implied_volatility(est::Union{AbstractEstimator, AbstractCovarianceEstimator}) -> Bool
reads_implied_volatility(est::ImpliedVolatility) -> Bool
reads_implied_volatility(v::AbstractVector) -> Bool
reads_implied_volatility(p::Pair) -> Bool
reads_implied_volatility(x) -> Bool

Returns true when an estimator tree holds an ImpliedVolatility, which reads the implied volatility surface iv.

The reduce-and-expand root of a moment verb, Statistics.cov(ce::AbstractCovarianceEstimator, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, kwargs...), reads the result. A gap in the surface removes an asset from the Coverage Universe only for a fit that reads the surface. A wrapper that holds an ImpliedVolatility and has no panel method of its own reaches the root. So the root must know that the tree reads the surface.

Algorithm

Each step is one method, and Julia selects the method from the type of the argument.

  1. An ImpliedVolatility returns true.
  2. Each other estimator returns true when one of its fields returns true. A covariance estimator subtypes StatsBase.CovarianceEstimator and not AbstractEstimator, so the method takes both supertypes.
  3. A vector of estimators returns true when one of its entries returns true. A vector of numbers returns false. A Pair returns the result for its value.
  4. Each other value returns false.

Arguments

  • est: An estimator, or one field of an estimator.

Returns

  • flag::Bool: true when the tree reads the implied volatility surface.

Related

source