Numpy Minimize COBYLA Constraints
<p>I am using scipy.minimize with the COBYLA method, but I seem to be unable to write the constraints properly because when I check the values of the objective function, they do not respect those constraints.</p>

<p>Basically, the objective function accepts an array as argument, that must follow two constraints:</p>

<ol>
<li>Each value in the array must be greater than 0</li>
<li>The sum of the values must be inferior to 1</li>
</ol>

<p>So far I wrote it this way:</p>

<pre><code>constraints = [{'type': 'ineq', 'fun': lambda x: 1 - sum(x)},
               {'type': 'ineq', 'fun': lambda x: x[0]},
               {'type': 'ineq', 'fun': lambda x: x[1]}]
</code></pre>

<p>However, sometimes I get values greater than 1...</p>

<p>Here is an example:</p>

<pre><code>from __future__ import division
from math import pow, exp
import numpy as np
from scipy.optimize import minimize

nbStudy = 3
nbCYP = 2
raucObserved = [3.98, 2.0, 0.12]
IXmat = np.matrix([[-0.98, 0], [-0.3, -0.98], [7.7, 4.2]])

NBITER = 50
estimatedCR = []
raucPred = []
varR = [0.0085, 0.0048, 0.0110]
sdR = [0.0922, 0.0692, 0.1051]

cnstrts = [{'type': 'ineq', 'fun': lambda x: 1 - sum(x)},
               {'type': 'ineq', 'fun': lambda x: x}]


def fun(CR):
    dum = []

    for i in range(nbStudy):
        crix = 0

        for j in range(nbCYP):
            crix += CR[j] * IXmat[i, j]

        raucPredicted = 1 / (1 + crix)
        dum.append(pow((np.log(raucPredicted) - np.log(raucObservedBiased[i])), 2) / varR[i])

    output = np.sum(dum)

    return output


for iter in range(NBITER):
    raucObservedBiased = []

    for k in range(nbStudy):
        raucObservedBiased.append(raucObserved[k] * exp(sdR[k] * np.random.normal()))

    initialCR = np.matrix([[(1 / nbCYP) * np.random.uniform()], [(1 / nbCYP) * np.random.uniform()]])

    output = minimize(fun, initialCR, method='COBYLA', constraints=cnstrts)
    estimatedCR.append(output.x)
</code></pre>