New Software: Rmetrics AMPL Interface

"Rmetrics2AMPL" is a project under development to make professional solvers available for portfolio design and optimization with R/Rmetrics. Rmetrics2AMPL provides functions and tools to create and extend a library of model, data, and run files for the AMPL environment, a comprehensive and powerful algebraic modeling language for linear and nonlinear optimization problems, in discrete or continuous variables.

This approach has many advantages compared to the use of the quadprog, Rsocp, Rglpk, Rsymphony, Rsolnp, or Rcplex contributed R packages for optimization. You write a solver R interface only once for a specific portfolio model and then you can use it for any optimizer which provides an AMPL interface. This makes life more easier and much more productive.

The library has already implemented many AMPL model and run files for linear, quadratic, and non-linear portfolio problems including mixed integer constraints. For these models we have tested several open source and commercial solvers. These include

  • Open source: Coin-or infrastructure including ipopt, symphony, bonmin, and couenne.
  • Commercial: cplex, donlp2, gurobi, loqo, minos, mosek, and snopt.

To get the idea how the interface works we give an example how to solve the "long only" mean variance Markowitz portfolio problem with AMPL and the Coin-or solver ipopt.

> # PORTFOLIO:
> Data <- portfolioData(data=100*LPP2005.RET[, 1:6])
> Spec <- portfolioSpec()
> setTargetReturn(Spec) <- mean(getSeries(Data))
> Constraints <- "LongOnly"
> 
> # CREATE AMPL BOX CONSTRAINED MODEL:
> model = c(                                                            
+     "param nAssets;",                                               
+     "param mu{1..nAssets};",                                        
+     "param Sigma{1..nAssets, 1..nAssets};",                    
+     "param targetReturn;",   
+     "param minW{1..nAssets};",
+     "param maxW{1..nAssets};",   
+     "set Weights = 1..nAssets;",                                                                                              
+     "var w{1..nAssets} >= 0;",                                                                                                 
+     "minimize Risk: sum {i in 1..nAssets} sum{j in 1..nAssets} w[i]*Sigma[i,j]*w[j];",                                                             
+     "s.t. Return: sum{i in 1..nAssets} mu[i]*w[i] = targetReturn;",                
+     "s.t. Budget: sum{i in 1..nAssets} w[i] = 1;" )
> amplModel(model, "longonly.mod")  
> 
> # CREATE AMPL DATA FILE:
> amplDataOpen("longonly.dat")
> amplDataValue(name="nAssets", value=getNAssets(Data), "longonly.dat")
> amplDataVector(name="mu", vector=getMu(Data), "longonly.dat")
> amplDataMatrix(name="Sigma", matrix=getSigma(Data), "longonly.dat")
> amplDataValue(name="targetReturn", value=getTargetReturn(Spec), "longonly.dat")   
>  
> # CREATE AMPL RUN FILE:
> run = c( 
+     "reset;",              
+     "option solver ipopt ;"  ,
+     "model longonly.mod ;"  ,   
+     "data longonly.dat ;"  ,        
+     "solve;"      ,           
+     "display w > longonly.txt ;",
+     "display solve_exitcode >> longonly.txt ;",
+     "exit;" ) 
> amplRun(run, "longonly.run")
>  
> # AMPL:
> system("ampl longonly.run", show.output.on.console=FALSE)
> cat(readLines("longonly.txt"), sep = "\n")
w [*] :=
1  3.96219e-08
2  0.0086407
3  0.254318
4  0.335778
5  0
6  0.401263
;
solve_exitcode = 0

The function amplModel() creates the AMPL model file, the functions amplData[Foo]() the AMPL conform data file, and the function amplRun() creates the AMPL run file. Furthermore the AMPL interface provides functions solveRampl[foo] which can be used for the fPortfolio environment in the same easy way as for example the functions solveRquadprog or solveRglpk.

Any Questions? Do not hesitate to contact us or come to the Meielisalp 2010 Workshop to meet the authors.