Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@copy_docstring(Laplace.randomise)
def randomise(self, value):
self.check_inputs(value)
if self._stored_gaussian is None:
unif_rv1 = random()
unif_rv2 = random()
self._stored_gaussian = np.sqrt(- 2 * np.log(unif_rv1)) * np.sin(2 * np.pi * unif_rv2)
standard_normal = np.sqrt(- 2 * np.log(unif_rv1)) * np.cos(2 * np.pi * unif_rv2)
else:
standard_normal = self._stored_gaussian
self._stored_gaussian = None
return standard_normal * self._scale + value
@copy_docstring(Binary.check_inputs)
def check_inputs(self, value):
super().check_inputs(value)
if self._utility_values is None:
raise ValueError("Utility function must be set")
if self._normalising_constant is None:
self._normalising_constant = self._build_normalising_constant()
if not isinstance(value, str):
raise TypeError("Value to be randomised must be a string")
if value not in self._domain_values:
raise ValueError("Value \"%s\" not in domain" % value)
return True
@copy_docstring(Laplace.set_sensitivity)
def set_sensitivity(self, sensitivity):
if not isinstance(sensitivity, Real):
raise TypeError("Sensitivity must be numeric")
if sensitivity <= 0:
raise ValueError("Sensitivity must be strictly positive")
self._sensitivity = float(sensitivity)
return self
@copy_docstring(Laplace.get_bias)
def get_bias(self, value):
return 0.0
@copy_docstring(Laplace.get_bias)
def get_bias(self, value):
return 0.0
@copy_docstring(Laplace.get_variance)
def get_variance(self, value):
self.check_inputs(0)
return self._scale ** 2
@copy_docstring(Laplace.get_variance)
def get_variance(self, value):
self.check_inputs(value)
if self._scale is None:
self._scale = self._find_scale()
variance = value**2
variance -= (np.exp((self._lower_bound - value) / self._scale) * (self._lower_bound ** 2)
+ np.exp((value - self._upper_bound) / self._scale) * (self._upper_bound ** 2)) / 2
variance += self._scale * (self._lower_bound * np.exp((self._lower_bound - value) / self._scale)
- self._upper_bound * np.exp((value - self._upper_bound) / self._scale))
variance += (self._scale ** 2) * (2 - np.exp((self._lower_bound - value) / self._scale)
- np.exp((value - self._upper_bound) / self._scale))
variance /= 1 - (np.exp(-(value - self._lower_bound) / self._scale)
+ np.exp(-(self._upper_bound - value) / self._scale)) / 2