Variance from covariance

Statistics.varMethod
Statistics.var(ce::AbstractCovarianceEstimator, X::MatNum; dims::Int = 1, kwargs...)

Compute the variance vector from the diagonal of the covariance matrix.

This method is the fallback that gives every AbstractCovarianceEstimator a marginal variance, so a caller needs no separate variance estimator to read the diagonal of the matrix the covariance estimator already builds.

Mathematical definition

\[\begin{align} \hat{\sigma}_i^2 &= \hat{\mathbf{\Sigma}}_{ii}\,. \end{align}\]

Where:

  • $\hat{\sigma}_i^2$: Variance of asset $i$.
  • $\hat{\mathbf{\Sigma}}$: Estimated covariance matrix.
  • $\hat{\mathbf{\Sigma}}_{ii}$: $i$-th diagonal entry of $\hat{\mathbf{\Sigma}}$.

Algorithm

  1. Compute the covariance matrix with Statistics.cov(ce, X; dims = dims, kwargs...).
  2. Read its diagonal into val, the variance of each asset.
  3. Reshape val to a 1 × N row vector when dims == 1, and to an N × 1 column vector otherwise.

Arguments

  • ce: Covariance estimator.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • dims: Dimension along which to perform the computation.
  • kwargs...: Additional keyword arguments passed to the covariance estimator.

Returns

  • var::Matrix{<:Number}: Variance vector, shaped as (1, N) if dims == 1 or (N, 1) if dims == 2.

Examples

julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];julia> var(Covariance(), X)1×2 Matrix{Float64}: 0.0001  0.0001

Related

source
Statistics.stdMethod
Statistics.std(ce::AbstractCovarianceEstimator, X::MatNum; dims::Int = 1, kwargs...)

Compute the standard deviation vector from the diagonal of the covariance matrix.

This method is the fallback that gives every AbstractCovarianceEstimator a marginal standard deviation, so a caller needs no separate variance estimator to read the diagonal of the matrix the covariance estimator already builds.

Mathematical definition

\[\begin{align} \hat{\sigma}_i &= \sqrt{\hat{\mathbf{\Sigma}}_{ii}}\,. \end{align}\]

Where:

  • $\hat{\sigma}_i$: Estimated standard deviation of asset $i$.
  • $\hat{\mathbf{\Sigma}}$: Estimated covariance matrix.
  • $\hat{\mathbf{\Sigma}}_{ii}$: $i$-th diagonal entry of $\hat{\mathbf{\Sigma}}$.

Algorithm

  1. Compute the covariance matrix with Statistics.cov(ce, X; dims = dims, kwargs...).
  2. Read its diagonal, take the element-wise square root, and store the result in val.
  3. Reshape val to a 1 × N row vector when dims == 1, and to an N × 1 column vector otherwise.

Arguments

  • ce: Covariance estimator.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • dims: Dimension along which to perform the computation.
  • kwargs...: Additional keyword arguments passed to the covariance estimator.

Returns

  • sd::Matrix{<:Number}: Standard deviation vector, shaped as (1, N) if dims == 1 or (N, 1) if dims == 2.

Examples

julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];julia> std(Covariance(), X)1×2 Matrix{Float64}: 0.01  0.01

Related

source
Statistics.covMethod
Statistics.cov(ve::AbstractVarianceEstimator, X::MatNum; dims::Int = 1, kwargs...)

Always throw a MethodError. AbstractVarianceEstimator resolves one marginal variance per asset and holds no cross-asset structure, so it has no covariance to return.

Without this method, cov would fall through to Statistics.cov(ce::AbstractCovarianceEstimator, X::MatNum; dims::Int = 1, kwargs...), which calls cor. A variance estimator has no cor method of its own either, so that call would fall through in turn to a generic fallback that calls cov again, and the two calls would recurse without end. This method throws before that fallback ever runs.

Use a covariance estimator, for example Covariance, for a covariance matrix.

Examples

julia> try           Statistics.cov(SimpleVariance(), [0.01 0.02; 0.03 0.04])       catch e           e isa MethodError       endtrue

Related

source
Statistics.corMethod
Statistics.cor(ve::AbstractVarianceEstimator, X::MatNum; dims::Int = 1, kwargs...)

Always throw a MethodError. AbstractVarianceEstimator resolves one marginal variance per asset and holds no cross-asset structure, so it has no correlation to return.

Without this method, cor would fall through to a generic fallback that calls Statistics.cov(ve::AbstractVarianceEstimator, X::MatNum; dims::Int = 1, kwargs...), which throws a MethodError on its own. This method throws the same kind of error one call sooner, and names cor in it, so the error names the verb the caller actually used.

Use a covariance estimator, for example Covariance, for a correlation matrix.

Examples

julia> try           Statistics.cor(SimpleVariance(), [0.01 0.02; 0.03 0.04])       catch e           e isa MethodError       endtrue

Related

source
Statistics.stdMethod
Statistics.std(ve::AbstractVarianceEstimator, X::MatNum; dims::Int = 1, kwargs...)

Compute the standard deviation vector as the element-wise square root of the variance vector.

This is the fallback for an AbstractVarianceEstimator that defines Statistics.var alone. It resolves std from var directly, so it never calls Statistics.cov, which an AbstractVarianceEstimator cannot answer. A member that computes its own standard deviation, for example SimpleVariance, overrides this method.

Mathematical definition

\[\begin{align} \hat{\sigma}_i &= \sqrt{\hat{\sigma}_i^2}\,. \end{align}\]

Where:

  • $\hat{\sigma}_i$: Estimated standard deviation of asset $i$.

Algorithm

  1. Compute the variance vector with Statistics.var(ve, X; dims = dims, kwargs...).
  2. Take the element-wise square root.

Arguments

  • ve: Variance estimator.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • dims: Dimension along which to perform the computation.
  • kwargs...: Additional keyword arguments passed to the variance estimator.

Returns

  • res::ArrNum: Variance or standard deviation vector of X, reshaped to be consistent with the dimension along which the value is computed.

Related

source