47 double cdf_c =
cdfAt(d, c);
48 if (std::isnan(cdf_c))
return cdf_c;
63 return std::numeric_limits<double>::quiet_NaN();
65 return std::numeric_limits<double>::quiet_NaN();
98 if (std::isnan(pLess))
100 if (pLess < 0.0) pLess = 0.0;
101 if (pLess > 1.0) pLess = 1.0;
111 return std::numeric_limits<double>::quiet_NaN();
113 return std::numeric_limits<double>::quiet_NaN();
118double bareValue(
const GenericCircuit &gc,
gate_t g)
121 return std::numeric_limits<double>::quiet_NaN();
123 catch (
const CircuitException &) {
124 return std::numeric_limits<double>::quiet_NaN();
130std::optional<DistributionSpec>
131bareRv(
const GenericCircuit &gc,
gate_t g)
149double categoricalDecide(
const GenericCircuit &gc,
gate_t mix,
152 const auto &wires = gc.
getWires(mix);
154 for (std::size_t i = 1; i < wires.size(); ++i) {
157 catch (
const CircuitException &) {
158 return std::numeric_limits<double>::quiet_NaN();
169 if (hit) p += gc.
getProb(wires[i]);
186double tryAnalyticDecide(
const GenericCircuit &gc,
gate_t cmp_gate)
190 if (!ok)
return std::numeric_limits<double>::quiet_NaN();
192 const auto &wires = gc.
getWires(cmp_gate);
193 if (wires.size() != 2)
return std::numeric_limits<double>::quiet_NaN();
194 gate_t lhs = wires[0], rhs = wires[1];
197 if (
auto specX = bareRv(gc, lhs)) {
198 double c = bareValue(gc, rhs);
199 if (!std::isnan(c))
return cdfDecide(*specX, op, c);
203 if (
auto specX = bareRv(gc, rhs)) {
204 double c = bareValue(gc, lhs);
205 if (!std::isnan(c))
return cdfDecide(*specX, flipCmpOp(op), c);
213 double c = bareValue(gc, rhs);
214 if (!std::isnan(c))
return categoricalDecide(gc, lhs, op, c);
217 double c = bareValue(gc, lhs);
218 if (!std::isnan(c))
return categoricalDecide(gc, rhs, flipCmpOp(op), c);
227 auto specX = bareRv(gc, lhs);
228 auto specY = bareRv(gc, rhs);
230 return rvVsRvDecide(*specX, *specY, op);
233 return std::numeric_limits<double>::quiet_NaN();
240 unsigned resolved = 0;
245 std::vector<gate_t> cmps;
246 for (std::size_t i = 0; i < nb; ++i) {
247 auto g =
static_cast<gate_t>(i);
254 double p = tryAnalyticDecide(gc, c);
255 if (!std::isnan(p)) {
259 if (p < 0.0) p = 0.0;
260 if (p > 1.0) p = 1.0;
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 (>=).
Closed-form CDF resolution for trivial gate_cmp shapes.
gate_t
Strongly-typed gate identifier.
Per-family polymorphic view over a continuous gate_rv distribution (§F.1 class hierarchy).
Continuous random-variable helpers (distribution parsing, moments).
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.
std::vector< gate_t >::size_type getNbGates() const
Return the total number of gates in the circuit.
In-memory provenance circuit with semiring-generic evaluation.
bool isCategoricalMixture(gate_t g) const
Test whether g is a categorical-form gate_mixture (the explicit provsql.categorical output).
std::string getExtra(gate_t g) const
Return the string extra for gate g.
double getProb(gate_t g) const
Return the probability for gate g.
void resolveCmpToBernoulli(gate_t g, double p)
Replace a gate_cmp by a constant Boolean leaf (gate_one for p == 1, gate_zero for p == 0) or by a Ber...
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
double comparatorPairLess(const Distribution &X, const Distribution &Y)
for two independent RVs.
double parseDoubleStrict(const std::string &s)
Strictly parse s as a double.
std::unique_ptr< Distribution > makeDistribution(const DistributionSpec &spec)
Construct the per-family Distribution for a parsed spec.
double pdfAt(const DistributionSpec &d, double c)
Closed-form probability density for a basic distribution.
std::optional< DistributionSpec > parse_distribution_spec(const std::string &s)
Parse the on-disk text encoding of a gate_rv distribution.
double cdfAt(const DistributionSpec &d, double c)
Closed-form CDF for a basic continuous distribution.
unsigned runAnalyticEvaluator(GenericCircuit &gc)
Run the closed-form CDF resolution pass over gc.
Core types, constants, and utilities shared across ProvSQL.
@ gate_rv
Continuous random-variable leaf (extra encodes distribution).
Parsed distribution spec (family + up to two parameters).