Exemplo n.º 1
0
def phik_from_hist2d(observed: np.ndarray, expected: np.ndarray, noise_correction: bool = True) -> float:
    """
    correlation coefficient of bivariate gaussian derived from chi2-value

    Chi2-value gets converted into correlation coefficient of bivariate gauss
    with correlation value rho, assuming giving binning and number of records.
    Correlation coefficient value is between 0 and 1.

    Bivariate gaussian's range is set to [-5,5] by construction.

    :param observed: 2d-array observed values
    :param expected: 2d-array expected values
    :param bool noise_correction: apply noise correction in phik calculation
    :returns float: correlation coefficient phik
    """
    if isinstance(observed, pd.DataFrame):
        observed = observed.values
    if isinstance(expected, pd.DataFrame):
        expected = expected.values

    # important to ensure that observed and expected have same normalization
    expected = expected * (np.sum(observed) / np.sum(expected))

    # chi2 contingency test
    chi2 = chi_square(observed, expected, lambda_='pearson')

    # noise pedestal
    endof = estimate_simple_ndof(observed) if noise_correction else 0
    pedestal = endof
    if pedestal < 0:
        pedestal = 0

    # phik calculation adds noise pedestal to theoretical chi2
    return phik_from_chi2(chi2, observed.sum(), *observed.shape, pedestal=pedestal)
Exemplo n.º 2
0
    def test_phik_calculation(self):
        """Test the calculation of Phi_K"""

        chi2 = bivariate.chi2_from_phik(0.5, 1000, nx=10, ny=10)
        self.assertTrue( np.isclose(chi2, 271.16068979654125, 1e-6) )

        phik = bivariate.phik_from_chi2(chi2, 1000, 10, 10)
        self.assertTrue( np.isclose(phik, 0.5, 1e-6) )