Re: Generate a sequence of random numbers that sum up to 1?

Discussion in 'Python' started by Anthony Liu, Apr 22, 2006.

  1. Anthony Liu

    Anthony Liu Guest

    OK, I actually just want to "manually" create Hidden
    Markov Models by randomly generating the initial state
    probabilities PI, the transition probabilities A and
    the emission probabilities B, instead of learning such
    statistics from a corpus. They have to be subject the
    constraint that

    sum(PI) = 1.0
    sum(each row of A) = 1.0
    sum(each row of B) = 1.0

    Got an idea?

    Thank you for your help. I guess I can use random()
    instead of uniform(0,1) given your comments about
    uniform. I did not know the uniform function until a
    moment ago when I checked the

    As a matter of fact, given that we have to specify the
    number of states for an HMM, I would like to create a
    specified number of random floating numbers whose sum
    is 1.0.

    --- Edward Elliott <nobody@> wrote:

    > Anthony Liu wrote:
    > > But, I want the random numbers just generated sum

    > up
    > > to 1 .

    > This seems like an odd request. Might I ask what
    > it's for?
    > Generating random numbers in [0,1) that are both
    > uniform and sum to 1 looks
    > like an unsatisfiable task. Each number you
    > generate restricts the
    > possibilities for future numbers. E.g. if the first
    > number is 0.5, all
    > future numbers must be < 0.5 (indeed, must *sum* to
    > 0.5). You'll end up
    > with a distribution increasingly skewed towards
    > smaller numbers the more
    > you generate. I can't imagine what that would be
    > useful for.
    > If that's not a problem, do this: generate the
    > numbers, add them up, and
    > divide each by the sum.
    > nums = [random.uniform(0,1) for x in range(0,100)]
    > sum = reduce(lambda x,y: x+y, nums)
    > norm = [x/sum for x in nums]
    > Of course now the numbers aren't uniform over [0,1)
    > anymore.
    > Also note that the sum of the normalized numbers
    > will be very close to 1,
    > but slightly off due to representation issues. If
    > that level of accuracy
    > matters, you might consider generating your rands as
    > integers and then
    > fp-dividing by the sum (or just store them as
    > integers/fractions).
    > --

    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    Anthony Liu, Apr 22, 2006
    1. Advertisements

  2. Paul Rubin

    Paul Rubin Guest

    Anthony Liu <> writes:
    > OK, I actually just want to "manually" create Hidden
    > Markov Models by randomly generating the initial state
    > probabilities PI,

    OK, this sounds like you actually want to divide the unit interval up
    into pieces of varying sizes. Example (untested):

    n = 10 # number of pieces desired

    # Take the unit interval [0,1] and sprinkle in n-1 random points
    s = sorted([0,1] + [random() for i in xrange(n-1)])

    # now find the size of each sub-interval and make a list
    r = [(s[i+1] - s) for i in xrange(n)]

    print sum(r) # should be 1.0 within rounding error
    Paul Rubin, Apr 30, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shivermetimbers15
    Amy G
    Feb 18, 2004
  2. Anthony Liu
    Terry Reedy
    Apr 24, 2006
  3. Anthony Liu
    Anthony Liu
    Apr 22, 2006
  4. Prakhar
    Default User
    Jun 12, 2007
  5. globalrev
    Gabriel Genellina
    Apr 20, 2008