-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtesting_portfolio_specification.R
61 lines (48 loc) · 1.99 KB
/
testing_portfolio_specification.R
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
# Testing for the new portfolio specification
# Load necessary packages
library(PortfolioAnalytics)
# Load the edhec data
data(edhec)
# Use the first 5 columns of edhec as returns
ret <- edhec[, 1:5]
funds <- colnames(ret)
# Specify a portfolio object
pspec <- portfolio.spec(assets=funds)
# pspec is an object of class "portfolio" that holds information about the
# assets, constraints, and objectives.
# The constraints will be stored as objects in the $constraints list
# The objectives will be stored as objects in the $constraints list. Note that
# this is just like how they are currently stored in the constraints object.
class(pspec)
str(pspec)
# Add a constraint object to pspec for the sum of the weights
pspec <- add.constraint(portfolio=pspec, type="weight_sum",
min_sum=0.99, max_sum=1.01)
print(pspec)
# Forgot to enable the weight_sum constraint
pspec <- add.constraint(portfolio=pspec, type="weight_sum",
min_sum=0.99, max_sum=1.01, enabled=TRUE,
indexnum=1)
print(pspec)
# Add box constraints to the pspec object
pspec <- add.constraint(portfolio=pspec, type="box", min=0.1, max=0.4, enabled=TRUE)
print(pspec)
# Update the box constraints to pass in a vector for min and max. Updates the
# object in place with the indexnum argument
pspec <- add.constraint(portfolio=pspec, type="box",
min=c(0.1, 0.05, 0.1, 0.15, 0.2),
max=c(0.4, 0.4, 0.5, 0.45, 0.6),
indexnum=2)
print(pspec)
# Add objectives to the pspec object
pspec <- add.objective(portfolio=pspec, type="return", name="mean",
enabled=FALSE, multiplier=0)
print(pspec)
pspec <- add.objective(portfolio=pspec, type="risk", name="var",
enabled=FALSE, multiplier=0, risk_aversion=10)
print(pspec)
pspec <- add.objective(portfolio=pspec, type="risk", name="CVaR",
enabled=FALSE, multiplier=0)
print(pspec)
str(pspec)
summary(pspec)