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




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 python.org
documentation.

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@xxxxxxxxx> 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).
--
http://mail.python.org/mailman/listinfo/python-list



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
.



Relevant Pages

  • No result is likely - Part IV (conclusion)
    ... "When do the dice know that it's time to give the player the win so ... To sum up: ... to be able to calculate the probabilities of different outcomes, ...
    (rec.gambling.craps)
  • Re: A modest proposal (was Calculus XOR Probability)
    ... Tony Orlow wrote: ... over an infinite set of possibilities is not well handled by the ... classical notion that all individual probabilities sum to 1, ... the individual probabilities are considered equal to zero. ...
    (sci.math)
  • Re: Beginners question
    ... When you take PROD and SUM you ... silently assume that the truth values behave as probabilities of independent events. ... suggested to abandon fuzzy and do it statistically. ...
    (comp.ai.fuzzy)
  • Re: Confirmation of Shannons Mistake about Perfect Secrecy of One-time-pad
    ... you cannot use the distribution on K to determine ... c not fixed so the distribution of K is uniform ... Computing the conditional probabilities. ... (sum of probabilities of all events where M=0 and C=0)/ ...
    (sci.math)
  • Re: random numbers with fixed sum
    ... exponential variables and normalize (divide by the sum). ... See the book "Non Uniform Random Variate Generation", ... intersection that contains points with coordinates of a fixed sum. ... I claim that there are simpler methods of filling a triangle ...
    (comp.soft-sys.matlab)