def test_jitter_synthetic( jitter_method, float_type, ctx=mx.Context('cpu') ) -> None: # Initialize problem parameters batch_size = 1 prediction_length = 50 context_length = 5 num_samples = 3 # Initialize test data to generate Gaussian Process from lb = -5 ub = 5 dx = (ub - lb) / (prediction_length - 1) x_test = nd.arange(lb, ub + dx, dx, ctx=ctx, dtype=float_type).reshape( -1, 1 ) x_test = nd.tile(x_test, reps=(batch_size, 1, 1)) # Define the GP hyper parameters amplitude = nd.ones((batch_size, 1, 1), ctx=ctx, dtype=float_type) length_scale = math.sqrt(0.4) * nd.ones_like(amplitude) sigma = math.sqrt(1e-5) * nd.ones_like(amplitude) # Instantiate desired kernel object and compute kernel matrix rbf_kernel = RBFKernel(amplitude, length_scale) # Generate samples from 0 mean Gaussian process with RBF Kernel and plot it gp = GaussianProcess( sigma=sigma, kernel=rbf_kernel, prediction_length=prediction_length, context_length=context_length, num_samples=num_samples, ctx=ctx, float_type=float_type, jitter_method=jitter_method, sample_noise=False, # Returns sample without noise ) # Generate training set on subset of interval using the sine function x_train = nd.array([-4, -3, -2, -1, 1], ctx=ctx, dtype=float_type).reshape( context_length, 1 ) x_train = nd.tile(x_train, reps=(batch_size, 1, 1)) y_train = nd.sin(x_train.squeeze(axis=2)) # Predict exact GP using the GP predictive mean and covariance using the same fixed hyper-parameters samples, predictive_mean, predictive_std = gp.exact_inference( x_train, y_train, x_test ) assert ( np.sum(np.isnan(samples.asnumpy())) == 0 ), 'NaNs in predictive samples!'
def hybrid_forward( self, F, past_target: Tensor, past_time_feat: Tensor, future_time_feat: Tensor, feat_static_cat: Tensor, ) -> Tensor: """ Parameters ---------- F A module that can either refer to the Symbol API or the NDArray API in MXNet. past_target Training time series values of shape (batch_size, context_length). past_time_feat Training features of shape (batch_size, context_length, num_features). future_time_feat Test features of shape (batch_size, prediction_length, num_features). feat_static_cat Time series indices of shape (batch_size, 1). Returns ------- Tensor GP samples of shape (batch_size, num_samples, prediction_length). """ kernel_args, sigma = self.get_gp_params(F, past_target, past_time_feat, feat_static_cat) gp = GaussianProcess( sigma=sigma, kernel=self.kernel_output.kernel(kernel_args), context_length=self.context_length, prediction_length=self.prediction_length, num_samples=self.num_samples, ctx=self.ctx, float_type=self.float_type, max_iter_jitter=self.max_iter_jitter, jitter_method=self.jitter_method, sample_noise=self.sample_noise, ) samples, _, _ = gp.exact_inference( past_time_feat, past_target, future_time_feat ) # Shape (batch_size, prediction_length, num_samples) return samples.swapaxes(1, 2)
def hybrid_forward( self, F, past_target: Tensor, past_time_feat: Tensor, feat_static_cat: Tensor, ) -> Tensor: """ Parameters ---------- F A module that can either refer to the Symbol API or the NDArray API in MXNet. past_target Training time series values of shape (batch_size, context_length). past_time_feat Training features of shape (batch_size, context_length, num_features). feat_static_cat Time series indices of shape (batch_size, 1). Returns ------- Tensor GP loss of shape (batch_size, 1) """ kernel_args, sigma = self.get_gp_params(F, past_target, past_time_feat, feat_static_cat) kernel = self.kernel_output.kernel(kernel_args) gp = GaussianProcess( sigma=sigma, kernel=kernel, context_length=self.context_length, ctx=self.ctx, float_type=self.float_type, max_iter_jitter=self.max_iter_jitter, jitter_method=self.jitter_method, ) return gp.log_prob(past_time_feat, past_target)
def test_inference(gp_params, mean_exact, std_exact, x_full, y_train) -> None: # Initialize problem parameters tol = 1e-2 num_samples = 100 context_length = y_train.shape[1] prediction_length = mean_exact.shape[1] # Extract training and test set x_train = x_full[:, :context_length, :] x_test = x_full[:, context_length : context_length + prediction_length, :] amplitude = gp_params[:, 0, :].expand_dims(axis=2) length_scale = gp_params[:, 1, :].expand_dims(axis=2) sigma = gp_params[:, 2, :].expand_dims(axis=2) # Instantiate RBFKernel with its hyper-parameters kernel = RBFKernel(amplitude, length_scale) # Instantiate gp_inference object and hybridize gp = GaussianProcess( sigma=sigma, kernel=kernel, context_length=context_length, prediction_length=prediction_length, num_samples=num_samples, float_type=np.float32, ) # Compute predictive mean and covariance _, mean, std = gp.exact_inference(x_train, y_train, x_test) # This test compares to the predictive mean and std generated from MATLAB's fitrgp, which # outputs the sample with the noise, i.e. adds :math:`sigma^2` to the diagonal of # the predictive covariance matrix. assert relative_error(mean, mean_exact) <= tol assert relative_error(std, std_exact) <= tol
def main(): # Initialize problem parameters batch_size = 1 prediction_length = 50 context_length = 5 axis = [-5, 5, -3, 3] float_type = np.float64 ctx = mx.Context("gpu") num_samples = 3 ts_idx = 0 # Initialize test data to generate Gaussian Process from lb = -5 ub = 5 dx = (ub - lb) / (prediction_length - 1) x_test = nd.arange(lb, ub + dx, dx, ctx=ctx, dtype=float_type).reshape(-1, 1) x_test = nd.tile(x_test, reps=(batch_size, 1, 1)) # Define the GP hyper parameters amplitude = nd.ones((batch_size, 1, 1), ctx=ctx, dtype=float_type) length_scale = math.sqrt(0.4) * nd.ones_like(amplitude) sigma = math.sqrt(1e-5) * nd.ones_like(amplitude) # Instantiate desired kernel object and compute kernel matrix rbf_kernel = RBFKernel(amplitude, length_scale) # Generate samples from 0 mean Gaussian process with RBF Kernel and plot it gp = GaussianProcess( sigma=sigma, kernel=rbf_kernel, prediction_length=prediction_length, context_length=context_length, num_samples=num_samples, ctx=ctx, float_type=float_type, sample_noise=False, # Returns sample without noise ) mean = nd.zeros((batch_size, prediction_length), ctx=ctx, dtype=float_type) covariance = rbf_kernel.kernel_matrix(x_test, x_test) gp.plot(x_test=x_test, samples=gp.sample(mean, covariance), ts_idx=ts_idx) # Generate training set on subset of interval using the sine function x_train = nd.array([-4, -3, -2, -1, 1], ctx=ctx, dtype=float_type).reshape(context_length, 1) x_train = nd.tile(x_train, reps=(batch_size, 1, 1)) y_train = nd.sin(x_train.squeeze(axis=2)) # Predict exact GP using the GP predictive mean and covariance using the same fixed hyper-parameters samples, predictive_mean, predictive_std = gp.exact_inference( x_train, y_train, x_test) assert (np.sum(np.isnan( samples.asnumpy())) == 0), "NaNs in predictive samples!" gp.plot( x_train=x_train, y_train=y_train, x_test=x_test, ts_idx=ts_idx, mean=predictive_mean, std=predictive_std, samples=samples, axis=axis, )