[Returnanalytics-commits] r2777 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Aug 14 01:58:02 CEST 2013
Author: rossbennett34
Date: 2013-08-14 01:58:01 +0200 (Wed, 14 Aug 2013)
New Revision: 2777
Added:
pkg/PortfolioAnalytics/man/factor_exposure_constraint.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/constraints.R
pkg/PortfolioAnalytics/man/add.constraint.Rd
Log:
adding factor exposure to supported constraint types
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-08-13 22:47:00 UTC (rev 2776)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-08-13 23:58:01 UTC (rev 2777)
@@ -40,6 +40,7 @@
export(extractWeights.optimize.portfolio.rebalancing)
export(extractWeights.optimize.portfolio)
export(extractWeights)
+export(factor_exposure_constraint)
export(fn_map)
export(generatesequence)
export(get_constraints)
Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R 2013-08-13 22:47:00 UTC (rev 2776)
+++ pkg/PortfolioAnalytics/R/constraints.R 2013-08-13 23:58:01 UTC (rev 2777)
@@ -183,12 +183,10 @@
#'
#' This is the main function for adding and/or updating constraints in an object of type \code{\link{portfolio}}.
#'
-#' In general, you will define your constraints as: 'weight_sum', 'box', 'group', 'turnover', 'diversification', or 'position_limit'.
-#'
#' Special cases for the weight_sum constraint are "full_investment" and "dollar_nuetral" or "active" with appropriate values set for min_sum and max_sum. see \code{\link{weight_sum_constraint}}
#'
#' @param portfolio an object of class 'portfolio' to add the constraint to, specifying the constraints for the optimization, see \code{\link{portfolio.spec}}
-#' @param type character type of the constraint to add or update, currently 'weight_sum', 'box', 'group', 'turnover', 'diversification', or 'position_limit'
+#' @param type character type of the constraint to add or update, currently 'weight_sum' (also 'leverage' or 'weight'), 'box', 'group', 'turnover', 'diversification', 'position_limit', 'return', or 'factor_exposure'
#' @param enabled TRUE/FALSE
#' @param message TRUE/FALSE. The default is message=FALSE. Display messages if TRUE.
#' @param \dots any other passthru parameters to specify constraints
@@ -278,6 +276,13 @@
message=message,
...=...)
},
+ # factor exposure constraint
+ factor_exposure=, factor_exposures = {tmp_constraint <- factor_exposure_constraint(assets=assets,
+ type=type,
+ enabled=enabled,
+ message=message,
+ ...=...)
+ },
# Do nothing and return the portfolio object if type is NULL
null = {return(portfolio)}
)
@@ -613,6 +618,11 @@
if(inherits(constraint, "return_constraint")){
out$return_target <- constraint$return_target
}
+ if(inherits(constraint, "factor_exposure_constraint")){
+ out$B <- constraint$B
+ out$lower <- constraint$lower
+ out$upper <- constraint$upper
+ }
}
}
@@ -781,6 +791,54 @@
return(Constraint)
}
+#' Constructor for factor exposure constraint
+#'
+#' This function is called by add.constraint when type="factor_exposure" is specified. see \code{\link{add.constraint}}
+#' \code{B} can be either a vector or matrix of risk factor exposures (i.e. betas).
+#' If \code{B} is a vector, the length of \code{B} must be equal to the number of
+#' assets and lower and upper must be scalars.
+#' If \code{B} is a matrix, the number of rows must be equal to the number
+#' of assets and the number of columns represent the number of factors. The length
+#' of lower and upper must be equal to the number of factors.
+#'
+#' @param type character type of the constraint
+#' @param assets named vector of assets specifying seed weights
+#' @param B vector or matrix of risk factor exposures
+#' @param lower vector of lower bounds of constraints for risk factor exposures
+#' @param upper vector of upper bounds of constraints for risk factor exposures
+#' @param enabled TRUE/FALSE
+#' @param message TRUE/FALSE. The default is message=FALSE. Display messages if TRUE.
+#' @param \dots any other passthru parameters to specify risk factor exposure constraints
+#' @author Ross Bennett
+#' @export
+factor_exposure_constraint <- function(type="factor_exposure", assets, B, lower, upper, enabled=TRUE, message=FALSE, ...){
+ # Number of assets
+ nassets <- length(assets)
+
+ # Assume the user has passed in a vector of betas
+ if(is.vector(B)){
+ # The number of betas must be equal to the number of assets
+ if(length(B) != nassets) stop("length of B must be equal to number of assets")
+ # The user passed in a vector of betas, lower and upper must be scalars
+ if(length(lower) != 1) stop("lower must be a scalar")
+ if(length(upper) != 1) stop("upper must be a scalar")
+ }
+ # The user has passed in a matrix for B
+ if(is.matrix(B)){
+ # The number of rows in B must be equal to the number of assets
+ if(nrow(B) != nassets) stop("number of rows of B must be equal to number of assets")
+ # The user passed in a matrix for B --> lower and upper must be equal to the number of columns in the beta matrix
+ if(length(lower) != ncol(B)) stop("length of lower must be equal to the number of columns in the B matrix")
+ if(length(upper) != ncol(B)) stop("length of upper must be equal to the number of columns in the B matrix")
+ }
+
+ Constraint <- constraint_v2(type=type, enabled=enabled, constrclass="factor_exposure_constraint", ...)
+ Constraint$B <- B
+ Constraint$lower <- lower
+ Constraint$upper <- upper
+ return(Constraint)
+}
+
#' function for updating constrints, not well tested, may be broken
#'
#' can we use the generic update.default function?
Modified: pkg/PortfolioAnalytics/man/add.constraint.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/add.constraint.Rd 2013-08-13 22:47:00 UTC (rev 2776)
+++ pkg/PortfolioAnalytics/man/add.constraint.Rd 2013-08-13 23:58:01 UTC (rev 2777)
@@ -11,8 +11,9 @@
optimization, see \code{\link{portfolio.spec}}}
\item{type}{character type of the constraint to add or
- update, currently 'weight_sum', 'box', 'group',
- 'turnover', 'diversification', or 'position_limit'}
+ update, currently 'weight_sum' (also 'leverage' or
+ 'weight'), 'box', 'group', 'turnover', 'diversification',
+ 'position_limit', 'return', or 'factor_exposure'}
\item{enabled}{TRUE/FALSE}
@@ -31,10 +32,6 @@
constraints in an object of type \code{\link{portfolio}}.
}
\details{
- In general, you will define your constraints as:
- 'weight_sum', 'box', 'group', 'turnover',
- 'diversification', or 'position_limit'.
-
Special cases for the weight_sum constraint are
"full_investment" and "dollar_nuetral" or "active" with
appropriate values set for min_sum and max_sum. see
Added: pkg/PortfolioAnalytics/man/factor_exposure_constraint.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/factor_exposure_constraint.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/factor_exposure_constraint.Rd 2013-08-13 23:58:01 UTC (rev 2777)
@@ -0,0 +1,46 @@
+\name{factor_exposure_constraint}
+\alias{factor_exposure_constraint}
+\title{Constructor for factor exposure constraint}
+\usage{
+ factor_exposure_constraint(type = "factor_exposure",
+ assets, B, lower, upper, enabled = TRUE,
+ message = FALSE, ...)
+}
+\arguments{
+ \item{type}{character type of the constraint}
+
+ \item{assets}{named vector of assets specifying seed
+ weights}
+
+ \item{B}{vector or matrix of risk factor exposures}
+
+ \item{lower}{vector of lower bounds of constraints for
+ risk factor exposures}
+
+ \item{upper}{vector of upper bounds of constraints for
+ risk factor exposures}
+
+ \item{enabled}{TRUE/FALSE}
+
+ \item{message}{TRUE/FALSE. The default is message=FALSE.
+ Display messages if TRUE.}
+
+ \item{\dots}{any other passthru parameters to specify
+ risk factor exposure constraints}
+}
+\description{
+ This function is called by add.constraint when
+ type="factor_exposure" is specified. see
+ \code{\link{add.constraint}} \code{B} can be either a
+ vector or matrix of risk factor exposures (i.e. betas).
+ If \code{B} is a vector, the length of \code{B} must be
+ equal to the number of assets and lower and upper must be
+ scalars. If \code{B} is a matrix, the number of rows must
+ be equal to the number of assets and the number of
+ columns represent the number of factors. The length of
+ lower and upper must be equal to the number of factors.
+}
+\author{
+ Ross Bennett
+}
+
More information about the Returnanalytics-commits
mailing list