Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: draw normal random numbers with fixed sum Date: Thu, 10 Mar 2011 14:21:04 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 51 Message-ID: <ilamog$3o0$1@fred.mathworks.com> References: <il7mnh$j2s$1@fred.mathworks.com> <il8eig$jht$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-03-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1299766864 3840 172.30.248.38 (10 Mar 2011 14:21:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 10 Mar 2011 14:21:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2773899 Xref: news.mathworks.com comp.soft-sys.matlab:715027 "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;