Expected Value

Discussion in 'Python' started by George, Sep 11, 2005.

  1. George

    George Guest

    How would I get the expected value out of this information. I have
    tried many times to understand this but am unable to.

    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.

    The function expectP(z) computes E(X) with r=z, using pf(r,c,x) where x
    ranges over the set of discrete items in [1,100000]

    The program:

    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
    George, Sep 11, 2005
    1. Advertisements

  2. On Sat, 10 Sep 2005 17:50:06 -0700, George wrote:

    > How would I get the expected value out of this information. I have
    > tried many times to understand this but am unable to.

    Do you have a specific Python problem here, or do you need help with the
    maths? If Python, please tell us what your problem is. If your problem is
    with the maths, then a statistics and/or probability forum is probably
    going to be more useful to you.

    If you are having a problem with the code, not the maths, I frequently
    find that it helps to reduce the problem. Instead of calculating the
    expectation of a discrete pdf with one hundred thousand values, start with
    a toy problem: write code that calculates the expectation of a pdf with
    only five values. That is small enough that you can experiment using the
    interactive Python interpreter, and if need be calculate the correct
    answer with paper and pencil.

    You might also find it useful to read this:


    Steven D'Aprano, Sep 12, 2005
    1. Advertisements

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. Phil Tomson
    Phil Tomson
    Feb 16, 2005
  2. =?Utf-8?B?amF2YXRvcGlh?=

    CrystalReports: Value does not fall within the expected range.

    =?Utf-8?B?amF2YXRvcGlh?=, Oct 5, 2005, in forum: ASP .Net
    Oct 14, 2005
  3. Yereth
    Yereth Jansen
    Jan 20, 2004
  4. Yereth
    Peter Flynn
    Jan 20, 2004
  5. anamika
    Jun 15, 2006

Share This Page