Python random.betavariate() Examples

The following are code examples for showing how to use random.betavariate(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: NiujiaoDebugger   Author: MrSrc   File: test_random.py    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
Project: fakeface   Author: Lomin-ai   File: dataset.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _crop_random(self, idx):
        t = random.betavariate(self.beta_a, self.beta_b)
        w = int((1 - t) * self.imdb.minsize[idx] + t * self.imdb.maxsize[idx] + 0.000001)
        h = int(w * self.cfg.dataset.ar + 0.000001)

        min_top, min_left, min_bottom, min_right = self.imdb.bbox_min[idx]
        max_top, max_left, max_bottom, max_right = self.imdb.bbox_max[idx]
        min_offset_x = max(max_left, min_right - w)
        min_offset_y = max(max_top, min_bottom - h)
        max_offset_x = min(max_right - w, min_left)
        max_offset_y = min(max_bottom - h, min_top)

        if min_offset_x > max_offset_x or min_offset_y > max_offset_y:
            raise OffsetError()

        offset_x = random.randrange(max_offset_x - min_offset_x + 1) + min_offset_x
        offset_y = random.randrange(max_offset_y - min_offset_y + 1) + min_offset_y
    
        return offset_y, offset_x, offset_y + h, offset_x + w 
Example 3
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_random.py    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 4
Project: cells   Author: AlesTsurko   File: test_random.py    MIT License 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
Project: NiujiaoDebugger   Author: MrSrc   File: test_random.py    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 6
Project: kernel_tuner   Author: benvanwerkhoven   File: genetic_algorithm.py    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
Project: HyperStream   Author: IRC-SPHERE   File: 2017-06-20_v1.0.0.py    MIT License 5 votes vote down vote up
def _execute(self, sources, alignment_stream, interval):
        if alignment_stream is None:
            raise ToolExecutionError("Alignment stream expected")

        for ti, _ in alignment_stream.window(interval, force_calculation=True):
            yield StreamInstance(ti, random.betavariate(alpha=self.alpha, beta=self.beta)) 
Example 8
Project: pytoolkit   Author: ak110   File: ndimage.py    MIT License 5 votes vote down vote up
def mixup(sample1: tuple, sample2: tuple, mode: str = "beta") -> tuple:
    """mixup。 <https://arxiv.org/abs/1710.09412>

    常に「sample1の重み >= sample2の重み」となるようにしている。

    Args:
        sample1: データその1
        sample2: データその2
        mode: 混ぜる割合の乱数の種類。
            - 'beta': β分布を0.5以上にした分布
            - 'uniform': [0.5, 1]の一様分布
            - 'uniform_ex': [0.5, √2]の一様分布

    Returns:
        tuple: 混ぜられたデータ。

    """
    if mode == "beta":
        r = np.float32(np.abs(random.betavariate(0.2, 0.2) - 0.5) + 0.5)
    elif mode == "uniform":
        r = np.float32(random.uniform(0.5, 1))
    elif mode == "uniform_ex":
        r = np.float32(random.uniform(0.5, np.sqrt(2)))
    else:
        raise ValueError(f"Invalid mode: {mode}")
    assert r >= 0.5
    return mix_data(sample1, sample2, r) 
Example 9
Project: pytoolkit   Author: ak110   File: ndimage.py    MIT License 5 votes vote down vote up
def cut_mix(sample1: tuple, sample2: tuple, beta: float = 1.0) -> tuple:
    """CutMix。 <https://arxiv.org/abs/1905.04899>

    Args:
        sample1: 画像とone-hot化したラベル(など)のタプル
        sample2: 画像とone-hot化したラベル(など)のタプル
        beta: beta分布のbeta

    Returns:
        image, label

    """
    image1, label1 = sample1
    image2, label2 = sample2
    assert image1.shape == image2.shape
    lam = random.betavariate(beta, beta)
    H, W = image1.shape[:2]
    cut_rat = np.sqrt(1.0 - lam)
    cut_w = np.int(W * cut_rat)
    cut_h = np.int(H * cut_rat)
    cx = random.randrange(W)
    cy = random.randrange(H)
    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)
    image = np.copy(image1)  # 念のためコピー
    image[bby1:bby2, bbx1:bbx2, :] = image2[bby1:bby2, bbx1:bbx2, :]
    label = label1 * lam + label2 * (1 - lam)
    return image, label 
Example 10
Project: gimpmaps   Author: mucximilian   File: randomize.py    GNU General Public License v2.0 5 votes vote down vote up
def random_beta(a = 5, b = 5):
    """
    Computes beta distributed value between 0 and 1 with given alpha and beta
    """ 
    
    x = random.betavariate(a, b)
    return x 
Example 11
Project: gimpmaps   Author: mucximilian   File: randomize.py    GNU General Public License v2.0 5 votes vote down vote up
def random_beta_int(x1 = -1, x2 = 1, a = 5, b = 5):
    """
    Computes beta distributed value between x1 and x2 with given alpha and beta
    """ 
    
    x = x1 + random.betavariate(a, b) * (x2 - x1)
    return x 
Example 12
Project: dirichletprocess   Author: tdhopper   File: dirichlet.py    MIT License 5 votes vote down vote up
def __call__(self):
        remaining = 1.0 - self.total_stick_used
        i = weighted_choice(self.weights + [remaining])
        if i < len(self.weights):
            return self.cache[i]
        else:
            stick_piece = betavariate(1, self.alpha) * remaining
            self.total_stick_used += stick_piece
            self.weights.append(stick_piece)
            new_value = self.base_measure()
            self.cache.append(new_value)
            return new_value 
Example 13
Project: Python-iBeacon-Scan   Author: NikNitro   File: crv_types.py    GNU General Public License v3.0 5 votes vote down vote up
def sample(self):
        return random.betavariate(self.alpha, self.beta) 
Example 14
Project: topopy   Author: maljovec   File: test_functions.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def genTorusInputSampleSet(N):
    r = 0.175
    n = 2
    x = np.zeros(N)
    y = np.zeros(N)
    z = np.zeros(N)

    # Solved the roots of f(x)=r^2 using wolfram alpha and truncate to
    # make sure we don't end up with negatives under the radical.
    # Note, changing r will affect these bounds
    minX = -0.073275
    maxX = n - minX

    for i in range(N):
        ySgn = (-1) ** random.randint(1, 2)
        zSgn = (-1) ** random.randint(1, 2)
        # x[i] = random.uniform(0, n)
        x[i] = random.uniform(minX, maxX)

        # The distribution below requires less samples, but may be
        # harder to sell in a paper, as it is clearly non-uniform,
        # whereas the sampling in y-space makes it look more uniform due
        # to the dependence on x which yields more samples closer to
        # zero, so we bias towards the more extreme values to counteract
        # this effect.

        # x[i] = random.betavariate(0.5,0.5)*(maxX-minX) + minX

        fx = torusPolyGeneratorF(x[i], n)
        minY = np.sqrt(max(-r - fx, 0))
        maxY = np.sqrt(r - fx)
        # y[i] = ySgn*(random.uniform(minY, maxY))
        y[i] = ySgn * (random.betavariate(0.5, 0.5) * (maxY - minY) + minY)
        z[i] = doubleTorus(x[i], y[i], n, r, zSgn)
    return (x, y, z)


# End https://bit.ly/2wWSJs4
############################################################################### 
Example 15
Project: gitcha-scripts   Author: yeonghoey   File: generate_dummy_data.py    MIT License 5 votes vote down vote up
def new_item_count():
    return int(random.betavariate(0.3, 0.5) * 100) 
Example 16
Project: Think-Bayes   Author: foamliu   File: thinkbayes.py    Apache License 2.0 5 votes vote down vote up
def Random(self):
        """Generates a random variate from this distribution."""
        return random.betavariate(self.alpha, self.beta) 
Example 17
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_random.py    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 18
Project: cells   Author: AlesTsurko   File: test_random.py    MIT License 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 19
Project: 02-synthesizers   Author: DistrictDataLabs   File: corruptors.py    Apache License 2.0 5 votes vote down vote up
def pick_beta(self):
        "use a beta distribution to pick positions"
        l = len(self)
        position = betavariate(5,1)*l/2 + l/2
        return round(position)