Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created January 30, 2011 21:27
Show Gist options
  • Save jfirebaugh/803264 to your computer and use it in GitHub Desktop.
Save jfirebaugh/803264 to your computer and use it in GitHub Desktop.
# Generate +n+ resamples (with replacement) from +sample+, computing each
# estimate from +estimators+ over each resample. +estimators+ is an array
# of arity 1 procs, where the argument is a +Statsample::Vector+. Returns
# an array of length +estimators.length+, where each element is an array
# of length +n+ containing the computed resample estimates.
#
# Example:
#
# a = (1..100).collect { rand(100) }.to_scale
# rs = Statsample::Resample.resample(a, 10, [:mean.to_proc, :sd.to_proc])
# rs.length #=> 2
# rs[0].size #=> 10
# rs => [Vector(type:scale, n:10)[47.96,48.11,...],
# Vector(type:scale, n:10)[31.341043538800847,...]]
#
def resample(sample, n, estimators)
result = Array.new(estimators.length) { Array.new(n) }
(0...n).each do |j|
resample = sample.sample_with_replacement(sample.size).to_scale
estimators.each_with_index do |est, i|
result[i][j] = est.call(resample)
end
end
result.map {|a| a.to_scale }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment