\chapter{}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{i.i.d.}{\sim} f(x \mid \eta)\), where \(\{f(x \mid \eta)\}_{\eta \in \mathcal{E}}\) is a full rank, \(k\)-parameter exponential family. Show that
    \begin{enumerate}[(a)]
        \item \(\{f(\mathbf{x} \mid \eta)\}_{\eta \in \mathcal{E}}\) is a full rank, \(k\)-parameter exponential family;
        \item \(\sum_{i=1}^{n} T\left(X_{i}\right)\) is minimal sufficient for \(\eta\).
        \item Conclude that, under i.i.d. measurements, the dimension of the minimal sufficient statistic is independent of the sample size \(n\). In other words, canonical exponential families display a strong dimension reduction property.
    \end{enumerate}
    \rmk{1} One question of interest is: what other families of distributions permit dimension reduction in the sense described in part (c) of this problem? The Darmois-Koopman-Pitman theorem, stated below without proof, answers this question. Under certain regularity assumptions, only exponential families have this property!

    \thm{1} (Darmois-Koopman-Pitman theorem; Theorem 1.6.18 in Lehmann and Casella (1998), p.40). Suppose that the following assumptions hold.
    \begin{enumerate}[(a)]
        \item \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} f\left(x_{1} \mid \theta\right)\), where \(f\left(x_{1} \mid \theta\right)\) is a.c.;
        \item \(\operatorname{supp}_{\theta} f\left(x_{1} \mid \theta\right)\) is an interval;
        \item for \(f(\boldsymbol{x} \mid \theta)=\prod_{i=1}^{n} f\left(x_{i} \mid \theta\right)\), there exists a continuous \(k\)-dimensional sufficient statistic, where \(k<n\).
    \end{enumerate}
    Then,
    \begin{enumerate}[(i)]
        \item if \(k=1\), then there are functions \(\eta_{1}(\cdot), B(\cdot), h(\cdot)\) such that
        \[
        f(\boldsymbol{x} \mid \theta)=h(\boldsymbol{x}) \exp \left\{\eta_{1}(\theta) T_{1}(\boldsymbol{x})-B(\theta)\right\}.
        \]
        \item if \(k \geq 2\) and \(f\left(x_{1} \mid \theta\right)\) has continuous partial derivatives with respect to \(x_{1}\), then there exist functions \(\eta_{i}(\cdot), B(\cdot), h(\cdot)\) such that
        \[
        f(\boldsymbol{x} \mid \theta)=h(\boldsymbol{x}) \exp \left\{\sum_{i=1}^{s} \eta_{i}(\theta) T_{i}(\boldsymbol{x})-B(\theta)\right\}, \quad s \leq k .
        \]
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}[(a)]
        \item Knowing that
        \[
            f(x_i|\eta)=h(x_i)\exp\left(\eta^T T(x_i)-A(\eta)\right).
        \]
        And the space for $\eta$ is $k$-dimension. So, \[
            f(x|\eta)=\prod_{i=1}^n h(x_i)\cdot\exp\left(\eta^T\sum_{i=1}^nT(x_i)-nA(\eta)\right).
        \]
        Its parameter space is also $k$-dimension. So, $f(x|\eta)$ is full rank.
        \item For $x\neq y$,
        \[
            \frac{f(x|\eta)}{f(y|\eta)}=\exp\left(\eta^T\left(\sum_{i=1}^nT(x_i)-\sum_{i=1}^nT(y_i)\right)\right).
        \]
        So, $\sum_{i=1}^nT(x_i)$ is minimal sufficient.
        \item Because the parameter space is $k$-dimension, the dimension for minimal sufficient statistic $\sum_{i=1}^nx_i$ is at most $k$-dimension, which is independent with $n$.
    \end{enumerate}
\end{solution}

\begin{ex}
    Let \(Z_{1}, \ldots, Z_{n} \stackrel{i.i.d. }{\sim} F(z)\), where \(F(z)\) is a c.d.f. that is continuous at zero. Let \(X_{i}=\sigma Z_{i}\), \(i=1, \ldots, n\). Show that \(\left(X_{1} / X_{n}, \ldots, X_{n-1} / X_{n}\right)^{T}\) is ancillary for \(\sigma\).
\end{ex}

\begin{solution}
    \[
        \frac{X_i}{X_n}=\frac{Z_i}{Z_n}
    \]
    it doesn't depend on $\sigma$. So, \(\left(X_{1} / X_{n}, \ldots, X_{n-1} / X_{n}\right)^{T}\) is ancillary for \(\sigma\).
\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} U[\theta, \theta+1]\), \(\theta \in \mathbb{R}\) (i.e., the location family generated by a \(U[0,1]\) distribution).
    \begin{enumerate}
        \item Show that the vector and univariate statistics
        \[
        T(\mathbf{X})=\left(X_{(n)}-X_{(1)}, \frac{X_{(1)}+X_{(n)}}{2}\right)^{T}, \quad V(\mathbf{X})=X_{(n)}-X_{(1)}
        \]
        are, respectively, minimal sufficient and ancillary for \(\theta\).
        \item Conclude that \(T(\mathbf{X})\) and \(V(\mathbf{X})\) are dependent (a paradoxical conclusion!).
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}
        \item $p(x_i|\theta)=\mathbf{1}_{\theta\leqslant x_i\leqslant \theta+1}$,
        \[
            p(x)=\prod_{i=1}^n\mathbf{1}_{\theta\leqslant x_i\leqslant \theta+1}=\mathbf{1}_{\theta\leqslant x_{(1)}, x_{(n)}\leqslant \theta+1}.
        \]

        Let $R=X_{(n)}-X_{(1)}$, $M=\frac{X_{(1)}+X_{(n)}}{2}$. Then $X_{(1)}=\frac{2M-R}{2}$, $X_{(n)}=\frac{2M+R}{2}$. The joint density function for $X_{(1)}$ and $X_{(n)}$ is
        \[
            g(x_{(1)}, x_{(n)})=n(n-1)(x_{(n)}-x_{(1)})^{n-2}.
        \]
        Replace $X_{(1)}, X_{(n)}$ with $M$ and $R$. We can get
        \[
            g(M,R)=n(n-1)(R)^{n-2}.
        \]
        So, \[
            g(M)=n, \qquad g(R)=n(n-1)R^{n-2}(1-R).
        \]
        Both of them are independent with $\theta$. So, they are ancillary.

        \item Obviously, $g(M)\cdot g(R)\neq g(M,R)$.
    \end{enumerate}

\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} \mathcal{N}\left(\sigma, \sigma^{2}\right), \sigma>0, n \geq 2\). As seen in class,
\[
T(\mathbf{X})=\left(\sum_{i=1}^{n} X_{i}^{2}, \sum_{i=1}^{n} X_{i}\right)^{T}
\]
is minimal sufficient. We now show that \(T(\mathbf{X})\) is not complete.
    \begin{enumerate}[(a)]
        \item Let \(\delta_{1}(\mathbf{X})=\bar{X}\). Show that \(\mathbb{E}\left(\delta_{1}(\mathbf{X}) \mid \sigma\right)=\sigma\).
        \item Let
        \[
        \delta_{2}(\mathbf{X})=\sqrt{\frac{n-1}{2}} \frac{\Gamma((n-1) / 2)}{\Gamma(n / 2)} \sqrt{S^{2}}
        \]
        Show that \(\mathbb{E}\left(\delta_{2}(\mathbf{X}) \mid \sigma\right)=\sigma\).
        \item Set \(g\left(t_{1}, t_{2}\right)=t_{1}-t_{2}\) and conclude that \(T(\mathbf{X})\) is not complete.
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}[(a)]
        \item From the Large Number Theorem, we know that $\bar{X}\sim N(\sigma, \sigma^2/n)$. So,
        \[
            \mathbf{E}(\bar{X})=\sigma.
        \]
        \item \[S^2=\frac{1}{n-1}\sum_{i=1}^n(X_i-\bar{X})^2\]
        \[
            \frac{(n-1)S^2}{\sigma^2}=\sum_{i=1}^n(\frac{X-\bar{X}}{\sigma})^2\sim\chi_n^2.
        \]
        So, $\mathbf{E}(S)=\sigma\sqrt{\frac{2}{n-1}}\cdot \frac{\Gamma(n/2)}{\Gamma((n-1)/2)}$. Then, we have
        \[
            \mathbf{E}(\delta_2(X)|\sigma)=\sigma.
        \]
        \item $\mathbf{E}(\delta_1(X)-\delta_2(X))=0$, but $\Pr(\delta_1(X)-\delta_2(X))=\Pr(\bar{X}-\delta_2(X))\neq 1$.
    \end{enumerate}
\end{solution}

\begin{ex}
    Let \(Y_{1}, \ldots, Y_{n}\) be independent random variables, each with distribution \(Y_{i} \sim B\left(n_{i}, p_{i}\right)\), \(i=1, \ldots, n\). To fix ideas, make the additional assumptions
    \begin{itemize}
        \item \(n=2, n_{1}=2, n_{2}=1 ;\)
        \item \(p_{i}=p_{i}\left(x_{i}\right)=1-e^{-\theta x_{i}} \in[0,1), \theta>0, x_{i} \geq 0, i=1,2\) (example: \(x_{i}\) represents the chosen level of a medication, \(p_{i}\left(x_{i}\right)\) is the probability a patient recovers and \(Y_{i}\) is the number of patients who recover);
        \item \(x_{1}:=1<2=: x_{2}\).
    \end{itemize}
    \begin{enumerate}[(a)]
        \item Express \(\eta_{2}(\theta)\) as a function of \(\eta_{1}(\theta)\) and conclude that the model forms a curved exponential family.
        \item Show that \(\mathbf{Y}=\left(Y_{1}, Y_{2}\right)^{T}\) is minimal sufficient for \(\theta\).
        \item Use the statistic \(1_{\left\{Y_{1}=0\right\}}-1_{\left\{Y_{2}=0\right\}}\) to conclude that \(\mathbf{Y}\) is not complete.
        \item Please solve the following conundrum. The technical condition used in part (b) implies the minimal sufficiency of \(T(\mathbf{Y})=\mathbf{Y}\) and also that \(\mathcal{E}\) is not contained in a unidimensional hyperplane. Therefore, by Problem set \#1, \(\operatorname{int}(\mathcal{E}) \neq \emptyset\), which, in turn, implies the completeness of \(T(\mathbf{Y})=\mathbf{Y}\). What is going on?
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}[(a)]
        \item \[f(y_1=k_1)=\binom{2}{k_1}p_1^{k_1}(1-p_1)^{2-k_1}, \]
        \[f(y_2=k_2)=\binom{1}{k_2}p_2^{k_2}(1-p_2)^{1-k_2}=p_2^{k_2}(1-p_2)^{1-k_2}. \]
        \item \[
            \begin{aligned}
                &\quad\, f(y_1=k_1, y_2=k_2)\\
                &=f(y_1=k_1)f(y_2=k_2)\\
                &=\binom{2}{k_1}\exp\left(k_1\left(\ln\left(1-e^{-\theta}\right)+\theta\right)+k_2\left(\ln\left(1-e^{-2\theta}\right)+2\theta\right)-4\theta\right).
            \end{aligned}
        \]
        \[
            \begin{aligned}
                &\frac{f(y_1=k_1, y_2=k_2)}{f(y_1=m_1, y_2=m_2)}\\
                ={}&{}\frac{\binom{2}{k_1}}{\binom{2}{m_1}}\exp\left((k_1-m_1)\left(\ln\left(1-e^{-\theta}\right)+\theta\right)+(k_2-m_2)\left(\ln\left(1-e^{-2\theta}\right)+2\theta\right)\right),
            \end{aligned}
        \]
        If we want this ratio doesn't depend on $\theta$, then $k_1=m_1$, $k_2=m_2$. $Y=(Y_1,Y_2)$ is minimal statistic.
        \item \[
            \mathbf{E}(1_{Y_1=0})=\Pr(Y_1=0)=(1-p_1)^2=e^{-2\theta}.
        \]
        \[
            \mathbf{E}(1_{Y_2=0})=\Pr(Y_2=0)=(1-p_2)^2=e^{-2\theta}.
        \]
        So, $\mathbf{E}(1_{Y_1=0}-1_{Y_2=0})=\mathbf{E}(f(Y))=0$. But $\Pr(1_{Y_1=0}-1_{Y_2=0})\neq 1$.
        \item I think the $\mathcal{E}$ is the parameter space of canonical form, and because it is curved, int$(\mathcal{E})=\emptyset$.
    \end{enumerate}
\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} \operatorname{Poi}(\lambda)\), \(\lambda>0\). Prove directly - i.e., without resorting to exponential families - that the statistic \(T(\mathbf{X})=\sum_{i=1}^{n} X_{i}\) is complete for \(\lambda\).
\end{ex}

\begin{solution}
    Knowing that
    \[
        \bar{X}\sim N\left(\lambda, \frac{\lambda}{n}\right), \qquad T=\sum_{i=1}^nX_i\sim N\left(n\lambda, n\lambda\right).
    \]
    For some function $f(T)$,
    \[
        \mathbf{E}(f(T))=\sum f(t)\frac{1}{\sqrt{2\pi n\lambda}}e^\frac{-(t-n\lambda)^2}{2n\lambda}=0.
    \]
    Because $\frac{1}{\sqrt{2\pi n\lambda}}e^\frac{-(t-n\lambda)^2}{2n\lambda}>0$. So, $f(t)=0$, i.e. $\Pr(f(t)=0)=1$, a.s.
\end{solution}

\begin{ex}
    Let \(Y_{1}, \ldots, Y_{n} \stackrel{i.i.d. }{\sim} E(\eta, 1), \eta \in \mathbb{R}\), namely, a shifted exponential distribution, which has density
    \[
    f_{Y_{1}}(y)=e^{-(y-\eta)} 1_{(\eta, \infty)}(y), \quad y \in \mathbb{R} .
    \]
    Let \(X_{i}=e^{-Y_{i}}, i=1, \ldots, n\), and \(\theta=e^{-\eta}\)
    \begin{enumerate}
        \item Show that \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} U[0, \theta], \theta>0\).
        \item Conclude that \(X_{(n)}\) is complete for \(\theta\) or \(\eta\).
        \item Now consider \(Y_{(1)}=-\log X_{(n)}\). Show that \(Y_{(1)}\) is complete for \(\eta\).
    \end{enumerate}
\end{ex}

\begin{solution}
    \begin{enumerate}
        \item \[
            F(y\leqslant Y)=\int_\eta^ye^{-t+\eta}\der t= 1-e^{-y+\eta}.
        \]
        \[
            \begin{aligned}
                \Pr(x_i\leqslant X_i)&=\Pr(e^{-y_i}\leqslant X_i)\\
                &=\Pr(y_i\geqslant -\ln(X_i))\\
                &=e^{\ln(X_i)+\eta}=\frac{1}{\theta}X_i.
            \end{aligned}
        \]
        So, we can know that \(f(x_i)=\frac{1}{\theta}\).
        \item $f_{X_{(n)}}(x)=nx^{n-1}\theta^{-n}$, $0<x<\theta$. If for some function $g(T)$, $\mathbf{E}(g(T))=0$, then
        \[
            \begin{aligned}
                0 &=\frac{\der}{\der  \theta} \mathbf{E} g(T) \\
                &=\frac{\der}{\der  \theta} \int_{0}^{\theta} g(t) n t^{n-1} \theta^{-n} \der  t \\
                &=\left(\theta^{-n}\right) \frac{\der}{\der  \theta} \int_{0}^{\theta} n g(t) t^{n-1} \der  t+\left(\frac{\der}{\der  \theta} \theta^{-n}\right) \int_{0}^{\theta} n g(t) t^{n-1} \der  t\\
                &=\frac{ng(\theta)}{\theta}.
            \end{aligned}
        \]
        And because $\theta>0$, $n>0$. So, $g(\theta)=0$. Then, $T$ is complete.
        \item They are equivalent, and $Y_{(1)}$ is complete for $\eta$.
    \end{enumerate}
\end{solution}

\begin{ex}
    Let \(\mathcal{P}\) be a family of probability measures. If \(T\) is complete for \(\mathcal{P}\) and \(U\) is equivalent to \(T\), show that \(U\) is complete for \(\mathcal{P}\).
\end{ex}

\begin{solution}
    Let $f(t)$ be the distribution of $T$ and $T=m(U)$. Then $f^*(u)=f(m(U))m'(U)$. For $T$, if it is complete, then for any function $g(T)$,
    \[
        \int g(t)f(t)\der t=0 \Rightarrow \Pr(g(t)=0)=1.
    \]
    Substitute $t$ with $m(u)$. Then,
    \[
        \begin{aligned}
            &\quad\,\int g(m(u))f(m(u))\der (m(u))\\
            &=\int g(m(u))f(m(u))m'(u)\der u\\
            &=\int g(m(u))f^*(u)\der u=0\Rightarrow \Pr(g(m(u))=0)=1.
        \end{aligned}
    \]
    And because the arbitrary of $g$, $g(m(u))$ can be taken as any function of $u$. So, $U$ is also complete.
\end{solution}

\begin{ex}
    Let \(X_{1}, \ldots, X_{n} \stackrel{i.i.d. }{\sim} \mathcal{N}\left(\mu, \sigma^{2}\right)\), \(\mu \in \mathbb{R}, \sigma^{2}>0 .\) Show that \(\bar{X}\) and \(S^{2}\) are independent.

    (\rmk{2}: this classical result can be shown by means of characteristic functions. However, in the context of our course, a much shorter proof can be provided).
\end{ex}

\begin{solution}
    We know that $(\bar{X}, S^2)$ are sufficient. And $\bar{X}\sim N(\mu,\sigma^2/n)$. So, $\bar{X}$ is complete for $\mu$ (Because $N(\mu,\sigma^2/n)$ is full rank exponential family).

    And $S^2$ only depends on $\sigma^2$ (Because $\frac{(n-1)S^2}{\sigma^2}=\sum_{i=1}^n(\frac{X-\bar{X}}{\sigma})^2\sim\chi_n^2$), it is an ancillary statistic. So, from Basu's theorem, they are independent.
\end{solution}


\subsection{Semirings}\label{subsec:semirings}

We will start by defining semirings, and to do that we will first motivate distributivity.

\begin{proposition}\label{thm:monoid_distributivity}
  Fix an \hyperref[rem:additive_magma/multiplication]{additive} \hyperref[def:monoid]{monoid} \( (R, +, \cdot) \), where \( +: R \times R \to R \) is the monoid operation and \( \cdot: \BbbN \times R \to R \) is defined via \eqref{eq:rem:additive_magma/multiplication}.

  We have the following property, which we call \term{distributivity} of \( \cdot \) over \( + \):
  \begin{equation}\label{eq:thm:monoid_distributivity}
    n \cdot (x + y) = n \cdot x + n \cdot y.
  \end{equation}
\end{proposition}
\begin{proof}
  We use induction on \( n \). The case \( n = 0 \) is trivial. Suppose that \eqref{eq:thm:monoid_distributivity} holds. Then
  \begin{equation*}
    (n + 1) \cdot (x + y)
    \reloset {\eqref{eq:def:magma/exponentiation}} =
    n \cdot (x + y) + (x + y)
    \reloset {\T{ind.}} =
    n \cdot x + n \cdot y + (x + y)
    \reloset {\eqref{eq:def:magma/exponentiation}} =
    (n + 1) \cdot x + (n + 1) \cdot y.
  \end{equation*}
\end{proof}

\begin{definition}\label{def:semiring}\mcite[1]{Golan2010}
  A \term{semiring} is a \hyperref[def:magma/commutative]{commutative} \hyperref[def:monoid]{monoid} \( (R, +) \) with a second \hyperref[def:magma/associative]{associative} operation \( \cdot: R \times R \to R \) called \term{multiplication}, which extends multiplication with natural numbers. The precise compatibility axioms are listed in \fullref{def:semiring/theory} because they fit nicely into first-order logic (unlike the \hyperref[def:semimodule/theory]{theory of semimodules}, for example, for which we prefer expressing these conditions in the metalogic).

  Although not strictly necessary, it will be convenient for us to assume that multiplication has an identity. If a multiplicative identity does not exist, we call \( (R, +, \cdot) \) a \term{nonunital semiring}. A canonical example of a nonunital semiring is a \hyperref[def:semiring_ideal]{semiring ideal}. We will not use nonunital semirings, but it is important to acknowledge their existence. In this context, if an identity exists, we will sometimes call \( (R, + \cdot) \) a \term{unital semiring}.

  We call \( (R, +) \) the \term{additive monoid} and \( (R, \cdot) \) the \term{multiplicative monoid} of the semiring. We also consider the \term{additive group} and the \term{multiplicative group} as the subsets of \hyperref[def:monoid_inverse]{invertible} elements. Both are instances of \fullref{thm:invertible_submonoid_is_group}. The multiplicative group is denoted by \( R^\times \); it is discussed further in \hyperref[def:divisibility/unit]{units}.

  Semirings have the following metamathematical properties:
  \begin{thmenum}
    \thmitem{def:semiring/theory} The \hyperref[def:first_order_theory]{first-order theory} for semirings extends the \hyperref[def:monoid/theory]{theory of monoids}.

    First, we add another \hyperref[rem:first_order_formula_conventions/infix]{infix} binary functional symbol \( \cdot \) and a constant \( 1 \). The notation for the constant is justified by \fullref{thm:semiring_characteristic_homomorphism}.

    We then extend the theory of monoids with \hyperref[def:magma/commutative]{commutativity} for \( + \), \hyperref[def:magma/associative]{associativity} for \( \cdot \), and the following axioms:
    \begin{thmenum}
      \thmitem{def:semiring/left_distributivity} Multiplication on the left distributes over addition:
      \begin{equation}\label{eq:def:semiring/left_distributivity}
        \xi \cdot (\eta + \zeta) \doteq \xi \cdot \eta + \xi \cdot \zeta.
      \end{equation}

      \thmitem{def:semiring/right_distributivity} Multiplication on the right also distributes over addition:
      \begin{equation}\label{eq:def:semiring/right_distributivity}
        (\xi + \eta) \cdot \zeta \doteq \xi \cdot \zeta + \eta \cdot \zeta.
      \end{equation}

      If multiplication is commutative, right distributivity follows from left distributivity.

      \thmitem{def:semiring/absorption} Zero is an absorbing element:
      \begin{equation}\label{eq:def:semiring/absorption}
        \xi \cdot 0 \doteq 0 \wedge 0 \cdot \xi \doteq 0.
      \end{equation}

      \thmitem{def:semiring/identity} We also restate the identity axiom \eqref{eq:def:monoid/theory/identity} for the multiplicative unit \( 1 \) to highlight its connection with \eqref{eq:def:semiring/absorption}:
      \begin{equation}\label{eq:def:semiring/identity}
        \xi \cdot 1 \doteq \xi \wedge 1 \cdot \xi \doteq \xi.
      \end{equation}
    \end{thmenum}

    \thmitem{def:semiring/homomorphism} A \hyperref[def:first_order_homomorphism]{first-order homomorphism} from the semiring \( R \) to \( T \) is a function \( \varphi: R \to T \) that is a \hyperref[def:monoid/homomorphism]{monoid homomorphism} both for their additive monoids also for their multiplicative monoids.

    \thmitem{def:semiring/submodel} The set \( A \subseteq R \) is a \hyperref[thm:substructure_is_model]{submodel} of \( R \) if it is a both \hyperref[def:monoid/submodel]{submonoid} of the additive monoid and also of the multiplicative monoid. We call \( A \) a \term{sub-semiring}.

    As a consequence of \fullref{thm:positive_formulas_preserved_under_homomorphism}, the \hyperref[def:multi_valued_function/image]{image} of a homomorphism \( \varphi: R \to T \) is a sub-semiring of \( A \).

    For an arbitrary set \( A \), we denote the \hyperref[def:first_order_generated_substructure]{generated submodel} by \( \braket{ A } \).

    \thmitem{def:semiring/trivial} The \hyperref[thm:substructures_form_complete_lattice/bottom]{trivial} semiring is the \hyperref[def:pointed_set/trivial]{trivial pointed set} \( \set{ 0 } \).

    See \fullref{ex:def:semiring/trivial} for some properties of the trivial semiring.

    \thmitem{def:semiring/exponentiation} As we shall see in \fullref{thm:semiring_characteristic_homomorphism}, multiplication in \( \cdot \) extends left multiplication with natural numbers in the monoid \( (R, +) \). We do have a third operation, however --- \hyperref[def:monoid/exponentiation]{monoid exponentiation} in \( (R, \cdot) \).

    For any integer \( n \), we have the fundamental property \( 1^n = 1 \).

    \thmitem{def:semiring/commutative} If multiplication is commutative, we call the semiring itself \term{commutative}. Unless multiplication corresponds to function composition, most semirings we will encounter will be commutative.

    Notable exceptions to this rule are \hyperref[def:ordinal]{ordinals}. A \hyperref[def:successor_and_limit_ordinal]{limit ordinal} \( \alpha \), regarded as the set of all smaller ordinals, is a semiring. It is not commutative, however, as shown in \fullref{ex:ordinal_addition}.

    \thmitem{def:semiring/power_set} Similarly to power set magmas defined in \fullref{def:magma/power_set}, the power set \( \pow(R) \) of a semiring is also a semiring with the operations
    \begin{align*}
      A \oplus B &\coloneqq \set{ x + y \given x \in A \T{and} y \in B } \\
      A \odot B  &\coloneqq \set{ x \cdot y \given x \in A \T{and} y \in B }
    \end{align*}

    \thmitem{def:semiring/category} The corresponding \hyperref[def:category_of_small_first_order_models]{category of \( \mscrU \)-small models} \( \ucat{SRing} \) is \hyperref[def:concrete_category]{concrete} over \hyperref[def:monoid]{\( \ucat{CMon} \)} with the forgetful functor taking the additive monoids. We denote the category of commutative semirings by \( \cat{CSRing} \).

    \thmitem{def:semiring/opposite}\mcite[555]{Knapp2016BasicAlgebra} The \term{opposite semiring} of \( (R, +, \cdot) \) is the semiring \( (R, +, \star) \), with multiplication defined as \( x \star y = y \cdot x \).
  \end{thmenum}
\end{definition}

\begin{remark}\label{rem:semiring_etymology}
  In \fullref{def:semiring}, we require semirings to have both an additive identity and a multiplicative identity. This is not consistent with semigroups defined in \fullref{def:magma/associative}, which in general do not have identities.

  \cite[ch. 3]{GondranMinoux1984Graphs} suggest using \enquote{dioid} (short for \enquote{double monoid}) instead of \enquote{semiring}. \cite[xi]{Golan2010} describes how the term \enquote{dioid} may refer to semirings with idempotent addition, i.e. a general form of the tropical semirings defined in \fullref{def:tropical_semiring}.

  We thus prefer using the term \enquote{semiring} as we have defined it in \fullref{def:semiring}.
\end{remark}

\begin{example}\label{ex:def:semiring}
  We list several examples of \hyperref[def:semiring]{semirings} that are not \hyperref[def:ring]{rings}.

  \begin{thmenum}
    \thmitem{ex:def:semiring/trivial} A \hyperref[def:semiring/homomorphism]{semiring} is trivial if and only if \( 0_R = 1_R \). This follows from \eqref{eq:def:semiring/absorption} and \eqref{eq:def:semiring/identity}.

    As a consequence, if \( \varphi: \set{ 0 } \to R \) is a \hyperref[def:semiring/homomorphism]{semiring} homomorphism, \( R \) is a trivial semiring. This is further strengthened by \fullref{thm:semiring_embedding_preserves_characterstic}.

    \thmitem{ex:def:semiring/natural_numbers} The \hyperref[def:set_of_natural_numbers]{natural numbers} are the quintessential example of a semiring. We prove in \fullref{thm:natural_number_multiplication_properties} that they are a semiring.

    \thmitem{ex:def:semiring/ordinals} Every \hyperref[def:successor_and_limit_ordinal]{limit ordinal} is a monoid under addition, as discussed in \fullref{ex:def:semiring/ordinals}, however it is not commutative.

    \hyperref[def:cardinal_arithmetic/addition]{Cardinal addition} is commutative, however, and hence for every \hyperref[def:successor_and_limit_cardinal/weak_limit]{limit cardinal} \( \kappa \), the set of all cardinals smaller than \( \kappa \) are a semiring.

    \thmitem{ex:def:semiring/lattice} We discussed in \fullref{ex:def:monoid/semilattice} that in a \hyperref[def:semilattice/bounded]{bounded lattice} \( (X, \vee, \wedge, \top, \bot) \), both \( (X, \vee, \bot) \) and \( (X, \wedge, \top) \) are monoids.

    As a consequence of \fullref{thm:bounded_lattice_absorbing}, \( \bot \) is absorbing with respect to \( \wedge \) and \( \top \) with respect to \( \vee \). Therefore, if the lattice is \hyperref[def:semilattice/distributive_lattice]{distributive}, as a consequence of \fullref{thm:bounded_lattice_absorbing}, both \( (X, \vee, \wedge) \) and \( (X, \wedge, \vee) \) are semirings.

    We refer to these semirings are the positive and negative semiring of the lattice. This terminology comes from \fullref{ex:def:ordered_semiring/lattice}.
  \end{thmenum}
\end{example}

\begin{definition}\label{def:tropical_semiring}\mcite[exmpl. 1.12]{Golan2010}
  Consider the additive monoid \( (\BbbN, +) \) of natural numbers or, more generally, an \hyperref[def:ordered_magma]{ordered} \hyperref[def:magma/commutative]{commutative} \hyperref[def:monoid]{monoid} \( (M, +, \leq) \).

  We adjoin a \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{top element} \( \infty \) to \( M \) that is absorbing with respect to addition. That is, \( x + \infty = \infty \) for every \( x \in M \).

  The \( \min \)-plus semiring over \( M \) is the triple \( (M \cup \set{ \infty }, \min, +) \). The \hyperref[def:partially_ordered_set_extremal_points/maximum_and_minimum]{minimum} as a binary operation plays the role of semiring addition, with \( \infty \) as the zero element. The usual addition in \( M \) extended with \( \infty \) plays the role of semiring multiplication, with \( 0 \) as the multiplicative identity.

  We analogously define the \( \max \)-plus semiring, adjoining a \hyperref[def:partially_ordered_set_extremal_points/top_and_bottom]{bottom element} \( -\infty \) rather than a top element \( \infty \).

  We will sometimes use \enquote{tropical semiring} to refer to either type of semirings. See \fullref{rem:tropical_semiring_etymology}.
\end{definition}
\begin{defproof}
  We will only show \hyperref[def:semiring/left_distributivity]{distributivity}. If \( x \leq y \), since \( \leq \) is compatible with \( + \), we have
  \begin{equation*}
    \underbrace{\min\set{ x, y }}_{x} + z = x + z \leq y + z.
  \end{equation*}

  Therefore,
  \begin{equation*}
    \min\set{ x, y } + z = \min\set{ x + z , y + z }.
  \end{equation*}
\end{defproof}

\begin{remark}\label{rem:tropical_semiring_etymology}
  \hyperref[def:tropical_semiring]{\( \min \)-plus} and \( \max \)-plus semirings are sometimes referred to as the \term{tropical semirings}. This term is ambiguous, unfortunately, but it gives rise to the terms \enquote{tropical geometry} and \enquote{tropical optimization}.

  According to \cite{Pin1994}, the name \enquote{tropical semiring} is a dedication to the Brazilian-born Imre Simon. The paper also introduces the terms \enquote{tropical integers}, \enquote{tropical reals}, etc. \cite[3]{Golan2010} refers to the more general notion of additively-idempotent semirings. Both reserve the term \enquote{tropical semiring} for the case where \( M = \BbbN \). \cite[ch. 3]{GondranMinoux1984Graphs} does not explicitly use the word \enquote{tropical}, but instead refers to semirings as \enquote{dioids}, and the latter term sometimes refers to additively-idempotent semirings.
\end{remark}

\begin{proposition}\label{thm:semiring_characteristic_homomorphism}
  For every \hyperref[def:semiring/identity]{semiring}, multiplication extends the abelian group multiplication.

  More precisely, denote the additive identity by \( 0_R \) and the multiplicative identity by \( 1_R \). Define the following semiring homomorphism:
  \begin{equation}\label{eq:thm:semiring_characteristic_homomorphism}
    \begin{aligned}
      &\iota: \BbbN \to R \\
      &\iota(n) \coloneqq \begin{cases}
        0_R                &n = 0, \\
        \iota(n - 1) + 1_R &n > 0.
      \end{cases}
    \end{aligned}
  \end{equation}

  This is the unique homomorphism from \( \BbbN \) to \( R \). Furthermore, we have the following analogue to \eqref{eq:def:magma/exponentiation}:
  \begin{equation}\label{eq:thm:semiring_characteristic_homomorphism/multiplication}
    \iota(n) \cdot x \coloneqq \begin{cases}
      0_R,                      &n = 0, \\
      \iota(n - 1) \cdot x + x, &n > 1.
    \end{cases}
  \end{equation}
\end{proposition}
\begin{proof}
  First note that \eqref{eq:thm:semiring_characteristic_homomorphism/multiplication} follows from \eqref{eq:thm:semiring_characteristic_homomorphism} via \hyperref[def:semiring/right_distributivity]{right distributivity}.

  It remains to show that \( \iota \) is a monoid homomorphism, and that it is unique. Clearly \( \iota(0) = 0_R \) and \( \iota(1) = 1_R \). Proving \( \iota(n + m) = \iota(n) + \iota(m) \) and \( \iota(nm) = \iota(n) \cdot \iota(m) \) can be done via nested induction.

  Now suppose \( \varphi: \BbbN \to R \) is a homomorphism. It is clear that \( \varphi(0) = 0_R \) and \( \varphi(1) = 1_R \), and also
  \begin{equation*}
    \varphi(n + 1) = \varphi(n) + \varphi(1) = \varphi(n) + 1_R.
  \end{equation*}

  This implies \( \iota = \varphi \).
\end{proof}

\begin{proposition}\label{thm:category_of_semirings_properties}
  The \hyperref[def:semiring/category]{category of semirings} has the following basic properties:
  \begin{thmenum}
    \thmitem{thm:category_of_semirings_properties/initial} The \hyperref[def:set_of_integers]{ring of integers} \( \BbbZ \) is an \hyperref[def:universal_objects/initial]{initial object}.

    \thmitem{thm:category_of_semirings_properties/terminal} The trivial semiring \( \set{ 0 } \) is an \hyperref[def:universal_objects/terminal]{terminal object}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:category_of_semirings_properties/initial} Follows from \fullref{thm:semiring_characteristic_homomorphism}.
  \SubProofOf{thm:category_of_semirings_properties/terminal} Follows from \fullref{ex:def:semiring/trivial}.
\end{proof}

\begin{definition}\label{def:ordered_semiring}\mcite[224]{Golan2010}
  An \term{ordered semiring} is a \hyperref[def:magma/commutative]{commutative} semiring \( R \) with a \hyperref[def:partially_ordered_set]{partial order} \( \leq \) such that \( (R, +) \) is an \hyperref[def:ordered_magma]{ordered magma} and, additionally, \( x \leq y \) and \( 0 \leq z \) imply \( xz \leq yz \).

  As in \fullref{def:ordered_magma}, the commutativity condition can be avoided, but then we would need to also require \( zx \leq zy \).

  If the semiring is \hyperref[def:totally_ordered_set]{totally ordered}, we can use the usual terminology that is conventional for real numbers:
  \begin{itemize}
    \item \( x \) is \term{positive} if \( x > 0 \).
    \item \( x \) is \term{nonnegative} if \( x \geq 0 \).
    \item \( x \) is \term{negative} if \( x < 0 \).
    \item \( x \) is \term{nonpositive} if \( x \leq 0 \).
  \end{itemize}
\end{definition}

\begin{example}\label{ex:def:ordered_semiring}
  We list several examples of \hyperref[def:ordered_semirings]{ordered semirings}.

  \begin{thmenum}
    \thmitem{ex:def:ordered_semiring/natural_numbers} The \hyperref[def:set_of_natural_numbers]{natural numbers} form an ordered semiring as shown in \fullref{thm:natural_numbers_are_well_ordered}.

    \thmitem{ex:def:ordered_semiring/lattice} We discussed in \fullref{ex:def:semiring/lattice} that a \hyperref[def:semilattice/bounded]{bounded} \hyperref[def:semilattice/distributive_lattice]{distributive} \hyperref[def:semilattice/lattice]{lattice} \( (X, \vee, \wedge) \) can be regarded as a semiring, and so can its opposite lattice.

    We discussed in \fullref{ex:def:ordered_magma/semilattice} that both \( (X, \vee) \) and \( (X, \wedge) \) are \hyperref[def:ordered_magma]{ordered magmas}. Both \( (X, \vee, \wedge) \) and \( (X, \wedge, \vee) \) vacuously satisfy the condition from \fullref{def:ordered_semiring}, which makes them ordered semirings.

    All elements of the ordered semiring \( (X, \vee, \wedge) \) are nonnegative and all elements of \( (X, \wedge, \vee) \) are nonpositive. With a slight abuse of notation, we refer to them as the \term{positive} and \term{negative} semirings of the lattice.
  \end{thmenum}
\end{example}

\begin{definition}\label{def:divisibility}\mimprovised
  Fix an arbitrary element \( x \) in a \hyperref[def:semiring]{semiring}. If there exist elements \( l \) and \( r \) such that \( x = lr \), we say that \( l \) is a \term{left divisor} of \( x \), and that \( r \) is a \term{right divisor}.

  In a \hyperref[def:semiring/commutative]{commutative semiring}, the two notions coincide, and we simply use the term \enquote{divisor}. If \( x \) is a divisor of \( y \), we write \( x \mid y \) and say that \( y \) is a \term{multiple} of \( x \). Most rings we will encounter will be commutative, but it is useful to have the weaker notions of left and right divisors.

  \begin{thmenum}
    \thmitem{def:divisibility/zero}\mcite[4]{Golan2010} Divisors of \( 0 \) are called \term{zero divisors}. Due to \hyperref[def:semiring/absorption]{absorption}, every semiring element is a zero divisor. If \( lr = 0 \) for nonzero \( l \) and \( r \), we say that \( l \) (resp. \( r \)) is a \term{nontrivial} left (resp. right) zero divisor.

    \thmitem{def:divisibility/unit} Divisors of \( 1 \) are called \term{invertible}, since they are precisely the \hyperref[def:monoid_inverse]{monoid inverses} under multiplication. They are also sometimes called \term{units}.

    The set of all two-sided units of \( R \) is precisely the \hyperref[def:semiring]{multiplicative group} \( R^\times \).
  \end{thmenum}
\end{definition}

\begin{example}\label{ex:def:divisibility}
  \hfill
  \begin{thmenum}
    \thmitem{ex:def:divisibility/integers} The positive integers are commutative and their left and right divisors coincide. They have no \hyperref[def:divisibility/zero]{nontrivial zero divisors} as a consequence of \fullref{thm:natural_number_multiplication_properties}.

    \thmitem{ex:def:divisibility/matrix_zero_divisors} A simple example of nontrivial zero divisors is given by the \hyperref[def:matrix_algebra]{matrix algebra} \( \BbbZ^{2 \times 2} \). We have
    \begin{equation*}
      \underbrace
      {
        \begin{pmatrix}
          0 & 1 \\
          0 & 0
        \end{pmatrix}
      }_{L}
      \underbrace
      {
        \begin{pmatrix}
          0 & 0 \\
          0 & 1
        \end{pmatrix}
      }_{R}
      =
      \begin{pmatrix}
        0 & 0 \\
        0 & 0
      \end{pmatrix}.
    \end{equation*}

    Therefore, \( L \) is a left zero divisor and \( R \) is a right zero divisor. The two do not commute because
    \begin{equation*}
      \underbrace
      {
        \begin{pmatrix}
          0 & 0 \\
          0 & 1
        \end{pmatrix}
      }_{R}
      \underbrace
      {
        \begin{pmatrix}
          0 & 1 \\
          0 & 0
        \end{pmatrix}
      }_{L}
      =
      \begin{pmatrix}
        0 & 0 \\
        1 & 0
      \end{pmatrix}.
    \end{equation*}

    Nevertheless, \( RLRL \) is the zero matrix, so \( R \) is a left zero divisor and \( L \) is a right zero divisor.
  \end{thmenum}
\end{example}

\begin{proposition}\label{thm:divisibility_and_isomorphisms}
  Suppose that \( R \) and \( S \) are \hyperref[def:semiring/commutative]{commutative semirings}.

  \begin{thmenum}
    \thmitem{thm:divisibility_and_isomorphisms/divisibility} If \( \varphi: R \to S \) is any homomorphism, then \( x \mid y \) implies \( \varphi(x) \mid \varphi(y) \). The converse holds of \( \varphi \) is an isomorphism.

    \thmitem{thm:divisibility_and_isomorphisms/zero} If \( R \) and \( S \) are isomorphic, the \hyperref[def:divisibility/zero]{zero divisors} of \( R \) are precisely the zero divisors of \( S \).

    \thmitem{thm:divisibility_and_isomorphisms/unit} If \( R \) and \( S \) are isomorphic, the \hyperref[def:divisibility/unit]{units} of \( R \) are precisely the units of \( S \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:divisibility_and_isomorphisms/divisibility} If \( x \mid y \), then \( xr = y \) for some \( r \in R \). Then \( \varphi(x) \varphi(r) = \varphi(y) \), hence \( \varphi(x) \mid \varphi(y) \). If \( \varphi \) is an isomorphism, the converse follows by using \( \varphi^{-1}: S \to R \).

  \SubProofOf{thm:divisibility_and_isomorphisms/zero} Follows from \fullref{thm:divisibility_and_isomorphisms/divisibility} by noting that homomorphisms preserve zeros.

  \SubProofOf{thm:divisibility_and_isomorphisms/unit} Follows from \fullref{thm:divisibility_and_isomorphisms/divisibility} by noting that homomorphisms preserve ones.
\end{proof}

\begin{proposition}\label{thm:semiring_cancellative_iff_no_zero_divisors}
  An element of a \hyperref[def:semiring/commutative]{commutative semiring} is cancellable if and only if it is not a \hyperref[def:divisibility]{zero divisor}. That is, \( x \mid 0 \) if and only if \( xy = xz \) does not imply \( y = z \).
\end{proposition}
\begin{proof}
  Let \( x \) be a nonzero element.

  \SufficiencySubProof Suppose that \( x \) is a zero divisor and let \( y \) be such that \( xy = 0 \). For any element \( z \), we have
  \begin{equation*}
    xy = 0 = x(yz).
  \end{equation*}

  But \( y \neq yz \) unless \( z = 1 \). Thus, \( x \) is not cancellable.

  \NecessitySubProof Suppose that \( x \) is cancellable.

  Suppose also that \( xy = 0 \) for some nonzero \( y \). Then \( xy = x0 \), which implies \( y = 0 \). But this contradicts out choice of \( y \).

  Thus, \( x \) is not a zero divisor.
\end{proof}

\begin{definition}\label{def:entire_semiring}
  We say that the \hyperref[def:semiring]{semiring} \( R \) is \term{entire} if any of the following equivalent conditions hold:
  \begin{thmenum}
    \thmitem{def:entire_semiring/zero_divisors}\mcite[4]{Golan2010} \( R \) has no \hyperref[def:divisibility/zero]{nontrivial zero divisors}.
    \thmitem{def:entire_semiring/cancellation} \( R \setminus \set{ 0_R } \) is a \hyperref[def:magma/cancellative]{cancellative} \hyperref[def:monoid]{monoid} with respect to multiplication.
  \end{thmenum}
\end{definition}
\begin{defproof}
  The equivalence follows from \fullref{thm:semiring_cancellative_iff_no_zero_divisors}.
\end{defproof}

\begin{proposition}\label{thm:semiring_divisibility_order}
  In an \hyperref[def:entire_semiring]{entire} \hyperref[def:semiring/commutative]{commutative semiring}, the \hyperref[def:divisibility]{divisibility} relation is a \hyperref[def:preordered_set]{preorder}.

  It is not a partial order in general. To avoid the nonuniqueness problems described in \fullref{ex:preorder_nonuniqueness}, we instead prefer working with ideals. See \fullref{rem:lattice_of_principal_ideals} and \fullref{rem:lattice_of_principal_ideals} for the general approach.
\end{proposition}
\begin{proof}
  Fix a semiring \( R \).

  \SubProofOf[def:binary_relation/reflexive]{reflexivity} Clearly every element of \( R \) divides itself.

  \SubProofOf[def:binary_relation/transitive]{transitivity} Let \( x \mid y \mid z \). Then there exist elements \( a \) and \( b \) such that \( y = a x \) and \( z = b y \). Hence, \( z = (ba) x \) and \( x \mid z \).
\end{proof}

\begin{definition}\label{def:zerosumfree}\mcite[4]{Golan2010}
  We say that an \hyperref[rem:additive_magma]{additive} \hyperref[def:monoid]{monoid} is \term{zerosumfree} if the \hyperref[thm:invertible_submonoid_is_group]{additive group} is trivial. That is, if \( x + y = 0 \) implies \( x = y = 0 \).
\end{definition}

\begin{example}\label{ex:def:zerosumfree}
  We list several examples of \hyperref[def:zerosumfree]{zerosumfree} semirings:
  \begin{thmenum}
    \thmitem{ex:def:zerosumfree/natural_numbers} By \fullref{thm:natural_number_addition_properties}, the natural numbers are zerosumfree.

    \thmitem{ex:def:zerosumfree/lattice} We discussed in \fullref{ex:def:semiring/lattice} that every bounded distributive lattice \( (X, \vee, \wedge) \) has two associated semirings.

    We will show that the positive semiring \( (X, \vee, \wedge) \) is zerosumfree. The proof only relies on \( \vee \) being idempotent. Suppose that \( x \vee y = \bot \). Then
    \begin{equation*}
      \bot
      =
      x \vee y
      \reloset {\eqref{eq:def:magma/idempotent}} =
      (x \vee x) \vee y
      \reloset {\eqref{eq:def:magma/associative}} =
      x \vee (x \vee y)
      =
      x \vee \bot
      \reloset {\eqref{eq:thm:binary_lattice_operations/identity/join}} =
      x.
    \end{equation*}

    Therefore, \( x = \bot \). But \( \bot \vee y = y \), hence \( x \vee y = \bot \) implies \( y = \bot \).

    This demonstrates that the positive semiring is zerosumfree.

    \thmitem{ex:def:zerosumfree/tropical} The \hyperref[def:tropical_semiring]{\( \min \)-plus semiring} \( (\BbbN \cup \set{ \infty }, \min, +) \) discussed in \fullref{def:tropical_semiring} is also zerosumfree. Indeed, \( \min \) is idempotent, and the proof is analogous to the one for lattices in \fullref{ex:def:zerosumfree/lattice}.
  \end{thmenum}
\end{example}


\section{Homework \# 05}

Given a spring mass damper system as shown below, obtain a function $x(t)$ to describe the movement of the object.

Consider

\begin{equation}
F(t) = \begin{cases}
      t &  0 \leq t < 2  \\
      2 & 2 \leq t < 4 \\
      2(5-t) & 4 \leq t < 5 \\
      0 & \text{for all other cases}
   \end{cases}
\end{equation}

Friction follows the following behaviour

\begin{equation}
F_f (t) = \begin{cases}
      t &  0 \leq t < \mu N  \\
      \mu N & \mu N \leq t < t_x \\
      -\mu N & t_x \leq t < 5 \\
      0 & \text{for all other cases}
   \end{cases}
\end{equation}

\begin{equation*}
N = m*g
\end{equation*}

The values for the constants are as follows:

\begin{itemize}
 \item Spring coefficient $k = 1$
 \item Damper coefficient $b = 2$
 \item Mass $m = 0.5$
 \item Friction coefficient $\mu = 0.1$
 \item Gravity $g = 9.81$
\end{itemize}

\subsection{Solution}

\begin{itemize}
 \item Split the analysis of the system in different time intervals, taking into account the change in values for both $F(t)$ and $F_f (t)$.
 \item Obtain the homogeneous and particular solutions at each time interval, and determine the coefficients associated for each of them.
 \item  Find the value for $t_x$. Recall $\dot{x}(t_x)=0$.
\end{itemize}

The sum of forces at any point in time is

$\sum F = F(t) - F_f (t) - F_b - F_k$

$F_k = k x(t)$

$F_b = b \dot{x}(t)$

Recall that $\sum F = m \ddot{x}(t)$ and rewrite the equation as follows

$ m \ddot{x}(t) = F(t) - F_f (t) - b \ddot{x}(t) - k x(t)$

Reorder the terms to express the equation as an ODE

$ m \ddot{x}(t) + b \ddot{x}(t) + k x(t) = F(t) - F_f (t)$

Divide all terms by $m$

$  \ddot{x}(t) + \dfrac{b}{m} \ddot{x}(t) + \dfrac{k}{m} x(t) = \dfrac{F(t) - F_f (t)}{m}$

Evaluate the known constants

$  \ddot{x}(t) + 4 \ddot{x}(t) + 2 x(t) = 2 F(t) - 2 F_f (t)$

The general solution to this ODE will have the following form

$x(t) = x_h (t) + x_p (t)$

The homogeneous solution is obtained by replacing the values on the right hand side of the equation with 0 and solving the ODE.

$\ddot{x}(t) + 4 \ddot{x}(t) + 2 x(t) = 0$

$\lambda^2 + 4 \lambda +2 = 0$

$\lambda = \dfrac{-4\pm \sqrt{4^2 - 4 (1) (2)}}{2}$

$\lambda = \dfrac{-4\pm \sqrt{8}}{2}$

$\lambda = -2\pm \sqrt{2}$

$\lambda_1 = -2 + \sqrt{2}$

$\lambda_2 = -2 - \sqrt{2}$

The homogeneous solution is the following

$x_h (t) = c_1 \exp{(\lambda_1 t)} + c_2 \exp{(\lambda_2 t)}$

$\dot{x}_h (t) = c_1 \lambda_1 \exp{(\lambda_1 t)} + c_2 \lambda_2 \exp{(\lambda_2 t)}$

The particulara solution is different at each time interval. See in the following sections for the process of obtaining it at each time interval.

The coefficients $c_1$ and $c_2$ are obtained at each time interval based on the initial conditions for each of them ($x(\tau)$, $\dot{x}(\tau)$).

A general solution is presented below

Let $t=\tau$ and evaluate for $x(t)$ and $\dot{x}(t)$

$x(\tau) = x_\tau$

$\dot{x}(\tau) = \dot{x}_\tau$

Solve for $c_2$ in $x_\tau$

$x_{\tau} = c_1 \exp{(\lambda_1 \tau)} + c_2 \exp{(\lambda_2 \tau)} + x_p (\tau)$

$x_{\tau} - x_p (\tau) = c_1 \exp{(\lambda_1 \tau)} + c_2 \exp{(\lambda_2 \tau)} $

$x_{\tau} - x_p (\tau) - c_1 \exp{(\lambda_1 \tau)} = c_2 \exp{(\lambda_2 \tau)} $

$c_2   = \dfrac{x_{\tau} - x_p (\tau) - c_1 \exp{(\lambda_1 \tau)}}{\exp{(\lambda_2 \tau)}} $

Now evaluate $c_2$ in $\dot{x}_\tau$ and solve for $c_1$. Recall that the first derivative is written as follows

$\dot{x}(t) = c_1 \lambda_1 \exp{(\lambda_1 t)} + c_2 \lambda_2 \exp{(\lambda_2 t)} +  \dot{x}_p (t) $

Substituting $c_2$

$\dot{x}_{\tau} = c_1 \lambda_1 \exp{(\lambda_1 t)} + (\dfrac{x_{\tau} - x_p (\tau) - c_1 \exp{(\lambda_1 \tau)}}{\exp{(\lambda_2 \tau)}}) \lambda_2 \exp{(\lambda_2 \tau)} +  \dot{x}_p (\tau) $

Factorise $c_1$ and simplify.

$\dot{x}_{\tau} = c_1 \lambda_1 \exp{(\lambda_1 t)} + (x_{\tau} - x_p (\tau) - c_1 \exp{(\lambda_1 \tau)}) \lambda_2  +  \dot{x}_p (\tau) $

$\dot{x}_{\tau} = c_1 (\lambda_1 \exp{(\lambda_1 t)} - \lambda_2 \exp{(\lambda_1 \tau)} )  + \lambda_2 x_{\tau} - \lambda_2 x_p (\tau)  +  \dot{x}_p (\tau) $

Solve for $c_1$

$c_1 = \dfrac{\dot{x}_{\tau} - \lambda_2 x_{\tau} + \lambda_2 x_p (\tau)  -  \dot{x}_p (\tau)}{ \lambda_1 \exp{(\lambda_1 \tau)} - \lambda_2 \exp{(\lambda_1 \tau)}  }  $


%
%==> Section: Plotting functions
%
\section{
  Plotting functions
}
%
%==> Plotting user-defined functions
%
\begin{frame}[fragile]
  \frametitle{
    Plotting user-defined functions
  }

  Ti$k$Z also has a math engine which enables you to plot functions:

  \lstinputlisting{./tex/src/plot_function.tex}

  gives you

  \begin{center}
    \input{./tex/src/plot_function.tex}
  \end{center}

  The domain instruction shows the range of $x$ which is plotted. In this case we are plotting the function $0.025+x+x^2.$ Note the braces around the function that we plot in

  \begin{lstlisting}
    plot (\x, {function});
  \end{lstlisting}

\end{frame}

%
%==> Built-in math functions
%
\begin{frame}[fragile]
  \frametitle{
    Built-in math functions
}

  \begin{itemize}
  \item
    Many mathematical functions are possible; you will probably have enough with

    \begin{lstlisting}
      factorial(\x), sqrt(\x), exp(\x), ln(\x)
      log2(\x), log10(\x), abs(\x)
    \end{lstlisting}

  \item

    \begin{lstlisting}
      pow(\x,y), mod(\x, y)
    \end{lstlisting}

    which gives $x^y$ and $x$ modulo $y.$

  \item

    \begin{lstlisting}
      round(\x), floor(\x), ceil(\x)
    \end{lstlisting}

    rounds $x$ to the nearest integer, the largest integer smaller than $x$, the smallest integer larger than $x$.
  \end{itemize}
\end{frame}

%
%==> Built-in math functions (continued)
%
\begin{frame}[fragile]
  \frametitle{
    Built-in math functions (continued)
}

  \begin{itemize}
  \item
    \begin{lstlisting}
      sin(\x)
    \end{lstlisting}

    it assumes that $x$ is in degrees; if $x$ is expressed in radians use

    \begin{lstlisting}
      sin(\x r)
    \end{lstlisting}

    In a similar fashion,

    \begin{lstlisting}
      cos(\x), cos(\x r), tan(\x), tan(\x r)
    \end{lstlisting}

    We also have

    \begin{lstlisting}
      min(\x,y), max(\x,y).
    \end{lstlisting}

  \item
    In mathematical expressions the two following \textcolor{violet}{\bf rational} variables can be useful:

    \begin{lstlisting}
      e
    \end{lstlisting}

    which is equal to \textcolor{violet}{2.718281828}, and

    \begin{lstlisting}
      pi
    \end{lstlisting}

    which is equal to \textcolor{violet}{3.141592654}.

  \end{itemize}
\end{frame}

%
%==> Plot trig functions
%
\begin{frame}[fragile]

  You can mix functions to compute more complicated expressions (see above for the reason for the {\tt r} parameter in the argument of $\sin$ and $\cos$ and note the use of \textcolor{violet}{\tt pi} to define the domains):
  \lstinputlisting{./tex/src/plot_trig_functions.tex}

  which gives you

  \begin{center}
    \input{./tex/src/plot_trig_functions.tex}
  \end{center}

\end{frame}


\chapter{Transforming Functions}

Let's say I gave you the graph of a function $f$, like this:

\begin{tikzpicture}
    \begin{axis}[
        xmin=-2.2,xmax=2.2,
        ymin=-1,ymax=5,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$x$},
        ylabel={$y$},
      ]
      \addplot[no marks,sdkblue] expression[domain=-2:2,samples=100]{x^2} node[below, yshift=-6mm] {$f(x)$};
    \end{axis}\end{tikzpicture}

And then I tell you that the function $g(x) = f(x) + 1.5$.  Can you guess what the graph of $g$ would look like? It is the same graph, just translated up 1.5:

\begin{tikzpicture}
    \begin{axis}[
        xmin=-2.2,xmax=2.2,
        ymin=-1,ymax=5,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$x$},
        ylabel={$y$},
      ]
      \addplot[dashed,sdkblue] expression[domain=-2:2,samples=100]{x^2} node[below, yshift=-6mm] {$f(x)$};
      \addplot[no marks,sdkblue] expression[domain=-2:2,samples=100]{x^2 + 1.5} node[below, yshift=-6mm] {$g(x)$};
    \end{axis}\end{tikzpicture}

There are four kinds of transformations that we do all the time:
\begin{itemize}
\item Translation up and down in the direction of $y$ axis (the one you just saw)
\item Translation left and right in the direciton of the $x$ axis
\item Scaling up and down along the $y$ axis
\item Scaling up and down along the $x$ axis
\end{itemize}

Now I will demonstrate each of the four using the graph of $\sin(x)$.

\section{Translation up and down}

When you add a positive constant to a function, you translate the
whole graph up that much. A negative constant translates it down.

Here is the graph of $\sin(x) - 0.5$:

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5235, ystep=0.5] (-6.6,-1.7) grid (6.6,1.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x}) - 0.5});
\end{tikzpicture}

\section{Translation left and right}

When you add a positive number to $x$ before running it through $f$,
you translate the graph to the left that much. Adding a negative
number translates the graph to the right.

Here is the graph of $\sin(x - \pi/6)$:

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-1.2) grid (6.6,1.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x} - 30)});
\end{tikzpicture}

Notice the sign:
\begin{itemize}
\item Add to $x$ before processing with the function translates the graph to the \emph{left}.
\item Subtract from $x$ before processing with the function translates the graph to the \emph{right}
\end{itemize}

\section{Scaling up and down in the $y$ direction}

To scale the function up and down, you multiply the result of the
function by a constant.  If the constant is larger than 1, it
stretches the function up and down.

Here is $y = 2\sin(x)$:

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-2.2) grid (6.6,2.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x}) * 2.0});
\end{tikzpicture}

With a wave like this, we speak of its \newterm{Amplitude}, which you
can think of as its height. The baseline that this wave oscillates
around is zero. The maximum distance that it gets from that baseline
is its amplitude.  Thus, the amplitude here has been increased from 1
to 2.

If you multiply by a negative number, the function gets flipped.  Here is $y = -0.5 \sin(x)$

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-1.2) grid (6.6,1.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x}) * -0.5});
\end{tikzpicture}

Amplitude is never negative.  Thus, the amplitude of this wave is 0.5.

\section{Scaling up and down in the $x$ direction}

If you multiply $x$ by a number larger than 1 before running it
through the function, the graph gets compressed toward zero.

Here is $y = \sin(3x)$:

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-1.2) grid (6.6,1.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x} * 3)});
\end{tikzpicture}

The distance between two peaks of a wave is known as its
\newterm{wavelength}.  The original wave had a wavelength of $2\pi$.
The compressed wave has a wavelength of $2\pi/3$.

If you multiply $x$ by a number smaller than 1, it will stretch the function out, away from the $y$ axis.

If you multiply $x$ by a negative number, it will flip the function around the $y$ axis.

Here is $y = 2^{(-0.5x)}$. Notice that it has flipped around the $y$ axis and is stretched out along the $x$ axis.

\begin{tikzpicture}
    \begin{axis}[
        xmin=-3.1,xmax=3.1,
        ymin=-0.5,ymax=8,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$x$},
        ylabel={$y$},
      ]
      \addplot[dashed,sdkblue] expression[domain=-3.0:3.0,samples=100]{2^x} node[left, xshift=-1mm,yshift=-6mm] {$2^x$};
      \addplot[no marks,sdkblue] expression[domain=-3.0:3.0,samples=100]{2^(-0.5 * x)}
      node[above, xshift=-6mm] {$2^{-0.5x}$};
    \end{axis}\end{tikzpicture}

\section{Order is important!}

We can combine these transformations. This allows us, for example, to
translate a function up 2 and then scale along the $y$ axis by 3.

Here is $y = 2.0 (\sin(x) + 1)$:

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-1.2) grid (6.6,4.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1, 2, 3, 4}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-1.25) -- (0, 4.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{sin(deg{\x})});
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{2.0 * (sin(deg{\x}) + 1)});
\end{tikzpicture}

A function is often a series of steps. Here are the steps in $f(x) = 2(\sin(x) + 1)$:
\begin{enumerate}
\item Take the sine of $x$
\item Add 1 to that
\item Multiply that by 2
\end{enumerate}

What if we change the order? Here are the steps in $g(x) = 2\sin(x) + 1$:
\begin{enumerate}
\item Take the sine of $x$
\item Multiply that by 2
\item Add 1 to that
\end{enumerate}

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-6.6,-2.2) grid (6.6,4.2);

% y tick label
\foreach \y in {-2, -1, -1/2, 1/2, 1, 2, 3, 4}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
       {-2\pi,
        -\frac{3\pi}{2},
        -\pi,
        -\frac{\pi}{2},
        { },
         \frac{\pi}{2},
         \pi,
         \frac{3\pi}{2},
         2\pi
        }{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
    \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-2.25) -- (0, 4.25) node[above] {$y$};
% curve
\draw[<->,dashed,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
plot (\x,{2.0 * (sin(deg{\x}) + 1)});
\draw (4, 3) node{$2(\sin(x) + 1)$};
\draw[<->,thick,draw=black,
      domain=-6.5:6.5,samples=300,variable=\x]
      plot (\x,{2.0 * sin(deg{\x} + 1)});
\draw (2.5, -1.5) node{$2\sin(x) + 1$};
\end{tikzpicture}

The moral: You can do multiple transformations of your function, but
the order in which you do them is important.

\begin{Exercise}[title={Transforms}, label=sine_transform]

Find a function that creates a sine wave such that the top of the first crest is
at the point $(\frac{\pi}{2}, 5)$ and the bottom of the trough that follows is at $(\pi, 1)$.

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-1,-0.6) grid (6.6,5.2);

% y tick label
\foreach \y in {1/2, 1, 2, 3, 4, 5}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% axes
    \draw[->,thick] (-0.9,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-0.25) -- (0, 5.25) node[above] {$y$};
    % curve
    \filldraw[black] (1.570796326794897, 5)  circle(3pt) node [right, yshift=1mm]{$(\pi/2,5)$};
    \filldraw[black] (3.141592653589793, 1)  circle(3pt) node[below]{$(\pi,1)$};
    \draw[<->,thick,draw=black,
      domain=-0.5:6.5,samples=300,variable=\x]
      plot (\x,{2.0 * sin(deg{2*\x} - 90) + 3});
\end{tikzpicture}

\end{Exercise}
\begin{Answer}[ref=sine_transform]

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]
% grid
\draw[sdkblue, very thin, xstep=0.5236, ystep=0.5] (-1,-0.6) grid (6.6,5.2);

% y tick label
\foreach \y in {1/2, 1, 2, 3, 4, 5}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% axes
    \draw[->,thick] (-0.9,0) -- (6.5,0) node[right] {$x$};
    \draw[->,thick] (0,-0.25) -- (0, 5.25) node[above] {$y$};
    % curvet
    \filldraw[black] (0.785398163397448, 3)  circle(3pt) node[right, yshift=1mm]{$(\pi/4, 3)$};
    \filldraw[black] (1.570796326794897, 5)  circle(3pt) node [right, yshift=1mm]{$(\pi/2, 5)$};
    \filldraw[black] (3.141592653589793, 1)  circle(3pt) node[below]{$(\pi, 1)$};
    \filldraw[black] (4.71238898038469, 5)  circle(3pt) node[right, yshift=1mm]{$(3\pi/2, 5)$};
    \draw[<->,thick,draw=black,
      domain=-0.5:6.5,samples=300,variable=\x]
      plot (\x,{2.0 * sin(deg{2*\x} - 90) + 3});
    \draw[dashed,thick, draw=black] (0,3) -- (6,3);
\end{tikzpicture}

This wave has an amplitude of 2.  Its baseline has been translated up to 3.

This wave has wavelength of $\pi$. A sine wave usually has a
wavelength of $2\pi$, so we need to compress the $x$ axis by a factor of 2.

The wave first crosses its baseline at $pi/4$.  The sine wave starts
by crossing its baseline, so we need to translate the curve right by
$\pi/4$.

$$f(x) = 2 \sin(2x - \frac{\pi}{4}) + 3$$

\end{Answer}


\subsection{Terms and predicates}

\subsubsection{Predicates}

Zero-order logic adds predicates. Like propositional variables, these have truth values. Unlike propositional variable, predicates take terms as inputs.

For example using propositional logic we can write the statement "you are 25" as \(\theta \).

With preterites we can write this as \(P(you, 25)\).

A propositional variable can be considered a special case of a predicate variable, where the number of inputs is \(0\).


\section{Basis and dimension}

\begin{outcome}
  \begin{enumerate}
  \item Find a basis of a given vector space.
  \item Determine the dimension of a vector space.
  \item Extend a linearly independent set of vectors to a basis.
  \item Shrink a spanning set of vectors to a basis.
  \end{enumerate}
\end{outcome}

\begin{definition}{Basis}{basis-vector-space}
  Let $V$ be a vector space. A set $B$ of vectors is called a
  \textbf{basis}%
  \index{basis}%
  \index{basis!of a vector space}%
  \index{vector space!basis} of $V$ if
  \begin{enumerate}
  \item $B$ is a spanning set for $V$, and
  \item $B$ is linearly independent.
  \end{enumerate}
\end{definition}

\begin{example}{Bases of $\Poly_2$}{basis-p2}
  Consider the vector space $\Poly_2$ of polynomials of degree at most
  2 with coefficients in a field $K$.%
  \index{P2@$\Poly_2$!basis of}
  \begin{itemize}
  \item $\set{1,~ x,~ x^2}$ is a basis of $\Poly_2$.
  \item $\set{x^2,~ (x+1)^2,~ (x+2)^2}$ is a basis of $\Poly_2$.
  \item $\set{1,~ x-1,~ (x-1)^2}$ is a basis of $\Poly_2$.
  \end{itemize}
  Unlike $\R^n$, a vector space like $\Poly_2$ does not
  necessarily have a ``standard'' basis. One basis might be useful for
  one application, and another basis for a different application.
\end{example}

\begin{proof}
  It is easy to verify that each set of vectors is linearly
  independent and spanning. See Examples~\ref{exa:spanning-set},
  {\ref{exa:linear-independence-polynomial}}, and
  {\ref{exa:polynomials-increasing-degree}} for similar calculations.
\end{proof}

\begin{example}{An infinite basis}{basis-p}
  Consider the vector space $\Poly$ of all polynomials with
  coefficients in a field $K$. The following is a basis for $\Poly$%
  \index{P@$\Poly$!basis of}:
  \begin{equation*}
    \set{1,~ x,~ x^2,~ x^3,~ x^4,~ \ldots}.
  \end{equation*}
  Note that this basis is infinite.
\end{example}

\begin{proof}
  The polynomials $1$, $x$, $x^2$, $x^3$, $x^4$, $\ldots$ are linearly
  independent by Proposition~\ref{prop:linear-dependence-redundant}.
  Namely, if they were linearly dependent, then one of the polynomials
  could be written as a linear combination of earlier ones. However,
  this is not possible because a polynomial of degree $n$ cannot be a
  linear combination of polynomials of degree less than $n$.

  To show that the polynomials $1$, $x$, $x^2$, $x^3$, $x^4$, $\ldots$
  are a spanning set, consider an arbitrary element $p(x)$ of $\Poly$.
  Then by definition, $p(x)$ is of the form
  \begin{equation*}
    p(x) = a_nx^n + a_{n-1}x^{n-1} + \ldots + a_1x + a_0,
  \end{equation*}
  for some $n\geq 0$ and $a_0,\ldots,a_n\in K$. But then $p(x)$ is a
  linear combination of $1,\ldots,x^n$, i.e., it is in the span of
  $\set{1,~ x,~ x^2,~ x^3,~ x^4,~ \ldots}$.
\end{proof}

\begin{example}{Not a basis}{not-a-basis}
  Consider the vector space $\Seq_K$ of infinite sequences. As before,
  let $e^k$ be the sequence whose $k\th$ element is $1$ and that is
  $0$ everywhere else, i.e.,
    \begin{equation*}
    \begin{array}{l}
      e^0 = (1,0,0,0,0,\ldots), \\
      e^1 = (0,1,0,0,0,\ldots), \\
      e^2 = (0,0,1,0,0,\ldots), \\
    \end{array}
  \end{equation*}
  and so on. Then the set
  \begin{equation*}
    \set{e^0,~ e^1,~ e^2,~ \ldots}
  \end{equation*}
  is \textit{not} a basis of $\Seq_K$. Indeed, although we saw in
  Example~\ref{exa:linear-independence-sequences} that the sequences
  $e^0$, $e^1$, $e^2$, $\ldots$ are linearly independent,
  Example~\ref{exa:spans-sequences} shows that they are not spanning.
  Indeed,
  \begin{equation*}
    W = \sspan\set{e^0,~ e^1,~ e^2,~ \ldots}
  \end{equation*}
  is a subspace of $\Seq_K$, consisting exactly of the
  \textbf{finitely supported}%
  \index{finitely supported sequence}%
  \index{sequence!finitely supported} sequences, i.e., those sequences
  that have only finitely many non-zero components. Thus, $\set{e^0,~ e^1,~
    e^2,~ \ldots}$ is a basis of $W$.
\end{example}

The following theorem ensures that every vector space has a basis.  We
will not prove this theorem, because when the spaces are
infinite-dimensional, the proof uses mathematics that is beyond the
scope of this book. The proof uses a reasoning principle called the
\textbf{axiom of choice}%
\index{axiom of choice}%
\index{choice!axiom of}, which allows us to prove the existence of a
basis even in cases where we cannot find an actual concrete example of
a basis.  For example, it is not possible to give a specific example
of a basis for the space $\Seq_K$, even though the following theorem
guarantees that such a basis exists.

\begin{theorem}{Existence of bases}{basis-existence}
  Every vector space has a basis.
\end{theorem}

The Exchange Lemma, which we proved in the context of $\R^n$ in
Section~\ref{sec:basis-and-dimension}, is true in general vector
spaces.

\begin{lemma}{Exchange Lemma}{exchange-lemma-vector-space}
  \index{exchange lemma!in a vector space}%
  Let $V$ be a vector space over a field $K$. Suppose
  $\vect{u}_1,\ldots,\vect{u}_r$ are linearly independent elements
  of $\sspan\set{\vect{v}_1,\ldots,\vect{v}_s}$. Then $r\leq s$.
\end{lemma}

The proof is exactly the same as that of
Lemma~\ref{lem:exchange-lemma}, so we do not repeat it here.  As in
Section~\ref{sec:basis-and-dimension}, an important consequence of the
Exchange Lemma is that any two bases of a vector space have the same
size.

\begin{theorem}{Bases are of the same size}{basis-same-size-vector-space}
  \index{basis!size of}%
  Let $V$ be a vector space over some field $K$, and let $B_1$ and
  $B_2$ be bases of $V$. Then either $B_1$ and $B_2$ are both finite
  and have the same number of elements, or else $B_1$ and $B_2$ are
  both infinite.
\end{theorem}

\begin{proof}
  We first show that $B_1$ and $B_2$ are either both finite or both
  infinite. Assume one of them, say $B_1$, is finite and contains $s$
  vectors. Since $B_1$ is spanning and $B_2$ is linearly independent,
  it follows from the Exchange Lemma that $B_2$ cannot contain more
  than $s$ vectors, and in particular, $B_2$ must be finite.  So the
  sets are either both finite or both infinite. If they are both
  finite, say of size $s$ and $r$, then by the Exchange Lemma, we have
  $s\leq r$ and $r\leq s$, hence $r=s$.
\end{proof}

This allows us to define the dimension of a vector space.

\begin{definition}{Dimension}{dimension-vector-space}
  Let $V$ be a vector space over a field $K$. If $V$ has a basis
  consisting of $n$ vectors, we say that $V$ has \textbf{dimension}%
  \index{dimension!of vector space}%
  \index{vector space!dimension} $n$, and we write $\dim(V)=n$. In
  this case we also say that $V$ is \textbf{finite-dimensional}%
  \index{finite-dimensional space}%
  \index{vector space!finite-dimensional}. If $V$ has an infinite
  basis, we say that $V$ is \textbf{infinite-dimensional}%
  \index{infinite-dimensional space}%
  \index{vector space!infinite-dimensional}, and we write
  $\dim(V) = \infty$.
\end{definition}

Note that the dimension is well-defined by Theorems
{\ref{thm:basis-existence}} and
{\ref{thm:basis-same-size-vector-space}}, since these theorems ensure
that every vector space has a basis (and therefore a dimension), and
that any two bases are of the same size (and therefore a vector space
cannot have more than one dimension).

We now calculate the dimensions of some vector spaces we encountered in
Sections~\ref{sec:definition-vector-spaces} and
{\ref{sec:vector-space-subspaces}}.

\begin{itemize}
\item The space $\R^n$ has dimension $n$.
\item The space $\Poly_2$ has dimension $3$. We found several bases
  for this space in Example~\ref{exa:basis-p2}.
\item The space $\Mat_{m,n}$ has dimension $mn$. A possible basis
  consists of all the matrices that contain a single $1$ and zeros
  everywhere else.
\item The space $\Func_{X,K}$ is infinite-dimensional if $X$ is an
  infinite set. If $X$ is a finite set of $n$ elements, then this
  space is $n$-dimensional. In that case, a basis is given by the set
  of functions whose value is $1$ for one input and $0$ for all other
  inputs.
\item The space $\Seq_K$ is infinite-dimensional. We found an infinite
  linearly independent set in
  Example~\ref{exa:linear-independence-sequences}, showing that the
  space cannot be finite-dimensional.
\item The space $\Poly$ is infinite-dimensional. We found a basis for
  this space in Example~\ref{exa:basis-p}.
\item The subspace of $\Func_{\R,\R}$ consisting of the continuous
  functions is infinite-dimensional. For example, the functions
  $\set{1,x,x^2,x^3,\ldots}$ form an infinite, linearly independent
  set of continuous functions.
\item The subspace of $\Func_{\R,\R}$ consisting of the differentiable
  functions is infinite-dimensional. Again, the set
  $\set{1,x,x^2,x^3,\ldots}$ is an infinite linearly independent set
  in this space.
\end{itemize}

\begin{example}{Space of sequences satisfying a linear recurrence}{subspace-recurrence-dimension}
  In Example~\ref{exa:subspace-recurrence}, we considered the space
  $W$ of sequences of real numbers that satisfy the recurrence%
  \index{recurrence!as a subspace}
  $a_{n+2}=a_n+a_{n+1}$. What is the dimension of this space?
\end{example}

\begin{solution}
  The space is $2$-dimensional. The easiest way to see this is to
  observe that a sequence $a\in W$ is determined by its first two
  elements. We can say that the first two elements of the sequence are
  parameters, and all the other elements are then computed by the
  recurrence relation. Specifically, suppose $a_0=x$ and
  $a_1=y$. Using the recurrence relation to compute the remaining
  elements, we have
  \begin{eqnarray*}
    a &=& (x,~y,~x+y,~x+2y,~2x+3y,~3x+5y,~\ldots) \\
      &=& x(1,0,1,1,2,3,\ldots) + y(0,1,1,2,3,5,\ldots).
  \end{eqnarray*}
  Since this is the general form of the elements of $W$, and since the
  two sequences starting with 1,0 and 0,1 are clearly linearly
  independent, it follows that
  \begin{equation*}
    \set{(1,0,1,1,2,3,\ldots),~ (0,1,1,2,3,5,\ldots)}
  \end{equation*}
  is a basis of $W$.
\end{solution}

\begin{example}{Solution space of a linear differential equation}{dimension-differential-equation}
  In Example~\ref{exa:subspace-differential-equation}, we considered
  the space of solutions of the differential equation $f''=-f$. What
  is the dimension of this space?
\end{example}

\begin{solution}
  From calculus, we know that the general solution of the differential
  equation $f''=-f$ is
  \begin{equation*}
    f(x) = A\sin x + B\cos x,
  \end{equation*}
  where $A,B$ are constants. We also know, from
  Example~\ref{exa:linearly-independent-functions}, that $\sin x$ and
  $\cos x$ are linearly independent. It follows that
  $\set{\sin x, \cos x}$ is a basis for the solution space. The
  solution space is therefore $2$-dimensional.
\end{solution}

We conclude this section by stating two properties of bases that
generalize Theorem~\ref{thm:linearly-independent-subset} and
Lemma~\ref{lem:extend-to-basis}: every linearly independent set can be
extended to a basis by adding 0 or more vectors, and every spanning
set can be reduced to a basis by removing 0 or more vectors.

\begin{proposition}{Extending a linearly independent set to a basis}{basis-from-linear-independent}
  Let $V$ be a vector space, and let $S\subseteq V$ be a linearly
  independent set of vectors. Then $S$ can be extended to a basis of
  $V$, i.e., there exists a basis $B$ of $V$ such that $S\subseteq
  B$.%
  \index{linear independence!extending to basis}%
  \index{basis!by extending linearly independent set}
\end{proposition}

\begin{proposition}{Shrinking a spanning set to a basis}{basis-from-spanning}
  \index{spanning set!shrink to basis}%
  \index{basis!by shrinking spanning set}%
  Let $V$ be a vector space, and let $S\subseteq V$ be a spanning set
  of $V$. Then $S$ can be shrunk to a basis, i.e., there exists a
  basis $B$ of $V$ such that $B\subseteq S$.
\end{proposition}

\begin{example}{Extending a linearly independent set to a basis}{adding-linear-independent-basis}
  Let $S \subseteq M_{22}$ be the linearly independent set given by
  \begin{equation*}
    S  = \set{
      \begin{mymatrix}{rr}
        1 & 0 \\
        0 & 0
      \end{mymatrix},~
      \begin{mymatrix}{rr}
        0 & 1 \\
        0 & 0
      \end{mymatrix}
    }
  \end{equation*}
  Enlarge $S$ to a basis of $M_{22}$.
\end{example}

\begin{solution}
  We can obtain a basis of $M_{22}$ by adding two more linearly
  independent matrices
  \begin{equation*}
    \begin{mymatrix}{rr}
      0 & 0 \\
      1 & 0
    \end{mymatrix}
    \quad\mbox{and}\quad
    \begin{mymatrix}{rr}
      0 & 0 \\
      0 & 1
    \end{mymatrix}.
  \end{equation*}
  The resulting basis is
  \begin{equation*}
    B = \set{
      \begin{mymatrix}{rr}
        1 & 0 \\
        0 & 0
      \end{mymatrix},~
      \begin{mymatrix}{rr}
        0 & 1 \\
        0 & 0
      \end{mymatrix},~
      \begin{mymatrix}{rr}
        0 & 0 \\
        1 & 0
      \end{mymatrix},~
      \begin{mymatrix}{rr}
        0 & 0 \\
        0 & 1
      \end{mymatrix}
    }.
  \end{equation*}
\end{solution}

\begin{example}{Shrinking a spanning set to a basis}{shrink-spanning}
  Consider the spanning set $S \subseteq \Poly_2$ given by
  \begin{equation*}
    S = \set{1,~ x,~ 2x+1,~ x^2+1,~ x^2+2}
  \end{equation*}
  Shrink $S$ to a basis of $\Poly_2$.
\end{example}

\begin{solution}
  We use a version of the casting-out method. We examine each element
  $S$ from left to right and cast out the elements that are linear
  combinations of previous elements. Clearly the first two elements,
  $1$ and $x$, are linearly independent. The next element, $2x+1$, is
  redundant because it is a linear combination of $1$ and $x$. The
  next element $x^2+1$ is linearly independent of $1$ and $x$. The
  final element $x^2+2$ is redundant because it is a linear
  combination of $1$ and $x^2+1$. Therefore, the following subset of
  $S$ is a basis of $\Poly_2$:
  \begin{equation*}
    B = \set{1,~ x,~ x^2+1}.
  \end{equation*}
\end{solution}


\section{The field equations of gravity}
The field equations of gravity are \cite{Einstein:1915ca}
\begin{equation}
	R_{\mu\nu}-\frac{1}{2}g_{\mu\nu}R + \Lambda g_{\mu\nu}= \frac{8\pi \symup{G}}{\symup{c}^4} T_{\mu\nu}.
\end{equation}

\subsection{Solutions}
The \emph{Schwarzschild} solution is:
\begin{equation}
	\dif s^2 = \l( 1 - \frac{r_{\symup{s}}}{r} \r) \symup{c}^2 \,\dif t^2 - \l(1-\frac{r_{\symup{s}}}{r}\r)^{-1} \,\dif r^2 - r^2 \l(\dif \Theta^2+\sin^2\Theta \dif \varphi^2\r).
\end{equation}
This is a vacuum solution ($T_{\mu\nu}=0$).


%!TEX root = index.tex

\section{The Discriminant}
\epigraph{In mathematics you don't understand things. You just get used to them.}{John von Neumann}

We'll start by analyzing polynomials using good old calculus. There is an algebraic invariant, called the \emph{discriminant}, that has a geometric interpretation coming from calculus on one hand and an algebraic interpretation coming from the properties of the roots of the polynomials on the other.\\

The \textbf{discriminant of a polynomial} is a number which can be computed using only the coefficients of the polynomial and which is 0 precisely when the polynomial has repeated roots. There is no reason, a priori, to expect that such an number should exist but it does, and you've encountered it already for the quadratic equation.

\subsection{Quadratic}

Let us start with the simplest case. Consider the quadratic polynomial $ x^2 + bx + c$ with roots $ \alpha_1$ and $ \alpha_2$.
\begin{questions}
  \item Express $ b, c$ in terms of $ \alpha_1, \alpha_2$.
\end{questions}
\begin{questions}[resume]
	\item What are the conditions on the coefficients $ b,c$ under which
	\begin{enumerate}
		\item $ \alpha_1 = \alpha_2$,
		\item $ \alpha_1, \alpha_2$ are both real,
		\item $ \alpha_1, \alpha_2$ are both non-real.
	\end{enumerate}
	Is it possible for exactly one of $ \alpha_1$ and $ \alpha_2$ to be real?
\end{questions}
Note that the answers to these questions depend on a single number. This number, denoted $ \Delta$ (delta), is called the \textbf{discriminant} of the quadratic polynomial.
	\begin{questions}[resume]
		  \item Express the discriminant $ \Delta$ in terms of $ \alpha_1, \alpha_2$.
	\end{questions}

\newpage
\subsection{Cubic}
Moving on to the next degree, consider the following cubic with roots $ \beta_1, \beta_2, \beta_3$.
\begin{align*}
	P(x) = x^3 + a_2 x^2 + a_1 x + a_0
\end{align*}
\begin{questions}
	\item Express $ a_2, a_1, a_0$ explicitly in terms of $ \beta_1, \beta_2, \beta_3$.
\end{questions}
We'll first get rid of the coefficient on $ x^2$ to simplify our computations.
\begin{questions}[resume]
	\item \begin{enumerate}
		\item Find the coefficients of the polynomial $ P_k(x)$ whose roots are $$ \beta_1 + k, \beta_2 + k, \beta_3 + k$$ where $ k$ is a constant, in terms of the coefficients of $ P(x)$.
		\item Find the value of $ k$ for which the coefficient of $ x^2$ in $ P_k(x)$ is 0.
	\end{enumerate}
	The value of $ k$ only depends on the coefficients of $ P(x)$ and hence we can simplify our polynomial \textbf{without knowing the roots}.
	\begin{enumerate}[resume]
		\item For the polynomial $ P(x) = x^3 + 3x^2 + 1$ use find the polynomial $ P_k(x)$ whose $ x^2$ coefficient is 0. What is the relationship between the roots of $ P(x)$ and $ P_k(x)$?
		\item Repeat the same exercise for the polynomial $ x^3 - 3x^2 + 3x - 1$.
	\end{enumerate}
\end{questions}

\newpage
\subsection{Graphing the cubic}
From now on we'll assume that our cubic is of the form:
\begin{align}
	P(x) = x^3 + px + q
\end{align}
We'll further assume $ p < 0$ for simplicity. Such a cubic is sadly called a \textbf{depressed cubic}. With roots $ \beta_1, \beta_2, \beta_3$ we've the relations
\begin{align*}
	0  & = \beta_1 + \beta_2 + \beta_3                    \\
	p  & = \beta_1 \beta_2 + \beta_2 \beta_3 + \beta_3\beta_1 \\
	-q & = \beta_1 \beta_2 \beta_3
\end{align*}
There are four possible cases for the roots.
\begin{center}
	\begin{tabular}{rl}
		\textbf{Case 0} & all three roots equal                     \\
		\textbf{Case 1} & all three roots distinct and real                   \\
		\textbf{Case 2} & one repeated real root  \\
		\textbf{Case 3} & two complex roots
	\end{tabular}
\end{center}

\begin{questions}[resume]
	\item \begin{enumerate}
		\item Convince yourselves that these cases are mutually exclusive and that are no other possibilities. Can $ P(x)$ have no real root? Can $ P(x)$ have a repeated complex root? all three roots complex (not real)?
		\item Case 0 is trivial, what are the roots in this case? What are $ p,q$ in this case?
	\end{enumerate}
\end{questions}
\begin{questions}[resume]
	\item Draw the (qualitatively correct) graphs of $ P(x)$ in each of the four cases.
\end{questions}
\begin{questions}[resume]
	\item By analyzing the graph of $ P(x)$, determine the relationship between the roots of $P(x)$ and $ P'(x)$ for each of the three cases: Case 1 (distinct real), Case 2 (repeated real), Case 3 (complex).
\end{questions}

Each of these cases are in fact completely determined by the coefficients $ p,q$. This is because $ P'(x)$ is a friendly quadratic equation whose roots are easy to find.
\begin{questions}[resume]
  \item \begin{enumerate}
  	\item Find the roots of $ P'(x)$.
		\item Use your answers to the previous questions to determine the conditions on the coefficients $ p,q$ corresponding to the three cases.
  \end{enumerate}
\end{questions}

Assuming you did the calculations correctly you should get conditions of the following form for some number $ \Delta$.
\begin{align*}
	\Delta > 0 & \implies P(x) \mbox{ has three real roots} \\
	\Delta = 0 & \implies P(x) \mbox{ has a repeated root}   \\
	\Delta < 0 & \implies P(x) \mbox{ has exactly one real root}
\end{align*}
These are exactly the relations we had for a quadratic!

\newpage
\subsection{The Discriminant of the Cubic}
By the previous section the roots of our polynomial are distinct real, repeated or complex according to whether the value of the polynomial is -ve, 0 or +ve at the roots of it's derivative $ \sqrt {{-p}/{3}}$, or the opposite for $ -\sqrt {{-p}/{3}}$.

\begin{questions}[resume]
  \item Simplify the condition $$P \left(\sqrt {{-p}/{3}}\right)= 0$$ to the form $ -4p^3 - 27q^2 = 0$. Similar simplifications are possible for the conditions $P (\sqrt {{-p}/{3}}) < 0$ and $> 0$.
\end{questions}

Using the other root $ -\sqrt {{-p}/{3}}$ we surprisingly get the \emph{exact} same conditions. Thus we've (almost) proved the following theorem.
\begin{thm}
	The cubic $ P(x) = x^3 + px + q$ has distinct real, repeated, or complex roots, according to whether the \textbf{discriminant} of $ P(x)$ $$ \Delta = -4p^3 - 27q^2$$ is positive, 0 or negative.
\end{thm}

\begin{questions}[resume]
  \item Determine whether the following polynomials have distinct real, repeated or complex roots.
	\begin{enumerate}
		\item $ x^3 + 1$
		\item $ x^3 - 1$
		\item $ x^3 - 3x + 2 $
		\item $ x^3 - 3x + 1 $
		\item (optional) $x^3 + 3x^2 + 1$
	\end{enumerate}
\end{questions}

\begin{questions}[resume]
	\item \begin{enumerate}
		\item What is the value of $ q$ for which $ x^3 -3x + q$ has distinct real roots, repeated roots, and complex roots respectively.
		\item In the case when $ x^3 - 3x + q$ has repeated roots, find the roots.
	\end{enumerate}
\end{questions}

\newpage
\subsection{The Roots \& the Discriminant}
As in the case of a quadratic $\left( b^2 - 4c = (\alpha_1 - \alpha_2)^2 \right)$ there is a surprising relation between $ \Delta$ and the roots $ \beta_1, \beta_2, \beta_3$.

\begin{thm}
\label{thm:discriminant}
	\begin{align*}
		-4p^3 - 27q^2 = (\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2
	\end{align*}
\end{thm}

The proof of this theorem is very cumbersome, we'll only verify it for some cases.
\begin{questions}[resume]
	\item
	\begin{enumerate}
		\item Argue directly that
		\begin{align*}
			P(x) \mbox{ has distinct real roots} & \Rightarrow  (\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2 > 0\\
			 P(x) \mbox{ has a repeated root}  & \Rightarrow  (\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2 = 0
		\end{align*}
	\item For the complex roots case, assume that $ \beta_1$ is real and, $ \beta_2 $ and $ \beta_3$ are complex. Show that
		\begin{align*}
			\beta_2 = -\beta_1/2 + i \beta' \mbox{ and } \beta_2 = -\beta_1/2 - i \beta'
		\end{align*}
		for some real number $ \beta'$. Prove that in this case
		\begin{align*}
		  (\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2 < 0
		\end{align*}
	\end{enumerate}
\end{questions}

\begin{questions}[resume]
	\item For $ \beta_1 = 0$, $\beta_2 = 1$, $\beta_3 = -1$,
	\begin{enumerate}
		\item Compute $(\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2$.
		\item Compute the coefficients $ p,q$ of the polynomial $ P(x) = (x - \beta_1)(x - \beta_2)(x - \beta_3)$.
		\item Compute $-4p^3 - 27q^2$.
	\end{enumerate}
\end{questions}

\begin{questions}[resume]
  \item (optional) If you're feeling ambitious prove Proposition \ref{thm:discriminant} by expanding and simplifying the left and the right hand sides.
\end{questions}

\newpage
\subsection{Symmetries \& the Discriminant}

There are generalizations of \ref{thm:discriminant} for all degrees i.e. we can always express ``the product of the squares of pairwise differences of roots'' as a polynomial in the coefficients.

The coefficients satisfy the equations
\begin{align*}
	0  & = \beta_1 + \beta_2 + \beta_3                    \\
	p  & = \beta_1 \beta_2 + \beta_2 \beta_3 + \beta_3\beta_1 \\
	-q & = \beta_1 \beta_2 \beta_3
\end{align*}
The right hand sides of these equations are called \textbf{elementary symmetric polynomials} (a symmetric polynomial is a multivariable polynomial which remains unchanged if we permute the $\beta_i$'s). They are called \emph{elementary} because of the following theorem.

\begin{thm}[Fundamental Theorem of Symmetric Polynomials]
	Every symmetric polynomial can be expressed uniquely as a polynomial in the elementary symmetric ones.
\end{thm}

This theorem is surprisingly easy to prove once you know your induction well. Try the next problem to get an idea of how the proof of this theorem goes in general.
\begin{questions}[resume]
	\item
	\begin{enumerate}
		\item Express $ \beta_1^2 + \beta_2^2 + \beta_3^2$ in terms of $ p,q$. \hint{Expand $(\beta_1 + \beta_2 + \beta_3)^2$.}

		\item Why is the expression $ \beta_1^2 \beta_2 + \beta_2^2 \beta_3  + \beta_3^2 \beta_1 $ not symmetric? What terms can you add to it to make it symmetric. Express the resulting polynomial in terms of $ p,q$. \hint{Expand $(\beta_1 + \beta_2 + \beta_3)(\beta_1 \beta_2 + \beta_2 \beta_3 + \beta_3\beta_1)$}
	\end{enumerate}
\end{questions}

The discriminant $(\beta_1 - \beta_2)^2(\beta_2 - \beta_3)^2(\beta_3 - \beta_1)^2$ is also symmetric in $ \beta_1, \beta_2, \beta_3$ (do you see why?) and hence can be written as a polynomial in the elementary symmetric ones, which turns out to be $-4p^3 - 27q^2$.\\

 This is the simplest connection between symmetries and polynomials. Galois' insight involved studying polynomials with \emph{fewer} symmetries.


\documentclass{article}
\usepackage{graphicx}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{geometry}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage{float}
\usepackage{cite}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{titletoc}
\usepackage{booktabs}

\geometry{left=3cm,right=3cm,top=3cm,bottom=3cm}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\logit}{logit}
\DeclareMathOperator*{\var}{var}
\DeclareMathOperator*{\cov}{cov}
\DeclareMathOperator*{\expec}{E}
\DeclareMathOperator*{\deriv}{d}
\DeclareMathOperator*{\const}{constant}

\begin{document}
\title{\textsf{Homework 8 for Bayesian Data Analysis}}
\author{Fan JIN\quad (2015011506)}
\maketitle

\section*{Question 11.3}
{
    \subsection*{Separate model and Pooled model:}
    {
        With the noninformative prior distribution, uniform for $(\theta, \log{\sigma})$,
        $$y_j | \theta, \sigma^2 \sim \mathrm{N}(\theta, \sigma^2),$$
        $$p(\theta, \sigma^2) \propto \sigma^{-2}.$$

        The conditional distributions are
        $$p(\theta | \sigma^2) \propto 1,$$
        $$p(\sigma^2 | \theta) \propto \sigma^{-2}.$$

        It follows that
        $$p(\theta | \sigma^2, y) \propto 1 \cdot \mathrm{N}(\bar{y}, \sigma^2/n) \propto \exp{\left( -\frac{n}{2\sigma^2} (\bar{y}-\theta)^2 \right)},$$
        $$p(\sigma^2 | \theta, y) \propto \sigma^{-2-n} \cdot \exp{\left( -\frac{1}{2\sigma^2} \left[ (n-1)s^2 + n(\bar{y}-\theta)^2 \right] \right)}.$$

        \begin{itemize}

        \item For the separate model, plug in $y$ for the sixth machine only.
        We can calculate the posterior distribution $\theta | y$ by simulation, using Metropolis-within-Gibbs on the two conditional distributions above.
        \\ But we have no idea about the seventh machine, for each machine has its separate parameters.
        Therefore, we cannot obtain the predictive distributions or the posterior mean for the seventh machine.

        \item For the pooled model, plug in $y$ for all the six machines.
        We can calculate the posterior distribution $\theta | y$ by simulation, using Metropolis-within-Gibbs on the two conditional distributions above.
        \\ And we use this posterior distribution to predict the seventh machine, for the machines have pooled parameters.

        \end{itemize}
    }

    \subsection*{Hierarchical model:}
    {
        The four conditional distributions are given on p.289 of textbook:

        \begin{itemize}

        \item $$\theta_j | \mu, \sigma, \tau, y \sim \mathrm{N}(\hat{\theta}_j, V_{\theta_j}),$$ where
        $$\hat{\theta}_j = \frac{ \frac{1}{\tau^2}\mu + \frac{n_j}{\sigma^2}\bar{y}_{.j} }{ \frac{1}{\tau^2} + \frac{n_j}{\sigma^2} },$$
        $$V_{\theta_j} = \frac{1}{ \frac{1}{\tau^2} + \frac{n_j}{\sigma^2} }.$$

        \item $$\mu | \theta, \sigma, \tau, y \sim \mathrm{N}(\hat{\mu}, \tau^2/J),$$ where
        $$\hat{\mu} = \frac{1}{J} \sum_{j=1}^{J} \theta_j.$$

        \item $$\sigma^2 | \theta, \mu, \tau, y \sim \mathrm{Inv-}\chi^2 (n, \hat{\sigma}^2),$$ where
        $$n = \sum_{j=1}^{J} n_j,$$
        $$\hat{\sigma}^2 = \frac{1}{n} \sum_{j=1}^{J} \sum_{i=1}^{n_j} (y_{ij} - \theta_j)^2.$$

        \item $$\tau^2 | \theta, \mu, \sigma, y \sim \mathrm{Inv-}\chi^2 (J-1, \hat{\tau}^2),$$ where
        $$\hat{\tau}^2 = \frac{1}{J-1} \sum_{j=1}^{J} (\theta_j - \mu)^2.$$

        \end{itemize}

        Use Metropolis-within-Gibbs on the four conditional distributions above, we can draw $\theta_j$, $\mu$, $\sigma^2$, $\tau^2$ by simulation.
        The posterior distribution $\theta_j | y$ can be obtained immediately.
        Then, we obtain $\mu | y$ and $\tau^2 | y$, which then give $\theta_7 | y$.
        Finally, the posterior distribution $y_{i7} | y$ can be estimated, given $\sigma^2 | y$ obtained.
    }

    \subsection*{Reports}
    {
        Source code: ``src1.R''

        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_mean_separate.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_mean_pooled.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_mean_hierarchical.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_predictive_pooled.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_predictive_hierarchical.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_predictive_mean_pooled.png}
            \caption{}
        \end{figure}
        \begin{figure}[H]
            \centering
            \includegraphics[width = 0.8\linewidth]{posterior_predictive_mean_hierarchical.png}
            \caption{}
        \end{figure}
    }
}

\section*{Random-walk Metroplis}
{
    Source code: ``src2.R''

    To make the adaptive tune of $\alpha$ and $\beta$ at each iteration, according to the empirical formula. For the first 20 iterations, we do not change the hyperparamters.

    \begin{lstlisting}[language=R]
    # Adapt
    if (i > 20) {
      tune$alpha = 2.4^2 * var(alpha_keep[1:i]) / 2
      tune$beta = 2.4^2 * var(beta_keep[1:i]) / 2
    }
    \end{lstlisting}

    The adaptived hyperparamters converge to $\alpha=0.04629154$ and $\beta=1.906087$.
    \begin{lstlisting}[language=R]
        $alpha
        [1] 0.04629154

        $beta
        [1] 1.906087
    \end{lstlisting}
}

\clearpage
\end{document}