Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. w ̂=argmin w'Σ w
  2. s. t. w'I = 1 #weights sum up to 1
  3. w'μ=ρ #target expected return
  4. w≥0 #non-negativity(short-sale) constraint
  5.  
  6. optimization<-function(returns) {
  7. p <- ncol(x) #number of assets
  8. n <- nrow(x) #number of observations
  9. x <- matrix(data$return,n,assets)
  10. mean <- colMeans(na.rm=FALSE,x)
  11. M <- as.integer(10) #nuber of ports on the eff.front.
  12. S <- cov(x) #covariance matrix
  13. Rmax<- 0.01 #max monthly return value
  14. Dmat <- solve(S) #inverse of covariance matrix
  15. u <- rep(1,p) #vector of ones
  16.  
  17. These codes are for the Lagrange solutions
  18.  
  19. a<- matrix(rep(0,4), nrow=2)
  20. a[1,1] <- t(u)%*% Dmat %*%u
  21. a[1,2] <- t(mean)%*%Dmat%*%u
  22. a[2,1] <- a[1,2]
  23. a[2,2] <- t(mean)%*%Dmat%*%mean
  24. d <- a[1,1]*a[2,2]-a[1,2]*a[1,2]
  25. f <- (Dmat%*%(a[2,2]*u-a[1,2]*mean))/d
  26. g <- (Dmat%*%(-a[1,2]*u+a[1,1]*mean))/d
  27. r <- seq(0, Rmax, length=M)
  28. w <- matrix((rep(0, p*M)), nrow=p)
  29.  
  30. for(i in 1:M) { w[,i] = f+r[i]*g #portfolio weights
  31. if (w[,i] <0) {w[,i]=0} else {w[,i]=w[,i]}
  32. }
  33.  
  34. while (w> 0)
  35. { for(i in 1:M) { w[,i] = f+r[i]*g }
  36. print(w)
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement