For sure combinatorial functions can be written quite efficiently in
Mata. I'll try a few different things and pick the best performance.
Perhaps then other combinatorial-type Stata programs can be re-written
to use the more efficient code that does nothing but provide the sets of
combinations and permutations.
Nick Cox wrote:
In addition to other replies, there is a -selectvars-
package on SSC:
----------------------- extract from help
-selectvars- produces a list of all possible n-tuples from a list of
variable names. That is, given a list of k variable names, it produces the
empty list; all possible distinct singletons (each individual variable
name); all possible distinct pairs; and so forth. Tuples are bound in " ".
As -selectvars- is intended for programming, the list is not displayed, but
left behind as a saved result.
Note that there is no checking of a possible error with even modest k:
that a full list of (at most 2^k) tuples can not be accommodated in a
Note that -selectvars- long predates Mata. I would be interested to know
if it could be speeded up. Mata or not, I have always been queasy
about programs like this because I feared that someone would
engender a combinatorial explosion (a genuine term in the literature).