def test_batch_exec_2(self): from scipy.stats import multivariate_normal def pdf(X, Y): std = 1 ndim_y = Y.shape[1] return multivariate_normal.pdf(Y, mean=np.zeros(ndim_y), cov=np.eye(ndim_y) * std**2) n_queries = 8 * 10**4 X = np.ones((n_queries, 2)) * 2 Y = np.stack([ np.linspace(-3, 3, num=n_queries), np.linspace(-3, 3, num=n_queries) ], axis=-1) p_true = pdf(X, Y) p_batched = execute_batch_async_pdf(pdf, X, Y, batch_size=10000, n_jobs=8) self.assertLessEqual(np.mean((p_true - p_batched)**2), 0.00001)
def cdf(self, X, Y): """ Predicts the conditional cumulative probability p(Y<=y|X=x). Requires the model to be fitted. Args: X: numpy array to be conditioned on - shape: (n_samples, n_dim_x) Y: numpy array of y targets - shape: (n_samples, n_dim_y) Returns: conditional cumulative probability p(Y<=y|X=x) - numpy array of shape (n_query_samples, ) """ assert self.fitted, "model must be fitted to compute likelihood score" X, Y = self._handle_input_dimensionality(X, Y) n_samples = X.shape[0] if n_samples > MULTIPROC_THRESHOLD: execute_batch_async_pdf(self.sm_kde.cdf, Y, X, n_jobs=self.n_jobs) else: return self.sm_kde.cdf(endog_predict=Y, exog_predict=X)
def test_batch_exec_1(self): def pdf(X, Y): return Y[:, 0] n_queries = 10**3 X = np.ones((n_queries, 2)) * 2 Y = np.stack([ np.linspace(-3, 3, num=n_queries), np.linspace(-3, 3, num=n_queries) ], axis=-1) p_true = pdf(X, Y) p_batched = execute_batch_async_pdf(pdf, X, Y, batch_size=10000) self.assertLessEqual(np.mean((p_true - p_batched)**2), 0.00001)
def log_pdf(self, X, Y): """ Predicts the conditional log-probability log p(y|x). Requires the model to be fitted. Args: X: numpy array to be conditioned on - shape: (n_samples, n_dim_x) Y: numpy array of y targets - shape: (n_samples, n_dim_y) Returns: conditional log-probability log p(y|x) - numpy array of shape (n_query_samples, ) """ X, Y = self._handle_input_dimensionality(X, Y, fitting=True) n_samples = X.shape[0] if n_samples >= _MULTIPROC_THRESHOLD: return execute_batch_async_pdf(self._log_pdf, X, Y, n_jobs=self.n_jobs) else: return self._log_pdf(X, Y)
def pdf(self, X, Y): """ Predicts the conditional density p(y|x). Requires the model to be fitted. Args: X: numpy array to be conditioned on - shape: (n_samples, n_dim_x) Y: numpy array of y targets - shape: (n_samples, n_dim_y) Returns: conditional probability density p(y|x) - numpy array of shape (n_query_samples, ) """ assert self.fitted, "model must be fitted for predictions" X, Y = self._handle_input_dimensionality(X, Y) n_samples = X.shape[0] if n_samples >= MULTIPROC_THRESHOLD: return execute_batch_async_pdf(self._pdf, X, Y, n_jobs=self.n_jobs) else: return self._pdf(X, Y)
def pdf(self, X, Y): """ Predicts the conditional likelihood p(y|x). Requires the model to be fitted. Args: X: numpy array to be conditioned on - shape: (n_samples, n_dim_x) Y: numpy array of y targets - shape: (n_samples, n_dim_y) Returns: conditional likelihood p(y|x) - numpy array of shape (n_query_samples, ) """ X, Y = self._handle_input_dimensionality(X, Y) n_samples = X.shape[0] if n_samples >= MULTIPROC_THRESHOLD: return execute_batch_async_pdf(self.sm_kde.pdf, Y, X, n_jobs=self.n_jobs) else: return self.sm_kde.pdf(endog_predict=Y, exog_predict=X)