Re: Restting Probabilities

• To: mathgroup at smc.vnet.net
• Subject: [mg29316] Re: [mg29284] Restting Probabilities
• From: Tomas Garza <tgarza01 at prodigy.net.mx>
• Date: Wed, 13 Jun 2001 03:10:47 -0400 (EDT)
• References: <200106110838.EAA06919@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```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:

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

In[3]:=
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}]

Example:

In[4]:=
genVect[x] // Last
Out[4]=
{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

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

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

Tomas Garza
Mexico City

----- Original Message -----
From: "Michael Loop" <loopm at yahoo.com>
To: mathgroup at smc.vnet.net
Subject: [mg29316] [mg29284] Restting Probabilities

> I am a new user of Mathematica, and I am having a little difficulty
grasping
> 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
with
> 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
generates
> a 1, I want the probability for the next xi in the row to be 0.05 again
and
> 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
row,
> I would be grateful.
>

```

• Prev by Date: Re: MathML
• Next by Date: Re: looking inside FindMininum
• Previous by thread: Restting Probabilities