\section{Safety Analysis}
% \urg{talk about maximin and safety margins}
\label{sec:safety}

The work in \cite{miller2020recovery} introduced the concept of `safety margins' that are solvable through peak estimation to certify safety of trajectories. Assume that $X_u = \{x \mid p_i(x) \geq 0, \ i = 1, \ldots, N_u\}$ is a basic semialgebraic set with $N_u$ constraints defining an unsafe set. $X_u$ may be equivalently redefined as $X_u = \{x \mid \min_i p_i(x) \geq 0\}$. If the maximum value of $\min_i p_i(x)$ is negative for all points on trajectories starting from $x_0 \in X_0$ in times $t \in [0, T]$, then all trajectories are certifiably safe. The quantity of a `safety margin' is an upper bound for $\min_i p_i(x) \geq 0$ which may be found through LMI approximations. Finding the safety margin is an instance of maximin optimization, aiming to maximize the minimum of a set of functions. 


A maximin optimization problem may be considered by replacing objectives \eqref{eq:peak_meas_un_obj} or \eqref{eq:peak_meas_un_disc_obj} with,
\begin{subequations}
\begin{align}
    & \textrm{max}_{q \in \R} \quad q  \label{eq:maximin_obj} \\
    & q \leq \inp{p_i}{\mu_p} & i = 1, \ldots, N_u.
    \end{align}
\end{subequations}

The dual formulation introduces variables $\beta \in \R_+^{N_u}$ as nonnegative multipliers. Constraints \eqref{eq:peak_cont_un_p} and \eqref{eq:peak_cont_disc_flow} are then replaced by, 
\begin{equation}
    v \geq \beta^T p(x) = \sum_{i=1}^{N_u} \beta_i p_i(x).
\end{equation}
over the valid region ($[0, T] \times X \times \Theta$ or $X \times \Theta$).

The value $q$ is a lower bound for all of the expectations $\inp{p_i}{\mu_p}$. A negative optimal value of $q$ for any degree of an LMI relaxation is sufficient to certify safety.


An example of a successful safety margin under uncertainty is depicted in Figure \ref{fig:flow_half}. The system under consideration is based on Example 1 of \cite{prajna2004safety}, with dynamics for time-varying $w$,
\begin{equation}
    \dot{x} = f(x, w) = \begin{bmatrix}x_2 \\ -x_1 + \frac{w}{3} x_1^3 - x_2 \end{bmatrix}.
\end{equation}

For trajectories originating in $X_0 = \{x \mid (x_1 - 1.5)^2 + x_2^2 \leq 0.4^2\}$, it is desired to determine if any trajectory reaches the half-circle unsafe set in red $X_u = \{x \mid p_1(x) = x_1^2 - (x_2 + 0.5)^2 \leq 0.25, \ p_2(x) = \frac{\sqrt{2}}{2} (x_1 + x_2 + 0.5) \geq 0$. When $w = 1$ is constant, the 5th-order LMI (relaxation of maximin peak estimation with infinite time) computes a safety margin of $p^*_5 = -0.1417 < 0$ certifying safety of all trajectories. This value is nearly optimal, and the trajectory starting at the blue circle in \ref{fig:flow_half_std} approximately maximizes $\min_i p_i(x)$ as recovered by Alg. 1 of \cite{miller2020recovery}. The black contour is the auxiliary function level set $\{x \mid v(x) = p^*_5\}$, and the red contour is the level set of $\{x \mid \min_i p_i(x) = p^*_5\}$.

The time-varying case where $w \in [0.5, 1.5]$ is shown in Figure \ref{fig:flow_half_b}. The safety margin of $p^*_5 = -0.0784 < 0 $ is computed at the 5th-order LMI relaxation.

\begin{figure}[ht]
     \centering
     \begin{subfigure}[b]{0.48\linewidth}
         \centering
         \includegraphics[width=\linewidth]{flow_half_5pi_4.pdf}
         \caption{$w = 1, \ p_5^* = -0.1417$ }
         \label{fig:flow_half_std}
     \end{subfigure}
     \;
     \begin{subfigure}[b]{0.48\linewidth}
         \centering
         \includegraphics[width=\linewidth]{flow_half_5pi_4_b.pdf}
         \caption{$w \in [-0.5, 1.5], \ p_6^* = -0.0487$}
         \label{fig:flow_half_b}
     \end{subfigure}
      \caption{\label{fig:flow_half} Safety margins on half-circle set}
\end{figure}

% As shown in Theorem IV.1 (equation 20), such a safety margin estimation problem may be bounded by a maximin program 
