You are on page 1of 2

# This is a sample Python script.

# Press ⇧F10 to execute it or replace it with your code.


# Press Double ⇧ to search everywhere for classes, files, tool windows, actions,
and settings.
import numpy as np
import scipy
import math

def beta1(a, b):


"""gamma method"""
x1 = np.random.gamma(a)
x2 = np.random.gamma(b)
return x1 / (x1 + x2)

def beta2(a, b):


""" a, b apartin N"""
n = a + b - 1

values = np.random.uniform(0, 1, n)
values = sorted(values)

return values[a - 1]

def geometricP(p):
"""pascal method"""
k = 1
j = 0

x = 0
while True:
y = np.random.binomial(k, p)
if y == 0:
x = x + 1
else:
j = j + 1
if j == k:
break

return x

def geometricI(p):
"""inverse method"""
q = 1 - p
u = np.random.uniform(0, 1, 1)[0]
x = math.ceil(np.log2(u) / np.log2(q)) - 1

return x

def stats1(numbers):
"""media de selectie di dispersia de selectie"""
avg = sum(numbers) / len(numbers)
dispersion = 0
for n in numbers:
dispersion += (n - avg) ** 2

dispersion /= (len(numbers) - 1)

return avg, dispersion

# Press the green button in the gutter to run the script.


if __name__ == '__main__':

# media de selectie si dispersie selectie, cel putin 1000


geometricNumbers1 = []
geometricNumbers2 = []
p = 0.25
for i in range(0, 100000):
geometricNumbers1.append(geometricP(p))
geometricNumbers2.append(geometricI(p))
print("Distributia geometrica")
print("metoda 1: ", stats1(geometricNumbers1))
print("metoda 2: ", stats1(geometricNumbers2))
print("teoretic: ({}, {})".format((1 - p) / p, (1 - p) / (p ** 2)))

betaNumbers1 = []
betaNumbers2 = []
a = 3
b = 5
for i in range(0, 10000):
betaNumbers1.append(beta1(a, b))
betaNumbers2.append(beta2(a, b))

print("Distributia beta")
print("metoda 1: ", stats1(betaNumbers1))
print("metoda 2: ", stats1(betaNumbers2))
print("teoretic: ({}, {})".format(a / (a + b), (a * b) / (((a + b) ** 2) * (a +
b + 1))))

You might also like