fslaborg / FSharp.Stats

statistical testing, linear algebra, machine learning, fitting and signal processing in F#
https://fslab.org/FSharp.Stats/
Other
205 stars 54 forks source link

[Feature Request] Add Parameter field to Distributions #271

Closed bvenn closed 1 year ago

bvenn commented 1 year ago

Problem

Currently the only option to get access to parameters from already initialized distribution is toString(). Of course in most cases it is not necessary to provide the parameters from a distribution because you still have access to the parameters the distribution was initialized with.

However in special cases in which an algorithm takes instances of the Distribution interface as input and e.g. fits the best fitting distribution, it may be beneficial to have access to the parameters to report them along with the optimized distribution.

Example

open FSharp.Stats

let normal = 
    Distributions.Continuous.Normal.Init 12. 0.4

let bernoulli = 
    Distributions.Discrete.Bernoulli.Init 0.2

let functionThatTakesDistributionAsInput (dists: Distributions.Distribution<'a> []) (sample: float []) =
    dists
    |> Array.map (fun singleDistribution -> 
        //do some stuff to assess the quality of the distribution fit to the sample
        let fitQuality = 13.37
        match singleDistribution.Parameters with 
            | Distributions.Normal x -> printfn "The normal distribution with Mean: %.2f and StDev: %f has a fit quality of %.2f" x.Mean x.StandardDeviation fitQuality
            | Distributions.Bernoulli x -> printfn "The bernoulli distribution with P: %f has a fit quality of %.2f" x.P fitQuality
            | _ -> failwithf "Distribution type was not expected within this function!"
        fitQuality
    ) 

functionThatTakesDistributionAsInput [|normal;bernoulli|] [|0..10|]

The result would be:


The bernoulli distribution with P: 0.200000 has a fit quality of 13.37
val it: float array = [|13.37; 13.37|]```

## Solution

Add parameter types for each distribution that are summarized as `DistributionParameter` type. This type should be added to the distribution interface.
bvenn commented 1 year ago

closed by 3a0940fa85d788064a8f423865f64575bce5ca86