Marc MarĂ Dell'Olmo
2014-Apr-01 12:56 UTC
[R] A vector of normal distributed values with a sum-to-zero constraint
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
Jeff Newmiller
2014-Apr-01 13:27 UTC
[R] A vector of normal distributed values with a sum-to-zero constraint
You are on a fool's errand. Read FAQ 7.31.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On April 1, 2014 5:56:24 AM PDT, "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.
Boris Steipe
2014-Apr-01 13:29 UTC
[R] A vector of normal distributed values with a sum-to-zero constraint
Make a copy with opposite sign. This is Normal, symmetric, but no longer random. set.seed(112358) x <- rnorm(5000, 0, 0.5) x <- c(x, -x) sum(x) hist(x) B. On 2014-04-01, at 8:56 AM, Marc Mar? Dell'Olmo 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.
Greg Snow
2014-Apr-01 18:55 UTC
[R] A vector of normal distributed values with a sum-to-zero constraint
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 eigen(v) x <- mvrnorm(100,mu=rep(0,9), Sigma=v, empirical=TRUE) rowSums(x) range(.Last.value) hist(x) sd(x) mean(x) apply(x,2,sd) 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