[Returnanalytics-commits] r2822 - in pkg/PortfolioAnalytics: R demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Aug 19 07:04:51 CEST 2013
Author: rossbennett34
Date: 2013-08-19 07:04:50 +0200 (Mon, 19 Aug 2013)
New Revision: 2822
Modified:
pkg/PortfolioAnalytics/R/constrained_objective.R
pkg/PortfolioAnalytics/R/objectiveFUN.R
pkg/PortfolioAnalytics/demo/testing_ROI.R
Log:
adding var.portfolio function to calculate portfolio variance via constrained_objective when 'var' is an objective
Modified: pkg/PortfolioAnalytics/R/constrained_objective.R
===================================================================
--- pkg/PortfolioAnalytics/R/constrained_objective.R 2013-08-19 04:50:32 UTC (rev 2821)
+++ pkg/PortfolioAnalytics/R/constrained_objective.R 2013-08-19 05:04:50 UTC (rev 2822)
@@ -532,6 +532,9 @@
StdDev = {
fun = match.fun(StdDev)
},
+ var = {
+ fun = match.fun(var.portfolio)
+ },
mVaR =,
VaR = {
fun = match.fun(VaR)
Modified: pkg/PortfolioAnalytics/R/objectiveFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/objectiveFUN.R 2013-08-19 04:50:32 UTC (rev 2821)
+++ pkg/PortfolioAnalytics/R/objectiveFUN.R 2013-08-19 05:04:50 UTC (rev 2822)
@@ -19,4 +19,20 @@
if(length(weights) != length(wts.init)) stop("weights and wts.init are not the same length")
return(sum(abs(wts.init - weights)) / N)
-}
\ No newline at end of file
+}
+
+#' Calculate portfolio variance
+#'
+#' This function is used to calculate the portfolio variance via a call to
+#' constrained_objective when var is an object for mean variance or quadratic
+#' utility optimization.
+#'
+#' @param R xts object of asset returns
+#' @param weights vector of asset weights
+#' @return numeric value of the portfolio variance
+#' @author Ross Bennett
+#' @export
+var.portfolio <- function(R, weights){
+ weights <- matrix(weights, ncol=1)
+ return(as.numeric(t(weights) %*% var(R) %*% weights))
+}
Modified: pkg/PortfolioAnalytics/demo/testing_ROI.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_ROI.R 2013-08-19 04:50:32 UTC (rev 2821)
+++ pkg/PortfolioAnalytics/demo/testing_ROI.R 2013-08-19 05:04:50 UTC (rev 2822)
@@ -12,11 +12,6 @@
library(Ecdat)
library(PortfolioAnalytics)
-var.portfolio <- function(R, weights){
- weights <- matrix(weights, ncol=1)
- return(as.numeric(t(weights) %*% var(R) %*% weights))
-}
-
# General Parameters for sample code
data(edhec)
funds <- names(edhec)
@@ -57,7 +52,7 @@
target.port$objectives[[2]]$enabled <- TRUE
target.solution <- optimize.portfolio(R=edhec, constraints=target.port, optimize_method="ROI")
-target.solution$weights %*% var(edhec) %*% target.solution$weights
+
# ========================
# Mean-variance: Maximize quadratic utility, dollar-neutral, target portfolio return
#
More information about the Returnanalytics-commits
mailing list