def ellipse_test(capacity): from matplotlib.patches import Ellipse _, sp = plt.subplots(1, 3, figsize=(16, 6)) for cor, subplot in zip([0, 0.5, 0.9], sp): dots = np.random.multivariate_normal((0, 0), [[1, cor], [cor, 1]], capacity).T vx = nc.dispersion_exp(dots[0]) vy = nc.dispersion_exp(dots[1]) angle = np.arctan(2 * np.sqrt(vx) * np.sqrt(vy) * cor / (vx - vy)) / 2 w = 5 * np.sqrt(vx * (np.cos(angle))**2 + cor * np.sqrt(vx) * np.sqrt(vy) * np.sin(2 * angle) + vy * (np.sin(angle))**2) h = 5 * np.sqrt(vx * (np.sin(angle))**2 - cor * np.sqrt(vx) * np.sqrt(vy) * np.sin(2 * angle) + vy * (np.cos(angle))**2) ell = Ellipse(xy=(nc.sample_mean(dots[0]), nc.sample_mean(dots[1])), width=w, height=h, angle=np.rad2deg(angle)) draw_ellipse("normal distribution %s = %.1f" % (r'$\rho$', cor), ell, dots, subplot) plt.show()
def complex_dist_test(capacity): def f(cap): return 0.9 * np.random.multivariate_normal((0, 0), [[1, 0.9], [0.9, 1]], cap).T + \ 0.1 * np.random.multivariate_normal((0, 0), [[100, -0.9], [-0.9, 100]], cap).T data = np.array([[ c_cor(f(capacity)) for c_cor in [scc.pearson, scc.spearman, scc.selective_quadrant] ] for _ in range(1000)]).T cell_text = [ list( map(lambda x: "%.4f" % x, [ nc.sample_mean(data[i]), nc.sample_mean(list(map(lambda x: x**2, data[i]))), nc.dispersion_exp(data[i]) ])) for i in range(3) ] draw_table("complex normal distribution n = %i" % capacity, np.array(cell_text).T)
def simple_dist_test(capacity): for cov in [0, 0.5, 0.9]: data = [] for _ in range(1000): sample = np.random.multivariate_normal( (0, 0), [[1, cov], [cov, 1]], capacity).T data += [[ c_cor(sample) for c_cor in [scc.pearson, scc.spearman, scc.selective_quadrant] ]] data = np.array(data).T cell_text = [ list( map(lambda x: "%.4f" % x, [ nc.sample_mean(data[i]), nc.sample_mean(list(map(lambda x: x**2, data[i]))), nc.dispersion_exp(data[i]) ])) for i in range(3) ] draw_table("normal distribution n = %i, p = %.2f" % (capacity, cov), np.array(cell_text).T)
def pearson(samples): ex = nc.sample_mean(samples[0]) ey = nc.sample_mean(samples[1]) sdx = np.sqrt(nc.dispersion_exp(samples[0])) sdy = np.sqrt(nc.dispersion_exp(samples[1])) return (nc.sample_mean(np.array(samples[0])*np.array(samples[1])) - ex * ey) / (sdx * sdy)
plt.plot(dots, nl, color='red') plt.title("%s, capacity:%s" % ("normal distribution", sum(n_list))) plt.ylabel("fx") plt.xlabel("X") plt.grid() plt.legend(('теоретические частоты', 'реальные частоты')) plt.show() if __name__ == "__main__": k = 7 delta = 0.5 for n in [10, 100]: x_i = sorted(dist.normal(n)) m0 = nc.sample_mean(x_i) d = nc.dispersion_exp(x_i) print("%.2f %.2f" % (m0, d)) d_i = [[-np.inf, m0 - delta * 2.5], [m0 - delta * 2.5, m0 - delta * 1.5], [m0 - delta * 1.5, m0 - delta * 0.5], [m0 - delta * 0.5, m0 + delta * 0.5], [m0 + delta * 0.5, m0 + delta * 1.5], [m0 + delta * 1.5, m0 + delta * 2.5], [m0 + delta * 2.5, np.inf]] n_i = [0] * k for x in x_i: for i in range(k): if d_i[i][0] < x <= d_i[i][1]: n_i[i] += 1 p_i = [integrate.quad(den.normal, i[0], i[1])[0] for i in d_i] npi = n * np.array(p_i)
import scipy.stats as st import numpy as np from Lab1.distribution import distribution as d from Lab2.num_char import numerical_characteristics as nc if __name__ == "__main__": alpha = 0.05 for n in [20, 100]: print("=========%i==========" % n) x = d.normal(n) m = nc.sample_mean(x) s = np.sqrt(nc.dispersion_exp(x)) print("m: %.2f, %.2f" % (m - s * (st.t.ppf(1 - alpha / 2, n - 1)) / np.sqrt(n - 1), m + s * (st.t.ppf(1 - alpha / 2, n - 1)) / np.sqrt(n - 1))) print("sigma: %.2f, %.2f" % (s * np.sqrt(n) / np.sqrt(st.chi2.ppf(1 - alpha / 2, n - 1)), s * np.sqrt(n) / np.sqrt(st.chi2.ppf(alpha / 2, n - 1)))) print("m asymptotic :%.2f, %.2f" % (m - st.norm.ppf(1 - alpha / 2) / np.sqrt(n), m + st.norm.ppf(1 - alpha / 2) / np.sqrt(n))) e = (sum(list(map(lambda el: (el - m)**4, x))) / n) / s**4 - 3 print("sigma asymptotic: %.2f, %.2f" % (s / np.sqrt(1 + st.norm.ppf(1 - alpha / 2) * np.sqrt( (e + 2) / n)), s / np.sqrt(1 - st.norm.ppf(1 - alpha / 2) * np.sqrt((e + 2) / n))))
from Lab1.distribution import distribution as d from Lab2.num_char import numerical_characteristics as nc if __name__ == "__main__": for dist in [d.uniform, d.poisson, d.laplace, d.normal, d.cauchy]: print(dist.__name__) for capacity in [10, 100, 1000]: for num_char in [ nc.sample_mean, nc.median, nc.halfsum_extreme, nc.halfsum_quartile, nc.truncated_mean ]: print(num_char.__name__ + " cap: " + str(capacity)) data = [num_char(sorted(dist(capacity))) for _ in range(1000)] print("%.6f" % nc.sample_mean(data)) print("%.6f" % nc.dispersion_exp(data)) print('==================================')