Created
October 13, 2013 15:07
-
-
Save misterspeedy/6963358 to your computer and use it in GitHub Desktop.
Cumulative binomial distribution probability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let CumulativeDistribution (x : int) (n : int) (p : float) = | |
let EssentiallyZero = 10E-12 | |
let m = (float(n) * p) |> truncate |> int | |
let CalcCurrent value k = | |
if k > m then | |
value * float(n - k + 1) * p / (float(k) * (1. - p)) | |
else | |
value * float(k + 1) * (1. - p) / (float(n - k) * p) | |
let Done k = if k > m then k > n else k < 0 | |
let NextK k = if k > m then k + 1 else k - 1 | |
let rec Calculate k totalUnscaledProbability previous unscaled = | |
let current = CalcCurrent previous k | |
let totalUnscaledProbability' = totalUnscaledProbability + current | |
let unscaled' = | |
if (k <= x) then | |
unscaled + current | |
else | |
unscaled | |
if (Done k) && (current <= EssentiallyZero) then | |
unscaled', totalUnscaledProbability' | |
else | |
Calculate (NextK k) totalUnscaledProbability' current unscaled' | |
let InitialUnscaled = if (m <= x) then 1. else 0. | |
let UnscaledResultAboveM, TotalUnscaledProbabilityAboveM = Calculate (m+1) 1. 1. InitialUnscaled | |
let UnscaledResult, TotalUnscaledProbability = Calculate (m-1) TotalUnscaledProbabilityAboveM 1. UnscaledResultAboveM | |
UnscaledResult / TotalUnscaledProbability |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment