<div dir="ltr">Peter,<div><br></div><div>Thanks for the note. The error is because you ran optimize.portfolio without trace=TRUE so the returns object, R, is not available. I'll add a check to the function that will throw an error with a more informative message if the R object is missing.</div>
<div><br></div><div style>Thanks,</div><div style>Ross</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Sep 17, 2013 at 6:53 PM, Peter Carl <span dir="ltr"><<a href="mailto:peter@braverock.com" target="_blank">peter@braverock.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Ross,<br>
<br>
You might already know about this, given that I'm a day behind on svn.<br>
Sorry for the lack of detail, I can do more debugging if you need.<br>
<br>
> R = edhec[,c("Convertible Arbitrage", "Equity Market Neutral","Fixed<br>
Income Arbitrage", "Event Driven", "CTA Global", "Global Macro",<br>
"Long/Short Equity")]<br>
> init.portf <- portfolio.spec(assets=colnames(R))<br>
> init.portf <- add.constraint(portfolio=init.portf,<br>
+                              type="leverage",<br>
+                              min_sum=1,<br>
+                              max_sum=1)<br>
> # Add box constraint<br>
> init.portf <- add.constraint(portfolio=init.portf,<br>
+                              type="box",<br>
+                              min=0.05,<br>
+                              max=0.3)<br>
><br>
> # Add group constraint<br>
> init.portf <- add.constraint(portfolio=init.portf, type="group",<br>
+                   groups=list(c(1:4),<br>
+                               c(5:7)),<br>
+                   group_min=c(0.1,.05),<br>
+                   group_max=c(0.85,0.75)<br>
+ )<br>
> #Add measure 1, annualized return<br>
> init.portf <- add.objective(portfolio=init.portf,<br>
+                             type="return", # the kind of objective this is<br>
+                             name="mean", # name of the function<br>
+                             enabled=TRUE, # enable or disable the objective<br>
+                             multiplier=0 # calculate it but don't use it<br>
in the objective<br>
+ )<br>
><br>
> # Add measure 2, annualized standard deviation<br>
> init.portf <- add.objective(portfolio=init.portf,<br>
+                             type="risk", # the kind of objective this is<br>
+                             name="var", # to minimize from the sample<br>
+                             enabled=TRUE, # enable or disable the objective<br>
+                             multiplier=0 # calculate it but don't use it<br>
in the objective<br>
+ )<br>
><br>
> # Add measure 3, ES<br>
> init.portf <- add.objective(portfolio=init.portf,<br>
+                             type="risk", # the kind of objective this is<br>
+                             name="ES", # the function to minimize<br>
+                             enabled=FALSE, # enable or disable the<br>
objective<br>
+                             multiplier=0, # calculate it but don't use<br>
it in the objective<br>
+                             arguments=list(p=p)<br>
+ )<br>
> MeanVar.portf <- init.portf<br>
# Turn back on the return and sd objectives<br>
> MeanVar.portf$objectives[[1]]$multiplier = -1 # mean<br>
> MeanVar.portf$objectives[[2]]$multiplier = 1 # var<br>
> MeanVar.ROI<-optimize.portfolio(R=R,<br>
+   portfolio=MeanVar.portf,<br>
+   optimize_method='ROI'<br>
+ )<br>
> summary(MeanVar.ROI)<br>
**************************************************<br>
PortfolioAnalytics Optimization Summary<br>
**************************************************<br>
<br>
Call:<br>
optimize.portfolio(R = R, portfolio = MeanVar.portf, optimize_method = "ROI")<br>
<br>
Optimal Weights:<br>
 Convertible Arbitrage  Equity Market Neutral Fixed Income Arbitrage<br>
    Event Driven             CTA Global           Global Macro<br>
                0.0500                 0.0500                 0.0500<br>
          0.2134                 0.0500<br>
0.3000<br>
     Long/Short Equity<br>
                0.2866<br>
<br>
Objective Measures:<br>
    mean<br>
0.007309<br>
<br>
<br>
 StdDev<br>
0.01561<br>
<br>
<br>
<br>
Portfolio Assets and Initial Weights:<br>
 Convertible Arbitrage  Equity Market Neutral Fixed Income Arbitrage<br>
    Event Driven             CTA Global           Global Macro<br>
             0.1428571              0.1428571              0.1428571<br>
       0.1428571              0.1428571<br>
0.1428571<br>
     Long/Short Equity<br>
             0.1428571<br>
<br>
**************************************************<br>
PortfolioAnalytics Portfolio Specification<br>
**************************************************<br>
<br>
Call:<br>
portfolio.spec(assets = colnames(R))<br>
<br>
Assets<br>
Number of assets: 7<br>
<br>
Asset Names<br>
[1] "Convertible Arbitrage"  "Equity Market Neutral"  "Fixed Income<br>
Arbitrage" "Event Driven"           "CTA Global"<br>
[6] "Global Macro"           "Long/Short Equity"<br>
<br>
Constraints<br>
Number of constraints: 3<br>
Number of enabled constraints: 3<br>
Enabled constraint types<br>
                - leverage<br>
                - box<br>
                - group<br>
Number of disabled constraints: 0<br>
<br>
Objectives<br>
Number of objectives: 3<br>
Number of enabled objectives: 2<br>
Enabled objective names<br>
                - mean<br>
                - var<br>
Number of disabled objectives: 1<br>
Disabled objective types<br>
                - ES<br>
<br>
****************************************<br>
Constraints<br>
****************************************<br>
Leverage Constraint:<br>
min_sum = 1<br>
max_sum = 1<br>
<br>
Box Constraints:<br>
min:<br>
 Convertible Arbitrage  Equity Market Neutral Fixed Income Arbitrage<br>
    Event Driven             CTA Global           Global Macro<br>
                  0.05                   0.05                   0.05<br>
            0.05                   0.05<br>
 0.05<br>
     Long/Short Equity<br>
                  0.05<br>
max:<br>
 Convertible Arbitrage  Equity Market Neutral Fixed Income Arbitrage<br>
    Event Driven             CTA Global           Global Macro<br>
                   0.3                    0.3                    0.3<br>
             0.3                    0.3<br>
   0.3<br>
     Long/Short Equity<br>
                   0.3<br>
<br>
Group Constraints:<br>
Groups:<br>
$group1<br>
[1] 1 2 3 4<br>
<br>
$group2<br>
[1] 5 6 7<br>
<br>
<br>
Lower bound on group weights, group_min:<br>
group1 group2<br>
  0.10   0.05<br>
<br>
Upper bound on group weights, group_max:<br>
group1 group2<br>
  0.85   0.75<br>
<br>
Group position limits, group_pos:<br>
NULL<br>
<br>
Group Weights:<br>
   group1    group2<br>
0.3633993 0.6366007<br>
<br>
Position Limit Constraints:<br>
Maximum number of non-zero weights, max_pos:<br>
NULL<br>
Realized number of non-zero weights (i.e. positions):<br>
[1] 7<br>
<br>
Maximum number of long positions, max_pos_long:<br>
NULL<br>
Realized number of long positions:<br>
[1] 7<br>
<br>
Maximum number of short positions, max_pos_short:<br>
NULL<br>
Realized number of short positions:<br>
[1] 0<br>
<br>
<br>
Diversification Target Constraint:<br>
NULL<br>
<br>
Realized diversification:<br>
[1] 0.7723208<br>
<br>
Turnover Target Constraint:<br>
NULL<br>
<br>
Realized turnover from initial weights:<br>
[1] 0.1061224<br>
<br>
****************************************<br>
Objectives<br>
****************************************<br>
<br>
Objective: return_objective<br>
$name<br>
[1] "mean"<br>
<br>
$target<br>
NULL<br>
<br>
$arguments<br>
list()<br>
<br>
$enabled<br>
[1] TRUE<br>
<br>
$multiplier<br>
[1] -1<br>
<br>
$call<br>
add.objective(portfolio = init.portf, type = "return", name = "mean",<br>
    enabled = TRUE, multiplier = 0)<br>
<br>
attr(,"class")<br>
[1] "return_objective" "objective"<br>
<br>
****************************************<br>
Objective: portfolio_risk_objective<br>
$name<br>
[1] "var"<br>
<br>
$target<br>
NULL<br>
<br>
$arguments<br>
$arguments$portfolio_method<br>
[1] "single"<br>
<br>
<br>
$enabled<br>
[1] TRUE<br>
<br>
$multiplier<br>
[1] 1<br>
<br>
$call<br>
add.objective(portfolio = init.portf, type = "risk", name = "var",<br>
    enabled = TRUE, multiplier = 0)<br>
<br>
attr(,"class")<br>
[1] "portfolio_risk_objective" "objective"<br>
<br>
****************************************<br>
Objective: portfolio_risk_objective<br>
$name<br>
[1] "ES"<br>
<br>
$target<br>
NULL<br>
<br>
$arguments<br>
$arguments$p<br>
[1] 0.9166667<br>
<br>
$arguments$portfolio_method<br>
[1] "single"<br>
<br>
<br>
$enabled<br>
[1] FALSE<br>
<br>
$multiplier<br>
[1] 0<br>
<br>
$call<br>
add.objective(portfolio = init.portf, type = "risk", name = "ES",<br>
    arguments = list(p = p), enabled = FALSE, multiplier = 0)<br>
<br>
attr(,"class")<br>
[1] "portfolio_risk_objective" "objective"<br>
<br>
****************************************<br>
<br>
Elapsed Time:<br>
Time difference of 0.5688059 secs<br>
<br>
> plot(MeanVar.ROI)<br>
Error in apply(R, 2, "mean") : dim(X) must have a positive length<br>
> traceback()<br>
10: stop("dim(X) must have a positive length")<br>
9: apply(R, 2, "mean")<br>
8: as.vector(apply(R, 2, "mean"))<br>
7: matrix(as.vector(apply(R, 2, "mean")), ncol = 1)<br>
6: moments(R)<br>
5: applyFUN(R = R, weights = rp, FUN = return.col, ... = ...)<br>
4: chart.Scatter.ROI(object = ROI, rp = rp, return.col = return.col,<br>
       risk.col = risk.col, ..., chart.assets = chart.assets,<br>
element.color = element.color,<br>
       cex.axis = cex.axis, main = main, xlim = xlim, ylim = ylim)<br>
3: charts.ROI(ROI = x, rp = rp, risk.col = risk.col, return.col = return.col,<br>
       chart.assets = chart.assets, main = main, xlim = xlim, ylim = ylim,<br>
       ...)<br>
2: plot.optimize.portfolio.ROI(MeanVar.ROI)<br>
1: plot(MeanVar.ROI)<br>
<br>
pcc<br>
<span class="HOEnZb"><font color="#888888">--<br>
Peter Carl<br>
<a href="http://www.braverock.com/peter" target="_blank">http://www.braverock.com/peter</a><br>
<br>
<br>
_______________________________________________<br>
GSoC-PortA mailing list<br>
<a href="mailto:GSoC-PortA@lists.r-forge.r-project.org">GSoC-PortA@lists.r-forge.r-project.org</a><br>
<a href="http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta" target="_blank">http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta</a><br>
</font></span></blockquote></div><br></div>