凸性限制最佳化

Python 最佳化入門

Jasmin Ludolf

Content Developer

凸性限制最佳化

凸函數。

  • 限制:對變數的約束
  • 無差異曲線:代表變數的組合
  • 在無差異曲線上找出使目標函式最小或最大的位置
Python 最佳化入門

無差異曲線

一位黑髮年輕女性正在使用筆電工作

  • 自由接案軟體工程師
  • 重視工作($w$)與休閒($l$)

 

效用函式:

  • $U(w, l)=w^{0.4}l^{0.6}$
Python 最佳化入門

繪製無差異曲線

import numpy as np
import matplotlib.pyplot as plt


w = np.linspace(1, 30, 100) l = np.linspace(1, 30, 100)
W, L = np.meshgrid(w, l)
F = W**0.4 * L**0.6
plt.figure(figsize=(8, 6))
contours = plt.contour(W, L, F, levels=[5, 10, 15, 20])
plt.clabel(contours)
plt.title('Indifference Curves for the function: w**0.4 * l**0.6') plt.xlabel('w') plt.ylabel('l') plt.grid(True) plt.show()
Python 最佳化入門

無差異曲線

視覺化無差異曲線

  • Alexia 對於 $w$ 與 $l$ 的具體組合無偏好差異
Python 最佳化入門

時間限制

  • 每天 24 小時
  • 線性等式限制

$$ \max_{w,l} w^{0.4}l^{0.6}$$ $$s.t.\ \ \ w+l = 24$$

  • 在圖上加入限制:
    • l = 24 - w
    • plt.plot(w, l, color='red')

無差異曲線與線性限制。最適無差異曲線與限制相切。於切點處,梯度同時垂直於限制與切線。

Python 最佳化入門

使用 SciPy 求解

def utility_function(vars):
    w, l = vars
    return -(w**0.4 * l**0.6)


def constraint(vars): return 24 - np.sum(vars)
initial_guess = [12, 12] constraint_definition = {'type': 'eq', 'fun': constraint} result = minimize(utility_function, initial_guess, constraints=constraint_definition) print(result.x)
[ 9.60001122 14.39998878]
Python 最佳化入門

一起來練習吧!

Python 最佳化入門

Preparing Video For Download...