Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Expected Value

Reply
Thread Tools

Expected Value

 
 
George
Guest
Posts: n/a
 
      09-09-2005
I have never done any programming with python in my life so I will most
definetly need help understanding how I can accomplish this part of my
program.

The function expectP(z) computes E(X) for the random variable
representing a sample over the probability generated by "pf" for the
set of discrete items [1,100000]. The constant c is needed so that the
probability sum to 1.0, as required by the definition of probability.
The function pf(r,c,x) returns the probability that item x will be
equal to a randomly chosen variable, denoted by P(x) or P(X=x) in
mathematical texts, where c is the constant mentioned above and r is
needed because we are considering a power law distribution. I need help
in writing the expectP function and do I compile and execute this code
on a linux box.

"""
Module to compute expected value.

>>> showExpectP(0.5)

33410
>>> showExpectP(0.85)

15578
>>> showExpectP(0.9)

12953
>>> showExpectP(0.99)

8693
>>> showExpectP(0.999)

8312

"""

def norm(r):
"calculate normalization factor for a given exponent"
# the function for this distribution is P(x) = c*(x**-r)
# and the job of this norm function is to calculate c based
# on the range [1,10**5]
sum = 0.0
for i in range(1,1+10**5):
# final sum would be more accurate by summing from small values
# to large ones, but this is just a homework, so sum 1,2,..10**5
sum += float(i)**-r
return 1.0/sum

def pf(r,c,x):
"return a power-law probability for a given value"
# the function for this distribution is P(x) = c*(x**-r)
# where the constant c is such that it normalizes the distribution
return c*(float(x)**-r)

#--------- between these lines, define expectP() function
-----------------


#--------- end of expectP() definition
------------------------------------

def showExpectP(limit):
"display ExpectP(limit) by rounding down to nearest integer"
k = expectP(limit)
return int(k)

if __name__ == '__main__':
import doctest, sys
doctest.testmod(sys.modules[__name__])

thankyou sooo much.

 
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
Value does not fall within the expected range - Crystal Report hot fix anamika ASP .Net 0 06-15-2006 10:49 AM
CrystalReports: Value does not fall within the expected range. =?Utf-8?B?amF2YXRvcGlh?= ASP .Net 4 10-14-2005 06:16 PM
ISE:ERROR:Xst:829: Constant Value expected for Generic 'U'? Phil Tomson VHDL 3 02-16-2005 08:54 AM
Newbie again: position() doesn't return expected value Yereth XML 2 01-20-2004 11:34 AM
position() doesn't return expected value Yereth XML 1 01-20-2004 12:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57