Question edited with the correct code - apologies
I have observed a somewhat puzzling negative correlation.
In code (R) (mathematical formulation below)
set.seed(123)
N=1000
R1=vector('double',N)
R2=vector('double',N)
x=rnorm(100)
for (k in 1:N){
y1=x+rnorm(100)
y2=x+rnorm(100)
X=c(x,x)
Y=c(y1,y2)
R1[k]=cor(y1,y2)
R2[k]=var(X)/var(Y)
}
cor.test(R1,R2)
Pearson's product-moment correlation
data: R1 and R2
t = -7.2284, df = 998, p-value = 9.704e-13
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.2811533 -0.1633130
sample estimates:
cor
-0.2230479
It turns out that R1
and R2
are very close to each other but negatively correlated. This is only the case when x
is kept out of the loop (i.e. fix across experiments).
Would you know why?
Mathematical formulation
Let
\begin{align} & x_i \sim \operatorname N(0,1),\quad i=1,\ldots,100 \quad (\sim\text{indicates i.i.d.}) \\[6pt] & y_{1,i} = x_i + u_i, \quad u_i\sim\operatorname N(0,1) \\[6pt] & y_{2,i} = x_i + v_i, \quad v_i\sim\operatorname N(0,1) \end{align}
We can arrange these random variables as vectors:
\begin{align} & \mathbf x = [x_i]_{i=1,\ldots,100} \\[6pt] & \mathbf y_1 = [y_{1,i}]_{i=1,\ldots,100} \\[6pt] & \mathbf y_2 = [y_{2,i}]_{i=1,\ldots,100} \end{align}
And we now create the concatenated vectors as follows:
\begin{align} & \mathbf X = [\mathbf x, \mathbf x] \\[6pt] & \mathbf Y = [\mathbf y_1, \mathbf y_2] \end{align}
It turns out that the Pearson's correlation between y1$\mathbf y_1$ and y2$\mathbf y_2$ is very close to Var(X)/Var(Y).$\operatorname{Var}(X)/\operatorname{Var}(Y).$ However, the two are negatively correlated when we repeat the process of realising y1,i,$y_{1,i}$ and yi$y_{2,i}$ while maintaining xi$x_i$ constant across realisations.
Would you know why?