Ejemplo n.º 1
0
    def __init__(self, refsample, refweights=None, ref_label='Reference'):
        """refsample is the reference (data) sample to be compared to."""

        self.ref         = np.asarray(refsample.ravel())
        if refweights is None:
            self.weights = None
        else:
            self.weights = np.asarray(refweights.ravel())
        self.ref_mean    = np.ma.average(self.ref, weights=self.weights)
        self.ref_STD     = weighted_std(self.ref, weights=self.weights)
        self.ref_label   = ref_label
Ejemplo n.º 2
0
    def __init__(self, refsample, refweights=None, ref_label='Reference'):
        """refsample is the reference (data) sample to be compared to."""

        self.ref = np.asarray(refsample.ravel())
        if refweights is None:
            self.weights = None
        else:
            self.weights = np.asarray(refweights.ravel())
        self.ref_mean = np.ma.average(self.ref, weights=self.weights)
        self.ref_STD = weighted_std(self.ref, weights=self.weights)
        self.ref_label = ref_label
Ejemplo n.º 3
0
    def get_coords(self, name, sample, weights=None, print_bool=False):
        """Computes theta=arccos(correlation),rad=stddev of sample
        wrt. reference sample."""

        # flatten
        my_sample  = sample.ravel()
        if weights is None:
            my_weights = weights
        else:
            my_weights = weights.ravel()
        # (weighted) average and standard deviation of the sample
        ave = np.ma.average(my_sample, weights=my_weights)
        std = weighted_std(my_sample, weights=my_weights)
        # (weighted) correlation coefficient of the sample with the reference
        # after http://wapedia.mobi/en/Pearson_product-moment_correlation_coefficient?p=1
        # NO WEIGHTS: corr = np.corrcoef(self.ref, my_sample) # [[1,rho],[rho,1]]
        if my_weights is None:
            my_weights = np.ones_like(self.ref)
        cov_x_y = ( np.ma.sum( my_weights
                                * (my_sample - ave)
                                * (self.ref - self.ref_mean) )
                     / np.ma.sum( my_weights ) )
        cov_x_x = ( np.ma.sum( my_weights
                                * (my_sample - ave)**2)
                     / np.ma.sum( my_weights ) )
        cov_y_y = ( np.ma.sum( my_weights
                                * (self.ref - self.ref_mean)**2 )
                     / np.ma.sum( my_weights ) )
        corr    = 1. * cov_x_y / (cov_x_x * cov_y_y)**0.5
        theta = np.arccos(corr)
        ## # info to see how much does corr coeff change when use weighted corr vs non-weighted corr
        ## print '#' * 80
        ## mtxt = "corr WITHOUT weights = %.2f %%, corr WITH weights = %.2f %%, abs diff = %.2f %%, rel diff = %.2f %%, "
        ## no_weight_corr = np.corrcoef(self.ref, my_sample)[0,1]
        ## mtup = (100. * no_weight_corr,
        ##         100. * corr,
        ##         100. * abs(corr - no_weight_corr),
        ##         100. * 2 * abs(corr - no_weight_corr) / (corr + no_weight_corr)
        ##         )
        ## print mtxt %mtup
        ## print '#' * 80
        ## #
        if print_bool:
            print "std=%.2f and corr=%.2f"%(std, corr[0,1]), "for %s"%name
        return theta, std, corr
Ejemplo n.º 4
0
    def get_coords(self, name, sample, weights=None, print_bool=False):
        """Computes theta=arccos(correlation),rad=stddev of sample
        wrt. reference sample."""

        # flatten
        my_sample = sample.ravel()
        if weights is None:
            my_weights = weights
        else:
            my_weights = weights.ravel()
        # (weighted) average and standard deviation of the sample
        ave = np.ma.average(my_sample, weights=my_weights)
        std = weighted_std(my_sample, weights=my_weights)
        # (weighted) correlation coefficient of the sample with the reference
        # after http://wapedia.mobi/en/Pearson_product-moment_correlation_coefficient?p=1
        # NO WEIGHTS: corr = np.corrcoef(self.ref, my_sample) # [[1,rho],[rho,1]]
        if my_weights is None:
            my_weights = np.ones_like(self.ref)
        cov_x_y = (np.ma.sum(my_weights * (my_sample - ave) *
                             (self.ref - self.ref_mean)) /
                   np.ma.sum(my_weights))
        cov_x_x = (np.ma.sum(my_weights * (my_sample - ave)**2) /
                   np.ma.sum(my_weights))
        cov_y_y = (np.ma.sum(my_weights * (self.ref - self.ref_mean)**2) /
                   np.ma.sum(my_weights))
        corr = 1. * cov_x_y / (cov_x_x * cov_y_y)**0.5
        theta = np.arccos(corr)
        ## # info to see how much does corr coeff change when use weighted corr vs non-weighted corr
        ## print '#' * 80
        ## mtxt = "corr WITHOUT weights = %.2f %%, corr WITH weights = %.2f %%, abs diff = %.2f %%, rel diff = %.2f %%, "
        ## no_weight_corr = np.corrcoef(self.ref, my_sample)[0,1]
        ## mtup = (100. * no_weight_corr,
        ##         100. * corr,
        ##         100. * abs(corr - no_weight_corr),
        ##         100. * 2 * abs(corr - no_weight_corr) / (corr + no_weight_corr)
        ##         )
        ## print mtxt %mtup
        ## print '#' * 80
        ## #
        if print_bool:
            print "std=%.2f and corr=%.2f" % (std, corr[0, 1]), "for %s" % name
        return theta, std, corr
Ejemplo n.º 5
0
    def check_sample(self, sample, weights=None, threshold=1e-12):
        """Check for the sample if the following relation holds:
        RMS^2 - STD^2 - STD_ref^2 + 2*STD*STD_ref*COR < threshold.
        """

        my_sample = sample.ravel()
        if weights is None:
            my_weights = weights
        else:
            my_weights = weights.ravel()
        means = np.ma.average(my_sample, weights=my_weights)
        STDs  = weighted_std(my_sample, weights=my_weights)
        terms = ((self.ref - self.ref_mean) - (my_sample - means))**2
        RMSs = (1. * np.sum(terms) / np.size(self.ref))**0.5
        # (weighted) correlation coefficient of the sample with the reference
        # after http://wapedia.mobi/en/Pearson_product-moment_correlation_coefficient?p=1
        # NO WEIGHTS: CORs = np.corrcoef(self.ref, my_sample)[0,1]  # [[1,rho],[rho,1]]
        if my_weights is None:
            my_weights = 1.
        cov_x_y = ( np.ma.sum( my_weights
                                * (my_sample - means)
                                * (self.ref - self.ref_mean) )
                     / np.ma.sum( my_weights ) )
        cov_x_x = ( np.ma.sum( my_weights
                                * (my_sample - means)**2)
                     / np.ma.sum( my_weights ) )
        cov_y_y = ( np.ma.sum( my_weights
                                * (self.ref - self.ref_mean)**2 )
                     / np.ma.sum( my_weights ) )
        CORs    = 1. * cov_x_y / (cov_x_x * cov_y_y)**0.5
        value = abs(RMSs - (STDs**2 + self.ref_STD**2 - 2*STDs*self.ref_STD*CORs)**0.5)
        test = value < threshold
        ## a = RMSs
        ## b = (STDs**2 + self.ref_STD**2 - 2*STDs*self.ref_STD*CORs)**0.5
        ## value1 = abs(2. * (a - b) / (a + b))
        ## value2 = abs(a - b)
        ## print "BITE, val = %.2e, val1 = %.2e, val2 = %.2e" %(value, value1, value2)
        return test, value
Ejemplo n.º 6
0
    def check_sample(self, sample, weights=None, threshold=1e-12):
        """Check for the sample if the following relation holds:
        RMS^2 - STD^2 - STD_ref^2 + 2*STD*STD_ref*COR < threshold.
        """

        my_sample = sample.ravel()
        if weights is None:
            my_weights = weights
        else:
            my_weights = weights.ravel()
        means = np.ma.average(my_sample, weights=my_weights)
        STDs = weighted_std(my_sample, weights=my_weights)
        terms = ((self.ref - self.ref_mean) - (my_sample - means))**2
        RMSs = (1. * np.sum(terms) / np.size(self.ref))**0.5
        # (weighted) correlation coefficient of the sample with the reference
        # after http://wapedia.mobi/en/Pearson_product-moment_correlation_coefficient?p=1
        # NO WEIGHTS: CORs = np.corrcoef(self.ref, my_sample)[0,1]  # [[1,rho],[rho,1]]
        if my_weights is None:
            my_weights = 1.
        cov_x_y = (np.ma.sum(my_weights * (my_sample - means) *
                             (self.ref - self.ref_mean)) /
                   np.ma.sum(my_weights))
        cov_x_x = (np.ma.sum(my_weights * (my_sample - means)**2) /
                   np.ma.sum(my_weights))
        cov_y_y = (np.ma.sum(my_weights * (self.ref - self.ref_mean)**2) /
                   np.ma.sum(my_weights))
        CORs = 1. * cov_x_y / (cov_x_x * cov_y_y)**0.5
        value = abs(RMSs - (STDs**2 + self.ref_STD**2 -
                            2 * STDs * self.ref_STD * CORs)**0.5)
        test = value < threshold
        ## a = RMSs
        ## b = (STDs**2 + self.ref_STD**2 - 2*STDs*self.ref_STD*CORs)**0.5
        ## value1 = abs(2. * (a - b) / (a + b))
        ## value2 = abs(a - b)
        ## print "BITE, val = %.2e, val1 = %.2e, val2 = %.2e" %(value, value1, value2)
        return test, value