29#include "utils/uuid.h"
44#include <unordered_map>
45#include <unordered_set>
58void extractSubcircuit(
const GenericCircuit &gc,
gate_t root,
59 GenericCircuit &sub,
gate_t &sub_root)
61 std::unordered_set<gate_t> seen;
62 std::stack<gate_t> stk;
64 while (!stk.empty()) {
65 gate_t g = stk.top(); stk.pop();
66 if (!seen.insert(g).second)
continue;
67 const std::string u = gc.
getUUID(g);
70 const double pr = gc.
getProb(g);
71 if (!std::isnan(pr)) sub.
setProb(
id, pr);
75 sub.
setInfos(
id, infos.first, infos.second);
78 if (infos.first != 0 || infos.second != 0)
79 sub.
setInfos(
id, infos.first, infos.second);
105 extractSubcircuit(gc, boolRoot, sub, sub_root);
112using RvSet = std::set<gate_t>;
128bool rvIsParametric(
const GenericCircuit &gc,
gate_t g)
132 return tmpl && tmpl->parametric();
140 return tmpl.family->factory(0.0, 0.0)->meanIsAffine();
148class FootprintCache {
150 explicit FootprintCache(
const GenericCircuit &gc) : gc_(gc) {}
152 const RvSet &of(
gate_t g) {
153 auto it = cache_.find(g);
154 if (it != cache_.end())
return it->second;
156 auto type = gc_.getGateType(g);
167 for (
gate_t c : gc_.getWires(g)) {
168 const auto &cs = of(c);
169 s.insert(cs.begin(), cs.end());
179 for (
gate_t c : gc_.getWires(g)) {
180 const auto &cs = of(c);
181 s.insert(cs.begin(), cs.end());
184 const auto &wires = gc_.getWires(g);
185 if (gc_.isCategoricalMixture(g)) {
190 for (std::size_t i = 1; i < wires.size(); ++i) {
191 const auto &fm = of(wires[i]);
192 s.insert(fm.begin(), fm.end());
194 }
else if (wires.size() == 3) {
203 const auto &fp = of(wires[0]);
204 s.insert(fp.begin(), fp.end());
205 const auto &fx = of(wires[1]);
206 s.insert(fx.begin(), fx.end());
207 const auto &fy = of(wires[2]);
208 s.insert(fy.begin(), fy.end());
218 const auto &wires = gc_.getWires(g);
219 if (!wires.empty()) {
220 const auto &fk = of(wires[0]);
221 s.insert(fk.begin(), fk.end());
241 for (
gate_t c : gc_.getWires(g)) {
242 const auto &cs = of(c);
243 s.insert(cs.begin(), cs.end());
254 return cache_.emplace(g, std::move(s)).first->second;
258 const GenericCircuit &gc_;
259 std::unordered_map<gate_t, RvSet> cache_;
262bool pairwise_disjoint(FootprintCache &fp,
const std::vector<gate_t> &children)
265 for (
gate_t c : children) {
266 const auto &fpc = fp.of(c);
268 if (!seen.insert(r).second)
return false;
287iidOrderStatMean(
const GenericCircuit &gc,
gate_t g,
bool isMax,
290 const auto &raw_wires = gc.
getWires(g);
291 if (raw_wires.empty())
297 std::vector<gate_t> wires;
298 wires.reserve(raw_wires.size());
300 std::set<gate_t> seen;
301 for (
gate_t c : raw_wires)
302 if (seen.insert(c).second)
305 if (!pairwise_disjoint(fp, wires))
308 std::vector<DistributionSpec> specs;
309 specs.reserve(wires.size());
322 for (std::size_t i = 1; i < specs.size(); ++i)
323 if (specs[i].family != specs[0].family ||
324 specs[i].p1 != specs[0].p1 || specs[i].p2 != specs[0].p2)
339mixedOrderStatMean(
const GenericCircuit &gc,
gate_t g,
bool isMax,
342 const auto &raw_wires = gc.
getWires(g);
343 if (raw_wires.empty())
345 std::vector<gate_t> wires;
347 std::set<gate_t> seen;
348 for (
gate_t c : raw_wires)
349 if (seen.insert(c).second)
352 if (!pairwise_disjoint(fp, wires))
357 std::vector<std::unique_ptr<Distribution>> dists;
358 double lo = 0.0, hi = 0.0;
368 if (!d->integrationRange(clo, chi))
370 if (first) { lo = clo; hi = chi; first =
false; }
371 else { lo = std::min(lo, clo); hi = std::max(hi, chi); }
372 dists.push_back(std::move(d));
381 for (
const auto &d : dists) {
382 const double F = d->cdf(t);
383 if (std::isnan(F))
return std::numeric_limits<double>::quiet_NaN();
388 double prod1mF = 1.0;
389 for (
const auto &d : dists) {
390 const double F = d->cdf(t);
391 if (std::isnan(F))
return std::numeric_limits<double>::quiet_NaN();
392 prod1mF *= (1.0 - F);
396 if (std::isnan(integral))
398 return lo + integral;
401unsigned mc_samples_or_throw(
const std::string &what)
405 throw CircuitException(
406 what +
" could not be decomposed analytically and "
407 "provsql.rv_mc_samples = 0 disables the Monte Carlo fallback");
417 "%s: no closed form found; estimating by Monte Carlo over %d samples "
418 "(an approximation, not an exact moment) -- set provsql.rv_mc_samples = 0 "
419 "to require an exact result instead", what.c_str(), n);
420 return static_cast<unsigned>(n);
425constexpr unsigned kEnumerateMaxInputs = 20;
438std::optional<double> enumerated_moment(
const GenericCircuit &gc,
gate_t g,
439 unsigned k,
double mu,
440 std::optional<gate_t> event,
441 const std::string &what)
444 if (!worlds)
return std::nullopt;
445 if (event && worlds->empty())
446 throw CircuitException(what +
": conditioning event is infeasible");
447 double total = 0.0, mass = 0.0;
448 for (
const auto &w : *worlds) {
449 if (!w.second)
continue;
450 total += w.first * std::pow(*w.second - mu,
static_cast<double>(k));
457double mc_raw_moment(
const GenericCircuit &gc,
gate_t g,
unsigned k,
458 const std::string &what)
460 if (
auto m = enumerated_moment(gc, g, k, 0.0, std::nullopt, what))
463 if (samples.empty())
return 0.0;
468 std::size_t defined_count = 0;
469 for (
const auto &x : samples) {
471 total += std::pow(*x,
static_cast<double>(k));
475 return total /
static_cast<double>(defined_count);
478double mc_central_moment(
const GenericCircuit &gc,
gate_t g,
unsigned k,
479 double mu,
const std::string &what)
481 if (
auto m = enumerated_moment(gc, g, k, mu, std::nullopt, what))
484 if (samples.empty())
return 0.0;
486 std::size_t defined_count = 0;
487 for (
const auto &x : samples) {
489 total += std::pow(*x - mu,
static_cast<double>(k));
493 return total /
static_cast<double>(defined_count);
500unsigned min_accepted_floor(
unsigned attempted)
502 unsigned floor = attempted / 1000;
503 return floor < 5 ? 5 : floor;
507 const std::string &what)
509 if (cs.accepted.empty()) {
515 throw CircuitException(
516 what +
": conditioning event is infeasible (0 of " +
517 std::to_string(cs.attempted) +
518 " Monte Carlo samples satisfied it)");
520 const unsigned floor = min_accepted_floor(cs.attempted);
521 if (cs.accepted.size() < floor) {
522 throw CircuitException(
523 what +
": conditional MC accepted only " +
524 std::to_string(cs.accepted.size()) +
" out of " +
525 std::to_string(cs.attempted) +
526 " samples (need >= " + std::to_string(floor) +
527 "); raise provsql.rv_mc_samples or tighten the event.");
531double mc_conditional_raw_moment(
const GenericCircuit &gc,
gate_t g,
532 unsigned k,
gate_t event_root,
533 const std::string &what)
535 if (
auto m = enumerated_moment(gc, g, k, 0.0, event_root, what))
538 gc, g, event_root, mc_samples_or_throw(what));
539 check_acceptance_or_throw(cs, what);
543 std::size_t defined_count = 0;
544 for (
const auto &x : cs.accepted) {
546 total += std::pow(*x,
static_cast<double>(k));
550 return total /
static_cast<double>(defined_count);
553double mc_conditional_central_moment(
const GenericCircuit &gc,
gate_t g,
554 unsigned k,
double mu,
556 const std::string &what)
558 if (
auto m = enumerated_moment(gc, g, k, mu, event_root, what))
561 gc, g, event_root, mc_samples_or_throw(what));
562 check_acceptance_or_throw(cs, what);
564 std::size_t defined_count = 0;
565 for (
const auto &x : cs.accepted) {
567 total += std::pow(*x - mu,
static_cast<double>(k));
571 return total /
static_cast<double>(defined_count);
574double rec_expectation(
const GenericCircuit &gc,
gate_t g, FootprintCache &fp);
575double rec_variance(
const GenericCircuit &gc,
gate_t g, FootprintCache &fp);
576double rec_raw_moment(
const GenericCircuit &gc,
gate_t g,
unsigned k,
596 double sw = 0.0, swx = 0.0;
597 for (
const auto &[x, w] : post.particles) {
599 swx += w * std::pow(x,
static_cast<double>(k));
609 double sw = 0.0, swd = 0.0;
610 for (
const auto &[x, w] : post.particles) {
612 swd += w * std::pow(x - mu,
static_cast<double>(k));
623 auto &pts = post.particles;
625 std::sort(pts.begin(), pts.end(), [](
const auto &a,
const auto &b) {
626 return sql_less(a.first, b.first);
629 for (
const auto &pw : pts) total += pw.second;
630 if (!(total > 0.0))
return pts.front().first;
631 const double target = p * total;
633 for (std::size_t i = 0; i < pts.size(); ++i) {
634 const double prev = cum;
635 cum += pts[i].second;
637 if (i == 0)
return pts[0].first;
640 const double frac = (pts[i].second > 0.0)
641 ? (target - prev) / pts[i].second
643 return pts[i - 1].first + frac * (pts[i].first - pts[i - 1].first);
646 return pts.back().first;
652 const std::string &what)
654 if (post.particles.empty() || post.weight_sum <= 0.0) {
655 throw CircuitException(
656 what +
": evidence is infeasible (no positive-weight draw among " +
657 std::to_string(post.attempted) +
658 " Monte Carlo samples); the observations may contradict the prior, "
659 "or raise provsql.rv_mc_samples");
661 const double ess = post.effectiveSampleSize();
662 const double nonzero =
static_cast<double>(post.particles.size());
666 "%s: posterior effective sample size low (%.1f of %u accepted); "
667 "likelihood weighting is degenerating -- raise provsql.rv_mc_samples, "
668 "or the model has many observations per latent (defer to SMC)",
669 what.c_str(), ess,
static_cast<unsigned>(post.particles.size()));
689try_truncated_closed_form(
const GenericCircuit &gc,
gate_t root,
690 gate_t event_root,
unsigned k,
bool central)
693 if (!m)
return std::nullopt;
694 const double lo = m->lo, hi = m->hi;
701 auto raw = [&](
unsigned q) -> std::optional<double> {
702 if (q == 0)
return 1.0;
703 return dist->truncatedRawMoment(lo, hi, q);
706 if (!central)
return raw(k);
714 DistributionSpec targetSpec;
715 DistributionSpec otherSpec;
723std::optional<RvVsRvCond>
724matchRvVsRvConditional(
const GenericCircuit &gc,
gate_t root,
gate_t event_root)
729 if (!specX)
return std::nullopt;
731 const auto &wires = gc.
getWires(event_root);
732 if (wires.size() != 2)
return std::nullopt;
740 if (wires[0] == root) { other = wires[1]; targetLeft =
true; }
741 else if (wires[1] == root) { other = wires[0]; targetLeft =
false; }
742 else return std::nullopt;
746 if (!specY)
return std::nullopt;
756 if (targetLeft) targetGreater = greaterOp;
757 else targetGreater = lessOp;
758 return RvVsRvCond{*specX, *specY, targetGreater};
769 bool targetGreater,
unsigned k)
776 if (!dX->integrationRange(lo, hi))
777 return std::numeric_limits<double>::quiet_NaN();
779 auto base = [&](
double x) {
780 const double fX = dX->pdf(x);
781 const double FY = dY->cdf(x);
782 if (std::isnan(fX) || std::isnan(FY))
783 return std::numeric_limits<double>::quiet_NaN();
784 const double w = targetGreater ? FY : (1.0 - FY);
788 if (std::isnan(den) || !(den > 1e-12))
789 return std::numeric_limits<double>::quiet_NaN();
792 return std::pow(x,
static_cast<double>(k)) * base(x);
795 return std::numeric_limits<double>::quiet_NaN();
803try_rvVsRv_conditional_moment(
const GenericCircuit &gc,
gate_t root,
804 gate_t event_root,
unsigned k,
bool central)
806 auto m = matchRvVsRvConditional(gc, root, event_root);
807 if (!m)
return std::nullopt;
809 auto raw = [&](
unsigned q) -> std::optional<double> {
810 if (q == 0)
return 1.0;
811 double r = rvVsRvConditionalMoment(m->targetSpec, m->otherSpec,
812 m->targetGreater, q);
813 if (std::isnan(r))
return std::nullopt;
817 if (!central)
return raw(k);
828 DistributionSpec other;
842 const std::vector<PivotFactor> &factors,
847 if (!dX->integrationRange(lo, hi))
848 return std::numeric_limits<double>::quiet_NaN();
849 for (
const auto &f : factors)
851 if (f.pivotGreater) lo = std::max(lo, f.konst);
852 else hi = std::min(hi, f.konst);
854 if (!(hi > lo))
return 0.0;
856 std::vector<std::unique_ptr<Distribution>> others;
857 std::vector<bool> greater;
858 for (
const auto &f : factors)
861 greater.push_back(f.pivotGreater);
865 const double fX = dX->pdf(x);
866 if (std::isnan(fX))
return std::numeric_limits<double>::quiet_NaN();
868 for (std::size_t j = 0; j < others.size(); ++j) {
869 const double FY = others[j]->cdf(x);
870 if (std::isnan(FY))
return std::numeric_limits<double>::quiet_NaN();
871 w *= greater[j] ? FY : (1.0 - FY);
873 return std::pow(x,
static_cast<double>(k)) * w;
879struct PivotConjunctionCond {
880 DistributionSpec targetSpec;
881 std::vector<PivotFactor> factors;
889std::optional<PivotConjunctionCond>
890matchPivotConjunctionConditional(
const GenericCircuit &gc,
gate_t root,
897 if (!specX)
return std::nullopt;
899 const auto &kids = gc.
getWires(event_root);
900 if (kids.size() < 2)
return std::nullopt;
901 std::vector<PivotFactor> factors;
902 std::set<gate_t> othersSeen;
906 if (w.size() != 2)
return std::nullopt;
911 gate_t other;
bool targetLeft;
912 if (w[0] == root) { other = w[1]; targetLeft =
true; }
913 else if (w[1] == root) { other = w[0]; targetLeft =
false; }
914 else return std::nullopt;
919 const bool pivotGreater = targetLeft ? greaterOp : lessOp;
926 if (!othersSeen.insert(other).second)
return std::nullopt;
928 if (!specY)
return std::nullopt;
929 f = {
false, *specY, 0.0, pivotGreater};
930 }
else return std::nullopt;
931 factors.push_back(std::move(f));
933 return PivotConjunctionCond{*specX, std::move(factors)};
940try_pivotConjunction_conditional_moment(
const GenericCircuit &gc,
gate_t root,
941 gate_t event_root,
unsigned k,
944 auto m = matchPivotConjunctionConditional(gc, root, event_root);
945 if (!m)
return std::nullopt;
947 const double den = pivotConjunctionIntegral(m->targetSpec, m->factors, 0);
948 if (std::isnan(den) || !(den > 1e-12))
950 auto raw = [&](
unsigned q) -> std::optional<double> {
951 if (q == 0)
return 1.0;
952 const double num = pivotConjunctionIntegral(m->targetSpec, m->factors, q);
953 if (std::isnan(num))
return std::nullopt;
957 if (!central)
return raw(k);
969std::unique_ptr<Distribution> transform_image(
const GenericCircuit &gc,
976 if (!transform)
return nullptr;
981 if (!spec)
return nullptr;
991std::unique_ptr<Distribution> product_image(
const GenericCircuit &gc,
995 if (wires.empty())
return nullptr;
996 double c_total = 1.0;
997 std::vector<std::unique_ptr<Distribution>> dists;
998 std::vector<const Distribution *> factors;
999 std::set<gate_t> seen;
1004 catch (
const CircuitException &) {
return nullptr; }
1007 if (t !=
gate_rv)
return nullptr;
1008 if (!seen.insert(w).second)
return nullptr;
1010 if (!spec)
return nullptr;
1012 factors.push_back(dists.back().get());
1014 if (factors.empty())
return nullptr;
1017 std::unique_ptr<Distribution> combined;
1018 if (factors.size() == 1)
1019 combined = std::move(dists.front());
1022 if (!combined)
return nullptr;
1023 if (c_total != 1.0)
return combined->scale(c_total);
1043struct AffineArm {
double a, b; };
1045std::optional<AffineArm>
1046affineInPivot(
const GenericCircuit &gc,
gate_t arm,
gate_t pivot)
1052 return (arm == pivot) ? std::optional<AffineArm>(AffineArm{1.0, 0.0})
1057 auto isPivot = [&](
gate_t x) {
1060 auto constVal = [&](
gate_t x,
double &out) {
1067 return AffineArm{-1.0, 0.0};
1069 if (isPivot(w[0]) && constVal(w[1], c))
return AffineArm{1.0, c};
1070 if (isPivot(w[1]) && constVal(w[0], c))
return AffineArm{1.0, c};
1073 if (isPivot(w[0]) && constVal(w[1], c))
return AffineArm{1.0, -c};
1074 if (isPivot(w[1]) && constVal(w[0], c))
return AffineArm{-1.0, c};
1077 if (isPivot(w[0]) && constVal(w[1], c))
return AffineArm{c, 0.0};
1078 if (isPivot(w[1]) && constVal(w[0], c))
return AffineArm{c, 0.0};
1081 return std::nullopt;
1084std::optional<double>
1085singlePivotCaseRawMoment(
const GenericCircuit &gc,
gate_t g,
unsigned k)
1088 const auto &wires = gc.
getWires(g);
1089 if (wires.size() < 3 || wires.size() % 2 == 0)
return std::nullopt;
1090 const std::size_t m = wires.size() / 2;
1093 gate_t pivot{};
bool havePivot =
false;
1095 std::vector<Guard> guards;
1097 for (std::size_t i = 0; i < m; ++i) {
1098 gate_t gd = wires[2 * i];
1101 if (gw.size() != 2)
return std::nullopt;
1105 return std::nullopt;
1106 gate_t rvSide, constSide;
bool pivotLeft;
1108 rvSide = gw[0]; constSide = gw[1]; pivotLeft =
true;
1111 rvSide = gw[1]; constSide = gw[0]; pivotLeft =
false;
1112 }
else return std::nullopt;
1113 if (!havePivot) { pivot = rvSide; havePivot =
true; }
1114 else if (rvSide != pivot)
return std::nullopt;
1117 if (!havePivot)
return std::nullopt;
1120 if (!spec)
return std::nullopt;
1124 std::vector<AffineArm> arms;
1125 arms.reserve(m + 1);
1126 for (std::size_t i = 0; i < m; ++i) {
1127 auto af = affineInPivot(gc, wires[2 * i + 1], pivot);
1128 if (!af)
return std::nullopt;
1129 arms.push_back(*af);
1132 auto af = affineInPivot(gc, wires.back(), pivot);
1133 if (!af)
return std::nullopt;
1134 arms.push_back(*af);
1139 auto guardTrue = [](
const Guard &gd,
double x) ->
bool {
1140 const double lhs = gd.pivotLeft ? x : gd.c;
1141 const double rhs = gd.pivotLeft ? gd.c : x;
1147 default:
return false;
1150 auto pickArm = [&](
double x) ->
const AffineArm & {
1151 for (std::size_t i = 0; i < m; ++i)
1152 if (guardTrue(guards[i], x))
return arms[i];
1157 std::vector<double> cuts;
1158 for (
const auto &gd : guards) cuts.push_back(gd.c);
1159 std::sort(cuts.begin(), cuts.end());
1160 cuts.erase(std::unique(cuts.begin(), cuts.end()), cuts.end());
1162 const double NINF = -std::numeric_limits<double>::infinity();
1163 const double PINF = std::numeric_limits<double>::infinity();
1166 const std::size_t nseg = cuts.size() + 1;
1167 for (std::size_t s = 0; s < nseg; ++s) {
1168 const double lo = (s == 0) ? NINF : cuts[s - 1];
1169 const double hi = (s == cuts.size()) ? PINF : cuts[s];
1170 if (lo == hi)
continue;
1173 if (lo == NINF && hi == PINF) t = 0.0;
1174 else if (lo == NINF) t = hi - 1.0;
1175 else if (hi == PINF) t = lo + 1.0;
1176 else t = 0.5 * (lo + hi);
1177 const AffineArm &arm = pickArm(t);
1179 const double dF = dist->cdf(hi) - dist->cdf(lo);
1180 if (std::isnan(dF))
return std::nullopt;
1181 if (dF <= 0.0)
continue;
1184 for (
unsigned j = 0; j <= k; ++j) {
1185 const double aj = std::pow(arm.a,
static_cast<double>(j));
1186 if (aj == 0.0)
continue;
1187 const double coef =
binomial(k, j) * aj
1188 * std::pow(arm.b,
static_cast<double>(k - j));
1189 if (coef == 0.0)
continue;
1194 auto trm = dist->truncatedRawMoment(lo, hi, j);
1195 if (!trm)
return std::nullopt;
1198 total += coef * intj;
1213std::optional<double>
1214twoArmCaseRawMoment(
const GenericCircuit &gc,
gate_t g,
unsigned k)
1217 const auto &wires = gc.
getWires(g);
1218 if (wires.size() != 3)
return std::nullopt;
1221 const auto &gw = gc.
getWires(guard);
1222 if (gw.size() != 2)
return std::nullopt;
1226 return std::nullopt;
1227 gate_t a = gw[0], b = gw[1];
1229 return std::nullopt;
1232 if (!specA || !specB)
return std::nullopt;
1239 auto armContribution = [&](
gate_t armGate,
bool regionAgtB)
1240 -> std::optional<double> {
1241 for (
int which = 0; which < 2; ++which) {
1242 gate_t pv = (which == 0) ? a : b;
1243 auto af = affineInPivot(gc, armGate, pv);
1249 const bool pivotGreater = (which == 0) ? regionAgtB : !regionAgtB;
1250 const PivotFactor f{
false, otSpec, 0.0, pivotGreater};
1252 for (
unsigned j = 0; j <= k; ++j) {
1253 const double aj = std::pow(af->a,
static_cast<double>(j));
1254 if (aj == 0.0)
continue;
1255 const double coef =
binomial(k, j) * aj
1256 * std::pow(af->b,
static_cast<double>(k - j));
1257 if (coef == 0.0)
continue;
1258 const double I = pivotConjunctionIntegral(pvSpec, {f}, j);
1259 if (std::isnan(I))
return std::nullopt;
1264 return std::nullopt;
1267 auto c0 = armContribution(wires[1], guard_a_gt_b);
1268 if (!c0)
return std::nullopt;
1269 auto c1 = armContribution(wires[2], !guard_a_gt_b);
1270 if (!c1)
return std::nullopt;
1279evalGuardUnderOrder(
const GenericCircuit &gc,
gate_t guard,
1280 const std::unordered_map<gate_t, int> &rank)
1284 const auto &w = gc.
getWires(guard);
1285 if (w.size() != 2)
return std::nullopt;
1288 if (!ok)
return std::nullopt;
1289 auto ia = rank.find(w[0]), ib = rank.find(w[1]);
1290 if (ia == rank.end() || ib == rank.end())
return std::nullopt;
1291 const int ra = ia->second, rb = ib->second;
1300 return std::nullopt;
1306 auto v = evalGuardUnderOrder(gc, c, rank);
1307 if (!v)
return std::nullopt;
1308 acc = isAnd ? (acc && *v) : (acc || *v);
1312 return std::nullopt;
1325std::optional<double>
1326orderStatCaseRawMoment(
const GenericCircuit &gc,
gate_t g,
unsigned k)
1329 const auto &wires = gc.
getWires(g);
1330 if (wires.size() < 3 || wires.size() % 2 == 0)
return std::nullopt;
1331 const std::size_t m = wires.size() / 2;
1334 std::vector<gate_t> armRV(m + 1);
1335 std::vector<gate_t> uniq;
1336 std::unordered_map<gate_t, DistributionSpec> specOf;
1337 auto noteRV = [&](
gate_t v) ->
bool {
1339 if (specOf.find(v) == specOf.end()) {
1341 if (!sp)
return false;
1342 specOf.emplace(v, *sp);
1347 for (std::size_t i = 0; i < m; ++i) {
1348 armRV[i] = wires[2 * i + 1];
1349 if (!noteRV(armRV[i]))
return std::nullopt;
1351 armRV[m] = wires.back();
1352 if (!noteRV(armRV[m]))
return std::nullopt;
1354 const std::size_t n = uniq.size();
1355 if (n < 2 || n > 7)
return std::nullopt;
1359 std::vector<gate_t> guards(m);
1360 for (std::size_t i = 0; i < m; ++i) guards[i] = wires[2 * i];
1363 std::vector<std::size_t> perm(n);
1364 for (std::size_t i = 0; i < n; ++i) perm[i] = i;
1365 bool alwaysMax =
true, alwaysMin =
true;
1367 std::unordered_map<gate_t, int> rank;
1368 for (std::size_t i = 0; i < n; ++i) rank[uniq[perm[i]]] = static_cast<int>(i);
1370 gate_t maxRV = uniq[perm[n - 1]], minRV = uniq[perm[0]];
1372 gate_t selected = armRV[m];
1373 for (std::size_t i = 0; i < m; ++i) {
1374 auto gv = evalGuardUnderOrder(gc, guards[i], rank);
1375 if (!gv)
return std::nullopt;
1376 if (*gv) { selected = armRV[i];
break; }
1378 if (selected != maxRV) alwaysMax =
false;
1379 if (selected != minRV) alwaysMin =
false;
1380 if (!alwaysMax && !alwaysMin)
return std::nullopt;
1381 }
while (std::next_permutation(perm.begin(), perm.end()));
1383 const bool isMax = alwaysMax;
1387 for (std::size_t i = 0; i < n; ++i) {
1388 std::vector<PivotFactor> factors;
1389 factors.reserve(n - 1);
1390 for (std::size_t j = 0; j < n; ++j) {
1391 if (j == i)
continue;
1392 factors.push_back({
false, specOf.at(uniq[j]), 0.0, isMax});
1394 const double I = pivotConjunctionIntegral(specOf.at(uniq[i]), factors, k);
1395 if (std::isnan(I))
return std::nullopt;
1402std::optional<double>
1403caseAnalyticRawMoment(
const GenericCircuit &gc,
gate_t g,
unsigned k)
1405 if (
auto v = singlePivotCaseRawMoment(gc, g, k))
return v;
1406 if (
auto v = twoArmCaseRawMoment(gc, g, k))
return v;
1407 if (
auto v = orderStatCaseRawMoment(gc, g, k))
return v;
1408 return std::nullopt;
1411double rec_expectation(
const GenericCircuit &gc,
gate_t g, FootprintCache &fp)
1426 if (rvIsParametric(gc, g)) {
1428 if (tmpl && familyMeanIsAffine(*tmpl)) {
1431 return p.wire_slot < 0 ? p.literal
1432 : rec_expectation(gc, w[p.wire_slot], fp);
1435 ->factory(param_mean(tmpl->p1), param_mean(tmpl->p2))
1438 return mc_raw_moment(gc, g, 1,
"Expectation of a latent gate_rv");
1442 throw CircuitException(
1443 "Expectation: malformed gate_rv extra: " + gc.
getExtra(g));
1448 const auto &wires = gc.
getWires(g);
1452 for (
gate_t c : wires) s += rec_expectation(gc, c, fp);
1456 if (wires.size() != 2)
1457 throw CircuitException(
"gate_arith MINUS must be binary");
1458 return rec_expectation(gc, wires[0], fp)
1459 - rec_expectation(gc, wires[1], fp);
1462 if (wires.size() != 1)
1463 throw CircuitException(
"gate_arith NEG must be unary");
1464 return -rec_expectation(gc, wires[0], fp);
1467 if (pairwise_disjoint(fp, wires)) {
1469 for (
gate_t c : wires) p *= rec_expectation(gc, c, fp);
1472 return mc_raw_moment(gc, g, 1,
1473 "Expectation of gate_arith TIMES with shared random variables");
1477 return mc_raw_moment(gc, g, 1,
1478 "Expectation of gate_arith INTDIV (integer division)");
1480 if (wires.size() != 2)
1481 throw CircuitException(
"gate_arith DIV must be binary");
1491 "Expectation of gate_arith DIV by a constant zero");
1492 return rec_expectation(gc, wires[0], fp) / divisor;
1494 return mc_raw_moment(gc, g, 1,
1495 "Expectation of gate_arith DIV with non-constant divisor");
1505 if (
auto v = iidOrderStatMean(gc, g, isMax, fp))
1507 if (
auto v = mixedOrderStatMean(gc, g, isMax, fp))
1509 return mc_raw_moment(gc, g, 1,
1510 "Expectation of gate_arith " + std::string(isMax ?
"MAX" :
"MIN"));
1515 if (wires.size() == 1)
1516 return rec_expectation(gc, wires[0], fp);
1528 return mc_raw_moment(gc, g, 1,
1529 "Expectation of a gate_arith rounding or absolute value");
1538 if (
auto image = transform_image(gc, g, op))
1539 return image->mean();
1540 return mc_raw_moment(gc, g, 1,
1541 "Expectation of a gate_arith nonlinear transform");
1545 return mc_raw_moment(gc, g, 1,
1546 "Expectation of a gate_arith PERCENTILE");
1548 throw CircuitException(
1549 "Expectation: unknown gate_arith op tag: " +
1550 std::to_string(
static_cast<unsigned>(op)));
1553 const auto &wires = gc.
getWires(g);
1558 for (std::size_t i = 1; i < wires.size(); ++i) {
1568 if (wires.size() != 3)
1569 throw CircuitException(
1570 "Expectation: gate_mixture must have exactly three children");
1571 const double pi = mixturePi(gc, wires[0]);
1572 return pi * rec_expectation(gc, wires[1], fp)
1573 + (1.0 - pi) * rec_expectation(gc, wires[2], fp);
1576 if (
auto v = caseAnalyticRawMoment(gc, g, 1))
1578 return mc_raw_moment(gc, g, 1,
"Expectation of gate type gate_case");
1580 return mc_raw_moment(gc, g, 1,
1581 "Expectation of gate type " + std::string(
gate_type_name[type]));
1585double rec_variance(
const GenericCircuit &gc,
gate_t g, FootprintCache &fp)
1595 if (rvIsParametric(gc, g)) {
1596 const std::string what =
"Variance of a latent gate_rv";
1597 const double mu = mc_raw_moment(gc, g, 1, what);
1598 return mc_central_moment(gc, g, 2, mu, what);
1602 throw CircuitException(
1603 "Variance: malformed gate_rv extra: " + gc.
getExtra(g));
1608 const auto &wires = gc.
getWires(g);
1609 auto mc_var = [&](
const std::string &what) {
1610 const double mu = mc_raw_moment(gc, g, 1, what);
1611 return mc_central_moment(gc, g, 2, mu, what);
1615 if (pairwise_disjoint(fp, wires)) {
1617 for (
gate_t c : wires) s += rec_variance(gc, c, fp);
1621 "Variance of gate_arith PLUS with shared random variables");
1624 if (wires.size() != 2)
1625 throw CircuitException(
"gate_arith MINUS must be binary");
1626 if (pairwise_disjoint(fp, wires)) {
1627 return rec_variance(gc, wires[0], fp)
1628 + rec_variance(gc, wires[1], fp);
1631 "Variance of gate_arith MINUS with shared random variables");
1634 if (wires.size() != 1)
1635 throw CircuitException(
"gate_arith NEG must be unary");
1636 return rec_variance(gc, wires[0], fp);
1639 if (pairwise_disjoint(fp, wires)) {
1642 double prod_e2 = 1.0;
1643 double prod_e1 = 1.0;
1645 const double mu_c = rec_expectation(gc, c, fp);
1646 const double v_c = rec_variance(gc, c, fp);
1647 prod_e2 *= (v_c + mu_c * mu_c);
1650 return prod_e2 - prod_e1 * prod_e1;
1653 "Variance of gate_arith TIMES with shared random variables");
1656 return mc_var(
"Variance of gate_arith INTDIV (integer division)");
1658 if (wires.size() != 2)
1659 throw CircuitException(
"gate_arith DIV must be binary");
1664 "Variance of gate_arith DIV by a constant zero");
1665 return rec_variance(gc, wires[0], fp) / (divisor * divisor);
1668 "Variance of gate_arith DIV with non-constant divisor");
1674 "Variance of gate_arith " +
1679 if (wires.size() == 1)
1680 return rec_variance(gc, wires[0], fp);
1689 "Variance of a gate_arith rounding or absolute value");
1693 if (
auto image = transform_image(gc, g, op))
1694 return image->variance();
1695 return mc_var(
"Variance of a gate_arith nonlinear transform");
1697 return mc_var(
"Variance of a gate_arith PERCENTILE");
1699 throw CircuitException(
1700 "Variance: unknown gate_arith op tag: " +
1701 std::to_string(
static_cast<unsigned>(op)));
1704 const auto &wires = gc.
getWires(g);
1707 double e1 = 0.0, e2 = 0.0;
1708 for (std::size_t i = 1; i < wires.size(); ++i) {
1709 const double p = gc.
getProb(wires[i]);
1714 return e2 - e1 * e1;
1718 if (wires.size() != 3)
1719 throw CircuitException(
1720 "Variance: gate_mixture must have exactly three children");
1721 const double pi = mixturePi(gc, wires[0]);
1722 const double ex = rec_expectation(gc, wires[1], fp);
1723 const double ey = rec_expectation(gc, wires[2], fp);
1724 const double vx = rec_variance(gc, wires[1], fp);
1725 const double vy = rec_variance(gc, wires[2], fp);
1726 const double em = pi * ex + (1.0 - pi) * ey;
1727 return pi * (vx + ex * ex)
1728 + (1.0 - pi) * (vy + ey * ey)
1732 if (
auto m2 = caseAnalyticRawMoment(gc, g, 2))
1733 if (
auto m1 = caseAnalyticRawMoment(gc, g, 1))
1734 return *m2 - (*m1) * (*m1);
1735 const std::string what =
"Variance of gate type gate_case";
1736 const double mu = mc_raw_moment(gc, g, 1, what);
1737 return mc_central_moment(gc, g, 2, mu, what);
1740 const std::string what =
1742 const double mu = mc_raw_moment(gc, g, 1, what);
1743 return mc_central_moment(gc, g, 2, mu, what);
1748double rec_raw_moment(
const GenericCircuit &gc,
gate_t g,
unsigned k,
1751 if (k == 0)
return 1.0;
1752 if (k == 1)
return rec_expectation(gc, g, fp);
1758 static_cast<double>(k));
1761 if (rvIsParametric(gc, g))
1762 return mc_raw_moment(gc, g, k,
"Raw moment of a latent gate_rv");
1765 throw CircuitException(
1766 "Moment: malformed gate_rv extra: " + gc.
getExtra(g));
1771 const auto &wires = gc.
getWires(g);
1774 if (wires.size() != 1)
1775 throw CircuitException(
"gate_arith NEG must be unary");
1776 const double v = rec_raw_moment(gc, wires[0], k, fp);
1777 return ((k % 2 == 0) ? 1.0 : -1.0) * v;
1780 if (pairwise_disjoint(fp, wires)) {
1784 std::vector<double> m_acc(k + 1, 0.0);
1785 for (
unsigned i = 0; i <= k; ++i)
1786 m_acc[i] = rec_raw_moment(gc, wires[0], i, fp);
1787 for (
size_t w = 1; w < wires.size(); ++w) {
1788 std::vector<double> next(k + 1, 0.0);
1789 std::vector<double> moments_y(k + 1, 0.0);
1790 for (
unsigned i = 0; i <= k; ++i)
1791 moments_y[i] = rec_raw_moment(gc, wires[w], i, fp);
1792 for (
unsigned kp = 0; kp <= k; ++kp) {
1794 for (
unsigned i = 0; i <= kp; ++i) {
1795 total +=
binomial(kp, i) * m_acc[i] * moments_y[kp - i];
1799 m_acc = std::move(next);
1803 return mc_raw_moment(gc, g, k,
1804 "Raw moment of gate_arith PLUS with shared random variables");
1807 if (wires.size() != 2)
1808 throw CircuitException(
"gate_arith MINUS must be binary");
1809 if (pairwise_disjoint(fp, wires)) {
1811 for (
unsigned i = 0; i <= k; ++i) {
1812 const double sign = ((k - i) % 2 == 0) ? 1.0 : -1.0;
1814 * rec_raw_moment(gc, wires[0], i, fp)
1816 * rec_raw_moment(gc, wires[1], k - i, fp);
1820 return mc_raw_moment(gc, g, k,
1821 "Raw moment of gate_arith MINUS with shared random variables");
1824 if (pairwise_disjoint(fp, wires)) {
1827 for (
gate_t c : wires) p *= rec_raw_moment(gc, c, k, fp);
1830 return mc_raw_moment(gc, g, k,
1831 "Raw moment of gate_arith TIMES with shared random variables");
1834 return mc_raw_moment(gc, g, k,
1835 "Raw moment of gate_arith INTDIV (integer division)");
1837 if (wires.size() != 2)
1838 throw CircuitException(
"gate_arith DIV must be binary");
1843 "Raw moment of gate_arith DIV by a constant zero");
1844 return rec_raw_moment(gc, wires[0], k, fp)
1845 / std::pow(divisor,
static_cast<double>(k));
1847 return mc_raw_moment(gc, g, k,
1848 "Raw moment of gate_arith DIV with non-constant divisor");
1853 return mc_raw_moment(gc, g, k,
1854 "Raw moment of gate_arith " +
1859 if (wires.size() == 1)
1860 return rec_raw_moment(gc, wires[0], k, fp);
1868 return mc_raw_moment(gc, g, k,
1869 "Raw moment of a gate_arith rounding or absolute value");
1873 if (
auto image = transform_image(gc, g, op))
1874 return image->rawMoment(k);
1875 return mc_raw_moment(gc, g, k,
1876 "Raw moment of a gate_arith nonlinear transform");
1878 return mc_raw_moment(gc, g, k,
1879 "Raw moment of a gate_arith PERCENTILE");
1881 throw CircuitException(
1882 "Moment: unknown gate_arith op tag: " +
1883 std::to_string(
static_cast<unsigned>(op)));
1886 const auto &wires = gc.
getWires(g);
1890 for (std::size_t i = 1; i < wires.size(); ++i) {
1893 * std::pow(v,
static_cast<double>(k));
1898 if (wires.size() != 3)
1899 throw CircuitException(
1900 "Moment: gate_mixture must have exactly three children");
1901 const double pi = mixturePi(gc, wires[0]);
1902 return pi * rec_raw_moment(gc, wires[1], k, fp)
1903 + (1.0 - pi) * rec_raw_moment(gc, wires[2], k, fp);
1906 if (
auto v = caseAnalyticRawMoment(gc, g, k))
1908 return mc_raw_moment(gc, g, k,
"Raw moment of gate type gate_case");
1910 return mc_raw_moment(gc, g, k,
1923[[noreturn]]
void raise_infeasible_event(
const GenericCircuit &gc,
gate_t root)
1925 (void)gc; (void)root;
1926 throw CircuitException(
1927 "conditioning event is infeasible (empty intersection with the "
1928 "random variable's support)");
1931double conditional_raw_moment(
const GenericCircuit &gc,
gate_t root,
1932 unsigned k,
gate_t event_root)
1934 if (k == 0)
return 1.0;
1952 const std::string what =
"Posterior raw moment";
1954 gc, root, event_root, mc_samples_or_throw(what));
1955 checkPosteriorOrThrow(post, what);
1956 return weightedRawMoment(post, k);
1958 if (
auto cf = try_truncated_closed_form(gc, root, event_root, k,
false))
1960 if (
auto cf = try_rvVsRv_conditional_moment(gc, root, event_root, k,
false))
1962 if (
auto cf = try_pivotConjunction_conditional_moment(gc, root, event_root,
1966 raise_infeasible_event(gc, root);
1967 return mc_conditional_raw_moment(
1968 gc, root, k, event_root,
1969 "Conditional raw moment of gate type " +
1973double conditional_central_moment(
const GenericCircuit &gc,
gate_t root,
1974 unsigned k,
gate_t event_root)
1976 if (k == 0)
return 1.0;
1977 if (k == 1)
return 0.0;
1983 if (m1 && m2)
return *m2 - (*m1) * (*m1);
1990 if (k == 2)
return dist->variance();
1991 const double mu = dist->mean();
1993 for (
unsigned i = 0; i <= k; ++i) {
1994 const double mu_pow = std::pow(-mu,
static_cast<double>(k - i));
1995 total +=
binomial(k, i) * mu_pow * dist->rawMoment(i);
2002 const std::string what =
"Posterior central moment";
2004 gc, root, event_root, mc_samples_or_throw(what));
2005 checkPosteriorOrThrow(post, what);
2006 const double mu = weightedRawMoment(post, 1);
2007 return weightedCentralMoment(post, k, mu);
2009 if (
auto cf = try_truncated_closed_form(gc, root, event_root, k,
true))
2011 if (
auto cf = try_rvVsRv_conditional_moment(gc, root, event_root, k,
true))
2013 if (
auto cf = try_pivotConjunction_conditional_moment(gc, root, event_root,
2017 raise_infeasible_event(gc, root);
2019 const double mu = conditional_raw_moment(gc, root, 1, event_root);
2020 return mc_conditional_central_moment(
2021 gc, root, k, mu, event_root,
2022 "Conditional central moment of gate type " +
2029 std::optional<gate_t> event_root)
2031 if (event_root.has_value())
2032 return conditional_raw_moment(gc, root, 1, *event_root);
2033 FootprintCache fp(gc);
2034 return rec_expectation(gc, root, fp);
2038 std::optional<gate_t> event_root)
2040 if (event_root.has_value())
2041 return conditional_raw_moment(gc, root, k, *event_root);
2042 FootprintCache fp(gc);
2043 return rec_raw_moment(gc, root, k, fp);
2047 std::optional<gate_t> event_root)
2049 if (event_root.has_value())
2050 return conditional_central_moment(gc, root, k, *event_root);
2051 if (k == 0)
return 1.0;
2052 if (k == 1)
return 0.0;
2053 FootprintCache fp(gc);
2054 if (k == 2)
return rec_variance(gc, root, fp);
2056 const double mu = rec_expectation(gc, root, fp);
2058 for (
unsigned i = 0; i <= k; ++i) {
2059 const double mu_pow = std::pow(-mu,
static_cast<double>(k - i));
2060 total +=
binomial(k, i) * mu_pow * rec_raw_moment(gc, root, i, fp);
2073double empirical_quantile(
const std::vector<std::optional<double>> &draws,
2077 if (xs.empty())
throw NoValueException(
"Quantile");
2079 if (p <= 0.0)
return xs.front();
2080 if (p >= 1.0)
return xs.back();
2081 const double h = p *
static_cast<double>(xs.size() - 1);
2082 const std::size_t i =
static_cast<std::size_t
>(h);
2083 if (i + 1 >= xs.size())
return xs.back();
2084 const double frac = h -
static_cast<double>(i);
2085 return xs[i] + frac * (xs[i + 1] - xs[i]);
2091std::optional<double> categorical_quantile(
const GenericCircuit &gc,
2094 const auto &wires = gc.
getWires(mix);
2095 std::vector<std::pair<double, double>> outcomes;
2096 outcomes.reserve(wires.size());
2097 for (std::size_t i = 1; i < wires.size(); ++i) {
2100 catch (
const CircuitException &) {
return std::nullopt; }
2101 outcomes.emplace_back(v, gc.
getProb(wires[i]));
2103 if (outcomes.empty())
return std::nullopt;
2104 std::sort(outcomes.begin(), outcomes.end());
2106 for (
const auto &vp : outcomes) {
2108 if (cum >= p && cum > 0.0)
return vp.first;
2110 return outcomes.back().first;
2119std::optional<double> analytic_dist_quantile(
const Distribution &dist,
2120 double p,
double lo,
double hi);
2123 double p,
double lo,
double hi)
2128std::optional<double> analytic_dist_quantile(
const Distribution &dist,
2129 double p,
double lo,
double hi)
2131 if (p <= 0.0 || p >= 1.0) {
2133 const auto sup = dist.support();
2134 return (p <= 0.0) ? std::max(sup.lo, lo) : std::min(sup.hi, hi);
2137 if (std::isfinite(lo) || std::isfinite(hi)) {
2138 const double f_lo = std::isfinite(lo) ? dist.cdf(lo) : 0.0;
2139 const double f_hi = std::isfinite(hi) ? dist.cdf(hi) : 1.0;
2140 if (std::isnan(f_lo) || std::isnan(f_hi))
return std::nullopt;
2141 const double mass = f_hi - f_lo;
2142 if (mass < 1e-12)
return std::nullopt;
2143 u = f_lo + p * mass;
2145 double q = std::numeric_limits<double>::quiet_NaN();
2146 if (
auto cf = dist.quantile(u)) q = *cf;
2148 if (std::isnan(q))
return std::nullopt;
2158 std::optional<gate_t> event_root)
2160 const double inf = std::numeric_limits<double>::infinity();
2162 if (event_root.has_value()) {
2166 if (
auto q = analytic_rv_quantile(*post, p, -inf, inf))
2170 const std::string what =
"Posterior quantile";
2172 gc, root, *event_root, mc_samples_or_throw(what));
2173 checkPosteriorOrThrow(post, what);
2174 return weightedQuantile(std::move(post), p);
2178 if (
auto q = analytic_rv_quantile(m->spec, p, m->lo, m->hi))
2182 raise_infeasible_event(gc, root);
2184 gc, root, *event_root,
2185 mc_samples_or_throw(
"Conditional quantile"));
2186 check_acceptance_or_throw(cs,
"Conditional quantile");
2187 return empirical_quantile(std::move(cs.accepted), p);
2197 if (
auto q = analytic_rv_quantile(*spec, p, -inf, inf))
2200 if (
auto q = categorical_quantile(gc, root, p))
2208 std::unique_ptr<Distribution> image =
2210 : transform_image(gc, root, op);
2212 if (
auto q = analytic_dist_quantile(*image, p, -inf, inf))
2219 return empirical_quantile(
2242 std::optional<gate_t> &event_opt)
2244 std::vector<gate_t> evidences;
2257 evidences.push_back(w[1]);
2263 std::set<gate_t> seen;
2264 std::vector<gate_t> stack{root};
2265 while (!stack.empty()) {
2268 if (!seen.insert(g).second)
continue;
2274 evidences.push_back(w[1]);
2276 stack.push_back(target);
2282 if (evidences.empty())
2284 if (event_opt.has_value())
2285 evidences.push_back(*event_opt);
2287 if (evidences.size() == 1)
2288 cond = evidences[0];
2292 for (
gate_t e : evidences)
2340 const int32 k_signed = PG_GETARG_INT32(1);
2343 provsql_error(
"agg_avg_moment_exact: k must be non-negative (got %d)",
2350 gc, root,
static_cast<unsigned>(k_signed), ok);
2353 return Float8GetDatum(r);
2354 }
catch (
const std::exception &e) {
2369 const int32 k_signed = PG_GETARG_INT32(1);
2370 const bool central = PG_GETARG_BOOL(2);
2374 provsql_error(
"rv_moment: k must be non-negative (got %d)", k_signed);
2375 const unsigned k =
static_cast<unsigned>(k_signed);
2377 gate_t root_gate, event_gate;
2381 std::optional<gate_t> event_opt;
2383 event_opt = event_gate;
2401 return Float8GetDatum(result);
2408 }
catch (
const std::exception &e) {
2434 const double p = PG_GETARG_FLOAT8(1);
2437 if (std::isnan(p) || p < 0.0 || p > 1.0)
2438 provsql_error(
"rv_quantile: p must be in [0, 1] (got %g)", p);
2440 gate_t root_gate, event_gate;
2444 std::optional<gate_t> event_opt;
2446 event_opt = event_gate;
2450 return Float8GetDatum(
2455 }
catch (
const std::exception &e) {
2485 return Float8GetDatum(std::exp(*le));
2488 "rv_evidence: provsql.rv_mc_samples is 0 (the marginal likelihood is "
2489 "estimated by Monte Carlo); set it to a positive sample budget");
2492 return Float8GetDatum(e);
2493 }
catch (
const std::exception &ex) {
Exact closed-form HAVING COUNT(*) op C probability over safe-join lineage – the recursive marginal-ve...
ComparisonOperator cmpOpFromOid(Oid op_oid, bool &ok)
Map a PostgreSQL comparison-operator OID to a ComparisonOperator.
Typed aggregation value, operator, and aggregator abstractions.
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
@ LE
Less than or equal (<=).
@ GE
Greater than or equal (>=).
bool sql_less(T a, T b)
The order PostgreSQL gives numbers: for floats, NaN equals NaN and is greater than every other value ...
Closed-form CDF resolution for trivial gate_cmp shapes.
Boolean-expression (lineage formula) semiring.
Boolean provenance circuit with support for knowledge compilation.
GenericCircuit getJointCircuit(const std::vector< pg_uuid_t > &tokens, std::vector< gate_t > &gates)
Multi-root variant of getJointCircuit.
GenericCircuit getGenericCircuit(pg_uuid_t token)
Build a GenericCircuit from the mmap store rooted at token.
Build in-memory circuits from the mmap-backed persistent store.
Generic directed-acyclic-graph circuit template and gate identifier.
gate_t
Strongly-typed gate identifier.
Rao-Blackwellised (collapsed) evaluation of a correlated COUNT / SUM and of a latent conditioned on s...
The single comparator-resolution pipeline and the single Boolean-subcircuit probability entry point,...
Exact conjugate-prior posteriors for observe-evidence circuits.
Per-family polymorphic view over a continuous gate_rv distribution (§F.1 class hierarchy).
Datum rv_quantile(PG_FUNCTION_ARGS)
SQL: rv_quantile(token uuid, p float8, prov uuid DEFAULT gate_one()) -> float8...
Datum rv_moment(PG_FUNCTION_ARGS)
Datum agg_avg_moment_exact(PG_FUNCTION_ARGS)
SQL: rv_moment(token uuid, k integer, central boolean, prov uuid DEFAULT gate_on...
Datum rv_evidence(PG_FUNCTION_ARGS)
SQL: rv_evidence(evidence uuid) -> float8.
Analytical expectation / variance / moment evaluator over RV circuits.
Monte Carlo sampling over a GenericCircuit, RV-aware.
Shared 1-D quadrature core for the pivot-conjunction and order-statistic closed forms.
Catalog of probability-evaluation methods (Strategy + registry).
Continuous random-variable helpers (distribution parsing, moments).
Support-based bound check for continuous-RV comparators.
Exception type thrown by circuit operations on invalid input.
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
gateType getGateType(gate_t g) const
Return the type of gate g.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
bool isCategoricalMixture(gate_t g) const
Test whether g is a categorical-form gate_mixture (the explicit provsql.categorical output).
void setInfos(gate_t g, unsigned info1, unsigned info2)
Set the integer annotation pair for gate g.
std::string getExtra(gate_t g) const
Return the string extra for gate g.
gate_t setGate(gate_type type) override
Allocate a new gate with type type and no UUID.
double getProb(gate_t g) const
Return the probability for gate g.
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
void liftConditionedToTarget(gate_t g, gate_t target)
Replace a gate_conditioned g by a transparent passthrough to its target child (a single-child gate_ar...
void setExtra(gate_t g, const std::string &ex)
Attach a string extra to gate g.
void setProb(gate_t g, double p)
Set the probability for gate g.
Abstract per-family continuous distribution.
The quantity asked for has a value in no world (an aggregate over a group empty in every world of pos...
The SIGINT handler installed for the lifetime of the object, the previous one restored as it ends (a ...
std::optional< std::vector< std::pair< double, std::optional< double > > > > enumerateScalarWorlds(const GenericCircuit &gc, gate_t root, std::optional< gate_t > event, unsigned max_inputs)
The possible worlds of a circuit whose only random sources are Boolean inputs: the exact alternative ...
double compute_raw_moment(const GenericCircuit &gc, gate_t root, unsigned k, std::optional< gate_t > event_root)
Compute the raw moment (or if event_root is set) for k >= 0.
std::optional< double > conjugateLogEvidence(const GenericCircuit &gc, gate_t evidence)
The exact log marginal likelihood of a conjugate-shaped evidence circuit; std::nullopt on any shape ...
double compute_quantile(const GenericCircuit &gc, gate_t root, double p, std::optional< gate_t > event_root)
Compute the p-quantile of the scalar rooted at root (of the truncated distribution if event_root is ...
double aggAvgRawMomentExact(GenericCircuit &gc, gate_t g, unsigned k, bool &ok)
Exact k-th raw moment of AVG = SUM/COUNT over independent rows, conditional on COUNT >= 1.
std::vector< std::optional< double > > monteCarloScalarSamples(const GenericCircuit &gc, gate_t root, unsigned samples)
Sample a scalar sub-circuit samples times and return the draws.
gate_t lift_conditioning(GenericCircuit &gc, gate_t root, std::optional< gate_t > &event_opt)
Lift conditioning out of a scalar arithmetic expression.
double analytical_variance(const DistributionSpec &d)
Closed-form variance Var(X) for a basic distribution.
double importanceEvidence(const GenericCircuit &gc, gate_t evidence, unsigned samples)
Marginal likelihood P(data) of evidence: the mean raw importance weight over samples prior draws.
double booleanSubcircuitProbability(GenericCircuit &gc, gate_t root, const std::string &method, const std::string &args, bool inv_free_cert, const Tolerance &tol, bool mc_fallback, std::string *actual_method_out)
Probability of the Boolean function rooted at root in gc – THE single entry point over the method por...
std::unique_ptr< Distribution > closeTransform(const char *transform, const Distribution &x)
The image distribution of transform applied to x, when a registered rule covers x's family; nullptr o...
double parseDoubleStrict(const std::string &s)
Strictly parse s as a double.
std::optional< double > centralFromRaw(unsigned k, Raw &&raw)
Central moment of order k from a raw-moment closure: .
bool eventIsProvablyInfeasible(const GenericCircuit &gc, gate_t root, std::optional< gate_t > event_root)
True iff the conditioning event is provably infeasible for a bare gate_rv root.
std::unique_ptr< Distribution > makeDistribution(const DistributionSpec &spec)
Construct the per-family Distribution for a parsed spec.
double compute_central_moment(const GenericCircuit &gc, gate_t root, unsigned k, std::optional< gate_t > event_root)
Compute the central moment (or if event_root is set).
double simpsonIntegrate(double lo, double hi, int N, F &&f)
Composite-Simpson with N panels.
ConditionalScalarSamples monteCarloConditionalScalarSamples(const GenericCircuit &gc, gate_t root, gate_t event_root, unsigned samples)
Rejection-sample root conditioned on event_root.
double binomial(unsigned n, unsigned k)
Binomial coefficient as a double (exact for the small orders the moment expansions use).
std::optional< double > 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 eq...
std::unique_ptr< Distribution > closeProductFactors(const std::vector< const Distribution * > &factors)
Fold a product of independent factors into a single distribution when a registered closure covers eve...
double evaluateBooleanProbability(const GenericCircuit &gc, gate_t boolRoot)
Probability that the Boolean subcircuit rooted at boolRoot evaluates to true under the tuple-independ...
std::optional< DistributionSpec > conjugatePosterior(const GenericCircuit &gc, gate_t target, gate_t evidence)
The exact posterior of target given evidence, as a resolved distribution spec, when the circuit match...
std::optional< DistributionSpec > parse_distribution_spec(const std::string &s)
Parse the on-disk text encoding of a gate_rv distribution.
std::optional< DistributionTemplate > parse_distribution_template(const std::string &s)
Parse the on-disk text encoding of a gate_rv distribution, keeping wired (token) parameters as wire r...
double numericQuantile(const Distribution &d, double p)
Numeric inverse CDF: monotone bisection of cdf() over the family's integration window.
double analytical_mean(const DistributionSpec &d)
Closed-form expectation E[X] for a basic distribution.
constexpr int kSimpsonPanels
Panel count shared by every composite-Simpson quadrature over a distribution's integration range: exa...
WeightedPosterior importanceSampleConditional(const GenericCircuit &gc, gate_t root, gate_t evidence, unsigned samples)
Self-normalised importance sampling of root given evidence.
std::optional< TruncatedSingleRv > matchTruncatedSingleRv(const GenericCircuit &gc, gate_t root, std::optional< gate_t > event_root)
Detect a closed-form, optionally-truncated single-RV shape.
double compute_expectation(const GenericCircuit &gc, gate_t root, std::optional< gate_t > event_root)
Compute (or if event_root is set) over the scalar sub-circuit rooted at root.
std::vector< double > definedDraws(const std::vector< std::optional< double > > &samples)
The draws of samples that have a value, in order.
double analytical_raw_moment(const DistributionSpec &d, unsigned k)
Closed-form raw moment for a basic distribution.
bool circuitHasObserve(const GenericCircuit &gc, gate_t root)
Whether the circuit reachable from root contains a gate_observe – the signal that a conditioning even...
void resolveComparators(GenericCircuit &gc, gate_t root, bool simplify, bool decompose)
Run the comparator-resolution pipeline on gc, rewriting every gate_cmp (RV comparison,...
int provsql_verbose
Verbosity level; controlled by the provsql.verbose_level GUC.
double provsql_ess_warn_fraction
Effective-sample-size warning threshold for likelihood weighting: warn when the posterior ESS falls b...
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.
#define provsql_warning(fmt,...)
Emit a ProvSQL warning message (execution continues).
#define provsql_notice(fmt,...)
Emit a ProvSQL informational notice (execution continues).
void provsql_cancel_if_interrupted(void)
Raise PG's cancel if the evaluation stopped for one; nothing otherwise.
Cancellation of the long C++ evaluation loops.
const char * gate_type_name[]
Names of gate types.
Core types, constants, and utilities shared across ProvSQL.
provsql_arith_op
Arithmetic operator tags used by gate_arith.
@ PROVSQL_ARITH_PERCENTILE
continuous percentile (order-statistic aggregate): wires are interleaved [ind_1, x_1,...
@ PROVSQL_ARITH_DIV
binary, child0 / child1
@ PROVSQL_ARITH_ASFLOAT8
unary, child0 as double precision
@ PROVSQL_ARITH_LN
unary, natural logarithm of child0 (a negative draw raises at evaluation)
@ PROVSQL_ARITH_ROUND
child0 rounded half away from zero, to child1 decimal digits where a second child is given (SQL round...
@ PROVSQL_ARITH_PLUS
n-ary, sum of children
@ PROVSQL_ARITH_POW
binary, child0 ^ child1 (real branch only: a negative base drawn with a non-integer exponent raises a...
@ PROVSQL_ARITH_ABS
unary, |child0|
@ PROVSQL_ARITH_FLOOR
unary, greatest integer <= child0
@ PROVSQL_ARITH_NEG
unary, -child0
@ PROVSQL_ARITH_INTDIV
binary, child0 / child1 truncated toward zero: SQL's division of two integers
@ PROVSQL_ARITH_MINUS
binary, child0 - child1
@ PROVSQL_ARITH_CEIL
unary, least integer >= child0
@ PROVSQL_ARITH_EXP
unary, e^child0
@ PROVSQL_ARITH_TIMES
n-ary, product of children
@ PROVSQL_ARITH_MIN
n-ary, min of children (order statistic; least / min aggregate)
@ PROVSQL_ARITH_MAX
n-ary, max of children (order statistic; greatest / max aggregate)
@ PROVSQL_ARITH_ASFLOAT4
unary, child0 as real reads it
@ gate_observe
Latent-variable observation (likelihood-weighting evidence): one wire → an observed bare gate_rv leaf...
@ gate_rv
Continuous random-variable leaf (extra encodes distribution).
@ gate_case
N-ary guarded selection over scalar (RV) children: wires are [guard_1, value_1, .....
@ gate_annotation
Transparent single-child wrapper carrying a query-level annotation in extra (inversion-free certifica...
@ gate_mobius
Signed Möbius combination: a MEASURE-only gate carrying one integer coefficient per child (in extra,...
@ gate_conditioned
Conditioning marker with two children [target, evidence]: measure-only, probability_evaluate returns ...
@ 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)
@ gate_assumed
Structural marker over a single child whose sub-circuit was computed under a Boolean-provenance assum...
string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
C++ utility functions for UUID manipulation.
Outcome of a conditional Monte Carlo sampling pass.
One parameter slot of a gate_rv, either a literal or a wire.
Parsed distribution spec (family + up to two parameters).
A gate_rv distribution spec that may carry wired (token) parameters – the parse-time counterpart of D...
Outcome of a likelihood-weighting (importance-sampling) pass.