我尝试解决以下环境:
p = (p1, p2)
,状态变量z
的向量,解决最优("个别")决策F(p, z)
(在离散网格上),这是一个包含F1, F2
的向量。 f = (f1, f2)
,找到根("超额需求")F(p, z) = f
。 所以,我攻击这个问题的方式是[(Param
,Grid
)只包含参数等。]:
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.
这是不幸的,因为我实际上想要使用半牛顿法(它自动逼近雅可比行列式。
使用broyden1
或broyden2
表示解算器从一开始就卡住了:
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
我该如何调试?