mcmc-types

Common types for implementing MCMC algorithms.
Log | Files | Refs | README | LICENSE

commit f16cb0ad01080b16182c6b22ec728d140619a38e
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri, 10 Apr 2015 18:53:42 +1200

Initial commit.

Diffstat:
A.gitignore | 2++
AData/Sampling/Types.hs | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ALICENSE | 20++++++++++++++++++++
ASetup.hs | 2++
Amcmc-types.cabal | 19+++++++++++++++++++
5 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +dist +*swp diff --git a/Data/Sampling/Types.hs b/Data/Sampling/Types.hs @@ -0,0 +1,70 @@ + +module Data.Sampling.Types ( + Parameter(..) + , Parameters + , Observations + , createTargetWithGradient + , createTargetWithoutGradient + , handleGradient + , lTarget + , glTarget + ) where + +import Data.Map (Map) + +data Parameter = + Continuous Double + | Discrete Int + | ContinuousVector [Double] + | DiscreteVector [Int] + deriving (Eq, Show) + +instance Num Parameter where + (Continuous a) + (Continuous b) = Continuous (a + b) + (Discrete a) + (Discrete b) = Discrete (a + b) + _ + _ = noInstanceError + + (Continuous a) - (Continuous b) = Continuous (a - b) + (Discrete a) - (Discrete b) = Discrete (a - b) + _ - _ = noInstanceError + + (Continuous a) * (Continuous b) = Continuous (a * b) + (Discrete a) * (Discrete b) = Discrete (a * b) + _ * _ = noInstanceError + + abs (Continuous a) = Continuous (abs a) + abs (Discrete a) = Discrete (abs a) + abs _ = noInstanceError + + signum (Continuous a) = Continuous (signum a) + signum (Discrete a) = Discrete (signum a) + signum _ = noInstanceError + + fromInteger = Continuous . fromInteger + +noInstanceError :: t +noInstanceError = error + "only continuous and discrete parameters support ring operations" + +-- | A @Target@ consists of a function from parameter space to the reals, as +-- well as possibly a gradient. +data Target = Target { + lTarget :: Parameters -> Double + , glTarget :: Maybe (Parameters -> Parameters) + } + +createTargetWithGradient + :: (Parameters -> Double) -> (Parameters -> Parameters) -> Target +createTargetWithGradient f g = Target f (Just g) + +createTargetWithoutGradient :: (Parameters -> Double) -> Target +createTargetWithoutGradient f = Target f Nothing + +handleGradient :: Maybe t -> t +handleGradient Nothing = error "handleGradient: no gradient provided" +handleGradient (Just g) = g + +type Environment a = Map String a +type Parameters = Environment Parameter +type Observations = Environment Parameter + diff --git a/LICENSE b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2015 Jared Tobin + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Setup.hs b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/mcmc-types.cabal b/mcmc-types.cabal @@ -0,0 +1,19 @@ +name: mcmc-types +version: 0.1.0.0 +synopsis: Common types for sampling. +description: Common types for sampling. +homepage: http://github.com/jtobin/mcmc-types +license: MIT +license-file: LICENSE +author: Jared Tobin +maintainer: jared@jtobin.ca +build-type: Simple +cabal-version: >=1.10 + +library + exposed-modules: Data.Sampling.Types + default-language: Haskell2010 + build-depends: + base >= 4.7 && < 4.8 + , containers +