def optimisation_demo(): """ Demonstrate construction of an SPDE model on a tringular mesh on a sphere. The mesh is constructed as a subdivided icosahedron. Observations are simulated and a latent variable model is learnt. """ import matplotlib.pyplot as plt from advanced_standard.linalg import sparse from advanced_standard.utilities import plot_tools print "############" print "# Setup #" print "############" my_spde = SphereMeshSPDE(level=4, project_onto_sphere=True) print "number of latent variables:" print my_spde.n_latent_variables() print "############" print "# Q matrix #" print "############" Q_parameters = np.log(np.array([2., 1.0])) print "Q_parameters:", Q_parameters Q = my_spde.build_Q(Q_parameters=Q_parameters, alpha=2) #dQdp0 = my_spde.build_dQdp(Q_parameters = np.log(np.array([1., 0.5])), alpha = 2, parameter_index = 0) #dQdp1 = my_spde.build_dQdp(Q_parameters = np.log(np.array([1., 0.5])), alpha = 2, parameter_index = 1) print "############" print "# Q sample #" print "############" c = sparse.sample_mvn(Q, 1) print "############" print "# Obs gen #" print "############" number_of_observations = 25000 #2592 test_points = np.random.uniform(-1, 1, (number_of_observations, 3)) standard_error = 1.0 Q_e = scipy.sparse.eye(number_of_observations) * (1.0 / standard_error**2) observation_error = sparse.sample_mvn(Q_e, 1) print "############" print "# A matrix #" print "############" A = my_spde.build_A(test_points) truth = A * c #y = np.atleast_2d(truth + observation_error).T y = truth + observation_error print "############" print "# solving #" print "############" Q_increment = A.T * Q_e * A i_increment = A.T * Q_e * y import advanced_standard.linalg.optimisation as optimisation Qkwargs = {'alpha': 2, 'sparse_format': 'csc'} # interval = 1e-5 # # cost_function = optimisation.cost_function(Q_parameters=Q_parameters, # i_c = np.zeros( (Q.shape[0],1) ), # i_increment= i_increment, # Q_increment=Q_increment, # Q_function = my_spde.build_Q, # dQ_function = my_spde.build_dQdp, set_point_mode = 'fixed', # fixed_set_point = np.zeros( (Q.shape[0],1) ), # observation_loglik=None, # **Qkwargs) # # vost_function = optimisation.cost_function(Q_parameters=Q_parameters +np.array([0.0, interval]), # i_c = np.zeros( (Q.shape[0],1) ), # i_increment= i_increment, # Q_increment=Q_increment, # Q_function = my_spde.build_Q, # dQ_function = my_spde.build_dQdp, set_point_mode = 'fixed', # fixed_set_point = np.zeros( (Q.shape[0],1) ), # observation_loglik=None, # **Qkwargs) # # dcost_function = optimisation.cost_function_derivative(Q_parameters=Q_parameters, # i_c = np.zeros( (Q.shape[0],1) ), # i_increment= i_increment, # Q_increment=Q_increment, # Q_function = my_spde.build_Q, # dQ_function = my_spde.build_dQdp, set_point_mode = 'fixed', # fixed_set_point = np.zeros( (Q.shape[0],1) ), # **Qkwargs) # # # print (vost_function - cost_function) / interval # # print dcost_function # print cost_function Q_init = np.log(np.array([2.0, np.pi / 4])) mu_c = np.zeros((Q.shape[0], 1)) i_increment = i_increment Q_increment = Q_increment Q_function = my_spde.build_Q dQ_function = my_spde.build_dQdp set_point_mode = 'fixed' fixed_set_point = np.zeros((Q.shape[0], 1)) observation_loglik = None opt_args = (mu_c, i_increment, Q_increment, Q_function, dQ_function, set_point_mode, fixed_set_point, observation_loglik, Qkwargs) print scipy.optimize.check_grad(optimisation.cost_function, optimisation.cost_function_derivative, Q_init, *opt_args) def opt_printer(x): print x fit = scipy.optimize.minimize(optimisation.cost_function, Q_init, args=opt_args, method='BFGS', jac=optimisation.cost_function_derivative, tol=None, callback=opt_printer, options={ 'disp': False, 'gtol': 1e-04, 'eps': 1e-08, 'return_all': False, 'maxiter': 20 }) print fit fitted_hyperparamters = fit['x'] new_Q = Q_function(Q_parameters=fitted_hyperparamters, **Qkwargs) Q_estimate = sparse.compute_Q_u(Q_e, A, new_Q) i_estimate = sparse.compute_i_u( y, Q_e, A, np.zeros((my_spde.n_latent_variables(), 1))) c_estimate = sparse.compute_mu_u(i_estimate, Q_estimate) reconstruction = A * c_estimate print "############" print "# plotting #" print "############" vmin = -6 vmax = 6 plt.figure() vertices_coords_2d = cartesian_to_polar(my_spde.triangulation.vertices) plot_tools.scatter2d(vertices_coords_2d[:, 1] * 180 / np.pi, vertices_coords_2d[:, 0] * 180 / np.pi, cs=c.ravel(), vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=truth.ravel(), vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=np.asarray(y).ravel(), vmax=vmax, vmin=vmin) plt.figure() vertices_coords_2d = cartesian_to_polar(my_spde.triangulation.vertices) plot_tools.scatter2d(vertices_coords_2d[:, 1] * 180 / np.pi, vertices_coords_2d[:, 0] * 180 / np.pi, cs=c_estimate.ravel(), vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=reconstruction.ravel(), vmax=vmax, vmin=vmin) plt.show()
def spde_demo(): """ Demonstrate construction of an SPDE model on a tringular mesh on a sphere. The mesh is constructed as a subdivided icosahedron. Observations are simulated and a latent variable model is learnt. """ import matplotlib.pyplot as plt from advanced_standard.linalg import sparse from advanced_standard.utilities import plot_tools print "############" print "# Setup #" print "############" my_spde = SphereMeshSPDE(level=4, project_onto_sphere=True) print "number of latent variables:" print my_spde.n_latent_variables() print "############" print "# Q matrix #" print "############" Q = my_spde.build_Q(Q_parameters=np.log(np.array([2., 0.5])), alpha=2) #dQdp0 = my_spde.build_dQdp(Q_parameters = np.log(np.array([1., 0.5])), alpha = 2, parameter_index = 0) #dQdp1 = my_spde.build_dQdp(Q_parameters = np.log(np.array([1., 0.5])), alpha = 2, parameter_index = 1) print "############" print "# Q sample #" print "############" c = sparse.sample_mvn(Q, 1) print "############" print "# Obs gen #" print "############" number_of_observations = 2592 test_points = np.random.uniform(-1, 1, (number_of_observations, 3)) standard_error = 0.5 Q_e = scipy.sparse.eye(number_of_observations) * (1.0 / standard_error**2) observation_error = sparse.sample_mvn(Q_e, 1) print "############" print "# A matrix #" print "############" A = my_spde.build_A(test_points) truth = A * c y = truth + observation_error print "############" print "# solving #" print "############" Q_estimate = sparse.compute_Q_u(Q_e, A, Q) i_estimate = sparse.compute_i_u(y, Q_e, A, np.zeros((my_spde.n_latent_variables()))) c_estimate = sparse.compute_mu_u(i_estimate, Q_estimate) reconstruction = A * c_estimate print "############" print "# plotting #" print "############" vmin = -3 vmax = 3 plt.figure() vertices_coords_2d = cartesian_to_polar(my_spde.triangulation.vertices) plot_tools.scatter2d(vertices_coords_2d[:, 1] * 180 / np.pi, vertices_coords_2d[:, 0] * 180 / np.pi, cs=c, vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=truth, vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=y, vmax=vmax, vmin=vmin) plt.figure() vertices_coords_2d = cartesian_to_polar(my_spde.triangulation.vertices) plot_tools.scatter2d(vertices_coords_2d[:, 1] * 180 / np.pi, vertices_coords_2d[:, 0] * 180 / np.pi, cs=c_estimate.ravel(), vmax=vmax, vmin=vmin) plt.figure() test_point_coords_2d = cartesian_to_polar(test_points) plot_tools.scatter2d(test_point_coords_2d[:, 1] * 180 / np.pi, test_point_coords_2d[:, 0] * 180 / np.pi, cs=reconstruction.ravel(), vmax=vmax, vmin=vmin) plt.show()