def test_save_load(self): fname = 'test-data/test-counter-sampler/test-counter-sampler.gz' # Make a sampler with probabilities proportional to counts counts = range(1,6) counter_sampler = CounterSampler() for outcome, count in enumerate(counts): counter_sampler.update([outcome]*count) counter_sampler.save(fname) new_counter_sampler = CounterSampler() new_counter_sampler.load(fname) self.assertEqual(new_counter_sampler.counts, counts)
def test_sampling(self): ''' Test basic function of assigning counts, and then sampling from The distribution implied by those counts. ''' counts = range(1,6) counter_sampler = CounterSampler() counter_sampler.update(counts) # Test asking for a single sample (where no shape tuple supplied) single_sample = counter_sampler.sample() self.assertTrue(type(single_sample) is np.int64) # Test asking for an array of samples (by passing a shape tuple) shape = (2,3,5) array_sample = counter_sampler.sample(shape) self.assertTrue(type(array_sample) is np.ndarray) self.assertTrue(array_sample.shape == shape)
def test_counter_sampler_statistics(self): ''' This tests that the sampler really does produce results whose statistics match those requested by the counts vector ''' # Seed numpy's random function to make the test reproducible np.random.seed(1) # Make a sampler with probabilities proportional to counts counts = range(1,6) counter_sampler = CounterSampler() for outcome, count in enumerate(counts): counter_sampler.update([outcome]*count) # Draw one hundred thousand samples, then total up the fraction of # each outcome obseved counter = Counter(counter_sampler.sample((100000,))) total = float(sum(counter.values())) found_normalized = [ counter[i] / total for i in range(len(counts)) ] # Make an list of the expected fractions by which each outcome # should be observed, in the limit of infinite sample total_in_expected = float(sum(counts)) expected_normalized = [ c / total_in_expected for c in counts ] # Check if each outcome was observed with a fraction that is within # 0.005 of the expected fraction close = [ abs(f - e) < 0.005 for f,e in zip(found_normalized, expected_normalized) ] self.assertTrue(all(close))