Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Last active April 28, 2017 20:19
Show Gist options
  • Save davidrichards/0c30345383ea1b035f04aa4771df0d69 to your computer and use it in GitHub Desktop.
Save davidrichards/0c30345383ea1b035f04aa4771df0d69 to your computer and use it in GitHub Desktop.
A simple true/false sampling tool for sampling n_times, n trials per epoch, with a given probability of success (defaults to 50%).

Try something twice, with a probability of failure at 10%, record wether the cummulative result was failure. Do that 1 million times, and form a posterior belief about this situation.

TruthSampling.call(n_samples: 1_000_000, trials: 2, probability: 0.1)

Or, here's a basketball example. If I've got a 1/6 free throw average, and I take 4 free throws, what is the chance I make at least one of those 4?

TruthSampling.call(trials: 4, probability: 1/6.0)
{false=>0.48256, true=>0.51744}

This gives me about a 52% chance of making at least one basket.

class TruthSampling
def self.call(opts={})
new(opts).call
end
attr_reader :opts
def initialize(opts={})
@opts = opts
end
def n_samples
@n_samples ||= opts.fetch(:n_samples, 100_000)
end
def trials
@trials ||= opts.fetch(:trials, 10)
end
def probability
@probability ||= opts.fetch(:probability, 0.5).to_f
end
def normalize(hash)
total = hash.values.reduce(&:+).to_f
hash.reduce({}) do |h, (k, v)|
h[k] = v / total
h
end
end
def distribution
n_samples.times.reduce(Hash.new(0)) do |hash, _|
hash[sample] += 1
hash
end
end
def sample
trials.times.any? { probe }
end
def probe
rand <= probability
end
def call
normalize(distribution)
end
def inspect
"#{self.class.name} n_samples: #{n_samples}, trials: #{trials}, probability: #{probability}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment