# Resampling Conserves Redundancy & Mediation (Approximately) Under the Jensen-Shannon Divergence

Around two months ago, John and I published [Resampling Conserves Redundancy (Approximately)](https://www.lesswrong.com/posts/cpxxfagD92ivLZCp4/resampling-conserves-redundancy-approximately?utm_campaign=post_share&utm_source=link). Fortunately, about two weeks ago, Jeremy Gillen and Alfred Harwood showed us that [we were wrong](https://www.lesswrong.com/posts/cpxxfagD92ivLZCp4?commentId=L6dpFjFmgpEtNuycc).  
  
This proof achieves, using the [Jensen-Shannon divergence](https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence) ("JS"), what the previous one failed to show using [KL divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence) ("$D_{KL}$"). In fact, while the previous attempt tried to show only that redundancy is conserved (in terms of $D_{KL}$) upon resampling latents, this proof shows that the redundancy *and* mediation conditions are conserved (in terms of JS).

Why Jensen-Shannon?
===================

In just about all of our previous work, we have used $D_{KL}$ as our factorization error. (The error meant to capture the extent to which a given distribution fails to factor according to some graphical structure.) In this post I use the Jensen Shannon divergence.

$D_{KL}(U||V) := \mathbb{E}_{U}ln\frac{U}{V}$

$JS(U||V) := \frac{1}{2}D_{KL}\left(U||\frac{U+V}{2}\right) + \frac{1}{2}D_{KL}\left(V||\frac{U+V}{2}\right)$

The KL divergence is a pretty fundamental quantity in information theory, and is used all over the place. (JS is usually defined in terms of $D_{KL}$, as above.) We have pretty strong intuitions about what $D_{KL}$ means and it has lots of nice properties which I won't go into detail about, but we have considered it a strong default when trying to quantify the extent to which two distributions differ.  
  
The JS divergence looks somewhat ad-hoc by comparison. It also has some nice mathematical properties (its square root is a metric, a feature sorely lacking from $D_{KL}$) and there is *some* reason to like it intuitively: $JS(U||V)$ is equivalent to the mutual information between X, a variable randomly sampled from one of the distributions, and Z, an indicator which determines the distribution X gets sampled from. So in this sense it captures the extent to which a sample distinguishes between the two distributions.

Ultimately, though, we want a more solid justification for our choice of error function going forward. 

*This* proof works, but it uses JS rather than $D_{KL}$. Is that a problem? Can/Should we switch everything over to JS? We aren't sure. Some of our focus for immediate next steps is going to be on how to better determine the "right" error function for comparing distributions for the purpose of working with (natural) latents.  
  
And now, for the proof:

Definitions
===========

Let $P$ be any distribution over $X$ and $\Lambda$.

I will omit the subscripts if the distribution at hand is the full joint distribution with all variables unbound. I.e. $P_{X, \Lambda}$ is the same as P. When variables are bound, they will be written as lower case in the subscript. When this is still ambiguous, the full bracket notation will be used.

First, define auxiliary distributions $Q$, $S$, $R$, and $M$:

$Q := P_XP_{\Lambda|X_1}$,  $S := P_XP_{\Lambda|X_2}$,   $R := P_XQ_{\Lambda|X_2} = P_X\sum_{X_1}\left[P_{X_1|X_2}P_{\Lambda|X_1}\right]$,   $M := P_{\Lambda}P_{X_1|\Lambda}P_{X_2|\Lambda}$

Q, S, and M each perfectly satisfy one of the (stochastic) Natural Latent conditions, with Q and S each satisfying one of the redundancy conditions ($X_2 \to X_1 \to \Lambda$, and $X_1 \to X_2 \to \Lambda$, respectively,) and M satisfying the mediation condition ($X_1 \leftarrow \Lambda \rightarrow X_2$).

R represents the distribution when *both* of the redundancy factorizations are applied in series to P.

Let $\Gamma$ be a latent variable defined by $P[\Gamma = \gamma|X] := P[\Lambda = \gamma|X_1] = P[\Gamma=\gamma|X_1]$, with $P^{\Gamma} := P_{X,\Lambda}P_{\Gamma|X}$

Now, define the auxiliary distributions $Q^{\Gamma}$, $S^{\Gamma}$, and $M^{\Gamma}$, similarly as above, and show some useful relationships to P, Q, S, R, and M:

$Q^{\Gamma}_{X, \gamma} := P_XP^{\Gamma}_{\gamma|X_1} = P_XQ[\Lambda=\gamma|X_1] = Q[X, \Lambda=\gamma]$$S^\Gamma_{X,\gamma} := P_XP^{\Gamma}_{\gamma|X_2} = P_X\sum_{X_1}\left(P_{X_1|X_2}P_{\gamma|X_1}\right) = R[X, \Lambda=\gamma]$, $M^{\Gamma}_{X, \gamma} := P^{\Gamma}_{\gamma}P^{\Gamma}_{X_1|\gamma}P^{\Gamma}_{X_2|\Gamma} = P[\Lambda=\gamma]P[X_1|\Lambda=\gamma]R[X_2|\Lambda=\gamma]$

$P^{\Gamma}_{X, \gamma} = P_XP_{\gamma|X} = Q[X, \Lambda=\gamma]$                                     $P^{\Gamma}_{\gamma} = Q[\Lambda=\gamma] = P[\Lambda=\gamma] = P^{\Gamma}[\Lambda=\gamma]$                                                                              $P^{\Gamma}_{X_1|\gamma} = P[X_1|\Lambda=\gamma] = Q[X_1|\Lambda=\gamma]$                                                                                           $P^{\Gamma}_{X_2|\gamma} = \frac{R[X_2,\Lambda=\gamma]}{P^{\Gamma}_{\gamma}} = R[X_2|\Lambda=\gamma]$

Next, the error metric and the errors of interest:

Jensen-Shannon Divergence, and Jensen-Shannon Distance (a true metric): 

$JS(U||V) := \frac{1}{2}D_{KL}\left(U||\frac{U+V}{2}\right) + \frac{1}{2}D_{KL}\left(V||\frac{U+V}{2}\right)$   

$\delta\left(U,V\right) := \sqrt{JS\left(U||V\right)} = \delta(V,U)$

$\epsilon_1 := JS(P||Q), \epsilon_2 := JS(P||S), \epsilon_{med} := JS(P||M)$

$\epsilon^{\Gamma}_1 := JS(P^{\Gamma}||Q^{\Gamma}), \epsilon^{\Gamma}_2 := JS(P^{\Gamma}||S^{\Gamma}) = JS(Q||R), \epsilon^{\Gamma}_{med} := JS(P^{\Gamma}||M^{\Gamma}) = JS(Q||M^{\Gamma})$

Theorem
=======

Finally, the theorem:

For any distribution $P$ over ($X$, $\Lambda$), the latent $\Gamma \sim P[\Lambda|X_i]$ has redundancy error of zero on one of it's factorizations, while the other factorization errors are bounded by small factor of the errors induced by $\Lambda$. More formally:

$\forall P[X, \Lambda]$, the latent $\Gamma$ defined by $P[\Gamma=\gamma|X] := P[\Lambda|X_1]$ has bounded factorization errors $\epsilon^{\Gamma}_1 = 0$ and $max(\epsilon^{\Gamma}_2, \epsilon^{\Gamma}_{med}) \leq 5(\epsilon_1 +\epsilon_2 + \epsilon_{med})$.  
  
In fact, that is a simpler but looser bound than that proven below which achieves the more bespoke bounds of: $\epsilon^{\Gamma}_1 = 0$, $\epsilon^{\Gamma}_2 \leq (2\sqrt{\epsilon_1}+\sqrt{\epsilon_2})^2$, and $\epsilon^{\Gamma}_{med} \leq (2\sqrt{\epsilon_1} + \sqrt{\epsilon_{med}})^2$.

Proof
=====

(1) $\epsilon^{\Gamma}_1 = 0$ 
------------------------------

### Proof of (1)

$JS(P^{\Gamma}||Q^{\Gamma}) = 0$, since $P^{\Gamma}_{X,\gamma} = Q[X,\Lambda=\gamma] = Q^{\Gamma}_{X,\gamma}$ and $P^{\Gamma}_{\Lambda|X} = P_{\Lambda|X}$  

                                                                                                                                                          $\blacksquare$

(2) $\epsilon^{\Gamma}_2 \leq (2\sqrt{\epsilon_1}+\sqrt{\epsilon_2})^2$
-----------------------------------------------------------------------

### Lemma 1: $JS(S||R) \leq \epsilon_1$

$S[\Lambda|X_2] = P[\Lambda|X_2] = \sum_{X_1}P[X_1|X_2]P[\Lambda|X]$

$R[\Lambda|X_2] = Q[\Lambda|X_2] = \sum_{X_1}P[X_1|X_2]P[\Lambda|X_1]$

$\begin{align} JS(S||R) &= \sum_{X_2}JS(S_{\Lambda|X_2}||R_{\Lambda|X_2}) &\ &\leq \sum_{X}P[X_2]P[X_1|X_2]JS(P_{\Lambda|X}||P[\Lambda|X_1]) &\ &= JS(P||Q) &\ &=: \epsilon_1 \end{align}$[^amo1dzgulpe]

### Lemma 2: $\delta(Q,R) \leq \sqrt{\epsilon_1} + \sqrt{\epsilon_2}$

$\text{Let }d_x := \delta(P_{\Lambda|x_1},P_{\Lambda|x_2}), a_x := \delta(P_{\Lambda|x},P_{\Lambda|x_1})\text{, and }b_x := \delta(P_{\Lambda|x},P_{\Lambda|x_2})$

$\begin{align} \delta(Q,S) = \sqrt{JS(Q,S)} &= \sqrt{\mathbb{E}_{P_X}JS(P_{\Lambda|X_1}||P_{\Lambda|X_2})} &\ &= \sqrt{\mathbb{E}_{P_X}(d_X)^2} &\ &\leq \sqrt{\mathbb{E}_{P_X}(a_X + b_X)^2} \text{ by the triangle inequality of metric }\delta &\ &\leq \sqrt{\mathbb{E}_{P_X}(a_X)^2} + \sqrt{\mathbb{E}_{P_X}(b_X)^2} \text{ via the Minkowski Ineqality} &\ &= \sqrt{JS(P||Q)} + \sqrt{JS(P||S)} = \sqrt{\epsilon_1} + \sqrt{\epsilon_2} \end{align}$    

### Proof of (2)

$\sqrt{\epsilon^{\Gamma}_2} = \sqrt{JS(P^{\Gamma}||S^{\Gamma})} = \sqrt{JS(Q||R)} =: \delta(Q,R)$

$\begin{align} \delta(Q,R) &\leq \delta(Q,S) + \delta(S,R)  \text{ by the triangle inequality of metric }\delta &\ &\leq \delta(Q,R) + \sqrt{\epsilon_1} \text{ by Lemma 1} &\ &\leq 2\sqrt{\epsilon_1} + \sqrt{\epsilon_2} \text{ by Lemma 2} \end{align}$

                                                                                                                                                          $\blacksquare$

(3) $\epsilon^{\Gamma}_{med} \leq (2\sqrt{\epsilon_1} + \sqrt{\epsilon_{med}})^2$
---------------------------------------------------------------------------------

### Proof of (3)

$\begin{aligned} JS(M||M^{\Gamma}) &= \sum_{\gamma}P[\Lambda=\gamma]JS(P[X_2|\Lambda=\gamma]||R[X_2|\Lambda=\gamma]) &\ &= \mathbb{E}_{P_{\Lambda}}JS(S_{X_2|\Lambda}||R_{X_2|\Lambda}) &\ &\leq JS(S||R) \text{ by the Data Processing Inequality } \end{aligned}$

$\begin{aligned} \sqrt{\epsilon^{\Gamma}_{med}} &= \delta(P^{\Gamma},M^{\Gamma}) &\ &= \delta(Q,M^{\Gamma}) &\ &\leq \delta(Q,P) + \delta(P,M) + \delta(M,M^{\Gamma}) \text{ by the triangle inequality of metric }\delta &\ &= \sqrt{\epsilon_1} + \sqrt{\epsilon_{med}} + \sqrt{JS(M,M^{\Gamma})} &\ &\leq \sqrt{\epsilon_1} + \sqrt{\epsilon_{med}} + \sqrt{JS(M,M^{\Gamma})} &\ &\leq 2\sqrt{\epsilon_1} +\sqrt{\epsilon_{med}} \text{ by Lemma 1 } \end{aligned}$

                                                                                                                                                        $\blacksquare$

Results
=======

So, as shown above, (using Jensen-Shannon Divergence as the error function,) resampling any latent variable according to either one of its redundancy diagrams (just swap $\epsilon_1$ and $\epsilon_2$ for the bounds when resampling from $X_2$) produces a new latent variable which satisfies the redundancy and mediation diagrams approximately as well as the original, and satisfies one of the redundancy diagrams perfectly.  
  
The bounds are:  
$\begin{align}&\epsilon^{\Gamma}_1 = 0 &\ &\epsilon^{\Gamma}_2 \leq (2\sqrt{\epsilon_1}+\sqrt{\epsilon_2})^2 &\ &\epsilon^{\Gamma}_{med} \leq (2\sqrt{\epsilon_1} + \sqrt{\epsilon_{med}})^2\end{align}$

Where the epsilons without superscripts are the errors corresponding to factorization via the respective naturality conditions of the original latent $\Lambda$ and X.

###   
Bonus

For $a,b \gt 0$, $(2\sqrt{a} + \sqrt{b})^2 \leq 5(a+b)$ by Cauchy-Schwartz with vectors $[2, 1], [\sqrt{a}, \sqrt{b}]$Thus the simpler, though looser, bound: $max\{\epsilon^{\Gamma}_1, \epsilon^{\Gamma}_2, \epsilon^{\Gamma}_{med}\} \leq 5(\epsilon_1 + \epsilon_2 + \epsilon_{med})$

\[Edit: [Here is a collab session](https://colab.research.google.com/drive/1dxAnNge_F8LdmtNDoXk0VcdVgnD-tew7?usp=sharing) where I numerically tested the bounds on a system with 3 binary variables, both with random sampling and with a simple gradient ascent test aiming to break the bound. All numerical checks passed.\]

[^amo1dzgulpe]: The joint convexity of \(JS(U||V)\), which justifies this inequality, is inherited from the joint convexity of KL Divergence.