# Generating random matrices with specific equality constraints

Suppose I want to generate a nonnegative $n \times n$ matrix $\mathbf A$ for an odd $n$ (say, $n=5$ for a good enough example), such that

• the individual elements are drawn from a uniform distribution

but with the equality constraints

• the sum of all the elements in the rows above the mid row = the sum of all the elements in the rows below the mid row

• the sum of all the elements in the columns left of the middle column = the sum of all the elements in the columns right of the middle column

• the sum of all elements = 1.0

That is, in effect I want to generate matrices "balanced" in a specific way (I intend to use them as convolution matrices and I don't care about translation).

For my current purposes I think I have a good enough ad hoc (not entirely correctly distributed) solution that generates a uniform matrix and then enforces the equality constraints by correcting the individual elements using a least squares approach.

However I'm curious, mainly for the learning experience, if there would be some easy-ish way to sample the resulting conditional distribution. So far I've had a quick peek on Wikipedia into the Bates distribution (mean of uniform random variables) and into Monte Carlo Markov Chain models, but I'm not sure if they help here or if there would be an easier solution.

I'm not necessarily looking for a complete solution; pointers to insightful or helpful material are also appreciated.

Edit: I'm looking for the conditional probability distribution, i.e. the elements do not need to be uniformly distributed; rather, I'm looking for a distribution where each sample that satisfies the equality constraints has the same probability (hence conditional uniform distribution given the equalities).

-
Your two conditions (1) individual elements uniformly distributed and (2) your equality constraints are mutually incompatible. So, it's not entirely clear what you're aiming for. – cardinal Nov 30 '11 at 17:42
The Sinkhorn-Knopp algorithm is somewhat similar in that the goal there is to generate a matrix of nonnegative elements that satisfy individual row and column sum constraints. Quite a lot is known about its convergence properties. – cardinal Nov 30 '11 at 17:43

1
– cardinal Nov 30 '11 at 17:51

1
What I'm thinking about is, if I'm not mistaken about the terminology, sampling the conditional probability distribution of uniformly distributed values conditioned with the given equations hold. Sure, the conditioned distribution is not necessarily uniform, but that's not a problem. Or in even other words, is there an efficient algorithm to replace the following: 1. Generate such a uniformly random matrix 2. If the equations do hold to within a difference of $\epsilon$, return the matrix; else goto 1 What I'm looking for is essentially this with $\lim \epsilon$ -> 0. – SLi Nov 30 '11 at 20:20

2
Ah, then @Cardinal is (as usual) correct: the marginals will no longer be uniform and you're not requiring that they be so. In fact, they can be far from uniform. (But for medium to large $n$ they ought to be pretty close, because the constraints are so mild.) What you're asking is this: the constraints along with the bounds of the uniform distributions describe a polytope of $n^2-3$ dimensions; you would like to sample this polytope uniformly. – whuber♦ Nov 30 '11 at 23:21
show 1 more comment
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 8, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9175267219543457, "perplexity_flag": "head"}