Python random.betavariate() Examples

The following are 13 code examples of random.betavariate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module random , or try the search function .
Example #1
Source File: test_random.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_zeroinputs(self):
        # Verify that distributions can handle a series of zero inputs'
        g = random.Random()
        x = [g.random() for i in range(50)] + [0.0]*5
        g.random = x[:].pop; g.uniform(1,10)
        g.random = x[:].pop; g.paretovariate(1.0)
        g.random = x[:].pop; g.expovariate(1.0)
        g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
        g.random = x[:].pop; g.normalvariate(0.0, 1.0)
        g.random = x[:].pop; g.gauss(0.0, 1.0)
        g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
        g.random = x[:].pop; g.gammavariate(0.01, 1.0)
        g.random = x[:].pop; g.gammavariate(1.0, 1.0)
        g.random = x[:].pop; g.gammavariate(200.0, 1.0)
        g.random = x[:].pop; g.betavariate(3.0, 3.0)
        g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) 
Example #2
Source File: test_random.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_zeroinputs(self):
        # Verify that distributions can handle a series of zero inputs'
        g = random.Random()
        x = [g.random() for i in range(50)] + [0.0]*5
        g.random = x[:].pop; g.uniform(1,10)
        g.random = x[:].pop; g.paretovariate(1.0)
        g.random = x[:].pop; g.expovariate(1.0)
        g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
        g.random = x[:].pop; g.normalvariate(0.0, 1.0)
        g.random = x[:].pop; g.gauss(0.0, 1.0)
        g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
        g.random = x[:].pop; g.gammavariate(0.01, 1.0)
        g.random = x[:].pop; g.gammavariate(1.0, 1.0)
        g.random = x[:].pop; g.gammavariate(200.0, 1.0)
        g.random = x[:].pop; g.betavariate(3.0, 3.0)
        g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) 
Example #3
Source File: predeval.py    From proficiency-metric with Apache License 2.0 6 votes vote down vote up
def test (mean_ratio,base_rate,length,alpha_pos=2,alpha_neg=2):
        print "\n *** ScoringRule(mean_ratio=%d,base_rate=%f,length=%d,alpha: %d/%d)" % (mean_ratio,base_rate,length,alpha_pos,alpha_neg)
        assert mean_ratio > 1
        mean_neg = base_rate / (mean_ratio * base_rate + 1 - base_rate)
        beta_neg = alpha_neg * (1-mean_neg) / mean_neg
        mean_pos = min(1-base_rate,mean_ratio * mean_neg)
        beta_pos = alpha_pos * (1-mean_pos) / mean_pos
        sr = ScoringRule("mr={mr:g}".format(mr=mean_ratio))
        lq = LiftQuality(sr.title)
        for _ in xrange(int(round(base_rate*length))):
            score = random.betavariate(alpha_pos,beta_pos)
            sr.add(True,score)
            lq.add(True,score)
        for _ in xrange(int(round(length*(1-base_rate)))):
            score = random.betavariate(alpha_neg,beta_neg)
            sr.add(False,score)
            lq.add(False,score)
        print sr
        print lq 
Example #4
Source File: test_random.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_zeroinputs(self):
        # Verify that distributions can handle a series of zero inputs'
        g = random.Random()
        x = [g.random() for i in range(50)] + [0.0]*5
        g.random = x[:].pop; g.uniform(1,10)
        g.random = x[:].pop; g.paretovariate(1.0)
        g.random = x[:].pop; g.expovariate(1.0)
        g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
        g.random = x[:].pop; g.normalvariate(0.0, 1.0)
        g.random = x[:].pop; g.gauss(0.0, 1.0)
        g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
        g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
        g.random = x[:].pop; g.gammavariate(0.01, 1.0)
        g.random = x[:].pop; g.gammavariate(1.0, 1.0)
        g.random = x[:].pop; g.gammavariate(200.0, 1.0)
        g.random = x[:].pop; g.betavariate(3.0, 3.0)
        g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) 
Example #5
Source File: __main__.py    From slicesim with MIT License 6 votes vote down vote up
def get_dist(d):
    return {
        'randrange': random.randrange, # start, stop, step
        'randint': random.randint, # a, b
        'random': random.random,
        'uniform': random, # a, b
        'triangular': random.triangular, # low, high, mode
        'beta': random.betavariate, # alpha, beta
        'expo': random.expovariate, # lambda
        'gamma': random.gammavariate, # alpha, beta
        'gauss': random.gauss, # mu, sigma
        'lognorm': random.lognormvariate, # mu, sigma
        'normal': random.normalvariate, # mu, sigma
        'vonmises': random.vonmisesvariate, # mu, kappa
        'pareto': random.paretovariate, # alpha
        'weibull': random.weibullvariate # alpha, beta
    }.get(d) 
Example #6
Source File: genetic_algorithm.py    From kernel_tuner with Apache License 2.0 5 votes vote down vote up
def weighted_choice(population, n):
    """Randomly select n unique individuals from a weighted population, fitness determines probability of being selected"""
    def random_index_betavariate(pop_size):
        #has a higher probability of returning index of item at the head of the list
        alpha = 1
        beta = 2.5
        return int(random.betavariate(alpha, beta)*pop_size)

    def random_index_weighted(pop_size):
        """might lead to problems: if there's only one valid configuration this method only returns that configuration"""
        weights = [w for _, w in population]
        #invert because lower is better
        inverted_weights = [1.0/w for w in weights]
        prefix_sum = np.cumsum(inverted_weights)
        total_weight = sum(inverted_weights)
        randf = random.random()*total_weight
        #return first index of prefix_sum larger than random number
        return next(i for i,v in enumerate(prefix_sum) if v > randf)

    random_index = random_index_betavariate

    indices = [random_index(len(population)) for _ in range(n)]
    chosen = []
    for ind in indices:
        while ind in chosen:
            ind = random_index(len(population))
        chosen.append(ind)

    return [population[ind][0] for ind in chosen] 
Example #7
Source File: test_random.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_betavariate_return_zero(self, gammavariate_mock):
        # betavariate() returns zero when the Gamma distribution
        # that it uses internally returns this same value.
        gammavariate_mock.return_value = 0.0
        self.assertEqual(0.0, random.betavariate(2.71828, 3.14159)) 
Example #8
Source File: rockgen.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def skewedGauss(mu, sigma, bounds, upperSkewed=True):
    raw = gauss(mu, sigma)

    # Quicker to check an extra condition than do unnecessary math. . . .
    if raw < mu and not upperSkewed:
        out = ((mu - bounds[0]) / (3 * sigma)) * raw + ((mu * (bounds[0] - (mu - 3 * sigma))) / (3 * sigma))
    elif raw > mu and upperSkewed:
        out = ((mu - bounds[1]) / (3 * -sigma)) * raw + ((mu * (bounds[1] - (mu + 3 * sigma))) / (3 * -sigma))
    else:
        out = raw

    return out


# @todo create a def for generating an alpha and beta for a beta distribution
#   given a mu, sigma, and an upper and lower bound.  This proved faster in
#   profiling in addition to providing a much better distribution curve
#   provided multiple iterations happen within this function; otherwise it was
#   slower.
#   This might be a scratch because of the bounds placed on mu and sigma:
#
#   For alpha > 1 and beta > 1:
#   mu^2 - mu^3           mu^3 - mu^2 + mu
#   ----------- < sigma < ----------------
#      1 + mu                  2 - mu
#
##def generateBeta(mu, sigma, scale, repitions=1):
##    results = []
##
##    return results

# Creates rock objects: 
Example #9
Source File: test_random.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_betavariate_return_zero(self, gammavariate_mock):
        # betavariate() returns zero when the Gamma distribution
        # that it uses internally returns this same value.
        gammavariate_mock.return_value = 0.0
        self.assertEqual(0.0, random.betavariate(2.71828, 3.14159)) 
Example #10
Source File: test_random.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_betavariate_return_zero(self, gammavariate_mock):
        # betavariate() returns zero when the Gamma distribution
        # that it uses internally returns this same value.
        gammavariate_mock.return_value = 0.0
        self.assertEqual(0.0, random.betavariate(2.71828, 3.14159)) 
Example #11
Source File: tilechain_shimmering_leaves.py    From lifxlan with MIT License 5 votes vote down vote up
def get_day_color():
    #return (int(800 + (5000 * betavariate(0.2, 0.9))), randint(0, 10000), int(65535 * betavariate(0.5, 1)), randint(3500, 5000))
    #return (randint(11739, 30836), randint(0, 10000), int(65535 * betavariate(0.15, 1)), randint(3500, 5000))
    return (randint(11739, 30836), randint(3000, 10000), int(65535 * betavariate(0.15, 1)), randint(3000, 4000)) 
Example #12
Source File: tilechain_coals.py    From lifxlan with MIT License 5 votes vote down vote up
def get_fire_color():
    return (int(800 + (5000 * betavariate(0.2, 0.9))), randint(60000, 65535), int(65535 * betavariate(0.05, 1)), randint(2500, 3500)) 
Example #13
Source File: thinkstats2.py    From DataExploration with MIT License 5 votes vote down vote up
def Random(self):
        """Generates a random variate from this distribution."""
        return random.betavariate(self.alpha, self.beta)