Range scaling with constraints

I'm not a mathematician, so sorry for the possible trivial question.

I have a set of values in $x_i\in[0,1]$ (say for $i=1,\ldots,n$) whose sum can be greater than $1$. Now I want to scale them so that the new values $\hat{x}_i$ fall in the interval $[a,b]\subseteq[0,1]$ such that $\sum_{i=1}^n\hat{x}_i=b$.

For instance, suppose that $x_1=0.2, \quad x_2=0.4, \quad x_3=0.8$ and that $[a,b]=[0.2,0.8]$. How can I compute $\hat{x}_1,\hat{x}_2,\hat{x}_3$ such that $\sum_{i=1}^3\hat{x}_i=0.8$?

Note: I've found a similar question here: http://math.stackexchange.com/questions/43698/range-scaling-problem. But the proposed solution fails to satisfy the constraint.

Thank you very much in advance!

-
Multiply all the $x_i$ with $\frac{\max_i x_i}{\sum_i x_i}$. – J. M. Aug 18 '11 at 9:23
Conditions need to be modified? Suppose $a=0.2$, $b=0.8$ specified and $n=7$. Whatever the $x_i$, we can't have $\hat{x}_i$ falling in $[a,b]$ and $\sum\hat{x}_i=b$, since $\hat{x}_i \ge 0.2$ so $\sum \hat{x}_i \ge 1.4$. – André Nicolas Aug 18 '11 at 9:46
@JM: can you be more explicit, that is $\hat{x}_i=\ldots$? – seg.fault Aug 18 '11 at 13:31
@André: good catch. Indeed, this could be a problem. However, I think that a simple normalization should solve it. That is: $x_i \rightarrow \frac{x_i}{\sum_{i}x_i} \rightarrow \hat{x}_i$. Do you? – seg.fault Aug 18 '11 at 13:35
@seg.fault: That's fine. It's also giving up on the condition that (say) all $\hat{x}_i$ are $\ge 0.2$. The point is that we can't just freely specify $a>0$ in advance, independent of $n$. What size are your "real" $n$ for your application? What are the real hoped for $a$, $b$, roughly? Are you willing for $\sum \hat{x}_i$ to be replaced by something like $\sum \hat{x}_i^2$? At least one of your wished-for specifications needs modification. The one you suggest above gives up on $a$. If it's OK with you, that's just fine, then multiply all by $b$ to make the sum $b$. – André Nicolas Aug 18 '11 at 15:26

In view of your example, suppose that $$a = x_1 < x_2 < \cdots < x_n = b,$$ where $0 \le a < b \le 1$, and that we want $\hat x_1 ,\hat x_2 , \ldots ,\hat x_n$ such that $$a = \hat x_1 < \hat x_2 < \cdots < \hat x_n \leq b$$ and $$\hat x_1 + \hat x_2 + \cdots + \hat x_n = b.$$ Noting that $$\hat x_1 + \hat x_2 + \cdots + \hat x_n > na,$$ we further assume that $b > na$. Then, you can use $$\hat x_i = a + \frac{{b - na}}{{\sum\nolimits_{i = 1}^n {x_i } - na}}(x_i - a).$$ Indeed, all the conditions are satisfied:

$\hat x_1 = a$,

$\hat x_1 < \hat x_2 < \cdots < \hat x_n$,

$\hat x_n = a + \frac{{b - na}}{{\sum\nolimits_{i = 1}^n {x_i } - na}}(b - a) \le a + (b - a) = b$,

$\sum\limits_{i = 1}^n {\hat x_i } = na + \frac{{b - na}}{{\sum\nolimits_{i = 1}^n {x_i } - na}}(\sum\nolimits_{i = 1}^n {x_i } - na) = b$.

In your example, $$\frac{{b - na}}{{\sum\nolimits_{i = 1}^n {x_i } - na}} = \frac{{0.8 - 3 \cdot 0.2}}{{1.4 - 3 \cdot 0.2}} = 0.25,$$ hence $$\hat x_1 = 0.2,$$ $$\hat x_2 = 0.2 + 0.25(0.4 - 0.2) = 0.25,$$ and $$\hat x_3 = 0.2 + 0.25(0.8 - 0.2) = 0.35.$$ Note that $\hat x_1 + \hat x_2 + \hat x_3 = 0.8 = b$.

-
Beautiful answer! It works like a charm.Thank you very much! – seg.fault Aug 18 '11 at 13:46
Thanks! Glad to help. – Shai Covo Aug 18 '11 at 13:51

I'm not sure to understand the question.

You can just normalize them dividing by the sum, so that their sum is equal to $1$ and the multiply each normalized number for b (0.8 in your example) so that now the sum is $b$.

$\hat{x}_1=0.8\frac{0.2}{1.4}, ~\hat{x}_2=0.8\frac{0.4}{1.4} ~\hat{x}_3=0.8\frac{0.8}{1.4}$
It does not work since in this case $\hat{x}_1 \notin [0.2,0.8]$. – seg.fault Aug 18 '11 at 13:28