MyNixOS website logo
Description

Easy and reasonably efficient probabilistic programming and random generation.

Easy and reasonably efficient probabilistic programming and random generation

This library gives a common language to speak about probability distributions and random generation, by wrapping both, when necessary, in a RandT monad defined in Math.Probable.Random. This module also provides a lot of useful little combinators for easily describing how random values for your types should be generated.

In Math.Probable.Distribution, you'll find functions for generating random values that follow any distribution supported by mwc-random.

In Math.Probable.Distribution.Finite, you'll find an adaptation of Eric Kidd's work on probability monads (from here).

You may want to check the examples bundled with this package, viewable online at https://github.com/alpmestan/probable/tree/master/examples. One of these examples is simple enough to be worth reproducing here.

module Main where

import Control.Applicative
import Control.Monad
import Math.Probable

import qualified Data.Vector.Unboxed as VU

data Person = Person Int    -- ^ age
                     Double -- ^ weight (kgs)
                     Double -- ^ salary (e.g euros)
    deriving (Eq, Show)

person :: RandT IO Person
person =
    Person <$> uniformIn (1, 100)
           <*> uniformIn (2, 130)
           <*> uniformIn (500, 10000)

randomPersons :: Int -> IO [Person]
randomPersons n = mwc $ listOf n person

randomDoubles :: Int -> IO (VU.Vector Double)
randomDoubles n = mwc $ vectorOf n double

main :: IO ()
main = do
    randomPersons 10 >>= mapM_ print
    randomDoubles 10 >>= VU.mapM_ print

Please report any feature request or problem, either by email or through github's issues/feature requests.

probable

Build Status

Simple random value generation for haskell, using an efficient random generator and minimizing system calls. But the library also lets you work with distributions over a finite set, adapting code from Eric Kidd's posts, and all the usual distributions covered in the statistics package.

You can see how it looks in examples, or below. You can view the documentation for 0.1 here.

Example

Simple example of random generation for your types, using probable.

module Main where

import Control.Applicative
import Control.Monad
import Math.Probable

import qualified Data.Vector.Unboxed as VU

data Person = Person 
    { age    :: Int
    , weight :: Double
    , salary :: Int
    } deriving (Eq, Show)

person :: RandT IO Person
person = 
    Person <$> intIn (1, 100)
           <*> doubleIn (2, 130)
           <*> intIn (500, 10000)

randomPersons :: Int -> IO [Person]
randomPersons n = mwc $ listOf n person

randomDoubles :: Int -> IO (VU.Vector Double)
randomDoubles n = mwc $ vectorOf n double

main :: IO ()
main = do
	randomPersons 10 >>= mapM_ print
	randomDoubles 10 >>= VU.mapM_ print

Distributions over finite sets, conditional probabilities and random sampling.

module Main where

import Math.Probable

import qualified Data.Vector as V

data Book = Interesting 
		  | Boring
	deriving (Eq, Show)

bookPrior :: Finite d => d Book
bookPrior = weighted [ (Interesting, 0.2) 
					 , (Boring, 0.8) 
					 ]

twoBooks :: Finite d => d (Book, Book)
twoBooks = do
	book1 <- bookPrior
	book2 <- bookPrior
	return (book1, book2)

sampleBooks :: RandT IO (V.Vector Book)
sampleBooks = vectorOf 10 bookPrior

oneInteresting :: Fin (Book, Book)
oneInteresting = bayes $ do
	(b1, b2) <- twoBooks
	condition (b1 == Interesting || b2 == Interesting)
	return (b1, b2)

main :: IO ()
main = do
	print $ exact bookPrior
	mwc sampleBooks >>= print
	print $ exact twoBooks
	print $ exact oneInteresting

Contact

This library is written and maintained by Alp Mestanogullari.

Feel free to contact me for any feedback, comment, suggestion, bug report and what not.

Metadata

Version

0.1.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows