[R] A vector of normal distributed values with a sum-to-zero constraint

Greg Snow 538280 at gmail.com
Tue Apr 1 20:55:50 CEST 2014

Here is one approach to generating a set (or in this case multiple
sets) of normals that sum to 0 (with a little round off error) and
works for an odd number of points:

v <- matrix(-1/8, 9, 9)
diag(v) <- 1
x <- mvrnorm(100,mu=rep(0,9), Sigma=v, empirical=TRUE)

the key is to find the value of the off diagonals in the covariance
matrix that gives you exactly one eigenvalue that is equal to 0 (or
close enough with rounding) and all the others are positive.  There is
probably a mathematical formula that gives the exact value to use, but
I found one that works with a little trial and error (it will change
for different sample sizes).

On Tue, Apr 1, 2014 at 6:56 AM, Marc Marí Dell'Olmo
<marceivissa at gmail.com> wrote:
> Dear all,
> Anyone knows how to generate a vector of Normal distributed values
> (for example N(0,0.5)), but with a sum-to-zero constraint??
> The sum would be exactly zero, without decimals.
> I made some attempts:
>> l <- 1000000
>> aux <- rnorm(l,0,0.5)
>> s <- sum(aux)/l
>> aux2 <- aux-s
>> sum(aux2)
> [1] -0.000000000006131392
>> aux[1]<- -sum(aux[2:l])
>> sum(aux)
> [1] -0.00000000000003530422
> but the sum is not exactly zero and not all parameters are N(0,0.5)
> distributed...
> Perhaps is obvious but I can't find the way to do it..
> Thank you very much!
> Marc
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com

More information about the R-help mailing list