Example #1
0
def cotri(a,b,c):
    selectStream(2)

    p1 = (c-a)/(b-a)
    u = random()
    variate = 0

    if (u < p1):
        variate = a + (c-a)*pow(random(),.5)
    else:
        variate = b - (b-c)*pow((1-random()),.5)

    return(variate)
Example #2
0
def artri(a,b,c):
    selectStream(1)
    temp=0

    while(temp==0):
        x = a + (b - a) *random() # gen U(a,b) for x */
        S = c*random() # use mode for majorizing fn */ 

        if(x <= c):
            test = (2*x - 2*a)/((b-a)*(c-a))
        else:
            test = (2*b - 2*x)/((b-a)*(b-c))
        if (S <= test):
            return(x)
Example #3
0
def Equilikely(a,b):
# ===================================================================
# * Returns an equilikely distributed integer between a and b inclusive. 
# * NOTE: use a < b
# * ===================================================================
# */
  return (a + int((b - a + 1) * random()))
Example #4
0
def exponential(m):
  #=========================================================
  #Returns an exponentially distributed positive real number. 
  #NOTE: use m > 0.0
  #=========================================================
  #
  return (-m * log(1.0 - random()))
Example #5
0
def uniform(a,b):
  #=========================================================== 
  #Returns a uniformly distributed real number between a and b. 
  #NOTE: use a < b
  #===========================================================
  #
  return (a + (b - a) * random())
Example #6
0
def Uniform(a,b):
# =========================================================== 
# * Returns a uniformly distributed real number between a and b. 
# * NOTE: use a < b
# * ===========================================================
# */
  return (a + (b - a) * random())
Example #7
0
def Uniform(a, b):
    #===========================================================
    #Returns a uniformly distributed real number between a and b.
    #NOTE: use a < b
    #===========================================================
    #
    return (a + (b - a) * random())
Example #8
0
def Exponential(m):
    #=========================================================
    #Returns an exponentially distributed positive real number.
    #NOTE: use m > 0.0
    #=========================================================
    #
    return (-m * log(1.0 - random()))
Example #9
0
def Normal(m, s):
    #========================================================================
    #Returns a normal (Gaussian) distributed real number.
    #NOTE: use s > 0.0
    #
    #Uses a very accurate approximation of the normal idf due to Odeh & Evans,
    #J. Applied Statistics, 1974, vol 23, pp 96-97.
    #========================================================================
    #
    p0 = 0.322232431088
    q0 = 0.099348462606
    p1 = 1.0
    q1 = 0.588581570495
    p2 = 0.342242088547
    q2 = 0.531103462366
    p3 = 0.204231210245e-1
    q3 = 0.103537752850
    p4 = 0.453642210148e-4
    q4 = 0.385607006340e-2

    u = random()
    if (u < 0.5):
        t = sqrt(-2.0 * log(u))
    else:
        t = sqrt(-2.0 * log(1.0 - u))

    p = p0 + t * (p1 + t * (p2 + t * (p3 + t * p4)))
    q = q0 + t * (q1 + t * (q2 + t * (q3 + t * q4)))

    if (u < 0.5):
        z = (p / q) - t
    else:
        z = t - (p / q)

    return (m + s * z)
Example #10
0
def normal(m,s):
  #========================================================================
  #Returns a normal (Gaussian) distributed real number.
  #NOTE: use s > 0.0
  #
  #Uses a very accurate approximation of the normal idf due to Odeh & Evans, 
  #J. Applied Statistics, 1974, vol 23, pp 96-97.
  #========================================================================
  #
  p0 = 0.322232431088     
  q0 = 0.099348462606
  p1 = 1.0                
  q1 = 0.588581570495
  p2 = 0.342242088547     
  q2 = 0.531103462366
  p3 = 0.204231210245e-1  
  q3 = 0.103537752850
  p4 = 0.453642210148e-4  
  q4 = 0.385607006340e-2

  u = random()
  if (u < 0.5):
    t = sqrt(-2.0 * log(u))
  else:
    t = sqrt(-2.0 * log(1.0 - u))

  p   = p0 + t * (p1 + t * (p2 + t * (p3 + t * p4)))
  q   = q0 + t * (q1 + t * (q2 + t * (q3 + t * q4)))

  if (u < 0.5):
    z = (p / q) - t
  else:
    z = t - (p / q)

  return (m + s * z)
Example #11
0
def geometric(p):
  #====================================================
  #Returns a geometric distributed non-negative integer.
  #NOTE: use 0.0 < p < 1.0
  #====================================================
  #

  return (int(log(1.0 - random()) / log(p)))
Example #12
0
def Geometric(p):
    #====================================================
    #Returns a geometric distributed non-negative integer.
    #NOTE: use 0.0 < p < 1.0
    #====================================================
    #

    return (int(log(1.0 - random()) / log(p)))
Example #13
0
def Bernoulli(p):
    #========================================================
    #Returns 1 with probability p or 0 with probability 1 - p.
    #NOTE: use 0.0 < p < 1.0
    #========================================================

    if (random() < 1 - p):
        return (0)
    else:
        return (1)
Example #14
0
def bernoulli(p):
  #========================================================
  #Returns 1 with probability p or 0 with probability 1 - p. 
  #NOTE: use 0.0 < p < 1.0                                   
  #========================================================
  
  if (random() < 1 - p):
    return(0)
  else:
    return(1)
Example #15
0
def intri(a,b,c):
    selectStream(0)

    u = random()
    variate = 0

    if (u < (c-a)/(b-a)):
        variate = a + pow((u*(b-a)*(c-a)),.5) 
    else:
        variate = b - pow(((1-u)*(b-a)*(b-c)), .5)

    return(variate)
Example #16
0
def Exponential(m):
# ---------------------------------------------------
# * generate an Exponential random variate, use m > 0.0 
# * ---------------------------------------------------
# */
  return (-m * log(1.0 - random()))
Example #17
0
def Uniform(a,b):  
# --------------------------------------------
# * generate a Uniform random variate, use a < b 
# * --------------------------------------------
# */
  return (a + (b - a) * random())  
Example #18
0
def Exponential(m):
    # ---------------------------------------------------
    # * generate an Exponential random variate, use m > 0.0
    # * ---------------------------------------------------
    # */
    return (-m * log(1.0 - random()))
Example #19
0
def Uniform(a,b):  
#  ---------------------------------------------
# * generate a Uniform random variate, use a < b 
# *--------------------------------------------- 
# */
  return (a + (b - a) * random())