324 lines
15 KiB
TeX
324 lines
15 KiB
TeX
\documentclass[titlepage]{article}
|
|
\usepackage{amsmath}
|
|
\usepackage{mathtools}
|
|
\usepackage{cancel}
|
|
\usepackage{amssymb}
|
|
|
|
|
|
\setcounter{section}{-1}
|
|
|
|
\begin{document}
|
|
|
|
\include{titlePage}
|
|
|
|
\section*{Introduction}
|
|
This document contains the work I, Uzair Hamed Mohammed, have done as required by this course. This is my first time using \LaTeX \ to type homework, so strange formatting may occur.\\[0.1in]
|
|
I once read in a math and Linux oriented blog that handwriting your math homework and notes, and then typing it up in \LaTeX \ greatly boosts mastery and memorization of concepts. My plan is to do exactly that, and append homework assignments to this document as they are completed. At the end of the semester, the result should be a nice and comprehensive compilation of assignments.\\[0.1in]
|
|
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!
|
|
|
|
\newpage
|
|
|
|
\section{Homework 0}
|
|
\textit{By turning in this assignment, I agree to abide by SFSU's academic integrity code and declare that all of my solutions are my own work: }
|
|
|
|
\subsection{About You}
|
|
\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.
|
|
\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}}
|
|
$$
|
|
|
|
\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}:
|
|
|
|
\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}:
|
|
|
|
\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$
|
|
\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?
|
|
|
|
\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*}
|
|
|
|
|
|
\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?
|
|
|
|
\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)$.
|
|
|
|
$$\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}
|
|
|
|
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)
|
|
|
|
\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}
|
|
|
|
\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?
|
|
\end{itemize}
|
|
|
|
\subsection{Counting}
|
|
|
|
\begin{itemize}
|
|
\item [a.] $n \times n$ grid, rectangle, Big O?\\
|
|
\underline{Solution}: $O(n^2)$
|
|
|
|
\item [b.] Three rectangles, possible ways?\\
|
|
\underline{Solution}: $n^2 * n^2 * n^2 = n^6$
|
|
\end{itemize}
|
|
|
|
\subsection{Programming in Python}
|
|
See attached .py file
|
|
|
|
\newpage
|
|
|
|
\section{Homework 1}
|
|
|
|
\textit{By turning in this assignment, I agree to abide by SFSU's academic integrity code and declare that all of my solutions are my own work: }
|
|
|
|
\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}
|
|
|
|
\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[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.
|
|
|
|
\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.
|
|
|
|
|
|
\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)$.
|
|
|
|
\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.
|
|
|
|
\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.
|
|
|
|
\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
|
|
|
|
\subsection{Tic Tac Toe}
|
|
See tictactoe.py
|
|
|
|
\end{itemize}
|
|
|
|
\section{Homework 2}
|
|
|
|
\subsection{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
|
|
|
|
|
|
\end{document} |