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

2014-04-02 Thread peter dalgaard

On 01 Apr 2014, at 17:22 , Rui Barradas  wrote:

> Hello,
> 
> One way is to use ?scale.
> 

...except that the sd will be less than 0.5 (not obvious at n=1e6, though). 
However, if you want

- normal distribution
- symmetry
- constant marginal variance of sigma^2
- fixed sum = 0 

I don't see any way more straightforward than generating n normal variates and 
subtracting the mean. The only snag is that the variance of a residual is 
sigma^2(1-1/n), so generate the original data with a variance of 
sigma^2/(1-1/n) = n/(n-1) sigma^2

I.e.

x <- rnorm(n,0,0.5*sqrt(n/(n-1)))
x <- x - mean(x) 

All of this applies within the boundaries of numerical precision. You're not 
going to beat the FPU:

> n <- 1e6
> x <- rnorm(n,0,0.5*sqrt(n/(n-1)))
> x <- x - mean(x) 
> sum(x)
[1] -1.625718e-11
> mean(x)
[1] -1.452682e-17

The problem of getting a sum or mean of _exactly_ 0 is just not well-defined, 
since sums and averages depend on the summation order:

> sum(x)
[1] -1.625718e-11
> sum(sample(x))
[1] -1.624851e-11
> sum(sort(x))
[1] -1.508771e-11
> sum(rev(sort(x)))
[1] -1.599831e-11


 


> set.seed(4867)
> l <- 100
> aux <- rnorm(l, 0, 0.5)
> aux <- scale(aux, scale = FALSE)
> sum(aux)
> 
> hist(aux, prob = TRUE)
> curve(dnorm(x, 0, 0.5), from = -2, to = 2, add = TRUE)
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 01-04-2014 16:01, jlu...@ria.buffalo.edu escreveu:
>> Then what's wrong with centering your initial values around the mean?
>> 
>> 
>> 
>> Marc Marí Dell'Olmo 
>> 04/01/2014 10:56 AM
>> 
>> To
>> Boris Steipe ,
>> cc
>> jlu...@ria.buffalo.edu, "r-help@r-project.org" 
>> Subject
>> Re: [R] A vector of normal distributed values with a sum-to-zero
>> constraint
>> 
>> 
>> 
>> 
>> 
>> 
>> Boris is right. I need this vector to include as initial values of a
>> MCMC process (with openbugs) and If I use this last approach sum(x)
>> could be a large (or extreme) value and can cause problems.
>> 
>> The other approach x <- c(x, -x) has the problem that only vectors
>> with even values are obtained.
>> 
>> Thank you!
>> 
>> 
>> 2014-04-01 16:25 GMT+02:00 Boris Steipe :
>>> But the result is not Normal. Consider:
>>> 
>>> set.seed(112358)
>>> N <- 100
>>> x <- rnorm(N-1)
>>> sum(x)
>>> 
>>> [1] 1.759446   !!!
>>> 
>>> i.e. you have an outlier at 1.7 sigma, and for larger N...
>>> 
>>> set.seed(112358)
>>> N <- 1
>>> x <- rnorm(N-1)
>>> sum(x)
>>> [1] -91.19731
>>> 
>>> B.
>>> 
>>> 
>>> On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:
>>> 
>>>> The sum-to-zero constraint imposes a loss of one degree of freedom.  Of
>>  N samples, only (N-1) can be random.   Thus the solution is
>>>>> N <- 100
>>>>> x <- rnorm(N-1)
>>>>> x <- c(x, -sum(x))
>>>>> sum(x)
>>>> [1] -7.199102e-17
>>>> 
>>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> Boris Steipe 
>>>> Sent by: r-help-boun...@r-project.org
>>>> 04/01/2014 09:29 AM
>>>> 
>>>> To
>>>> Marc Marí Dell'Olmo ,
>>>> cc
>>>> "r-help@r-project.org" 
>>>> Subject
>>>> Re: [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 <- 100
>>>>>> aux <- rnorm(l,0,0.5)
>>>&

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

2014-04-02 Thread Frede Aakmann Tøgersen
Hi Marc

I think that we could help you better if we knew in which context you need 
sample from a sum constrained normal distribution. However this is more a 
question on probability theory than on how to do it in R.

The proposal so far has been linear transformation of multivariate normal 
distribution (Marc, Rui), mixture of normal and reflected normal distribution 
(Boris, try that with e.g. mu = 2), normal distribution mixed with single point 
with positive mass (Jlucke), degenerated normal distribution (Greg).

What you in fact want to do is to draw samples from a conditional distribution. 
The condition is the sum constraint so if we have x = (x1, x2, ..., xn) then 
sum_{i=1}^n xi = 0 or x1 + x2 + ... x{n-1} = xn so you want to draw samples 
from P(x given that x is normal distributed and sum(x)=0). The sum constraint 
gives in fact what is called distributions on the simplex. Google for "normal 
distribution simplex" and you will get almost 2 mill hits. The second shows how 
to sample using Gibbs sampling 
(http://dobigeon.perso.enseeiht.fr/papers/Dobigeon_TechReport_2007b.pdf).

However you can probably just use other distributions given sum constraint 
since you say that you only need the sample as initial values for a MCMC 
algorithm. Many methods are available from "compositional statistics" (google 
for that, Aitchison 1986 is the pioneer). 

At least two packages are available for R:"compositions" with the latest 
version from 2013 and can be found in the archives and "robComposition" still 
maintained.

Hope that helps, it is help for yourself to find a solution.


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
fr...@vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of Marc Marí Dell'Olmo
> Sent: 1. april 2014 16:57
> To: Boris Steipe
> Cc: r-help@r-project.org
> Subject: Re: [R] A vector of normal distributed values with a sum-to-zero
> constraint
> 
> Boris is right. I need this vector to include as initial values of a
> MCMC process (with openbugs) and If I use this last approach sum(x)
> could be a large (or extreme) value and can cause problems.
> 
> The other approach x <- c(x, -x) has the problem that only vectors
> with even values are obtained.
> 
> Thank you!
> 
> 
> 2014-04-01 16:25 GMT+02:00 Boris Steipe :
> > But the result is not Normal. Consider:
> >
> > set.seed(112358)
> > N <- 100
> > x <- rnorm(N-1)
> > sum(x)
> >
> > [1] 1.759446   !!!
> >
> > i.e. you have an outlier at 1.7 sigma, and for larger N...
> >
> > set.seed(112358)
> > N <- 1
> > x <- rnorm(N-1)
> > sum(x)
> > [1] -91.19731
> >
> > B.
> >
> >
> > On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:
> >
> >> The sum-to-zero constraint imposes a loss of one degree of freedom.  Of
> N samples, only (N-1) can be random.   Thus the solution is
> >> > N <- 100
> >> > x <- rnorm(N-1)
> >> > x <- c(x, -sum(x))
> >> > sum(x)
> >> [1] -7.199102e-17
> >>
> >> >
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> Boris Steipe 
> >> Sent by: r-help-boun...@r-project.org
> >> 04/01/2014 09:29 AM
> >>
> >> To
> >> Marc Marí Dell'Olmo ,
> >> cc
> >> "r-help@r-project.org" 
> >> Subject
> >> Re: [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.
> >> >
> >> 

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

2014-04-01 Thread Greg Snow
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
 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 <- 100
>> aux <- rnorm(l,0,0.5)
>> s <- sum(aux)/l
>> aux2 <- aux-s
>> sum(aux2)
> [1] -0.0006131392
>>
>> aux[1]<- -sum(aux[2:l])
>> sum(aux)
> [1] -0.03530422
>
>
> 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@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.
538...@gmail.com

__
R-help@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.


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

2014-04-01 Thread Rui Barradas

Hello,

One way is to use ?scale.

set.seed(4867)
l <- 100
aux <- rnorm(l, 0, 0.5)
aux <- scale(aux, scale = FALSE)
sum(aux)

hist(aux, prob = TRUE)
curve(dnorm(x, 0, 0.5), from = -2, to = 2, add = TRUE)

Hope this helps,

Rui Barradas

Em 01-04-2014 16:01, jlu...@ria.buffalo.edu escreveu:

Then what's wrong with centering your initial values around the mean?



Marc Marí Dell'Olmo 
04/01/2014 10:56 AM

To
Boris Steipe ,
cc
jlu...@ria.buffalo.edu, "r-help@r-project.org" 
Subject
Re: [R] A vector of normal distributed values with a sum-to-zero
constraint






Boris is right. I need this vector to include as initial values of a
MCMC process (with openbugs) and If I use this last approach sum(x)
could be a large (or extreme) value and can cause problems.

The other approach x <- c(x, -x) has the problem that only vectors
with even values are obtained.

Thank you!


2014-04-01 16:25 GMT+02:00 Boris Steipe :

But the result is not Normal. Consider:

set.seed(112358)
N <- 100
x <- rnorm(N-1)
sum(x)

[1] 1.759446   !!!

i.e. you have an outlier at 1.7 sigma, and for larger N...

set.seed(112358)
N <- 1
x <- rnorm(N-1)
sum(x)
[1] -91.19731

B.


On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:


The sum-to-zero constraint imposes a loss of one degree of freedom.  Of

  N samples, only (N-1) can be random.   Thus the solution is

N <- 100
x <- rnorm(N-1)
x <- c(x, -sum(x))
sum(x)

[1] -7.199102e-17












Boris Steipe 
Sent by: r-help-boun...@r-project.org
04/01/2014 09:29 AM

To
Marc Marí Dell'Olmo ,
cc
"r-help@r-project.org" 
Subject
Re: [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 <- 100
aux <- rnorm(l,0,0.5)
s <- sum(aux)/l
aux2 <- aux-s
sum(aux2)

[1] -0.0006131392


aux[1]<- -sum(aux[2:l])
sum(aux)

[1] -0.03530422


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


__
R-help@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.



__
R-help@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.



[[alternative HTML version deleted]]



__
R-help@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.



__
R-help@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.


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

2014-04-01 Thread JLucke
Then what's wrong with centering your initial values around the mean?



Marc Marí Dell'Olmo  
04/01/2014 10:56 AM

To
Boris Steipe , 
cc
jlu...@ria.buffalo.edu, "r-help@r-project.org" 
Subject
Re: [R] A vector of normal distributed values with a sum-to-zero 
constraint






Boris is right. I need this vector to include as initial values of a
MCMC process (with openbugs) and If I use this last approach sum(x)
could be a large (or extreme) value and can cause problems.

The other approach x <- c(x, -x) has the problem that only vectors
with even values are obtained.

Thank you!


2014-04-01 16:25 GMT+02:00 Boris Steipe :
> But the result is not Normal. Consider:
>
> set.seed(112358)
> N <- 100
> x <- rnorm(N-1)
> sum(x)
>
> [1] 1.759446   !!!
>
> i.e. you have an outlier at 1.7 sigma, and for larger N...
>
> set.seed(112358)
> N <- 1
> x <- rnorm(N-1)
> sum(x)
> [1] -91.19731
>
> B.
>
>
> On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:
>
>> The sum-to-zero constraint imposes a loss of one degree of freedom.  Of 
 N samples, only (N-1) can be random.   Thus the solution is
>> > N <- 100
>> > x <- rnorm(N-1)
>> > x <- c(x, -sum(x))
>> > sum(x)
>> [1] -7.199102e-17
>>
>> >
>>
>>
>>
>>
>>
>>
>>
>>
>> Boris Steipe 
>> Sent by: r-help-boun...@r-project.org
>> 04/01/2014 09:29 AM
>>
>> To
>> Marc Marí Dell'Olmo ,
>> cc
>> "r-help@r-project.org" 
>> Subject
>> Re: [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 <- 100
>> >> aux <- rnorm(l,0,0.5)
>> >> s <- sum(aux)/l
>> >> aux2 <- aux-s
>> >> sum(aux2)
>> > [1] -0.0006131392
>> >>
>> >> aux[1]<- -sum(aux[2:l])
>> >> sum(aux)
>> > [1] -0.03530422
>> >
>> >
>> > 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@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.
>>
>> __
>> R-help@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.
>>
>
> __
> R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


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

2014-04-01 Thread Keith Jewell

It seems so simple to me, that I must be missing something.

Subject to Jeff Newmiller's reminder of FAQ 7.31; if the sum is zero 
then the mean is zero and vice versa.


The OP's original attempt of:
-
l <- 100
aux <- rnorm(l,0,0.5)
s <- sum(aux)/l
aux2 <- aux-s
sum(aux2)
-
is equivalent to

  aux2 <- rnorm(l,0,0.5)
  aux2 <- aux2-mean(aux2)

If calculations were exact then aux2 would have mean, and thus sum, 
equal to zero - any difference from zero is attributable entirely to 
machine precision.



On 01/04/2014 15:25, Boris Steipe wrote:

But the result is not Normal. Consider:

set.seed(112358)
N <- 100
x <- rnorm(N-1)
sum(x)

[1] 1.759446   !!!

i.e. you have an outlier at 1.7 sigma, and for larger N...

set.seed(112358)
N <- 1
x <- rnorm(N-1)
sum(x)
[1] -91.19731

B.


On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:


The sum-to-zero constraint imposes a loss of one degree of freedom.  Of  N 
samples, only (N-1) can be random.   Thus the solution is

N <- 100
x <- rnorm(N-1)
x <- c(x, -sum(x))
sum(x)

[1] -7.199102e-17












Boris Steipe 
Sent by: r-help-boun...@r-project.org
04/01/2014 09:29 AM

To
Marc Marí Dell'Olmo ,
cc
"r-help@r-project.org" 
Subject
Re: [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 <- 100
aux <- rnorm(l,0,0.5)
s <- sum(aux)/l
aux2 <- aux-s
sum(aux2)

[1] -0.0006131392


aux[1]<- -sum(aux[2:l])
sum(aux)

[1] -0.03530422


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


__
R-help@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.





__
R-help@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.


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

2014-04-01 Thread Marc Marí Dell'Olmo
Boris is right. I need this vector to include as initial values of a
MCMC process (with openbugs) and If I use this last approach sum(x)
could be a large (or extreme) value and can cause problems.

The other approach x <- c(x, -x) has the problem that only vectors
with even values are obtained.

Thank you!


2014-04-01 16:25 GMT+02:00 Boris Steipe :
> But the result is not Normal. Consider:
>
> set.seed(112358)
> N <- 100
> x <- rnorm(N-1)
> sum(x)
>
> [1] 1.759446   !!!
>
> i.e. you have an outlier at 1.7 sigma, and for larger N...
>
> set.seed(112358)
> N <- 1
> x <- rnorm(N-1)
> sum(x)
> [1] -91.19731
>
> B.
>
>
> On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:
>
>> The sum-to-zero constraint imposes a loss of one degree of freedom.  Of  N 
>> samples, only (N-1) can be random.   Thus the solution is
>> > N <- 100
>> > x <- rnorm(N-1)
>> > x <- c(x, -sum(x))
>> > sum(x)
>> [1] -7.199102e-17
>>
>> >
>>
>>
>>
>>
>>
>>
>>
>>
>> Boris Steipe 
>> Sent by: r-help-boun...@r-project.org
>> 04/01/2014 09:29 AM
>>
>> To
>> Marc Marí Dell'Olmo ,
>> cc
>> "r-help@r-project.org" 
>> Subject
>> Re: [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 <- 100
>> >> aux <- rnorm(l,0,0.5)
>> >> s <- sum(aux)/l
>> >> aux2 <- aux-s
>> >> sum(aux2)
>> > [1] -0.0006131392
>> >>
>> >> aux[1]<- -sum(aux[2:l])
>> >> sum(aux)
>> > [1] -0.03530422
>> >
>> >
>> > 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@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.
>>
>> __
>> R-help@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.
>>
>
> __
> R-help@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.

__
R-help@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.


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

2014-04-01 Thread Boris Steipe
But the result is not Normal. Consider:

set.seed(112358)
N <- 100
x <- rnorm(N-1)
sum(x)

[1] 1.759446   !!!

i.e. you have an outlier at 1.7 sigma, and for larger N...

set.seed(112358)
N <- 1
x <- rnorm(N-1)
sum(x)
[1] -91.19731

B.


On 2014-04-01, at 10:14 AM, jlu...@ria.buffalo.edu wrote:

> The sum-to-zero constraint imposes a loss of one degree of freedom.  Of  N 
> samples, only (N-1) can be random.   Thus the solution is 
> > N <- 100
> > x <- rnorm(N-1)
> > x <- c(x, -sum(x))
> > sum(x)
> [1] -7.199102e-17
> 
> >
> 
> 
> 
> 
> 
> 
> 
> 
> Boris Steipe  
> Sent by: r-help-boun...@r-project.org
> 04/01/2014 09:29 AM
> 
> To
> Marc Marí Dell'Olmo ,
> cc
> "r-help@r-project.org" 
> Subject
> Re: [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 <- 100
> >> aux <- rnorm(l,0,0.5)
> >> s <- sum(aux)/l
> >> aux2 <- aux-s
> >> sum(aux2)
> > [1] -0.0006131392
> >> 
> >> aux[1]<- -sum(aux[2:l])
> >> sum(aux)
> > [1] -0.03530422
> > 
> > 
> > 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@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.
> 
> __
> R-help@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.
> 

__
R-help@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.


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

2014-04-01 Thread JLucke
The sum-to-zero constraint imposes a loss of one degree of freedom.  Of  N 
samples, only (N-1) can be random.   Thus the solution is

> N <- 100
> x <- rnorm(N-1)
> x <- c(x, -sum(x))
> sum(x)
[1] -7.199102e-17




> 








Boris Steipe  
Sent by: r-help-boun...@r-project.org
04/01/2014 09:29 AM

To
Marc Marí Dell'Olmo , 
cc
"r-help@r-project.org" 
Subject
Re: [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 <- 100
>> aux <- rnorm(l,0,0.5)
>> s <- sum(aux)/l
>> aux2 <- aux-s
>> sum(aux2)
> [1] -0.0006131392
>> 
>> aux[1]<- -sum(aux[2:l])
>> sum(aux)
> [1] -0.03530422
> 
> 
> 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@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.

__
R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


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

2014-04-01 Thread Boris Steipe
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 <- 100
>> aux <- rnorm(l,0,0.5)
>> s <- sum(aux)/l
>> aux2 <- aux-s
>> sum(aux2)
> [1] -0.0006131392
>> 
>> aux[1]<- -sum(aux[2:l])
>> sum(aux)
> [1] -0.03530422
> 
> 
> 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@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.

__
R-help@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.


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

2014-04-01 Thread Jeff Newmiller
You are on a fool's errand. Read FAQ 7.31.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.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"  
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 <- 100
>> aux <- rnorm(l,0,0.5)
>> s <- sum(aux)/l
>> aux2 <- aux-s
>> sum(aux2)
>[1] -0.0006131392
>>
>> aux[1]<- -sum(aux[2:l])
>> sum(aux)
>[1] -0.03530422
>
>
>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@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.

__
R-help@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.


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

2014-04-01 Thread Marc Marí Dell'Olmo
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 <- 100
> aux <- rnorm(l,0,0.5)
> s <- sum(aux)/l
> aux2 <- aux-s
> sum(aux2)
[1] -0.0006131392
>
> aux[1]<- -sum(aux[2:l])
> sum(aux)
[1] -0.03530422


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