如何解决在sympy中用隐函数求解KKT方程
我是 SymPy 的新手,想解决以下优化问题的 KKT 方程:
\begin{对齐*} &\max_{L_A,L_C,X_a,X_{na},s,u} U(L_A,X_{na})=(\hat{\imath}-L_C)(X_a-S_a)^{ \alpha_a}(X_{na}-S_{na})^{\alpha_{na}}(I-\hat{\imath}-L_A)^{\alpha_{A}}(\hat{\imath}- L_C)^{\alpha_C}\ st\quad& p_a X_a + p_{na} X_{na} + p_A (I-\hat{\imath}-L_A)+p_C (\hat{\imath}-L_C) = Y = p_a f(L,\theta,K)-p_A L_A - p_C L_C+p_A(I-\hat{\imath})+p_C(\hat{\imath}) \end{对齐*}
约束中有一个隐式函数(我的意思是 $f(L,K)$ 的实际形式是未知的)。由假设,该函数是正单调凹函数,即 $$\frac{\partial f}{\partial L}>0 \quad \frac{\partial f}{\partial \theta}>0 \quad \frac{\partial^2 f}{\partial L^2 }0.$$
为了使事情复杂化,函数 $f(L,K)$ 的输入 $L$ 有一个明确的形式,因为它涉及六个选择变量中的四个:
$$L(L_C,L_A,u,s) = L_A - s u L_C + s v(u) L_C + (1-s) v_0 L_C$$
其中 v(u) 又是正单调和凹面。
下面是我在 SymPy 中实现它的尝试。我在求解阶段(最后)遇到一个错误,我认为这是 SymPy 不知道如何取隐函数 ($f(\cdot)$) 与另一个函数 ($L(\ cdot)$) 本身定义。
我如何告诉 SymPy $f(\cdot)$ 应该是正单调和凹面?
任何关于如何进行的想法将不胜感激。
# import stuff
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import Function
from sympy.solvers import solve
import sympy as sp
# define the maximization variables
X1,X2,X3,X4,LC,LA,u = sp.var('X_1,X_2,X_3,X_4,u',positive=True);
# import exogenous variables
a1 = sp.symbols('alpha_1') # alphas
a2 = sp.symbols('alpha_2')
a3 = sp.symbols('alpha_3')
a4 = sp.symbols('alpha_4')
S1 = sp.symbols('S_1') # Subsistence levels
S2 = sp.symbols('S_2')
S3 = sp.symbols('S_3')
S4 = sp.symbols('S_4')
v0 = sp.symbols('v_0') # raw skill level
i = sp.symbols('\hat{\imath}') # children in the household - time endowment
I = sp.symbols('I') # household size and total time endowment
p1 = sp.symbols('p_1') # price vector
p2 = sp.symbols('p_2')
p3 = sp.symbols('p_3')
p4 = sp.symbols('p_4')
climate = sp.symbols('theta') # climate variable
capital = sp.symbols('K') # quasi fixed land and (non-human) capital
lam = sp.symbols('\lambda') # shadow prices (lagrange multiplier)
# technology of skill enhancement
v = Function('v')(u)
# labour equivalence function (cases 1 and 2)
L1 = Function('L_1')(u,LC)
L1 = LA-s * u * LC + s * v * LC + (1 - s) * v0 * LC
L2 = Function('L_2')(u,LC)
L2 = v * (LA/u)+v0*(LC-LA/u)
# define production function
f1 = Function('f')(L1,capital,climate) # positive monotonic and convex in L1,and climate
f2 = Function('f')(L2,climate)
# utility function (cases 1 and 2)
U1 = Function ('U_1')(X1,LC)
U2 = Function ('U_2')(X1,LC)
U1 = (i-LC) * (X1-S1)**a1 *(X2-S2)**a2 * (I-i-LA-S3)**a3 * (i-LC-S4)**a4
U2 = (X1-S1)**a1 *(X2-S2)**a2 * (I-i-LA-S3)**a3 * (i-LC-S4)**a4
# main constraints
#G1 = Function('G_1')(X1,f1)
G1 = p1*X1 + p2*X2 + p3*I - p3*i -p3*LA + p4*i - p4*LC - p1*f1 + p3*LA + p4*LC - p3*I-p3*i - p4*i
#G1 = Function('G_1')(X1,f2)
G2 = p1*X1 + p2*X2 + p3*I - p3*i -p3*LA + p4*i - p4*LC - p1*f2 + p3*LA + p4*LC - p3*I-p3*i - p4*i
# Lagrangians
Lagrange1 = U1 - lam * G1 # case 1
Lagrange2 = U2 - lam * G1 # case 2
Lagrange3 = U1 - lam * G2 # case 3
Lagrange4 = U2 - lam * G2 # case 4
# Calculate KKT-conditions
grad1 = [sp.diff(Lagrange1,c) for c in [X1,u]] # gradient of Lagrangian w.r.t choice variables
KKT1 = grad1 + [G1]
grad2 = [sp.diff(Lagrange2,u]] # gradient of Lagrangian w.r.t choice variables
KKT2 = grad2 + [G1]
grad3 = [sp.diff(Lagrange3,u]] # gradient of Lagrangian w.r.t choice variables
KKT3 = grad3 + [G2]
grad4 = [sp.diff(Lagrange4,u]] # gradient of Lagrangian w.r.t choice variables
KKT4 = grad4 + [G2]
### Here the thing breaks down - perhaps because the convexity of f1 is not specified.
########################################################################################################
candidates1 = sp.solve(KKT1,[X1,lam],dict=True) # solve the KKT equations #
candidates2 = sp.solve(KKT2,dict=True) # solve the KKT equations #
candidates3 = sp.solve(KKT3,dict=True) # solve the KKT equations #
candidates4 = sp.solve(KKT4,dict=True) # solve the KKT equations #
########################################################################################################
# return candidates for optima
print(candidates)
´´´
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。