Expected Value

G

George

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top