From d09033c1f42da89514ec87bf4aba5d1614e9810d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20K=C3=B6ster?= Date: Mon, 4 Oct 2021 10:35:23 +0200 Subject: [PATCH] feat: Allow to compute bayesian model via the exploration of the marginal distribution. (#453) * feat: Allow to compute bayesian model via the exploration of the marginal distribution. * fmt --- src/stats/bayesian/model.rs | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/stats/bayesian/model.rs b/src/stats/bayesian/model.rs index 6760374e4..3a1d23f20 100644 --- a/src/stats/bayesian/model.rs +++ b/src/stats/bayesian/model.rs @@ -130,6 +130,54 @@ where marginal, } } + + /// Compute model via the exploration of the marginal distribution of the data. + pub fn compute_from_marginal( + &self, + marginal: &M, + data: &Data, + ) -> ModelInstance + where + M: Marginal, + { + let mut joint_probs = HashMap::new(); + let mut posterior_probs = HashMap::new(); + let mut payload = Payload::default(); + let marginal = { + let mut joint_prob = |event: &Event, data: &Data| { + let p = self.joint_prob(event, data, &mut payload); + joint_probs.insert(event.clone(), p); + p + }; + + let mut joint_prob_posterior = |event: &PosteriorEvent, data: &Data| { + let p = self.posterior.compute(event, data, &mut joint_prob); + posterior_probs.insert(event.clone(), p); + p + }; + + marginal.compute(data, &mut joint_prob_posterior) + }; + + ModelInstance { + joint_probs, + posterior_probs, + marginal, + } + } +} + +/// A trait for the exploration of the marginal distribution of the data. +pub trait Marginal { + type Event; + type BaseEvent; + type Data; + + fn compute LogProb>( + &self, + data: &Self::Data, + joint_prob: &mut F, + ) -> LogProb; } /// Instance of a model for given data and event universe.