Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 71819ef838
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 48 lines (37 sloc) 1.7 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
"""Main class representing the BLB algorithm.

"""

import random

class BLB:
    def __init__(self, num_subsamples=100, num_bootstraps=25,
                 subsample_len_exp=0.5):
        self.num_subsamples = num_subsamples
        self.num_bootstraps = num_bootstraps
        self.subsample_len_exp = subsample_len_exp

    def run(self, data):
        subsample_estimates = []
        for i in range(self.num_subsamples):
            subsample = self.__subsample(data, self.subsample_len_exp)
            bootstrap_estimates = []
            for j in range(self.num_bootstraps):
                bootstrap = self.__bootstrap(subsample)
                estimate = self.compute_estimate(bootstrap)
                bootstrap_estimates.append(estimate)
            subsample_estimates.append(self.reduce_bootstraps(bootstrap_estimates))
        return self.average(subsample_estimates)
    
    def __subsample(self, data, subsample_len_exp):
        subsample_len = int(len(data) ** subsample_len_exp)
        subsample = random.sample(data, subsample_len)
        return subsample

    def __bootstrap(self, data):
        bootstrap = [random.choice(data) for i in range(len(data))]
        return bootstrap

    # These three methods are to be implemented by subclasses
    def compute_estimate(self, sample):
        '''The actual statistic being computed. E.g. mean, standard deviation,
etc. This is run on just the bootstrapped samples in the inner loops.
'''
        TypeError('compute_estimate not defined')

    def reduce_bootstraps(self, sample):
        TypeError('reduce_bootstraps not defined')

    def average(self, sample):
        TypeError('average not defined')

Something went wrong with that request. Please try again.