File: Normal.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 (63 lines) | stat: -rw-r--r-- 1,880 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
54
55
56
57
58
59
60
61
62
63
module Probability.Distribution.Normal where

import Probability.Random
import MCMC

foreign import bpcall "Distribution:" normal_density :: Double -> Double -> Double -> LogDouble
foreign import bpcall "Distribution:" normal_cdf :: Double -> Double -> Double -> Double
foreign import bpcall "Distribution:" normal_quantile :: Double -> Double -> Double -> Double
foreign import bpcall "Distribution:" sample_normal :: Double -> Double -> IO Double

data Normal = Normal Double Double

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

instance IOSampleable Normal where
    sampleIO (Normal mu sigma) = sample_normal mu sigma

instance HasPdf Normal where
    pdf (Normal mu sigma) x = normal_density mu sigma x

instance Dist1D Normal where
    cdf (Normal mu sigma) x = normal_cdf mu sigma x

instance ContDist1D Normal where
    quantile (Normal mu sigma) p = normal_quantile mu sigma p

instance MaybeMean Normal where
    maybeMean (Normal mu _) = Just mu

instance Mean Normal

instance MaybeVariance Normal where
    maybeStdDev   (Normal _ sigma) = Just $ sigma

instance Variance Normal

instance HasAnnotatedPdf Normal where
    annotated_densities dist@(Normal mu sigma) x = do
                                               in_edge "mu" mu
                                               in_edge "sigma" sigma
                                               return ([pdf dist x], ())


{-
  So, to sample something for MCMC we need
  - pure sampling routine.
  + MCMC-specific:
    - effect
    - (dist_name dist)
    - annotated_densities dist
OR
  - sampling routine made up of other sampling actions.
 -}
instance Sampleable Normal where
    sample dist@(Normal mu sigma) = RanDistribution2 dist normal_effect

normal_bounds = realLine
normal_effect x = add_move $ sliceSample x normal_bounds

normal mu sigma = Normal mu sigma