-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Data.Sample, start of stat-rethinking
- Loading branch information
Showing
6 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Main where | ||
|
||
import Data.Permutation (Permutation, permutations, getPermutationList) | ||
import Data.Sample (Sample, fromList, filter, sample2, sample3) | ||
import Data.Ratio (Ratio, (%)) | ||
|
||
import Prelude hiding (filter) | ||
|
||
main :: IO () | ||
main = putStrLn "hello!" | ||
|
||
|
||
|
||
|
||
|
||
-- | Ratio of observed outcomes to all possible outcomes for samples of length 3 | ||
-- | ||
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,1,0,0]) | ||
-- 1 % 8 | ||
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,0,0,0]) | ||
-- 3 % 64 | ||
pOutcomes :: Sample Int -- ^ observed sample | ||
-> Sample Int -- ^ hypothesis | ||
-> Ratio Int | ||
pOutcomes spl hypot = nsps % ntot | ||
where | ||
xs = samples3 hypot | ||
nsps = length $ filter (== spl) xs | ||
ntot = length xs | ||
|
||
-- | All possible samples of length 3 | ||
samples3 :: Sample Int -> Sample (Sample Int) | ||
samples3 xs = sample3 <$> xs <*> xs <*> xs | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{-# language DeriveFunctor, GeneralizedNewtypeDeriving #-} | ||
module Data.Sample (Sample, empty, cons, fromList | ||
, filter | ||
, sample2, sample3) where | ||
|
||
import Data.Foldable (Foldable(..)) | ||
import qualified Data.Sequence as S | ||
import Prelude hiding (filter) | ||
|
||
|
||
-- | Finite sample, internally represented as a 'S.Seq' (i.e. a finger tree) | ||
newtype Sample a = Sample { | ||
getSample_ :: S.Seq a | ||
} deriving (Eq, Functor, Applicative, Foldable) | ||
instance Show a => Show (Sample a) where | ||
show (Sample xs) = show $ toList xs | ||
|
||
sample2 :: a -> a -> Sample a | ||
sample2 a b = a `cons` (b `cons` empty) | ||
{-# INLINE sample2 #-} | ||
|
||
sample3 :: a -> a -> a -> Sample a | ||
sample3 a b c = fromList [a, b, c] | ||
{-# INLINE sample3 #-} | ||
|
||
-- | Filter a sample according to a predicate | ||
filter :: (a -> Bool) -> Sample a -> Sample a | ||
filter q (Sample s) = Sample $ S.filter q s | ||
|
||
-- | Empty sample | ||
empty :: Sample a | ||
empty = Sample S.empty | ||
|
||
-- | O(1) Left append | ||
cons :: a -> Sample a -> Sample a | ||
x `cons` s = Sample $ x S.<| getSample_ s | ||
{-# INLINE cons #-} | ||
|
||
fromList :: [a] -> Sample a | ||
fromList = Sample . S.fromList | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters