Created
July 1, 2023 19:07
-
-
Save bdatko/a3db959abbcfddc5a2266fa0473d4760 to your computer and use it in GitHub Desktop.
All Factorizing Joint Distributions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import permutations | |
def chain_rule(permutation): | |
"""Compute factorization for a given permutation of variables.""" | |
n = len(permutation) | |
if n == 1: | |
return [f'P({permutation[0]})'] | |
else: | |
conditional = ','.join(permutation[1:]) | |
return [f'P({permutation[0]}|{conditional})'] + chain_rule(permutation[1:]) | |
def compute_factorizations(variables): | |
"""Compute all factorizations of the joint distribution of the given variables.""" | |
factorizations = [] | |
for perm in permutations(variables): | |
factorizations.append(chain_rule(perm)) | |
return factorizations | |
# Test with n=3 | |
variables = ['A', 'B', 'C'] | |
for factoring in compute_factorizations(variables): | |
factoring.reverse() | |
print("".join(factoring)) | |
# Output | |
# P(C)P(B|C)P(A|B,C) | |
# P(B)P(C|B)P(A|C,B) | |
# P(C)P(A|C)P(B|A,C) | |
# P(A)P(C|A)P(B|C,A) | |
# P(B)P(A|B)P(C|A,B) | |
# P(A)P(B|A)P(C|B,A) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obtained from ChatGPT-4 with the following prompts:
Input Prompt
ChatGPT Output
Input Prompt
ChatGPT Output