您似乎将样本相关系数与理论相关系数混淆了。前者是一个随机值,由模拟中产生的(随机)信号产生;后者是一个数字,它是从信号生成过程的统计模型中计算出来的。
您在代码中计算的是 sample 相关系数,它取决于随机生成的实际信号(代码中的a 和b)。这些信号是随机过程的实现(在你的例子中是白色高斯过程,因为你使用randn)。
另一方面,理论相关系数由随机过程的统计表征决定,这些随机过程会产生您生成的两个随机过程的信号。所以它不是从模拟中获得的(如在您的代码中),而是通过数学计算得出的。
您的情况下的理论相关性为 0,因为随机过程是独立的。请注意,我知道这一点 来自代码(来自 如何你生成信号),而不是来自代码碰巧生成的实际值。这就是我所说的理论值的意思:它是根据您对如何生成实际信号的了解计算得出的。
样本相关性可以作为理论相关性的估计;并且随着信号大小的增加,估计会变得更好。这是law of large numbers。因此,您设置的样本量越大(代码中的1e6),结果(样本相关系数)就越集中在0(理论相关系数)附近。
为了说明这一点,我做了10 组1000 模拟,每组都有不同的样本量。因此,对于每个样本量,我收集1000 样本相关系数的不同值并计算 直方图 以查看这些值的分布情况。该图证实,随着样本量的增加,直方图变窄(变高),说明样本相关系数更集中在理论值 0 附近。
生成图形的代码(Matlab R2015b)为:
S = 1e5:1e5:1e6; %// sample sizes
N = 1000; %// number of repetitions to generate histogram
binlimits = [-.015 .015]; %// set manually depending on S
B = 31; %// number of bins in the histogram
stretch = 7; %// stretch factor for plotting the histograms
result = NaN(numel(S),B); %// preallocate
for m = 1:numel(S)
cc = NaN(1,S(m));
for n = 1:N
a = randn(1,S(m));
b = randn(1,S(m));
c = corrcoef(a,b);
cc(n) = c(2,1); %// correlation coefficient
end
[hist, edges] = histcounts(cc,31,'BinLimits',binlimits,'Normalization','pdf');
result(m,:) = hist; %// histogram of correlation coefficient for this sample size
end
bins = (edges(1:end-1) + edges(2:end))/2; %// axis for plotting the histograms
resultbar = NaN(numel(S)*stretch,B);
resultbar(1:stretch:end,:) = result; %// separate the histograms for better visualization
h = bar3(bins, resultbar.'); %'// plot histograms
set(gca,'xtick',1:stretch:numel(h),'xticklabels',S)
delete(h(mod(0:numel(h)-1,stretch)>0)) %// remove zeros
xlabel('Sample correlation coefficient')
ylabel('Sample size')