0
$\begingroup$

I have a large list of numeric (decimal) values ranging from approximately [-2, 2] with some of them being equal to 0 and I need to scale them so that their sum is equal to 0 while keeping their sign (i.e. negative numbers must remain negative after scaling).

The first try was to divide each number by the sum of all numbers in the list, at which point the sum of those is 1 then subtract $\frac{1}{c}$ where $c$ is the number of elements in the list but that has the drawback of not respecting the sign for values that are close to 0 to begin with.

Anyone has any tips on the best way to achieve this? Thanks

$\endgroup$
2
  • 2
    $\begingroup$ It looks as though this would mean treating the positive and negative numbers differently. For instance multiplying all the negative ones by a constant to equalise their sum with that of the positives. $\endgroup$
    – mdewey
    Mar 22, 2017 at 18:28
  • 3
    $\begingroup$ What else are you trying to achieve with this? Why does it have to sum to 0? Are there other constraints, like are you trying to maintain some distribution? There are many ways to transform data, and they will do other things to your data in the process. Can you provide us your data, a sample of it, or maybe include a histogram? $\endgroup$ Mar 22, 2017 at 19:39

1 Answer 1

2
$\begingroup$

Without more information, a reasonable option might be:

Let S = sum of unscaled values. Let A = sum of abs(unscaled values). Then for each number x in your inputs, scaled(x) = x - abs(x) * S/A.

This gives you a sum of zero, it preserves sign, it preserves the ordering of your data, and it's similar to a proportional adjustment: all positive values are scaled by the same factor (1+f) and all negatives are scaled by (1-f).

(Exception to the above: if your data are all zero/positive, or all zero/negative, it will scale everything to zero. But in these cases there's no good solution that meets your requirements.)

However, it does mean that if S is positive, your positive values will be squished closer together while your negatives are moved further apart, and vice versa if S is negative. That may not be desirable, depending on what your data is and what you're using it for. As Robin suggested above, you might get a better answer if you can give more context.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.