0
$\begingroup$

I am constructing correlated random variables according to the equations given in in this post: How does the formula for generating correlated random variables work?

I understand this equation, although what I don't understand is if I run a bunch of simulations where I calculate the correlation numerically between $Y$ and $X_1$ and calculate the mean of this correlation, it seems to be systematically reduced as compared to the true value $\rho$. It seems, specifically, that there needs to be a "correction factor" to the mean of the correlation of $(N+1)/N$, where $N$ is the number of sampled values. This was just found as a guess, it could also be wrong but seems to fit. Here's the python code to reproduce this issue:

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import ttest_ind


N = 50000
Npoints_list = [2, 100]

corr_values = [0.5]


list_of_ab = []
list_of_cd = []


rand_b_list = [1.0]
std_scale_factor = 1
C = 0
delta = 0

for Npoints in Npoints_list:
    corr_values_ac = []
    for Delta in rand_b_list:
        list_of_ab = []
        list_of_cd = []
        for corr in corr_values:
            p_values_ab = []
            p_values_cd = []
            for ii in range(N):
                a = (np.random.randn((Npoints))*std_scale_factor+C)

                c =corr*a+np.sqrt(1-corr**2)*(C*(1-corr)/np.sqrt(1-corr**2)+np.random.randn((Npoints))*std_scale_factor)
    
                corr_ac = np.corrcoef(a,c)
        
                corr_values_ac.append(corr_ac[1,0])
                
    print(f'mean of corr of ac for {Npoints} points is {np.mean(corr_values_ac)}')
    print(f'mean of corr of ac for {Npoints} points after correction is {np.mean(corr_values_ac)*(Npoints+1)/Npoints}')
$\endgroup$
5
  • $\begingroup$ what is the question? $\endgroup$
    – NN2
    Commented Oct 23 at 9:31
  • $\begingroup$ Hello, sorry perhaps I should have been more clear. Why is the mean of the correlation not the true correlation? I am a bit surprised by this $\endgroup$ Commented Oct 23 at 23:24
  • $\begingroup$ Your estimate (not calculation!) of the correlation is biased. This is a good demonstration of that fact. $\endgroup$
    – whuber
    Commented Nov 17 at 17:46
  • $\begingroup$ OK, I believe that. Is there an unbiased estimator? I assume that (N+1)/N correction factor is unbiased, but do you know how/where that's derived? $\endgroup$ Commented Nov 18 at 0:50
  • $\begingroup$ That correction is used for unbiased estimates of covariance. That lack of bias does not translate to correlation. You can find derivations sprinkled in (literally) hundreds of posts here on CV. Have you considered searching our site? Variance bias correction picks up some good hits. A similar search for correlation bias gives a useful answer at stats.stackexchange.com/questions/96987. $\endgroup$
    – whuber
    Commented Nov 19 at 11:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.