MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Restting Probabilities

  • To: mathgroup at
  • Subject: [mg29316] Re: [mg29284] Restting Probabilities
  • From: Tomas Garza <tgarza01 at>
  • Date: Wed, 13 Jun 2001 03:10:47 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

I'm not altogether sure I grasped your problem (why talk of a matrix, when
for example your x is a vector?). However, if I get you right, you want to
construct a sequence of (in your example) 5 Bernoulli random variables with
varying probabilities according to x, except that, whenever you get a 1, you
go back to the first value of x and proceed from there on (i.e., reset your
probabilities) until you get another 1 (or finish). I find the problem not
too trivial, so that if you are having only a "little" difficulty, then
you're OK. A possible approach, which I'm sure can be enormously improved by
many of the distinguished members of this group, uses the function defined
below "genVect" to generate the vector as follows:

<< Statistics`DiscreteDistributions`
b[u_, k_] := Random[BernoulliDistribution[u[[k]]]];

genVect[x_] := Module[{r = x},
    y = {b[r, 1]};
    (r = If[Last[y] == 1, RotateRight[r, Length[y]], r];
          y = Flatten[{y, b[r, #]}]) & /@ {2, 3, 4, 5}]


genVect[x] // Last
{0, 1, 0, 0, 0}

The last list (i.e., {0,1,0,0,0}) is the result you are looking for. Let me
explain (if you're really new to Mathematica, I'm afraid you'll have to
think hard). Starting from a length-1 vector with a variable produced with b
(the Bernoulli random generator defined above) using the first probability
(0.05 in the example), you append a new variable depending on the value of
y: if 0, just take the next probability in the original list; if 0, then
shift the original list one position to the right, so that the first value
of x is now in the second position, and produce the second variable. And so
on. The procedure constructs, a list of vectors until it gets to the last
one, which is the one you want. I'm sure this can be improved, but it seems
to work. I checked it with a simulation running the procedure 10,000 times
and looking at the frequencies. They correspond closely to the probabilities
in x, taking into account that every time there occurs a 1, the next value
must have a frequency close to 0.05, and so on. The simulation process is
performed with

sim = {};
Do[AppendTo[sim, genVect[x] // Last], {7000}] // Timing
{13.02 Second, Null}

and the 10,000 results are kept in "sim".

Tomas Garza
Mexico City

----- Original Message -----
From: "Michael Loop" <loopm at>
To: mathgroup at
Subject: [mg29316] [mg29284] Restting Probabilities

> I am a new user of Mathematica, and I am having a little difficulty
> the syntax.  I am trying to generate a matrix of 0's and 1's given by a
> Bernoulli probability distribution.   If we look at a case for a matrix
> five different probabilities, x={0.05,0.09,.15,.23,.47}.  I have been able
> to construct a matrix with these probabilities, but what I cannot do is
> reset the probabilites back to the start of the list when a 1 was the last
> result in the table.  In other words, each time a random Bernoulli
> a 1, I want the probability for the next xi in the row to be 0.05 again
> continue to preoceed down the list again until another 1 is reached.  If
> anyone could provide me with some help on the proper syntax for resetting
> the probability at the start of the list {x}after a 1 is reached in the
> I would be grateful.

  • Prev by Date: Re: MathML
  • Next by Date: Re: looking inside FindMininum
  • Previous by thread: Restting Probabilities
  • Next by thread: Lucas Sequences and Primaility Testing ... Please Help