ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Expectation.cpp File Reference

Implementation of the analytical expectation / variance / moment evaluator over scalar RV sub-circuits. More...

#include "Expectation.h"
#include "AggMarginalEvaluator.h"
#include "AnalyticEvaluator.h"
#include "Aggregation.h"
#include "BooleanCircuit.h"
#include "Circuit.h"
#include "CircuitFromMMap.h"
#include "CollapsedAggMoment.h"
#include "ComparatorResolution.h"
#include "ConjugatePosterior.h"
#include "ProbabilityMethod.h"
#include "MonteCarloSampler.h"
#include "RandomVariable.h"
#include "distributions/Distribution.h"
#include "PivotIntegration.h"
#include "RangeCheck.h"
#include "provsql_utils_cpp.h"
#include "semiring/BoolExpr.h"
#include "postgres.h"
#include "fmgr.h"
#include "utils/uuid.h"
#include "provsql_utils.h"
#include "provsql_error.h"
#include <algorithm>
#include <cmath>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Include dependency graph for Expectation.cpp:

Go to the source code of this file.

Namespaces

namespace  provsql

Functions

double provsql::evaluateBooleanProbability (const GenericCircuit &gc, gate_t boolRoot)
 Probability that the Boolean subcircuit rooted at boolRoot evaluates to true under the tuple-independent probabilistic-database model.
double provsql::compute_expectation (const GenericCircuit &gc, gate_t root, std::optional< gate_t > event_root=std::nullopt)
 Compute \(E[X]\) (or \(E[X \mid A]\) if event_root is set) over the scalar sub-circuit rooted at root.
double provsql::compute_raw_moment (const GenericCircuit &gc, gate_t root, unsigned k, std::optional< gate_t > event_root=std::nullopt)
 Compute the raw moment \(E[X^k]\) (or \(E[X^k \mid A]\) if event_root is set) for k >= 0.
double provsql::compute_central_moment (const GenericCircuit &gc, gate_t root, unsigned k, std::optional< gate_t > event_root=std::nullopt)
 Compute the central moment \(E[(X - E[X])^k]\) (or \(E[(X - E[X \mid A])^k \mid A]\) if event_root is set).
double provsql::compute_quantile (const GenericCircuit &gc, gate_t root, double p, std::optional< gate_t > event_root=std::nullopt)
 Compute the p-quantile \(F^{-1}(p)\) of the scalar rooted at root (of the truncated distribution if event_root is set).
gate_t provsql::lift_conditioning (GenericCircuit &gc, gate_t root, std::optional< gate_t > &event_opt)
 Lift conditioning out of a scalar arithmetic expression.
Datum agg_avg_moment_exact (PG_FUNCTION_ARGS)
 SQL: rv_moment(token uuid, k integer, central boolean, prov uuid DEFAULT gate_one()) -> float8.
Datum rv_moment (PG_FUNCTION_ARGS)
Datum rv_quantile (PG_FUNCTION_ARGS)
 SQL: rv_quantile(token uuid, p float8, prov uuid DEFAULT gate_one()) -> float8.
Datum rv_evidence (PG_FUNCTION_ARGS)
 SQL: rv_evidence(evidence uuid) -> float8.

Detailed Description

Implementation of the analytical expectation / variance / moment evaluator over scalar RV sub-circuits.

Definition in file Expectation.cpp.

Function Documentation

◆ agg_avg_moment_exact()

Datum agg_avg_moment_exact ( PG_FUNCTION_ARGS )

SQL: rv_moment(token uuid, k integer, central boolean, prov uuid DEFAULT gate_one()) -> float8.

Single C entry point shared by the expected / variance / moment / central_moment SQL functions. The SQL wrappers select the (k, central) pair that matches their semantics:

  • expected(rv, prov): k=1, central=false.
  • variance(rv, prov): k=2, central=true.
  • moment(rv, k, prov): central=false.
  • central_moment(rv, k, prov): central=true.

The prov argument carries the conditioning event: typically the row's provenance() gate after a WHERE predicate folded a gate_cmp into it. When prov resolves to gate_one (the default, or the load-time simplification of any always-true sub-circuit) the unconditional path runs unchanged. Otherwise we load a JOINT circuit reaching both roots, so shared gate_rv leaves collapse to a single gate_t – the property the conditional MC sampler relies on to couple the indicator's draw with the value's draw.

SQL: agg_avg_moment_exact(token uuid, k integer) -> float8

The exact independent-rows arm behind agg_raw_moment's avg dispatch: E[AVG^k | COUNT >= 1] from the joint (sum, count) PMF over pairwise leaf-disjoint contributors (aggAvgRawMomentExact). Returns NULL when the shape is out of scope – shared leaves, compound contributors, unset probabilities – and the SQL caller falls back to the Monte-Carlo scalar path.

Definition at line 2228 of file Expectation.cpp.

Here is the call graph for this function:

◆ rv_evidence()

Datum rv_evidence ( PG_FUNCTION_ARGS )

SQL: rv_evidence(evidence uuid) -> float8.

The marginal likelihood P(data) of an evidence circuit: the mean raw importance weight over provsql.rv_mc_samples prior draws (the same quantity rejection conditioning computes as P(C), now a product of the observations' densities). Backs provsql.evidence.

Definition at line 2348 of file Expectation.cpp.

Here is the call graph for this function:

◆ rv_moment()

Datum rv_moment ( PG_FUNCTION_ARGS )

Definition at line 2254 of file Expectation.cpp.

Here is the call graph for this function:

◆ rv_quantile()

Datum rv_quantile ( PG_FUNCTION_ARGS )

SQL: rv_quantile(token uuid, p float8, prov uuid DEFAULT gate_one()) -> float8.

C entry point behind the polymorphic quantile SQL dispatcher. Same conditioning plumbing as rv_moment (joint circuit, nested gate_conditioned lifting); the evaluation itself is compute_quantile: closed-form / numerically inverted CDF for a (possibly truncated) bare gate_rv, exact generalised inverse for a categorical mixture, empirical MC quantile for compound circuits.

Definition at line 2310 of file Expectation.cpp.

Here is the call graph for this function: