I encountered the following question today: Given $n$ real numbers $r_1\le ...\le r_n.$ Can we find $k_1,...,k_n \ge 0$ real with $\sum_{i=1}^n k_i=1$ such that $$(r_1-k_1)^2+...+(r_n-k_n)^2$$ is minimal?
Is there a way to do this analytically or are there good optimization algorithms that can do this? (The problem has apparently a unique solution (compactness) and a unique-solution by convexity of the euclidean norm).
You are projecting the point $(r_1,\ldots,r_n)$ onto the probability simplex. This projection is needed often as a substep in convex optimization algorithms, and the solution can be found for example in this paper:
"Projection onto the probability simplex: An efficient algorithm with a simple proof, and an application" by Wang and Carreira-Perpinan.
This paper provides the following Matlab code which projects each row vector in the $N × D$ matrix $Y$ onto the probability simplex in $D$ dimensions.
See also slide 8-12 of Vandenberghe's 236c notes:
http://www.seas.ucla.edu/~vandenbe/236C/lectures/proxop.pdf