Coded Data

AS · S1 · 14 min

Coding means replacing each value xx by a simpler one, usually x−ax - a for some convenient number aa, before doing the arithmetic. The syllabus asks you to find the mean and standard deviation "from coded totals ∑(x−a)\sum (x - a) and ∑(x−a)2\sum (x - a)^2", and to use them in problems with up to two data sets. This appears on almost every Paper 5: a short question gives coded totals and asks for xˉ\bar{x} and the standard deviation, or asks you to recover ∑x\sum x and ∑x2\sum x^2, or to combine two sets that were coded differently.

Why coding works

Suppose the masses of six bags of flour, in grams, are

1012, 1005, 998, 1010, 1003, 1008.1012, \ 1005, \ 998, \ 1010, \ 1003, \ 1008.

Squaring numbers like 10121012 is slow and error-prone. Subtract 10001000 from each value instead:

12, 5, −2, 10, 3, 8.12, \ 5, \ -2, \ 10, \ 3, \ 8.

These coded values are exactly the original values slid 10001000 units to the left on the number line. Sliding every value by the same amount moves the mean by that amount but does not change how far apart the values are. So:

  • the mean of the coded values is 10001000 less than the mean of the original values;
  • the standard deviation of the coded values is the same as the standard deviation of the original values.

The coded values have ∑(x−1000)=36\sum (x - 1000) = 36 and ∑(x−1000)2=144+25+4+100+9+64=346\sum (x - 1000)^2 = 144 + 25 + 4 + 100 + 9 + 64 = 346. Their mean is 66, so the mean mass is 1000+6=1006 g1000 + 6 = 1006\ \text{g}. Their variance is 3466−62=21.667\tfrac{346}{6} - 6^2 = 21.667, so the standard deviation of the masses is 21.667=4.65 g\sqrt{21.667} = 4.65\ \text{g}, with no large numbers anywhere.

The coded formulas

Key result

For nn values coded as x−ax - a:

xˉ=a+∑(x−a)n\bar{x} = a + \frac{\sum (x - a)}{n}variance of x=∑(x−a)2n−(∑(x−a)n)2,sd of x=∑(x−a)2n−(∑(x−a)n)2.\text{variance of } x = \frac{\sum (x - a)^2}{n} - \left(\frac{\sum (x - a)}{n}\right)^2, \qquad \text{sd of } x = \sqrt{\frac{\sum (x - a)^2}{n} - \left(\frac{\sum (x - a)}{n}\right)^2}.

Subtracting aa changes the mean by aa and leaves the variance and standard deviation unchanged.

The variance formula is just the usual "mean of the squares minus the square of the mean", applied to the coded values. The aa appears only when you convert the mean back.

Watch out

Adding aa to the standard deviation. The most common error in coding questions is writing sd of x=a+sd of (x−a)\text{sd of } x = a + \text{sd of } (x - a). Shifting values does not change their spread. Only the mean gets aa added back.

Watch out

Subtracting a2a^2 in the variance. The variance is ∑(x−a)2n−(∑(x−a)n)2\dfrac{\sum (x - a)^2}{n} - \left(\dfrac{\sum (x - a)}{n}\right)^2. The second term is the square of the coded mean, not xˉ2\bar{x}^2 and not a2a^2.

Converting coded totals to ∑x\sum x and ∑x2\sum x^2

Some questions, especially ones that combine data sets, need the uncoded totals. Expand the brackets:

Key result
∑(x−a)=∑x−na⟺∑x=∑(x−a)+na\sum (x - a) = \sum x - na \qquad\Longleftrightarrow\qquad \sum x = \sum (x - a) + na∑(x−a)2=∑x2−2a∑x+na2⟺∑x2=∑(x−a)2+2a∑x−na2\sum (x - a)^2 = \sum x^2 - 2a\sum x + na^2 \qquad\Longleftrightarrow\qquad \sum x^2 = \sum (x - a)^2 + 2a \sum x - na^2

In the second line the "−2a∑x-2a\sum x" comes from the cross term: (x−a)2=x2−2ax+a2(x - a)^2 = x^2 - 2ax + a^2, and summing nn copies of a2a^2 gives na2na^2. There is no need to memorise these; write out (x−a)2=x2−2ax+a2(x - a)^2 = x^2 - 2ax + a^2 and sum each term.

An equivalent route, often quicker, is to find xˉ\bar{x} and the standard deviation from the coded totals first, then use ∑x=nxˉ\sum x = n\bar{x} and ∑x2=n(σ2+xˉ2)\sum x^2 = n(\sigma^2 + \bar{x}^2) from the standard deviation note.

Watch out

∑(x−a)≠∑x−a\sum (x - a) \ne \sum x - a. Subtracting aa from every one of nn values subtracts nana in total. Likewise ∑(x−a)2≠∑x2−a2\sum (x - a)^2 \ne \sum x^2 - a^2.

Combining two coded data sets

Totals can only be added when they are on the same code. Two cases:

  • Same aa. Add the nns, add the ∑(x−a)\sum (x - a)s and add the ∑(x−a)2\sum (x - a)^2s, then use the coded formulas.
  • Different aas. Convert one set to the other's code, or convert both to ∑x\sum x and ∑x2\sum x^2, then add.
Method
  1. Check whether both sets use the same coding constant.
  2. If not, convert: either find ∑x\sum x and ∑x2\sum x^2 for each set, or re-code one set. To change ∑(x−b)\sum (x - b) to ∑(x−a)\sum (x - a), write x−a=(x−b)+(b−a)x - a = (x - b) + (b - a), so ∑(x−a)=∑(x−b)+n(b−a)\sum (x - a) = \sum (x - b) + n(b - a) and ∑(x−a)2=∑(x−b)2+2(b−a)∑(x−b)+n(b−a)2\sum (x - a)^2 = \sum (x - b)^2 + 2(b - a)\sum (x - b) + n(b - a)^2.
  3. Add the totals for the two sets.
  4. Find the combined mean and standard deviation from the combined totals.

Scaling as well as shifting

Sometimes every value is multiplied by a constant, for instance converting units or giving everyone a percentage pay rise. Multiplying every value by bb multiplies the distances between values by bb too.

Key result

If every value is transformed to y=bx+cy = bx + c (with b>0b > 0):

yˉ=bxˉ+c,sd of y=b×sd of x,variance of y=b2×variance of x.\bar{y} = b\bar{x} + c, \qquad \text{sd of } y = b \times \text{sd of } x, \qquad \text{variance of } y = b^2 \times \text{variance of } x.

Adding cc affects the mean only; multiplying by bb affects both.

The same idea run backwards handles codes such as y=x−aby = \dfrac{x - a}{b}: then x=by+ax = by + a, so xˉ=byˉ+a\bar{x} = b\bar{y} + a and sd of x=b×sd of y\text{sd of } x = b \times \text{sd of } y. Paper 5 coding questions mostly use the shift x−ax - a alone, but scaled versions do appear in context (temperature conversions, pay rises, unit changes).

Choosing a coding constant

If a question asks you to choose the code yourself, pick aa close to the middle of the data (or a round number near the mean). The coded values are then small and both positive and negative, so the squares stay small. Any value of aa gives the same final answers; a good choice just reduces the arithmetic.

Worked examples

Mean and standard deviation from coded totals (routine)

For 2020 values of xx, ∑(x−50)=64\sum (x - 50) = 64 and ∑(x−50)2=1052\sum (x - 50)^2 = 1052. Find the mean and standard deviation of xx.

Solutionxˉ=50+6420=50+3.2=53.2\bar{x} = 50 + \frac{64}{20} = 50 + 3.2 = 53.2sd=105220−3.22=52.6−10.24=42.36=6.51 (3 s.f.)\text{sd} = \sqrt{\frac{1052}{20} - 3.2^2} = \sqrt{52.6 - 10.24} = \sqrt{42.36} = 6.51 \text{ (3 s.f.)}

Note that the 3.23.2 being squared is the coded mean, not 53.253.2.

Recovering uncoded totals

For 3030 values of xx, ∑(x−10)=45\sum (x - 10) = 45 and ∑(x−10)2=320\sum (x - 10)^2 = 320.

(a) Find ∑x\sum x and ∑x2\sum x^2.

(b) Find the variance of xx in two ways, and confirm that they agree.

Solution

(a) ∑x=∑(x−10)+30×10=45+300=345\sum x = \sum (x - 10) + 30 \times 10 = 45 + 300 = 345.

Expand: ∑(x−10)2=∑x2−20∑x+30×100\sum (x - 10)^2 = \sum x^2 - 20\sum x + 30 \times 100, so

320=∑x2−20(345)+3000 ⇒ ∑x2=320+6900−3000=4220.320 = \sum x^2 - 20(345) + 3000 \ \Rightarrow \ \sum x^2 = 320 + 6900 - 3000 = 4220.

(b) From coded totals: 32030−(4530)2=10.667−2.25=8.42\dfrac{320}{30} - \left(\dfrac{45}{30}\right)^2 = 10.667 - 2.25 = 8.42 (3 s.f.).

From uncoded totals: xˉ=34530=11.5\bar{x} = \tfrac{345}{30} = 11.5, and 422030−11.52=140.667−132.25=8.42\dfrac{4220}{30} - 11.5^2 = 140.667 - 132.25 = 8.42 (3 s.f.).

Both give 8.416…8.416\ldots, as they must: coding by subtraction does not change the variance.

Coded totals from the mean and standard deviation

The marks of 4040 students in an examination have mean 62.562.5 and standard deviation 4.24.2. Find ∑(x−60)\sum (x - 60) and ∑(x−60)2\sum (x - 60)^2.

Solution

The coded values x−60x - 60 have mean 62.5−60=2.562.5 - 60 = 2.5 and the same standard deviation, 4.24.2.

∑(x−60)=40×2.5=100\sum (x - 60) = 40 \times 2.5 = 100

For the coded values, 4.22=∑(x−60)240−2.524.2^2 = \dfrac{\sum (x - 60)^2}{40} - 2.5^2, so

∑(x−60)2=40(4.22+2.52)=40(17.64+6.25)=40×23.89=955.6.\sum (x - 60)^2 = 40(4.2^2 + 2.5^2) = 40(17.64 + 6.25) = 40 \times 23.89 = 955.6.
Finding the coding constant

For 2020 values of xx, ∑(x−a)=70\sum (x - a) = 70 and ∑(x−a)2=830\sum (x - a)^2 = 830. The mean of xx is 13.513.5. Find aa and the standard deviation of xx.

Solution13.5=a+7020=a+3.5 ⇒ a=10.13.5 = a + \frac{70}{20} = a + 3.5 \ \Rightarrow \ a = 10.sd=83020−3.52=41.5−12.25=29.25=5.41 (3 s.f.)\text{sd} = \sqrt{\frac{830}{20} - 3.5^2} = \sqrt{41.5 - 12.25} = \sqrt{29.25} = 5.41 \text{ (3 s.f.)}
Two data sets with different codes (exam-hard)

The lengths, xx cm, of 1212 fish of species A satisfy ∑(x−20)=36\sum (x - 20) = 36 and ∑(x−20)2=250\sum (x - 20)^2 = 250. The lengths of 1818 fish of species B satisfy ∑(x−25)=−27\sum (x - 25) = -27 and ∑(x−25)2=300\sum (x - 25)^2 = 300. Find the mean and standard deviation of the lengths of all 3030 fish.

Solution

The codes differ, so convert species B to the code x−20x - 20. Write x−20=(x−25)+5x - 20 = (x - 25) + 5.

∑(x−20)=∑(x−25)+18×5=−27+90=63\sum (x - 20) = \sum (x - 25) + 18 \times 5 = -27 + 90 = 63∑(x−20)2=∑(x−25)2+2×5∑(x−25)+18×52=300−270+450=480\sum (x - 20)^2 = \sum (x - 25)^2 + 2 \times 5 \sum (x - 25) + 18 \times 5^2 = 300 - 270 + 450 = 480

Combine with species A on the same code: n=30n = 30, ∑(x−20)=36+63=99\sum (x - 20) = 36 + 63 = 99, ∑(x−20)2=250+480=730\sum (x - 20)^2 = 250 + 480 = 730.

xˉ=20+9930=20+3.3=23.3 cm\bar{x} = 20 + \frac{99}{30} = 20 + 3.3 = 23.3\ \text{cm}sd=73030−3.32=24.333−10.89=13.443=3.67 cm (3 s.f.)\text{sd} = \sqrt{\frac{730}{30} - 3.3^2} = \sqrt{24.333 - 10.89} = \sqrt{13.443} = 3.67\ \text{cm (3 s.f.)}

Check by the other route. Species A: ∑x=36+12×20=276\sum x = 36 + 12 \times 20 = 276 and ∑x2=250+40×276−12×400=6490\sum x^2 = 250 + 40 \times 276 - 12 \times 400 = 6490. Species B: ∑x=−27+18×25=423\sum x = -27 + 18 \times 25 = 423 and ∑x2=300+50×423−18×625=10200\sum x^2 = 300 + 50 \times 423 - 18 \times 625 = 10200. Together ∑x=699\sum x = 699 and ∑x2=16690\sum x^2 = 16690, so xˉ=23.3\bar{x} = 23.3 and the variance is 1669030−23.32=556.333−542.89=13.443\tfrac{16690}{30} - 23.3^2 = 556.333 - 542.89 = 13.443, as before. Re-coding one set involved smaller numbers, which is why it is usually the faster method.

Scaling and shifting in context

The daily midday temperatures in a town during one month had mean 15 ∘C15\ ^\circ\text{C} and standard deviation 4 ∘C4\ ^\circ\text{C}. The temperature in degrees Fahrenheit is F=1.8C+32F = 1.8C + 32. Find the mean and standard deviation of the temperatures in ∘F^\circ\text{F}.

Solution

Mean: 1.8×15+32=59 ∘F1.8 \times 15 + 32 = 59\ ^\circ\text{F}.

Standard deviation: 1.8×4=7.2 ∘F1.8 \times 4 = 7.2\ ^\circ\text{F}. Adding 3232 shifts every temperature equally and so does not change the spread.

Exam tip
  • State the uncoded mean clearly: examiners see many answers that stop at the coded mean (3.23.2 instead of 53.253.2).
  • Show the coded formula with numbers in it, for example 105220−3.22\sqrt{\tfrac{1052}{20} - 3.2^2}. This earns the method mark even if the arithmetic goes wrong.
  • When asked for ∑x2\sum x^2, write out ∑(x−a)2=∑x2−2a∑x+na2\sum (x - a)^2 = \sum x^2 - 2a\sum x + na^2 before substituting; the na2na^2 term is where marks are usually lost.
  • When combining sets, say which code you are converting to. Combining totals with different codes is a zero-mark error.
  • Give answers to 3 significant figures, but keep exact values (3.33.3, 24.333…24.333\ldots) in the working.
Summary
  • Coding x→x−ax \to x - a makes arithmetic easier and does not change the answers.
  • xˉ=a+∑(x−a)n\bar{x} = a + \dfrac{\sum (x - a)}{n}: add aa back to the coded mean.
  • Variance and standard deviation are unchanged by subtracting aa: use ∑(x−a)2n−(∑(x−a)n)2\dfrac{\sum (x - a)^2}{n} - \left(\dfrac{\sum (x - a)}{n}\right)^2.
  • ∑x=∑(x−a)+na\sum x = \sum (x - a) + na and ∑(x−a)2=∑x2−2a∑x+na2\sum (x - a)^2 = \sum x^2 - 2a\sum x + na^2.
  • Totals on different codes cannot be added until one is converted.
  • For y=bx+cy = bx + c: mean ×b\times b then +c+ c; standard deviation ×b\times b only; variance ×b2\times b^2.

Practice questions

Question
  1. For 1515 values of xx, ∑(x−100)=−45\sum (x - 100) = -45 and ∑(x−100)2=1200\sum (x - 100)^2 = 1200. Find the mean and standard deviation of xx.
  2. The masses, xx kg, of six parcels are 2.43,2.47,2.51,2.38,2.46,2.452.43, 2.47, 2.51, 2.38, 2.46, 2.45. Using the coding y=100(x−2.4)y = 100(x - 2.4), find the mean and standard deviation of the masses.
  3. The ages, xx years, of 2525 people have mean 23.123.1 and standard deviation 2.22.2. Find ∑(x−20)\sum (x - 20) and ∑(x−20)2\sum (x - 20)^2.
  4. For 5050 values, ∑(x−30)=−120\sum (x - 30) = -120 and ∑(x−30)2=2088\sum (x - 30)^2 = 2088. Find the mean and standard deviation of xx.
  5. Ten values of xx have mean 7.67.6 and standard deviation 2.32.3. Find ∑(x−5)\sum (x - 5) and ∑(x−5)2\sum (x - 5)^2.
  6. The mean of 2020 values of xx is 88, and ∑(x−3)2=680\sum (x - 3)^2 = 680. Find the standard deviation of xx.
  7. Each of the 1212 employees in a small firm earns a weekly wage. The wages have mean $1200 and standard deviation $150. Every wage is increased by 4%4\% and then by a further $50. Find the new mean and standard deviation.
  8. For 1616 values of xx, ∑(x−a)=−12\sum (x - a) = -12, ∑(x−a)2=148\sum (x - a)^2 = 148 and ∑x=388\sum x = 388. Find aa, and the mean and standard deviation of xx.
  9. Set A has 1010 values with ∑(x−50)=24\sum (x - 50) = 24 and ∑(x−50)2=420\sum (x - 50)^2 = 420. Set B has 1515 values with ∑(x−40)=180\sum (x - 40) = 180 and ∑(x−40)2=2600\sum (x - 40)^2 = 2600. Find the mean and standard deviation of all 2525 values.
  10. For 1212 values of xx, ∑(x−c)=30\sum (x - c) = 30 and ∑(x−c)2=375\sum (x - c)^2 = 375. Also ∑(x−2c)=−42\sum (x - 2c) = -42. Find cc, the mean of xx and the standard deviation of xx.
Answers
  1. xˉ=100+−4515=100−3=97\bar{x} = 100 + \tfrac{-45}{15} = 100 - 3 = 97. Standard deviation =120015−(−3)2=80−9=71=8.43= \sqrt{\tfrac{1200}{15} - (-3)^2} = \sqrt{80 - 9} = \sqrt{71} = 8.43 (3 s.f.).

  2. The coded values are 3,7,11,−2,6,53, 7, 11, -2, 6, 5: ∑y=30\sum y = 30, ∑y2=9+49+121+4+36+25=244\sum y^2 = 9 + 49 + 121 + 4 + 36 + 25 = 244. yˉ=5\bar{y} = 5, and the standard deviation of yy is 2446−25=15.667=3.958\sqrt{\tfrac{244}{6} - 25} = \sqrt{15.667} = 3.958. Since x=y100+2.4x = \tfrac{y}{100} + 2.4: mean mass =5100+2.4=2.45 kg= \tfrac{5}{100} + 2.4 = 2.45\ \text{kg}; standard deviation =3.958100=0.0396 kg= \tfrac{3.958}{100} = 0.0396\ \text{kg} (3 s.f.).

  3. Coded mean =23.1−20=3.1= 23.1 - 20 = 3.1, so ∑(x−20)=25×3.1=77.5\sum (x - 20) = 25 \times 3.1 = 77.5. ∑(x−20)2=25(2.22+3.12)=25(4.84+9.61)=361.25\sum (x - 20)^2 = 25(2.2^2 + 3.1^2) = 25(4.84 + 9.61) = 361.25.

  4. Coded mean =−12050=−2.4= \tfrac{-120}{50} = -2.4, so xˉ=30−2.4=27.6\bar{x} = 30 - 2.4 = 27.6. Standard deviation =208850−(−2.4)2=41.76−5.76=36=6= \sqrt{\tfrac{2088}{50} - (-2.4)^2} = \sqrt{41.76 - 5.76} = \sqrt{36} = 6.

  5. Coded mean =7.6−5=2.6= 7.6 - 5 = 2.6, so ∑(x−5)=26\sum (x - 5) = 26. ∑(x−5)2=10(2.32+2.62)=10(5.29+6.76)=120.5\sum (x - 5)^2 = 10(2.3^2 + 2.6^2) = 10(5.29 + 6.76) = 120.5.

  6. Coded mean =8−3=5= 8 - 3 = 5. Variance =68020−52=34−25=9= \tfrac{680}{20} - 5^2 = 34 - 25 = 9, so the standard deviation is 33.

  7. New wage =1.04x+50= 1.04x + 50. New mean =1.04×1200+50=1298= 1.04 \times 1200 + 50 = 1298, i.e. $1298. New standard deviation =1.04×150=156= 1.04 \times 150 = 156, i.e. $156 (adding $50 does not change the spread).

  8. ∑(x−a)=∑x−16a\sum (x - a) = \sum x - 16a, so −12=388−16a-12 = 388 - 16a and a=25a = 25. Mean =25+−1216=25−0.75=24.25= 25 + \tfrac{-12}{16} = 25 - 0.75 = 24.25. Standard deviation =14816−0.752=9.25−0.5625=8.6875=2.95= \sqrt{\tfrac{148}{16} - 0.75^2} = \sqrt{9.25 - 0.5625} = \sqrt{8.6875} = 2.95 (3 s.f.).

  9. Convert set B to the code x−50x - 50, using x−50=(x−40)−10x - 50 = (x - 40) - 10: ∑(x−50)=180−15×10=30\sum (x - 50) = 180 - 15 \times 10 = 30; ∑(x−50)2=2600−2×10×180+15×100=2600−3600+1500=500\sum (x - 50)^2 = 2600 - 2 \times 10 \times 180 + 15 \times 100 = 2600 - 3600 + 1500 = 500. Combined: n=25n = 25, ∑(x−50)=24+30=54\sum (x - 50) = 24 + 30 = 54, ∑(x−50)2=420+500=920\sum (x - 50)^2 = 420 + 500 = 920. Mean =50+5425=52.16= 50 + \tfrac{54}{25} = 52.16. Standard deviation =92025−2.162=36.8−4.6656=32.1344=5.67= \sqrt{\tfrac{920}{25} - 2.16^2} = \sqrt{36.8 - 4.6656} = \sqrt{32.1344} = 5.67 (3 s.f.).

  10. ∑(x−2c)=∑(x−c)−12c\sum (x - 2c) = \sum (x - c) - 12c, so −42=30−12c-42 = 30 - 12c and c=6c = 6. Mean =6+3012=8.5= 6 + \tfrac{30}{12} = 8.5. Standard deviation =37512−2.52=31.25−6.25=25=5= \sqrt{\tfrac{375}{12} - 2.5^2} = \sqrt{31.25 - 6.25} = \sqrt{25} = 5.

How well do you know this?

Builds on

Where this leads

Console

Search notes, courses and tools, or run an action