def test_acceptance_interval_poisson():
    background = 0.5
    n_bins_x = 100
    cl = 0.90

    x_bins = np.arange(0, n_bins_x)

    # The test reverses a result from the Feldman and Cousins paper. According
    # to Table IV, for a measured value of 10 the 90% confidence interval
    # should be 5.00 and 16.00. Reversed that means that for mu=5.0, the
    # acceptance interval should end at 10 and for mu=16.00 should start at 10.
    (x_min, x_max) = fc_find_acceptance_interval_poisson(5.00, background, x_bins, cl)
    assert_allclose(x_max, 10)

    (x_min, x_max) = fc_find_acceptance_interval_poisson(16.00, background, x_bins, cl)
    assert_allclose(x_min, 10)

    # Pass too few x_bins to reach confidence level.
    with pytest.raises(ValueError):
        fc_find_acceptance_interval_poisson(0, 7, x_bins[0:10], cl)
"""Demonstrate the artefact that can arise if
   fc_fix_limits is not used."""
import numpy as np
from astropy.table import Table
from gammapy.stats import fc_find_acceptance_interval_poisson

background = 3.5
cl = 0.90
x_bins = np.arange(0, 100)

table = Table()
table['mu'] = [0.745, 0.750, 0.755, 1.030, 1.035, 1.040, 1.045, 1.050, 1.055,
               1.060, 1.065]
table['x_min'] = 0.0
table['x_max'] = 0.0

for row in table:
    x_min, x_max = fc_find_acceptance_interval_poisson(row['mu'], background,
                                                       x_bins, cl)
    row['x_min'] = x_min
    row['x_max'] = x_max

table.pprint()
step_width_mu = 0.005
mu_min = 0
mu_max = 50
cl = 0.90

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))
Beispiel #4
0
background = 3.5
cl = 0.90
x_bins = np.arange(0, 100)

table = Table()
table["mu"] = [
    0.745,
    0.750,
    0.755,
    1.030,
    1.035,
    1.040,
    1.045,
    1.050,
    1.055,
    1.060,
    1.065,
]
table["x_min"] = 0.0
table["x_max"] = 0.0

for row in table:
    x_min, x_max = fc_find_acceptance_interval_poisson(
        row["mu"], background, x_bins, cl
    )
    row["x_min"] = x_min
    row["x_max"] = x_max

table.pprint()