[Returnanalytics-commits] r3406 - in pkg/PortfolioAnalytics: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 4 02:47:01 CEST 2014


Author: rossbennett34
Date: 2014-06-04 02:47:01 +0200 (Wed, 04 Jun 2014)
New Revision: 3406

Added:
   pkg/PortfolioAnalytics/man/portfolio.moments.boudt.Rd
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
   pkg/PortfolioAnalytics/R/moment.functions.R
Log:
Adding function to set portfolio moments using statistical factor model based on Boudt paper

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2014-06-03 19:52:08 UTC (rev 3405)
+++ pkg/PortfolioAnalytics/NAMESPACE	2014-06-04 00:47:01 UTC (rev 3406)
@@ -50,6 +50,7 @@
 export(optimize.portfolio.rebalancing)
 export(optimize.portfolio)
 export(portfolio_risk_objective)
+export(portfolio.moments.boudt)
 export(portfolio.spec)
 export(pos_limit_fail)
 export(position_limit_constraint)

Modified: pkg/PortfolioAnalytics/R/moment.functions.R
===================================================================
--- pkg/PortfolioAnalytics/R/moment.functions.R	2014-06-03 19:52:08 UTC (rev 3405)
+++ pkg/PortfolioAnalytics/R/moment.functions.R	2014-06-04 00:47:01 UTC (rev 3406)
@@ -250,6 +250,73 @@
     if(is.null(momentargs$m4)) momentargs$m4 = PerformanceAnalytics:::M4.MM(R)
     return(momentargs)
 }
+
+#' Portfolio Moments
+#' 
+#' Set portfolio moments for use by lower level optimization functions using
+#' a statistical factor model based on the work of Kris Boudt.
+#' 
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of 
+#' asset returns
+#' @param portfolio an object of type \code{portfolio} specifying the 
+#' constraints and objectives for the optimization, see 
+#' \code{\link{portfolio.spec}}
+#' @param momentargs list containing arguments to be passed down to lower level 
+#' functions, default NULL
+#' @param k number of factors used for fitting statistical factor model
+#' @param \dots any other passthru parameters
+#' @export
+portfolio.moments.boudt <- function(R, portfolio, momentargs=NULL, k=1, ...){
+  
+  # Fit the statistical factor model
+  fit <- statistical.factor.model(R=R, k=k)
+  
+  if(!hasArg(momentargs) | is.null(momentargs)) momentargs<-list()
+  if(is.null(portfolio$objectives)) {
+    warning("no objectives specified in portfolio")
+    next()
+  } else {
+    for (objective in portfolio$objectives){
+      switch(objective$name,
+             mean = {
+               if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1)
+             },
+             var =,
+             sd =,
+             StdDev = { 
+               if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean', na.rm=TRUE)),ncol=1);
+               if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
+             },
+             mVaR =,
+             VaR = {
+               if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
+               if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
+               if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
+               if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
+             },
+             es =,
+             mES =,
+             CVaR =,
+             cVaR =,
+             ETL=,
+             mETL=,
+             ES = {
+               # We don't want to calculate these moments if we have an ES 
+               # objective and are solving as an LP problem.
+               if(hasArg(ROI)) ROI=match.call(expand.dots=TRUE)$ROI else ROI=FALSE
+               if(!ROI){
+                 if(is.null(momentargs$mu)) momentargs$mu = matrix( as.vector(apply(R,2,'mean')),ncol=1);
+                 if(is.null(momentargs$sigma)) momentargs$sigma = extractCovariance(fit)
+                 if(is.null(momentargs$m3)) momentargs$m3 = extractCoskewness(fit)
+                 if(is.null(momentargs$m4)) momentargs$m4 = extractCokurtosis(fit)
+               }
+             }
+      ) # end switch on objectives    
+    }    
+  }    
+  return(momentargs)
+}
+
 ###############################################################################
 # $Id$
 ###############################################################################

Added: pkg/PortfolioAnalytics/man/portfolio.moments.boudt.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/portfolio.moments.boudt.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/portfolio.moments.boudt.Rd	2014-06-04 00:47:01 UTC (rev 3406)
@@ -0,0 +1,29 @@
+\name{portfolio.moments.boudt}
+\alias{portfolio.moments.boudt}
+\title{Portfolio Moments}
+\usage{
+  portfolio.moments.boudt(R, portfolio, momentargs = NULL,
+    k = 1, ...)
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{portfolio}{an object of type \code{portfolio}
+  specifying the constraints and objectives for the
+  optimization, see \code{\link{portfolio.spec}}}
+
+  \item{momentargs}{list containing arguments to be passed
+  down to lower level functions, default NULL}
+
+  \item{k}{number of factors used for fitting statistical
+  factor model}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  Set portfolio moments for use by lower level optimization
+  functions using a statistical factor model based on the
+  work of Kris Boudt.
+}
+



More information about the Returnanalytics-commits mailing list