Documentation

Linglib.Core.Computability.ContextFreeGrammar.Dirichlet

Dirichlet priors on probabilistic context-free grammars #

A Dirichlet PCFG over G is a Dirichlet prior on the rule distribution at each nonterminal, given by a positive pseudo-count for every rule. Integrating the rule weights out, the probability of a corpus is a product over nonterminals of Pólya-urn likelihoods of the corpus rule counts, so derivation trees are exchangeable but not independent: the corpus probability does not factorise over sub-corpora as PCFG.corpusProb does. The posterior given a corpus is again a Dirichlet PCFG, with pseudo-counts raised by the corpus rule counts, and the posterior predictive rule distribution is the PCFG of normalised posterior pseudo-counts.

Main definitions #

Main results #

References #

structure DirichletPCFG {T : Type u_1} [DecidableEq T] (G : ContextFreeGrammar T) [DecidableEq G.NT] :
Type u_1

A Dirichlet prior on the rule distributions of G: a positive pseudo-count for every rule of the grammar.

  • pseudo : ContextFreeRule T G.NT → ℝ

    The Dirichlet pseudo-count of a rule.

  • pseudo_pos (r : ContextFreeRule T G.NT) : r ∈ G.rules → 0 < self.pseudo r

    Pseudo-counts are positive on the rules of the grammar.

Instances For
    theorem DirichletPCFG.ext {T : Type u_1} {inst✝ : DecidableEq T} {G : ContextFreeGrammar T} {inst✝¹ : DecidableEq G.NT} {x y : DirichletPCFG G} (pseudo : x.pseudo = y.pseudo) :
    x = y
    theorem DirichletPCFG.ext_iff {T : Type u_1} {inst✝ : DecidableEq T} {G : ContextFreeGrammar T} {inst✝¹ : DecidableEq G.NT} {x y : DirichletPCFG G} :
    x = y ↔ x.pseudo = y.pseudo
    def DirichletPCFG.lhsCounts {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (a : G.NT) (D : Multiset (RoseTree (Symbol T G.NT))) :
    G.RulesWithLHS a → ℕ

    The corpus counts of the rules with left-hand side a.

    Equations
    Instances For
      noncomputable def DirichletPCFG.lhsFactor {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (a : G.NT) (D : Multiset (RoseTree (Symbol T G.NT))) :
      ℝ

      The likelihood of the corpus counts at nonterminal a under the Pólya urn over the rules with left-hand side a, weighted by M's pseudo-counts.

      Equations
      Instances For
        noncomputable def DirichletPCFG.corpusProb {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D : Multiset (RoseTree (Symbol T G.NT))) :
        ℝ

        The probability of a corpus with the rule weights integrated out: the product over the nonterminals the grammar expands of the Pólya-urn likelihood there.

        Equations
        • M.corpusProb D = ∏ a ∈ Finset.image (fun (x : ContextFreeRule T G.NT) => x.input) G.rules, M.lhsFactor a D
        Instances For
          theorem DirichletPCFG.nonempty_rulesWithLHS_of_mem_image {T : Type u_1} {G : ContextFreeGrammar T} [DecidableEq G.NT] {a : G.NT} (ha : a ∈ Finset.image (fun (x : ContextFreeRule T G.NT) => x.input) G.rules) :
          Nonempty (G.RulesWithLHS a)
          theorem DirichletPCFG.lhsFactor_pos {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {a : G.NT} (ha : a ∈ Finset.image (fun (x : ContextFreeRule T G.NT) => x.input) G.rules) (D : Multiset (RoseTree (Symbol T G.NT))) :
          0 < M.lhsFactor a D
          theorem DirichletPCFG.corpusProb_nonneg {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D : Multiset (RoseTree (Symbol T G.NT))) :
          0 ≤ M.corpusProb D
          @[simp]
          theorem DirichletPCFG.lhsCounts_zero {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (a : G.NT) :
          lhsCounts a 0 = 0
          @[simp]
          theorem DirichletPCFG.lhsFactor_zero {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (a : G.NT) :
          M.lhsFactor a 0 = 1
          @[simp]
          theorem DirichletPCFG.corpusProb_zero {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) :
          M.corpusProb 0 = 1

          Conjugate update #

          noncomputable def DirichletPCFG.posterior {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D : Multiset (RoseTree (Symbol T G.NT))) :

          The posterior given a corpus: each rule's pseudo-count grows by its corpus count.

          Equations
          Instances For
            @[simp]
            theorem DirichletPCFG.posterior_zero {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) :
            M.posterior 0 = M
            theorem DirichletPCFG.posterior_add {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D₁ D₂ : Multiset (RoseTree (Symbol T G.NT))) :
            M.posterior (D₁ + D₂) = (M.posterior D₁).posterior D₂

            Posterior predictive rule distribution #

            noncomputable def DirichletPCFG.predictive {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (r : ContextFreeRule T G.NT) (D : Multiset (RoseTree (Symbol T G.NT))) :
            ℝ

            The posterior predictive probability of rule r at its left-hand side given a corpus, the posterior mean of its weight: its posterior pseudo-count over the total at that nonterminal.

            Equations
            Instances For
              theorem DirichletPCFG.predictive_denom_pos {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {a : G.NT} [hne : Nonempty (G.RulesWithLHS a)] (D : Multiset (RoseTree (Symbol T G.NT))) :
              0 < ∑ r' ∈ G.rules with r'.input = a, (M.pseudo r' + ↑(RoseTree.corpusRuleCount r' D))
              theorem DirichletPCFG.predictive_pos {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r : ContextFreeRule T G.NT} (hr : r ∈ G.rules) (D : Multiset (RoseTree (Symbol T G.NT))) :
              0 < M.predictive r D
              theorem DirichletPCFG.predictive_nonneg {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r : ContextFreeRule T G.NT} (hr : r ∈ G.rules) (D : Multiset (RoseTree (Symbol T G.NT))) :
              0 ≤ M.predictive r D
              @[simp]
              theorem DirichletPCFG.predictive_zero {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (r : ContextFreeRule T G.NT) :
              M.predictive r 0 = M.pseudo r / ∑ r' ∈ G.rules with r'.input = r.input, M.pseudo r'

              With no data the predictive probability is the normalised prior pseudo-count.

              theorem DirichletPCFG.predictive_lt_iff_of_same_lhs {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r r' : ContextFreeRule T G.NT} (hr' : r' ∈ G.rules) (h : r.input = r'.input) {D : Multiset (RoseTree (Symbol T G.NT))} :
              M.predictive r D < M.predictive r' D ↔ M.pseudo r + ↑(RoseTree.corpusRuleCount r D) < M.pseudo r' + ↑(RoseTree.corpusRuleCount r' D)

              At a shared left-hand side, predictive probabilities order rules by posterior pseudo-count.

              theorem DirichletPCFG.sum_predictive_eq_one {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {a : G.NT} [Nonempty (G.RulesWithLHS a)] (D : Multiset (RoseTree (Symbol T G.NT))) :
              ∑ r ∈ G.rules with r.input = a, M.predictive r D = 1
              noncomputable def DirichletPCFG.predictivePCFG {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D : Multiset (RoseTree (Symbol T G.NT))) :

              The posterior predictive PCFG: the predictive probability of each rule of the grammar.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                theorem DirichletPCFG.predictivePCFG_weight {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r : ContextFreeRule T G.NT} (hr : r ∈ G.rules) (D : Multiset (RoseTree (Symbol T G.NT))) :
                (M.predictivePCFG D).weight r = ENNReal.ofReal (M.predictive r D)
                theorem DirichletPCFG.predictivePCFG_posterior {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (D : Multiset (RoseTree (Symbol T G.NT))) :

                Conditioning on a corpus and then taking the prior predictive is taking the posterior predictive.

                Posterior mode #

                The mode of the posterior Dirichlet at a nonterminal lies in the interior of the simplex when every posterior pseudo-count there exceeds 1, and is then (π_r + x_r - 1) / ∑ (π_r' + x_r' - 1). Where it is defined it orders rules exactly as the predictive probability does.

                noncomputable def DirichletPCFG.posteriorMode {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (r : ContextFreeRule T G.NT) (D : Multiset (RoseTree (Symbol T G.NT))) :
                ℝ

                The mode of the posterior Dirichlet weight of rule r, 0 by the division convention outside the interior regime M.PosteriorModeInterior r.input D.

                Equations
                Instances For
                  def DirichletPCFG.PosteriorModeInterior {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) (a : G.NT) (D : Multiset (RoseTree (Symbol T G.NT))) :

                  The posterior mode at nonterminal a lies in the interior of the simplex: every posterior pseudo-count there exceeds 1.

                  Equations
                  Instances For
                    theorem DirichletPCFG.posteriorMode_denom_pos {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {a : G.NT} [hne : Nonempty (G.RulesWithLHS a)] (D : Multiset (RoseTree (Symbol T G.NT))) (h : M.PosteriorModeInterior a D) :
                    0 < ∑ r' ∈ G.rules with r'.input = a, (M.pseudo r' + ↑(RoseTree.corpusRuleCount r' D) - 1)
                    theorem DirichletPCFG.posteriorMode_pos {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r : ContextFreeRule T G.NT} (hr : r ∈ G.rules) (D : Multiset (RoseTree (Symbol T G.NT))) (h : M.PosteriorModeInterior r.input D) :
                    0 < M.posteriorMode r D
                    theorem DirichletPCFG.posteriorMode_lt_iff_of_same_lhs {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r r' : ContextFreeRule T G.NT} (hr' : r' ∈ G.rules) (h : r.input = r'.input) {D : Multiset (RoseTree (Symbol T G.NT))} (hint : M.PosteriorModeInterior r'.input D) :
                    M.posteriorMode r D < M.posteriorMode r' D ↔ M.pseudo r + ↑(RoseTree.corpusRuleCount r D) < M.pseudo r' + ↑(RoseTree.corpusRuleCount r' D)
                    theorem DirichletPCFG.predictive_lt_iff_posteriorMode_lt {T : Type u_1} [DecidableEq T] {G : ContextFreeGrammar T} [DecidableEq G.NT] (M : DirichletPCFG G) {r r' : ContextFreeRule T G.NT} (hr' : r' ∈ G.rules) (h : r.input = r'.input) {D : Multiset (RoseTree (Symbol T G.NT))} (hint : M.PosteriorModeInterior r'.input D) :
                    M.predictive r D < M.predictive r' D ↔ M.posteriorMode r D < M.posteriorMode r' D

                    Where the posterior mode is defined, it orders rules at a shared left-hand side exactly as the predictive probability does.