commitcaf2c4a3db97c660c99cf29b088fe171a7c5b88aparent80164e3efa5ed478ac8c8f548722d5651cf6f8a1Author:Jared Tobin <jared@jtobin.ca>Date:Sat, 19 Oct 2013 16:34:58 +1300 Update examples. Kind of mind-blowing.Diffstat:

M | src/Measurable.hs | | | 20 | +++++++++++++------- |

M | tests/Test.hs | | | 26 | +++++++++++++++++++++++++- |

2 files changed, 38 insertions(+), 8 deletions(-)diff --git a/src/Measurable.hs b/src/Measurable.hs@@ -71,7 +71,8 @@ push :: (a -> b) -> Measure a -> Measure b push f mu = Measure pushforward where pushforward g = measure mu $ g . f --- | The volume is obtained by integrating against a constant. +-- | The volume is obtained by integrating against a constant. This is '1' for +-- any probability measure. volume :: Measure a -> Double volume mu = measure mu (const 1) @@ -99,16 +100,21 @@ convolute mu nu = Measure $ \f -> measure nu $ \y -> measure mu $ \x -> f (x + y) +-- | Measure subtraction. (does this make sense?) +msubtract mu nu = Measure $ \f -> measure nu + $ \y -> measure mu + $ \x -> f (x - y) + +-- | Measure multiplication. (does this make sense?) +prod :: Num a => Measure a -> Measure a -> Measure a +prod mu nu = Measure $ \f -> measure nu + $ \y -> measure mu + $ \x -> f (x * y) + -- | The (sum) identity measure. identityMeasure :: Fractional a => Measure a identityMeasure = fromObservations [] --- | Lebesgue measure. -lebesgue :: Double -> Double -> Measure Double -lebesgue a b = fromDensity rectangle - where rectangle x | x >= a && x <= b = 1 - | otherwise = 0 - -- | Simple average. average :: Fractional a => [a] -> a average xs = fst $ foldl'diff --git a/tests/Test.hs b/tests/Test.hs@@ -13,10 +13,12 @@ import Control.Monad import Measurable import Statistics.Distribution hiding (mean, variance) import Statistics.Distribution.Normal +import Statistics.Distribution.ChiSquared import System.Random.MWC import System.Random.MWC.Distributions -standardNormal = density $ normalDistr 0 1 +standardNormal = density $ normalDistr 0 1 +genLocationNormal m = density $ normalDistr m 1 main = do expSamples <- withSystemRandom . asGenIO $ \g -> @@ -37,3 +39,25 @@ main = do putStrLn $ "let X ~ N(0, 1), Y ~ observed. mean of exp(cos X + sin Y): " ++ show (mean eta) + putStrLn "" + putStrLn "and now some 'woah, this actally seems to make sense' examples:" + putStrLn "" + + let iota = mu `msubtract` mu + + putStrLn $ "let X, Y be independent N(0, 1). mean of X - Y: " ++ + show (mean iota) + putStrLn $ "let X, Y be independent N(0, 1). variance of X - Y: " ++ + show (variance iota) + + -- + + let phi = fromDensity $ genLocationNormal 2 + xi = fromDensity $ genLocationNormal 3 + zeta = prod phi xi + + putStrLn $ "let X ~ N(2, 1), Y ~ N(3, 1). mean of XY (should be 6) " ++ + show (mean zeta) + putStrLn $ "let X ~ N(2, 1), Y ~ N(3, 1). variance of XY (should be 14) " ++ + show (variance zeta) +