Correctness of OnePass started.

This commit is contained in:
Aaron Huber 2020-08-13 20:54:06 -04:00
parent 3e4d42afd5
commit f10b65525b

View file

@ -64,6 +64,7 @@ Let $\expresstree{\smb}$ be the set of all possible polynomial expressions equiv
\subsubsection{Correctness}
\subsubsection{Run-time Analysis}
\subsection{Sampling Algorithm}
@ -74,13 +75,13 @@ The input $\polytree$ can be seen as an expression tree, with leaf nodes being t
\AH{A tree diagram would work much better, but to do that it appears that I need to spend time learning the tikz package, which I haven't had time for yet.}
\textit{Sample} first computes $\abstree(1,\ldots, 1)$ in $O(|\poly|)$ time. Note, that $\abstree(1,\ldots, 1)$ is indeed the sum of coefficients in $\abstree$, or equivalently the number of terms in $\expandtree$.
Algorithm ~\ref{alg:one-pass} performs two important tasks in one pass over expression tree $\polytree$. One of those tasks is to compute $\abstree(1,\ldots, 1)$ in $O(|\poly|)$ time. Note, that $\abstree(1,\ldots, 1)$ is indeed the sum of coefficients in $\abstree$, or equivalently the number of terms in $\expandtree$.
In one pass, \textit{Sample} recursively visits nodes, where it performs the following operations. Given a leaf node, \textit{Sample} stores the coefficient $c_i$ absolute value, denoted $|c_i|$. When \textit{Sample} stops at a $+$ node, it memoizes the probabilities proportional to the partial values of the subtree children of the parent $+$ node. For the case when \textit{Sample} encounters an inner $\times$ node, the partial values of both subtree children are multiplied.
Secondly, algorithm ~\ref{alg:one-pass} recursively visits nodes, where it performs the following operations. Given a leaf node, the algorithm stores the coefficient $c_i$ absolute value, denoted $|c_i|$. When it stops at a $+$ node, it memoizes the probabilities proportional to the partial values of the subtree children of the parent $+$ node. For the case when it encounters an inner $\times$ node, the partial values of both subtree children are multiplied.
For the running example, after the first pass, \textit{Sample} would have learned to sample the two children of the root $+$ node with $P\left(\times\left(+\left(x_1, x_2\right), +\left(x_1, -x_2\right)\right)\right) = \frac{4}{5}$ and $P\left(\times\left(x_2, x_2\right)\right) = \frac{1}{5}$. Similarly, the two inner $+$ nodes of the root's left child, call them $+_1$ and $+_2$, using $l$ for left child and $r$ for right child are $P_{+_1}(l) = P_{+_1}(r) = P_{+_2}(l) = P_{+_2}(r) = \frac{1}{2}$. Note that in this example, the sampling probabilities for the children of each inner $+$ node are equal to one another because both parents have the same number of children, and, in each case, the children of each parent $+$ node share the same $|c_i|$.
For the running example, after the first pass, \cref{alg:one-pass} would have learned to sample the two children of the root $+$ node with $P\left(\times\left(+\left(x_1, x_2\right), +\left(x_1, -x_2\right)\right)\right) = \frac{4}{5}$ and $P\left(\times\left(x_2, x_2\right)\right) = \frac{1}{5}$. Similarly, the two inner $+$ nodes of the root's left child, call them $+_1$ and $+_2$, using $l$ for left child and $r$ for right child are $P_{+_1}(l) = P_{+_1}(r) = P_{+_2}(l) = P_{+_2}(r) = \frac{1}{2}$. Note that in this example, the sampling probabilities for the children of each inner $+$ node are equal to one another because both parents have the same number of children, and, in each case, the children of each parent $+$ node share the same $|c_i|$.
\textit{Sample} then uniformly selects a monomial from $\expandtree$ by the following recursive method. For each leaf node, the monomial is returned with a coefficient reduced to either $\{-1, 1\}$ depending on its sign. For a parent $+$ node, a subtree is chosen over the previously computed weighted sampling distribution. When a parent $\times$ node is visited, the monomials are combined into one monomial. The algorithm concludes outputting $sign(c_i)\cdot\prob^{d_i}$.
Algorithm ~\ref{alg:sample} then uniformly selects a monomial from $\expandtree$ by the following recursive method. For each leaf node, the monomial is returned with a coefficient reduced to either $\{-1, 1\}$ depending on its sign. For a parent $+$ node, a subtree is chosen over the previously computed weighted sampling distribution. When a parent $\times$ node is visited, the monomials are combined into one monomial. The algorithm concludes outputting $sign(c_i)\cdot\prob^{d_i}$.
\subsubsection{Psuedo Code}
@ -89,19 +90,19 @@ For the running example, after the first pass, \textit{Sample} would have learne
\label{alg:one-pass}
\begin{algorithmic}[1]
\State $acc \gets 0$
\If{$\polytree.head.val == "+"$}
\If{$\polytree.head.val = "+"$}
\For{$child$ in $T.children$}
\State $acc = acc + OnePass(child)$
\State $acc \gets acc + OnePass(child)$
\EndFor
\State $T.partial \gets acc$
\For{$child$ in $T.children$}
\State $child.weight = \frac{child.c_i}{T.partial}$
\State $child.weight \gets \frac{child.c_i}{T.partial}$
\EndFor
\State Return $T.partial$
\ElsIf{$T.head.val == "\times"$}
\ElsIf{$T.head.val = "\times"$}
\State $acc \gets 1$
\For{$child$ in $T.children$}
\State $acc = acc \times= OnePass(child)$
\State $acc \gets acc \times OnePass(child)$
\EndFor
\State $T.partial \gets acc$
\Else
@ -114,15 +115,15 @@ For the running example, after the first pass, \textit{Sample} would have learne
\caption{Sample($\polytree$)}
\label{alg:sample}
\begin{algorithmic}[1]
\If{$T.head.val == "+":$}
\If{$T.head.val = "+":$}
\State $T_{samp} \gets$ WeightedSample($T.children$, $T.weights$)
\State $Sample(T_{samp})$
\ElsIf{$T.head.val == "\times":$}
\State $c_i = 1$
\ElsIf{$T.head.val = "\times":$}
\State $c_i \gets 1$
\State $monom \gets ""$
\For {$child$ in $T.children:$}
\State $monom = monom ++ Sample(child)\_1$
\State $c_i = c_i \times Sample(child)\_2$
\State $c_i \gets c_i \times Sample(child)\_2$
\EndFor
\Else
\State Return $(T.head.val, 1 \times T.c_i)$
@ -130,11 +131,11 @@ For the running example, after the first pass, \textit{Sample} would have learne
\end{algorithmic}
\end{algorithm}
\subsubsection{Correctness}
\subsubsection{Correctness of Algorithm ~\ref{alg:one-pass}}
The algorithm begins with recursively visiting the root node and all of its children, until it reaches all leaves. Thus, every node is visited. When going from the bottom up, it is the case for a parent node $+$ that the algorithm records the sum of its children's coefficient values, and produces a weighted distribution based on the partial values. This weighted distribution across each child subtree is in exact proportion to the probability of choosing either child given that it's parent's subtree was chosen. Consider the base case, when we have $n$ leaf nodes whose parent (root) node is $+$. For each $|c_i|$, it is the case that $\frac{|c_i|}{\sum_{i \in [n]}|c_i|}$ is exactly the uniform distribution of $\expandtree$.
When a $\times$ node is visited, \cref{alg:one-pass} takes the product of each of its children. Note that this is correct, since it is the case that a product of polynomials has a sum of coefficients equal to the product of the sum of each polynomial's coefficients.
Note that for the case of a $+$ subtree of a parent $\times$ node, when the parent node passes its partial sum up to it's parent node, it is the case that the subtrees of the $+$ node probabilities are exactly the proportion of the parent's parent node.
\subsubsection{Run-time Analysis}
%Next, \textit{Sample} uses that information to compute a weighted sampling distribution over $\polytree$ that is the equivalent to sampling uniformly over $\expandtree$. In the recent example, setting $S_1 = +\left(x_1, x_2\right)$, $S_2 = +\left(x_1, -x_2\right)$, and $S_3 = \times\left(x_2, x_2\right)$, it is the case that $S_1(x_1) = 1$, $S_1(x_2) = 1$, $S_2(x_1) = 1$, $S_2(x_2) = 1$, and $S_3(x_2) = 1$, where we overload notation to also view $S_i$ as a function whose input is the set of variables, and whose output is the coefficient for the input variable of $S_i$. The weighted distribution for each monomial term $M$ in $\expandtree$ is then $\frac{S_i(x_j)}{\abstree(1,\ldots, 1)}$.
%
%Sample then uses that weighted distribution to sample
%
%When \textit{Sample} encounters a $+$ node, it \textit{chooses} one of the subtree children with probability proportional to the partial values of the subtree children. For the case that \textit{Sample} encounters a $\times$ node, both subtree partial values are multiplied