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

hmh hugomh @ending from gmx@fr
Thu Oct 4 12:09:01 CEST 2018


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

cheers,

hugo


-- 
- no title specified

Hugo Mathé-Hubert

ATER

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.



More information about the R-help mailing list