def linear_trans_opt(param, *args): """ Computes the Log Marginal Likelihood using standard GP regression by first performing transformation of the data set :param param: transform_mat :param args: :return: """ # Define arguments x_scatter = args[0] y_scatter = args[1] c = args[2] kernel = args[3] # Define parameters to be optimized - the matrix variables transform_mat = param # Begin transformation of the regression window xy_scatter = np.vstack( (x_scatter, y_scatter)) # Create the sample points to be rotated xy_scatter_transformed = fn.transform_array(transform_mat, xy_scatter, c) x_points_trans = xy_scatter_transformed[0] y_points_trans = xy_scatter_transformed[1] # 1. Obtain the maximum range in x and y in the transformed space - to define the regression window x_down = min(x_points_trans) x_up = max(x_points_trans) y_down = min(y_points_trans) y_up = max(y_points_trans) # --------------------- Conduct binning into transformed space - the x and y quad lengths will be different # ChangeParam quads_on_side = 20 # define the number of quads along each dimension k_mesh, y_edges, x_edges = np.histogram2d(y_points_trans, x_points_trans, bins=quads_on_side, range=[[y_down, y_up], [x_down, x_up]]) x_mesh_plot, y_mesh_plot = np.meshgrid( x_edges, y_edges) # creating mesh-grid for use x_mesh = x_mesh_plot[:-1, : -1] # Removing extra rows and columns due to edges y_mesh = y_mesh_plot[:-1, :-1] x_quad = fn.row_create(x_mesh) # Creating the rows from the mesh y_quad = fn.row_create(y_mesh) xy_quad = np.vstack((x_quad, y_quad)) k_quad = fn.row_create(k_mesh) # Start Optimization arguments = (xy_quad, k_quad, kernel) # Initialise kernel hyper-parameters - arbitrary value but should be as close to actual value as possible initial_hyperparameters = np.array([1, 1, 1, 1]) # An optimization process is embedded within another optimization process solution = scopt.minimize(fun=short_log_integrand_data, args=arguments, x0=initial_hyperparameters, method='Nelder-Mead', options={ 'xatol': 1, 'fatol': 100, 'disp': True, 'maxfev': 500 }) print('Last function evaluation is ', solution.fun) # This will be a negative value neg_log_likelihood = solution.fun # We want to minimize the mirror image return neg_log_likelihood
def linear_trans_skinny_opt(param, *args): """ Computes the Log Marginal Likelihood using standard GP regression by first performing transformation of the data set This finds the average log marginal likelihood instead of the combined log_likelhood, and will find this average while adapting to the number of quadrats in the regression window after transformation :param param: transform_mat - matrix variables to be optimized :param args: x and y coordinates of scatter points,, center, kernel type, and array containing vertices the right order :return: the average log likelihood by dividing total log likelihood with number of selected quadrats """ # Define original required arguments xy_scatter = args[0] c = args[1] kernel = args[2] vertex_array = args[ 3] # Have to be in the right order in the original mathematical space # Define parameters to be optimized - the matrix variables transform_mat = param # Begin transformation of the regression window xy_scatter_transformed = fn.transform_array(transform_mat, xy_scatter, c) x_points_trans = xy_scatter_transformed[0] y_points_trans = xy_scatter_transformed[1] # 1. Obtain the maximum range in x and y in the transformed space - to define the regression window x_down = min(x_points_trans) x_up = max(x_points_trans) y_down = min(y_points_trans) y_up = max(y_points_trans) # Conduct binning into transformed space - the x and y quad lengths will be different # ChangeParam quads_on_side = 20 # define the number of quads along each dimension k_mesh, y_edges, x_edges = np.histogram2d(y_points_trans, x_points_trans, bins=quads_on_side, range=[[y_down, y_up], [x_down, x_up]]) x_mesh_plot, y_mesh_plot = np.meshgrid( x_edges, y_edges) # creating mesh-grid for use x_mesh = x_mesh_plot[:-1, : -1] # Removing extra rows and columns due to edges y_mesh = y_mesh_plot[:-1, :-1] x_quad = fn.row_create(x_mesh) # Creating the rows from the mesh y_quad = fn.row_create(y_mesh) xy_quad = np.vstack((x_quad, y_quad)) k_quad = fn.row_create(k_mesh) # Selection of quadrats that fall inside the polygon # Transform the vertices using the same transformation matrix transformed_vertices = fn.transform_array(transform_mat, vertex_array, center) # Create polygon and polygon = mpath.Path(np.transpose(transformed_vertices)) polygon_indicator = polygon.contains_points(np.transpose(xy_quad), transform=None, radius=1.0) x_quad_polygon = x_quad[polygon_indicator] y_quad_polygon = y_quad[polygon_indicator] xy_quad_polygon = np.vstack((x_quad_polygon, y_quad_polygon)) k_quad_polygon = k_quad[polygon_indicator] # Begin Optimization using selected quadrats arguments = (xy_quad_polygon, k_quad_polygon, kernel) # Initialise kernel hyper-parameters - arbitrary value but should be as close to actual value as possible initial_hyperparameters = np.array([1, 1, 1, 1]) # An optimization process is embedded within another optimization process solution = scopt.minimize(fun=short_log_integrand_data, args=arguments, x0=initial_hyperparameters, method='Nelder-Mead', options={ 'xatol': 1, 'fatol': 100, 'disp': True, 'maxfev': 1000 }) positive_log_likelihood = solution.fun # We want to minimize the mirror image selected_quadrats_n = k_quad_polygon.size avg_positive_log_likelihood = positive_log_likelihood / selected_quadrats_n print('Last function evaluation is ', solution.fun) # This will be a negative value print('The number of selected quadrats inside polygon is', selected_quadrats_n) return avg_positive_log_likelihood
# Initialise Array containing Frobenius Norm frob_norm = np.zeros_like(element_skew) start_iteration = time.clock() # Over here, I am not trying to optimize for matrix variables, but just optimizing for the kernel for i in range(iterate_count): # initial_mat_var = np.array([mat_element[a], 0, mat_element[c], mat_element[d]]) start_iteration = time.clock() initial_mat_var = np.array([element_skew[i], 0, 0, element_skew[i]]) frob_norm[i] = fn.frob_norm(initial_mat_var) print(' ------------- Start of Current Iteration', i + 1) print('The Current Matrix Variables are', initial_mat_var) print('The Current Frobenius Norm is', frob_norm[i]) xy_scatter_transformed = fn.transform_array(initial_mat_var, xy_within_box, center) x_points_trans = xy_scatter_transformed[0] y_points_trans = xy_scatter_transformed[1] # Obtain the maximum range in x and y in the transformed space # Transform the vertices transformed_vertices = fn.transform_array(initial_mat_var, vertices, center) x_down = min(transformed_vertices[0]) x_up = max(transformed_vertices[0]) y_down = min(transformed_vertices[1]) y_up = max(transformed_vertices[1]) # ChangeParam - create histogram in transformed space before quadrat selection quads_on_side = 20 # define the number of quads along each dimension
# ------------------------------------------ End of Regression Window Selection before Transformation # ------------------------------------------ Start of Performing Transformation xy_within_box = np.vstack( (x_within_box, y_within_box)) # Create the sample points to be rotated # Initialise kernel hyperparameters initial_hyperparameters = np.array([1, 1, 1, 1]) # ChangeParam *** IMPORTANT # transform_matrix_array = np.array([0.3, 3.5, 3.5, 0.3]) # transform_matrix_array = np.array([1, 0, 0, 1]) transform_matrix_array = np.array([0.30, 0.93, 0.65, -0.23]) transformed_xy_within_box = fn.transform_array(transform_matrix_array, xy_within_box, center) x_points_trans = transformed_xy_within_box[0] y_points_trans = transformed_xy_within_box[1] vertices = np.array([[x_lower, x_lower, x_upper, x_upper], [y_lower, y_upper, y_upper, y_lower]]) print('The Original Vertices are', vertices) # Transform the vertices using the same transformation matrix transformed_vertices = fn.transform_array(transform_matrix_array, vertices, center) print('The Transformed Vertices are', transformed_vertices) # 1. Obtain the maximum range in x and y in the transformed space x_min = min(transformed_vertices[0]) x_max = max(transformed_vertices[0])
# Initialise Log Likelihood Array and Frobenius Norm array likelihood_matrix = np.zeros((alpha_array.size, beta_array.size)) frob_matrix = np.zeros((alpha_array.size, beta_array.size)) start_iteration = time.clock() # iterate for each value of alpha for i in range(alpha_array.size): for j in range(beta_array.size): transform_matrix_array = np.array( [alpha_array[i], beta_array[j], beta_array[j], alpha_array[i]]) # transform_matrix_array = np.array([0, alpha_array[i], alpha_array[i], 0]) frob_matrix[i, j] = np.sqrt(sum(transform_matrix_array**2)) # ChangeParam - Conduct the transformation transformed_xy_within_box = fn.transform_array(transform_matrix_array, xy_within_box, center) x_points_trans = transformed_xy_within_box[0] y_points_trans = transformed_xy_within_box[1] # 1. Obtain the maximum range in x and y in the transformed space x_min = min(x_points_trans) x_max = max(x_points_trans) y_min = min(y_points_trans) y_max = max(y_points_trans) print('x range is', x_max - x_min) print('y range is', y_max - y_min) # Display regression window size at each iteration # ChangeParam quads_on_side = 10 # define the number of quads along each dimension
# Create index of the maximum log likelihood opt_index = np.argmax(log_likelihood_array) max_likelihood = log_likelihood_array[opt_index] opt_matrix_variables = matrix_variables_mat[opt_index, :] print('The optimal points are at', matrix_variables_mat) print('The globally-optimal matrix variables are', opt_matrix_variables) print('The globally-optimal log marginal likelihood is', max_likelihood) print('The kernel is ', ker) print('The year is', year) # Perform the transformation using the optimized matrix variables xy_within_box = np.vstack((x_within_box, y_within_box)) # Perform transformation using the optimal matrix variables that were tabulated beforehand transformed_xy = fn.transform_array(opt_matrix_variables, xy_within_box, center) # Split coordinates for plotting transformed_x = transformed_xy[0] transformed_y = transformed_xy[1] # This is the plot including all scatter points in Brazil scatter_plot_fig = plt.figure() scatter_plot = scatter_plot_fig.add_subplot(111) scatter_plot.scatter(x_within_box, y_within_box, marker='o', color='red', s=0.3) scatter_plot.scatter(x_points, y_points, marker='o', color='black', s=0.3) scatter_plot.scatter(transformed_x,
elif transform == 'special': transform_matrix_array = np.array( [-0.30117594, 0.92893405, -0.65028918, -0.2277159]) elif transform == 'line': transform_matrix_array = np.array([1, 1, 1, 1]) else: transform_matrix_array = np.array([1, 0, 0, 1]) frob_norm = fn.frob_norm(transform_matrix_array) print('The optimal Transformation Matrix Variables are', transform_matrix_array) print('The optimal Frobenius Norm is', frob_norm) # ChangeParam - Conduct the transformation about the center of the regression window transformed_xy_within_box = fn.transform_array(transform_matrix_array, xy_within_box, center) x_points_trans = transformed_xy_within_box[0] y_points_trans = transformed_xy_within_box[1] # Obtain the maximum range in x and y in the transformed space - to define the regression window # This is to maximise the number of selected quadrats x_min = min(x_points_trans) x_max = max(x_points_trans) y_min = min(y_points_trans) y_max = max(y_points_trans) # First create a regression window with 20 x 20 quadrats before selecting the relevant quadrats # ChangeParam quads_on_side = 30 # define the number of quads along each dimension for the large regression window k_mesh, y_edges, x_edges = np.histogram2d(y_points_trans, x_points_trans,