# ------------------------------------------------------ # Create a regular square grid in 2 dims. my_grid = TriangularGrid(31) print("Working on an equilateral triangular grid with {} nodes.".format( my_grid.n_points)) # Discretize the GRF on a grid and be done with it. # From now on we only consider locatoins on the grid. my_discrete_grf = DiscreteGRF.from_model(myGRF, my_grid) # ------------------------------------------------------ # Sample and plot # ------------------------------------------------------ # Sample all components at all locations. sample = my_discrete_grf.sample() plot_grid_values(my_grid, sample) # From now on, we will consider the drawn sample as ground truth. # --------------------------------------------------------------- ground_truth = sample # Save for reproducibility. np.save("ground_truth.npy", ground_truth.numpy()) # Use it to declare the data feed. noise_std = torch.tensor([0.1, 0.1]) # Noise distribution lower_chol = psd_safe_cholesky(torch.diag(noise_std**2)) noise_distr = MultivariateNormal(loc=torch.zeros(n_out), scale_tril=lower_chol) def data_feed(node_ind):
# Specify mean function mean = ConstantMean([0.0, 5.0]) # Create the GRF. myGRF = GRF(mean, covariance) # Create an equilateral triangular grid in 2 dims. # Number of respones. my_grid = TriangularGrid(40) my_square_grid = SquareGrid(50, 2) my_discrete_grf = DiscreteGRF.from_model(myGRF, my_grid) # Sample and plot. sample = my_discrete_grf.sample() plot_grid_values(my_grid, sample) # Sample and plot. sample = my_discrete_grf.sample() plot_grid_values(my_grid, sample) # Sample the continuous version and compare. sample_cont = myGRF.sample_isotopic(my_grid.points) plot_grid_values(my_grid, sample_cont) # Observe some data. S_y = torch.tensor([[0.2, 0.1], [0.2, 0.2], [0.2, 0.3], [0.2, 0.4], [0.2, 0.5], [0.2, 0.6], [0.2, 0.7], [0.2, 0.8], [0.2, 0.9], [0.2, 1.0], [0.6, 0.5]]) L_y = torch.tensor([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]) y = torch.tensor(11 * [-6]).float()
# ------------------------------------------------------ # Create a regular square grid in 2 dims. my_grid = TriangularGrid(31) print("Working on an equilateral triangular grid with {} nodes.".format( my_grid.n_points)) # Discretize the GRF on a grid and be done with it. # From now on we only consider locatoins on the grid. my_discrete_grf = DiscreteGRF.from_model(myGRF, my_grid) # ------------------------------------------------------ # Sample and plot # ------------------------------------------------------ # Sample all components at all locations. sample = my_discrete_grf.sample() plot_grid_values(my_grid, sample) # From now on, we will consider the drawn sample as ground truth. # --------------------------------------------------------------- ground_truth = sample # Use it to declare the data feed. noise_std = torch.tensor([0.1, 0.1]) # Noise distribution lower_chol = psd_safe_cholesky(torch.diag(noise_std**2)) noise_distr = MultivariateNormal(loc=torch.zeros(n_out), scale_tril=lower_chol) def data_feed(node_ind): noise_realization = noise_distr.sample() return ground_truth[node_ind] + noise_realization
# Number of respones. my_grid = TriangularGrid(70) my_square_grid = SquareGrid(50, 2) my_discrete_grf = DiscreteGRF.from_model(myGRF, my_grid) # Observe some data. S_y = torch.tensor([[0.2, 0.1], [0.2, 0.2], [0.2, 0.3], [0.2, 0.4], [0.2, 0.5], [0.2, 0.6], [0.2, 0.7], [0.2, 0.8], [0.2, 0.9], [0.2, 1.0], [0.6, 0.5]]) L_y = torch.tensor([0, 0, 0, 0, 0, 1, 1, 0 ,0 ,0, 0]) y = torch.tensor(11*[-6]).float() # Since we are working with a discrete GRF, we can only observa data at grid # nodes. Hence get the ones corresponding to the measured data. S_y_inds = my_grid.get_closest(S_y) # Compute the covariance reduction that would result from observing at those # generalized locations. cov_reduction = my_discrete_grf.compute_cov_reduction(S_y_inds, L_y, noise_std=0.05) # Extract the diagonals. var_reduction = torch.diagonal( (torch.diagonal(cov_reduction.isotopic, dim1=0, dim2=1).T), dim1=1, dim2=2) plot_grid_values(my_grid, var_reduction, S_y, L_y, cmap="proba")
# The argument specified the number of cells along 1 dimension, hence total # size of the grid is roughly the square of this number. my_grid = TriangularGrid(21) print("Working on an equilateral triangular grid with {} nodes.".format( my_grid.n_points)) # Discretize the GRF on a grid and be done with it. # From now on we only consider locations on the grid. my_discrete_grf = DiscreteGRF.from_model(myGRF, my_grid) # ------------------------------------------------------ # Sample and plot # ------------------------------------------------------ # Sample all components at all locations. sample = my_discrete_grf.sample() plot_grid_values(my_grid, sample) # ------------------------------------------------------ # Observe some data. # ------------------------------------------------------ # Data observations must be specified by a so-called generalized location. # A generalized location is a couple of vectors (S, L). The first vector # specifies WHERE the observations have been made, whereas the second vector # indicates WHICH component was measured at that location (remember we are # considering multivariate GRFs). # # Example consider S = [[0, 0], [-1, -1]] and L = [0, 8]. # Then, the generalized location (S, L) describes a set of observations that # consists of one observation of the 0-th component of the field at the points # (0,0) and one observation of the 8-th component of the field at (-1, -1).
L_y, y, noise_std=0.05, compute_post_cov=True) mu_cond_sq, K_cond_sq = myGRF.krig_isotopic(my_square_grid.points, S_y, L_y, y, noise_std=0.05, compute_post_cov=True) # Plot. from meslas.plotting import plot_grid_values, plot_grid_probas plot_grid_values(my_grid, mu_cond, S_y, L_y) plot_grid_values(my_square_grid, mu_cond_sq, S_y, L_y) # Try the variance part. mu_cond, var_cond = myGRF.krig_isotopic(my_grid.points, S_y, L_y, y, noise_std=0.05, compute_post_var=True) plot_grid_values(my_grid, var_cond, S_y, L_y, cmap="proba") variance_reduction = myGRF.variance_reduction_isotopic(my_grid.points, S_y, L_y,