Skip to content

Commit

Permalink
fix: minor conv fixes, removed class level caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-eschle committed Oct 8, 2023
1 parent d29d69f commit c9132ca
Showing 1 changed file with 29 additions and 44 deletions.
73 changes: 29 additions & 44 deletions zfit_physics/models/pdf_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,36 @@ def __init__(
# func = lambda x: func.unnormalized_pdf(x=x)
# kernel = lambda x: kernel.unnormalized_pdf(x=x)

self._grid_points = None # true vars # callable func of reco - true vars
self._func_values = None
self.conv_limits = limits
self._ndraws = ndraws
self._experimental_pdf_normalized = experimental_pdf_normalized

@z.function
def _unnormalized_pdf(self, x):
limits = self.conv_limits
# area = limits.area() # new spaces
area = limits.rect_area()[0] # new spaces

samples = self._grid_points
func_values = self._func_values
# if func_values is None:
if True:
# create sample for numerical integral
lower, upper = limits.rect_limits
lower = z.convert_to_tensor(lower, dtype=self.dtype)
upper = z.convert_to_tensor(upper, dtype=self.dtype)
samples_normed = tfp.mcmc.sample_halton_sequence(
dim=limits.n_obs,
num_results=self._ndraws,
dtype=self.dtype,
randomized=False,
)
samples = samples_normed * (upper - lower) + lower # samples is [0, 1], stretch it
samples = zfit.Data.from_tensor(obs=limits, tensor=samples)
self._grid_points = samples

func_values = self.pdfs[0].pdf(samples, norm=False) # func of true vars
self._func_values = func_values
# create sample for numerical integral
lower, upper = limits.rect_limits
lower = z.convert_to_tensor(lower, dtype=self.dtype)
upper = z.convert_to_tensor(upper, dtype=self.dtype)
samples_normed = tfp.mcmc.sample_halton_sequence(
dim=limits.n_obs,
num_results=self._ndraws,
dtype=self.dtype,
randomized=False,
)
samples = samples_normed * (upper - lower) + lower # samples is [0, 1], stretch it
samples = zfit.Data.from_tensor(obs=limits, tensor=samples)

func_values = self.pdfs[0].pdf(samples, norm=False) # func of true vars

return tf.map_fn(
lambda xi: area * tf.reduce_mean(func_values * self.pdfs[1].pdf(xi - samples.value(), norm=False)),
x.value(),
)
# func of reco vars

@zfit.supports(norm=True)
@z.function
def _pdf(self, x, norm_range):
if not self._experimental_pdf_normalized:
Expand All @@ -99,26 +90,20 @@ def _pdf(self, x, norm_range):
# area = limits.area() # new spaces
area = limits.rect_area()[0] # new spaces

samples = self._grid_points
func_values = self._func_values
# if func_values is None:
if True:
# create sample for numerical integral
lower, upper = limits.rect_limits
lower = z.convert_to_tensor(lower, dtype=self.dtype)
upper = z.convert_to_tensor(upper, dtype=self.dtype)
samples_normed = tfp.mcmc.sample_halton_sequence(
dim=limits.n_obs,
num_results=self._ndraws,
dtype=self.dtype,
randomized=False,
)
samples = samples_normed * (upper - lower) + lower # samples is [0, 1], stretch it
samples = zfit.Data.from_tensor(obs=limits, tensor=samples)
self._grid_points = samples

func_values = self.pdfs[0].pdf(samples) # func of true vars
self._func_values = func_values
# create sample for numerical integral
lower, upper = limits.rect_limits
lower = z.convert_to_tensor(lower, dtype=self.dtype)
upper = z.convert_to_tensor(upper, dtype=self.dtype)
samples_normed = tfp.mcmc.sample_halton_sequence(
dim=limits.n_obs,
num_results=self._ndraws,
dtype=self.dtype,
randomized=False,
)
samples = samples_normed * (upper - lower) + lower # samples is [0, 1], stretch it
samples = zfit.Data.from_tensor(obs=limits, tensor=samples)

func_values = self.pdfs[0].pdf(samples) # func of true vars

return tf.map_fn(
lambda xi: area * tf.reduce_mean(func_values * self.pdfs[1].pdf(xi - samples.value())),
Expand Down

0 comments on commit c9132ca

Please sign in to comment.