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   [email protected] (Vince Wiggins, StataCorp)
To   [email protected]
Subject   Re: st: Sampling with replacement or bootstrapping in Mata
Date   Fri, 04 Nov 2005 09:38:59 -0600

Michael Manti <[email protected]> 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 <[email protected]> a few weeks ago.

Referring to Bill Gould's <[email protected]> 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
   [email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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