### Install riskParityPortfolio package Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Instructions for installing the package from CRAN, GitHub, or using Docker. ```r install.packages("riskParityPortfolio") # Development version install.packages("devtools") devtools::install_github("dppalomar/riskParityPortfolio") ``` ```bash docker pull mirca/riskparityportfolio ``` -------------------------------- ### Install riskParityPortfolio Development Version from GitHub (Python) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Installs the development version of the Python implementation of the riskParityPortfolio package by cloning the GitHub repository and installing it in editable mode. ```bash $ git clone https://github.com/dppalomar/riskparity.py $ cd riskparity.py $ pip install -e . ``` -------------------------------- ### Install riskParityPortfolio from PyPI (Python) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Installs the stable version of the Python implementation of the riskParityPortfolio package from PyPI using pip. ```bash $ pip install riskparityportfolio ``` -------------------------------- ### Install riskParityPortfolio Development Version from GitHub (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Installs the development version of the riskParityPortfolio package from GitHub. This requires the devtools package and involves cloning the repository and installing it locally. ```r > install.packages("devtools") > devtools::install_github("dppalomar/riskParityPortfolio") ``` -------------------------------- ### Complete Backtesting Example with portfolioBacktest Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Integrates riskParityPortfolio with the portfolioBacktest package for realistic portfolio backtesting. This example downloads stock data, defines risk parity and tangency portfolio functions, runs a backtest, and visualizes the results. ```r library(xts) library(portfolioBacktest) library(riskParityPortfolio) # Download FAANG stock data faang_data <- stockDataDownload( c("GOOG", "NFLX", "AAPL", "AMZN", "FB"), from = "2014-01-01", to = "2019-06-25" ) # Define risk parity portfolio function risk_parity <- function(dataset, w_current) { prices <- dataset$adjusted log_returns <- diff(log(prices))[-1] return(riskParityPortfolio(cov(log_returns))$w) } # Define tangency portfolio (maximum Sharpe ratio) for comparison library(quadprog) max_sharpe_ratio <- function(dataset, w_current) { prices <- dataset$adjusted log_returns <- diff(log(prices))[-1] N <- ncol(prices) Sigma <- cov(log_returns) mu <- colMeans(log_returns) if (all(mu <= 1e-8)) return(rep(0, N)) Dmat <- 2 * Sigma Amat <- cbind(mu, diag(N)) bvec <- c(1, rep(0, N)) dvec <- rep(0, N) res <- solve.QP(Dmat = Dmat, dvec = dvec, Amat = Amat, bvec = bvec, meq = 1) w <- res$solution return(w / sum(w)) } # Run backtest bt <- portfolioBacktest( list("Risk Parity Portfolio" = risk_parity, "Tangency Portfolio" = max_sharpe_ratio), list(faang_data), lookback = 12 * 20, # 12 months lookback optimize_every = 3 * 20, # Reoptimize every 3 months rebalance_every = 3 * 20 # Rebalance every 3 months ) # View performance summary backtestSummary(bt)$performance # Plot cumulative returns backtestChartCumReturn(bt) # Plot drawdowns backtestChartDrawdown(bt) # Plot portfolio allocation over time backtestChartStackedBar(bt, portfolio = "Risk Parity Portfolio", legend = TRUE) ``` -------------------------------- ### Install riskParityPortfolio from CRAN (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Installs the latest stable version of the riskParityPortfolio package from CRAN using the install.packages function in R. ```r > install.packages("riskParityPortfolio") ``` -------------------------------- ### Load and Get Help for riskParityPortfolio (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Loads the riskParityPortfolio library in R and provides commands to access its documentation and help files. ```r > library(riskParityPortfolio) > help(package = "riskParityPortfolio") > package?riskParityPortfolio > ?riskParityPortfolio ``` -------------------------------- ### Plotting Portfolio Risk with barplotPortfolioRisk Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Demonstrates how to plot portfolio risk using the barplotPortfolioRisk function. It shows a basic usage and an example with custom colors, requiring the viridisLite package. ```r barplotPortfolioRisk(rpp_vanilla$w, Sigma) # Custom colors (requires viridisLite package)arplotPortfolioRisk(w_all, Sigma, colors = viridisLite::viridis(3)) ``` -------------------------------- ### Get riskParityPortfolio from Docker Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Pulls the latest Docker image for the riskParityPortfolio package. This allows users to run the package in a containerized environment. ```bash $ docker pull mirca/riskparityportfolio ``` -------------------------------- ### Select Initialization Algorithms Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Demonstrates how to choose between different initialization methods (Spinu, Roncalli, Newton) for computing the initial vanilla risk parity solution. ```r library(riskParityPortfolio) set.seed(42) N <- 100 V <- matrix(rnorm(N^2), ncol = N) Sigma <- cov(V) rpp_spinu <- riskParityPortfolio(Sigma, method_init = "cyclical-spinu") rpp_roncalli <- riskParityPortfolio(Sigma, method_init = "cyclical-roncalli") rpp_newton <- riskParityPortfolio(Sigma, method_init = "newton") max(abs(rpp_spinu$w - rpp_roncalli$w)) ``` -------------------------------- ### Apply General Linear Constraints to Portfolios Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Shows how to define arbitrary equality and inequality constraints using matrices. Validates that the resulting portfolio satisfies the budget constraint and the custom linear constraints. ```r library(riskParityPortfolio) vol <- c(0.05, 0.05, 0.07, 0.1, 0.15, 0.15, 0.15, 0.18) Corr <- rbind( c(100, 80, 60, -20, -10, -20, -20, -20), c( 80, 100, 40, -20, -20, -10, -20, -20), c( 60, 40, 100, 50, 30, 20, 20, 30), c(-20, -20, 50, 100, 60, 60, 50, 60), c(-10, -20, 30, 60, 100, 90, 70, 70), c(-20, -10, 20, 60, 90, 100, 60, 70), c(-20, -20, 20, 50, 70, 60, 100, 70), c(-20, -20, 30, 60, 70, 70, 70, 100) ) / 100 Sigma <- Corr * (vol %o% vol) Dmat <- matrix(0, 2, 8) Dmat[1, ] <- c(0, 0, 0, 0, -1, -1, -1, -1) Dmat[2, ] <- c(1, -1, 0, 0, 1, -1, 0, 0) dvec <- c(-0.30, -0.05) rpp <- riskParityPortfolio(Sigma, Dmat = Dmat, dvec = dvec) sum(rpp$w) sum(rpp$w[5:8]) rpp$is_feasible ``` -------------------------------- ### Configure Risk Concentration Formulations Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Explores different mathematical formulations for risk concentration in nonconvex problems. Compares weight distributions across various objective functions. ```r library(riskParityPortfolio) set.seed(42) N <- 10 V <- matrix(rnorm(N^2), nrow = N) Sigma <- cov(V) rpp1 <- riskParityPortfolio(Sigma, formulation = "rc-double-index", w_ub = 0.15) rpp2 <- riskParityPortfolio(Sigma, formulation = "rc-over-var vs b", w_ub = 0.15) cbind("rc-double-index" = rpp1$w, "rc-over-var vs b" = rpp2$w) ``` -------------------------------- ### Apply Box Constraints to Risk Parity Portfolios Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Demonstrates how to enforce lower and upper bounds on portfolio weights to control asset concentration. Uses the 'rc-over-b-double-index' formulation to ensure weights remain within specified limits. ```r library(riskParityPortfolio) set.seed(42) N <- 9 V <- matrix(rnorm(N^2), nrow = N) Sigma <- cov(V) mu <- runif(N) # Box constraints: 0% <= w_i <= 16% rpp <- riskParityPortfolio( Sigma, formulation = "rc-over-b-double-index", mu = mu, lmd_mu = 1e-3, w_lb = 0, # Lower bound (can be vector) w_ub = 0.16 # Upper bound (can be vector) ) # All weights should be within bounds print(rpp$w) all(rpp$w >= 0 & rpp$w <= 0.16) ``` -------------------------------- ### Design risk parity and risk budgeting portfolios Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Demonstrates the primary function for creating portfolios with equal risk contributions or custom risk budget targets. ```r library(riskParityPortfolio) set.seed(42) N <- 5 Sigma <- cov(matrix(rnorm(N^2), ncol = N)) # Equal risk contribution rpp <- riskParityPortfolio(Sigma) # Custom risk budget b <- c(0.4, 0.4, 0.1, 0.05, 0.05) rpp_budget <- riskParityPortfolio(Sigma, b = b) ``` -------------------------------- ### Performance Benchmarking of riskParityPortfolio Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Compares the execution speed of riskParityPortfolio against other R packages like cccp and RiskPortfolios using the microbenchmark package. The results indicate that riskParityPortfolio is significantly faster. ```r library(microbenchmark) library(riskParityPortfolio) library(cccp) library(RiskPortfolios) N <- 100 V <- matrix(rnorm(N^2), ncol = N) Sigma <- cov(V) b <- rep(1/N, N) # Benchmark different implementations op <- microbenchmark( riskParityPortfolio = riskParityPortfolio(Sigma), cccp = rp(b, Sigma, b, optctrl = ctrl(trace = FALSE)), RiskPortfolios = optimalPortfolio( Sigma = Sigma, control = list(type = "erc", constraint = "lo") ), times = 10L ) print(op) # riskParityPortfolio is typically 100-1000x faster than alternatives ``` -------------------------------- ### Compute Risk and Gradient for rc-over-var in R Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the risk value and its gradient with respect to weights for the rc-over-var formulation. The implementation is optimized for performance using vectorization. ```R risk <- sum((r/sum(r))^2) sum_r <- sum(r) r_sumr <- r/sum_r v <- r_sumr - sum(r_sumr^2) risk_grad <- (2/sum_r) * as.vector(Sigma %*% (w*v) + Sigma_w*v) ``` -------------------------------- ### Calculate Risk and Gradient for Theta-based Formulation Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Implements the risk function and its gradient for a formulation incorporating an auxiliary variable theta. The gradient includes components for both weights and theta. ```R risk <- sum((r - theta)^2) v <- r - theta risk_grad <- 2*c(as.vector(Sigma %*% (w*v) + Sigma_w*v), -sum(v)) ``` -------------------------------- ### Basic Risk Parity Portfolio Calculation (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Demonstrates the basic usage of the riskParityPortfolio function in R. It involves creating a covariance matrix and then calculating the risk parity portfolio weights. ```r library(riskParityPortfolio) set.seed(42) # create covariance matrix N <- 5 V <- matrix(rnorm(N^2), ncol = N) Sigma <- cov(V) # risk parity portfolio res <- riskParityPortfolio(Sigma) names(res) #> [1] "w" "relative_risk_contribution" #> [3] "obj_fun" "is_feasible" res$w #> [1] 0.32715962 0.27110678 0.14480081 0.09766356 0.15926922 # risk budgeting portfolio res <- riskParityPortfolio(Sigma, b = c(0.4, 0.4, 0.1, 0.05, 0.05)) res$relative_risk_contribution #> [1] 0.40 0.40 0.10 0.05 0.05 ``` -------------------------------- ### Return Value Structure of riskParityPortfolio Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Explains the detailed output structure returned by the riskParityPortfolio function. It shows how to call the function with various options and how to access individual components of the result, such as optimal weights, risk contributions, and convergence status. ```r library(riskParityPortfolio) set.seed(42) N <- 5 V <- matrix(rnorm(N^2), ncol = N) Sigma <- cov(V) mu <- runif(N) # Full formulation with all options rpp <- riskParityPortfolio( Sigma, b = rep(1/N, N), mu = mu, lmd_mu = 0.01, lmd_var = 0.1, w_lb = 0.05, w_ub = 0.30, formulation = "rc-over-sd vs b-times-sd", maxiter = 1000, ftol = 1e-8, wtol = 1e-6 ) # Complete result structure: str(rpp) # List of 10 # $ risk_concentration : num - risk parity objective R(w) # $ w : num [1:N] - optimal portfolio weights # $ relative_risk_contribution : num [1:N] - RRC_i for each asset # $ mean_return : num - portfolio expected return w'*mu # $ variance : num - portfolio variance w'*Sigma*w # $ obj_fun : num [1:iter] - objective at each iteration # $ elapsed_time : num [1:iter] - time at each iteration # $ convergence : logi - TRUE if converged # $ is_feasible : logi - TRUE if constraints satisfied # Access individual components cat("Portfolio weights:", rpp$w, "\n") cat("Risk contributions:", rpp$relative_risk_contribution, "\n") cat("Expected return:", rpp$mean_return, "\n") cat("Portfolio variance:", rpp$variance, "\n") cat("Converged:", rpp$convergence, "\n") cat("Feasible:", rpp$is_feasible, "\n") ``` -------------------------------- ### Calculate inverse volatility portfolio Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Uses the analytical closed-form solution for diagonal covariance matrices to compute inverse volatility weights. ```r library(riskParityPortfolio) set.seed(42) Sigma <- cov(matrix(rnorm(100), nrow = 10)) # Naive diagonal formulation rpp_naive <- riskParityPortfolio(Sigma, formulation = "diag") ``` -------------------------------- ### Compute Risk Deviations for SCA in R Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the risk deviations g for the Successive Convex Approximation (SCA) method. ```R g <- r/sum(r) ``` -------------------------------- ### Calculate Portfolio Risk (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the portfolio risk based on the formula R(w) = sum((wi(Sigma*w)i - wj(Sigma*w)j)^2). This implementation is efficient for large N. ```R risk <- 2*N*sum(r^2) - 2*sum(r)^2 ``` -------------------------------- ### Calculate Portfolio Risk Gradient (w) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the gradient of the portfolio risk with respect to the weights w. It utilizes Jacobians and gradients of intermediate terms for efficiency. Dependencies include Sigma, w, r, and N. ```R v <- N*r - sum(r) Ut <- Sigma*w + diag(Sigma_w) risk_grad <- 4 * t(Ut) %*% v ``` ```R v <- N*r - sum(r) risk_grad <- as.vector(4*(Sigma %*% (w*v) + Sigma_w*v)) ``` -------------------------------- ### Compute SCA Risk Deviations and Jacobian Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Implements risk deviations and the Jacobian matrix for Sequential Convex Approximation (SCA). These snippets utilize matrix replication and broadcasting to compute pairwise differences efficiently. ```R rb <- r/b g <- rep(rb, times = N) - rep(rb, each = N) ``` ```R Ut <- diag(Sigma_w) + Sigma*w Utb <- Ut / b A <- matrix(rep(t(Utb), N), ncol = N, byrow = TRUE) - matrix(rep(Utb, each = N), ncol = N) ``` -------------------------------- ### Precompute Risk Contribution Terms in R Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the vector of risk contributions for a given portfolio weight vector and covariance matrix. This snippet assumes the existence of variables 'Sigma' (covariance matrix) and 'w' (weight vector). ```R Sigma_w <- as.vector(Sigma %*% w) r <- w * Sigma_w ``` -------------------------------- ### Compute Risk for rc-over-sd vs b-times-sd in R Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the risk value for the rc-over-sd formulation given risk contributions r, target b, and covariance-based parameters. ```R sqrt_sum_r <- sqrt(sum(r)) risk <- sum((r/sqrt_sum_r - b*sqrt_sum_r)^2) ``` -------------------------------- ### Optimize risk parity with additional objectives Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Incorporates expected return objectives or variance penalties into the risk parity optimization using the SCA method. ```r library(riskParityPortfolio) set.seed(42) Sigma <- cov(matrix(rnorm(100), nrow = 10)) mu <- runif(10) # Risk parity with expected return rpp_mu <- riskParityPortfolio(Sigma, mu = mu, lmd_mu = 1e-3, formulation = "rc-over-sd vs b-times-sd") # Risk parity with variance penalty rpp_var <- riskParityPortfolio(Sigma, lmd_var = 1.0, formulation = "rc-over-sd vs b-times-sd") ``` -------------------------------- ### Calculate Portfolio Risk (rc-over-b-double-index) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes portfolio risk using the rc-over-b-double-index formulation, R(w) = sum((ri/bi - rj/bj)^2). This is an efficient calculation. ```R rb <- r/b risk <- 2*N*sum(rb^2) - 2*sum(rb)^2 ``` -------------------------------- ### Construct SCA Jacobian Matrix Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Constructs the Jacobian matrix A for the SCA method using the covariance matrix Sigma, weights w, and target weights b. ```R Ut <- diag(Sigma_w) + Sigma*w A <- cbind(Ut/b, -1) ``` -------------------------------- ### Calculate Risk Gradient for Portfolio Weights Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the gradient of the risk parity objective function with respect to weights. These implementations use vectorized matrix operations to avoid loops, improving performance for large portfolios. ```R rb <- r/b v <- N*rb - sum(rb) Ut <- Sigma*w + diag(Sigma_w) risk_grad <- 4 * t(Ut) %*% v ``` ```R rb <- r/b v <- N*rb - sum(rb) risk_grad <- as.vector(4*(Sigma %*% (w*v) + Sigma_w*v)) ``` -------------------------------- ### Single-Index Risk Formulation and Gradient Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the risk expression and its gradient for single-index formulations. This approach simplifies the objective function relative to total risk. ```R risk <- sum((r/sum(r) - b)^2) ``` ```R sum_r <- sum(r) r_sumr_b <- r/sum_r - b v <- r_sumr_b - sum(r_sumr_b*r)/sum_r risk_grad <- (2/sum_r) * as.vector(Sigma %*% (w*v) + Sigma_w*v) ``` ```R g <- r/sum(r) - b ``` -------------------------------- ### Compute Jacobian Matrix (A) for SCA Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the Jacobian matrix A for the SCA method, representing the gradients of risk deviations. This implementation avoids loops for efficiency, using matrix operations based on U. ```R Ut <- diag(Sigma_w) + Sigma*w A <- matrix(rep(t(Ut), N), ncol = N, byrow = TRUE) - matrix(rep(Ut, each = N), ncol = N) ``` -------------------------------- ### Compute Jacobian for SCA Theta-based Formulation Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the Jacobian matrix for the SCA approach in the theta-based risk parity formulation by combining the matrix U and a constant vector. ```R Ut <- diag(Sigma_w) + Sigma*w A <- cbind(Ut, -1) ``` -------------------------------- ### Visualize Portfolio Risk Contributions Source: https://context7.com/dppalomar/riskparityportfolio/llms.txt Uses the barplotPortfolioRisk function to compare capital allocation and risk contribution across multiple portfolios, supporting both ggplot2 and base R graphics. ```r library(riskParityPortfolio) set.seed(42) N <- 10 V <- matrix(rnorm(N^2), nrow = N) Sigma <- cov(V) rpp_vanilla <- riskParityPortfolio(Sigma) rpp_naive <- riskParityPortfolio(Sigma, formulation = "diag") w_equal <- rep(1/N, N) w_all <- cbind( "Equal Weighted" = w_equal, "Risk Parity (naive)" = rpp_naive$w, "Risk Parity (vanilla)" = rpp_vanilla$w ) rownames(w_all) <- paste0("Asset", 1:N) barplotPortfolioRisk(w_all, Sigma) barplotPortfolioRisk(w_all, Sigma, type = "simple") ``` -------------------------------- ### Calculate Risk Parity Objective Function Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the risk parity objective function value given risk contributions r, target weights b, and optimization variable theta. ```R risk <- sum((r/b - theta)^2) ``` -------------------------------- ### Cite riskParityPortfolio (R) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/README.md Generates a citation for the riskParityPortfolio package in R, which should be used when referencing the package in publications. ```r > citation("riskParityPortfolio") ``` -------------------------------- ### Calculate Risk Expression (rc vs b-times-var) Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the total risk objective function based on the squared difference between individual risk contributions and target risk allocations. ```R risk <- sum((r - b*sum(r))^2) ``` -------------------------------- ### Compute Jacobian for rc-over-var formulation in R Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the Jacobian matrix A for the risk parity formulation using matrix operations to avoid loops. It utilizes the covariance matrix Sigma, weights w, and the vector of risk contributions r. ```R sum_r <- sum(r) Ut <- diag(Sigma_w) + Sigma*w A <- Ut/sum_r - 2/(sum_r^2) * r %o% Sigma_w ``` -------------------------------- ### Calculate Risk Gradient for Standard Risk Parity Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the gradient of the risk function with respect to weights using vectorized matrix operations in R. This implementation assumes pre-computed covariance matrices and weight-related vectors. ```R sum_r <- sum(r) v <- r - b*sum_r - sum(b*r) + sum(b^2)*sum_r risk_grad <- 2*as.vector(Sigma %*% (w*v) + Sigma_w*v) ``` -------------------------------- ### Calculate SCA Risk Deviations Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the risk deviations g for the Successive Convex Approximation (SCA) method based on risk contributions r, target weights b, and variable theta. ```R g <- r/b - theta ``` -------------------------------- ### Calculate Risk Deviations (g) for SCA Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the risk deviations g_ij = r_i - r_j, which are used in the SCA method. While correct, this method is less efficient than direct risk calculation due to the N^2 size of g. ```R g <- rep(r, times = N) - rep(r, each = N) ``` -------------------------------- ### Compute Risk Parity Gradient Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the gradient of the risk parity objective function with respect to weights w and the optimization variable theta. It utilizes precomputed covariance-related matrices Sigma and Sigma_w. ```R v <- r/b - theta vb <- v/b risk_grad <- 2*c(as.vector(Sigma %*% (w*vb) + Sigma_w*vb), -sum(v)) ``` -------------------------------- ### Compute Jacobian Matrix for Risk Deviations Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the Jacobian matrix (A) of the risk deviation functions. This is used to linearize the risk constraints during the optimization process. ```R Ut <- diag(Sigma_w) + Sigma*w A <- (Ut - (r/sum_r + b) %o% Sigma_w) / sqrt(sum_r) ``` -------------------------------- ### Calculate Risk Deviations for SCA Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Computes the risk deviations (g) for the Sequential Convex Approximation (SCA) method. It measures the difference between component risk contributions and target risk allocations. ```R sum_r <- sum(r) sqrt_sum_r <- sqrt(sum_r) g <- r/sqrt_sum_r - b*sqrt_sum_r ``` -------------------------------- ### Compute Jacobian for SCA Risk Parity Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the Jacobian matrix for Successive Convex Approximation (SCA) by leveraging the matrix U derived from the covariance and weight vectors. ```R Ut <- diag(Sigma_w) + Sigma*w A <- Ut - 2 * b %o% Sigma_w ``` -------------------------------- ### Calculate Risk Gradient for Risk Parity Source: https://github.com/dppalomar/riskparityportfolio/blob/master/R_buildignore/EfficientComputations.html Calculates the gradient of the risk function with respect to weights. It utilizes the covariance matrix and precomputed values to determine the direction of steepest descent for the risk parity objective. ```R sum_r <- sum(r) r_sumr_b <- r/sum_r - b v <- 2*r_sumr_b - sum(r_sumr_b^2) risk_grad <- as.vector(Sigma %*% (w*v) + Sigma_w*v) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.