Composite price-level statistics: private API
PortfolioOptimisers.elastic_net_path — Function
elastic_net_path(
path::ElasticNetPath,
P::AbstractMatrix,
y::AbstractVector
) -> Any
Returns the coefficients of the elastic net of y on the columns of P at the middle point of the path, by cyclic coordinate descent from zero.
ElasticNetPath states the objective and the middle point. The function solves the middle point alone. The problem has as many columns as the window has levels, so it does not need the rest of the path as warm starts.
Mathematical definition
The minimiser of the objective over one coefficient, with the others fixed, is
\[\begin{align} z_k &= \frac{S\left(\mathbf{P}_k^\intercal \boldsymbol{r}_k,\ \gamma \vartheta\right)}{\lVert \mathbf{P}_k \rVert^2 + \gamma (1 - \vartheta)}\,,\quad S(v, a) = \operatorname{sign}(v) \max(\lvert v \rvert - a, 0)\,. \end{align}\]
Where:
- $\boldsymbol{z}$: Coefficients of the elastic net, one per column of $\mathbf{P}$. Coefficient $k$ is $z_k$.
- $\mathbf{P}$: Regressors of the elastic net, one row per observation and one column per regressor. Column $k$ is $\mathbf{P}_k$.
- $\boldsymbol{r}_k$: Residual of the response $\boldsymbol{y}$ without the term of column $k$.
- $S$: Soft threshold.
- $\gamma$: Regularisation strength of the elastic net. Here it is the strength at the middle point of the path.
- $\vartheta$: Share of the $L_1$ penalty in the elastic net, the
thetafield ofElasticNetPath.
Algorithm
- Compute
g, the product of each column withy, andlmax, the smallest strength at which every coefficient is zero. Whenlmaxis zero, return zero coefficients. - Take
lam, the strength at the middle point, the thresholdaof the soft threshold, andden, the denominator of each coordinate update. - Start from the coefficients
zat zero and the residualrequal toy. - Sweep the columns in order. For each column
k, computerho, the product of the column with the residual without its own term, and apply the coordinate update, which givesznew. Updater, and recorddelta, the largest change of a coefficient in the sweep. - Try the exact solve of
elastic_net_polishon the sign pattern ofz. When it gives the optimum, return it. - Repeat steps 4 and 5 until
deltais belowtoloriterssweeps have run, and then returnz.
Arguments
path: The path.P: The regressors,observations × columns.y: The response,observations × 1.
Returns
z::Vector: The coefficients,columns × 1.
Related
PortfolioOptimisers.elastic_net_polish — Function
elastic_net_polish(
P::AbstractMatrix,
y::AbstractVector,
z::AbstractVector,
lam::Real,
theta::Real
) -> Any
Returns the exact elastic-net optimum on the sign pattern of z when that pattern is the pattern of the optimum, and nothing otherwise.
A point that meets the optimality conditions below minimises the objective of ElasticNetPath, because the objective is convex. For theta < 1 the objective is strictly convex, and the point is its only minimiser. At theta = 1 the objective is the lasso, and it can have more than one minimiser. Then the equations of the active columns are singular when the pattern holds more columns than there are observations, and the function returns nothing for such a pattern.
Mathematical definition
With $\mathcal{A}$ the nonzero coefficients of $\boldsymbol{z}$, the optimum on its sign pattern $\boldsymbol{s}$ solves
\[\begin{align} \left(\mathbf{P}_{\mathcal{A}}^\intercal \mathbf{P}_{\mathcal{A}} + \gamma (1 - \vartheta) \mathbf{I}\right) \boldsymbol{z}_{\mathcal{A}} &= \mathbf{P}_{\mathcal{A}}^\intercal \boldsymbol{y} - \gamma \vartheta\, \boldsymbol{s}\,, \end{align}\]
and it is the optimum of the objective when
\[\begin{align} \operatorname{sign}(\boldsymbol{z}_{\mathcal{A}}) &= \boldsymbol{s}\,,\\ \lvert \mathbf{P}_k^\intercal (\boldsymbol{y} - \mathbf{P}_{\mathcal{A}} \boldsymbol{z}_{\mathcal{A}}) \rvert &\leq \gamma \vartheta\,,\quad k \notin \mathcal{A}\,. \end{align}\]
Where:
- $\mathcal{A}$: Active set, the columns whose coefficient in $\boldsymbol{z}$ is nonzero.
- $\mathbf{P}_{\mathcal{A}}$, $\boldsymbol{z}_{\mathcal{A}}$: Columns of the regressors and coefficients of the active set.
- $\boldsymbol{s}$: Signs of the coefficients of the active set in $\boldsymbol{z}$.
- $\boldsymbol{z}$: Coefficients of the elastic net, one per column of $\mathbf{P}$. Coefficient $k$ is $z_k$.
- $\mathbf{P}$: Regressors of the elastic net, one row per observation and one column per regressor. Column $k$ is $\mathbf{P}_k$.
- $\boldsymbol{y}$: Response of the elastic net, one entry per observation.
- $\gamma$: Regularisation strength of the elastic net.
- $\vartheta$: Share of the $L_1$ penalty in the elastic net, the
thetafield ofElasticNetPath.
Algorithm
- Find
A, the nonzero coefficients ofz. WhenAis empty, returnnothing. - Take
s, the signs of the coefficients ofA, and factorise the matrix of the equations of the active columns, which givesF. When the matrix is singular, returnnothing. - Solve the equations with
F, which giveszA. When a coefficient ofzAdoes not keep its sign ins, returnnothing. - Write
zAintozp, zero elsewhere, and computecorr, the product of each column with the residual. - When an inactive column has
abs(corr[k]) > lam * theta, returnnothing. Otherwise returnzp.
Arguments
P: The regressors,observations × columns.y: The response.z: The coefficients whose sign pattern the function tries.lam: The regularisation strength.theta: The share of the $L_1$ penalty.
Returns
z'::Union{Nothing, Vector}: The optimum, ornothing.
Related
PortfolioOptimisers.trend_reverting_fraction — Function
trend_reverting_fraction(P::AbstractMatrix) -> Any
Returns the trend-reverting fraction of a matrix of levels, the share of the interior levels, over every asset, at which the path turns.
A level turns when its two neighbours are both above it or both below it, so the path rises and then falls there, or falls and then rises. A matrix of $L$ levels and $N$ assets has $(L - 2) N$ interior levels. The fraction is zero below three levels. KernelTrendPattern states it as $\lambda_{t+1}$, after eq. (27) of the kernel trend pattern paper of Lai, Yang, Wu and Fang (2018).
Arguments
P: The levels,levels × assets.
Returns
lambda::Real: The fraction, in[0, 1], in the element type of the levels or a wider one.
Related