Mean test draws: private API

Functions

PortfolioOptimisers.draw_statistics — Function
draw_statistics(kernel::BartlettKernel, alg::ObservationDraws, M::Integer, d::VecNum,
                f::AbstractVector{Bool}, n::Integer, nd::Integer, B::Real, buf::VecNum,
                rng::Random.AbstractRNG) -> Vector{<:Real}
draw_statistics(kernel::AbstractLongRunKernel, alg::ObservationDraws, M::Integer,
                d::VecNum, f::AbstractVector{Bool}, n::Integer, nd::Integer, B::Real,
                buf::VecNum, rng::Random.AbstractRNG) -> Vector{<:Real}
draw_statistics(kernel::AbstractLongRunKernel, alg::EigenSumDraws, M::Integer,
                d::VecNum, f::AbstractVector{Bool}, n::Integer, nd::Integer, B::Real,
                buf::VecNum, rng::Random.AbstractRNG) -> Vector{<:Real}

Return the absolute t-statistics of M wild bootstrap draws of a centred series.

Each draw multiplies each finite entry of the centred series by an independent standard normal draw, centres the product over the finite entries again, and computes its t-statistic with the kernel and the bandwidth of the observed one. A missing entry stays zero, and it takes no draw.

Under ObservationDraws both methods take the draws in the same order, one per finite entry and draw by draw, so they answer the same statistics from the same generator. The Bartlett method reads each draw with long_run_quadratic_form. The generic method writes the weights of every pair into the matrix $\mathbf{A} = \mathbf{D} \mathbf{W} \mathbf{D}$ once, and reads a block of draws with one matrix product. For a draw $\boldsymbol{r}$ the centred product is $\boldsymbol{u} = \mathbf{D} \boldsymbol{r} - \bar{u} \boldsymbol{f}$, so

\[\boldsymbol{u}^{\intercal} \mathbf{W} \boldsymbol{u} = \boldsymbol{r}^{\intercal} \mathbf{A} \boldsymbol{r} - 2 \bar{u} \left( \mathbf{D} \mathbf{W} \boldsymbol{f} \right)^{\intercal} \boldsymbol{r} + \bar{u}^{2} \boldsymbol{f}^{\intercal} \mathbf{W} \boldsymbol{f}\,.\]

Where:

  • $\mathbf{D}$: The diagonal matrix of the centred series.
  • $\mathbf{W}$: The matrix of the kernel weights of every pair.
  • $\boldsymbol{f}$: The mask of the finite entries.
  • $\bar{u}$: The mean of the product, $\boldsymbol{d}^{\intercal} \boldsymbol{r} / n$.

Under EigenSumDraws the method writes $\mathbf{W}$ as a product of features by eigen_kernel_features, and draws the Gaussian sums of a draw directly, by sum_draw_statistics.

Arguments

  • kernel: The kernel.
  • alg: The draw algorithm.
  • M: The number of draws.
  • d: The centred series, with a zero at each missing observation. It is not changed.
  • f: Whether each entry of d is a finite observation.
  • n: The number of finite entries.
  • nd: The divisor of the long-run variance, n - ddof.
  • B: The bandwidth, in entries.
  • buf: Work buffer of length length(d) + 1. Only the Bartlett method reads it.
  • rng: The generator.

Returns

  • ats::Vector{<:Real}: The M absolute t-statistics. A statistic that is not finite is Inf.

Related

source
PortfolioOptimisers.quadrature_kernel_features — Function
quadrature_kernel_features(kernel::QuadraticSpectralKernel, alg::QuadratureSumDraws,
                           P::Integer, B::Real) -> Matrix{<:Real}

Return the quadrature features of the quadratic spectral kernel at every entry of a span.

The number of positive nodes is alg.nodes, or quadrature_node_count at the largest angle of the span and the tolerance of alg. The nodes and the weights come from gauss_legendre_positive_nodes. QuadratureSumDraws states the features.

Arguments

  • kernel: The kernel.
  • alg: The draw algorithm, which holds the tolerance and the count.
  • P: The length of the span.
  • B: The bandwidth, in entries.

Returns

  • Phi::Matrix{<:Real}: The P × 2N features, the cosine features of the $N$ nodes and then their sine features, so $\mathbf{\Phi} \mathbf{\Phi}^{\intercal}$ is the matrix of the weights to the tolerance.

Related

source
PortfolioOptimisers.quadrature_node_count — Function
quadrature_node_count(c::Real, tol::Real) -> Int

Return the least number of positive Gauss-Legendre nodes whose bound on the error of the quadratic spectral kernel, at every angle up to c, is at most tol.

quadrature_error_bound gives the bound. It falls as the count grows, so the function counts up from one.

Arguments

  • c: The largest angle, $6 \pi x / 5$ at the largest lag $x$ over the bandwidth.
  • tol: The bound.

Returns

  • N::Int: The number of positive nodes, half the nodes of the rule.

Related

source
PortfolioOptimisers.quadrature_error_bound — Function
quadrature_error_bound(N::Integer, c::Real) -> Real

Return the logarithm of the bound on the error of a Gauss-Legendre rule of 2N nodes for $\frac{3}{4} (1 - u^{2}) \cos(\theta u)$ on $[-1, 1]$, at every angle $\theta \leq c$.

The integrand is entire. On the Bernstein ellipse $E_{\rho}$ its modulus is at most $\frac{3}{4} (1 + \alpha_{\rho}^{2}) \cosh(c \beta_{\rho})$, and Gauss quadrature of $m$ nodes errs by at most $\frac{64}{15} M_{\rho} \rho^{-2m} / (\rho^{2} - 1)$ for a function bounded by $M_{\rho}$ there. The function minimises the bound over $\log \rho$ in steps of 1 / 50 up to 8, and works in logarithms so that no factor overflows. QuadratureSumDraws states the bound.

Arguments

  • N: The number of positive nodes.
  • c: The largest angle.

Returns

  • lb::Real: The logarithm of the bound.

Related

source
PortfolioOptimisers.gauss_legendre_positive_nodes — Function
gauss_legendre_positive_nodes(N::Integer, T::Type) -> Tuple

Return the positive nodes and their weights of the Gauss-Legendre rule of 2N nodes on $[-1, 1]$.

The nodes are the eigenvalues of the symmetric tridiagonal Jacobi matrix of the Legendre polynomials, with the off-diagonal entries $k / \sqrt{4 k^{2} - 1}$. The weight of a node is twice the square of the first entry of its unit eigenvector. The rule is symmetric, so the positive half carries every node.

Arguments

  • N: The number of positive nodes.
  • T: The type the rule is computed in.

Returns

  • u::Vector: The N positive nodes, in ascending order.
  • w::Vector: Their weights.

Related

source
PortfolioOptimisers.eigen_kernel_features — Function
eigen_kernel_features(kernel::AbstractLongRunKernel, P::Integer, B::Real) -> Tuple

Return the features of an exact eigen factor of the matrix of the kernel weights of a span, the columns of its positive eigenvalues first.

The matrix is that of kernel_weight_matrix. EigenSumDraws states the factor and the rule that drops an eigenvalue as round-off.

Arguments

  • kernel: The kernel.
  • P: The length of the span.
  • B: The bandwidth, in entries.

Returns

  • Phi::Matrix{<:Real}: The features, $\mathbf{\Phi} = (\mathbf{\Phi}_{+}, \mathbf{\Phi}_{-})$, with $\mathbf{W} = \mathbf{\Phi}_{+} \mathbf{\Phi}_{+}^{\intercal} - \mathbf{\Phi}_{-} \mathbf{\Phi}_{-}^{\intercal}$ to round-off.
  • kp::Int: The number of columns of $\mathbf{\Phi}_{+}$.

Related

source
PortfolioOptimisers.sum_draw_statistics — Function
sum_draw_statistics(M::Integer, d::VecNum, f::AbstractVector{Bool}, n::Integer,
                    nd::Integer, Phi::MatNum, kp::Integer,
                    rng::Random.AbstractRNG) -> Vector{<:Real}

Return the absolute t-statistics of M wild bootstrap draws, drawn as the Gaussian sums that the statistic of a draw reads.

The rows of $\mathbf{H}$ are $d_{i} (1, \boldsymbol{\phi}_{i} - \bar{\boldsymbol{\phi}})$ over the finite entries, so the sums of a draw $\boldsymbol{r}$ are $\boldsymbol{v} = \mathbf{H}^{\intercal} \boldsymbol{r}$, with the covariance $\mathbf{H}^{\intercal} \mathbf{H} = \mathbf{R}^{\intercal} \mathbf{R}$. The function draws $\boldsymbol{v} = \mathbf{R}^{\intercal} \boldsymbol{z}$ with $\boldsymbol{z}$ standard normal, one draw at a time, and reads the statistic off $v_{1} / n$ and the signed sum of squares of the other sums. QuadratureSumDraws states the rule.

Arguments

  • M: The number of draws.
  • d: The centred series, with a zero at each missing observation.
  • f: Whether each entry of d is a finite observation.
  • n: The number of finite entries.
  • nd: The divisor of the long-run variance, n - ddof.
  • Phi: The features of every entry of the span.
  • kp: The number of leading columns of Phi whose squares add. The squares of the other columns subtract.
  • rng: The generator.

Returns

  • ats::Vector{<:Real}: The M absolute t-statistics. A statistic that is not finite is Inf.

Related

source
PortfolioOptimisers.sum_draw_factor — Function
sum_draw_factor(d::VecNum, f::AbstractVector{Bool}, n::Integer,
                Phi::MatNum) -> Matrix{<:Real}

Return a square-root factor of the covariance of the Gaussian sums of a wild bootstrap draw.

Arguments

  • d: The centred series, with a zero at each missing observation.
  • f: Whether each entry of d is a finite observation.
  • n: The number of finite entries.
  • Phi: The features of every entry of the span.

Returns

  • R::Matrix{<:Real}: The factor $\mathbf{R}$ with $\mathbf{R}^{\intercal} \mathbf{R} = \mathbf{H}^{\intercal} \mathbf{H}$, one row for each eigenvalue of $\mathbf{H}^{\intercal} \mathbf{H}$ above $(K + 1) \epsilon$ times the largest, with $K$ the number of features. The row of an eigenvalue $\lambda$ with the unit eigenvector $\boldsymbol{e}$ is $\sqrt{\lambda} \boldsymbol{e}^{\intercal}$. sum_draw_statistics states $\mathbf{H}$.

Related

source