通過嘗試重新創建使用遞歸的簡單二分法,繼續今天的F#學習,此處我使用MathNet庫從Beta分佈中繼承。F#值不是函數錯誤
我收到函數「搜索」(二進制搜索方法)錯誤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
任何人都可以幫忙嗎?同樣明顯的是,如何讓這個更「功能性」的任何輸入都會很酷。由於這個錯誤,還沒有能夠運行這個測試。由於我試圖返回cdf
的當前值,因此我的前兩個匹配模式看起來很可疑。
再次感謝+ 1d這是一個非常好的解釋。 –
您可以將兩個中間匹配案例移到頂部,然後將頂部和底部案例合併到一個'_'個案例中。 – ildjarn
@ildjarn,謝謝你的提示。這段代碼看起來很好,並且合併了真實和錯誤的語句,它看起來比C#代碼更好。 –