ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
ConjugatePosterior.h
Go to the documentation of this file.
1/**
2 * @file ConjugatePosterior.h
3 * @brief Exact conjugate-prior posteriors for observe-evidence circuits.
4 *
5 * Recognises conjugate prior/likelihood structure in an @c and_agg
6 * evidence conjunction of @c gate_observe atoms and computes the latent's
7 * posterior in closed form: the posterior of a bare all-literal @c gate_rv
8 * prior, observed through leaves that wire it into one parameter slot,
9 * stays in the prior's family with parameters folded one observation at a
10 * time through the conjugate-update registry
11 * (@c registerConjugateRule in @c distributions/Distribution.h).
12 *
13 * The recogniser computes exactly the estimand likelihood-weighting
14 * importance sampling targets (the IS weight is @f$\prod_i f(d_i \mid
15 * \theta)@f$ and the conjugate posterior is the prior times that same
16 * product, renormalised), so where it fires only the method changes,
17 * never the semantics -- exact, deterministic, and available at
18 * @c provsql.rv_mc_samples @c = @c 0. Any shape mismatch (a Boolean or
19 * @c gate_cmp evidence factor, a latent reaching the leaf through
20 * @c gate_arith, a registry miss, an out-of-support datum...) declines
21 * with @c std::nullopt and the caller falls back to importance sampling
22 * unchanged.
23 */
24#ifndef PROVSQL_CONJUGATE_POSTERIOR_H
25#define PROVSQL_CONJUGATE_POSTERIOR_H
26
27#include <optional>
28
29#include "GenericCircuit.h"
30#include "RandomVariable.h"
31
32namespace provsql {
33
34/**
35 * @brief The exact posterior of @p target given @p evidence, as a
36 * resolved distribution spec, when the circuit matches the
37 * conjugate shape; @c std::nullopt on any mismatch (the caller
38 * falls back to importance sampling).
39 *
40 * Recognised shape: @p target is a bare all-literal @c gate_rv (the
41 * prior); @p evidence flattens through the @c gate_times conjunction
42 * spine (the shape @c and_agg builds) into @c gate_observe atoms only
43 * (@c gate_one factors are transparently skipped); each observed leaf
44 * parses to a @c DistributionTemplate with exactly one wired slot whose
45 * wire is @p target itself; and the registry has an update rule for
46 * every (likelihood family, wired slot, running posterior family)
47 * triple. Every registered update is exchangeable, so the left-nested
48 * @c and_agg fold order is irrelevant, and mixed likelihoods sharing
49 * one conjugate prior compose (a Gamma-prior rate observed through
50 * interleaved Poisson counts and Exponential gaps stays Gamma).
51 */
52std::optional<DistributionSpec>
53conjugatePosterior(const GenericCircuit &gc, gate_t target, gate_t evidence);
54
55/**
56 * @brief The exact log marginal likelihood @f$\ln P(\mathrm{data})@f$ of
57 * a conjugate-shaped @p evidence circuit; @c std::nullopt on any
58 * shape mismatch or when a rule in the fold has no predictive
59 * density (the caller keeps the Monte Carlo mean-weight path).
60 *
61 * The latent is inferred from the evidence itself: every observed leaf
62 * must wire the same bare all-literal @c gate_rv prior. The result is
63 * the sum of each rule's log predictive density of its datum under the
64 * running posterior (the sequential chain-rule factorisation of the
65 * joint marginal), accumulated in log space.
66 */
67std::optional<double>
68conjugateLogEvidence(const GenericCircuit &gc, gate_t evidence);
69
70} // namespace provsql
71
72#endif // PROVSQL_CONJUGATE_POSTERIOR_H
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:49
Semiring-agnostic in-memory provenance circuit.
Continuous random-variable helpers (distribution parsing, moments).
std::optional< double > conjugateLogEvidence(const GenericCircuit &gc, gate_t evidence)
The exact log marginal likelihood of a conjugate-shaped evidence circuit; std::nullopt on any shape ...
std::optional< DistributionSpec > conjugatePosterior(const GenericCircuit &gc, gate_t target, gate_t evidence)
The exact posterior of target given evidence, as a resolved distribution spec, when the circuit match...