再帰を使用して単純な二分法を再作成することで、今日も F# の学習を続けています。ここでは、MathNet ライブラリを使用して Beta ディストリビューションから継承しています。
関数「検索」(二分探索法) で というエラーが表示されますthe value is not a function and cannot be applied
。
//Beta class inheriting from MathNet Beta distribution
//Extends the class by implementing an InverseCDF function
type newBeta(alpha:double, beta:double) =
inherit MathNet.Numerics.Distributions.Beta(alpha, beta)
member this.InverseCDF(p: float) =
let rec search (min: float, max: float, acc: uint32) =
let x = (min + max) / 2.0
let error = 0.001
let maxiters : uint32 = 1000u
let cdf = this.CumulativeDistribution(x)
match cdf, (Math.Abs(cdf - p) < error || acc > maxiters) with //while statement
| _ , true -> cdf //auto match cdf and if while statement evaluates true then break and return cdf result
| p , _ -> cdf //if exactly matches p then break and return cdf result
| p , false when p > cdf -> search (min) (x) (acc + 1) //if p > cdf then set max = x and increment then call func with new params
| p , false when p < cdf -> search (x) (max) (acc + 1) //if p < cdf then set min = x and increment then call func with new params
search (0.0) (1.0) (0) //Call the bisection method with initial parameters
誰でも助けることができますか?また、明らかに、これをより「機能的」にする方法についての意見はクールです。ただし、エラーのため、これをテストするためにまだ実行できていません。の現在の値を返そうとしていることを考えると、最初の 2 つの一致パターンは疑わしいように見えますcdf
。