Why Scale a Predictor to a Fixed Range?
Min-max scaling changes the numerical range of a predictor without changing the order of its observed values. It can be useful when a method is affected by predictor scales, as discussed in the earlier material on distance-based methods. The endpoints of the chosen interval become the transformed values of the smallest and largest observations used to calculate the scaling parameters.
The key qualification is that min-max scaling is tied to those observed extrema. A new value can fall outside the target interval, and an extreme observation can draw the rest of the scaled values close together. This tutorial derives the transformation, implements it in R, and examines those consequences. The treatment is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
Deriving the Transformation
The desired transformation is affine: it has a scale and a shift. Write it as \(z=c+dx\), where \(c\) and \(d\) are constants. To map the observed minimum \(m\) to \(a\) and the observed maximum \(M\) to \(b\), impose the endpoint requirements \(c+dm=a\) and \(c+dM=b\). Subtracting the first equation from the second gives the scale \(d\); substituting back gives the shift \(c\).
Substitution into \(z=c+dx\) yields the stated formula. Equivalently, subtract \(m\) from \(x\) to measure its position above the observed minimum, divide by \(M-m\) to express that position as a fraction of the observed range, and multiply by \(b-a\) to put it on the target range. Adding \(a\) shifts the result to the target interval’s lower endpoint.
This also verifies the endpoint behavior directly: at \(x=m\), the fraction is zero and \(z=a\); at \(x=M\), the fraction is one and \(z=b\). For \(a<b\), the scale \(d\) is positive, so the transformation preserves the ordering of values. It is an affine transformation, not merely a rescaling about zero: the shift is generally needed so that the minimum maps to \(a\).
Worked Example: Mapping Observations to a Nonzero Interval
A hypothetical portfolio analysis uses four recorded predictor values, \(12,18,30,42\). Transform them to \([2,8]\). Calculate each transformed value and verify the endpoints. The method is min-max scaling based on these four observations; the common mistake to avoid is forgetting the shift by the target lower endpoint.
Step 1: Find the observed extrema and ranges. The minimum is \(m=12\), the maximum is \(M=42\), and the observed range is \(M-m=42-12=30\). The target range is \(b-a=8-2=6\).
Step 2: Substitute into the formula. For each value, \(z=2+\frac{x-12}{30}\times6=2+\frac{x-12}{5}\).
Step 3: Transform each value. For \(x=12\), \(z=2+(12-12)/5=2\). For \(x=18\), \(z=2+6/5=3.2\). For \(x=30\), \(z=2+18/5=5.6\). For \(x=42\), \(z=2+30/5=8\).
The transformed values are \(2,3.2,5.6,8\). The first and last equal the target endpoints, as required. No rounding was needed. If the shift were omitted, the results would instead range from 0 to 6, not from 2 to 8.
Implementing Min-Max Scaling in R
A scaling function needs the interval endpoints and a clear rule for which observations determine \(m\) and \(M\). The following R function uses finite values in the supplied vector to find the extrema. It returns missing values for missing or non-finite entries, and stops if there is no finite value or if the reference values are constant. The latter case is outside the formula’s conditions; this tutorial does not prescribe a general policy for handling constant predictors.
The complete function is: scale_minmax <- function(x, a=0, b=1) { if (!is.numeric(x) || !is.numeric(a) || !is.numeric(b) || length(a)!=1L || length(b)!=1L || !is.finite(a) || !is.finite(b) || a >= b) stop("invalid input or target interval"); ok <- is.finite(x); if (!any(ok)) stop("no finite observations"); lo <- min(x[ok]); hi <- max(x[ok]); if (hi==lo) stop("constant reference values"); z <- rep(NA_real_, length(x)); z[ok] <- a+(x[ok]-lo)*(b-a)/(hi-lo); z }
For example, applying scale_minmax to c(12, 18, 30, 42) with a=2 and b=8 returns c(2.0, 3.2, 5.6, 8.0). The vector keeps its original length and order; entries that are NA, NaN, Inf, or -Inf remain NA in the result. In ordinary use, define clearly which observations are intended to set the extrema, and apply those same values of \(m\) and \(M\) when transforming the values to be compared.
The function checks that \(a<b\), because the stated method maps the minimum to the lower endpoint and the maximum to the upper endpoint. Reversing the endpoints would reverse the ordering instead. That is a different transformation from the increasing min-max scaling defined here.
The Range Is Determined by the Reference Extrema
A common interpretation is that every transformed value must lie in the chosen interval. That is true for the observations used to calculate \(m\) and \(M\): each lies between those extrema, so its fraction \((x-m)/(M-m)\) is between 0 and 1. It is not automatically true for values outside that reference range. The formula extrapolates rather than clipping such values to an endpoint.
Worked Example: Transforming Values Outside the Reference Range
A hypothetical score predictor has reference observations \(10,20,30\), which are scaled to \([0,1]\). A later record has value 35 and another has value 5. Apply the same reference extrema to both new records. The method is to retain the original scaling parameters; recalculating them would define a different transformation.
Step 1: Calculate the reference parameters. Here \(m=10\), \(M=30\), and \(M-m=20\). The transformation is \(z=(x-10)/20\).
Step 2: Transform the new values. For 35, \(z=(35-10)/20=25/20=1.25\). For 5, \(z=(5-10)/20=-5/20=-0.25\).
Thus the transformed values are 1.25 and -0.25, both outside \([0,1]\). This is correct: the formula sends the reference minimum and maximum to 0 and 1, but it does not clip values beyond them. The common mistake is to assume that the target interval is a guaranteed bound for every future value.
Whether values outside the target interval are acceptable depends on the purpose of the analysis. Clipping would replace the formula’s result with an endpoint and would erase the distinction between different values beyond that endpoint. If clipping is required, it must be specified as an additional operation rather than assumed to be part of min-max scaling.
Why Extreme Values Matter
The denominator \(M-m\) is the observed range. If an extreme value increases that range while other observations remain unchanged, the same differences between those ordinary observations are divided by a larger number. Their transformed values consequently become closer together. The order of the observations is preserved, but their relative spacing within the chosen interval changes.
Worked Example: An Extreme Value Compresses Other Scaled Values
A hypothetical predictor has values \(2,4,6,8\), scaled to \([0,1]\). Then an additional value of 100 is included in the reference data. Compare the transformed values of 4, 6, and 8 before and after adding 100. The method is to calculate the extrema afresh in each case; the mistake to avoid is assuming the old scaled values remain unchanged after the reference range changes.
Step 1: Scale the original four values. Here \(m=2\), \(M=8\), and the transformation is \(z=(x-2)/6\). Thus 2 maps to 0; 4 maps to \(2/6=1/3\); 6 maps to \(4/6=2/3\); and 8 maps to \(6/6=1\).
Step 2: Recalculate after adding 100. The new extrema are \(m=2\) and \(M=100\), giving \(z=(x-2)/98\). The value 4 maps to \(2/98=1/49\), or 0.0204 to four decimal places. The value 6 maps to \(4/98=2/49\), or 0.0408. The value 8 maps to \(6/98=3/49\), or 0.0612. The added value 100 maps to \(98/98=1\).
The ordinary values now occupy only a small part of the target interval, even though their order has not changed. The calculations show why a single extreme observation can materially affect this scaling. They do not show that every extreme value should be removed: that is a data-quality and modeling judgment that depends on the context.
Properties, Limitations, and Exam Traps
For \(M>m\), min-max scaling is strictly increasing when \(a<b\), so any two reference values retain their ordering. Differences are multiplied by the same constant \((b-a)/(M-m)\), while the shift changes their locations. The method does not make a predictor normally distributed, remove skewness, or make extreme observations harmless. It changes the measurement scale, not the information contained in the observed ordering.
A frequent calculation error is to divide \(x\) by the observed range and treat the result as min-max scaling. For instance, with reference values 5 and 15 and target interval \([0,1]\), that incorrect calculation gives \(5/10=0.5\) for the minimum and \(15/10=1.5\) for the maximum. The corrected calculation subtracts the minimum first: \((5-5)/10=0\), and \((15-5)/10=1\). The subtraction is essential because the observed range starts at \(m\), not necessarily at zero.
Check Your Understanding
For each problem, identify the extrema and target interval before applying the formula. Answers are given so that you can check both the arithmetic and the interpretation.
- A predictor has reference values \(3,9,15\) and is to be scaled to \([0,1]\). Calculate all three transformed values and state which values equal the target endpoints. (Answer: \(0,0.5,1\); 3 maps to 0 and 15 maps to 1.)
- Reference values \(20,30,50\) are scaled to \([-1,1]\). Calculate the transformed value for 35, show the substituted formula, and state whether it is below, within, or above the target interval. (Answer: \(-1+\frac{35-20}{50-20}\times2=0\), within the interval.)
- A reference predictor has minimum 4 and maximum 16 and is scaled to \([2,5]\). A new value is 20. Calculate its transformed value using the reference extrema, and explain why the answer does not contradict the endpoint mapping. (Answer: \(2+\frac{20-4}{16-4}\times3=6\); it is above 5 because 20 exceeds the reference maximum.)
- Values \(1,5,9\) are first scaled to \([0,1]\), then a reference value of 25 is added and the scaling is recalculated. Find the transformed value of 5 before and after the addition, to four decimal places, and describe the change. (Answer: Before, \((5-1)/(9-1)=0.5000\); after, \((5-1)/(25-1)=0.1667\), so the scaled value moves closer to the lower endpoint.)
- A candidate scales reference values 10 and 30 to \([2,8]\) and claims that the transformed value of 20 is 4. Calculate the correct value, identify the likely error, and verify the midpoint property. (Answer: \(2+\frac{20-10}{30-10}\times(8-2)=5\); the likely error is mishandling the target interval’s shift or width. Since 20 is halfway between 10 and 30, its transformed value is halfway between 2 and 8, namely 5.)