def sample_indices(self, rng): """ :param rng: random number generator :return: (list) indices of sampled categories (in the example above, [1, 0] corresponds to age group 1 and sex group 0. """ probs = [] if self._ifOneDim: probs = self._objs else: for df in self._objs: probs.append(df.get_sum()) empirical_dist = RVGs.Empirical(probabilities=np.array(probs) / sum(probs)) idx = empirical_dist.sample(rng=rng) if self._ifOneDim: return [idx] else: a = [idx] a.extend(self._objs[idx].sample_indices(rng)) return a
def test_fitting_empirical(): print("\nTesting empirical with p=[0.1, 0.2, 0.7]") dist = RVGs.Empirical(probabilities=[0.1, 0.2, 0.7]) data = np.array(get_samples(dist, np.random)) # method of moments dict_mm_results = RVGs.Empirical.fit_mm(data=data, bin_size=1) print(" Fit:") print(" MM:", dict_mm_results)
def test_empirical(rnd, prob): # empirical random variate generator empirical_dist = RVGs.Empirical(prob) # obtain samples samples = get_samples(empirical_dist, rnd) # report mean and variance if type(prob) == list: prob = np.array(prob) outcome = np.array(range(len(prob))) mean = sum(outcome * prob) var = sum((outcome**2) * prob) - mean**2 print_test_results('Empirical', samples, expectation=mean, variance=var)