Angara.Statistics


Angara.Statistics

A collection of essential algorithms for Bayesian data constrained modelling. Includes Mersenne twister random number generator, common probability distributions, sampling statistics and quantiles, a kernel density estimator and a resumable Metropolis-Hastings MCMC sampler.

Example

Here is how you draw from normal distribution

1: 
2: 
3: 
4: 
5: 
6: 
#r "Angara.Statistics.dll"
open Angara.Statistics

let generator = MT19937() // Mersenne twister with default seed
let distribution = Normal(37.0, 9.0)
draw generator distribution

See Probability distributions tutorial for more information.

Next snippet shows how to infer a parameter of a Poisson distribution data using MCMC sampler.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let fake_data = [for _ in 1..1000 -> draw generator (Poisson 5.)]

open Angara.Filzbach
let log_likelihood (p:Parameters) =
    let lambda = p.GetValue("lambda")
    Seq.sumBy (fun d -> log_pdf (Poisson lambda) d) fake_data
let prior = Parameters.Empty.Add("lambda",LogUniform(0.001, 1000.0))
let posterior = Sampler.runmcmc(prior, log_likelihood, 1000, 100)
Sampler.print posterior

The last line of the above snippet prints credible intervals of the posterior distribution of the parameter:

Samples max log likelihood*prior = -2227.54, acceptance rate at sampling = 0.317
------------+------------+------------+------------+------------+------------+------------+------------+
       name |      lower |  lower 95% |  lower 68% |     median |  upper 68% |  upper 95% |      upper | isLog
------------+------------+------------+------------+------------+------------+------------+------------+
     lambda |      0.001 |    4.91295 |    4.94402 |    5.02152 |    5.10106 |    5.18737 |       1000 | true

See Filzbach tutorial for more information.

Samples & documentation

Documentation includes tutorials automatically generated from *.fsx files in the content folder. The API reference is automatically generated from Markdown comments in the library implementation.

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

namespace Angara
module Statistics

from Angara
val generator : MT19937

Full name: Index.generator
Multiple items
type MT19937 =
  new : copy:MT19937 -> MT19937
  new : seed:uint32 [] -> MT19937
  new : ?seed:uint32 -> MT19937
  private new : mt:uint32 [] * idx:int -> MT19937
  member bernoulli : p:float -> bool
  member private getIdx : int
  member private getMt : uint32 []
  member get_seed : unit -> uint32 []
  member normal : unit -> float
  member uniform_float64 : unit -> float
  ...

Full name: Angara.Statistics.MT19937

--------------------
new : ?seed:uint32 -> MT19937
new : seed:uint32 [] -> MT19937
new : copy:MT19937 -> MT19937
val distribution : Distribution

Full name: Index.distribution
union case Distribution.Normal: float * float -> Distribution
val draw : gen:MT19937 -> d:Distribution -> float

Full name: Angara.Statistics.draw
val fake_data : float list

Full name: Index.fake_data
union case Distribution.Poisson: mean: float -> Distribution
module Filzbach

from Angara
val log_likelihood : p:'a -> float

Full name: Index.log_likelihood
val p : 'a
val lambda : float
module Seq

from Microsoft.FSharp.Collections
val sumBy : projection:('T -> 'U) -> source:seq<'T> -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sumBy
val d : float
val log_pdf : d:Distribution -> v:float -> float

Full name: Angara.Statistics.log_pdf
val prior : obj

Full name: Index.prior
union case Distribution.LogUniform: float * float -> Distribution
val posterior : obj

Full name: Index.posterior
Fork me on GitHub