3
$\begingroup$

I have found this in regards to transforming to specific mean and maximum, but I need to specify a minimum as well. Is there a way to do so?

I believe it will be a more complicated solution similar to what's proposed here, but I haven't found in this second link a solution to what I need.

Thanks in advance!

$\endgroup$
1
  • 3
    $\begingroup$ Out of curiosity, what application requires you to both standardize to a different mean and normalize to a specific range? $\endgroup$ Commented Nov 29, 2017 at 0:52

2 Answers 2

5
$\begingroup$

There are many different transformations that could work for this (probably infinite), but here is a fairly simple one that works for the median instead of the mean (thanks @Glen_b for catching the mistake):

  1. Subtract the current median from all data points (now the median will be 0).
  2. Divide all the positive values by the current max, then multiply them by the difference between the desired max and the desired median.
  3. Divide all the negative values by the absolute value of the current min, then multiply them by the difference between the desired median and the desired min.
  4. Add the desired median to all points.

Now the data will have the desired min, max, and median.

If the ratio of the differences between the extremes and the mean and the desired extremes are close then this will be close as well, otherwise some additional shifting will be needed.

Another approach for the mean (that keeps the order of the points):

  1. Subtract the minimum value from all values and divide all the new values by the new max (now the values will all be between 0 and 1).
  2. Raise all the values to the same power ($x^p$), finding the correct value of $p$ will be the tricky part, but you could use trial and error or an optimization or root finding function.
  3. Multiply by the difference between the desired max and min, then add the min.

The dataset will need to have at least 3 unique values for this (or anything that retains the order) to work.

Edit

Here is some example R code that uses the second option (tested this time to make sure that it works):

scaleMMM <- function(x, min.target=-1, max.target=1, mean.target=0,
                     p.range=c(0.01,10)) {
  tmpfun <- function(p) {
    x2 <- x-min(x)
    x2 <- x2/max(x2)
    x2 <- x2^p
    x2 <- x2*(max.target-min.target) + min.target
    x2
  }

  p <- uniroot(function(p){mean(tmpfun(p))-mean.target}, p.range)$root

  out <- tmpfun(p)
  attr(out, 'p') <- p
  out
}


x1 <- rexp(25)
summary(x1)
x2 <- scaleMMM(x1)
summary(x2)

plot(x1,x2)
$\endgroup$
5
  • 1
    $\begingroup$ I don't think this works. Try observations 0,0,3 and rescale to have desired stats of min -1, max 1 and mean 0. The min and max will be correct but the mean is not 0. The problem is that when you take the divide step the mean is no longer 0. $\endgroup$
    – Glen_b
    Commented Nov 28, 2017 at 23:19
  • $\begingroup$ @Glen_b, oops, you are correct (this would work with the median instead of the mean. A couple more steps will be needed. Of course, with only 3 data points, just set one to the min, one to the max, and the last to whatever value will give the correct mean. $\endgroup$
    – Greg Snow
    Commented Nov 28, 2017 at 23:32
  • 1
    $\begingroup$ I think your original linear-rescaling of positive and negative deviations approach could work, with a small modification $\endgroup$
    – Glen_b
    Commented Nov 29, 2017 at 0:17
  • $\begingroup$ @Glen_b, any thoughts on what you would suggest for the modification? I have thought of a few, but some of them could change the ordering in the data, which may or may not be an issue. $\endgroup$
    – Greg Snow
    Commented Nov 29, 2017 at 17:04
  • $\begingroup$ I don't think we have to change the order; see my answer that builds on yours. I'll have to come back to it later $\endgroup$
    – Glen_b
    Commented Nov 29, 2017 at 22:57
1
$\begingroup$

This explains how to get Greg's original idea for rescaling positive and negative parts each in a linear way to give the desired outcome.

Let $P = (P_1,P_2,...,P_n)'$, and $N = (N_1,N_2,...,N_m)'$, where the elements of $P$ and $N$ are all non-negative.

Let $\bar{P}$ and $\bar{N}$ be the mean of the vectors $P$ and $N$ respectively

Consider a set of data with both positive and negative elements (as might be obtained by mean-correcting or median-correcting an existing set of data), where $X=(-N,P)'$.

Now let $Y = (-aN,bP)+c$ for $a,b>0$.

Then $\min(Y)=-aN_{(m)}+c$, $\max(Y)=bP_{(n)}+c$ and $\bar{Y}=(nb\bar{P}-ma\bar{N})/(m+n)+c$.

To obtain a desired min, max and mean for $Y$, simply solve the above three (linear) equations for $a,b,c$

If $\min(Y)=l,\max(Y)=u$ and $\bar{Y}=d$ then we solve

\begin{eqnarray} -aN_{(m)} + c &=& l\\ bP_{n}+c &=&u\\ (nb\bar{P}-ma\bar{N})/(m+n)+c&=&d \end{eqnarray}

for $a,b,c$.

Let $v = (a,b,c)'$ and let $z=(l,u,d)'$. In matrix form we are trying to solve:

$$Av = z$$

where

$$A= \begin{bmatrix} -N_{(m)} & 0 & 1 \\ 0 & P_{(n)} & 1 \\ -\frac{m}{m+n}\bar{N} & \frac{n}{m+n}\bar{P} & 1 \\ \end{bmatrix}\,. $$

with a few more lines of algebra we could write an explicit solution, but I'll have to come back to that later. As it is, this is straightforward to implement as is in many programs, since solving linear systems is easy, it would be a simple matter to write a few lines of code to get the scale factors $a,b$ and $c$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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