Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · MATHEMATICS & STATISTICS · CHAPTER 5

Chapter 5 worked answers: Statistical Reasoning and Randomness

On this page

Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.

B2-05-E01 · Compare location summaries

Calculate mean and median and explain the influence of 40.

The mean uses all magnitudes. The median depends on the two middle ordered values.

x=np.array([10.,12.,14.,40.])
mean=x.mean();median=np.median(x)
assert mean==19 and median==13
print(mean,median)
19.0 13.0

B2-05-E02 · Compute a conditional probability

Under a fair-die model, calculate P(result>4 | result is odd).

Conditional outcomes are 1,3,5; only 5 satisfies the event.

die=np.arange(1,7)
odd=die%2==1
probability=(die[odd]>4).mean()
assert probability==1/3
print(probability)
0.3333333333333333

B2-05-E03 · Separate spread and standard error

Calculate the sample standard deviation and estimated standard error of the mean; state the assumptions for using s/sqrt(n).

The squared deviations sum to 20; sample variance is 20/3. The standard-error expression assumes suitable independent identical sampling with finite variance.

x=np.array([4.,6.,8.,10.])
s=x.std(ddof=1);se=s/np.sqrt(len(x))
assert np.isclose(s,np.sqrt(20/3)) and np.isclose(se,np.sqrt(5/3))
print(s,se)
2.581988897471611 1.2909944487358056

B2-05-E04 · Original cultural-production challenge: all six tasks

Complete the original challenge: shapes, means, total, slices, threshold selection, logarithms, spread, square roots and a reproducible random comparison.

Row totals are 850,680,840, giving 2370 overall. The descriptive standard deviation uses all fifteen values and ddof=0, giving 49.15. These correct two arithmetic errors in the recovered expected-output text. The three countries and values are invented; simulation compares two teaching constructions, not evidence about real national production.

books=np.array([[120,150,180,210,190],[80,100,120,160,220],[90,130,170,200,250]])
decades=np.array([1970,1980,1990,2000,2010])
print(decades,books.shape)
print(books.mean(axis=1),books.mean(axis=0),books.sum())
print(books[1],books[:,1:4],books[2,4])
print(books[books>200],(books>200).sum())
print(np.round(np.log(books),2),round(books.std(),2),np.round(np.sqrt(books.mean(axis=1)),2))
rng=np.random.default_rng(23);simulated=rng.integers(50,301,size=(3,5))
print(simulated,simulated.min(),simulated.max(),simulated.mean())
assert books.sum()==2370 and np.isclose(books.std(),49.15282290977803)
assert (books>200).sum()==3
[1970 1980 1990 2000 2010] (3, 5)
[170. 136. 168.] [ 96.66666667 126.66666667 156.66666667 190.         220.        ] 2370
[ 80 100 120 160 220] [[150 180 210]
 [100 120 160]
 [130 170 200]] 250
[210 220 250] 3
[[4.79 5.01 5.19 5.35 5.25]
 [4.38 4.61 4.79 5.08 5.39]
 [4.5  4.87 5.14 5.3  5.52]] 49.15 [13.04 11.66 12.96]
[[ 59 224 156 211 116]
 [ 82 202  78  80 213]
 [215 264 159 100 100]] 59 264 150.6

Return to practice

Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.

Teaching edition · verified experiments · PDF and EPUB editions not generated.