i have a problem that i'd like to solve in R.
I see that i can use the function lsei
in the package limSolve
to minimise a system of linear equations written Ax=b in the matrix form, subject to equality constraints Ex=f and the inequality constraints Gx>=h.
However, rather than a system of linear equations, i now have a system of quadratic equations that can be written t(x)Ax=b.
I see there's the package quadprog
for the quadratic case, but it doesn't seem to allow for a set of quadratic equations, just the one equation.
Does anyone know what i could use to minimise a system of quadratic equations under both an equality and an inequality constraint?
Here's my example. I'm trying to combine 3 probabilities P(A), P(B), P(C) - this creates 7 segments v1 to v7, where v1 is P(A solus) etc... v4 is P((A AND B) NOT C) etc.. and v7 is P(A AND B AND C).
The function i'm trying to minimise is:
obj.fc<-function(x){
f<-rep(NA,4)
v1<-x[1]
v2<-x[2]
v3<-x[3]
v4<-x[4]
v5<-x[5]
v6<-x[6]
v7<-x[7]
f[1]<-(v4+v7)*(1-(v1+v2+v4+v5+v6+v7))-2*(v1+v6)*(v2+v5)
f[2]<-(v5+v7)*(1-(v2+v3+v4+v5+v6+v7))-13*(v2+v4)*(v3+v6)
f[3]<-(v6+v7)*(1-(v1+v3+v4+v5+v6+v7))-11*(v1+v4)*(v3+v5)
f[4]<-(v4+v5+v6)*(1-(v1+v2+v3+v4+v5+v6+v7))-4*(v1+v2+v3)*v7
return(f)
}
My equality constraints are:
v1+v4+v6+v7=0.14
v2+v4+v5+v7=0.01
v3+v5+v6+v7=0.08
And my inequality constraints are that the Vi have to be between 0 and 1 and their sum can't exceed 1.
solve.QP
from thequadprog
package is used quite routinely to solve quadratic programming problems with both linear and nonlinear constraints. A search forsolve.QP
on SO will return many examples. To address your specific question, you'll need to construct the constraint matrixA
with the equality constraints first and then the inequality constraints. The argumentmeq
is used to tellsolve.QP
how many of the first equations inA
are equality constraints. – WaltS