39#include "utils/jsonb.h"
40#include "utils/fmgrprotos.h"
41#include "utils/uuid.h"
66void emit_bin(std::ostringstream &out,
bool &first,
67 double lo,
double hi,
unsigned count)
69 if (!first) out <<
',';
71 out <<
"{\"bin_lo\":" << lo
72 <<
",\"bin_hi\":" << hi
73 <<
",\"count\":" << count <<
'}';
82 int bins = PG_GETARG_INT32(1);
86 provsql_error(
"rv_histogram: bins must be positive (got %d)", bins);
88 std::ostringstream out;
92 out << std::setprecision(17);
103 gate_t root_gate, event_gate;
109 Datum json = DirectFunctionCall1(
110 jsonb_in, CStringGetDatum(pstrdup(out.str().c_str())));
111 PG_RETURN_DATUM(json);
116 std::optional<gate_t> event_opt;
117 if (conditional) event_opt = event_gate;
129 emit_bin(out, first, v, v, n);
133 "rv_histogram: provsql.rv_mc_samples = 0 disables sampling; "
134 "raise it above 0 to compute a histogram");
137 std::vector<double> samples;
146 gc, root_gate, event_gate, N);
148 samples = std::move(*direct);
151 gc, root_gate, event_gate, N);
152 if (cs.accepted.empty())
154 "rv_histogram: conditional MC accepted 0 of %u samples; "
155 "raise provsql.rv_mc_samples or check that the event is "
158 samples = std::move(cs.accepted);
164 if (!samples.empty()) {
178 std::sort(samples.begin(), samples.end());
179 const std::size_t n = samples.size();
180 const std::size_t lo_idx = n / 1000;
181 const std::size_t hi_idx = n - 1 - lo_idx;
183 double smin = std::isfinite(support.first)
186 double smax = std::isfinite(support.second)
193 emit_bin(out, first, smin, smax,
194 static_cast<unsigned>(samples.size()));
196 std::vector<unsigned> counts(bins, 0);
197 const double width = (smax - smin) / bins;
198 for (
double x : samples) {
199 int b =
static_cast<int>((x - smin) / width);
201 if (b >= bins) b = bins - 1;
204 for (
int i = 0; i < bins; ++i) {
205 const double lo = smin + i * width;
206 const double hi = (i == bins - 1) ? smax : lo + width;
207 emit_bin(out, first, lo, hi, counts[i]);
215 "rv_histogram: root gate type '%s' is not a scalar "
216 "(expected gate_value, gate_rv, gate_arith, or gate_mixture)",
219 }
catch (
const std::exception &e) {
226 Datum json = DirectFunctionCall1(
227 jsonb_in, CStringGetDatum(pstrdup(out.str().c_str())));
228 PG_RETURN_DATUM(json);
GenericCircuit getJointCircuit(pg_uuid_t root_token, pg_uuid_t event_token, gate_t &root_gate, gate_t &event_gate)
Build a GenericCircuit containing the closures of two roots, with shared subgraphs unified.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Semiring-agnostic in-memory provenance circuit.
Monte Carlo sampling over a GenericCircuit, RV-aware.
Continuous random-variable helpers (distribution parsing, moments).
Support-based bound check for continuous-RV comparators.
Datum rv_histogram(PG_FUNCTION_ARGS)
Exception type thrown by circuit operations on invalid input.
gateType getGateType(gate_t g) const
Return the type of gate g.
In-memory provenance circuit with semiring-generic evaluation.
std::string getExtra(gate_t g) const
Return the string extra for gate g.
std::pair< double, double > compute_support(const GenericCircuit &gc, gate_t root, std::optional< gate_t > event_root)
Compute the [lo, hi] support interval of a scalar sub-circuit rooted at root.
double parseDoubleStrict(const std::string &s)
Strictly parse s as a double.
ConditionalScalarSamples monteCarloConditionalScalarSamples(const GenericCircuit &gc, gate_t root, gate_t event_root, unsigned samples)
Rejection-sample root conditioned on event_root.
std::vector< double > monteCarloScalarSamples(const GenericCircuit &gc, gate_t root, unsigned samples)
Sample a scalar sub-circuit samples times and return the draws.
std::optional< std::vector< double > > 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-for...
int provsql_rv_mc_samples
Default sample count for analytical-evaluator MC fallbacks; 0 disables fallback (callers raise instea...
Uniform error-reporting macros for ProvSQL.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
const char * gate_type_name[]
Names of gate types.
Core types, constants, and utilities shared across ProvSQL.
@ gate_rv
Continuous random-variable leaf (extra encodes distribution).
@ gate_mixture
Probabilistic mixture: three wires [p_token (gate_input Bernoulli), x_token, y_token]; samples x when...
@ gate_arith
n-ary arithmetic gate over scalar-valued children (info1 holds operator tag)
C++ utility functions for UUID manipulation.