measurable

A simple shallowly-embedded DSL for dealing with measures.
Log | Files | Refs | README | LICENSE

commit 80164e3efa5ed478ac8c8f548722d5651cf6f8a1
parent c2cad7d41c1000eb85ae5414dfc46f55dde521aa
Author: Jared Tobin <jared@jtobin.ca>
Date:   Sat, 19 Oct 2013 13:58:11 +1300

Add volume & a Lebesgue measure analog.

Diffstat:
Msrc/Measurable.hs | 19+++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/Measurable.hs b/src/Measurable.hs @@ -2,10 +2,10 @@ module Measurable where +import Control.Applicative import Control.Monad import Data.List import Data.Monoid -import Control.Applicative import Numeric.Integration.TanhSinh -- | A measure is a set function from some sigma-algebra to the extended real @@ -71,6 +71,10 @@ 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. +volume :: Measure a -> Double +volume mu = measure mu (const 1) + -- | The mean is obtained by integrating against the identity function. mean :: Measure Double -> Double mean mu = measure mu id @@ -81,7 +85,8 @@ variance mu = measure mu (^ 2) - mean mu ^ 2 -- | Create a measure from a collection of observations from some distribution. fromObservations :: Fractional a => [a] -> Measure a -fromObservations xs = Measure $ \f -> average . map f $ xs +fromObservations xs = Measure $ \f -> + average . map f $ xs -- | Create a measure from a density function. fromDensity :: (Double -> Double) -> Measure Double @@ -96,8 +101,14 @@ convolute mu nu = Measure $ \f -> measure nu -- | The (sum) identity measure. identityMeasure :: Fractional a => Measure a -identityMeasure = fromObservations [0] - +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'