# Toward Explainable AutoEncoder-Based Diagnosis of Dynamical Systems
## 4 Gap Distance Metric
### Metrics for Temporal Sequences  
We can extend this notion to temporal sequences of vectors as follows. Assume that we have a system \(\mathcal{S}\) that generates a language \(\mathbb{B}\) consisting of valid behaviors. We define length-\(\lambda\) behaviors as subspaces of equal dimension, which may be represented directly by data matrices. Thus, length-\(\lambda\) behaviors may be identified with points on the Grassmannian \(Gr(k,N)\), i.e., the set of all subspaces of dimension \(k\) in \(\mathbb{R}^{3}\), endowed with the structure of a (quotient) manifold. Proposition 1: _The function \(d\) is a metric on the set of all restricted behaviors \(\xi|_{\lambda}\), with \(\lambda>l\), whenever \(d\) is a metric on \(Gr(m\lambda+n,q\lambda)\)._  
Definition 4 (Gap Function Projection Operator \(P\)): Let \(\mathcal{H}=\mathcal{X}\times\mathcal{Z}\), with \(\mathcal{X}\) and \(\mathcal{Z}\) Hilbert spaces. Let \(P:dom(P)\rightarrow\mathcal{Z}\) and \(\tilde{P}:dom(\tilde{P})\rightarrow\mathcal{Z}\) be closed operators, with \(dom(P)\) and \(dom(\tilde{P})\) being subspaces of \(\mathcal{X}\). The gap between \(P\) and \(\tilde{P}\) is defined as  
\[gap_{\mathcal{H}}(P,\tilde{P})=gap_{\mathcal{H}}(\mathcal{G}(P),\mathcal{G}( \tilde{P})). \tag{8}\]  
Definition 5: The set of all behaviors \(\xi|_{\lambda}\), with \(\lambda>l\), equipped with gap \(\lambda\) is a metric space. The underlying Grassmannian structure enables us to induce metrics between behaviors. We represent the distance between behaviors \(\xi\) and \(\tilde{\xi}\) using the function \(gap_{\lambda}(\xi,\tilde{\xi})\). Dynamical systems projections are viewed as a graph, which is defined as follows:  
Definition 6 (Graph): Let \(T:\mathcal{X}\rightarrow\mathcal{Z}\) be a map between sets. Then the graph \(\mathcal{G}(T)\subseteq\mathcal{X}\times\mathcal{Z}\) is given by \(\mathcal{G}(T)=\left\{(\mathcal{G},T\xi):\xi\in\mathcal{X}\right\}\). If \(T\) is linear then \(\mathcal{G}(T)\) is a linear subspace of \(\mathcal{X}\times\mathcal{Z}\). The gap metric has a well-known geometric interpretation in terms of the sine of the largest principal angle between two subspaces. We define the principal angle as follows. Definition 7 (Principal Angle): Consider two subspaces \(\mathcal{X}\) and \(\mathcal{Z}\) of \(\mathbb{R}^{n}\) with dimensions \(r\) and \(s\) respectively, where \(r\leq s\). The principal angles between \(\mathcal{X}\) and \(\mathcal{Z}\), denoted as \(\theta_{1},\ldots,\theta_{r}\), are defined recursively as follows for \(j=2,\ldots,r\):  
\[\theta_{1} = \min_{\mathbf{x}_{1}\in\mathcal{X},\mathbf{z}_{1}\in\mathcal{Z}} \arccos\left(\frac{\mathbf{x}_{1}^{\top}\mathbf{z}_{1}}{\|\mathbf{x}_{1}\| \|\mathbf{z}_{1}\|}\right),\] \[\vdots\] \[\theta_{j} = \min_{ \begin{subarray}{c}s_{j}\in\mathcal{X},\mathbf{z}_{j}\in \mathcal{Z}\\ \mathbf{y}_{j}\perp\mathbf{z}_{1}\ldots\mathbf{y}_{j-1}\\ \mathbf{z}_{j}\perp\mathbf{z}_{1}\ldots\mathbf{z}_{j-1}\end{subarray} } \arccos\left(\frac{\mathbf{x}_{j}^{\top}\mathbf{z}_{j}}{\|\mathbf{x}_{j}\|\| \mathbf{z}_{j}\|}\right)\]  
The vectors \(\mathbf{x}_{1},\ldots\mathbf{x}_{r}\) and \(\mathbf{z}_{1},\ldots\mathbf{z}_{r}\), are called principal vectors. The dimension of \(\mathcal{X}\cap\mathcal{Z}\) is the multiplicity of zero as a principal angle. It is straightforward to compute the principal angles by calculating the singular values of \(\boldsymbol{X}^{T}\boldsymbol{Z}\), where \(\boldsymbol{X}\) and \(\boldsymbol{Z}\) are orthonormal bases for \(\mathcal{X}\) and \(\mathcal{Z}\), respectively. The singular values of \(\boldsymbol{X}^{T}\boldsymbol{Z}\) are then \(cos\theta_{1},\ldots,cos\theta_{r}\). A principal angle induces several distance metrics on the Grassmann manifold.