Skip to content
Prev 376779 / 398502 Next

Bug : Autocorrelation in sample drawn from stats::rnorm

Did you take into account that the sample serial correlation coefficient has a bias of approximately -1/T (with T the sample size)? Its variance is approximately 1/T.
Jan Annaert



-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of hmh
Sent: donderdag 4 oktober 2018 12:09
To: R <r-help at r-project.org>
Subject: [R] Bug : Autocorrelation in sample drawn from stats::rnorm

Hi,


I just noticed the following bug:

When we draw a random sample using the function stats::rnorm, there should be not auto-correlation in the sample. But their is some auto-correlation _when the sample that is drawn is small_.

I describe the problem using two functions:

DistributionAutocorrelation_Unexpected which as the wrong behavior : 
_when drawing some small samples using rnorm, there is generally a strong negative auto-correlation in the sample_.

and

DistributionAutocorrelation_Expected which illustrate the expected behavior



*Unexpected : *

DistributionAutocorrelation_Unexpected = function(SampleSize){
 ? Cor = NULL
 ? for(repetition in 1:1e5){
 ??? X = rnorm(SampleSize)
 ??? Cor[repetition] = cor(X[-1],X[-length(X)])
 ? }
 ? return(Cor)
}

par(mfrow=c(3,3))
for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){
hist(DistributionAutocorrelation_Unexpected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_))
; abline(v=0,col=2)
}

output:


*Expected**:*

DistributionAutocorrelation_Expected = function(SampleSize){
 ? Cor = NULL
 ? for(repetition in 1:1e5){
 ??? X = rnorm(SampleSize)
*??? Cor[repetition] = cor(sample(X[-1]),sample(X[-length(X)]))*
 ? }
 ? return(Cor)
}

par(mfrow=c(3,3))
for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){
hist(DistributionAutocorrelation_Expected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_))
; abline(v=0,col=2)
}




Some more information you might need:


packageDescription("stats")
Package: stats
Version: 3.5.1
Priority: base
Title: The R Stats Package
Author: R Core Team and contributors worldwide
Maintainer: R Core Team <R-core at r-project.org>
Description: R statistical functions.
License: Part of R 3.5.1
Imports: utils, grDevices, graphics
Suggests: MASS, Matrix, SuppDists, methods, stats4
NeedsCompilation: yes
Built: R 3.5.1; x86_64-pc-linux-gnu; 2018-07-03 02:12:37 UTC; unix

Thanks for correcting that.

fill free to ask any further information you would need.

cheers,

hugo