How to generate geometric random numbers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MyInfoStation@gmail.com

    How to generate geometric random numbers?

    Hi all,

    I am a newbie to Python and would like to genereate some numbers
    according to geometric distribution. However, the Python Random package
    seems do not have implemented functionality. I am wondering is there
    exist any other libraries that can do this job?

    Thanks a lot,

    Da

  • Gerhard Fiedler

    #2
    Re: How to generate geometric random numbers?

    On 2006-07-23 17:12:20, MyInfoStation@g mail.com wrote:
    I am a newbie to Python and would like to genereate some numbers
    according to geometric distribution. However, the Python Random package
    seems do not have implemented functionality. I am wondering is there
    exist any other libraries that can do this job?
    The usual way is to generate standard random numbers (linear distribution)
    and then apply whatever transformation you need to generate the desired
    distribution.

    Gerhard

    Comment

    • Robert Kern

      #3
      Re: How to generate geometric random numbers?

      Gerhard Fiedler wrote:
      On 2006-07-23 17:12:20, MyInfoStation@g mail.com wrote:
      >
      >I am a newbie to Python and would like to genereate some numbers
      >according to geometric distribution. However, the Python Random package
      >seems do not have implemented functionality. I am wondering is there
      >exist any other libraries that can do this job?
      >
      The usual way is to generate standard random numbers (linear distribution)
      and then apply whatever transformation you need to generate the desired
      distribution.
      That only works if there is such a transformation.

      The geometric distribution and many others have been implemented in numpy:



      In [1]: from numpy import random

      In [2]: random.geometri c(0.5, size=100)
      Out[2]:
      array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
      2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
      4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
      1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
      4, 2, 1, 1, 3, 1, 3, 1])

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • MyInfoStation@gmail.com

        #4
        Re: How to generate geometric random numbers?

        Robert Kern wrote:
        Gerhard Fiedler wrote:
        On 2006-07-23 17:12:20, MyInfoStation@g mail.com wrote:
        I am a newbie to Python and would like to genereate some numbers
        according to geometric distribution. However, the Python Random package
        seems do not have implemented functionality. I am wondering is there
        exist any other libraries that can do this job?
        The usual way is to generate standard random numbers (linear distribution)
        and then apply whatever transformation you need to generate the desired
        distribution.
        >
        That only works if there is such a transformation.
        >
        The geometric distribution and many others have been implemented in numpy:
        >

        >
        In [1]: from numpy import random
        >
        In [2]: random.geometri c(0.5, size=100)
        Out[2]:
        array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
        2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
        4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
        1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
        4, 2, 1, 1, 3, 1, 3, 1])
        >
        --
        Robert Kern
        >
        "I have come to believe that the whole world is an enigma, a harmless enigma
        that is made terrible by our own mad attempt to interpret it as though it had
        an underlying truth."
        -- Umberto Eco
        Thanks a lot. I will try it out.

        But I am still surprised because the default Random package in Python
        can generate so few discrete random distritbuions, while it can
        generate quite a few continuous distribution, including some not very
        common one.

        Da

        Comment

        • Paul Rubin

          #5
          Re: How to generate geometric random numbers?

          MyInfoStation@g mail.com writes:
          But I am still surprised because the default Random package in Python
          can generate so few discrete random distritbuions, while it can
          generate quite a few continuous distribution, including some not very
          common one.
          It looks pretty simple to transform the uniform distribution to the
          geometric distribution. The formula for its cdf is pretty simple:

          cdf(p,n) = (1-p)**(n-1)*p

          For fixed p, if the cdf is c, we get (unless I made an error),

          n = log(c, 1-p) - 1

          So choose a uniform point c in the unit interval, run it through that
          formula, and round up to the nearest integer.

          See http://en.wikipedia.org/wiki/Geometric_distribution
          for more about the distribution.

          Comment

          • Paul Rubin

            #6
            Re: How to generate geometric random numbers?

            Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
            n = log(c, 1-p) - 1
            I meant n = log(c/p, 1-p) - 1
            sorry.

            Comment

            • Robert Kern

              #7
              Re: How to generate geometric random numbers?

              Paul Rubin wrote:
              Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
              > n = log(c, 1-p) - 1
              >
              I meant n = log(c/p, 1-p) - 1
              sorry.
              import random
              from math import ceil, log

              def geometric(p):
              """ Geometric distribution per Devroye, Luc. _Non-Uniform Random Variate
              Generation_, 1986, p 500. http://cg.scs.carleton.ca/~luc/rnbookindex.html
              """

              # p should be in (0.0, 1.0].
              if p <= 0.0 or p 1.0:
              raise ValueError("p must be in the interval (0.0, 1.0]")
              elif p == 1.0:
              # If p is exactly 1.0, then the only possible generated value is 1.
              # Recognizing this case early means that we can avoid a log(0.0) later.
              # The exact floating point comparison should be fine. log(eps) works just
              # dandy.
              return 1

              # random() returns a number in [0, 1). The log() function does not
              # like 0.
              U = 1.0 - random.random()

              # Find the corresponding geometric variate by inverting the uniform variate.
              G = int(ceil(log(U) / log(1.0 - p)))
              return G

              --
              Robert Kern

              "I have come to believe that the whole world is an enigma, a harmless enigma
              that is made terrible by our own mad attempt to interpret it as though it had
              an underlying truth."
              -- Umberto Eco

              Comment

              • Paul Rubin

                #8
                Re: How to generate geometric random numbers?

                Robert Kern <robert.kern@gm ail.comwrites:
                G = int(ceil(log(U) / log(1.0 - p)))
                I usually owuld write that as int(ceil(log(U, 1.0 - p))).

                Comment

                • Robert Kern

                  #9
                  Re: How to generate geometric random numbers?

                  Paul Rubin wrote:
                  Robert Kern <robert.kern@gm ail.comwrites:
                  > G = int(ceil(log(U) / log(1.0 - p)))
                  >
                  I usually owuld write that as int(ceil(log(U, 1.0 - p))).
                  Knock yourself out. I was cribbing from my C implementation in numpy.

                  --
                  Robert Kern

                  "I have come to believe that the whole world is an enigma, a harmless enigma
                  that is made terrible by our own mad attempt to interpret it as though it had
                  an underlying truth."
                  -- Umberto Eco

                  Comment

                  • Paul Rubin

                    #10
                    Re: How to generate geometric random numbers?

                    Robert Kern <robert.kern@gm ail.comwrites:
                    I usually owuld write that as int(ceil(log(U, 1.0 - p))).
                    Knock yourself out. I was cribbing from my C implementation in numpy.
                    Oh cool, I thought you were pasting from a Python implementation. No prob.

                    Comment

                    Working...