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

Add effective sample size to Population #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions src/Control/Monad/Bayes/Population.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module Control.Monad.Bayes.Population
resampleSystematic,
stratified,
resampleStratified,
onlyBelowEffectiveSampleSize,
effectiveSampleSize,
extractEvidence,
pushEvidence,
proper,
Expand Down Expand Up @@ -210,6 +212,31 @@ resampleMultinomial ::
Population m a
resampleMultinomial = resampleGeneric multinomial

-- | Only use the given resampler when the effective sample size is below a certain threshold
onlyBelowEffectiveSampleSize ::
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I should have one fixture test and a unit test where this is used.

MonadDistribution m =>
-- | The threshold under which the effective sample size must fall before the resampler is used.
-- For example, this may be half of the number of particles.
Double ->
-- | The resampler to user under the threshold
(MonadDistribution m => Population m a -> Population m a) ->
-- | The new resampler
(Population m a -> Population m a)
onlyBelowEffectiveSampleSize threshold resampler pop = do
ess <- lift $ effectiveSampleSize pop
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is wrong because it will execute the m effects of pop again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It might be better to have a function withEffectiveSampleSize :: Functor m => Population m a -> m (a, Double)

if ess < threshold then resampler pop else pop

-- | Compute the effective sample size of a population from the weights.
--
-- See https://en.wikipedia.org/wiki/Design_effect#Effective_sample_size
effectiveSampleSize :: Functor m => Population m a -> m Double
effectiveSampleSize = fmap (effectiveSampleSizeKish . map (exp . ln . snd)) . runPopulation
where
effectiveSampleSizeKish :: [Double] -> Double
effectiveSampleSizeKish weights = square (Data.List.sum weights) / Data.List.sum (square <$> weights)
square :: Double -> Double
square x = x * x

-- | Separate the sum of weights into the 'Weighted' transformer.
-- Weights are normalized after this operation.
extractEvidence ::
Expand Down