scipy.optimize.minimize收敛问题

时间:2015-01-21 17:15:30

标签: python numpy statistics scipy

我有一个我希望优化的功能,但这会返回nan s。

这是函数(fnRestrictParams是辅助函数):

def fnRestrictParams(vParams):
    vRestrictedParams = vParams
    vRestrictedParams[1] = exp(vParams[1])
    vRestrictedParams[2] = exp(vParams[2]) / (1 + exp(vParams[2]))
    return(vRestrictedParams)


def fnGASGaussianCopulaLikelihood(vParams, iT, mData):
    dLL = 0  # initialize the likelihood at zero
    vRestrictedParams = fnRestrictParams(vParams)
    dOmega = vRestrictedParams[0]
    dA = vRestrictedParams[1]
    dB = vRestrictedParams[2]

    dFactor = dOmega
    dOmega = dOmega*(1 - dB)
    vFactor = np.zeros(iT)
    for t in range(iT):
        # compute the copula parameters based on the factors
        rho = (1 - exp(-dFactor))/(1 + exp(-dFactor))
        rho2 = rho * rho
        vFactor[t] = rho

        # quantile functions
        qu = sps.norm.ppf(mData[t, :])
        x = qu[0] ** 2 + qu[1] ** 2
        y = qu[0] * qu[1]

        # get the log pdf of the copula, and its gradient with respect to the copula parameters
        dLL += -0.5 * np.log(1 - rho2) - 0.5 * (rho2 * x - 2 * rho * y) / (1 - rho2)

        # scaled score function
        dSt = (2 / (1 - rho2)) * (y - rho - rho * (x - 2) / (1 + rho2))

        # GAS recursion
        dFactor = dOmega + dA * dSt + dB * dFactor
    dLL = dLL/iT
    return(-dLL)

测试此功能的数据是here

我知道这个函数可以正常工作,因为我已经将输出与原作者提供的实现(另一种编程语言)进行了比较。

# load the data
mData = np.loadtxt("./Data/Patton4filtered.csv",
           skiprows=1, usecols = tuple(range(1, 4)), delimiter = ",")

# test the likelihood function
for x in np.arange(3.1, 4, .1):
    print(fnGASGaussianCopulaLikelihood([x, -5, 5.0], mData.shape[0], mData[:, [0, 1]]))

但是,当我尝试使用scipy.optimize.minimize优化此功能时:

# optimize the function without the gradient
spoGC = spo.minimize(fnGASGaussianCopulaLikelihood, np.array([0.005,-5,5.0]),
                     args = (int(mData.shape[0]), mData[:, [0, 1]]),
             method = 'BFGS', options = {'disp': True, 'gtol': 1e-10, 'eps': 1e-10})

我明白了:

Optimization terminated successfully.
         Current function value: nan
         Iterations: 0
         Function evaluations: 5
         Gradient evaluations: 1
Out[34]: 
(array([ 0.005     ,  1.0067607 ,  0.72974065]),
 nan,
 array([ nan,  nan,  nan]),
 array([[1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]]),
 5,
 1,
 0)

这显然不好。我无法弄清楚导致这个问题的原因。任何帮助,将不胜感激。

0 个答案:

没有答案