def all_info_measures(vars): """ """ for stuff in islice(powerset(vars), 1, None): others = set(vars) - set(stuff) for part in partitions(stuff, tuples=True): for cond in powerset(others): yield (part , cond)
def all_info_measures(vars): """ """ for stuff in islice(powerset(vars), 1, None): others = set(vars) - set(stuff) for part in partitions(stuff, tuples=True): for cond in powerset(others): yield (part, cond)
def full_constraint_lattice(elements): """ Return a lattice of constrained marginals, with the same partial order relationship as Ryan James' constraint lattice, but where the nodes are not restricted to cover the whole set of variables. Parameters ---------- elements : iter of iters Input variables to the PID. Returns ------- lattice : nx.DiGraph The lattice of antichains. """ def comparable(a, b): return a < b or b < a def antichain(ss): if not ss: return False return all(not comparable(frozenset(a), frozenset(b)) for a, b in combinations(ss, 2)) def less_than(sss1, sss2): return all(any(set(ss1) <= set(ss2) for ss2 in sss2) for ss1 in sss1) def normalize(sss): return tuple( sorted(tuple(tuple(sorted(ss)) for ss in sss), key=lambda s: (-len(s), s))) # Enumerate all nodes in the lattice (same nodes as in usual PID lattice) elements = set(elements) combos = (sum(s, tuple()) for s in powerset(elements)) pps = [ss for ss in powerset(combos) if antichain(ss)] # Compute all order relationships using the constraint lattice ordering order = [(a, b) for a, b in permutations(pps, 2) if less_than(a, b)] # Build the DiGraph by removing redundant order relationships lattice = nx.DiGraph() for a, b in order: if not any(((a, c) in order) and ((c, b) in order) for c in pps): lattice.add_edge(normalize(b), normalize(a)) lattice.root = next(iter(nx.topological_sort(lattice))) return lattice
def test_tse1(i, j): """ Test identity comparing TSE to B from Olbrich's talk """ d = n_mod_m(i, j) indices = [[k] for k in range(i)] tse = TSE(d) x = 1 / 2 * sum(B(d, rv) / nCk(i, len(rv)) for rv in powerset(indices)) assert tse == pytest.approx(x)
def method1(): """ # of ops = len(F) * 2**len(largest element in F) """ atoms = [] for cet in F: if not cet: # An atom must be nonempty. continue # Now look at all nonempty, proper subsets of cet. # # If you have a sample space with 64 elements, and then consider # the trivial sigma algebra, then one element of F will be the # empty set, while the other will have 64 elements. Taking the # powerset of this set will require going through a list of 2^64 # elements...in addition to taking forever, we can't even store # that in memory. # subsets = sorted(powerset(cet))[1:-1] # nonempty and proper for subset in subsets: if frozenset(subset) in F: break else: # Then `cet` has no nonempty proper subset that is also in F. atoms.append(frozenset(cet)) return atoms
def method1(): """ # of ops = len(F) * 2**len(largest element in F) """ atoms = [] for cet in F: if not cet: # An atom must be nonempty. continue # Now look at all nonempty, proper subsets of cet. # # If you have a sample space with 64 elements, and then consider # the trivial sigma algebra, then one element of F will be the # empty set, while the other will have 64 elements. Taking the # powerset of this set will require going through a list of 2^64 # elements...in addition to taking forever, we can't even store # that in memory. # subsets = list(powerset(cet))[1:-1] # nonempty and proper for subset in subsets: if frozenset(subset) in F: break else: # Then `cet` has no nonempty proper subset that is also in F. atoms.append(frozenset(cet)) return atoms
def test_tse1(i, j): """ Test identity comparing TSE to B from Olbrich's talk """ d = n_mod_m(i, j) indices = [[k] for k in range(i)] tse = TSE(d) x = 1/2 * sum(B(d, rv)/nCk(i, len(rv)) for rv in powerset(indices)) assert tse == pytest.approx(x)
def test_join_sigalg(): """ Test join_sigalg """ outcomes = ['00', '01', '10', '11'] pmf = [1 / 4] * 4 d = Distribution(outcomes, pmf) sigalg = frozenset([frozenset(_) for _ in powerset(outcomes)]) joined = join_sigalg(d, [[0], [1]]) assert sigalg == joined
def test_join_sigalg(): """ Test join_sigalg """ outcomes = ['00', '01', '10', '11'] pmf = [1/4]*4 d = Distribution(outcomes, pmf) sigalg = frozenset([frozenset(_) for _ in powerset(outcomes)]) joined = join_sigalg(d, [[0], [1]]) assert sigalg == joined
def event_space(self): """ Returns a generator over the event space. The event space is the powerset of the sample space. """ from dit.utils import powerset return powerset(list(self.sample_space()))
def atom_set(F, X=None): """ Returns the atoms of the sigma-algebra F. Parameters ---------- F : set of frozensets The candidate sigma algebra. X : frozenset, None The universal set. If None, then X is taken to be the union of the sets in F. Returns ------- atoms : frozenset A frozenset of frozensets, representing the atoms of the sigma algebra. """ if not isinstance(next(iter(F)), frozenset): raise Exception('Input to `atom_set` must contain frozensets.') atoms = [] for cet in F: if not cet: # An atom must be nonempty. continue # Now look at all nonempty, proper subsets of cet. subsets = list(powerset(cet))[1:-1] for subset in subsets: if frozenset(subset) in F: break else: # Then `cet` has no proper subset that is also in F. atoms.append(frozenset(cet)) return frozenset(atoms)