probability 0.2.8 → 0.2.8.1
raw patch · 7 files changed
+60/−53 lines, 7 files
Files
- probability.cabal +8/−7
- src/Numeric/Probability/Example/Alarm.hs +14/−12
- src/Numeric/Probability/Example/Barber.hs +1/−1
- src/Numeric/Probability/Example/DiceAccum.hs +5/−7
- src/Numeric/Probability/Example/Queuing.hs +10/−10
- src/Numeric/Probability/Example/TreeGrowth.hs +5/−6
- src/Numeric/Probability/Visualize.hs +17/−10
probability.cabal view
@@ -1,6 +1,6 @@ Cabal-Version: 1.24 Name: probability-Version: 0.2.8+Version: 0.2.8.1 License: BSD3 License-File: COPYRIGHT Author: Martin Erwig <erwig@eecs.oregonstate.edu>, Steve Kollmansberger@@ -14,14 +14,15 @@ The monad is similar to the List monad for non-deterministic computations, but extends the List monad by a measure of probability. Small interface to R plotting.-Tested-With: GHC==7.0.2, GHC==7.2.2, GHC==7.4.2, GHC==7.8.2-Tested-With: GHC==8.6.5, GHC==8.8.3+Tested-With: GHC==7.*, GHC==8.*, GHC==9.* Build-Type: Simple Extra-Source-Files:- Changes.md README ToDo +Extra-Doc-Files:+ Changes.md+ Source-Repository head type: darcs location: http://code.haskell.org/~thielema/probability/@@ -29,7 +30,7 @@ Source-Repository this type: darcs location: http://code.haskell.org/~thielema/probability/- tag: 0.2.8+ tag: 0.2.8.1 Flag splitBase@@ -41,8 +42,8 @@ transformers >=0.4 && <0.7 If flag(splitBase) Build-Depends:- containers >=0.1 && <0.7,- random >=1.0 && <1.3,+ containers >=0.1 && <0.9,+ random >=1.0 && <1.4, base >=2 && <5 Else Build-Depends:
src/Numeric/Probability/Example/Alarm.hs view
@@ -42,20 +42,22 @@ m a0 = if a0 then flp 0.9 else flp 0.1 -- | calculate the full joint distribution-data Burglary = B { burglary :: Bool,- earthquake :: Bool,- alarm :: Bool,- john :: Bool,- mary :: Bool }- deriving (Eq, Ord, Show)+data Burglary =+ B {+ burglary :: Bool,+ earthquake :: Bool,+ alarm :: Bool,+ john :: Bool,+ mary :: Bool+ } deriving (Eq, Ord, Show) bJoint :: Dist Burglary-bJoint = do b' <- b -- burglary- e' <- e -- earthquake- a' <- a b' e' -- alarm- j' <- j a' -- john- m' <- m a' -- mary- return (B b' e' a' j' m')+bJoint = do b' <- b -- burglary+ e' <- e -- earthquake+ a' <- a b' e' -- alarm+ j' <- j a' -- john+ m' <- m a' -- mary+ return (B b' e' a' j' m') -- | what is the probability that mary calls given that john calls? pmj :: Probability
src/Numeric/Probability/Example/Barber.hs view
@@ -45,7 +45,7 @@ -- * category data Category = ThreeOrLess | FourToTen | MoreThanTen- deriving (Eq,Ord,Show)+ deriving (Eq,Ord,Show) cat :: Time -> Category cat n | n <= 3 = ThreeOrLess
src/Numeric/Probability/Example/DiceAccum.hs view
@@ -16,7 +16,9 @@ import Numeric.Probability.Example.Dice (Die, ) +import Data.Maybe.HT (toMaybe) + type Score = Int @@ -26,13 +28,9 @@ roll :: Fractional prob => Trans.T prob (Maybe Score) roll = maybe- (return Nothing)- (\score -> flip fmap die $- \spots ->- -- where is my beloved 'toMaybe' ?- if spots == 3- then Nothing- else Just (score + spots))+ (return Nothing)+ (\score -> flip fmap die $+ \spots -> toMaybe (spots /= 3) (score + spots)) continue :: Score -> Bool continue scoreInt =
src/Numeric/Probability/Example/Queuing.hs view
@@ -62,15 +62,15 @@ mEvent :: Int -> Time -> Events a -> Queue a -> [State a] mEvent _ _ [] [] = []-mEvent n 0 ((c,(s,a)):es) q = mEvent n a es (q++[(c,s)])+mEvent n 0 ((c,(s,a)):es) q = mEvent n a es (q++[(c,s)]) mEvent n a es [] = ([],a):mEvent n 0 es []-mEvent n _ [] q = (q,s):mEvent n 0 [] (mServe n s q)- where s = mTimeStep n q+mEvent n _ [] q = (q,s):mEvent n 0 [] (mServe n s q)+ where s = mTimeStep n q mEvent n a es q = if a < s then (q,a) : mEvent n 0 es (mServe n a q) else (q,s) : mEvent n (a-s) es (mServe n s q)- where s = mTimeStep n q+ where s = mTimeStep n q -- | decrease served customers remaining time by specified amount@@ -103,15 +103,15 @@ rSystem :: Int -> REvents a -> Rnd.T (System a) rSystem n re = do- e <- rBuildEvents re- return (mSystem n e)+ e <- rBuildEvents re+ return (mSystem n e) rBuildEvents :: REvents a -> Rnd.T (Events a) rBuildEvents ((a,(dt,tt)):ex) = do- rest <- rBuildEvents ex- t <- Rnd.pick dt- nt <- Rnd.pick $ tt t- return ((a,(t,nt)):rest)+ rest <- rBuildEvents ex+ t <- Rnd.pick dt+ nt <- Rnd.pick $ tt t+ return ((a,(t,nt)):rest) rBuildEvents [] = return [] rmSystem :: Ord a => Int -> Int -> REvents a -> RDist (System a)
src/Numeric/Probability/Example/TreeGrowth.hs view
@@ -20,7 +20,7 @@ type Height = Int data Tree = Alive Height | Hit Height | Fallen- deriving (Ord,Eq,Show)+ deriving (Ord,Eq,Show) grow :: Trans Tree grow (Alive h) = Dist.normal (map Alive [h+1..h+5])@@ -103,9 +103,8 @@ myPlot2 = figP figure{title="Tree Growth",xLabel="Height (m)", yLabel="Probability"} (autoColor [- plotD ((5 *. evolve) (Alive 0) >>= height)- ])-+ plotD ((5 *. evolve) (Alive 0) >>= height)+ ]) --} p1, p2, p3, p4, p5, p6 :: Vis@@ -117,7 +116,7 @@ p3 = figP figure{title="Tree Growth", xLabel="Height (ft)", yLabel="Probability"}- [plotD $ Dist.map height (tree 5 seed)]+ [plotD $ Dist.map height (tree 5 seed)] p4 = figP figure{title="Tree Growth",@@ -140,7 +139,7 @@ xLabel="Height (ft)", yLabel="Probability"} (map heightCurve- [(3,Blue),(5,Green),(7,Red)])+ [(3,Blue),(5,Green),(7,Red)]) done :: Tree -> Bool
src/Numeric/Probability/Visualize.hs view
@@ -1,5 +1,6 @@ module Numeric.Probability.Visualize where +import qualified Numeric.Probability.Distribution as Dist import qualified Numeric.Probability.Random as Rnd import Numeric.Probability.Expectation (ToFloat, FromFloat, toFloat, fromFloat, )@@ -7,10 +8,9 @@ (Dist, RDist, ) import Numeric.Probability.PrintList (asTuple, ) -import qualified Numeric.Probability.Distribution as Dist+import qualified Data.Set as Set import Control.Monad (when, )-import Data.List (nub, sort, ) {- TO DO:@@ -122,7 +122,7 @@ -- plotD :: ToFloat a => Dist a -> Plot --plotD d = plot{ys = map (\x->(dp $ prob' x d')) (extract d'),--- xs = extract d'}+-- xs = extract d'} plotD d = let (tfl, pdl) = unzip $ Dist.sortElem $@@ -156,9 +156,9 @@ yls :: [Float] -> Plot -> Plot yls xl p = p{xs=x', ys=y'}- where t = zip (xs p) (ys p)- t' = metaTuple xl t- (x', y') = unzip t'+ where t = zip (xs p) (ys p)+ t' = metaTuple xl t+ (x', y') = unzip t' metaTuple :: [Float] -> [(Float,Float)] -> [(Float,Float)] metaTuple (x:xl) ((p,v):px) | p == x = (p,v):(metaTuple xl px)@@ -189,11 +189,18 @@ fig :: [Plot] -> Vis fig = figP figure ++{- |+Example session:++> *Numeric.Probability.Visualize> figP figure [plotL [3,1,4,1,5,9::Float]]+> *Numeric.Probability.Visualize> :! Rscript FuSE.R+-} figP :: FigureEnv -> [Plot] -> Vis-figP fe ps = do let xl = sort $ nub $ concatMap xs ps- let minx = minimum xl--- let maxx = maximum xl- let n = length xl+figP fe ps = do let xset = Set.fromList $ concatMap xs ps+ let minx = Set.findMin xset+ let n = Set.size xset+ let xl = Set.toList xset let ys' = map ys (map (yls xl) ps) -- yls xl ps let miny = minimum (map minimum ys') let maxy = maximum (map maximum ys')