Skip to content

Instantly share code, notes, and snippets.

@ligfx
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save ligfx/175f9fa5786e51526f2d to your computer and use it in GitHub Desktop.

Select an option

Save ligfx/175f9fa5786e51526f2d to your computer and use it in GitHub Desktop.
-- Run me with runhaskell gibbs.hs
-- Compile me with ghc gibbs.hs -main-is Gibbs
module Gibbs where
import Prelude hiding (putStrLn)
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.Aeson as Aeson (encode)
import Data.Random (sampleFrom, RVar)
import qualified Data.Random.Distribution.Normal as N (doubleStdNormal, normal)
import Data.Vector (singleton)
import System.Random.MWC as MWC (initialize)
stdNormal = N.doubleStdNormal
normal :: Double -> Double -> RVar Double
normal = N.normal
rollM :: Monad m => Int -> (a -> m a) -> a -> m [a]
rollM 0 f initial = return []
rollM n f initial = do
x <- f initial
xs <- rollM (n - 1) f x
return $ initial : xs
genPref = rollM 100 (\x -> normal x 0.007) 0.49
main = do
mwc <- MWC.initialize (singleton 14)
pref <- sampleFrom mwc genPref
B.putStrLn $ Aeson.encode pref
source("runhaskell.R")
plot(runhaskellJ("Gibbs.main"), type="l")
library(stringr)
runhaskell <- function(name) {
module <- unlist(str_split(name, "\\.", n=2))[1]
return(system(paste("export LD_LIBRARY_PATH=/usr/local/lib && echo 'import qualified ", module, "; main = ", name, "' | runhaskell"), intern=TRUE))
}
library(jsonlite)
library(functional)
runhaskellJ <- Compose(runhaskell, fromJSON)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment