微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

(Python) RuntimeWarning: 在 double_scalars 中遇到无效值

如何解决(Python) RuntimeWarning: 在 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.,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])
  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:double_scalars 中遇到无效值”的信息,并且知道除以零时会显示错误。但是,我没有在我的函数中发现任何可能的错误

谁能帮我解决这个问题?非常感谢。

解决方法

首先,这不是一个最小的工作示例 (MWE),因为您没有在 sol 函数中定义 markets。所以让我们假设

# 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。