---
title: "Calculus and Optimisation"
book: "Interviews I: Maths, Probability and Speed"
subject: quant
language: en
chapter: 20
exercises: 0
source: https://one-course.com/books/quant/18/en/chapter/20-calculus-and-optimisation
license: CC-BY-NC-SA-4.0
credit: "One Quant Book, One Course (one-course.com)"
---

# Chapter 20 — Calculus and Optimisation

Calculating with a clear head: integrals by symmetry, Lagrange multipliers, convexity, gradient and Newton steps, Stirling’s formula, and the places where floating-point arithmetic gives wrong answers. The questions want a short route, and a check that the result is the right size.

**Question 20.1 ★ QR, QT, Trader, Analyst • any • junior.**

Where does $x^x$ reach its minimum for $x > 0$, and what is the minimum? Without a calculator, which is larger: $e^\pi$ or $\pi^e$? Prove it with a single function.

**Answer to Question 20.1.**

The minimum is at $x = 1/e$, with value $e^{-1/e} \approx 0.692$, and $e^\pi$ is the larger.

Write $x^x = e^{x\ln x}$. The derivative of $x\ln x$ is $\ln x + 1$, which vanishes at $x = 1/e$. As $x \to 0$ the function tends to 1, and it rises again beyond $1/e$, so this point is the minimum.

For the comparison, use $g(x) = \ln x / x$. Its derivative $(1 - \ln x)/x^2$ is positive below $e$ and negative above, so $g$ peaks at $e$. Hence $\ln\pi/\pi < 1/e$, which rearranges to $e\ln\pi < \pi$, that is, $\pi^e < e^\pi$. The values are about $22.46$ and $23.14$, close, which is why a guess is unsafe.

**Classic wrong answer.** Comparing the two with rough decimals in the head. The gap is only 3%, so a rough estimate can go either way. The peak of $\ln x / x$ settles it with no arithmetic.

**Follow-up.** *Which is larger: $99^{100}$ or $100^{99}$?* $99^{100}$. Past $e$ the function $\ln x / x$ falls, so $\ln 99/99 > \ln 100/100$, which rearranges to $100\ln 99 > 99\ln 100$.

**Why it matters.** Whether a one-line function can replace a computation. Reducing a comparison of powers to one function’s shape is the habit being checked.

**Question 20.2 ★ QR, QT, DS, QD • any • junior.**

Compute $\int_{-\infty}^{\infty} e^{-x^2}\,dx$ by squaring it and using polar coordinates. Use the result to find the normal density’s constant, and the mean of $|Z|$ for a standard normal $Z$.

**Answer to Question 20.2.**

The integral is $\sqrt{\pi}$, the normal density’s constant is $1/\sqrt{2\pi}$, and $\mathbb{E}|Z| = \sqrt{2/\pi} \approx 0.798$.

Let $I$ be the integral. Then

$$
I^2 = \iint e^{-x^2 - y^2}\,dx\,dy = \int_0^{2\pi}\!\!\int_0^\infty e^{-r^2} r\,dr\,d\theta = 2\pi \cdot \tfrac12 = \pi.
$$

The factor $r$ from the change of coordinates is what makes the inner integral exact.

Substituting $x = t/\sqrt{2}$ gives $\int e^{-t^2/2}\,dt = \sqrt{2\pi}$, so the density needs the constant $1/\sqrt{2\pi}$.

Finally, by symmetry,

$$
\mathbb{E}|Z| = \frac{2}{\sqrt{2\pi}}\int_0^\infty z e^{-z^2/2}\,dz = \frac{2}{\sqrt{2\pi}} = \sqrt{\frac{2}{\pi}}.
$$

That is below 1, as it must be, since $\mathbb{E}|Z| \le \sqrt{\mathbb{E}Z^2} = 1$.

**Classic wrong answer.** Forgetting the factor $r$, which leaves an integral with no closed form. Or giving $\mathbb{E}|Z| = 1$, which confuses the mean absolute value with the standard deviation.

**Follow-up.** *What is $\int_0^\infty x^2 e^{-x^2}\,dx$?* $\sqrt{\pi}/4$. Integrate by parts with $u = x$, which leaves half of $\int_0^\infty e^{-x^2}\,dx = \sqrt{\pi}/2$.

**Why it matters.** Everyone quotes the constant; few can produce it. The ratio $\sqrt{2/\pi}$ is also a working number, linking the mean absolute deviation and the volatility.

**Question 20.3 ★ QR, QT, QD, Dev • any • junior.**

Use Newton’s method on $f(x) = x^2 - 2$, starting from $x_0 = 1$. Write the update as a simple formula, and give the first three iterates as fractions. How many correct digits does each step add?

**Answer to Question 20.3.**

The update is $x_{n+1} = \frac12(x_n + 2/x_n)$, the iterates are $3/2$, $17/12$ and $577/408$, and the number of correct digits about doubles at each step.

Newton’s method gives $x - (x^2 - 2)/(2x)$, which simplifies to the average of $x$ and $2/x$. That is the old idea of averaging a guess that is too high with one that is too low.

From 1: $\frac12(1 + 2) = 3/2$; then $\frac12(3/2 + 4/3) = 17/12$; then $\frac12(17/12 + 24/17) = 577/408$.

The errors are about $0.086$, $0.0025$ and $0.000\,002$. Each error is about $e^2/(2x)$, where $e$ is the one before, so the error is squared at each step, up to a constant of about $0.35$.

**Classic wrong answer.** Saying that each step adds a fixed number of digits. That describes bisection and other linear methods. Here the count of correct digits doubles.

**Follow-up.** *How can a machine with no division compute $1/a$?* Apply Newton to $1/x - a$, which gives $x_{n+1} = x_n(2 - ax_n)$, with no division. For $a = 3$ from $0.3$, it gives $0.33$, then $0.3333$. The digits double here too.

**Why it matters.** Whether the iteration has been run by hand once. The doubling of digits is the property worth quoting, and the same argument gives division in hardware.

**Question 20.4 ★ QR, QT, Trader, Analyst • any • junior.**

Estimate $\ln 1.1$, $e^{0.1}$ and $\sqrt{1.04}$ to three decimals using only two or three terms of a Taylor series. A zero-coupon bond, paying 1 in 10 years, is priced at $e^{-10y}$, where $y$ is its yield. Using a first-order and a second-order approximation, what is the percentage change in price when the yield rises by one percentage point?

**Answer to Question 20.4.**

$\ln 1.1 \approx 0.095$, $e^{0.1} \approx 1.105$, $\sqrt{1.04} \approx 1.020$, and the bond falls by 10% to first order and $9.5\%$ to second order; the exact fall is $9.52\%$.

The three series are $\ln(1 + x) = x - x^2/2 + x^3/3$, giving $0.1 - 0.005 + 0.000\,33 = 0.0953$; $e^x = 1 + x + x^2/2$, giving $1.105$; and $\sqrt{1 + x} = 1 + x/2 - x^2/8$, giving $1 + 0.02 - 0.0002 = 1.0198$.

Whatever the starting yield $y$, the bond’s relative change is $e^{-10\,\Delta y} - 1 \approx -10\,\Delta y + \frac12(10\,\Delta y)^2$. With $\Delta y = 0.01$, the terms are $-10\%$ and $+0.5\%$. The first is the duration and the second the convexity. The convexity term is positive whichever way rates move, which is why a large move always leaves the holder better off than the duration estimate says.

**Classic wrong answer.** Dropping the convexity term and calling $-10\%$ exact. It is half a percent off at this size. For a two-point move the gap quadruples to two percent.

**Follow-up.** *Why does the rule of 72 use 72, when $\ln 2 = 0.693$ suggests 69?* Since $\ln(1 + r) \approx r - r^2/2$ is below $r$, the true number rises with the rate, and 72 fits near 8%, where doubling takes $9.01$ years. It also divides easily.

**Why it matters.** Taylor series to two terms is a daily tool of the desk. A candidate who can carry it out quickly can also check any pricer.

**Question 20.5 ★★ QR, QT, Trader • any • junior.**

Compute $\int_0^{\pi/2} \frac{\sin^n x}{\sin^n x + \cos^n x}\,dx$ for any positive $n$. Then compute $\int_0^1 \frac{\ln(1 + x)}{1 + x^2}\,dx$, using a similar trick after the substitution $x = \tan\theta$.

**Answer to Question 20.5.**

The first integral is $\pi/4$ for every $n$, and the second is $\frac{\pi}{8}\ln 2 \approx 0.272$.

For the first, substitute $x \to \pi/2 - x$, which swaps sine and cosine. Adding the two forms of the integral, the integrand becomes 1, so $2I = \pi/2$. The exponent plays no part, and that is the point.

For the second, $x = \tan\theta$ cancels the denominator and gives

$$
I = \int_0^{\pi/4}\ln(1 + \tan\theta)\,d\theta.
$$

Now substitute $\theta \to \pi/4 - \theta$. The tangent subtraction formula gives $1 + \tan(\pi/4 - \theta) = 2/(1 + \tan\theta)$. So

$$
I = \int_0^{\pi/4}\bigl(\ln 2 - \ln(1 + \tan\theta)\bigr)\,d\theta = \frac{\pi}{4}\ln 2 - I,
$$

which gives $I = \frac{\pi}{8}\ln 2$.

A size check: the integrand rises from 0 to $\frac12\ln 2 \approx 0.35$ across an interval of length 1, so a value near $0.27$ is plausible.

**Classic wrong answer.** Trying to find an antiderivative. Neither integral has a useful one. The substitution that maps the interval to itself is the only short route.

**Follow-up.** *Compute $\int_0^\pi \frac{x\sin x}{1 + \cos^2 x}\,dx$.* $\pi^2/4$. Substituting $x \to \pi - x$ replaces $x$ with $\pi/2$ on average, and what remains is $\frac{\pi}{2}\int_0^\pi \frac{\sin x}{1 + \cos^2 x}\,dx = \frac{\pi}{2}\cdot\frac{\pi}{2}$.

**Why it matters.** Reflection is a trick worth having ready, and an interviewer learns from how long the candidate fights with an antiderivative before looking for it.

**Question 20.6 ★★ QR, QT, DS, QD • any • junior.**

Derive Stirling’s formula for $n!$ from $n! = \int_0^\infty x^n e^{-x}\,dx$, by expanding around the peak of the integrand. Use it to approximate $\binom{100}{50}$. What is the chance of exactly 50 heads in 100 flips?

**Answer to Question 20.6.**

$n! \approx \sqrt{2\pi n}\,(n/e)^n$, so $\binom{100}{50} \approx 1.011 \times 10^{29}$, and the chance of exactly 50 heads is about $0.08$.

Write the integrand as $e^{h(x)}$ with $h(x) = n\ln x - x$. The peak is at $x = n$, where $h^{\prime\prime} = -n/x^2 = -1/n$. Expanding to second order,

$$
n! \approx e^{n\ln n - n}\int_{-\infty}^{\infty} e^{-(x - n)^2/(2n)}\,dx = n^n e^{-n}\sqrt{2\pi n},
$$

which is the Stirling formula. Extending the range to the whole line costs almost nothing, as the mass sits within a few $\sqrt{n}$ of $n$. The relative error is about $1/(12n)$, so at $n = 10$ the formula is already within 1%.

For the middle binomial coefficient, the powers cancel:

$$
\binom{2m}{m} \approx \frac{4^m}{\sqrt{\pi m}}.
$$

With $m = 50$ this gives $1.011 \times 10^{29}$, against the true $1.009 \times 10^{29}$. Dividing by $2^{100}$ gives $1/\sqrt{50\pi} = 0.0798$, against the exact $0.0796$.

**Classic wrong answer.** Forgetting the factor $\sqrt{2\pi n}$ and using $(n/e)^n$ alone. It is off by a factor of 25 at $n = 100$, though it is fine for the leading growth rate.

**Follow-up.** *In 1000 flips, what is the chance of an exact tie?* $1/\sqrt{500\pi} \approx 0.025$. Ten times as many flips reduce the chance of a tie by a factor of $\sqrt{10}$, which many people find surprisingly slow.

**Why it matters.** The derivation is the template for Laplace’s method, which is the source of many large-deviation and saddle-point estimates.

**Question 20.7 ★★ QR, QT, QD • any • junior.**

Let $I_n = \int_0^{\pi/2} \sin^n x\,dx$. Derive a recursion using integration by parts, and compute $I_{10}$ exactly. How close is the large-$n$ approximation $\sqrt{\pi/(2n)}$?

**Answer to Question 20.7.**

The recursion is $I_n = \frac{n - 1}{n}I_{n - 2}$, and $I_{10} = 63\pi/512 \approx 0.3866$. The approximation gives $0.3963$, 2.5% high.

Integrating by parts with $\sin^{n - 1}x$ and $\sin x$, the boundary term vanishes and

$$
I_n = (n - 1)\int_0^{\pi/2}\sin^{n - 2}x\cos^2 x\,dx = (n - 1)(I_{n - 2} - I_n).
$$

Solving gives the recursion. Starting from $I_0 = \pi/2$,

$$
I_{10} = \frac{\pi}{2}\cdot\frac12\cdot\frac34\cdot\frac56\cdot\frac78\cdot\frac{9}{10} = \frac{\pi}{2}\cdot\frac{63}{256}.
$$

For the large-$n$ form, the recursion shows that $nI_nI_{n - 1}$ does not change with $n$, and at $n = 1$ it equals $\pi/2$. As $I_n \approx I_{n - 1}$ for large $n$, this gives $I_n \approx \sqrt{\pi/(2n)}$. The gap at $n = 10$ shrinks to almost nothing if $2n$ is replaced by $2n + 1$, which gives $\sqrt{\pi/21} = 0.3868$.

**Classic wrong answer.** Using the recursion with $n/(n - 1)$, the wrong way up. That makes the integrals grow, though $\sin^n x$ falls with $n$.

**Follow-up.** *What is the mean of $\sin^{10}x$ over a full circle, and what else equals it?* $63/256$, which is $\frac{2}{\pi}I_{10}$. It is also the chance of exactly five heads in ten flips, $\binom{10}{5}/2^{10}$, which is no accident: expanding $\sin x$ in complex exponentials gives that coefficient.

**Why it matters.** This is the standard reduction formula, and the Wallis limit and the constant in Stirling’s formula both follow from it.

**Question 20.8 ★★ QR, QT, Analyst, DS • any • junior.**

Maximise $xyz$ subject to $x + y + z = 1$ with positive variables, by Lagrange multipliers and by another route. Then maximise $\sqrt{x} + \sqrt{y}$ subject to $x + y = B$. Show for $B = 2$ that the multiplier equals the change in the best value per unit of $B$, and say what this means.

**Answer to Question 20.8.**

The first maximum is $1/27$ at $x = y = z = 1/3$, and the second is $\sqrt{2B}$ at $x = y = B/2$. At $B = 2$, the multiplier is $\frac12$, which is the marginal value of the budget.

For the first, set the gradient of $xyz$ proportional to that of the constraint: $yz = xz = xy = \lambda$. Positive variables then give $x = y = z$. The other route is the inequality of arithmetic and geometric means: $(xyz)^{1/3} \le (x + y + z)/3 = 1/3$, with equality when all are equal.

For the second, $1/(2\sqrt{x}) = 1/(2\sqrt{y}) = \lambda$, so $x = y = B/2$, and the value is $V(B) = \sqrt{2B}$. At $B = 2$: $x = y = 1$, $V = 2$ and $\lambda = \frac12$. Separately, $V'(B) = 1/\sqrt{2B} = \frac12$. The two agree.

That is the general meaning of the multiplier: the rate at which the best value rises with the constraint, or shadow price. It is what the last unit of budget is worth, and so what it makes sense to pay for one more.

**Classic wrong answer.** Treating the multiplier as a by-product with no meaning. Or stopping at the critical point with no check; here the corner $(B, 0)$ gives $\sqrt{2}$, below 2.

**Follow-up.** *The budget rises from 2 to $2.1$. Estimate the new best value with no re-solving.* $2 + 0.1 \times 0.5 = 2.05$. The exact value is $\sqrt{4.2} = 2.0494$. The estimate is slightly high, because $V$ is concave.

**Why it matters.** Pricing a constraint is the purpose of the multiplier in practice, from the risk limit to the capital allocation.

**Question 20.9 ★★ QR, QT, DS, Analyst • any • junior.**

Which of these are convex: $-\ln x$, $x^2$, $|x|$, $\sqrt{x}$, $\max(x, y)$, $xy$? Give a one-line reason for each. Use Jensen’s inequality to explain why a normal annual return with mean 10% and volatility 20% compounds at a rate below 10%, and estimate that rate.

**Answer to Question 20.9.**

Convex: $-\ln x$, $x^2$, $|x|$, $\max(x, y)$. Not convex: $\sqrt{x}$, which is concave, and $xy$, which is neither. The compound rate is about $8\%$.

The reasons, in order. $-\ln x$ has second derivative $1/x^2 > 0$. So does $x^2$, with second derivative 2. $|x|$ satisfies the triangle inequality. $\sqrt{x}$ has a negative second derivative. A maximum of linear functions is always convex. $xy$ has Hessian eigenvalues $\pm 1$, so it curves up along $x = y$ and down along $x = -y$.

Now for the return. Compounding takes logs, and $\ln$ is concave, so Jensen’s inequality gives $\mathbb{E}\ln(1 + R) \le \ln(1 + \mathbb{E}R)$. Expanding around the mean,

$$
\mathbb{E}\ln(1 + R) \approx \ln 1.1 - \frac{\sigma^2}{2(1.1)^2} = 0.0953 - 0.0165 = 0.079.
$$

So the log growth is about $7.9\%$ to this order. A numerical integral, and a simulation of the normal, give $0.078$, which is $8.1\%$ a year as a simple rate. The common shortcut $\mu - \sigma^2/2 = 8\%$ lands in the same place. With no volatility the formula gives back exactly 10%. A normal return can in principle fall below $-100\%$, where the log is undefined, but that is $5.5$ standard deviations away and does not affect the size.

**Classic wrong answer.** Calling $xy$ convex because each variable alone is linear. Being separately linear says nothing about the joint shape, as the saddle shows.

**Follow-up.** *With mean excess return 10% and volatility 20%, what leverage maximises the growth $L\mu - L^2\sigma^2/2$?* $L = \mu/\sigma^2 = 2.5$, which gives growth of $0.125$. At twice this leverage the growth falls to zero.

**Why it matters.** Volatility drag is a direct cost in any compounding strategy, and the concavity of the log is the entire explanation.

**Question 20.10 ★★ QR, DS, QD • any • junior.**

Gradient descent minimises $\frac12 x^\top Ax$ with $A$ having eigenvalues 1 and 100. What step sizes converge, and what is the best one? How many steps does it take to reduce the error by a factor of $10^6$? What does Newton’s method do on this function, and what does heavy-ball momentum do?

**Answer to Question 20.10.**

Converging steps are $0 < \alpha < 0.02$; the best is $\alpha = 2/101$; it needs about 691 steps; Newton finishes in one; and heavy-ball momentum needs about 93.

In the eigenbasis the coordinates decouple, and each is multiplied by $1 - \alpha\lambda$ per step. To converge, every factor must be below 1 in size, and the largest eigenvalue gives $\alpha < 2/100$.

The best step balances the two factors: $1 - \alpha = -(1 - 100\alpha)$, so $\alpha = 2/101$. Both factors then have size $99/101 = 0.9802$. To reduce the error by $10^6$ takes $\ln 10^6 / (-\ln 0.9802) \approx 691$ steps, confirmed by running it. A step just over $0.02$ makes the second coordinate grow without limit.

The number that matters is the condition number $\kappa = 100$; the step count is about $\frac{\kappa}{2}\ln 10^6$.

Newton multiplies the gradient by $A^{-1}$, which lands on the minimum at once for any quadratic. Heavy-ball momentum, tuned with $\alpha = 4/121$ and $\beta = (9/11)^2$, has rate $(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1) = 9/11$. That rate suggests 69 steps, but the tuned recursion has a repeated root, which adds a factor growing with the step count, and a run takes 93.

**Classic wrong answer.** Choosing $\alpha$ from the smallest eigenvalue, say $\alpha = 1$. That is excellent for the flat direction, and it diverges at once in the steep one.

**Follow-up.** *What does conjugate gradient do on this function?* It finishes in two steps, one for each distinct eigenvalue, in exact arithmetic. A direct run confirms it, with no tuning needed and only matrix products required.

**Why it matters.** Why learning rates must be tuned, and why ill conditioning rather than the dimension sets the cost, are the first lessons of optimisation.

**Question 20.11 ★★ QR, QD, Dev, QT • any • junior.**

Give an example in which Newton’s method on a root moves away from the root at each step, whatever the start. For $f(x) = \arctan x$, for what starting values does it fail? Why does solving for implied volatility need care, and how do you make a Newton iteration safe?

**Answer to Question 20.11.**

$f(x) = \operatorname{sign}(x)|x|^{1/3}$ gives $x \mapsto -2x$, which moves away from the root at every step. $\arctan$ fails when $|x_0| > 1.392$, and implied volatility is safe only with a good start or a bracket.

For the cube root, the update is $x - x^{1/3}/\bigl(\frac13 x^{-2/3}\bigr) = x - 3x = -2x$. The derivative is infinite at the root, so the tangent line always overshoots.

For the arctangent, the update is $x - (1 + x^2)\arctan x$. A start far out has a nearly flat tangent, which throws the next point further away. The boundary is the two-point cycle, $x \to -x$, which solves $\arctan x = 2x/(1 + x^2)$, at $x \approx 1.392$. Runs from $1.3$ and from $1.5$ converge and diverge as predicted.

Implied volatility has the same shape: the option price is flat at low volatility for a far out-of-the-money option, which means a tiny vega, and then the step blows up. With $F = 1$, $K = 1.5$, $T = 1$ and a true volatility of $0.2$, a start at $0.05$ jumps past 100 in one step. The price is convex in volatility up to an inflection point, here at $\sqrt{2\ln 1.5} = 0.90$, and concave beyond. A run started at the inflection falls steadily to the answer.

A safe general method keeps a bracket on the root and takes the Newton step only if it stays inside. Otherwise, it bisects.

**Classic wrong answer.** Assuming that a monotonic function makes Newton safe. Both functions above are monotonic.

**Follow-up.** *Bisecting from 0.01 to 5, how many steps reach a precision of $0.0001$?* 16, since $\log_2(4.99/0.0001) \approx 15.6$. That is slow but sure, and the bound does not depend on the option.

**Why it matters.** Solvers that fail on rare inputs are what cause production incidents. The candidate is expected to know when Newton is not safe and to have a fallback ready.

**Question 20.12 ★★ QR, QD, Dev, LLE • any • junior.**

In double precision, the smaller root of $x^2 - 10^8x + 1 = 0$ from the usual formula comes out wrong. What does it give, what is the true value, and how do you fix it? Why does $1 - \cos x$ fail for small $x$, and what do you use instead? Why does the plain formula for variance fail on large offset values, and what is a better method?

**Answer to Question 20.12.**

The formula returns about $7.45 \times 10^{-9}$ for a true $10^{-8}$; use $c/x_{\text{large}}$. Use $2\sin^2(x/2)$ for $1 - \cos x$. Use a running update such as Welford’s for the variance.

In the formula, $\sqrt{10^{16} - 4}$ is very close to $10^8$, and spacing between doubles there is about $1.5 \times 10^{-8}$. The subtraction therefore returns a small multiple of that spacing, and half of one spacing is the printed $7.45 \times 10^{-9}$: 25% wrong. This is cancellation: the subtraction itself is exact, but it exposes the error already in its inputs.

The fix uses the fact that the product of the roots is 1. Compute the large root, where the two terms add, and divide: $x_{\text{small}} = 1/x_{\text{large}}$, which gives $10^{-8}$ to full accuracy.

For $x = 10^{-8}$, $\cos x$ rounds to exactly 1, so $1 - \cos x$ returns 0, where the truth is $5 \times 10^{-17}$. The half-angle form involves no subtraction. Library functions such as `expm1` and `log1p` solve the same problem for their own cases.

For the variance, $\sum x^2 - (\sum x)^2/n$ subtracts two huge, nearly equal numbers. With values $10^{15} + 4$, $+7$, $+13$, $+16$, the true variance is 30, and the plain formula is off by more than 1. Welford’s update tracks the distance from the running mean and returns 30.

**Classic wrong answer.** Blaming the rounding of the last operation. The subtraction is exact. The fault is that the operands already carried errors, and the leading digits cancelled.

**Follow-up.** *Why does summing $10^{16}$, 1, 1, $-10^{16}$ in order give 0, and what returns 2?* Each 1 is exactly half the spacing at $10^{16}$, and the tie rounds back to the even neighbour, so it is lost. Kahan’s compensated summation carries the lost part into the next term and returns 2.

**Why it matters.** Numerical bugs give plausible numbers and pass most tests. The ability to find the operation that loses accuracy is what the question measures.

**Question 20.13 ★★ QR, QD, Dev • any • junior.**

You estimate a derivative with a forward difference in double precision. What step size is best, and what accuracy do you reach? How do both change for a central difference? Is there a way to reach almost full accuracy for an analytic function?

**Answer to Question 20.13.**

Forward difference: $h \approx 10^{-8}$, accuracy about $10^{-8}$. Central difference: $h \approx 6 \times 10^{-6}$, accuracy about $10^{-11}$. A complex step reaches almost full accuracy.

The forward difference has two errors. Truncation is about $h|f^{\prime\prime}|/2$, which shrinks with $h$. Rounding is about $\varepsilon|f|/h$, with $\varepsilon \approx 2.2 \times 10^{-16}$, which grows as $h$ shrinks. Balancing the two gives $h \sim \sqrt{\varepsilon} = 1.5 \times 10^{-8}$ and an error of about the same size, for a function and derivatives of order 1. So half the digits are lost, and they cannot be recovered by smaller steps. Too small a step makes things worse.

The central difference has truncation $h^2|f^{\prime\prime\prime}|/6$. Balancing with rounding $\varepsilon/h$ gives $h \sim \varepsilon^{1/3} \approx 6 \times 10^{-6}$ and an error around $\varepsilon^{2/3} \approx 4 \times 10^{-11}$. A sweep on $e^x$ at $x = 1$ shows both minima where predicted.

For an analytic function, $f(x + ih) = f(x) + ihf'(x) + O(h^2)$, so $\operatorname{Im} f(x + ih)/h$ gives the derivative with no subtraction, and hence no cancellation. A step of $10^{-30}$ is allowed, and the result is correct to about 15 digits. Automatic differentiation goes further and needs no analytic extension.

**Classic wrong answer.** Saying that the smaller the step, the better. With $h = 10^{-15}$ the forward difference is mostly rounding noise.

**Follow-up.** *What are the best step and the accuracy for the central second difference?* Here the rounding error is about $\varepsilon/h^2$, and truncation is $h^2$. So $h \sim \varepsilon^{1/4} \approx 10^{-4}$, with an accuracy of about $10^{-8}$. Half the digits are lost again.

**Why it matters.** Every Greek computed by bumping has this trade-off. A candidate who bumps by $10^{-12}$ has never looked at the result.

**Question 20.14 ★★★ QR, QT • hedge fund, market maker • junior, experienced.**

Compute $\int_0^{\pi/2} \ln \sin x\,dx$ with no series, using only symmetries of the sine and the double-angle formula.

**Answer to Question 20.14.**

The integral is $-\frac{\pi}{2}\ln 2 \approx -1.0888$.

Call it $I$. The integrand diverges at 0 only like $\ln x$, which is integrable, so $I$ is finite.

The reflection $x \to \pi/2 - x$ shows that $\int_0^{\pi/2}\ln\cos x\,dx = I$. Adding the two,

$$
2I = \int_0^{\pi/2}\ln(\sin x\cos x)\,dx = \int_0^{\pi/2}\ln\frac{\sin 2x}{2}\,dx = \int_0^{\pi/2}\ln\sin 2x\,dx - \frac{\pi}{2}\ln 2.
$$

Substitute $u = 2x$ in the remaining integral:

$$
\int_0^{\pi/2}\ln\sin 2x\,dx = \frac12\int_0^{\pi}\ln\sin u\,du.
$$

The sine is symmetric about $\pi/2$, so the integral on $[0, \pi]$ is $2I$, and the whole term is $I$. So $2I = I - \frac{\pi}{2}\ln 2$, which gives the result.

Two checks. The sign is right, because $\ln\sin x \le 0$ on the interval. And dividing by the length $\pi/2$ shows that the mean of $\ln\sin x$ is $-\ln 2$, so the geometric mean of the sine is exactly $\frac12$. That lies below the arithmetic mean, $2/\pi = 0.637$, as it must. Numerical integration gives the same number.

**Classic wrong answer.** Stopping at the term $\int\ln\sin 2x\,dx$ with no recognition that it is $I$ again. The self-reference is the whole trick. A second common error is forgetting the $\frac12$ from the substitution, which gives the wrong multiple.

**Follow-up.** *Compute $\int_0^{\pi/2} x\cot x\,dx$.* $\frac{\pi}{2}\ln 2$. Integrate by parts: the boundary term $x\ln\sin x$ vanishes at both ends, and what remains is $-\int\ln\sin x\,dx = \frac{\pi}{2}\ln 2$.

**Why it matters.** Reducing an integral to itself with a different coefficient is a recurring technique. The candidate must also be willing to carry out a substitution that seems to go nowhere.

**Question 20.15 ★★★ QR, QT • hedge fund • junior, experienced.**

Compute $\int_0^\infty \frac{\sin x}{x}\,dx$ by introducing a parameter: define $I(t) = \int_0^\infty e^{-tx}\frac{\sin x}{x}\,dx$, differentiate under the integral, and solve. What is $I(1)$? Why does the original integral converge, though not absolutely?

**Answer to Question 20.15.**

The integral is $\pi/2$, with $I(t) = \pi/2 - \arctan t$ and $I(1) = \pi/4$.

Differentiating under the integral removes the awkward $1/x$:

$$
I'(t) = -\int_0^\infty e^{-tx}\sin x\,dx = -\frac{1}{1 + t^2},
$$

where the last step is the imaginary part of a complex exponential integral. So $I(t) = c - \arctan t$. As $t \to \infty$, $I(t) \to 0$, which fixes $c = \pi/2$. Letting $t \to 0$ gives $\pi/2$. At $t = 1$, the value is $\pi/2 - \pi/4 = \pi/4$, and numerical integration at $t = \frac12, 1, 2$ agrees.

Convergence comes from cancellation. The arcs of $\sin x/x$ alternate in sign with falling size, so the partial integrals approach the limit with an error of about $1/R$. Absolutely, it diverges: over the $k$-th arc, $\int|\sin x|/x\,dx$ is about $2/(k\pi)$, a harmonic series growing like $\frac{2}{\pi}\ln R$.

Passing to $t = 0$ needs justifying because the integral is not absolutely convergent. The second mean value theorem applies, since $e^{-tx}/x$ falls with $x$: the tail beyond $R$ is at most $2/R$ for every $t \ge 0$. The convergence is therefore uniform in $t$, and $I$ is continuous at 0.

**Classic wrong answer.** Using dominated convergence directly at $t = 0$. There is no integrable dominating function, because $|\sin x|/x$ is not integrable. The uniform tail bound replaces it.

**Follow-up.** *What is $\int_0^\infty (\sin x/x)^2\,dx$?* Also $\pi/2$. Integrating by parts turns it into $\int_0^\infty \sin 2x / x\,dx$, which is the integral above after a rescaling. This time the convergence is absolute.

**Why it matters.** Feynman’s parameter trick is common in quant interviews because it rewards seeing a way in. The care about the limit at 0 separates the prepared candidates from the careful ones.

**Question 20.16 ★★★ QR, QT, DS • hedge fund • junior, experienced.**

Let $A$ be a symmetric positive definite $n$ by $n$ matrix. Compute $\int_{\mathbb{R}^n} e^{-\frac12 x^\top Ax}\,dx$. Deduce the covariance of the corresponding normal. Evaluate both for $A$ with rows $(2, 1)$ and $(1, 2)$. What is the sign of the correlation, and why?

**Answer to Question 20.16.**

The integral is $(2\pi)^{n/2}/\sqrt{\det A}$, and the covariance is $A^{-1}$. For the example, the integral is $2\pi/\sqrt{3} \approx 3.628$, the covariance is $\frac13$ times rows $(2, -1)$, $(-1, 2)$, and the correlation is $-\frac12$.

Diagonalise $A = Q\Lambda Q^\top$ and substitute $x = Qy$. Since $Q$ is orthogonal, the Jacobian is 1, and the exponent becomes $-\frac12\sum_i\lambda_iy_i^2$. The integral factorises:

$$
\prod_i\sqrt{\frac{2\pi}{\lambda_i}} = \frac{(2\pi)^{n/2}}{\sqrt{\det A}}.
$$

In the $y$ coordinates, $y_i$ is normal with variance $1/\lambda_i$, independently. So the covariance of $x$ is $Q\Lambda^{-1}Q^\top = A^{-1}$.

For the example, $\det A = 3$, which gives $2\pi/\sqrt{3}$; direct quadrature agrees. The inverse is the covariance stated, with variance $\frac23$ and covariance $-\frac13$, so the correlation is $-\frac12$.

The sign is the point to explain. The exponent contains the term $-xy$, with a positive entry in $A$. That term penalises $x$ and $y$ having the same sign and rewards opposite signs, so a positive entry in the precision matrix means a negative correlation. In general, the partial correlation of two variables is $-A_{ij}/\sqrt{A_{ii}A_{jj}}$, and a simulation of the normal confirms $-\frac12$.

**Classic wrong answer.** Reading the covariance off $A$ directly and saying that the correlation is $+\frac12$. $A$ is the inverse of the covariance, and for two variables the inversion flips the sign of the off-diagonal.

**Follow-up.** *Given $x_2 = 1$, what are the mean and variance of $x_1$?* Mean $-\frac12$ and variance $\frac12$. The conditional variance is $1/A_{11}$, read straight off the precision matrix, and the mean is $-A_{12}/A_{11}$ times the given value.

**Why it matters.** Gaussian integrals are behind every likelihood and every Bayesian update. The distinction between covariance and precision is often the one where a good candidate’s intuition goes wrong.

**Question 20.17 ★★★ QR, QT, DS, Risk • hedge fund • junior, experienced.**

You allocate one unit of capital across three strategies, with amounts $x_i \ge 0$, to maximise $\sum_i \ln(1 + x_i/n_i)$, with $n = (0.2, 0.5, 1)$. Write the optimality conditions, with the sign constraints, and solve. Why is the last strategy unfunded? How does the answer change if the capital is 3 units?

**Answer to Question 20.17.**

The solution is $x = (0.65, 0.35, 0)$ with common level $L = 0.85$. With 3 units, all three are funded, with $x \approx (1.37, 1.07, 0.57)$ and $L = 4.7/3 \approx 1.57$.

The derivative of $\ln(1 + x_i/n_i) = \ln(n_i + x_i) - \ln n_i$ is $1/(n_i + x_i)$. With a multiplier $\nu$ on the budget and non-negativity constraints, the optimality conditions are

$$
\frac{1}{n_i + x_i} \le \nu, \qquad x_i \ge 0, \qquad x_i\left(\nu - \frac{1}{n_i + x_i}\right) = 0, \qquad \sum_i x_i = 1.
$$

So every funded line has the same $n_i + x_i = L = 1/\nu$, and $x_i = \max(L - n_i, 0)$. This is called water-filling: capital is poured over levels $n_i$ until the budget is used up.

Try funding all three: $3L - 1.7 = 1$ gives $L = 0.9 < 1$, which makes $x_3$ negative. So fund two: $2L - 0.7 = 1$ gives $L = 0.85$, and then $x_1 = 0.65$, $x_2 = 0.35$. The third is correctly unfunded, because its marginal value at zero, $1/1$, is below $\nu = 1/0.85$. The last strategy is unfunded because even its first unit earns less than the current level on the others.

With 3 units, $3L - 1.7 = 3$ gives $L = 4.7/3$, above 1, so all three now receive capital. A numerical optimiser returns the same allocation.

**Classic wrong answer.** Solving the equal-marginal system with no sign constraint and reporting a negative amount, or setting the bad line to zero with no re-solving for the others. The budget must be re-split once any line drops out.

**Follow-up.** *At what capital does the third line start receiving money?* At $1.3$. It is funded once $L$ reaches 1, and at that level the first two lines hold $0.8 + 0.5 = 1.3$. Below this, all capital goes to the first two.

**Why it matters.** Sign constraints in allocation problems arise constantly, and the KKT conditions are what make them tractable. The candidate who can check the unfunded line has understood what the multiplier means.

**Question 20.18 ★★★ QR, QD, DS • any • junior, experienced.**

Prove that Newton’s method converges quadratically near a simple root. Show that convergence is only linear at a double root, by using $f(x) = (x - 1)^2$, and find the rate. How would you fix the double-root case if you know the multiplicity?

**Answer to Question 20.18.**

Near a simple root, the error satisfies $e_{n + 1} \approx \frac{f^{\prime\prime}(r)}{2f'(r)}e_n^2$. At the double root, it halves at each step, and using the step $x - 2f/f'$ restores fast convergence.

Let $r$ be the root and $e_n = x_n - r$. Taylor’s formula at $x_n$, with a remainder, gives

$$
0 = f(r) = f(x_n) - f'(x_n)e_n + \tfrac12 f^{\prime\prime}(\xi)e_n^2.
$$

Divide by $f'(x_n)$ and rearrange. Since $x_{n + 1} = x_n - f(x_n)/f'(x_n)$,

$$
e_{n + 1} = \frac{f^{\prime\prime}(\xi)}{2f'(x_n)}e_n^2.
$$

If $f$ is twice continuously differentiable and $f'(r) \ne 0$, the fraction is bounded by some $M$ on a neighbourhood of $r$. Once $M|e_n| < 1$, the errors fall and $|e_{n + 1}| \le M e_n^2$, which is quadratic convergence. On $x^2 - 2$ the ratio $e_{n + 1}/e_n^2$ tends to $1/(2\sqrt{2})$, as the formula predicts.

For a double root the proof fails, as $f'(r) = 0$. With $f = (x - 1)^2$, the step gives $x - (x - 1)/2$, so $e_{n + 1} = \frac12 e_n$: linear convergence with rate $\frac12$, one bit per step. A reduction of $10^6$ takes 20 steps instead of about 4. In general, for multiplicity $m$, the rate is $(m - 1)/m$.

If $m$ is known, take $x - mf/f'$. On the example, that lands on the root in one step, and in general it restores quadratic convergence.

**Classic wrong answer.** Assuming that Newton is always quadratic. Or proving it with no assumption on $f'(r)$. The denominator is the entire condition.

**Follow-up.** *What if the multiplicity is unknown, and how accurately can a double root be found in double precision?* Apply Newton to $u = f/f'$, which has a simple root at the same place. In double precision, $f$ itself is pure noise when $|x - r|$ is around $10^{-8}$, so only half the digits can be obtained.

**Why it matters.** This question distinguishes people who recall the slogan from those who can state its assumptions. A slow Newton iteration in practice is often a double root.

**Question 20.19 ★★★★ QR, QT • hedge fund • experienced.**

Show that $\sum_{k \ge 1} 1/k^2 = \pi^2/6$, by evaluating the double integral $\int_0^1\int_0^1 \frac{dx\,dy}{1 - xy}$ in two ways. Hint: expand as a geometric series for one route, and rotate the axes by 45 degrees for the other.

**Answer to Question 20.19.**

The geometric series gives $\sum_{k \ge 1} 1/k^2$, and the rotated integral gives $\pi^2/18 + \pi^2/9 = \pi^2/6$.

**Series route.** Expanding $1/(1 - xy) = \sum_{k \ge 0}(xy)^k$, with non-negative terms so the sum and integral can be swapped, each term integrates to $1/(k + 1)^2$.

**Rotation route.** Put $u = (x + y)/2$ and $v = (y - x)/2$, so that $x = u - v$, $y = u + v$, the Jacobian is 2, and $1 - xy = 1 - u^2 + v^2$. The unit square becomes a diamond with $|v| \le u$ for $u \le \frac12$ and $|v| \le 1 - u$ for $u \ge \frac12$. By symmetry in $v$, the integral is

$$
4\int_0^{1/2}\!\!\int_0^u \frac{dv}{1 - u^2 + v^2}\,du + 4\int_{1/2}^1\!\!\int_0^{1 - u}\frac{dv}{1 - u^2 + v^2}\,du.
$$

With $a = \sqrt{1 - u^2}$, the inner integral is $\frac1a\arctan(c/a)$, where $c$ is the upper limit.

In the first part, $\arctan(u/\sqrt{1 - u^2}) = \arcsin u$, so the part equals

$$
4\int_0^{1/2}\frac{\arcsin u}{\sqrt{1 - u^2}}\,du = 2\left(\arcsin\tfrac12\right)^2 = \frac{\pi^2}{18}.
$$

In the second, $\arctan\sqrt{(1 - u)/(1 + u)} = \frac12\arccos u$ by the half-angle formula, so the part equals

$$
2\int_{1/2}^1\frac{\arccos u}{\sqrt{1 - u^2}}\,du = \left(\arccos\tfrac12\right)^2 = \frac{\pi^2}{9}.
$$

The two add to $\pi^2/6$, and a direct partial sum confirms $1.6449$.

**Classic wrong answer.** Swapping the sum and the integral with no reason, or missing the Jacobian. The factor of 2 from the change of variables is the usual cause of a wrong constant.

**Follow-up.** *What is the sum of $1/k^2$ over odd $k$, and what does $6/\pi^2$ count?* $\pi^2/8$, because the even terms are a quarter of the whole. And $6/\pi^2 \approx 0.608$ is the limiting chance that two random integers are coprime; an exact count up to 1000 agrees to about two decimals.

**Why it matters.** This is a hard calculation done to a plan, and checks that the candidate can carry a change of variables on a non-trivial region to the end.

**Question 20.20 ★★★★ QR, QT • hedge fund • experienced.**

Prove that $n! \sim C\sqrt{n}(n/e)^n$ for some constant $C$, by showing a suitable sequence is monotone and bounded. Then find $C = \sqrt{2\pi}$ exactly by using Wallis’s result, $\lim_{n \to \infty} I_{2n}/I_{2n + 1} = 1$, with $I_n = \int_0^{\pi/2} \sin^n x\,dx$.

**Answer to Question 20.20.**

The sequence $d_n = \ln n! - (n + \frac12)\ln n + n$ falls and is bounded below, which gives the limit, and Wallis then fixes $C = \sqrt{2\pi}$.

**Monotone.** From the definition, $d_n - d_{n + 1} = (n + \frac12)\ln\frac{n + 1}{n} - 1$. Put $t = 1/(2n + 1)$, so $(n + 1)/n = (1 + t)/(1 - t)$ and $n + \frac12 = 1/(2t)$. The series for $\ln\frac{1 + t}{1 - t}$ gives

$$
d_n - d_{n + 1} = \sum_{k \ge 1}\frac{t^{2k}}{2k + 1} > 0.
$$

**Bounded.** Replacing each $2k + 1$ by 3 gives an upper bound of $\frac13\cdot t^2/(1 - t^2) = \frac{1}{12n(n + 1)}$, which is $\frac{1}{12n} - \frac{1}{12(n + 1)}$. So $d_n - \frac{1}{12n}$ rises with $n$. Since $d_1 = 1$, we get $d_n > \frac{11}{12}$ for all $n$. A falling sequence bounded below converges, so $n!/(\sqrt{n}(n/e)^n) \to C = e^{\lim d_n}$.

**The constant.** The reduction formula gives

$$
I_{2n} = \frac{\pi}{2}\frac{(2n)!}{2^{2n}(n!)^2}, \qquad I_{2n + 1} = \frac{2^{2n}(n!)^2}{(2n + 1)!}.
$$

Their ratio tends to 1, since $I_n$ falls and $I_{2n - 1}/I_{2n + 1} = (2n + 1)/(2n) \to 1$. Thus

$$
\frac{\pi}{2}\cdot\frac{(2n + 1)\,((2n)!)^2}{2^{4n}(n!)^4} \to 1.
$$

Now substitute $n! \sim C\sqrt{n}(n/e)^n$. All the powers cancel, $((2n)!)^2/(2^{4n}(n!)^4) \sim 2/(C^2n)$, and the limit becomes $2\pi/C^2 = 1$. So $C = \sqrt{2\pi}$.

**Classic wrong answer.** Using the sharper Stirling expansion to prove itself, or deriving the constant by Laplace’s method with no error control and calling this a proof. That is a good heuristic, but an interviewer who asks for monotonicity wants the argument above.

**Follow-up.** *How accurate is the formula with the correction $1 + 1/(12n)$ at $n = 10$?* It gives $3\,628\,685$ against the true $3\,628\,800$, an error of 30 parts per million. The plain formula gives $3\,598\,696$, an error of $0.8\%$. The $1/(12n)$ comes from the same bound.

**Why it matters.** The hardest level of the chapter. It tests whether the candidate can make a heuristic exact, and whether the classical limit can be applied with its assumptions in view.
