SCM

SCM Repository

[rmetrics] Diff of /pkg/fPortfolio/R/102C-MarkowitzPortfolio.R
ViewVC logotype

Diff of /pkg/fPortfolio/R/102C-MarkowitzPortfolio.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 48, Wed Mar 1 07:43:04 2006 UTC revision 49, Wed Mar 1 13:55:41 2006 UTC
# Line 123  Line 123 
123    
124      # Check Data:      # Check Data:
125      if (is.list(data)) {      if (is.list(data)) {
126          x = NA          # data is a list with entries list(mu, Sigma)
127            x.mat = NA
128          mu = data$mu          mu = data$mu
129          Sigma = data$Sigma          Sigma = data$Sigma
130          plottype = "mucov"          plottype = "mucov"
131      } else {      } else {
132            # data is a rectangular time series object:
133          x.mat = as.matrix(data)          x.mat = as.matrix(data)
134          # Mean Vector  and Covariance Matrix:          # Mean Vector  and Covariance Matrix:
135          mu = apply(x.mat, MARGIN = 2, FUN = mean)          mu = apply(x.mat, MARGIN = 2, FUN = mean)
# Line 135  Line 137 
137          plottype = "series"          plottype = "series"
138      }      }
139    
     # Settings:  
     C0 = 1  
   
140      # Ranges for mean and Standard Deviation:      # Ranges for mean and Standard Deviation:
141      if (is.null(r.range)) r.range = range(mu)      if (is.null(r.range)) r.range = range(mu)
142      if (is.null(s.range)) s.range = c(0, max(sqrt(diag(Sigma))))      if (is.null(s.range)) s.range = c(0, max(sqrt(diag(Sigma))))
143    
     # Settings:  
     returns = mu  
     covar = Sigma  
     dimension = length(returns)  
   
144      # Calculate Efficient Frontier:      # Calculate Efficient Frontier:
145      pmmin = min(r.range)      pmmin = min(r.range)
146      pmmax = max(r.range)      pmmax = max(r.range)
147      ps = pm = diversification = rep(0, length = length)      ps = pm = diversification = rep(0, times = length)
148      eps = 1.0e-6      eps = 1.0e-6
149      k = 0      k = 0
150      for (pm.now in seq(pmmin + eps, pmmax - eps, length = length)) {      for (PM in seq(pmmin+eps, pmmax-eps, length = length)) {
151          k = k+1          k = k+1
152          ef = .portfolio.optim(x = x, pm = pm.now, covmat = covar)          ef = .portfolio.optim(pm = PM, returns = mu, covmat = Sigma)
153          pm[k] = ef$pm          pm[k] = ef$pm
154          ps[k] = ef$ps          ps[k] = ef$ps
155          diversification[k] = length (ef$pw[ef$pw > 0.01])          diversification[k] = length (ef$pw[ef$pw > 0.01])
# Line 165  Line 159 
159      pfolio = list(      pfolio = list(
160          what = "frontier",          what = "frontier",
161          plottype = plottype,          plottype = plottype,
162          data = x,          data = data,
163          pw = NA, pm = pm, ps = ps,          pw = NA, pm = pm, ps = ps,
164          returns = returns, cov = covar,          returns = mu, cov = Sigma,
165          r.range = r.range, s.range = s.range,          r.range = r.range, s.range = s.range,
166          Rf = NA, Rm = NA, Sm = NA,          Rf = NA, Rm = NA, Sm = NA,
167          Rew = NA, Sew = NA,          Rew = NA, Sew = NA,
# Line 325  Line 319 
319    
320      # Transform to matrix:      # Transform to matrix:
321      x = as.matrix(x)      x = as.matrix(x)
322        MU = apply(x, 2, mean)
323      COV = cov(x)      COV = cov(x)
324    
325      # Quadratic Programming:      # Quadratic Programming:
326      pfolio = list()      pfolio = list()
327      pfolio$what = "portfolio"      pfolio$what = "portfolio"
328      pfolio$method = "QP"      pfolio$method = "QP"
329      opt = .portfolio.optim(x = x, pm = targetReturn, covmat = COV)      opt = .portfolio.optim(pm = targetReturn, returns = MU, covmat = COV)
330      pfolio$pw = opt$pw      pfolio$pw = opt$pw
331      pfolio$pm = opt$pm      pfolio$pm = opt$pm
332      pfolio$ps = opt$ps      pfolio$ps = opt$ps
# Line 716  Line 711 
711      # Calculate Tangency Portfolio, if it exists!      # Calculate Tangency Portfolio, if it exists!
712      # The test is true, when the slope could be evaluated.      # The test is true, when the slope could be evaluated.
713      if (!is.na(slope)) {      if (!is.na(slope)) {
714        tangency = .portfolio.optim(x = object$data, pm = ym,        tangency = .portfolio.optim(pm = ym, returns = object$returns,
715          covmat = object$cov)          covmat = object$cov)
716        # Portfolio Weights in Percent:        # Portfolio Weights in Percent:
717        pw = 100 * tangency$pw        pw = 100 * tangency$pw
# Line 749  Line 744 
744      # FUNCTION:      # FUNCTION:
745    
746      # Calculate equally weighted portfolio:      # Calculate equally weighted portfolio:
747      x = object$data      means = object$returns
748      means = apply(x, 2, mean)      covmat = object$cov
     covmat = cov(x)  
749      ew.weights = rep(1/length(object$returns), times = length(object$returns))      ew.weights = rep(1/length(object$returns), times = length(object$returns))
750      Rew = (ew.weights %*% means)[[1,1]]      Rew = (ew.weights %*% means)[[1,1]]
751      Sew = (ew.weights %*% covmat %*% ew.weights)[[1,1]]      Sew = (ew.weights %*% covmat %*% ew.weights)[[1,1]]
752      if (add) {      if (add) {
753          points(sqrt(Sew), Rew, col = "steelblue4")          points(sqrt(Sew), Rew, col = "steelblue")
754      }      }
755    
756      # Return Value:      # Return Value:
# Line 797  Line 791 
791    
792      # Check Data:      # Check Data:
793      if (is.list(data)) {      if (is.list(data)) {
794          x = NA          x.mat = NA
795          mu = data$mu          mu = data$mu
796          Sigma = data$Sigma          Sigma = data$Sigma
797          plottype = "mucov"          plottype = "mucov"
# Line 928  Line 922 
922    
923    
924  .portfolio.optim =  .portfolio.optim =
925  function(x, pm = mean(x), covmat = cov(x))  function(pm, returns, covmat)
926  {   # A Builtin function modified by Diethelm Wuertz  {   # A Builtin function modified by Diethelm Wuertz
927    
928      # Description:      # Description:
# Line 948  Line 942 
942      # FUNCTION:      # FUNCTION:
943    
944      # Optimize:      # Optimize:
945      k = dim(x)[2]      k = dim(covmat)[2] # dim(x)[2]
946      dvec = rep(0, k)      dvec = rep(0, k)
947      a1 = rep(1, k)      a1 = rep(1, k)
948      a2 = apply(x, 2, mean)      a2 = returns # apply(x, 2, mean)
949      a3 = matrix(0, k, k)      a3 = matrix(0, k, k)
950      diag(a3) = 1      diag(a3) = 1
951      b3 = rep(0, k)      b3 = rep(0, k)
952      Amat = t(rbind(a1, a2, a3))      Amat = t(rbind(a1, a2, a3))
953      b0 = c(1, pm, b3)      b0 = c(1, pm, b3)
954      res = .solve.QP(covmat, dvec, Amat, bvec = b0, meq = 2)      res = .solve.QP(covmat, dvec, Amat, bvec = b0, meq = 2)
955      y = t(res$solution %*% t(x))      weights = res$solution
956        ps = sqrt(weights %*% covmat %*% weights)
957    
958      # Result:      # Result:
959      ans = list(pw = res$solution, px = y, pm = mean(y), ps = sd(y))      ans = list(pw = weights, pm = pm, ps = ps)
960    
961      # Return value:      # Return value:
962      ans      ans
# Line 1011  Line 1006 
1006          iter = as.integer(iter), work = as.double(work),          iter = as.integer(iter), work = as.double(work),
1007          ierr = as.integer(factorized), PACKAGE = "fPortfolio")          ierr = as.integer(factorized), PACKAGE = "fPortfolio")
1008      if (res1$ierr == 1) {      if (res1$ierr == 1) {
1009          stop("constraints are inconsistent, no solution!")          warning("constraints are inconsistent, no solution!")
1010      } else if (res1$ierr == 2) {      } else if (res1$ierr == 2) {
1011          stop("matrix D in quadratic function is not positive definite!")          warning("matrix D in quadratic function is not positive definite!")
1012      }      }
1013    
1014      # Return Value:      # Return Value:

Legend:
Removed from v.48  
changed lines
  Added in v.49

R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge