From ab0cf1df7e889f431a16aa3b5293d6242bdea10c Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Fri, 4 Aug 2017 12:54:03 +0200 Subject: algorithms/bootstrap --- algorithms/bootstrap.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 algorithms/bootstrap.rb (limited to 'algorithms') 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) + -- cgit v1.2.3