Documentation

DescriptiveComplexity.Examples.ProbabilisticQueries

Worked example: query evaluation over probabilistic databases #

This file is a tutorial, read top to bottom like DescriptiveComplexity.Examples.ConjunctiveQueries, and the first one about a counting problem. The domain is probabilistic databases: a database whose facts are present independently, each with its own probability, and the problem of computing the probability that a fixed Boolean query holds (probabilistic query evaluation). Its data complexity is #P-hard already for the conjunctive query

h₀ = ∃ x y, R(x) ∧ S(x, y) ∧ T(y)

(Dalvi and Suciu 2012, Proposition 5.2), which is the first result formalized here; the second is that on the same instances the query

∃ x y, R(x) ∧ S(x, y)

is easy: the numerator of its probability is in FP, the polynomial-time functions. The two queries are the smallest pair on the two sides of the dichotomy of that paper, the second being hierarchical and the first not; the dichotomy itself is not formalized, and nothing here is generic in the query.

The model #

A fact is certain, or it is uncertain and present with some probability, independently of the others. The file works in two stages.

The steps #

  1. Vocabulary. The schema FirstOrder.Language.rst of the query; an instance is a structure over two copies of it, certain facts and uncertain ones. This step, and the next two, are generic in the schema and are library helpers, in DescriptiveComplexity.Counting.PossibleWorlds.
  2. Semantics. A possible world keeps the certain facts and some of the uncertain ones (DescriptiveComplexity.IsWorld), and DescriptiveComplexity.PossibleWorlds φ counts the worlds satisfying φ.
  3. Membership. A world is one relation per symbol of the schema, so the problem counts the witnesses of a first-order kernel: it is in #P for every first-order query (DescriptiveComplexity.possibleWorlds_mem_sharpP). Nothing is specific to h₀ up to here.
  4. The query, DescriptiveComplexity.h0, and what it says of a world (DescriptiveComplexity.realize_h0).
  5. Hardness. From #PP2DNF (DescriptiveComplexity.SharpPP2DNF), the models of ⋁ (x ∧ y) over the edges of a bipartite graph. The instance has the edges as certain S-facts, the left vertices as uncertain R-facts and the right vertices as uncertain T-facts (DescriptiveComplexity.h0Interp); a world is then a set of vertices, and h₀ holds in it exactly when some edge has both ends chosen (DescriptiveComplexity.possibleWorlds_h0_eq). The reduction is parsimonious, one-dimensional and quantifier-free.
  6. The theorem: counting the worlds of h₀ is one-call #P-complete (DescriptiveComplexity.possibleWorlds_h0_sharpP_oneCallComplete).
  7. Probabilities in the instance: the numerator of the probability of h₀ is one-call #P-complete (DescriptiveComplexity.weightedWorlds_h0_sharpP_oneCallComplete), by the ordered parsimonious reduction from the uniform case (DescriptiveComplexity.possibleWorlds_ordered_parsimonious_weightedWorlds).
  8. A concrete database. DescriptiveComplexity.ProbDb is a probabilistic database in plain terms: n constants and, for each possible fact, whether it is absent, certain, or uncertain with two weights. Its weighted count DescriptiveComplexity.ProbDb.count and its total weight DescriptiveComplexity.ProbDb.total are computed (the #guards at the end run them). DescriptiveComplexity.probDbEncoding encodes a database as a weighted instance, with the size bounds of DescriptiveComplexity.Encoding discharged, and it is faithful (DescriptiveComplexity.probDbEncoding_countFaithful, DescriptiveComplexity.probDbEncoding_countFaithful_total): the abstract counting problems return, on the encoded instance, the two numbers computed from the database. So the probability of h₀ over a database is count / total (DescriptiveComplexity.probDb_worldProb_eq).

What kind of hardness #

#PP2DNF is itself complete under one-call reductions only, so the statement is one-call completeness, not plain completeness: every problem of #P is answered by one question about the probability of h₀, followed by arithmetic. A formula of this kind always has a world in which it holds as soon as the graph has an edge, so no parsimonious reduction from #SAT can exist unless P = NP. Whether the problem is complete under subtractive reductions is not known here.

  1. The decoder. DescriptiveComplexity.probDbDecode reads a database back from any presented weighted instance whose position order is linear, by a computation (it runs, too), and the database has the weighted count of the instance (DescriptiveComplexity.probDbDecoding, a DescriptiveComplexity.CountDecoding). So the hardness of the abstract problem is hardness on instances that are databases.

The easy query #

Steps 10 to 15 are the second result, on the same weighted instances.

  1. The query, DescriptiveComplexity.rs.
  2. The weights. Every fact is a Boolean variable with a weight of presence and a weight of absence (DescriptiveComplexity.Counting.WeightedFacts); the facts of the schema are the R-facts, the S-facts and the T-facts (DescriptiveComplexity.factEquiv).
  3. Failing is a product. The query fails in a world when no x has both R(x) and some S(x, y). Given which R-facts are present, this constrains each S-fact separately; summing over the R-facts then factorizes too. The weight of the worlds in which the query fails is ∏x. F(x), times the total weight of the T-facts (DescriptiveComplexity.SafeQ.weightedCount_fail), F(x) being the weight of failing at x.
  4. Succeeding, without a subtraction. The weight of the worlds in which the query holds is the total weight minus that product. A quantitative term has no subtraction; the first success identity (DescriptiveComplexity.prod_add_eq_prod_add_sum) removes it, by sorting the worlds by the first x at which the query succeeds – and, inside the weight of succeeding at x, by the first y (DescriptiveComplexity.SafeQ.weightedWorlds_rs_eq).
  5. The term. The closed form is a term of quantitative first-order logic over the instance, its leaves being the weights, read in binary (DescriptiveComplexity.SafeQ.rsT). The order it sorts by is the order of the definition, on which the value does not depend.
  6. The theorem: DescriptiveComplexity.weightedWorlds_rs_mem_FP.

Steps 12 and 13 use the library's lemmas on independent Boolean variables (DescriptiveComplexity.Counting.Independence), and step 14 its helpers for writing terms (DescriptiveComplexity.Counting.QuantitativeBinders, DescriptiveComplexity.Counting.WeightedTerms).

The concrete step, and what it relies on #

The two other tutorials open with the concrete instances; here they come last (steps 8 and 9), the hardness result needing none of it. The encoder and the decoder are two corollaries of one theorem: an instance that matches a database (DescriptiveComplexity.ProbDb.Matches) has its counts (DescriptiveComplexity.ProbDb.Matches.count_eq). The step is short because its two generic ingredients are library lemmas: the binary digits written by an encoder decode to the number they came from (DescriptiveComplexity.binNum_fin_of_testBit, in DescriptiveComplexity.Numbers.BinEnum), and faithfulness for a counting problem is DescriptiveComplexity.Encoding.CountFaithful. What remains is specific to the database format: reading a status off each fact (DescriptiveComplexity.factWeight_of_status) and matching the abstract worlds and facts with the three tables of a concrete world (DescriptiveComplexity.worldEquiv, DescriptiveComplexity.factEquiv).

One restriction of the format is deliberate: the weights are written on n bits, n being the number of constants, since the constants double as bit positions. A database with longer weights is padded with unused constants.

The relation symbols of the language.

Instances For
    Dependency graph
    Dependency graph
    Dependency graph
    Dependency graph
    Dependency graph

    The schema of the example: two unary relations and a binary one.

    Equations
    Instances For
      Dependency graph
      Dependency graph
      Dependency graph

      Steps 1 and 4: the schema, and the query #

      Dependency graph

      The query h₀ = ∃ x y, R(x) ∧ S(x, y) ∧ T(y).

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        Dependency graph
        Dependency graph

        Step 5: hardness, from #PP2DNF #

        The worlds of a bipartite graph. Let the instance B hold a bipartite graph A this way: S is certain and is the set of edges from the left side to the right side, R is uncertain and is the left side, T is uncertain and is the right side. Then the worlds satisfying h₀ are as many as the models of the partitioned positive 2-DNF formula of the graph: a world is a set of left vertices and a set of right vertices, and h₀ says that some edge has both its ends chosen.

        Dependency graph

        The instance of a bipartite graph: the edges from left to right are the certain S-facts, the left vertices the uncertain R-facts, and the right vertices the uncertain T-facts. One-dimensional, single-tagged and quantifier-free.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          Dependency graph

          #PP2DNF reduces parsimoniously to counting the worlds of h₀ (Dalvi and Suciu 2012, Proposition 5.2).

          Equations
          Instances For
            Dependency graph

            Step 6: the theorems #

            Dependency graph

            Counting the worlds of h₀ is one-call #P-complete: it is in #P, like the count of the worlds of any first-order query, and every problem of #P reduces to it with one call. Registered in the Lax archive as Lax794877.H0Complete.possibleWorlds_h0_sharpP_oneCallComplete.

            Dependency graph

            Step 7: probabilities in the instance #

            The weighted worlds of h₀ are one-call #P-hard to count, already when every weight is 1, i.e., at uniform probability 1/2.

            Dependency graph

            The numerator of the probability of h₀ is one-call #P-complete: counting the weighted worlds of h₀ is in #P, like those of any first-order query, and every problem of #P reduces to it with one call. Registered in the Lax archive as Lax794877.H0Complete.weightedWorlds_h0_sharpP_oneCallComplete.

            Dependency graph

            Step 8: a concrete database, and its encoding #

            What a database says of a fact: it is absent, it is certain, or it is uncertain with weights a and c, i.e., present with probability a / (a + c).

            • absent : FactStatus

              The fact is not in the database.

            • certain : FactStatus

              The fact is in the database for sure.

            • uncertain (a c : ℕ) : FactStatus

              The fact is present with weight a and absent with weight c.

            Instances For
              Dependency graph
              Dependency graph
              Dependency graph
              Dependency graph
              Dependency graph

              The weight of presence of an uncertain fact (and 0 otherwise).

              Equations
              Instances For
                Dependency graph

                The weight of absence of an uncertain fact (and 0 otherwise).

                Equations
                Instances For
                  Dependency graph

                  A probabilistic database over the schema R, S, T, with n constants: the status of every possible fact. The weights are written in binary on n bits, so they are below 2 ^ n.

                  Instances For
                    Dependency graph

                    The encoding of a probabilistic database as a weighted instance: the universe is the set of constants, which serve as bit positions too, in their own order, and the bits of a weight are its binary digits.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      Dependency graph

                      The concrete semantics #

                      Everything here is computed: a world is three Boolean tables, and the weighted count of the query can be evaluated on a small database.

                      A world may give the fact the truth value b: an absent fact is false, a certain fact is true, an uncertain fact is either.

                      Equations
                      Instances For
                        Dependency graph

                        The weight the fact contributes to a world giving it the truth value b: its weight of presence or of absence if it is uncertain, and 1 otherwise.

                        Equations
                        Instances For
                          Dependency graph
                          @[reducible, inline]

                          A world over n constants: the truth value of every possible fact.

                          Equations
                          Instances For
                            Dependency graph

                            The world is a possible world of the database.

                            Equations
                            Instances For
                              Dependency graph
                              Dependency graph

                              The weight of a world: the product of the weights of the facts.

                              Equations
                              Instances For
                                Dependency graph

                                The query h₀ holds in a world.

                                Equations
                                Instances For
                                  Dependency graph
                                  Dependency graph

                                  The concrete weighted count of h₀: the sum of the weights of the possible worlds of the database in which the query holds.

                                  Equations
                                  Instances For
                                    Dependency graph

                                    The weighted count of all the possible worlds: the denominator of the probability.

                                    Equations
                                    Instances For
                                      Dependency graph

                                      Faithfulness #

                                      theorem DescriptiveComplexity.vec1_eta {α : Type} (x : Fin 1 → α) :
                                      ![x 0] = x
                                      Dependency graph
                                      theorem DescriptiveComplexity.vec2_eta {α : Type} (x : Fin 2 → α) :
                                      ![x 0, x 1] = x
                                      Dependency graph

                                      The concrete world of a family of relations.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        Dependency graph

                                        The family of relations of a concrete world.

                                        Equations
                                        Instances For
                                          Dependency graph

                                          Families of relations over the schema are concrete worlds.

                                          Equations
                                          Instances For
                                            Dependency graph

                                            The facts over the schema: an R-fact, an S-fact or a T-fact.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              Dependency graph

                                              What a world may do with a fact, in terms of its status.

                                              Dependency graph

                                              The status of a fact, by cases: an uncertain fact is not certain.

                                              Dependency graph

                                              A weighted instance says of a fact what a status says: the fact is certain exactly when the status is, it is open exactly when the status is uncertain, and then its two weights, read in binary on the order of the instance, are those of the status.

                                              Instances For
                                                Dependency graph

                                                A weighted instance over the constants of a database matches it: its position order is linear, and it says of every fact what the database says.

                                                Instances For
                                                  Dependency graph

                                                  The status the database gives a fact.

                                                  Equations
                                                  Instances For
                                                    Dependency graph
                                                    Dependency graph

                                                    The product of the weights of the facts, in a family of relations, is the weight of its concrete world.

                                                    Dependency graph

                                                    The possible worlds of the instance are the possible worlds of the database.

                                                    Dependency graph

                                                    On an instance matching a database, the abstract count of the weighted worlds of h₀ is the weighted count computed from the database. The encoder's faithfulness and the decoder's soundness are both this.

                                                    Dependency graph

                                                    On an instance matching a database, the abstract count of all the weighted worlds is the total weight computed from the database.

                                                    Dependency graph

                                                    The encoding is faithful #

                                                    @[instance_reducible]

                                                    The encoded structure of a database, on its constants.

                                                    Equations
                                                    Instances For
                                                      Dependency graph

                                                      The encoding is faithful: the abstract count of the weighted worlds of h₀, on the encoded instance, is the weighted count computed from the database.

                                                      Dependency graph

                                                      The encoding is faithful for the denominator too: the abstract count of all the weighted worlds is the total weight computed from the database.

                                                      Dependency graph

                                                      The probability of h₀ over a database is the ratio of the two numbers computed from it. The uncertain facts of the database being present independently, each with probability its weight of presence over the sum of its weights, the probability that the query holds is count / total. Registered in the Lax archive as Lax794877.ExampleDatabaseProbability.probDb_worldProb_eq.

                                                      Dependency graph

                                                      Step 9: the decoder #

                                                      The converse of the encoding: from a concretely presented weighted instance back to a database, by a computation. The presented order has to be linear – otherwise the instance carries no number – and that is the only well-formedness condition. A fact that is both certain and uncertain is read as certain, which is what the semantics does with it.

                                                      The presented order of the positions, as a computation.

                                                      Equations
                                                      Instances For
                                                        Dependency graph
                                                        Dependency graph

                                                        Is the presented order linear?

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          Dependency graph
                                                          Dependency graph

                                                          A binary number of the presentation, as a computation: the sum of the place values of its bits, the place of a position being the number of positions strictly below it.

                                                          Equations
                                                          Instances For
                                                            Dependency graph

                                                            The status of a fact, from what the four relations say of it.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              Dependency graph
                                                              Dependency graph
                                                              theorem DescriptiveComplexity.statusOf_fits {N : ℕ} (c u : Bool) {a b : ℕ} (ha : a < 2 ^ N) (hb : b < 2 ^ N) :
                                                              (statusOf c u a b).presW < 2 ^ N ∧ (statusOf c u a b).absW < 2 ^ N
                                                              Dependency graph

                                                              The bits the presentation attaches to a fact, through a symbol of arity one more than the fact's.

                                                              Equations
                                                              Instances For
                                                                Dependency graph

                                                                The database a presentation with a linear order presents.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  Dependency graph

                                                                  The presented structure matches the database decoded from it.

                                                                  Dependency graph

                                                                  The decoder: on a presentation whose order is linear, the database it presents; none otherwise.

                                                                  Equations
                                                                  Instances For
                                                                    Dependency graph
                                                                    Dependency graph

                                                                    The decoding: every well-formed presented instance decodes to a database with the same weighted count of h₀. So the hardness of the abstract problem is not hardness on structures no database produces.

                                                                    Equations
                                                                    Instances For
                                                                      Dependency graph

                                                                      A database, computed #

                                                                      Two constants. R(0) is uncertain with probability 1/2, S(0, 1) is certain, T(1) is uncertain with probability 1/4. The query holds in the one world keeping both uncertain facts, of weight 1 · 1, out of a total weight (1 + 1) · (1 + 3) = 8: its probability is 1/8.

                                                                      A small probabilistic database.

                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For
                                                                        Dependency graph

                                                                        The encoded instance of the database, as a presentation: what a decoder reads.

                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For
                                                                          Dependency graph

                                                                          The decoder runs too, and gets the two numbers back from the encoded instance.

                                                                          Step 10: the easy query #

                                                                          The query ∃ x y, R(x) ∧ S(x, y).

                                                                          Equations
                                                                          • One or more equations did not get rendered due to their size.
                                                                          Instances For
                                                                            Dependency graph

                                                                            Step 11: the weights #

                                                                            Dependency graph
                                                                            Dependency graph
                                                                            Dependency graph
                                                                            Dependency graph
                                                                            Dependency graph

                                                                            The weight of failing at x: R(x) is present and no S(x, y) is, or R(x) is absent.

                                                                            Equations
                                                                            • One or more equations did not get rendered due to their size.
                                                                            Instances For
                                                                              Dependency graph

                                                                              The weight of succeeding at x: R(x) is present, and the S(x, y) are sorted by the first y that is present.

                                                                              Equations
                                                                              • One or more equations did not get rendered due to their size.
                                                                              Instances For
                                                                                Dependency graph

                                                                                Step 12: failing is a product #

                                                                                @[reducible, inline]

                                                                                The facts, as variables: the R-facts, the S-facts and the T-facts.

                                                                                Equations
                                                                                Instances For
                                                                                  Dependency graph

                                                                                  “The query holds”, on a valuation of the facts.

                                                                                  Equations
                                                                                  Instances For
                                                                                    Dependency graph
                                                                                    theorem DescriptiveComplexity.SafeQ.weightedCount_fail {A : Type} [(weightedLang FirstOrder.Language.rst).Structure A] [Fintype A] [LinearOrder A] :
                                                                                    (weightedCount (fun (z : Var A) => fullPresWeight ((factEquiv A).symm z)) (fun (z : Var A) => fullAbsWeight ((factEquiv A).symm z)) fun (v : Var A → Bool) => !holdsRs v) = (∏ y : A, tT A y) * ∏ x : A, failW A x

                                                                                    Failing is a product: the weight of the worlds in which the query fails is the product, over x, of the weight of failing at x, times the total weight of the T-facts.

                                                                                    Dependency graph

                                                                                    Step 13: succeeding, without a subtraction #

                                                                                    theorem DescriptiveComplexity.SafeQ.weightedWorlds_rs_eq {A : Type} [(weightedLang FirstOrder.Language.rst).Structure A] [Fintype A] [LinearOrder A] (hlin : IsLinOrd (WLe FirstOrder.Language.rst A)) :
                                                                                    (WeightedWorlds rs).Count A = (∏ y : A, tT A y) * ∑ x : A, (∏ x' : A with x' < x, failW A x') * succW A x * ∏ x' : A with x < x', (failW A x' + succW A x')

                                                                                    The count of the weighted worlds of the query, in closed form: the worlds in which the query holds, sorted by the first x at which it succeeds.

                                                                                    Dependency graph

                                                                                    Step 14: the term #

                                                                                    The weight of presence of R(x), as a term.

                                                                                    Equations
                                                                                    Instances For
                                                                                      Dependency graph

                                                                                      The weight of absence of R(x), as a term.

                                                                                      Equations
                                                                                      Instances For
                                                                                        Dependency graph
                                                                                        noncomputable def DescriptiveComplexity.SafeQ.aST {δ : Type} (x y : δ) :

                                                                                        The weight of presence of S(x, y), as a term.

                                                                                        Equations
                                                                                        Instances For
                                                                                          Dependency graph
                                                                                          noncomputable def DescriptiveComplexity.SafeQ.cST {δ : Type} (x y : δ) :

                                                                                          The weight of absence of S(x, y), as a term.

                                                                                          Equations
                                                                                          Instances For
                                                                                            Dependency graph
                                                                                            Dependency graph

                                                                                            The weight of failing at x, as a term.

                                                                                            Equations
                                                                                            • One or more equations did not get rendered due to their size.
                                                                                            Instances For
                                                                                              Dependency graph

                                                                                              The weight of succeeding at x, as a term: a sum over the first y.

                                                                                              Equations
                                                                                              • One or more equations did not get rendered due to their size.
                                                                                              Instances For
                                                                                                Dependency graph

                                                                                                The term computing the weighted count of the query: the total weight of the T-facts, times the sum, over the first x at which the query succeeds, of the weight of failing before, succeeding at x, and doing anything after; all of it 0 unless the positions are linearly ordered.

                                                                                                Equations
                                                                                                • One or more equations did not get rendered due to their size.
                                                                                                Instances For
                                                                                                  Dependency graph

                                                                                                  Step 15: the theorem #

                                                                                                  The weighted count of ∃ x y, R(x) ∧ S(x, y) is in FP: the numerator of the probability of this query over a probabilistic database is computed in polynomial time, where that of ∃ x y, R(x) ∧ S(x, y) ∧ T(y) is #P-hard (DescriptiveComplexity.weightedWorlds_h0_sharpP_oneCallComplete). Registered in the Lax archive as Lax794877.HierarchicalQueryInFP.weightedWorlds_rs_mem_FP.

                                                                                                  Dependency graph