Gaussian supposedly finished.

This commit is contained in:
2026-07-02 23:40:57 +03:00
parent fa20fe0eb7
commit e57efde395
+68 -17
View File
@@ -348,15 +348,15 @@ Firstly, we must estimate the parameters to fit the gaussian distributions
to the true and false populations of the score output. to the true and false populations of the score output.
\begin{equation} \begin{equation}
\mu_0 = \frac{\sum_{\mathbf{x}_0 \in \mathcal{B}_0}{f_{\theta}(\mathbf{x}_0)}}{|\mathcal{B}_0|} \mu_0 = \frac{1}{|\mathcal{B}_0|}\sum_{\mathbf{x}_j \in \mathcal{B}_0}{f_{\theta}(\mathbf{x}_j)}
\qquad \qquad
\mu_1 = \frac{\sum_{\mathbf{x}_0 \in \mathcal{B}_1}{f_{\theta}(\mathbf{x}_1)}}{|\mathcal{B}_1|} \mu_1 = \frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_j \in \mathcal{B}_1}{f_{\theta}(\mathbf{x}_j)}
\end{equation} \end{equation}
\begin{equation} \begin{equation}
\sigma_0^2 = \frac{1}{|\mathcal{B}_0|}\sum_{\mathbf{x}_0 \in \mathcal{B}_0}(\mathbf{x}_0 - \mu_0)^2 \sigma_0^2 = \frac{1}{|\mathcal{B}_0|}\sum_{\mathbf{x}_j \in \mathcal{B}_0}(f_\theta(\mathbf{x}_j) - \mu_0)^2
\qquad \qquad
\sigma_1^2 = \frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_1 \in \mathcal{B}_1}(\mathbf{x}_1 - \mu_1)^2 \sigma_1^2 = \frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_j \in \mathcal{B}_1}(f_\theta(\mathbf{x}_j) - \mu_1)^2
\end{equation} \end{equation}
Once these parameters are estimated, we can then define our ROLL objective as required. Once these parameters are estimated, we can then define our ROLL objective as required.
@@ -369,7 +369,11 @@ and its inverse (quantile function) is:
\[ \[
F^{-1}(p) = \mu + \sigma\sqrt{2}\,\text{ierf}(2p - 1) F^{-1}(p) = \mu + \sigma\sqrt{2}\,\text{ierf}(2p - 1)
\] \]
where TODO EXPLAIN ERF AND IERF. Plugging into the general ROLL framework, the threshold where $\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t$ is the Gauss
error function and $\operatorname{ierf}$ denotes its functional inverse, satisfying
$\operatorname{ierf}(\operatorname{erf}(x)) = x$. Neither has a closed form, but both are
available in standard numerical libraries (e.g.\ \texttt{torch.erf} and
\texttt{torch.erfinv}). Plugging into the general ROLL framework, the threshold
$\tau$ achieving FPR $= \alpha$ and the resulting loss are: $\tau$ achieving FPR $= \alpha$ and the resulting loss are:
\begin{align} \begin{align}
\tau &= \mu_0 + \sigma_0\sqrt{2}\,\text{ierf}(2\alpha - 1) \nonumber \\ \tau &= \mu_0 + \sigma_0\sqrt{2}\,\text{ierf}(2\alpha - 1) \nonumber \\
@@ -395,7 +399,7 @@ Firstly, for $y_i = 1$.
\] \]
\[ \[
= \frac{1}{\sqrt{2}\sigma_1}\exp\left(\frac{\hat{F}_0^{-1}(\alpha) - \mu_1}{\sqrt{2}\sigma_1}\right) = -\frac{1}{\sigma_1\sqrt{2\pi}}\exp\!\left(-\frac{(\hat{F}_0^{-1}(\alpha) - \mu_1)^2}{2\sigma_1^2}\right)
\] \]
\[ \[
@@ -408,28 +412,75 @@ Firstly, for $y_i = 1$.
\] \]
\[ \[
= \frac{\hat{F}_0^{-1}(\alpha)-\mu_1}{\sqrt{2\pi}\sigma_1^2} \exp\left( - {\left( \frac{\hat{F}_0^{-1}(\alpha) - \mu_1}{\sqrt{2}\sigma_1} \right)}^2 \right) = \frac{\mu_1 - \hat{F}_0^{-1}(\alpha)}{\sigma_1^2\sqrt{2\pi}} \exp\!\left( -\frac{(\hat{F}_0^{-1}(\alpha) - \mu_1)^2}{2\sigma_1^2} \right)
\] \]
\[ \[
\frac{\partial \sigma_1}{\partial f_{\theta}(\mathbf{x}_i)} = \frac{\mathbf{x}_i - \mu_1}{|\mathcal{B}_1| \sigma_1} \frac{\partial \sigma_1}{\partial f_{\theta}(\mathbf{x}_i)} = \frac{f_\theta(\mathbf{x}_i) - \mu_1}{|\mathcal{B}_1| \sigma_1}
\] \]
The final derivation, given $y_i = 1$, is given as: Combining via the chain rule, the gradient for $y_i = 1$ is:
\begin{equation}
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial f_{\theta}(\mathbf{x}_i)}
= -\frac{1}{|\mathcal{B}_1|\,\sigma_1\sqrt{2\pi}}
\exp\!\left(-\frac{(\tau - \mu_1)^2}{2\sigma_1^2}\right)
\left(1 + \frac{(\tau - \mu_1)(f_\theta(\mathbf{x}_i) - \mu_1)}{\sigma_1^2}\right)
\label{eq:gauss-grad-y1}
\end{equation}
where $\tau = \hat{F}_0^{-1}(\alpha) = \mu_0 + \sigma_0\sqrt{2}\,\operatorname{ierf}(2\alpha - 1)$.
Given $y_i = 0$, the score $f_\theta(\mathbf{x}_i)$ affects the loss only through the threshold
$\tau = \hat{F}_0^{-1}(\alpha)$, which depends on $\mu_0$ and $\sigma_0$.
Applying the chain rule:
\[ \[
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial f_{\theta}(\mathbf{x}_i)} = \dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial \mu_1} \cdot \frac{1}{|\mathcal{B}_1|} + \frac{\partial \hat{F}_1(\tau)}{\partial f_\theta(\mathbf{x}_i)}
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial \sigma_1} \cdot \frac{\partial \sigma_1}{\partial f_{\theta}(\mathbf{x}_i)} = \frac{\mathbf{x}_i - \mu_1}{|\mathcal{B}_1| \sigma_1} = \underbrace{\frac{\partial \hat{F}_1(\tau)}{\partial \tau}}_{\text{PDF of class 1 at }\tau}
\cdot \frac{\partial \tau}{\partial f_\theta(\mathbf{x}_i)}
\] \]
TODO - above equation is based off of mathexchange derivation. Link and reference properly! The first factor is the Gaussian PDF evaluated at $\tau$:
Given $y_i = 0$ we first must derive:
\[ \[
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial \hat{F}_0^{-1}(\alpha)} = \frac{1}{\sqrt{2 \pi \sigma_1^2}}\exp\left( -{\left( \frac{\hat{F}_0^{-1}(\alpha) - \mu_1}{2\sigma_1^2}\right)}^2\right) \frac{\partial \hat{F}_1(\tau)}{\partial \tau}
= \frac{1}{\sigma_1\sqrt{2\pi}}\exp\!\left(-\frac{(\tau - \mu_1)^2}{2\sigma_1^2}\right)
\] \]
TODO finish somehow - later! For the second factor, $\tau = \mu_0 + \sigma_0\sqrt{2}\,\operatorname{ierf}(2\alpha - 1)$, so
$\frac{\partial \tau}{\partial \mu_0} = 1$ and
$\frac{\partial \tau}{\partial \sigma_0} = \sqrt{2}\,\operatorname{ierf}(2\alpha - 1) = \frac{\tau - \mu_0}{\sigma_0}$.
Combined with $\frac{\partial \mu_0}{\partial f_\theta(\mathbf{x}_i)} = \frac{1}{|\mathcal{B}_0|}$ and
$\frac{\partial \sigma_0}{\partial f_\theta(\mathbf{x}_i)} = \frac{f_\theta(\mathbf{x}_i) - \mu_0}{|\mathcal{B}_0|\sigma_0}$:
\[
\frac{\partial \tau}{\partial f_\theta(\mathbf{x}_i)}
= \frac{1}{|\mathcal{B}_0|}\left(1 + \frac{(\tau - \mu_0)(f_\theta(\mathbf{x}_i) - \mu_0)}{\sigma_0^2}\right)
\]
Therefore, the gradient for $y_i = 0$ is:
\begin{equation}
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(\alpha))}{\partial f_\theta(\mathbf{x}_i)}
= \frac{1}{|\mathcal{B}_0|\,\sigma_1\sqrt{2\pi}}
\exp\!\left(-\frac{(\tau - \mu_1)^2}{2\sigma_1^2}\right)
\left(1 + \frac{(\tau - \mu_0)(f_\theta(\mathbf{x}_i) - \mu_0)}{\sigma_0^2}\right)
\label{eq:gauss-grad-y0}
\end{equation}
\paragraph{Combined gradient.}
Substituting \Cref{eq:gauss-grad-y1} and \Cref{eq:gauss-grad-y0} into \Cref{eq:roll-gradient},
and letting $\tau = \hat{F}_0^{-1}(\alpha) = \mu_0 + \sigma_0\sqrt{2}\,\operatorname{ierf}(2\alpha-1)$:
\begin{equation}
\frac{\partial \mathcal{L}_{\text{ROLL-TPR@FPR}}^{\text{GAUSSIAN}}}{\partial f_\theta(\mathbf{x}_i)} =
\begin{cases}
\dfrac{-1}{|\mathcal{B}_1|\,\sigma_1\sqrt{2\pi}}
\exp\!\left(-\dfrac{(\tau-\mu_1)^2}{2\sigma_1^2}\right)
\!\left(1 + \dfrac{(\tau-\mu_1)(f_\theta(\mathbf{x}_i)-\mu_1)}{\sigma_1^2}\right)
& \text{if } y_i = 1 \\[18pt]
\dfrac{1}{|\mathcal{B}_0|\,\sigma_1\sqrt{2\pi}}
\exp\!\left(-\dfrac{(\tau-\mu_1)^2}{2\sigma_1^2}\right)
\!\left(1 + \dfrac{(\tau-\mu_0)(f_\theta(\mathbf{x}_i)-\mu_0)}{\sigma_0^2}\right)
& \text{if } y_i = 0
\end{cases}
\label{eq:gauss-grad-combined}
\end{equation}
\subsection{Beta ROLL} \subsection{Beta ROLL}
\label{sec:roll-beta} \label{sec:roll-beta}