Skip to content

Commit

Permalink
add pulls gaussian curve_fit
Browse files Browse the repository at this point in the history
  • Loading branch information
luigifvr committed Apr 11, 2023
1 parent e8a7ace commit b6b0e35
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages
from scipy.stats import lognorm, norm
from scipy.optimize import curve_fit

from .observable import Observable

Expand Down Expand Up @@ -61,8 +63,8 @@ def __init__(

def process_weights(self, weights_true, weights_fake):
w_comb = np.concatenate((weights_true, weights_fake), axis=0)
self.p_low = np.percentile(w_comb[w_comb!=0], 0.1)
self.p_high = np.percentile(w_comb[w_comb!=np.inf], 99.9)
self.p_low = np.percentile(w_comb[w_comb!=0], 0.05)
self.p_high = np.percentile(w_comb[w_comb!=np.inf], 99.95)

weights_true[weights_true >= self.p_high] = self.p_high
weights_fake[weights_fake <= self.p_low] = self.p_low
Expand Down Expand Up @@ -369,8 +371,8 @@ def plot_bgen_weights(self, file: str):
x_bins = np.linspace(0,4,30)
y_bins = np.linspace(0,3,30)

fig, ax = plt.subplots(figsize=(4,3.5))
ax.hist2d(
fig, ax = plt.subplots(figsize=(4.5,4.0))
_, _, _, img = ax.hist2d(
std_log_w_bgen,
w_disc,
bins=(x_bins, y_bins),
Expand All @@ -379,6 +381,8 @@ def plot_bgen_weights(self, file: str):
density=True,
cmap="jet"
)
cb = plt.colorbar(img)
cb.set_label('norm.')
ax.set_xlabel(r"$\sigma(\log p_\text{gen})$")
ax.set_ylabel(r"$w_\text{disc}$")

Expand All @@ -389,8 +393,8 @@ def plot_bgen_weights(self, file: str):
pull = mean_w_bgen * (w_disc - 1) / std_w_bgen
low_lim = np.quantile(pull[np.isfinite(pull)], 0.0001)
high_lim = np.quantile(pull[np.isfinite(pull)], 0.9999)
lim = max(high_lim, -low_lim)
bins = np.linspace(-lim, lim, 40)
#lim = max(high_lim, -low_lim)
bins = np.linspace(low_lim, high_lim, 50)
y, _ = np.histogram(pull, bins, density=True)
self.hist_line(
ax,
Expand All @@ -400,10 +404,14 @@ def plot_bgen_weights(self, file: str):
label = "Gen",
color = self.colors[0]
)
ax.set_xlabel(r"$\mu(p_\text{gen}) (w_\text{disc} - 1) / \sigma(p_\text{gen})$")
x_fit, bins_fit, norm_param = self.fit_norm(bins, y, p0=(y.mean(), y.var()))
ax.plot(bins_fit, x_fit, label=r"$\mu={:.3f}$ $\sigma={:.3f}$".format(norm_param[0], norm_param[1]))
ax.set_xlabel(r"$(\mu(p_\text{gen}) (w_\text{disc} - 1) / \sigma(p_\text{gen}))$")
ax.set_ylabel("normalized")
ax.set_yscale("log")
#ax.set_yscale("log")
ax.set_xlim(bins[0], bins[-1])
ax.legend(loc='upper left')
#ax.set_ylim(top=2.0e1, bottom=1.0e-4)
plt.savefig(pdf, format="pdf", bbox_inches="tight")
plt.close()

Expand All @@ -412,24 +420,35 @@ def plot_bgen_weights(self, file: str):
bins = np.linspace(
np.quantile(log_pull[np.isfinite(log_pull)], 0.0001),
np.quantile(log_pull[np.isfinite(log_pull)], 0.9999),
40
50
)
y, _ = np.histogram(log_pull, bins, density=True)
self.hist_line(
ax,
bins,
y,
y_err = None,
label = "Gen",
label = "Pull",
color = self.colors[0]
)
x_fit, bins_fit, norm_param = self.fit_norm(bins, y, p0=(y.mean(), y.var()))

ax.plot(bins_fit, x_fit, label=r"$\mu={:.3f}$ ""\n"" $\sigma={:.3f}$".format(norm_param[0], norm_param[1]), color='red')
ax.set_xlabel(r"$\log w_\text{disc} / \sigma(\log p_\text{gen})$")
ax.set_ylabel("normalized")
ax.set_yscale("log")
#ax.set_yscale("log")
ax.set_xlim(bins[0], bins[-1])
#ax.set_ylim(bottom=1.0e-2, top=y.max()+5)
ax.legend(loc='upper left')
plt.savefig(pdf, format="pdf", bbox_inches="tight")
plt.close()

def fit_norm(self, bins, y, p0, points=1000):
to_fit = norm.pdf
param, cov = curve_fit(to_fit, bins[:-1], y, p0=p0)
bins_fit = np.linspace(bins[0], bins[-1], points)
pred = norm.pdf(bins_fit, loc=param[0], scale=param[1])
return pred, bins_fit, param

def plot_observables(self, file: str):
"""
Expand Down

0 comments on commit b6b0e35

Please sign in to comment.