Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_params2(nameadd=""):
mu2 = zfit.Parameter("mu25" + nameadd, z.to_real(mu_true) - 0.2, mu_true - 1., mu_true + 1.)
sigma2 = zfit.Parameter("sigma25" + nameadd, z.to_real(sigma_true) - 0.3, sigma_true - 2., sigma_true + 2.)
return mu2, sigma2
def _unnormalized_pdf(self, x):
mu = self.params['mu']
sigma = self.params['sigma']
x = z.unstack_x(x)
return z.exp(-z.square((x - mu) / sigma))
def test_param_func():
param1 = Parameter('param1', 1.)
param2 = Parameter('param2', 2.)
param3 = Parameter('param3', 3., floating=False)
param4 = Parameter('param4', 4.)
a = z.math.log(3. * param1) * tf.square(param2) - param3
func = SimpleFunc(func=lambda self, x: a * x, obs=obs1)
new_func = param4 * func
new_func_equivalent = func * param4
result1 = new_func.func(x=rnd_test_values).numpy()
result1_equivalent = new_func_equivalent.func(x=rnd_test_values).numpy()
result2 = func.func(x=rnd_test_values) * param4
np.testing.assert_array_equal(result1, result2)
np.testing.assert_array_equal(result1_equivalent, result2)
def at_limit(self) -> tf.Tensor:
"""If the value is at the limit (or over it).
The precision is up to 1e-5 relative.
Returns:
`tf.Tensor`: Boolean `tf.Tensor` that tells whether the value is at the limits.
"""
if not self.has_limits:
return tf.constant(False)
# Adding a slight tolerance to make sure we're not tricked by numerics
at_lower = z.unstable.less_equal(self.value(), self.lower + (tf.math.abs(self.lower * 1e-5)))
at_upper = z.unstable.greater_equal(self.value(), self.upper - (tf.math.abs(self.upper * 1e-5)))
return z.unstable.logical_or(at_lower, at_upper)
if rect_limits_are_any(limit1) or rect_limits_are_any(limit2):
return True
try:
lower1, upper1 = limit1.rect_limits_np
lower2, upper2 = limit2.rect_limits_np
except CannotConvertToNumpyError:
if not allow_graph:
raise IllegalInGraphModeError(
"Cannot use equality in graph mode, e.g. inside a `tf.function` decorated "
"function. To retrieve a symbolic Tensor, use `.equal(..., allow_graph=True)`")
else:
lower1, upper1 = limit1.rect_limits
lower2, upper2 = limit2.rect_limits
lower_le = z.unstable.reduce_all(z.unstable.less_equal(lower1, lower2), axis=-1)
upper_le = z.unstable.reduce_all(z.unstable.less_equal(upper1, upper2), axis=-1)
rect_limits_le = z.unstable.logical_and(lower_le, upper_le)
# if both are functional, they have to coincide
if not (limit1.has_rect_limits or limit2.has_rect_limits):
funcs_equal = limit1.limit_fn == limit2.limit_fn
# if one is functional, one is rect: the bigger one can be rect
elif not limit1.has_rect_limits and limit2.has_rect_limits:
funcs_equal = True
else:
funcs_equal = limit1.limit_fn == limit2.limit_fn
return z.unstable.logical_and(rect_limits_le, funcs_equal)
def _unnormalized_pdf(self, x): # NOT _pdf, as the normalization range can differ
pdfs = self.pdfs
fracs = self.params.values()
probs = [pdf.pdf(x) * frac for pdf, frac in zip(pdfs, fracs)]
prob = functools.reduce(operator.add, probs)
# prob = tf.math.accumulate_n([pdf.pdf(x) * frac for pdf, frac in zip(pdfs, fracs)])
return z.convert_to_tensor(prob)