Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize from double #171

Closed
wants to merge 158 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
3263e4b
analytic
Nov 23, 2021
9b79de6
docs
Dec 18, 2021
c4ae117
docs
Dec 19, 2021
9f3e49b
docs
Dec 26, 2021
e750b1e
docs
Jan 16, 2022
99d4c04
inference
reubenharry Mar 15, 2022
6478c0a
executable docs
reubenharry Mar 18, 2022
97517a2
more comments, added BayesianModel
reubenharry Mar 21, 2022
4577135
Remove MaybeT and use ExceptT
reubenharry Mar 23, 2022
e9b7158
remove vscode and update gitignore
reubenharry Mar 23, 2022
6c1214a
delete additional changes: Failure
reubenharry Mar 23, 2022
1bbdb2d
delete broken files
reubenharry Mar 23, 2022
003aca4
update test for enumerator
reubenharry Mar 23, 2022
31d3dd1
update models
reubenharry Mar 27, 2022
a48d806
tips on inference
reubenharry Mar 27, 2022
cb100df
density
reubenharry Mar 27, 2022
530f4d9
analytic
reubenharry Mar 29, 2022
4d2a447
analytic
reubenharry Mar 29, 2022
a057100
more pipes
reubenharry Mar 30, 2022
344fb10
update docs
reubenharry Mar 31, 2022
91f968b
linear outliers as in Gen tutorial
reubenharry Apr 1, 2022
0909ea9
update docs
reubenharry Apr 7, 2022
4ef4ccd
cleanup SMC.hs
reubenharry Apr 7, 2022
aa18cfc
more cleanup, and explicit imports
reubenharry Apr 7, 2022
10e464e
remove Rejection
reubenharry Apr 7, 2022
42a8a40
cleanup
reubenharry Apr 8, 2022
753e72a
enumerate
reubenharry Apr 8, 2022
e87ab45
binning
reubenharry Apr 8, 2022
7787b2a
comment
reubenharry Apr 11, 2022
ae590fc
nested inference example
reubenharry Apr 18, 2022
105a63f
found bug in Gamma
reubenharry Apr 25, 2022
a8d9a17
debugging
reubenharry Apr 26, 2022
f63cd79
add conjugacy tests
reubenharry Apr 28, 2022
5ea0f16
working branch
reubenharry Apr 28, 2022
b446c47
tried symbolic
reubenharry Apr 29, 2022
00f087a
no symbolic
reubenharry May 2, 2022
dc065ea
Merge branch 'analytic' into models
reubenharry May 2, 2022
84633be
remove reflection
reubenharry May 2, 2022
8e899cd
Merge branch 'analytic' into models
reubenharry May 2, 2022
24f6343
midway: need to finish hmm and enumerator:
reubenharry May 2, 2022
5bd1837
update docs
reubenharry May 4, 2022
b08e9dd
DP mixture: remove
reubenharry May 9, 2022
5875b43
build docs manually
reubenharry May 9, 2022
656ff50
insegel theme
reubenharry May 9, 2022
e49115e
stanford theme
reubenharry May 9, 2022
e8236f0
lint
reubenharry May 9, 2022
aab90b0
lint
reubenharry May 10, 2022
84dd4f0
ormulo
May 10, 2022
072137a
nix ormolu fixes:
May 10, 2022
3bce561
merge main
reubenharry May 11, 2022
803327b
docs
reubenharry May 12, 2022
01512ec
trying to fix mathjax
reubenharry May 15, 2022
54814c1
Merge branch 'master' into docs
reubenharry May 15, 2022
69173bb
merge docs
reubenharry May 15, 2022
f3f53c6
remove empirical
reubenharry May 15, 2022
ec3fe94
merge docs
reubenharry May 15, 2022
af06b06
notebooks
reubenharry May 15, 2022
810aad9
Merge branch 'notebook' into histogram
reubenharry May 15, 2022
aab8095
notebooks: sampling
reubenharry May 15, 2022
8d143d2
merge cleanup
reubenharry May 15, 2022
0b94b79
merge notebook
reubenharry May 15, 2022
5287df4
cleaning up notebooks
reubenharry May 16, 2022
5dddb4f
remove helpers
reubenharry May 16, 2022
b829b03
Merge branch 'cleanup' into notebook
reubenharry May 16, 2022
a6d29da
when
reubenharry May 16, 2022
e7652d7
no helpers
reubenharry May 16, 2022
62319ee
models: lts
reubenharry May 16, 2022
9cff121
models: lts
reubenharry May 16, 2022
1cf551e
SMC notebook
reubenharry May 16, 2022
c2988d6
merged
reubenharry May 16, 2022
fd97ec3
weighted sampling
reubenharry May 17, 2022
2e6dd30
formatting
reubenharry May 18, 2022
70426f6
Merge branch 'notebook' into cleanup
reubenharry May 18, 2022
9f887b3
working on analytic
reubenharry May 18, 2022
6d36c1c
test enumerator
reubenharry May 18, 2022
6d4839c
discovered a bug in Integrator
reubenharry May 18, 2022
13cc36f
first attempt to fix normalization bug
reubenharry May 18, 2022
e19b2db
bug seems to be fixed
reubenharry May 18, 2022
bf7bdbf
update tests
reubenharry May 18, 2022
96ea8df
working tests: normalNormal goes awry if observations are huge
reubenharry May 19, 2022
328a16f
cleanup
reubenharry May 19, 2022
f378401
Bayesian in class, and remove LinearWithOutliers
reubenharry May 19, 2022
4b3e4e5
model updates
reubenharry May 23, 2022
3e21cff
yaml
Jun 4, 2022
77df67d
prepare for PR
Jun 6, 2022
93cf699
address warnings
Jun 6, 2022
9782c8c
review
Jun 6, 2022
4d8e98c
more warnings fixed
Jun 8, 2022
ea6cb50
merged
reubenharry Jun 9, 2022
51268c7
less warnings
Jun 9, 2022
9978861
update ghc version stated in cabal
reubenharry Jun 9, 2022
1aef717
Merge branch 'cleanup' of github.com:tweag/monad-bayes into cleanup
reubenharry Jun 9, 2022
8e32fbf
remove warnings
Jun 9, 2022
16f5e11
merge main
Jun 9, 2022
6c78b06
fix build
Jun 9, 2022
7c69878
version bump
Jun 9, 2022
8174b55
merged but tests fail
Jun 9, 2022
0fc99be
remove safe
Jun 9, 2022
d7011b5
Merge branch 'cleanup' into histogram
Jun 9, 2022
b293eca
fix merge
Jun 9, 2022
54b0165
fix rmsmc bug you introduced
reubenharry Jun 9, 2022
cb6cc06
integrator as MonadInfer
reubenharry Jun 10, 2022
3777955
updates
reubenharry Jun 10, 2022
165bd24
updates
reubenharry Jun 10, 2022
9fd5c00
updates
reubenharry Jun 10, 2022
e2ee3a9
fix the integrator
reubenharry Jun 10, 2022
b418558
fix the normalization of the integrator
reubenharry Jun 10, 2022
59ef114
merged
reubenharry Jun 11, 2022
fe29850
inference
reubenharry Jun 11, 2022
ae07cda
rope in analyticT
reubenharry Jun 13, 2022
7a1f775
remove integratorT
Jun 13, 2022
3e388f4
nix notebooks
Jun 14, 2022
1dad9f5
notebooks
Jun 14, 2022
fdb0177
rmsmc
Jun 14, 2022
0a2dd21
hoistFirst bug again
Jun 15, 2022
44d3363
hoistFirst bug again
Jun 15, 2022
935ad8f
diagrams
Jun 15, 2022
277cb63
fp notebook
Jun 15, 2022
021aa60
remove integratorT
Jun 15, 2022
757d8cc
Merge branch 'cleanup' into histogram
Jun 15, 2022
ea01eaa
Merge branch 'histogram' into analytic
Jun 15, 2022
463b12f
changelog and version bump
Jun 15, 2022
96d6523
notebooks
Jun 17, 2022
d1c6a19
cleanup
Jun 17, 2022
9ec7a05
got the classes working better
Jun 27, 2022
c765c76
got the classes working better
Jun 27, 2022
3883adc
got the classes working better
Jun 27, 2022
6e643ac
struggling with ad
Jun 27, 2022
99575ec
first working example of marginalization
Jun 27, 2022
c0cae57
renaming
Jun 28, 2022
3b3eb3d
continue cleanup
Jun 28, 2022
a71dabe
tests: not yet integrated in
Jun 28, 2022
343acde
tests: integrated in
Jun 28, 2022
49b72a7
bayesian pmmh
Jun 28, 2022
b4baf7b
switch over to new smc
Jun 28, 2022
bb4304c
switch over to new smc
Jun 28, 2022
d6beee0
improve resampleGeneric sum
Jun 28, 2022
78a0d52
independent in Class
Jun 28, 2022
b9fa06c
make clean
reubenharry Jul 15, 2022
8c5e533
merge
reubenharry Jul 15, 2022
9f414a3
fix tests
reubenharry Jul 15, 2022
f096bf0
fix tests
reubenharry Jul 15, 2022
22c20b8
fix tests
reubenharry Jul 15, 2022
4849cb7
fix tests
reubenharry Jul 15, 2022
bb82c1b
pmmh
reubenharry Jul 15, 2022
5453626
withParticles
reubenharry Jul 15, 2022
4890596
withParticles
reubenharry Jul 15, 2022
4428664
erf
reubenharry Jul 20, 2022
0f8bd34
update nix
reubenharry Jul 25, 2022
72c5a79
cabal format
reubenharry Jul 25, 2022
d64962c
update nix
reubenharry Jul 25, 2022
7245f09
fix build
reubenharry Jul 25, 2022
86a72ef
merge sampler changes, fix changelog
reubenharry Jul 26, 2022
409b14d
bump changelog and version. bump ghc version to 9.2.3
reubenharry Jul 26, 2022
f99ed20
update docs and change mcmc so unweighted is not needed
reubenharry Jul 26, 2022
95bbbe7
comment out HD
reubenharry Aug 8, 2022
8a77047
some merging
reubenharry Aug 8, 2022
ede131e
merge
reubenharry Aug 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/data
_cache
docs/build
*.csv
Expand Down
25 changes: 16 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# 0.2.0 (2021-07-18)
# 0.2.0 (2021-07-26)

* Addition of `sampleIOwith` and `sampleSTwith` allowing the usage
of your favourite RNG with seed of your choice.
*
- rename various functions to match the names of the corresponding types (e.g. `Enumerator` goes with `enumerator`)
- add configs as arguments to inference methods `smc` and `mcmc`
- add rudimentary tests for all inference methods
- put `mcmc` as inference method in new module `Control.Monad.Bayes.Inference.MCMC`
- update history of changelog in line with semantic versioning conventions
- bumped to GHC 9.2.3

# 0.1.1.3 (2021-06-15)
# 0.1.5 (2021-07-26)

- Refactor of sampler to be parametric in the choice of a pair of IO monad and RNG

# 0.1.4 (2021-06-15)

Addition of new helper functions, plotting tools, tests, and Integrator monad.

Expand All @@ -15,7 +22,7 @@ Addition of new helper functions, plotting tools, tests, and Integrator monad.
- `models` directory is cleaned up. New sequential models using `pipes` package to represent monadic streams


# 0.1.1.2 (2021-06-08)
# 0.1.3 (2021-06-08)

Clean up of unused functions and broken code

Expand All @@ -24,18 +31,18 @@ Clean up of unused functions and broken code
- explicit imports
- added some global language extensions

# 0.1.1.1 (2021-06-08)
# 0.1.2 (2021-06-08)

Add documentation

- docs written in markdown
- docs built by sphinx


# 0.1.1.0 (2020-04-08)
# 0.1.1 (2020-04-08)

- New exported function: `Control.Monad.Bayes.Class` now exports `discrete`.

# 0.1.0.0 (2020-02-17)
# 0.1.0 (2020-02-17)

Initial release.
60 changes: 34 additions & 26 deletions benchmark/SSM.hs
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
module Main where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we had already made the changes to use a record to contain the various options?


import Control.Monad.Bayes.Inference.MCMC
import Control.Monad.Bayes.Inference.PMMH as PMMH (pmmh)
import Control.Monad.Bayes.Inference.RMSMC (rmsmcLocal)
import Control.Monad.Bayes.Inference.SMC (smcMultinomial)
import Control.Monad.Bayes.Inference.RMSMC (rmsmcDynamic)
import Control.Monad.Bayes.Inference.SMC
import Control.Monad.Bayes.Inference.SMC2 as SMC2 (smc2)
import Control.Monad.Bayes.Population (runPopulation)
import Control.Monad.Bayes.Sampler (sampleWith)
import Control.Monad.Bayes.Weighted (prior)
import Control.Monad.Bayes.Population
import Control.Monad.Bayes.Population (population, resampleMultinomial, runPopulation)
import Control.Monad.Bayes.Sampler (sampleIO, sampleIOfixed, sampleWith)
import Control.Monad.Bayes.Weighted (unweighted)
import Control.Monad.IO.Class (MonadIO (liftIO))
import NonlinearSSM (generateData, model, param)
import System.Random.Stateful (mkStdGen, newIOGenM)

main :: IO ()
main = do
(smcRes, smcrmRes, pmmhRes, smc2Res) <-
newIOGenM (mkStdGen 1729)
>>= ( sampleWith $ do
let t = 5
dat <- generateData t
let ys = map snd dat
smcRes <- runPopulation $ smcMultinomial t 10 (param >>= model ys)
smcrmRes <- runPopulation $ rmsmcLocal t 10 10 (param >>= model ys)
pmmhRes <- prior $ pmmh 2 t 3 param (model ys)
smc2Res <- runPopulation $ smc2 t 3 2 1 param (model ys)
return (smcRes, smcrmRes, pmmhRes, smc2Res)
)
print "SMC"
print $ show smcRes
print "RM-SMC"
print $ show smcrmRes
print "PMMH"
print $ show pmmhRes
print "SMC2"
print $ show smc2Res
main = sampleIOfixed $ do
let t = 5
dat <- generateData t
let ys = map snd dat
liftIO $ print "SMC"
smcRes <- population $ smc SMCConfig {numSteps = t, numParticles = 10, resampler = resampleMultinomial} (param >>= model ys)
liftIO $ print $ show smcRes
liftIO $ print "RM-SMC"
smcrmRes <-
population $
rmsmcDynamic
MCMCConfig {numMCMCSteps = 10, numBurnIn = 0, proposal = SingleSiteMH}
SMCConfig {numSteps = t, numParticles = 10, resampler = resampleSystematic}
(param >>= model ys)
liftIO $ print $ show smcrmRes
liftIO $ print "PMMH"
pmmhRes <-
unweighted $
pmmh
MCMCConfig {numMCMCSteps = 2, numBurnIn = 0, proposal = SingleSiteMH}
SMCConfig {numSteps = t, numParticles = 3, resampler = resampleSystematic}
param
(model ys)
liftIO $ print $ show pmmhRes
liftIO $ print "SMC2"
smc2Res <- population $ smc2 t 3 2 1 param (model ys)
liftIO $ print $ show smc2Res
132 changes: 67 additions & 65 deletions benchmark/Single.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{-# LANGUAGE ImportQualifiedPost #-}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is everything commented out?


import Control.Monad.Bayes.Class
import Control.Monad.Bayes.Inference.MCMC (MCMCConfig (..), Proposal (SingleSiteMH))
import Control.Monad.Bayes.Inference.RMSMC
import Control.Monad.Bayes.Inference.SMC
import Control.Monad.Bayes.Population
import Control.Monad.Bayes.Population (population)
import Control.Monad.Bayes.Sampler
import Control.Monad.Bayes.Traced
import Control.Monad.Bayes.Weighted
Expand All @@ -16,75 +18,75 @@ import LogReg qualified
import Options.Applicative
import System.Random.MWC (GenIO, createSystemRandom)

data Model = LR Int | HMM Int | LDA (Int, Int)
deriving stock (Show, Read)
-- data Model = LR Int | HMM Int | LDA (Int, Int)
-- deriving stock (Show, Read)

parseModel :: String -> Maybe Model
parseModel s =
case s of
'L' : 'R' : n -> Just $ LR (read n)
'H' : 'M' : 'M' : n -> Just $ HMM (read n)
'L' : 'D' : 'A' : n -> Just $ LDA (5, read n)
_ -> Nothing
-- parseModel :: String -> Maybe Model
-- parseModel s =
-- case s of
-- 'L' : 'R' : n -> Just $ LR (read n)
-- 'H' : 'M' : 'M' : n -> Just $ HMM (read n)
-- 'L' : 'D' : 'A' : n -> Just $ LDA (5, read n)
-- _ -> Nothing

getModel :: MonadInfer m => Model -> (Int, m String)
getModel model = (size model, program model)
where
size (LR n) = n
size (HMM n) = n
size (LDA (d, w)) = d * w
program (LR n) = show <$> (LogReg.logisticRegression (runST $ sampleSTfixed (LogReg.syntheticData n)))
program (HMM n) = show <$> (HMM.hmm (runST $ sampleSTfixed (HMM.syntheticData n)))
program (LDA (d, w)) = show <$> (LDA.lda (runST $ sampleSTfixed (LDA.syntheticData d w)))
-- getModel :: MonadInfer m => Model -> (Int, m String)
-- getModel model = (size model, program model)
-- where
-- size (LR n) = n
-- size (HMM n) = n
-- size (LDA (d, w)) = d * w
-- program (LR n) = show <$> (LogReg.logisticRegression (runST $ sampleSTfixed (LogReg.syntheticData n)))
-- program (HMM n) = show <$> (HMM.hmm (runST $ sampleSTfixed (HMM.syntheticData n)))
-- program (LDA (d, w)) = show <$> (LDA.lda (runST $ sampleSTfixed (LDA.syntheticData d w)))

data Alg = SMC | MH | RMSMC
deriving stock (Read, Show)
-- -- data Alg = SMC | MH | RMSMC
-- -- deriving stock (Read, Show)

runAlg :: Model -> Alg -> Sampler GenIO IO String
runAlg model alg =
case alg of
SMC ->
let n = 100
(k, m) = getModel model
in show <$> runPopulation (smcSystematic k n m)
MH ->
let t = 100
(_, m) = getModel model
in show <$> prior (mh t m)
RMSMC ->
let n = 10
t = 1
(k, m) = getModel model
in show <$> runPopulation (rmsmcBasic k n t m)
-- runAlg :: Model -> Alg -> Sampler GenIO IO String
-- runAlg model alg =
-- case alg of
-- SMC ->
-- let n = 100
-- (k, m) = getModel model
-- in show <$> population (smc SMCConfig {numSteps = k, numParticles = n, resampler = resampleSystematic} m)
-- MH ->
-- let t = 100
-- (_, m) = getModel model
-- in show <$> unweighted (mh t m)
-- RMSMC ->
-- let n = 10
-- t = 1
-- (k, m) = getModel model
-- in show <$> population (rmsmcBasic MCMCConfig {numMCMCSteps = t, numBurnIn = 0, proposal = SingleSiteMH} (SMCConfig {numSteps = k, numParticles = n, resampler = resampleSystematic}) m)

infer :: Model -> Alg -> IO ()
infer model alg = do
g <- createSystemRandom
x <- sampleWith (runAlg model alg) g
print x
-- infer :: Model -> Alg -> IO ()
-- infer model alg = do
-- g <- createSystemRandom
-- x <- sampleWith (runAlg model alg) g
-- print x

opts :: ParserInfo (Model, Alg)
opts = flip info fullDesc $ liftA2 (,) model alg
where
model =
option
(maybeReader parseModel)
( long "model"
<> short 'm'
<> help "Model"
)
alg =
option
auto
( long "alg"
<> short 'a'
<> help "Inference algorithm"
)
-- -- opts :: ParserInfo (Model, Alg)
-- -- opts = flip info fullDesc $ liftA2 (,) model alg
-- -- where
-- -- model =
-- -- option
-- -- (maybeReader parseModel)
-- -- ( long "model"
-- -- <> short 'm'
-- -- <> help "Model"
-- -- )
-- -- alg =
-- -- option
-- -- auto
-- -- ( long "alg"
-- -- <> short 'a'
-- -- <> help "Inference algorithm"
-- -- )

main :: IO ()
main = do
(model, alg) <- execParser opts
startTime <- getCurrentTime
infer model alg
endTime <- getCurrentTime
print (diffUTCTime endTime startTime)
-- -- main :: IO ()
-- -- main = do
-- -- (model, alg) <- execParser opts
-- -- startTime <- getCurrentTime
-- -- infer model alg
-- -- endTime <- getCurrentTime
-- -- print (diffUTCTime endTime startTime)
Loading