<!-- Generated by book/tools/make-index.py from the book source. -->

> Hedengren, J. (Ed.) (2027). Appendix B: Mathematics Refresher. In *Data-Driven Athletics: Measuring, Modeling and Coaching Human Performance with Video and Wearable Data* (pp. 465–469). IRSRI Sports Research Institute. CC BY 4.0.

# B Mathematics Refresher

This appendix collects the mathematics the chapters use, at the level they use
it. Each section gives the idea, the formula and one worked number from the
book's examples. If a section is familiar, skip it; if a chapter's
*Engineer's note* lost you, start here. Every worked number was recomputed
with the script `code/check_math.py` in this appendix's folder.

## B.1 Angles and Radians

An angle measures turning. A full turn is 360 °. The
*radian* measures the same turn by arc length: an angle of one radian
sweeps an arc as long as the radius, so a full turn is $2\pi$ radians and

$$\theta_\text{rad} = \theta_\text{deg}\times\frac{\pi}{180},\qquad
  \SI{1}{rad} = \SI{57.30}{\degree},\qquad \SI{90}{\degree} = \SI{1.5708}{rad}.$$

The book reports angles in degrees, because coaches and protractors use them.
Formulas need radians in two places. First, the *arc length* a point moves
when a segment of length $r$ turns through $\theta$ is

$$s = r\,\theta_\text{rad},\qquad v = r\,\omega,$$

where $\omega$ is the angular speed in radians per second and $v$ the speed of
the point. Second, every programming library's `sin`, `cos` and
`tan` expects radians. NumPy's `np.radians` and
`np.degrees` convert.

**Example B.1: The ankle moves with the knee.**

A shank 0.45 m long turns 30 ° about the knee. The ankle
moves along an arc of $0.45\times(30\pi/180)=\SI{0.236}{\metre}$. A kicking leg
whose shank turns at 600 °/s turns at
$600\pi/180=\SI{10.47}{rad\per\second}$; a foot 0.70 m from the knee
moves at $0.70\times10.47=\SI{7.33}{\metre\per\second}$.

Three angles in the book sound alike and are not. The *included angle* at a
joint is the angle between the two segments, 180 ° for a straight
leg. *Flexion* as the apps report it at the knee and elbow is
$180^\circ$ minus the included angle, 0 ° for a straight joint.
A *segment angle* is a segment's angle to the vertical or the horizontal,
such as trunk lean. Always say which one a number is.

## B.2 Trigonometry for Joint Angles

In a right triangle with an angle $\theta$, the side across from it is the
*opposite*, the side next to it the *adjacent*, and the long side the
*hypotenuse*:

$$\sin\theta=\frac{\text{opposite}}{\text{hypotenuse}},\quad
  \cos\theta=\frac{\text{adjacent}}{\text{hypotenuse}},\quad
  \tan\theta=\frac{\text{opposite}}{\text{adjacent}}.$$

Figure B.1 uses them twice. Going forward, a speed $v$
at an angle $\theta$ above the horizontal splits into a horizontal part
$v_x = v\cos\theta$ and a vertical part $v_z = v\sin\theta$: a ball kicked at
20 m/s and 30 ° has $v_x=\SI{17.32}{\metre\per\second}$
and $v_z=\SI{10.0}{\metre\per\second}$ ($\sin 30^\circ = 0.5$,
$\cos 30^\circ=0.866$). Going backward, two measured sides give the angle with
the inverse functions $\arcsin$, $\arccos$ and $\arctan$.

*Figure B.1. Trigonometry in the book. Left: a segment angle from two measured sides, here trunk lean from the shoulder's offset ahead of the hip. Right: a speed split into horizontal and vertical parts.*

**Example B.2: Lean and tilt from two lengths.**

In one frame the shoulder is 0.10 m ahead of the hip and
0.50 m above it. Trunk lean is $\arctan(0.10/0.50)=\SI{11.3}{\degree}$
from the vertical. The hip drop app reports the slope of a waist band as a
percentage, rise over run times 100; a slope of 8% is a tilt of
$\arctan(0.08)=\SI{4.6}{\degree}$.

\paragraph{Use atan2, not atan.} The ratio $\Delta y/\Delta x$ loses the signs of
its parts: a segment pointing up and right ($\Delta x=3$, $\Delta y=4$) and one
pointing down and left ($-3$, $-4$) have the same ratio, and $\arctan$ gives
53.1 ° for both. The two-argument function
`np.arctan2(dy, dx)` keeps the quadrant: 53.1 ° and
-126.9 °. It also works when $\Delta x=0$, where the ratio is
undefined. The apps use it for directions such as trunk lean and release angle. Remember too that image
coordinates have $y$ growing *down* the frame, so “up” in the world is a
negative $\Delta y$ in pixels.

## B.3 Vectors and the Dot Product

A *vector* is a quantity with a size and a direction, written as a list of
components: a keypoint's position $(x,y)$, a displacement between two keypoints,
a velocity. The vector from point $B$ to point $A$ is $A-B$, component by
component. Its length (its *norm*) is by Pythagoras

$$|\vec u| = \sqrt{u_x^2+u_y^2}\qquad(\texttt{np.linalg.norm(u)}).$$

The *dot product* of two vectors multiplies matching components and adds
them. It equals the product of the two lengths and the cosine of the angle
between the vectors, which is why it gives a joint angle:

$$\vec u\cdot\vec w = u_x w_x + u_y w_y = |\vec u|\,|\vec w|\cos\theta,
  \qquad
  \theta = \arccos\frac{\vec u\cdot\vec w}{|\vec u|\,|\vec w|}.$$

At a joint $B$ between points $A$ and $C$, take $\vec u=A-B$ and $\vec w=C-B$.
This is the formula of the running, canvas and range-of-motion apps, and of
Chapter 3.

**Example B.3: A knee angle from three keypoints.**

The hip is at $(0.50,0.40)$, the knee at $(0.55,0.60)$ and the ankle at
$(0.48,0.80)$, in units of the frame width (Chapter 3). Then
$\vec u=(-0.05,-0.20)$, $\vec w=(-0.07,0.20)$,
$\vec u\cdot\vec w=0.0035-0.0400=-0.0365$, $|\vec u|=0.2062$, $|\vec w|=0.2119$,
$\cos\theta=-0.8356$ and $\theta=\SI{146.7}{\degree}$: a knee flexion of
$180-146.7=\SI{33.3}{\degree}$. The lengths divide out, so the angle is the same
in pixels, frame widths or meters; a distance, by contrast, needs a scale in
pixels per meter before it means anything.

A positive dot product means the angle is under 90 °; zero means the
vectors are at right angles; negative means over 90 °. Two cautions.
A camera sees the projection of the body on the image plane, so a
two-dimensional angle equals the real one only when the limb moves in a plane
square to the camera (Chapter 8). And `arccos` of a value that
rounding has pushed just past 1 or $-1$ is undefined; code clips the cosine to
$[-1,1]$ first.

## B.4 Derivatives as Rates from Samples

A *derivative* is a rate of change: velocity is the rate of change of
position, acceleration the rate of change of velocity. A video or a sensor gives
samples, not a smooth curve, so the book estimates derivatives by
*finite differences*. With samples $y_i$ taken $\Delta t = 1/f_s$ apart,

$$\text{forward:}\quad v_i &\approx \frac{y_{i+1}-y_i}{\Delta t}, &
  \text{central:}\quad v_i &\approx \frac{y_{i+1}-y_{i-1}}{2\,\Delta t},
  \\
  \text{second:}\quad a_i &\approx \frac{y_{i+1}-2y_i+y_{i-1}}{\Delta t^2}. \nonumber$$

The central difference is centered on sample $i$ and is the better estimate
there; `np.gradient(y, t)` uses it inside the array and one-sided
differences at the two ends. In pandas, `s.diff() / dt` is the backward
difference.

**Example B.4: Takeoff speed from five frames.**

After takeoff a jumper's hip rises 0.000, 0.042, 0.081, 0.117 and
0.150 m in five frames at 60 fps ($\Delta t=1/60$ s). The
forward difference at the first frame is $0.042\times60=\SI{2.52}{\metre\per\second}$;
the central difference at the second is $(0.081-0.000)/(2/60)=\SI{2.43}{\metre\per\second}$.
The second difference at the second frame is
$(0.081-2\times0.042+0.000)\times60^2=\SI{-10.8}{\metre\per\second\squared}$,
close to $-g$, as it should be for a body in flight.

\paragraph{Differences amplify noise.} If each position carries independent noise
with standard deviation $\sigma$, a forward difference carries
$\sigma\sqrt{2}/\Delta t$. A keypoint that jitters by 2 mm gives a
velocity noise of $0.002\times\sqrt2\times60=\SI{0.17}{\metre\per\second}$ at
60 fps and four times as much, 0.68 m/s, at
240 fps. A faster camera times events better but differentiates noise
worse, which is why the apps smooth positions before they take rates
(Chapter 4, Chapter 8). The second difference is worse again,
since it divides by $\Delta t^2$.

## B.5 Exponentials and Logarithms

Many body responses approach a new level quickly at first and then more slowly:
heart rate after the end of an interval, oxygen uptake at the start of one. The
first-order model of Chapter 2 describes them,

$$y(t) = y_\infty + (y_0-y_\infty)\,\ee^{-t/\tau},$$

where $y_0$ is the starting value, $y_\infty$ the level it settles to and $\tau$
the *time constant*. The factor $\ee^{-t/\tau}$ is the share of the change
still to come. After one $\tau$ it is $\ee^{-1}=0.368$, so 63.2% of the change
is done; after $2\tau$, 86.5%; after $3\tau$, 95.0%; after $5\tau$, 99.3%. The
number $\ee=2.7183$ is the base of the natural exponential, `np.exp`.

The *natural logarithm* undoes the exponential: $\ln(\ee^x)=x$
(`np.log`; `np.log10` is the base-10 logarithm). Its rules turn
products into sums, $\ln(ab)=\ln a+\ln b$, and ratios into differences,
$\ln(a/b)=\ln a-\ln b$. Those rules are why it appears twice in the book.

\paragraph{A time constant from two readings.} Solve
Equation \eqref{back/appB-math:eq:first} for $\tau$ with two readings $t$ seconds
apart:

$$\tau = \frac{t}{\ln\big[(y_0-y_\infty)/(y_t-y_\infty)\big]},\qquad
  t_{1/2} = \tau\ln 2 .$$

**Example B.5: Heart-rate recovery.**

Heart rate is 170 bpm at the end of an interval and settles toward
90 bpm; 60 s later it reads 122 bpm. The remaining
gap fell from 80 to 32 beats, so $\tau = 60/\ln(80/32)=\SI{65.5}{\second}$, and
the gap halves every $65.5\times\ln 2=\SI{45.4}{\second}$. The model predicts
$90+80\,\ee^{-120/65.5}=\SI{102.8}{\bpm}$ at 120 s; a reading far
from that says the single exponential does not fit this athlete or this
recovery.

\paragraph{Why ln RMSSD.} Heart rate variability values are skewed: most days
cluster low, a few run high. Taking the logarithm makes the spread more even and
turns ratios into differences, so a change has the same meaning at any level.
An RMSSD of 22.8 ms is $\ln 22.8=3.13$; doubling it to
45.6 ms adds $\ln 2=0.693$, giving 3.82, whatever the starting
value. For small changes, a difference in logarithms is close to the relative
change: from 42  to 38 ms is $3.64-3.74=-0.100$, near the
$-9.5\%$ change. To go back, exponentiate: $\ee^{3.13}=22.9$ (the small
difference from 22.8 is rounding in the 3.13).

## B.6 Straight-Line Fits

A *least-squares line* $\hat y = a + b x$ is the line that makes the sum of
squared vertical gaps between the points and the line as small as possible. Its
slope and intercept are

$$b = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\qquad
  a = \bar y - b\,\bar x,$$

where a bar marks a mean. The gaps $y_i-\hat y_i$ are the *residuals*. The
share of the spread in $y$ that the line accounts for is
$R^2 = 1-\sum(\text{residual}^2)/\sum(y_i-\bar y)^2$, which for a straight line
equals the square of the correlation coefficient $r$. In code,
`b, a = np.polyfit(x, y, 1)` returns the slope first.

**Example B.6: Cadence against speed.**

A runner's cadence at five treadmill speeds:

| Speed $x$ (m/s) | 2.5 | 3.0 | 3.5 | 4.0 | 4.5 |
|---|---|---|---|---|---|
| Cadence $y$ (steps/min) | 160 | 165 | 171 | 176 | 183 |

The means are $\bar x=3.5$ and $\bar y=171$. The cross sum
$\sum(x_i-\bar x)(y_i-\bar y)=28.5$ and $\sum(x_i-\bar x)^2=2.5$, so
$b=28.5/2.5=\SI{11.4}{\spm}$ per m/s and
$a=171-11.4\times3.5=131.1$. The residuals are $0.4$, $-0.3$, $0.0$, $-0.7$ and
$0.6$; $R^2=1-1.10/326=0.997$. At 3.8 m/s the line predicts
$131.1+11.4\times3.8=\SI{174.4}{\spm}$.

Read a fit with three questions. Do the residuals look like noise, or do they
curve? Is the prediction inside the range of $x$ that was measured (the
intercept, cadence at a speed of zero, is not a real cadence)? And is a high
$R^2$ telling you the relation is strong, or only that the $x$ values were
spread widely? Chapter 11 takes these further.

## B.7 Mean, Standard Deviation and Percentiles

For $n$ values $x_1,\dots,x_n$ the *mean* is $\bar x=\sum x_i/n$ and the
*standard deviation* (SD) is

$$s = \sqrt{\frac{\sum (x_i-\bar x)^2}{n-1}}.$$

Dividing by $n-1$ gives the *sample* SD, the usual choice when the values
are a sample of what could have been measured; dividing by $n$ gives the
*population* SD. The libraries disagree on the default: pandas
`s.std()` divides by $n-1$, NumPy `np.std(x)` by $n$ unless you
pass `ddof=1`. The Heart app's SDNN divides by $n$. For long records the
difference is small; for seven values it is not.

The *median* is the middle value once sorted (the mean of the two middle
values when $n$ is even). It ignores how far out the extreme values are, so one
bad frame or one missed beat moves it little. A *percentile* $p$ is the
value below which $p$% of the data fall; the 25th and 75th are the quartiles,
and their difference is the *interquartile range* (IQR). Software
interpolates between sorted values, and methods differ slightly;
`np.percentile` and pandas `quantile` both interpolate linearly by
default.

**Example B.7: Seven jump heights.**

Seven jumps measure 31.2, 32.5, 30.8, 33.1, 32.0, 29.7 and 32.6 cm.
Sorted: 29.7, 30.8, 31.2, 32.0, 32.5, 32.6, 33.1. The mean is
$221.9/7=\SI{31.7}{\centi\metre}$ and the median 32.0 cm. The
squared deviations sum to 8.56, so the sample SD is $\sqrt{8.56/6}=\SI{1.19}{\centi\metre}$
(the population SD is $\sqrt{8.56/7}=\SI{1.11}{\centi\metre}$). The 25th and 75th
percentiles are 31.0 and 32.55 cm, an IQR of
1.55 cm. The *coefficient of variation*, SD over mean, is
$1.19/31.7=3.8\%$, and the *standard error* of the mean, $s/\sqrt n$, is
0.45 cm.

The SD describes the spread of single values; the standard error describes how
well the mean is known. Chapter 11 uses the first to decide whether
one new jump is a change and the second to compare averages.

*Table B.1. The formulas of this appendix and the NumPy or pandas call for each.*

| Quantity | Formula | In code |
|---|---|---|
| Degrees to radians | $\theta\pi/180$ | `np.radians` |
| Arc length, point speed | $s=r\theta$ , $v=r\omega$ (radians) |  |
| Angle from two sides | $\theta=\operatorname{atan2}(\Delta y,\Delta x)$ | `np.arctan2(dy, dx)` |
| Vector length | $\sqrt{u_x^2+u_y^2}$ | `np.linalg.norm` |
| Joint angle | $\arccos\big(\vec u\cdot\vec w/\|\vec u\|\|\vec w\|\big)$ | `u @ w`, `np.arccos` |
| Rate from samples | $(y_{i+1}-y_{i-1})/2\Delta t$ | `np.gradient(y, t)` |
| First-order response | $y_\infty+(y_0-y_\infty)\ee^{-t/\tau}$ | `np.exp` |
| Time constant | $t/\ln[(y_0-y_\infty)/(y_t-y_\infty)]$ | `np.log` |
| Line fit | Equation back/appB-math:eq:ls | `np.polyfit(x, y, 1)` |
| Standard deviation | Equation back/appB-math:eq:sd | `s.std()`, `np.std(x, ddof=1)` |
| Percentile | linear interpolation | `np.percentile(x, 75)` |
