Skip to content

Commit

Permalink
Lecture 2 Uncertainty
Browse files Browse the repository at this point in the history
  • Loading branch information
doongz committed Sep 29, 2022
1 parent a12dc3d commit f837c99
Show file tree
Hide file tree
Showing 55 changed files with 1,658 additions and 0 deletions.
550 changes: 550 additions & 0 deletions Lecture-2-Uncertainty/Lecture-2.md

Large diffs are not rendered by default.

189 changes: 189 additions & 0 deletions Lecture-2-Uncertainty/Project-2-Heredity.md

Large diffs are not rendered by default.

185 changes: 185 additions & 0 deletions Lecture-2-Uncertainty/Project-2-PageRank.md

Large diffs are not rendered by default.

Binary file added Lecture-2-Uncertainty/Slides-2.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions Lecture-2-Uncertainty/heredity/data/family0.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name,mother,father,trait
Harry,Lily,James,
James,,,1
Lily,,,0
7 changes: 7 additions & 0 deletions Lecture-2-Uncertainty/heredity/data/family1.csv
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 6 additions & 0 deletions Lecture-2-Uncertainty/heredity/data/family2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name,mother,father,trait
Arthur,,,0
Hermione,,,0
Molly,,,
Ron,Molly,Arthur,0
Rose,Ron,Hermione,1
164 changes: 164 additions & 0 deletions Lecture-2-Uncertainty/heredity/heredity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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.
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.
"""
raise NotImplementedError


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.
"""
raise NotImplementedError


def normalize(probabilities):
"""
Update `probabilities` such that each probability distribution
is normalized (i.e., sums to 1, with relative proportions the same).
"""
raise NotImplementedError


if __name__ == "__main__":
main()
14 changes: 14 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus0/1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>1</title>
</head>
<body>
<h1>1</h1>

<div>Links:</div>
<ul>
<li><a href="2.html">2</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus0/2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>2</title>
</head>
<body>
<h1>2</h1>

<div>Links:</div>
<ul>
<li><a href="1.html">1</a></li>
<li><a href="3.html">3</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus0/3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>3</title>
</head>
<body>
<h1>3</h1>

<div>Links:</div>
<ul>
<li><a href="2.html">2</a></li>
<li><a href="4.html">4</a></li>
</ul>
</body>
</html>
14 changes: 14 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus0/4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>4</title>
</head>
<body>
<h1>4</h1>

<div>Links:</div>
<ul>
<li><a href="2.html">2</a></li>
</ul>
</body>
</html>
14 changes: 14 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/bfs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>BFS</title>
</head>
<body>
<h1>BFS</h1>

<div>Links:</div>
<ul>
<li><a href="search.html">Search</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/dfs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>DFS</title>
</head>
<body>
<h1>DFS</h1>

<div>Links:</div>
<ul>
<li><a href="bfs.html">BFS</a></li>
<li><a href="search.html">Search</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/games.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Games</title>
</head>
<body>
<h1>Games</h1>

<div>Links:</div>
<ul>
<li><a href="tictactoe.html">TicTacToe</a></li>
<li><a href="minesweeper.html">Minesweeper</a></li>
</ul>
</body>
</html>
14 changes: 14 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/minesweeper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Minesweeper</title>
</head>
<body>
<h1>Minesweeper</h1>

<div>Links:</div>
<ul>
<li><a href="games.html">Games</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/minimax.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Minimax</title>
</head>
<body>
<h1>Minimax</h1>

<div>Links:</div>
<ul>
<li><a href="search.html">Search</a></li>
<li><a href="games.html">Games</a></li>
</ul>
</body>
</html>
16 changes: 16 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Search</title>
</head>
<body>
<h1>Search</h1>

<div>Links:</div>
<ul>
<li><a href="dfs.html">DFS</a></li>
<li><a href="bfs.html">BFS</a></li>
<li><a href="minimax.html">Minimax</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus1/tictactoe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>TicTacToe</title>
</head>
<body>
<h1>TicTacToe</h1>

<div>Links:</div>
<ul>
<li><a href="games.html">Games</a></li>
<li><a href="minimax.html">Minimax</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus2/ai.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>AI</title>
</head>
<body>
<h1>AI</h1>

<div>Links:</div>
<ul>
<li><a href="inference.html">Inference</a></li>
<li><a href="algorithms.html">Algorithms</a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions Lecture-2-Uncertainty/pagerank/corpus2/algorithms.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Algorithms</title>
</head>
<body>
<h1>Algorithms</h1>

<div>Links:</div>
<ul>
<li><a href="programming.html">Programming</a></li>
<li><a href="recursion.html">Recursion</a></li>
</ul>
</body>
</html>
Loading

0 comments on commit f837c99

Please sign in to comment.