Re: [R] Optim with two constraints
AD == Alexis Diamond [EMAIL PROTECTED] writes: AD I have a follow-up from Jens's question and Professor Ripley's AD response. AD Jens wants to do quadratic optimization with 2 constraints: # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1, i.e. the elements in par need to sum to 1 AD how does one set both constraints in quadprog, per AD Prof. Ripley's suggestion? i know how to get quadprog to AD handle the second constraint, The first is actually not one constraint but 2*k constraints, where k is the number of elements in par. But there is quite some redundancy in this set of equation. It suffices to constrain each element to be bigger or equal to 0 and that they should sum to 1. If these constraints are fulfilled, then each element must be less or equal to one. AD but not BOTH, since quadprog only takes as inputs the AD constraint matrix A and constraint vector b-- So what stops you from coding the k constraints from 1.) in the form that quadprog requires them? From memory, i.e. untested: m - length(par) Amat - cbind(rep(1,m), diag(m)) bvec - c(1,rep(0,m)) meq - 1 solve.QP(Dmat, dvec, Amat, bvec, meq) AD unlike in ipop (kernlab), there is no additional option for AD box constraints. Well, the problems that I had (and still have) usually don't involve box constraints, but I see that other people use them again and again. So probably it would be a good idea to implement them... But, more importantly, would be to implement Powell's modifications of the Goldfarb-Idnani algorithm to make it numerically more robust... Oh, yeah, and a warm start option from a feasible point would be nice too Probably all in a future version which should be released sometime before Xmas 20xx. :) AD apologies if i am not seeing something obvious here. Apologies accepted. :) Cheers, Berwin __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Optim with two constraints
Alexis, WIKI: You create the box constraints with two inequality constraints for each element.Suppose that you have five elements, and your upper bound is .33, and your lower bound is 0. Then quadprog would require constraints as: A[1,]=(1,0,0,0,0)b=(0) A[2,]=(-1,0,0,0,0) b=(-.33) A[3,]=(0,1,0,0,0) b=(0) A[4,]=(0,-1,0,0,0) b=(-.33) .and so on. The syntax is not quite correct but you get the picture. Remember that quadprog distinguishes between equality and inequality constraints, and these must be inequality constraints. The trick to the upper bound is to multiply the constraint by -1 (as indicated), which effectively translates the constraint from a = constraint into the = type of constraint required by quadprog. Regards, Tom Alexis Diamond wrote: I have a follow-up from Jens's question and Professor Ripley's response. Jens wants to do quadratic optimization with 2 constraints: # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1, i.e. the elements in par need to sum to 1 how does one set both constraints in quadprog, per Prof. Ripley's suggestion? i know how to get quadprog to handle the second constraint, but not BOTH, since quadprog only takes as inputs the constraint matrix A and constraint vector b-- unlike in ipop (kernlab), there is no additional option for box constraints. apologies if i am not seeing something obvious here. thanks in advance, alexis -- Tom Wood Fort Mason Capital 456 Montgomery Street 22nd Floor San Francisco, CA 94104 Direct: 415-249-3387 Fax: 415-249-3389 [EMAIL PROTECTED] References 1. mailto:[EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Optim with two constraints
Hello, I have a follow-up from Jens's question and Professor Ripley's response. Jens wants to do quadratic optimization with 2 constraints: # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1, i.e. the elements in par need to sum to 1 how does one set both constraints in quadprog, per Prof. Ripley's suggestion? i know how to get quadprog to handle the second constraint, but not BOTH, since quadprog only takes as inputs the constraint matrix A and constraint vector b-- unlike in ipop (kernlab), there is no additional option for box constraints. apologies if i am not seeing something obvious here. thanks in advance, alexis On 10/19/05, Jens Hainmueller [EMAIL PROTECTED] wrote: -Ursprüngliche Nachricht- Von: Prof Brian Ripley [mailto:[EMAIL PROTECTED] Gesendet: Thursday, October 13, 2005 2:46 AM An: Jens Hainmueller Cc: r-help@stat.math.ethz.ch Betreff: Re: [R] Optim with two constraints This is actually quadratic programming, so why do you want to use optim()? There are packages specifically for QP, e.g. quadprog. A more general approach is to eliminate one variable, which gives you an inequality constrained problem in n-1 variables to which you could apply contrOptim(). Other re-parametrizations (e.g. of weights as a log-linear model) will work provided none of the parameters are going to be zero at the optimum (one cannot be one without all the others being zero). On Wed, 12 Oct 2005, Jens Hainmueller wrote: Hi R-list, I am new to optimization in R and would appreciate help on the following question. I would like to minimize the following function using two constraints: ## fn - function(par,H,F){ fval - 0.5 * t(par) %*% H %*% par + F%*% par fval } # matrix H is (n by k) # matrix F is (n by 1) # par is a (n by 1) set of weights # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1 i.e. the elements in par need to sum to 1 ## I try to use optim res - optim(c(runif(16),fn, method=L-BFGS-B, H=H, F=f ,control=list(fnscale=-1), lower=0, upper=1) ## If I understand this correctly, using L-BFGS-B with lower=0 and upper=1 should take care of constraint 1 (box constraints). What I am lacking is the skill to include constraint no 2. I guess I could solve this by reparametrization but I am not sure how exactly. I could not find (i.e. wasn't able to infer) the answer to this in the archives despite the many comments on optim and constrained optimization (sorry if I missed it there). I am using version 2.1.1 under windows XP. Thank you very much. Jens __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Optim with two constraints
This is actually quadratic programming, so why do you want to use optim()? There are packages specifically for QP, e.g. quadprog. A more general approach is to eliminate one variable, which gives you an inequality constrained problem in n-1 variables to which you could apply contrOptim(). Other re-parametrizations (e.g. of weights as a log-linear model) will work provided none of the parameters are going to be zero at the optimum (one cannot be one without all the others being zero). On Wed, 12 Oct 2005, Jens Hainmueller wrote: Hi R-list, I am new to optimization in R and would appreciate help on the following question. I would like to minimize the following function using two constraints: ## fn - function(par,H,F){ fval - 0.5 * t(par) %*% H %*% par + F%*% par fval } # matrix H is (n by k) # matrix F is (n by 1) # par is a (n by 1) set of weights # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1 i.e. the elements in par need to sum to 1 ## I try to use optim res - optim(c(runif(16),fn, method=L-BFGS-B, H=H, F=f ,control=list(fnscale=-1), lower=0, upper=1) ## If I understand this correctly, using L-BFGS-B with lower=0 and upper=1 should take care of constraint 1 (box constraints). What I am lacking is the skill to include constraint no 2. I guess I could solve this by reparametrization but I am not sure how exactly. I could not find (i.e. wasn't able to infer) the answer to this in the archives despite the many comments on optim and constrained optimization (sorry if I missed it there). I am using version 2.1.1 under windows XP. Thank you very much. Jens __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Optim with two constraints
Hi R-list, I am new to optimization in R and would appreciate help on the following question. I would like to minimize the following function using two constraints: ## fn - function(par,H,F){ fval - 0.5 * t(par) %*% H %*% par + F%*% par fval } # matrix H is (n by k) # matrix F is (n by 1) # par is a (n by 1) set of weights # I need two constraints: # 1. each element in par needs to be between 0 and 1 # 2. sum(par)=1 i.e. the elements in par need to sum to 1 ## I try to use optim res - optim(c(runif(16),fn, method=L-BFGS-B, H=H, F=f ,control=list(fnscale=-1), lower=0, upper=1) ## If I understand this correctly, using L-BFGS-B with lower=0 and upper=1 should take care of constraint 1 (box constraints). What I am lacking is the skill to include constraint no 2. I guess I could solve this by reparametrization but I am not sure how exactly. I could not find (i.e. wasn't able to infer) the answer to this in the archives despite the many comments on optim and constrained optimization (sorry if I missed it there). I am using version 2.1.1 under windows XP. Thank you very much. Jens __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html