Python scipy.optimize 模块,brent() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用scipy.optimize.brent()

项目:skutil    作者:tgsmith61591    | 项目源码 | 文件源码
def _yj_normmax(x, brack=(-2, 2)):
    """Compute optimal YJ transform parameter for input data.

    Parameters
    ----------

    x : array_like
       Input array.
    brack : 2-tuple
       The starting interval for a downhill bracket search
    """

    # Use MLE to compute the optimal YJ parameter
    def _mle_opt(i, brck):
        def _eval_mle(lmb, data):
            # Function to minimize
            return -_yj_llf(data, lmb)

        return optimize.brent(_eval_mle, brack=brck, args=(i,))

    return _mle_opt(x, brack)  # _mle(x, brack)
项目:sGLMM    作者:YeWenting    | 项目源码 | 文件源码
def train_nullmodel(self, y, K, S=None, U=None):
        self.ldeltamin += self.scale
        self.ldeltamax += self.scale

        if S is None or U is None:
            S, U = linalg.eigh(K)

        Uy = scipy.dot(U.T, y)
        # grid search
        nllgrid = scipy.ones(self.numintervals + 1) * scipy.inf
        ldeltagrid = scipy.arange(self.numintervals + 1) / (self.numintervals * 1.0) * (self.ldeltamax - self.ldeltamin) + self.ldeltamin
        for i in scipy.arange(self.numintervals + 1):
            nllgrid[i] = nLLeval(ldeltagrid[i], Uy, S)

        nllmin = nllgrid.min()
        ldeltaopt_glob = ldeltagrid[nllgrid.argmin()]

        for i in scipy.arange(self.numintervals - 1) + 1:
            if (nllgrid[i] < nllgrid[i - 1] and nllgrid[i] < nllgrid[i + 1]):
                ldeltaopt, nllopt, iter, funcalls = opt.brent(nLLeval, (Uy, S),
                                                              (ldeltagrid[i - 1], ldeltagrid[i], ldeltagrid[i + 1]),
                                                              full_output=True)
                if nllopt < nllmin:
                    nllmin = nllopt
                    ldeltaopt_glob = ldeltaopt


        return S, U, ldeltaopt_glob, None
项目:CS-LMM    作者:HaohanWang    | 项目源码 | 文件源码
def nullModel(self, y, K, S=None, U=None, numintervals=500, ldeltamin=-5, ldeltamax=5, scale=0, p=1):
        ldeltamin += scale
        ldeltamax += scale

        if S is None or U is None:
            S, U = linalg.eigh(K)

        Uy = scipy.dot(U.T, y)

        # grid search
        nllgrid = scipy.ones(numintervals + 1) * scipy.inf
        ldeltagrid = scipy.arange(numintervals + 1) / (numintervals * 1.0) * (ldeltamax - ldeltamin) + ldeltamin
        for i in scipy.arange(numintervals + 1):
            nllgrid[i] = nLLeval(ldeltagrid[i], Uy, S)  # the method is in helpingMethods

        nllmin = nllgrid.min()
        ldeltaopt_glob = ldeltagrid[nllgrid.argmin()]

        for i in scipy.arange(numintervals - 1) + 1:
            if (nllgrid[i] < nllgrid[i - 1] and nllgrid[i] < nllgrid[i + 1]):
                ldeltaopt, nllopt, iter, funcalls = opt.brent(nLLeval, (Uy, S),
                                                              (ldeltagrid[i - 1], ldeltagrid[i], ldeltagrid[i + 1]),
                                                              full_output=True)
                if nllopt < nllmin:
                    nllmin = nllopt
                    ldeltaopt_glob = ldeltaopt
        return S, U, ldeltaopt_glob