na <- 100
inc <- 5
R <- randomReturns(na = na,
ns = 1000,
sd = seq(0.01, 0.02, length.out = 100),
rho = 0.5)
S <- cov(R)
OF <- function(x, S, ...) {
w <- 1/sum(x)
sum(w * w * S[x, x])
}
x <- logical(na)
x[1:inc] <- TRUE
all.neighbours <- function(x, ...) {
true <- which( x)
false <- which(!x)
ans <- list()
for (i in true) {
for (j in false) {
ans1 <- x
ans1[i] <- !x[i]
ans1[j] <- !x[j]
ans <- c(ans, list(ans1))
}
}
ans
}
algo <- list(loopOF = TRUE,
maxit = 1000,
all.neighbours = all.neighbours,
x0 = x)
system.time(sol.gs <- greedySearch(OF, algo = algo, S = S))
sqrt(sol.gs$OFvalue)
Run the code above in your browser using DataLab