Module: ErrorBootstrapping
- Defined in:
- lib/ml_ratiosolve/error_bootstrapping.rb
Overview
Methods for using parametric bootstrapping to estimate confidence intervals for the ML ratio estimation.
Class Method Summary collapse
-
.bootstrap_ci(results, level) ⇒ Array
Calculate a bootstrapped confidence interval from the output of #estimate_with_gen_data.
-
.estimate_with_gen_data(n_gen, parameters, x, n_iter, tol = nil) ⇒ Array
Re-estimate distribution parameters by generating simulated data a number of times and performing the iterative estimation in MLRatioSolve.
-
.gen_data(parameters, x) ⇒ NMatrix
Generate a set of simulated data consisting of random numbers drawn from the distributions with the supplied parameters x.
-
.randnorm(mu, s2) ⇒ Float
Generate a random normal variate using the Box-Muller transform.
Class Method Details
.bootstrap_ci(results, level) ⇒ Array
Calculate a bootstrapped confidence interval from the output of #estimate_with_gen_data.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ml_ratiosolve/error_bootstrapping.rb', line 112 def bootstrap_ci(results, level) means = results.map { |e| e[:mu].to_a } means = N[*means] size_i = means.shape[1] size_sim = means.shape[0] ci_lower = N.new([size_i], 0.0) ci_upper = N.zeros_like(ci_lower) size_i.times do |i| means_i = means[0...size_sim, i].to_a means_i.flatten! means_i = means_i.select { |e| e.finite? } means_i.sort! lower_ci_index = ((1.0-level)/2.0 * means_i.length).ceil upper_ci_index = ((1.0 - (1.0-level)/2.0) * means_i.length).floor ci_lower[i] = means_i[lower_ci_index] ci_upper[i] = means_i[upper_ci_index] end [ci_lower, ci_upper] end |
.estimate_with_gen_data(n_gen, parameters, x, n_iter, tol = nil) ⇒ Array
Re-estimate distribution parameters by generating simulated data a number of times and performing the iterative estimation in MLRatioSolve
91 92 93 94 95 96 97 98 |
# File 'lib/ml_ratiosolve/error_bootstrapping.rb', line 91 def estimate_with_gen_data(n_gen, parameters, x, n_iter, tol=nil) result = Array.new(n_gen) { nil } n_gen.times do |i| xi = gen_data(parameters, x) result[i] = MLRatioSolve.do_iters_with_start(n_iter, xi, parameters[:gamma], tol) end result end |
.gen_data(parameters, x) ⇒ NMatrix
Generate a set of simulated data consisting of random numbers drawn from the distributions with the supplied parameters
-
Any skipped data points (as returned by MLRatioSolve::skip_indices)
will be set to 0 here.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ml_ratiosolve/error_bootstrapping.rb', line 58 def gen_data(parameters, x) mu = parameters[:mu] sig2 = parameters[:sig2] gamma = parameters[:gamma] size_i = x.shape[0] size_n = x.shape[1] sim_data_out = N.zeros_like(x) size_i.times do |i| size_n.times do |n| next if MLRatioSolve.skip_indices.include?([i,n]) sim_data_out[i,n] = randnorm(mu[i]/gamma[n], sig2[i]/gamma[n]**2) end end sim_data_out end |
.randnorm(mu, s2) ⇒ Float
Generate a random normal variate using the Box-Muller transform
40 41 42 43 44 |
# File 'lib/ml_ratiosolve/error_bootstrapping.rb', line 40 def randnorm(mu, s2) ru0 = rand ru1 = rand ([Math.sqrt(-2.0 * Math.log(ru0)) * Math.cos(2*Math::PI*ru1), Math.sqrt(-2.0 * Math.log(ru0)) * Math.sin(2*Math::PI*ru1)].sample)*Math.sqrt(s2) + mu end |