Solved – How to perform a chi-square test for independence on signal samples

categorical datachi-squared-testhistogramindependenceMATLAB

Let's say I have two signals $x$ and $y$, sampled $N$ times, i.e.

$$ x = [ x_{1}, x_{2}, …, x_{N} ] $$
$$ y = [ y_{1}, y_{2}, …, y_{N} ] $$

I would like to check whether $x$ and $y$ are statistically independent, with a certain level of probability.

I have been looking into the Chi-Square Test For Independence. However, since it can be applied to categorical data, I do not know how I can apply it to my signal samples.

As was suggested on this related question, once we compute the histogram for each signal, we do have categories on which a chi-squared test can be applied. But how do we use the histograms to generate the required contingency table?

For what it's worth, I am currently computing the histograms using this code:

n1 = hist(x);
n2 = hist(y);
n3 = hist3([x' y']);

Thank you for your help and suggestions.

EDIT

As an example, two signal samples would be the following:

xx = 0.2:0.2:34;
x = sin(xx);
y = randn(size(xx));

Best Answer

I agree with @rolando2, if your data are continuous, a chi-square test is not really best. I would make a scatterplot and overlay a loess line on it. I don't really know MATLAB, but googling led me to these two pages. I can tell you that in R, the code would be plot(x,y); lines(lowess(y~x)). Although it's true that variables can be dependent while being uncorrelated, you should be able to assess this via your scatterplot. That is because correlation (specifically, Pearson's product-moment correlation) is a measure of linear dependence, so for example, a perfect parabola could be uncorrelated despite being dependent, or a sine wave that ran perfectly horizontally. My point is that you would be able to recognize that the correlation statistic was failing to capture the dependence by looking at your scatterplot. The smoothed fit would make this even easier to see.

On the other hand, what the histograms are doing depends on how the bins are generated, and how well those bins capture the underlying continuous variable. Since you have the underlying continuous variable, why use a more or less bad approximation to it? If you wanted to know how to do this out of purely academic curiosity, you would dice up your continuous variable into bins somehow (perhaps based on theory, perhaps using the automatic binning algorithm in your software, or whatever) for each variable individually, ignoring the other variable. Then you would cross-tabulate your data by counting the number of cases that fell into each 2D bin. As a rule of thumb, you want at least 5 cases in each combination, although it's actually more complicated than that. Then you would simply run a standard chi-squared test for independence. For the record, I don't see any value in pursuing this course.

Related Question