\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

\newpage

\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]{hw3/bayesian_network_1_hw3.png}
          \end{center}

          $$
              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)}
          $$

    \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 B
          \end{itemize}

    \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}

    \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}

\newpage

\section{Homework 4}

\subsection{Inference in Bayesian Networks}

\begin{itemize}
    \item[a - c. ] Refer to bn.py

    \item[d. ] Likelihood weighting gets more accurate as we use more samples. With more samples, the estimated probabilities get closer to the real chances we calculated before. This shows that using a bigger sample size is helpful for getting better results.
\end{itemize}

\subsection{Gradient Descent}

\begin{itemize}
    \item[a. ]
          % \begin{center}
          %     \includegraphics[scale=0.5]{hw4/Gemini_Chart_Image_5v3caz5v3caz5v3c.png}
          % \end{center}


          Hinge loss is a reasonable cost function for binary classification because it focuses on creating a good margin between classes. It only penalizes models when their predictions fall close to the wrong class, and ignores classified points even with a small margin. This makes the model prioritize a clear separation between positive and negative classes.

    \item[b. ]

          $$
              \frac{\partial c(h(x), y)}{\partial w_0} = 0
          $$

          $$
              \frac{\partial c(h(x), y)}{\partial w_1} = \begin{cases}
                  -y, & \text{if } w_0 + w_1x < 1    \\
                  0,  & \text{if } w_0 + w_1x \geq 1
              \end{cases}
          $$

    \item[c. ]

          \begin{center}
              \includegraphics[scale=0.3]{hw4/Picture1.png}
          \end{center}


\end{itemize}

\newpage

\section{Homework 5}

\subsection{Regularization}

\begin{itemize}
    \item[a.]
          $$
              \begin{aligned}
                  \frac{d}{dw_0} C(w) & = \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-1) = \boxed{-2\sum_{i=1}^{n} (y_i - (w_0 + w_1x_i))}      \\
                  \frac{d}{dw_1} C(w) & = \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-x_i) = \boxed{-2\sum_{i=1}^{n} x_i(y_i - (w_0 + w_1x_i))}
              \end{aligned}
          $$

    \item[b. ] 
          $$
              \begin{aligned}
                \frac{d}{dw_0} \tilde{C}(w) &= \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-1) + 2\lambda w_0 \\
                &= 2\sum_{i=1}^{n} (w_0 + w_1x_i - y_i) + 2\lambda w_0 ] \\ 
                &= \boxed{2\sum_{i=1}^{n} (w_0 + w_1x_i - y_i + \lambda w_0)}
              \end{aligned}
          $$

          and 

          $$
              \begin{aligned}
                \frac{d}{dw_1} \tilde{C}(w) &= \sum_{i=1}^{n} 2(y_i - (w_0 + w_1x_i))(-x_i) + 2\lambda w_1 \\
                &= \boxed{2\sum_{i=1}^{n} x_i(y_i - (w_0 + w_1x_i)) + 2\lambda w_1}
              \end{aligned}
          $$

    \item[c-d.] Refer to regularization.py
    \item[e. ] Avg loss, regularization: 1.715104320688656 \\
    Avg loss, no regularization: 0.4384015750703455 

\end{itemize}

\end{document}
