haskell / statistics

A fast, high quality library for computing with statistics in Haskell.
http://hackage.haskell.org/package/statistics
BSD 2-Clause "Simplified" License
300 stars 68 forks source link

Enumerating discrete distributions #89

Open dbeacham opened 8 years ago

dbeacham commented 8 years ago

I have quite a few applications where I need to compute the pdf (or cdf) of a discrete distribution over the whole range [n, n+1, ..., n+N]. At the moment, that requires calling the pdf (or cdf) function for each value in the range. However, for many distributions it's much quicker to calculate the first case and then use your knowledge of the distribution to calculate the values for [n+1, ..., n+N]. You then end up with an unfolding operation:

pdfFromTo :: (DiscreteDistribution d, Vector v Double) => d -> Int -> Int -> v Double

which for the binomial distribution would look like

-- assume 0 <= n0 <= n1 <= n for simplicity
pdfFromTo binDist n0 n1 = iterateN (n1 - n0) (\x -> x * (n-k) * p  / (k+1) / (1-p)) (probability binDist n0)
Shimuuar commented 7 years ago

Sorry for long silence

So you're proposing to add method with default implementation to DiscreteDistribution type class, right? I think it's nice addition, I won't add it myself so I mark this issue as "help needed"

dbeacham commented 7 years ago

I'd completely forgotten about this!

Yeah - the default can just do the dumb thing of calling the pdf or cdf multiple times.

I'll hopefully have a bit of free time over the next week - will try and get a pull request over to you.