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 @@
+
+
+
+
\ 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)