I have a number generator that can generate numbers between 0 and 1. I want to somehow use this generate a set of $N$ numbers that add up to $1$, and include negative numbers. These numbers must be uniformly distributed.

For example, if $N = 3$, one possible result might be $(0.4, -0.5, 1.1)$. Another might be $(0.2, 0.4, 0.4)$.

Is this possible? And if so, how would I go about it?

EDIT: I would rather the numbers are limited to be between $-1$ and $1$. So $(0.4, -0.5, 1.1)$ is no longer a possible combination.

  • 1
    $\begingroup$ How can the numbers be uniformly ditributed? After the first one is picked, then you have restrictions on the second one based on the value of the first one. They are not independent of each other. Also if $x \in [0,1]$ then $2x-1 \in [-1,1]$. $\endgroup$ – Fixed Point Nov 9 '13 at 23:43

An $n$-tuple $(x_{1}, \cdots, x_{n})$ of independently, uniformly distributed numbers can be thought as a uniformly chosen point in an $n$-dimensional box (with probability measure chosen as the normalized Lebesgue measure on this box).

In this case, note that the region

$$D = \{ (x_{1}, \cdots, x_{n} ) \in \Bbb{R}^{n} : -1 \leq x_{j} \leq 1 \text{ and } x_{1} + \cdots + x_{n} = 1 \}$$

defines an $(n-1)$-dimensional flat surface. So it makes sense to choose the uniform distribution for this problem as the normalized surface measure on $D$.

Though we may devise a clever formula for generating such uniform samples on $D$, I will adopt a crude method.

Let $\Bbb{A}$ be the hyperplane containing $D$ and let $\pi : \Bbb{R}^{n} \to \Bbb{A}$ be the orthogonal projection onto $\Bbb{A}$. Choose a box $B$ in $\Bbb{R}^{n-1} \simeq \Bbb{R}^{n-1}\times\{0\}$ so that $D \subset \pi(B)$. If $U$ denotes the uniform distribution on $B$, then our desired uniform distribution for $D$ is given by

$$ \Bbb{E}[\pi U \vert \pi^{-1}D]. $$

Implementing this random variable is very simple:

  1. Generate an instance of random $n$-tuple $U$ on $B$.
  2. Calculate $\pi U$.
  3. Accept of discard this instance according to whether $\pi U$ lies inside $D$ or not.

It is easy to check that

$$ \pi(x_{1}, \cdots, x_{n}) = \left(x_{1} - \bar{x}+\frac{1}{n}, \cdots, x_{n} - \bar{x}+\frac{1}{n} \right), \qquad \bar{x} = \frac{x_{1} + \cdots + x_{n}}{n}. $$

Also it turns out that $B$ can be taken as $[-1-\sqrt{n}, 1+\sqrt{n}]^{n-1}$. The following is a simulation for $n = 3$ with $10000$ samples.

Proj[l_] := l - Mean[l] + 1/Length[l];
IsInD[l_] := And @@ Table[-1 <= l[[j]] <= 1, {j, 1, Length[l]}];
Rp[n_] := Module[{l},
    l = RandomVariate[UniformDistribution[{-1 - Sqrt[n], 1 + Sqrt[n]}], n - 1];
    AppendTo[l, 0];
    l = Proj[l];
    If[IsInD[l], Break[];];

enter image description here

  • $\begingroup$ The questioner seems to be asking that the marginal distributions for each component be uniform. It looks to me like your solution is a Dirichlet distribution, which won't satisfy that constraint. $\endgroup$ – soakley Feb 7 '14 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.