如何调试scipy.optimize.root()

时间:2014-07-20 15:45:25

标签: python debugging optimization scipy

我尝试解决以下环境:

  • 给定价格p = (p1, p2),状态变量z的向量,解决最优("个别")决策F(p, z)(在离散网格上),这是一个包含F1, F2的向量。
  • 给定常量f = (f1, f2),找到根("超额需求")F(p, z) = f

所以,我攻击这个问题的方式是[(ParamGrid)只包含参数等。]:

def iterateIndividual (Prices, Param, Grid):
    returns F1, F2

def computeRoots (Prices, states, Param, Grid):
    F1, F2 = iterateIndividual(Prices, Param, Grid)
    excessDemand = np.concatenate( ( (F1 - Grid.A)[..., np.newaxis], (F2 - Grid.M)[..., np.newaxis]), axis=-1)
    return excessDemand

def solveEverything(states, Grid, Param):
    from scipy.optimize import root

    guessA = ones(states.shape)
    guessM = ones(states.shape)
    #zipPrices returns object of shape `(A, B, 2)`, stacking the guesses on the third dimension
    guess = zipPrices(guessA, guessM)
    sol = root(computeExcessDemand, guess, method='krylov', options={'disp':True}, args=[states, Param, Grid])

对于解决p的每个州,应该有一个唯一的对f = F,而且它应该满足全局收敛的条件。一个问题可能是iterateIndividual产生一个离散网格,我没有插入。但是,root()告诉我

ValueError: Jacobian inversion yielded zero vector. This indicates a bug in the Jacobian approximation.

这是不幸的,因为我实际上想要使用半牛顿法(它自动逼近雅可比行列式。

使用broyden1broyden2表示解算器从一开始就卡住了:

In[388]: sol = root(computeExcessDemand, guess, method="broyden2", options={'disp':True}, args=[states, Param, Grid])
0:  |F(x)| = 131.909; step 1; tol 0.9
1:  |F(x)| = 159.687; step 1; tol 0.9999
2:  |F(x)| = 159.687; step 1; tol 0.9
3:  |F(x)| = 159.687; step 1; tol 0.9

我该如何调试?

0 个答案:

没有答案