[Returnanalytics-commits] r3292 - pkg/PortfolioAnalytics/vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 19 20:47:51 CET 2013


Author: rossbennett34
Date: 2013-12-19 20:47:50 +0100 (Thu, 19 Dec 2013)
New Revision: 3292

Modified:
   pkg/PortfolioAnalytics/vignettes/ROI_vignette.Rnw
   pkg/PortfolioAnalytics/vignettes/ROI_vignette.pdf
   pkg/PortfolioAnalytics/vignettes/portfolio_vignette.Rnw
   pkg/PortfolioAnalytics/vignettes/portfolio_vignette.pdf
   pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.Rnw
   pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.pdf
Log:
Edits to fix vignettes

Modified: pkg/PortfolioAnalytics/vignettes/ROI_vignette.Rnw
===================================================================
--- pkg/PortfolioAnalytics/vignettes/ROI_vignette.Rnw	2013-12-19 19:47:08 UTC (rev 3291)
+++ pkg/PortfolioAnalytics/vignettes/ROI_vignette.Rnw	2013-12-19 19:47:50 UTC (rev 3292)
@@ -5,6 +5,8 @@
 \usepackage{Rd}
 \usepackage{amsmath}
 
+\VignetteIndexEntry{Portfolio Optimization with ROI in PortfolioAnalytics}
+
 \begin{document}
 
 \title{Using the ROI solvers with PortfolioAnalytics}
@@ -34,6 +36,7 @@
 
 # Use the first 4 columns in edhec for a returns object
 returns <- edhec[, 1:4]
+colnames(returns) <- c("CA", "CTAG", "DS", "EM")
 print(head(returns, 5))
 
 # Get a character vector of the fund names
@@ -55,7 +58,7 @@
 \subsection{Portfolio Object}
 
 The first step is to create the portfolio object. Then add constraints and a return objective.
-<<>>=
+<<tidy=FALSE>>=
 # Create portfolio object
 portf_maxret <- portfolio.spec(assets=funds)
 
@@ -77,9 +80,10 @@
 
 \subsection{Optimization}
 The next step is to run the optimization. Note that \code{optimize\_method="ROI"} is specified in the call to \code{optimize.portfolio} to select the solver used for the optimization.
-<<>>=
+<<tidy=FALSE>>=
 # Run the optimization
-opt_maxret <- optimize.portfolio(R=returns, portfolio=portf_maxret, optimize_method="ROI", trace=TRUE)
+opt_maxret <- optimize.portfolio(R=returns, portfolio=portf_maxret, 
+                                 optimize_method="ROI", trace=TRUE)
 @
 
 The print method for the \code{opt\_maxret} object shows the call, optimal weights, and the objective measure
@@ -109,28 +113,22 @@
 @
 
 \subsection{Visualization}
-The chart of the optimal weights as well as the box constraints can be created with \code{chart.Weights}. The blue dots are the optimal weights and the gray triangles are the \code{min} and \code{max} of the box constraints.
-<<fig.align='center',fig.height=5, fig.width=5>>=
-chart.Weights(opt_maxret)
+The \code{plot} method charts of the optimal weights with the box constraints along with the optimal portfolio in risk-return space. The blue dots are the optimal weights and the gray triangles are the \code{min} and \code{max} of the box constraints.
+<<>>=
+plot(opt_maxret, chart.assets=TRUE, xlim=c(0.02, 0.18))
 @
 
 The optimal portfolio can be plotted in risk-return space along with other feasible portfolios. The return metric is defined in the \code{return.col} argument and the risk metric is defined in the \code{risk.col} argument. The scatter chart includes the optimal portfolio (blue dot) and other feasible portfolios (gray circles) to show the overall feasible space given the constraints. By default, if \code{rp} is not passed in, the feasible portfolios are generated with \code{random\_portfolios} to satisfy the constraints of the portfolio object.
 
 Volatility as the risk metric
-<<fig.align='center',fig.height=5, fig.width=5>>=
+<<tidy=FALSE, fig.align='center',fig.height=5, fig.width=5>>=
 chart.RiskReward(opt_maxret,return.col="mean", risk.col="sd", 
                  chart.assets=TRUE, xlim=c(0.01, 0.05), main="Maximum Return")
 @
 
-Expected tail loss as the risk metric
-<<fig.align='center',fig.height=5, fig.width=5>>=
-chart.RiskReward(opt_maxret, return.col="mean", risk.col="ES", 
-                 chart.assets=TRUE, xlim=c(0.02, 0.18), main="Maximum Return")
-@
-
 \subsection{Backtesting}
 An out of sample backtest is run with \code{optimize.portfolio.rebalancing}. In this example, an initial training period of 36 months is used and the portfolio is rebalanced quarterly. 
-<<>>=
+<<tidy=FALSE>>=
 bt_maxret <- optimize.portfolio.rebalancing(R=returns, portfolio=portf_maxret,
                                             optimize_method="ROI", 
                                             rebalance_on="quarters", 
@@ -166,7 +164,7 @@
 The only constraint specified is the full investment constraint, therefore the optimization problem is solving for the global minimum variance portfolio.
 
 \subsubsection{Optimization}
-<<>>=
+<<tidy=FALSE>>=
 # Run the optimization
 opt_gmv <- optimize.portfolio(R=returns, portfolio=portf_minvar, 
                               optimize_method="ROI", trace=TRUE)
@@ -174,7 +172,7 @@
 @
 
 \subsubsection{Backtesting}
-<<>>=
+<<tidy=FALSE>>=
 bt_gmv <- optimize.portfolio.rebalancing(R=returns, portfolio=portf_minvar,
                                          optimize_method="ROI", 
                                          rebalance_on="quarters", 
@@ -186,7 +184,7 @@
 
 \subsubsection{Portfolio Object}
 Constraints can be added to the \code{portf\_minvar} portfolio object previously created.
-<<>>=
+<<tidy=FALSE>>=
 # Add long only constraints
 portf_minvar <- add.constraint(portfolio=portf_minvar, type="box", 
                                min=0, max=1)
@@ -202,7 +200,7 @@
 @
 
 \subsubsection{Optimization}
-<<>>=
+<<tidy=FALSE>>=
 # Run the optimization
 opt_minvar <- optimize.portfolio(R=returns, portfolio=portf_minvar, 
                                  optimize_method="ROI", trace=TRUE)
@@ -210,7 +208,7 @@
 @
 
 \subsubsection{Backtesting}
-<<>>=
+<<tidy=FALSE>>=
 bt_minvar <- optimize.portfolio.rebalancing(R=returns, portfolio=portf_minvar,
                                             optimize_method="ROI", 
                                             rebalance_on="quarters", 
@@ -256,7 +254,7 @@
 
 \subsection{Optimization}
 Note how the constraints and objectives are passed to optimize.portfolio.
-<<>>=
+<<tidy=FALSE>>=
 # Run the optimization
 opt_qu <- optimize.portfolio(R=returns, portfolio=init_portf, 
                              constraints=qu_constr, 
@@ -266,7 +264,7 @@
 @
 
 \subsection{Backtesting}
-<<>>=
+<<tidy=FALSE>>=
 bt_qu <- optimize.portfolio.rebalancing(R=returns, portfolio=init_portf,
                                         constraints=qu_constr, 
                                         objectives=qu_obj, 

Modified: pkg/PortfolioAnalytics/vignettes/ROI_vignette.pdf
===================================================================
(Binary files differ)

Modified: pkg/PortfolioAnalytics/vignettes/portfolio_vignette.Rnw
===================================================================
--- pkg/PortfolioAnalytics/vignettes/portfolio_vignette.Rnw	2013-12-19 19:47:08 UTC (rev 3291)
+++ pkg/PortfolioAnalytics/vignettes/portfolio_vignette.Rnw	2013-12-19 19:47:50 UTC (rev 3292)
@@ -35,10 +35,10 @@
 % Or whatever. Note that the encoding and the font should match. If T1
 % does not look nice, try deleting the line with the fontenc.
 
+\VignetteIndexEntry{An Introduction to Portfolio Optimization with PortfolioAnalytics}
+
 \begin{document}
-\SweaveOpts{concordance=TRUE}
 
-
 \title{Introduction to PortfolioAnalytics}
 \author{Ross Bennett}
 
@@ -90,7 +90,7 @@
 \subsection{Leverage Constraint}
 
 The \code{leverage} constraint specifies the constraint on the sum of the weights. Aliases for the \code{leverage} constraint type include \code{weight\_sum}, \code{weight}, and \code{leverage}. Here we add a constraint that the weights must sum to 1, or the full investment constraint.
-<<>>=
+<<tidy=FALSE>>=
 # Add the full investment constraint that specifies the weights must sum to 1.
 pspec <- add.constraint(portfolio=pspec, 
                         type="weight_sum", 
@@ -104,7 +104,7 @@
 \item The sum of the weights equal 0, i.e. the dollar neutral or active constraint. This constraint can be specified with \code{type="dollar\_neutral"} or \code{type="active"}.
 \end{enumerate}
 
-<<>>=
+<<tidy=FALSE>>=
 # The full investment constraint can also be specified with type="full_investment"
 # pspec <- add.constraint(portfolio=pspec, type="full_investment")
 
@@ -119,7 +119,7 @@
 
 \subsection{Box Constraint}
 Box constraints allows the user to specify upper and lower bounds on the weights of the assets. Here we add box constraints for the asset weights so that the minimum weight of any asset must be greater than or equal to 0.05 and the maximum weight of any asset must be less than or equal to 0.4. The values for min and max can be passed in as scalars or vectors. If min and max are scalars, the values for min and max will be replicated as vectors to the length of assets. If min and max are not specified, a minimum weight of 0 and maximum weight of 1 are assumed. Note that min and max can be specified as vectors with different weights for linear inequality constraints.
-<<>>=
+<<tidy=FALSE>>=
 # Add box constraints
 pspec <- add.constraint(portfolio=pspec,
                         type="box",
@@ -141,7 +141,7 @@
 
 \subsection{Group Constraint}
 Group constraints allow the user to specify the the sum of weights by group. Group constraints are currently supported by the ROI, DEoptim, and random portfolio solvers. The following code groups the assets such that the first 3 assets are grouped together labeled GroupA and the fourth asset is in its own group labeled GroupB. The \code{group\_min} argument specifies that the sum of the weights in GroupA must be greater than or equal to 0.1 and the sum of the weights in GroupB must be greater than or equal to 0.15. The \code{group\_max} argument specifies that the sum of the weights in GroupA must be less than or equal to 0.85 and the sum of the weights in GroupB must be less than or equal to 0.55.The \code{group\_labels} argument is optional and is useful if groups is not a named list for labeling groups in terms of market capitalization, sector, etc.
-<<>>=
+<<tidy=FALSE>>=
 # Add group constraints
 pspec <- add.constraint(portfolio=pspec, type="group",
                         groups=list(groupA=c(1, 2, 3),
@@ -153,7 +153,7 @@
 \subsection{Position Limit Constraint}
 The position limit constraint allows the user to specify limits on the number of assets with non-zero, long, or short positions. The ROI solver interfaces to the Rglpk package (i.e. using the glpk plugin) for solving maximizing return and ETL/ES/cVaR objectives. The Rglpk package supports integer programming and thus supports position limit constraints for the \code{max\_pos} argument. The quadprog package does not support integer programming, and therefore \code{max\_pos} is not supported for the ROI solver using the quadprog plugin. Note that \code{max\_pos\_long} and \code{max\_pos\_short} are not supported for either ROI solver. All position limit constraints are fully supported for DEoptim and random solvers.
 
-<<>>=
+<<tidy=FALSE>>=
 # Add position limit constraint such that we have a maximum number of three assets with non-zero weights.
 pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=3)
 
@@ -181,7 +181,7 @@
 
 \subsection{Factor Exposure Constraint}
 The factor exposure constraint allows the user to set upper and lower bounds on exposures to risk factors. The exposures can be passed in as a vector or matrix. Here we specify a vector for \code{B} with arbitrary values, e.g. betas of the assets,  with a market risk exposure range of 0.6 to 0.9.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.constraint(portfolio=pspec, type="factor_exposure",
                         B=c(-0.08, 0.37, 0.79, 1.43),
                         lower=0.6, upper=0.9)
@@ -208,7 +208,7 @@
 \subsection{Specifying Constraints as Separate Objects}
 The following examples will demonstrate how to specify constraints as separate objects for all constraints types.
 
-<<>>=
+<<tidy=FALSE>>=
 # full investment constraint
 weight_constr <- weight_sum_constraint(min_sum=1, max_sum=1)
 
@@ -250,7 +250,7 @@
 \subsection{Portfolio Risk Objective}
 The portfolio risk objective allows the user to specify a risk function to minimize
 Here we add a risk objective to minimize portfolio expected tail loss with a confidence level of 0.95. Other default arguments to the function can be passed in as a named list to arguments.  Note that the name of the function must correspond to a function in R. Many functions are available in the \verb"PerformanceAnalytics" package or a user defined function.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.objective(portfolio=pspec,
                        type='risk',
                        name='ETL',
@@ -259,7 +259,7 @@
 
 \subsection{Portfolio Return Objective}
 The return objective allows the user to specify a return function to maximize. Here we add a return objective to maximize the portfolio mean return.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.objective(portfolio=pspec,
                        type='return',
                        name='mean')
@@ -267,7 +267,7 @@
 
 \subsection{Portfolio Risk Budget Objective}
 The portfolio risk objective allows the user to specify constraints to minimize component contribution (i.e. equal risk contribution) or specify upper and lower bounds on percentage risk contribution. Here we specify that no asset can contribute more than 30\% to total portfolio risk. See the risk budget optimization vignette for more detailed examples of portfolio optimizations with risk budgets.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.objective(portfolio=pspec, type="risk_budget", name="ETL", 
                        arguments=list(p=0.95), max_prisk=0.3)
 
@@ -289,13 +289,13 @@
 Where $\mu$ is the estimated mean asset returns, $\lambda$ is the risk aversion parameter, $lambda_{hhi}$ is the concentration aversion parameter, $HHI$ is the portfolio $HHI$, $\boldsymbol{\Sigma}$ is the estimated covariance matrix of asset returns and $\boldsymbol{w}$ is the set of weights.
 
 Here we add a weight concentration objective for the overall portfolio HHI.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.objective(portfolio=pspec, type="weight_concentration", 
                        name="HHI", conc_aversion=0.1)
 @
 
 The weight concentration aversion parameter by groups can also be specified. Here we add a weight concentration objective specifying groups and concentration aversion parameters by group.
-<<>>=
+<<tidy=FALSE>>=
 pspec <- add.objective(portfolio=pspec, type="weight_concentration", 
                        name="HHI",
                        conc_aversion=c(0.03, 0.06),
@@ -328,7 +328,7 @@
 
 The following plots illustrate the various methods to generate random portfolios.
 
-<<fig.cap="Random portfolio methods", fig.width=5, fig.height=5>>=
+<<fig.cap="Random portfolio methods", fig.width=5, fig.height=5, tidy=FALSE>>=
 R <- edhec[, 1:4]
 
 # set up simple portfolio with leverage and box constraints 
@@ -370,8 +370,7 @@
 fev <- 0:5
 par(mfrow=c(2, 3))
 for(i in 1:length(fev)){
-  rp <- random_portfolios(portfolio=pspec, permutations=2000, 
-                          rp_method='simplex', fev=fev[i])
+  rp <- rp_simplex(portfolio=pspec, permutations=2000, fev=fev[i])
   tmp.mean <- apply(rp, 1, function(x) mean(R %*% x))
   tmp.StdDev <- apply(rp, 1, function(x) StdDev(R=R, weights=x))
   plot(x=tmp.StdDev, y=tmp.mean, main=paste("FEV =", fev[i]),
@@ -387,7 +386,7 @@
 par(mfrow=c(1, 2))
 # simplex
 rp_simplex <- random_portfolios(portfolio=pspec, permutations=2000, 
-                                rp_method='simplex', fev=0:5)
+                                rp_method='simplex')
 tmp.mean <- apply(rp_simplex, 1, function(x) mean(R %*% x))
 tmp.StdDev <- apply(rp_simplex, 1, function(x) StdDev(R=R, weights=x))
 plot(x=tmp.StdDev, y=tmp.mean, main="rp_method=simplex fev=0:5",
@@ -450,15 +449,16 @@
 @
 
 Run the optimization.
-<<>>=
+<<tidy=FALSE>>=
 opt_maxret <- optimize.portfolio(R=R, portfolio=maxret, 
-                                 optimize_method="ROI", trace=TRUE)
+                                 optimize_method="ROI", 
+                                 trace=TRUE)
 
 print(opt_maxret)
 @
 
 Chart the weights and optimal portfolio in risk-return space. The weights and a risk-reward scatter plot can be plotted separately as shown below with the \code{chart.Weights} and \code{chart.RiskReward} functions. The \code{plot} function will plot the weights and risk-reward scatter together.
-<<fig.cap="Maximum Return Optimization", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="Maximum Return Optimization", fig.align='center', fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_maxret, risk.col="StdDev", return.col="mean", 
      main="Maximum Return Optimization", chart.assets=TRUE,
      xlim=c(0, 0.05), ylim=c(0,0.0085))
@@ -471,14 +471,14 @@
 @
 
 Run the optimization. Note that although 'var' is the risk metric, 'StdDev' is returned as an objective measure.
-<<>>=
+<<tidy=FALSE>>=
 opt_minvar <- optimize.portfolio(R=R, portfolio=minvar, 
                                  optimize_method="ROI", trace=TRUE)
 print(opt_minvar)
 @
 
 Chart the weights and optimal portfolio in risk-return space.
-<<fig.cap="Minimum Variance Optimization", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="Minimum Variance Optimization", fig.align='center', fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_minvar, risk.col="StdDev", return.col="mean", 
      main="Minimum Variance Optimization", chart.assets=TRUE,
      xlim=c(0, 0.05), ylim=c(0,0.0085))
@@ -492,13 +492,14 @@
 @
 
 Run the optimization.
-<<>>=
+<<tidy=FALSE>>=
 opt_qu <- optimize.portfolio(R=R, portfolio=qu, 
-                             optimize_method="ROI", trace=TRUE)
+                             optimize_method="ROI", 
+                             trace=TRUE)
 print(opt_qu)
 @
 
-<<fig.cap="Maximum Quadratic Utility Optimization", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="Maximum Quadratic Utility Optimization", fig.align='center', fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_qu, risk.col="StdDev", return.col="mean", 
      main="Quadratic Utility Optimization", chart.assets=TRUE,
      xlim=c(0, 0.05), ylim=c(0, 0.0085))
@@ -511,13 +512,14 @@
 @
 
 Run the optimization.
-<<>>=
+<<tidy=FALSE>>=
 opt_etl <- optimize.portfolio(R=R, portfolio=etl, 
-                              optimize_method="ROI", trace=TRUE)
+                              optimize_method="ROI", 
+                              trace=TRUE)
 print(opt_etl)
 @
 
-<<fig.cap="Minimum ETL Optimization", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="Minimum ETL Optimization", fig.align='center', fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_etl, risk.col="ES", return.col="mean", 
      main="ETL Optimization", chart.assets=TRUE,
      xlim=c(0, 0.14), ylim=c(0,0.0085))
@@ -525,14 +527,14 @@
 
 \subsection{Maximize mean return per unit ETL with random portfolios}
 Add mean and ETL objectives.
-<<>>=
+<<tidy=FALSE>>=
 meanETL <- add.objective(portfolio=init, type="return", name="mean")
 meanETL <- add.objective(portfolio=meanETL, type="risk", name="ETL",
                          arguments=list(p=0.95))
 @
 
 Run the optimization. The default random portfolio method is 'sample'.
-<<>>=
+<<tidy=FALSE>>=
 opt_meanETL <- optimize.portfolio(R=R, portfolio=meanETL, 
                                   optimize_method="random",
                                   trace=TRUE, search_size=2000)
@@ -547,13 +549,13 @@
 @
 
 Chart the optimal weights and optimal portfolio in risk-return space. Because the optimization was run with \code{trace=TRUE}, the chart of the optimal portfolio also includes the trace portfolios of the optimization. This is usefule to visualize the feasible space of the portfolios. The 'neighbor' portfolios relative to the optimal portfolio weights can be included the chart of the optimal weights.
-<<fig.cap="mean-ETL Optimization", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="mean-ETL Optimization", fig.align='center', fig.width=5, fig.height=8,tidy=FALSE>>=
 plot(opt_meanETL, risk.col="ETL", return.col="mean", 
      main="mean-ETL Optimization", neighbors=25)
 @
 
 Calculate and plot the portfolio component ETL contribution.
-<<fig.cap="mean-ETL risk contribution", fig.height=3.5, fig.width=3.5, fig.align='center'>>=
+<<fig.cap="mean-ETL risk contribution", fig.height=3.5, fig.width=3.5, fig.align='center', tidy=FALSE>>=
 pct_contrib <- ES(R=R, p=0.95, portfolio_method="component", 
                   weights=extractWeights(opt_meanETL))
 barplot(pct_contrib$pct_contrib_MES, cex.names=0.8, las=3, col="lightblue")
@@ -563,7 +565,7 @@
 
 \subsection{Maximize mean return per unit ETL with ETL risk budgets}
 Add objectives to maximize mean return per unit ETL with 40\% limit ETL risk budgets.
-<<>>=
+<<tidy=FALSE>>=
 # change the box constraints to long only
 init$constraints[[2]]$min <- rep(0, 6)
 init$constraints[[2]]$max <- rep(1, 6)
@@ -576,15 +578,15 @@
 @
 
 Run the optimization. Set \code{traceDE=5} so that every fifth iteration is printed. The default is to print every iteration.
-<<>>=
+<<tidy=FALSE>>=
 opt_rb_meanETL <- optimize.portfolio(R=R, portfolio=rb_meanETL, 
                                      optimize_method="DEoptim", 
                                      search_size=2000, 
-                                     trace=TRUE, traceDE=5)
+                                     trace=TRUE)
 print(opt_rb_meanETL)
 @
 
-<<fig.cap="mean-ETL Optimization with Risk Budget", fig.align="center", fig.width=5, fig.height=8>>=
+<<fig.cap="mean-ETL Optimization with Risk Budget", fig.align="center", fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_rb_meanETL, risk.col="ETL", return.col="mean", 
      main="Risk Budget mean-ETL Optimization",
      xlim=c(0,0.12), ylim=c(0.005,0.009))
@@ -598,7 +600,7 @@
 
 \subsection{Maximize mean return per unit ETL with ETL equal contribution to risk}
 Add objective to maximize mean return per unit ETL with ETL equal contribution to risk.
-<<>>=
+<<tidy=FALSE>>=
 eq_meanETL <- add.objective(portfolio=init, type="return", name="mean")
 eq_meanETL <- add.objective(portfolio=eq_meanETL, type="risk", name="ETL",
                             arguments=list(p=0.95))
@@ -608,7 +610,7 @@
 @
 
 Run the optimization. Set \code{traceDE=5} so that every fifth iteration is printed. The default is to print every iteration.
-<<>>=
+<<tidy=FALSE>>=
 opt_eq_meanETL <- optimize.portfolio(R=R, portfolio=eq_meanETL, 
                                      optimize_method="DEoptim", 
                                      search_size=2000, 
@@ -617,7 +619,7 @@
 @
 
 Chart the optimal weights and optimal portfolio in risk-return space.
-<<fig.cap="mean-ETL with ETL Equal Risk Contribution", fig.align='center', fig.width=5, fig.height=8>>=
+<<fig.cap="mean-ETL with ETL Equal Risk Contribution", fig.align='center', fig.width=5, fig.height=8, tidy=FALSE>>=
 plot(opt_eq_meanETL, risk.col="ETL", return.col="mean", 
      main="Risk Budget mean-ETL Optimization",
      xlim=c(0,0.12), ylim=c(0.005,0.009))
@@ -637,7 +639,7 @@
 \end{enumerate}
 
 Combine the optimizations for easy comparison.
-<<>>=
+<<tidy=FALSE>>=
 opt_combine <- combine.optimizations(list(meanETL=opt_meanETL,
                                           rbmeanETL=opt_rb_meanETL,
                                           eqmeanETL=opt_eq_meanETL))
@@ -652,20 +654,20 @@
 @
 
 Chart the optimal portfolios of each optimization in risk-return space.
-<<fig.cap="Optimal Portfolios in Risk-Return Space", fig.height=4, fig.width=6, fig.align='center'>>=
+<<fig.cap="Optimal Portfolios in Risk-Return Space", fig.height=4, fig.width=6, fig.align='center', tidy=FALSE>>=
 chart.RiskReward(opt_combine, risk.col="ETL", return.col="mean", 
                  main="ETL Optimization Comparison", xlim=c(0.018, 0.024),
                  ylim=c(0.005, 0.008))
 @
 
 Calculate the STARR of each optimization
-<<fig.cap="STARR of Optimizations", fig.height=3.5, fig.width=5, fig.align='center'>>=
+<<fig.cap="STARR of Optimizations", fig.height=3.5, fig.width=5, fig.align='center', tidy=FALSE>>=
 STARR <- obj_combine[, "mean"] / obj_combine[, "ETL"]
 barplot(STARR, col="blue", cex.names=0.8, cex.axis=0.8,
         las=3, main="STARR", ylim=c(0,1))
 @
 
-<<fig.cap="Percentage Contribution to Risk of Optimizations", fig.height=3.5, fig.width=5, fig.align='center'>>=
+<<fig.cap="Percentage Contribution to Risk of Optimizations", fig.height=3.5, fig.width=5, fig.align='center', tidy=FALSE>>=
 chart.RiskBudget(opt_combine, match.col="ETL", risk.type="percent", 
                  ylim=c(0,1), legend.loc="topright")
 @

Modified: pkg/PortfolioAnalytics/vignettes/portfolio_vignette.pdf
===================================================================
(Binary files differ)

Modified: pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.Rnw
===================================================================
--- pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.Rnw	2013-12-19 19:47:08 UTC (rev 3291)
+++ pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.Rnw	2013-12-19 19:47:50 UTC (rev 3292)
@@ -3,7 +3,6 @@
 \usepackage{bm}
 \usepackage{verbatim}
 \usepackage[latin1]{inputenc}
-% \VignetteIndexEntry{Portfolio Optimization with CVaR budgets in PortfolioAnalytics}
 \bibliographystyle{abbrvnat}
 
 \usepackage{url}
@@ -30,8 +29,9 @@
 % Or whatever. Note that the encoding and the font should match. If T1
 % does not look nice, try deleting the line with the fontenc.
 
+\VignetteIndexEntry{Portfolio Optimization with CVaR budgets in PortfolioAnalytics}
+
 \begin{document}
-\SweaveOpts{concordance=TRUE}
 
 \title{Vignette: Portfolio Optimization with CVaR budgets\\
 in PortfolioAnalytics}
@@ -87,7 +87,7 @@
 options(width=80)
 @
 
-<<echo=TRUE>>=| 
+<<echo=TRUE>>=
 library(PortfolioAnalytics)
 data(indexes)
 class(indexes)
@@ -102,7 +102,7 @@
 
 \subsection{Weight constraints}
   
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 # Create the portfolio specification object
 Wcons <- portfolio.spec( assets = colnames(indexes) )
 # Add box constraints
@@ -112,7 +112,7 @@
 @
 
 Given the weight constraints, we can call the value of the function to be minimized. We consider the case of no violation and a case of violation. By default, \verb"normalize=TRUE" which means that if the sum of weights exceeds \verb"max_sum", the weight vector is normalized by multiplying it with \verb"sum(weights)/max_sum" such that the weights evaluated in the objective function satisfy the \verb"max_sum" constraint.    
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 constrained_objective( w = rep(1/4,4) , R = indexes, portfolio = Wcons) 
 constrained_objective( w = rep(1/3,4) , R = indexes, portfolio = Wcons) 
 constrained_objective( w = rep(1/3,4) , R = indexes, portfolio = Wcons, 
@@ -125,32 +125,33 @@
 
 Suppose now we want to find the portfolio that minimizes the 95\% portfolio CVaR subject to the weight constraints listed above.  
 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ObjSpec = add.objective( portfolio = Wcons , type="risk",name="CVaR",
                          arguments=list(p=0.95), enabled=TRUE)   
 @
 
 The value of the objective function is: 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 constrained_objective( w = rep(1/4,4) , R = indexes, portfolio = ObjSpec) 
 @
 
 This is the CVaR of the equal-weight portfolio as computed by the function \verb"ES" in the \verb"PerformanceAnalytics" package of \citet{ Carl2007}
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 library(PerformanceAnalytics)
-out<-ES(indexes, weights = rep(1/4,4),p=0.95, portfolio_method="component")
+out<-ES(indexes, weights = rep(1/4,4),p=0.95, 
+        portfolio_method="component")
 out$MES
 @
 All arguments in the function \verb"ES" can be passed on through \verb"arguments". E.g. to reduce the impact of extremes on the portfolio results, it is recommended to winsorize the data using the option clean="boudt".
 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 out<-ES(indexes, weights = rep(1/4,4),p=0.95, clean="boudt", 
         portfolio_method="component")
 out$MES
 @
 
 For the formulation of the objective function, this implies setting: 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ObjSpec = add.objective( portfolio = Wcons , type="risk",name="CVaR",
                          arguments=list(p=0.95,clean="boudt"), enabled=TRUE)   
 constrained_objective( w = rep(1/4,4) , R = indexes[,1:4] , portfolio = ObjSpec)
@@ -159,7 +160,7 @@
 An additional argument that is not available for the moment in \verb"ES" is to estimate the conditional covariance matrix through the constant conditional correlation model of \citet{Bollerslev90}. 
 
 For the formulation of the objective function, this implies setting: 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ObjSpec = add.objective( portfolio = Wcons , type="risk",name="CVaR",
                          arguments=list(p=0.95,clean="boudt"), 
                          enabled=TRUE, garch=TRUE)   
@@ -169,20 +170,20 @@
 \subsection{Minimum CVaR concentration objective function}
 
 Add the minimum 95\% CVaR concentration objective to the objective function: 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ObjSpec = add.objective( portfolio = Wcons , type="risk_budget_objective",
                          name="CVaR", arguments=list(p=0.95, clean="boudt"), 
                          min_concentration=TRUE, enabled=TRUE)   
 @
 
 The value of the objective function is: 
-<<echo=TRUE>>=| 
-constrained_objective( w = rep(1/4,4) , R = indexes, portfolio = ObjSpec,
-                       trace=TRUE)
+<<echo=TRUE, tidy=FALSE>>=
+constrained_objective( w = rep(1/4,4) , R = indexes, 
+                       portfolio = ObjSpec, trace=TRUE)
 @
 
 We can verify that this is effectively the largest CVaR contribution of that portfolio as follows:
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ES(indexes[,1:4],weights = rep(1/4,4),p=0.95,clean="boudt", 
    portfolio_method="component")
 @
@@ -191,7 +192,7 @@
 
 We see that in the equal-weight portfolio, the international equities and commodities investment cause more than 30\% of total risk. We could specify as a constraint that no asset can contribute more than 30\% to total portfolio risk with the argument \verb"max_prisk=0.3". This involves the construction of the following objective function:
 
-<<echo=TRUE>>=| 
+<<echo=TRUE, tidy=FALSE>>=
 ObjSpec = add.objective( portfolio = Wcons , type="risk_budget_objective", 
                          name="CVaR", max_prisk = 0.3, 
                          arguments=list(p=0.95,clean="boudt"), enabled=TRUE) 
@@ -213,13 +214,14 @@
 \subsection{Minimum CVaR portfolio under an upper 40\% CVaR allocation constraint} 
 
 The portfolio object and functions needed to obtain the minimum CVaR portfolio under an upper 40\% CVaR allocation objective are the following:
-<<echo=TRUE>>=
+<<echo=TRUE, tidy=FALSE>>=
 # Create the portfolio specification object
 ObjSpec <- portfolio.spec(assets=colnames(indexes[,1:4]))
 # Add box constraints
 ObjSpec <- add.constraint(portfolio=ObjSpec, type='box', min = 0, max=1)
 # Add the full investment constraint that specifies the weights must sum to 1.
-ObjSpec <- add.constraint(portfolio=ObjSpec, type="full_investment")
+ObjSpec <- add.constraint(portfolio=ObjSpec, type="weight_sum", 
+                          min_sum=0.99, max_sum=1.01)
 # Add objective to minimize CVaR
 ObjSpec <- add.objective(portfolio=ObjSpec, type="risk", name="CVaR", 
                          arguments=list(p=0.95, clean="boudt"))
@@ -231,7 +233,7 @@
 
 After the call to these functions it starts to explore the feasible space iteratively and is shown in the output. Iterations are given as intermediate output and by default every iteration will be printed. We set \verb"traceDE=5" to print every 5 iterations and \verb"itermax=50" for a maximum of 50 iterations.
 
-<<echo=TRUE>>=
+<<echo=TRUE, tidy=FALSE>>=
 set.seed(1234)
 out <- optimize.portfolio(R=indexes, portfolio=ObjSpec, 
                           optimize_method="DEoptim", search_size=2000,
@@ -245,7 +247,7 @@
 <<echo=TRUE>>=
 names(out)
 # View the DEoptim_objective_results information at the last iteration
-out$DEoptim_objective_results[[601]]
+out$DEoptim_objective_results[[length(out$DEoptim_objective_results)]]
 
 # Extract stats from the out object into a matrix
 xtract <- extractStats(out)
@@ -264,7 +266,7 @@
 
 The functions needed to obtain the minimum CVaR concentration portfolio are the following:
 
-<<echo=TRUE>>=
+<<echo=TRUE, tidy=FALSE>>=
 # Create the portfolio specification object
 ObjSpec <- portfolio.spec(assets=colnames(indexes))
 # Add box constraints
@@ -284,7 +286,7 @@
 
 
 This portfolio has the near equal risk contribution characteristic: 
-<<echo=TRUE>>=
+<<echo=TRUE, tidy=FALSE>>=
 print(out)
 
 # Verify results with ES function
@@ -293,8 +295,9 @@
 @
 
 The 95\% CVaR percent contribution to risk is near equal for all four indexes. The neighbor portfolios can be plotted to view other near optimal portfolios. Alternatively, the contribution to risk in absolute terms can plotted by setting \verb"risk.type="absolute".
-<<>>=
-chart.RiskBudget(out, neighbors=25, risk.type="pct_contrib", col="blue", pch=18)
+<<tidy=FALSE>>=
+chart.RiskBudget(out, neighbors=25, risk.type="pct_contrib", 
+                 col="blue", pch=18)
 @
 
 
@@ -304,7 +307,7 @@
 
 As an example, consider the minimum CVaR concentration portfolio, with estimation from inception and monthly rebalancing. Since we require a minimum estimation length of total number of observations -1, we can optimize the portfolio only for the last two months.  
 
-<<echo=TRUE>>=
+<<echo=TRUE, tidy=FALSE>>=
 library(iterators)
 set.seed(1234)
 out <- optimize.portfolio.rebalancing(R=indexes, portfolio=ObjSpec, 

Modified: pkg/PortfolioAnalytics/vignettes/risk_budget_optimization.pdf
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/returnanalytics -r 3292


More information about the Returnanalytics-commits mailing list