©2022 Raazesh Sainudiin, Benny Avelin. Attribution 4.0 International (CC BY 4.0)
from Utils import linConGen
import numpy as np
import matplotlib.pyplot as plt
from Utils import discrete_histogram
m,a,b = (2**32, 1103515245,12345)
seed = 1
np.array(linConGen(m,a,b,seed,10))/m
def random():
"""Generates one random sample from the uniform [0,1] distribution"""
global seed
seed = linConGen(m,a,b,seed,2)[1]
return seed/m
unif_x = [random() for i in range(1000)]
_=plt.hist(unif_x,bins=10)
def unif_to_bernoulli(x,p):
from math import floor
return floor(x+p)
bernoulli_x = [unif_to_bernoulli(random(),0.9) for i in range(1000)]
discrete_histogram(bernoulli_x,normed=True)
p = np.array([0.1,0.2,0.5,0.2])
p_cumsum = np.cumsum(p)
uniform_x = np.array([random() for i in range(10000)])
#np.argmax(uniform_x-p_cumsum,)
xp = (uniform_x.reshape(-1,1)-p_cumsum.reshape(1,-1)) # (100,1), (1,4) -> (100,4)
xp.shape
discrete_histogram(np.argmax(xp <= 0,axis=1),normed=True)
-- To shuffle an array a of n elements (indices 0..n-1):
for i from nā1 downto 1 do
j ā random integer such that 0 ⤠j ⤠i
exchange a[j] and a[i]
def randint(b):
"""Producing random integers between 0 and b inclusive"""
u = random()
from math import floor
return floor(u*(b+1))
discrete_histogram([randint(10) for i in range(100)])
arr = np.arange(0,100)
n = len(arr)
for i in range(n-1,0,-1):
j = randint(i) # Random integer between 0 and i inclusive
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
arr
def random_shuffle(arr):
"""Shuffles an array in place"""
n = len(arr)
for i in range(n-1,0,-1):
j = randint(i) # Random integer between 0 and i inclusive
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
return arr
aa = np.arange(10)
random_shuffle(aa)
aa
See chapter 10 in All of Statistics
from math import floor
X = np.array([floor(random()+0.5) for i in range(100)])
Y = np.array([floor(random()+0.2) for i in range(100)])
XY = np.concatenate([X,Y])
def compute_diff(arr):
return np.abs(np.mean(arr[:100])-np.mean(arr[100:]))
compute_diff(XY)
differences = np.array([compute_diff(random_shuffle(XY)) for i in range(10000)])
plt.hist(differences)