2016-11-22 7 views
0

Ich versuche, an Portfolio-Optimierung Problem zu arbeiten, im Grunde haben wir ein Produkt, das% -Portfolio und die Rücklaufquote. Grundsätzlich muss ich die Gesamtrücklaufrate optimieren, um maximal zu sein. Das Problem wird schwierig, weil es eine minimale Einschränkung spezifisch Produkt istOptimieren mit Einschränkungen in R

Daten:

product share_per return_per min_share_per 
prod1 0.5  0.1  0.2 
prod2 0.2  0.4  0.1 
prod3 0.2  0.05  0.0 
prod4 0.1  0.04  0.0 
prod5 0.0  0.3  0.0 

Grundsätzlich werden wir die Optimierung auf Spalte share_per so durchführen als Produkt * (share_per * return_per) zu machen * maximale zu dieser hoffnungslos schlechten Versuch Mein war

mat <- matrix(c(0.5, 0.2, 0.2, 0.1, 0.0)) 
colnames(mat) <- c("return_per") 

minmax <- function(x, a) (sum(a*x)) 
opt <- apply(mat, 1, function(i) { 
    optimize(minmax, c(0, 1), a = i[["return_per"]], maximum=T)$maximum 
}) 

mat2 <- cbind(mat, opt) 
mat2 

Wie Sie ich kann, wo weder heraus sehen spe cify die Einschränkung für eine Zeile

Ich weiß, constrOptim ist etwas, was ich suchen sollte, aber ich kann nicht die Einschränkung Teil.

Antwort

1

Sie können dies versuchen:

df <- read.table(text='product share_per return_per min_share_per 
       prod1 0.5  0.1  0.2 
       prod2 0.2  0.4  0.1 
       prod3 0.2  0.05  0.0 
       prod4 0.1  0.04  0.0 
       prod5 0.0  0.3  0.0', header=TRUE) 

ret <- df$return_per 

fn <- function(sp) sum(ret*sp) # objective 

Amat <- rbind(diag(nrow(df)), diag(-1,nrow(df)), rep(-1,nrow(df))) # constraints 
bvec <- c(df$min_share_per, rep(-1, nrow(df)), -1)    # sp_j >= min_share_per, 
                    # sp_j <= 1 and 
                    # sum_j sp_j <= 1 
init <- c(0.5,0.2,0.2,0.05,0.01) # making sure that the initial value is in the feasible region 

sol <- constrOptim(init, fn, NULL, ui = Amat, ci = bvec, control=list(fnscale=-1)) # maximize 
round(sol$par, 2) 
# [1] 0.2 0.8 0.0 0.0 0.0 
+0

Oh wow! Schön gemacht!! Ich kann dir nicht genug danken !!! –

Verwandte Themen