0

Bernoulli is a probability distribution. I need to sample from an exponential bernoulli and returns a binary value (i.e. either 0 or 1). I found this algorithm exponential bernoulli sampling and i want to implement it but i do not understand the step 3 of the algorithm where : r1 = r1 & (2^h - 1 ). Could someone give help ?

anbas22
  • 1
  • 1
  • If it is a question about the algorithm remove the Python tag. Ortherwise please include full code to be checked, sample input and output/error message. Is it working or not ? If not Maybe & should be replaced by "and" – Malo Jan 24 '22 at 10:59
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 24 '22 at 11:00

1 Answers1

-1

You can use a library which implements sampling from a Bernoulli distribution, e.g., np.random.binomial (as the binomial distribution with n = 1 is the Bernoulli distribution).

import numpy as np
np.random.binomial(n=1, p=.2, size=20)
# output: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0])
cheersmate
  • 2,385
  • 4
  • 19
  • 32
  • Downvoted - it might be a way to sample, but it has nothing do to with the question which is regarding `r1 = r1 & (2^h-1)` – CutePoison Jan 14 '22 at 11:49
  • The question says "I need to sample from an exponential bernoulli and ...", so "don't reinvent the wheel" is an answer. – cheersmate Jan 14 '22 at 12:52
  • No, it's not an answer to the question. The question is specific stated as the `r1` part, and not "how to sample in Python". – CutePoison Jan 14 '22 at 13:28