| Title: | Rolling Optimizations |
|---|---|
| Description: | Analytical computation of rolling optimization for time-series data. The 'rolloptim' package solves constrained quadratic and linear programs in closed form by applying Lagrangian multipliers and the Karush-Kuhn-Tucker conditions (Kuhn and Tucker, 1951, <doi:10.1525/9780520411586-036>) to perform mean-variance portfolio optimization (Markowitz, 1952, <doi:10.1111/j.1540-6261.1952.tb01525.x>) over rolling windows. For each window, the analytical solution computes the optimal weights that minimize variance, maximize expected return, minimize residual sum of squares, or maximize quadratic utility, subject to a total-weight equality constraint and box bounds on each weight. Use cases include mean-variance portfolio optimization, expected-return maximization, and constrained regression. The package supports rolling optimizations with constraints via the total, lower, and upper arguments. The implementation accepts rolling moments computed via the 'roll' package and uses 'RcppArmadillo' for linear algebra, with parallelism across windows provided by 'RcppParallel'. |
| Authors: | Jason Foster [aut, cre] |
| Maintainer: | Jason Foster <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.0.1 |
| Built: | 2026-11-20 01:53:19 UTC |
| Source: | https://github.com/jasonjfoster/rolloptim |
Analytical computation of rolling optimization for time-series data. The 'rolloptim' package solves constrained quadratic and linear programs in closed form by applying Lagrangian multipliers and the Karush-Kuhn-Tucker conditions (Kuhn and Tucker, 1951, <doi:10.1525/9780520411586-036>) to perform mean-variance portfolio optimization (Markowitz, 1952, <doi:10.1111/j.1540-6261.1952.tb01525.x>) over rolling windows. For each window, the analytical solution computes the optimal weights that minimize variance, maximize expected return, minimize residual sum of squares, or maximize quadratic utility, subject to a total-weight equality constraint and box bounds on each weight. Use cases include mean-variance portfolio optimization, expected-return maximization, and constrained regression. The package supports rolling optimizations with constraints via the total, lower, and upper arguments. The implementation accepts rolling moments computed via the 'roll' package and uses 'RcppArmadillo' for linear algebra, with parallelism across windows provided by 'RcppParallel'.
rolloptim is a package that provides analytical computation of rolling optimization for time-series data.
Jason Foster [aut, cre]
Kuhn, H.W. and Tucker, A.W. (1951). "Nonlinear Programming." In Proceedings of the Second Berkeley Symposium on Mathematical Statistics and Probability, edited by J. Neyman, 481-492. University of California Press. doi:10.1525/9780520411586-036
Markowitz, H.M. (1952). "Portfolio Selection." The Journal of Finance, 7(1), 77-91. doi:10.1111/j.1540-6261.1952.tb01525.x
A function for computing rolling optimizations to maximize mean.
roll_max_mean(mu, sigma = NULL, target = NULL, total = 1, lower = 0, upper = 1)roll_max_mean(mu, sigma = NULL, target = NULL, total = 1, lower = 0, upper = 1)
mu |
matrix. Rows are means and columns are variables. |
sigma |
cube. Slices are covariance matrices. |
target |
vector. Rows are target variances. |
total |
numeric. Sum of the weights. |
lower |
numeric. Lower bound of the weights. |
upper |
numeric. Upper bound of the weights. |
Every combination of active lower and upper bounds is enumerated within each window, the Karush-Kuhn-Tucker system is solved for each combination, and the feasible solution with the largest mean is kept.
The number of combinations grows as 4^p in the number of variables p, so p
is limited to 14. sigma and target are not supported, so a warning is given and the
arguments are ignored. A window with no feasible solution, including one whose moments contain
missing values, returns NA.
An object of the same class and dimension as mu with the rolling
optimizations to maximize mean.
if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) mu <- roll::roll_mean(x, 5) # rolling optimizations to maximize mean roll_max_mean(mu) }if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) mu <- roll::roll_mean(x, 5) # rolling optimizations to maximize mean roll_max_mean(mu) }
A function for computing rolling optimizations to maximize utility.
roll_max_utility(mu, sigma, lambda = 1, total = 1, lower = 0, upper = 1)roll_max_utility(mu, sigma, lambda = 1, total = 1, lower = 0, upper = 1)
mu |
matrix. Rows are means and columns are variables. |
sigma |
cube or vector. Slices are covariance matrices, or rows are variances for one variable. |
lambda |
numeric. Risk aversion parameter. |
total |
numeric. Sum of the weights. |
lower |
numeric. Lower bound of the weights. |
upper |
numeric. Upper bound of the weights. |
Every combination of active lower and upper bounds is enumerated within each window,
the Karush-Kuhn-Tucker system is solved for each combination, and the feasible solution with the
largest quadratic utility, the expected return less one half of lambda times the
variance, is kept.
The number of combinations grows as 4^p in the number of variables p, so p
is limited to 14. A window with no feasible solution, including one whose moments contain
missing values, returns NA.
An object of the same class and dimension as mu with the rolling
optimizations to maximize utility.
if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) mu <- roll::roll_mean(x, 5) sigma <- roll::roll_cov(x, width = 5) # rolling optimizations to maximize utility roll_max_utility(mu, sigma, lambda = 1) }if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) mu <- roll::roll_mean(x, 5) sigma <- roll::roll_cov(x, width = 5) # rolling optimizations to maximize utility roll_max_utility(mu, sigma, lambda = 1) }
A function for computing rolling optimizations to minimize residual sum of squares.
roll_min_rss(xx, xy, total = 1, lower = 0, upper = 1)roll_min_rss(xx, xy, total = 1, lower = 0, upper = 1)
xx |
cube or vector. Slices are crossproducts of |
xy |
cube or vector. Slices are crossproducts of |
total |
numeric. Sum of the weights. |
lower |
numeric. Lower bound of the weights. |
upper |
numeric. Upper bound of the weights. |
Every combination of active lower and upper bounds is enumerated within each window,
the Karush-Kuhn-Tucker system is solved for each combination, and the feasible solution with the
smallest residual sum of squares is kept, so the result is the constrained least squares
coefficients implied by the crossproducts xx and xy.
The number of combinations grows as 4^p in the number of variables p, so p
is limited to 14. A window with no feasible solution, including one whose crossproducts contain
missing values, returns NA.
A matrix with each row the rolling optimizations to minimize residual sum of squares.
if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) y <- rnorm(n_obs) xx <- roll::roll_crossprod(x, x, 5) xy <- roll::roll_crossprod(x, y, 5) # rolling optimizations to minimize residual sum of squares roll_min_rss(xx, xy) }if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) y <- rnorm(n_obs) xx <- roll::roll_crossprod(x, x, 5) xy <- roll::roll_crossprod(x, y, 5) # rolling optimizations to minimize residual sum of squares roll_min_rss(xx, xy) }
A function for computing rolling optimizations to minimize variance.
roll_min_var(sigma, mu = NULL, target = NULL, total = 1, lower = 0, upper = 1)roll_min_var(sigma, mu = NULL, target = NULL, total = 1, lower = 0, upper = 1)
sigma |
cube or vector. Slices are covariance matrices, or rows are variances for one
variable if |
mu |
matrix. Rows are means and columns are variables. |
target |
vector. Rows are target means. |
total |
numeric. Sum of the weights. |
lower |
numeric. Lower bound of the weights. |
upper |
numeric. Upper bound of the weights. |
Every combination of active lower and upper bounds is enumerated within each window,
the Karush-Kuhn-Tucker system is solved for each combination, and the feasible solution with the
smallest variance is kept. If mu and target are supplied then the expected return
must be greater than or equal to target.
The number of combinations grows as 4^p in the number of variables p, so p
is limited to 14. A window with no feasible solution, including one whose moments contain
missing values or whose target is unattainable, returns NA.
An object of the same class and dimension as mu with the rolling
optimizations to minimize variance, or a matrix with each row the rolling optimizations
if mu is NULL.
if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) sigma <- roll::roll_cov(x, width = 5) # rolling optimizations to minimize variance roll_min_var(sigma) }if (requireNamespace("roll", quietly = TRUE)) { n_vars <- 3 n_obs <- 15 x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) sigma <- roll::roll_cov(x, width = 5) # rolling optimizations to minimize variance roll_min_var(sigma) }