19#ifndef PROVSQL_DISTRIBUTION_H
20#define PROVSQL_DISTRIBUTION_H
61 virtual double p1()
const = 0;
62 virtual double p2()
const = 0;
67 virtual double mean()
const = 0;
106 virtual double pdf(
double x)
const = 0;
107 virtual double cdf(
double x)
const = 0;
124 virtual std::pair<double, double>
plotRange(
double trunc_lo,
125 double trunc_hi)
const = 0;
128 virtual double sample(std::mt19937_64 &rng)
const = 0;
141 virtual std::optional<double>
quantile(
double p)
const {
158 (void) lo; (void) hi; (void) k;
174 std::mt19937_64 &rng,
double lo,
double hi,
unsigned n)
const {
175 (void) rng; (void) lo; (void) hi; (void) n;
194 (void) n; (void) isMax;
209 virtual std::unique_ptr<Distribution>
affine(
double a,
double b)
const = 0;
212 std::unique_ptr<Distribution>
scale(
double c)
const {
217 std::unique_ptr<Distribution>
negate()
const {
238 virtual std::optional<double>
asDirac()
const {
return std::nullopt; }
249std::unique_ptr<Distribution>
makeDistribution(
const DistributionSpec &spec);
266 std::unique_ptr<Distribution> (*)(
double p1,
double p2);
432 std::unique_ptr<Distribution> (*)(
const std::vector<ClosureTerm> &terms);
454 const std::vector<const Distribution *> &factors);
474 const std::vector<const Distribution *> &factors);
504std::unique_ptr<Distribution>
closeTransform(
const char *transform,
505 const Distribution &x);
519 const std::vector<ClosureTerm> &terms);
578 const char *prior_family,
587 const std::string &prior_family);
Continuous random-variable helpers (distribution parsing, moments).
Abstract per-family continuous distribution.
virtual double p2() const =0
virtual DistSupport support() const =0
Natural support interval of X.
virtual std::optional< double > iidOrderStatMean(std::size_t n, bool isMax) const
Closed-form mean of the max / min of n i.i.d.
virtual std::optional< double > asDirac() const
The point-mass value when the parameters make the distribution degenerate (a Dirac),...
virtual double rawMoment(unsigned k) const =0
E[X^k].
virtual std::optional< std::vector< double > > sampleTruncated(std::mt19937_64 &rng, double lo, double hi, unsigned n) const
Draw n rejection-free samples of X conditioned on lo < X < hi (±infinity for a semi-infinite side).
virtual double sample(std::mt19937_64 &rng) const =0
Draw one sample using the shared MC generator.
virtual double variance() const =0
Var(X).
virtual std::unique_ptr< Distribution > affine(double a, double b) const =0
Closed-form location-scale transform a·X + b within the family.
virtual std::optional< double > quantile(double p) const
Inverse CDF for .
virtual double mean() const =0
E[X].
virtual bool isDiscrete() const
Whether the family is discrete (integer-valued), so a point event X = c carries positive mass.
virtual bool integrationRange(double &lo, double &hi) const =0
Finite window [lo, hi] covering essentially all of X's mass, for numerical quadrature.
virtual double pdf(double x) const =0
f(x); NaN if the family declines
virtual const DistributionFamily & family() const =0
The family's interned registry descriptor.
virtual std::string serialise() const =0
The on-disk gate_rv extra text encoding (e.g.
std::unique_ptr< Distribution > negate() const
Closed-form -X (affine with coefficient -1).
virtual double p1() const =0
virtual double cdf(double x) const =0
F(x); NaN if the family declines.
virtual std::pair< double, double > plotRange(double trunc_lo, double trunc_hi) const =0
Plot x-window given optional truncation bounds (±infinity means "unbounded on that side"); used by th...
virtual bool meanIsAffine() const
Whether mean() is an affine function of the raw parameters (p1, p2) – i.e.
std::unique_ptr< Distribution > scale(double c) const
Closed-form c·X (affine with no offset).
virtual std::optional< double > truncatedRawMoment(double lo, double hi, unsigned k) const
Closed-form raw moment of the distribution truncated to [lo, hi] (±infinity for a semi-infinite side...
virtual ~Distribution()=default
double comparatorPairLess(const Distribution &X, const Distribution &Y)
for two independent RVs.
std::unique_ptr< Distribution >(*)( const Distribution &x) TransformRule
A closed-form image of a monotone transform of one family (e.g.
const ConjugateRule * lookupConjugateRule(const std::string &likelihood_family, int wired_param, const std::string &prior_family)
Look up the conjugate rule for (likelihood family, wired parameter position, prior family); nullptr o...
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...
std::unique_ptr< Distribution >(*)(const std::vector< ClosureTerm > &terms) ClosureRule
A family sum-closure fold.
std::unique_ptr< Distribution > makeDistribution(const DistributionSpec &spec)
Construct the per-family Distribution for a parsed spec.
void registerProductRule(const char *x, const char *y, ProductRule rule)
Register the product-closure rule for a family pair (name-token keyed, like the sum-closure rules).
std::unique_ptr< Distribution > closePlusTerms(const std::vector< ClosureTerm > &terms)
Fold PLUS(terms) into a single distribution when a registered closure covers every family in the sum.
void registerComparatorRule(const char *x, const char *y, ComparatorRule rule)
Register the closed form for a family pair.
std::unique_ptr< Distribution >(*)(double p1, double p2) DistributionFactory
Construct a family instance from its (up to) two parameters.
double(*)(const Distribution &X, const Distribution &Y) ComparatorRule
A pairwise closed form for , X and Y independent.
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...
std::vector< const DistributionFamily * > listDistributionFamilies()
Every registered family, sorted by name token.
void registerDistributionFamily(const DistributionFamily &descriptor)
Register a family; called by the registrar at static init.
std::unique_ptr< Distribution >(*)( const std::vector< const Distribution * > &factors) ProductRule
A family product-closure fold: the distribution of the product of the (independent) factors,...
double numericQuantile(const Distribution &d, double p)
Numeric inverse CDF: monotone bisection of cdf() over the family's integration window.
const DistributionFamily * lookupDistributionFamily(const std::string &name)
Look up a family by its on-disk name token.
void registerTransformRule(const char *transform, const char *family, TransformRule rule)
Register the image rule for transform ("ln" / "exp" / ...; the evaluator maps its arith opcodes to th...
void registerClosureRule(const char *x, const char *y, ClosureRule rule)
Register the sum-closure rule for a family pair (name-token keyed, like the comparator rules).
void registerConjugateRule(const char *likelihood_family, int wired_param, const char *prior_family, const ConjugateRule &rule)
Register the conjugate update for observations of likelihood_family whose parameter wired_param (0 = ...
ClosureRuleRegistrar(const char *x, const char *y, ClosureRule rule)
One wire of a PLUS under closure: a·Z + b for a base RV Z (dist non-null), or a pure additive constan...
const Distribution * dist
ComparatorRuleRegistrar(const char *x, const char *y, ComparatorRule rule)
ConjugateRuleRegistrar(const char *likelihood_family, int wired_param, const char *prior_family, const ConjugateRule &rule)
The conjugate update for one observation of a given likelihood family against the running posterior (...
double(* log_predictive)(double q1, double q2, const DistributionTemplate &lik, double datum)
Log predictive density of the datum under the running posterior BEFORE the update; summed over the f...
bool(* update)(double &q1, double &q2, const DistributionTemplate &lik, double datum)
Update the running posterior parameters q1 / q2 (a distribution of the prior's family) by one observa...
A closed support interval [lo, hi] (±infinity for unbounded).
DistributionFamilyRegistrar(const DistributionFamily &descriptor)
A registered family's descriptor: its complete identity.
DistributionFactory factory
unsigned nparams
1 or 2 (a 1-parameter family leaves p2 = 0)
const char * param_names[2]
A gate_rv distribution spec that may carry wired (token) parameters – the parse-time counterpart of D...
ProductRuleRegistrar(const char *x, const char *y, ProductRule rule)