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]]
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 <maija.sirkjarvi at gmail.com> 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 at 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.
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 <spurdle.a at gmail.com> 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 > <maija.sirkjarvi at gmail.com> 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 at 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.
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 <maija.sirkjarvi at gmail.com> 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 at 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.
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 <spurdle.a at gmail.com> 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 > <maija.sirkjarvi at gmail.com> 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 at 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.