Two-Coin Bernoulli Factory Algorithm
- Two-coin Bernoulli factory algorithm is a method that uses i.i.d. coin flips from two unknown biases to simulate a Bernoulli variable with a prescribed function.
- It employs techniques such as logistic thinning and recursive splitting to efficiently simulate targets like linear combinations and ratios.
- Its efficiency, optimal flip usage in low-probability regimes, and extensions to quantum and robust distributed settings underscore its practical significance.
A two-coin Bernoulli factory is an algorithm that is given sample access to two coins with unknown biases and must output a single Bernoulli random variable with success probability equal to a prescribed function of those biases. In the standard formulation, one may flip the input coins as often as desired, use auxiliary randomness, and require exactness rather than approximation. Within this framework, the literature contains both a specific “2-coin algorithm” for simulating a ratio of the form and more general two-input factories for linear targets such as and for broader multiparameter classes of functions (Huber, 2015, Gonçalves et al., 2017).
1. Problem setting and terminological scope
The general Bernoulli-factory problem asks for an algorithm that uses coin flips together with auxiliary randomness to flip a single coin with probability of heads when only a coin with unknown probability of heads is available. In the two-input setting, the corresponding task is to simulate a Bernoulli variable with mean given only the ability to draw i.i.d. Bernoulli samples from two unknown biases and (Huber, 2015, Leme et al., 2022).
Across the two-input literature, three target forms are especially prominent. The first is the linear target
typically under a known constraint
The second is the ratio target
which appears directly in Barker-type accept/reject steps for intractable likelihoods. The third is the fully general multiparameter problem of determining which continuous functions 0 admit a finite-expected-time Bernoulli factory (Gonçalves et al., 2017, Leme et al., 2022).
| Target | Assumptions stated in the source material | Representative source |
|---|---|---|
| 1 | 2 | (Huber, 2015) |
| 3 | 4; exact simulation of Bernoulli5 and Bernoulli6 | (Gonçalves et al., 2017) |
| General 7 | continuity and polynomial bounds for both 8 and 9 on faces of 0 | (Leme et al., 2022) |
A common misconception is that a Bernoulli factory requires closed-form evaluation of 1 and 2. The two-coin constructions described here do not: the operative assumption is access to Bernoulli3 draws, not numerical knowledge of the biases themselves (Gonçalves et al., 2017).
2. Linear two-coin factory for 4
For the linear problem, the objective is to simulate a Bernoulli random variable 5 with
6
under the assumption that 7 is known, often with 8 or 9. The notation 0 is used throughout. The algorithm follows the same three-step “small-1” structure as in the univariate case (Huber, 2015).
The first step is logistic thinning. One produces
2
by embedding 3 as the rate of a thinned Poisson process. The subroutine LogisticBF2 simulates two independent Poisson processes of rates 4 and 5, thins them by the two coins, and compares the earliest retained event against an independent 6 clock. If
7
then
8
which is exactly the success probability required for the logistic coin (Huber, 2015).
The second step is a splitting construction based on a constant 9. The source material writes
0
Operationally, one samples
1
If 2 the algorithm returns 3; if 4 and 5 it returns 6; otherwise it recurses with inflated coefficients
7
In the pseudocode, 8 is chosen so that 9, and the implementation guide updates
0
before recursion (Huber, 2015).
The third step is tail recursion and termination. The probability of another recursive call shrinks by a fixed factor smaller than 1, so the procedure halts after a finite random number of recursions with probability 2. For very small 3, the routine may instead call the univariate SmallR_UnivariateBF with 4, effectively treating 5 as a single weighted input (Huber, 2015).
3. Correctness, stopping behavior, and efficiency
The linear two-coin factory is unbiased because the splitting identity decomposes 6 into an immediate-accept term and a recursive remainder. The same source gives the recursion probability bound
7
which yields almost-sure halting (Huber, 2015).
Its most prominent feature is the small-8 cost profile. Let 9 and 0. For 1, the source states that
2
In particular,
3
so to first order one uses 4 flips. The same source further states that information-theoretic lower bounds via the Cramér–Rao argument in §6 show that no Bernoulli-factory can use fewer than approximately 5 flips in the small-6 regime; on that basis, the two-coin linear factory is described as first-order optimal (Huber, 2015).
The large-7 regime is treated by imposing a known cutoff 8. Under that assumption, the expected cost is stated to be 9, improving substantially on older 0 factories. The same implementation notes recommend that, in practice, once 1 becomes say 2, one may switch to the classical Nacu–Peres factory for functions of the form 3; the resulting hybrid is stated to use at most approximately 4 flips when 5 (Huber, 2015).
This efficiency claim is specific to the linear target. A plausible implication is that “two-coin Bernoulli factory” should not be treated as a single complexity class: the expected cost depends sharply on the functional form of the target, the boundary regime, and the availability of structure such as linearity.
4. The ratio-form 2-coin algorithm and Barker acceptance
A second, widely cited meaning of “two-coin algorithm” is the Bernoulli-factory subroutine for
6
where 7 are known constants and 8 are unknown probabilities from which Bernoulli draws can be simulated (Gonçalves et al., 2017).
The algorithm is iterative and exceptionally simple. In each loop, one first flips
9
If 0, one flips a Bernoulli1 coin and returns 2 if that coin is 3; otherwise the loop continues. If 4, one flips a Bernoulli5 coin and returns 6 if that coin is 7; otherwise the loop continues. The source formulates correctness by defining the one-iteration output probabilities
8
with repeat probability 9. Summing the geometric series gives
0
and similarly
1
Hence the returned variable is exactly 2 (Gonçalves et al., 2017).
The expected cost is also explicit. The termination probability per loop is
3
so the number of loops is geometric with mean
4
Since each loop uses exactly one “5-coin” plus one “6-coin,” the expected total number of coin flips is
7
The source notes that the cost grows as 8 when 9 or 00 when 01 (Gonçalves et al., 2017).
In Bayesian computation, this ratio form is used to realize Barker’s acceptance rule for intractable target densities. With
02
the Barker acceptance probability can be written as
03
where 04 arise from unbiased simulation subroutines for the unknown target densities. The source emphasizes that this permits implementation of the “marginal Barker’s” instead of the extended state space pseudo-marginal Metropolis–Hastings. It also notes that Peskun ordering gives 05, and that the asymptotic variance of Barker is at worst twice that of Metropolis–Hastings, plus a 06 term (Gonçalves et al., 2017).
5. General two-input theory
The multiparameter theory of Bernoulli factories places the two-coin problem in a complete characterization framework. For 07, a Bernoulli factory that terminates almost surely with finite expectation everywhere exists if and only if two conditions hold: first, 08 is continuous on 09; second, both 10 and 11 are polynomially bounded on every face of the square in the sense specified by the boundary monomials associated with the face decomposition (Leme et al., 2022).
The source summarizes this criterion in words: on each face where 12 does not vanish identically, it can never dip below a fixed constant times the corresponding boundary-monomial to some power, and likewise for 13. It states that this exactly generalizes the one-coin Keane–O’Brien condition
14
This result is a structural characterization rather than a special-purpose construction for a single target (Leme et al., 2022).
For implementable 15, the source gives a general two-coin factory based on “recursive splitting plus Boolean test.” One precomputes a threshold-test sample size 16 and a helper-coin probability 17. A finite-depth subroutine 18 flips each input coin exactly 19 times, forms 20, computes the deterministic predicate
21
and outputs 22. The full factory then uses a geometric helper-coin mechanism with parameter 23 to mix calls to 24. The source states that the expected number of 25-calls is 26, each call to 27 uses 28 flips of the unknown coins, and therefore the expected total number of flips is 29 (Leme et al., 2022).
The same exposition gives two illustrative examples. For
30
the direct factory is simply: flip coin31 once; if 32 then flip coin33 once and output that result; otherwise output 34. The source states that the complexity is at most 35 flips. It also discusses
36
on the domain 37 as a further worked example (Leme et al., 2022).
A plausible implication of this characterization is that two-coin factories are best viewed not as isolated tricks but as instances of a boundary-sensitive realizability theory. The central obstruction is not the number of unknown coins by itself, but the interaction between continuity, boundary behavior, and exact simulation.
6. Extensions, robustness, and quantum implementations
Two-coin randomness-processing ideas also appear in adjacent settings that are not limited to Bernoulli targets. One example is the mechanism of jointly controlled lotteries with two biased coins. There, the goal is to implement a distribution on a finite set of elements so that even if the outcomes of one of the coins are determined by an adversary, the final distribution remains unchanged. The source describes both a fixed-length procedure that 38-approximates any target distribution 39 and an unbounded-length, almost-surely finite procedure that implements 40 exactly. It states that the fixed-length version uses 41 flips per coin, while the unbounded exact version uses expected 42 flips per coin with
43
and that both schemes terminate with probability 44 even if one coin is fully controlled (Solan et al., 2018).
A different extension is quantum. Randomness processing in a Bernoulli factory has been identified as a task for which quantum technology can be advantageous. An experimental photonic realization reports two quantum Bernoulli factories, one utilising quantum coherence and single-qubit measurements and the other using quantum coherence and entangling measurements of two qubits. The source states that the former consumes three orders of magnitude fewer resources than the best known classical method, while entanglement offers a further five-fold reduction. It further suggests applications to the simulation of stochastic processes and sampling tasks (Patel et al., 2018).
These directions clarify the broader significance of the two-coin Bernoulli-factory idea. In one direction, the framework supports exact accept/reject decisions in MCMC with intractable likelihoods; in another, it yields nearly optimal classical factories for linear functions of multiple unknown biases; in another, it connects to robust distributed randomization and to experimentally demonstrated quantum-enhanced randomness processing (Huber, 2015, Gonçalves et al., 2017, Patel et al., 2018).