Skip to content

Instantly share code, notes, and snippets.

@yoavram
Last active July 27, 2017 15:09
Show Gist options
  • Select an option

  • Save yoavram/963892760c83e6a984e10bd1ee2bf762 to your computer and use it in GitHub Desktop.

Select an option

Save yoavram/963892760c83e6a984e10bd1ee2bf762 to your computer and use it in GitHub Desktop.

Revisions

  1. Yoav Ram revised this gist Jul 27, 2017. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions jeffreys_interval.py
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,25 @@
    def bin_prop_jeffreys_interval(trials, successes, α=0.05):
    """Binomial proportions confidence Jeffrey's interval.
    """Binomial proportions confidence Jeffrey's interval.
    Parameters
    ----------
    trials : np.ndarray
    number of trials
    successes : int
    number of successes
    α : float, 0<α<1
    width of confidence interval
    Parameters
    ----------
    trials : np.ndarray
    number of trials
    successes : int
    number of successes
    α : float, 0<α<1
    width of confidence interval
    Returns
    -------
    low, high : tuple of floats
    The lower and upper bounds of the proportions confidence interval
    Returns
    -------
    low, high : tuple of floats
    The lower and upper bounds of the proportions confidence interval
    See
    ---
    https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Jeffreys_interval
    """
    import scipy.stats
    See
    ---
    https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Jeffreys_interval
    """
    import scipy.stats
    n = int(trials)
    x = int(successes)
    assert 0 <= x <= n
  2. Yoav Ram revised this gist Jul 27, 2017. 1 changed file with 27 additions and 5 deletions.
    32 changes: 27 additions & 5 deletions jeffreys_interval.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,35 @@
    def jeffreys_interval(trials, successes, α=0.05):
    def bin_prop_jeffreys_interval(trials, successes, α=0.05):
    """Binomial proportions confidence Jeffrey's interval.
    Parameters
    ----------
    trials : np.ndarray
    number of trials
    successes : int
    number of successes
    α : float, 0<α<1
    width of confidence interval
    Returns
    -------
    low, high : tuple of floats
    The lower and upper bounds of the proportions confidence interval
    See
    ---
    https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Jeffreys_interval
    """
    import scipy.stats
    n = int(trials)
    x = int(successes)
    beta = scipy.stats.beta(x + 0.5, n-x+0.5)
    assert 0 <= x <= n
    beta = scipy.stats.beta(x + 0.5, n - x + 0.5)
    if x == 0:
    low = 0
    else:
    low = beta.ppf(α/2)
    low = beta.ppf(α / 2)
    if x == n:
    high = 1
    else:
    high = beta.ppf(1-α/2)
    return x/n-low, high-x/n
    high = beta.ppf(1 - α / 2)
    return x / n - low, high - x / n
  3. Yoav Ram revised this gist Jul 20, 2017. No changes.
  4. Yoav Ram created this gist Jul 20, 2017.
    13 changes: 13 additions & 0 deletions jeffreys_interval.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    def jeffreys_interval(trials, successes, α=0.05):
    n = int(trials)
    x = int(successes)
    beta = scipy.stats.beta(x + 0.5, n-x+0.5)
    if x == 0:
    low = 0
    else:
    low = beta.ppf(α/2)
    if x == n:
    high = 1
    else:
    high = beta.ppf(1-α/2)
    return x/n-low, high-x/n