File: Beta.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 (53 lines) | stat: -rw-r--r-- 1,602 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
48
49
50
51
52
53
module Probability.Distribution.Beta where

import Probability.Random
import MCMC (sliceSample)

foreign import bpcall "Distribution:"  beta_density   :: Double -> Double -> Double -> LogDouble
foreign import bpcall "Distribution:"  beta_cdf       :: Double -> Double -> Double -> Double
foreign import bpcall "Distribution:"  beta_quantile  :: Double -> Double -> Double -> Double
foreign import bpcall "Distribution:"  sample_beta    :: Double -> Double -> IO Double

data Beta = Beta Double Double

instance Dist Beta where
    type Result Beta = Double
    dist_name _ = "Beta"

instance IOSampleable Beta where
    sampleIO (Beta a b) = sample_beta a b

instance HasPdf Beta where
    pdf (Beta a b) x = beta_density a b x

instance Dist1D Beta where
    cdf (Beta a b) p = beta_cdf a b p
    lower_bound _ = Just 0
    upper_bound _ = Just 1

instance ContDist1D Beta where
    quantile (Beta a b) p = beta_quantile a b p

instance MaybeMean Beta where
    maybeMean (Beta a b) = Just (a * b)

instance Mean Beta

instance MaybeVariance Beta where
    maybeVariance (Beta a b) = Just $ a * b /((a+b)^2)/(a+b+a)

instance Variance Beta

instance HasAnnotatedPdf Beta where
    annotated_densities dist@(Beta a b) x = do
                                        in_edge "a" a
                                        in_edge "b" b
                                        return ([beta_density a b x],())

instance Sampleable Beta where
    sample dist@(Beta a b) = RanDistribution2 dist beta_effect

beta_bounds = between 0 1
beta_effect x = add_move $ sliceSample x beta_bounds

beta a b = Beta a b