diff --git a/hw3/bayesian_network_1_hw3.png b/hw3/bayesian_network_1_hw3.png new file mode 100644 index 0000000..b528b30 Binary files /dev/null and b/hw3/bayesian_network_1_hw3.png differ diff --git a/hw3/bayesian_network_1_hw3.svg b/hw3/bayesian_network_1_hw3.svg new file mode 100644 index 0000000..c0be55b --- /dev/null +++ b/hw3/bayesian_network_1_hw3.svg @@ -0,0 +1,4 @@ + + + +
A
A
B
B
C
C
E
E
D
D
Text is not SVG - cannot display
\ No newline at end of file diff --git a/hw3/data/family0.csv b/hw3/data/family0.csv new file mode 100644 index 0000000..fa103d1 --- /dev/null +++ b/hw3/data/family0.csv @@ -0,0 +1,4 @@ +name,mother,father,trait +Harry,Lily,James, +James,,,1 +Lily,,,0 diff --git a/hw3/data/family1.csv b/hw3/data/family1.csv new file mode 100644 index 0000000..786b300 --- /dev/null +++ b/hw3/data/family1.csv @@ -0,0 +1,7 @@ +name,mother,father,trait +Arthur,,,0 +Charlie,Molly,Arthur,0 +Fred,Molly,Arthur,1 +Ginny,Molly,Arthur, +Molly,,,0 +Ron,Molly,Arthur, diff --git a/hw3/data/family2.csv b/hw3/data/family2.csv new file mode 100644 index 0000000..b609dc0 --- /dev/null +++ b/hw3/data/family2.csv @@ -0,0 +1,6 @@ +name,mother,father,trait +Arthur,,,0 +Hermione,,,0 +Molly,,, +Ron,Molly,Arthur,0 +Rose,Ron,Hermione,1 diff --git a/hw3/heredity.py b/hw3/heredity.py new file mode 100644 index 0000000..6469519 --- /dev/null +++ b/hw3/heredity.py @@ -0,0 +1,286 @@ +import csv +import itertools +import sys + +PROBS = { + + # Unconditional probabilities for having gene + "gene": { + 2: 0.01, + 1: 0.03, + 0: 0.96 + }, + + "trait": { + + # Probability of trait given two copies of gene + 2: { + True: 0.65, + False: 0.35 + }, + + # Probability of trait given one copy of gene + 1: { + True: 0.56, + False: 0.44 + }, + + # Probability of trait given no gene + 0: { + True: 0.01, + False: 0.99 + } + }, + + # Mutation probability + "mutation": 0.01 +} + + +def main(): + + # Check for proper usage + if len(sys.argv) != 2: + sys.exit("Usage: python heredity.py data.csv") + people = load_data(sys.argv[1]) + + # Keep track of gene and trait probabilities for each person + probabilities = { + person: { + "gene": { + 2: 0, + 1: 0, + 0: 0 + }, + "trait": { + True: 0, + False: 0 + } + } + for person in people + } + + # Loop over all sets of people who might have the trait + names = set(people) + for have_trait in powerset(names): + + # Check if current set of people violates known information + fails_evidence = any( + (people[person]["trait"] is not None and + people[person]["trait"] != (person in have_trait)) + for person in names + ) + if fails_evidence: + continue + + # Loop over all sets of people who might have the gene + for one_gene in powerset(names): + for two_genes in powerset(names - one_gene): + + # Update probabilities with new joint probability + p = joint_probability(people, one_gene, two_genes, have_trait) + update(probabilities, one_gene, two_genes, have_trait, p) + + # Ensure probabilities sum to 1 + normalize(probabilities) + + # Print results + for person in people: + print(f"{person}:") + for field in probabilities[person]: + print(f" {field.capitalize()}:") + for value in probabilities[person][field]: + p = probabilities[person][field][value] + print(f" {value}: {p:.4f}") + + +def load_data(filename): + """ + Load gene and trait data from a file into a dictionary. + File assumed to be a CSV containing fields name, mother, father, trait. + mother, father must both be blank, or both be valid names in the CSV. + trait should be 0 or 1 if trait is known, blank otherwise. + """ + data = dict() + with open(filename) as f: + reader = csv.DictReader(f) + for row in reader: + name = row["name"] + data[name] = { + "name": name, + "mother": row["mother"] or None, + "father": row["father"] or None, + "trait": (True if row["trait"] == "1" else + False if row["trait"] == "0" else None) + } + return data + + +def powerset(s): + """ + Return a list of all possible subsets of set s. + """ + s = list(s) + return [ + set(s) for s in itertools.chain.from_iterable( + itertools.combinations(s, r) for r in range(len(s) + 1) + ) + ] + + +def joint_probability(people, one_gene, two_genes, have_trait): + """ + Compute and return a joint probability. + + Args: + - people is a dictionary of people. The keys represent names, and + the values are dictionaries that contain mother and father keys. + You may assume that either mother and father are both blank (no + parental information in the data set), or mother and father will + both refer to other people in the people dictionary. + - one_gene is a set of all people for whom we want to compute the + probability that they have one copy of the gene. + - two_genes is a set of all people for whom we want to compute the + probability that they have two copies of the gene. + - have_trait is a set of all people for whom we want to compute the + probability that they have the trait. + + The probability returned should be the probability that + - everyone in set `one_gene` has one copy of the gene, and + - everyone in set `two_genes` has two copies of the gene, and + - everyone not in `one_gene` or `two_gene` does not have the gene, and + - everyone in set `have_trait` has the trait, and + - everyone not in set` have_trait` does not have the trait. + + and trait = {"Harry", "James"} should calculate the probability that + Lily has zero copies of the gene, Harry has one copy of the gene, + James has two copies of the gene, Harry exhibits the trait, James + exhibits the trait, and Lily does not exhibit the trait. + + For anyone with no parents listed in the data set, use the probability + distribution PROBS["gene"] to determine the probability that they have + a particular number of the gene. + + For anyone with parents in the data set, each parent will pass one of + their two genes on to their child randomly, and there is a + PROBS["mutation"] chance that it mutates (goes from being the gene to not + being the gene, or vice versa). + + Use the probability distribution PROBS["trait"] to compute the probability + that a person does or does not have a particular trait. + """ + probability = 1 + + for person in people: + # Check if the person has one copy of the gene + if person in one_gene: + # Calculate the probability of having one copy of the gene + gene_prob = PROBS["gene"][1] + # Check if the person has two copies of the gene + elif person in two_genes: + # Calculate the probability of having two copies of the gene + gene_prob = PROBS["gene"][2] + else: + # Calculate the probability of not having the gene + gene_prob = PROBS["gene"][0] + + # Check if the person has the trait + if person in have_trait: + # Calculate the probability of having the trait + trait_prob = PROBS["trait"][gene_prob][True] + else: + # Calculate the probability of not having the trait + trait_prob = PROBS["trait"][gene_prob][False] + + # Multiply the gene and trait probabilities + probability *= gene_prob * trait_prob + + return probability + + + +def update(probabilities, one_gene, two_genes, have_trait, p): + """ + Add to `probabilities` a new joint probability `p`. + Each person should have their "gene" and "trait" distributions updated. + Which value for each distribution is updated depends on whether + the person is in `have_gene` and `have_trait`, respectively. + + Args: + - probabilities is a dictionary of people. Each person is mapped to a + "gene" distribution and a "trait" distribution. + - one_gene is a set of people with one copy of the gene in the current + joint distribution. + - two_genes is a set of people with two copies of the gene in the + current joint distribution. + - have_trait is a set of people with the trait in the current joint + distribution. + - p is the probability of the joint distribution. + + For each person person in `probabilities`, the function should update + the probabilities[person]["gene"] distribution and probabilities[person]["trait"] + distribution by adding p to the appropriate value in each distribution. + All other values should be left unchanged. + + For example, if "Harry" were in both two_genes and in have_trait, then p would + be added to probabilities["Harry"]["gene"][2] and to + probabilities["Harry"]["trait"][True]. + + The function should not return any value: it just needs to update the + probabilities dictionary. + """ + + for person in probabilities: + if person in one_gene: + probabilities[person]["gene"][1] += p + elif person in two_genes: + probabilities[person]["gene"][2] += p + else: + probabilities[person]["gene"][0] += p + + if person in have_trait: + probabilities[person]["trait"][True] += p + else: + probabilities[person]["trait"][False] += p + + +def normalize(probabilities): + """ + Update `probabilities` such that each probability distribution + is normalized (i.e., sums to 1, with relative proportions the same). + + Args: + - probabilities is a dictionary of people. Each person is mapped to a + "gene" distribution and a "trait" distribution. + + For both of the distributions for each person in probabilities, this + function should normalize that distribution so that the values in the + distribution sum to 1, and the relative values in the distribution are the same. + + For example, if probabilities["Harry"]["trait"][True] were equal to 0.1 and + probabilities["Harry"]["trait"][False] were equal to 0.3, then your function + should update the former value to be 0.25 and the latter value to be 0.75: the + numbers now sum to 1, and the latter value is still three times larger than + the former value. + + The function should not return any value: it just needs to update the + probabilities dictionary. + """ + + for person in probabilities: + # Calculate the sum of the gene probabilities + gene_sum = sum(probabilities[person]["gene"].values()) + # Calculate the sum of the trait probabilities + trait_sum = sum(probabilities[person]["trait"].values()) + + # Normalize the gene probabilities + for gene in probabilities[person]["gene"]: + probabilities[person]["gene"][gene] /= gene_sum + + # Normalize the trait probabilities + for trait in probabilities[person]["trait"]: + probabilities[person]["trait"][trait] /= trait_sum + + +if __name__ == "__main__": + main() diff --git a/hw3/hw3.pdf b/hw3/hw3.pdf new file mode 100644 index 0000000..013f10a Binary files /dev/null and b/hw3/hw3.pdf differ diff --git a/hw3/motorcade.png b/hw3/motorcade.png new file mode 100644 index 0000000..cae9b14 Binary files /dev/null and b/hw3/motorcade.png differ diff --git a/main.aux b/main.aux index afe5557..d9e7752 100644 --- a/main.aux +++ b/main.aux @@ -14,4 +14,7 @@ \@writefile{toc}{\contentsline {subsection}{\numberline {2.1}Cake}{8}{}\protected@file@percent } \@writefile{toc}{\contentsline {subsection}{\numberline {2.2}Knights and Knaves}{10}{}\protected@file@percent } \@writefile{toc}{\contentsline {subsection}{\numberline {2.3}Odds and Evens}{10}{}\protected@file@percent } -\gdef \@abspage@last{11} +\@writefile{toc}{\contentsline {section}{\numberline {3}Homework 3}{10}{}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {3.1}Probability}{10}{}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {3.2}Bayesian Networks}{12}{}\protected@file@percent } +\gdef \@abspage@last{14} diff --git a/main.fdb_latexmk b/main.fdb_latexmk index 2339b38..18521c7 100644 --- a/main.fdb_latexmk +++ b/main.fdb_latexmk @@ -1,8 +1,8 @@ # Fdb version 4 -["pdflatex"] 1710976757.77869 "c:/Users/uzair/OneDrive/Documents/Programming/classes/spring-2024/CSC_665_Homework/main.tex" "main.pdf" "main" 1710976758.61168 0 - "C:/Users/uzair/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map" 1710451113 80909 eab91d9745dd2edfd62a31d53cd5fe15 "" - "C:/Users/uzair/AppData/Local/MiKTeX/fonts/pk/ljfour/jknappen/ec/dpi600/tcrm1000.pk" 1710450570 11548 75f9244b55c9330932ad6d45f833429f "" - "C:/Users/uzair/AppData/Local/MiKTeX/miktex/data/le/pdftex/pdflatex.fmt" 1710872278 24233103 0233cc216d9fba92d9492b92406b1361 "" +["pdflatex"] 1712556659.16135 "d:/uzair/projects/sfsu/csc-665-homework/main.tex" "main.pdf" "main" 1712556660.13536 0 + "C:/Users/uzair/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map" 1712556651 80909 eab91d9745dd2edfd62a31d53cd5fe15 "" + "C:/Users/uzair/AppData/Local/MiKTeX/fonts/pk/ljfour/jknappen/ec/dpi600/tcrm1000.pk" 1712556658 11548 a6bc95c64a58583bcc706361e95fcb96 "" + "C:/Users/uzair/AppData/Local/MiKTeX/miktex/data/le/pdftex/pdflatex.fmt" 1712556641 24236005 6c179fc8e955800dbf130a4c2d55d82a "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/jknappen/ec/tcrm1000.tfm" 993062508 1436 c7f957a372ef2fbe93c0982f96625e12 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/public/amsfonts/cmextra/cmex7.tfm" 1233951848 1004 54797486969f23fa377b128694d548df "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/fonts/tfm/public/amsfonts/symbols/msam10.tfm" 1233951854 916 f87d7c45f9c908e672703b83b72241a3 "" @@ -49,16 +49,18 @@ "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/graphicx.sty" 1665067579 8010 a8d949cbdbc5c983593827c9eec252e1 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/keyval.sty" 1665067579 2671 7e67d78d9b88c845599a85b2d41f2e39 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/graphics/trig.sty" 1665067579 4023 293ea1c16429fc0c4cf605f4da1791a9 "" - "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/l3backend/l3backend-pdftex.def" 1708427688 30006 3d512c0edd558928ddea1690180ef77e "" - "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mathtools.sty" 1656514886 62269 5c1837a5bc5db4c0d255eedc225ca44b "" - "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mhsetup.sty" 1656514886 5582 a43dedf8e5ec418356f1e9dfe5d29fc3 "" + "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/l3backend/l3backend-pdftex.def" 1710412547 29918 d08e4795c0778dfb000e82d9225c6ce2 "" + "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mathtools.sty" 1710159379 62672 9ff036bc89365461cc2bd482cc1e4879 "" + "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/mathtools/mhsetup.sty" 1710159379 5582 a43dedf8e5ec418356f1e9dfe5d29fc3 "" "C:/Users/uzair/AppData/Local/Programs/MiKTeX/tex/latex/tools/calc.sty" 1700599895 10214 547fd4d29642cb7c80bf54b49d447f01 "" - "c:/Users/uzair/OneDrive/Documents/Programming/classes/spring-2024/CSC_665_Homework/main.tex" 1710976753 15294 eefeb5115f03a7d6344da121e4de1ec8 "" - "hw2/gameTree.png" 1710872445 51736 b13904e696801b947e455a901505f683 "" - "main.aux" 1710976758 1503 e54afcc87f6e15c8bb521b6c8a85270b "pdflatex" - "main.tex" 1710976753 15294 eefeb5115f03a7d6344da121e4de1ec8 "" - "titlePage.aux" 1710976758 456 c979711f53deacf2b09031977e556db8 "pdflatex" - "titlePage.tex" 1710042044 407 ccdbd50244b3194dbad72dd1c7995bf0 "" + "d:/uzair/projects/sfsu/csc-665-homework/main.tex" 1712556613 22454 30dc189b4988585fda24a0c9b097eb14 "" + "hw2/gameTree.png" 1712556571 51736 b13904e696801b947e455a901505f683 "" + "hw3/bayesian_network_1_hw3.png" 1712533911 42320 08df33c7ebceab0a98b148f3e43f6efe "" + "hw3/motorcade.png" 1712535186 144191 cdb52bfe315df9dd25d1d06a6414fa5c "" + "main.aux" 1712556659 1818 bf4e5b12f9cccaa5b4ee523e32be112d "pdflatex" + "main.tex" 1712556613 22454 30dc189b4988585fda24a0c9b097eb14 "" + "titlePage.aux" 1712556659 456 c979711f53deacf2b09031977e556db8 "pdflatex" + "titlePage.tex" 1712556571 407 ccdbd50244b3194dbad72dd1c7995bf0 "" (generated) "main.aux" "main.log" diff --git a/main.fls b/main.fls index 6ae3368..adc4d6d 100644 --- a/main.fls +++ b/main.fls @@ -1,6 +1,6 @@ -PWD c:\Users\uzair\OneDrive\Documents\Programming\classes\spring-2024\CSC_665_Homework +PWD d:\uzair\projects\sfsu\csc-665-homework INPUT C:\Users\uzair\AppData\Local\MiKTeX\miktex\data\le\pdftex\pdflatex.fmt -INPUT c:\Users\uzair\OneDrive\Documents\Programming\classes\spring-2024\CSC_665_Homework\main.tex +INPUT d:\uzair\projects\sfsu\csc-665-homework\main.tex OUTPUT main.log INPUT C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex\latex\base\article.cls INPUT C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex\latex\base\article.cls @@ -98,6 +98,20 @@ INPUT .\hw2\gameTree.png INPUT .\hw2\gameTree.png INPUT .\hw2\gameTree.png INPUT .\hw2\gameTree.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\bayesian_network_1_hw3.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png +INPUT .\hw3\motorcade.png INPUT main.aux INPUT .\titlePage.aux INPUT .\titlePage.aux diff --git a/main.log b/main.log index ac32880..2ed6356 100644 --- a/main.log +++ b/main.log @@ -1,12 +1,12 @@ -This is pdfTeX, Version 3.141592653-2.6-1.40.26 (MiKTeX 24.3) (preloaded format=pdflatex 2024.3.19) 20 MAR 2024 16:19 +This is pdfTeX, Version 3.141592653-2.6-1.40.26 (MiKTeX 24.3.31) (preloaded format=pdflatex 2024.4.7) 7 APR 2024 23:10 entering extended mode restricted \write18 enabled. file:line:error style messages enabled. %&-line parsing enabled. -**c:/Users/uzair/OneDrive/Documents/Programming/classes/spring-2024/CSC_665_Homework/main.tex -(c:/Users/uzair/OneDrive/Documents/Programming/classes/spring-2024/CSC_665_Homework/main.tex +**d:/uzair/projects/sfsu/csc-665-homework/main.tex +(d:/uzair/projects/sfsu/csc-665-homework/main.tex LaTeX2e <2023-11-01> patch level 1 -L3 programming layer <2024-02-20> +L3 programming layer <2024-03-14> (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/base\article.cls Document Class: article 2023/05/17 v1.4n Standard LaTeX document class (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/base\size10.clo @@ -88,7 +88,7 @@ LaTeX Info: Redefining \Relbar on input line 971. LaTeX Info: Redefining \[ on input line 2953. LaTeX Info: Redefining \] on input line 2954. ) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/mathtools\mathtools.sty -Package: mathtools 2022/06/29 v1.29 mathematical typesetting tools +Package: mathtools 2024/03/11 v1.30 mathematical typesetting tools (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/graphics\keyval.sty Package: keyval 2022/05/29 v1.15 key=value parser (DPC) \KV@toks@=\toks22 @@ -130,7 +130,7 @@ LaTeX Font Info: Redeclaring math symbol \hbar on input line 98. LaTeX Font Info: Overwriting math alphabet `\mathfrak' in version `bold' (Font) U/euf/m/n --> U/euf/b/n on input line 106. )) (C:\Users\uzair\AppData\Local\Programs\MiKTeX\tex/latex/l3backend\l3backend-pdftex.def -File: l3backend-pdftex.def 2024-02-20 L3 backend support: PDF output (pdfTeX) +File: l3backend-pdftex.def 2024-03-14 L3 backend support: PDF output (pdfTeX) \l__color_backend_stack_int=\count278 \l__pdf_internal_box=\box54 ) (main.aux (titlePage.aux)) @@ -221,24 +221,40 @@ File: hw2/gameTree.png Graphic file (type png) Package pdftex.def Info: hw2/gameTree.png used on input line 271. (pdftex.def) Requested size: 153.57494pt x 128.73192pt. - [8] [9 <./hw2/gameTree.png>] [10] (main.aux (titlePage.aux)) + [8] [9 <./hw2/gameTree.png>] [10] [11] + +File: hw3/bayesian_network_1_hw3.png Graphic file (type png) + +Package pdftex.def Info: hw3/bayesian_network_1_hw3.png used on input line 424. +(pdftex.def) Requested size: 117.74077pt x 90.63931pt. + +File: hw3/motorcade.png Graphic file (type png) + +Package pdftex.def Info: hw3/motorcade.png used on input line 445. +(pdftex.def) Requested size: 182.25731pt x 180.90224pt. + [12 <./hw3/bayesian_network_1_hw3.png>] +Underfull \hbox (badness 10000) in paragraph at lines 448--449 + + [] + +[13 <./hw3/motorcade.png>] (main.aux (titlePage.aux)) *********** LaTeX2e <2023-11-01> patch level 1 -L3 programming layer <2024-02-20> +L3 programming layer <2024-03-14> *********** ) Here is how much of TeX's memory you used: - 3231 strings out of 474436 - 50875 string characters out of 5741954 - 1938190 words of memory out of 5000000 - 25549 multiletter control sequences out of 15000+600000 + 3249 strings out of 474424 + 50932 string characters out of 5741740 + 1939496 words of memory out of 5000000 + 25579 multiletter control sequences out of 15000+600000 562573 words of font info for 53 fonts, out of 8000000 for 9000 1141 hyphenation exceptions out of 8191 - 65i,19n,72p,505b,279s stack positions out of 10000i,1000n,20000p,200000b,200000s + 65i,19n,72p,462b,279s stack positions out of 10000i,1000n,20000p,200000b,200000s -Output written on main.pdf (11 pages, 219801 bytes). +Output written on main.pdf (14 pages, 381374 bytes). PDF statistics: - 106 PDF objects out of 1000 (max. 8388607) + 120 PDF objects out of 1000 (max. 8388607) 0 named destinations out of 1000 (max. 500000) - 6 words of extra memory for PDF output out of 10000 (max. 10000000) + 16 words of extra memory for PDF output out of 10000 (max. 10000000) diff --git a/main.pdf b/main.pdf index b3f875e..42b6fe8 100644 Binary files a/main.pdf and b/main.pdf differ diff --git a/main.synctex.gz b/main.synctex.gz index 164b79d..334b659 100644 Binary files a/main.synctex.gz and b/main.synctex.gz differ diff --git a/main.tex b/main.tex index dc8188d..714de15 100644 --- a/main.tex +++ b/main.tex @@ -18,9 +18,9 @@ My current workflow is as follows: \begin{enumerate} \item Read the homework instructions and try to understand the assignment. - + \item Work by hand, on my tablet, in the Samsung Notes app. - + \item Beautify my work by transcribing it here using \LaTeX. \end{enumerate} When it's time to submit an assignment, I will export this document as a PDF file and turn in just the relevant pages. Please let me know what you think of this format! @@ -34,157 +34,157 @@ When it's time to submit an assignment, I will export this document as a PDF fil \begin{enumerate} \item[a.] My pronouns are he/him. \item[b.] I've taken a lot of math and computer science courses. I'm not sure if I can list them, as I took most of them at the College of San Mateo and their course numbers are different. However, off the top of my head, I've taken: - \begin{itemize} - \item Calculus 1 - \item Calculus 2 - \item Calculus 3 - \item Discrete Mathematics - \item Linear Algebra - \item Analysis of Algorithms - \item Data Structures - \end{itemize} - This list consists of the courses I think are relevant to this class; I've taken other CS courses, of course. + \begin{itemize} + \item Calculus 1 + \item Calculus 2 + \item Calculus 3 + \item Discrete Mathematics + \item Linear Algebra + \item Analysis of Algorithms + \item Data Structures + \end{itemize} + This list consists of the courses I think are relevant to this class; I've taken other CS courses, of course. \item [c.] Yes, I am okay with being called on. I won't always know the answer, and I might embarrass myself sometimes, but that's alright because I believe it will force me to pay more attention and learn better, and as a result be more prepared for assignments and exams. \end{enumerate} \subsection{Optimization} \begin{itemize} \item [a.] Let a, b, and c be positive real numbers. Consider the quadratic function - $$f(\theta) = a{\theta}^2 + b\theta + c$$ - Note that $\theta$ here is a real number. What value of $\theta$ minimizes $f(\theta)$?\\[0.1in] - \underline{Solution}: - Since this is a positive parabola, we can use the formula for the vertex of a parabola, $\frac{-b}{2a}$, to find the minimum:\\ - $$ - \boxed{\theta = \frac{-b}{2a}} - $$ + $$f(\theta) = a{\theta}^2 + b\theta + c$$ + Note that $\theta$ here is a real number. What value of $\theta$ minimizes $f(\theta)$?\\[0.1in] + \underline{Solution}: + Since this is a positive parabola, we can use the formula for the vertex of a parabola, $\frac{-b}{2a}$, to find the minimum:\\ + $$ + \boxed{\theta = \frac{-b}{2a}} + $$ + + \fbox{\parbox{\textwidth}{For the following problems, I'm going to use the general approach of: + \begin{enumerate} + \item Take derivative of equation + \item Set derivative equal to 0 + \item Solve for $\theta$ + \end{enumerate}}} - \fbox{\parbox{\textwidth}{For the following problems, I'm going to use the general approach of: - \begin{enumerate} - \item Take derivative of equation - \item Set derivative equal to 0 - \item Solve for $\theta$ - \end{enumerate}}} - \item [b.] Let $x_1,\dots,x_n$ be real numbers. Consider the quadratic function - $$g(\theta) = \sum^n_{i=1}(\theta - x_i)^2.$$ - What value of $\theta$ minimizes $g(\theta)$?\\ - \underline{Solution}: + $$g(\theta) = \sum^n_{i=1}(\theta - x_i)^2.$$ + What value of $\theta$ minimizes $g(\theta)$?\\ + \underline{Solution}: + + \begin{align*} + g'(\theta) & = \sum_{i=1}^{n} 2(\theta -x_i) \\ + 0 & = \sum_{i=1}^{n} 2(\theta -x_i) \\ + \theta \sum_{i=1}^{n} 2 & = \sum_{i=1}^{n} 2x_i \\ + \Aboxed{\theta & = \frac{\sum_{i=1}^{n}x_i}{n}} + \end{align*} - \begin{align*} - g'(\theta) &= \sum_{i=1}^{n} 2(\theta -x_i)\\ - 0 &= \sum_{i=1}^{n} 2(\theta -x_i)\\ - \theta \sum_{i=1}^{n} 2 &= \sum_{i=1}^{n} 2x_i\\ - \Aboxed{\theta &= \frac{\sum_{i=1}^{n}x_i}{n}} - \end{align*} - \item [c.] Let $x_1,\dots,x_n$ again be real numbers, and let $w_1,\dots,w_n$ be positive real numbers that we can interpret as representing the importance of each of the $x_i$'s. Consider the weighted quadratic function - $$h(\theta) = \sum_{i=1}^{n}w_i(\theta - x_i)^2.$$ - What value of $\theta$ minimizes $h(\theta)$?\\ - \underline{Solution}: + $$h(\theta) = \sum_{i=1}^{n}w_i(\theta - x_i)^2.$$ + What value of $\theta$ minimizes $h(\theta)$?\\ + \underline{Solution}: - \begin{align*} - h'(\theta) &= 2 * \sum_{i=1}^{n} w_i (\theta - x_i)\\ - 0 &= 2 * \sum_{i=1}^{n} w_i (\theta - x_i)\\ - \frac{0}{2} &= \frac{\cancel{2}*\sum_{i=1}^{n} w_i (\theta - x_i)}{\cancel{2}}\\ - 0 &= \sum_{i=1}^{n} w_i (\theta - x_i)\\ - 0 &= \sum_{i=1}^{n} (w_i \theta - w_i x_i)\\ - 0 &= \sum_{i=1}^{n} w_i \theta - \sum_{i=1}^{n} w_i x_i\\ - \sum_{i=1}^{n} w_i x_i &= \sum_{i=1}^{n} w_i \theta\\ - \Aboxed{\frac{\sum_{i=1}^{n} w_i x_i}{\sum_{i=1}^{n} w_i } &= \theta} - \end{align*} + \begin{align*} + h'(\theta) & = 2 * \sum_{i=1}^{n} w_i (\theta - x_i) \\ + 0 & = 2 * \sum_{i=1}^{n} w_i (\theta - x_i) \\ + \frac{0}{2} & = \frac{\cancel{2}*\sum_{i=1}^{n} w_i (\theta - x_i)}{\cancel{2}} \\ + 0 & = \sum_{i=1}^{n} w_i (\theta - x_i) \\ + 0 & = \sum_{i=1}^{n} (w_i \theta - w_i x_i) \\ + 0 & = \sum_{i=1}^{n} w_i \theta - \sum_{i=1}^{n} w_i x_i \\ + \sum_{i=1}^{n} w_i x_i & = \sum_{i=1}^{n} w_i \theta \\ + \Aboxed{\frac{\sum_{i=1}^{n} w_i x_i}{\sum_{i=1}^{n} w_i } & = \theta} + \end{align*} \item [d.] What issue could arise in the minimization of $h$ if some of the $w_i$'s are negative?\\ - \underline{Solution}: If some of the $w_i$'s are negative, it can lead to convoluted results and a possible divergence towards $\infty$ + \underline{Solution}: If some of the $w_i$'s are negative, it can lead to convoluted results and a possible divergence towards $\infty$ \end{itemize} \subsection{Probability} \begin{itemize} \item [a.] Consider a standard 52-card deck of cards with 13 card values (Ace, King, Queen, Jack, and - 2-10) in each of the four suits (clubs, diamonds, hearts, spades). If a card is drawn at random, what - is the probability that it is a spade or a two? + 2-10) in each of the four suits (clubs, diamonds, hearts, spades). If a card is drawn at random, what + is the probability that it is a spade or a two? - \underline{Solution}:\\ - $\rightarrow$ \underline{Let} $P(S)$ be the probability of drawing a spade = $\frac{13}{52} = \frac{1}{4}$\\ - $\rightarrow$ \underline{Let} $P(T)$ be the probability of drawing a card that is a two = $\frac{4}{52} = \frac{1}{13}$\\ - - $$ - P(S \cap T) = \frac{1}{52} - $$ + \underline{Solution}:\\ + $\rightarrow$ \underline{Let} $P(S)$ be the probability of drawing a spade = $\frac{13}{52} = \frac{1}{4}$\\ + $\rightarrow$ \underline{Let} $P(T)$ be the probability of drawing a card that is a two = $\frac{4}{52} = \frac{1}{13}$\\ + + $$ + P(S \cap T) = \frac{1}{52} + $$ + + The overall probability of drawing a spade or a two is: + \begin{align*} + P(S \cup T) & = (P(S) + P(T)) - P(S \cap T) \\ + & = (\frac{1}{4} + \frac{1}{13}) - \frac{1}{52} \\ + \Aboxed{ & = \frac{4}{13} \approx 30.7\%} + \end{align*} - The overall probability of drawing a spade or a two is: - \begin{align*} - P(S \cup T) &= (P(S) + P(T)) - P(S \cap T)\\ - &= (\frac{1}{4} + \frac{1}{13}) - \frac{1}{52}\\ - \Aboxed{&= \frac{4}{13} \approx 30.7\%} - \end{align*} - \item [b.] Two factories — Factory A and Factory B — design batteries to be used in mobile phones. - Factory A produces 60\% of all batteries, and Factory B produces the other 40\%. 2\% of Factory A's - batteries have defects, and 4\% of Factory B's batteries have defects. What is the probability that a - battery is both made by Factory A and defective? + Factory A produces 60\% of all batteries, and Factory B produces the other 40\%. 2\% of Factory A's + batteries have defects, and 4\% of Factory B's batteries have defects. What is the probability that a + battery is both made by Factory A and defective? - \underline{Solution}:\\ - $\rightarrow$ \underline{Let} $P(A) = 0.6$ (probability battery is from Factory A)\\ - $\rightarrow$ \underline{Let} $P(D|A) = 0.02$ (probability battery is defective if it's from Factory A)\\ - $\rightarrow$ \underline{Let} $P(A)' = 0.4$ (probability battery is not from Factory A)\\ - $\rightarrow$ \underline{Let} $P(D|A)' = 0.04$ (probability battery is defective if it's not from Factory A) - - $\rightarrow$ \underline{Find} $P(A \cap D)$. + \underline{Solution}:\\ + $\rightarrow$ \underline{Let} $P(A) = 0.6$ (probability battery is from Factory A)\\ + $\rightarrow$ \underline{Let} $P(D|A) = 0.02$ (probability battery is defective if it's from Factory A)\\ + $\rightarrow$ \underline{Let} $P(A)' = 0.4$ (probability battery is not from Factory A)\\ + $\rightarrow$ \underline{Let} $P(D|A)' = 0.04$ (probability battery is defective if it's not from Factory A) - $$\begin{gathered}$$ - ...\\...\\...\\P(A \cap D) = 0.012 \approx 1.2\% - $$\end{gathered}$$ + $\rightarrow$ \underline{Find} $P(A \cap D)$. + + $$\begin{gathered}$$ + ...\\...\\...\\P(A \cap D) = 0.012 \approx 1.2\% + $$\end{gathered}$$ \item [c.] Consider the following (made up) facts about COVID incidence and testing: - \begin{itemize} - \item In the absence of any special information, the probability that a person has COVID is 1\%. - \item If a person has COVID, the probability that a test will correctly read positive is 80\%. - \item If a person does not have COVID, the probability that a test will incorrectly produce a false positive is 10\%. - \end{itemize} + \begin{itemize} + \item In the absence of any special information, the probability that a person has COVID is 1\%. + \item If a person has COVID, the probability that a test will correctly read positive is 80\%. + \item If a person does not have COVID, the probability that a test will incorrectly produce a false positive is 10\%. + \end{itemize} - Suppose you take a COVID test and it reads positive. Given the facts above, what is the probability -that you have COVID? + Suppose you take a COVID test and it reads positive. Given the facts above, what is the probability + that you have COVID? - \underline{Solution}:\\ - $\rightarrow$ \underline{Let} $C$ = COVID\\ - $\rightarrow$ \underline{Let} $T$ = Positive Test\\[0.5cm] - $\rightarrow$ \underline{Let} $P(C) = 0.01$ (probability of having COVID)\\ - $\rightarrow$ \underline{Let} $P(T|C)$ = $0.8$ (probability of a positive test if a person has COVID)\\ - $\rightarrow$ \underline{Let} $P(T|C)' = 0.1$ (probability of a false positive)\\ - $\rightarrow$ \underline{Find} $P(C|T)$ (probability of having COVID with a positive test result) + \underline{Solution}:\\ + $\rightarrow$ \underline{Let} $C$ = COVID\\ + $\rightarrow$ \underline{Let} $T$ = Positive Test\\[0.5cm] + $\rightarrow$ \underline{Let} $P(C) = 0.01$ (probability of having COVID)\\ + $\rightarrow$ \underline{Let} $P(T|C)$ = $0.8$ (probability of a positive test if a person has COVID)\\ + $\rightarrow$ \underline{Let} $P(T|C)' = 0.1$ (probability of a false positive)\\ + $\rightarrow$ \underline{Find} $P(C|T)$ (probability of having COVID with a positive test result) - \begin{align*} - P(T) &= P(T|C) * (P(C) + (P(T|C)' * P(C)'))\\ - P(T) &= 0.8 * (0.01 + (0.1 * 0.99))\\ - P(T) &= 0.0872 - \end{align*} + \begin{align*} + P(T) & = P(T|C) * (P(C) + (P(T|C)' * P(C)')) \\ + P(T) & = 0.8 * (0.01 + (0.1 * 0.99)) \\ + P(T) & = 0.0872 + \end{align*} - \rule{4.4in}{0.5pt} + \rule{4.4in}{0.5pt} - \begin{align*} - P(C|T) &= \frac{(0.8*0.01)}{0.0872}\\ - &= \frac{0.008}{0.0872}\\ - \Aboxed{&= \frac{10}{109}\approx 9.17\%} - \end{align*} + \begin{align*} + P(C|T) & = \frac{(0.8*0.01)}{0.0872} \\ + & = \frac{0.008}{0.0872} \\ + \Aboxed{ & = \frac{10}{109}\approx 9.17\%} + \end{align*} \item [d. ] Suppose you repeatedly roll a fair six-sided die until you roll a 1 (and then you stop). Every - time you roll a 3, you win a points, and every time you roll a 6, you lose b points. You do not win or - lose any points if you roll a 2, 4, or 5. What is the expected number of points (as a function of a and - b) you will have when you stop? + time you roll a 3, you win a points, and every time you roll a 6, you lose b points. You do not win or + lose any points if you roll a 2, 4, or 5. What is the expected number of points (as a function of a and + b) you will have when you stop? \end{itemize} \subsection{Counting} \begin{itemize} \item [a.] $n \times n$ grid, rectangle, Big O?\\ - \underline{Solution}: $O(n^2)$ + \underline{Solution}: $O(n^2)$ \item [b.] Three rectangles, possible ways?\\ - \underline{Solution}: $n^2 * n^2 * n^2 = n^6$ + \underline{Solution}: $n^2 * n^2 * n^2 = n^6$ \end{itemize} \subsection{Programming in Python} @@ -198,127 +198,275 @@ See attached .py file \subsection{Grid City} - Modeled as a search problem: - \begin{itemize} - \item $s_0 = (0,0)$ - \item Actions$(s) = \lbrace{(+1,0), (-1,0), (0,+1), (0,-1)}\rbrace$ - \item Succ$(s,a) = s+a$ - \item Cost$((x,y), a) = 1+$ max$(x,0)$ (it is more expensive as you go further to the right) - \item IsEnd$(s) = \begin{cases} - \text{True} &\text{if\ } s = (m,n)\\ - \text{False} &\text{otherwise} - \end{cases}$ - \end{itemize} +Modeled as a search problem: +\begin{itemize} + \item $s_0 = (0,0)$ + \item Actions$(s) = \lbrace{(+1,0), (-1,0), (0,+1), (0,-1)}\rbrace$ + \item Succ$(s,a) = s+a$ + \item Cost$((x,y), a) = 1+$ max$(x,0)$ (it is more expensive as you go further to the right) + \item IsEnd$(s) = \begin{cases} + \text{True} & \text{if\ } s = (m,n) \\ + \text{False} & \text{otherwise} + \end{cases}$ +\end{itemize} \begin{itemize} - \item[a.] Minimum cost of reaching location $(m,n)$ starting from $(0,0)$? Describe possible path achieving the minimum cost. Is it unique? - - \underline{Solution}: Since $(m,n) \geq 0$, we can safely say that $(m,n)$ can't be at $(0,0)$ in the minimum cost path. The next closest location to $(0,0)$ that $(m,n)$ - could be is $(1,1)$, as it satisfies the condition listed earlier. With this in mind, the minimum cost would be: - \begin{align*} - \text{Cost}((x,y),a) &= 1 + \text{max}(x,0) \\ - &= (1 + \text{max}(m,0)) + (1 + \text{max}(n,0)) \\ - &= 1 + m + 1 + n \\ - \Aboxed{&= 2 + m + n} - \end{align*} - + \item[a.] Minimum cost of reaching location $(m,n)$ starting from $(0,0)$? Describe possible path achieving the minimum cost. Is it unique? + + \underline{Solution}: Since $(m,n) \geq 0$, we can safely say that $(m,n)$ can't be at $(0,0)$ in the minimum cost path. The next closest location to $(0,0)$ that $(m,n)$ + could be is $(1,1)$, as it satisfies the condition listed earlier. With this in mind, the minimum cost would be: + \begin{align*} + \text{Cost}((x,y),a) & = 1 + \text{max}(x,0) \\ + & = (1 + \text{max}(m,0)) + (1 + \text{max}(n,0)) \\ + & = 1 + m + 1 + n \\ + \Aboxed{ & = 2 + m + n} + \end{align*} + \item[b.] True or false (and explain): UCS will never terminate on this problem because the number of states is infinite. - - \underline{Solution}: False, because UCS explores nodes with the lowest cost first. If $(m,n)$ is available at the minimum cost from $(0,0)$, then UCS will terminate, and that too fairly quickly. + + \underline{Solution}: False, because UCS explores nodes with the lowest cost first. If $(m,n)$ is available at the minimum cost from $(0,0)$, then UCS will terminate, and that too fairly quickly. \item[c.] True or false (and explain): UCS will return the minimum cost path and explore only locations between $(0, 0)$ and $(m, n)$; that is, locations $(x, y)$ such that $0 \leq x \leq m$ and $0 \leq y \leq n$. - - \underline{Solution}: True, if $(m,n)$ is available at the minimum cost from $(0,0)$, because UCS will terminate very quickly and therefore won't explore other paths. This is because UCS prioritizes the lowest cost first. + + \underline{Solution}: True, if $(m,n)$ is available at the minimum cost from $(0,0)$, because UCS will terminate very quickly and therefore won't explore other paths. This is because UCS prioritizes the lowest cost first. \item[d.] True or false (and explain): UCS will return the minimum cost path and explore only - locations whose path costs are strictly less than the minimum cost from $(0, 0)$ to $(m, n)$. + locations whose path costs are strictly less than the minimum cost from $(0, 0)$ to $(m, n)$. + + \underline{Solution}: True, because UCS focuses on cumulative costs that are less than the optimal cost to reach $(m,n)$. - \underline{Solution}: True, because UCS focuses on cumulative costs that are less than the optimal cost to reach $(m,n)$. - \end{itemize} Now consider UCS running on an arbitrary graph. \begin{itemize} \item[e.] True or false (and explain): If you add an edge between two nodes, the cost of the min-cost path cannot go up. - - \underline{Solution}: True, because adding an edge can only maintain or reduce the cost of the min-cost path. + + \underline{Solution}: True, because adding an edge can only maintain or reduce the cost of the min-cost path. \item[f.] True or false (and explain): If you make the cost of an action from some state small enough (possibly negative), you can guarantee that that action will show up in the minimum cost path. - - \underline{Solution}: False, because that action isn't guaranteed to lead us to the goal, no matter how cheap we make it. + + \underline{Solution}: False, because that action isn't guaranteed to lead us to the goal, no matter how cheap we make it. \item[g.] True or false (and explain): If you increase the cost of every action by 1, the minimum cost path does not change (even though its cost does). - - \underline{Solution}: True. Since we are raising the cost of \underline{all} costs equally, the minimum cost path will remain the the same even despite the costs going up. -\subsection{Six Degrees} - See degrees.py + \underline{Solution}: True. Since we are raising the cost of \underline{all} costs equally, the minimum cost path will remain the the same even despite the costs going up. -\subsection{Tic Tac Toe} - See tictactoe.py + \subsection{Six Degrees} + See degrees.py + + \subsection{Tic Tac Toe} + See tictactoe.py \end{itemize} \section{Homework 2} - \subsection{Cake} +\subsection{Cake} - Alice and Bob are sharing a cake.... +Alice and Bob are sharing a cake.... +\begin{center} + \includegraphics[scale=0.3]{hw2/gameTree.png} +\end{center} + + +Write down the utility Alice should expect to receive in each of the game states given the following knowlege of player strategies: + +\begin{itemize} + \item [a.] Alice and Bob playing adversarially, each trying to maximize the cake they receive. + \begin{enumerate} + \item 1/2 + \item 1/2 + \item 1/2 + \end{enumerate} + \item [b. ] Alice still trying to maximize, Bob now playing collaboratively and helping Alice. + \begin{enumerate} + \item 2/3 + \item 1/2 + \item 2/3 + \end{enumerate} + \item [c. ] Random play. + \begin{enumerate} + \item Node 2 or 3 + \item Doesn't matter because of constant utility + \item Node 3 + \end{enumerate} + \item [d. ] Alpha Beta pruning + \begin{enumerate} + \item Leaf notes with utility 1/3, 1/3, 1/3, 1/3 on left, and right leaf node with utility 1/6 + \end{enumerate} + \item [e. ] Alpha Beta pruning with tree elements swapped. + \begin{enumerate} + \item No nodes are pruned + \end{enumerate} +\end{itemize} + +\begin{itemize} + \item [2. ] Logical Formulas + \begin{itemize} + \item [a. ] $(P \land \neg Q) \lor (\neg P \land Q)$ + \item [b. ] $(\exists x \text{Course}(x) \land \text{Enrolled}(A, x) \land \text{Enrolled}(B,x))$ + \item [c. ] $(\forall x \text{Course}(x) \land \text{Enrolled}(A, x) \rightarrow \text{Enrolled}(B,x))$ + \item [d. ] $\forall x \forall y (\text{Enrolled}(x,y) \land \text{Course}(y) \rightarrow \text{Student}(x))$ + \item [e. ] $\forall x (\text{Course}(x) \rightarrow \exists y (\text{Student}(y) \land \text{Enrolled}(y,x)))$ + \end{itemize} +\end{itemize} + +\subsection{Knights and Knaves} +See attached puzzle.py file + +\subsection{Odds and Evens} +See attached submission.py file + +\section{Homework 3} +\subsection{Probability} + +\begin{itemize} + \item[a.] An urn contains w white balls and b black balls... + \begin{enumerate} + \item What is the probability that the first ball is white? + $$ + P(\text{first ball is white}) = \boxed{\frac{w}{w + b}} + $$ + + \item What is the probability that the second ball is white? + $$ + \begin{aligned} + \text{If first ball was white}: \\ + P(\text{second ball white | first ball white}) = \Aboxed{\frac{w-1}{w + b - 1}} \\ + \text{If first ball was black}: \\ + P(\text{second ball white | first ball black}) = \Aboxed{\frac{w}{w + b + 1}} \\ + \end{aligned} + $$ + \end{enumerate} + \item[b.] Alice has two brothers, Bob and Charlie... + \begin{enumerate} + \item What is the probability that Alice is older than Charlie? \\[0.1in] + There are $3! = 6$ possible scenarios, and Alice is older in 3 of them. So, $\boxed{\frac{3}{6} \ \text{or} \ 0.5}$. + \item Alice tells you that she is older than Bob \\[0.1in] + In this case, the probability of Alice being older than Charlie is $\boxed{\frac{2}{3} \ \text{or} \ 0.66}$. + \end{enumerate} + \item[c.] The inhabitants of a small island... \\[0.1in] + Let: + \begin{itemize} + \item T = first person tells truth + \item L = first person lies + \item Y = second person says yes + \end{itemize} + + We know that: + \begin{itemize} + \item P(T) = 1/3 + \item P(L) = 2/3 + \item P(Y | T) = 1/3 + \item P(Y | L) = 2/3 + \end{itemize} + + We find: $P(T | Y) = \frac{P(Y | T) \cdot P(T)}{P(Y)}$ + + $$ + \begin{aligned} + P(Y) & = P(Y | T) \cdot P(T) + P(Y | L) \cdot P(L) \\ + & = \frac{1}{3} \cdot \frac{1}{3} + \frac{2}{3} \cdot \frac{2}{3} \\ + & = \frac{1}{9} + \frac{4}{5} \\ + & = \frac{5}{9} + \end{aligned} + $$ + + $$ + \begin{aligned} + P(T | Y) & = \frac{\frac{1}{3} \cdot \frac{1}{3}}{\frac{5}{9}} \\ + & = \Aboxed{\frac{1}{5} \ \text{or} \ 0.2} + \end{aligned} + $$ + + \item[d.] Bag contains one ball... \\[0.1in] + Let: + \begin{itemize} + \item W = original ball was white + \item B = original ball was black + \item D = white ball is drawn + \end{itemize} + + We know: + \begin{itemize} + \item P(W) = 1/2 + \item P(B) = 1/2 + \item P(D | W) = 1 + \item P(D | B) = 1/2 + \end{itemize} + + We find: $P(W | D) = \frac{P(D | W) \cdot P(W)}{P(D)}$ + + $$ + \begin{aligned} + P(D) & = P(D | W) \cdot P(W) + P(D | B) \cdot P(B) \\ + & = 1 \cdot \frac{1}{2} + \frac{1}{2} \cdot \frac{1}{2} \\ + & = \frac{1}{2} + \frac{1}{4} \\ + & = \frac{3}{4} + \end{aligned} + $$ + + $$ + \begin{aligned} + P(W | D) & = \frac{1 \cdot \frac{1}{2}}{\frac{3}{4}} \\ + \Aboxed{ & = \frac{2}{3}} + \end{aligned} + $$ +\end{itemize} + +\subsection{Bayesian Networks} +\begin{itemize} + \item[a.] Given the following Bayesian network, write down a factorization of the joint probability distribution P(A,B, C,D, E) as a product of local conditional distributions, one for each random variable. \begin{center} - \includegraphics[scale=0.3]{hw2/gameTree.png} + \includegraphics[scale=0.3]{hw3/bayesian_network_1_hw3.png} \end{center} - - Write down the utility Alice should expect to receive in each of the game states given the following knowlege of player strategies: + $$ + P(A, B, C, D, E) = \ \boxed{P(A) \cdot P(B) \cdot P(C | A, B) \cdot P(D | B) \cdot P(E | C)} + $$ - \begin{itemize} - \item [a.] Alice and Bob playing adversarially, each trying to maximize the cake they receive. - \begin{enumerate} - \item 1/2 - \item 1/2 - \item 1/2 - \end{enumerate} - \item [b. ] Alice still trying to maximize, Bob now playing collaboratively and helping Alice. - \begin{enumerate} - \item 2/3 - \item 1/2 - \item 2/3 - \end{enumerate} - \item [c. ] Random play. - \begin{enumerate} - \item Node 2 or 3 - \item Doesn't matter because of constant utility - \item Node 3 - \end{enumerate} - \item [d. ] Alpha Beta pruning - \begin{enumerate} - \item Leaf notes with utility 1/3, 1/3, 1/3, 1/3 on left, and right leaf node with utility 1/6 - \end{enumerate} - \item [e. ] Alpha Beta pruning with tree elements swapped. - \begin{enumerate} - \item No nodes are pruned - \end{enumerate} - \end{itemize} - - \begin{itemize} - \item [2. ] Logical Formulas + \item[b.] For the same Bayesian network above, list all the random variables that are independent of A, assuming that none of the variables have been observed. \begin{itemize} - \item [a. ] $(P \land \neg Q) \lor (\neg P \land Q)$ - \item [b. ] $(\exists x \text{Course}(x) \land \text{Enrolled}(A, x) \land \text{Enrolled}(B,x))$ - \item [c. ] $(\forall x \text{Course}(x) \land \text{Enrolled}(A, x) \rightarrow \text{Enrolled}(B,x))$ - \item [d. ] $\forall x \forall y (\text{Enrolled}(x,y) \land \text{Course}(y) \rightarrow \text{Student}(x))$ - \item [e. ] $\forall x (\text{Course}(x) \rightarrow \exists y (\text{Student}(y) \land \text{Enrolled}(y,x)))$ + \item B \end{itemize} - \end{itemize} - \subsection{Knights and Knaves} - See attached puzzle.py file + \item[c.] For the same Bayesian network above, assume that E is now observed. List all pairs of random variables (not including E) that are conditionally independent given E. + \begin{itemize} + \item (A, B) + \item (A, D) + \item (B, D) + \end{itemize} - \subsection{Odds and Evens} - See attached submission.py file - + \item[d.] Consider the following model for traffic jams in a small town, which can be caused by either a car accident or by a visit from the president (and the accompanying security motorcade). + \begin{center} + \includegraphics[scale=0.3]{hw3/motorcade.png} + \end{center} + + Compute P(Accident = 1 | Traffic = 1) and P(Accident = 1 | Traffic = 1, President = 1). \\ + + Let: + \begin{itemize} + \item A = event of accident + \item P = event of president's visit + \item T = event of traffic jam + \end{itemize} + + We're given: + \begin{itemize} + \item P(A) = prior probability of an accident + \item P(P) = prior probability of the president's visit + \item P(T | A) = probability of a traffic jam given an accident + \item P(T | P) = probability of a traffic jam given the president's visit + \end{itemize} + + $$ + P(\text{Accident} = 1 | \text{Traffic} = 1) \rightarrow P(A | T) = \frac{P(T | A) \cdot P(A)}{P(T)} + $$ + + +\end{itemize} \end{document} \ No newline at end of file