def confidence_interval_estimate(l, k, s, i, re, im, w, h, sampling_method=pure_random): """ Compute mandelbrot set area sample mean, variance and confidence interval (assuming central limit theorem). Continue simulations until interval condition is met. :param l: :param k: Minimal number of simulation to run :param s: Number of samples for Monte carlo :param i: Maximal number of iteration :param sampling_method: sampling method used :return: sample mean, sample variance and confidence interval """ x = [] s2_ = x0_ = x1_ = 0 interval = 1 it = 0 while it < k or interval >= l: a, _, _ = monte_carlo_integration(re, im, w, h, s, i, sampling_method) x.append(a) x1_ = recursive_sample_mean(a, x0_, len(x) - 1) s2_ = recursive_sample_variance(s2_, x1_, x0_, len(x) - 1) x0_ = x1_ min, max, interval = confidence_interval_ppf(x1_, np.sqrt(s2_), 0.05, len(x)) it += 1 return x1_, s2_, min, max, it
def confidence_interval_estimate_fixed(k, s, i, re, im, w, h, sampling_method=pure_random): """ Compute mandelbrot set area sample mean, variance and confidence interval (assuming central limit theorem). Interval given fixed number of simulations :param k: Fixed number of simulation to run :param s: Number of samples for Monte carlo :param i: Iteration :param sampling_method: sampling method used :return: sample mean, sample variance and confidence interval """ x = [] s2_ = x0_ = x1_ = 0 it = 0 while it < k: a, _, _ = monte_carlo_integration(re, im, w, h, s, i, sampling_method) x.append(a) x1_ = recursive_sample_mean(a, x0_, len(x) - 1) s2_ = recursive_sample_variance(s2_, x1_, x0_, len(x) - 1) x0_ = x1_ min, max, interval = confidence_interval_ppf(x1_, np.sqrt(s2_), 0.05, len(x)) it += 1 return x1_, s2_, min, max
def study_samples_convergence_single_method(s, i, re=RE, im=IM, w=WIDTH, h=HEIGHT): """ Get difference between A_js and A_is Examing which sampling method requires fewer samples to converge Get abs(A_js - A_is) for all j < i, then plot the results. """ nb_try = 50 area_stack = np.zeros((nb_try, s)) a = (re[1] - re[0]) * (im[1] - im[0]) print("Estimating area...") for tr in range(nb_try): a_is_rand, _, details_rand = monte_carlo_integration( re, im, w, h, s, i, sampling_method=pure_random) s_range = range(1, s + 1) y_rand = [] for t in s_range: count_rand = np.sum((details_rand[:t + 1] == i).astype(int)) a_rand_it = (count_rand / t) * a y_rand.append(a_rand_it) area_stack[tr] = np.array(y_rand) graphic_utils.plot_convergence_single_method(np.array(s_range), area_stack, 'Number of samples s')
def study_iteration_convergence_single_method(s, i, re=RE, im=IM, w=WIDTH, h=HEIGHT): """ Get difference between A_js and A_is Examing which sampling method requires fewer samples to converge Get abs(A_js - A_is) for all j < i, then plot the results. """ nb_try = 50 area_stack = np.zeros((nb_try, i + 1)) print("Estimating area...") for t in range(nb_try): a = (re[1] - re[0]) * (im[1] - im[0]) a_is_rand, _, details_rand = monte_carlo_integration( re, im, w, h, s, i, sampling_method=pure_random) i_range = range(i + 1) y_rand = [] for j in i_range: count_rand = np.sum((details_rand >= j).astype(int)) a_rand_js = (count_rand / s) * a y_rand.append(a_rand_js) area_stack[t] = np.array(y_rand) graphic_utils.plot_convergence_single_method( np.array(i_range), area_stack, 'Maximal number of iterations i')
def estimate_error_by_sampling(re, im, w, h, s, i, sampling_method): """ :param re: tuple of (minimal, maximal) coordinates of real axis :param im: tuple of (minimal, maximal) coordinates of imaginary axis. :param w: width of the plan :param h: height of the plan :param s: Maximal number of samples :param i: Number of iteration """ # Area of the complex plane a = (re[1] - re[0]) * (im[1] - im[0]) # Estimation of the area surface of Mandelbrot set for i iterations and s samples. a_is, _, details = monte_carlo_integration(re, im, w, h, s, i, sampling_method=sampling_method) # Estimated error for range of iterations between 0 and s. x = range(1, s + 1) y = [] # Compute error for t in x: # For an increasing number of sample j until s. count = np.sum((details[:t + 1] == i).astype(int)) a_it = ( count / t ) * a # Estimation of the area of the Mandelbrot set for i iteration and t samples. err = 100 * (abs(a_it - a_is) / abs(a_is)) # Relative error in percent y.append(err) # Error return x, y, a_is
def estimate_iteration_area_per_method(re, im, w, h, s, i): """ Compute an approximation of mandelbrot set area by maximal number of iteration for 4 sampling methods: pure random, halton sequence, latin square, orthogonal :param re: tuple of (minimal, maximal) coordinates of real axis :param im: tuple of (minimal, maximal) coordinates of imaginary axis. :param w: width of the plan :param h: height of the plan :param s: Maximum number of samples :param i: Number of iteration (should be minimum with which we have reasonable convergence) """ # Area of the complex plane a = (re[1] - re[0]) * (im[1] - im[0]) # Estimation of the area surface of Mandelbrot set for i iterations and s samples. a_is_rand, _, details_rand = monte_carlo_integration( re, im, w, h, s, i, sampling_method=pure_random) a_is_halton, _, details_halton = monte_carlo_integration( re, im, w, h, s, i, sampling_method=halton_sequence) a_is_lhs, _, details_lhs = monte_carlo_integration( re, im, w, h, s, i, sampling_method=latin_square_chaos) a_is_orth, _, details_orth = monte_carlo_integration( re, im, w, h, s, i, sampling_method=orthogonal_native) # Estimated error for range of iterations between 0 and i. i_range = range(i + 1) y_rand, y_halton, y_lhs, y_orth = [], [], [], [] # Compute error by samples for random, halton sequence and latin hypercube for j in i_range: # Get number of samples which converge for j iteration count_rand = np.sum((details_rand >= j).astype(int)) count_halton = np.sum((details_halton >= j).astype(int)) count_lhs = np.sum((details_lhs >= j).astype(int)) count_orth = np.sum((details_orth >= j).astype(int)) a_rand_js = (count_rand / s) * a a_halton_js = (count_halton / s) * a a_lhs_js = (count_lhs / s) * a a_orth_js = (count_orth / s) * a y_rand.append(a_rand_js) y_halton.append(a_halton_js) y_lhs.append(a_lhs_js) y_orth.append(a_orth_js) return i_range, y_rand, y_halton, y_lhs, y_orth
def study_convergence_mandelbrot(re=RE, im=IM, w=WIDTH, h=HEIGHT): """ Study convergence of points in complex plane. Get a list of complex number supposed to converge, and plot evolution of the function f_c(z) """ max_i = 1000 # Get sample of complex numbers and the number of iteration where they converge in mandelbrot set. _, complex_sample, details = monte_carlo_integration( re, im, w, h, 200, max_i) # Index of a sample of complex number which converge for at least max_i iterations. idx = np.random.choice(np.argwhere(details == max_i)[:, 0], 20) # Sample of complex numbers which converge for at least max_i iterations. sample = np.array(complex_sample)[idx] for val in sample: convergence(val, max_i)
def estimate_error_by_iteration(re, im, w, h, s, i, sampling_method): """ :param s: Number of samples :param i: Maximal number of iteration :param w: width of the plan :param h: height of the plan :param re: tuple of (minimal, maximal) coordinates of real axis :param im: tuple of (minimal, maximal) coordinates of imaginary axis. """ # Area of the complex plane a = (re[1] - re[0]) * (im[1] - im[0]) # Estimation of the area surface of Mandelbrot set for i iterations and s samples. a_is, _, details = monte_carlo_integration(re, im, w, h, s, i, sampling_method=sampling_method) # Estimated error for range of iterations between 1 and i. x = range(1, i + 1) y = [] # Compute error for j in x: # For each number of iteration until max_i # Get number of samples which converge for j iteration count = np.sum((details >= j).astype(int)) a_js = ( count / s ) * a # Estimation of the area of the Mandelbrot set for j iteration and s samples. err = 100 * (abs(a_js - a_is) / abs(a_is)) # Relative error in percent y.append(err) return x, y, a_is