File: NegativeBinomial.hs

package info (click to toggle)
bali-phy 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,392 kB
  • sloc: cpp: 120,442; xml: 13,966; haskell: 9,975; python: 2,936; yacc: 1,328; perl: 1,169; lex: 912; sh: 343; makefile: 26
file content (47 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Probability.Distribution.NegativeBinomial where

import Probability.Random
import MCMC

foreign import bpcall "Distribution:" negative_binomial_density :: Int -> Double -> Int -> LogDouble
foreign import bpcall "Distribution:" sample_negative_binomial :: Int -> Double -> IO Int

data NegativeBinomial = NegativeBinomial Int Prob

instance Dist NegativeBinomial where
    type Result NegativeBinomial = Int

instance IOSampleable NegativeBinomial where
    sampleIO (NegativeBinomial r p) = sample_negative_binomial r p

instance HasPdf NegativeBinomial where
    pdf (NegativeBinomial r p) = negative_binomial_density r p

instance Dist1D NegativeBinomial where
    cdf (NegativeBinomial r p) = undefined
    lower_bound _ = Just 0

instance MaybeMean NegativeBinomial where
    maybeMean (NegativeBinomial r p) = Just $ r * toFloating $ (1-p)/p

instance Mean NegativeBinomial where

instance MaybeVariance NegativeBinomial where
    maybeVariance (NegativeBinomial r p) = Just $ r * toFloating $ (1-p)/(p*p)

instance Variance NegativeBinomial

instance HasAnnotatedPdf NegativeBinomial where
    annotated_densities dist x = return [ pdf dist x ]

instance Sampleable NegativeBinomial where
    sample dist@(NegativeBinomial r p) = RanDistribution2 dist (negative_binomial_effect r)

negative_binomial_bounds = integer_above 0

negative_binomial_effect r x = do
    add_move $ sliceSampleInteger x negative_binomial_bounds
    add_move $ incDecMH x negative_binomial_bounds

negativeBinomial :: Int -> Double -> NegativeBinomial
negativeBinomial r p = NegativeBinomial r (toFloating p)