summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xalgorithms/bootstrap.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/algorithms/bootstrap.rb b/algorithms/bootstrap.rb
new file mode 100755
index 0000000..0f84d57
--- /dev/null
+++ b/algorithms/bootstrap.rb
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+
+aspirin_total = 11037
+aspirin_heart = 104
+aspirin_pop = ([0]*(aspirin_total-aspirin_heart) + [1.0]*aspirin_heart).shuffle
+
+placebo_total = 11034
+placebo_heart = 189
+placebo_pop = ([0]*(placebo_total-placebo_heart) + [1.0]*placebo_heart).shuffle
+
+def sample a, n
+ sample = []
+ n.times {
+ sample << a[rand(n)]
+ }
+
+ return sample
+end
+
+ratios = []
+(0).upto(100000) {
+
+ aspirin_sample = sample(aspirin_pop, aspirin_total)
+ placebo_sample = sample(placebo_pop, placebo_total)
+
+ r = aspirin_sample.inject(:+) / placebo_sample.inject(:+)
+ ratios << r
+}
+
+ratios.sort!
+
+mean = ratios.inject(:+)/ratios.size
+var = 0
+ratios.each { |r|
+ var += (mean - r)**2
+}
+stddev = Math.sqrt(var/ratios.size)
+
+puts "mean = %f"%mean
+puts "stddev = %f"%stddev
+
+_25th = ratios[0,0.25*ratios.size]
+puts "25th = %f"%(_25th.inject(:+)/_25th.size)
+
+_975th = ratios[0.975*ratios.size, ratios.size]
+puts "975th = %f"%(_975th.inject(:+)/_975th.size)
+