
APSTA-GE 2006: Applied Statistics for Social Science Research
Linear, exponential, and logarithmic functions
Limits
Definition of the derivative
Rules of differentiation
The chain rule and product rule

Image source: Wikipedia
A graph of a line with the equation \(y = 1 + 2x\).

A graph of a line with the equation \(y = 7.5 - 2x\).

plot_line(slope, intercept)slope and interceptChoose test values whose lines are visible in the fixed plotting window.
\[ \frac{\text{d}[y(t)]}{\text{d}t} = k \cdot y(t) \]
The general solution to this differential equation is \(y(t) = C e^{kt}\), where \(C = y(0)\).
Think of population growth or growth of an interest-bearing asset.
An asset that has a value \(A\) is invested with an annual interest rate \(r\). What is the balance in the account, \(P_1\), at the end of the first year?
\[ P_1 = A + rA = A(1 + r) \]
After year two, the value is:
\[ P_2 = P_1 + rP_1 = P_1(1 + r) = \\ A(1 + r)(1 + r) = A(1 + r)^2 \]
And after year \(t\), the value is:
\[ P(t) = A(1 + r)^t \]
What if we compound the interest twice per year? In that case:
\[ P_1 = A \left (1 + \frac{r}{2} \right)^2 \]
If we compound \(n\) times per year, the balance after one year would be:
\[ P_n(1) = A \left (1 + \frac{r}{n} \right)^n \]
Combining these ideas, if we compound \(n\) times per year, for \(t\) years, we get:
\[ P_n(t) = A \left (1 + \frac{r}{n} \right)^{nt} \]
Letting \(n \to \infty\) gives continuous compounding. Both the finite- and continuous-compounding formulas are exponential functions of time.
rate <- 0.05 # interest rate r
P <- 100 # principal P
n_comp <- 1:100 # number of compoundings n
Pn <- function(n, A, r, t) A * (1 + r/n)^(t*n)
Pe <- function(A, r, t) A * exp(r * t)
pn <- Pn(n = n_comp, A = P, r = rate, t = 50)
pe <- Pe(A = P, r = rate, t = 50)
d <- data.frame(n_comp, pn)
p <- ggplot(d, aes(n_comp, pn))
p + geom_line(linewidth = 0.2) +
geom_hline(yintercept = pe, color = 'red', linewidth = 0.2) +
xlab("Number of times the interest is compounded") +
ylab("Value of an asset") +
ggtitle("Value of $100 at r = 0.05 after 50 years") 


Suppose a particular population of bacteria is known to double in size every 4 hours. If a culture starts with 1000 bacteria, what is the population after 10 hours and 24 hours?
\[ P_n(t) = A \left (1 + \frac{r}{n} \right)^{nt} \]
\[ P_\infty(t) = A \lim_{m \to \infty} \left ( 1 + \frac{1}{m} \right)^{m(rt)} \]

\[ P_\infty(t) = A \lim_{m \to \infty} \left ( 1 + \frac{1}{m} \right)^{m(rt)} = A e^{rt} \]
Suppose $750 is invested in an account at an annual interest rate of 5.5%, compounded continuously. Let \(t\) denote the number of years after the initial investment and \(A(t)\) denote the amount of money in the account at time \(t\). Find a formula for \(A(t)\). Find the amount of money in the account after 5 years, 10 years, and 50 years.
For \(a > 0\) and \(a \ne 1\), an exponential function of the form \(f(x) = a^x\) is one-to-one, so it has an inverse called a logarithmic function. Logarithm arguments must be positive.
In R and this course, \(\log\) means the natural log, base \(e\). For a log-scale coefficient \(\beta\), the exact percentage change is \(100(e^\beta - 1)\%\); \(100\beta\%\) is a useful approximation only when \(\beta\) is small. For example, \(\log(1.04) \approx 0.0392\).
The following relationship always holds for \(a > 0\): \(a^t = e^{\log(a)t}\) since \(e^{\log(a)} = a\)
\(\log_a(uv) = \log_a(u) + \log_a(v)\)
\(\log_a(u/v) = \log_a(u) - \log_a(v)\)
\(\log_a u^n = n \log_a u\)
In statistics and machine learning, we often want to normalize a vector so its components add to one. The softmax function is
\[ \operatorname{softmax}(x)_i = \frac{\exp(x_i)}{\sum_{j=1}^N \exp(x_j)}. \]
If \(x\) has length 3, the numerator is a length-3 vector, the denominator is a scalar, and the resulting length-3 vector adds to 1.
[1] 0.09003057 0.24472847 0.66524096
[1] Inf Inf Inf
[1] NaN NaN NaN
The large inputs overflow: their exponentials are too large for the computer to represent directly.
We can instead work on the log scale:
\[ \log \operatorname{softmax}(x)_i = x_i - \log \sum_{j=1}^N \exp(x_j). \]
The second term is the log-sum-exp, or LSE. To evaluate it stably, subtract the largest input before exponentiating.
\[ \begin{aligned} m &= \max_j x_j, \\ \operatorname{LSE}(x) &= \log \sum_{j=1}^N \exp(x_j) \\ &= m + \log \sum_{j=1}^N \exp(x_j - m). \end{aligned} \]
Because \(m = \max_j x_j\), the largest shifted exponent is zero and every other shifted exponent is negative.
Your Turn: write LSE(x) and log_softmax(x), test them on small and large inputs, exponentiate the log probabilities, and check that they add to 1.
A limit is the value a function approaches as its input gets arbitrarily close to a specified point. The limit can exist even when the function is undefined there or has a different value there.
This demo shows what happens to the secant line as it approaches the tangent.
Source: Calculus Volume 1
A thousand years from now, your friends drug you, load you onto a ship, and fire it at the Moon. You wake in a spacesuit on the surface: a ship, a 100 m tower beside you, a metal ball stamped 1 kg, and a stopwatch.
The ship’s autopilot has the equations of motion programmed in — but it is missing one number: the Moon’s gravitational acceleration \(g\)
Your plan: climb the tower, drop the ball, and record the time it passes each red mark (spaced 5 m apart, 20 marks down)
Each mark has a sensor: as the ball passes, it flashes a light, so from the top you read the time off your stopwatch at each flash
From those \((t_k, x_k)\) pairs you will infer \(g\) — and fly home
\[ m\,\ddot{x} = m\,g \quad\Rightarrow\quad \ddot{x} = g \]
The mass cancels — that is why the 1 kg label is a red herring. This is Galileo’s insight, confirmed on the Moon by Apollo 15’s hammer-and-feather drop.
Integrating twice (we cover integration in Session 3), and dropping from rest, gives the solution we will use:
\[ v(t) = g\,t, \qquad x(t) = \tfrac{1}{2}\, g\, t^2 \]
Low gravity is a feature: on the Moon a 100 m drop takes about 11 s, slow enough to read marks with a stopwatch. On Earth it would take 4.5 s.

The inference problem (Session 6) is to recover \(g\) from noisy time readings at the known sensor positions, assuming \(x(t) = \tfrac{1}{2} g t^2\). The curve below uses the accepted reference value \(g_{\mathrm{ref}} = 1.625\ \text{m/s}^2\); it is not an estimate from these observations.

Oxygen is limited, so each drop costs you. One drop gives up to 20 timed marks (this run caught 19) — how many marks, and how many drops, do we need to pin down \(g\) to one decimal place? To two? We return to this in Session 7.
Before we can find \(g\), we need to understand velocity. What was the ball’s average velocity between \(t_1\) and \(t_2\) seconds? This is the slope of the secant line:
\[ \bar{v} = \frac{\Delta x}{\Delta t} = \frac{x(t_2) - x(t_1)}{t_2 - t_1} = \frac{\tfrac{1}{2} g\, t_2^2 - \tfrac{1}{2} g\, t_1^2}{t_2 - t_1} = \tfrac{1}{2} g\, (t_1 + t_2) \]
t1 <- 4; t2 <- 9
x1 <- 0.5 * g_ref * t1^2; x2 <- 0.5 * g_ref * t2^2
slope <- (x2 - x1) / (t2 - t1) # avg velocity = ½ g (t1 + t2)
ggplot(sol, aes(t, x)) + geom_line(linewidth = 0.2) +
geom_abline(slope = slope, intercept = x1 - slope * t1,
color = 'red', linewidth = 0.2) +
geom_point(aes(x = t1, y = x1), color = 'blue') +
geom_point(aes(x = t2, y = x2), color = 'blue') +
xlab("time t (s)") + ylab("distance fallen x (m)") +
ggtitle("Position curve and a secant line")
What if we wanted the speedometer reading at an instant \(t\), not the average over an interval? This is where the limit comes in.
\[ v = \lim_{\Delta t \to 0} \frac{\Delta x}{\Delta t} = \frac{dx}{dt} \]

\[ \begin{eqnarray} \bar v & = & \frac{x(a + h) - x(a)}{a + h - a} = \frac{x(a + h) - x(a)}{h} \\ v & = & \lim_{h \to 0} \frac{x(a + h) - x(a)}{h} \\ \end{eqnarray} \]
\[ \begin{eqnarray} v & = & \lim_{h \to 0} \frac{x(t + h) - x(t)}{h} = \lim_{h \to 0} \frac{\tfrac{1}{2} g (t + h)^2 - \tfrac{1}{2} g t^2}{h} = \\ & \lim_{h \to 0} & \frac{g t h + \tfrac{1}{2} g h^2}{h} = \lim_{h \to 0} \left(g t + \tfrac{1}{2} g h\right) = g t \end{eqnarray} \]
Using the accepted value \(g \approx 1.62\ \text{m/s}^2\), the speedometer at \(t = 4\) reads \(1.62 \cdot 4 \approx 6.5\ \text{m/s}\) downward.
The velocity is the derivative of position with respect to time, and we can write:
\[ v(t) = \frac{dx}{dt} = g t \]
\[ a(t) = \frac{dv}{dt} = \frac{d}{dt} \left( \frac{d x}{d t} \right) = \frac{d^2 x}{d t^2} \]
\[ a = \frac{dv}{dt} = \lim_{h \to 0} \frac{g(t + h) - g t}{h} = \lim_{h \to 0} \frac{g h}{h} = \lim_{h \to 0} g = g \]
\[ \begin{eqnarray} \frac{d}{dx}(c) & = & 0 \\ \frac{d}{dx}(x) & = & 1 \\ \frac{d}{dx}(x^n) & = & n x^{n-1} \\ \frac{d}{dx}[c f(x)] & = & c \frac{d}{dx}[f(x)] \\ \frac{d}{dx}[f(x) + g(x)] & = & \frac{d}{dx}f(x) + \frac{d}{dx}g(x) \\ \frac{d}{dx}(e^x) & = & e^x \\ \frac{d}{dx}\log(x) & = & \frac{1}{x} \end{eqnarray} \]
\[ x(t) = 4 + 3t + 5t^2 \]
What is the ball’s position at time zero and time 1 second?
What is the velocity at time zero?
What is the velocity and acceleration at 5 seconds?
What is this moon’s gravitational acceleration \(g\)? (Hint: compare \(a(t)\) to \(\ddot{x} = g\).)
\[ \frac{d}{dx}[f(x) g(x)] = f(x) \frac{d}{dx}[g(x)] + g(x)\frac{d}{dx}[f(x)] \]
\[ F'(x) = f'(g(x))g'(x) \]
\[ \begin{eqnarray} \frac{d}{dt}(\sin(t) \cos(t)) & = & \\ \sin(t)\frac{d}{dt}[\cos(t)] + \cos(t)\frac{d}{dt}[\sin(t)] & = & \\ \sin(t) (-\sin(t)) + \cos(t)\cos(t) & = & \\ \cos^2(t) - \sin^2(t) \end{eqnarray} \]


What is the derivative of \(\sin(x^2)\)? It is \(2x \cos(x^2)\)
Your turn: \[ \frac{d}{dx} \left( e^{x^2 \cos(x)} \right ) = \]
\[ e^{x^2 \cos(x)} \left(2x \cos(x) - x^2 \sin(x)\right) \]
\[ x(t) = R \cos(\omega t) \\ y(t) = R \sin(\omega t) \]