bernoulli factory algorithms - github pages

60
Bernoulli Factory Algorithms This version of the document is dated 2021-10-02. Peter Occil Abstract: This page catalogs algorithms to turn coins biased one way into coins biased another way, also known as Bernoulli factories. It provides step-by-step instructions to help programmers implement these Bernoulli factory algorithms. This page also contains algorithms to exactly sample probabilities that are irrational numbers, using only random bits, which is related to the Bernoulli factory problem. This page is focused on methods that exactly sample a given probability without introducing new errors, assuming "truly random" numbers are available. The page links to a Python module that implements several Bernoulli factories. 2020 Mathematics Subject Classification: 68W20, 60-08, 60-04. 1 Introduction We're given a coin that shows heads with an unknown probability, λ. The goal is to use that coin (and possibly also a fair coin) to build a "new" coin that shows heads with a probability that depends on λ, call it f(λ). This is the Bernoulli factory problem. And this page catalogs algorithms to solve this problem for a wide variety of functions, algorithms known as Bernoulli factories. Many of these algorithms were suggested in (Flajolet et al., 2010) (1) , but without step-by- step instructions in many cases. This page provides these instructions to help programmers implement the Bernoulli factories they describe. The Python module bernoulli.py includes implementations of several Bernoulli factories. This page also contains algorithms to exactly sample probabilities that are irrational numbers, which is related to the Bernoulli factory problem. (An irrational number is a number that can't be written as a ratio of two integers.) Again, many of these algorithms were suggested in (Flajolet et al., 2010) (1) . This page is focused on methods that exactly sample the probability described, without introducing rounding errors or other errors beyond those already present in the inputs (and assuming that we have a source of independent and unbiased random bits). For extra notes, see: Supplemental Notes for Bernoulli Factory Algorithms 1.1 About This Document This is an open-source document; for an updated version, see the source code or its rendering on GitHub . You can send comments on this document on the GitHub issues page . See "Requests and Open Questions" for a list of things about this document that I seek answers to. I encourage readers to implement any of the algorithms given in this page, and report their implementation experiences. This may help improve this page.

Upload: others

Post on 17-Oct-2021

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bernoulli Factory Algorithms - GitHub Pages

BernoulliFactoryAlgorithmsThisversionofthedocumentisdated2021-10-02.

PeterOccil

Abstract:Thispagecatalogsalgorithmstoturncoinsbiasedonewayintocoinsbiasedanotherway,alsoknownasBernoullifactories.Itprovidesstep-by-stepinstructionstohelpprogrammersimplementtheseBernoullifactoryalgorithms.Thispagealsocontainsalgorithmstoexactlysampleprobabilitiesthatareirrationalnumbers,usingonlyrandombits,whichisrelatedtotheBernoullifactoryproblem.Thispageisfocusedonmethodsthatexactlysampleagivenprobabilitywithoutintroducingnewerrors,assuming"trulyrandom"numbersareavailable.ThepagelinkstoaPythonmodulethatimplementsseveralBernoullifactories.

2020MathematicsSubjectClassification:68W20,60-08,60-04.

1IntroductionWe'regivenacointhatshowsheadswithanunknownprobability,λ.Thegoalistousethatcoin(andpossiblyalsoafaircoin)tobuilda"new"cointhatshowsheadswithaprobabilitythatdependsonλ,callitf(λ).ThisistheBernoullifactoryproblem.

Andthispagecatalogsalgorithmstosolvethisproblemforawidevarietyoffunctions,algorithmsknownasBernoullifactories.

Manyofthesealgorithmsweresuggestedin(Flajoletetal.,2010)(1),butwithoutstep-by-stepinstructionsinmanycases.ThispageprovidestheseinstructionstohelpprogrammersimplementtheBernoullifactoriestheydescribe.ThePythonmodulebernoulli.pyincludesimplementationsofseveralBernoullifactories.

Thispagealsocontainsalgorithmstoexactlysampleprobabilitiesthatareirrationalnumbers,whichisrelatedtotheBernoullifactoryproblem.(Anirrationalnumberisanumberthatcan'tbewrittenasaratiooftwointegers.)Again,manyofthesealgorithmsweresuggestedin(Flajoletetal.,2010)(1).

Thispageisfocusedonmethodsthatexactlysampletheprobabilitydescribed,withoutintroducingroundingerrorsorothererrorsbeyondthosealreadypresentintheinputs(andassumingthatwehaveasourceofindependentandunbiasedrandombits).

Forextranotes,see:SupplementalNotesforBernoulliFactoryAlgorithms

1.1AboutThisDocumentThisisanopen-sourcedocument;foranupdatedversion,seethesourcecodeoritsrenderingonGitHub.YoucansendcommentsonthisdocumentontheGitHubissuespage.See"RequestsandOpenQuestions"foralistofthingsaboutthisdocumentthatIseekanswersto.

Iencouragereaderstoimplementanyofthealgorithmsgiveninthispage,andreporttheirimplementationexperiences.Thismayhelpimprovethispage.

Page 2: Bernoulli Factory Algorithms - GitHub Pages

2ContentsIntroduction

AboutThisDocumentContentsAboutBernoulliFactoriesAlgorithms

ImplementationNotesAlgorithmsforGeneralFunctionsofλ

CertainPolynomialsCertainRationalFunctionsCertainAlgebraicFunctionsCertainPowerSeriesGeneralFactoryFunctions

AlgorithmsforGeneralIrrationalConstantsDigitExpansionsContinuedFractionsContinuedLogarithmsCertainConvergingSeries

OtherGeneralAlgorithmsConvexCombinationsIntegralsGeneralizedBernoulliRace

AlgorithmsforSpecificFunctionsofλexp(−λ)(exp(λ)−1)/exp(−λ)exp(−(λk*c))exp(−(λ+m)k)exp(λ)*(1−λ)1/(2k+λ)orexp(−(k+λ)*ln(2))1/(2m*(k+λ))or1/((2m)*(k+λ))orexp(−(k+λ)*ln(2m))c*λ*β/(β*(c*λ+d*μ)−(β−1)*(c+d))c*λ/(c*λ+d)or(c/d)*λ/(1+(c/d)*λ))(d+λ)/cd/(c+λ)(d+μ)/(c+λ)(d+μ)/((d+μ)+(c+λ))dk/(c+λ)k,or(d/(c+λ))k

1/(1+λ)1/(2−λ)1/(1+(x/y)*λ)λ+μλ−μϵ/λμ/λλx/y

λμ

sqrt(λ)λ*x/y(λ*x/y)i

LinearBernoulliFactories

Page 3: Bernoulli Factory Algorithms - GitHub Pages

arctan(λ)/λarctan(λ)cos(λ)sin(λ)(1−λ)/cos(λ)(1−λ)*tan(λ)ln(1+λ)ln((c+d+λ)/c)arcsin(λ)+sqrt(1−λ2)−1arcsin(λ)/2ExpressionsInvolvingPolylogarithmsOtherFactoryFunctions

AlgorithmsforSpecificConstants1/φ(1dividedbythegoldenratio)sqrt(2)−11/sqrt(2)tanh(1/2)or(exp(1)−1)/(exp(1)+1)arctan(x/y)*y/xπ/12π/41/π(a/b)x/y

exp(−x/y)exp(−z)(a/b)z

1/(1+exp(x/(y*2prec))(LogisticExp)1/(1+exp(z/2prec))(LogisticExp)ζ(3)*3/4andOtherZeta-RelatedConstantserf(x)/erf(1)2/(1+exp(2))or(1+exp(0))/(1+exp(1))(1+exp(1))/(1+exp(2))(1+exp(k))/(1+exp(k+1))Euler–Mascheroniconstantγexp(−x/y)*z/tln(1+y/z)

RequestsandOpenQuestionsCorrectnessandPerformanceChartsAcknowledgmentsNotesAppendix

Randomizedvs.Non-RandomizedAlgorithmsBernoulliFactoriesandUnbiasedEstimationCorrectnessProoffortheContinuedLogarithmSimulationAlgorithmCorrectnessProofforContinuedFractionSimulationAlgorithm3ThevonNeumannSchemaProbabilitiesArisingfromCertainPermutationsSketchofDerivationoftheAlgorithmfor1/πCalculatingBoundsforexp(1)PreparingRationalFunctions

License

Page 4: Bernoulli Factory Algorithms - GitHub Pages

3AboutBernoulliFactoriesABernoullifactory(KeaneandO'Brien1994)(2)isanalgorithmthattakesaninputcoin(amethodthatreturns1,orheads,withanunknownprobability,or0,ortails,otherwise)andreturns0or1withaprobabilitythatdependsontheinputcoin'sprobabilityofheads.Theunknownprobabilityofheadsiscalledλinthisdocument.Forexample,aBernoullifactoryalgorithmcantakeacointhatreturnsheadswithprobabilityλandproduceacointhatreturnsheadswithprobabilityexp(−λ).

Afactoryfunctionisaknownfunctionthatrelatestheoldprobabilitytothenewone.Itsdomainistheclosedinterval[0,1]orasubsetofthatinterval,andreturnsaprobabilityin[0,1].

ManyBernoullifactoriesalsohaveaccesstooutsiderandomness(suchasasourceofunbiasedrandombits).AfactoryfunctionisstronglysimulableifitadmitsaBernoullifactorywhoseonlysourceofrandomnessistheinputcoin.

KeaneandO'Brien(1994)(2)showedthatafunctionfthatmaps[0,1](orasubsetofit)to[0,1]admitsaBernoullifactoryifandonlyif—

fisconstantonitsdomain,orfiscontinuousandpolynomiallyboundedonitsdomain(polynomiallyboundedmeansthatbothf(λ)and1−f(λ)areboundedfrombelowbymin(λn,(1−λ)n)forsomeintegern).

Thefollowingshowssomefunctionsthatarefactoryfunctionsandsomethatarenot.Inthetablebelow,ϵisanumbergreaterthan0andlessthan1/2.

Functionf(λ) Domain Canfbeafactoryfunction?0 [0,1] Yes;constant.1 [0,1] Yes;constant.1/2 (0,1) Yes;constant.floor(λ/2)*3+1/4 (0,1) No;discontinuous.

2*λ [0,1]or\0,1/2)

No;notpolynomiallyboundedsinceitsgraphtouches1somewhereintheinterval(0,1)onitsdomain.[(3).

1−2*λ [0,1]or[0,1/2)

No;notpolynomiallyboundedsinceitsgraphtouches0somewhereintheinterval(0,1)onitsdomain.

2*λ [0,1/2−ϵ]Yes;continuousandpolynomiallyboundedondomain(KeaneandO'Brien1994)(2).

min(2*λ,1−ϵ) [0,1]Yes;continuousandpolynomiallyboundedondomain(Huber2014,introduction)(4).

0ifλ=0,orexp(−1/λ)otherwise (0,1)

No;notpolynomiallyboundedsinceitmovesawayfrom0moreslowlythananypolynomial.

ϵifλ=0,orexp(−1/λ)+ϵotherwise

(0,1) Yes;continuousandboundedawayfrom0and1.

Iff'sdomainincludes0and/or1(sothattheinputcoinisallowedtoreturn0everytimeor1everytime,respectively),thenfcanbeafactoryfunctiononlyif—

1. fisconstantonitsdomain,oriscontinuousandpolynomiallyboundedonitsdomain,and

Page 5: Bernoulli Factory Algorithms - GitHub Pages

2. f(0)equals0or1whenever0isinthedomainoff,and3. f(1)equals0or1whenever1isinthedomainoff,

unlessoutsiderandomness(besidestheinputcoin)isavailable.

4AlgorithmsThissectionwillshowalgorithmsforanumberoffactoryfunctions,allowingdifferentkindsofprobabilitiestobesampledfrominputcoins.

Thealgorithmsasdescribedheredonotalwaysleadtothebestperformance.Animplementationmaychangethesealgorithmsaslongastheyproducethesameresultsasthealgorithmsasdescribedhere.

Mostofthealgorithmsassumethatasourceofindependentandunbiasedrandombitsisavailable,inadditiontotheinputcoins.Butinmanycases,theycanbeimplementedusingnothingbutthosecoinsasasourceofrandomness.Seetheappendixfordetails.

Bernoullifactoryalgorithmsthatsampletheprobabilityf(λ)actasunbiasedestimatorsoff(λ).Seetheappendixfordetails.

4.1ImplementationNotesThissectionshowsimplementationnotesthatapplytothealgorithmsinthisarticle.Theyshouldbefollowedtoavoidintroducingerrorinthealgorithms.

Inthefollowingalgorithms:

λistheunknownprobabilityofheadsoftheinputcoin.

choose(n,k)=(1*2*3*...*n)/((1*...*k)*(1*...*(n−k)))=n!/(k!*(n−k)!)isabinomialcoefficient,orthenumberofwaystochoosekoutofnlabeleditems.Itcanbecalculated,forexample,bycalculatingi/(n−i+1)foreachintegeriin[n−k+1,n],thenmultiplyingtheresults(Manolopoulos2002)(5).Notethatforeverym>0,choose(m,0)=choose(m,m)=1andchoose(m,1)=choose(m,m−1)=m;also,inthisdocument,choose(n,k)is0whenkislessthan0orgreaterthann.

n!=1*2*3*...*nisalsoknownasnfactorial.

Theinstructionto"generateauniform(0,1)randomvariate"canbeimplemented—

bycreatingauniformpartially-sampledrandomvariate(PSRN)withapositivesign,anintegerpartof0,andanemptyfractionalpart(mostaccurate),orbygeneratingauniformrandomvariateinthehalf-openinterval[**0,1)(e.g.,RNDRANGEMaxExc(0,1)in"RandomizationandSamplingMethods"(lessaccurate).

Theinstructionto"generateanexponentialrandomvariate"canbeimplemented—

bycreatinganemptyexponentialPSRN(mostaccurate),orbygettingtheresultoftheExpRandorExpRand2algorithm(describedinmyarticleonPSRNs)witharateof1,orbygenerating-ln(1/X),whereXisauniformrandomvariateinthehalf-openinterval(0,1],(e.g.,RNDRANGEMinExc(0,1)in"RandomizationandSampling

Page 6: Bernoulli Factory Algorithms - GitHub Pages

Methods")(lessaccurate).

Theinstructionto"choose[integers]withprobabilityproportionalto[weights]"canbeimplementedinoneofthefollowingways:

Iftheweightsarerationalnumbers,taketheresultofWeightedChoice(NormalizeRatios(weights))),whereWeightedChoiceandNormalizeRatiosaregivenin"RandomizationandSamplingMethods".IftheweightsareuniformPSRNs,usethealgorithmgivenin"WeightedChoiceInvolvingPSRNs".

Forexample,"Choose0,1,or2withprobabilityproportionaltotheweights[A,B,C]"meanstochoose0,1,or2atrandomsothat0ischosenwithprobabilityA/(A+B+C),1withprobabilityB/(A+B+C),and2withprobabilityC/(A+B+C).

Tosamplefromanumberumeanstogenerateanumberthatis1withprobabilityuand0otherwise.

IfthenumberisauniformPSRN,calltheSampleGeometricBagalgorithmwiththePSRNandtaketheresultofthatcall(whichwillbe0or1)(mostaccurate).(SampleGeometricBagisdescribedinmyarticleonPSRNs.)Otherwise,thiscanbeimplementedbygeneratinganotheruniform(0,1)randomvariatevandgenerating1ifvislessthanuor0otherwise(lessaccurate).

Whereanalgorithmsays"ifaislessthanb",whereaandbarerandomvariates,itmeanstoruntheRandLessalgorithmonthetwonumbers(iftheyarebothPSRNs),ordoaless-thanoperationonaandb,asappropriate.(RandLessisdescribedinmyarticleonPSRNs.)

Whereanalgorithmsays"ifaislessthan(orequalto)b",whereaandbarerandomvariates,itmeanstoruntheRandLessalgorithmonthetwonumbers(iftheyarebothPSRNs),ordoaless-than-or-equaloperationonaandb,asappropriate.

Whereastepinthealgorithmsays"withprobabilityx"torefertoaneventthatmayormaynothappen,thenthiscanbeimplementedinoneofthefollowingways:

Generateauniform(0,1)randomvariatev(seeabove).Theeventoccursifvislessthanx(seeabove).Convertxtoarationalnumbery/z,thencallZeroOrOne(y,z).Theeventoccursifthecallreturns1.Forexample,ifaninstructionsays"Withprobability3/5,return1",thenimplementitas"CallZeroOrOne(3,5).Ifthecallreturns1,return1."ZeroOrOneisdescribedinmyarticleonrandomsamplingmethods.Notethatifxisnotarationalnumber,thenroundingerrorwillresult.

Forbestresults,thealgorithmsshouldbeimplementedusingexactrationalarithmetic(suchasFractioninPythonorRationalinRuby).Floating-pointarithmeticisdiscouragedbecauseitcanintroduceerrorsduetofixed-precisioncalculations,suchasroundingandcancellations.

4.2AlgorithmsforGeneralFunctionsofλThissectiondescribesgeneral-purposealgorithmsforsamplingprobabilitiesthatarepolynomials,rationalfunctions,orfunctionsingeneral.

4.2.1CertainPolynomials

Page 7: Bernoulli Factory Algorithms - GitHub Pages

AnypolynomialcanbewritteninBernsteinformas∑i=0,...,nchoose(n,i)*λi*(1−λ)n−i*a[i],wherenisthepolynomial'sdegreeanda[i]areitsnplusonecoefficients.

ButtheonlypolynomialsthatadmitaBernoullifactoryarethosewhosecoefficientsareallintheinterval[0,1](oncethepolynomialsarewritteninBernsteinform),andthesepolynomialsaretheonlyfunctionsthatcanbesimulatedwithafixednumberofcoinflips(GoyalandSigman2012(6);Qianetal.2011)(7);seealsoWästlund1999,section4(8)).GoyalandSigmangiveanalgorithmforsimulatingthesepolynomials,whichisgivenbelow.

1. Fliptheinputcoinntimes,andletjbethenumberoftimesthecoinreturned1thisway.(9)

2. Returnanumberthatis1withprobabilitya[j],or0otherwise.

Forcertainpolynomialswithduplicatecoefficients,thefollowingisanoptimizedversionofthisalgorithm,notgivenbyGoyalandSigman:

1. Setjto0andito0.Ifnis0,return0.2. Ifiisnorgreater,orifthecoefficientsa[k],withkintheinterval[j,j+(n−i)],areall

equal,returnanumberthatis1withprobabilitya[j],or0otherwise.3. Fliptheinputcoin.Ifitreturns1,add1toj.4. Add1toiandgotostep2.

Andhereisanotheroptimizedalgorithm:

1. Setjto0andito0.Ifnis0,return0.Otherwise,generateauniform(0,1)randomvariate,callitu.

2. Ifuislessthanalowerboundofthelowestcoefficient,return1.Otherwise,ifuislessthan(orequalto)anupperboundofthehighestcoefficient,gotothenextstep.Otherwise,return0.

3. Ifiisnorgreater,orifthecoefficientsa[k],withkintheinterval[j,j+(n−i)],areallequal,returnanumberthatis1ifuislessthana[j],or0otherwise.

4. Fliptheinputcoin.Ifitreturns1,add1toj.5. Add1toiandgotostep3.

Becausethecoefficientsa[i]mustbeintheinterval[0,1],someorallofthemcanthemselvesbecoinswithunknownprobabilityofheads.Inthatcase,thefirstalgorithmcanreadasfollows:

1. Fliptheinputcoinntimes,andletjbethenumberoftimesthecoinreturned1thisway.

2. Ifa[j]isacoin,flipitandreturntheresult.Otherwise,returnanumberthatis1withprobabilitya[j],or0otherwise.

Notes:

1. Eacha[i]actsasacontrolpointfora1-dimensionalBéziercurve,whereλistherelativepositiononthatcurve,thecurvebeginsata[0],andthecurveendsata[n].Forexample,givencontrolpoints0.2,0.3,and0.6,thecurveisat0.2whenλ=0,and0.6whenλ=1.(Thecurve,however,isnotat0.3whenλ=1/2;ingeneral,Béziercurvesdonotcrosstheircontrolpointsotherthanthefirstandthelast.)

2. TheproblemofsimulatingpolynomialsinBernsteinformisrelatedtostochasticlogic,whichinvolvessimulatingprobabilitiesthatariseoutofBooleanfunctions(functionsthatuseonlyAND,OR,NOT,andXORoperations)thattakeafixednumberofbitsasinput,whereeachbithasa

Page 8: Bernoulli Factory Algorithms - GitHub Pages

separateprobabilityofbeing1ratherthan0,andoutputasinglebit(forfurtherdiscussionsee(Qianetal.2011)(7),QianandRiedel2008(10)).

3. Thesealgorithmscanserveasanapproximatewaytosimulateanycontinuousfunctionf(orevenanyfunctionthatmapstheinterval[0,1]to[0,1],evenifit'snotcontinuous).Inthiscase,a[j]iscalculatedasf(j/n),sothattheresultingpolynomialcloselyapproximatesthefunction.Infact,iffiscontinuous,it'spossibletochoosenhighenoughtoachieveagivenmaximumerror(thisisaresultoftheso-called"Weierstrassapproximationtheorem"),butthisisnotthecaseingeneraliffisnotcontinuous.Formoreinformation,seemySupplementalNotesonBernoulliFactories.

Examples:

1. TakethefollowingparabolicfunctiondiscussedinThomasandBlanchet(2012)(11):(1−4*(λ−1/2)2)*c,wherecisintheinterval(0,1).Thisisapolynomialofdegree2thatcanberewrittenas−4*c*λ2+4*c*λ,sothatthispowerformhascoefficients(0,4*c,−4*c)andadegree(n)of2.ByrewritingthepolynomialinBernsteinform(suchasviathematrixmethodbyRayandNataraj(2012)(12)),wegetcoefficients(0,2*c,0).Thus,forthispolynomial,a[0]is0,a[1]is2*c,anda[2]is0.Thus,ifcisintheinterval(0,1/2],wecansimulatethisfunctionasfollows:"Fliptheinputcointwice.Ifexactlyoneoftheflipsreturns1,returnanumberthatis1withprobability2*cand0otherwise.Otherwise,return0."Forothervaluesofc,thealgorithmrequiresrewritingthepolynomialinBernsteinform,thenelevatingthedegreeoftherewrittenpolynomialenoughtimestobringitscoefficientsin[0,1];therequireddegreeapproachesinfinityascapproaches1.(13)

2. Theconditionalconstruction,mentionedinFlajoletetal.(2010)(1),hastheform—(λ)*a[0]+(1−λ)*a[1].Thisisadegree-1polynomialinBernsteinformwithvariableλandcoefficientsa[0]anda[1].Thefollowingalgorithmsimulatesthispolynomial:"Fliptheλinputcoin.Iftheresultis0,flipthea[0]inputcoinandreturntheresult.Otherwise,flipthea[1]inputcoinandreturntheresult."Specialcasesoftheconditionalconstructionincludecomplement,mean,product,andlogicalOR;see"OtherFactoryFunctions".

Multiplecoins.Niazadehetal.(2021)(14)describesmonomials(involvingoneormorecoins)oftheformλ[1]a[1]*(1−λ[1])b[1]*λ[2]a[2]*(1−λ[2])b[2]*...*λ[n]a[n]*(1−λ[n])b[n],wheretherearencoins,λ[i]istheprobabilityofheadsofcoini,anda[i]≥0andb[i]≥0areparametersforcoini(specifically,ofa+bflips,thefirstaflipsmustreturnheadsandtherestmustreturntailstosucceed).

1. Foreachiin[1,n]:1. Fliptheλ[i]inputcoina[i]times.Ifanyoftheflipsreturns0,return0.2. Fliptheλ[i]inputcoinb[i]times.Ifanyoftheflipsreturns1,return0.

2. Return1.

Thesamepaperalsodescribespolynomialsthatareweightedsumsofthiskindofmonomials,namelypolynomialsoftheformP=∑j=1,...,kc[j]*M[j](λ),wheretherearekmonomials,M[j](.)identifiesmonomialj,λidentifiesthecoins'probabilitiesofheads,andc[j]≥0istheweightformonomialj.

Page 9: Bernoulli Factory Algorithms - GitHub Pages

LetCbethesumofallc[j].TosimulatetheprobabilityP/C,chooseoneofthemonomialswithprobabilityproportionaltoitsweight(see"WeightedChoiceWithReplacement"),thenrunthealgorithmaboveonthatmonomial(seealso"ConvexCombinations",later).

Thefollowingisaspecialcase:

Ifthereisonlyonecoin,thepolynomialsPareinBernsteinformifc[j]isα[j]*choose(k−1,j−1)whereα[j]isacoefficientintheinterval[0,1],andifa[1]=j−1andb[1]=k−jforeachmonomialj.

4.2.2CertainRationalFunctions

Arationalfunctionisaratioofpolynomials.

AccordingtoMosselandPeres(2005)(15),afunctionthatmapstheopeninterval(0,1)to(0,1)canbesimulatedbyafinite-statemachineifandonlyifthefunctioncanbewrittenasarationalfunctionwhosecoefficientsarerationalnumbers.

ThefollowingalgorithmissuggestedfromtheMosselandPerespaperandfrom(ThomasandBlanchet2012)(11).ItassumestherationalfunctionisoftheformD(λ)/E(λ),where—

D(λ)=∑i=0,...,nλi*(1−λ)n−i*d[i],E(λ)=∑i=0,...,nλi*(1−λ)n−i*e[i],everyd[i]islessthanorequaltothecorrespondinge[i],andeachd[i]andeache[i]isanintegerorrationalnumberintheinterval[0,choose(n,i)],wheretheupperboundisthetotalnumberofn-bitwordswithiones.

Here,d[i]isakintothenumberof"passing"n-bitwordswithiones,ande[i]isakintothatnumberplusthenumberof"failing"n-bitwordswithiones.

Thealgorithmfollows.

1. Fliptheinputcoinntimes,andletjbethenumberoftimesthecoinreturned1thisway.

2. Choose0,1,or2withprobabilityproportionaltotheseweights:[e[j]−d[j],d[j],choose(n,j)−e[j]].If0or1ischosenthisway,returnit.Otherwise,gotostep1.

Notes:

1. Intheformulasabove—

d[i]canbereplacedwithδ[i]*choose(n,i),whereδ[i]isarationalnumberintheinterval[0,1](andthusexpressestheprobabilitythatagivenwordisa"passing"wordamongalln-bitwordswithiones),ande[i]canbereplacedwithη[i]*choose(n,i),whereη[i]isarationalnumberintheinterval[0,1](andthusexpressestheprobabilitythatagivenwordisa"passing"or"failing"wordamongalln-bitwordswithiones),

andthenδ[i]andη[i]canbeseenascontrolpointsfortwodifferent1-dimensionalBéziercurves,wheretheδcurveisalwaysonor"below"theηcurve.Foreachcurve,λistherelativepositiononthatcurve,thecurvebeginsatδ[0]orη[0],andthecurveendsatδ[n]orη[n].Seealsothenextsection.

Page 10: Bernoulli Factory Algorithms - GitHub Pages

2. Thisalgorithmcouldbemodifiedtoavoidadditionalrandomnessbesidestheinputcoinflipsbypackingthecoinflipsintoann-bitwordandlookingupwhetherthatwordis"passing","failing",orneither,amongalln-bitwordswithjones,butthiscanbeimpractical(ingeneral,alookuptableofsize2nfirsthastobebuiltinasetupstep;asngrows,thetablesizegrowsexponentially).Moreover,thisapproachworksonlyifd[i]ande[i]areintegers(orifd[i]isreplacedwithfloor(d[i])ande[i]withceil(e[i])(NacuandPeres2005)(16),butthis,ofcourse,suffersfromroundingerrorwhendoneinthisalgorithm).Seealso(ThomasandBlanchet2012)(11).

3. Aswithpolynomials,thisalgorithm(ortheonegivenlater)canserveasanapproximatewaytosimulateanyfactoryfunction,viaarationalfunctionthatcloselyapproximatesthatfunction.Thehighernis,thebetterthisapproximation,andingeneral,adegree-nrationalfunctionapproximatesagivenfunctionbetterthanadegree-npolynomial.However,toachieveagivenerrortolerancewitharationalfunction,thedegreenaswellasd[i]ande[i]havetobeoptimized.Thisisunlikethepolynomialcasewhereonlythedegreenhastobeoptimized.

Example:Takethefunctionf(λ)=1/(λ−2)2.Thisisarationalfunction,inthiscasearatiooftwopolynomialsthatarebothnon-negativeontheinterval[0,1].Onealgorithmtosimulatefis:(1)Fliptheinputcointwice,andletheadsbethenumberoftimesthecoinreturned1thisway.(2)Dependingonheads,choose0,1,or2withprobabilityproportionaltothefollowingweights:heads=0→[3,1,0],heads=1→[1,1,2],heads=2→[0,1,3];if0or1ischosenthisway,returnit;otherwise,gotostep1.Hereishowfwaspreparedtoderivethisalgorithm:(1)Takethenumerator1,andthedenominator(λ−2)2.Rewritethedenominatoras1*λ2−4*λ+4.(2)Rewritethenumeratoranddenominatorintohomogeneouspolynomials(polynomialswhosetermshavethesamedegree)ofdegree2;seethe"homogenizing"sectionin"PreparingRationalFunctions".Theresultis(1,2,1)and(4,4,1)respectively.(3)Dividebothpolynomialsby4(themaximumcoefficient)sothattheyareall1orless.Theresultisd=(1/4,1/2,1/4),e=(1,1,1/4).(4)Preparetheweightsasgiveninstep2oftheoriginalalgorithm.Theresultis[3/4,1/4,0],[1/2,1/2,1],and[0,1/4,3/4],fordifferentcountsofheads.Theseweightscanbesimplifiedinthiscasetointegerswithoutaffectingthealgorithm:[3,1,0],[1,1,2],[0,1,3],respectively.

"DiceEnterprise"specialcase.Thefollowingalgorithmimplementsaspecialcaseofthe"DiceEnterprise"methodofMorinaetal.(2019)(17).Thealgorithmreturnsoneofmoutcomes(namelyX,anintegerin[0,m))withprobabilityPX(λ)/(P0(λ)+P1(λ)+...+Pm−1(λ)),whereλistheinputcoin'sprobabilityofheadsandmis2orgreater.Specifically,theprobabilityisarationalfunction,orratioofpolynomials.Here,allthePk(λ)areintheformofpolynomialsasfollows:

Thepolynomialsarehomogeneous,thatis,theyarewrittenas∑i=0,...,nλi*(1−λ)n−i*a[i],wherenisthepolynomial'sdegreeanda[i]isacoefficient.Thepolynomialshavethesamedegree(namelyn)andalla[i]are0orgreater.Thesumofjthcoefficientsisgreaterthan0,foreachjstartingat0andendingatn,exceptthatthelistofsumsmaybeginand/orendwithzeros.CallthislistR.For

Page 11: Bernoulli Factory Algorithms - GitHub Pages

example,thisconditionholdstrueifRis(2,4,4,2)or(0,2,4,0),butnotifRis(2,0,4,3).

AnyrationalfunctionthatadmitsaBernoullifactorycanbebroughtintotheformjustdescribed,asdetailedintheappendixunder"PreparingRationalFunctions".Inthisalgorithm,letR[j]bethesumofjthcoefficientsofthepolynomials(withjstartingat0).First,definethefollowingoperation:

Getthenewstategivenstate,b,u,andn:1. Ifstate>0andbis0,returneitherstate−1ifuislessthan(orequalto)PA,or

stateotherwise,wherePAisR[state−1]/max(R[state],R[state−1]).2. Ifstate<nandbis1,returneitherstate+1ifuislessthan(orequalto)PB,or

stateotherwise,wherePBisR[state+1]/max(R[state],R[state+1]).3. Returnstate.

Thenthealgorithmisasfollows:

1. Createtwoemptylists:blistandulist.2. Setstate1tothepositionofthefirstnon-zeroiteminR.Setstate2tothepositionof

thelastnon-zeroiteminR.Inbothcases,positionsstartat0.IfalltheitemsinRarezeros,return0.

3. Fliptheinputcoinandappendtheresult(whichis0or1)totheendofblist.Generateauniform(0,1)randomvariateandappendittotheendofulist.

4. (Monotoniccouplingfromthepast(Morinaetal.,2019)(17),(ProppandWilson1996)(18).)Setitothenumberofitemsinblistminus1,thenwhileiis0orgreater:1. Letbbetheitematpositioni(startingat0)inblist,andletubetheitemat

thatpositioninulist.2. Getthenewstategivenstate1,b,u,andn,andsetstate1tothenewstate.3. Getthenewstategivenstate2,b,u,andn,andsetstate2tothenewstate.4. Subtract1fromi.

5. Ifstate1andstate2arenotequal,gotostep2.6. Letb(j)becoefficienta[state1]ofthepolynomialforj.Chooseanintegerin[0,m)

withprobabilityproportionaltotheseweights:[b(0),b(1),...,b(m−1)].Thenreturnthechoseninteger.

Notes:

1. Ifthereareonlytwooutcomes,thenthisisthespecialBernoullifactorycase;thealgorithmwouldthenreturn1withprobabilityP1(λ)/(P0(λ)+P1(λ)).

2. IfR[j]=choose(n,j),steps1through5havethesameeffectascountingthenumberofonesfromninputcoinflips(whichwouldbestoredinstate1inthiscase),butunfortunately,thesestepswouldn'tbemoreefficient.Inthiscase,PAisequivalentto"1ifstateisgreaterthanfloor(n/2),andstate/(n+1−state)otherwise",andPBisequivalentto"1ifstateislessthanfloor(n/2),and(n−state)/(state+1)otherwise".

Example:LetP0(λ)=2*λ*(1−λ)andP1(λ)=(4*λ*(1−λ))2/2.Thegoalistoproduce1withprobabilityP1(λ)/(P0(λ)+P1(λ)).Afterpreparingthisfunction(andnotingthatthemaximumdegreeisn=4),wegetthecoefficientsumsR=(0,2,12,2,0).SinceRbeginsandendswith0,step2ofthealgorithmsetsstate1andstate2,respectively,tothepositionofthefirstorlastnonzeroitem,namely1or3.(Alternatively,becauseRbeginsandendswith0,wecouldincludeathirdpolynomial,namelytheconstantP2(λ)=0.001,sothatthenewcoefficientsumswouldbeR′=(0.001,10.004,12.006,2.006,0.001)

Page 12: Bernoulli Factory Algorithms - GitHub Pages

[formedbyaddingthecoefficient0.001*choose(n,i)tothesumati,startingati=0].NowwewouldrunthealgorithmusingR′,andifitreturns2[meaningthattheconstantpolynomialwaschosen],wewouldtryagainuntilthealgorithmnolongerreturns2.)

4.2.3CertainAlgebraicFunctions

(Flajoletetal.,2010)(1)showedhowcertainfunctionscanbesimulatedbygeneratingabitstringanddeterminingwhetherthatbitstringbelongstoacertainclassofbitstrings.Therulesfordeterminingwhetherabitstringbelongstothatclassarecalledabinarystochasticgrammar,whichusesanalphabetofonlytwo"letters",ormoregenerallyastochasticgrammar.Thefunctionsbelongtoaclasscalledalgebraicfunctions(functionsthatcanbeasolutionofapolynomialequation).

Accordingto(MosselandPeres2005)(15),afactoryfunctioncanbesimulatedbyapushdownautomaton(astatemachinewithastack)onlyifthatfunctioncanbeasolutionofapolynomialequationwhosecoefficientsarerationalnumbers.

Thefollowingalgorithmsimulatesthefollowingalgebraicfunction:

∑k=0,1,2,...(λk*(1−λ)*W(k)/βk),oralternatively,(1−λ)*OGF(λ/β),

where—

W(k)isanumberintheinterval[0,βk],andinmanycasesisthenumberofk-letterwordsthatcanbeproducedbythestochasticgrammarinquestion,βisthealphabetsize,orthenumberof"letters"inthealphabet(e.g.,2forthecasesdiscussedintheFlajoletpaper),andisaninteger2orgreater,theordinarygeneratingfunctionOGF(x)=W(0)+W(1)*x+W(2)*x2+W(3)*x3+...,andthesecondformulaincorporatesacorrectiontoTheorem3.2ofthepaper(19).

(Here,thekthcoefficientofOGF(x)correspondstoW(k).)Thealgorithmfollows.

1. Fliptheinputcoinrepeatedlyuntilitreturns0.Setgtothenumberoftimesthecoinreturned1thisway.

2. Returnanumberthatis1withprobabilityW(g)/βg,and0otherwise.(IntheFlajoletpaper,thisisdonebygeneratingag-letterworduniformlyatrandomand"parsing"thatwordusingabinarystochasticgrammartodeterminewhetherthatwordcanbeproducedbythatgrammar.Notethatthisdeterminationcanbemadethiswayaseachoftheword's"letters"isgenerated.)

Anextensiontothisalgorithm,notmentionedintheFlajoletetal.paper,istheuseofstochasticgrammarswithabiggeralphabetthantwo"letters".Forexample,inthecaseofternarystochasticgrammars,thealphabetsizeis3andβis3inthealgorithmabove.Ingeneral,forβ-arystochasticgrammars,thealphabetsizeisβ,whichcanbeanyinteger2orgreater.

Note:TheradiusofconvergenceofOGFisthegreatestnumberρsuchthatthefunctionisdefinedontheinterval[0,ρ).Inthisalgorithm,theradiusofconvergenceisintheinterval[1/β,1](Flajolet1987)(20).Forexample,theOGFinvolvedinthesquarerootconstructiongivenintheexamplesbelowhasradiusofconvergence1/2.

Page 13: Bernoulli Factory Algorithms - GitHub Pages

Examples:

1. ThefollowingisanexamplefromtheFlajoletetal.paper.Ag-letterbinarywordcanbe"parsed"asfollowstodeterminewhetherthatwordencodesaternarytree:"2.Ifgis0,return0.Otherwise,setito1anddto1.;2a.Generateanunbiasedrandombit(thatis,either0or1,chosenwithequalprobability),thensubtract1fromdifthatbitis0,oradd2todotherwise.;2b.Add1toi.Then,ifi<gandd>0,gotostep3a.;2c.Return1ifdis0andiisg,or0otherwise."

2. IfW(g),thenumberofg-letterwordsthatcanbeproducedbythestochasticgrammarinquestion,hastheform—

choose(g,g/t)*(β−1)g−g/t(thenumberofg-letterwordswithexactlyg/tA's,foranalphabetsizeofβ)ifgisdivisiblebyt(21),and0otherwise,

wheretisaninteger2orgreaterandβisthealphabetsizeandisaninteger2orgreater,step2ofthealgorithmcanbedoneasfollows:"2.Ifgisnotdivisiblebyt,return0.Otherwise,generateguniformrandomintegersintheinterval0,β)(e.g.,gunbiasedrandombitsifβis2),thenreturn1ifexactlyg/tzerosweregeneratedthisway,or0otherwise."Ifβ=2,thenthisreproducesanotherexamplefromtheFlajoletpaper.

3. IfW(g)hastheform—choose(g*α,g)*(β−1)g*α−g/βg*α−g,whereαisaninteger1orgreaterandβisthealphabetsizeandisaninteger2orgreater[(22),step2ofthealgorithmcanbedoneasfollows:"2.Generateg*αuniformrandomintegersintheinterval0,β)(e.g.,g*αunbiasedrandombitsifβis2),thenreturn1ifexactlygzerosweregeneratedthisway,or0otherwise."Ifα=2andβ=2,thenthisexpressesthesquare-rootconstructionsqrt(1−λ),mentionedintheFlajoletetal.paper.Ifαis1,themodifiedalgorithmsimulatesthefollowingprobability:(β*(λ−1))/(λ−β).Andinterestingly,Ihavefoundthatifαis2orgreater,theprobabilitysimplifiestoinvolveahypergeometricfunction.Specifically,theprobabilitybecomes—

(1−λ)*α−1Fα−2(1/α,2/α,...,(α−1)/α;1/(α−1),...,(α−2)/(α−1);λ*αα/((α−1)α−1*2α))ifβ=2,ormoregenerally,(1−λ)*α−1Fα−2(1/α,2/α,...,(α−1)/α;1/(α−1),...,(α−2)/(α−1);λ*αα*(β−1)α−1/((α−1)α−1*βα)).

Theordinarygeneratingfunctionforthismodifiedalgorithmisthus—OGF(z)=α−1Fα−2(1/α,2/α,...,(α−1)/α;1/(α−1),...,(α−2)/(α−1);z*αα*(β−1)α−1/((α−1)α−1*βα−1)).

4. Theprobabilityinvolvedinexample2likewiseinvolveshypergeometricfunctions:

(1−λ)*t−1Ft−2(1/t,2/t,...,(t−1)/t;1/(t−1),...,(t−2)/(t−1);λt*tt*(β−1)t−1/((t−1)t−1*βt)).

4.2.4CertainPowerSeries

Page 14: Bernoulli Factory Algorithms - GitHub Pages

Mendo(2019)[(23)gaveaBernoullifactoryalgorithmforcertainfunctionsthatcanberewrittenasapowerseries.Thealgorithmusesparametervandisgivenbelow.Atableofsupportedpowerserieswillfollowthealgorithm.

1. Setdsumto0andito0.2. Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:

1. Fliptheinputcoin.Ifitreturnsv,return1.2. Ifthisisaninfiniteseriesorifiislessthanthenumberofcoefficients:

Withprobabilityc[i]/(1−dsum),return0.Otherwise,addc[i]todsum.3. Add1toi.

Atableofsupportedpowerseriesfollows:

PowerSeries Algorithmf(λ)=1−(c[0]*(1−λ)+...+c[i]*(1−λ)i+1+...)

WithprobabilityCS,runthealgorithmabovewithv=1andreturntheresult.Otherwise,return0.

f(λ)=(c[0]*(1−λ)+...+c[i]*(1−λ)i+1+...)

WithprobabilityCS,runthealgorithmabovewithv=1andreturn1minustheresult.Otherwise,return1.

f(λ)=(c[0]*λ+...+c[i]*λi+1+...)

WithprobabilityCS,runthealgorithmabovewithv=0andreturn1minustheresult.Otherwise,return0.

f(λ)=1−(c[0]*λ+...+c[i]*λi+1+...)

WithprobabilityCS,runthealgorithmabovewithv=0andreturntheresult.Otherwise,return1.

Inthetableabove,c[i]≥0arethecoefficientsoftheseries.CSisthesumofallthecoefficientsandmustbe1orless.(AccordingtoMendo,thisimpliesthattheseriesisdifferentiable—itsgraphhasno"sharpcorners".)

(Łatuszyńskietal.2009/2011)(24)gaveanalgorithmthatworksforawideclassofseriesandotherconstructsthatconvergetothedesiredprobabilityfromaboveandfrombelow.

Oneofthesecasesiswhenf(λ)canbewrittenas—

f(λ)=d[0]−d[1]*λ+d[2]*λ2−...,

whichisanalternatingserieswhered[i]areallintheinterval[0,1]andformanonincreasingsequenceofcoefficients,andf(1)mustconvergetoanumberinthehalf-openinterval[0,1).

Thefollowingisthegeneralalgorithmforthiskindofseries,calledthegeneralmartingalealgorithm.Ittakesalistofcoefficientsandaninputcoin,andreturns1withtheprobabilitygivenbytheseriesabove,and0otherwise.

1. Letd[0],d[1],etc.bethefirst,second,etc.coefficientsofthealternatingseries.Setutod[0],setwto1,setℓto0,andsetnto1.

2. Generateauniform(0,1)randomvariateret.3. Ifwisnot0,fliptheinputcoinandmultiplywbytheresultoftheflip.4. Ifniseven,setutoℓ+w*d[n].Otherwise,setℓtou−w*d[n].5. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenextstep.If

neitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

6. Add1tonandgotostep3.

Page 15: Bernoulli Factory Algorithms - GitHub Pages

Anothercaseiswhenf(λ)canbewrittenas—

f(λ)=d[0]−d[1]*λ2+d[2]*λ4−...,

which,again,isanalternatingserieswhered[i]areallintheinterval[0,1]andformanonincreasingsequenceofcoefficients,andf(1)mustconvergetoanumberinthehalf-openinterval0,1).Inthatcase,modifythegeneralmartingalealgorithmbyaddingthefollowingafterstep3:"3a.Repeatstep3once."(Examplesofthiskindofseriesarefoundinsin(λ)andcos(λ).)

(NacuandPeres2005,proposition16)[(16).Thealgorithmbelowsimulatesafunctionoftheform—

f(λ)=d[0]+d[1]*λ+d[2]*λ2+...,

whereeachd[i]is0orgreater,andtakesthefollowingparameters:

tisarationalnumberintheinterval(B,1]suchthatf(t)<1.ϵisarationalnumberintheinterval(0,(t−B)/2).

Bisnotaparameter,butisthemaximumallowedvalueforλ(probabilityofheads)andintheinterval(0,1).

1. Createaνinputcointhatdoesthefollowing:"(1)Setnto0.(2)Withprobabilityϵ/t,gotothenextsubstep.Otherwise,add1tonandrepeatthissubstep.(3)Withprobability1−d[n]*tn,return0.(4)RunalinearBernoullifactoryntimes,usingthe(λ)inputcoin,x/y=1/(t−ϵ),andϵ=ϵ.Ifanyoftheserunsreturns0,return0.Otherwise,return1."

2. RunalinearBernoullifactoryonce,usingtheνinputcoindescribedearlier,x/y=t/ϵ,andϵ=ϵ,andreturntheresult.

4.2.5GeneralFactoryFunctions

Acoinwithunknownprobabilityofheadsofλcanbeturnedintoacoinwithprobabilityofheadsoff(λ),wherefisanyfactoryfunction,viaanalgorithmthatbuildsrandomizedboundsonf(λ)basedontheoutcomesofthecoinflips.Theserandomizedboundscomefromtwosequencesofpolynomials:

Onesequenceofpolynomialsconvergesfromabovetof,theotherfrombelow.Foreachsequence,thepolynomialsmusthaveincreasingdegree.ThepolynomialsarewritteninBernsteinform(see"CertainPolynomials").Foreachsequence,thedegree-npolynomials'coefficientsmustlieator"inside"thoseofthepreviousupperpolynomialandthepreviouslowerone(oncethepolynomialsareelevatedtodegreen).Thisisalsocalledtheconsistencyrequirement.

Thissectionsetsforthtwoalgorithmstosimulatefactoryfunctionsviapolynomials.Inbothalgorithms:

fbelow(n,k)isalowerboundofthekthcoefficientforadegree-npolynomialinBernsteinformthatapproximatesffrombelow,wherekisintheinterval[0,n].Forexample,thiscanbef(k/n)minusaconstantthatdependsonn.(Seenote3below.)fabove(n,k)isanupperboundofthekthcoefficientforadegree-npolynomialinBernsteinformthatapproximatesffromabove.Forexample,thiscanbef(k/n)plusaconstantthatdependsonn.(Seenote3.)

Page 16: Bernoulli Factory Algorithms - GitHub Pages

Thefirstalgorithmimplementsthereverse-timemartingaleframework(Algorithm4)inŁatuszyńskietal.(2009/2011)(24)andthedegree-doublingsuggestioninAlgorithmIofFlegalandHerbei(2012)(25),althoughanerrorinAlgorithmIisnotedbelow.Thefirstalgorithmfollows.

1. Generateauniform(0,1)randomvariate,callitret.2. Setℓandℓtto0.Setuandutto1.Setlastdegreeto0,andsetonesto0.3. Setdegreesothatthefirstpairofpolynomialshasdegreeequaltodegreeandhas

coefficientsalllyingin[0,1].Forexample,thiscanbedoneasfollows:Letfbound(n)betheminimumvalueforfbelow(n,k)andthemaximumvalueforfabove(n,k)foranykintheinterval[0,n];thensetdegreeto1;thenwhilefbound(degree)returnsanupperorlowerboundthatislessthan0orgreaterthan1,multiplydegreeby2;thengotothenextstep.

4. Setstartdegreetodegree.5. (Theremainingstepsarenowdonerepeatedlyuntilthealgorithmfinishesby

returningavalue.)Fliptheinputcointtimes,wheretisdegree−lastdegree.Foreachtimethecoinreturns1thisway,add1toones.

6. Calculateℓanduasfollows:1. DefineFB(a,b)asfollows:Letcbechoose(a,b).Calculatefbelow(a,b)as

lowerandupperboundsLBandUBthatareaccurateenoughthatfloor(LB*c)=floor(UB*c),thenreturnfloor(LB*c).

2. DefineFA(a,b)asfollows:Letcbechoose(a,b).Calculatefabove(a,b)aslowerandupperboundsLBandUBthatareaccurateenoughthatceil(LB*c)=ceil(UB*c),thenreturnceil(LB*c).

3. Letcbechoose(degree,ones).Setℓto(FB(degree,ones))/candsetuto(FA(degree,ones))/c.

7. (Thisstepandthenextfindtheexpectedvaluesofthepreviousℓandugiventhecurrentcoinflips.)Ifdegreeequalsstartdegree,setℓsto0andusto1.(AlgorithmIofFlegalandHerbei2012doesn'ttakethisintoaccount.)

8. Ifdegreeisgreaterthanstartdegree:Letnhbechoose(degree,ones),andletodbedegree/2.Setℓsto∑j=0,...,onesFB(od,j)*choose(degree−od,ones−j)/nh,andsetusto∑j=0,...,onesFA(od,j)*choose(degree−od,ones−j)/nh.

9. Letmbe(ut−ℓt)/(us−ℓs).Setℓttoℓt+(ℓ−ℓs)*m,andsetuttout−(us−u)*m.10. Ifretislessthan(orequalto)ℓt,return1.Ifretislessthanut,gotothenextstep.If

neitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

11. (Findthenextpairofpolynomialsandrestarttheloop.)Increasedegreesothatthenextpairofpolynomialshasdegreeequaltoahighervalueofdegreeandgetsclosertothetargetfunction(forexample,multiplydegreeby2).Then,gotostep5.

ThesecondalgorithmwasgiveninThomasandBlanchet(2012)(11);itassumesthesamesequencesofpolynomialsareavailableasinthepreviousalgorithm.Analgorithmequivalenttothatalgorithmisgivenbelow.

1. Setonesto0,andsetlastdegreeto0.2. Setdegreesothatthefirstpairofpolynomialshasdegreeequaltodegreeandhas

coefficientsalllyingin[0,1].Forexample,thiscanbedoneasfollows:Letfbound(n)betheminimumvalueforfbelow(n,k)andthemaximumvalueforfabove(n,k)foranykintheinterval[0,n];thensetdegreeto1;thenwhilefbound(degree)returnsanupperorlowerboundthatislessthan0orgreaterthan1,multiplydegreeby2;thengotothenextstep.

3. Setstartdegreetodegree.4. (Theremainingstepsarenowdonerepeatedlyuntilthealgorithmfinishesby

Page 17: Bernoulli Factory Algorithms - GitHub Pages

returningavalue.)Fliptheinputcointtimes,wheretisdegree−lastdegree.Foreachtimethecoinreturns1thisway,add1toones.

5. Setctochoose(degree,ones).Optionally,multiplycby2degree(seenote3below).6. Findacountandbcountasfollows:

1. Calculatefbelow(degree,ones)aslowerandupperboundsLBandUBthatareaccurateenoughthatfloor(LB*c)=floor(UB*c).Thenseta[degree,ones]andacounttofloor(LB*c).

2. Calculate1−fabove(degree,ones)aslowerandupperboundsLBandUBthatareaccurateenoughthatfloor(LB*c)=floor(UB*c).Thensetb[degree,ones]andbcounttofloor(LB*c).

3. Subtract(acount+bcount)fromc.7. Ifdegreeisgreaterthanstartdegree,then:

1. Letdiffbedegree−lastdegree,letubemax(0,ones−lastdegree),andletvbemin(ones,diff).(Thefollowingsubstepsremoveoutcomesfromacountandbcountthatwouldhaveterminatedthealgorithmearlier.Theprocedurediffersfromstep(f)ofsection3ofthepaper,whichappearstobeincorrect,andtheprocedurewasderivedfromthesupplementalsourcecodeuploadedbyA.C.Thomasatmyrequest.)

2. Setgtochoose(lastdegree,ones−u).Sethto1.Ifcwasmultipliedasinstep5,multiplyhby2lastdegree(seenote3below).

3. Foreachintegerkintheinterval[u,v]:1. Setdtochoose(diff,k).Letωbeones−k.2. Ifnotalreadycalculated:Calculatefbelow(lastdegree,ω)aslowerand

upperboundsLBandUBthatareaccurateenoughthatfloor(LB*g*h)=floor(UB*g*h).Thenseta[lastdegree,ω]tofloor(LB*g*h).

3. Ifnotalreadycalculated:Calculate1−fabove(lastdegree,ω)aslowerandupperboundsLBandUBthatareaccurateenoughthatfloor(LB*g*h)=floor(UB*g*h).Thensetb[lastdegree,ω]tofloor(LB*g*h).

4. Subtract(a[lastdegree,ω]*d)fromacount.5. Subtract(b[lastdegree,ω]*d)frombcount.6. Multiplygbyω,thendividegby(lastdegree+1−ω).(Setsgto

choose(lastdegree,(ones−k)−1).)8. Choose0,1,or2withprobabilityproportionaltothefollowingweights:[acount,

bcount,c].9. Ifthenumberchosenbythepreviousstepis0,return1.Ifthenumberchosenby

thatstepis1,return0.10. (Findthenextpairofpolynomialsandrestarttheloop.)Setlastdegreetodegree,

thenincreasedegreesothatthenextpairofpolynomialshasdegreeequaltoahighervalueofdegreeandgetsclosertothetargetfunction(forexample,multiplydegreeby2).Then,gotostep4.

Notes:

1. Theefficiencyofthesetwoalgorithmsdependsonmanythings,includinghow"smooth"fisandhoweasyitistocalculatetheappropriatevaluesforfbelowandfabove.Thebestwaytoimplementfbelowandfaboveforagivenfunctionfwillrequireadeepmathematicalanalysisofthatfunction.Formoreinformation,seemySupplementalNotesonBernoulliFactories.

2. Insomecases,asinglepairofpolynomialsequencesmaynotconvergequicklytothedesiredfunctionf,especiallywhenfisnot"smooth"enough.AnintriguingsuggestionfromThomasandBlanchet(2012)(11)istousemultiplepairsofpolynomialsequencesthatconvergetof,whereeachpairisoptimizedforparticularrangesofλ:firstfliptheinputcoinseveraltimestogetaroughestimateofλ,thenchoosethepairthat'soptimized

Page 18: Bernoulli Factory Algorithms - GitHub Pages

fortheestimatedλ,andruneitheralgorithminthissectiononthatpair.3. Thesecondalgorithm,aspresentedinThomasandBlanchet(2012)(11),

wasbasedontheonefromNacuandPeres(2005)(16).Inbothpapers,thealgorithmworksonlyifλisintheinterval(0,1).Ifλcanbe0or1(meaningtheinputcoinisallowedtoreturn1everytimeor0everytime),thenbasedonasuggestioninHoltzetal.(2011)(26),thecinstep5canbemultipliedby2degreeandthehinstep7,substep2,multipliedby2lastdegreetoensurecorrectnessforeveryvalueofλ.

4.3AlgorithmsforGeneralIrrationalConstantsThissectionshowsgeneral-purposealgorithmstogenerateheadswithaprobabilityequaltoanirrationalnumber(anumberthatisn'taratiooftwointegers),whenthatnumberisknownbyitsdigitorseriesexpansion,continuedfraction,orcontinuedlogarithm.

Butontheotherhand,probabilitiesthatarerationalconstantsaretrivialtosimulate.Iffaircoinsareavailable,theZeroOrOnemethod,whichisdescribedinmyarticleonrandomsamplingmethods,shouldbeused.Ifcoinswithunknownprobabilityofheadsareavailable,thenarandomnessextractionmethodshouldbeusedtoturnthemintofaircoins.

4.3.1DigitExpansions

Probabilitiescanbeexpressedasadigitexpansion(oftheform0.dddddd...).Thefollowingalgorithmreturns1withprobabilitypand0otherwise,wherepisaprobabilityinthehalf-openinterval0,1).Notethatthenumber0isalsoaninfinitedigitexpansionofzeros,andthenumber1isalsoaninfinitedigitexpansionofbase-minus-ones.Irrationalnumbersalwayshaveinfinitedigitexpansions,whichmustbecalculated"on-the-fly".

Inthealgorithm(seealso(Brassardetal.,2019)[(27),(Devroye1986,p.769)(28)),BASEisthedigitbase,suchas2forbinaryor10fordecimal.

1. Setuto0andkto1.2. Setuto(u*BASE)+v,wherevisauniformrandomintegerintheinterval[0,BASE)(if

BASEis2,thenvissimplyanunbiasedrandombit).Calculatepa,whichisanapproximationtopsuchthatabs(p−pa)≤BASE−k.Setpktopa'sdigitexpansionuptothekdigitsafterthepoint.Example:Ifpisπ/4,BASEis10,andkis5,thenpk=78539.

3. Ifpk+1<=u,return0.Ifpk-2>=u,return1.Ifneitheristhecase,add1tokandgotostep2.

4.3.2ContinuedFractions

Thefollowingalgorithmsimulatesaprobabilityexpressedasasimplecontinuedfractionofthefollowingform:0+1/(a[1]+1/(a[2]+1/(a[3]+...))).Thea[i]arethepartialdenominators,noneofwhichmayhaveanabsolutevaluelessthan1.Inspiredby(Flajoletetal.,2010,"Finitegraphs(Markovchains)andrationalfunctions")(1),Idevelopedthefollowingalgorithm.

Algorithm1.Thisalgorithmworksonlyifeacha[i]'sabsolutevalueis1orgreateranda[1]isgreaterthan0,butotherwise,eacha[i]maybenegativeand/oranon-integer.Thealgorithmbeginswithposequalto1.Thenthefollowingstepsaretaken.

1. Setktoa[pos].2. Ifthepartialdenominatoratposisthelast,returnanumberthatis1with

Page 19: Bernoulli Factory Algorithms - GitHub Pages

probability1/kand0otherwise.3. Ifa[pos]islessthan0,setkptok−1andsto0.Otherwise,setkptokandsto1.

(Thisstepaccountsfornegativepartialdenominators.)4. Dothefollowingprocessrepeatedlyuntilthisrunofthealgorithmreturnsavalue:

1. Withprobabilitykp/(1+kp),returnanumberthatis1withprobability1/kpand0otherwise.

2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithpos=pos+1.Iftheseparaterunreturnss,return0.

Algorithm2.

Ageneralizedcontinuedfractionhastheform0+b[1]/(a[1]+b[2]/(a[2]+b[3]/(a[3]+...))).Thea[i]arethesameasbefore,buttheb[i]arethepartialnumerators.Thefollowingaretwoalgorithmstosimulateaprobabilityintheformofageneralizedcontinuedfraction.

Thefollowingalgorithmworksonlyifeachratiob[i]/a[i]hasanabsolutevalueof1orless,butotherwise,eachb[i]andeacha[i]maybenegativeand/oranon-integer.Thisalgorithmemploysanequivalencetransformfromgeneralizedtosimplecontinuedfractions.Thealgorithmbeginswithposandrbothequalto1.Thenthefollowingstepsaretaken.

1. Setrto1/(r*b[pos]),thensetktoa[pos]*r.(kisthepartialdenominatorfortheequivalentsimplecontinuedfraction.)

2. Ifthepartialnumerator/denominatorpairatposisthelast,returnanumberthatis1withprobability1/abs(k)and0otherwise.

3. Setkptoabs(k)andsto1.4. Setr2to1/(r*b[pos+1]).Ifa[pos+1]*r2islessthan0,setkptokp−1andsto

0.(Thisstepaccountsfornegativepartialnumeratorsanddenominators.)5. Dothefollowingprocessrepeatedlyuntilthisrunofthealgorithmreturnsavalue:

1. Withprobabilitykp/(1+kp),returnanumberthatis1withprobability1/kpand0otherwise.

2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithpos=pos+1andr=r.Iftheseparaterunreturnss,return0.

Algorithm3.Thisalgorithmworksonlyifeachratiob[i]/a[i]is1orlessandifeachb[i]andeacha[i]isgreaterthan0,butotherwise,eachb[i]andeacha[i]maybeanon-integer.Thealgorithmbeginswithposequalto1.Thenthefollowingstepsaretaken.

1. Ifthepartialnumerator/denominatorpairatposisthelast,returnanumberthatis1withprobabilityb[pos]/a[pos]and0otherwise.

2. Dothefollowingprocessrepeatedlyuntilthisrunofthealgorithmreturnsavalue:1. Withprobabilitya[pos]/(1+a[pos]),returnanumberthatis1withprobability

b[pos]/a[pos]and0otherwise.2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithpos=pos+1.If

theseparaterunreturns1,return0.

SeetheappendixforacorrectnessproofofAlgorithm3.

Notes:

Ifanyofthesealgorithmsencountersaprobabilityoutsidetheinterval[0,1],theentirealgorithmwillfailforthatcontinuedfraction.

Thesealgorithmswillworkforcontinuedfractionsoftheform"1−..."(ratherthan"0+...")if—

Page 20: Bernoulli Factory Algorithms - GitHub Pages

beforerunningthealgorithm,thefirstpartialnumeratoranddenominatorhavetheirsignremoved,andafterrunningthealgorithm,1minustheresult(ratherthanjusttheresult)istaken.

Thesealgorithmsaredesignedtoallowthepartialnumeratorsanddenominatorstobecalculated"onthefly".

ThefollowingisanalternativewaytowriteAlgorithm1,whichbettershowstheinspirationbecauseitshowshowthe"evenparityconstruction"(orthetwo-coinalgorithm)aswellasthe"1−x"constructioncanbeusedtodeveloprationalnumbersimulatorsthatareasbigastheircontinuedfractionexpansions,assuggestedinthecitedpartoftheFlajoletpaper.However,itonlyworksifthesizeofthecontinuedfractionexpansion(here,size)isknowninadvance.

1. Setitosize.2. Createaninputcointhatdoesthefollowing:"Returnanumberthatis

1withprobability1/a[size]or0otherwise".3. Whileiis1orgreater:

1. Setktoa[i].2. Createaninputcointhattakesthepreviousinputcoinandkand

doesthefollowing:"(a)Withprobabilityk/(1+k),returnanumberthatis1withprobability1/kand0otherwise;(b)Flipthepreviousinputcoin.Iftheresultis1,return0.Otherwise,gotostep(a)".(Theprobabilityk/(1+k)isrelatedtoλ/(1+λ)=1−1/(1+λ),whichinvolvestheeven-parityconstruction—orthetwo-coinalgorithm—for1/(1+λ)aswellascomplementationfor"1−x".)

3. Subtract1fromi.4. Flipthelastinputcoincreatedbythisalgorithm,andreturnthe

result.

4.3.3ContinuedLogarithms

Thecontinuedlogarithm(Gosper1978)(29),(Borweinetal.,2016)(30)ofanumberintheopeninterval(0,1)hasthefollowingcontinuedfractionform:0+(1/2c[1])/(1+(1/2c[2])/(1+...)),wherec[i]arethecoefficientsofthecontinuedlogarithmandall0orgreater.Ihavecomeupwiththefollowingalgorithmthatsimulatesaprobabilityexpressedasacontinuedlogarithmexpansion.

Thealgorithmbeginswithposequalto1.Thenthefollowingstepsaretaken.

1. Ifthecoefficientatposisthelast,returnanumberthatis1withprobability1/(2c[pos])and0otherwise.

2. Dothefollowingprocessrepeatedlyuntilthisrunofthealgorithmreturnsavalue:1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswith

probability1/2),returnanumberthatis1withprobability1/(2c[pos])and0otherwise.

2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithpos=pos+1.Iftheseparaterunreturns1,return0.

Foracorrectnessproof,seetheappendix.

4.3.4CertainConvergingSeries

Page 21: Bernoulli Factory Algorithms - GitHub Pages

Ageneral-purposealgorithmwasgivenbyMendo(2020)(31)thatcansimulateanyprobabilityintheopeninterval(0,1),aslongasitcanberewrittenasaconvergingseries—

thathastheforma[0]+a[1]+...,wherea[n]areallrationalnumbersgreaterthan0,andforwhichasequenceerr[0],err[1],...,isavailablethatisnonincreasingandconvergesto0,whereerr[n]isanupperboundontheerrorfromtruncatingtheseriesaaftersummingthefirstn+1terms.

Thealgorithmfollows.

1. Setϵto1,thensetn,lamunq,lam,s,andkto0each.2. Add1tok,thenadds/(2k)tolam.3. Iflamunq+ϵ≤lam+1/(2k),gotostep8.4. Iflamunq>lam+1/(2k),gotostep8.5. Iflamunq>lam+1/(2k+1)andlamunq+ϵ<3/(2k+1),gotostep8.6. Adda[n]tolamunqandsetϵtoerr[n].7. Add1ton,thengotostep3.8. Letboundbelam+1/(2k).Iflamunq+ϵ≤bound,setsto0.Otherwise,iflamunq>

bound,setsto2.Otherwise,setsto1.9. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),gotostep2.Otherwise,returnanumberthatis0ifsis0,1ifsis2,oranunbiasedrandombit(either0or1withequalprobability)otherwise.

Ifa,givenabove,isinsteadasequencethatconvergestothebase-2logarithmofaprobabilityintheopeninterval(0,1),thefollowingalgorithmIdevelopedsimulatesthatprobability.Forsimplicity'ssake,eventhoughlogarithmsforsuchprobabilitiesarenegative,allthea[i]mustbe0orgreater(andthusarethenegatedvaluesofthealreadynegativelogarithmapproximations)andmustformanondecreasingsequence,andalltheerr[i]mustbe0orgreater.

1. Setintinftofloor(max(0,abs(a[0]))).(Thisistheabsoluteintegerpartofthefirsttermintheseries,or0,whicheverisgreater.)

2. Ifintinfisgreaterthan0,generateunbiasedrandombitsuntilazerobitorintinfbitsweregeneratedthisway.Ifazerowasgeneratedthisway,return0.

3. GenerateanexponentialrandomvariateEwithrateln(2).Thiscanbedone,forexample,byusingthealgorithmgivenin"MoreAlgorithmsforArbitrary-PrecisionSampling".(Wetakeadvantageoftheexponentialdistribution'smemorylessproperty:giventhatanexponentialrandomvariateEisgreaterthanintinf,Eminusintinfhasthesamedistribution.)

4. Setnto0.5. Dothefollowingprocessrepeatedlyuntilthealgorithmreturnsavalue:

1. Setinftomax(0,a[n]),thensetsuptomin(0,inf+err[n]).2. IfEislessthaninf+intinf,return0.IfEislessthansup+intinf,gotothenext

step.Ifneitheristhecase,return1.3. Setnto1.

Thecasewhenaconvergestoanaturallogarithmratherthanabase-2logarithmistrivialbycomparison.Againforthisalgorithm,allthea[i]mustbe0orgreaterandformanondecreasingsequence,andalltheerr[i]mustbe0orgreater.

1. GenerateanexponentialrandomvariateE(withrate1).2. Setnto0.3. Dothefollowingprocessrepeatedlyuntilthealgorithmreturnsavalue:

Page 22: Bernoulli Factory Algorithms - GitHub Pages

1. Setinftomax(0,a[n]),thensetsuptomin(0,inf+err[n]).2. IfEislessthaninf+intinf,return0.IfEislessthansup+intinf,gotothenext

step.Ifneitheristhecase,return1.3. Setnto1.

Examples:

Letf(λ)=cosh(1)−1.Thefirstalgorithminthissectioncansimulatethisconstantifstep6ismodifiedtoread:"Letmbe((n+1)*2),andletαbe1/(m!)(atermoftheTaylorseries).Addαtolamunqandsetϵto2/((m+1)!)(theerrorterm).".(32)Logarithmscanformthebasisofefficientalgorithmstosimulatetheprobabilityz=choose(n,k)/2nwhenncanbeverylarge(e.g.,aslargeas230),withoutrelyingonfloating-pointarithmetic.Inthisexample,thetrivialalgorithmforchoose(n,k),thebinomialcoefficient,willgenerallyrequireagrowingamountofstoragethatdependsonnandk.Ontheotherhand,anyconstantcanbesimulatedusinguptotwounbiasedrandombitsonaverage,andevenslightlylessthanthatfortheconstantsathandhere(Kozen2014)(33).Insteadofcalculatingthebinomialcoefficientdirectly,aseriescanbecalculatedthatconvergestothatcoefficient'slogarithm,suchasln(choose(n,k)),whichiseconomicalinspaceevenforlargenandk.Thenthealgorithmabovecanbeusedwiththatseriestosimulatetheprobabilityz.Asimilarapproachhasbeenimplemented(seeinterval.pyandbetadist.py).Seealsoanappendixin(Bringmannetal.2014)(34).

4.4OtherGeneralAlgorithms

4.4.1ConvexCombinations

Assumewehaveoneormoreinputcoinshi(λ)thatreturnheadswithaprobabilitythatdependsonλ.(Thenumberofcoinsmaybeinfinite.)Thefollowingalgorithmchoosesoneofthesecoinsatrandomthenflipsthatcoin.Specifically,thealgorithmgenerates1withprobabilityequaltothefollowingweightedsum:g(0)*h0(λ)+g(1)*h1(λ)+...,whereg(i)istheprobabilitythatcoiniwillbechosen,hiisthefunctionsimulatedbycoini,andalltheg(i)sumto1.See(Wästlund1999,Theorem2.7)(8).(Alternatively,thealgorithmcanbeseenasreturningheadswithprobabilityE[hX(λ)],thatis,theexpectedoraveragevalueofhXwhereXisthenumberthatidentifiestherandomlychosencoin.)

1. GeneratearandomintegerXinsomeway.Forexample,itcouldbeauniformrandomintegerin[1,6],oritcouldbeaPoissonrandomvariate.(Specifically,thenumberXisgeneratedwithprobabilityg(X).)

2. FlipthecoinrepresentedbyXandreturntheresult.

Notes:

1. Buildingconvexcombinations.Assumewehaveafunctionoftheformf(λ)=∑n=0,1,...wn(λ),wherewnarecontinuousfunctionswhosemaximumvaluesinthedomain[0,1]sumto1orless.Letg(n)betheprobabilitythatarandomlychosennumberXisn.ThenbygeneratingXandflippingacoinwithprobabilityofheadsofwX(λ)/g(X),wecansimulatethe

Page 23: Bernoulli Factory Algorithms - GitHub Pages

probabilityf(λ)astheconvexcombination—

f(λ)=∑n=0,1,...g(n)*(wn(λ)/g(n)),

butthisworksonlyifthefollowingtwoconditionsaremetforeachintegern≥0:

g(n)≥wn(λ)≥0foreveryλintheinterval[0,1](whichroughlymeansthatwnisboundedfromaboveor"dominated"byg(n)).Thefunctionwn(λ)/g(n)admitsaBernoullifactory(whichitwon'tifittouches0or1insidetheinterval(0,1),butisn'tconstant,forexample).

SeealsoMendo(2019)(23).

2. Constantswithnon-negativeseriesexpansions.Aspecialcaseofnote1.Letgbeasinnote1.Assumewehaveaconstantwiththefollowingseriesexpansion:c=a0+a1+a2+...,where—

anareeach0orgreaterandsumto1orless,andg(n)≥an≥0foreachintegern≥0.

ThenbygeneratingXandflippingacoinwithprobabilityofheadsofaX/g(X),wecansimulatetheprobabilitycastheconvexcombination—

c=∑n=0,1,...g(n)*(an/g(n)).

Examples:

1. GenerateaPoisson(μ)randomvariateX,thenfliptheinputcoin.Withprobability1/(1+X),returntheresultofthecoinflip;otherwise,return0.Thiscorrespondstog(i)beingthePoisson(μ)probabilitiesandthecoinforhireturning1withprobability1/(1+i),and0otherwise.Theprobabilitythatthismethodreturns1isE[1/(1+X)],or(exp(μ)−1)/(exp(μ)*μ).

2. GenerateaPoisson(μ)randomvariateXandreturn1ifXis0,or0otherwise.ThisisaBernoullifactoryforexp(−μ)mentionedearlier,andcorrespondstog(i)beingthePoisson(μ)probabilitiesandthecoinforhireturning1ifiis0,and0otherwise.

3. GenerateaPoisson(μ)randomvariateX,runthealgorithmforexp(−z)withz=X,andreturntheresult.Theprobabilityofreturning1thiswayisE[exp(−X)],orexp(μ*exp(−1)−μ).ThefollowingPythoncodeusesthecomputeralgebralibrarySymPytofindthisprobability:fromsympy.statsimport*;E(exp(-Poisson('P',x))).simplify().

4. BernoulliRace(Dughmietal.2017)(35):Saywehavencoins,thenchooseoneofthemuniformlyatrandomandflipthatcoin.Iftheflipreturns1,returnX;otherwise,repeatthisalgorithm.Thisalgorithmchoosesarandomcoinbasedonitsprobabilityofheads.Eachiterationcorrespondstog(i)being1/nandhi()beingtheprobabilityforthecorrespondingcoini.

5. (Wästlund1999)(8):GenerateaPoissonrandomvariateXwithmean1,thenfliptheinputcoinXtimes.Return0ifanyoftheflipsreturns1,or1otherwise.ThisisaBernoullifactoryforexp(−λ),andcorrespondstog(i)beingthePoissonprobabilities,namely1/(i!*exp(1)),andhi()being(1−λ)i.

Page 24: Bernoulli Factory Algorithms - GitHub Pages

6. MultivariateBernoullifactory(Huber2016)(36)oftheformR=C0*λ0+C1*λ1+...+Cm−1*λm−1,whereCiareknownconstantsgreaterthan0,andR≤1−ϵforanyϵ>0:Chooseanintegerin0,m)uniformlyatrandom,calliti,thenrunalinearBernoullifactoryfor(m*Ci)*λi.ThisdiffersfromHuber'ssuggestionof"thinning"aPoissonprocessdrivenbymultipleinputcoins.

7. Probabilitygeneratingfunction(PGF)(Dughmietal.2017)[(35).GeneratesheadswithprobabilityE[λX],thatis,theexpectedoraveragevalueofλX.E[λX]isthePGFforthedistributionofX.Thealgorithmfollows:(1)GeneratearandomintegerXinsomeway;(2)Fliptheinputcoinuntiltheflipreturns0orthecoinisflippedXtimes,whichevercomesfirst.Return1ifallthecoinflips,includingthelast,returned1(orifXis0);orreturn0otherwise.

8. AssumeXisthenumberofunbiasedrandombitsthatshow0beforethefirst1isgenerated.Theng(n)=1/(2n+1).

4.4.2Integrals

Roughlyspeaking,theintegraloff(x)onaninterval[a,b]istheareaunderthatfunction'sgraphwhenthefunctionisrestrictedtothatinterval.

Algorithm1.(Flajoletetal.,2010)(1)showedhowtoturnanalgorithmthatsimulatesf(λ)intoanalgorithmthatsimulatesthefollowingprobability:

(1/λ)*∫[0,λ]f(u)du,orequivalently,∫[0,1]f(u*λ)du(anintegral),

usingthefollowingalgorithm:

1. Generateauniform(0,1)randomvariateu.2. Createaninputcointhatdoesthefollowing:"Fliptheoriginalinputcoin,then

samplefromthenumberu.Return1ifboththecallandtheflipreturn1,andreturn0otherwise."

3. RuntheoriginalBernoullifactoryalgorithm,usingtheinputcoindescribedinstep2ratherthantheoriginalinputcoin.Returntheresultofthatrun.

Algorithm2.AspecialcaseofAlgorithm1istheintegral∫[0,1]f(u)du,whentheoriginalinputcoinalwaysreturns1:

1. Generateauniform(0,1)randomvariateu.2. Createaninputcointhatdoesthefollowing:"Samplefromthenumberuand

returntheresult."3. RuntheoriginalBernoullifactoryalgorithm,usingtheinputcoindescribedinstep2

ratherthantheoriginalinputcoin.Returntheresultofthatrun.

Algorithm3.Ihavefoundthatit'spossibletosimulatethefollowingintegral,namely—

∫[a,b]f(u)du,

where[a,b]is[0,1]oraclosedintervaltherein,usingthefollowingalgorithm:

1. Generateauniform(0,1)randomvariateu.Thenifuislessthanaorisgreaterthanb,repeatthisstep.(IfuisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm.)

Page 25: Bernoulli Factory Algorithms - GitHub Pages

2. Createaninputcointhatdoesthefollowing:"Samplefromthenumberuandreturntheresult."

3. RuntheoriginalBernoullifactoryalgorithm,usingtheinputcoindescribedinstep2.Iftherunreturns0,return0.Otherwise,generateauniform(0,1)randomvariatevandreturnanumberthatis0ifvislessthanaorisgreaterthanb,or1otherwise.

Note:Ifais0andbis1,theprobabilitysimulatedbythisalgorithmwillbemonotonicallyincreasing(willkeepgoingup),haveaslopenogreaterthan1,andequal0atthepoint0.

4.4.3GeneralizedBernoulliRace

TheBernoullifactoryapproach,whichsimulatesacoinwithunknownheadsprobability,leadstoanalgorithmtorollann-facediewherethechanceofeachfaceisunknown.Hereisonesuchdie-rollingalgorithm(Schmonetal.2019)(37).ItgeneralizestheBernoulliRacefromthe"ConvexCombinations"section,andreturnsiwithprobability—

φi=g(i)*hi(λ)/∑k=0,...,rg(k)*hk(λ),

where:

risanintegergreaterthan0.Therearer+1valuesthisalgorithmcanchoosefrom.g(i)takesanintegeriandreturnsanumber0orgreater.Thisservesasaweightforthe"coin"labeledi;thehighertheweight,themorelikelythe"coin"willbe"flipped".hi(λ)takesinanumberiandtheprobabilitiesofheadsofoneormoreinputcoins,andreturnsanumberintheinterval[0,1].Thisrepresentsthe"coin"foroneofther+1choices.

Thealgorithmfollows.

1. Chooseanintegerin[0,r]withprobabilityproportionaltothefollowingweights:[g(0),g(1),...,g(r)].Callthechosenintegeri.

2. RunaBernoullifactoryalgorithmforhi(λ).Iftherunreturns0,gotostep1.3. Returni.

Notes:

1. TheBernoulliRace(see"ConvexCombinations")isaspecialcaseofthisalgorithmwithg(k)=1foreveryk.

2. IfwedefineStobeasubsetofintegersin[0,r]andreplacestep3with"IfiisinthesetS,return1.Otherwise,return0.",thealgorithmreturns1withprobability∑kinSφk,and0otherwise.Inthatcase,themodifiedalgorithmhastheso-called"die-coinalgorithm"ofAgrawaletal.(2021,AppendixD)(38)asaspecialcasewith—g(k)=ck*dr−k,hk(λ,μ)=λk*μr−k(forthefollowingalgorithm:fliptheλcoinktimesandtheμcoinr−ktimes;return1ifallflipsreturn1,or0otherwise),andSistheclosedinterval[1,r],wherec≥0,d≥0,andλandμaretheprobabilitiesofheadsoftwoinputcoins.Inthatpaper,c,d,λ,andμcorrespondtocy,cx,py,andpx,respectively.

3. AlthoughnotnotedintheSchmonpaper,therinthealgorithmcanbeinfinity(seealsoWästlund1999,Theorem2.7(8)).Inthatcase,Step1is

Page 26: Bernoulli Factory Algorithms - GitHub Pages

changedtosay"Chooseaninteger0orgreateratrandomwithprobabilityg(k)forintegerk.Callthechosenintegeri."Asanexample,step1cansamplefromaPoissondistribution,whichcantakeonanyinteger0orgreater.

4.5AlgorithmsforSpecificFunctionsofλThissectiondescribesalgorithmsforspecificfunctions,especiallywhentheyhaveamoreconvenientsimulationthanthegeneral-purposealgorithmsgivenearlier.Theycanbegroupedasfollows:

Functionsinvolvingtheexponentialfunctionexp(x).Rationalfunctionsofseveralvariables.Addition,subtraction,anddivision.Powersandroots.LinearBernoullifactories.Transcendentalfunctions.Otherfactoryfunctions.

4.5.1exp(−λ)

Thisalgorithmisadaptedfromthegeneralmartingalealgorithm(in"CertainPowerSeries",above),andmakesuseofthefactthatexp(−λ)canberewrittenas1−λ+λ2/2−λ3/6+λ4/24−...,whichisanalternatingserieswhosecoefficientsare1,1,1/(2!),1/(3!),1/(4!),....Thisalgorithmconvergesquicklyeverywhereintheopeninterval(0,1).(Inotherwords,thealgorithmisuniformlyfast,meaningtheaveragerunningtimeisfiniteforeverychoiceofλandotherparameters(Devroye1986,esp.p.717)(28).(39))

1. Setuto1,setwto1,setℓto0,andsetnto1.2. Generateauniform(0,1)randomvariateret.3. Dothefollowingprocessrepeatedlyuntilthisalgorithmreturnsavalue:

1. Ifwisnot0,fliptheinputcoin,multiplywbytheresultoftheflip,anddividewbyn.(Thisischangedfromthegeneralmartingalealgorithmtotakeaccountofthefactorialmoreefficientlyinthesecondandlatercoefficients.)

2. Ifniseven,setutoℓ+w.Otherwise,setℓtou−w.3. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenext

substep.Ifneitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

4. Add1ton.

4.5.2(exp(λ)−1)/exp(−λ)

Thisalgorithmusesthegeneralmartingalealgorithm;thisfunctioncanberewrittenasλ*(1−λ/2+λ2/6−λ3/24+...),whichincludesanalternatingserieswhosecoefficientsare1,1/(2!),1/(3!),1/(4!),....

1. Fliptheinputcoin.Ifitreturns0,return0.2. Setuto1,setwto1,setℓto0,andsetnto2.3. Generateauniform(0,1)randomvariateret.4. Dothefollowingprocessrepeatedlyuntilthisalgorithmreturnsavalue:

1. Ifwisnot0,fliptheinputcoin,multiplywbytheresultoftheflip,anddividewbyn.(Thisischangedfromthegeneralmartingalealgorithmtotakeaccountofthefactorialmoreefficientlyinthesecondandlatercoefficients.)

Page 27: Bernoulli Factory Algorithms - GitHub Pages

2. Ifniseven,setutoℓ+w.Otherwise,setℓtou−w.3. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenext

substep.Ifneitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

4. Add1ton.

4.5.3exp(−(λk*c))

Inthealgorithmsinthissection,kisaninteger0orgreater,andcisarealnumber.

Algorithm1.Workswhencis0orgreater.(Seealsoalgorithmforexp(−((1−λ)1*c))in"OtherFactoryFunctions".)

1. Specialcase:Ifcis0,return1.Ifkis0,runthealgorithmforexp(−z)(givenlaterinthispage)withz=c,andreturntheresult.

2. GenerateaPoisson(c)randominteger,callitN.(SeetheappendixonthevonNeumannschemaforinformationongeneratingthisintegerexactly.)

3. Setito0,thenwhilei<N:1. Fliptheinputcoinuntiltheflipreturns0orthecoinisflippedktimes,

whichevercomesfirst.Return0ifallofthecoinflips(includingthelast)return1.

2. Add1toi.4. Return1.

Algorithm2.Appliesthegeneralmartingalealgorithm,butworksonlywhencisarationalnumberintheinterval[0,1].Thetargetfunctionisrepresentedasaseries1−λk*c+λ2*k*c/2!−λ3*k*x/3!,...,andthecoefficientsare1,c,c/(2!),c/(3!),....

1. Specialcases:Ifcis0,return1.Ifkis0,runthealgorithmforexp(−x/y)(givenlaterinthispage)withx/y=c,andreturntheresult.

2. Setuto1,setwto1,setℓto0,andsetnto1.3. Generateauniform(0,1)randomvariateret.4. Ifwisnot0,fliptheinputcoinktimesoruntiltheflipreturns0.Ifanyoftheflips

returns0,setwto0,orifalltheflipsreturn1,dividewbyn.Then,multiplywbyanumberthatis1withprobabilitycand0otherwise.

5. Ifniseven,setutoℓ+w.Otherwise,setℓtou−w.6. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenextstep.If

neitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

7. Add1tonandgotostep4.

Algorithm3.BuildsonAlgorithm3andworkswhencisarationalnumber0orgreater.

1. Letmbefloor(c).Callthesecondalgorithmmtimeswithk=kandc=1.Ifanyofthesecallsreturns0,return0.

2. Ifcisaninteger,return1.3. Callthesecondalgorithmonce,withk=kandc=c−floor(c).Returntheresultof

thiscall.

4.5.4exp(−(λ+m)k)

Inthefollowingalgorithm,mandkarebothintegers0orgreaterunlessnotedotherwise.

Page 28: Bernoulli Factory Algorithms - GitHub Pages

1. Ifkis0,runthealgorithmforexp(−x/y)(givenlateronthispage)withx/y=1/1,andreturntheresult.

2. Ifkis1andmis0,runthealgorithmforexp(−λ)andreturntheresult.3. Ifkis1andmisgreaterthan0(andinthiscase,mcanbeanyrationalnumber):

Runthealgorithmforexp(−x/y)withx/y=m.Ifthealgorithmreturns0,return0.Otherwise,returntheresultofthealgorithmforexp(−λ).

4. Runthealgorithmforexp(−x/y)withx/y=mk/1.Ifthealgorithmreturns0,return0.

5. Runthealgorithmforexp(−(λk*c)),withk=kandx=1.Ifthealgorithmreturns0,return0.

6. Ifmis0,return1.7. Setito1,thenwhilei<k:

1. Setztochoose(k,i)*mk−i.2. Runthealgorithmforexp(−(λk*c))ztimes,withk=iandx=1.Ifanyof

thesecallsreturns0,return0.3. Add1toi.

8. Return1.

4.5.5exp(λ)*(1−λ)

(Flajoletetal.,2010)(1):

1. Setkandweachto0.2. Fliptheinputcoin.Ifitreturns0,return1.3. Generateauniform(0,1)randomvariateU.4. Ifk>0andwislessthanU,return0.5. SetwtoU,add1tok,andgotostep2.

4.5.61/(2k+λ)orexp(−(k+λ)*ln(2))

Thisnewalgorithmusesthebase-2logarithmk+λ,wherekisaninteger0orgreater,andisusefulwhenthislogarithmisverylarge.

1. Ifk>0,generateunbiasedrandombitsuntilazerobitorkbitsweregeneratedthisway,whichevercomesfirst.Ifazerobitwasgeneratedthisway,return0.

2. Createaninputcoinμthatdoesthefollowing:"Fliptheinputcoin,thenrunthealgorithmforln(1+y/z)(givenlater)withy/z=1/1.Ifboththecallandtheflipreturn1,return1.Otherwise,return0."

3. Runthealgorithmforexp(−μ)usingtheμinputcoin,andreturntheresult.

4.5.71/(2m*(k+λ))or1/((2m)*(k+λ))orexp(−(k+λ)*ln(2m))

Anextensionofthepreviousalgorithm.Here,misanintegergreaterthan0.

1. Ifk>0,generateunbiasedrandombitsuntilazerobitork*mbitsweregeneratedthisway,whichevercomesfirst.Ifazerobitwasgeneratedthisway,return0.

2. Createaninputcoinμthatdoesthefollowing:"Fliptheinputcoin,thenrunthealgorithmforln(1+y/z)(givenlater)withy/z=1/1.Ifboththecallandtheflipreturn1,return1.Otherwise,return0."

3. Runthealgorithmforexp(−μ)mtimes,usingtheμinputcoin.Ifanyofthecallsreturns0,return0.Otherwise,return1.

4.5.8c*λ*β/(β*(c*λ+d*μ)−(β−1)*(c+d))

Page 29: Bernoulli Factory Algorithms - GitHub Pages

Thisisthegeneraltwo-coinalgorithmof(Gonçalvesetal.,2017)(40)and(Vatsetal.2020)(41).Ittakestwoinputcoinsthateachoutputheads(1)withprobabilityλorμ,respectively.Italsotakesparameterscandd,each0orgreater,andβintheinterval[0,1],whichisaso-called"portkey"orearlyrejectionparameter(whenβ=1,theformulasimplifiestoc*λ/(c*λ+d*μ)).InVatsetal.(2020)(41),β,c,d,λandμcorrespondtoβ,cy,cx,py,andpx,respectively,inthe"portkey"algorithm,ortoβ,cx,cy,px,andpy,respectively,inthe"flippedportkey"algorithm.

1. Withprobabilityβ,gotostep2.Otherwise,return0.(Forexample,callZeroOrOnewithβ'snumeratoranddenominator,andreturn0ifthatcallreturns0,orgotostep2otherwise.ZeroOrOneisdescribedinmyarticleonrandomsamplingmethods.)

2. Withprobabilityc/(c+d),fliptheλinputcoin.Otherwise,fliptheμinputcoin.Iftheλinputcoinreturns1,return1.Iftheμinputcoinreturns1,return0.Ifthecorrespondingcoinreturns0,gotostep1.

4.5.9c*λ/(c*λ+d)or(c/d)*λ/(1+(c/d)*λ))

Thisalgorithm,alsoknownasthelogisticBernoullifactory(Huber2016)(36),(Morinaetal.,2019)(17),isaspecialcaseofthetwo-coinalgorithmabove,butthistimeusesonlyoneinputcoin.

1. Withprobabilityd/(c+d),return0.2. Fliptheinputcoin.Iftheflipreturns1,return1.Otherwise,gotostep1.

(NotethatHuber[2016]specifiesthisBernoullifactoryintermsofaPoissonpointprocess,whichseemstorequiremuchmorerandomnessonaverage.)

4.5.10(d+λ)/c

Inthisalgorithm,dandcmustbeintegers,and0≤d<c.

1. Generateanintegerin0,c)uniformlyatrandom,calliti.2. Ifi<d,return1.Ifi=d,fliptheinputcoinandreturntheresult.Ifneitheristhe

case,return0.

4.5.11d/(c+λ)

Inthisalgorithm,canddmustberationalnumbers,c≥1,and0≤d≤c.Seealsothealgorithmsforcontinuedfractions.(Forexample,whend=1,thisalgorithmcansimulateaprobabilityoftheform1/z,wherezis1orgreaterandmadeupofanintegerpart(c)andafractionalpart(λ)thatcanbesimulatedbyaBernoullifactory.)

1. Withprobabilityc/(1+c),returnanumberthatis1withprobabilityd/cand0otherwise.

2. Fliptheinputcoin.Iftheflipreturns1,return0.Otherwise,gotostep1.

Note:Aquickproofthisalgorithmworks:Letxbethedesiredprobability.Then—x=(c/(1+c))*(d/c)+(1−c/(1+c))*(λ*0+(1−λ)*x),andsolvingforxleadstox=d/(c+λ).

4.5.12(d+μ)/(c+λ)

Combinesthealgorithmsintheprevioustwosections.

Page 30: Bernoulli Factory Algorithms - GitHub Pages

Inthisalgorithm,canddmustbeintegers,and0≤d<c.

1. Withprobabilityc/(1+c),dothefollowing:1. Generateanintegerin[0,c)uniformlyatrandom,calliti.2. Ifi<d,return1.Ifi=d,fliptheμinputcoinandreturntheresult.Ifneitheris

thecase,return0.2. Fliptheλinputcoin.Iftheflipreturns1,return0.Otherwise,gotostep1.

4.5.13(d+μ)/((d+μ)+(c+λ))

Inthisalgorithm,canddareintegers0orgreater,andλandμaretheprobabilitiesofheadsoftwodifferentinputcoins.Intheintendeduseofthisalgorithm,λandμarebackedbythefractionalpartsoftwouniformPSRNs,andcanddaretheirintegerparts,respectively.

1. LetD=dandC=c.Runthealgorithmfor(d+μ)/(c+λ)withλandμbothbeingtheμinputcoin,withd=D+C,andwithc=1+D+C.Iftherunreturns1:1. Ifcis0,return1.2. Runthealgorithmfor(d+μ)/(c+λ)withλandμbothbeingtheμinput

coin,withd=D,andwithc=D+C.Iftherunreturns1,return1.Otherwise,return0.

2. Fliptheλinputcoin.Iftheflipreturns1,return0.Otherwise,gotostep1.

4.5.14dk/(c+λ)k,or(d/(c+λ))k

Inthisalgorithm,canddmustberationalnumbers,c≥1,and0≤d≤c,andkmustbeaninteger0orgreater.

1. Setito0.2. Ifkis0,return1.3. Withprobabilityc/(1+c),dothefollowing:

1. Withprobabilityd/c,add1toiandtheneitherreturn1ifiisnowkorgreater,orabortthesesubstepsandgotostep2otherwise.

2. Return0.4. Fliptheinputcoin.Iftheflipreturns1,return0.Otherwise,gotostep2.

4.5.151/(1+λ)

Thisalgorithmisaspecialcaseofthetwo-coinalgorithmof(Gonçalvesetal.,2017)[(40)andisuniformlyfast.(42)

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),return1.

2. Fliptheinputcoin.Ifitreturns1,return0.Otherwise,gotostep1.

Note:Inthisspecialcaseofthetwo-coinalgorithm,β=1,c=1,d=1,oldλequals1,andμequalsnewλ.

4.5.161/(2−λ)

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),return1.

2. Fliptheinputcoin.Ifitreturns0,return0.Otherwise,gotostep1.

Note:Canbederivedfromthepreviousalgorithmbyobservingthat1/(2−λ)=

Page 31: Bernoulli Factory Algorithms - GitHub Pages

1/(1+(1−λ)).

4.5.171/(1+(x/y)*λ)

Anotherspecialcaseofthetwo-coinalgorithm.Inthisalgorithm,c/dmustbe0orgreater.

1. Ifxis0,fliptheμinputcoinandreturntheresult.2. Withprobabilityy/(x+y),fliptheμinputcoinandreturntheresult.3. Fliptheinputcoin.Iftheflipreturns1,return0.Otherwise,gotostep2.

Note:Inthisspecialcaseofthetwo-coinalgorithm,β=1,c=1,d=x/y,oldλequals1,andμequalsnewλ.

Example:μ/(1+(x/y)*λ)(takestwoinputcoinsthatsimulateλorμ,respectively):Runthealgorithmfor1/(1+(x/y)*λ)usingtheλinputcoin.Ifitreturns0,return0.Otherwise,fliptheμinputcoinandreturntheresult.

4.5.18λ+μ

(NacuandPeres2005,proposition14(iii))(16).Thisalgorithmtakestwoinputcoinsthatsimulateλorμ,respectively,andaparameterϵintheopeninterval(0,1−λ−μ).

1. Createaνinputcointhatdoesthefollowing:"Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),fliptheλinputcoinandreturntheresult.Otherwise,fliptheμinputcoinandreturntheresult."

2. RunalinearBernoullifactoryusingtheνinputcoin,x/y=2/1,andϵ=ϵ,andreturntheresult.

4.5.19λ−μ

(NacuandPeres2005,proposition14(iii-iv))(16).Thisalgorithmtakestwoinputcoinsthatsimulateλorμ,respectively,andaparameterϵintheinterval(0,λ−μ](thegreaterϵis,themoreefficient).

1. Createaνinputcointhatdoesthefollowing:"Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),fliptheλinputcoinandreturn1minustheresult.Otherwise,fliptheμinputcoinandreturntheresult."

2. RunalinearBernoullifactoryusingtheνinputcoin,x/y=2/1,andϵ=ϵ,andreturn1minustheresult.

4.5.20ϵ/λ

(Leeetal.2014)(43).Thisalgorithm,inadditiontotheinputcoin,takesaparameterϵintheinterval(0,λ).

1. Setβtomax(ϵ,1/2)andsetγto1−(1−β)/(1−(β/2)).2. Createaμinputcointhatflipstheinputcoinandreturns1minustheresult.3. Withprobabilityϵ,return1.4. RunalinearBernoullifactorywiththeμinputcoin,x/y=1/(1−ϵ),andϵ=γ.If

theresultis0,return0.Otherwise,gotostep3.(Notethatrunningthealgorithmthiswaysimulatestheprobability(λ−ϵ)/(1−ϵ)or1−(1−λ)/(1−ϵ)).

4.5.21μ/λ

Page 32: Bernoulli Factory Algorithms - GitHub Pages

(Morina2021)(44).Thisdivisionalgorithmtakestwoinputcoins,namelyacoinsimulatingthedividendμandacoinsimulatingthedivisorλ,andaparameterϵintheinterval(0,λ−μ].Inthisalgorithm,μmustbelessthanλ.

Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Generateanunbiasedrandombit(either0or1withequalprobability).2. Ifthebitgeneratedinstep1is1,fliptheμinputcoin.Ifitreturns1,return1.3. Ifthebitgeneratedinstep1is0,runthealgorithmforλ−μwithϵ=ϵ.Ifit

returns1,return0.

4.5.22λx/y

Inthealgorithmbelow,thecasewherex/yisintheopeninterval(0,1)isduetoMendo(2019)(23).Thealgorithmworksonlywhenx/yis0orgreater.

1. Ifx/yis0,return1.2. Ifx/yisequalto1,fliptheinputcoinandreturntheresult.3. Ifx/yisgreaterthan1:

1. Setiparttofloor(x/y)andfparttorem(x,y)(equivalenttox-y*floor(x/y)).2. Iffpartisgreaterthan0,subtract1fromipart,thencallthisalgorithm

recursivelywithx=floor(fpart/2)andy=y,thencallthisalgorithm,againrecursively,withx=fpart−floor(fpart/2)andy=y.Return0ifeithercallreturns0.(Thisisdoneratherthanthemoreobviousapproachinordertoavoidcallingthisalgorithmwithfractionalpartsverycloseto0,becausethealgorithmrunsmuchmoreslowlythanforfractionalpartscloserto1.)

3. Ifipartis1orgreater,fliptheinputcoinipartmanytimes.Return0ifanyoftheseflipsreturns1.

4. Return1.4. x/yislessthan1,sosetito1.5. Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:

1. Fliptheinputcoin;ifitreturns1,return1.2. Withprobabilityx/(y*i),return0.(Note:x/(y*i)=(x/y)*(1/i).)3. Add1toi.

Notes:

1. Whenx/yislessthan1,theminimumnumberofcoinflipsneeded,onaverage,bythisalgorithmwillgrowwithoutboundasλapproaches0.Infact,nofastBernoullifactoryalgorithmcanavoidthisunboundedgrowthwithoutadditionalinformationonλ(Mendo2019)(23).

2. AnotheralgorithmisdiscussedintheonlinecommunityCrossValidated.

4.5.23λμ

Thisalgorithmisbasedonthepreviousone,butchangedtoacceptasecondinputcoin(whichoutputsheadswithprobabilityμ)ratherthanafixedvaluefortheexponent.

Setito1.Thendothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Fliptheinputcointhatsimulatesthebase,λ;ifitreturns1,return1.2. Fliptheinputcointhatsimulatestheexponent,μ;ifitreturns1,return0with

probability1/i.3. Add1toi.

4.5.24sqrt(λ)

Page 33: Bernoulli Factory Algorithms - GitHub Pages

Specialcaseofthepreviousalgorithmwithμ=1/2.

Setito1.Thendothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Fliptheinputcoin.Ifitreturns1,return1.2. Withprobability1/(i*2),return0.3. Add1toiandgotostep1.

4.5.25λ*x/y

Ingeneral,thisfunctionwilltouch0or1somewhereintheopeninterval(0,1),whenx/y>1.Thismakesthefunctionrelativelynon-trivialtosimulateinthiscase.

Huberhassuggestedseveralalgorithmsforthisfunctionovertheyears.

ThefirstalgorithminthisdocumentcomesfromHuber(2014)(4).Itusesthreeparameters:

xandyareintegerssuchthatx/y>0andy!=0.ϵisarationalnumberintheopeninterval(0,1).Ifx/yisgreaterthan1,ϵmustbeintheopeninterval(0,1−λ*x/y),inordertoboundthefunctionawayfrom0and1.Thegreaterϵis,themoreefficient.

Asaresult,someknowledgeofλhastobeavailabletothealgorithm.Thealgorithmasdescribedbelowalsoincludescertainspecialcases,notmentionedinHuber,tomakeitmoregeneral.

1. Specialcases:Ifxis0,return0.Otherwise,ifxequalsy,fliptheinputcoinandreturntheresult.Otherwise,ifxislessthany,thendothefollowing:"Withprobabilityx/y,fliptheinputcoinandreturntheresult;otherwisereturn0."

2. Setctox/y,andsetkto23/(5*ϵ).3. Ifϵisgreaterthan644/1000,setϵto644/1000.4. Setito1.5. Whileiisnot0:

1. Fliptheinputcoin.Ifitreturns0,thengeneratenumbersthatareeach1withprobability(c−1)/cand0otherwise,until1isgeneratedthisway,thenadd1toiforeachnumbergeneratedthisway(includingthelast).

2. Subtract1fromi.3. Ifiiskorgreater:

1. Generateinumbersthatareeach1withprobability2/(ϵ+2)or0otherwise.Ifanyofthosenumbersis0,return0.

2. Multiplycby2/(ϵ+2),thendivideϵby2,thenmultiplykby2.6. (iis0.)Return1.

Huber(2016)(36)presentedasecondalgorithmusingthesamethreeparameters,butit'somittedherebecauseitappearstoperformworsethanthealgorithmgivenaboveandthealgorithmfor(λ*x/y)ibelow(seealsoMorina2021(44)).

Huber(2016)alsoincludedathirdalgorithmthatsimulatesλ*x/y.Thealgorithmworksonlyifλ*x/yisknowntobelessthan1/2.Thisthirdalgorithmtakesthreeparameters:

xandyareintegerssuchthatx/y>0andy!=0.misarationalnumbersuchthatλ*x/y≤m<1/2.

Thealgorithmfollows.

1. Thesamespecialcasesasforthefirstalgorithminthissectionapply.

Page 34: Bernoulli Factory Algorithms - GitHub Pages

2. RunthelogisticBernoullifactoryalgorithmwithc/d=(x/y)/(1−2*m).Ifitreturns0,return0.

3. Withprobability1−2*m,return1.4. RunalinearBernoullifactorywithx/y=(x/y)/(2*m)andϵ=1−m.

Note:Forapproximatemethodstosimulateλ*(x/y),seethepage"SupplementalNotesforBernoulliFactoryAlgorithms".

4.5.26(λ*x/y)i

(Huber2019)(45).Thisalgorithmusesfourparameters:

xandyareintegerssuchthatx/y>0andy!=0.iisaninteger0orgreater.ϵisarationalnumberintheopeninterval(0,1).Ifx/yisgreaterthan1,ϵmustbeintheopeninterval(0,1−λ*x/y).

ThealgorithmalsohasspecialcasesnotmentionedinHuber2019.

1. Specialcases:Ifiis0,return1.Ifxis0,return0.Otherwise,ifxequalsyandiequals1,fliptheinputcoinandreturntheresult.

2. Specialcase:Ifxislessthanyandi=1,thendothefollowing:"Withprobabilityx/y,fliptheinputcoinandreturntheresult;otherwisereturn0."

3. Specialcase:Ifxislessthany,thencreateasecondarycointhatdoesthefollowing:"Withprobabilityx/y,fliptheinputcoinandreturntheresult;otherwisereturn0",thenrunthealgorithmforλx/ywithx=i,y=1,andλbeingthesecondarycoin,thenreturntheresultofthatrun.

4. Settto355/100andctox/y.5. Whileiisnot0:

1. Whilei>t/ϵ:1. Setβto(1−ϵ/2)/(1−ϵ).2. Runthealgorithmfor(a/b)x/y(givenintheirrationalconstantssection)

witha=1,b=β,x=i,andy=1.Iftherunreturns0,return0.3. Multiplycbyβ,thendivideϵby2.

2. RunthelogisticBernoullifactorywithc/d=c,thensetztotheresult.Setitoi+1−z*2.

6. (iis0.)Return1.

4.5.27LinearBernoulliFactories

Inthisdocument,alinearBernoullifactoryreferstooneofthefollowing:

Thefirstalgorithmforλ*x/ywiththestatedparametersx,y,andϵ.Thealgorithmfor(λ*x/y)iwiththestatedparametersx,y,andϵ,andwithi=1(seeprevioussection).

4.5.28arctan(λ)/λ

BasedonthealgorithmfromFlajoletetal.(2010)(1),butusesthetwo-coinalgorithm(whichisuniformlyfastforeveryλparameter)ratherthantheeven-parityconstruction(whichisnot).(46)

Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswith

Page 35: Bernoulli Factory Algorithms - GitHub Pages

probability1/2),return1.2. Generateauniform(0,1)randomvariateu,ifitwasn'tgeneratedyet.3. Samplefromthenumberutwice,andfliptheinputcointwice.Ifallofthese

callsandflipsreturn1,return0.

4.5.29arctan(λ)

(Flajoletetal.,2010)(1):Callthealgorithmforarctan(λ)/λandfliptheinputcoin.Return1ifthecallandflipbothreturn1,or0otherwise.

4.5.30cos(λ)

Thisalgorithmadaptsthegeneralmartingalealgorithmforthisfunction'sseriesexpansion.Infact,thisisaspecialcaseofAlgorithm3of(Łatuszyńskietal.2009/2011)(24)(whichismoregeneralthanProposition3.4,thegeneralmartingalealgorithm).Theseriesexpansionforcos(λ)is1−λ2/(2!)+λ4/(4!)−...,whichisanalternatingseriesexcepttheexponentisincreasedby2(ratherthan1)witheachterm.Thecoefficientsarethus1,1/(2!),1/(4!),....Alowertruncationoftheseriesisatruncationofthatseriesthatendswithaminusterm,andthecorrespondinguppertruncationisthesametruncationbutwithoutthelastminusterm.ThisseriesexpansionmeetstherequirementsofAlgorithm3because,withprobability1—

thelowertruncationislessthanorequaltoitscorrespondinguppertruncation,theloweranduppertruncationsareintheinterval[0,1],eachlowertruncationisgreaterthanorequaltothepreviouslowertruncation,eachuppertruncationislessthanorequaltothepreviousuppertruncation,andtheloweranduppertruncationshaveanexpectedvaluethatapproachesλfrombelowandabove.

Thealgorithmtosimulatecos(λ)follows.

1. Setuto1,setwto1,setℓto0,setnto1,andsetfacto2.2. Generateauniform(0,1)randomvariateret.3. Ifwisnot0,fliptheinputcoin.Iftheflipreturns0,setwto0.Dothisstepagain.

(Notethatinthegeneralmartingalealgorithm,onlyonecoinisflippedinthisstep.Uptotwocoinsareflippedinsteadbecausetheexponentincreasesby2ratherthan1.)

4. Ifniseven,setutoℓ+w/fac.Otherwise,setℓtou−w/fac.(Herewedividebythefactorialof2-times-n.)

5. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenextstep.Ifneitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

6. Add1ton,thenmultiplyfacby(n*2−1)*(n*2),thengotostep3.

4.5.31sin(λ)

ThisalgorithmislikewiseaspecialcaseofAlgorithm3of(Łatuszyńskietal.2009/2011)(24).sin(λ)canberewrittenasλ*(1−λ2/(3!)+λ4/(5!)−...),whichincludesanalternatingserieswheretheexponentisincreasedby2(ratherthan1)witheachterm.Thecoefficientsarethus1,1/(3!),1/(5!),....ThisseriesexpansionmeetstherequirementsofAlgorithm3forthesamereasonsasthecos(λ)seriesdoes.

Thealgorithmtosimulatesin(λ)follows.

Page 36: Bernoulli Factory Algorithms - GitHub Pages

1. Fliptheinputcoin.Ifitreturns0,return0.2. Setuto1,setwto1,setℓto0,setnto1,andsetfacto6.3. Generateauniform(0,1)randomvariateret.4. Ifwisnot0,fliptheinputcoin.Iftheflipreturns0,setwto0.Dothisstepagain.5. Ifniseven,setutoℓ+w/fac.Otherwise,setℓtou−w/fac.6. Ifretislessthan(orequalto)ℓ,return1.Ifretislessthanu,gotothenextstep.If

neitheristhecase,return0.(IfretisauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

7. Add1ton,thenmultiplyfacby(n*2)*(n*2+1),thengotostep4.

4.5.32(1−λ)/cos(λ)

(Flajoletetal.,2010)(1):

1. Fliptheinputcoinuntiltheflipreturns0.ThensetGtothenumberoftimestheflipreturns1thisway.

2. IfGisodd,return0.3. Generateauniform(0,1)randomvariateU,thensetito1.4. WhileiislessthanG:

1. Generateauniform(0,1)randomvariateV.2. IfiisoddandVislessthanU,return0.3. IfiisevenandUislessthanV,return0.4. Add1toi,thensetUtoV.

5. Return1.

4.5.33(1−λ)*tan(λ)

(Flajoletetal.,2010)(1):

1. Fliptheinputcoinuntiltheflipreturns0.ThensetGtothenumberoftimestheflipreturns1thisway.

2. IfGiseven,return0.3. Generateauniform(0,1)randomvariateU,thensetito1.4. WhileiislessthanG:

1. Generateauniform(0,1)randomvariateV.2. IfiisoddandVislessthanU,return0.3. IfiisevenandUislessthanV,return0.4. Add1toi,thensetUtoV.

5. Return1.

4.5.34ln(1+λ)

BasedonthealgorithmfromFlajoletetal.(2010)(1),butusesthetwo-coinalgorithm(whichisuniformlyfastforeveryλparameter)ratherthantheeven-parityconstruction(whichisnot).(47)

Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswith

probability1/2),fliptheinputcoinandreturntheresult.2. Generateauniform(0,1)randomvariateu,ifuwasn'tgeneratedyet.3. Samplefromthenumberu,thenfliptheinputcoin.Ifthecallandtheflip

bothreturn1,return0.

Page 37: Bernoulli Factory Algorithms - GitHub Pages

4.5.35ln((c+d+λ)/c)

Inthisalgorithm,dandcareintegers,0<c<d,andd≥0.

Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswith

probability1/2),runthealgorithmfor(d+λ)/cwithd=dandc=c,andreturntheresult.

2. Generateauniform(0,1)randomvariateu,ifuwasn'tgeneratedyet.3. Samplefromthenumberu,thenrunthealgorithmfor(d+λ)/cwithd=

dandc=c.Ifbothcallsreturn1,return0.

4.5.36arcsin(λ)+sqrt(1−λ2)−1

(Flajoletetal.,2010)(1).Thealgorithmgivenhereusesthetwo-coinalgorithmratherthantheeven-parityconstruction.

1. Generateauniform(0,1)randomvariateu.2. Createasecondarycoinμthatdoesthefollowing:"Samplefromthenumberu

twice,andfliptheinputcointwice.Ifallofthesecallsandflipsreturn1,return0.Otherwise,return1."

3. Callthealgorithmforμ1/2usingthesecondarycoinμ.Ifitreturns0,return0.4. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),fliptheinputcoinandreturntheresult.5. Samplefromthenumberuonce,andfliptheinputcoinonce.Ifboththecalland

flipreturn1,return0.Otherwise,gotostep4.

4.5.37arcsin(λ)/2

TheFlajoletpaperdoesn'texplainindetailhowarcsin(λ)/2arisesoutofarcsin(λ)+sqrt(1−λ2)−1viaBernoullifactoryconstructions,buthereisanalgorithm.(48)However,thenumberofinputcoinflipsisexpectedtogrowwithoutboundasλapproaches1.

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),runthealgorithmforarcsin(λ)+sqrt(1−λ2)−1andreturntheresult.

2. Createasecondarycoinμthatdoesthefollowing:"Fliptheinputcointwice.Ifbothflipsreturn1,return0.Otherwise,return1."(Thecoinsimulates1−λ2.)

3. Callthealgorithmforμ1/2usingthesecondarycoinμ.Ifitreturns0,return1;otherwise,return0.(Thisstepeffectivelycancelsoutthesqrt(1−λ2)−1partanddividesby2.)

4.5.38ExpressionsInvolvingPolylogarithms

ThefollowingalgorithmsimulatestheexpressionLir(λ)*(1/λ−1),whererisaninteger1orgreater.However,evenwitharelativelysmallrsuchas6,theexpressionquicklyapproachesastraightline.

Ifλis1/2,thisexpressionsimplifiestoLir(1/2).Seealso(Flajoletetal.,2010)(1).Seealso"ConvexCombinations"(thecaseof1/2worksbydecomposingtheseriesformingthepolylogarithmicconstantintog(i)=(1/2)i,whichsumsto1,andhi()=1/ir,wherei≥1).

1. Fliptheinputcoinuntilitreturns0,andlettbe1plusthenumberoftimesthecoin

Page 38: Bernoulli Factory Algorithms - GitHub Pages

returned1thisway.2. Returnanumberthatis1withprobability1/trand0otherwise.

4.5.39OtherFactoryFunctions

Algorithmsinboldaregiveninthispage.

Tosimulate: Followthisalgorithm:

1/sqrt(π) Createλcoinforalgorithm1/π.Runalgorithmforsqrt(λ).

1/sqrt(h+λ)(λisunknownheadsprobabilityofacoin;h≥1isarationalnumber.)Createμcoinforalgorithmd/(c+λ)withc=handd=1.Runalgorithmforsqrt(λ)withλbeingtheμcoin.

1/(c+λ) (c≥1isarationalnumber.)Runalgorithmford/(c+λ)withd=1.

exp(−((1−λ)1*c))

((Dughmietal.2017)(35);appliesanexponentialweight—here,c—toaninputcoin)(1)Ifcis0,return1.(2)GenerateaPoisson(c)randominteger,callitN.(3)Fliptheinputcoinuntiltheflipreturns0orthecoinisflippedNtimes,whichevercomesfirst,thenreturnanumberthatis1ifNis0orallofthecoinflips(includingthelast)return1,or0otherwise.

1−ln(1+λ) Runalgorithmforln(1+λ),thenreturn1minustheresult.(49)λ/(1+λ) Runalgorithmfor1/(1+λ),thenreturn1minustheresult.

ν*1+(1−ν)*μ=ν+μ−(ν*μ)

(LogicalOR.Flajoletetal.,2010(1).Specialcaseofν*λ+(1−ν)*μwithλ=1.νandμareunknownheadsprobabilitiesoftwocoins.)Fliptheνinputcoinandtheμinputcoin.Return1ifeitherflipreturns1,and0otherwise.

1−ν(Complement.Flajoletetal.,2010(1).Specialcaseofν*λ+(1−ν)*μwithλ=0andμ=1.νisunknownheadsprobabilityofacoin.)Fliptheνinputcoinandreturn1minustheresult.

ν*λ(LogicalANDorProduct.Flajoletetal.,2010(1).Specialcaseofν*λ+(1−ν)*μwithμ=0.νandλareunknownheadsprobabilitiesoftwocoins.)Fliptheνinputcoinandtheλinputcoin.Return1ifbothflipsreturn1,and0otherwise.

(λ+μ)/2=(1/2)*λ+(1/2)*μ

(Mean.NacuandPeres2005,proposition14(iii)(16);Flajoletetal.,2010(1).Specialcaseofν*λ+(1−ν)*μwithν=1/2.λandμareunknownheadsprobabilitiesoftwocoins.)Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),fliptheλinputcoinandreturntheresult.Otherwise,fliptheμinputcoinandreturntheresult.

4.6AlgorithmsforSpecificConstantsThissectionshowsalgorithmstosimulateaprobabilityequaltoaspecifickindofirrationalnumber.

4.6.11/φ(1dividedbythegoldenratio)

Thisalgorithmusesthealgorithmdescribedinthesectiononcontinuedfractionsto

Page 39: Bernoulli Factory Algorithms - GitHub Pages

simulate1dividedbythegoldenratio,whosecontinuedfraction'spartialdenominatorsare1,1,1,1,....

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),return1.

2. Doaseparaterunofthecurrentlyrunningalgorithm.Iftheseparaterunreturns1,return0.Otherwise,gotostep1.

Anotheralgorithmtosamplethisprobability,whichappearsinFrenchin(PenaudandRoques2002)(50),isiterativebutrequiresarithmeticoperations.

4.6.2sqrt(2)−1

Anotherexampleofacontinuedfractionisthatofthefractionalpartofthesquarerootof2,wherethepartialdenominatorsare2,2,2,2,....Thealgorithmtosimulatethisnumberisasfollows:

1. Withprobability2/3,generateanunbiasedrandombitandreturnthatbit.2. Doaseparaterunofthecurrentlyrunningalgorithm.Iftheseparaterunreturns1,

return0.Otherwise,gotostep1.

4.6.31/sqrt(2)

Thisthirdexampleofacontinuedfractionshowshowtosimulateaprobability1/z,wherez>1hasaknownsimplecontinuedfractionexpansion.Inthiscase,thepartialdenominatorsareasfollows:floor(z),a[1],a[2],...,wherethea[i]arez'spartialdenominators(notincludingz'sintegerpart).Intheexampleof1/sqrt(2),thepartialdenominatorsare1,2,2,2,...,where1comesfirstsincefloor(sqrt(2))=1.Thealgorithmtosimulate1/sqrt(2)isasfollows:

Thealgorithmbeginswithposequalto1.Thenthefollowingstepsaretaken.

1. Ifposis1,return1withprobability1/2.Ifposisgreaterthan1,thenwithprobability2/3,generateanunbiasedrandombitandreturnthatbit.

2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithpos=pos+1.Iftheseparaterunreturns1,return0.Otherwise,gotostep1.

4.6.4tanh(1/2)or(exp(1)−1)/(exp(1)+1)

Thealgorithmbeginswithkequalto2.Thenthefollowingstepsaretaken.

1. Withprobabilityk/(1+k),returnanumberthatis1withprobability1/kand0otherwise.

2. Doaseparaterunofthecurrentlyrunningalgorithm,butwithk=k+4.Iftheseparaterunreturns1,return0.Otherwise,gotostep1.

4.6.5arctan(x/y)*y/x

(Flajoletetal.,2010)(1):

1. Generateauniform(0,1)randomvariateu.2. Generateanumberthatis1withprobabilityx*x/(y*y),or0otherwise.Ifthe

numberis0,return1.3. Samplefromthenumberutwice.Ifeitherofthesecallsreturns0,return1.4. Generateanumberthatis1withprobabilityx*x/(y*y),or0otherwise.Ifthe

numberis0,return0.

Page 40: Bernoulli Factory Algorithms - GitHub Pages

5. Samplefromthenumberutwice.Ifeitherofthesecallsreturns0,return0.Otherwise,gotostep2.

Observingthattheeven-parityconstructionusedintheFlajoletpaperisequivalenttothetwo-coinalgorithm,whichisuniformlyfast,thealgorithmabovecanbemadeuniformlyfastasfollows:

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),return1.

2. Generateauniform(0,1)randomvariateu,ifitwasn'tgeneratedyet.3. Withprobabilityx*x/(y*y),samplefromthenumberutwice.Ifbothofthese

callsreturn1,return0.4. Gotostep1.

4.6.6π/12

Twoalgorithms:

Firstalgorithm:Usethealgorithmforarcsin(λ)/2,butwherethealgorithmsaysto"fliptheinputcoin",insteadgenerateanunbiasedrandombit.Secondalgorithm:Withprobability2/3,return0.Otherwise,runthealgorithmforπ/4andreturntheresult.

4.6.7π/4

(Flajoletetal.,2010)(1):

1. Generatearandomintegerintheinterval0,6),callitn.2. Ifnislessthan3,returntheresultofthealgorithmforarctan(1/2)*2.

Otherwise,ifnis3,return0.Otherwise,returntheresultofthealgorithmforarctan(1/3)*3.

4.6.81/π

(Flajoletetal.,2010)[(1):

1. Settto0.2. Withprobability1/4,add1totandrepeatthisstep.Otherwise,gotostep3.3. Withprobability1/4,add1totandrepeatthisstep.Otherwise,gotostep4.4. Withprobability5/9,add1tot.5. Generate2*tunbiasedrandombits(thatis,either0or1,chosenwithequal

probability),andreturn0iftherearemorezerosthanonesgeneratedthiswayormoreonesthanzeros.(Notethatthisconditioncanbecheckedevenbeforeallthebitsaregeneratedthisway.)Dothissteptwomoretimes.

6. Return1.

Forasketchofhowthisalgorithmisderived,seetheappendix.

4.6.9(a/b)x/y

Inthealgorithmbelow,a,b,x,andyareintegers,andthecasewherex/yisintheopeninterval(0,1)isduetorecentworkbyMendo(2019)(23).Thisalgorithmworksonlyif—

x/yis0orgreateranda/bisintheinterval[0,1],orx/yislessthan0anda/bis1orgreater.

Page 41: Bernoulli Factory Algorithms - GitHub Pages

Thealgorithmfollows.

1. Ifx/yislessthan0,swapaandb,andremovethesignfromx/y.Ifa/bisnownolongerintheinterval[0,1],returnanerror.

2. Ifx/yisequalto1,return1withprobabilitya/band0otherwise.3. Ifxis0,return1.Otherwise,ifais0,return0.Otherwise,ifaequalsb,return1.4. Ifx/yisgreaterthan1:

1. Setiparttofloor(x/y)andfparttorem(x,y)(equivalenttox-y*floor(x/y)).2. Iffpartisgreaterthan0,subtract1fromipart,thencallthisalgorithm

recursivelywithx=floor(fpart/2)andy=y,thencallthisalgorithm,againrecursively,withx=fpart−floor(fpart/2)andy=y.Return0ifeithercallreturns0.(Thisisdoneratherthanthemoreobviousapproachinordertoavoidcallingthisalgorithmwithfractionalpartsverycloseto0,becausethealgorithmrunsmuchmoreslowlythanforfractionalpartscloserto1.)

3. Ifipartis1orgreater,generateatrandomanumberthatis1withprobabilityaipart/bipartor0otherwise.(Orgenerate,atrandom,ipartmanynumbersthatareeach1withprobabilitya/bor0otherwise,thenmultiplythemallintoonenumber.)Ifthatnumberis0,return0.

4. Return1.5. Setito1.6. Withprobabilitya/b,return1.7. Otherwise,withprobabilityx/(y*i),return0.8. Add1toiandgotostep6.

4.6.10exp(−x/y)

Thisalgorithmtakesintegersx≥0andy>0andoutputs1withprobabilityexp(-x/y)or0otherwise.Itoriginatesfrom(Canonneetal.2020)(51).

1. Specialcase:Ifxis0,return1.(Thisisbecausetheprobabilitybecomesexp(0)=1.)2. Ifx>y(sox/yisgreaterthan1),callthisalgorithm(recursively)floor(x/y)times

withx=y=1andoncewithx=x−floor(x/y)*yandy=y.Return1ifallthesecallsreturn1;otherwise,return0.

3. Setrto1andito1.4. Returnrwithprobability(y*i−x)/(y*i).5. Setrto1−r,add1toi,andgotostep4.

4.6.11exp(−z)

Thisalgorithmissimilartothepreviousalgorithm,exceptthattheexponent,z,canbeanyrealnumber0orgreater,aslongaszcanberewrittenasthesumofoneormorecomponentswhosefractionalpartscaneachbesimulatedbyaBernoullifactoryalgorithmthatoutputsheadswithprobabilityequaltothatfractionalpart.(Thismakesuseoftheidentityexp(−a)=exp(−b)*exp(−c).)

Morespecifically:

1. Decomposezinton>0componentsthatsumtoz,allofwhicharegreaterthan0.Forexample,ifz=3.5,itcanbedecomposedintoonlyonecomponent,3.5(whosefractionalpartistrivialtosimulate),andifz=π,itcanbedecomposedintofourcomponentsthatareall(π/4),whichhasanot-so-trivialsimulationdescribedearlieronthispage.

2. ForeachcomponentLC[i]foundthisway(whereiisin[1,n]),letLI[i]befloor(LC[i])andletLF[i]beLC[i]−floor(LC[i])(LC[i]'sfractionalpart).

Thealgorithmisthenasfollows:

Page 42: Bernoulli Factory Algorithms - GitHub Pages

ForeachcomponentLC[i],callthealgorithmforexp(−LI[i]/1),andcallthegeneralmartingalealgorithmadaptedforexp(−λ)usingtheinputcointhatsimulatesLF[i].Ifanyofthesecallsreturns0,return0;otherwise,return1.(Seealso(Canonneetal.2020)(51).)

4.6.12(a/b)z

Thisalgorithmissimilartothepreviousalgorithmforpowering,exceptthattheexponent,z,canbeanyrealnumber0orgreater,aslongaszcanberewrittenasthesumofoneormorecomponentswhosefractionalpartscaneachbesimulatedbyaBernoullifactoryalgorithmthatoutputsheadswithprobabilityequaltothatfractionalpart.Thisalgorithmmakesuseofasimilaridentityasforexpandworksonlyifzis0orgreateranda/bisintheinterval[0,1].

DecomposezintoLC[i],LI[i],andLF[i]justasfortheexp(−z)algorithm.Thealgorithmisthenasfollows.

Ifzis0,return1.Otherwise,ifais0,return0.Otherwise,foreachcomponentLC[i](untilthealgorithmreturnsanumber):1. Callthealgorithmfor(a/b)x/ywitha=a,b=b,x=LI[i]andy=1.Ifit

returns0,return0.2. Setjto1.3. Generateanumberthatis1withprobabilitya/band0otherwise.Ifthat

numberis1,abortthesestepsandmoveontothenextcomponentor,iftherearenomorecomponents,return1.

4. FliptheinputcointhatsimulatesLF[i](whichistheexponent);ifitreturns1,return0withprobability1/j.

5. Add1tojandgotosubstep2.

4.6.131/(1+exp(x/(y*2prec))(LogisticExp)

Thisistheprobabilitythatthebitatprec(theprecthbitafterthepoint)issetforanexponentialrandomvariatewithratex/y.ThisalgorithmisaspecialcaseofthelogisticBernoullifactory.

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),return1.

2. Callthealgorithmforexp(−x/(y*2prec)).Ifthecallreturns1,return1.Otherwise,gotostep1.

4.6.141/(1+exp(z/2prec))(LogisticExp)

Thisissimilartothepreviousalgorithm,exceptthatzcanbeanyrealnumberdescribedinthealgorithmforexp(−z).

DecomposezintoLC[i],LI[i],andLF[i]justasfortheexp(−z)algorithm.Thealgorithmisthenasfollows.

1. ForeachcomponentLC[i],createaninputcointhatdoesthefollowing:"(a)Withprobability1/(2prec),return1iftheinputcointhatsimulatesLF[i]returns1;(b)Return0".

2. Return0withprobability1/2.3. Callthealgorithmforexp(−x/y)withx=LI[1]+LI[2]+...+LI[n]andy=2prec.

Ifthiscallreturns0,gotostep2.

Page 43: Bernoulli Factory Algorithms - GitHub Pages

4. ForeachcomponentLC[i],callthealgorithmforexp(−λ),usingthecorrespondinginputcoinforLC[i]createdinstep1.Ifanyofthesecallsreturns0,gotostep2.Otherwise,return1.

4.6.15ζ(3)*3/4andOtherZeta-RelatedConstants

(Flajoletetal.,2010)(1).Itcanbeseenasatripleintegralofthefunction1/(1+a*b*c),wherea,b,andcareuniform(0,1)randomvariates.Thisalgorithmisgivenbelow,butusingthetwo-coinalgorithminsteadoftheeven-parityconstruction.Notethatthetripleintegralinsection5ofthepaperisζ(3)*3/4,notζ(3)*7/8.(Here,ζ(x)istheRiemannzetafunction.)

1. Generatethreeuniform(0,1)randomvariates.2. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),return1.3. Samplefromeachofthethreenumbersgeneratedinstep1.Ifallthreecalls

return1,return0.Otherwise,gotostep2.(Thisimplementsatripleintegralinvolvingtheuniformrandomvariates.)

Thiscanbeextendedtocoveranyconstantoftheformζ(k)*(1−2−(k−1))wherek≥2isaninteger,assuggestedslightlybytheFlajoletpaperwhenitmentionsζ(5)*31/32(whichshouldprobablyreadζ(5)*15/16instead),usingthefollowingalgorithm.

1. Generatekuniform(0,1)randomvariates.2. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),return1.3. Samplefromeachoftheknumbersgeneratedinstep1.Ifallkcallsreturn1,

return0.Otherwise,gotostep2.

4.6.16erf(x)/erf(1)

Inthefollowingalgorithm,xisarealnumberintheinterval[0,1].

1. Generateauniform(0,1)randomvariate,callitret.2. Setutopointtothesamevalueasret,andsetkto1.3. (Inthisandthenextstep,wecreatev,whichisthemaximumoftwouniform[0,1]

randomvariates.)Generatetwouniform(0,1)randomvariates,callthemaandb.4. Ifaislessthanb,setvtob.Otherwise,setvtoa.5. Ifvislessthanu,setutov,thenadd1tok,thengotostep3.6. Ifkisodd,return1ifretislessthanx,or0otherwise.(Ifretisimplementedasa

uniformPSRN,thiscomparisonshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

7. Gotostep1.

Infact,thisalgorithmtakesadvantageofatheoremrelatedtotheForsythemethodofrandomsampling(Forsythe1972)(52).Seethesection"ProbabilitiesArisingfromCertainPermutations"intheappendixformoreinformation.

Note:Ifthelaststepinthealgorithmreads"Return0"ratherthan"Gotostep1",thenthealgorithmsimulatestheprobabilityerf(x)*sqrt(π)/2instead.

4.6.172/(1+exp(2))or(1+exp(0))/(1+exp(1))

Thisalgorithmtakesadvantageofformula2mentionedinthesection"ProbabilitiesArisingfromCertainPermutations"intheappendix.Here,therelevantprobabilityis

Page 44: Bernoulli Factory Algorithms - GitHub Pages

rewrittenas1−(∫(−∞,1)(1−exp(−max(0,min(1,z))))*exp(−z)dz)/(∫(−∞,∞)(1−exp(−max(0,min(1,z)))*exp(−z)dz).

1. Generateanexponentialrandomvariateex,thensetkto1.2. Setutopointtothesamevalueasex.3. Generateauniform(0,1)randomvariatev.4. Setstopto1ifuislessthanv,and0otherwise.5. Ifstopis1andkiseven,returnanumberthatis0ifexislessthan1,and1

otherwise.Otherwise,ifstopis1,gotostep1.6. Setutov,thenadd1tok,thengotostep3.

4.6.18(1+exp(1))/(1+exp(2))

Thisalgorithmtakesadvantageofthetheoremmentionedinthesection"ProbabilitiesArisingfromCertainPermutations"intheappendix.Here,therelevantprobabilityisrewrittenas1−(∫(−∞,1/2)exp(−max(0,min(1,z)))*exp(−z)dz)/(∫(−∞,∞)exp(−max(0,min(1,z))*exp(−z)dz).

1. Generateanexponentialrandomvariateex,thensetkto1.2. Setutopointtothesamevalueasex.3. Generateauniform(0,1)randomvariatev.4. Setstopto1ifuislessthanv,and0otherwise.5. Ifstopis1andkisodd,returnanumberthatis0ifexislessthan1/2,and1

otherwise.Otherwise,ifstopis1,gotostep1.6. Setutov,thenadd1tok,thengotostep3.

4.6.19(1+exp(k))/(1+exp(k+1))

Thisalgorithmsimulatesthisprobabilitybycomputinglowerandupperboundsofexp(1),whichimproveasmoreandmoredigitsarecalculated.TheseboundsarecalculatedbyanalgorithmbyCitterioandPavani(2016)(53).Notetheuseofthemethodologyin(Łatuszyńskietal.2009/2011,algorithm2)(24)inthisalgorithm.Inthisalgorithm,kmustbeaninteger0orgreater.

1. Ifkis0,runthealgorithmfor2/(1+exp(2))andreturntheresult.Ifkis1,runthealgorithmfor(1+exp(1))/(1+exp(2))andreturntheresult.

2. Generateauniform(0,1)randomvariate,callitret.3. Ifkis3orgreater,return0ifretisgreaterthan38/100,or1ifretislessthan

36/100.(Thisisanearlyreturnstep.IfretisimplementedasauniformPSRN,thesecomparisonsshouldbedoneviatheURandLessThanRealalgorithm,whichisdescribedinmyarticleonPSRNs.)

4. Setdto2.5. Calculatealowerandupperboundofexp(1)(LBandUB,respectively)intheformof

rationalnumberswhosenumeratorhasatmostddigits,usingtheCitterioandPavanialgorithm.Fordetails,seetheappendix.

6. Setrlto(1+LBk)/(1+UBk+1),andsetruto(1+UBk)/(1+LBk+1);boththesenumbersshouldbecalculatedusingrationalarithmetic.

7. Ifretisgreaterthanru,return0.Ifretislessthanrl,return1.(IfretisimplementedasauniformPSRN,thesecomparisonsshouldbedoneviaURandLessThanReal.)

8. Add1todandgotostep5.

4.6.20Euler–Mascheroniconstantγ

Page 45: Bernoulli Factory Algorithms - GitHub Pages

ThefollowingalgorithmtosimulatetheEuler–MascheroniconstantγisduetoMendo(2020)(31).Thissolvesanopenquestiongivenin(Flajoletetal.,2010)(1).TheseriesusedwasgivenbySondow(2005)(54).Analgorithmforγappearshereeventhoughitisnotyetknownwhetherthisconstantisirrational.

1. Setϵto1,thensetn,lamunq,lam,s,k,andprevto0each.2. Add1tok,thenadds/(2k)tolam.3. Iflamunq+ϵ≤lam+1/(2k),gotostep8.4. Iflamunq>lam+1/(2k),gotostep8.5. Iflamunq>lam+1/(2k+1)andlamunq+ϵ<3/(2k+1),gotostep8.6. (Thisstepaddsatermoftheseriesforγtolamunq,andsetsϵtoanupperboundon

theerrorthatresultsiftheseriesistruncatedaftersummingthisandthepreviousterms.)Ifnis0,add1/2tolamunqandsetϵto1/2.Otherwise,addB(n)/(2*n*(2*n+1)*(2*n+2))tolamunqandsetϵtomin(prev,(2+B(n)+(1/n))/(16*n*n)),whereB(n)istheminimumnumberofbitsneededtostoren(orthesmallestintegerb≥1suchthatn<2b).

7. Add1ton,thensetprevtoϵ,thengotostep3.8. Letboundbelam+1/(2k).Iflamunq+ϵ≤bound,setsto0.Otherwise,iflamunq>

bound,setsto2.Otherwise,setsto1.9. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),gotostep2.Otherwise,returnanumberthatis0ifsis0,1ifsis2,oranunbiasedrandombit(either0or1withequalprobability)otherwise.

4.6.21exp(−x/y)*z/t

ThisalgorithmisagainbasedonanalgorithmduetoMendo(2020)(31).Inthisalgorithm,x,y,z,andtareintegersgreaterthan0,exceptxand/orzmaybe0,andmustbesuchthatexp(−x/y)*z/tisintheinterval[0,1].

1. Ifzis0,return0.Ifxis0,returnanumberthatis1withprobabilityz/tand0otherwise.

2. Setϵto1,thensetn,lamunq,lam,s,andkto0each.3. Add1tok,thenadds/(2k)tolam.4. Iflamunq+ϵ≤lam+1/(2k),gotostep9.5. Iflamunq>lam+1/(2k),gotostep9.6. Iflamunq>lam+1/(2k+1)andlamunq+ϵ<3/(2k+1),gotostep8.7. (Thisstepaddstwotermsofexp(−x/y)'salternatingseries,multipliedbyz/t,to

lamunq,andsetsϵtoanupperboundonhowclosethecurrentsumistothedesiredprobability.)Letmben*2.Setϵtoz*xm/(t*(m!)*ym).Ifmis0,addz*(y−x)/(t*y)tolamunq.Otherwise,addz*xm*(m*y−x+y)/(t*ym+1*((m+1)!))tolamunq.

8. Add1tonandgotostep4.9. Letboundbelam+1/(2k).Iflamunq+ϵ≤bound,setsto0.Otherwise,iflamunq>

bound,setsto2.Otherwise,setsto1.10. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability

1/2),gotostep3.Otherwise,returnanumberthatis0ifsis0,1ifsis2,oranunbiasedrandombit(either0or1withequalprobability)otherwise.

4.6.22ln(1+y/z)

Seealsothealgorithmgivenearlierforln(1+λ).Inthisalgorithm,y/zisarationalnumberintheinterval[0,1].(Thus,thespecialcaseln(2)resultswheny/z=1/1.)

Page 46: Bernoulli Factory Algorithms - GitHub Pages

1. Ify/zis0,return0.2. Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:

1. Generateanunbiasedrandombit.Ifthatbitis1(whichhappenswithprobability1/2),returnanumberthatis1withprobabilityy/zand0otherwise.

2. Generateauniform(0,1)randomvariateu,ifuwasn'tgeneratedyet.3. Samplefromthenumberu,thengenerateanumberthatis1with

probabilityy/zand0otherwise.Ifthecallreturns1andthenumbergeneratedis1,return0.

5RequestsandOpenQuestions1. Besidesthealgorithmsonthispage,whatsimulationsexistthatare"relatively

simple"andsucceedwithanirrationalprobabilitybetween0and1?Whatabout"relativelysimple"Bernoullifactoryalgorithmsforfactoryfunctions?Here,"relativelysimple"meansthatthealgorithm:

Shoulduseonlyuniformrandomintegers(orbits)andintegerarithmetic.Doesnotusefloating-pointarithmeticormakedirectuseofsquarerootortranscendentalfunctions.Doesnotcalculatebase-nexpansionsdirectly.Shouldnotuserationalarithmeticorincreasinglycomplexapproximations,exceptasalastresort.

SeealsoFlajoletetal.(2010)(1).Therearemanywaystodescribetheirrationalprobabilityorfactoryfunction.Iseekreferencestopapersorbooksthatdescribeirrationalconstantsorfactoryfunctionsinanyofthefollowingways:

Forirrationalconstants:Simplecontinuedfractionexpansions.Closedshapesinsidetheunitsquarewhoseareaisanirrationalnumber.(Includesalgorithmsthattellwhetheraboxliesinside,outside,orpartlyinsideoroutsidetheshape.)Example.Generateauniform(x,y)pointinsideaclosedshape,thenreturn1withprobabilityx.Forwhatshapesistheexpectedvalueofxanirrationalnumber?Example.Functionsthatmap[0,1]to[0,1]whoseintegral(areaundercurve)isanirrationalnumber.

ForBernoullifactoryfunctions:Functionswithanyofthefollowingseriesexpansions,usingrationalarithmeticonly:

Powerserieswheref(0)is0andf(1)isrationalorviceversa(see"CertainPowerSeries").Serieswithnon-negativetermsthatcanbe"tucked"underadiscreteprobabilitymassfunction(see"ConvexCombinations").Alternatingpowerserieswhosecoefficientsareallintheinterval[0,1]andformanonincreasingsequence(see"CertainPowerSeries").Serieswithnon-negativetermsandboundsonthetruncationerror(see"CertainConvergingSeries").

AwaytocomputetwosequencesofpolynomialswritteninBernsteinformthatconvergefromaboveandbelowtoafactoryfunctionasfollows:(a)Eachsequence'spolynomialsmusthavecoefficientslyingin[0,1],andbeofincreasingdegree;(b)thedegree-npolynomials'coefficientsmustlieator"inside"thoseofthepreviousupperpolynomialandthepreviouslowerone(oncethepolynomialsareelevatedtodegreen).Foraformal

Page 47: Bernoulli Factory Algorithms - GitHub Pages

statementofthesepolynomials,seemyquestiononMathematicsStackExchange.

Thesupplementalnotesincludeformulasforcomputingthesepolynomialsforlargeclassesoffactoryfunctions,butnoneofthemensureafiniteexpectednumberofcoinflipsingeneral,anditissuspectedthatafinitenumberofflipsisn'tpossibleunlessthefactoryfunctionisC2continuous(hastwoormorecontinuous"slope"functions).Thusonequestionis:GivenaC2continuousfactoryfunction,aretherepracticalalgorithmsforbuildingpolynomialsthatconvergetothatfunctioninamannerneededfortheBernoullifactoryproblem,wheretheexpectednumberofcoinflipsisfinite(besidesthealgorithmsinthisarticleorthesupplementalnotes)?

2. Letapermutationclass(suchasnumbersindescendingorder)andtwocontinuousprobabilitydistributionsDandEbegiven.Considerthefollowingalgorithm:Generateasequenceofindependentrandomvariates(wherethefirstisdistributedasDandtherestasE)untilthesequencenolongerfollowsthepermutationclass,thenreturnn,whichishowmanynumbersweregeneratedthisway,minus1.Inthiscase:

1. Whatistheprobabilitythatnisreturned?2. Whatistheprobabilitythatnisoddorevenorbelongstoacertainclassof

numbers?3. Whatisthedistributionfunction(CDF)ofthefirstgeneratednumbergiventhat

nisodd,orthatniseven?

Obviously,theseanswersdependonthespecificpermutationclassand/ordistributionsDandE.Thus,answersthatworkonlyforparticularclassesand/ordistributionsarewelcome.SeealsomyStackExchangequestionProbabilitiesarisingfrompermutations.

3. Isthereasimplerorfasterwaytoimplementthebase-2ornaturallogarithmofbinomialcoefficients?Seetheexampleinthesection"CertainConvergingSeries".

4. Partofthereverse-timemartingalealgorithmofŁatuszyńskietal.(2009/2011)(24)(see"GeneralFactoryFunctions")tosimulateafactoryfunctionf(λ)isasfollows.Foreachnstartingwith1:

1. Fliptheinputcoin,andcomputethenthupperandlowerboundsoffgiventhenumberofheadssofar,callthemLandU.

2. Computethe(n−1)thupperandlowerboundsoffgiventhenumberofheadssofar,callthemL′andU′.(Theseboundsmustbethesameregardlessoftheoutcomesoffuturecoinflips,andtheinterval[L′,U′]mustequalorentirelycontaintheinterval[L,U].)

Thesepartsofthealgorithmappeartoworkforanytwosequencesoffunctions(notjustpolynomials)thatconvergetof,whereLorL′andUorU′aretheirlowerandupperboundapproximations.Thesectionongeneralfactoryfunctionsshowshowthisalgorithmcanbeimplementedforpolynomials.Buthowdothesestepsworkwhentheapproximatingfunctions(thefunctionsthatconvergetof)arerationalfunctionswhosecoefficientsareintegers?Rationalfunctionswhosecoefficientsarerationalnumbers?Arbitraryapproximatingfunctions?

5. Apushdownautomatonisastatemachinethatholdsastackofsymbols.Mosseland

Page 48: Bernoulli Factory Algorithms - GitHub Pages

Peres(2005)(3)investigatedwhichfunctions(f(λ))canbesimulatedbythesemachineswhenthey'regivenaninfinite"tape"offlipsofacointhatshowsheadswithprobabilityλ.Theyshowedthatpushdownautomatacansimulateonlyalgebraicfunctions,butperhapsnotallofthem.(See"CertainAlgebraicFunctions".)Thequestionis:Whatistheexactclassofalgebraicfunctionsapushdownautomatoncansimulate?Canitsimulatethefunctionsmin(λ,1−λ)andλ1/pwherep>2isaprimenumber?Ihavewrittenanarticleappendixshowingmyprogress,butarethereotherresultsonthisquestion?

6. Afactoryfunctionf(λ)isstronglysimulableifthereisaBernoullifactoryforfusingonlythebiasedcoinandnootherrandomness.KeaneandO'Brien(1994)showedalreadythatf(λ)isstronglysimulableifneither0nor1isinf'sdomain.It'salsoeasytoshowthatiffisstronglysimulable,thenf(0)andf(1)musteachbe0,1,orundefined.Isthisasufficientconditiontobestronglysimulable?Ihavewrittenanarticleappendixshowingmyprogress,butarethereotherresultsonthisquestion?

6CorrectnessandPerformanceChartsChartsshowingthecorrectnessandperformanceofsomeofthesealgorithmsarefoundinaseparatepage.

7AcknowledgmentsIacknowledgeLuisMendo,whorespondedtooneofmyopenquestions,aswellasC.Karney.

8Notes(1)Flajolet,P.,Pelletier,M.,Soria,M.,"OnBuffonmachinesandnumbers",arXiv:0906.5560[math.PR],2010.(2)Keane,M.S.,andO'Brien,G.L.,"ABernoullifactory",ACMTransactionsonModelingandComputerSimulation4(2),1994.(3)ThereisananaloguetotheBernoullifactoryproblemcalledthequantumBernoullifactory,withthesamegoalofsimulatingfunctionsofunknownprobabilities,butthistimewithalgorithmsthatemployquantum-mechanicaloperations(unlikeclassicalalgorithmsthatemploynosuchoperations).However,quantum-mechanicalprogrammingisfarfrombeingaccessibletomostprogrammersatthesamelevelasclassicalprogramming,andwilllikelyremainsofortheforeseeablefuture.Forthisreason,thequantumBernoullifactoryisoutsidethescopeofthisdocument,butitshouldbenotedthatmorefactoryfunctionscanbe"constructed"usingquantum-mechanicaloperationsthanbyclassicalalgorithms.Forexample,afactoryfunctiondefinedin[0,1]hastomeettherequirementsprovedbyKeaneandO'Brienexceptitcantouch0and/or1atafinitenumberofpointsinthedomain(Dale,H.,Jennings,D.andRudolph,T.,2015,"Provablequantumadvantageinrandomnessprocessing",Naturecommunications6(1),pp.1-4).(4)Huber,M.,"NearlyoptimalBernoullifactoriesforlinearfunctions",arXiv:1308.1562v2[math.PR],2014.(5)YannisManolopoulos.2002."Binomialcoefficientcomputation:recursionoriteration?",SIGCSEBull.34,4(December2002),65–67.DOI:https://doi.org/10.1145/820127.820168.(6)Goyal,V.andSigman,K.,2012.OnsimulatingaclassofBernsteinpolynomials.ACMTransactionsonModelingandComputerSimulation(TOMACS),22(2),pp.1-5.(7)WeikangQian,MarcD.Riedel,IvoRosenberg,"UniformapproximationandBernsteinpolynomialswithcoefficientsintheunitinterval",EuropeanJournalofCombinatorics32(3),2011,

Page 49: Bernoulli Factory Algorithms - GitHub Pages

https://doi.org/10.1016/j.ejc.2010.11.004http://www.sciencedirect.com/science/article/pii/S0195669810001666(8)Wästlund,J.,"Functionsarisingbycoinflipping",1999.(9)Thenjisabinomialrandomvariateexpressingthenumberofsuccessesinntrialsthateachsucceedwithprobabilityλ.(10)Qian,W.andRiedel,M.D.,2008,June.Thesynthesisofrobustpolynomialarithmeticwithstochasticlogic.In200845thACM/IEEEDesignAutomationConference(pp.648-653).IEEE.(11)Thomas,A.C.,Blanchet,J.,"APracticalImplementationoftheBernoulliFactory",arXiv:1106.2508v3[stat.AP],2012.(12)S.Ray,P.S.V.Nataraj,"AMatrixMethodforEfficientComputationofBernsteinCoefficients",ReliableComputing17(1),2012.(13)Andthisshowsthatthepolynomialcouldn'tbesimulatedifcwereallowedtobe1,sincetherequireddegreewouldbeinfinity;infact,thepolynomialwouldtouch1atthepoint0.5inthiscase,rulingoutitssimulationbyanyalgorithm(see"AboutBernoulliFactories",earlier).(14)Niazadeh,R.,Leme,R.P.,Schneider,J.,"CombinatorialBernoulliFactories:Matchings,Flows,andPolytopes",inProceedingsofthe53rdAnnualACMSIGACTSymposiumonTheoryofComputing,pp.833-846,June2021;alsoathttps://arxiv.org/abs/2011.03865.pdf.(15)Mossel,Elchanan,andYuvalPeres.Newcoinsfromold:computingwithunknownbias.Combinatorica,25(6),pp.707-724,2005.(16)Nacu,Şerban,andYuvalPeres."Fastsimulationofnewcoinsfromold",TheAnnalsofAppliedProbability15,no.1A(2005):93-115.(17)Morina,G.,Łatuszyński,K.,etal.,"FromtheBernoulliFactorytoaDiceEnterpriseviaPerfectSamplingofMarkovChains",arXiv:1912.09229[math.PR],2019/2020.(18)Propp,J.G.,Wilson,D.B.,"ExactsamplingwithcoupledMarkovchainsandapplicationstostatisticalmechanics",1996.(19)TheprobabilitygiveninTheorem3.2oftheFlajoletpaper,namelyjust"∑k=0,1,2,...(W(k)*(λ/2)k)",appearstobeincorrectinconjunctionwithFigure4ofthatpaper.(20)Flajolet,Ph.,"Analyticmodelsandambiguityofcontext-freelanguages",TheoreticalComputerScience49,pp.283-309,1987(21)Here,"choose(g,g/t)"meansthatoutofgletters,g/tofthemmustbeA's,and"(β−1)g−g/t"isthenumberofwordsthathaveg−g/tlettersotherthanA,giventhattheremainingletterswereA's.(22)Inthisformula,whichissimilartoExample2's,thedivisionbyβg*α−gbringsW(g)fromtheinterval[0,βg*α]((g*α)-letterwords)totheinterval[0,βg](g-letterwords),asrequiredbythemainalgorithm.(23)Mendo,Luis."AnasymptoticallyoptimalBernoullifactoryforcertainfunctionsthatcanbeexpressedaspowerseries."StochasticProcessesandtheirApplications129,no.11(2019):4366-4384.(24)Łatuszyński,K.,Kosmidis,I.,Papaspiliopoulos,O.,Roberts,G.O.,"Simulatingeventsofunknownprobabilitiesviareversetimemartingales",arXiv:0907.4018v2[stat.CO],2009/2011.(25)Flegal,J.M.,Herbei,R.,"ExactsamplingfromintractibleprobabilitydistributionsviaaBernoullifactory",ElectronicJournalofStatistics6,10-37,2012.(26)Holtz,O.,Nazarov,F.,Peres,Y.,"NewCoinsfromOld,Smoothly",ConstructiveApproximation33(2011).(27)Brassard,G.,Devroye,L.,Gravel,C.,"RemoteSamplingwithApplicationstoGeneralEntanglementSimulation",Entropy2019(21)(92),https://doi.org/10.3390/e21010092.(28)Devroye,L.,Non-UniformRandomVariateGeneration,1986.(29)BillGosper,"ContinuedFractionArithmetic",1978.(30)Borwein,J.etal.“ContinuedLogarithmsandAssociatedContinuedFractions.”ExperimentalMathematics26(2017):412-429.(31)Mendo,L.,"Simulatingacoinwithirrationalbiasusingrationalarithmetic",arXiv:2010.14901[math.PR],2020.(32)Theerrorterm,whichfollowsfromTaylor'stheorem,hasanumeratorof2because2ishigherthanthemaximumvalueatthepoint1(incosh(1))thatf'sslope,slope-of-slope,etc.functionscanachieve.(33)Kozen,D.,"OptimalCoinFlipping",2014.(34)K.Bringmann,F.Kuhn,etal.,“InternalDLA:EfficientSimulationofaPhysicalGrowthModel.”In:Proc.41stInternationalColloquiumonAutomata,Languages,andProgramming(ICALP'14),2014.

Page 50: Bernoulli Factory Algorithms - GitHub Pages

(35)ShaddinDughmi,JasonD.Hartline,RobertKleinberg,andRadNiazadeh.2017.BernoulliFactoriesandBlack-BoxReductionsinMechanismDesign.InProceedingsof49thAnnualACMSIGACTSymposiumontheTheoryofComputing,Montreal,Canada,June2017(STOC’17).(36)Huber,M.,"OptimallinearBernoullifactoriesforsmallmeanproblems",arXiv:1507.00843v2[math.PR],2016.(37)Schmon,S.M.,Doucet,A.andDeligiannidis,G.,2019,April.Bernoulliraceparticlefilters.InThe22ndInternationalConferenceonArtificialIntelligenceandStatistics(pp.2350-2358).(38)Agrawal,S.,Vats,D.,Łatuszyński,K.andRoberts,G.O.,2021."OptimalScalingofMCMCBeyondMetropolis",arXiv:2104.02020.(39)Anotheralgorithmforexp(−λ)involvesthevonNeumannschemadescribedintheappendix,butunfortunately,itconvergesslowlyasλapproaches1.(40)Gonçalves,F.B.,Łatuszyński,K.G.,Roberts,G.O.(2017).ExactMonteCarlolikelihood-basedinferenceforjump-diffusionprocesses.(41)Vats,D.,Gonçalves,F.B.,Łatuszyński,K.G.,Roberts,G.O.,"EfficientBernoullifactoryMCMCforintractableposteriors",Biometrika,2021(alsoinarXiv:2004.07471[stat.CO]).(42)Therearetwootheralgorithmsforthisfunction,buttheybothconvergeveryslowlywhenλisverycloseto1.Oneisthegeneralmartingalealgorithm,sincewhenλisin[0,1],thisfunctionisanalternatingseriesoftheform1-x+x^2-x^3+...,whosecoefficientsare1,1,1,1,....Theotheristheso-called"even-parity"constructionfromFlajoletetal.2010:"(1)Fliptheinputcoin.Ifitreturns0,return1.(2)Fliptheinputcoin.Ifitreturns0,return0.Otherwise,gotostep1."(43)Lee,A.,Doucet,A.andŁatuszyński,K.,2014."PerfectsimulationusingatomicregenerationwithapplicationtoSequentialMonteCarlo",arXiv:1407.5770v1[stat.CO].(44)Morina,Giulio(2021)ExtendingtheBernoulliFactorytoadiceenterprise.PhDthesis,UniversityofWarwick.(45)Huber,M.,"Designingperfectsimulationalgorithmsusinglocalcorrectness",arXiv:1907.06748v1[cs.DS],2019.(46)Theeven-parityversionfromFlajoletetal.(2010)couldbewrittenasfollows.Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:(1)Generateauniform(0,1)randomvariateu,ifuwasn'tgeneratedyet;(2)Samplefromthenumberutwice,andfliptheinputcointwice.Ifanyofthesecallsorflipsreturns0,return1;(3)Samplefromthenumberutwice,andfliptheinputcointwice.Ifanyofthesecallsorflipsreturns0,return0.(47)Theeven-parityversionfromFlajoletetal.(2010)couldbewrittenasfollows.Dothefollowingprocessrepeatedly,untilthisalgorithmreturnsavalue:(1)Fliptheinputcoin.Ifitreturns0,flipthecoinagainandreturntheresult;(2)Generateauniform(0,1)randomvariateu,ifuwasn'tgeneratedyet;(3)Samplefromthenumberu.Iftheresultis0,fliptheinputcoinandreturntheresult;(4)Fliptheinputcoin.Ifitreturns0,return0;(5)Samplefromthenumberu.Iftheresultis0,return0.(48)OneoftheonlyimplementationsIcouldfindofthis,ifnottheonly,wasaHaskellimplementation.(49)Anotheralgorithmforthisfunctionusesthegeneralmartingalealgorithm,butusesmorebitsonaverageasλapproaches1.Here,thealternatingseriesis1-x+x^2/2-x^3/3+...,whosecoefficientsare1,1,1/2,1/3,...(50)Penaud,J.G.,Roques,O.,"TirageàpileoufacedemotsdeFibonacci",DiscreteMathematics256,2002.(51)Canonne,C.,Kamath,G.,Steinke,T.,"TheDiscreteGaussianforDifferentialPrivacy",arXiv:2004.00010[cs.DS],2020.(52)Forsythe,G.E.,"VonNeumann'sComparisonMethodforRandomSamplingfromtheNormalandOtherDistributions",MathematicsofComputation26(120),October1972.(53)Citterio,M.,Pavani,R.,"AFastComputationoftheBestk-DigitRationalApproximationtoaRealNumber",MediterraneanJournalofMathematics13(2016).(54)Sondow,Jonathan.“NewVacca-TypeRationalSeriesforEuler'sConstantandIts'Alternating'Analogln4/π.”,2005.(55)vonNeumann,J.,"Varioustechniquesusedinconnectionwithrandomdigits",1951.(56)Pae,S.,"Randomnumbergenerationusingabiasedsource",dissertation,UniversityofIllinoisatUrbana-Champaign,2005.(57)Peres,Y.,"IteratingvonNeumann'sprocedureforextractingrandombits",AnnalsofStatistics

Page 51: Bernoulli Factory Algorithms - GitHub Pages

1992,20,1,p.590-597.(58)Flajolet,P.,Sedgewick,R.,AnalyticCombinatorics,CambridgeUniversityPress,2009.(59)Monahan,J.."ExtensionsofvonNeumann’smethodforgeneratingrandomvariables."MathematicsofComputation33(1979):1065-1069.(60)Tsai,Yi-Feng,Farouki,R.T.,"Algorithm812:BPOLY:AnObject-OrientedLibraryofNumericalAlgorithmsforPolynomialsinBernsteinForm",ACMTrans.Math.Softw.27(2),2001.

9Appendix

9.1Randomizedvs.Non-RandomizedAlgorithmsAnon-randomizedalgorithmisasimulationalgorithmthatusesnothingbuttheinputcoinasasourceofrandomness(incontrasttorandomizedalgorithms,whichdouseothersourcesofrandomness)(Mendo2019)(23).Insteadofgeneratingoutsiderandomness,arandomizedalgorithmcanimplementarandomnessextractionproceduretogeneratethatrandomnessusingtheinputcoinsthemselves.Inthisway,thealgorithmbecomesanon-randomizedalgorithm.Forexample,ifanalgorithmimplementsthetwo-coinalgorithmbygeneratingarandombitinstep1,itcouldreplacegeneratingthatbitwithflippingtheinputcointwiceuntiltheflipreturns0then1or1then0thisway,thentakingtheresultas0or1,respectively(vonNeumann1951)(55).

Infact,thereisalowerboundontheaveragenumberofcoinflipsneededtoturnacoinwithoneprobabilityofheads(λ)intoacoinwithanother(τ=f(λ)).It'scalledtheentropybound(see,e.g.,(Pae2005)(56),(Peres1992)(57))andiscalculatedas—

((τ−1)*ln(1−τ)−τ*ln(τ))/((λ−1)*ln(1−λ)−λ*ln(λ)).

Forexample,iff(λ)isaconstant,non-randomizedalgorithmswillgenerallyrequireagrowingnumberofcoinflipstosimulatethatconstantiftheinputcoinstronglyleanstowardsheadsortails.Butthisformulaonlyworksifnothingbutcoinflipsisallowedasrandomness.

Forcertainvaluesofλ,Kozen(2014)(33)showedatighterlowerboundofthiskind,butthisboundisgenerallynon-trivialandassumesλisknown.However,ifλis1/2(theinputcoinisunbiased),thisboundissimple:atleast2flipsoftheinputcoinareneededonaveragetosimulateaknownconstantτ,exceptwhenτisamultipleof1/(2n)foranyintegern.

Afunctionf(λ)isstronglysimulable(KeaneandO'Brien1994)(23)ifitadmitsanon-randomizedBernoullifactoryalgorithm.Afactoryfunctionisstronglysimulable,forexample,iftheinputcoin'sprobabilityofheads(λ)canbeneither0nor1.

9.2BernoulliFactoriesandUnbiasedEstimationIfanalgorithm—

takesflipsofacoinwithanunknownprobabilityofheads(λ),andproducesheadswithaprobabilitythatdependsonλ(f(λ))andtailsotherwise,

thealgorithmactsasanunbiasedestimatoroff(λ)thatproducesestimatesin[0,1]with

Page 52: Bernoulli Factory Algorithms - GitHub Pages

probability1(Łatuszyńskietal.2009/2011)(24).(Andanestimatorlikethisispossibleonlyiffisafactoryfunction;seeŁatuszyński.)Becausethealgorithmisunbiased,itsexpectedvalue(ormean)isf(λ).Here'soneresultofunbiasedness:Takeasampleofnindependentoutputsofthealgorithm,sumthem,thendividebyn.Thenwithprobability1,thisaverageapproachesf(λ)asngetslarge.Thisisthelawoflargenumbersinaction.

Ontheotherhand—

estimatingλasλ′(e.g.,byaveragingmultipleflipsofaλ-coin),thencalculatingf(λ′),

isnotnecessarilyanunbiasedestimatoroff(λ),evenifλ′isanunbiasedestimator.

Thispagefocusesonunbiasedestimatorsbecause"exactsampling"dependsonit.Seealso(MosselandPeres2005,section4)(15).

Note:Biasandvariancearethetwosourcesoferrorinarandomizedestimationalgorithm.Anunbiasedestimatorhasnobias,butisnotwithouterror.Inthecaseathandhere,thevarianceofaBernoullifactoryforf(λ)equalsf(λ)*(1−f(λ))andcangoashighas1/4.Therearewaystoreducethisvariance,whichareoutsidethescopeofthisdocument.Anestimationalgorithm'smeansquarederrorequalsvarianceplussquareofbias.

9.3CorrectnessProoffortheContinuedLogarithmSimulationAlgorithmTheorem.Ifthealgorithmgivenin"ContinuedLogarithms"terminateswithprobability1,itreturns1withprobabilityexactlyequaltothenumberrepresentedbythecontinuedlogarithmc,and0otherwise.

Proof.ThisproofofcorrectnesstakesadvantageofHuber's"fundamentaltheoremofperfectsimulation"(Huber2019)(45).UsingHuber'stheoremrequiresprovingtwothings:

Thealgorithmfinisheswithprobability1byassumption.Second,weshowthealgorithmislocallycorrectwhentherecursivecallintheloopisreplacedwitha"blackbox"thatsimulatesthecorrect"continuedsub-logarithm".Ifstep1reachesthelastcoefficient,thealgorithmobviouslypasseswiththecorrectprobability.Otherwise,wewillbesimulatingtheprobability(1/2c[i])/(1+x),wherexisthe"continuedsub-logarithm"andwillbeatmost1byconstruction.Step2definesaloopthatdividestheprobabilityspaceintothreepieces:thefirstpiecetakesuponehalf,thesecondpiece(inthesecondsubstep)takesupaportionoftheotherhalf(whichhereisequaltox/2),andthelastpieceisthe"rejectionpiece"thatrerunstheloop.Sincethisloopchangesnovariablesthataffectlateriterations,eachiterationactslikeanacceptance/rejectionalgorithmalreadyprovedtobeaperfectsimulatorbyHuber.Thealgorithmwillpassatthefirstsubstepwithprobabilityp=(1/2c[i])/2andfaileitheratthefirstsubstepoftheloopwithprobabilityf1=(1−1/2c[i])/2,oratthesecondsubstepwithprobabilityf2=x/2(alltheseprobabilitiesarerelativetothewholeiteration).Finally,dividingthepassesbythesumofpassesandfails(p/(p+f1+f2))leadsto(1/2c[i])/(1+x),whichistheprobabilitywewanted.

SincebothconditionsofHuber'stheoremaresatisfied,thiscompletestheproof.□

Page 53: Bernoulli Factory Algorithms - GitHub Pages

9.4CorrectnessProofforContinuedFractionSimulationAlgorithm3Theorem.Supposeageneralizedcontinuedfraction'spartialnumeratorsareb[i]andallgreaterthan0,anditspartialdenominatorsarea[i]andall1orgreater,andsupposefurtherthateachb[i]/a[i]is1orless.ThenthealgorithmgivenasAlgorithm3in"ContinuedFractions"returns1withprobabilityexactlyequaltothenumberrepresentedbythatcontinuedfraction,and0otherwise.

Proof.WeuseHuber's"fundamentaltheoremofperfectsimulation"againintheproofofcorrectness.

Thealgorithmfinisheswithprobability1becausewitheachrecursion,themethodisnevermorelikelytodoarecursiverunthannottodoso;observethata[i]canneverbemorethan1,sothata[i]/(1+a[i]),thatis,theprobabilityoffinishingtherunineachiteration,isalways1/2orgreater.Iftherecursivecallintheloopisreplacedwitha"blackbox"thatsimulatesthecorrect"sub-fraction",thealgorithmislocallycorrect.Ifstep1reachesthelastelementofthecontinuedfraction,thealgorithmobviouslypasseswiththecorrectprobability.Otherwise,wewillbesimulatingtheprobabilityb[i]/(a[i]+x),wherexisthe"continuedsub-fraction"andwillbeatmost1byassumption.Step2definesaloopthatdividestheprobabilityspaceintothreepieces:thefirstpiecetakesupapartequaltoh=a[i]/(a[i]+1),thesecondpiece(inthesecondsubstep)takesupaportionoftheremainder(whichhereisequaltox*(1−h)),andthelastpieceisthe"rejectionpiece".Thealgorithmwillpassatthefirstsubstepwithprobabilityp=(b[i]/a[pos])*handfaileitheratthefirstsubstepoftheloopwithprobabilityf1=(1−b[i]/a[pos])*h,oratthesecondsubstepwithprobabilityf2=x*(1−h)(alltheseprobabilitiesarerelativetothewholeiteration).Finally,dividingthepassesbythesumofpassesandfailsleadstob[i]/(a[i]+x),whichistheprobabilitywewanted,sothatbothofHuber'sconditionsaresatisfiedandwearedone.□

9.5ThevonNeumannSchema(Flajoletetal.,2010)(1)describeswhatitcallsthevonNeumannschema(sec.2).AlthoughthevonNeumannschemaisusedinseveralBernoullifactoriesgivenhere,it'snotaBernoullifactoryitselfsinceitcouldproducerandomvariatesotherthan0and1,whichiswhythissectionappearsintheappendix.Givenapermutationclassandaninputcoin,thevonNeumannschemageneratesarandomintegern,0orgreater,withprobabilityequalto—

(λn*V(n)/n!)/EGF(λ),

where—

EGF(λ)=∑k=0,1,...(λk*V(k)/k!)(theexponentialgeneratingfunctionorEGF,whichcompletelydeterminesapermutationclass),andV(n)isanumberintheinterval[0,n!]andisthenumberofpermutationsofsizenthatmeettherequirementsofthepermutationclassinquestion.

Effectively,arandomvariateGisgeneratedbyflippingthecoinuntilitreturns0andcountingthenumberofones(thepapercallsGageometric(λ)randomvariate,butthisterminologyisavoidedinthisarticlebecauseithasseveralconflictingmeaningsinacademicworks),andthenacceptedwithprobabilityV(G)/(G!)andrejectedotherwise.Theprobabilitythatrvariatesarerejectedthiswayisp*(1−p)r,wherep=(1−λ)*

Page 54: Bernoulli Factory Algorithms - GitHub Pages

EGF(λ).

Examplesofpermutationclassesinclude—

single-cyclepermutations(EGF(λ)=Cyc(λ)=ln(1/(1−λ));V(n)=(n−1)!)sortedpermutations,orpermutationswhosenumbersaresortedindescendingorder(EGF(λ)=Set(λ)=exp(λ);V(n)=1),allpermutations(EGF(λ)=Seq(λ)=1/(1−λ);V(n)=n!),alternatingpermutationsofevensize(EGF(λ)=1/cos(λ);theV(n)startingatn=0isA000364intheOn-LineEncyclopediaofIntegerSequences),andalternatingpermutationsofoddsize(EGF(λ)=tan(λ);theV(n)startingatn=0isA000182),

usingthenotationin"AnalyticCombinatorics"(FlajoletandSedgewick2009)(58).

ThefollowingalgorithmgeneratesarandomvariatethatfollowsthevonNeumannschema.

1. Setrto0.(Thisisthenumberoftimesthealgorithmrejectsarandomvariate.)2. Fliptheinputcoinuntiltheflipreturns0.ThensetGtothenumberoftimestheflip

returns1thisway.3. WithprobabilityV(G)/G!,returnG(orrifdesired).(Inpractice,theprobabilitycheck

isdonebygeneratingGuniform(0,1)randomvariatesanddeterminingwhetherthosenumberssatisfythegivenpermutationclass,orgeneratingasmanyofthosenumbersasnecessarytomakethisdetermination.ThisisespeciallybecauseG!,thefactorialofG,caneasilybecomeverylarge.)

4. Add1torandgotostep2.

AvarietyofBernoullifactoryprobabilityfunctionscanarisefromthevonNeumannschema,dependingontheEGFandwhichvaluesofGand/orrtheBernoullifactoryalgorithmtreatsasheadsortails.ThefollowingPythonfunctionsusetheSymPycomputeralgebralibrarytofindprobabilitiesandotherusefulinformationforapplyingthevonNeumannschema,givenapermutationclass'sEGF.

defcoeffext(f,x,power):#Extractacoefficientfromageneratingfunction#NOTE:Canalsobedonewithjustthefollowingline:#returndiff(f,(x,power)).subs(x,0)/factorial(power)px=2foriinrange(10):try:poly=Poly(series(f,x=x,n=power+px).removeO())returnpoly.as_expr().coeff(x,power)except:px+=2#Failed,assume0return0

defnumber_n_prob(f,x,n):#Probabilitythatthenumbernisgenerated#forthevonNeumannschemawiththegiven#exponentialgeneratingfunction(e.g.f.)#Example:number_n_prob(exp(x),x,1)-->x**exp(-x)return(x**n*coeffext(f,x,n))/f

defr_rejects_prob(f,x,r):#ProbabilitythatthevonNeumannschema#withthegivene.g.f.willrejectrrandomvariates#beforeacceptingthenextone

Page 55: Bernoulli Factory Algorithms - GitHub Pages

p=(1-x)*freturnp*(1-p)**r

defvalid_perm(f,x,n):#Numberofpermutationsofsizenthatmeet#therequirementsofthepermutationclass#determinedbythegivene.g.f.forthe#vonNeumannschemareturncoeffext(f,x,n)*factorial(n)

Note:ThevonNeumannschemacansimulateanypowerseriesdistribution(suchasPoisson,negativebinomial,geometric,andlogarithmicseries),givenasuitableexponentialgeneratingfunction.However,becauseofstep2,thenumberofinputcoinflipsrequiredbytheschemagrowswithoutboundasλapproaches1.

Example:Usingtheclassofsortedpermutations,wecangenerateaPoisson(λ)randomvariateviathevonNeumannschema,whereλistheprobabilityofheadsoftheinputcoin.Thiswouldleadtoanalgorithmforexp(−λ)—return1ifaPoisson(λ)randomvariateis0,or0otherwise—butforthereasongiveninthenote,thisalgorithmconvergesslowlyasλapproaches1.Also,ifc>0isarealnumber,addingaPoisson(floor(c))randomvariatetoaPoisson(c−floor(c))variategeneratesaPoisson(c)randomvariate.

AvariationonthevonNeumannschemaoccursifGisgenerateddifferentlythangiveninstep2,butisstillgeneratedbyflippingtheinputcoin.Inthatcase,thealgorithmabovewillreturnnwithprobability—

(κ(n;λ)*V(n)/(n!))/p,

wherep=(∑k=0,1,...(κ(k;λ)*V(k)/(k!))),andwhereκ(n;λ)istheprobabilitythatGisn,withparameterλortheinputcoin'sprobabilityofheads.Also,theprobabilitythatrrandomvariatesarerejectedbythemodifiedalgorithmisp*(1−p)r.

Example:IfGisaPoisson(z2/4)randomvariateandthesortedpermutationclassisused,thealgorithmwillreturn0withprobability1/I0(z),whereI0(.)isthemodifiedBesselfunctionofthefirstkind.

9.6ProbabilitiesArisingfromCertainPermutationsCertaininterestingprobabilityfunctionscanarisefrompermutations,suchaspermutationsthataresortedorpermutationswhosehighestnumberappearsfirst.InspiredbythevonNeumannschemagivenearlierinthisappendix,wecandescribethefollowingalgorithm:

Letapermutationclass(suchasnumbersindescendingorder)andtwocontinuousprobabilitydistributionsDandEbegiven.Considerthefollowingalgorithm:Generateasequenceofindependentrandomvariates(wherethefirstisdistributedasDandtherestasE)untilthesequencenolongerfollowsthepermutationclass,thenreturnn,whichishowmanynumbersweregeneratedthisway,minus1.

Thenthealgorithm'sbehaviorisgiveninthetablesbelow.

|PermutationClass|DistributionsDandE|Thealgorithmreturnsnwiththisprobability:|Theprobabilitythatnis...|---|---|---|---|---||Numberssortedindescendingorder|Bothuniform(0,1)|n/((n+1)!).|Oddis1−exp(−1).

Page 56: Bernoulli Factory Algorithms - GitHub Pages

Evenisexp(−1).||Numberssortedindescendingorder|Eacharbitrary|(∫(−∞,∞)DPDF(z)*((ECDF(z))n−1/((n−1)!)−(ECDF(z))n/(n!))dz),foreveryn>0(seealsoproofofTheorem2.1of(Devroye1986,ChapterIV)(28).DPDFandECDFaredefinedlater.|Oddisdenominatorofformula1below.||Alternatingnumbers|Bothuniform(0,1)|(an*(n+1)−an+1)/(n+1)!,whereaiistheintegeratpositioni(startingat0)ofthesequenceA000111intheOn-LineEncyclopediaofIntegerSequences.|Oddis1−cos(1)/(sin(1)+1);eveniscos(1)/(sin(1)+1).||Any|Bothuniform(0,1)|(∫[0,1]1*(zn−1*V(n)/((n−1)!)−zn*V(n+1)/(n!))dz),foreveryn>0.V(n)isthenumberofpermutationsofsizenthatmeetthepermutationclass'srequirements.Forthisalgorithm,V(n)mustbeintheinterval(0,n!];thisalgorithmwon'twork,forexample,ifthereare0permutationsofoddsize.|Oddis1−1/EGF(1);evenis1/EGF(1).Lessthankis(V(0)−V(k)/(k!))/V(0).|

|PermutationClass|DistributionsDandE|Theprobabilitythatthefirstnumberinthesequenceislessthanxgiventhatnis...|---|---|---|---||Numberssortedindescendingorder|Eacharbitrary|Oddisψ(x)=(∫(−∞,x)exp(−ECDF(z))*DPDF(z)dz)/(∫(−∞,∞)exp(−ECDF(z))*DPDF(z)dz)(Formula1;seeTheorem2.1(iii)of(Devroye1986,ChapterIV)(28);seealsoForsythe1972(52)).Here,DPDFistheprobabilitydensityfunction(PDF)ofD,andECDFisthecumulativedistributionfunction(CDF)ofE.Ifxisuniform(0,1),thisprobabilitybecomes∫[0,1]ψ(z)dz.||Numberssortedindescendingorder|Eacharbitrary|Evenis(∫(−∞,x)(1−exp(−ECDF(z)))*DPDF(z)dz)/(∫(−∞,∞)(1−exp(−ECDF(z)))*DPDF(z)dz)(Formula2;seealsoMonahan1979(59)).DPDFandECDFareasabove.||Numberssortedindescendingorder|Bothuniform(0,1)|Oddis((1−exp(−x))−exp(1))/(1−exp(1)).Therefore,thefirstnumberinthesequenceisdistributedasexponential(1)and"truncated"totheinterval[0,1](vonNeumann1951)(55).||Numberssortedindescendingorder|Disuniform(0,1);Eismax.oftwouniform(0,1)|Oddiserf(x)/erf(1)(usesFormula1,whereDPDF(z)=1andECDF(z)=z2forzin[0,1];seealsoerf(x)/erf(1)).|

Notes:

1. Allthefunctionspossibleforformulas1and2arenondecreasingfunctions.BothformulasexpressthecumulativedistributionfunctionFD(xgiventhatnisodd)orFD(xgiventhatniseven),respectively.

2. EGF(z)istheexponentialgeneratingfunction(EGF)forthekindofpermutationinvolvedinthealgorithm.Forexample,theclassofalternatingpermutations(permutationswhosenumbersalternatebetweenlowandhigh,thatis,X1>X2<X3>...)usestheEGFtan(λ)+1/cos(λ).OtherexamplesofEGFsweregiveninthesectiononthevonNeumannschema.

OpenQuestion:HowcanthetablesabovebefilledforotherpermutationclassesanddifferentcombinationsofdistributionsDandE?

9.7SketchofDerivationoftheAlgorithmfor1/πTheFlajoletpaperpresentedanalgorithmtosimulate1/πbutprovidednoderivation.Hereisasketchofhowthisalgorithmworks.

Thealgorithmisanapplicationoftheconvexcombinationtechnique.Namely,1/πcanbeseenasaconvexcombinationoftwocomponents:

Page 57: Bernoulli Factory Algorithms - GitHub Pages

g(n):26*n*(6*n+1)/28*n+2=2−2*n*(6*n+1)/4=(6*n+1)/(22*n+2),whichistheprobabilitythatthesumofthefollowingindependentrandomvariatesequalsn:

Tworandomvariatesthateachexpressthenumberoffailuresbeforethefirstsuccess,wherethechanceofasuccessis1−1/4(thepapercallsthesetwonumbersgeometric(1/4)randomvariates,butthisterminologyisavoidedinthisarticlebecauseithasseveralconflictingmeaningsinacademicworks).OneBernoullirandomvariatewithmean5/9.

Thiscorrespondstostep1oftheconvexcombinationalgorithmandsteps2through4ofthe1/πalgorithm.(Thisalsoshowsthatthereisanerrorintheidentityfor1/πgivenintheFlajoletpaper:the"8n+4"shouldread"8n+2".)

hn():(choose(n*2,n)/2n*2)3,whichistheprobabilityofheadsofthe"coin"numberedn.Thiscorrespondstostep2oftheconvexcombinationalgorithmandstep5ofthe1/πalgorithm.

Notes:

1. 9*(n+1)/(22*n+4)istheprobabilitythatthesumoftwoindependentrandomvariatesequalsn,whereeachofthetwovariatesexpressesthenumberoffailuresbeforethefirstsuccessandthechanceofasuccessis1−1/4.

2. pm*(1−p)n*choose(n+m−1,m−1)istheprobabilitythatthesumofmindependentrandomvariatesequalsn(anegativebinomialdistribution),whereeachofthemvariatesexpressesthenumberoffailuresbeforethefirstsuccessandthechanceofasuccessisp.

3. p*f(z−1)+(1−p)*f(z)istheprobabilitythatthesumoftwoindependentrandomvariates—aBernoullivariatewithmeanpaswellasanintegerthatequalsxwithprobabilityf(x)—equalsz.

9.8CalculatingBoundsforexp(1)ThefollowingimplementsthepartsofCitterioandPavani'salgorithm(2016)(53)neededtocalculatelowerandupperboundsforexp(1)intheformofrationalnumbers.

Definethefollowingoperations:

Setup:Setptothelist[0,1],setqtothelist[1,0],setatothelist[0,0,2](twozeros,followedbytheintegerpartforexp(1)),setvto0,andsetavto0.Ensuren:Whilevislessthanorequalton:1. (Ensurepartialdenominatorv,startingfrom0,isavailable.)Ifv+2isgreater

thanorequaltothesizeofa,append1,av,and1,inthatorder,tothelista,thenadd2toav.

2. (Calculateconvergentv,startingfrom0.)Appenda[n+2]*p[n+1]+p[n]tothelistp,andappenda[n+2]*q[n+1]+q[n]tothelistq.(Positionsinlistsstartat0.Forexample,p[0]meansthefirstiteminp;p[1]meansthesecond;andsoon.)

3. Add1tov.Getthenumeratorforconvergentn:Ensuren,thenreturnp[n+2].Getconvergentn:Ensuren,thenreturnp[n+2]/q[n+2].Getsemiconvergentngivend:1. Ensuren,thensetmtofloor(((10d)−1−p[n+1])/p[n+2]).

Page 58: Bernoulli Factory Algorithms - GitHub Pages

2. Return(p[n+2]*m+p[n+1])/(q[n+2]*m+q[n+1]).

Thenthealgorithmtocalculatelowerandupperboundsforexp(1),givend,isasfollows:

1. Setito0,thenrunthesetup.2. Getthenumeratorforconvergenti,callitc.Ifcislessthan10d,add1toiand

repeatthisstep.Otherwise,gotothenextstep.3. Getconvergenti−1andgetsemiconvergenti−1givend,callthemconvand

semi,respectively.4. If(i−1)isodd,returnsemiasthelowerboundandconvastheupperbound.

Otherwise,returnconvasthelowerboundandsemiastheupperbound.

9.9PreparingRationalFunctionsThissectiondescribeshowtoturnasingle-variablerationalfunction(ratioofpolynomials)intoanarrayofpolynomialsneededtoapplythe"DiceEnterprise"specialcasedescribedin"CertainRationalFunctions".Inshort,thestepstodosocanbedescribedasseparating,homogenizing,andaugmenting.

Separating.Ifarationalfunction'snumerator(D)anddenominator(E)arewritten—

asasumoftermsoftheformz*λi*(1−λ)j,wherezisarealnumberandi≥0andj≥0areintegers(calledform1inthissection),

thenthefunctioncanbeseparatedintotwopolynomialsthatsumtothedenominator.(Here,i+jistheterm'sdegree,andthepolynomial'sdegreeisthehighestdegreeamongitsterms.)Todothisseparation,subtractthenumeratorfromthedenominatortogetanewpolynomial(G)suchthatG=E−D(orD+G=E).(ThenDandGarethetwopolynomialswewilluse.)Similarly,ifwehavemultiplerationalfunctionswithacommondenominator,namely(D1/E),...,(DN/E),whereD1,...,DNandEarewritteninform1,thentheycanbeseparatedintoN+1polynomialsbysubtractingthenumeratorsfromthedenominator,sothatG=E−D1−...−DN.(ThenD1,...,DNandGarethepolynomialswewilluse.)Tousethepolynomialsinthealgorithm,however,theyneedtobehomogenized,thenaugmented,asdescribednext.

Example:Wehavetherationalfunction(4*λ1*(1−λ)2)/(7−5*λ1*(1−λ)2).Subtractingthenumeratorfromthedenominatorleadsto:7−1*λ1*(1−λ)2.

Homogenizing.Thenextstepistohomogenizethepolynomialssotheyhavethesamedegreeandaparticularform.Forthisstep,choosentobeanintegernolessthanthehighestdegreeamongthepolynomials.

Supposeapolynomial—

is0orgreaterforeveryλintheinterval[0,1],hasdegreenorless,andiswritteninform1asgivenabove.

Thenthepolynomialcanbeturnedintoahomogeneouspolynomialofdegreen(allitstermshavedegreen)asfollows.

Foreachintegermin[0,n],thenewhomogeneouspolynomial'scoefficientatmisfoundasfollows:1. Setrto0.2. Foreachterm(intheoldpolynomial)oftheformz*λi*(1−λ)j:

Page 59: Bernoulli Factory Algorithms - GitHub Pages

Ifi≤m,and(n−m)≥j,andi+j≤n,addz*choose(n−(i+j),(n−m)−j)tor.3. Now,risthenewcoefficient(correspondingtothetermr*λm*(1−λ)n−m).

Ifthepolynomialiswritteninso-called"powerform"asc[0]+c[1]*λ+c[2]*λ2+...+c[n]*λn,thenthemethodisinsteadasfollows:

Foreachintegermin[0,n],thenewhomogeneouspolynomial'scoefficientatmisfoundasfollows:1. Setrto0.2. Foreachintegeriin[0,m],ifthereisacoefficientc[i],addc[i]*choose(n−i,

n−m)tor.3. Now,risthenewcoefficient(correspondingtothetermr*λm*(1−λ)n−m).

Example:Wehavethefollowingpolynomial:3*λ2+10*λ1*(1−λ)2.Thisisadegree-3polynomial,andweseektoturnitintoadegree-5homogeneouspolynomial.Theresultbecomesthesumoftheterms—

0*λ0*(1−λ)5;10*choose(2,2)*λ1*(1−λ)4=10*λ1*(1−λ)4;(3*choose(3,3)+10*choose(2,1))*λ2*(1−λ)3=23*λ2*(1−λ)3;(3*choose(3,2)+10*choose(2,0))*λ3*(1−λ)2=19*λ3*(1−λ)2;3*choose(3,1)*λ4*(1−λ)1=9*λ4*(1−λ)1;and3*choose(3,0)*λ5*(1−λ)0=3*λ5*(1−λ)0,

resultinginthecoefficients(0,10,23,19,9,3)forthenewhomogeneouspolynomial.

Augmenting.Ifwehaveanarrayofhomogeneoussingle-variablepolynomialsofthesamedegree,theyarereadyforuseintheDiceEnterprisespecialcaseif—

thepolynomialshavethesamedegree,namelyn,theircoefficientsareall0orgreater,andthesumofjthcoefficientsisgreaterthan0,foreachjstartingat0andendingatn,exceptthatthelistofsumsmaybeginand/orendwithzeros.

Ifthoseconditionsarenotmet,theneachpolynomialcanbeaugmentedasoftenasnecessarytomeettheconditions(Morinaetal.,2019)(17).Forpolynomialsofthekindrelevanthere,augmentingapolynomialamountstodegreeelevationsimilartothatofpolynomialsinBernsteinform(seealsoTsaiandFarouki2001(60)).Itisimplementedasfollows:

Letnbethepolynomial'solddegree.Foreachkin[0,n+1],thenewpolynomial'scoefficientatkisfoundasfollows:

Letc[j]betheoldpolynomial'sjthcoefficient(startingat0).Calculatec[j]*choose(1,k−j)foreachjintheinterval[max(0,k−1),min(n,k)],thenaddthemtogether.Thesumisthenewcoefficient.

AccordingtotheMorinapaper,it'senoughtodonaugmentationsoneachpolynomialforthewholearraytomeettheconditionsabove(althoughfewerthannwilloftensuffice).

Note:Forbestresults,theinputpolynomials'coefficientsshouldberationalnumbers.Iftheyarenot,thenspecialmethodsareneededtoensureexactresults,suchasintervalarithmeticthatcalculateslowerandupperbounds.

Page 60: Bernoulli Factory Algorithms - GitHub Pages

10LicenseAnycopyrighttothispageisreleasedtothePublicDomain.Incasethisisnotpossible,thispageisalsolicensedunderCreativeCommonsZero.