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

Description Usage Arguments Details Author(s) See Also Examples

Description

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

Usage

1
2
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

Details

The following constraint types may be specified:

Author(s)

Ross Bennett

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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)

Example output

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: xts
Loading required package: foreach
Loading required package: PerformanceAnalytics

Attaching package: 'PerformanceAnalytics'

The following object is masked from 'package:graphics':

    legend

$type
[1] "full_investment"

$enabled
[1] TRUE

$message
[1] FALSE

$min_sum
[1] 1

$max_sum
[1] 1

$call
add.constraint(portfolio = portf, type = "full_investment")

attr(,"class")
[1] "weight_sum_constraint" "constraint"           
$type
[1] "long_only"

$enabled
[1] TRUE

$min
Convertible Arbitrage            CTA Global Distressed Securities 
                    0                     0                     0 
     Emerging Markets 
                    0 

$max
Convertible Arbitrage            CTA Global Distressed Securities 
                    1                     1                     1 
     Emerging Markets 
                    1 

$call
add.constraint(portfolio = portf, type = "long_only")

attr(,"class")
[1] "box_constraint" "constraint"    

PortfolioAnalytics documentation built on May 1, 2019, 10:56 p.m.