Example #1
0
    def power(self, seq1, seq2, totalSeq1, totalSeq2, alpha):
        # The chi-square test is equivalent to the difference between proportions
        # test as illustrated by Rivals et al., 2007. Here we use the standard
        # asymptotic power formulation for a difference between proportions test.
        oneMinusAlpha = 1.0 - alpha

        p1 = float(seq1) / totalSeq1
        p2 = float(seq2) / totalSeq2
        d = p1 - p2

        stdDev = math.sqrt((p1 * (1 - p1)) / totalSeq1 +
                           (p2 * (1 - p2)) / totalSeq2)

        if stdDev != 0:
            p = float(totalSeq1 * p1 + totalSeq2 * p2) / (totalSeq1 +
                                                          totalSeq2)
            q = 1 - p
            pooledStdDev = math.sqrt((p * q) / totalSeq1 + (p * q) / totalSeq2)

            zScore = zScore(oneMinusAlpha)
            zLower = (-zScore * pooledStdDev - d) / stdDev
            zUpper = (zScore * pooledStdDev - d) / stdDev

            return standardNormalCDF(zLower) + (1.0 -
                                                standardNormalCDF(zUpper))
        else:
            return 1.0
Example #2
0
	def testNormalDist(self):
		"""Verify computation of normal distribution methods"""
		from metagenomics.stats.distributions.NormalDist import standardNormalCDF, zScore
		
		self.assertAlmostEqual(standardNormalCDF(-2), 0.022750131948179209)
		self.assertAlmostEqual(standardNormalCDF(-1), 0.15865525393145705)
		self.assertAlmostEqual(standardNormalCDF(0), 0.5)
		self.assertAlmostEqual(standardNormalCDF(1), 0.84134474606854293)
		self.assertAlmostEqual(standardNormalCDF(2), 0.97724986805182079)
		self.assertAlmostEqual(standardNormalCDF(-1e-6), 1.0 - standardNormalCDF(1e-6))
		self.assertAlmostEqual(standardNormalCDF(-1e-12), 1.0 - standardNormalCDF(1e-12))
		
		self.assertAlmostEqual(zScore(0.90), 1.6448536269514722)
		self.assertAlmostEqual(zScore(0.95), 1.959963984540054)
		self.assertAlmostEqual(zScore(0.98), 2.3263478740408408)
		self.assertAlmostEqual(zScore(0.99), 2.5758293035489004)
		self.assertAlmostEqual(zScore(0.80), 1.2815515655446004)
Example #3
0
	def power(self, seq1, seq2, totalSeq1, totalSeq2, alpha): 
		oneMinusAlpha = 1.0 - alpha
		 
		p1 = float(seq1) / totalSeq1
		p2 = float(seq2) / totalSeq2
		d = p1 - p2

		stdDev = math.sqrt( (p1 * (1-p1)) / totalSeq1 + (p2 * (1 - p2)) / totalSeq2 )
		
		if stdDev != 0:		
			p = float(totalSeq1*p1 + totalSeq2*p2) / (totalSeq1 + totalSeq2)
			q = 1-p
			pooledStdDev = math.sqrt( (p*q) / totalSeq1 + (p*q) / totalSeq2 )
			
			zScore = zScore(oneMinusAlpha)
			zLower = ( -zScore * pooledStdDev - d ) / stdDev
			zUpper= ( zScore * pooledStdDev - d ) / stdDev
		
			return standardNormalCDF(zLower) + (1.0 - standardNormalCDF(zUpper))
		else:
			return 1.0
Example #4
0
	def power(self, seq1, seq2, totalSeq1, totalSeq2, alpha): 
		oneMinusAlpha = 1.0 - alpha
		 
		p1 = float(seq1) / totalSeq1
		p2 = float(seq2) / totalSeq2
		d = p1 - p2

		stdDev = math.sqrt( (p1 * (1-p1)) / totalSeq1 + (p2 * (1 - p2)) / totalSeq2 )
		
		if stdDev != 0:		
			p = float(totalSeq1*p1 + totalSeq2*p2) / (totalSeq1 + totalSeq2)
			q = 1-p
			pooledStdDev = math.sqrt( (p*q) / totalSeq1 + (p*q) / totalSeq2 )
			
			zScore = zScore(oneMinusAlpha)
			zLower = ( -zScore * pooledStdDev - d ) / stdDev
			zUpper= ( zScore * pooledStdDev - d ) / stdDev
		
			return standardNormalCDF(zLower) + (1.0 - standardNormalCDF(zUpper))
		else:
			return 1.0
Example #5
0
    def testNormalDist(self):
        """Verify computation of normal distribution methods"""
        from metagenomics.stats.distributions.NormalDist import standardNormalCDF, zScore

        self.assertAlmostEqual(standardNormalCDF(-2), 0.022750131948179209)
        self.assertAlmostEqual(standardNormalCDF(-1), 0.15865525393145705)
        self.assertAlmostEqual(standardNormalCDF(0), 0.5)
        self.assertAlmostEqual(standardNormalCDF(1), 0.84134474606854293)
        self.assertAlmostEqual(standardNormalCDF(2), 0.97724986805182079)
        self.assertAlmostEqual(standardNormalCDF(-1e-6),
                               1.0 - standardNormalCDF(1e-6))
        self.assertAlmostEqual(standardNormalCDF(-1e-12),
                               1.0 - standardNormalCDF(1e-12))

        self.assertAlmostEqual(zScore(0.90), 1.6448536269514722)
        self.assertAlmostEqual(zScore(0.95), 1.959963984540054)
        self.assertAlmostEqual(zScore(0.98), 2.3263478740408408)
        self.assertAlmostEqual(zScore(0.99), 2.5758293035489004)
        self.assertAlmostEqual(zScore(0.80), 1.2815515655446004)
Example #6
0
  def power(self, seq1, seq2, totalSeq1, totalSeq2, alpha): 
    # The chi-square test is equivalent to the difference between proportions
    # test as illustrated by Rivals et al., 2007. Here we use the standard
    # asymptotic power formulation for a difference between proportions test.
    oneMinusAlpha = 1.0 - alpha
     
    p1 = float(seq1) / totalSeq1
    p2 = float(seq2) / totalSeq2
    d = p1 - p2

    stdDev = math.sqrt( (p1 * (1-p1)) / totalSeq1 + (p2 * (1 - p2)) / totalSeq2 )
    
    if stdDev != 0:    
      p = float(totalSeq1*p1 + totalSeq2*p2) / (totalSeq1 + totalSeq2)
      q = 1-p
      pooledStdDev = math.sqrt( (p*q) / totalSeq1 + (p*q) / totalSeq2 )
      
      zScore = zScore(oneMinusAlpha)
      zLower = ( -zScore * pooledStdDev - d ) / stdDev
      zUpper= ( zScore * pooledStdDev - d ) / stdDev
    
      return standardNormalCDF(zLower) + (1.0 - standardNormalCDF(zUpper))
    else:
      return 1.0
Example #7
0
	def hypothesisTest(self, seq1, seq2, totalSeq1, totalSeq2):
		R1 = float(seq1) / totalSeq1
		R2 = float(seq2) / totalSeq2
		diff = R1 - R2
		P = float(seq1 + seq2) / (totalSeq1 + totalSeq2)
		Q = 1.0 - P
		
		if (seq1 == 0 and seq2 == 0) or (seq1 == totalSeq1 and seq2 == totalSeq2):
			D = 0
			note = 'degenerate case: suspect p-value'
		else:		
			D = diff / math.sqrt(P*Q*((1.0/totalSeq1) + (1.0/totalSeq2)))
			note = ''
		
		# calculate one-sided and two-sided p-value
		ZScore = abs(D)
		pValueOneSided = standardNormalCDF(ZScore)
		if pValueOneSided > 0.5:
			pValueOneSided = 1.0 - pValueOneSided
		pValueTwoSided = 2*pValueOneSided
	
		return pValueOneSided, pValueTwoSided, note
Example #8
0
	def hypothesisTest(self, seq1, seq2, totalSeq1, totalSeq2):
		if (seq1 == 0 and seq2 == 0) or (seq1 == totalSeq1 and seq2 == totalSeq2):
			D = 0
			note = 'degenerate case: suspect p-value'
		else:
			R1 = float(seq1) / totalSeq1
			R2 = float(seq2) / totalSeq2
			diff = R1 - R2
			P = float(seq1 + seq2) / (totalSeq1 + totalSeq2)
			Q = 1.0 - P
		
			D = diff / math.sqrt(P*Q*((1.0/totalSeq1) + (1.0/totalSeq2)))
			note = ''
		
		# calculate one-sided and two-sided p-value
		ZScore = abs(D)
		pValueOneSided = standardNormalCDF(ZScore)
		if pValueOneSided > 0.5:
			pValueOneSided = 1.0 - pValueOneSided
		pValueTwoSided = 2*pValueOneSided
	
		return pValueOneSided, pValueTwoSided, note