317 字
2 分钟
对宏观经济贫富差距的简单分析
概述
模型
Python中的线性规划求解
本部分内容引自《数学建模算法与应用》
求解如下的线性规划问题:
用Python进行线性规划求解时,
import numpy as npfrom scipy import optimize
z = np.array([2, 3, 1])a = np.array([[1, 4, 2], [3, 2, 0]]b = np.array([8, 6])a_eq = np.array([[1, 1, 1]])b_eq = np.array([7])x1_bound = x2_bound = x3_bound = (0, None)bounds = (x1_bound, x2_bound, x3_bound)
res = optimize.linprog(z, A_ub=-a, b_ub=-b, A_eq=a_eq, b_eq=b_eq, bounds=bounds)print(res)
Python中的非线性规划求解
详见scipy的官方文档。
考虑如下的非线性规划问题:
计算的最小值,这里对任意均有成立。
Python代码如下:
import numpy as npfrom scipy import optimize
# 定义目标函数def func1(nums): a, b, c, d = nums def _func0(x): y = (a+x[0])/(b+x[1]) - c*x[0] + d*x[2] return y return _func0
# 有两种方法表示约束def cons1(nums): # 'eq'表示等式,'ineq'表示大于等于 x1_min, x1_max, x1_min, x2_max, x3_min, x3_max = nums _cons = ({'type':'ineq', 'fun':lambda x: x[0]-x1_min}, {'type':'ineq', 'fun':lambda x: x1_max-x[0]}, {'type':'ineq', 'fun':lambda x: x[1]-x2_min}, {'type':'ineq', 'fun':lambda x: x2_max-x[1]}, {'type':'ineq', 'fun':lambda x: x[2]-x3_min}, {'type':'ineq', 'fun':lambda x: x3_max-x[2]}) return _cons
# 法2bouds = []for k in range(3): # 'None'为不限制 bounds.append((0.1, 0.9))
args = [2, 1, 3, 4]x0 = np.array([0.5, 0.5, 0.5])res = optimize.minimize(func1(args), x0=x0, constraints=cons1([.1, .9]*3), bounds=bounds)print(res)
# res.x = [0.9, 0.9, 0.1# res.success = True]
对宏观经济贫富差距的简单分析
https://blog.xiaobaizhang.top/posts/macro25/