Statistics · Measures of Location and Spread · Year 1
Coding
A trick for turning unwieldy numbers into simple ones before you calculate — then translating your answer back at the end.
9 min read4 worked examples
What is coding?
Coding means applying a linear transformation to every value in a dataset to produce a new, simpler dataset — usually to make mental or written arithmetic easier when the original values are large, decimal, or awkward. The most common coding is:
$y = \dfrac{x-a}{b}.$
where $a$ is chosen close to the centre of the data (often a convenient round number) and $b$ is chosen to reduce the values to small whole numbers where possible. Once summary statistics have been found for the coded data $y$, they're converted back to give the summary statistics of the original data $x$.
Effect on the mean
Since every $x$ becomes $y=(x-a)/b$, the mean transforms the same way:
$\bar y = \dfrac{\bar x - a}{b} \qquad\Longleftrightarrow\qquad \bar x = a + b\bar y.$
To recover the original mean, decode by multiplying by $b$ and adding $a$ — the reverse of the coding transformation.
Effect on the standard deviation
Subtracting $a$ shifts every value by the same amount, so it doesn't change how spread out the data is — only dividing by $b$ affects the spread. This gives:
To decode the standard deviation (or variance), you only ever multiply by $b$ — never add $a$. The constant $a$ shifts the location of the data but has no effect on its spread, so it plays no part in decoding the standard deviation. This is the single most common error with coding questions.
Coding and the sum of squared deviations
Some questions give the sum of squared deviations directly, often written $S_{yy}=\sum(y-\bar y)^2$, rather than $\sum y^2$. Because $y-\bar y = (x-a)/b - (\bar x-a)/b = (x-\bar x)/b$, squaring and summing gives:
where $S_{xx}=\sum(x-\bar x)^2$. Once you have $S_{xx}$, the variance of $x$ is $S_{xx}/n$ and the standard deviation is $\sqrt{S_{xx}/n}$.
Worked example 1
Coding a raw dataset
The masses, in grams, of six parcels are $340, 360, 370, 390, 410, 430$. Using the coding $y=\dfrac{x-370}{10}$, find the mean and standard deviation of the original masses.
MethodCode, calculate, then decode
1
Coded values: $y = -3,-1,0,2,4,6$.
Apply $(x-370)/10$ to each mass.
2
$\sum y = 8$, so $\bar y = 8/6 \approx 1.333$.
Mean of the coded data.
3
$\bar x = 370+10(1.333) \approx 383.3$ g.
Decode the mean: multiply by $b$, add $a$.
4
$\sum y^2 = 9+1+0+4+16+36=66$, so variance of $y = \dfrac{66}{6}-1.333^2 \approx 9.222$.
Computational variance formula on the coded data.
5
$\sigma_y \approx 3.037$, so $\sigma_x = 10(3.037) \approx 30.4$ g.
Decode the SD: multiply by $b$ only.
▪ solved
Worked example 2
Decoding from given summary statistics
Fifteen values of $x$ are coded using $y=\dfrac{x-100}{2}$. For the coded data, $\sum y = 45$ and $\sum y^2 = 189$. Find the mean and standard deviation of the original data.
MethodWork entirely from the coded totals
1
$\bar y = 45/15=3$.
Mean of the coded data.
2
$\bar x = 100+2(3)=106$.
Decode the mean.
3
Variance of $y = \dfrac{189}{15}-3^2=12.6-9=3.6$.
Computational variance formula.
4
$\sigma_y = \sqrt{3.6}\approx 1.897$, so $\sigma_x = 2(1.897)\approx 3.79$.
Decode the SD: multiply by $b=2$ only.
▪ solved
Worked example 3
Coding grouped data with large values
The table shows the annual salaries of 45 employees at a company. Using the coding $y=\dfrac{x-30000}{5000}$ applied to the class midpoints, estimate the mean and standard deviation of the salaries.
Variance of $y \approx \dfrac{39.25}{45}-0.1^2 \approx 0.862$, so $\sigma_y \approx 0.929$.
Computational formula on the coded data.
5
$\sigma_x = 5000(0.929) \approx \$4643$.
Decode the SD: multiply by $b=5000$ only.
▪ solved
Why bother coding here?
Working directly with midpoints like $32500$ means squaring five- and six-digit numbers by hand. Coding first reduces every value to something between $-1.5$ and $1.5$, making the arithmetic far more manageable, especially without a calculator with statistical memory functions.
Worked example 4
Decoding using $S_{yy}$ · Challenge
The times, $t$ seconds, taken by 12 athletes to complete a course are coded using $y=\dfrac{t-58}{2}$. For the coded data, $\sum y = 18$ and $S_{yy}=\sum(y-\bar y)^2=45$. Find the mean and standard deviation of the original times.
MethodDecode the mean, then use $S_{xx}=b^2S_{yy}$
1
$\bar y = 18/12=1.5$, so $\bar t = 58+2(1.5)=61$ seconds.
Decode the mean as usual.
2
$S_{tt} = b^2 S_{yy} = 2^2(45)=180$.
$S_{xx}=b^2S_{yy}$, with $b=2$.
3
Variance of $t = \dfrac{S_{tt}}{n}=\dfrac{180}{12}=15$.
Variance is $S_{xx}$ divided by $n$.
4
Standard deviation of $t = \sqrt{15}\approx 3.87$ seconds.
Square root of the variance.
▪ solved
Look deeper
You can check this matches the earlier method: variance of $y = S_{yy}/n = 45/12=3.75$, so $\sigma_y=\sqrt{3.75}\approx 1.936$, and $\sigma_t = 2(1.936)\approx 3.87$ — the same answer, reached a different way.
Common mistake
When decoding the standard deviation, students often write $\sigma_x = a+b\sigma_y$ by copying the mean's decoding rule. There is no "$+a$" step for the standard deviation — only for the mean. Spread is unaffected by shifting the whole dataset.
Check yourself
A dataset is coded using $y=\dfrac{x-50}{4}$. For the coded data, $n=10$, $\sum y = 20$ and $\sum y^2=90$. Find the mean and standard deviation of the original data. (Hint: decode the mean with "$\times b$, $+a$"; decode the SD with "$\times b$" only.)
Method
1
$\bar y = 20/10=2$, so $\bar x = 50+4(2)=58$.
2
Variance of $y = 90/10-2^2=9-4=5$, so $\sigma_y=\sqrt5\approx 2.236$.