### L2 projection onto the probability simplex with bound constraints

Projecting onto the probability simplex is a common problem that arises in frequency estimation and related tasks. This problem can be solved efficiently with an $O(n \log{(n)})$ algorithm. In this blog post I consider the related problem of projecting onto the probability simplex with bound constraints. Given a vector $ \mathbf{r} \in \mathbb{R}^n $, our goal is to find a vector $\mathbf{p}^*$ that solves the following optimization problem.
$$
\begin{equation*}
\begin{aligned}
& \underset{\mathbf{p}}{\text{minimize}}
& & \frac{1}{2} \lvert \lvert \mathbf{p} - \mathbf{r} \rvert \rvert_2^2 \\
& \text{subject to}
& & \mathbf{1}^T \mathbf{p} = 1 \\
& & & \mathbf{a} \leq \mathbf{p} \leq \mathbf{b} \\
\end{aligned}
\end{equation*}
$$
This problem generalizes the standard probability simplex projection problem by introducing arbitrary bound constraints $ \mathbf{a} \leq \mathbf{p} \leq \mathbf{b}$. Here, $\mathbf{a}, \mathbf{b} \in \mathbb{R}^n $. …