Thesis - wip

This commit is contained in:
2026-07-15 20:58:17 +03:00
parent ce726d938f
commit a85bb2a551
13 changed files with 963 additions and 399 deletions
+68
View File
@@ -0,0 +1,68 @@
We derive the gradient of \eqref{eq:roll-tpr-at-fpr-gaussian} with respect to
$f_\theta(\mathbf{x}_i)$, instantiating the general structure~\eqref{eq:roll-gradient}
for the Gaussian case. Throughout, $\tau = \hat{F}_0^{-1}(1-\alpha) =
\mu_0 + \sigma_0\sqrt{2}\,\operatorname{ierf}(1-2\alpha)$.
\paragraph{Case $y_i = 1$.}
The score $f_\theta(\mathbf{x}_i)$ enters through $\mu_1$ and $\sigma_1$.
Differentiating $\hat{F}_1(\tau) = \frac{1}{2}\bigl[1+\operatorname{erf}\!\bigl(\frac{\tau-\mu_1}{\sigma_1\sqrt{2}}\bigr)\bigr]$:
\[
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(1-\alpha))}{\partial \mu_1} =
-\frac{1}{\sigma_1\sqrt{2\pi}}\exp\!\left(-\frac{(\tau - \mu_1)^2}{2\sigma_1^2}\right)
\]
\[
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(1-\alpha))}{\partial \sigma_1} =
\frac{\mu_1 - \tau}{\sigma_1^2\sqrt{2\pi}}\exp\!\left(-\frac{(\tau - \mu_1)^2}{2\sigma_1^2}\right)
\]
Combined with $\frac{\partial \mu_1}{\partial f_\theta(\mathbf{x}_i)} = \frac{1}{|\mathcal{B}_1|}$
and $\frac{\partial \sigma_1}{\partial f_\theta(\mathbf{x}_i)} = \frac{f_\theta(\mathbf{x}_i)-\mu_1}{|\mathcal{B}_1|\sigma_1}$,
the chain rule gives:
\begin{equation}
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(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}
\paragraph{Case $y_i = 0$.}
The score $f_\theta(\mathbf{x}_i)$ affects the loss only through $\tau$. By the chain rule:
\[
\frac{\partial \hat{F}_1(\tau)}{\partial f_\theta(\mathbf{x}_i)}
= \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)}
\]
The first factor is the Gaussian PDF at $\tau$:
\[
\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)
\]
For the second factor, since $\tau = \mu_0 + \sigma_0\sqrt{2}\,\operatorname{ierf}(1-2\alpha)$:
$\frac{\partial\tau}{\partial\mu_0}=1$ and
$\frac{\partial\tau}{\partial\sigma_0}=\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:
\begin{equation}
\dfrac{\partial \hat{F}_1(\hat{F}_0^{-1}(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}
Substituting \eqref{eq:gauss-grad-y1} and \eqref{eq:gauss-grad-y0} into \eqref{eq:roll-gradient} yields \eqref{eq:gauss-grad-combined}.
@@ -0,0 +1,30 @@
We describe the bisection procedure used to compute $\tau = \hat{F}_0^{-1}(1-\alpha;\mathcal{B}_0)$
in the KDE-ROLL forward pass (\Cref{sec:roll-kde-forward}).
\paragraph{Bracket initialisation.}
Because $\sigma(u;\,v) \to 0$ exponentially as $u \to -\infty$ and $\sigma(u;\,v) \to 1$
as $u \to +\infty$, with tails decaying within roughly $10/v_0$ of the data range, the
interval
\[
\mathrm{lo} = \min(\mathbf{s}^{(0)}) - \tfrac{10}{v_0},
\qquad
\mathrm{hi} = \max(\mathbf{s}^{(0)}) + \tfrac{10}{v_0}
\]
is guaranteed to satisfy $\hat{F}_0(\mathrm{lo}) < 1-\alpha < \hat{F}_0(\mathrm{hi})$.
\paragraph{Bisection iterations.}
At each step we evaluate the midpoint,
\[
\tau_{\mathrm{mid}} = \tfrac{1}{2}(\mathrm{lo} + \mathrm{hi}),
\]
and update the bracket:
\[
\begin{cases}
\mathrm{lo} \leftarrow \tau_{\mathrm{mid}} & \text{if } \hat{F}_0(\tau_{\mathrm{mid}};\,\mathcal{B}_0) < 1-\alpha, \\
\mathrm{hi} \leftarrow \tau_{\mathrm{mid}} & \text{otherwise.}
\end{cases}
\]
Iteration continues until $\lvert\hat{F}_0(\tau_{\mathrm{mid}};\,\mathcal{B}_0)-(1-\alpha)\rvert < \varepsilon$
(with $\varepsilon = 10^{-4}$), or until a maximum iteration count is reached.
The method converges unconditionally and requires no derivative evaluation, in contrast
to the Newton--Raphson alternative documented in Appendix~\ref{appendix:kde-nr}.
+142
View File
@@ -0,0 +1,142 @@
We derive the KDE gradient, instantiating the general form~\eqref{eq:roll-gradient}.
Throughout, $\tau = \hat{F}_0^{-1}(1-\alpha;\mathcal{B}_0)$ and bandwidths $v_0, v_1$
are as defined in \Cref{sec:kde-bandwidth}.
\paragraph{Case $y_i = 1$: expanding the KDE sum.}
When $\mathbf{x}_i \in \mathcal{B}_1$, the score $f_\theta(\mathbf{x}_i)$ appears directly
in $\hat{F}_1$ and has no effect on $\tau$. Expanding the KDE CDF:
%
\[
\dfrac{\partial \hat{F}_1\!\left(\hat{F}_0^{-1}(1-\alpha)\right)}{\partial f_{\theta}(\mathbf{x}_i)}
= \frac{\partial}{\partial f_{\theta}(\mathbf{x}_i)}
\frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_j \in \mathcal{B}_1}
\sigma_1\!\left(\tau - f_{\theta}(\mathbf{x}_j)\right)
\]
%
Separating the $j = i$ term (the only one depending on $f_\theta(\mathbf{x}_i)$):
%
\[
= \frac{\partial}{\partial f_{\theta}(\mathbf{x}_i)}\!\left(
\sum_{\substack{\mathbf{x}_j \in \mathcal{B}_1 \\ j \neq i}}
\frac{\sigma_1\!\left(\tau - f_{\theta}(\mathbf{x}_j)\right)}{|\mathcal{B}_1|}
\;+\;
\frac{\sigma_1\!\left(\tau - f_{\theta}(\mathbf{x}_i)\right)}{|\mathcal{B}_1|}
\right)
\]
%
The first sum is constant in $f_\theta(\mathbf{x}_i)$. Applying the chain rule to the
last term with $\partial(\tau - f_\theta(\mathbf{x}_i))/\partial f_\theta(\mathbf{x}_i) = -1$:
%
\begin{equation}
\frac{\partial \hat{F}_1(\hat{F}_0^{-1}(1-\alpha))}{\partial f_{\theta}(\mathbf{x}_i)}
= -\frac{1}{|\mathcal{B}_1|}\sigma_1'(\tau - f_{\theta}(\mathbf{x}_i))
\label{eq:kde-grad-y1}
\end{equation}
\paragraph{Case $y_i = 0$: derivative of $\hat{F}_1$ with respect to $\tau$.}
Differentiating the KDE CDF with respect to $\tau$:
%
\[
\frac{\partial \hat{F}_1\!\left(\hat{F}_0^{-1}(1-\alpha)\right)}{\partial \hat{F}_0^{-1}(1-\alpha)}
= \frac{\partial}{\partial \tau}
\frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_j \in \mathcal{B}_1}
\sigma_1\!\left(\tau - f_{\theta}(\mathbf{x}_j)\right)
\]
%
Since $\partial(\tau - f_\theta(\mathbf{x}_j))/\partial\tau = +1$ for every $j$,
the chain rule gives:
%
\begin{equation}
\frac{\partial \hat{F}_1(\hat{F}_0^{-1}(1-\alpha))}{\partial \hat{F}_0^{-1}(1-\alpha)}
= +\frac{1}{|\mathcal{B}_1|}\sum_{\mathbf{x}_j \in \mathcal{B}_1}\sigma_1'(\tau - f_{\theta}(\mathbf{x}_j))
\label{eq:kde-dF1-dtau}
\end{equation}
\paragraph{Case $y_i = 0$: derivative of $\tau$ with respect to $f_\theta(\mathbf{x}_i)$.}
Since $\tau = \hat{F}_0^{-1}(1-\alpha;\mathcal{B}_0)$ is computed numerically, we apply the \emph{inverse function theorem}: if $h$ has inverse $g$,
then $g'(y) = 1/h'(g(y))$.
We view $\hat{F}_0^{-1}(1-\alpha;\mathcal{B}_0)$ as a function of $f_\theta(\mathbf{x}_i)$
and find its inverse. From the defining equation
$|\mathcal{B}_0|\cdot(1-\alpha) = \sum_{\mathbf{x}_j \in \mathcal{B}_0}
\sigma_0(\tau - f_\theta(\mathbf{x}_j))$, solving for $f_\theta(\mathbf{x}_i)$:
%
\[
f_\theta(\mathbf{x}_i)
= \tau - \sigma_0^{-1}\!\!\left(
|\mathcal{B}_0|\cdot(1-\alpha)
- \sum_{\substack{\mathbf{x}_j \in \mathcal{B}_0 \\ j \neq i}}
\sigma_0\!\left(\tau - f_{\theta}(\mathbf{x}_j)\right)
\right)
\]
%
Differentiating this expression with respect to $\tau$ and applying the chain rule:
%
\[
\frac{\partial f_\theta(\mathbf{x}_i)}{\partial \tau}
= 1 + \sigma_0^{-1}{}'\!\!\left(\cdots\right)
\cdot
\sum_{\substack{\mathbf{x}_j \in \mathcal{B}_0 \\ j \neq i}}
\sigma_0'\!\left(\tau - f_{\theta}(\mathbf{x}_j)\right)
\]
%
(the leading $+1$ comes from differentiating $\tau$; the second term from differentiating
through $\sigma_0^{-1}$). By the inverse function theorem:
%
\[
\frac{\partial \hat{F}_0^{-1}(1-\alpha ; \mathcal{B}_0)}{\partial f_\theta(\mathbf{x}_i)}
= \frac{1}{\displaystyle
1 + \sigma_0^{-1}{}'\!\!\left(
|\mathcal{B}_0|\cdot(1-\alpha)
- \sum_{j \neq i}\sigma_0(\tau - f_\theta(\mathbf{x}_j))
\right)
\cdot \sum_{j \neq i}\sigma_0'(\tau - f_\theta(\mathbf{x}_j))}
\]
%
Using $|\mathcal{B}_0|\cdot(1-\alpha) = \sum_j \sigma_0(\tau - f_\theta(\mathbf{x}_j))$,
the argument of $\sigma_0^{-1}{}'$ simplifies:
%
\[
|\mathcal{B}_0|\cdot(1-\alpha)
- \sum_{j \neq i}\sigma_0(\tau - f_\theta(\mathbf{x}_j))
= \sigma_0(\tau - f_\theta(\mathbf{x}_i))
\]
%
Substituting and applying $\sigma_0^{-1}{}'(\sigma_0(u)) = 1/\sigma_0'(u)$,
then multiplying numerator and denominator by $\sigma_0'(\tau - f_\theta(\mathbf{x}_i))$:
%
\begin{equation}
\frac{\partial \hat{F}_0^{-1}(1-\alpha ; \mathcal{B}_0)}{\partial f_\theta(\mathbf{x}_i)} =
\frac{\sigma_0'(\tau - f_{\theta}(\mathbf{x}_i))}{\displaystyle\sum_{\mathbf{x}_j \in \mathcal{B}_0}\sigma_0'(\tau - f_{\theta}(\mathbf{x}_j))}
\label{eq:kde-grad-tau}
\end{equation}
\paragraph{Efficient computation via the sigmoid identity.}
Direct evaluation of \eqref{eq:kde-grad-tau} requires computing $\sigma_0'$ for every
point — potentially unstable when a point is far from $\tau$. For the sigmoid kernel,
the identity
\begin{equation}
\sigma(u;\,v)\,\bigl(1 - \sigma(u;\,v)\bigr) = \frac{\sigma'(u;\,v)}{v}
\label{eq:sigmoid-identity}
\end{equation}
(proved by direct substitution: both sides equal $\exp(-v|u|)/(1+\exp(-v|u|))^2$)
allows $v_0$ to cancel between numerator and denominator, so the ratio is expressed
entirely in terms of sigmoid values already cached from the forward pass:
\begin{equation}
\frac{\partial \hat{F}_0^{-1}(1-\alpha ; \mathcal{B}_0)}{\partial f_\theta(\mathbf{x}_i)}
=
\frac{\sigma_0(\tau-f_\theta(\mathbf{x}_i))\,\bigl(1-\sigma_0(\tau-f_\theta(\mathbf{x}_i))\bigr)}
{\displaystyle\sum_{\mathbf{x}_j \in \mathcal{B}_0}
\sigma_0(\tau-f_\theta(\mathbf{x}_j))\,\bigl(1-\sigma_0(\tau-f_\theta(\mathbf{x}_j))\bigr)}
\label{eq:kde-grad-tau-efficient}
\end{equation}
Substituting \eqref{eq:kde-grad-y1}, \eqref{eq:kde-dF1-dtau}, and \eqref{eq:kde-grad-tau}
into \eqref{eq:roll-gradient} yields \eqref{eq:kde-grad-combined}.
+28
View File
@@ -0,0 +1,28 @@
An earlier version of the implementation used Newton--Raphson to invert the KDE CDF
instead of bisection (\Cref{sec:roll-kde-forward}). We document it here as a reference
alternative.
\paragraph{Method.}
To find $\tau = \hat{F}_0^{-1}(1-\alpha;\mathcal{B}_0)$, choose an initial guess $\tau_0$
and iterate:
\begin{equation}
\tau_{n+1} = \tau_n
- \frac{\hat{F}_0(\tau_n;\,\mathcal{B}_0) - (1-\alpha)}
{\dfrac{\partial\hat{F}_0(\tau_n;\,\mathcal{B}_0)}{\partial\tau}},
\label{eq:kde-nr-step}
\end{equation}
where the denominator is the KDE density evaluated at $\tau_n$:
\[
\frac{\partial\hat{F}_0(\tau_n;\,\mathcal{B}_0)}{\partial\tau}
= \frac{1}{|\mathcal{B}_0|}\sum_{\mathbf{x}_j\in\mathcal{B}_0}
\sigma_0'(\tau_n - f_\theta(\mathbf{x}_j)).
\]
Iteration continues until
$\lvert\hat{F}_0(\tau_n;\,\mathcal{B}_0)-(1-\alpha)\rvert < \varepsilon$.
\paragraph{Limitations.}
Newton--Raphson requires evaluating $\sigma_0'$ at every step and can diverge if
$\tau_0$ is far from the root or if the KDE density is very small near the target
quantile (e.g.\ early in training when scores cluster tightly and $v_0$ is large).
These failure modes motivated the switch to bisection, which requires no derivative
evaluation in the forward pass and is guaranteed to converge given a valid bracket.
@@ -0,0 +1,70 @@
We instantiate the KDE gradient~\eqref{eq:kde-grad-combined} for the logistic sigmoid
kernel used in our implementation.
\paragraph{Kernel definition.}
\begin{equation}
\sigma(x ; v) = \frac{1}{1 + \exp(-vx)},
\qquad
\sigma'(x ; v) = \frac{v\,\exp(-v|x|)}{\bigl(1 + \exp(-v|x|)\bigr)^{2}}
\label{eq:sigmoid-kernel}
\end{equation}
where $v > 0$ is the bandwidth parameter. The derivative uses $|x|$ rather than $x$:
since $\sigma'$ is even, both forms are mathematically identical, but $\exp(-v|x|) \to 0$
as $|x| \to \infty$ whereas $\exp(-vx) \to \infty$ for $x \to -\infty$, so the $|x|$
form avoids floating-point overflow when scores fall well below $\tau$.
\paragraph{Forward pass.}
Substituting \eqref{eq:sigmoid-kernel} into the KDE CDF:
%
\begin{equation}
\hat{F}_{k}(\tau ; \mathbf{s}^{(k)}) =
\frac{1}{|\mathcal{B}_k|}
\sum_{\mathbf{x}_j \in \mathcal{B}_k}
\frac{1}{1 + \exp\!\bigl(-v_k\bigl(\tau - f_\theta(\mathbf{x}_j)\bigr)\bigr)}
\label{eq:kde-sigmoid-cdf}
\end{equation}
%
The derivative of $\hat{F}_k$ with respect to $\tau$ — used in Newton--Raphson inversion
and in the gradient — is:
%
\begin{equation}
\frac{\partial \hat{F}_{k}(\tau)}{\partial \tau}
=
\frac{1}{|\mathcal{B}_k|}
\sum_{\mathbf{x}_j \in \mathcal{B}_k}
\frac{v_k\,\exp\!\bigl(-v_k\bigl|\tau - f_\theta(\mathbf{x}_j)\bigr|\bigr)}
{\Bigl(1 + \exp\!\bigl(-v_k\bigl|\tau - f_\theta(\mathbf{x}_j)\bigr|\bigr)\Bigr)^{2}}
\label{eq:kde-sigmoid-pdf}
\end{equation}
\paragraph{Gradient.}
Define the shorthand $\varphi_k(u) = \sigma'(u ; v_k)$:
\begin{equation}
\varphi_k(u) \;=\;
\frac{v_k\,\exp(-v_k|u|)}{\bigl(1+\exp(-v_k|u|)\bigr)^{2}}
\label{eq:sigmoid-phi}
\end{equation}
Substituting \eqref{eq:sigmoid-kernel} into \eqref{eq:kde-grad-combined}:
\begin{equation}
\frac{\partial \mathcal{L}_{\text{ROLL-TPR@FPR}}}{\partial f_\theta(\mathbf{x}_i)} =
\begin{cases}
-\dfrac{\varphi_1\!\left(\tau - f_\theta(\mathbf{x}_i)\right)}{|\mathcal{B}_1|}
& \text{if } y_i = 1\\[14pt]
+\,\dfrac{\displaystyle\sum_{\mathbf{x}_j \in \mathcal{B}_1}
\varphi_1\!\left(\tau - f_\theta(\mathbf{x}_j)\right)}{|\mathcal{B}_1|}
\;\cdot\;
\dfrac{\varphi_0\!\left(\tau-f_\theta(\mathbf{x}_i)\right)}
{\displaystyle\sum_{\mathbf{x}_j \in \mathcal{B}_0}
\varphi_0\!\left(\tau-f_\theta(\mathbf{x}_j)\right)}
& \text{if } y_i = 0
\end{cases}
\label{eq:kde-sigmoid-grad}
\end{equation}
where $\tau = \hat{F}_0^{-1}(1-\alpha ; \mathcal{B}_0)$.