예제 #1
0
def trace_bounds(power, counts):
    """Gets the mean, upper and lower confidence interval for counts

    Parameters
    ----------
    power : ndarray
        An array with multiple power returns where each row corresponds to a
        permutation run and each column corresponds to a number of samples
        drawn.
    counts : ndarray
        The number of samples drawn per group for each power calculation.

    Returns
    -------
        pwr_mean, pwr_lower, pwr_upper : ndarray
            The mean, upper and lower bounds for the power array.

    """

    pwr_mean = np.nanmean(power, 0)
    pwr_bound = confidence_bound(power, axis=0)

    # Prevents the confidence interval of being less than 0 or more than 1
    pwr_lower = pwr_mean - pwr_bound
    pwr_lower = np.where(pwr_lower < 0, 0, pwr_lower)

    pwr_upper = pwr_mean + pwr_bound
    pwr_upper = np.where(pwr_upper > 1, 1, pwr_upper)

    return pwr_mean, pwr_lower, pwr_upper
예제 #2
0
 def test_confidence_bound_axis_none(self):
     # Sets the value to test
     samples = np.array([[4, 3.2, 3.05], [2, 2.8, 2.95], [5, 2.9, 3.07],
                         [1, 3.1, 2.93], [3, np.nan, 3.00]])
     # Sest the known value
     known = 0.52852
     # Tests the output
     test = confidence_bound(samples, axis=None)
     npt.assert_almost_equal(known, test, 3)
예제 #3
0
 def test_confidence_bound_nan(self):
     # Sets the value to test
     samples = np.array([[4, 3.2, 3.05], [2, 2.8, 2.95], [5, 2.9, 3.07],
                         [1, 3.1, 2.93], [3, np.nan, 3.00]])
     # Sets the know value
     known = np.array([2.2284, 0.2573, 0.08573])
     # Tests the function
     test = confidence_bound(samples, axis=0)
     npt.assert_almost_equal(known, test, 3)
예제 #4
0
 def test_confidence_bound_axis_none(self):
     # Sets the value to test
     samples = np.array([[4, 3.2, 3.05],
                         [2, 2.8, 2.95],
                         [5, 2.9, 3.07],
                         [1, 3.1, 2.93],
                         [3, np.nan, 3.00]])
     # Sest the known value
     known = 0.52852
     # Tests the output
     test = confidence_bound(samples, axis=None)
     npt.assert_almost_equal(known, test, 3)
예제 #5
0
 def test_confidence_bound_nan(self):
     # Sets the value to test
     samples = np.array([[4, 3.2, 3.05],
                         [2, 2.8, 2.95],
                         [5, 2.9, 3.07],
                         [1, 3.1, 2.93],
                         [3, np.nan, 3.00]])
     # Sets the know value
     known = np.array([2.2284, 0.2573, 0.08573])
     # Tests the function
     test = confidence_bound(samples, axis=0)
     npt.assert_almost_equal(known, test, 3)
예제 #6
0
 def test_confidence_bound_alpha(self):
     known = 3.2797886
     test = confidence_bound(self.s1, alpha=0.01)
     npt.assert_almost_equal(known, test, 3)
예제 #7
0
 def test_confidence_bound_df(self):
     known = 2.15109
     test = confidence_bound(self.s1, df=15)
     npt.assert_almost_equal(known, test, 3)
예제 #8
0
 def test_confidence_bound_default(self):
     # Sets the know confidence bound
     known = 2.2830070
     test = confidence_bound(self.s1)
     npt.assert_almost_equal(test, known, 3)
예제 #9
0
def collate_effect_size(counts, powers, alpha):
    """Calculates the effects for power values

    Parameters
    ----------
    counts : array
        the number of samples used to calculate the power
    powers : {list, ndarray}
        list of arrays of power values. If there are multiple power arrays,
        each power array should have the same dimensions. If samples are
        missing, these should be denoted by a nan.
    alpha : float
        the critical value used to calculate the power.

    Returns
    -------
    effect_means : array
        A 1-dimensional array of the mean effect sizes for the counts array.
    effect_bounds : array
        A 1-dimensional array with the confidence bound for the effect size
        array. The lower bound is `effect_mean - effect_bounds` and the upper
        bound is `effect_means - effect_bounds1`.

    Raises
    ------
    TypeError
        if counts is not a one-dimensional array
    ValueError
        if the arrays in powers have different shapes
    ValueError
        if the length of the power arrays and the length of the count arrays
        are different
    """

    # Checks the power and counts iteratibility
    if isinstance(powers, np.ndarray):
        powers = [powers]

    num_powers = len(powers)
    if isinstance(counts, np.ndarray):
        counts = [counts]*num_powers

    # Checks there is a count for each power
    if len(counts) != len(powers):
        raise ValueError('There must be a counts array for each power array.')

    # Checks the shape array
    for idx in xrange(num_powers):
        count_shape = counts[idx].shape
        power_shape = powers[idx].shape
        # Checks the count array is 1d
        if len(count_shape) != 1:
            raise TypeError('Each count array must be a 1d array.')

        if len(power_shape) == 1:
            if count_shape[0] != power_shape[0]:
                raise ValueError('There must be a sample count for each '
                                 'power.')
        elif count_shape[0] != power_shape[1]:
            raise ValueError('There must be a sample count for each power.')

    # Prealocates the output arrays
    effect_means = np.zeros((num_powers))
    effect_bounds = np.zeros((num_powers))

    # Iterates through the powers and calculates the effect sizes
    for idp, pwr in enumerate(powers):
        count = counts[idp]
        pwr_shape = pwr.shape
        # Calculates the effect size for the power array
        eff = np.zeros(pwr_shape)*np.nan
        for id2, cnt in enumerate(count):
            if len(pwr_shape) == 1:
                try:
                    eff[id2] = ft.solve_power(effect_size=None,
                                              nobs=cnt,
                                              alpha=alpha,
                                              power=pwr[id2])
                except:
                    pass
            else:
                for id1 in xrange(pwr_shape[0]):
                    try:
                        eff[id1, id2] = ft.solve_power(effect_size=None,
                                                       nobs=cnt,
                                                       alpha=alpha,
                                                       power=pwr[id1, id2])
                    except:
                        pass
        # Caluclates the mean and bound
        if np.isnan(eff).all():
            effect_means[idp] = np.nan
            effect_bounds[idp] = np.nan
        else:
            effect_means[idp] = np.nanmean(eff, None)
            effect_bounds[idp] = confidence_bound(eff, alpha, None)

    return effect_means, effect_bounds