Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Sampling with replacement or bootstrapping in Mata

From (Vince Wiggins, StataCorp)
Subject   Re: st: Sampling with replacement or bootstrapping in Mata
Date   Fri, 04 Nov 2005 09:38:59 -0600

Michael Manti <> answered his own question about how to sample
with replacement (i.e., bootstrap sample) a vector in Mata.  As he shows, a
three line Mata function will accomplish the task,

        real vector sample(real vector x) {
                real scalar n
                n = length(x)
                return(x[ceil(n * uniform(n, 1))])

Moreover, the line declaring n, -real scalar n-, is not required, but is good
programming practice.  Michael's solution is close to one suggested by Scott
Merryman <> a few weeks ago.

Referring to Bill Gould's <> comment that the "process is not
easy [...]", Michael asks,

> Bill, your comment makes me think that I've missed some subtlety of
> either random number generation or sampling or both. [...]

Just to reassure, Michael, you have missed nothing.  Your solution is both
elegant and correct.  Bill was thinking about what is required in Stata to
implement sampling with replacement, specifically that an expansion of the
vector (data) is required after selecting the replicates and counting the
number of times to replicate each.  He simply had not hit upon how easily this
can be avoided in Mata.

-- Vince

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index