def evaluate(self, var): """ Generate a discrete representation of the equation for the space represented by ``var``. .. note: numexpr doesn't support gamma function """ # get gamma functions self.parameters["gamma_a_1"] = sp_gamma(self.parameters["a_1"]) self.parameters["gamma_a_2"] = sp_gamma(self.parameters["a_2"]) return RefBase.evaluate(self.equation, global_dict=self.parameters)
def _set_pattern(self, var): """ Generate a discrete representation of the equation for the space represented by ``var``. .. note: numexpr doesn't support gamma function """ # get gamma functions from scipy.special import gamma as sp_gamma self.parameters["gamma_a_1"] = sp_gamma(self.parameters["a_1"]) self.parameters["gamma_a_2"] = sp_gamma(self.parameters["a_2"]) self._pattern = numexpr.evaluate(self.equation, global_dict=self.parameters)
def testGeneralizedNormalSample(self): loc = tf.constant(3., self.dtype) scale = tf.constant(math.sqrt(3.), self.dtype) power = tf.constant(3., self.dtype) loc_v = 3. scale_v = np.sqrt(3. / sp_gamma(1. / 3.)) n = tf.constant(100000) gnormal = tfd.GeneralizedNormal(loc=loc, scale=scale, power=power, validate_args=True) samples = gnormal.sample(n, seed=test_util.test_seed()) sample_values = self.evaluate(samples) # Note that the standard error for the sample mean is ~ sigma / sqrt(n). # The sample variance similarly is dependent on sigma and n. # Thus, the tolerances below are very sensitive to number of samples # as well as the variances chosen. self.assertEqual(sample_values.shape, (100000, )) self.assertAllClose(sample_values.mean(), loc_v, atol=1e-1) self.assertAllClose(sample_values.std(), scale_v, atol=1e-1) expected_samples_shape = tf.TensorShape( [self.evaluate(n)]).concatenate( tf.TensorShape(self.evaluate(gnormal.batch_shape_tensor()))) self.assertAllEqual(expected_samples_shape, samples.shape) self.assertAllEqual(expected_samples_shape, sample_values.shape) expected_samples_shape = (tf.TensorShape( [self.evaluate(n)]).concatenate(gnormal.batch_shape)) self.assertAllEqual(expected_samples_shape, samples.shape) self.assertAllEqual(expected_samples_shape, sample_values.shape)
def apply(self, solution): corr = solution.corr err = solution.err m = solution.model cond = solution.conditions undec = solution.undec evolution = solution.evolution # Make gamma distribution gamma_mean = self.pausestop - self.pausestart gamma_var = pow(self.pauseblurwidth, 2) shape = gamma_mean**2/gamma_var scale = gamma_var/gamma_mean gamma_pdf = lambda t : 1/(sp_gamma(shape)*(scale**shape)) * t**(shape-1) * np.exp(-t/scale) gamma_vals = np.asarray([gamma_pdf(t) for t in m.t_domain() - self.pausestart if t >= 0]) sumgamma = np.sum(gamma_vals) gamma_start = next(i for i,t in enumerate(m.t_domain() - self.pausestart) if t >= 0) # Generate first part of pdf (before the pause) newcorr = np.zeros(m.t_domain().shape, dtype=corr.dtype) newerr = np.zeros(m.t_domain().shape, dtype=err.dtype) # Generate pdf after the pause for i,t in enumerate(m.t_domain()): #print(np.sum(newcorr)+np.sum(newerr)) if 0 <= t < self.pausestart: newcorr[i] = corr[i] newerr[i] = err[i] elif self.pausestart <= t: newcorr[i:] += corr[gamma_start:len(corr)-(i-gamma_start)]*gamma_vals[int(i-gamma_start)]/sumgamma newerr[i:] += err[gamma_start:len(corr)-(i-gamma_start)]*gamma_vals[int(i-gamma_start)]/sumgamma else: raise ValueError("Invalid domain") return Solution(newcorr, newerr, m, cond, undec, evolution)
def testGeneralizedNormalVariance(self): # scale will be broadcast to [[7, 7, 7], [7, 7, 7], [7, 7, 7]]. loc = np.array([[1., 2., 3.]]).T scale = [7.] power = np.array([.5, 1., 3.]) gnormal = tfd.GeneralizedNormal(loc=self.make_input(loc), scale=self.make_input(scale), power=self.make_input(power), validate_args=True) self.assertAllEqual((3, 3), gnormal.variance().shape) scale_broadcast = scale * np.ones_like(loc) reference = np.square(scale_broadcast) * (sp_gamma(3. / power) / sp_gamma(1. / power)) self.assertAllClose(reference, self.evaluate(gnormal.variance()), atol=0, rtol=1e-5) # relaxed tol for fp32 in JAX
def testGeneralizedNormalEntropy(self): loc_v = np.array([1., 1., 1.]) scale_v = np.array([[1., 2., 3.]]).T power_v = np.array([1.]) gnormal = tfd.GeneralizedNormal(loc=self.make_input(loc_v), scale=self.make_input(scale_v), power=self.make_input(power_v), validate_args=True) # scipy.sp_stats.norm cannot deal with these shapes. scale_broadcast = loc_v * scale_v expected_entropy = 1. / power_v - np.log( power_v / (2. * scale_broadcast * sp_gamma(1. / power_v))) entropy = gnormal.entropy() np.testing.assert_allclose(expected_entropy, self.evaluate(entropy)) self.assertAllEqual(self.evaluate(gnormal.batch_shape_tensor()), entropy.shape) self.assertAllEqual(self.evaluate(gnormal.batch_shape_tensor()), self.evaluate(entropy).shape) self.assertAllEqual(gnormal.batch_shape, entropy.shape) self.assertAllEqual(gnormal.batch_shape, self.evaluate(entropy).shape)
def fui(h, i): return (h**(2 * i)) / ((2**i) * sp_gamma(i + 1))
def gamma(x): """Return the gamma function.""" return sp_gamma(x)
def fui(h, i): return (h ** (2 * i)) / ((2 ** i) * sp_gamma(i + 1))