Example #1
0
    def test_variance_of_weighted_mean(self):
        """variance_of_weighted_mean calculates the variance of a weighted mean"""
        
        #Just a hand calculated example using the formula from here:
        #http://en.wikipedia.org/wiki/Weighted_mean
       

        #TODO: test if this works for arrays of variances

        #If all weights and standard deviations are equal, then
        #variance = stdev/sqrt(n)
        weights = array([0.5,0.5])
        sample_stdevs = array([4.0,4.0])
        variances = sample_stdevs**2
        exp = 4.0/sqrt(2.0)
        obs = variance_of_weighted_mean(weights,variances)
        self.assertFloatEqual(obs,exp)

        #If standard deviations are equal, but weights are not, the result
        #is equal to stdev*sqrt(sum(squared_weights))

        weights = array([0.1,0.9])
        sample_stdevs = array([4.0,4.0])
        variances = sample_stdevs**2
        exp_unbalanced = 4.0*sqrt(sum(weights**2))
        obs = variance_of_weighted_mean(weights,variances)
        self.assertEqual(obs,exp_unbalanced)

        #If all standard deviations are equal:
        #The minimal value for the variance is when all weights are equal
        #the maximal value is when one weight is 1.0 and another is 0.0

        sample_variances = array([3.0,3.0,3.0,3.0])
        
        balanced_weights = array([0.25,0.25,0.25,0.25])
        two_weights = array([0.0,0.50,0.50,0.0])
        unbalanced_weights = array([0.0,1.0,0.0,0.0])

        balanced_variance = variance_of_weighted_mean(balanced_weights,sample_variances)
        two_weight_variance = variance_of_weighted_mean(two_weights,sample_variances)
        unbalanced_variance = variance_of_weighted_mean(unbalanced_weights,sample_variances)
        
        #We expect balanced_variance < two-weight_variance < unbalanced_variance
        self.assertTrue(balanced_variance < two_weight_variance)
        self.assertTrue(balanced_variance < unbalanced_variance)
        self.assertTrue(two_weight_variance < unbalanced_variance)


        #Check that doing this for two 1D arrays is equal to using a single 2d array
        weights1 = array([0.1,0.9])
        weights2 = array([0.5,0.5])
        vars1 = array([4.0,4.0])
        vars2 = array([1000.0,1000.0])
        obs1 = variance_of_weighted_mean(weights1,vars1)
        obs2 = variance_of_weighted_mean(weights2,vars2)
        
        #Expect that calculating the result as a single 2D array
        #gives identical results to calculating as two 1D arrays
        exp = array([obs1,obs2])
        
        combined_weights = array([[0.1,0.9],[0.5,0.5]])
        combined_vars = array([[4.0,4.0],[1000.0,1000.0]])
        combined_obs = variance_of_weighted_mean(combined_weights,combined_vars)

        self.assertFloatEqual(combined_obs,exp)
Example #2
0
    def test_variance_of_weighted_mean(self):
        """variance_of_weighted_mean calculates the variance of a weighted mean"""
        
        #Just a hand calculated example using the formula from here:
        #http://en.wikipedia.org/wiki/Weighted_mean
       

        #TODO: test if this works for arrays of variances

        #If all weights and standard deviations are equal, then
        #variance = stdev/sqrt(n)
        weights = array([0.5,0.5])
        sample_stdevs = array([4.0,4.0])
        variances = sample_stdevs**2
        exp = 4.0/sqrt(2.0)
        obs = variance_of_weighted_mean(weights,variances)
        self.assertFloatEqual(obs,exp)

        #If standard deviations are equal, but weights are not, the result
        #is equal to stdev*sqrt(sum(squared_weights))

        weights = array([0.1,0.9])
        sample_stdevs = array([4.0,4.0])
        variances = sample_stdevs**2
        exp_unbalanced = 4.0*sqrt(sum(weights**2))
        obs = variance_of_weighted_mean(weights,variances)
        self.assertEqual(obs,exp_unbalanced)

        #If all standard deviations are equal:
        #The minimal value for the variance is when all weights are equal
        #the maximal value is when one weight is 1.0 and another is 0.0

        sample_variances = array([3.0,3.0,3.0,3.0])
        
        balanced_weights = array([0.25,0.25,0.25,0.25])
        two_weights = array([0.0,0.50,0.50,0.0])
        unbalanced_weights = array([0.0,1.0,0.0,0.0])

        balanced_variance = variance_of_weighted_mean(balanced_weights,sample_variances)
        two_weight_variance = variance_of_weighted_mean(two_weights,sample_variances)
        unbalanced_variance = variance_of_weighted_mean(unbalanced_weights,sample_variances)
        
        #We expect balanced_variance < two-weight_variance < unbalanced_variance
        self.assertTrue(balanced_variance < two_weight_variance)
        self.assertTrue(balanced_variance < unbalanced_variance)
        self.assertTrue(two_weight_variance < unbalanced_variance)


        #Check that doing this for two 1D arrays is equal to using a single 2d array
        weights1 = array([0.1,0.9])
        weights2 = array([0.5,0.5])
        vars1 = array([4.0,4.0])
        vars2 = array([1000.0,1000.0])
        obs1 = variance_of_weighted_mean(weights1,vars1)
        obs2 = variance_of_weighted_mean(weights2,vars2)
        
        #Expect that calculating the result as a single 2D array
        #gives identical results to calculating as two 1D arrays
        exp = array([obs1,obs2])
        
        combined_weights = array([[0.1,0.9],[0.5,0.5]])
        combined_vars = array([[4.0,4.0],[1000.0,1000.0]])
        combined_obs = variance_of_weighted_mean(combined_weights,combined_vars)

        self.assertFloatEqual(combined_obs,exp)