Membership Circuits (MCs) ========================= A Membership Circuit turns a smooth and decomposable probabilistic circuit into a hypothesis test. For every data row it tests the membership hypothesis "this row is a sample of the circuit" and returns a p-value. A small p-value is evidence against the hypothesis, for example because the row is an anomaly or comes from another distribution. Reference --------- Membership Circuits are described in: - B\. Wittelsbach, A. Skryagin, J. Seng, M. Willig and K. Kersting (2026), `Membership Circuits: Tractable Membership Testing via Probabilistic Circuits `_, ICML 2026 Workshop on Hypothesis Testing. The combination rules are described in: - R\. A. Fisher (1932), *Statistical Methods for Research Workers*, 4th edition (Fisher's method). - R\. J. Simes (1986), `An improved Bonferroni procedure for multiple tests of significance `_, Biometrika 73(3). - V\. Vovk and R. Wang (2020), `Combining p-values via averaging `_, Biometrika 107(4) (arithmetic and harmonic mean under arbitrary dependence, multiplier of the harmonic mean). - V\. Vovk, B. Wang and R. Wang (2022), `Admissible ways of merging p-values under arbitrary dependence `_, Annals of Statistics 50(1) (Harmonic* and grid harmonic rule, multiplier 2 for two p-values). Overview -------- The p-value of a row is computed in one bottom-up pass over the circuit: - A **leaf** turns its observed value into a univariate p-value from its cumulative distribution function (left, right or two-sided). - A **product node** merges the p-values of its children with a *product rule*. The children of a product node are independent under the circuit, so rules for independent p-values (Fisher, Simes) are valid here, as are rules that are valid under arbitrary dependence. - A **sum node** merges the p-values of its children with a *sum rule*. A row comes from one child only, and only the p-value of that child is known to be valid. The sum rules are valid in this situation. - Every inner node clips its result to :math:`[0, 1]`. The p-value of the root is the result. The result is a valid p-value: under the membership hypothesis the probability that it falls below a level :math:`\varepsilon` is at most :math:`\varepsilon`. Most rules are conservative, some more than others; the choice of the rules decides the power of the test. Combination Rules ----------------- A :class:`spflow.zoo.mc.MembershipTestConfig` holds one sum rule and one product rule. Both are required; there are no defaults. .. list-table:: :header-rows: 1 :widths: 28 14 58 * - Rule - Node type - Formula and validity * - :class:`spflow.zoo.mc.ArithmeticMeanK` - sum - :math:`K \sum_i w_i p_i`; valid at sum nodes. * - :class:`spflow.zoo.mc.ArithmeticMeanSqrtW` - sum - :math:`S \sum_i \sqrt{w_i}\, p_i` with :math:`S = \sum_j \sqrt{w_j}`; valid at sum nodes, with the smallest valid constant :math:`S^2 \le K`. * - :class:`spflow.zoo.mc.WeightedArithmeticMean` - sum - :math:`c \sum_i a_i p_i` with coefficients from a callable; valid if :math:`c \ge \sum_i w_i / a_i`, which is checked. * - :class:`spflow.zoo.mc.MaxP` - sum, product - :math:`\max_i p_i`; valid at every node, very conservative. * - :class:`spflow.zoo.mc.Fisher` - product - :math:`-2 \sum_i \log p_i` against :math:`\chi^2_{2k}`; valid for independent children, exact if their p-values are uniform. * - :class:`spflow.zoo.mc.Simes` - product - :math:`\min_i (k / i)\, p_{(i)}`; valid for independent children. * - :class:`spflow.zoo.mc.Bonferroni` - product - :math:`k\, p_{(1)}`; valid under any dependence. * - :class:`spflow.zoo.mc.HarmonicMean` - product - :math:`a_k` times the harmonic mean; valid under any dependence. * - :class:`spflow.zoo.mc.HarmonicMeanStar` - product - Harmonic* rule, never larger than the harmonic mean rule; valid under any dependence. Under evaluation. * - :class:`spflow.zoo.mc.GridHarmonic` - product - Grid harmonic rule; valid under any dependence. Under evaluation, and more expensive than the other rules. Missing Values -------------- NaN marks a missing value. A row with missing values is tested on its observed variables only: a node whose variables are all missing is left out, a product node merges its observed children, and a row without any observed value gets the p-value 1. The result equals the p-value under the circuit in which the missing variables are marginalized out. It is a valid test of the observed part of the row only if the missingness does not depend on the values of the row. Discrete Leaves --------------- The p-value of a discrete leaf has three modes (``discrete_mode``): - ``"exact"``: the tail probability includes the probability of the observed value. Valid and conservative. - ``"midp"``: half of the probability of the observed value is counted. The mid-p value is **not** a valid p-value in general. - ``"randomized"``: a uniform random fraction of the probability of the observed value is counted. The p-value of the leaf is then exactly uniform, at the price of a decision that depends on a random number. Pass ``seed`` for reproducible results. Goodness of Fit --------------- The p-values of held-out rows also tell whether the circuit fits the data: if the rows are samples of the circuit, small p-values are not more frequent than under the uniform distribution. :func:`spflow.zoo.mc.binomial_gof` and :func:`spflow.zoo.mc.ks_gof` test this, and :func:`spflow.zoo.mc.gof_report` collects both. A small result means misfit. A large result is no proof of fit, because a conservative combination rule can hide a misfit. Rows that were used to fit the circuit give optimistic p-values and should not be used. Supported Modules ----------------- The pass supports ``Sum``, ``Product``, ``Cat``, ``ElementwiseSum``, ``ElementwiseProduct``, ``OuterProduct``, ``Factorize``, ``RepetitionMixingLayer``, the split modules as the input of ``ElementwiseProduct`` or ``OuterProduct``, and :class:`spflow.zoo.rat.RatSPN`. The supported leaf families are Normal, LogNormal, Exponential, Gamma, Laplace, Uniform, Histogram, Bernoulli, Categorical, Poisson, Geometric, Binomial and Hypergeometric. Every other module or leaf raises an ``UnsupportedOperationError``. Minimal Example --------------- .. code-block:: python import torch from spflow.meta import Scope from spflow.modules.leaves import Normal from spflow.modules.products import Product from spflow.modules.sums import Sum from spflow.zoo.mc import ( ArithmeticMeanSqrtW, HarmonicMean, MembershipCircuit, MembershipTestConfig, gof_report, ) # A mixture of two product nodes over two variables. def normal(variable, loc, scale): return Normal(scope=Scope([variable]), loc=torch.tensor([[[loc]]]), scale=torch.tensor([[[scale]]])) circuit = Sum( inputs=[ Product(inputs=[normal(0, 0.0, 1.0), normal(1, 1.0, 2.0)]), Product(inputs=[normal(0, 4.0, 1.0), normal(1, -2.0, 1.0)]), ], weights=[0.3, 0.7], ) config = MembershipTestConfig(sum_rule=ArithmeticMeanSqrtW(), product_rule=HarmonicMean()) mc = MembershipCircuit(circuit, config) x = torch.tensor( [ [0.1, 1.5], # typical for the first component [9.0, 9.0], # far away from both components [4.2, float("nan")], # second variable missing ] ) p = mc(x) # one p-value per row, shape (3,) # Goodness of fit on rows that were not used for training (here: samples of the circuit). held_out = circuit.sample(num_samples=2000) report = gof_report(mc(held_out)) Calling ``mc(x)`` returns p-values, not log-likelihoods; the log-likelihood of the wrapped circuit is available as ``mc.log_likelihood(x)``. The function :func:`spflow.zoo.mc.membership_pvalues` computes the same p-values without a wrapper. API Reference ------------- Membership Circuit ~~~~~~~~~~~~~~~~~~ .. autoclass:: spflow.zoo.mc.MembershipCircuit :members: :show-inheritance: .. autofunction:: spflow.zoo.mc.membership_pvalues .. autoclass:: spflow.zoo.mc.MembershipTestConfig :members: Sum Rules ~~~~~~~~~ .. autoclass:: spflow.zoo.mc.ArithmeticMeanK :show-inheritance: .. autoclass:: spflow.zoo.mc.ArithmeticMeanSqrtW :show-inheritance: .. autoclass:: spflow.zoo.mc.WeightedArithmeticMean :members: :show-inheritance: .. autofunction:: spflow.zoo.mc.am_k_coefficients .. autofunction:: spflow.zoo.mc.am_sqrtw_coefficients .. autoclass:: spflow.zoo.mc.SumNodeContext .. autoclass:: spflow.zoo.mc.MaxP :members: :show-inheritance: Product Rules ~~~~~~~~~~~~~ All product rules are :class:`spflow.zoo.mc.ProductCombination` rules and follow its conventions. .. autoclass:: spflow.zoo.mc.Fisher :no-show-inheritance: .. autoclass:: spflow.zoo.mc.Simes :no-show-inheritance: .. autoclass:: spflow.zoo.mc.Bonferroni :no-show-inheritance: .. autoclass:: spflow.zoo.mc.HarmonicMean :no-show-inheritance: .. autoclass:: spflow.zoo.mc.HarmonicMeanStar :no-show-inheritance: .. autoclass:: spflow.zoo.mc.GridHarmonic :no-show-inheritance: Rule Interfaces ~~~~~~~~~~~~~~~ .. autoclass:: spflow.zoo.mc.PValueCombination :members: .. autoclass:: spflow.zoo.mc.SumCombination :show-inheritance: .. autoclass:: spflow.zoo.mc.ProductCombination :show-inheritance: Leaf P-Values ~~~~~~~~~~~~~ .. autofunction:: spflow.zoo.mc.leaf_pvalues Goodness of Fit ~~~~~~~~~~~~~~~ .. autofunction:: spflow.zoo.mc.binomial_gof .. autofunction:: spflow.zoo.mc.ks_gof .. autofunction:: spflow.zoo.mc.gof_report