array_binom_manual = np.zeros(10**4) array_binom_standard = np.zeros(10**4) for i in range(10**4): alpha = np.random.uniform() r = 0 while alpha > 0: alpha -= get_p(N, p, r) r += 1 array_binom_manual[i] = r-1 array_binom_standard[i] = np.random.binomial(N, p) D_m = np.var(array_binom_manual) M_m = np.mean(array_binom_manual) D_s = np.var(array_binom_standard) M_s = np.mean(array_binom_standard) print(tabulate([["M", M_m, M_s, get_expected_value(N, p)], ["D", D_m, D_s, get_variance(N, p)]], headers=["Characteristics", "Manual", "Standard", "Theoretical"])) plots_functions.draw_hist(array_binom_standard, "Binomial")
p = 0.5 array_geom_manual = run_first_alg(p, 10**5) array_geom_direct = run_second_alg(p, 10**6) array_geom_log = run_third_alg(p, 10**5) D_m = np.var(array_geom_manual) M_m = np.mean(array_geom_manual) D_d = np.var(array_geom_direct) M_d = np.mean(array_geom_direct) D_l = np.var(array_geom_log) M_l = np.mean(array_geom_log) print(M_m, D_m) print(M_d, D_d) print(M_l, D_l) plots_functions.draw_hist(array_geom_manual, "Geometric, manual") plots_functions.draw_hist(array_geom_direct, "Geometric, direct") plots_functions.draw_hist(array_geom_log, "Geometric, log") print( tabulate([["M", M_m, M_d, M_l, get_expected_value(p)], ["D", D_m, D_d, D_l, get_variance(p)]], headers=[ "Characteristics", "Cumulative", "Direct", "Log", "Theoretical" ]))
def get_num_BoxMuller(): return np.sqrt(-2 * np.log(np.random.uniform())) * np.cos( 2 * np.pi * np.random.uniform()) def run_bm_alg(num_loop): variables = np.zeros(num_loop) for i in range(num_loop): variables[i] = get_num_BoxMuller() return variables variables_c = run_central_alg(10**5) variables_bm = run_bm_alg(10**5) plots_functions.draw_hist(variables_c) plots_functions.draw_hist(variables_bm) M_c = np.mean(variables_c) D_c = np.var(variables_c) M_bm = np.mean(variables_bm) D_bm = np.var(variables_bm) print( tabulate( [["M", M_c, M_bm, get_expected_value(0)], ["D", D_c, D_bm, get_variance(1)]], headers=["Characteristics", "Central", "Box-Muller", "Theoretical"]))
def get_variance(r_low, r_up): n = r_up - r_low + 1 return (n**2 - 1) / 12 r_low = 1 r_up = 100 array_uniform = np.zeros(10**6) for i in range(10**6): array_uniform[i] = get_number_uniform(r_low, r_up) D = np.var(array_uniform) M = np.mean(array_uniform) plots_functions.draw_hist(array_uniform) print( tabulate([[ "M", M, abs(M - get_expected_value(r_low, r_up)), get_expected_value(r_low, r_up) ], ["D", D, abs(D - get_variance(r_low, r_up)), get_variance(r_low, r_up)]], headers=["", "Experimental", "Inaccuracy", "Theoretical"]))
if r == 0: return np.exp(-mu) else: return get_p(mu, r - 1) * mu / r def run_alg1(mu, num_loop: int) -> np.array: variables = np.zeros(num_loop) for i in range(num_loop): alpha = np.random.uniform() r = 0 while alpha > 0: alpha -= get_p(mu, r) r += 1 variables[i] = r - 1 return variables mu = 10 variables_1 = run_alg1(mu, 10**5) variables_2 = run_alg2(mu, 10**5) plots_functions.draw_hist(variables_1) print( tabulate([["M", mu, mu, "10.0"], ["D", mu, mu, "10.0"]], headers=[ "Characteristics", "Cumulative", "Alternative", "Theoretical" ]))
r = 0 while alpha > 0: alpha -= get_p(N, p, r) r += 1 #print(get_p_binom(N, p, r)) array_binom_manual[i] = r - 1 array_binom_standard[i] = np.random.binomial(N, p) D_m = np.var(array_binom_manual) M_m = np.mean(array_binom_manual) D_s = np.var(array_binom_standard) M_s = np.mean(array_binom_standard) print( tabulate([["M", M_m, M_s, get_expected_value(N, p)], ["D", D_m, D_s, get_variance(N, p)]], headers=["Characteristics", "Manual", "Standard", "Theoretical"])) plots_functions.draw_hist(array_binom_standard) # test get_p_binom array_test = np.zeros(N) for i in range(N): array_test[i] = get_p_binom(N, p, i) print(array_test[i]) print("test sum = ", sum(array_test))
return np.sqrt(-2 * np.log(np.random.uniform())) * np.cos( 2 * np.pi * np.random.uniform()) def run_bm_alg(num_loop): variables = np.zeros(num_loop) for i in range(num_loop): variables[i] = get_num_BoxMuller() return variables if __name__ == '__main__': variables_c = run_central_alg(10**5) variables_bm = run_bm_alg(10**5) plots_functions.draw_hist(variables_c, "Normal, central") plots_functions.draw_hist(variables_bm, "Normal, Box-Muller") M_c = np.mean(variables_c) D_c = np.var(variables_c) M_bm = np.mean(variables_bm) D_bm = np.var(variables_bm) print( tabulate([["M", M_c, M_bm, get_expected_value(0)], ["D", D_c, D_bm, get_variance(1)]], headers=[ "Characteristics", "Central", "Box-Muller", "Theoretical" ]))
standard_deviation = np.zeros(len(testsTypes)) for k in range(len(testsTypes)): n = testsTypes[k] u = np.random.rand(n) expected_value[k] = sum(u)/n variance[k] = sum((u - expected_value[k])**2)/n standard_deviation[k] = math.sqrt(variance[k]) correlation = np.zeros(n) x_data = np.zeros(n) for i in range(1, n): correlation[i] = sum((u[:(n-i)] - expected_value[k])*(u[i:] - expected_value[k]))/sum((u - expected_value[k])**2) x_data[i] = i draw_plot(x_data, correlation, 1) plt.show(block=True) plots_functions.draw_hist(u, "Uniform") header = ["n", "characteristics", "experimental data", "theoretical data", "difference"] table = [[testsTypes[0], "M", expected_value[0], 0.5, abs(0.5-expected_value[0])], [testsTypes[0], "D", variance[0], 0.08333, abs(0.08333-variance[0])]] for i in range(1, len(testsTypes)): table.append([testsTypes[i], "M", expected_value[i], 0.5, abs(0.5-expected_value[i])]) table.append([testsTypes[i], "D", variance[i], 0.08333, abs(0.08333-variance[i])]) print(tabulate(table, header, tablefmt="simple"))
return (r_low + r_up) / 2 def get_variance(r_low, r_up): n = r_up - r_low + 1 return (n**2 - 1) / 12 r_low = 1 r_up = 100 array_uniform = np.zeros(10**6) for i in range(10**6): array_uniform[i] = get_number_uniform(r_low, r_up) D = np.var(array_uniform) M = np.mean(array_uniform) plots_functions.draw_hist(array_uniform, "Uniform") print( tabulate([[ "M", M, abs(M - get_expected_value(r_low, r_up)), get_expected_value(r_low, r_up) ], ["D", D, abs(D - get_variance(r_low, r_up)), get_variance(r_low, r_up)]], headers=["", "Experimental", "Inaccuracy", "Theoretical"]))
return np.exp(-mu) else: return get_p(mu, r - 1) * mu / r def run_alg1(mu, num_loop: int) -> np.array: variables = np.zeros(num_loop) for i in range(num_loop): alpha = np.random.uniform() r = 0 while alpha > 0: alpha -= get_p(mu, r) r += 1 variables[i] = r - 1 return variables mu = 10 variables_1 = run_alg1(mu, 10**5) variables_2 = run_alg2(mu, 10**5) plots_functions.draw_hist(variables_1, "Poisson, cumulative") plots_functions.draw_hist(variables_2, "Poisson, alternative") print( tabulate([["M", mu, mu, "10.0"], ["D", mu, mu, "10.0"]], headers=[ "Characteristics", "Cumulative", "Alternative", "Theoretical" ]))
def get_num(a, b): return (b - a) * np.random.uniform() + a def get_expected_value(a, b): return (a+b)/2 def get_variance(a, b): return (b-a)**2/12 a = 1 b = 100 array_uniform = np.zeros(10**6) for i in range(10**6): array_uniform[i] = get_num(a, b) D = np.var(array_uniform) M = np.mean(array_uniform) print(tabulate([["M", M, abs(M - get_expected_value(a, b)), get_expected_value(a, b)], ["D", D, abs(D - get_variance(a, b)), get_variance(a, b)]], headers=["", "Experimental", "Inaccuracy", "Theoretical"])) plots_functions.draw_hist(array_uniform, "Unifrom continuous")
return 0 def get_variance(N): return N / (N - 2) def get_random_num(N): return get_norm() / np.sqrt(get_chi(N) / N) def run_alg(num_loop, N): variables = np.zeros(num_loop) for i in range(num_loop): variables[i] = get_random_num(N) return variables N = 10 variables_ = run_alg(10**5, N) plots_functions.draw_hist(variables_, "Student") D = np.var(variables_) M = np.mean(variables_) print( tabulate([["M", M, get_expected_value()], ["D", D, get_variance(N)]], headers=["", "Experimental", "Theoretical"]))
D[k] = sum((u - M[k])**2) / n S[k] = math.sqrt(D[k]) #print(M, D) K = np.zeros(n) x_data = np.zeros(n) for i in range(1, n): K[i] = sum((u[:(n - i)] - M[k]) * (u[i:] - M[k])) / sum((u - M[k])**2) x_data[i] = i draw_plot(x_data, K, 1) plt.show(block=True) plots_functions.draw_hist(u) header = [ "n", "characteristics", "experimental data", "theoretical data", "difference" ] table = [[testsTypes[0], "M", M[0], 0.5, abs(0.5 - M[0])], [testsTypes[0], "D", D[0], 0.08333, abs(0.08333 - D[0])]] for i in range(1, len(testsTypes)): table.append([testsTypes[i], "M", M[i], 0.5, abs(0.5 - M[i])]) table.append([testsTypes[i], "D", D[i], 0.08333, abs(0.08333 - D[i])]) print(tabulate(table, header, tablefmt="simple"))
import numpy as np import plots_functions from tabulate import tabulate def get_random_num(beta): return -beta*np.log(np.random.uniform()) def run_alg(num_loop, beta): variables = np.zeros(num_loop) for i in range(num_loop): variables[i] = get_random_num(beta) return variables variables_ = run_alg(10**5, 1) plots_functions.draw_hist(variables_, "Exponential") D = np.var(variables_) M = np.mean(variables_) print(tabulate([["M", M, 1], ["D", D, 1]], headers=["", "Experimental", "Theoretical"]))
def get_p(p, r): alpha = 1 / np.log(p) return -alpha*(1-p)**r/r def run_alg(p, num_loop): variables = np.zeros(num_loop) for i in range(num_loop): r = 1 uniform_rand = np.random.uniform() while uniform_rand > 0: uniform_rand -= get_p(p, r) r += 1 variables[i] = r - 1 return variables p = 0.5 variables_ = run_alg(p, 10**5) plots_functions.draw_hist(variables_, "Logarithmic") D_m = np.var(variables_) M_m = np.mean(variables_) print(tabulate([["M", M_m, M_m-get_expected_value(p), get_expected_value(p)], ["D", D_m, D_m - get_variance(p), get_variance(p)]], headers=["Characteristics", "Manual", "Difference", "Theoretical"]))
def get_random_num(N): variable = 0 for i in range(N): variable += norm.get_num_CentralTh()**2 return variable def run_alg(num_loop, N): variables = np.zeros(num_loop) for i in range(num_loop): variables[i] = get_random_num(N) return variables if __name__ == '__main__': N = 10 variables_ = run_alg(10**5, N) plots_functions.draw_hist(variables_, "Chi") D = np.var(variables_) M = np.mean(variables_) print( tabulate( [["M", M, get_expected_value(N)], ["D", D, get_variance(N)]], headers=["", "Experimental", "Theoretical"]))