def __init__(self, n=None, alpha=None, beta=None, power=None, p=None):
        # There should be a better way.
        # This loop both checks the values for p and caclualtes a value needed
        # for the calculation.
        self._denom = 0
        if isinstance(p, list):
            len_p = len(p)
            self.df = len_p * (len_p - 1) / 2.0
            self._adjustment = 0
            for i in range(len_p - 1):
                if isinstance(p, list):
                    for j in range(i, len_p):
                        is_in_0_1(p[i][j],
                                  'All values of p should be in [0, 1].')
                        is_in_0_1(p[j][i],
                                  'All values of p should be in [0, 1].')
                        self._denom += ((p[i][j] - p[j][i])**2 /
                                        (p[i][j] + p[j][i]))
                else:
                    raise ValueError("Each list in `p` must be a "
                                     "list numerics")
        else:
            raise ValueError("`p` must be a list of lists  of numerics")

        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        # Set remaining variables.
        super(StuartMaxwell, self).__init__(alpha=alpha,
                                            beta=beta,
                                            power=power)
Beispiel #2
0
    def __init__(self,
                 n=None,
                 p=None,
                 p_0=None,
                 margin=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 hypothesis=None):

        is_in_0_1(p, 'p')
        self.p = p

        is_in_0_1(p_0, 'p_0')
        self.p_0 = p_0

        stdev = math.sqrt(p * (1 - p))

        # Initialize the remaining arguments through the parent.
        super(OneSample, self).__init__(n=n,
                                        mu=p,
                                        mu_0=p_0,
                                        stdev=stdev,
                                        known_stdev=True,
                                        alpha=alpha,
                                        beta=beta,
                                        power=power,
                                        margin=margin,
                                        hypothesis=hypothesis)
Beispiel #3
0
    def __init__(self,
                 n=None,
                 p=None,
                 p_0=None,
                 alpha=None,
                 beta=None,
                 power=None):
        if n is not None:
            if isinstance(n, int):
                self.n = n
            else:
                raise ValueError('`n` should be of type Int.')
        else:
            self.n = None

        for value in p:
            is_in_0_1(value, 'Each value in `p` should be in [0, 1].')

        if p_0 is not None:
            is_in_0_1(value, '`p_0` should be in [0, 1].')

        self.p = p
        self.p_0 = p_0
        self.mean_mu = statistics.mean(self.p)
        self.n_groups = len(self.p)

        # The number of comparisons of interest for pairwise comparisons
        self.tau = self.n_groups * (self.n_groups - 1) / 2.0

        # Initialize the remaining arguments (alpha, beta, power)
        # through the parent.
        super(OneWayAnova, self).__init__(alpha=alpha,
                                          power=power,
                                          beta=beta,
                                          hypothesis='equality')
Beispiel #4
0
    def __init__(self,
                 n=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 p_0=None,
                 p=None):
        if isinstance(p, list):
            for value in p:
                is_in_0_1(value, 'All values of p should be in (0, 1).')
        else:
            raise ValueError("`p` must be a list of Numerics.")
        self.p = p

        if isinstance(p_0, list):
            for value in p_0:
                is_in_0_1(value, 'All values of p_0 should be in (0, 1).')
        else:
            raise ValueError("`p_0` must be a list of Numerics.")
        self.p_0 = p_0

        if not len(p) == len(p_0):
            raise ValueError("`p_0` and `p` must have the same length.")

        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        # Set remaining variables.
        super(Pearson, self).__init__(alpha=alpha, beta=beta, power=power)
Beispiel #5
0
    def __init__(self,
                 n_1=None,
                 n_2=None,
                 ratio=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 p_1=None,
                 p_2=None):

        is_in_0_1(p_1, 'p_1')
        self.p_1 = p_1

        is_in_0_1(p_2, 'p_2')
        self.p_2 = p_2

        # n is only used to help with control flow
        if ratio is None:
            if n_1 is None:
                ratio = 1
                if n_2 is None:
                    n = None
                else:
                    n_1 = n_2
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    ratio = 1
                    n_2 = n_1
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2
                    ratio = n_1 / float(n_2)
        else:
            if n_1 is None:
                if n_2 is None:
                    n = None
                else:
                    n_1 = math.ceil(ratio * n_2)
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    n_2 = math.ceil(n_1 / ratio)
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2

        self.n_1 = n_1
        self.n_2 = n_2
        self.n = n
        self.ratio = float(ratio)

        super(Fisher, self).__init__(alpha=alpha,
                                     power=power,
                                     beta=beta,
                                     hypothesis='equality')
    def __init__(self, n=None, epsilon=None, stdev=None, hypothesis=None,
                 margin=None, alpha=None, beta=None, power=None):

        is_in_0_1(epsilon, '`epsilon` should be in [0, 1]')

        # Initialize the remaining arguments through the parent.
        super(TwoSampleCrossover, self).__init__(n=n, mu_1=epsilon, mu_2=0,
                                                 stdev=stdev, known_stdev=True,
                                                 alpha=alpha, beta=beta,
                                                 power=power, margin=margin,
                                                 hypothesis=hypothesis)
Beispiel #7
0
 def _check_list(self, lst, label):
     """ Recursively check the lists in lst """
     if isinstance(lst, list):
         for values in lst:
             if isinstance(values, list):
                 self._check_list(values, label=label)
             else:
                 is_in_0_1(
                     values,
                     ('All values of ' + label + 'p should be in (0, 1).'))
     else:
         raise ValueError("`p` must be a list of lists of numerics")
Beispiel #8
0
    def __init__(self, n=None, alpha=None, beta=None, power=None,
                 p_1=None, p_2=None):
        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        is_in_0_1(p_1, 'p_1 should be in [0, 1].')
        is_in_0_1(p_2, 'p_2 should be in [0, 1].')

        self.p_1 = p_1
        self.p_2 = p_2

        # Initialize the remaining arguments through the parent.
        super(Independance, self).__init__(alpha=alpha, power=power,
                                           beta=beta, hypothesis=None)
Beispiel #9
0
    def __init__(self, n=None, alpha=None, beta=None, power=None, p_01=None,
                 p_10=None):

        is_in_0_1(p_01, 'p_01 should be in [0, 1].')
        is_in_0_1(p_10, 'p_10 should be in [0, 1].')

        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        self.p_01 = p_01
        self.p_10 = p_10
        self.alpha_factor = math.sqrt(p_01 + p_10)
        self.beta_factor = math.sqrt(p_10 + p_01 - (p_01 - p_10)**2)

        # Set remaining variables.
        super(McNemar, self).__init__(alpha=alpha, beta=beta, power=power)
    def __init__(self, n=None, alpha=None, beta=None, power=None, p=None):
        # This should be made much better (see CMH)
        if isinstance(p, list):
            for values in p:
                if isinstance(values, list):
                    for value in values:
                        is_in_0_1(value,
                                  'All values of p should be in (0, 1).')
                else:
                    raise ValueError("Each list in `p` must be a list "
                                     "of numerics")
        else:
            raise ValueError("`p` must be a list of lists  of numerics")
        self.p = p

        # needed for degree of freedom calculations
        rows = len(p)
        cols = len(p[0])
        self.df = (rows - 1) * (cols - 1)
        self._denom = 0

        # This will be much improved when I implement this as a matrix!
        colsums = [0] * cols
        for i in range(rows):
            for j in range(cols):
                colsums[j] += p[i][j]

        for i in range(rows):
            rowsum = sum(p[i])
            for j in range(cols):
                self._denom += ((p[i][j] - rowsum * colsums[j])**2 /
                                (rowsum * colsums[j]))

        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        # Set remaining variables.
        super(PearsonIndependance, self).__init__(alpha=alpha,
                                                  beta=beta,
                                                  power=power)
Beispiel #11
0
    def __init__(self,
                 n=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 p_2=None,
                 p_3=None,
                 p_4=None):
        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        is_in_0_1(p_2, 'p_2 should be in [0, 1].')
        is_in_0_1(p_3, 'p_3 should be in [0, 1].')
        is_in_0_1(p_4, 'p_4 should be in [0, 1].')

        self.p_2 = p_2
        self.p_3 = p_3
        self.p_4 = p_4

        # Initialize the remaining arguments through the parent.
        super(OneSample, self).__init__(alpha=alpha,
                                        power=power,
                                        beta=beta,
                                        hypothesis=None)
Beispiel #12
0
    def __init__(self,
                 n=None,
                 alpha=None,
                 power=None,
                 beta=None,
                 hypothesis=None,
                 margin=None,
                 hazard_ratio=None,
                 proportion_visible=None,
                 control_proportion=None,
                 treatment_proportion=None):
        is_positive(hazard_ratio, 'Hazard Ratio')
        is_in_0_1(treatment_proportion, 'Treatment Proportion')
        is_in_0_1(control_proportion, 'Control Proportion')
        is_in_0_1(proportion_visible, 'Proportion Visible')

        epsilon = math.log(hazard_ratio)
        stdev = treatment_proportion * control_proportion * proportion_visible
        stdev = 1 / math.sqrt(stdev)

        super(Cox, self).__init__(n=n,
                                  mu=epsilon,
                                  mu_0=0,
                                  stdev=stdev,
                                  known_stdev=True,
                                  alpha=alpha,
                                  beta=beta,
                                  power=power,
                                  margin=margin,
                                  hypothesis=hypothesis)
Beispiel #13
0
    def __init__(self,
                 n=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 p=None,
                 p_0=None,
                 margin=None):

        is_in_0_1(p, 'p')
        self.p = p

        is_in_0_1(p_0, 'p_0')
        self.p_0 = p_0

        if margin is not None:
            is_in_0_1(margin + p_0, 'margin + p_0')
            self.margin = margin
            self.p_0 += margin
        else:
            self.margin = 0

        if n is not None:
            is_integer(n, 'n')
        self.n = n

        super(Binomial, self).__init__(alpha=alpha,
                                       power=power,
                                       beta=beta,
                                       hypothesis='equality')
Beispiel #14
0
    def __init__(self,
                 delta=None,
                 stdev_wr=None,
                 stdev_wt=None,
                 stdev_br=None,
                 stdev_bt=None,
                 rho=None,
                 theta_IBE=None,
                 alpha=None,
                 power=None,
                 beta=None):
        is_numeric(delta, 'delta')
        self.delta = delta

        is_positive(stdev_wr, 'stdev_wr')
        self.stdev_wr = stdev_wr
        is_positive(stdev_wt, 'stdev_wt')
        self.stdev_wt = stdev_wt
        is_positive(stdev_bt, 'stdev_br')
        self.stdev_br = stdev_br
        is_positive(stdev_bt, 'stdev_bt')
        self.stdev_bt = stdev_bt
        is_in_0_1(rho, 'rho')
        self.rho = rho

        var_d = self.stdev_bt**2 + self.stdev_br**2 - 2 * self.rho * self.stdev_bt**2 * self.stdev_bt**2
        self.stdev_d = math.sqrt(var_d)
        if theta_IBE is None:
            theta_IBE = 1.74
        else:
            is_positive(theta_IBE, 'theta_IBE')
        self.theta_IBE = theta_IBE

        # Initialize the remaining arguments through the parent.
        super(Individual, self).__init__(hypothesis="equivalence",
                                         alpha=alpha,
                                         beta=beta,
                                         power=power)
Beispiel #15
0
    def __init__(self,
                 n=None,
                 p=None,
                 stdev=None,
                 hypothesis=None,
                 margin=None,
                 alpha=None,
                 beta=None,
                 power=None):

        for value in p:
            is_in_0_1(value, 'Each value in `p` should be in [0, 1].')

        # This is the same as the Multi-Sample Williams design for means
        super(MultiSampleWilliams, self).__init__(n=n,
                                                  mu=p,
                                                  stdev=stdev,
                                                  margin=margin,
                                                  alpha=alpha,
                                                  power=power,
                                                  beta=beta,
                                                  known_stdev=True,
                                                  hypothesis=hypothesis)
Beispiel #16
0
    def __init__(self, delta=None, l=None, stdev_11=None, stdev_tt=None,
                 stdev_tr=None, stdev_bt=None, stdev_br=None, rho=None,
                 theta_PBE=None, alpha=None, power=None, beta=None):

        if delta is None:
            delta = 0
        else:
            is_numeric(delta, 'delta')
        self.delta = delta

        is_numeric(l, 'l')
        self.l = l

        is_positive(stdev_11, 'stdev_11')
        self.stdev_11 = stdev_11
        is_positive(stdev_tt, 'stdev_tt')
        self.stdev_tt = stdev_tt
        is_positive(stdev_tr, 'stdev_tr')
        self.stdev_tr = stdev_tr
        is_positive(stdev_bt, 'stdev_bt')
        self.stdev_bt = stdev_bt
        is_positive(stdev_br, 'stdev')
        self.stdev_br = stdev_br

        is_in_0_1(rho, 'rho')
        self.rho = rho

        if theta_PBE is None:
            theta_PBE = 1.74
        else:
            is_positive(theta_PBE, 'theta_PBE')
        self.theta_PBE = theta_PBE

        # Initialize the remaining arguments through the parent.
        super(Population, self).__init__(hypothesis="equivalence",
                                         alpha=alpha, beta=beta, power=power)
Beispiel #17
0
    def __init__(self,
                 n=None,
                 alpha=None,
                 beta=None,
                 power=None,
                 p_11=None,
                 p_12=None,
                 p_21=None,
                 p_22=None,
                 gamma=None,
                 stdev_1=None,
                 stdev_2=None):

        if gamma is None:
            is_in_0_1(p_11, 'p_11 should be in [0, 1].')
            is_in_0_1(p_12, 'p_12 should be in [0, 1].')
            is_in_0_1(p_21, 'p_21 should be in [0, 1].')
            is_in_0_1(p_22, 'p_22 should be in [0, 1].')

            gamma = (math.log(p_11 / (1 - p_11)) + math.log(p_12 /
                                                            (1 - p_12)) -
                     math.log(p_21 / (1 - p_21)) - math.log(p_22 / (1 - p_22)))
        else:
            is_numeric(gamma, '`gamma` should be a number.')

        if n is not None:
            is_integer(n, '`n` should be of type Int.')
        self.n = n

        is_positive(stdev_1, 'stdev_1')
        self.stdev_1 = stdev_1

        is_positive(stdev_2, 'stdev_2')
        self.stdev_2 = stdev_2

        self.theta = gamma / math.sqrt(stdev_1**2 + stdev_2**2)

        # Set remaining variables.
        super(CarryOverEffect, self).__init__(alpha=alpha,
                                              beta=beta,
                                              power=power)
    def __init__(self,
                 n_1=None,
                 n_2=None,
                 p_1=None,
                 p_2=None,
                 margin=None,
                 ratio=None,
                 hypothesis=None,
                 alpha=None,
                 beta=None,
                 power=None):
        is_in_0_1(p_1, '`p_1` should be in (0, 1).')
        is_in_0_1(p_2, '`p_2` should be in (0, 1).')

        # n is only used to help with control flow
        if ratio is None:
            if n_1 is None:
                ratio = 1
                if n_2 is None:
                    n = None
                else:
                    n_1 = n_2
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    ratio = 1
                    n_2 = n_1
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2
                    ratio = n_1 / float(n_2)
        else:
            if n_1 is None:
                if n_2 is None:
                    n = None
                else:
                    n_1 = math.ceil(ratio * n_2)
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    n_2 = math.ceil(n_1 / ratio)
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2

        self.n_1 = n_1
        self.n_2 = n_2
        self.n = n
        self.ratio = float(ratio)

        stdev = p_1 * (1 - p_1) / ratio + p_2 * (1 - p_2)
        stdev = math.sqrt(stdev)

        epsilon = abs(p_1 - p_2)

        if hypothesis is 'superiority':
            epsilon = epsilon + margin
        elif hypothesis is 'equivalence':
            # This should be margin - abs(epsilon), but the abs() is taken care
            # of when epsilon is set for generality purposes
            epsilon = margin - abs(epsilon)

        self.theta = epsilon / stdev

        # Initialize the remaining arguments through the parent.
        # Initialize the remaining arguments through the parent.
        super(TwoSampleParallel, self).__init__(alpha=alpha,
                                                power=power,
                                                beta=beta,
                                                hypothesis=hypothesis)
Beispiel #19
0
    def __init__(self,
                 n_1=None,
                 n_2=None,
                 p_1=None,
                 p_2=None,
                 margin=None,
                 ratio=None,
                 hypothesis=None,
                 alpha=None,
                 beta=None,
                 power=None):
        is_in_0_1(p_1, '`p_1` should be in (0, 1).')
        is_in_0_1(p_2, '`p_2` should be in (0, 1).')

        # n is only used to help with control flow
        if ratio is None:
            if n_1 is None:
                ratio = 1
                if n_2 is None:
                    n = None
                else:
                    n_1 = n_2
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    ratio = 1
                    n_2 = n_1
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2
                    ratio = n_1 / float(n_2)
        else:
            if n_1 is None:
                if n_2 is None:
                    n = None
                else:
                    n_1 = math.ceil(ratio * n_2)
                    n = n_1 + n_2
            else:
                if n_2 is None:
                    n_2 = math.ceil(n_1 / ratio)
                    n = n_1 + n_2
                else:
                    n = n_1 + n_2

        self.n_1 = n_1
        self.n_2 = n_2
        self.n = n
        self.ratio = float(ratio)

        stdev = (1 / (p_1 * (1 - p_1) * ratio)) + (1 / (p_2 * (1 - p_2)))
        stdev = math.sqrt(stdev)

        odds_ratio = math.log((p_2 * (1 - p_1)) / (p_1 * (1 - p_2)))

        if hypothesis is 'superiority':
            epsilon = odds_ratio + margin
        elif hypothesis is 'equivalence':
            epsilon = margin - abs(odds_ratio)
        else:
            epsilon = odds_ratio

        self.theta = epsilon / stdev

        # Initialize the remaining arguments through the parent.
        super(RelativeRiskParallel, self).__init__(alpha=alpha,
                                                   power=power,
                                                   beta=beta,
                                                   hypothesis=hypothesis)