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   Michael Manti <[email protected]>
To   [email protected]
Subject   Re: st: Sampling with replacement or bootstrapping in Mata
Date   Fri, 04 Nov 2005 09:46:11 -0500

On Friday, November 04, 2005, at 08:50AM, William Gould, Stata <[email protected]> wrote:

>Michael Manti <[email protected]> asked
>
>> What's the canonical way to sample with replacement from a vector in Mata?
>> I'm trying to implement a bootstrap procedure. I've looked at jumble(), but
>> that samples without replacement, and I'd prefer to avoid calling out to
>> Stata proper if I can. I assume that scaling the results from uniform() in
>> some fashion and using them as subscripts is the way to go, but I can't
>> quite work out what the code should be.
>
>I do not have a satisfactory answer except to say there is no 
>sample-with-replacement function in Mata right now.  The 
>process is not simple or I would  dash off a routine and include
>in this email, or I would tell Michael how to write his own.  Instead, 
>I have just made a note that we need to add this function.
>
>-- Bill
>[email protected]

After thinking a little bit, I came up with the following (algorithmically naive?) solution:

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

Then a random sample from a vector x is just sample(x).

Bill, your comment makes me think that I've missed some subtlety of either random number generation or sampling or both. To me, it seems that the indices should have a discrete uniform distribution, and I believe that's what I'm generating as indices into the original vector. (Though, I haven't run a large number of samples and tested them out yet.)

Thanks again,

Michael Manti
[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