Hi, I am trying to constraint `cposc1`

and `cposc2`

(below) to be smaller than zero, it seems that the procedure gives me only cpoc1 smaller than zero but it does not work for `cpoc2`

which turns out higher than zero. Any idea why this may be? Many thanks.

```
proc ineqp(a);
local wposc, w2c, w3c, w4c, pposc, qposc, rposc;
wposc=zeros(1,1);
wposc=a[1]*tau4/(4*a[2]*tau3);
w2c=wposc^2;
w3c=wposc^3;
w4c=wposc^4;
pposc=-6*w2c-a2;
qposc=8*w3c+2*wposc*(a2-2*a1);
rposc=-3*w4c+4*w2c*(a1-a2/4)+a3+(tau4/a[2]);
local cposc1, cposc2, k1c, k2c;
cposc1=pposc^2-4*rposc-1e-5;
k1c=-(pposc^2)/9 -(4*rposc)/3;
k2c=-(pposc^3)/27 + (4*pposc*rposc)/3 -(qposc^2)/2;
cposc2=(k1c^3)+(k2c^2)-1e-5;
retp(-cposc1|-cposc2);
endp;
```

## 6 Answers

The inequality procedure is called by the contstrained optimizer, CO. This procedure needs to take in the current parameter estimates and return a single number. If this number is greater than or equal to zero, the parameters pass the test. If the number returned is less 0, this tells the optimizer that the current parameter estimates violate the constraint.

For a simple example, let's say we have two parameters and we want to constrain the second parameter to be smaller than the first.

```
b_1 = { 1, 2 };
b_2 = { 1, 0.5 };
c_1 = ineqp(b_1);
c_2 = ineqp(b_2);
proc (1) = ineqp(b);
retp(b[1] - b[2]);
endp;
```

After running the above code, the value of `c_1`

will be -1 and the value of `c_2`

will be 0.5. This will tell the optimizer that the parameters `{ 1, 2 }`

violate the constraintes, but `{ 1, 0.5 }`

do not violate the constraints.

Many thanks for this insightful answer. I use the inequality constraints for maximum likelihood estimation. The maximization of the likelihood function is constrained to both conditions. I need the parameters estimates to meet both conditions. I find my first condition (cposc1<0) is met but not the second one (cposc2<0) there my doubt on whether I am missing something in my code. Any suggestions is greatly appreciated.

If you need the parameters to be such that both 'cposc1`and`

cpos2` are less than zero, then the return value from your inequality procedure needs to return a negative number if one of them greater than or equal to zero. For example, you could replace the last couple of lines of the procedure you posted with this:

```
if cpos1 < 0 and cpos2 < 0;
retp(1);
else;
retp(-1);
endp;
```

So many thanks for your reply. Yes I need to estimate the values of the parameters a[1] and a[2] subject to that both cposc1`and`

cpos2 are less than zero. The parameters a[1] and a[2] are estimated by maximum likelihood through another procedure which is subject to the inequality constraints cposc1<0 and cpos2<0. Any suggestion is very much appreciated.

As the last post suggested, this should solve your problem:

```
proc ineqp(a);
local wposc, w2c, w3c, w4c, pposc, qposc, rposc;
wposc=zeros(1,1);
wposc=a[1]*tau4/(4*a[2]*tau3);
w2c=wposc^2;
w3c=wposc^3;
w4c=wposc^4;
pposc=-6*w2c-a2;
qposc=8*w3c+2*wposc*(a2-2*a1);
rposc=-3*w4c+4*w2c*(a1-a2/4)+a3+(tau4/a[2]);
local cposc1, cposc2, k1c, k2c;
cposc1=pposc^2-4*rposc-1e-5;
k1c=-(pposc^2)/9 -(4*rposc)/3;
k2c=-(pposc^3)/27 + (4*pposc*rposc)/3 -(qposc^2)/2;
cposc2=(k1c^3)+(k2c^2)-1e-5;
if cpos1 < 0 and cpos2 < 0;
retp(1);
else;
retp(-1);
endif;
endp;
```

Many thanks for this reply. Following your suggestion I find that the code returns sometimes a combination of estimates for a[1] and a[2] that do not meet both constraints cpos1<0 and cpos2<0. This is odd as if I am estimating subject to these constraints I would expect the outcome to meet the constraints. Is it possible to force the optimization to give a result that meets both constraints? Any reply is much appreciated.