MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: help on bootstrap sample


I'm totally guessing, but I gather you want sampleNoReplace to return a
sample of size n from 'data', without duplications, with n>Length[data]
resulting in a rearrangement of 'data'.  If so, here's a simple
solution:

ClearAll[sampleNoReplace, sampleReplace]
sampleNoReplace[data_List, n_Integer?Positive] :=
     Take[Sort[data, Random[] > 1/2 &], Min[n, Length[data]]]
sampleReplace[data_List:{0, 1}, n_Integer:1] :=
     data[[Table[Random[Integer, {1, Length@data}], {n}]]]

There's no purpose for Module if you're not declaring local variables.

Bobby

-----Original Message-----
From: Søren Merser [mailto:merser at image.dk] 
To: mathgroup at smc.vnet.net
Subject: [mg37360] [mg37338] help on bootstrap sample


Hi Mathematica freeks
I've made two small rutines for sampling (bootstrap statistics)
They are working , but at least 'SampleNoReplace' is rather slow
I was wondering if any of you know of a faster way to do this
Regards soren

SampelReplace[data_List:{0, 1}, n_:1] := 
  Module[{}, data[[Table[Random[Integer, {1, Length@data} ], {n}]]]]

SampelNoReplace[data_List, n_] := Module[{idx, len, res, d, hi, i},
    d = data;
    res = {};
    len = hi = Length@d;
    
    For[i = 1, i <= n && i <= len, i++,
      idx = Random[Integer, {1, hi--} ];
      AppendTo[res, d[[idx]]];
      d = Drop[d, {idx}];
      ];
    res
    ]







  • Prev by Date: Re: Rounding numbers
  • Next by Date: Re: To plot solutions, FindRoot as a function
  • Previous by thread: help on bootstrap sample
  • Next by thread: RE: help on bootstrap sample