Last active
February 7, 2020 17:14
-
-
Save GregSilverman/3e09cb6b7c7bf664b4df14d309192bb3 to your computer and use it in GitHub Desktop.
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
""" | |
CALCULATION FOR NUMBER OF EXPECTED EXPRESSIONS NOTATION README -> | |
CASE: 5 SYSTEMS | |
I'm using the following notation | |
# N_X_Ys is the number of combinations present using Y Systems, selected from X Total Systems. | |
# FOR EXAMPLE N_5_2s is the number of combinations using 2 systems, selected from 5 total systems | |
# N_X_Y_PQs is the number of combinations present using Y Systems by adding system combinations of P and Q elements. | |
# FOR EXAMPLE N_5_4_31s is the number of combinations using 4 systems formed by joining a combination of 3 systems and a combination of 1 system. | |
Author: Michael v. Heinz | |
""" | |
def n_expected_expressions(): | |
TOTAL = 5 # This is the total number of systems we're using. | |
def n_5_s(TOTAL): | |
X = 1 # This is the # of combinations present using X SYSTEMS | |
N_5_1s = math.factorial(TOTAL)/(math.factorial(TOTAL-X)*math.factorial(X)) | |
print(N_5_1s) | |
return N_5_1s | |
N_5_1s = n_5_s(TOTAL) | |
# B Lets look at possible combinations using 2 systems. | |
def n_5_2s(TOTAL): | |
X = 2 | |
N_5_2s = math.factorial(TOTAL)/(math.factorial(TOTAL-X)*math.factorial(X)) * 2 # multiply by 2 to account for binary operations | |
print(N_5_2s) | |
return N_5_2s | |
N_5_2s = n_5_2s(TOTAL) | |
# C Lets look at possible combinations using 3 systems | |
def n_5_3s(N_5_2s, N_5_1s): | |
X=3 | |
N_5_3s = N_5_2s * N_5_1s * (3/5) # we take cartesian product of combinations of 2s and combinations of 1s | |
# we multiply by 3/5 because we have only 3 options left since 2 are already used in the combination of 2 systems | |
N_5_3s = N_5_3s * 2 # we multiply by 2 to account for binary operations | |
print(N_5_3s) | |
return N_5_3s | |
N_5_3s = n_5_3s(N_5_2s, N_5_1s) | |
def n_5_4s(N_5_3s, N_5_2s, N_5_1s): | |
# Lets Look at possible combinations of 4 systems drawing from set of 5 systems | |
# We can get to 4 systems with 3 systems + 1 system | |
# We can also get to 4 systems with 2 systems + 2 systems | |
X=4 | |
#(1) Let's start with 3 systems + 1 system | |
# Lets start by taking the cartesian product of N_5_3s and N_5_1s and multiplying by (2/5) | |
N_5_4_31s = N_5_3s * N_5_1s * (2/5) # we multiply by (2/5) in this case b/c we have two choices left from the 5 | |
N_5_4_31s = N_5_4_31s * 2 # we multiply by 2 because we have 2 options for operator between the 3 systems and 1 system | |
print(N_5_4_31s) | |
#(2) Now lets look at 2 systems + 2 sytems to make 4. THis will be a bit more challenging. We have to draw from our | |
# N_5_2s | |
N_5_4_22s = N_5_2s * N_5_2s * (3/10) * (1/2) * 2 # We first find the cartesian product of combos of 2 with combos of 2 | |
# Then we multiply by (1/2) because we don't care about order | |
# Then we multiply by (3/10), which is derived from C(3,2)/C(5,2) | |
# Then we multiply by 2 to account for choice of binary operators | |
N_5_4s = N_5_4_22s + N_5_4_31s | |
print(N_5_4_22s) | |
print(N_5_4s) | |
return N_5_4s | |
N_5_4s = n_5_4s(N_5_3s, N_5_2s, N_5_1s) | |
def n_5_5_total(N_5_4s, N_5_3s, N_5_2s, N_5_1s): | |
# Lets Look at possible combinations of 5 systems drawing from set of 5 systems | |
# We can get to 5 systems with 4 systems + 1 system | |
# We can also get to 5 systems with 3 systems + 2 systems | |
#(1) Let's start with 4 systems + 1 system | |
# Lets start by taking the cartesian product of N_5_4s and N_5_1s | |
# RECALL THAT N_5_4s includes combinations of 2s and 2s, as well as combinations of 3s and 1s. We need to | |
#take both into account. | |
N_5_5_41s = N_5_4s * N_5_1s * (1/5) * 2 # we multiply by 1/5 because we only have 1 element left in the N_5_1s | |
# that has not already been used in the N_5_4s. We multiply by the 2 to account for binary operation. | |
print(N_5_5_41s) | |
# (2) Now lets move to 2 systems + 3 systems | |
N_5_5_32s = N_5_3s * N_5_2s * 1/10 * 2 # we multiply by 1/10 because there is only one possible combination in the 2s | |
print(N_5_5_32s) | |
# that will fit with the 3s (ie, C(2,2)/C(5,2)= (1/10)); we multiply by 2 to account for binary operators | |
N_5_5s = N_5_5_32s+N_5_5_41s | |
print(N_5_5s) | |
return N_5_1s + N_5_2s + N_5_3s + N_5_4s + N_5_5s | |
print('Total:', n_5_5_total(N_5_4s, N_5_3s, N_5_2s, N_5_1s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment