Exemplo n.º 1
0
def test_numerical_confidence_interval_values():
    sigma = 1
    n_sigma = 10
    n_bins_x = 100
    step_width_mu = 0.05
    mu_min = 0
    mu_max = 8
    cl = 0.90

    x_bins = np.linspace(-n_sigma * sigma, n_sigma * sigma, n_bins_x, endpoint=True)
    mu_bins = np.linspace(
        mu_min, mu_max, int(mu_max / step_width_mu) + 1, endpoint=True
    )

    rng = np.random.RandomState(seed=0)

    dist = {
        mu: scipy.stats.norm.rvs(loc=mu, scale=sigma, size=2500, random_state=rng)
        for mu in mu_bins
    }

    acceptance_intervals = fc_construct_acceptance_intervals(dist, x_bins, cl)

    lower_limit_num, upper_limit_num, _ = fc_get_limits(
        mu_bins, x_bins, acceptance_intervals
    )

    fc_fix_limits(lower_limit_num, upper_limit_num)

    x_measured = 1.7
    upper_limit = fc_find_limit(x_measured, upper_limit_num, mu_bins)

    # Value taken from Table X in the Feldman and Cousins paper.
    assert_allclose(upper_limit, 3.34, atol=0.1)
Exemplo n.º 2
0
def test_numerical_confidence_interval_pdfs():
    background = 3.0
    step_width_mu = 0.005
    mu_min = 0
    mu_max = 15
    n_bins_x = 50
    cl = 0.90

    x_bins = np.arange(0, n_bins_x)
    mu_bins = np.linspace(
        mu_min, mu_max, int(mu_max / step_width_mu) + 1, endpoint=True
    )

    matrix = [scipy.stats.poisson(mu + background).pmf(x_bins) for mu in mu_bins]

    acceptance_intervals = fc_construct_acceptance_intervals_pdfs(matrix, cl)

    lower_limit_num, upper_limit_num, _ = fc_get_limits(
        mu_bins, x_bins, acceptance_intervals
    )

    fc_fix_limits(lower_limit_num, upper_limit_num)

    x_measured = 6
    upper_limit = fc_find_limit(x_measured, upper_limit_num, mu_bins)
    lower_limit = fc_find_limit(x_measured, lower_limit_num, mu_bins)

    # Values are taken from Table IV in the Feldman and Cousins paper.
    assert_allclose(upper_limit, 8.47, atol=0.01)
    assert_allclose(lower_limit, 0.15, atol=0.01)

    # A value which is not inside the x axis range should raise an exception
    with pytest.raises(ValueError):
        fc_find_limit(51, upper_limit_num, mu_bins)

    # Calculate the average upper limit. The upper limit calculated here is
    # only defined for a small x range, so limit the x bins here so the
    # calculation of the average limit is meaningful.
    average_upper_limit = fc_find_average_upper_limit(
        x_bins, matrix, upper_limit_num, mu_bins
    )

    # Values are taken from Table XII in the Feldman and Cousins paper.
    # A higher accuracy would require a higher mu_max, which would increase
    # the computation time.
    assert_allclose(average_upper_limit, 4.42, atol=0.1)
Exemplo n.º 3
0
mu_bins = np.linspace(mu_min,
                      mu_max,
                      int(mu_max / step_width_mu + 1),
                      endpoint=True)

matrix = [
    dist / sum(dist)
    for dist in (norm(loc=mu, scale=sigma).pdf(x_bins) for mu in mu_bins)
]

acceptance_intervals = fc_construct_acceptance_intervals_pdfs(matrix, cl)

LowerLimitNum, UpperLimitNum, _ = fc_get_limits(mu_bins, x_bins,
                                                acceptance_intervals)

fc_fix_limits(LowerLimitNum, UpperLimitNum)

fig = plt.figure()
ax = fig.add_subplot(111)

plt.plot(UpperLimitNum, mu_bins, ls="-", color="red")
plt.plot(LowerLimitNum, mu_bins, ls="-", color="red")

plt.grid(True)
ax.xaxis.set_ticks(ticks=np.arange(-10, 10, 1))
ax.xaxis.set_ticks(ticks=np.arange(-10, 10, 0.2), minor=True)
ax.yaxis.set_ticks(ticks=np.arange(0, 8, 0.2), minor=True)
ax.set_xlabel(r"Measured Mean x")
ax.set_ylabel(r"Mean $\mu$")
plt.axis([-2, 4, 0, 6])
plt.show()
x_bins = np.arange(0, n_bins_x)
mu_bins = np.linspace(mu_min, mu_max, mu_max / step_width_mu + 1, endpoint=True)

print("Generating Feldman Cousins confidence belt for " + str(len(mu_bins)) +
      " values of mu.")

UpperLimitAna = []
LowerLimitAna = []

for mu in ProgressBar(mu_bins):
    goodChoice = fc_find_acceptance_interval_poisson(mu, background, x_bins, cl)
    UpperLimitAna.append(goodChoice[0])
    LowerLimitAna.append(goodChoice[1])

fc_fix_limits(LowerLimitAna, UpperLimitAna)

fig = plt.figure()
ax = fig.add_subplot(111)

plt.plot(LowerLimitAna, mu_bins, ls='-', color='red')
plt.plot(UpperLimitAna, mu_bins, ls='-', color='red')

plt.grid(True)
ax.yaxis.set_label_coords(-0.08, 0.5)
plt.xticks(range(15))
plt.yticks(range(15))
ax.set_xlabel(r'Measured n')
ax.set_ylabel(r'Signal Mean $\mu$')
plt.axis([0, 15, 0, 15])
plt.show()
Exemplo n.º 5
0
mu_max = 8
cl = 0.90

x_bins = np.linspace(-n_sigma * sigma, n_sigma * sigma, n_bins_x, endpoint=True)
mu_bins = np.linspace(mu_min, mu_max, mu_max / step_width_mu + 1, endpoint=True)

matrix = [
    dist / sum(dist)
    for dist in (norm(loc=mu, scale=sigma).pdf(x_bins) for mu in mu_bins)
]

acceptance_intervals = fc_construct_acceptance_intervals_pdfs(matrix, cl)

LowerLimitNum, UpperLimitNum, _ = fc_get_limits(mu_bins, x_bins, acceptance_intervals)

fc_fix_limits(LowerLimitNum, UpperLimitNum)

fig = plt.figure()
ax = fig.add_subplot(111)

plt.plot(UpperLimitNum, mu_bins, ls="-", color="red")
plt.plot(LowerLimitNum, mu_bins, ls="-", color="red")

plt.grid(True)
ax.xaxis.set_ticks(np.arange(-10, 10, 1))
ax.xaxis.set_ticks(np.arange(-10, 10, 0.2), True)
ax.yaxis.set_ticks(np.arange(0, 8, 0.2), True)
ax.set_xlabel(r"Measured Mean x")
ax.set_ylabel(r"Mean $\mu$")
plt.axis([-2, 4, 0, 6])
plt.show()