-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review of random variate generation articles for a programmer audience #18
Comments
Still seeking reviews on my articles on random variate generation. Again letting them know about my articles in this issue on random variate generation: @lmendo, @PaulSanchez, @maciej-bendkowski, @lcrocker, @dj-on-github . |
Transitioning from this conversation. |
Sampling the probability The following is a proof of the algorithm First, suppose |
Ah, it's basically the same as Cannone et al, but with a Poisson sampler instead. I was hesitant to choose those parameters because I was concerned about finding the PSRN of Duchon poisson1 testdef poisson1():
"""Samples from Poisson(λ = 1)
https://www.combinatorics.org/ojs/plugins/generic/pdfJsViewer/pdf.js/web/viewer.html?file=https%3A%2F%2Fwww.combinatorics.org%2Fojs%2Findex.php%2Feljc%2Farticle%2Fdownload%2Fv23i4p22%2Fpdf%2F#subsection.5.3
"""
import random
n = 1
g = 0
k = 1
while True:
i = random.randint(0, n + 1)
if i == n + 1:
k += 1
elif i > g:
k -= 1
g = n + 1
else:
return k
n += 1
def test_poisson1():
from collections import Counter
trials = 5000
counts = Counter(poisson1() for _ in range(trials))
from math import exp, factorial
print("i: actual ideal")
for i in range(5):
print(f"{i}: {(counts.get(i) or 0) / trials:.4f} {exp(-1) / factorial(i):.4f}")
test_poisson1()
exact exp(-exp(-x)) samplerimport random
from math import floor
def bernoulli(p):
"""Sample from Bernoulli(`p`) when `p` in [0, 1]"""
assert 0 <= p <= 1
return random.randrange(p.denominator) < p.numerator
def poisson1():
"""Samples from Poisson(λ = 1)
Algorithm from: https://www.combinatorics.org/ojs/plugins/generic/pdfJsViewer/pdf.js/web/viewer.html?file=https%3A%2F%2Fwww.combinatorics.org%2Fojs%2Findex.php%2Feljc%2Farticle%2Fdownload%2Fv23i4p22%2Fpdf%2F#subsection.5.3
Implementation from: https://stats.stackexchange.com/a/551576
"""
n = 1
g = 0
k = 1
while True:
j = random.randint(0, n)
if j < n and j < g:
return k
if j == n:
k += 1
else:
k -= 1
g = 1 + n
n += 1
def bernoulli_exp_neg_01(f):
"""Sample from Bernoulli(p = exp(-E[f()])) for f() in [0, 1]"""
return not any(f() for _ in range(poisson1()))
def bernoulli_exp_neg(x):
"""Sample from Bernoulli(p = exp(-(m + λ)))
https://peteroupc.github.io/bernoulli.html#exp_minus__m____lambda_____mu
"""
m = floor(x)
if any(poisson1() for _ in range(m)):
return False
return bernoulli_exp_neg_01(lambda: bernoulli(x - m))
def bernoulli_exp_neg_exp_neg(x):
"""Sample from Bernoulli(p = exp(-exp(-x)))"""
return bernoulli_exp_neg_01(lambda: bernoulli_exp_neg(x)) At this point I just need to figure out a Bernoulli factory for the fractional part of the exponential PSRN. Thanks again for all your help! |
Your implementation of Duchon and Duvignau's algorithm (as given in their paper) is implemented incorrectly. |
@Shoeboxam : Do you have further comments? By the way you are encouraged to implement yourself any of the algorithms in "Bernoulli Factory Algorithms" and report on your implementation experience; that's pretty much one of the requests in the opening post. The same is true for the other articles in the opening post. Part of my goal of this issue is to correct any errors or unclear points in those articles. |
@peteroupc Thanks for checking in. I used inverse transform sampling to address the original reason why I was trying to sample Gumbel variates: You've amassed an incredible collection of very cool algorithms and it's taking some time to take it in. The PSRN Tulap sampler, for example, is neat. Feedback It is also easy to get lost in the site. I found it surprising there are more Bernoulli factories and PSRNs in "More Algorithms", when you already have pages for them. In my experience so far, the best way to not get lost on the site is to just read the entire site! ;) Nits:
|
I appreciate your comments. In response I have addressed your "nits" and rearranged the content. In particular, the "More Arbitrary-Precision Samplers" page is now obsolete and its content was moved to other pages in the site. |
@Shoeboxam : For your information, issue #17 lists open questions I have on the articles in this repository, including the pages listed in this issue. One question I've just added that may interest you is: Find additional algorithms to sample continuous distributions with PSRNs that meet the properties desired for such algorithms. Other open questions relate to Bernoulli factories. |
@Shoeboxam : For your information, the following paper on a security consideration when sampling for information security (and differential privacy) purposes came to my attention. I have updated the security considerations on "Randomization and Sampling Methods" to take it into account. Ben Dov, Y., David, L., et al., "Resistance to Timing Attacks for Sampling and Privacy Preserving Schemes¨, FORC 2023. |
The following pages of mine deal with random variate generation, Bernoulli factories, and exact sampling. They contain numerous algorithms for such sampling.
My audience for these articles is computer programmers with mathematics knowledge, but little or no familiarity with calculus.
I post this issue to seek comments on the following aspects:
Comments on other aspects of these articles are also welcome.
The articles follow:
Arbitrary-Precision Samplers for the Sum or Ratio of Uniform Random VariatesMore Algorithms for Arbitrary-Precision SamplingLetting them know: @lmendo, @PaulSanchez, @maciej-bendkowski
The text was updated successfully, but these errors were encountered: