ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
AnalyticEvaluator.h
Go to the documentation of this file.
1/**
2 * @file AnalyticEvaluator.h
3 * @brief Closed-form CDF resolution for trivial @c gate_cmp shapes.
4 *
5 * For comparators that reduce to either:
6 * - @c X @c cmp @c c with @c X a bare @c gate_rv leaf and @c c a
7 * bare @c gate_value constant, or
8 * - @c X @c cmp @c Y with @c X and @c Y two distinct independent
9 * normal @c gate_rv leaves,
10 *
11 * the comparator's probability has a closed form via the
12 * distribution's CDF. Implementations live inline in the @c .cpp
13 * (normal: @c std::erf; uniform: arithmetic; exponential:
14 * @c std::expm1) so the pass has no external math dependency. The
15 * difference of two independent normals is itself normal, with the
16 * obvious mean and variance. Replacing the @c gate_cmp by a
17 * Bernoulli @c gate_input carrying the analytical probability lets
18 * the surrounding circuit be evaluated exactly by every downstream
19 * probability method – no MC noise.
20 *
21 * Unlike @c RangeCheck, this pass is probability-specific:
22 * fractional probabilities are meaningful only on the probability
23 * path. It therefore runs in @c probability_evaluate.cpp, not
24 * inside @c getGenericCircuit (which is shared with semiring
25 * evaluation, view_circuit, PROV export, etc.).
26 *
27 * Compositions involving @c gate_arith (e.g. <tt>aX + bY > c</tt>
28 * for independent normals) are out of scope here; folding them
29 * into a single distribution requires the family-closure simplifier
30 * planned for the hybrid evaluator.
31 */
32#ifndef PROVSQL_ANALYTIC_EVALUATOR_H
33#define PROVSQL_ANALYTIC_EVALUATOR_H
34
35#include "GenericCircuit.h"
36#include "RandomVariable.h" // DistributionSpec
37
38namespace provsql {
39
40/**
41 * @brief Closed-form CDF @f$F_X(c) = P(X \le c)@f$ for a basic
42 * continuous distribution.
43 *
44 * Returns the cumulative distribution at @p c for the distribution
45 * @p d. Used internally by @c AnalyticEvaluator's @c gate_cmp
46 * resolution and by the @c HybridEvaluator decomposer's
47 * monotone-shared-scalar fast path to compute interval probabilities
48 * analytically (no MC noise) when the shared scalar is a bare
49 * @c gate_rv. Returns @c NaN when @p d carries a parameter shape
50 * the CDF doesn't cover (e.g. non-integer Erlang shape, which would
51 * require the regularised lower incomplete gamma function).
52 *
53 * - Normal(μ, σ): @f$\Phi((c - \mu) / \sigma)@f$ via @c std::erf.
54 * - Uniform(a, b): piecewise linear; 0 for @c c<=a, 1 for
55 * @c c>=b, @c (c - a) / (b - a) otherwise.
56 * - Exponential(λ): @c 1 - exp(-λc) for @c c>0; 0 for @c c<=0.
57 * - Erlang(k, λ) (integer @c k≥1): finite-sum form
58 * @f$1 - e^{-\lambda c} \sum_{n=0}^{k-1}
59 * (\lambda c)^n / n!@f$ for @c c>0.
60 */
61double cdfAt(const DistributionSpec &d, double c);
62
63/**
64 * @brief Closed-form probability density @f$f(c)@f$ for a basic
65 * distribution.
66 *
67 * Used by @c rv_analytical_curves to ship a sampled curve to clients
68 * (Studio's Distribution profile overlay). Returns @c 0 outside the
69 * natural support and @c NaN for parameter shapes the analytical
70 * form doesn't cover (e.g. non-integer Erlang shape).
71 *
72 * - Normal(μ, σ): @f$\frac{1}{\sigma\sqrt{2\pi}}
73 * \exp(-(c-\mu)^2 / (2\sigma^2))@f$.
74 * - Uniform(a, b): @c 1/(b-a) for @c a<=c<=b, @c 0 otherwise.
75 * - Exponential(λ): @c λ·exp(-λc) for @c c>=0, @c 0 otherwise.
76 * - Erlang(k, λ) (integer @c k>=1):
77 * @f$\frac{\lambda^k c^{k-1} e^{-\lambda c}}{(k-1)!}@f$
78 * for @c c>=0, @c 0 otherwise.
79 */
80double pdfAt(const DistributionSpec &d, double c);
81
82/**
83 * @brief Run the closed-form CDF resolution pass over @p gc.
84 *
85 * For every @c gate_cmp in the circuit whose two sides match one of
86 * the supported shapes (see the header docstring), computes the
87 * comparator's probability analytically and replaces the cmp by a
88 * Bernoulli @c gate_input via @c GenericCircuit::resolveCmpToBernoulli.
89 *
90 * @param gc Circuit to mutate in place.
91 * @return Number of comparators resolved by this pass.
92 */
93unsigned runAnalyticEvaluator(GenericCircuit &gc);
94
95} // namespace provsql
96
97#endif // PROVSQL_ANALYTIC_EVALUATOR_H
Semiring-agnostic in-memory provenance circuit.
Continuous random-variable helpers (distribution parsing, moments).
double pdfAt(const DistributionSpec &d, double c)
Closed-form probability density for a basic 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.
Parsed distribution spec (kind + up to two parameters).