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

Monte Carlo sampling over a GenericCircuit, RV-aware. More...

#include <optional>
#include <random>
#include <utility>
#include <vector>
#include "GenericCircuit.h"
#include "provsql_utils.h"
Include dependency graph for MonteCarloSampler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  provsql::ConditionalScalarSamples
 Outcome of a conditional Monte Carlo sampling pass. More...
struct  provsql::ConditionalScalarPairSamples
 Outcome of a conditional coupled-pair Monte Carlo pass: xs[i] / ys[i] are the two roots' values from the same accepted iteration. More...
struct  provsql::WeightedPosterior
 Outcome of a likelihood-weighting (importance-sampling) pass. More...

Namespaces

namespace  provsql

Functions

std::mt19937_64 provsql::seedRng ()
 The shared Monte Carlo generator, seeded from the provsql.monte_carlo_seed GUC (-1 = non-deterministic from std::random_device).
double provsql::monteCarloRV (const GenericCircuit &gc, gate_t root, unsigned samples)
 Run Monte Carlo on a circuit that may contain gate_rv leaves.
double provsql::monteCarloRVStopping (const GenericCircuit &gc, gate_t root, double eps, double delta, unsigned long max_samples, unsigned long &samples_used, bool &reached_target)
 Whole-circuit (eps,delta)-relative probability via the Dagum-Karp-Luby-Ross stopping rule.
bool provsql::circuitHasRV (const GenericCircuit &gc, gate_t root)
 Walk the circuit reachable from root looking for any gate_rv.
bool provsql::circuitHasUnresolvedSampleableAgg (const GenericCircuit &gc, gate_t root)
 Whether a surviving gate_agg exists and every one is sample-faithful (SUM / AVG / MIN / MAX / COUNT – every aggregate the sampler reproduces exactly).
std::vector< double > provsql::monteCarloJointDistribution (const GenericCircuit &gc, const std::vector< gate_t > &cmps, unsigned samples)
 Estimate the joint distribution of cmps via Monte Carlo.
std::vector< double > provsql::monteCarloScalarSamples (const GenericCircuit &gc, gate_t root, unsigned samples)
 Sample a scalar sub-circuit samples times and return the draws.
std::pair< std::vector< double >, std::vector< double > > provsql::monteCarloScalarPairSamples (const GenericCircuit &gc, gate_t root_a, gate_t root_b, unsigned samples)
 Coupled per-iteration draws of two scalar roots.
ConditionalScalarSamples provsql::monteCarloConditionalScalarSamples (const GenericCircuit &gc, gate_t root, gate_t event_root, unsigned samples)
 Rejection-sample root conditioned on event_root.
ConditionalScalarPairSamples provsql::monteCarloConditionalScalarPairSamples (const GenericCircuit &gc, gate_t root_a, gate_t root_b, gate_t event_root, unsigned samples)
 Rejection-sample the PAIR (root_a, root_b) conditioned on event_root.
std::optional< std::vector< double > > provsql::try_truncated_closed_form_sample (const GenericCircuit &gc, gate_t root, gate_t event_root, unsigned n)
 Try to draw n exact samples from the conditional distribution of root given event_root via closed-form truncation, bypassing MC rejection.
WeightedPosterior provsql::importanceSampleConditional (const GenericCircuit &gc, gate_t root, gate_t evidence, unsigned samples)
 Self-normalised importance sampling of root given evidence.
double provsql::importanceEvidence (const GenericCircuit &gc, gate_t evidence, unsigned samples)
 Marginal likelihood P(data) of evidence: the mean raw importance weight over samples prior draws.
std::vector< double > provsql::posteriorResample (const WeightedPosterior &post, unsigned n)
 Sampling-importance-resampling: draw n posterior samples from a weighted particle set (proportional to weight, with replacement).
bool provsql::circuitHasObserve (const GenericCircuit &gc, gate_t root)
 Whether the circuit reachable from root contains a gate_observe – the signal that a conditioning event is continuous-density evidence and must be evaluated by importance sampling rather than the analytic / rejection conditional paths.

Detailed Description

Monte Carlo sampling over a GenericCircuit, RV-aware.

Drop-in replacement for BooleanCircuit::monteCarlo for circuits that contain continuous random variables (gate_rv) or arithmetic over RVs (gate_arith). Operates directly on the GenericCircuit produced by CircuitFromMMap, so the BoolExpr-semiring translation that drops non-Boolean gates is not needed.

Gate handling:

  • gate_input (and gate_update) – Bernoulli draw at getProb, memoised per iteration (so the same input feeding two children produces the same draw).
  • gate_plus / gate_times / gate_monus – Boolean OR / AND / AND-NOT.
  • gate_zero / gate_one – false / true.
  • gate_cmp with scalar (gate_rv / gate_arith / gate_value) children – compare two scalar samples per the comparison-operator OID stored in info1. Aggregate-vs-constant gate_cmp gates from HAVING semantics are handled by the existing BooleanCircuit path and are not reached here.
  • gate_value – parse extra as float8.
  • gate_rv – fresh draw from the distribution serialised in extra (memoised per iteration so the SAME RV inside an arithmetic expression uses the same draw, per the thesis's SampleOne).
  • gate_arith – recurse on scalar children, combine per the operator tag in info1 (provsql_arith_op enum: PLUS / TIMES are n-ary; MINUS / DIV are binary; NEG is unary).

The RNG is seeded from the provsql.monte_carlo_seed GUC: zero (default) seeds non-deterministically from std::random_device, any other value is a literal seed shared across the Bernoulli and continuous paths so a single GUC pins the whole computation.

Definition in file MonteCarloSampler.h.