How to maximize Std Dev given a range of possible values, a number of values, and a specific mean? (I'm asking here and not stats.stackexchange because I'd like a mathematical proof of this)
In this question: Prove how to maximize Standard Deviation given a certain mean $\bar{x}$ and set of values; as pointed out by @mathguy in his second paragraph, I assumed that the mean and values were not independent and that the mean was $\frac{a+b}{2}$ where $a$ and $b$ are the minimum and maximimum values of the range respectively.
I'd like to understand how to maximize the SD of $n$ values in a range $[a,b]$ for an arbitrary mean $\bar{x}=y$.
For example, how would you maximize the standard deviation for $5$ values in the range $[0,1]$ with a mean of $0.3$?
Ideally I'd love to have a solution for this specific example (or another) and an understanding for the general solution.
Should we use calc, and if so how? Can we use something else?
 A: Maximising the standard deviation or variance for a given mean is equivalent to maximising the sum of squares of the values for a given sum of the values.
Meanwhile if $b \ge a$ and $\delta \gt 0$, $$(a-\delta)^2 +(b+\delta)^2 $$ $$= a^2-2a\delta+\delta^2+b^2+2b\delta + \delta^2 $$ $$= a^2+b^2 +2(b-a)\delta+2\delta^2  $$ $$\gt a^2+b^2$$
so the sum of squares is maximised if and only if as many terms as possible are as large as possible, even if this makes others smaller.
In your example of five terms in $[0,1]$ with a mean of $0.3$ 


*

*this make the sum of the terms $5\times 0.3=1.5$  

*we can make one term as large as possible in the interval, namely $1$, leaving $0.5$ for the sum of the other terms

*then we can make a second term as large as possible in the remaining sum, namely $0.5$, leaving $0$ for the sum of the remaining other terms

*so the remaining terms are $0$ 
and the standard deviation is maximised with values of $\{1,0.5,0,0,0\}$ 
A: First, you might as well assume that the mean is zero, so that $a < 0 < b$; that just simplifies things. (Otherwise: let $a' = a - \mu; b' = b - \mu$ and you can convert your problem for $[a, b], \mu$ to mine for $[a, b], 0$ by subtracting $\mu$ from all the sample values, etc.)
The SD is 
$$
s = \sum x_i^2
$$
and you want to maximize this subject to 
$$
\sum x_i = 0.
$$
That's a classic constrained optimization, which you can solve using lagrange multipliers and a little more. 
The gradient of the target, $s$, is 
$$
\nabla s = (2x_1, 2x_2, \ldots, 2x_n)
$$
while the gradient of the constraint is 
$$
\nabla c = (1, 1, \ldots, 1)
$$
At the optimum, your variables must satisfy the constraint, and must have
$$
\nabla c = \lambda \nabla s
$$
for some real value $\lambda$. That means that all variables will have the same value, and hence that value must be zero. The target is optimized when the values are all zero. Unfortunately, this optimum is a min rather than a max. :( 
Now on to the "a little more": if the constraint doesn't occur in the interior of the domain at a point where lagrange multipliers find it, it must happen on the boundary, where some $x_i$ is either $a$ or $b$. 
If you look at the two-variable case, and let $q = \min{|a|, |b|}$, you find that $x_1 = b$ leads to $x_2 = -b$, and if $|b| > |a|$, this is not valid. So the solution is either $x_1 = q, x_2 = -q$, or its negative. 
(In fact, for any solution, you can permute the $x_i$ and get another solution, or any of the $x_i$s to get other solutions. So I'm just going to find one optimum, not all of them.)
For the three-variable case, let's simplify things a little and assume that $|a| \le |b|$ (or else rename them). Then picking $x_1$ as a variable that must have a boundary value, there are cases $x_1 = a$ and $x_1 = b$. If $x_1 = b$, then $x_2 + x_3 = -b$. 
If $|a| < |b|/2$, this has no solutions. 
Otherwise, we have the same optimization problem as before, for the two-variable case, but with the "mean" constrained to be $-b$. Hmmm. 
There's another possibility, which is that $x_1 = a$, and then $x_2 + x_3 = -a$, and again we're reduced to a smaller problem. 
This just begs for a recurrence relation. Let's define 
$$
M(a, b, \mu, n)
$$
to be the max of $\sum_{i = 1}^n (x_i - \mu)^2$, subject to $\sum x_i = \mu$, and $a \le x_i \le b$ for all $i$. 
$$
P(a, b, \mu, n)
$$
to be the point $(x_1, x_2, \ldots, x_n)$ at which this is achieved,  with $x_1 = a$ or $x_1 = b$. 
[...and I stopped work here...]
If you look closely, you can see that my answer is headed (albeit very slowly) towards the same place as @Henry's more pithy and to-the-point answer, so I'm going to stop work, but leave the answer here as a guide for others who might be asking similar questions. 
