# Learning Kernel Stein Discrepancy for Training Energy-Based Models
## 3 Maximum Mean Discrepancy and Kernel Stein Discrepancy
### Kernel Stein Discrepancy: A Goodness-of-Fit Test Method  
In Formula (2), \(S_{q}(x)=\
abla_{x}\log q(x)\) is also named a Stein score function. The condition is that \(q(x)\) is a continuous and differentiable density function and that the derivative of \(q(x)\) exists. In [25], a continuous differentiable function \(f:\chi\to R\) is defined in the Stein class of \(p\) when it satisfies the following:  
\[\int\limits_{x\in X}\left(f(x)p(x)\right)dx=0 \tag{5}\]  
When \(F(x)=[f_{1}(x),\  
Inspired by the equation \(E_{p}\big{[}A_{q}f(x)\big{]}=E_{p}\big{[}\big{(}s_{q}(x)-s_{p}(x)\big{)}f(x)^{T} \big{]}\), Kernelized Stein Discrepancy (KSD) [24; 25] is proposed to measure the distance between the two distributions. KSD is denoted as follows:  
\[S(q,p)=E_{p}\Big{[}\big{(}s_{q}\big{(}y_{j}\big{)}-s_{p}\big{(}y_{j}\big{)} \big{)}^{T}k\Big{(}y_{j},y^{\prime}_{j}\Big{)}\Big{(}s_{q}\Big{(}y^{\prime}_{ j}\Big{)}-s_{p}\Big{(}y^{\prime}_{j}\Big{)}\Big{)}\Big{]} \tag{8}\]  
In the above equation, \(s_{q}(y_{i})\) and \(s_{p}\big{(}y_{j}\big{)}\) are the score function of \(q\) and \(p\); they are unknown in most unnormalized models, and we only have their samples in hand. In [25], the author eliminated the explicit expression of \(p\) by applying the constraints of the Stein class. Kernel function \(k\Big{(}y_{j},\ y^{\prime}_{j}\Big{)}\) is a special continuous differentiable function \(f\). And, it is easy to prove that if \(k\Big{(}y_{j},\ y^{\prime}_{j}\Big{)}\) has continuous second-order partial derivatives, \(k\Big{(}y_{j},\ y^{\prime}_{j}\Big{)}\) is in the Stein class of \(p\). In other words, \(k\Big{(}y_{j},\ y^{\prime}_{j}\Big{)}\) meets the condition in Equation (5). Then:  
\[E_{p}\Big{[}k\Big{(}y_{j},y^{\prime}_{j}\Big{)}\Big{(}s_{q}\Big{(}y^{\prime}_{ j}\Big{)}-s_{p}\Big{(}y^{\prime}_{j}\Big{)}\Big{)}\Big{]}=E_{p}\Big{[}k\Big{(}y_{j},y^{ \prime}_{j}\Big{)}s_{q}\Big{(}y^{\prime}_{j}\Big{)}+\
abla_{y^{\prime}_{j}}k \Big{(}y_{j},y^{\prime}_{j}\Big{)}\Big{]} \tag{9}\]  
Let us plug Formula (9) into Formula (8) and simplify:  
\[\begin{split} S(q,p)&=E_{p}\big{[}u_{q}(y,y^{\prime })\big{]}\\ u_{q}(y,y^{\prime})&=s_{q}\big{(}y_{j}\big{)}^{T}k \Big{(}y_{j},y^{\prime}_{j}\Big{)}s_{q}\Big{(}y^{\prime}_{j}\Big{)}+s_{q} \big{(}y_{j}\big{)}^{T}\
abla_{y^{\prime}_{j}}k\Big{(}y_{j},y^{\prime}_{j} \Big{)}\\ &\quad+\
abla_{y_{j}}k\Big{(}y_{j},y^{\prime}_{j}\Big{)}s_{q} \Big{(}y^{\prime}_{j}\Big{)}+trace\Big{(}\
abla_{y_{j},y^{\prime}_{j}}k\Big{(} y_{j},y^{\prime}_{j}\Big{)}\Big{)}\end{split} \tag{10}\]  
We can compute \(S(q,\,p)\) without the density distribution \(p\) and only need score function \(s_{q}(y_{i})\) of the density distribution \(q\). And, when \(k\Big{(}y_{j},\ y^{\prime}_{j}\Big{)}\) is a strictly positive definite kernel, it is easy to demonstrate that \(p=q\), when and only when \(S(q,\ p)=0\). In [25], KSD is applied to bootstrap goodness-of-fit testing to measure differences between two probability distributions. That is, we can calculate the KSD between the density distribution \(q\) and samples from \(p\) to determine whether \(q\) equals \(p\).