Notebookの特徴は、Notebookに説明文や数式を簡単に挿入できることです。
$$y = sin(2 \pi x) + \mathcal{N}(0,0.3)$$
|
@interactコマンドでインタラクティブに変数の値を変更し、結果をみることができます。
|
Click to the left again to hide and once more to show the dynamic interactive window |
以下のような2次形式の関数を考えます。 $$ f(x) = \frac{3}{2} x_1^2 + x_1 x_2 + x_2^2 - 6 x_1 - 7 x_2 $$ 極値に達するには、勾配▽fからある程度接線方向tにずれた共役勾配$d_n$方向に進みます。 $$ d_n = - \nabla f(x_n) + \beta_n d_{n-1} $$
$\beta_n$は $$ \beta_n = \frac{(\nabla f(x_n))^T \nabla f(x_n)}{(\nabla f(x_{n-1}))^T \nabla f(x_{n-1})} $$ となり、dの初期値は$d_0 = - \nabla f(x_0)$から始めます。
$x$は刻み値$\alpha$、 $$ \alpha_n = - \frac{d_n^T \nabla f(x_n)}{d_n^T H d_n} $$ を使って次式で更新します。 ここでHは、f(x)のヘッセ行列です。 $$ x_{n+1} = x_n + \alpha_n d_n $$
|
|
▽fの計算は、ちょっとトリックを使います。あらかじめ関数fの 各変数での偏微分をdfsに保持しておき、その結果に引数のベクトル vxの値を代入した結果を返しています。
|
ヘッセ行列もSageの数式機能を使えば、簡単にもとめることができます。
|
|
共役勾配法の反復処理は、至って単純です。条件を満たすまで与えられた式でxと共役勾配を 更新するだけです。
x= (1, 3) k= 2 x= (1, 3) k= 2 |
求まった解x= (1, 3)をSageの最適化機能で求めた結果と比較します。
Optimization terminated successfully. Current function value: -13.500000 Iterations: 2 Function evaluations: 5 Gradient evaluations: 5 (1.0, 3.0) Optimization terminated successfully. Current function value: -13.500000 Iterations: 2 Function evaluations: 5 Gradient evaluations: 5 (1.0, 3.0) |
Sleeping...
![]() |
|