ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
RvCovariance.cpp File Reference

Single-pass covariance / correlation readouts over RV circuits. More...

#include "CircuitFromMMap.h"
#include "Expectation.h"
#include "MonteCarloSampler.h"
#include "provsql_utils_cpp.h"
#include "postgres.h"
#include "fmgr.h"
#include "utils/uuid.h"
#include "provsql_utils.h"
#include "provsql_error.h"
#include <cmath>
#include <limits>
#include <optional>
#include <set>
#include <string>
#include <vector>
Include dependency graph for RvCovariance.cpp:

Go to the source code of this file.

Namespaces

namespace  provsql

Functions

double provsql::computeCovariance (GenericCircuit &gc, gate_t x, gate_t y, std::optional< gate_t > event)
 \(Cov(X, Y)\) (or \(Cov(X, Y \mid E)\)) over a joint circuit.
std::optional< double > provsql::computeCorrelation (GenericCircuit &gc, gate_t x, gate_t y, std::optional< gate_t > event)
 Pearson \(\rho(X, Y) = Cov(X, Y) / (\sigma_X \sigma_Y)\) (conditioned on event when set).
Datum rv_covariance (PG_FUNCTION_ARGS)
 SQL: rv_covariance(x uuid, y uuid, prov uuid) -> float8.
Datum rv_correlation (PG_FUNCTION_ARGS)
 SQL: rv_correlation(x uuid, y uuid, prov uuid) -> float8.

Detailed Description

Single-pass covariance / correlation readouts over RV circuits.

Backs the SQL covariance(x, y[, prov]) and correlation(x, y[, prov]) readouts. The definitional decomposition \(Cov(X, Y) = E[XY] - E[X]\,E[Y]\) is kept for the exact cases (every factor decomposes analytically, so the subtraction is closed-form), but the Monte Carlo fallback deliberately does NOT go through it: estimating E[XY] and the two means from three independent sampling passes and subtracting puts the estimator's noise on the scale of \(E[X]\,E[Y]\) – the product of the means – rather than of the covariance itself, a catastrophic cancellation when the means dominate the coupling. Instead a single coupled pass draws \((x_i, y_i)\) pairs from the joint circuit (shared stochastic leaves produce one draw per iteration that both roots observe) and returns the plain sample covariance \(\tfrac1n \sum (x_i - \bar x)(y_i - \bar y)\), whose noise scales with \(\sqrt{(\sigma_X^2 \sigma_Y^2 + Cov^2)/n}\).

correlation reads \(Cov\), \(\sigma_X\) and \(\sigma_Y\) off the SAME pass, instead of stacking five independent MC estimates.

Definition in file RvCovariance.cpp.

Function Documentation

◆ rv_correlation()

Datum rv_correlation ( PG_FUNCTION_ARGS )

SQL: rv_correlation(x uuid, y uuid, prov uuid) -> float8.

Returns SQL NULL for a degenerate (zero-variance) argument, matching the NULLIF convention of the former SQL-level definition.

Definition at line 357 of file RvCovariance.cpp.

Here is the call graph for this function:

◆ rv_covariance()

Datum rv_covariance ( PG_FUNCTION_ARGS )

SQL: rv_covariance(x uuid, y uuid, prov uuid) -> float8.

Definition at line 326 of file RvCovariance.cpp.

Here is the call graph for this function: