Comment coder quatre exercice selon J
Help R :: Coder sur R :: Votre 1er forum
Page 1 sur 1
Comment coder quatre exercice selon J
#1
Er <- matrix( diff(log(STOCKS)),dim(STOCKS)[1]-1 ,dim(STOCKS)[2])
Vc <- covars(Er)
install.packages("quadprog")
library('quadprog')
#2
Optimal <- function(a,b,c,d) {
w <- solve.QP(a,b,t(c),d,meq=1)
return(w)
}
Dmat <- Vc
dvec <- matrix(colMeans(Er),dim(Er)[2],1)
amat <- rbind(rep(1,5),diag(5))
Amat <- t(amat)
bvec <- c(1,rep(0,5))
Sol<-solve.QP(Dmat,dvec,Amat,bvec,meq=1,factorized=FALSE)
Sol$solution
#3
n <- 200
lambda <- matrix(seq(-5,5,length.out=n),n,1)
solution <- matrix(NA,dim(STOCKS)[2],n)
for (i in 1:n) {
solution[i,] <- solve.QP(Dmat,dvec/lambda[i],Amat,bvec,meq=1,factorized=FALSE)$solution
}
Ep <- matrix(NA,n,1)
Vp <- matrix(NA,n,1)
for (i in 1:n) {
Ep[i,] <- PortRet(solution[i,],dvec)
Vp[i,] <- PortSig(Solution[i,],Dmat)
}
Matplot(Vp,Ep)
plot(Vp,Ep)
Er <- matrix( diff(log(STOCKS)),dim(STOCKS)[1]-1 ,dim(STOCKS)[2])
Vc <- covars(Er)
install.packages("quadprog")
library('quadprog')
#2
Optimal <- function(a,b,c,d) {
w <- solve.QP(a,b,t(c),d,meq=1)
return(w)
}
Dmat <- Vc
dvec <- matrix(colMeans(Er),dim(Er)[2],1)
amat <- rbind(rep(1,5),diag(5))
Amat <- t(amat)
bvec <- c(1,rep(0,5))
Sol<-solve.QP(Dmat,dvec,Amat,bvec,meq=1,factorized=FALSE)
Sol$solution
#3
n <- 200
lambda <- matrix(seq(-5,5,length.out=n),n,1)
solution <- matrix(NA,dim(STOCKS)[2],n)
for (i in 1:n) {
solution[i,] <- solve.QP(Dmat,dvec/lambda[i],Amat,bvec,meq=1,factorized=FALSE)$solution
}
Ep <- matrix(NA,n,1)
Vp <- matrix(NA,n,1)
for (i in 1:n) {
Ep[i,] <- PortRet(solution[i,],dvec)
Vp[i,] <- PortSig(Solution[i,],Dmat)
}
Matplot(Vp,Ep)
plot(Vp,Ep)
Help R :: Coder sur R :: Votre 1er forum
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|