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 <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...

Namespaces

namespace  provsql

Functions

double provsql::monteCarloRV (const GenericCircuit &gc, gate_t root, unsigned samples)
 Run Monte Carlo on a circuit that may contain gate_rv leaves.
bool provsql::circuitHasRV (const GenericCircuit &gc, gate_t root)
 Walk the circuit reachable from root looking for any gate_rv.
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.
ConditionalScalarSamples provsql::monteCarloConditionalScalarSamples (const GenericCircuit &gc, gate_t root, gate_t event_root, unsigned samples)
 Rejection-sample root 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.

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.