John D. Cook
banner
johndcook.mathstodon.xyz.ap.brid.gy
John D. Cook
@johndcook.mathstodon.xyz.ap.brid.gy
Consultant in applied mathematics and data privacy

https://www.johndcook.com

[bridged from https://mathstodon.xyz/@johndcook on the fediverse by https://fed.brid.gy/ ]
Four generalizations of the Pythagorean theorem

https://www.johndcook.com/blog/2025/11/13/pythagorean-generalizations/
November 13, 2025 at 3:31 PM
How to weight an average to minimize variance

https://www.johndcook.com/blog/2025/11/12/minimum-variance/
Weighting an average to minimize variance
Suppose you have $100 to invest in two independent assets, _A_ and _B_ , and you want to minimize volatility. Suppose _A_ is more volatile than _B_. Then putting all your money on _A_ would be the worst thing to do, but putting all your money on _B_ would not be the best thing to do. The optimal allocation would be some mix of _A_ and _B_ , with more (but not all) going to _B_. We will formalize this problem and determine the optimal allocation, then generalize the problem to more assets. ## Two variables Let _X_ and _Y_ be two independent random variables with finite variance and assume at least one of _X_ and _Y_ is not constant. We want to find _t_ that minimizes subject to the constraint 0 ≤ _t_ ≤ 1. Because _X_ and _Y_ are independent, Taking the derivative with respect to _t_ and setting it to zero shows that So the smaller the variance on _Y_ , the less we allocate to _X_. If _Y_ is constant, we allocate nothing to _X_ and go all in on _Y_. If _X_ and _Y_ have equal variance, we allocate an equal amount to each. If _X_ has twice the variance of _Y_ , we allocate 1/3 to _X_ and 2/3 to _Y_. ## Muliple variables Now suppose we have _n_ independent random variables _X_ _i_ for _i_ running from 1 to _n_ , and at least one of the variables is not constant. Then we want to minimize subject to the constraint and all _t_ _i_ non-negative. We can solve this optimization problem with Lagrange multipliers and find that for all 1 ≤ _i_ , _j_ ≤ _n_. These (_n_ − 1) equations along with the constraint that all the _t_ _i_ sum to 1 give us a system of equations whose solution is Incidentally, the denominator has a name: the (_n_ − 1)st elementary symmetric polynomial in _n_ variables. Here the variables are the variances. ## Related posts * Symmetric funcions and U-statistics * Regular solids and Monte Carlo integration
www.johndcook.com
November 12, 2025 at 1:06 PM
The graph below appears to show that the correlation between two stocks peaks about every 50 days. But this is purely an analysis artifact. The two series are in fact independent random walks. Shows how analysis can be misleading.

https://www.johndcook.com/blog/2025/11/09/rolling-correlation/
November 9, 2025 at 7:03 PM
An analog of Heron's formula that can be used to find the area of a spherical triangle.

https://www.johndcook.com/blog/2025/11/08/heron-on-a-sphere/
Analog of Heron’s formula on a sphere
The area of a triangle can be computed directly from the lengths of its sides via Heron’s formula. Here _s_ is the semiperimeter, _s_ = (_a_ + _b_ + _c_)/2. Is there an analogous formula for spherical triangles? It’s not obvious there should be, but there is a formula by Simon Antoine Jean L’Huilier (1750–1840). Here we denote area by _S_ for surface area, rather than _A_ because in the context of spherical trigonometry _A_ usually denotes the angle opposite side _a_. Now tan θ ≈ θ for small θ, and so L’Huilier’s formula reduces to Heron’s formula for small triangles. Imagine the Earth as a sphere of radius 1 and take a spherical triangle with one vertex at the north pole and two vertices on the equator 90° longitude apart. Such a triangle takes of 1/8 of the Earth’s surface area of 4π, so the area should be π/2. You can verify that L’Huilier’s formula gives the correct area. It’s not a proof, but it’s a good sanity check that L’Huilier’s formula is correct for small triangles and for at least one big triangle.
www.johndcook.com
November 9, 2025 at 2:11 AM
There's talk of building gigawatt data centers. How much is a gigawatt? https://www.johndcook.com/blog/2025/11/07/how-much-is-a-gigawatt/
November 7, 2025 at 2:17 PM
The sum of the inradii in any triangulation of a cyclic polygon is the same for all triangulations.

https://www.johndcook.com/blog/2025/11/05/japanese-polygon-theorem/
November 5, 2025 at 12:03 PM
The Pythagorean theorem squares the lengths of the sides in a right triangle. An analogous theorem squares the areas of faces of a right tetrahedron and generalizes to higher dimensions.

https://www.johndcook.com/blog/2025/11/03/de-gua/
November 3, 2025 at 4:44 PM
November 2, 2025 at 10:14 PM
November 2, 2025 at 1:13 PM
Cross ratio
The cross ratio of four points _A_ , _B_ , _C_ , _D_ is defined by where _XY_ denotes the length of the line segment from _X_ to _Y_. The idea of a cross ratio goes back at least as far as Pappus of Alexandria (c. 290 – c. 350 AD). Numerous theorems from geometry are stated in terms of the cross ratio. For example, the cross ratio of four points is unchanged under a projective transformation. ## Complex numbers The cross ratio of four (extended [1]) complex numbers is defined by The absolute value of the complex cross ratio is the cross ratio of the four numbers as points in a plane. The cross ratio is invariant under Möbius transformations, i.e. if _T_ is any Möbius transformation, then This is connected to the invariance of the cross ratio in geometry: Möbius transformations are projective transformations on a complex projective line. (More on that here.) If we fix the first three arguments but leave the last argument variable, then is the unique Möbius transformation mapping _z_ 1, _z_ 2, and _z_ 3 to ∞, 0, and 1 respectively. ## The anharmonic group Suppose (_a_ , _b_ ; _c_ , _d_) = λ ≠ 1. Then there are 4! = 24 permutations of the arguments and 6 corresponding cross ratios: Viewed as functions of λ, these six functions form a group, generated by This group is called the anharmonic group. Four numbers are said to be in harmonic relation if their cross ratio is 1, so the requirement that λ ≠ 1 says that the four numbers are anharmonic. The six elements of the group can be written as ## Hypergeometric transformations When I was looking at the six possible cross ratios for permutations of the arguments, I thought about where I’d seen them before: the linear transformation formulas for hypergeometric functions. These are, for example, equations 15.3.3 through 15.3.9 in A&S. They relate the hypergeometric function _F_(_a_ , _b_ ; _c_ ; _z_) to similar functions where the argument _z_ is replaced with one of the elements of the anharmonic group. I’ve written about these transformations before here. For example, There are deep relationships between hypergeometric functions and projective geometry, so I assume there’s an elegant explanation for the similarity between the transformation formulas and the anharmonic group, though I can’t say right now what it is. ## Related posts * Projective duality * Finite projective planes * Area of the unit disk after a Möbius transformation [1] For completeness we need to include a point at infinity. If one of the _z_ equals ∞ then the terms involving ∞ are dropped from the definition of the cross ratio.
www.johndcook.com
November 1, 2025 at 2:31 PM
Capitalization impacts the size of QR codes

https://www.johndcook.com/blog/2025/10/31/smaller-qr-codes/
October 31, 2025 at 3:55 PM
An ancient generalization of Pythagorean theorem

https://www.johndcook.com/blog/2025/10/30/apollonius-theorem/
October 30, 2025 at 1:11 PM
October 29, 2025 at 3:20 PM
How to mentally approximate logs base 2.
For example log_2 5 ≈ 7/3.

https://www.johndcook.com/blog/2025/10/29/estimating-log-base-2/
Mentally compute logs base 2
The previous post required computing After writing the post, I thought about how you would mentally approximate log2 5. The most crude approximation would round 5 down to 4 and use This is good enough for an order of magnitude guess, but we can do much better without too much more work. ## Simple approximation I’ve written before about the approximation for _x_ between 1/√2 and √2. We can write 5 as 4 (5/4) and so How accurate is this? The exact value of log2 5 is 2.3219…. Approximating this number by 7/3 is much better than approximating it by just 2, reducing the relative error from 16% down to 0.5%. ## Origin story Where did the approximation come from? I don’t remember where I found it. I wouldn’t be surprised if it was from something Ron Doerfler wrote. But how might someone have derived it? You’d like an approximation that works on the interval from 1/√2 to √2 because you can always multiply or divide by a power of 2 to reduce the problem to this interval. Rational approximations are the usual way to approximate functions over an interval [1], and for mental calculation you’d want to use the lowest order possible, i.e. degree 1 in the numerator and denominator. Here’s how we could ask Mathematica to find a rational approximation for us [2]. Simplify[ N[ ResourceFunction["EconomizedRationalApproximation"][ Log[2, x], { x, {1/Sqrt[2], Sqrt[2]}, 1, 1}]]] This returns (2.97035 _x_ − 2.97155) / (1.04593 + _x_) which we round off to (3 _x_ − 3) / (1 + _x_). The `N` function turns a symbolic result into one with floating point numbers. Without this call we get a complicated expression involving square roots and logs of rational numbers. The `Simplify` function returns an algebraically equivalent but simpler expression for its argument. In our case the function finishes the calculation by removing some parentheses. ## Related posts * Mentally computing common functions * Mentally multiply by π * Numbers worth remembering [1] Power series approximations are easier to compute, but power series approximations don’t give the best accuracy over an interval. Power series are excellent at the point where they’re centered, but degrade as you move away from the center. Rational approximations spread the error more uniformly. [2] I first tried using Mathematica’s `MiniMaxApproximation` function, but it ran into numerical problems, so I switched to `EconomizedRationalApproximation`.
www.johndcook.com
October 29, 2025 at 2:29 PM
October 28, 2025 at 2:09 PM
How blocks are chained in a blockchain, in full detail

https://www.johndcook.com/blog/2025/10/27/blockchain/
October 27, 2025 at 4:00 PM
I saw @ColinTheMathmo post the other day that 987654321/123456789 is very nearly 8, so I investigated to see how the result generalizes.

https://www.johndcook.com/blog/2025/10/26/987654321/
987654321 / 123456789
I recently saw someone post that 987654321/123456789 is very nearly 8, specifically 8.0000000729. I wondered whether there’s anything distinct about base 10 in this. For example, would the ratio of 54321six and 12345six be close to an integer? The ratio is 4.00268, which is pretty close to 4. What about a larger base? Let’s try base 16. The expression 0xFEDCBA987654321 / 0x123456789ABCDEF in Python returns 14. The exact ratio is not 14, but it’s as close to 14 as a standard floating point number can be. For a base _b_ , let denom(_b_) to be the number formed by concatenating all the digits in ascending order and let num(_b_) be the number formed by concatenating all the digits in descending order. Then for _b_ > 2 we have The following Python code demonstrates that this is true for _b_ up to 1000. num = lambda b: sum([k*b**(k-1) for k in range(1, b)]) denom = lambda b: sum([(b-k)*b**(k-1) for k in range(1, b)]) for b in range(3, 1001): n, d = num(b), denom(b) assert(n // d == b-2) assert(n % d == b-1) So for any base the ratio is nearly an integer, namely _b_ − 2, and the fractional part is roughly 1/_b_ _b_ −2. When _b_ = 16, as in the example above, the result is approximately 14 + 16−14 = 8 + 4 + 2 + 2−56 which would take 60 bits to represent exactly, but a floating point fraction only has 53 bits. That’s why our calculation returned exactly 14 with no fractional part.
www.johndcook.com
October 26, 2025 at 1:41 PM
Follow up to my post on the Smith chart:
How to adjust the spacing of the circles

https://www.johndcook.com/blog/2025/10/25/smith-chart-spacing/
October 25, 2025 at 2:57 PM
October 23, 2025 at 2:24 PM
October 22, 2025 at 1:07 PM
Random spherical coordinates
The topic of generating random points on a unit sphere has come up several times here. The standard method using normal random samples generates points (_x_ , _y_ , _z_) in Cartesian coordinates. If you wanted points in spherical coordinates, you could first generate points in Cartesian coordinates, then convert the points to spherical coordinates. But it would seem more natural, and possibly more efficient, to directly generate spherical coordinates (ρ, θ, ϕ). That’s what we’ll do in this post. Unfortunately there are several conventions for spherical coordinates, as described here, so we should start by saying we’re using the math convention that (ρ, θ, ϕ) means (radius, longitude, polar angle). The polar angle ϕ is 0 at the north pole and π at the south pole. Generating the first two spherical coordinate components is trivial. On a unit sphere, ρ = 1, and θ we generate uniformly on [0, 2φ]. The polar angle ϕ requires more thought. If we simply generated φ uniformly from [0, π] we’d put too many points near the poles and too few points near the equator. As I wrote about a few days ago, the _x_ , _y_ , and _z_ coordinates of points on a sphere are uniformly distributed. In particular, the _z_ coordinate is uniformly distributed, and so we need cos ϕ to be uniformly distributed, not ϕ itself. We can accomplish this by generating uniform points from [−1, 1] and taking the inverse cosine. Here’s Python code summarizing the algorithm for generating random points on a sphere in spherical coordinates. from numpy import random, pi, arccos def random_pt() # in spherical coordinates rho = 1 theta = 2*pi*random.random() phi = arccos(1 - 2*random.random()) return (rho, theta, phi) See the next post for a demonstration.
www.johndcook.com
October 22, 2025 at 1:05 PM
October 20, 2025 at 12:16 PM
October 18, 2025 at 7:55 PM
Turning trig identities into Fibonacci identities

https://www.johndcook.com/blog/2025/10/17/trig-fibonacci/
October 17, 2025 at 12:08 PM
Ethereum’s consensus layer elliptic curve: BLS12-381

https://www.johndcook.com/blog/2025/10/13/ethereum-bls12-381/
October 13, 2025 at 1:44 PM