New Reply

Generate a uniform random vector

 
Share Thread Thread Tools
May17-12, 03:49 AM   #1
 

Generate a uniform random vector


Hi all, I'm trying to generate uniform random vectors with n dimensions.
To be more precise, each of the elements of the vector must be a uniform distributed variable in [0,1]
The constraint is that the sum of the elements of the vector must be 1.
I tried different solutions for over a week but i can't get it working.
Can you help me, please?
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
>> Google eyes emerging markets networks
May17-12, 04:16 AM   #2
 
Quote by 3HeadedMonkey View Post
Hi all, I'm trying to generate uniform random vectors with n dimensions.
To be more precise, each of the elements of the vector must be a uniform distributed variable.
The constraint is that the sum of the elements of the vector must be 1.
I tried different solutions for over a week but i can't get it working.
Can you help me, please?
Hey 3HeadedMonkey and welcome to the forums.

There are many ways to do this, but one that I think is simple an intuitive is to generate N independent U(0,1) realizations and then normalize all elements by dividing by the total sum of all elements.

In terms of generation, the actual mechanisms can be complicated but you can use anything from R to QBASIC to generate a random number from U(0,1). You can also use a random number table which is just a printed table of random numbers.

Also another good source of random numbers is http://www.random.org

In R (which you can download for free) the command for generating a realization from a uniform distribution is runif() and you can type help(runif) in R to give the command line arguments.
May17-12, 04:42 AM   #3
 
Quote by chiro View Post
Hey 3HeadedMonkey and welcome to the forums.

There are many ways to do this, but one that I think is simple an intuitive is to generate N independent U(0,1) realizations and then normalize all elements by dividing by the total sum of all elements.

In terms of generation, the actual mechanisms can be complicated but you can use anything from R to QBASIC to generate a random number from U(0,1). You can also use a random number table which is just a printed table of random numbers.

Also another good source of random numbers is
In R (which you can download for free) the command for generating a realization from a uniform distribution is runif() and you can type help(runif) in R to give the command line arguments.
Hi chiro, thanks for the reply. If you generate N variables and then you divide by the total sum you no longer have N uniform random variables. If you try, you can see that the plot of the frequencies of a single variable is no longer "flat". I could try to use R, but only a question: can I easily use it by command line? I need this vector to be generated automatically many times by a C++ program
May17-12, 04:46 AM   #4
 

Generate a uniform random vector


Quote by 3HeadedMonkey View Post
Hi chiro, thanks for the reply. If you generate N variables and then you divide by the total sum you no longer have N uniform random variables. If you try, you can see that the plot of the frequencies of a single variable is no longer "flat". I could try to use R, but only a question: can I easily use it by command line? I need this vector to be generated automatically many times by a C++ program
What you are saying is true, but the thing is you will never truly have N independent U(0,1) variables with your constraint. Because you have introduce a dependency between observations, there will always be a dependency not only between the variables, but also between the definitions of the variables as well.

You can use R like a console: you type in one command and it will spit out the output. You type one command at a time and you can store results in variables. You don't need to declare variables like you do in C or C++ and it's a lot easier than using C or C++.
May17-12, 07:20 AM   #5

Math 2012
 
Recognitions:
Science Advisor Science Advisor
We can't guess what you really want to do here, but think about the case where N = 1 and it should be obvious that what you desribed in your OP is impossible.

If there is a constraint involving N variables, you can only make N-1 indepedent choices. You probably need to think what that means in terms of the problem you want to solve, before you get into the details of writing code.
May17-12, 07:37 AM   #6
 
Quote by AlephZero View Post
We can't guess what you really want to do here, but think about the case where N = 1 and it should be obvious that what you desribed in your OP is impossible.

If there is a constraint involving N variables, you can only make N-1 indepedent choices. You probably need to think what that means in terms of the problem you want to solve, before you get into the details of writing code.
Take the case with N=2
Generate v[0] as uniform[0,1]
Calculate v[1] as 1-v[0]
Result: 2 uniform variables (not independent but uniform)
If you plot the frequencies of the two variables, the plots are "flat". I mean each of the two variables is uniformly distributed. Obviously v[1] is uniform by definition. Then v[1] is uniform too but "mirrored" with respect to 0.5.
I think that this could be valid with more than 2 variables.

I thought that someone could know some well known mathemagical formula to generate this kind of vectors ;-)
May17-12, 11:45 AM   #7

Math 2012
 
Recognitions:
Science Advisor Science Advisor
Quote by 3HeadedMonkey View Post
I think that this could be valid with more than 2 variables.
It doesn't work for N > 2, because the sum of N-1 uniformly distributed variables is not uniformly distributed. In fact, using the central limit theorem, the sum will approximate to a normal distribution for large N.
May17-12, 12:02 PM   #8
 
It doesn't work for N > 2, because the sum of N-1 uniformly distributed variables is not uniformly distributed. In fact, using the central limit theorem, the sum will approximate to a normal distribution for large N.
ok this is what i feared. Thank you anyway
May17-12, 03:24 PM   #9
 
Recognitions:
Science Advisor Science Advisor
In order to get a unit N dimensional unit vector, uniform on the surface of an N sphere, you need to be careful. The suggestion that you take N independent uniformly distributed between -1 and 1 is incorrect since the distribution will not be the same as uniform on the N sphere. To illustrate let N = 2, then you would be picking points uniform on a square so that directions along the diagonals would be preferred.

To avoid this use a rejection technique, and keep vectors only if the original length is < 1. The main problem with this approach is that it is inefficient for higher dimensions. Alternatively one could convert to the N dimensional analog of spherical coordinates and choose angles uniform in the appropriate domain.
May18-12, 08:24 AM   #10
 
Quote by mathman View Post
Alternatively one could convert to the N dimensional analog of spherical coordinates and choose angles uniform in the appropriate domain.
That seems like the easy way to do it.
May18-12, 09:15 AM   #11
 
Recognitions:
Science Advisor Science Advisor
I don't know if this is relevant to the goal of the original post, but in post #4 of the thread, http://physicsforums.com/showthread.php?t=587264, the member Petawa gives a link to a paper that deals with selecting a random subspace in N dimensions. "Some Remarks On A Lemma Of Ran Raz" by Milman and Wagner.
May18-12, 11:00 AM   #12
 
That seems like the easy way to do it.
Yes, easy but wrong. In fact if you look at the plot of sin and cos you see that there are more points in the codomain when sin and cos come near to 1.
My original idea was to find a distribution (not uniform) to generate these angles such that the result of sin and cos would be uniform. I don't know if it is possible or not
May18-12, 11:08 AM   #13
 
Quote by Stephen Tashi View Post
I don't know if this is relevant to the goal of the original post, but in post #4 of the thread, [], the member Petawa gives a link to a paper that deals with selecting a random subspace in N dimensions. "Some Remarks On A Lemma Of Ran Raz" by Milman and Wagner.
Thank you, i could try to look at this later, now i returned to use the normalization (much easier than the angles generation and equally bad )
May18-12, 02:41 PM   #14

Math 2012
 
Recognitions:
Science Advisor Science Advisor
If the sum of the elements must be 1, the subspace isn't a sphere.

If N = 2 it is a straight line joining (1,0) and (0,1), which gives a simple interpretation of why post #6 "works" for that special case.
If N = 3 it is the surface of an equilateral triangle with vertices (1,0,0), (0,1,0) and (0,0,1). Maybe you really want a uniform distribution of points on the area of the triangle? If so, it's fairly clear that the x y and z coordinate are not an easy way to measure that. It would probably be better to choose the random variables using two parameters that describe the surface, then transform the point back to (x,y,z).

For N = 4 the points fill the volume of an "equilateral" tetrahedron embedded in 4-D space, and similarly for higher values of N.
May18-12, 03:04 PM   #15
 
Quote by AlephZero View Post
Maybe you really want a uniform distribution of points on the area of the triangle?
I have to think about this. If I want this, I can do it by repeatedly folding the hyperspace. I already coded it and it works. In this case the single elements are not uniform. But maybe the right thing to do.
I explain better the problem:
This vector is the input of an heuristic optimization algorithm, the random restarts are useful to find the global maximum. I want to restart in very random points (uniformly). So, I think that sampling the hyper-tetrahedron uniformly could be the best solution. Do you agree?
May18-12, 04:21 PM   #16
 
Recognitions:
Science Advisor Science Advisor
Quote by 3HeadedMonkey View Post
Yes, easy but wrong. In fact if you look at the plot of sin and cos you see that there are more points in the codomain when sin and cos come near to 1.
My original idea was to find a distribution (not uniform) to generate these angles such that the result of sin and cos would be uniform. I don't know if it is possible or not
You are right. I didn't make myself clear. For some angles it will be uniform in angle. For others it will be uniform in some trig. function. Essentially one needs to know how dxdydz... converts to generalized spherical coordinates.
May18-12, 04:34 PM   #17
 
Quote by mathman View Post
You are right. I didn't make myself clear. For some angles it will be uniform in angle. For others it will be uniform in some trig. function. Essentially one needs to know how dxdydz... converts to generalized spherical coordinates.
I tried to "uniformize" the resulting variables by generating angles with a distribution such that there were more angles near π/4, but it didn't work properly. I think it was because each angle uses a certain combination of cos and sin in the conversion to cartesian coordinates. Maybe I should generate each single angle with a different distribution depending on the sin and cos in the hyperspherical coordinates formula of that angle? This would be a suicide
New Reply
Thread Tools


Similar Threads for: Generate a uniform random vector
Thread Forum Replies
Can the mind generate random numbers? General Discussion 61
Generate random numbers by hand? General Math 5
How to get Excel to generate any random number besides x? Math & Science Software 6
MATLAB code to Generate Uniform Random Variable Engineering, Comp Sci, & Technology Homework 0
Random Unit Vector From a uniform Distribution Set Theory, Logic, Probability, Statistics 2