def test_product_with_badrvs(): """ Test product_distribution() with overlapping rvs specification. """ d = dit.example_dists.Xor() with pytest.raises(Exception): dit.product_distribution(d, [[0, 1], [0]])
def test_product_nonjoint(): """ Test product_distribution() from a ScalarDistribution. """ d = dit.ScalarDistribution([.5, .5]) with pytest.raises(Exception): dit.product_distribution(d)
def test_product_with_badrvs(): """ Test product_distribution() with overlapping rvs specification. """ d = dit.example_dists.Xor() with pytest.raises(Exception): dit.product_distribution(d, [[0,1], [0]])
def marginal_maxent_dists(dist, k_max=None, maxiters=1000, tol=1e-3, verbose=False): """ Return the marginal-constrained maximum entropy distributions. Parameters ---------- dist : distribution The distribution used to constrain the maxent distributions. k_max : int The maximum order to calculate. """ dist = prepare_dist(dist) n_variables = dist.outcome_length() symbols = dist.alphabet[0] if k_max is None: k_max = n_variables outcomes = list(dist._sample_space) # Optimization for the k=0 and k=1 cases are slow since you have to optimize # the full space. We also know the answer in these cases. # This is safe since the distribution must be dense. k0 = dit.Distribution(outcomes, [1] * len(outcomes), base='linear', validate=False) k0.normalize() k1 = dit.product_distribution(dist) dists = [k0, k1] for k in range(k_max + 1): if verbose: print( "Constraining maxent dist to match {0}-way marginals.".format( k)) if k in [0, 1, n_variables]: continue kwargs = {'maxiters': maxiters, 'tol': tol, 'verbose': verbose} pmf_opt, opt = marginal_maxent(dist, k, **kwargs) d = dit.Distribution(outcomes, pmf_opt) d.make_sparse() dists.append(d) # To match the all-way marginal is to match itself. Again, this is a time # savings decision, even though the optimization should be fast. if k_max == n_variables: dists.append(dist) return dists
def test_product_with_rvs2(): """ Test product_distribution() with an rvs specification. """ d = dit.example_dists.Xor() d_iid = dit.product_distribution(d, [[0, 1]]) d_truth = dit.uniform_distribution(2, ['01']) d_truth = dit.modify_outcomes(d_truth, lambda x: ''.join(x)) assert d_truth.is_approx_equal(d_iid)
def test_product(): """ Smoke test for product_distribution(). """ d = dit.example_dists.Xor() d_iid = dit.product_distribution(d) d_truth = dit.uniform_distribution(3, ['01']) d_truth = dit.modify_outcomes(d_truth, lambda x: ''.join(x)) assert d_truth.is_approx_equal(d_iid)
def test_product_with_rvs2(): """ Test product_distribution() with an rvs specification. """ d = dit.example_dists.Xor() d_iid = dit.product_distribution(d, [[0,1]]) d_truth = dit.uniform_distribution(2, ['01']) d_truth = dit.modify_outcomes(d_truth, lambda x: ''.join(x)) assert_true(d_truth.is_approx_equal(d_iid))
def test_product(): """ Smoke test for product_distribution(). """ d = dit.example_dists.Xor() d_iid = dit.product_distribution(d) d_truth = dit.uniform_distribution(3, ['01']) d_truth = dit.modify_outcomes(d_truth, lambda x: ''.join(x)) assert_true(d_truth.is_approx_equal(d_iid))
def marginal_maxent_dists(dist, k_max=None, maxiters=1000, tol=1e-3, verbose=False): """ Return the marginal-constrained maximum entropy distributions. Parameters ---------- dist : distribution The distribution used to constrain the maxent distributions. k_max : int The maximum order to calculate. """ dist = prepare_dist(dist) n_variables = dist.outcome_length() symbols = dist.alphabet[0] if k_max is None: k_max = n_variables outcomes = list(dist._sample_space) # Optimization for the k=0 and k=1 cases are slow since you have to optimize # the full space. We also know the answer in these cases. # This is safe since the distribution must be dense. k0 = dit.Distribution(outcomes, [1]*len(outcomes), base='linear', validate=False) k0.normalize() k1 = dit.product_distribution(dist) dists = [k0, k1] for k in range(k_max + 1): if verbose: print("Constraining maxent dist to match {0}-way marginals.".format(k)) if k in [0, 1, n_variables]: continue kwargs = {'maxiters': maxiters, 'tol': tol, 'verbose': verbose} pmf_opt, opt = marginal_maxent(dist, k, **kwargs) d = dit.Distribution(outcomes, pmf_opt) d.make_sparse() dists.append(d) # To match the all-way marginal is to match itself. Again, this is a time # savings decision, even though the optimization should be fast. if k_max == n_variables: dists.append(dist) return dists