Skip to content
Snippets Groups Projects
Commit b5d47950 authored by Jack Christopher Hutchinson Rolph's avatar Jack Christopher Hutchinson Rolph
Browse files

Update LossFunctions.py

parent 602e550e
Branches
Tags
No related merge requests found
...@@ -14,6 +14,7 @@ class BinnedLH: ...@@ -14,6 +14,7 @@ class BinnedLH:
def __init__(self, f, bin_centres, counts, bw): def __init__(self, f, bin_centres, counts, bw):
self.f = f self.f = f
self.x = bin_centres self.x = bin_centres
self.len_x = len(self.x)
self.dx = bw self.dx = bw
self.counts = counts self.counts = counts
self.N = np.sum(counts) self.N = np.sum(counts)
...@@ -22,6 +23,8 @@ class BinnedLH: ...@@ -22,6 +23,8 @@ class BinnedLH:
self.func_code = FakeFuncCode(f, dock=True) self.func_code = FakeFuncCode(f, dock=True)
self.n_calls=0 self.n_calls=0
self.eps = epsilon() self.eps = epsilon()
self.mask = (self.counts>0)
def __call__(self, *arg): def __call__(self, *arg):
...@@ -30,16 +33,18 @@ class BinnedLH: ...@@ -30,16 +33,18 @@ class BinnedLH:
y_hat = self.f(self.x, *arg) y_hat = self.f(self.x, *arg)
y_hat = np.nan_to_num(y_hat, nan=self.eps, posinf=self.eps, neginf=self.eps)
y_hat = np.where(y_hat<self.eps, self.eps, y_hat)
E = y_hat*self.N*self.dx E = y_hat*self.dx
h = self.counts h = self.counts
mask = (h>0) mask = self.mask
E = E[mask]
h = h[mask]
nlogL= np.zeros(self.len_x)
nlogL[mask] = h[mask]*(np.log(E[mask]) - np.log(h[mask])) + (h[mask]-E[mask])
nlogL[~mask] = -E[~mask]
nlogL = -np.sum(h*(np.log(E) - np.log(h)) + (h-E)) nlogL = -np.sum(nlogL)
self.n_calls+=1 self.n_calls+=1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment