Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Some questions on pow and random

Reply
Thread Tools

Some questions on pow and random

 
 
joy99
Guest
Posts: n/a
 
      03-27-2011
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.



 
Reply With Quote
 
 
 
 
Mark Dickinson
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 11:07*am, joy99 <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mark Dickinson
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 11:07*am, joy99 <(E-Mail Removed)> 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

 
Reply With Quote
 
joy99
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 4:36*pm, Mark Dickinson <(E-Mail Removed)> wrote:
> On Mar 27, 11:07*am, joy99 <(E-Mail Removed)> 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.






 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 3:00*pm, joy99 <(E-Mail Removed)> 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) -
> ,

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
 
Reply With Quote
 
joy99
Guest
Posts: n/a
 
      03-27-2011
On Mar 27, 8:52*pm, Mark Dickinson <(E-Mail Removed)> wrote:
> On Mar 27, 3:00*pm, joy99 <(E-Mail Removed)> 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)-> ,
>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
pow(2, 1/2) != pow(2, 0.5) problem Michel Rouzic C Programming 52 06-20-2005 10:24 PM
math.pow vs pow Clueless Moron Python 5 11-28-2003 09:56 PM
D100 in Scotland - 10 random things I learned (some OT, some rant) mikey2 Digital Photography 6 09-09-2003 05:10 PM



Advertisments