Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
draw normal random numbers with fixed sum

Subject: draw normal random numbers with fixed sum

From: Claudia

Date: 9 Mar, 2011 11:02:09

Message: 1 of 10

Hi all,

I would like to draw a given number k of random numbers from a normal
distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.

How can I do that in Matlab?

Thank you in advance

Subject: draw normal random numbers with fixed sum

From: John D'Errico

Date: 9 Mar, 2011 11:17:22

Message: 2 of 10

"Claudia" wrote in message <il7mnh$j2s$1@fred.mathworks.com>...
> Hi all,
>
> I would like to draw a given number k of random numbers from a normal
> distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.
>
> How can I do that in Matlab?
>
> Thank you in advance

If the sum is fixed, then they are not normally distributed,
unless the desired value of s is EXACTLY k*mu.

And even then, the numbers will not be normally distributed,
since you can only specify the mean of the POPULATION, not
of any sample from that distribution.

John

Subject: draw normal random numbers with fixed sum

From: Claudia

Date: 9 Mar, 2011 11:46:22

Message: 3 of 10

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <il7nk2$g64$1@fred.mathworks.com>...
> "Claudia" wrote in message <il7mnh$j2s$1@fred.mathworks.com>...
> > Hi all,
> >
> > I would like to draw a given number k of random numbers from a normal
> > distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.
> >
> > How can I do that in Matlab?
> >
> > Thank you in advance
>
> If the sum is fixed, then they are not normally distributed,
> unless the desired value of s is EXACTLY k*mu.
>
> And even then, the numbers will not be normally distributed,
> since you can only specify the mean of the POPULATION, not
> of any sample from that distribution.
>
> John

Hi John,

thanks for your answer!

the value s is k*mu. okay, I explain my whole task:

I have a normal distribution of produced Chips, from which I know the parameters mu and sigma.
This distribution is the average of k independent normally distributed random variables with different parameters mu and the same parameter sigma.
So, I think the mu's are also normally distributed with the given parameters of the Chip-distribution (mu and sigma).
What I need are the mu's to calculate other things...
But how can I generate them???

Subject: draw normal random numbers with fixed sum

From: Torsten

Date: 9 Mar, 2011 12:19:26

Message: 4 of 10

On 9 Mrz., 12:46, "Claudia " <claudiahasens...@web.de> wrote:
> "John D'Errico" <woodch...@rochester.rr.com> wrote in message <il7nk2$g6...@fred.mathworks.com>...
> > "Claudia" wrote in message <il7mnh$j2...@fred.mathworks.com>...
> > > Hi all,
>
> > > I would like to draw a given number k of random numbers from a normal
> > > distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.
>
> > > How can I do that in Matlab?
>
> > > Thank you in advance
>
> > If the sum is fixed, then they are not normally distributed,
> > unless the desired value of s is EXACTLY k*mu.
>
> > And even then, the numbers will not be normally distributed,
> > since you can only specify the mean of the POPULATION, not
> > of any sample from that distribution.
>
> > John
>
> Hi John,
>
> thanks for your answer!
>
> the value s is k*mu. okay, I explain my whole task:
>
> I have a normal distribution of produced Chips, from which I know the parameters mu and sigma.
> This distribution is the average of k independent normally distributed random variables with different parameters mu and the same parameter sigma.
> So, I think the mu's are also normally distributed with the given parameters of the Chip-distribution (mu and sigma).
> What I need are the mu's to calculate other things...
> But how can I generate them???- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

The mu_i 's are given and don't follow a distribution.
I don't know if it helps, but if
X = 1/k * sum_{i=1}^{k} X_i
and the X_i are normal random variables with expectation mu_i and
variance sigma^2,
then X also follows a normal distribution with mean mu = 1/k*sum_{i=1}
^{k} mu_i and
variance 1/k * sigma^2.

Best wishes
Torsten.

Subject: draw normal random numbers with fixed sum

From: Bruno Luong

Date: 9 Mar, 2011 12:48:04

Message: 5 of 10

"Claudia" wrote in message <il7pae$4pe$1@fred.mathworks.com>...
>
> the value s is k*mu. okay, I explain my whole task:
>
> I have a normal distribution of produced Chips, from which I know the parameters mu and sigma.
> This distribution is the average of k independent normally distributed random variables with different parameters mu and the same parameter sigma.
> So, I think the mu's are also normally distributed with the given parameters of the Chip-distribution (mu and sigma).

The "explanation" does not any sense as it stands. The mu are the mean - the parameters of the random distribution, they must be known, so how can we understand the sentense: "mu's are also normally distributed"?

Bruno

Subject: draw normal random numbers with fixed sum

From: Roger Stafford

Date: 9 Mar, 2011 17:49:04

Message: 6 of 10

"Claudia" wrote in message <il7mnh$j2s$1@fred.mathworks.com>...
> Hi all,
>
> I would like to draw a given number k of random numbers from a normal
> distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.
>
> How can I do that in Matlab?
>
> Thank you in advance
- - - - - - - - -
  Claudia, in my opinion the problem as you originally posed it does make sense if you interpret it as a conditional probability statement. With k mutually independent normal random variables, x1, x2, ..., xk, you want to generate samples of them given that their sum is some specified amount. That is a perfectly well-defined statement in conditional probability theory. Of course the resulting samples will no longer be mutually independent, but in fact each one will still be normally distributed, though with an altered variance.

  If we subtract the mean values from each variable we then have independent normal variables with mean zero and constant variance. It is known that if we apply any k by k unitary transformation to these we will still have independent normal variables with mean zero and the same variance.

  Suppose we select a unitary matrix in which the top row has all values equal to 1/sqrt(k) as it is to be applied to a column of the k x's: y = U*x. This means that y1 will be equal to the sum of the x's divided by sqrt(k). Now since the resulting y's are also mutually independent and have the same normal distribution, if we place the condition that y1 be equal to the specified constant sum of the x's divided by sqrt(k), the original independence of the y's implies that the distribution of the remaining y's will be unaffected - placing a condition on one variable does not affect the distribution of variables that are independent of it. Then if we apply the inverse transformation on the y's with y1 specified in this way and the remaining y's still independent and normal, the x's we obtain will have the necessary conditional probability distribution given that their sum is specified.

  This tells us how to generate the desired x's. Set y1 equal to the above value and generate k-1 additional y's using 'randn' appropriately. An appropriate U can easily be created using matlab's 'null' function. Then apply the inverse of such a U to these y's and obtain the required x's. The x's will then possess the desired conditional probability distribution with fixed sum. At this point the required means can then be added back again.

  I haven't spelled this out in detail because I am not sure that this is what you really want. If it is, one of us can no doubt lay out some specific matlab code that would do the job.

Roger Stafford

Subject: draw normal random numbers with fixed sum

From: Claudia

Date: 10 Mar, 2011 14:21:04

Message: 7 of 10

"Roger Stafford" wrote in message <il8eig$jht$1@fred.mathworks.com>...
> "Claudia" wrote in message <il7mnh$j2s$1@fred.mathworks.com>...
> > Hi all,
> >
> > I would like to draw a given number k of random numbers from a normal
> > distribution with given parameters mu and sigma, and I would like the sum of this random numbers to be like another given parameter call it s.
> >
> > How can I do that in Matlab?
> >
> > Thank you in advance
> - - - - - - - - -
> Claudia, in my opinion the problem as you originally posed it does make sense if you interpret it as a conditional probability statement. With k mutually independent normal random variables, x1, x2, ..., xk, you want to generate samples of them given that their sum is some specified amount. That is a perfectly well-defined statement in conditional probability theory. Of course the resulting samples will no longer be mutually independent, but in fact each one will still be normally distributed, though with an altered variance.
>
> If we subtract the mean values from each variable we then have independent normal variables with mean zero and constant variance. It is known that if we apply any k by k unitary transformation to these we will still have independent normal variables with mean zero and the same variance.
>
> Suppose we select a unitary matrix in which the top row has all values equal to 1/sqrt(k) as it is to be applied to a column of the k x's: y = U*x. This means that y1 will be equal to the sum of the x's divided by sqrt(k). Now since the resulting y's are also mutually independent and have the same normal distribution, if we place the condition that y1 be equal to the specified constant sum of the x's divided by sqrt(k), the original independence of the y's implies that the distribution of the remaining y's will be unaffected - placing a condition on one variable does not affect the distribution of variables that are independent of it. Then if we apply the inverse transformation on the y's with y1 specified in this way and the remaining y's still independent and normal, the x's we obtain will have the necessary conditional probability distribution given that their sum is specified.
>
> This tells us how to generate the desired x's. Set y1 equal to the above value and generate k-1 additional y's using 'randn' appropriately. An appropriate U can easily be created using matlab's 'null' function. Then apply the inverse of such a U to these y's and obtain the required x's. The x's will then possess the desired conditional probability distribution with fixed sum. At this point the required means can then be added back again.
>
> I haven't spelled this out in detail because I am not sure that this is what you really want. If it is, one of us can no doubt lay out some specific matlab code that would do the job.
>
> Roger Stafford

Thank's a lot. That's exactly what I was looking for! I tried it by myself to write the code. What I don't understand is how to generate the matrix U...

Here my code:

% given parameters
mu =
sigma =
k = % count of variables
% in my case s = mu * k
s = % sum of variables

% subtract the mean -> mean value 0
% x = x_norm - mu;

% generate y; (Transformation y = sum(x)/sqrt(k))
y = s / sqrt(k); %first value
% generate k-1 normally distributed random numbers
y(2:k) = randn(k-1, 1);

% How should I generate the matrix U??? U = null(?)
U = 1/sqrt(k) * ones(1, k); %first row
U(2:k, :) = null(y); %???

% calculate the x's
x = inv(U)*y;

% add the mean to get the normally distributed values with mean mu
m = x + mu;

Subject: draw normal random numbers with fixed sum

From: Bruno Luong

Date: 10 Mar, 2011 17:22:05

Message: 8 of 10

s = 10; % target sum
N=3; % number of random variables
sigma = 2; % standard deviation
ndata = 1e6; % number of data

Q = null(ones(1,N));
X = (sigma*sqrt(N/(N-1)))*Q*randn(N-1,ndata);
mu = s/N;
X = mu + X;

% Test
sum(X(:,1:10),1)
std(X')

% Bruno

As Roger wrote earlier, by imposing the sum, the three variables will not be independent.

Subject: draw normal random numbers with fixed sum

From: Claudia

Date: 11 Mar, 2011 13:19:04

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ilb1bt$gnb$1@fred.mathworks.com>...
> s = 10; % target sum
> N=3; % number of random variables
> sigma = 2; % standard deviation
> ndata = 1e6; % number of data
>
> Q = null(ones(1,N));
> X = (sigma*sqrt(N/(N-1)))*Q*randn(N-1,ndata);
> mu = s/N;
> X = mu + X;
>
> % Test
> sum(X(:,1:10),1)
> std(X')
>
> % Bruno
>
> As Roger wrote earlier, by imposing the sum, the three variables will not be independent.

Okay, I now understand it, but why the factor "sigma*sqrt(N/(N-1))"?

Subject: draw normal random numbers with fixed sum

From: Bruno Luong

Date: 11 Mar, 2011 14:19:04

Message: 10 of 10

"Claudia" wrote in message <ild7g8$kgd$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ilb1bt$gnb$1@fred.mathworks.com>...
> > s = 10; % target sum
> > N=3; % number of random variables
> > sigma = 2; % standard deviation
> > ndata = 1e6; % number of data
> >
> > Q = null(ones(1,N));
> > X = (sigma*sqrt(N/(N-1)))*Q*randn(N-1,ndata);
> > mu = s/N;
> > X = mu + X;
> >
> > % Test
> > sum(X(:,1:10),1)
> > std(X')
> >
> > % Bruno
> >
> > As Roger wrote earlier, by imposing the sum, the three variables will not be independent.
>
> Okay, I now understand it, but why the factor "sigma*sqrt(N/(N-1))"?

If Y = Q*randn; then Y has covariance matrix H = Q*Q'. The standard deviation of each component of Y is:

sqrt(H(i,i)).

It can be showed that the diagonal of H: H(i,i) is (N-1)/N for Q = null(ones(1,N)).

Thus in order to normalize X from Y such that the standard deviation is sigma; we need to multiply Y by (sigma*sqrt(N/(N-1))).

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us