Generating a uniform distribution

Suppose we want N independent uniform points on an interval, sorted from left to right, but want to generate them one at a time in order. We can do this without generating all N points and sorting them.

The conditional distribution

Let U(1)<U(2)<<U(N)U_{(1)} < U_{(2)} < \cdots < U_{(N)} be the order statistics of N independent Uniform(0,1)\operatorname{Uniform}(0,1) draws. Conditional on U(k+1)=uU_{(k+1)}=u, the k points below u are themselves uniform on [0,u][0,u]. Their maximum is U(k)U_{(k)}, so

U(k)uBeta(k,1).\frac{U_{(k)}}{u} \sim \operatorname{Beta}(k,1).

Equivalently, because 1Beta(1,k)Beta(k,1)1-\operatorname{Beta}(1,k) \sim \operatorname{Beta}(k,1),

U(k)=U(k+1)(1Bk),BkBeta(1,k).U_{(k)}=U_{(k+1)}\left(1-B_k\right), \qquad B_k\sim\operatorname{Beta}(1,k).

Set the artificial upper boundary U(N+1)=1U_{(N+1)}=1 and apply this relation for k=N,N1,,1k=N,N-1,\ldots,1. Each new point is sampled conditionally on the point immediately above it. This produces exactly the same joint distribution as sorting N independent uniform draws.

Code

import numpy as np

def uniform_ordinal(n, rng=np.random.default_rng()):
    points = np.empty(n)
    upper = 1.0
    for k in range(n, 0, -1):
        upper *= rng.beta(k, 1)
        points[k - 1] = upper
    return points

For an interval [a,b][a,b], transform the result with a+(ba)U(k)a+(b-a)U_{(k)}. The original code used the equivalent 1Beta(1,k)1-\operatorname{Beta}(1,k) form.

We can teach children to be more effectively empirical

We can teach children to smell bullshit - Vox

A good critical thinker is able to keep track of assumptions made when evaluating evidence, and over time builds up a collection of powerful and general assumptions that accelerate their accurate modelling of reality.

Double-blind tests make the least assumptions, but are not the mechanism by which most knowledge is obtained. Teaching children to avoid assumptions altogether is misguided - what they need is to introspect and make more aspects of their own thinking explicit and hence improvable.

Introspecting into motivations and detecting unrealistic wishful thinking is a large part of this. For example, someone with a privileged background has to account for their own propensity to adopt meritocratic world views that tell them they deserve what they have, and someone with a disadvantaged background has to be cautious about attributing too much to bad luck.

Powerful implicit motivations feed the spread of bullshit. Why is bullshit so attractive? What makes something feel like a good explanation? Confirmation of existing beliefs? Simplification of reality? These have to be balanced against "changing your mind" and "accepting that the world is complex and messy", depending on the strength of the evidence.

Nested condition variables are fun!

That release and acquire surrounding the wait are how you allow one of the timelines to start slipping relative to the other one.

def current_updates(self):
    with self.app.cond:
        self.app.cond.wait_for(
            lambda: self.app.positions_ready
        )
        while 1:
            if not self.app.isConnected():
                self.stop()
            if not self.running:
                break
            with self.cond:
                # Skip all position updates while dead-reckoning
                if self.dead_reckoning:
                    self.app.cond.release()
                    self.cond.wait_for(lambda: not self.dead_reckoning)
                    self.app.cond.acquire()
                current = self.app.positions.copy()
                if self.current != current:
                    self.current = current
                    self.cond.notify_all()
            self.app.cond.wait(timeout=2)

Condition variables

Original code on GitHub Gist