TITLE: Finding the vertices of a convex set of matrices
QUESTION [8 upvotes]: I'm a little new here so wasn't sure if this was the right area.
I've been trying to figure out how to generate a set of random $K \times N$ (for $K<N$) matrices that are subject to a several constraints: all columns sum to 1, all rows sum to $N/K$, and all elements of the matrix are non-negative.
I've managed to reduce a problem a bit; because the set of matrices bound by these properties is convex, one could take the vertices of this set and take random weighted averages of the vertices (and use these averages for the pool, and so on); you should end up populating the space.
What I'm trying to figure out is how to find the vertices of this set in the first place. So for example for the set of $2 \times 3$ matrices the vertices are the following set of pairs on the 2-D simplex (I think):

(if you're reducing the 6-D space to ordered pairs in 3-D space then technically it also includes the re-ordered pairs, but this was just for illustration).
Many thanks in advance.

REPLY [4 votes]: If $N = K$, all rows sum to one, and all columns sum to one, so the matrices are the doubly stochastic matrices, which forms a convex set known as the Birkhoff polytope. The Birkhoff-von Neumann theorem states that the vertices of the Birkhoff polytope are the permutation matrices.
A wider generalization of the Birkhoff polytope is the transportation polytope, in which columns and rows sum to arbitrary values, and two different rows (or columns) can sum to different values.
Answer to the the new question
Warmup: A special case
You can express your requirements as a matrix equation, e.g. for the $2 \times 3$ case
$$A =
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23}
\end{pmatrix}$$
you can see that every matrix $A$ satisfying your conditions must satisfy
$$\underbrace{
\begin{pmatrix}
1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 1 & 1 \\
1 & 0 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 & 1 & 0
\end{pmatrix}}_{C}
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{13} \\ a_{21} \\ a_{22} \\ a_{23}
\end{pmatrix}
=
\underbrace{
\begin{pmatrix}
\frac{3}{2} \\ \frac{3}{2} \\ 1 \\ 1
\end{pmatrix}
}_{b}
$$
I have here removed the last row which would correspond to the last column constraint, the reason being that it is linearly dependent on the other rows, and therefore will be satisfied if automatically if the other constraints are satisfied. For the rest of this to work, it is important that the rows of $C$ are linearly independent.
You want to find the vertices of the simplex defined by
$$\begin{align}
Ca &= b \\
a &\geq 0
\end{align}$$
An algorithm that uses the vertices of a simplex as defined above is the simplex algorithm for linear programming, but in our case we do not have a goal function to optimize; we only want the vertices, but it might be a good source to draw inspiration from when solving this problem. I quite like Robert Fourers notes on the simplex algorithm.
In context of the simplex algorithm, a vector $a$ that is a vertex of the simplex is called a basic feasible solution. The algebraic characterization of a basic feasible solution is that $a \geq 0$ (this is the feasible part) and if $i_1, \dots, i_k$ are all the positions of $a$ where $a$ is non-zero, then the columns $C_{i_1}, \dots C_{i_k}$ are linearly independent (this is the basic part).
An example. From the $C$ above, we can pick columns 1, 2, 4, and 6, to form $\tilde C$. For this to form a basic solution, we must have $a_3 = a_5 = 0$, so we can remove them from our system and form $\tilde a$. We can then solve the system $\tilde C \tilde a = b$:
$$
\begin{pmatrix}
1 & 1 & 0 & 0 \\
0 & 0 & 1 & 1 \\
1 & 0 & 1 & 0 \\
0 & 1 & 0 & 0
\end{pmatrix}
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{21} \\ a_{23}
\end{pmatrix}
=
\begin{pmatrix}
\frac{3}{2} \\ \frac{3}{2} \\ 1 \\ 1
\end{pmatrix}
$$
popping this into a solver for linear systems gives:
$$
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{21} \\ a_{23}
\end{pmatrix}
=
\begin{pmatrix}
\frac{1}{2} \\ 1 \\ \frac{1}{2} \\ 1
\end{pmatrix}
\Rightarrow
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{13} \\ a_{21} \\ a_{22} \\ a_{23}
\end{pmatrix}
=
\begin{pmatrix}
\frac{1}{2} \\ 1 \\ 0 \\ \frac{1}{2} \\ 0 \\ 1 
\end{pmatrix}
$$
and this solution is basic and feasible, so this is one of the vertices.
So, now we have found one vertex of the simplex. How do we find the others? We can do it by finding all sets of linearly independent columns in $C$ and making sure that the solutions are feasible. We could instead, once we have found one vertex, do what the simplex method do; move to another vertex directly from another vertex.
In the simplex algorithm, one chooses an edge to move along which increases (or decreases) the objective value. Since this is not relevant for us, we will have to do some kind of search to find the vertices. How one moves along an edge is maybe a bit too much to go into detail here, check the link for notes above.
I went through the linearly independent sets of columns that forms a $4 \times 4$ matrix $\tilde C$ and got the following vertices:
$$
\begin{pmatrix}
 \frac{1}{2} \\
 1 \\
 0 \\
 \frac{1}{2} \\
 0 \\
 1
\end{pmatrix}
,
\begin{pmatrix}
 1 \\
 \frac{1}{2} \\
 0 \\
 0 \\
 \frac{1}{2} \\
 1
\end{pmatrix}
,
\begin{pmatrix}
 \frac{1}{2} \\
 0 \\
 1 \\
 \frac{1}{2} \\
 1 \\
 0
\end{pmatrix}
,
\begin{pmatrix}
 1 \\
 0 \\
 \frac{1}{2} \\
 0 \\
 1 \\
 \frac{1}{2}
\end{pmatrix}
,
\begin{pmatrix}
 0 \\
 \frac{1}{2} \\
 1 \\
 1 \\
 \frac{1}{2} \\
 0
\end{pmatrix}
,
\begin{pmatrix}
 0 \\
 1 \\
 \frac{1}{2} \\
 1 \\
 0 \\
 \frac{1}{2}
\end{pmatrix}
$$
In general
For general $N \times K$ matrices, the matrix $C$ is quite easy to construct: The first $K$ rows will be correspond to the row conditions and will have a sequence of $N$ ones starting at position $K(i-1) + 1$, where $i$ is the row number. The last $N$ rows will be the column conditions, it is probably easiest to think of these as a sequence of identity matrices of size $N \times N$, i.e.:
$$\begin{pmatrix} I_N & I_N & \cdots & I_N \end{pmatrix}$$
the rows will always be linearly dependent, I think it is enough to remove the last row to make the rows be linearly independent.
I made a program to solve this problem in general; you quickly get a lot of vertices. For the $3 \times 5$ case you get 360 vertices and for the $4 \times 5$ case you get 3000 vertices, so it might not be a very fast way to generate random matrices.
Answer to the original question
You can express your requirements as a matrix equation, e.g. for the $2 \times 3$ case
$$A =
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23}
\end{pmatrix}$$
you can see that every matrix $A$ satisfying your conditions must satisfy
$$
\begin{pmatrix}
1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 1 & 1 \\
1 & 0 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{13} \\ a_{21} \\ a_{22} \\ a_{23}
\end{pmatrix}
=
\begin{pmatrix}
\frac{2}{3} \\ \frac{2}{3} \\ 1 \\ 1 \\ 1
\end{pmatrix}
$$
and this system actually has no solution. We can see this by doing some row operations on the matrix. Subtract the first and second row from the last, then add the third and fourth row. You get:
$$
\begin{pmatrix}
1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 1 & 1 \\
1 & 0 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0
\end{pmatrix}
\begin{pmatrix}
a_{11} \\ a_{12} \\ a_{13} \\ a_{21} \\ a_{22} \\ a_{23}
\end{pmatrix}
=
\begin{pmatrix}
\frac{2}{3} \\ \frac{2}{3} \\ 1 \\ 1 \\ \frac{5}{3}
\end{pmatrix}
$$
so the last row exhibits the unsatisfiable condition $0 = \frac{5}{3}$.
And it seems like you would get something like this in general. If you sum your whole matrix in two ways, the results should be the same. Let $A$ be an $N \times K$ matrix as you have described. If you add up all the elements you should get the columns sum (i.e. 1) times the number of columns ($K$):
$$\sum a_{ij} = K$$
and you should get the row sum (i.e. $N/K$) times the number of rows ($N$):
$$\sum a_{ij} = N^2/K$$
which leads to the necessary condition
$$N^2/K = K \Leftrightarrow N^2 = K^2 \Leftrightarrow N = K$$
since $N, K > 0$. Thus your matrices have to be square, and my first paragraph is the answer.