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
- When
chunk,TorNisnothing, readTandNfromsize(X)and setchunktodiv(T, ws). - Set
offsettoT - chunk * ws, the number of rows at the start ofXthat no block covers. - For each block
cin1:chunk, setrowsto thewsrow indices of that block. - Slice
cetorowswithobs_weights_view. The result is the estimator for this block.obs_weights_viewindexes each weights field on its own. So an estimator with a weighted mean and an unweighted dispersion keeps that shape, and an unweightedcestays unchanged. - Call
Statistics.stdwith that estimator on the rows of the block ofX, withdims = 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. - Stack the
chunkrows withvcatto makerv.
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 matrixobservations × assets.ws: Window size, the number of rows in each block.chunk: Number of blocks, ornothing. Step 1 states the rule when one ofchunk,TandNisnothing.T: Number of observations, ornothing.N: Number of assets, ornothing.
Returns
rv::Matrix{<:Number}: Realised volatility matrixchunk × 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.0707107Related
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
- When
chunk,TorNisnothing, readTandNfromsize(X)and setchunktodiv(T, ws). - Return the view of
Xover the rows(T - (chunk - 1) * ws):ws:T. The view shares its memory withX.
Arguments
X: Implied volatility matrixobservations × assets.ws: Window size, the number of rows in each window.chunk: Number of windows, ornothing. Step 1 states the rule when one ofchunk,TandNisnothing.T: Number of observations, ornothing.N: Number of assets, ornothing.
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.3Related
PortfolioOptimisers.coverage_reduced_ivpa — Function
coverage_reduced_ivpa(ivpa::Option{<:Number}, cmsk) -> Option{<:Number}
coverage_reduced_ivpa(ivpa::VecNum, cmsk::BitVector) -> VecNumReduces 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, ornothing.cmsk: The Coverage Universe as a mask of the assets, ornothing.
Returns
ivpa: The premium on the Coverage Universe.
Related
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, ornothing.
Validation
ivis notnothing. Otherwise the function throws anIsNothingError.
Returns
nothing.
Related
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
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) -> BoolReturns 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.
- An
ImpliedVolatilityreturnstrue. - Each other estimator returns
truewhen one of its fields returnstrue. A covariance estimator subtypesStatsBase.CovarianceEstimatorand notAbstractEstimator, so the method takes both supertypes. - A vector of estimators returns
truewhen one of its entries returnstrue. A vector of numbers returnsfalse. APairreturns the result for its value. - Each other value returns
false.
Arguments
est: An estimator, or one field of an estimator.
Returns
flag::Bool:truewhen the tree reads the implied volatility surface.
Related