# Compute Correlation between two binary variables

$X_1$ and $X_2$ are two binary stochastic variables with simultaneous probabilitydistribution:

$$\begin{matrix} &X_2=0&X_2=1\\ X_1=0&0.30&0.17\\ X_1=1&0.08&0.45 \end{matrix}$$

I want to calculate the correlation between $X_1$ and $X_2$.

Correlation[{{0.30, 0.17}, {0.08, 0.45}}]


The output I get is {{1,-1},{-1,1}}

The solution I seek is: 0.05011

It should be pretty simple, but I have no idea where I am wrong.

You could set this up in symbolic form as a bivariate distribution with pmf $f(x,y)$: Then, using the mathStatica add-on to Mathematica, the correlation you seek is:

Corr[{x, y}, f]


$\frac{607}{\sqrt{1467199}}$

Note that this is slightly different to the solution you posted,as the numerical value is: 0.501123... (not 0.0501).

You can make Mma do this operation too, by itself, as per:

dist = ProbabilityDistribution[f, {x, 0, 1, 1}, {y, 0, 1, 1}]


where f is the piecewise function above, and then evaluate:

Correlation[dist]


The problem with your use of Correlation[{{0.30, 0.17}, {0.08, 0.45}}] is this ... You could use Correlation[xdata, ydata] to find the sample correlation between xdata and ydata ... but (a) you are not seeking a sample correlation ... you are seeking the population correlation, and (b) {{0.30, 0.17}, {0.08, 0.45}} is not your data ... it represents the pmf or distribution of the population data.

• Great solution. When I try to work futher with the ProbabilityDistribution-function (´dist´), then I cannot use it with Probability[...] I have an example with $X_1$ conditioned $X_1 \neq X_2$. How come? – Jens Jensen Aug 6 '13 at 11:08
• Would need to see the exact example to be able to comment on that ... – wolfies Aug 6 '13 at 11:17
• How would you calculate: $P(X_1=0|X_1 \neq X_2)$ and $P(X_1=1|X_1 \neq X_2)$. If it is any help. the results are 0.68 and 0.32 – Jens Jensen Aug 6 '13 at 11:22
• You could continue: Probability[x == 0 \[Conditioned] x != y, {x, y} \[Distributed] dist] ... and ... Probability[x == 1 \[Conditioned] x != y, {x, y} \[Distributed] dist] .. works fine – wolfies Aug 6 '13 at 11:39
• Checked it in v8 and v9 ->> works fine on both on my Mac. Suggest check your input carefully ... or quit kernel and start from fresh :) – wolfies Aug 6 '13 at 12:09