雑記

以下の記事を投稿しました。

上の記事の「凸関数の最小点の勾配が満たす必要十分条件」のパートで、雑記: KKT条件の話で「証明はここでは割愛します。」といっていた箇所の証明をしています(上の記事では凸関数を仮定しているので必要十分条件になっていますが、KKT条件の話では仮定していないので必要条件です)。

あと上の記事のイラストをGIFにしたものです。
f:id:cookie-box:20201121023037g:plain:w420

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pylab import rcParams
rcParams['figure.figsize'] = 8, 6
rcParams['font.size'] = 16
rcParams['font.family']='Ume Hy Gothic O5'


fig, ax = plt.subplots()

if True:
    x0 = 1.0  # 記事中のイラストでいう x
    x1 = 4.2  # 記事中のイラストでいう y
    y0 = np.exp(x0)
    y1 = np.exp(x1)
    list_x = np.arange(0.2, 4.5, 0.2)
    ax.plot([x0, x0], [-10, y0], color='#cccccc')
    ax.plot([x1, x1], [-10, y1], color='#cccccc')
    ax.plot([0.2, x1], [y0, y0], color='#cccccc')
    ax.plot([0.2, x1], [y1, y1], color='#cccccc')
    ax.plot(list_x, np.exp(list_x), color='black')
    list_x_ = np.array([x0, x1])
    ax.plot(list_x_, np.exp(list_x_), color='green')

ims = []

for theta in np.arange(0.84, 0.0, -0.02):
    x2 = (1.0 - theta) * x0 + theta * x1
    y2 = np.exp(x2)
    y2_ = y0 + (y2 - y0) / theta
    im = plt.plot([x2, x2], [-10, y2], color='#cccccc') \
         + plt.plot([0.2, x1], [y2_, y2_], color='#cccccc', zorder=-10) \
         + plt.plot([x0, x1], [y0, y2_], color='blue')
    ims.append(im)

for i in range(10):  # 最後の状態でしばらく止めるためにコマ数を取っているだけ
    y2_ = y0 + y0 * (x1 - x0)
    im = plt.plot([0.2, x1], [y2_, y2_], color='#cccccc', zorder=-10) \
         + plt.plot([x0, x1], [y0, y2_], color='blue')
    ims.append(im)

ani = animation.ArtistAnimation(fig, ims, interval=150)
ani.save("animation.gif")