e
In [ ]:
In [1]:
def detection_efficiency(x, y):
return 1 / (1 + np.exp((x**2 / np.sqrt(y) - 1) / 0.1))
In [2]:
x, y = np.linspace(1, 2, 101), np.linspace(1, 10, 103)
X, Y = np.meshgrid(x, y, indexing="ij")
pl.pcolor(X, Y, detection_efficiency(X, Y), cmap="gray")
pl.xlim(min(x), max(x))
pl.ylim(min(y), max(y))
pl.colorbar();
In [3]:
bin_edges = np.linspace(min(y), max(y), 11)
int_comp = np.empty(len(bin_edges) - 1)
x0 = np.random.uniform(min(x), max(x), 50000)
for i in range(len(int_comp)):
y0 = np.random.uniform(bin_edges[i], bin_edges[i + 1], len(x0))
int_comp[i] = np.mean(detection_efficiency(x0, y0))
In [4]:
def plot_histogram(bin_edges, bin_heights, reluncert=None, **kwargs):
x = np.array(list(zip(bin_edges[:-1], bin_edges[1:]))).flatten()
y = np.array(list(zip(bin_heights, bin_heights))).flatten()
pl.plot(x, y, **kwargs)
if reluncert is not None:
pl.errorbar(0.5 * (bin_edges[:-1] + bin_edges[1:]), bin_heights,
yerr=bin_heights * reluncert, fmt="+", capsize=0,
color=kwargs.get("color", "k"))
In [5]:
plot_histogram(bin_edges, int_comp, color="k")
pl.ylim(0, 1);
In [6]:
true_samp = np.random.multivariate_normal([1.5, 6], [[1.0, 0.0], [0.0, 4.0]], size=5000)
true_samp = true_samp[(min(x) < true_samp[:, 0]) * (true_samp[:, 0] < max(x))
* (min(y) < true_samp[:, 1]) * (true_samp[:, 1] < max(y))]
m = np.random.rand(len(true_samp)) < detection_efficiency(true_samp[:, 0], true_samp[:, 1])
obs_samp = true_samp[m]
obs_det = detection_efficiency(obs_samp[:, 0], obs_samp[:, 1])
In [7]:
pl.pcolor(X, Y, detection_efficiency(X, Y), cmap="gray")
pl.plot(true_samp[~m, 0], true_samp[~m, 1], ".r", ms=3)
pl.plot(obs_samp[:, 0], obs_samp[:, 1], ".b", ms=3)
pl.xlim(min(x), max(x))
pl.ylim(min(y), max(y));
In [8]:
bin_areas = np.diff(bin_edges)
pl.hist(true_samp[:, 1], bin_edges, color="k", histtype="step", lw=3, alpha=0.2)
n0, _ = np.histogram(obs_samp[:, 1], bin_edges)
n1, _ = np.histogram(obs_samp[:, 1], bin_edges, weights=1.0 / obs_det)
plot_histogram(bin_edges, n0 / int_comp, 1 / np.sqrt(n0), color="k", ls="--")
plot_histogram(bin_edges, n1, 1 / np.sqrt(n0), color="k", ls=":")
In [ ]: