« 高周波・RFニュース 2025年5月12日 IEEE Microwave Magazineでミリ波ガラス基板・超伝導・液晶などの記事、Pythonの高周波ライブラリscikit-rfがv1.7.0に、Megamagneticsの希土類を使わないミリ波サーキュレータ、CoilcraftのRFインダクタホワイトペーパー | トップページ | 「数学がゲームを動かす! ゲームデザインから人工知能まで」を読んだ。面白い!パックマンのアルゴリズムやドラクエの計算式、ドンキーコングはベルレ法でジャンプ、カルマンフィルタ、遺伝的アルゴリズム、セガの線形代数本を書かれた方は理論物理出身など話題が豊富。 »

2025年5月12日 (月)

関数型プログラミング言語 F# (fsharp)の数値計算ライブラリ Math.NET Numericsを使う(7) OptimizationのLevenberg-Marquardt法(LevenbergMarquardtMinimizer)で非線形最小二乗法(回帰)でNISTの例題Rat43を計算する。

今回はこの例題。

 Visual C# (C_sharp)の数値計算ライブラリ MathNET Numericsを使う(7) OptimizationのLevenberg-Marquardt法(LevenbergMarquardtMinimizer)で非線形最小二乗法(回帰)でNISTの例題Rat43を計算する。

コードはこんな感じで。

open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.Optimization
open Plotly.NET

let xdata = vector [ 1.00; 2.00; 3.00; 4.00; 5.00; 6.00; 7.00; 8.00; 9.00; 10.00; 11.00; 12.00; 13.00; 14.00; 15.00]
let ydata = vector [16.08; 33.83; 65.80; 97.20; 191.55; 326.20; 386.87; 520.53; 590.03; 651.92;724.93; 699.56; 689.96; 637.56; 717.41]
let InitialValue = vector [100.0; 10.0; 1.0; 1.0]

let OptimizeFunction (p : Vector<float>) (x : Vector<float>) =
    vector [for i in 0..x.Count-1 -> p[0] / Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3])]

let Gradient (p : Vector<float>) (x : Vector<float>) =
    let m =
        matrix [
            [for i in 0..x.Count-1 -> 1.0 / Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3])]
            [for i in 0..x.Count-1 -> -p[0]* Math.Exp(p[1] - p[2] * x[i]) / (p[3]*Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3]+1.0))]
            [for i in 0..x.Count-1 -> p[0] * x[i] * Math.Exp(p[1] - p[2] * x[i])/(p[3]  * Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3] + 1.0))]
            [for i in 0..x.Count-1 ->Math.Log(1.0 + Math.Exp(p[1] - p[2] * x[i])) * p[0] /(p[3] *p[3]* Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3] ))]
        ]
    m.Transpose()

let obj = ObjectiveFunction.NonlinearModel(OptimizeFunction, Gradient, xdata, ydata)
let solver = LevenbergMarquardtMinimizer()
let result = solver.FindMinimum(obj, InitialValue)

let x = vector [ for i in 0..100 -> 1.0 + (float)i * 14.0/100.0 ]
let y = OptimizeFunction result.MinimizingPoint  x

[Chart.Point(xdata, ydata, ShowLegend = true, Name = "Data");
Chart.Line(x,y, ShowLegend = true, Name = "Nonlinear Fitting")]
|> Chart.combine |> Chart.withXAxisStyle("x") |> Chart.withYAxisStyle("y")
|> Chart.withTitle("Levenberg-Marquard Method") |> Chart.withLayoutStyle(Width = 800, Height = 500)
結果はこちら。
Fsharpnonlinear1

« 高周波・RFニュース 2025年5月12日 IEEE Microwave Magazineでミリ波ガラス基板・超伝導・液晶などの記事、Pythonの高周波ライブラリscikit-rfがv1.7.0に、Megamagneticsの希土類を使わないミリ波サーキュレータ、CoilcraftのRFインダクタホワイトペーパー | トップページ | 「数学がゲームを動かす! ゲームデザインから人工知能まで」を読んだ。面白い!パックマンのアルゴリズムやドラクエの計算式、ドンキーコングはベルレ法でジャンプ、カルマンフィルタ、遺伝的アルゴリズム、セガの線形代数本を書かれた方は理論物理出身など話題が豊富。 »

パソコン・インターネット」カテゴリの記事

学問・資格」カテゴリの記事

日記・コラム・つぶやき」カテゴリの記事

コメント

コメントを書く

(ウェブ上には掲載しません)

« 高周波・RFニュース 2025年5月12日 IEEE Microwave Magazineでミリ波ガラス基板・超伝導・液晶などの記事、Pythonの高周波ライブラリscikit-rfがv1.7.0に、Megamagneticsの希土類を使わないミリ波サーキュレータ、CoilcraftのRFインダクタホワイトペーパー | トップページ | 「数学がゲームを動かす! ゲームデザインから人工知能まで」を読んだ。面白い!パックマンのアルゴリズムやドラクエの計算式、ドンキーコングはベルレ法でジャンプ、カルマンフィルタ、遺伝的アルゴリズム、セガの線形代数本を書かれた方は理論物理出身など話題が豊富。 »

最近の記事

2025年6月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

最近のコメント

無料ブログはココログ
フォト