Serving the Quantitative Finance Community

 
User avatar
Hansi
Topic Author
Posts: 41
Joined: January 25th, 2010, 11:47 am

R optimization

January 19th, 2011, 9:59 am

I'm working on a R optimazation but am a bit hindered by the fact that I'm not that good with R and haven't done a proper optimization for two years.First off does any one know any good reference material with explicit detailed examples for R optimization that works for a rather complex function and numerous restrictions. Simplex might work though, not quite sure atm.Secondly how would you approach something like this: I have a set of items with 3 category labels, 1 binary variable for +/- values, 1 numeric value. The solution needs to be a set of numerbers for one of the categories where they are split for the +/- binary so that you have two solveable numbers for each category.Might make more sense with the data like this:Items:Item, Category 1, Category 2, Category 3, IsPlus, NumberNR1, Colour, Red, Ball, 1, 0.1NR2, Colour, Red, Jacket, 0, 0.2NR3, Colour, Red, Pen, 1, 0.2NR4, Colour, Blue, Ball, 1, 0.2NR5, Colour, Blue, Pen, 0, 0.1NR6, Soda, Red, Ball, 1, 0.2NR7, Soda, Blue, Ball, 0, 0.5So basically any permutation of the categories is possible and the items are unique on the IsPlus they can only be either plus or minus. Numbers are only a factor of the item.I need to optimize based on category 2 and whether it's plus or minus. So let's say all Reds that have IsPlus==1 get one number, all Reds that have IsPlus==2 have another.So the x is something like:Category 2,PlusValue,MinusValueRed,0.1234,0.2345Blue,0.3456,0.4567My restrictions are: - The sum of (abs(PlusValue-MinusValue)) for Category 1 must be less than a%- The sum of (abs(PlusValue-MinusValue)) for Category 2 must be less than b%- The sum of (abs(PlusValue-MinusValue)) for Category 3 must be less than c%- The sum of (abs(PlusValue*Number-MinusValue*Number)) must be less than d%- The sum of (abs(PlusValue+MinusValue)) independant of Category must be less than e%- The sum of (abs(PlusValue-MinusValue)) independant of Category must be less than f%- x values can only be between g% and h%Each Category 2 can be mapped to a single Category 1 so that can simplify things for the restriction matrix but since each category 2 maps to multiple category 3 values there is a problem. I can easily figure out most of the stuff except for that one to many relationship.Anyone have any ideas as to how this would work?
 
User avatar
Hansi
Topic Author
Posts: 41
Joined: January 25th, 2010, 11:47 am

R optimization

January 19th, 2011, 1:30 pm

Bleh, just rewrote the function to penalize for the restrictions and then used L-BFGS with finite differences to work out the hessian. Works now, still might be interesting if anyone has an alternative way in mind, since the L-BFGS does blow up sometimes. I think it happens at the edges of the FD mesh but haven't bothered debugging, it's just a blind guess.
 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

R optimization

January 19th, 2011, 7:39 pm

If the constraints are box constraints (a.k.a. bounds), then you can also use L-BFGS-B, http://hosho.ees.hokudai.ac.jp/~kubo/Rd ... htmlThough it seems you need finite function values for it: https://stat.ethz.ch/pipermail/r-help/2 ... 74.htmlSee also slides "Constrained Optimization" -- http://www.stats.ox.ac.uk/~ruth/RCourse/Rcourse5.pdf -- which mention:"optim() with method=?L-BFGS-B? will accept box constraintsnlminb is an alternative function allowing box constraintsconstrOptim is a wrapper for optim which enforces linearinequality constraints by adding a barrier function: can usewith any optim method except ?L-BFGS-B?"One good thing to go through (to check if an algo you need is already implemented) is CRAN Task View: Optimization and Mathematical Programming -- http://cran.r-project.org/web/views/Opt ... on.htmlE.g. (from the above view) for general nonlinear constraints there's a wrapper Rdonlp2 (DONLP2 that it wraps has some weird license, so I haven't used it personally):http://arumat.net/Rdonlp2/https://svn.r ... ONLP2/This is a nice guide for optimization algos in general:http://plato.la.asu.edu/guide.htmlThere's plenty of implementations for C/C++ available, see GSL (well-known), OOL, O2scl and NLopt:http://ool.sourceforge.net/http://ab-in ... Algorithms -- some allow for non-linear constraintshttp://o2scl.sourceforge.net/o2scl/html/index.html#conmin_section// Apparently, you can call NLopt from R! http://ab-initio.mit.edu/wiki/index.php ... nceAnother way you can proceed is to find an implementation in C/C++ and write your own wrapper to use it in R.Rcpp -- http://dirk.eddelbuettel.com/code/rcpp.html -- might be of help for this; the author even provides RcppGSL as an example: http://dirk.eddelbuettel.com/code/rcpp.gsl.html
 
User avatar
Hansi
Topic Author
Posts: 41
Joined: January 25th, 2010, 11:47 am

R optimization

January 19th, 2011, 7:49 pm

Yeah optim with L-BFGS-B was what I wound up using. Thanks for the links though, marked some of them in case they might help in the future.
Last edited by Hansi on January 18th, 2011, 11:00 pm, edited 1 time in total.
 
User avatar
pnrodriguez
Posts: 1
Joined: December 19th, 2008, 1:12 pm

R optimization

January 19th, 2011, 11:03 pm

Hansi,There are several good packages in R for optimization. For your case, I would use "DEoptim" and use your restrictions as penalties in the objective function. Hope this helps!