Package: phik
Version: 0.12.4. Released: Jan 2024Release notes:https://github.com/KaveIO/PhiK/blob/master/CHANGES.rstRepository:https://github.com/kaveio/phikDocumentation:https://phik.readthedocs.ioPublication:[offical][arxiv pre-print]Phi_K is a practical correlation constant that works consistently between categorical, ordinal and interval variables.
It is based on several refinements to Pearson’s hypothesis test of independence of two variables. Essentially, the
contingency test statistic of two variables is interpreted as coming from a rotated bi-variate normal distribution,
where the tilt is interpreted as Phi_K.The combined features of Phi_K form an advantage over existing coefficients. First, it works consistently between categorical, ordinal and interval variables.
Second, it captures non-linear dependency. Third, it reverts to the Pearson correlation coefficient in case of a bi-variate normal input distribution.
These are useful features when studying the correlation matrix of variables with mixed types.For details on the methodology behind the calculations, please see our publication. Emphasis is paid to the proper evaluation of statistical significance of correlations and to the interpretation of variable relationships
in a contingency table, in particular in case of low statistics samples.
The presented algorithms are easy to use and available through this public Python library.Example notebooksStatic linkGoogle Colab linkbasic tutorialbasic on colabadvanced tutorial (detailed configuration)advanced on colabspark tutorialno spark availableDocumentationThe entire Phi_K documentation including tutorials can be found atread-the-docs.
See the tutorials for detailed examples on how to run the code with pandas. We also have one example on how
calculate the Phi_K correlation matrix for a spark dataframe.Check it outThe Phi_K library requires Python >= 3.8 and is pip friendly. To get started, simply do:$pipinstallphikor check out the code from out GitHub repository:$gitclonehttps://github.com/KaveIO/PhiK.git$pipinstall-ePhiK/where in this example the code is installed in edit mode (option -e).You can now use the package in Python with:importphikCongratulations, you are now ready to use the PhiK correlation analyzer library!Quick runAs a quick example, you can do:importpandasaspdimportphikfromphikimportresources,report# open fake car insurance datadf=pd.read_csv(resources.fixture('fake_insurance_data.csv.gz'))df.head()# Pearson's correlation matrix between numeric variables (pandas functionality)df.corr()# get the phi_k correlation matrix between all variablesdf.phik_matrix()# get global correlations based on phi_k correlation matrixdf.global_phik()# get the significance matrix (expressed as one-sided Z)# of the hypothesis test of each variable-pair dependencydf.significance_matrix()# contingency table of two columnscols=['mileage','car_size']df[cols].hist2d()# normalized residuals of contingency test applied to colsdf[cols].outlier_significance_matrix()# show the normalized residuals of each variable-pairdf.outlier_significance_matrices()# generate a phik correlation report and save as test.pdfreport.correlation_report(df,pdf_file_name='test.pdf')For all available examples, please see thetutorialsat read-the-docs.Contact and supportIssues and Ideas:https://github.com/kaveio/phik/issuesPlease note that support is (only) provided on a best-effort basis.