0 people like it.

Binomial probabilities

Simple function for commute binomial probabilities. For quick summary about binomial distribution: 1.) There are a fixed number of trials (n). 2.) Each trial has two possible outcomes: success of failure 3.) The probability of success (p) is the same for each trial. 4.) The trials are independent, meaning the outcome of one trial doesn't influence that of any other.

 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: 
31: 
32: 
33: 
(* Factorial function *)
let rec (!!) x =
    if x = 1 || x = 0 then 1
    else x * !! (x-1)

(* Choose function *)
let (+?) (n:int) (x:int) =
    !!n / (!!x * !!(n - x))

(*
Binomal probabilities formula 
n is the fixed number of trials.
x is the specified number of successes
n - x is the number of failures
p is probability of sucess on any given trial
*)
let binomal (n:int) (x:int) (p:float) =
    (float (n +? x)) * (p ** (float x)) * ((1.0 - p) ** (float (n - x)))

(* Function for binomial probabilities *)
let ``binomal probabilities`` (data : int list) (p:float) : float list =
    (* We are always involve 0 as input random variable *)
    let n = data.Length - 1
    data
    |> List.map(fun x -> binomal n x p)

let fact3 = !!3
let choose = (3 +? 2)
let b = binomal 3 0 0.30
let p_list = ``binomal probabilities`` [0..3] 0.30 
(* The first result says what is probability of no success in my binomial distribution
   the second result says what is probability of one success in my binomial distribution 
   and so on ... *)
val x : int
val n : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val binomal : n:int -> x:int -> p:float -> float

Full name: Script.binomal
val p : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val ( binomal probabilities ) : data:int list -> p:float -> float list

Full name: Script.( binomal probabilities )
val data : int list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
property List.Length: int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val fact3 : int

Full name: Script.fact3
val choose : int

Full name: Script.choose
val b : float

Full name: Script.b
val p_list : float list

Full name: Script.p_list
Raw view Test code New version

More information

Link:http://fssnip.net/nu
Posted:9 years ago
Author:Martin Bodocky
Tags: statistics , binomal , distributions