amachine.am_random API documentation

GitLab Repo

# amachine.am_random

View Source

     1from scipy.special import softmax
     2from scipy.stats import beta
     3import numpy as np
     4import random
     5
     6_rng = np.random.default_rng()
     7
     8def resolve_rng(np_rng):
     9	global _rng
    10	return _rng if np_rng is None else np_rng
    11
    12def exp_uniform_blend(n: int, alpha: float, np_rng=None ) -> list[float]:
    13	global _rng
    14	rng = resolve_rng(np_rng)
    15	exp_probs = np.exp(-np.arange(n))
    16	exp_probs /= exp_probs.sum()
    17	uniform = np.ones(n) / n
    18	probs = ( (1 - alpha) * exp_probs + alpha * uniform)
    19	return rng.permutation( probs ).tolist()
    20
    21def beta_dist( n, a, b, np_rng=None ):
    22	global _rng
    23	rng = resolve_rng(np_rng)
    24	points = np.linspace(0, 1, n + 2 )[ 1:-1]
    25	values = beta.pdf(points, a, b)
    26	probs  = values / values.sum()
    27	return rng.permutation( probs ).to_list()
    28
    29def dirichlet_dist(n, alpha, np_rng=None ):
    30	global _rng
    31	rng = resolve_rng(np_rng)
    32	return rng.dirichlet(np.ones(n) * alpha)
    33
    34def uniform_dist( n ):
    35	return np.ones(n) / n
    36
    37def srand_global(seed: int):
    38	global _rng
    39	# _rng = np.random.default_rng(11)
    40	np.random.seed(seed)
    41	random.seed(seed)

def resolve_rng(np_rng): View Source

     9def resolve_rng(np_rng):
    10	global _rng
    11	return _rng if np_rng is None else np_rng

def exp_uniform_blend(n: int, alpha: float, np_rng=None) -> list[float]: View Source

    13def exp_uniform_blend(n: int, alpha: float, np_rng=None ) -> list[float]:
    14	global _rng
    15	rng = resolve_rng(np_rng)
    16	exp_probs = np.exp(-np.arange(n))
    17	exp_probs /= exp_probs.sum()
    18	uniform = np.ones(n) / n
    19	probs = ( (1 - alpha) * exp_probs + alpha * uniform)
    20	return rng.permutation( probs ).tolist()

def beta_dist(n, a, b, np_rng=None): View Source

    22def beta_dist( n, a, b, np_rng=None ):
    23	global _rng
    24	rng = resolve_rng(np_rng)
    25	points = np.linspace(0, 1, n + 2 )[ 1:-1]
    26	values = beta.pdf(points, a, b)
    27	probs  = values / values.sum()
    28	return rng.permutation( probs ).to_list()

def dirichlet_dist(n, alpha, np_rng=None): View Source

    30def dirichlet_dist(n, alpha, np_rng=None ):
    31	global _rng
    32	rng = resolve_rng(np_rng)
    33	return rng.dirichlet(np.ones(n) * alpha)

def uniform_dist(n): View Source

    35def uniform_dist( n ):
    36	return np.ones(n) / n

def srand_global(seed: int): View Source

    38def srand_global(seed: int):
    39	global _rng
    40	# _rng = np.random.default_rng(11)
    41	np.random.seed(seed)
    42	random.seed(seed)