Re: [R] Quadratic programming
Before going any further, I have to check, what is: MinQuadProg qp Also, if I'm following the C++ code correctly, H, is an identity matrix. This implies the input to the C++ solver, requires the QP in a different form to the R solver. In which case, the C++ inputs and the R inputs, should be different...? (A) It may be worthwhile comparing the solver output (for C++ and R) using a *much* simpler example. e.g. the examples from the quadprog package. (B) If they produce the same output (using a simple example), then that implies there's a difference in your inputs. So, you just need to work out which input values are different. Expanding on my previous post, just print them out. But check (A) above first. On Thu, Sep 24, 2020 at 11:15 PM Maija Sirkjärvi wrote: > > Thank you for giving me your time! > > The problem is the quadratic optimization part. Something goes wrong along > the way. In C++ loops run from 0 and in R they run from 1, and I've tried to > take that into account. Still I'm having hard time figuring out the mistake I > make, cause I get a result from my R code. It's just not the same that I get > with the C++. > > Here are the quadratic optimization parts for both codes. > > C++ > > /* Set Up Quadratic Programing Problem */ > Vector hSmooth(J); > for(int j=0; j Vector Q(J); > for(int j=0; j One) / (One + Eta)); > SymmetricMatrix H(J,Zero); > Vector c(J,Zero); > Matrix Aeq(0,J); > Vector beq(0); > Matrix Aneq(2*J-3,J,Zero); > Vector bneq(2*J-3); > Vector lb(J,-Inf); > Vector ub(J,Inf); > for(int j=0; j for(int j=0; j > for(int j=1; j { > Aneq(j-1,j-1) = -One; > Aneq(j-1,j) = One; > bneq[j-1] = Delta1; > } > for(int j=2; j { > Aneq(J-1+j-2,j) = -One / (Q(j) - Q(j-1)); > Aneq(J-1+j-2,j-1) = One / (Q(j) - Q(j-1)) + One / (Q(j-1) - Q(j-2)); > Aneq(J-1+j-2,j-2) = -One / (Q(j-1) - Q(j-2)); > bneq[J-1+j-2] = Delta2; > } > > /* Solve Constrained Optimization Problem Using Quadratic Programming */ > MinQuadProg qp(c,H,Aeq,beq,Aneq,bneq,lb,ub); > qp.PrintLevel = 0; > qp.Solve(); > > And R > > J <- length(Price) > hs <- numeric(J) > for(j in 1:J){ > hs[j] <-(-(gEst$KernelRegPartLin..Phi[j]^(-0.1))) > } > hs > > Q <- rep(0,J) > for(j in 1:(length(Price))){ > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > } > Q > plot(Q) > Dmat <- matrix(0,nrow= J, ncol=J) > diag(Dmat) <- 1 > dvec <- -hs > Aeq <- 0 > beq <- 0 > Amat <- matrix(0,J,2*J-3) > bvec <- rep(0,2*J-3) > > for(j in 2:nrow(Amat)){ > Amat[j-1,j-1] = -1 > Amat[j,j-1] = 1 > } > > for(j in 3:nrow(Amat)){ > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > } > > for(j in 2:nrow(bvec)) { > bvec[j-1] = Delta1 > } > for(j in 3:nrow(bvec)) { > bvec[J-1+j-2] = Delta2 > } > > solution <- solve.QP(Dmat,dvec,Amat,bvec) > > > > > > ke 23. syysk. 2020 klo 9.12 Abby Spurdle (spurdl...@gmail.com) kirjoitti: >> >> > I'm trying to replicate a C++ code with R. >> >> Notes: >> (1) I'd recommend you make the code more modular. >> i.e. One function for initial data prep/modelling, one function for >> setting up and solving the QP, etc. >> This should be easier to debug. >> (However, you would probably have to do it to the C++ code first). >> (2) Your R code is not completely reproducible. >> i.e. AEJData >> (3) For the purposes of a reproducible example, your code can be simplified. >> i.e. Only one contributed R package should be attached. >> >> Regardless of (1) above, you should be able to identify at what point >> the C++ and R code becomes inconsistent. >> The simplest approach is to add print-based functions into both the >> C++ and R code, and print out state data, at each major step. >> Then all you need to do is compare the output for both. >> >> > Is there a better/another package for these types of problems? >> >> I'm not sure. >> After a quick search, this is the best I found: >> >> scam::scam >> scam::shape.constrained.smooth.terms __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
Thank you for giving me your time! The problem is the quadratic optimization part. Something goes wrong along the way. In C++ loops run from 0 and in R they run from 1, and I've tried to take that into account. Still I'm having hard time figuring out the mistake I make, cause I get a result from my R code. It's just not the same that I get with the C++. Here are the quadratic optimization parts for both codes. C++ /* Set Up Quadratic Programing Problem */ Vector hSmooth(J); for(int j=0; j Q(J); for(int j=0; j H(J,Zero); Vector c(J,Zero); Matrix Aeq(0,J); Vector beq(0); Matrix Aneq(2*J-3,J,Zero); Vector bneq(2*J-3); Vector lb(J,-Inf); Vector ub(J,Inf); for(int j=0; j > I'm trying to replicate a C++ code with R. > > Notes: > (1) I'd recommend you make the code more modular. > i.e. One function for initial data prep/modelling, one function for > setting up and solving the QP, etc. > This should be easier to debug. > (However, you would probably have to do it to the C++ code first). > (2) Your R code is not completely reproducible. > i.e. AEJData > (3) For the purposes of a reproducible example, your code can be > simplified. > i.e. Only one contributed R package should be attached. > > Regardless of (1) above, you should be able to identify at what point > the C++ and R code becomes inconsistent. > The simplest approach is to add print-based functions into both the > C++ and R code, and print out state data, at each major step. > Then all you need to do is compare the output for both. > > > Is there a better/another package for these types of problems? > > I'm not sure. > After a quick search, this is the best I found: > > scam::scam > scam::shape.constrained.smooth.terms > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
> I'm trying to replicate a C++ code with R. Notes: (1) I'd recommend you make the code more modular. i.e. One function for initial data prep/modelling, one function for setting up and solving the QP, etc. This should be easier to debug. (However, you would probably have to do it to the C++ code first). (2) Your R code is not completely reproducible. i.e. AEJData (3) For the purposes of a reproducible example, your code can be simplified. i.e. Only one contributed R package should be attached. Regardless of (1) above, you should be able to identify at what point the C++ and R code becomes inconsistent. The simplest approach is to add print-based functions into both the C++ and R code, and print out state data, at each major step. Then all you need to do is compare the output for both. > Is there a better/another package for these types of problems? I'm not sure. After a quick search, this is the best I found: scam::scam scam::shape.constrained.smooth.terms __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
I really appreciate you helping me with this! I just don't seem to figure it out. (1) I don't know why you think bvec should be a matrix. The documentation clearly says it should be a vector (implying not a matrix). - I've written it in a form of a matrix with one row and 2*J-3 columns. (0,2*J-3,1). I thought that it would pass as a vector. I made it a vector now. The thing is that I'm trying to replicate a C++ code with R. The C++ code imposes shape restrictions on the function and works perfectly. The C++ code is attached and after that the same for R. You said you haven't used the QP package for a decade. Is there a better/another package for these types of problems? Thanks again! C++ code: int main() { Print("Begin"); /* Bootstrap Parameters */ long RandomSeed = -98345; // Set random seed const int S = 100; // Number of bootstrap replications /* Housing Demand Parameters */ const double Beta = 1.1613; const double v= 0.7837; const double Eta = -0.5140; /* Quadratic Programming Tolerance Parameters */ const double Delta1 = 0.01; const double Delta2 = 0.0001; /* Read in Dataset */ DataFileDelim d("K:\\Data\\Local Jurisdictions\\AEJ Data.dat",'\t'); d.SortBy("AfterTaxPrice"); int J = d.NumObs(); Vector Price = d.Get("AfterTaxPrice"); Vector Educ = d.Get("EducAll"); Vector Crime = d.Get("CrimeIndex"); Vector Dist = d.Get("RushTrav"); /* Adjust Crime Data */ for(int j=0; j Rank1(J); for(int j=0; j X(J); Matrix Y(J,2); Vector Z(J); for(int j=0; j RhoGrid1 = Grid(-1.2,-0.1,RhoK1); Matrix gTrans(J,RhoK1); for(int rk=0; rk hSmooth(J); for(int j=0; j Q(J); for(int j=0; j H(J,Zero); Vector c(J,Zero); Matrix Aeq(0,J); Vector beq(0); Matrix Aneq(2*J-3,J,Zero); Vector bneq(2*J-3); Vector lb(J,-Inf); Vector ub(J,Inf); for(int j=0; j I was wondering if you're trying to fit a curve, subject to > monotonicity/convexity constraints... > If you are, this is a challenging topic, best of luck... > > > On Tue, Sep 22, 2020 at 8:12 AM Abby Spurdle wrote: > > > > Hi, > > > > Sorry, for my rushed responses, last night. > > (Shouldn't post when I'm about to log out). > > > > I haven't used the quadprog package for nearly a decade. > > And I was hoping that an expert using optimization in finance in > > economics would reply. > > > > Some comments: > > (1) I don't know why you think bvec should be a matrix. The > > documentation clearly says it should be a vector (implying not a > > matrix). > > The only arguments that should be matrices are Dmat and Amat. > > (2) I'm having some difficulty following your quadratic program, even > > after rendering it. > > Perhaps you could rewrite your expressions, in a form that is > > consistent with the input to solve.QP. That's a math problem, not an R > > programming problem, as such. > > (3) If that fails, then you'll need to produce a minimal reproducible > example. > > I strongly recommend that the R code matches the quadratic program, as > > closely as possible. > > > > > > On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi > > wrote: > > > > > > Hi! > > > > > > I was wondering if someone could help me out. I'm minimizing a > following > > > function: > > > > > > \begin{equation} > > > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > > > \text{subject to} > > > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > > > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq > \frac{1}{Q_{j}-Q_{j-1}} > > > (m_{j-1}-m_{j})-\delta_{2} $$ > > > \end{equation} > > > > > > I have tried quadratic programming, but something is off. Does anyone > have > > > an idea how to approach this? > > > > > > Thanks in advance! > > > > > > Q <- rep(0,J) > > > for(j in 1:(length(Price))){ > > > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > > > } > > > > > > Dmat <- matrix(0,nrow= J, ncol=J) > > > diag(Dmat) <- 1 > > > dvec <- -hs > > > Aeq <- 0 > > > beq <- 0 > > > Amat <- matrix(0,J,2*J-3) > > > bvec <- matrix(0,2*J-3,1) > > > > > > for(j in 2:nrow(Amat)){ > > > Amat[j-1,j-1] = -1 > > > Amat[j,j-1] = 1 > > > } > > > for(j in 3:nrow(Amat)){ > > > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > > > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > > > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > > > } > > > for(j in 2:ncol(bvec)) { > > > bvec[j-1] = Delta1 > > > } > > > for(j in 3:ncol(bvec)) { > > > bvec[J-1+j-2] = Delta2 > > > } > > > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > > > > > [[alternative HTML version deleted]] > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
Re: [R] Quadratic programming
I was wondering if you're trying to fit a curve, subject to monotonicity/convexity constraints... If you are, this is a challenging topic, best of luck... On Tue, Sep 22, 2020 at 8:12 AM Abby Spurdle wrote: > > Hi, > > Sorry, for my rushed responses, last night. > (Shouldn't post when I'm about to log out). > > I haven't used the quadprog package for nearly a decade. > And I was hoping that an expert using optimization in finance in > economics would reply. > > Some comments: > (1) I don't know why you think bvec should be a matrix. The > documentation clearly says it should be a vector (implying not a > matrix). > The only arguments that should be matrices are Dmat and Amat. > (2) I'm having some difficulty following your quadratic program, even > after rendering it. > Perhaps you could rewrite your expressions, in a form that is > consistent with the input to solve.QP. That's a math problem, not an R > programming problem, as such. > (3) If that fails, then you'll need to produce a minimal reproducible example. > I strongly recommend that the R code matches the quadratic program, as > closely as possible. > > > On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi > wrote: > > > > Hi! > > > > I was wondering if someone could help me out. I'm minimizing a following > > function: > > > > \begin{equation} > > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > > \text{subject to} > > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq \frac{1}{Q_{j}-Q_{j-1}} > > (m_{j-1}-m_{j})-\delta_{2} $$ > > \end{equation} > > > > I have tried quadratic programming, but something is off. Does anyone have > > an idea how to approach this? > > > > Thanks in advance! > > > > Q <- rep(0,J) > > for(j in 1:(length(Price))){ > > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > > } > > > > Dmat <- matrix(0,nrow= J, ncol=J) > > diag(Dmat) <- 1 > > dvec <- -hs > > Aeq <- 0 > > beq <- 0 > > Amat <- matrix(0,J,2*J-3) > > bvec <- matrix(0,2*J-3,1) > > > > for(j in 2:nrow(Amat)){ > > Amat[j-1,j-1] = -1 > > Amat[j,j-1] = 1 > > } > > for(j in 3:nrow(Amat)){ > > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > > } > > for(j in 2:ncol(bvec)) { > > bvec[j-1] = Delta1 > > } > > for(j in 3:ncol(bvec)) { > > bvec[J-1+j-2] = Delta2 > > } > > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
Hi, Sorry, for my rushed responses, last night. (Shouldn't post when I'm about to log out). I haven't used the quadprog package for nearly a decade. And I was hoping that an expert using optimization in finance in economics would reply. Some comments: (1) I don't know why you think bvec should be a matrix. The documentation clearly says it should be a vector (implying not a matrix). The only arguments that should be matrices are Dmat and Amat. (2) I'm having some difficulty following your quadratic program, even after rendering it. Perhaps you could rewrite your expressions, in a form that is consistent with the input to solve.QP. That's a math problem, not an R programming problem, as such. (3) If that fails, then you'll need to produce a minimal reproducible example. I strongly recommend that the R code matches the quadratic program, as closely as possible. On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi wrote: > > Hi! > > I was wondering if someone could help me out. I'm minimizing a following > function: > > \begin{equation} > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > \text{subject to} > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq \frac{1}{Q_{j}-Q_{j-1}} > (m_{j-1}-m_{j})-\delta_{2} $$ > \end{equation} > > I have tried quadratic programming, but something is off. Does anyone have > an idea how to approach this? > > Thanks in advance! > > Q <- rep(0,J) > for(j in 1:(length(Price))){ > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > } > > Dmat <- matrix(0,nrow= J, ncol=J) > diag(Dmat) <- 1 > dvec <- -hs > Aeq <- 0 > beq <- 0 > Amat <- matrix(0,J,2*J-3) > bvec <- matrix(0,2*J-3,1) > > for(j in 2:nrow(Amat)){ > Amat[j-1,j-1] = -1 > Amat[j,j-1] = 1 > } > for(j in 3:nrow(Amat)){ > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > } > for(j in 2:ncol(bvec)) { > bvec[j-1] = Delta1 > } > for(j in 3:ncol(bvec)) { > bvec[J-1+j-2] = Delta2 > } > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
Thank you for your response! Bvec is supposed to be a matxit. I'm following the solve.QP ( https://www.rdocumentation.org/packages/quadprog/versions/1.5-8/topics/solve.QP ). I'm not sure what would be the best way to solve a quadratic programming problem in R. ma 21. syysk. 2020 klo 13.20 Abby Spurdle (spurdl...@gmail.com) kirjoitti: > One more thing, is bvec supposed to be a matrix? > > Note you may need to provide a reproducible example, for better help... > > On Mon, Sep 21, 2020 at 10:09 PM Abby Spurdle wrote: > > > > Sorry, ignore the last part. > > What I should have said, is the inequality has the opposite sign. > > >= bvec (not <= bvec) > > > > > > On Mon, Sep 21, 2020 at 10:05 PM Abby Spurdle > wrote: > > > > > > Are you using the quadprog package? > > > If I can take a random shot in the dark, should bvec be -bvec? > > > > > > > > > On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi > > > wrote: > > > > > > > > Hi! > > > > > > > > I was wondering if someone could help me out. I'm minimizing a > following > > > > function: > > > > > > > > \begin{equation} > > > > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > > > > \text{subject to} > > > > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > > > > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq > \frac{1}{Q_{j}-Q_{j-1}} > > > > (m_{j-1}-m_{j})-\delta_{2} $$ > > > > \end{equation} > > > > > > > > I have tried quadratic programming, but something is off. Does > anyone have > > > > an idea how to approach this? > > > > > > > > Thanks in advance! > > > > > > > > Q <- rep(0,J) > > > > for(j in 1:(length(Price))){ > > > > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > > > > } > > > > > > > > Dmat <- matrix(0,nrow= J, ncol=J) > > > > diag(Dmat) <- 1 > > > > dvec <- -hs > > > > Aeq <- 0 > > > > beq <- 0 > > > > Amat <- matrix(0,J,2*J-3) > > > > bvec <- matrix(0,2*J-3,1) > > > > > > > > for(j in 2:nrow(Amat)){ > > > > Amat[j-1,j-1] = -1 > > > > Amat[j,j-1] = 1 > > > > } > > > > for(j in 3:nrow(Amat)){ > > > > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > > > > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > > > > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > > > > } > > > > for(j in 2:ncol(bvec)) { > > > > bvec[j-1] = Delta1 > > > > } > > > > for(j in 3:ncol(bvec)) { > > > > bvec[J-1+j-2] = Delta2 > > > > } > > > > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > __ > > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
One more thing, is bvec supposed to be a matrix? Note you may need to provide a reproducible example, for better help... On Mon, Sep 21, 2020 at 10:09 PM Abby Spurdle wrote: > > Sorry, ignore the last part. > What I should have said, is the inequality has the opposite sign. > >= bvec (not <= bvec) > > > On Mon, Sep 21, 2020 at 10:05 PM Abby Spurdle wrote: > > > > Are you using the quadprog package? > > If I can take a random shot in the dark, should bvec be -bvec? > > > > > > On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi > > wrote: > > > > > > Hi! > > > > > > I was wondering if someone could help me out. I'm minimizing a following > > > function: > > > > > > \begin{equation} > > > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > > > \text{subject to} > > > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > > > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq \frac{1}{Q_{j}-Q_{j-1}} > > > (m_{j-1}-m_{j})-\delta_{2} $$ > > > \end{equation} > > > > > > I have tried quadratic programming, but something is off. Does anyone have > > > an idea how to approach this? > > > > > > Thanks in advance! > > > > > > Q <- rep(0,J) > > > for(j in 1:(length(Price))){ > > > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > > > } > > > > > > Dmat <- matrix(0,nrow= J, ncol=J) > > > diag(Dmat) <- 1 > > > dvec <- -hs > > > Aeq <- 0 > > > beq <- 0 > > > Amat <- matrix(0,J,2*J-3) > > > bvec <- matrix(0,2*J-3,1) > > > > > > for(j in 2:nrow(Amat)){ > > > Amat[j-1,j-1] = -1 > > > Amat[j,j-1] = 1 > > > } > > > for(j in 3:nrow(Amat)){ > > > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > > > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > > > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > > > } > > > for(j in 2:ncol(bvec)) { > > > bvec[j-1] = Delta1 > > > } > > > for(j in 3:ncol(bvec)) { > > > bvec[J-1+j-2] = Delta2 > > > } > > > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > > > > > [[alternative HTML version deleted]] > > > > > > __ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
Sorry, ignore the last part. What I should have said, is the inequality has the opposite sign. >= bvec (not <= bvec) On Mon, Sep 21, 2020 at 10:05 PM Abby Spurdle wrote: > > Are you using the quadprog package? > If I can take a random shot in the dark, should bvec be -bvec? > > > On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi > wrote: > > > > Hi! > > > > I was wondering if someone could help me out. I'm minimizing a following > > function: > > > > \begin{equation} > > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > > \text{subject to} > > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq \frac{1}{Q_{j}-Q_{j-1}} > > (m_{j-1}-m_{j})-\delta_{2} $$ > > \end{equation} > > > > I have tried quadratic programming, but something is off. Does anyone have > > an idea how to approach this? > > > > Thanks in advance! > > > > Q <- rep(0,J) > > for(j in 1:(length(Price))){ > > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > > } > > > > Dmat <- matrix(0,nrow= J, ncol=J) > > diag(Dmat) <- 1 > > dvec <- -hs > > Aeq <- 0 > > beq <- 0 > > Amat <- matrix(0,J,2*J-3) > > bvec <- matrix(0,2*J-3,1) > > > > for(j in 2:nrow(Amat)){ > > Amat[j-1,j-1] = -1 > > Amat[j,j-1] = 1 > > } > > for(j in 3:nrow(Amat)){ > > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > > } > > for(j in 2:ncol(bvec)) { > > bvec[j-1] = Delta1 > > } > > for(j in 3:ncol(bvec)) { > > bvec[J-1+j-2] = Delta2 > > } > > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
Are you using the quadprog package? If I can take a random shot in the dark, should bvec be -bvec? On Mon, Sep 21, 2020 at 9:28 PM Maija Sirkjärvi wrote: > > Hi! > > I was wondering if someone could help me out. I'm minimizing a following > function: > > \begin{equation} > $$\sum_{j=1}^{J}(m_{j} -\hat{m_{j}})^2,$$ > \text{subject to} > $$m_{j-1}\leq m_{j}-\delta_{1}$$ > $$\frac{1}{Q_{j-1}-Q_{j-2}} (m_{j-2}-m_{j-1}) \leq \frac{1}{Q_{j}-Q_{j-1}} > (m_{j-1}-m_{j})-\delta_{2} $$ > \end{equation} > > I have tried quadratic programming, but something is off. Does anyone have > an idea how to approach this? > > Thanks in advance! > > Q <- rep(0,J) > for(j in 1:(length(Price))){ > Q[j] <- exp((-0.1) * (Beta *Price[j]^(Eta + 1) - 1) / (1 + Eta)) > } > > Dmat <- matrix(0,nrow= J, ncol=J) > diag(Dmat) <- 1 > dvec <- -hs > Aeq <- 0 > beq <- 0 > Amat <- matrix(0,J,2*J-3) > bvec <- matrix(0,2*J-3,1) > > for(j in 2:nrow(Amat)){ > Amat[j-1,j-1] = -1 > Amat[j,j-1] = 1 > } > for(j in 3:nrow(Amat)){ > Amat[j,J+j-3] = -1/(Q[j]-Q[j-1]) > Amat[j-1,J+j-3] = 1/(Q[j]-Q[j-1]) > Amat[j-2,J+j-3] = -1/(Q[j-1]-Q[j-2]) > } > for(j in 2:ncol(bvec)) { > bvec[j-1] = Delta1 > } > for(j in 3:ncol(bvec)) { > bvec[J-1+j-2] = Delta2 > } > solution <- solve.QP(Dmat,dvec,Amat,bvec=bvec) > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
On 19/07/2015 4:58 PM, Preetam Pal wrote: Hi, If i have a quadratic objective function with a system of linear constraints for multiple variables, is there any inbuilt function that i can use? Google says the quadprog package should help. Duncan Murdoch __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] QUADRATIC PROGRAMMING
We don't do homework on this list: also, this is basic calculus, not quadratic programming. Best, Michael On Jul 24, 2012, at 1:10 PM, Joel Muli joe...@yahoo.com wrote: hi, what code in R would I use to solve the problem below? An apartment complex has 250 apartments to rent.If they rent x apartments then their monthly profit is given by: p(x)= -8^x^2 + 3200x -8 Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quadratic programming-maximization instead of
Maximizing f(x) = x'Ax makes sense only when A is negative-definite. Therefore, this is the same as minimizing x'Bx, where B = -A, and B is positive-definite. In other words, you should be able to simply flip the sign of the original matrix . This should yield a positive-definite matrix since the original matrix ought to be negative-definite. Ravi --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quadratic programming-maximization instead of minization
Hi Riccardo, Would it be possible to use max(diag(D))*diag(ncol(D)) - D ? That also reverses the order of eigenvalues/-vectors. Cheers, Tsjerk On Jan 2, 2012 4:35 PM, riccardo24 riccardo.giacome...@gmail.com wrote: Hi, I need to maximize a quadratic function under constraints in R. For minimization I used solve.QP but for maximization it is not useful since the matrix D of the quadratic function should be positive definite hence I cannot simply change the sign. any suggestion ? thanks -- View this message in context: http://r.789695.n4.nabble.com/quadratic-programming-maximization-instead-of-minization-tp4253011p4253011.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quadratic programming-maximization instead of minization
Sorry, that should've been sum(diag(D)) or max(eigen(D)$values) in stead of max(diag(D)). Tsjerk On Jan 3, 2012 4:52 PM, Tsjerk Wassenaar tsje...@gmail.com wrote: Hi Riccardo, Would it be possible to use max(diag(D))*diag(ncol(D)) - D ? That also reverses the order of eigenvalues/-vectors. Cheers, Tsjerk On Jan 2, 2012 4:35 PM, riccardo24 riccardo.giacome...@gmail.com wrote: Hi, I need to m... [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quadratic programming-maximization instead of minization
I don't have experience with this in R and I'm not sure I understand the question that well but maybe something like nearPD()? Ken Hutchison On Jan 2, 2012, at 6:36 AM, riccardo24 riccardo.giacome...@gmail.com wrote: Hi, I need to maximize a quadratic function under constraints in R. For minimization I used solve.QP but for maximization it is not useful since the matrix D of the quadratic function should be positive definite hence I cannot simply change the sign. any suggestion ? thanks -- View this message in context: http://r.789695.n4.nabble.com/quadratic-programming-maximization-instead-of-minization-tp4253011p4253011.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming with semi-definite matrix
Andreas Jensen bentrevisor at gmail.com writes: Hello. I'm trying to solve a quadratic programming problem of the form min ||Hx - y||^2 s.t. x = 0 and x = t using solve.QP in the quadprog package but I'm having problems with Dmat not being positive definite, which is kinda okay since I expect it to be numerically semi-definite in most cases. As far as I'm aware the problem arises because the Goldfarb and Idnani method first solves the unconstrained problem requiring a positive definite matrix. Are there any (fast) packages that allows me to do QP with (large) semidefinite matrices? Example: t - 100 y - signalConvNoisy[1,] D - crossprod(H,H) d - crossprod(H,y) A - cbind(rep(-1, nrow(H)), diag(ncol(H))) b0 - c(t, rep(0, ncol(H))) sol - solve.QP(Dmat=D, dvec = d, Amat = A, bvec = b0)$solution Error in solve.QP(Dmat = D, dvec = d, Amat = A, bvec = b0) : matrix D in quadratic function is not positive definite! Thanks in advance, Andreas Jensen See the Optimization task view, there are several packages that promise to handle quadratic programming problems. You may also try one of the nonlinear optimization packages, they are not much slower nowadays. Your code cannot be executed as signalConvNoisy is unknown. Hans Werner __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quadratic programming
Hi, The CRAN Task View on Optimization may help: http://stat.ethz.ch/CRAN/web/views/Optimization.html HTH, Stephan barbara.r...@uniroma1.it schrieb: Devo risolvere un problema di minimo vincolato con vincoli di uguaglianza e un altro con vincoli di uguaglianza e disuguaglianza. Cosa posso utilizzare? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic Programming
G'day Jorge, On Fri, 15 Feb 2008 17:51:16 -0500 Jorge Aseff [EMAIL PROTECTED] wrote: I am using solve.QP (from quadprog) to solve a standard quadratic programming problem: min_w -0.5*w'Qw st ... I would like solve.QP to do two things: 1) to start the optimization from a user-supplied initial condition; i.e., from a vector w_0 that satisfies the constraints, Not possible. solve.QP is based on the Goldfarb-Idnani algorithm which first calculates the unconstrained solution of the problem and then checks for violated constraints; if such exist, then they are iteratively enforced until all constraints are satisfied. If you want to specify starting values (or have warm starts), then you would need to use other kind of algorithms. and 2) to return the values of the lagrange multiplieres associated with the constraints. See: https://stat.ethz.ch/pipermail/r-devel/2007-December/047636.html Hope this helps. Best wishes, Berrwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: [EMAIL PROTECTED] Singapore 117546http://www.stat.nus.edu.sg/~statba __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Quadratic programming
G'day Serge, On Wed, 5 Dec 2007 11:25:41 +0100 de Gosson de Varennes Serge (4100) [EMAIL PROTECTED] wrote: I am using the quadprog package and its solve.QP routine to solve and quadratic programming problem with inconsistent constraints, which obviously doesn't work since the constraint matrix doesn't have full rank. I guess it will help to fix some terminology first. In my book, inconsistent constraints are constraints that cannot be fulfilled simultaneously, e.g. something like x_1 = 3 and x_1 = 5 for an obvious example. Thus, a problem with inconsistent constraints cannot be solved, regardless of the rank of the constraint matrix. (Anyway, that matrix is typically not square, so would be be talking about full column rank or full row rank?) Of course, it can happen that the constraints are consistent but that there are some redundancy in the specified constraints, e.g. a simply case would be x_1 = 0, x_2 = 0 and x_1+x_2 = 0; if the first two constraints are fulfilled, then the last one is automatically fulfilled too. In my experience, it can happen that solve.QP comes to the conclusion that a constraint that ought to be fulfilled, given the constraints that have already been enforced, is deemed to be violated and to be inconsistent with the constraints already enforced. In that case solve.QP stops, rather misleadingly, with the message that the constraints are inconsistent. I guess the package should be worked over by someone with a better understanding of the kind of fudges that do not come back to bite and of finite precision arithmetic than the original author's appreciation of such issues when the code was written. ;-)) A way to solve this is to perturb the objective function and the constraints with a parameter that changes at each iteration (so you can dismiss it), but that's where it gets tricky! Solve.QP doesn't seem to admitt constant terms, it need Dmat (a matrix) and dvec (a vector) as defined in the package description. Now, some might object that a constant is a vector but the problem looks like this Min f(x) = (1/2)x^t Q x + D^t x + d It is a bit unclear to me what you call the constant term. Is it `d'? In that case, it does not perturb the constraints and it is irrelevant for the minimizer of f(x); also for the minimizer of f(x) under linear constraints. Regardless of d, the solution is always the same. I do not know of any quadratic programming solver that allows `d' as input, probably because it is irrelevant for determining the solution of the problem. Can anyone help me, PLEASEEE? In my experience, rescaling the problem might help, i.e. use Q* = Q/2 and D*=D/2 instead of the original Q and D; but do not forget to rescale the constraints accordingly. Or you might want to try another quadratic program solver in R, e.g. ipop() in package kernlab. Hope this helps. Best wishes, Berwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: [EMAIL PROTECTED] Singapore 117546http://www.stat.nus.edu.sg/~statba __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.