A Bernoulli Process is a sequence of iid (independent, identically-distributed) random variables x1,x2,...,xi,..., where the probability mass function of xi is
xi = S(success) with probability p, xi = F(failure) with probability q, with p+q = 1.
Depending on the application, (S,F) may be (1,0), (H,T), (true, false), etc.
Here are two obvious ways to generate a (1,0) Bernoulli process in Matlab:
The "best" answer will depend on the application and computer at hand.
If the computer has lots of memory then the vectorized version is the simplest, and may be the fastest.
If "Process vector or scalar" takes a lot of time then the time to generate bernp by either method may be negligible.
If a fixed amount of memory is available for bernp then we must use the component form or a mini-vectorized form.
Using rand to generate 1 random bit seems wasteful, given that rand returns a double precision floating point number which has about 53 random bits. Can we use rand more efficiently?
My interest in the Bernoulli process was sparked by re-reading Feller Volume 1. At the bottom of page 18 Feller says: "In this volume we shall consider only discrete sample spaces". A closer examination shows that Feller derives (nearly) everything in this book from the Bernoulli process.
No products are associated with this question.
Interesting question. The number of wasted bits depends on the number of bits you need to define p. If p = 1/sqrt(3), no wasted bits! Any algorithm trying to beat your algorithm 1 would require a lot of special cases, and would be competing with a built-in algorithm.
The Statistics Toolbox uses a variation on your first algorithm.
( edited to expand on answer )
Yes, you can make better use of rand.
If the probability is expressible in lowest terms with a denominator of 2^n, n <= 26. With the 2^n denominator, you take n consecutive bits at a time out of the float representation.
For other probabilities, you use Arithmetic Coding
Play games and win prizes!Learn more