[R] Bug : Autocorrelation in sample drawn from stats::rnorm

Annaert Jan j@n@@nn@ert @end|ng |rom u@ntwerpen@be
Thu Oct 4 13:23:59 CEST 2018

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 using r-project.org> On Behalf Of hmh
Sent: donderdag 4 oktober 2018 12:09
To: R <r-help using r-project.org>
Subject: [R] Bug : Autocorrelation in sample drawn from stats::rnorm


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_.


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)])

for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){
; abline(v=0,col=2)



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

for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){
; abline(v=0,col=2)

Some more information you might need:

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 using 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.



- no title specified

Hugo Mathé-Hubert


Laboratoire Interdisciplinaire des Environnements Continentaux (LIEC)

UMR 7360 CNRS -  Bât IBISE

Université de Lorraine  -  UFR SciFA

8, Rue du Général Delestraint

F-57070 METZ

+33(0)9 77 21 66 66
- - - - - - - - - - - - - - - - - -
Les réflexions naissent dans les doutes et meurent dans les certitudes. 
Les doutes sont donc un signe de force et les certitudes un signe de 
faiblesse. La plupart des gens sont pourtant certains du contraire.
- - - - - - - - - - - - - - - - - -
Thoughts appear from doubts and die in convictions. Therefore, doubts 
are an indication of strength and convictions an indication of weakness. 
Yet, most people believe the opposite.

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list