GithubHelp home page GithubHelp logo

michielstock / xicor.jl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stefftaelman/xicor.jl

0.0 2.0 0.0 17 KB

An implementation of the Xi Correlation coefficient and hypothesis test as originally described by Chatterjee (2021).

License: MIT License

Julia 100.00%

xicor.jl's Introduction

Xicor.jl

An implementation of the Xi Correlation coefficient and hypothesis test as originally described by Chatterjee (2021). The implementation is based on the XICOR CRAN package by Chatterjee and Holmes.

Installation

Xicor.jl is available from the Julia registry, you can install it locally from your Julia REPL by typing:

] add Xicor

Running

Xicor.jl allows to eaily calculate the Xi correlation coefficient between two vectors $X$ and $Y$.

Calculating Xi correlation

xvec = rand(100)
yvec = rand(100)
xicor(xvec, yvec)

Dependence testing

Aside retrieving a correlation coefficient, Xicor.jl also allows to test for dependence between two vectors $X$ and $Y$. This is possible by using the xicor fucntion with the keyword argument 'pvalue' set to true. The function will then return a tuple of the Xi correlation coefficient, the standard deviation, and the associated p-value.

xicor(xvec, yvec; pvalue=true, method="asymptotic")

The method keyword argument allows to choose between an asymptotic approximation test and a Monte Carlo permutation test. The asymptotic approximation test is based on the asymptotic normal distribution of the Xi correlation coefficient. The Monte Carlo permutation test is based on the permutation distribution of the Xi correlation coefficient. The Monte Carlo permutation test also uses a keyword argument nperm to specify the number of permutations to be used. The default value is 1000.

xicor(xvec, yvec; pvalue=true, method="permutation", nperm=1000)

Application

Due to the asymmetry of the Xi correlation coefficient, it finds an interesting application in the inference of directionality of cause-effect relationships. To test its strengths in this endeavor, we apply the coefficient on the Tuebingen cause-effect pairs database described in Mooij et al. (2017). The data can be downloaded from https://webdav.tuebingen.mpg.de/cause-effect/pairs.zip, and consists of a number of txt files, each containing a list of cause-effect pairs and description files containing information about the data sources and the ground truth direction of the causal relationships.

We test it here on the first pair file, which contains data from the German Weather Service on the relationship between temperature and altitude. The data is loaded in and the Xi correlation coefficient is calculated for both directions of the causal relationship. The direction with the higher Xi correlation coefficient is then chosen as the predicted direction of the causal relationship.

    # load in the data
    pair_values = hcat(
        [parse.(
            Float64, 
            [q for q in split(i, r"\s+") if q != ""]
            ) for i in split(chomp(readchomp("./data/pair0001.txt")), "\n")
        ]...
        )
    altitudes = pair_values[1, :]
    temperatures = pair_values[2, :]

    # calculate the Xi correlation coefficient for both directions
    using Xicor
    xy_xi, xy_sd, xy_p = xicor(altitudes, temperatures; pvalue=true)
    yx_xi, yx_sd, yx_p = xicor(temperatures, altitudes; pvalue=true)
    predicted_direction = xy_xi > yx_xi ? "Altitude causes temperature" : "Temperature causes altitude"
    println("Predicted direction: $predicted_direction")

And it correctly predicts the direction of the causal relationship in this case.

For a deeper exploration of directional inference using Xi correlation, check out the causal direction prediction example.

xicor.jl's People

Contributors

stefftaelman avatar michielstock avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.