![]() |
ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
|
Rao-Blackwellised (collapsed) moments of a correlated COUNT / SUM, and the exact posterior of a latent conditioned on such a count. More...
#include "CollapsedAggMoment.h"#include "CircuitFromMMap.h"#include "RandomVariable.h"#include "Aggregation.h"#include "distributions/Distribution.h"#include "Circuit.h"#include "provsql_utils_cpp.h"#include "postgres.h"#include "fmgr.h"#include "utils/uuid.h"#include "utils/array.h"#include "catalog/pg_type.h"#include "provsql_utils.h"#include "provsql_error.h"#include <cmath>#include <limits>#include <memory>#include <optional>#include <unordered_map>#include <unordered_set>#include <vector>
Go to the source code of this file.
Namespaces | |
| namespace | provsql |
Functions | |
| std::optional< std::pair< double, double > > | provsql::aggCollapsedRawMoments (const GenericCircuit &gc, gate_t agg) |
| std::optional< double > | provsql::aggCollapsedRawMoment (const GenericCircuit &gc, gate_t agg, unsigned k) |
Collapsed raw moment E[C^k] of a correlated COUNT / SUM agg, or std::nullopt when the circuit does not match the shared-latent shape. | |
| std::optional< double > | provsql::collapsedConditionalMoment (const GenericCircuit &gc, gate_t target, gate_t event, unsigned k) |
Collapsed exact posterior raw moment E[R^k | Y = C] for a latent target R conditioned (through the equality event event) on a discrete rv Y – parametrised by target – equalling a correlated COUNT C. | |
| Datum | agg_collapsed_moment (PG_FUNCTION_ARGS) |
| SQL: agg_collapsed_moment(token uuid, k integer) -> float8. | |
| Datum | agg_collapsed_moments (PG_FUNCTION_ARGS) |
| SQL: agg_collapsed_moments(token uuid) -> float8[] {E[C], E[C^2]}. | |
Rao-Blackwellised (collapsed) moments of a correlated COUNT / SUM, and the exact posterior of a latent conditioned on such a count.
The recurring shape in latent-variable relational models is an aggregate over probabilistically-selected rows whose selection events are coupled by a small shared latent:
C = COUNT/SUM over rows i of [ eps_i op g_i(b) ]
where eps_i is a per-row independent random-variable leaf, b is a shared continuous latent that every row references, and g_i(b) is an expression in b (plus per-row constants). The exact moment path enumerates n^k row tuples and evaluates a joint probability for each – O(n^2) pair-probabilities for the variance, which does not scale.
Conditional on b, the indicators are INDEPENDENT (the only coupling is through b), so this collapses to a 1-D quadrature over b: at each node the per-row success probability q_i(b) = Pr[eps_i op g_i(b)] is a closed-form CDF, and the count's conditional law is the elementary sum-of-independent-Bernoulli (Poisson-binomial) distribution. Cost O(G·n) (moments) / O(G·n^2) (pmf) – milliseconds where the exact path took minutes – and exact up to the quadrature (no sampling).
The same collapse yields the EXACT posterior of a latent R conditioned on a discrete rv Y(R) equalling the count: P(C=j) is the collapsed pmf, and E[R^k|C] is a 1-D quadrature over R weighted by the likelihood L(r)=Σ_j P(C=j) pmf_Y(j;θ(r)) – replacing a degenerating importance sampler with a closed quadrature.
Scope (returns std::nullopt, i.e. "fall back", on any mismatch): a COUNT / SUM gate_agg whose per-row indicators reduce to "(constant Bernoullis) AND (one gate_cmp)", the cmp comparing a bare per-row gate_rv leaf against an expression over exactly ONE shared continuous latent leaf. Everything else declines.
Definition in file CollapsedAggMoment.cpp.
| Datum agg_collapsed_moment | ( | PG_FUNCTION_ARGS | ) |
SQL: agg_collapsed_moment(token uuid, k integer) -> float8.
The collapsed raw moment E[C^k] of a correlated COUNT / SUM, or NULL when the circuit does not match the recognised shared-latent pattern (the caller then falls back to the exact enumeration). k in {1, 2}.
Definition at line 619 of file CollapsedAggMoment.cpp.

| Datum agg_collapsed_moments | ( | PG_FUNCTION_ARGS | ) |
SQL: agg_collapsed_moments(token uuid) -> float8[] {E[C], E[C^2]}.
Both collapsed raw moments from a single circuit load and plan build, or NULL when the shared-latent pattern does not match. variance() calls this so a mean+variance readout traverses the circuit once, not twice (the load and plan build dominate once the grid loop is arithmetic-only).
Definition at line 651 of file CollapsedAggMoment.cpp.
