def discretize_prior(grf, grid): """ Discretize a grf prior on a grid and return the mean vector on the grid and covariance matrix between grid nodes. Parameters ---------- grf: GRF Gaussian Random Field to discretize. grid: Grid Grid on which to discretize. Returns ------- mean_vec: GeneralizedVector covariance_mat: GeneralizedMatrix """ n_out = grf.covariance.n_out # Generate the measurment vector that corresponds to all components. S, L = get_isotopic_generalized_location( grid.points, n_out) mean_vec = grf.mean(S, L) mean_vec = GeneralizedVector.from_list(mean_vec, grid.n_points, n_out) covariance_mat = grf.covariance.K(S, S, L, L) covariance_mat = GeneralizedMatrix.from_list(covariance_mat, grid.n_points, n_out, grid.n_points, n_out) return mean_vec, covariance_mat
def variance_reduction_isotopic(self, points, S_y, L_y, noise_std=None): """ Computes the reduction in variance (all components) at location S that would be caused by observing data at generalized locations (S_y, L_y). Note that this doesn't depend on the measured data. Parameters ---------- points: (N, d) Tensor List of points at which to predict. S_y: (M, d) Tensor Spatial locations of the measurements. L_y: (M) Tensor Response indices of the measurements. noise_std: (n_out) Tensor Noise standard deviation for each response. Defaults to 0. Returns ------- variance_reduction: (N) Tensor Reduction in variance at each generalized location. """ n_pts = points.shape[0] # Generate prediction locations corrresponding to the full grid. S, L = get_isotopic_generalized_location( points, self.n_out) variance_reduction_list = self.variance_reduction( S, L, S_y, L_y, noise_std=noise_std) # Reshape to isotopic form by adding dimensions for the response # indices. variance_reduction = GeneralizedVector.from_list( variance_reduction_list, n_pts, self.n_out) return variance_reduction
def sample_isotopic(self, points): """ Sample the GRF (all components) on a list of points. Parameters ---------- points: (N, d) Tensor Spatial locations where to sample. Returns ------- sample_list: (N, p) Tensor The sampled values. """ S_iso, L_iso = get_isotopic_generalized_location(points, self.n_out) sample = self.sample(S_iso, L_iso) # Separate indices. sample_list = sample.reshape((points.shape[0], self.n_out)) return sample_list
def krig_grid(self, grid, S_y, L_y, y, noise_std=None, compute_post_cov=False): """ Predict field at some points, based on some measured data at other points. Parameters ---------- grid: Grid Regular grid of size (n1, ..., nd). S_y: (M, d) Tensor Spatial locations of the measurements. L_y: (M) Tensor Response indices of the measurements. y: (M) Tensor Measured values. noise_std: (n_out) Tensor Noise standard deviation for each response. Defaults to 0. compute_post_cov: bool If true, compute and return posterior covariance. Returns ------- mu_cond_grid: (grid.shape, p) Tensor Kriging means at each grid node. mu_cond_list: (grid.n_points*p) Tensor Kriging mean, but in list form. mu_cond_iso: (grid.n_points, p) Tensor Kriging means in isotopic list form. K_cond_list: (grid.n_points * p, grid.n_points * p) Tensor Conditional covariance matrix in heterotopic form. K_cond_iso: (grid.n_points, grid.n_points, p, p) Tensor Conditional covariance matrix in isotopic ordered form. It means that the covariance matrix at cell i can be otained by subsetting K_cond_iso[i, i, :, :]. """ # Generate prediction locations corrresponding to the full grid. S, L = get_isotopic_generalized_location( grid.points, self.n_out) if compute_post_cov: mu_cond_list, K_cond_list = self.krig( S, L, S_y, L_y, y, noise_std=noise_std, compute_post_cov=compute_post_cov) else: mu_cond_list = self.krig( S, L, S_y, L_y, y, noise_std=noise_std, compute_post_cov=compute_post_cov) # Reshape to regular grid form. Begin by adding a dimension for the # response indices. mu_cond_iso = mu_cond_list.reshape((grid.n_points, self.n_out)) # Put back in grid form. mu_cond_grid = mu_cond_iso.reshape((*grid.shape, self.n_out)) if compute_post_cov: # Reshape to isotopic form by adding dimensions for the response # indices. K_cond_iso = K_cond_list.reshape( (grid.n_points, self.n_out, grid.n_points, self.n_out)).transpose(1,2) return mu_cond_grid, mu_cond_list, mu_cond_iso, K_cond_list, K_cond_iso return mu_cond_grid
def krig_isotopic(self, points, S_y, L_y, y, noise_std=None, compute_post_var=False, compute_post_cov=False): """ Predict field at some points, based on some measured data at other points. Predicts all repsonses (isotopic). Parameters ---------- points: (N, d) Tensor List of points at which to predict. S_y: (M, d) Tensor Spatial locations of the measurements. L_y: (M) Tensor Response indices of the measurements. y: (M) Tensor Measured values. noise_std: (n_out) Tensor Noise standard deviation for each response. Defaults to 0. compute_post_var: bool If true, compute and return posterior variance (matrix) at each point. compute_post_cov: bool If true, compute and return posterior covariance. Only one of compute_post_var or compute_post_var may be avtivated. Returns ------- mu_cond_list: (N*p) Tensor Kriging mean, but in list form. mu_cond_iso: (N, p) Tensor Kriging means in isotopic list form. K_cond_list: (N * p, N * p) Tensor Conditional covariance matrix in heterotopic form. K_cond_iso: (N, N, p, p) Tensor Conditional covariance matrix in isotopic ordered form. It means that the covariance matrix at cell i can be otained by subsetting K_cond_iso[i, i, :, :]. var_cond_list: (N*p) Tensor Conditional varance at each generalized location. Only returned if specified. var_cond_iso: (N, p) Tensor Same as above, but in list form. """ n_pts = points.shape[0] # Generate prediction locations corrresponding to the full grid. S, L = get_isotopic_generalized_location( points, self.n_out) if compute_post_var: mu_cond_list, var_cond_list = self.krig( S, L, S_y, L_y, y, noise_std=noise_std, compute_post_var=True) elif compute_post_cov: mu_cond_list, K_cond_list = self.krig( S, L, S_y, L_y, y, noise_std=noise_std, compute_post_cov=True) else: mu_cond_list = self.krig( S, L, S_y, L_y, y, noise_std=noise_std) # Wrap in a GeneralizedVector. mu_cond = GeneralizedVector.from_list(mu_cond_list, n_pts, self.n_out) if compute_post_var: var_cond = GeneralizedVector.from_list(var_cond_list, n_pts, self.n_out) return mu_cond, var_cond elif compute_post_cov: K_cond = GeneralizedMatrix( K_cond_list, n_pts, self.n_out, n_pts, self.n_out) return mu_cond, K_cond return mu_cond