(Python)RuntimeWarning:在 double_scalars 中遇到无效值
(Python) RuntimeWarning: invalid value encountered in double_scalars
我想用scipy.optimize.root求解静态平衡模型。具体来说,我需要找到三维函数系统的根。
代码如下:
# Import package
import numpy as np
from scipy.optimize import root,fsolve
# Parameters
Kbar = 10 # Total capital
Lbar = 20 # Total labour
α = 0.3
β = np.array([0.3,0.6])
p = np.array([1.0,0.1])
# The first element of the price array is p1=1 (constant), the second is p2 which needs to be optimized.
# Defining the objective function system
def markets(x):
# prices (excluded p1=1), x is a 3-dimentional array
p[1] = x[0] # p2 is the first element of input x
w = x[1] # wage w is the second element of input x
r = x[2] # interest rate r is the third elemendt of input x
# total income
Ybar = w * Lbar + r * Kbar
# get market equation
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
sol[2] = β[0]*α*Ybar/w + β[1]*(1-α)*Ybar/w - Lbar
return sol
# Initial guess x0 is a 3*1 array
x0 = np.zeros(3) + 5
# Find market equilibrium
res = root(markets, x0)
print(res)
但是结果报错:
fjac: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
fun: array([nan, nan, nan])
message: 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.'
nfev: 17
qtf: array([ 0.89142371, 0.09796604, -4.69999992])
r: array([nan, nan, nan, nan, nan, nan])
status: 5
success: False
x: array([5., 5., 5.])
<ipython-input-37-15479e9f467a>:24: RuntimeWarning: invalid value encountered in double_scalars
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
<ipython-input-37-15479e9f467a>:25: RuntimeWarning: invalid value encountered in double_scalars
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
我搜索了一些关于“RuntimeWarning: invalid value encountered in double_scalars”的信息,知道除以零时会出现这个错误。但是,我没有在我的函数中发现任何可能的错误。
谁能帮我解决这个问题?非常感谢。
首先,这不是一个最小工作示例 (MWE),因为您没有在 markets
函数中定义 sol
。所以让我们假设
# Defining the objective function system
def markets(x):
# prices (excluded p1=1), x is a 3-dimentional array
p[1] = x[0] # p2 is the first element of input x
w = x[1] # wage w is the second element of input x
r = x[2] # interest rate r is the third elemendt of input x
# total income
Ybar = w * Lbar + r * Kbar
# get market equation
sol = np.zeros(3)
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
sol[2] = β[0]*α*Ybar/w + β[1]*(1-α)*Ybar/w - Lbar
return sol
那么,正如您已经提到的,错误是由于 1/p[1]
项中被零除造成的。因此,你需要找到一个根 x > 0
,即我们寻找一个点 x > 0
使得 markets(x) = 0
。但是,scipy.optimize.root
方法不支持简单的变量边界。
通过将根问题写成等效约束最小化问题:
min ||markets(x)|| s.t. x >= eps
其中eps > 0
,可以借助scipy.optimize.minimize
解决问题:
from scipy.optimize import minimize
# Variable bounds
eps = 1.0e-4
bnds = [(eps, None) for _ in range(3)]
# initial point
x0 = np.zeros(3) + 5
# Solve the minimization problem
res = minimize(lambda x: np.linalg.norm(markets(x)), x0=x0, bounds=bnds)
我想用scipy.optimize.root求解静态平衡模型。具体来说,我需要找到三维函数系统的根。 代码如下:
# Import package
import numpy as np
from scipy.optimize import root,fsolve
# Parameters
Kbar = 10 # Total capital
Lbar = 20 # Total labour
α = 0.3
β = np.array([0.3,0.6])
p = np.array([1.0,0.1])
# The first element of the price array is p1=1 (constant), the second is p2 which needs to be optimized.
# Defining the objective function system
def markets(x):
# prices (excluded p1=1), x is a 3-dimentional array
p[1] = x[0] # p2 is the first element of input x
w = x[1] # wage w is the second element of input x
r = x[2] # interest rate r is the third elemendt of input x
# total income
Ybar = w * Lbar + r * Kbar
# get market equation
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
sol[2] = β[0]*α*Ybar/w + β[1]*(1-α)*Ybar/w - Lbar
return sol
# Initial guess x0 is a 3*1 array
x0 = np.zeros(3) + 5
# Find market equilibrium
res = root(markets, x0)
print(res)
但是结果报错:
fjac: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
fun: array([nan, nan, nan])
message: 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.'
nfev: 17
qtf: array([ 0.89142371, 0.09796604, -4.69999992])
r: array([nan, nan, nan, nan, nan, nan])
status: 5
success: False
x: array([5., 5., 5.])
<ipython-input-37-15479e9f467a>:24: RuntimeWarning: invalid value encountered in double_scalars
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
<ipython-input-37-15479e9f467a>:25: RuntimeWarning: invalid value encountered in double_scalars
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
我搜索了一些关于“RuntimeWarning: invalid value encountered in double_scalars”的信息,知道除以零时会出现这个错误。但是,我没有在我的函数中发现任何可能的错误。
谁能帮我解决这个问题?非常感谢。
首先,这不是一个最小工作示例 (MWE),因为您没有在 markets
函数中定义 sol
。所以让我们假设
# Defining the objective function system
def markets(x):
# prices (excluded p1=1), x is a 3-dimentional array
p[1] = x[0] # p2 is the first element of input x
w = x[1] # wage w is the second element of input x
r = x[2] # interest rate r is the third elemendt of input x
# total income
Ybar = w * Lbar + r * Kbar
# get market equation
sol = np.zeros(3)
sol[0] = 1/p[0]-(β[0]/w)**β[0]*((1-β[0])/r)**(1-β[0])
sol[1] = 1/p[1]-(β[1]/w)**β[1]*((1-β[1])/r)**(1-β[1])
sol[2] = β[0]*α*Ybar/w + β[1]*(1-α)*Ybar/w - Lbar
return sol
那么,正如您已经提到的,错误是由于 1/p[1]
项中被零除造成的。因此,你需要找到一个根 x > 0
,即我们寻找一个点 x > 0
使得 markets(x) = 0
。但是,scipy.optimize.root
方法不支持简单的变量边界。
通过将根问题写成等效约束最小化问题:
min ||markets(x)|| s.t. x >= eps
其中eps > 0
,可以借助scipy.optimize.minimize
解决问题:
from scipy.optimize import minimize
# Variable bounds
eps = 1.0e-4
bnds = [(eps, None) for _ in range(3)]
# initial point
x0 = np.zeros(3) + 5
# Solve the minimization problem
res = minimize(lambda x: np.linalg.norm(markets(x)), x0=x0, bounds=bnds)