Skip to content

Instantly share code, notes, and snippets.

@guipn
Created June 3, 2012 23:54
Show Gist options
  • Save guipn/2865475 to your computer and use it in GitHub Desktop.
Save guipn/2865475 to your computer and use it in GitHub Desktop.
Haskell/Poisson
{-
- Some functionality related to Poisson Processes.
-
- gpn
-}
module Poisson (
Process(..)
, pNt
, eNt
, eSn
, pSn
, factorial
) where
type Rate = Float
type Chance = Float
type Interval = Float
type Average = Float
type EventCount = Integer
-- λ = E[N(t)] / t
data Process = Process {
λ :: Rate
} deriving (Show)
factorial :: Integer -> Integer
factorial n = product [1 .. n]
-- P(N(t) = n)
pNt :: Process -> Interval -> EventCount -> Chance
pNt p t n | t == 0 = 0
| otherwise = (exp (negate mean) * (mean ** intN)) / facN
where mean = (λ p) * t
intN = fromIntegral n
facN = fromIntegral (factorial n)
-- E[N(t)]
eNt :: Process -> Interval -> Average
eNt p t = λ p * t
--
-- Sn is the time until the nth event.
-- Sn ~ Γ(n,λ)
--
-- P(T1 > t)
pT1 :: Process -> Interval -> Chance
pT1 p t = exp (negate (λ p) * t)
-- E[Sn]
eSn :: Process -> EventCount -> Interval
eSn p n = (fromIntegral n) / λ p
-- P(Sn < t)
pSn :: Process -> EventCount -> Interval -> Chance
pSn p n t = λ p * (exp (negate mean)) * mean ** (fromIntegral (n - 1)) / fac
where mean = λ p * t
fac = fromIntegral (factorial (n - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment