def check_versus_numpy(self, x_, axis, biased, keepdims): x_ = np.asarray(x_) x = tf1.placeholder_with_default( x_, shape=x_.shape if self.use_static_shape else None) var = _reduce_variance(x, axis=axis, biased=biased, keepdims=keepdims) np_var = np.var(x_, axis=axis, ddof=0 if biased else 1, keepdims=keepdims) if self.use_static_shape: self.assertAllEqual(np_var.shape, var.shape) var_ = self.evaluate(var) # We will mask below, which changes shape, so check shape explicitly here. self.assertAllEqual(np_var.shape, var_.shape) # We get NaN when we divide by zero due to the size being the same as ddof nan_mask = np.isnan(np_var) if nan_mask.any(): self.assertTrue(np.isnan(var_[nan_mask]).all()) self.assertAllClose(np_var[~nan_mask], var_[~nan_mask], atol=0, rtol=0.02)
def _finalize_for_one_state(chain_ndims, chain_variances): """Calculates R-hat for one group of Markov chains.""" # using notation from Brooks and Gelman (1998), # n := num samples / chain; m := number of chains n = chain_variances.num_samples shape = chain_variances.mean.shape m = tf.cast( functools.reduce((lambda x, y: x * y), (shape[:chain_ndims])), n.dtype) # b/n is the between-chain variance (the variance of the chain means) b_div_n = diagnostic._reduce_variance( # pylint:disable=protected-access tf.convert_to_tensor(chain_variances.mean), axis=tf.range(chain_ndims), biased=False) # W is the within sequence variance (the mean of the chain variances) sum_of_chain_squared_residuals = tf.reduce_sum( chain_variances.sum_squared_residuals, axis=tf.range(chain_ndims)) w = sum_of_chain_squared_residuals / (m * (n - 1)) # the `true_variance_estimate` is denoted as sigma^2_+ in the 1998 paper true_variance_estimate = ((n - 1) / n) * w + b_div_n return ( (m + 1.) / m) * true_variance_estimate / w - (n - 1.) / (m * n)
def check_versus_numpy(self, x_, axis, biased, keepdims): with self.cached_session(): x_ = np.asarray(x_) x = tf.placeholder_with_default( input=x_, shape=x_.shape if self.use_static_shape else None) var = _reduce_variance( x, axis=axis, biased=biased, keepdims=keepdims) np_var = np.var(x_, axis=axis, ddof=0 if biased else 1, keepdims=keepdims) if self.use_static_shape: self.assertAllEqual(np_var.shape, var.shape) var_ = self.evaluate(var) # We will mask below, which changes shape, so check shape explicitly here. self.assertAllEqual(np_var.shape, var_.shape) # We get NaN when we divide by zero due to the size being the same as ddof nan_mask = np.isnan(np_var) if nan_mask.any(): self.assertTrue(np.isnan(var_[nan_mask]).all()) self.assertAllClose(np_var[~nan_mask], var_[~nan_mask], atol=0, rtol=0.02)