Skip to content

Commit c45ce6c

Browse files
committed
add Data.Sample, start of stat-rethinking
1 parent 5db460d commit c45ce6c

File tree

6 files changed

+99
-25
lines changed

6 files changed

+99
-25
lines changed

app/stat-rethinking/Main.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Main where
2+
3+
import Data.Permutation (Permutation, permutations, getPermutationList)
4+
import Data.Sample (Sample, fromList, filter, sample2, sample3)
5+
import Data.Ratio (Ratio, (%))
6+
7+
import Prelude hiding (filter)
8+
9+
main :: IO ()
10+
main = putStrLn "hello!"
11+
12+
13+
14+
15+
16+
-- | Ratio of observed outcomes to all possible outcomes for samples of length 3
17+
--
18+
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,1,0,0])
19+
-- 1 % 8
20+
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,0,0,0])
21+
-- 3 % 64
22+
pOutcomes :: Sample Int -- ^ observed sample
23+
-> Sample Int -- ^ hypothesis
24+
-> Ratio Int
25+
pOutcomes spl hypot = nsps % ntot
26+
where
27+
xs = samples3 hypot
28+
nsps = length $ filter (== spl) xs
29+
ntot = length xs
30+
31+
-- | All possible samples of length 3
32+
samples3 :: Sample Int -> Sample (Sample Int)
33+
samples3 xs = sample3 <$> xs <*> xs <*> xs
34+
35+
36+
37+

bayesian-inference.cabal

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ library
2828
Numeric.Math
2929
Data.Graph.Examples
3030
Data.Permutation
31+
Data.Sample
3132
other-modules: System.Random.MWC.Probability.Conditional
3233
build-depends: base >= 4.7 && < 5
3334
, algebraic-graphs
@@ -37,8 +38,8 @@ library
3738
, ghc-prim
3839
, logging-effect
3940
, massiv
40-
, microlens
41-
, microlens-mtl
41+
-- , microlens
42+
-- , microlens-mtl
4243
, mtl
4344
, mwc-probability
4445
, mwc-probability-transition
@@ -50,6 +51,14 @@ library
5051
, hspec
5152
, QuickCheck
5253

54+
executable stat-rethinking
55+
default-language: Haskell2010
56+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
57+
hs-source-dirs: app/stat-rethinking
58+
main-is: Main.hs
59+
build-depends: base
60+
, bayesian-inference
61+
5362
executable graph-export
5463
default-language: Haskell2010
5564
ghc-options: -threaded -rtsopts -with-rtsopts=-N

src/Data/Permutation.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Finite permutations
44
-}
55
module Data.Permutation (
6-
Permutation, permutation, getPermutation, permutations
6+
Permutation, permutation, getPermutation, getPermutationList, permutations
77
-- * Helper functions
88
, swaps, swapsEnum
99
) where
@@ -21,12 +21,16 @@ newtype Permutation a = Permutation {
2121
_getPermutation :: V.Vector a
2222
}
2323

24+
getPermutationList :: Permutation a -> [a]
25+
getPermutationList = V.toList . getPermutation
26+
2427
getPermutation :: Permutation a -> V.Vector a
2528
getPermutation = _getPermutation
2629

2730
instance Show a => Show (Permutation a) where
2831
show (Permutation pv) = show $ V.toList pv
2932

33+
-- | Compute all permutations of the given list
3034
permutations :: [a] -> [Permutation a]
3135
permutations xs = permSwap p0 `map` swaps n where
3236
p0 = permutation xs

src/Data/Sample.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{-# language DeriveFunctor, GeneralizedNewtypeDeriving #-}
2+
module Data.Sample (Sample, empty, cons, fromList
3+
, filter
4+
, sample2, sample3) where
5+
6+
import Data.Foldable (Foldable(..))
7+
import qualified Data.Sequence as S
8+
import Prelude hiding (filter)
9+
10+
11+
-- | Finite sample, internally represented as a 'S.Seq' (i.e. a finger tree)
12+
newtype Sample a = Sample {
13+
getSample_ :: S.Seq a
14+
} deriving (Eq, Functor, Applicative, Foldable)
15+
instance Show a => Show (Sample a) where
16+
show (Sample xs) = show $ toList xs
17+
18+
sample2 :: a -> a -> Sample a
19+
sample2 a b = a `cons` (b `cons` empty)
20+
{-# INLINE sample2 #-}
21+
22+
sample3 :: a -> a -> a -> Sample a
23+
sample3 a b c = fromList [a, b, c]
24+
{-# INLINE sample3 #-}
25+
26+
-- | Filter a sample according to a predicate
27+
filter :: (a -> Bool) -> Sample a -> Sample a
28+
filter q (Sample s) = Sample $ S.filter q s
29+
30+
-- | Empty sample
31+
empty :: Sample a
32+
empty = Sample S.empty
33+
34+
-- | O(1) Left append
35+
cons :: a -> Sample a -> Sample a
36+
x `cons` s = Sample $ x S.<| getSample_ s
37+
{-# INLINE cons #-}
38+
39+
fromList :: [a] -> Sample a
40+
fromList = Sample . S.fromList
41+
42+
43+

src/Numeric/Statistics/Inference/Bayes/Exact/VariableElimination.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import Control.Monad.Catch (MonadThrow(..))
2727
-- import qualified Data.Massiv.Array as A (Array, all, Comp(..), makeArray, Construct(..), Sz(..))
2828
-- import Data.Massiv.Array (Index, Ix1(..), D, (..:), ifoldlWithin', foldlWithin', Lower, Dim(..), Source)
2929
-- microlens
30-
import Lens.Micro (Lens(..), Lens', lens, (^.), (.~), (%~), Getting)
30+
-- import Lens.Micro (Lens(..), Lens', lens, (^.), (.~), (%~), Getting)
3131
-- microlens-mtl
32-
import Lens.Micro.Mtl (view, (%=))
32+
-- import Lens.Micro.Mtl (view, (%=))
3333
-- mtl
3434
import Control.Monad.State (MonadState(..), gets)
3535
import Control.Monad.Reader (MonadReader(..), asks)

stack.yaml

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,9 @@ resolver: lts-12.14
55
# resolver: nightly-2019-08-04
66
# resolver: nightly-2019-11-29
77

8-
# User packages to be built.
9-
# Various formats can be used as shown in the example below.
10-
#
11-
# packages:
12-
# - some-directory
13-
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
14-
# - location:
15-
# git: https://github.com/commercialhaskell/stack.git
16-
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
17-
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
18-
# extra-dep: true
19-
# subdirs:
20-
# - auto-update
21-
# - wai
22-
#
23-
# A package marked 'extra-dep: true' will only be built if demanded by a
24-
# non-dependency (i.e. a user package), and its test suites and benchmarks
25-
# will not be run. This is useful for tweaking upstream packages.
268
packages:
279
- .
28-
# Dependency packages to be pulled from upstream that are not in the resolver
29-
# (e.g., acme-missiles-0.3)
10+
3011
extra-deps:
3112
- mwc-probability-transition-0.3.0.3
3213
- algebraic-graphs-0.4@sha256:5d163af6f2f8c6729572f5378e1b0037b563c765930a957a135aa9d34e4518c5

0 commit comments

Comments
 (0)