Learn R Programming

PortfolioAnalytics (version 2.0.0)

add.constraint: General interface for adding and/or updating optimization constraints.

Description

This is the main function for adding and/or updating constraints to the portfolio.spec object.

Usage

add.constraint(
  portfolio,
  type,
  enabled = TRUE,
  message = FALSE,
  ...,
  indexnum = NULL
)

Arguments

portfolio

an object of class 'portfolio' to add the constraint to, specifying the constraints for the optimization, see portfolio.spec

type

character type of the constraint to add or update, currently 'weight_sum' (also 'leverage' or 'weight'), 'box', 'group', 'turnover', 'diversification', 'position_limit', 'return', 'factor_exposure', or 'leverage_exposure'

enabled

TRUE/FALSE. The default is enabled=TRUE.

message

TRUE/FALSE. The default is message=FALSE. Display messages if TRUE.

...

any other passthru parameters to specify constraints

indexnum

if you are updating a specific constraint, the index number in the $constraints list to update

Author

Ross Bennett

Details

The following constraint types may be specified:

weight_sum, weight, leverage

Specify constraint on the sum of the weights, see weight_sum_constraint

full_investment

Special case to set min_sum=1 and max_sum=1 of weight sum constraints

dollar_neutral, active

Special case to set min_sum=0 and max_sum=0 of weight sum constraints

box

box constraints for the individual asset weights, see box_constraint

long_only

Special case to set min=0 and max=1 of box constraints

group

specify the sum of weights within groups and the number of assets with non-zero weights in groups, see group_constraint

turnover

Specify a constraint for target turnover. Turnover is calculated from a set of initial weights, see turnover_constraint

diversification

target diversification of a set of weights, see diversification_constraint

position_limit

Specify the number of non-zero, long, and/or short positions, see position_limit_constraint

return

Specify the target mean return, see return_constraint

factor_exposure

Specify risk factor exposures, see factor_exposure_constraint

leverage_exposure

Specify a maximum leverage exposure, see leverage_exposure_constraint

See Also

portfolio.spec weight_sum_constraint, box_constraint, group_constraint, turnover_constraint, diversification_constraint, position_limit_constraint, return_constraint, factor_exposure_constraint, leverage_exposure_constraint

Examples

Run this code
data(edhec)
returns <- edhec[, 1:4]
fund.names <- colnames(returns)
pspec <- portfolio.spec(assets=fund.names)

# Add the full investment constraint that specifies the weights must sum to 1.
pspec <- add.constraint(portfolio=pspec, type="weight_sum", min_sum=1, max_sum=1)

# The full investment constraint can also be specified with type="full_investment"
pspec <- add.constraint(portfolio=pspec, type="full_investment")

# Another common constraint is that portfolio weights sum to 0.
pspec <- add.constraint(portfolio=pspec, type="weight_sum", min_sum=0, max_sum=0)
pspec <- add.constraint(portfolio=pspec, type="dollar_neutral")
pspec <- add.constraint(portfolio=pspec, type="active")

# Add box constraints
pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.4)

# min and max can also be specified per asset
pspec <- add.constraint(portfolio=pspec, 
                        type="box", 
                        min=c(0.05, 0, 0.08, 0.1), 
                        max=c(0.4, 0.3, 0.7, 0.55))
                        
# A special case of box constraints is long only where min=0 and max=1
# The default action is long only if min and max are not specified
pspec <- add.constraint(portfolio=pspec, type="box")
pspec <- add.constraint(portfolio=pspec, type="long_only")

# Add group constraints
pspec <- add.constraint(portfolio=pspec, 
                        type="group", 
                        groups=list(c(1, 2, 1), 4), 
                        group_min=c(0.1, 0.15), 
                        group_max=c(0.85, 0.55), 
                        group_labels=c("GroupA", "GroupB"), 
                        group_pos=c(2, 1))

# 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)

# Add diversification constraint
pspec <- add.constraint(portfolio=pspec, type="diversification", div_target=0.7)

# Add turnover constraint
pspec <- add.constraint(portfolio=pspec, type="turnover", turnover_target=0.2)

# Add target mean return constraint
pspec <- add.constraint(portfolio=pspec, type="return", return_target=0.007)

# Example using the indexnum argument
portf <- portfolio.spec(assets=fund.names)
portf <- add.constraint(portf, type="full_investment")
portf <- add.constraint(portf, type="long_only")

# indexnum corresponds to the index number of the constraint
# The full_investment constraint was the first constraint added and has 
# indexnum=1
portf$constraints[[1]]

# View the constraint with indexnum=2
portf$constraints[[2]]

# Update the constraint to relax the sum of weights constraint
portf <- add.constraint(portf, type="weight_sum", 
min_sum=0.99, max_sum=1.01, 
indexnum=1)

# Update the constraint to modify the box constraint
portf <- add.constraint(portf, type="box", 
min=0.1, max=0.8, 
indexnum=2)

Run the code above in your browser using DataLab