Some questions on pow and random

Discussion in 'Python' started by joy99, Mar 27, 2011.

  1. joy99

    joy99 Guest

    Dear Group,

    I have two questions one related to pow() and other is related to
    random.
    My questions are as below:

    (i) By standard definition of Likelihood Estimation, we get if x EURO X,
    where X is a countable set of types, p is probability and f is
    frequency.
    L(f;p)=Ðp(x)f(x)

    My question is python provides two functions,
    (a) pow for power.
    (b) reduce(mul, list)

    Now, how to combine them? If any one can suggest any help?
    As p(x)f(x), would be huge would pow support it?

    (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
    the E of EM algorithm, without going into much technical details is,
    P0(x1,x2), P1(x1,x2) ....

    Now I am taking random.random() to generate both x1 and x2 and trying
    to multiply them, is it fine? Or should I take anything else?

    If any one can find time to resolve my query or suggest some better
    approaches, I would be grateful.

    Thanks in advance,
    Best Regards,
    Subhabrata.
     
    joy99, Mar 27, 2011
    #1
    1. Advertising

  2. On Mar 27, 11:07 am, joy99 <> wrote:
    > (i) By standard definition of Likelihood Estimation, we get if x EURO X,
    > where X is a countable set of types, p is probability and f is
    > frequency.
    > L(f;p)=Ðp(x)f(x)
    >
    > My question is python provides two functions,
    > (a)     pow for power.
    > (b)     reduce(mul, list)
    >
    > Now, how to combine them? If any one can suggest any help?


    If I'm understanding your question correctly, it sounds as though
    you've got a vector p = (p_1, p_2, ..., p_n) of probabilities and a
    corresponding vector f = (f_1, f_2, ..., f_n) of integer frequencies,
    and you want to compute the product of the quantities p_i ** f_i. Is
    that right?

    Assuming that p and f are represented by Python lists, you might do
    something like this:

    >>> p = [0.1, 0.5, 0.4]
    >>> f = [3, 24, 18]
    >>> product = 1.0
    >>> for pi, fi in zip(p, f):

    .... product *= pi**fi
    ....
    >>> product

    4.096000000000005e-18

    I wouldn't particularly recommend using 'reduce' for this, since it
    doesn't lead to readable code, but if you must you can do:

    >>> import operator
    >>> reduce(operator.mul, map(pow, p, f), 1)

    4.096000000000005e-18

    or:

    >>> reduce(operator.mul, [pi**fi for pi, fi in zip(p, f)], 1)

    4.096000000000005e-18

    > As p(x)f(x), would be huge would pow support it?


    You'll run into problems with underflow to zero fairly quickly. The
    usual solution is to work with log likelihood instead of likelihood
    itself, in which case you'd want a sum instead:

    >>> sum(fi*log(pi) for pi, fi in zip(p, f))

    -40.03652078615561

    If you really need the likelihood itself, you might try using the
    Decimal module, which allows a much wider exponent range than Python's
    builtin float.

    --
    Mark
     
    Mark Dickinson, Mar 27, 2011
    #2
    1. Advertising

  3. On Mar 27, 11:07 am, joy99 <> wrote:

    > (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
    > the E of EM algorithm, without going into much technical details is,
    > P0(x1,x2), P1(x1,x2) ....
    >
    > Now I am taking random.random() to generate both x1 and x2 and trying
    > to multiply them, is it fine? Or should I take anything else?


    Sorry, it's unclear to me what you're asking here. Can you rephrase
    this as a question about Python's random.random() function?

    If you're asking whether it's okay to regard your generated x1 and x2
    as independent, then the answer is yes.

    --
    Mark
     
    Mark Dickinson, Mar 27, 2011
    #3
  4. joy99

    joy99 Guest

    On Mar 27, 4:36 pm, Mark Dickinson <> wrote:
    > On Mar 27, 11:07 am, joy99 <> wrote:
    >
    > > (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
    > > the E of EM algorithm, without going into much technical details is,
    > > P0(x1,x2), P1(x1,x2) ....

    >
    > > Now I am taking random.random() to generate both x1 and x2 and trying
    > > to multiply them, is it fine? Or should I take anything else?

    >
    > Sorry, it's unclear to me what you're asking here.  Can you rephrase
    > this as a question about Python's random.random() function?
    >
    > If you're asking whether it's okay to regard your generated x1 and x2
    > as independent, then the answer is yes.
    >
    > --
    > Mark


    Dear Mark,
    Thank you for kindly allowing your time to put your kind suggestions.
    I am trying to rephrase my questions, as you might have asked it.

    (i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
    are writing in Python as,
    variable1=2
    variable2=3
    result=pow(variable1,variable2)

    In my first problem p(x) a list of float/decimals and f(x) is another
    such.
    Here,
    variable1=p(x)
    variable2=f(x)
    so that we can write, pow(variable1,variable2) but as it is a list not
    a number and as the size is huge, so would it pow support it?
    As I copied the question from word processor to the post, so there was
    a slight confusion.

    (ii) The second question is, if I have another set of variables,

    variable1=random.random()
    variable2=random.random()

    Now my question is, can I do
    result=variable1*variable2

    Or should I do anything else?

    Best Regards,
    Subhabrata.
     
    joy99, Mar 27, 2011
    #4
  5. On Mar 27, 3:00 pm, joy99 <> wrote:
    > (i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
    > are writing in Python as,
    > variable1=2
    > variable2=3
    > result=pow(variable1,variable2)
    >
    > In my first problem p(x) a list of float/decimals and f(x) is another
    > such.
    > Here,
    > variable1=p(x)
    > variable2=f(x)
    > so that we can write, pow(variable1,variable2) but as it is a list not
    > a number and as the size is huge, so would it pow support it?


    No: pow won't work on lists. It will work on (a) numbers (pow(2, 3) -
    > 8),

    or (b) numpy arrays, e.g.:

    >>> import numpy as np
    >>> x = np.array([0.1, 0.5, 0.4])
    >>> y = np.array([3, 24, 18])
    >>> pow(x, y)

    array([ 1.00000000e-03, 5.96046448e-08, 6.87194767e-08])
    >>> x ** y # exactly equivalent

    array([ 1.00000000e-03, 5.96046448e-08, 6.87194767e-08])

    > (ii) The second question is, if I have another set of variables,
    >
    > variable1=random.random()
    > variable2=random.random()


    In this case 'variable1' and 'variable2' are Python floats, so yes,
    you can multiply them directly. (BTW, you can always experiment
    directly at the Python interactive prompt to answer this sort of
    question.)

    Mark
     
    Mark Dickinson, Mar 27, 2011
    #5
  6. joy99

    joy99 Guest

    On Mar 27, 8:52 pm, Mark Dickinson <> wrote:
    > On Mar 27, 3:00 pm, joy99 <> wrote:
    >
    > > (i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
    > > are writing in Python as,
    > > variable1=2
    > > variable2=3
    > > result=pow(variable1,variable2)

    >
    > > In my first problem p(x) a list of float/decimals and f(x) is another
    > > such.
    > > Here,
    > > variable1=p(x)
    > > variable2=f(x)
    > > so that we can write, pow(variable1,variable2) but as it is a list not
    > > a number and as the size is huge, so would it pow support it?

    >
    > No:  pow won't work on lists.  It will work on (a) numbers (pow(2, 3)-> 8),
    >
    > or (b) numpy arrays, e.g.:
    >
    > >>> import numpy as np
    > >>> x = np.array([0.1, 0.5, 0.4])
    > >>> y = np.array([3, 24, 18])
    > >>> pow(x, y)

    >
    > array([  1.00000000e-03,   5.96046448e-08,   6.87194767e-08])>>> x ** y # exactly equivalent
    >
    > array([  1.00000000e-03,   5.96046448e-08,   6.87194767e-08])
    >
    > > (ii) The second question is, if I have another set of variables,

    >
    > > variable1=random.random()
    > > variable2=random.random()

    >
    > In this case 'variable1' and 'variable2' are Python floats, so yes,
    > you can multiply them directly.  (BTW, you can always experiment
    > directly at the Python interactive prompt to answer this sort of
    > question.)
    >
    > Mark


    Thanks Mark. Wishing you a nice day ahead.
    Best Regards,
    Subhabrata.
     
    joy99, Mar 27, 2011
    #6
    1. Advertising

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. Clueless Moron

    math.pow vs pow

    Clueless Moron, Nov 27, 2003, in forum: Python
    Replies:
    5
    Views:
    927
    John J. Lee
    Nov 28, 2003
  2. Replies:
    9
    Views:
    692
    Mark Jeffcoat
    Oct 19, 2006
  3. Michel Rouzic

    pow(2, 1/2) != pow(2, 0.5) problem

    Michel Rouzic, Jun 15, 2005, in forum: C Programming
    Replies:
    52
    Views:
    1,673
    Alan Balmer
    Jun 20, 2005
  4. globalrev
    Replies:
    4
    Views:
    774
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page