# 随机算法 (Spring 2014)/The Monte Carlo Method

Jump to: navigation, search

# Parameter Estimation

Consider the following abstract problem of parameter estimation.

Let ${\displaystyle U}$ be a finite set of known size, and let ${\displaystyle G\subseteq U}$. We want to estimate the parameter ${\displaystyle |G|}$, i.e. the size of ${\displaystyle G}$, or equivalently, the density (or frequency) ${\displaystyle {\frac {|G|}{|U|}}}$.

We assume two devices:

• A uniform sampler ${\displaystyle {\mathcal {U}}}$, which uniformly and independently samples a member of ${\displaystyle U}$ upon each calling.
• A membership oracle of ${\displaystyle G}$, denoted ${\displaystyle {\mathcal {O}}}$. Given as the input an ${\displaystyle x\in U}$, ${\displaystyle {\mathcal {O}}(x)}$ indicates whether or not ${\displaystyle x}$ is a member of ${\displaystyle G}$.

Equipped by ${\displaystyle {\mathcal {U}}}$ and ${\displaystyle {\mathcal {O}}}$, we can have the following Monte Carlo method:

• Choose ${\displaystyle N}$ independent samples from ${\displaystyle U}$ by the uniform sampler ${\displaystyle {\mathcal {U}}}$, represented by the random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{N}}$.
• Let ${\displaystyle Y_{i}}$ be the indicator random variable defined as ${\displaystyle Y_{i}={\mathcal {O}}(X_{i})}$, namely, ${\displaystyle Y_{i}}$ indicates whether ${\displaystyle X_{i}\in G}$.
• Define the estimator random variable for ${\displaystyle |G|}$ as
${\displaystyle Z={\frac {|U|}{N}}\sum _{i=1}^{N}Y_{i}}$
• Define the estimator random variable for ${\displaystyle \alpha ={\frac {|G|}{|U|}}}$ as
${\displaystyle {\overline {Y}}={\frac {1}{N}}\sum _{i=1}^{N}Y_{i}}$.

It is easy to see that ${\displaystyle \mathbf {E} [Z]=|G|}$ and ${\displaystyle \mathbf {E} [{\overline {Y}}]=\alpha }$. We might hope that with high probability the values of ${\displaystyle Z}$ and ${\displaystyle {\overline {Y}}}$ are close to their respective expectations. Formally, ${\displaystyle Z}$ is called an ${\displaystyle \epsilon }$-approximation of ${\displaystyle |G|}$ if

${\displaystyle (1-\epsilon )|G|\leq Z\leq (1+\epsilon )|G|.}$

And ${\displaystyle {\overline {Y}}}$ is called an ${\displaystyle (\epsilon ,\delta )}$-estimator for ${\displaystyle \alpha }$ if

${\displaystyle \Pr[|{\overline {Y}}-\alpha |\leq \epsilon ]\geq 1-\delta .}$

The following general theorem bounds the reliance between the accuracy of estimation and the number of samples.

 Theorem (estimator theorem) Let ${\displaystyle \alpha ={\frac {|G|}{|U|}}}$. The following holds: 1. ${\displaystyle Z}$ is an ${\displaystyle \epsilon }$-approximation to ${\displaystyle |G|}$ with probability at least ${\displaystyle 1-\delta }$ if ${\displaystyle N\geq {\frac {4}{\epsilon ^{2}\alpha }}\ln {\frac {2}{\delta }}}$. 2. ${\displaystyle {\overline {Y}}}$ is an ${\displaystyle (\epsilon ,\delta )}$-estimator for ${\displaystyle \alpha }$ if ${\displaystyle N\geq {\frac {4}{\epsilon ^{2}}}\ln {\frac {2}{\delta }}}$.
Proof.
 Recall that ${\displaystyle Y_{i}}$ indicates whether the ${\displaystyle i}$-th sample is in ${\displaystyle G}$. Let ${\displaystyle Y=\sum _{i=1}^{N}Y_{i}}$. Then we have ${\displaystyle Z={\frac {|U|}{N}}Y}$, and hence the event ${\displaystyle (1-\epsilon )|G|\leq Z\leq (1+\epsilon )|G|}$ is equivalent to that ${\displaystyle (1-\epsilon ){\frac {|G|}{|U|}}N\leq Y\leq (1+\epsilon ){\frac {|G|}{|U|}}N}$. Note that each ${\displaystyle Y_{i}}$ is a Bernoulli trial that succeeds with probability ${\displaystyle {\frac {|G|}{|U|}}}$, thus ${\displaystyle \mathbb {E} [Y]={\frac {|G|}{|U|}}N}$. Then the rest is due to Chernoff bound.
${\displaystyle \square }$

A counting algorithm for the set ${\displaystyle G}$ has to deal with the following three issues:

1. Implement the membership oracle ${\displaystyle {\mathcal {O}}}$. This is usually straightforward, or assumed by the model.
2. Implement the uniform sampler ${\displaystyle {\mathcal {U}}}$. This can be straightforward or highly nontrivial, depending on the problem.
3. Deal with exponentially small ${\displaystyle \alpha ={\frac {|G|}{|U|}}}$. This requires us to cleverly choose the universe ${\displaystyle U}$. And this part usually uses some beautiful ideas.

# Counting DNFs

A disjunctive normal form (DNF) formular is a disjunction (OR) of clauses, where each clause is a conjunction (AND) of literals. For example:

${\displaystyle (x_{1}\wedge {\overline {x_{2}}}\wedge x_{3})\vee (x_{2}\wedge x_{4})\vee ({\overline {x_{1}}}\wedge x_{3}\wedge x_{4})}$.

Note the difference from the conjunctive normal forms (CNF).

Given a DNF formular ${\displaystyle \phi }$ as the input, the problem is to count the number of satisfying assignments of ${\displaystyle \phi }$. This problem is #P-complete. So we do not expect to have a polynomial-time algorithm for exact solving the problem. Instead we are seeking for approximation algorithms. Interestingly, even though the computational complexity of the problem is so high, it admits some very nice approximation algorithm which has very good approximation ratio (theoretically speaking). We need to introduce some general concepts for approximation algorithms.

 FPTAS (Fully Polynomial-Time Approximation Scheme) Consider a computational problem ${\displaystyle f:\{0,1\}^{*}\to \mathbb {Z} ^{+}}$ whose outputs are positive integers. A FPTAS (fully polynomial-time approximation scheme) for ${\displaystyle f}$ is an algorithm ${\displaystyle {\mathcal {A}}}$ that takes the following inputs: an input instance ${\displaystyle x}$ of problem ${\displaystyle f}$, which we use ${\displaystyle n=|x|}$ to denote its length; a real number ${\displaystyle 0<\epsilon <1}$; and in time polynomial in both ${\displaystyle n}$ and ${\displaystyle {\frac {1}{\epsilon }}}$, returns an output ${\displaystyle {\mathcal {A}}(x,\epsilon )}$ such that ${\displaystyle (1-\epsilon )f(x)\leq {\mathcal {A}}(x,\epsilon )\leq (1+\epsilon )f(x).}$

The adverb "fully" stresses the polynomial reliance on not just the input size ${\displaystyle n}$ but also the approximation parameter ${\displaystyle {\frac {1}{\epsilon }}}$. An FPTAS may approximate the true value of the output within any accuracy in polynomial time, though the polynomial becomes larger when the approximation is more accurate. Moreover, the running time grows just polynomially with repeat to the accuracy bounds. Theoretically speaking, FPTAS is the best we can get for computational hard problems, though in practice sometimes the polynomial bound might be too large even for a moderate ${\displaystyle \epsilon }$.

The FPRAS (fully polynomial-time randomized approximation scheme), on the other hand, is the randomized relaxation of FPTAS.

 FPRAS (Fully Polynomial-time Randomized Approximation Scheme) Consider a computational problem ${\displaystyle f:\{0,1\}^{*}\to \mathbb {Z} ^{+}}$ whose outputs are positive integers. A FPRAS (fully polynomial-time randomized approximation scheme) for ${\displaystyle f}$ is a randomized algorithm ${\displaystyle {\mathcal {A}}}$ that takes the following inputs: an input instance ${\displaystyle x}$ of problem ${\displaystyle f}$, which we use ${\displaystyle n=|x|}$ to denote its length; two real numbers ${\displaystyle 0<\epsilon ,\delta <1}$; and in time polynomial in ${\displaystyle n}$, ${\displaystyle {\frac {1}{\epsilon }}}$, and ${\displaystyle \log {\frac {1}{\delta }}}$, returns an output ${\displaystyle {\mathcal {A}}(x,\epsilon ,\delta )}$ such that ${\displaystyle \Pr \left[(1-\epsilon )f(x)\leq {\mathcal {A}}(x,\epsilon ,\delta )\leq (1+\epsilon )f(x)\right]\geq 1-\delta .}$

Note that the reliance on the confidence error ${\displaystyle \delta }$ is polynomial in ${\displaystyle \log {\frac {1}{\delta }}}$, instead of ${\displaystyle {\frac {1}{\delta }}}$. This is because due to the Chernoff bound, the confidence error can be reduced in an exponential rate by independent repetitions. This motivates the following simpler but equivalent definition of FPRAS, which only considers the special confidence error ${\displaystyle \delta ={\frac {1}{3}}}$. Apparently, this is a more natural way to relax the FPTAS to randomized algorithms as it mimics the way of generalizing class P to BPP for decision problems. If you are familiar with the Chernoff bound, it is quite easy to observe the equivalence between the following definition and the above one. And this simple definition is also easier to check (you only need to verify the case for the error ${\displaystyle \delta ={\frac {1}{3}}}$), which makes it more convenient to apply.

 FPRAS (equivalent simple version) Consider a computational problem ${\displaystyle f:\{0,1\}^{*}\to \mathbb {Z} ^{+}}$ whose outputs are positive integers. A FPRAS (fully polynomial-time randomized approximation scheme) for ${\displaystyle f}$ is a randomized algorithm ${\displaystyle {\mathcal {A}}}$ that takes the following inputs: an input instance ${\displaystyle x}$ of problem ${\displaystyle f}$, which we use ${\displaystyle n=|x|}$ to denote its length; a real number ${\displaystyle 0<\epsilon <1}$; and in time polynomial in ${\displaystyle n}$ and ${\displaystyle {\frac {1}{\epsilon }}}$, returns an output ${\displaystyle {\mathcal {A}}(x,\epsilon )}$ such that ${\displaystyle \Pr \left[(1-\epsilon )f(x)\leq {\mathcal {A}}(x,\epsilon )\leq (1+\epsilon )f(x)\right]\geq {\frac {2}{3}}.}$

The Monte Carlo method for the parameter estimation problem would give us an FPRAS if the sampler and membership oracle are poly-time realizable and the fraction ${\displaystyle \alpha ={\frac {|G|}{|U|}}}$ is bounded from below by a ${\displaystyle {\frac {1}{\mathrm {poly} (n)}}}$.

However, for the DNF counting problem, naively applying the Monte Carlo method (treat the set of all ${\displaystyle 2^{n}}$ truth assignments as ${\displaystyle U}$ and the set of DNF solutions as ${\displaystyle G}$) will not give a good algorithm. The problem is that the fraction ${\displaystyle \alpha ={\frac {|G|}{|U|}}}$ can be exponentially small:

Suppose that there are ${\displaystyle n}$ variables. Let ${\displaystyle U=\{\mathrm {true} ,\mathrm {false} \}^{n}}$ be the set of all truth assignments of the ${\displaystyle n}$ variables. Let ${\displaystyle G=\{x\in U\mid \phi (x)=\mathrm {true} \}}$ be the set of satisfying assignments for ${\displaystyle \phi }$. The straightforward use of Monte Carlo method samples ${\displaystyle N}$ assignments from ${\displaystyle U}$ and check how many of them satisfy ${\displaystyle \phi }$. This algorithm fails when ${\displaystyle |G|/|U|}$ is exponentially small, namely, when exponentially small fraction of the assignments satisfy the input DNF formula.

So instead of naively considering the space of satisfying solutions and the space of all solutions, we represent the space of satisfying solutions to a DNF formula as a union of sets of satisfying solutions to the individual clauses, and count this union of sets. This gives us an abstract problem called the union of sets problem.

## The union of sets problem

We reformulate the DNF counting problem in a more abstract framework, called the union of sets problem.

 The union of sets problem Let ${\displaystyle V}$ be a finite universe. Input: ${\displaystyle m}$ subsets ${\displaystyle H_{1},H_{2},\ldots ,H_{m}\subseteq V}$ of the universe; Output: the size of the union set ${\displaystyle H=\bigcup _{i=1}^{m}H_{i}}$

To make the algorithm work, we make the following assumptions:

1. For all ${\displaystyle i}$, the value of ${\displaystyle |H_{i}|}$ can be computed efficiently.
2. It is possible to sample uniformly from each individual ${\displaystyle H_{i}}$.
3. For any ${\displaystyle x\in V}$, it can be determined efficiently whether ${\displaystyle x\in H_{i}}$.

DNF counting can be easily put into this general framework: Suppose that the DNF formula is a disjunction (${\displaystyle \vee }$) of ${\displaystyle m}$ clauses ${\displaystyle C_{1},C_{2},\ldots ,C_{m}}$ on ${\displaystyle n}$ variables, where each clause ${\displaystyle C_{i}}$ is a conjunction (${\displaystyle \wedge }$) of ${\displaystyle k_{i}}$ literals. Without loss of generality, we may assume that in each clause, each variable appears at most once. The satisfying solutions to this DNF can be represented as a union of ${\displaystyle m}$ sets as follows:

• ${\displaystyle V=\{{\text{true}},{\text{false}}\}^{n}}$ is the set of all ${\displaystyle 2^{n}}$ truth assignments.
• Each ${\displaystyle H_{i}=\{x\in \{{\text{true}},{\text{false}}\}^{n}\mid x{\mbox{ satisfies }}C_{i}\}}$ is the set of satisfying assignments for the ${\displaystyle i}$-th clause ${\displaystyle C_{i}}$ of the DNF formula.

Then the union of sets ${\displaystyle H=\bigcup _{i}H_{i}}$ gives the set of satisfying assignments for the DNF formula ${\displaystyle C_{1}\vee C_{2}\vee \cdots \vee C_{m}}$. Its size ${\displaystyle |H|}$ gives the number of satisfying assignments for the DNF.

Furthermore, the three assumptions above can also be guaranteed in this construction:

1. Since each clause ${\displaystyle C_{i}}$ is a conjunction of ${\displaystyle k_{i}}$ literals, it is easy to see that ${\displaystyle |H_{i}|=2^{n-k_{i}}}$, which is obviously efficiently computable.
2. Sampling from each ${\displaystyle H_{i}}$ is very easy: we just fix the assignments of the ${\displaystyle k_{i}}$ variables involved in the corresponding clause ${\displaystyle C_{i}}$ to satisfy ${\displaystyle C_{i}}$ (there is exactly one such choice), and sample uniformly and independently the rest ${\displaystyle (n-k_{i})}$ variable assignments.
3. For each assignment ${\displaystyle x}$, it is easy to check whether it satisfies a clause ${\displaystyle C_{i}}$, thus it is easy to determine whether ${\displaystyle x\in H_{i}}$.

## The coverage algorithm

We now introduce the coverage algorithm for the union of sets problem.

Consider the multiset ${\displaystyle U}$ defined by

${\displaystyle U=H_{1}\uplus H_{2}\uplus \cdots \uplus H_{m}}$,

where ${\displaystyle \uplus }$ denotes the multiset union. It is more convenient to define ${\displaystyle U}$ as the set

${\displaystyle U=\{(x,i)\mid x\in H_{i}\}}$.

For each ${\displaystyle x\in H}$, there may be more than one instances of ${\displaystyle (x,i)\in U}$. We may choose the ${\displaystyle (x,i)}$ with the minimum ${\displaystyle i}$, and collect all such ${\displaystyle (x,i)}$ to form a set ${\displaystyle G}$.

Formally, the set ${\displaystyle G}$ is defined as

${\displaystyle G=\{(x,i)\in U\mid \forall (x,j)\in U,j\geq i\}}$.

Every ${\displaystyle x\in H}$ corresponds to a unique ${\displaystyle (x,i)\in G}$ where ${\displaystyle i}$ is the smallest among ${\displaystyle x\in H_{i}}$.

The following proposition is obvious

 Proposition ${\displaystyle G\subseteq U}$; ${\displaystyle |G|=|H|\,}$.

Therefore, the estimation of the size of the union set ${\displaystyle H\bigcup _{i=1}^{m}H_{i}}$ is reduced to the estimation of the size of the subset ${\displaystyle G\subseteq U}$, where ${\displaystyle U}$ is the multiset union of ${\displaystyle H_{1},H_{2},\ldots ,H_{m}}$ and ${\displaystyle H}$. Note that now the fraction ${\displaystyle \alpha =|G|/|U|}$ becomes large enough. As long as we can implement a uniform sampler for ${\displaystyle U}$ and a membership oracle for ${\displaystyle G}$, we can apply the estimator theorem to get a good approximation algorithm for ${\displaystyle |G|=|H|}$.

An uniform sample from ${\displaystyle U}$ can be implemented as follows.

 Uniform sampler for ${\displaystyle U}$ generate an ${\displaystyle i\in \{1,2,\ldots ,m\}}$ with probability ${\displaystyle {\frac {|H_{i}|}{\sum _{i=1}^{m}|H_{i}|}}}$; uniformly sample an ${\displaystyle x\in H_{i}}$, and return ${\displaystyle (x,i)}$.

It is easy to see that this gives a uniform member of ${\displaystyle U}$. And this uniform sampler is efficient as long as each ${\displaystyle |H_{i}|}$ is efficiently computable and we have an efficient uniform sample for each ${\displaystyle H_{i}}$, both of which are assumed.

It is also easy to implement the membership oracle for ${\displaystyle G}$:

 Membership oracle for ${\displaystyle G}$ upon each query ${\displaystyle (x,i)}$: if ${\displaystyle i}$ is the smallest among all ${\displaystyle j}$ that ${\displaystyle x\in H_{j}}$ return "yes"; else return "no";

By definition of ${\displaystyle G}$, it is easy to see this membership oracle works, and it is efficient if the membership of each ${\displaystyle H_{i}}$ can be efficiently determined, which is also assumed.

We now only need to lower bound the fraction

${\displaystyle \alpha ={\frac {|G|}{|U|}}}$.
 Proposition ${\displaystyle \alpha \geq {\frac {1}{m}}}$.
Proof.
 For every ${\displaystyle x\in H}$, there are at most ${\displaystyle m}$ different pairs ${\displaystyle (x,i)}$ in ${\displaystyle U}$, thus ${\displaystyle |U|\leq m|H|}$. And we already know that ${\displaystyle |G|=|H|}$.
${\displaystyle \square }$

Applying the estimator theorem, this gives us an algorithm which returns an ${\displaystyle \epsilon }$-approximation of ${\displaystyle |H|}$ with probability at least ${\displaystyle 1-\delta }$, by making at most ${\displaystyle {\frac {4m}{\epsilon ^{2}}}\ln {\frac {2}{\delta }}}$ calls to the uniform random sampler for ${\displaystyle U}$ and the membership oracle for ${\displaystyle G}$, due to the three efficiency assumptions, both of which can be efficiently implemented. The resulting algorithm is called the coverage algorithm.

Applying the coverage algorithm to the DNF counting, which as we have discussed above, is a special case for the union of set problem, we have an FPRAS.

 Theorem (Karp-Luby, 1983) There exists an FPRAS for the DNF counting problem.

# Count Matchings

Recall that a matching ${\displaystyle M}$ in a graph ${\displaystyle G(V,E)}$ is a subset ${\displaystyle M\subseteq E}$ of edges such that no vertex in ${\displaystyle V}$ is incident to more than one edges in ${\displaystyle M}$, i.e. a matching is an "edge-version" of independent set.

Let us consider a more complicated counting problem, counting the number matchings in an input graph. The problem is formally defined as follows.

Input: an indirected graph ${\displaystyle G(V,E)}$;
Output: ${\displaystyle Z(G)=}$ number of matchings in ${\displaystyle G}$.

Note that we count all matchings, not just maximum or maximal matchings. For example, an empty set (containing no edge) is always a matching.

This problem is #P-complete. In fact, it is among the first few problems known to be #P-complete. The studies of the computational complexity and efficient approximation algorithms for this problem pioneer the fields of counting complexity and randomized algorithms.

## A self-reduction procedure

We show a generic self-reduction procedure due to Jerrum-Valiant-Vazirani, which reduces a counting problem to the problems of smaller instances. This technique does not just work for counting matchings, but may also work a wide range of counting version of CSPs (constraint satisfaction problem) who show self-reducibility property.

Fix an input graph ${\displaystyle G(V,E)}$. Let ${\displaystyle Z(G)}$ be the number of matchings in ${\displaystyle G}$.

We consider the uniform probability distribution over all matchings in ${\displaystyle G}$. Let ${\displaystyle X}$ be such a random matching sampled uniformly at random from all matchings in ${\displaystyle G}$. More formally, for any ${\displaystyle M\subseteq E}$,

${\displaystyle \Pr[X=M]={\begin{cases}{\frac {1}{Z(G)}}&{\mbox{if }}M{\mbox{ is a matching in }}G,\\0&{\mbox{otherwise.}}\\\end{cases}}}$

Consider the special matching ${\displaystyle M=\emptyset }$. It clearly holds that ${\displaystyle \Pr[X=\emptyset ]={\frac {1}{Z(G)}}}$, thus

${\displaystyle Z(G)={\frac {1}{\Pr[X=\emptyset ]}}.}$

Thus we can estimate ${\displaystyle Z(G)}$ by estimating the probability ${\displaystyle \Pr[X=\emptyset ]}$.

We denote ${\displaystyle E=\{e_{1},e_{2},\ldots ,e_{m}\}}$, where the order of edges ${\displaystyle e_{1},e_{2},\ldots ,e_{m}}$ is arbitrary.

 The random matching ${\displaystyle X}$ can be seen as a random vector ${\displaystyle X=(X_{1},X_{2},\ldots ,X_{m})}$, where each ${\displaystyle X_{i}}$ indicates whether ${\displaystyle e_{i}}$ appears in the random matching ${\displaystyle X}$. Thus the probability ${\displaystyle \Pr[X=\emptyset ]}$ is usually called a joint probability of the random vector ${\displaystyle X}$. The joint probability can be represented as a product of marginal probabilities by the chain rule. ${\displaystyle \Pr[X=(0,0,\ldots ,0)]=\prod _{i=1}^{m}\Pr[X_{i}=0\mid \forall j

Back to the matching problem, note that the event ${\displaystyle X=\emptyset }$ is equivalent to the event that ${\displaystyle \forall i,e_{i}\not \in X}$. Due to the chain rule, we have

{\displaystyle {\begin{aligned}\Pr[X=\emptyset ]&=\Pr[\forall i,e_{i}\not \in X]\\&=\prod _{i=1}^{m}\Pr[e_{i}\not \in X\mid \forall j

For a graph ${\displaystyle G(V,E)}$ and an edge ${\displaystyle e}$, we use

${\displaystyle P_{G}(e)=\Pr[e\in X]}$

to denote the marginal probability of ${\displaystyle e}$ being occupied by a uniformly random matching ${\displaystyle X}$ in ${\displaystyle G}$.

 Observation (self-reduction) Let ${\displaystyle G_{i}=G\setminus \{e_{1},e_{2},\ldots ,e_{i-1}\}}$ be the subgraph of ${\displaystyle G}$ obtained from removing the first ${\displaystyle (i-1)}$ edges from ${\displaystyle G}$. It holds that ${\displaystyle P_{G_{i}}(e_{i})=\Pr[e_{i}\in X\mid \forall j

This observation can be easily proved by the following "telescopic product" version of the chain rule. Note that with the notation ${\displaystyle G_{i}}$ we have ${\displaystyle G_{1}=G}$ and ${\displaystyle G_{m+1}=\emptyset }$ to be the empty graph.

{\displaystyle {\begin{aligned}\Pr[X=\emptyset ]&={\frac {1}{Z(G)}}={\frac {Z(G_{m+1})}{Z(G_{1})}}=\prod _{i=1}^{m}{\frac {Z(G_{i+1})}{Z(G_{i})}}.\end{aligned}}}

By definition of conditional probability, we have

${\displaystyle {\frac {Z(G_{i+1})}{Z(G_{i})}}={\frac {|\{{\text{matchings formed by edges }}e_{i+1},e_{e+2},\ldots ,e_{m}\}|}{|\{{\text{matchings formed by edges }}e_{i},e_{i+1},\ldots ,e_{m}\}|}}=\Pr[e_{i}\not \in X\mid \forall j

On the other hand, it is also obvious that

${\displaystyle {\frac {Z(G_{i+1})}{Z(G_{i})}}={\frac {|\{{\text{matchings in }}G_{i}{\text{ that not contains }}e_{i}\}|}{|\{{\text{matchings in }}G_{i}\}|}}=1-P_{G_{i}}(e_{i}).}$

So the above observation is proved.

## From counting to sampling

With the above observation and recalling that ${\displaystyle Z(G)={\frac {1}{\Pr[X=\emptyset ]}}}$, we have

${\displaystyle Z(G)={\frac {1}{\Pr[X=\emptyset ]}}=\prod _{i=1}^{m}{\frac {1}{(1-P_{G_{i}}(e_{i}))}}.}$

The number of matchings in graph ${\displaystyle G}$, ${\displaystyle Z(G)}$, can be efficiently approximated if the marginal probabilities ${\displaystyle P_{G_{i}}(e_{i})}$ can be efficiently approximated, where the latter can be achieved by doing the statistical experiments by sampling uniform random matchings in graph ${\displaystyle G_{i}}$.

This generic process is described in more details as follows:

1. If we can sample a uniform (or almost uniform, whose meaning to be specified later) random matching from any graph ${\displaystyle G}$ in polynomial time, then due to the estimator theorem, we can have an ${\displaystyle (\epsilon ,\delta )}$-estimator of each ${\displaystyle P_{G_{i}}(e_{i})}$. This estimator runs in time polynomial in ${\displaystyle n=|V|}$, ${\displaystyle {\frac {1}{\epsilon }}}$, and ${\displaystyle \log {\frac {1}{\delta }}}$, and returns an estimation ${\displaystyle {\hat {P}}_{i}}$ for each ${\displaystyle P_{G_{i}}(e_{i})}$ such that ${\displaystyle |{\hat {P}}_{i}-P_{G_{i}}(e_{i})|>\epsilon }$ holds with probability at most ${\displaystyle \delta }$.
2. The above ${\displaystyle (\epsilon ,\delta )}$-estimators are efficient approximations of marginal probabilities with small additive errors. To get an FPRAS for ${\displaystyle Z(G)}$, whose error is multiplicative, we still need that each ${\displaystyle P_{G_{i}}(e_{i})}$ is bounded away from 1. i.e. there is a constant ${\displaystyle c>0}$ such that it always holds that ${\displaystyle P_{G_{i}}(e_{i})\leq 1-c}$. For matching, this is always guaranteed. We have the following lemma.
 Lemma For any graph ${\displaystyle G}$ and edge ${\displaystyle e}$, it always holds that ${\displaystyle P_{G}(e)\leq {\frac {1}{2}}}$.
This lemma holds for quite obvious reasons: If ${\displaystyle e}$ is an isolated edge (with both endpoints of degree 1), then it is covered by an uniformly random matching ${\displaystyle X}$ with probability exactly 1/2. And more edges sharing vertices with ${\displaystyle e}$ can not increase this probability.

With the above lemma, the task of designing a FPRAS for the number of matchings in an input graph is reduced to the task of uniformly sampling a matching in the input graph in polynomial time. In fact, precisely uniform sampling such complicated structure is quite difficult, but there are ways to almost uniform sampling matchings or other complicated structures in polynomial time. This is achieved by random walks in a very large but fast converging Markov chain.