#' ---1#' title: "Sortino Demo"2#' author: Brian Peterson3#' date: "7/17/2014"4#' ---56#' Load the necessary packages7# Include optimizer and multi-core packages8library(PortfolioAnalytics)9library(PerformanceAnalytics)10require(xts)11require(DEoptim)12require(TTR)1314#' Register parallel backend.15#' note: these may not be appropriate on Windows16require(doMC)17registerDoMC()18# for Windows19#require(doParallel)20#registerDoParallel()212223#' Load the data. Here we use monthly total returns of four asset-class indexes.24data(indexes)25#only look at 2000 onward26#indexes<-indexes["2000::"]272829#' Set the MAR parameter30MAR =.005 #~6%/year3132#' Example 1 maximize Sortino Ratio33SortinoConstr <- constraint(assets = colnames(indexes[,1:4]), min = 0.05, max = 1, min_sum=.99, max_sum=1.01, weight_seq = generatesequence(by=.001))34SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="SortinoRatio", enabled=TRUE, arguments = list(MAR=MAR))35SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="mean", enabled=TRUE, multiplier=0) # multiplier 0 makes it availble for plotting, but not affect optimization3637#' Use random portfolio engine38SortinoResult<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)39plot(SortinoResult, risk.col='SortinoRatio')4041#' Alternately, Use DEoptim engine42#SortinoResultDE<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='DEoptim', search_size=2000, trace=TRUE, verbose=FALSE,strategy=6, parallel=TRUE) #itermax=55, CR=0.99, F=0.5,43#plot(SortinoResultDE, risk.col='SortinoRatio')4445#' Now rebalance quarterly46SortinoRebalance <- optimize.portfolio.rebalancing(R=indexes[,1:4], constraints=SortinoConstr, optimize_method="random", trace=TRUE, rebalance_on='quarters', trailing_periods=NULL, training_period=36, search_size=2000)4748###############################################################################49# R (http://r-project.org/) Numeric Methods for Optimization of Portfolios50#51# Copyright (c) 2004-2014 Kris Boudt, Peter Carl and Brian G. Peterson52#53# This library is distributed under the terms of the GNU Public License (GPL)54# for full details see the file COPYING55#56# $Id$57#58###############################################################################5960