def test_sample_choice(self): prob = array([[0.25, 0.25, 0.25, 0.25], [0, 0, 1, 0], [0.99, 0, 0.001, 0.009]]) start_time = time.time() sample = sample_choice(prob) logger.log_status("sample_choice from (%s, %s) items array in " % prob.shape + str(time.time() - start_time) + " sec") self.assertEqual(sample[0].size, prob.shape[0], msg ="sample size not equal to sample size parameter") self.assertEqual(prob[sample].size, prob.shape[0], msg ="sample size not equal to sample size parameter") assert 0 <= sample[1].min() <= prob.shape[1]-1, "sampled elements not in between min and max of source array" assert 0 <= sample[1].max() <= prob.shape[1]-1, "sampled elements not in between min and max of source array" assert all(not_equal(prob[sample], 0.0)), "elements with zero weight in the sample"
def run(self, probability, resources=None): """ Compute choices according to given probability. 'probability' is a 2D numpy array (nobservations x nequations). The returned value is a 1D array of choice indices [0, nequations-1] of length nobservations. """ if probability.ndim == 1: # if 1d array, add column of 1-probability probability = reshape(probability, (probability.size,1)) probability = concatenate((1.0-probability, probability), axis=1) if probability.ndim < 2: raise StandardError, "Argument 'probability' must be a 2D numpy array." prob = probability/reshape(probability.sum(axis=1),(probability.shape[0],1)) return sample_choice(prob)[1]
def run(self, probability, resources=None): """ Compute choices according to given probability. 'probability' is a 2D numpy array (nobservations x nequations). The returned value is a 1D array of choice indices [0, nequations-1] of length nobservations. """ if probability.ndim == 1: # if 1d array, add column of 1-probability probability = reshape(probability, (probability.size, 1)) probability = concatenate((1.0 - probability, probability), axis=1) if probability.ndim < 2: raise StandardError, "Argument 'probability' must be a 2D numpy array." prob = probability / reshape(probability.sum(axis=1), (probability.shape[0], 1)) return sample_choice(prob)[1]