-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsample_pso.R
51 lines (34 loc) · 967 Bytes
/
sample_pso.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
# # # # # # # # # #
# Try DEoptim
library(xts)
library(quantmod)
library(PerformanceAnalytics)
library(DEoptim)
library(pso)
library(GenSA)
data(edhec)
N <- 4
R <- edhec[, 1:N]
T <- nrow(edhec)
mu <- colMeans(R)
sigma <- cov(R)
obj <- function(w){
if (sum(w)==0) {w <- w + 1e-2}
w <- w/sum(w)
CVaR <- ES(weights= w,
method="gaussian",
portfolio_method="component",
mu=mu,
sigma=sigma)
tmp1 <- CVaR$ES
tmp2 <- max(CVaR$pct_contrib_ES - 0.225, 0)
out <- tmp1 + 1e3 * tmp2
}
set.seed(1234)
deoptim.sol <- DEoptim(fn=obj, lower = rep(0, N), upper = rep(1, N))
deoptim.sol$optim$bestval
wts.deoptim <- deoptim.sol$optim$bestmem / sum(deoptim.sol$optim$bestmem)
test <- psoptim(rep(NA,N), obj, lower=0, upper=1, control=list(abstol=1e-8, trace=TRUE))
test$value
wts.pso <- test$par/sum(test$par)
gen.sol <- GenSA(rep(1/N, N), lower=rep(0,N), upper=rep(1,N), obj, control=list(verbose=TRUE))