\documentclass[twoside]{homework}

\usepackage{dsfont}

\usepackage{graphicx}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{xcolor}
\lstset{
    rulesepcolor= \color{gray}, %代码块边框颜色
    breaklines=true,  %代码过长则换行
    numbers=left, %行号在左侧显示
    numberstyle= \small,%行号字体
    %keywordstyle= \color{red},%关键字颜色
    commentstyle=\color{gray}, %注释颜色
    frame=shadowbox%用方框框住代码块
}

\studname{Chenye Yang}
\uni{cy2540}
\studmail{cy2540@columbia.edu}
\coursename{Statistical Learning for Biological and Information Systems}
\hwNo{1}

\begin{document}
\maketitle

\section*{P1}

\subsection*{(a)}
\textbf{Ans:}\\
\begin{equation}
\begin{aligned}
(n-1)S^2+n\bar{X}^2 &= \sum_{i=1}^{n} (X_i-\bar{X})^2 + n\bar{X}^2\\
&= \sum_{i=1}^{n}X_i^2 - 2\bar{X} \sum_{i=1}^{n} X_i +2n\bar{X}^2\\
&= \sum_{i=1}^{n}X_i^2 - 2\bar{X} n \bar{X} +2n\bar{X}^2\\
&= \sum_{i=1}^{n}X_i^2
\end{aligned}
\end{equation}

\subsection*{(b)}
\textbf{Ans:}\\
Because $X_1, X_2, ..., X_n$ are independent and identically distributed, using $a$ as the expected value of the distribution, we have $\mathbb{E}(X_i)=\mathbb{E}(\bar{X})=a$. \\
Thus:
\begin{equation}
\begin{aligned}
    \sum_{i=1}^n (X_i - \bar{X})^2 &= \sum_{i=1}^n [(X_i - a)-(\bar{X}-a)]^2 \\
    &=  \sum_{i=1}^n [(X_i - a)^2 -2(X_i - a)(\bar{X}-a) + (\bar{X}-a)^2] \\
    &= \sum_{i=1}^n (X_i - a)^2 - 2 (\bar{X}-a)(\sum_{i=1}^n X_i - \sum_{i=1}^n a) + n(\bar{X}-a)^2\\
    &= \sum_{i=1}^n (X_i - a)^2 - 2(\bar{X}-a)(n\bar{X}-na)+n(\bar{X}-a)^2\\
    &= \sum_{i=1}^n (X_i - a)^2 - n(\bar{X}-a)^2
\end{aligned}{}
\end{equation}{}
Because:
\begin{equation}
\begin{aligned}
    &\mathbb{E}[X_i - \mathbb{E}(X_i)]^2 = \mathrm{Var}(X_i) = \sigma ^2 \\
    &\mathbb{E}[\bar{X} - \mathbb{E}(\bar{X})]^2 = \mathrm{Var}(\bar{X}) = \sum_{i=1}^n \mathrm{Var}(X_i)/n^2 = \sigma ^2 / n
\end{aligned}
\end{equation}{}
Thus:
\begin{equation}
\begin{aligned}
    \mathbb{E}S^2 &= \mathbb{E} [\frac{1}{n-1} \sum_{i=1}^n(X_i - \bar{X})^2] = \frac{1}{n-1} \mathbb{E} [\sum_{i=1}^n (X_i - a)^2 - n(\bar{X}-a)^2]\\
    &= \frac{1}{n-1} (n \sigma ^2 - \sigma ^2) = \sigma ^2
\end{aligned}
\end{equation}
Namely, the $S^2$ is an unbiased estimator of $\sigma ^2$

\subsection*{(c)}
\textbf{Ans:}\\
Because $X_i \thicksim N(\mu, \sigma ^2)\  (i=1,2,...,n)$ and $X_1, X_2, ..., X_n$ are i.i.d.\\
Therefore, $\bar{X}$ and $X_i-\bar{X}\  (i=1,2,...,n)$ have normal/Gaussian distribution, and we have:

\begin{equation}
\begin{aligned}
    &\mathbb{E}(\bar{X}) = \mathbb{E} (\frac{1}{n} \sum_{i=1}^n X_i) = \frac{1}{n} \sum_{i=1}^n \mathbb{E}(X_i)= \mu \\
    &\mathrm{Var}(\bar{X}) = \mathrm{Var} (\frac{1}{n} \sum_{i=1}^n X_i) = \frac{1}{n^2} \sum_{i=1}^n \mathrm{Var}(X_i) = \frac{1}{n} \sigma ^2\\
    &\mathbb{E}(X_i - \bar{X}) = \mathbb{E}(X_i) - \mathbb{E}(\bar{X}) = \mu - \mu = 0\\
    &\mathrm{Var}(X_i - \bar{X}) = \mathrm{Var}(X_i) - \mathrm{Var}(\bar{X}) = \sigma^2 - \frac{1}{n} \sigma ^2 = \frac{n-1}{n} \sigma ^2
\end{aligned}
\end{equation}
Thus:
\begin{equation}
\begin{aligned}
\mathrm{Cov}(\bar{X}, X_i - \bar{X})&=\mathbb{E}[(\bar{X}-\mathbb{E}(\bar{X}))((X_i - \bar{X})-\mathbb{E}(X_i - \bar{X}))]\\
&=\mathbb{E}[(\bar{X}-\mu)(X_i - \bar{X})]\\
&=\mathbb{E}[\bar{X}X_i-\bar{X}^2-\mu X_i+\mu \bar{X}]\\
&=\mathbb{E}(\bar{X}X_i)-\mathbb{E}(\bar{X}^2)-\mu^2+\mu^2\\
&=\mathbb{E}(\bar{X}X_i)-[\mathrm{Var}(\bar{X})+\mu^2]\\
&=\mathbb{E}(\bar{X}X_i)-(\frac{\sigma^2}{n}+\mu^2)
\end{aligned}
\end{equation}
Because:
\begin{equation}
\begin{aligned}
\mathbb{E}(\bar{X}X_i) &= \mathbb{E}(X_i \frac{1}{n} \sum_{j=1}^{n} X_j ) = \frac{1}{n} \mathbb{E} (\sum_{j=1}^{n} X_j X_i)\\
&= \frac{1}{n} \mathbb{E} [\sum_{j=1,j\neq i}^{n} (X_j X_i) + X_i^2]\\
&= \frac{1}{n} [\sum_{j=1,j\neq i}^{n} \mathbb{E}(X_j)\mathbb{E}(X_i) + \mathbb{E}(X_i^2)]\\
&= \frac{1}{n} [(n-1)\mu^2+\mathrm{Var}(X_i)+\mu^2]\\
&= \mu^2 + \frac{1}{n} \sigma^2
\end{aligned}
\end{equation}
Therefore:
\begin{equation}
\begin{aligned}
&\mathrm{Cov}(\bar{X}, X_i - \bar{X}) = (\mu^2 + \frac{1}{n} \sigma^2) - (\frac{\sigma^2}{n}+\mu^2) = 0\\
&\mathrm{Corr}(\bar{X}, X_i - \bar{X}) = \mathrm{Cov}(\bar{X}, X_i - \bar{X})/(\sqrt{\frac{1}{n} \sigma ^2}\sqrt{\frac{n-1}{n} \sigma ^2}) = 0
\end{aligned}
\end{equation}
Considering $\bar{X}$ and $X_i-\bar{X}\  (i=1,2,...,n)$ have normal/Gaussian distribution, thus $\bar{X}$ is independent of $X_i-\bar{X}\  (i=1,2,...,n)$.

\subsection*{(d)}
\textbf{Ans:}\\
Let $Z_i = \frac{X_i -\mu}{\sigma}\ i=1,2,...,n$, thus $Z_1, Z_2,..., Z_n$ are independent and $Z_i\thicksim N(0,1)\ i=1,2,...,n$. Also:\\
\begin{equation}
    \begin{aligned}
    \bar{Z} &= \frac{1}{n}\sum_{i=1}^{n} Z_i = \frac{1}{n}\sum_{i=1}^{n} \frac{X_i -\mu}{\sigma} = \frac{\bar{X}-\mu}{\sigma}\\
    \frac{(n-1)S^2}{\sigma^2} &= \frac{\sum_{i=1}^{n} (X_i - \bar{X})^2}{\sigma^2} = \sum_{i=1}^{n} [\frac{ (X_i - \mu) - (\bar{X} - \mu)}{\sigma }]^2\\
    &= \sum_{i=1}^{n} (Z_i - \bar{Z})^2 = \sum_{i=1}^{n} (Z_i^2 - 2Z_i\bar{Z} + \bar{Z}^2)\\
    &= \sum_{i=1}^{n}Z_i^2 - 2\bar{Z}\sum_{i=1}^{n}Z_i + n\bar{Z}^2 = \sum_{i=1}^{n}Z_i^2 - n\bar{Z}^2
    \end{aligned}
\end{equation}
Choose a matrix $\bm{A} = (a_{ij})$, in which all the entries in the first row are $1/\sqrt{n}$, and each row vector of the matrix should be orthogonal. Do:\\
$$\bm{Y} = \bm{AZ}$$
In which $$\bm{Y} = (Y_1, Y_2,...,Y_n)^T$$ $$\bm{Z} = (Z_1, Z_2,...,Z_n)^T$$ $$\bm{A} = (\bm{a_1}^T, \bm{a_2}^T,...,\bm{a_n}^T)^T$$
Because $Y_i$ is the linear combination of $Z_i$ and $Z_i\thicksim N(0,1)\ i=1,2,...,n$, therefore $Y_i\ i=1,2,...,n$ have normal/Gaussian distribution and:
\begin{equation}
\begin{aligned}
\mathbb{E}(Y_i) &= \mathbb{E}(\sum_{j=1}^{n} a_{ij}Z_j) = \sum_{j=1}^{n} a_{ij}\mathbb{E}(Z_j) = 0\\
\mathrm{Var}(Y_i) &= \mathrm{Var}(\sum_{j=1}^{n} a_{ij}Z_j) = \sum_{j=1}^{n} a_{ij}^2 = <\bm{a_i}, \bm{a_i}> = 1
\end{aligned}
\end{equation}\\
Because $\mathrm{Cov}(Z_k, Z_h)$ equals 1 when $k=h$ and equals 0 when $k\neq h$:
\begin{equation}
    \begin{aligned}
    \mathrm{Cov}(Y_i, Y_j) &= \mathrm{Cov}(\sum_{k=1}^{n} a_{ik}Z_k, \sum_{h=1}^{n} a_{jh}Z_h) \\
    &= \sum_{k=1}^{n} \sum_{h=1}^{n} a_{ik} a_{jh} \mathrm{Cov}(Z_k, Z_h) \\
    &= \sum_{k=1}^{n} a_{ik} a_{jk}\\
    &= <\bm{a_i}, \bm{a_j}>\\
    &=
    \left\{
             \begin{aligned}
             1\qquad&, i=j \\
             0\qquad&, i\neq j
             \end{aligned}
    \right. \\
    &= \mathrm{Cov}(Z_i, Z_j)
    \end{aligned}
\end{equation}\\
Therefore, $Y_i\ i=1,2,...,n$ has no correlation with each other. Considering $Y_i \thicksim N(0,1)$, thus $Y_1, Y_2,..., Y_n$ are independent.
Also:\\
\begin{equation}
\begin{aligned}
&Y_1 = \sum_{j=1}^{n} a_{1j}Z_j = \sum_{j=1}^{n}\frac{1}{\sqrt{n}}Z_j = \sqrt{n}\bar{Z}\\
&\sum_{i=1}^{n}Y_i^2 = \bm{Y}^T\bm{Y} = (\bm{AZ})^T(\bm{AZ}) = \bm{Z}^T (\bm{A} \bm{A}^T)^T \bm{Z} = \bm{Z}^T \bm{I} \bm{Z} = \sum_{i=1}^{n}Z_i^2
\end{aligned}
\end{equation}\\
Therefore:
\begin{equation}
\begin{aligned}
\frac{(n-1)S^2}{\sigma^2} = \sum_{i=1}^{n}Z_i^2 - n\bar{Z}^2 = \sum_{i=1}^{n}(Y_i^2) - Y_1^2 = \sum_{i=2}^{n}(Y_i^2)
\end{aligned}
\end{equation}\\
Because $\bar{X} = \sigma \bar{Z} + \mu = \frac{\sigma}{\sqrt{n}}Y_1 + \mu$ only depends on $Y_i$ and $S^2 = \frac{\sigma^2}{n-1} \sum_{i=2}^{n}(Y_i^2)$ only depends on $Y_2, Y_3,...,Y_n$, also $Y_1, Y_2,..., Y_n$ are independent, therefore, the sample mean $\bar{X}$ is independent of the sample variance $S^2$.

\newpage

\section*{P2}
\textbf{Ans:}\\
For simple linear regression, we have $Y \approx \beta_0 + \beta_1 X$, in which the value of $\beta_0$ and $\beta_1$ to minimize the Residual Sum of Squares (RSS) is as follow:\\
\begin{equation}
    \begin{aligned}
    &\hat{\beta}_1 = \frac{\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^n (x_i-\bar{x})^2} = \frac{\mathrm{Cov}(X, Y)}{\mathrm{Var}(X)}\\
    &\hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x}
    \end{aligned}
\end{equation}{}
Considering:\\
\begin{equation}
    \begin{aligned}
    \sum_{i=1}^n (y_i-\hat{y}_i)^2 &= \sum_{i=1}^n (y_i - \hat{\beta}_0 - \hat{\beta}_1 x_i)^2\\
    &= \sum_{i=1}^n (y_i - \bar{y} + \hat{\beta}_1 \bar{x} - \hat{\beta}_1 x_i)^2\\
    &= \sum_{i=1}^n [y_i - \bar{y} - \hat{\beta}_1 (x_i - \bar{x})]^2\\
    &= \sum_{i=1}^n [(y_i - \bar{y})^2 - 2 \hat{\beta}_1 (y_i - \bar{y}) (x_i - \bar{x}) + \hat{\beta}_1^2 (x_i - \bar{x})^2]\\
    &= \sum_{i=1}^n (y_i - \bar{y})^2 - 2 \hat{\beta}_1 \sum_{i=1}^n (y_i - \bar{y}) (x_i - \bar{x}) + \hat{\beta}_1^2 \sum_{i=1}^n (x_i - \bar{x})^2
    \end{aligned}
\end{equation}{}
Thus:\\
\begin{equation}
    \begin{aligned}
    R^2 &= 1 - \frac{\sum_{i=1}^n (y_i-\hat{y}_i)^2}{\sum_{i=1}^n (y_i-\bar{y})^2}\\
    &= 1 - \frac{\sum_{i=1}^n (y_i - \bar{y})^2 - 2 \hat{\beta}_1 \sum_{i=1}^n (y_i - \bar{y}) (x_i - \bar{x}) + \hat{\beta}_1^2 \sum_{i=1}^n (x_i - \bar{x})^2}{\sum_{i=1}^n (y_i-\bar{y})^2}\\
    &= 1 - 1 + 2 \hat{\beta}_1 \frac{\sum_{i=1}^n (y_i - \bar{y}) (x_i - \bar{x})}{\sum_{i=1}^n (y_i-\bar{y})^2} - \hat{\beta}_1^2 \frac{\sum_{i=1}^n (x_i - \bar{x})^2}{\sum_{i=1}^n (y_i-\bar{y})^2}\\
    &= 2 \hat{\beta}_1 \frac{\mathrm{Cov}(X,Y)}{\mathrm{Var}(Y)} - \hat{\beta}_1^2 \frac{\mathrm{Var}(X)}{\mathrm{Var}(Y)}
    \end{aligned}
\end{equation}{}
Replace $\hat{\beta}_1$ in the equation:\\
\begin{equation}
    \begin{aligned}
    R^2 &= 2 \frac{\mathrm{Cov}(X, Y)}{\mathrm{Var}(X)} \frac{\mathrm{Cov}(X,Y)}{\mathrm{Var}(Y)} - [\frac{\mathrm{Cov}(X, Y)}{\mathrm{Var}(X)}]^2 \frac{\mathrm{Var}(X)}{\mathrm{Var}(Y)}\\
    &= \frac{\mathrm{Cov}(X,Y)^2}{\mathrm{Var}(X) \mathrm{Var}(Y)}\\
    &= r^2
    \end{aligned}
\end{equation}{}\\
Therefore, in the case of simple linear regression of $Y$ onto $X$, the $R^2$ statistic is equal to the square of the correlation coefficient between $X$ and $Y$ ($r^2$).

\newpage

\section*{P3}
\subsection*{(a)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> set.seed(1)
> x=rnorm(100, mean=0, sd=1)
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
x
  [1] -0.626453811  0.183643324 -0.835628612  1.595280802  0.329507772
  [6] -0.820468384  0.487429052  0.738324705  0.575781352 -0.305388387
 [11]  1.511781168  0.389843236 -0.621240581 -2.214699887  1.124930918
 [16] -0.044933609 -0.016190263  0.943836211  0.821221195  0.593901321
 [21]  0.918977372  0.782136301  0.074564983 -1.989351696  0.619825748
 [26] -0.056128740 -0.155795507 -1.470752384 -0.478150055  0.417941560
 [31]  1.358679552 -0.102787727  0.387671612 -0.053805041 -1.377059557
 [36] -0.414994563 -0.394289954 -0.059313397  1.100025372  0.763175748
 [41] -0.164523596 -0.253361680  0.696963375  0.556663199 -0.688755695
 [46] -0.707495157  0.364581962  0.768532925 -0.112346212  0.881107726
 [51]  0.398105880 -0.612026393  0.341119691 -1.129363096  1.433023702
 [56]  1.980399899 -0.367221476 -1.044134626  0.569719627 -0.135054604
 [61]  2.401617761 -0.039240003  0.689739362  0.028002159 -0.743273209
 [66]  0.188792300 -1.804958629  1.465554862  0.153253338  2.172611670
 [71]  0.475509529 -0.709946431  0.610726353 -0.934097632 -1.253633400
 [76]  0.291446236 -0.443291873  0.001105352  0.074341324 -0.589520946
 [81] -0.568668733 -0.135178615  1.178086997 -1.523566800  0.593946188
 [86]  0.332950371  1.063099837 -0.304183924  0.370018810  0.267098791
 [91] -0.542520031  1.207867806  1.160402616  0.700213650  1.586833455
 [96]  0.558486426 -1.276592208 -0.573265414 -1.224612615 -0.473400636
\end{lstlisting}

\subsection*{(b)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> eps=rnorm(100, mean=0, sd=0.25)
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
eps
  [1] -0.155091669  0.010528968 -0.227730412  0.039507193 -0.163646161
  [6]  0.441821817  0.179176869  0.227543557  0.096046339  0.420544020
 [11] -0.158934113 -0.115411183  0.358070560 -0.162674088 -0.051845186
 [16] -0.098201982 -0.079998217 -0.069778326  0.123547083 -0.044332621
 [21] -0.126489366  0.335759706 -0.053644852 -0.044889133 -0.025047685
 [26]  0.178166577 -0.018391101 -0.009408543 -0.170415120 -0.081067568
 [31]  0.015040110 -0.147223622  0.132874048 -0.379598520  0.076639465
 [36] -0.384112456 -0.075244032 -0.132069976 -0.163023695 -0.014224194
 [41] -0.478589856  0.294145828 -0.416243109 -0.115882600 -0.278980026
 [46] -0.187704750  0.521791636  0.004348905 -0.321575133 -0.410151384
 [51]  0.112546775 -0.004639958 -0.079517094 -0.232340537 -0.371865078
 [56] -0.268798074  0.250007201 -0.155316674 -0.346106712  0.467322656
 [61]  0.106275094 -0.059661775  0.264620762  0.221605663 -0.154810762
 [66]  0.551525616 -0.063756758 -0.356123663 -0.036099900  0.051884585
 [71]  0.576994600  0.026450592  0.114249701 -0.019288234 -0.083500211
 [76] -0.008681507  0.196909901  0.518811252  0.256848110  0.301977100
 [81] -0.307830855  0.245973893  0.054981201 -0.366812507  0.130255686
 [86] -0.039688651  0.366146828 -0.191520500 -0.107552938 -0.231527374
 [91] -0.044275990  0.100502945 -0.182937043  0.207593292 -0.302020697
 [96] -0.261996103  0.360289427 -0.253961866  0.102993678 -0.095269013
\end{lstlisting}

\subsection*{(c)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> y = -1 + 0.5*x + eps
> length(y)
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
y
  [1] -1.4683186 -0.8976494 -1.6455447 -0.1628524 -0.9988923 -0.9684124 -0.5771086
  [8] -0.4032941 -0.6160630 -0.7321502 -0.4030435 -0.9204896 -0.9525497 -2.2700240
 [15] -0.4893797 -1.1206688 -1.0880933 -0.5978602 -0.4658423 -0.7473820 -0.6670007
 [22] -0.2731721 -1.0163624 -2.0395650 -0.7151348 -0.8498978 -1.0962889 -1.7447847
 [29] -1.4094901 -0.8720968 -0.3056201 -1.1986175 -0.6732901 -1.4065010 -1.6118903
 [36] -1.5916097 -1.2723890 -1.1617267 -0.6130110 -0.6326363 -1.5608517 -0.8325350
 [43] -1.0677614 -0.8375510 -1.6233579 -1.5414523 -0.2959174 -0.6113846 -1.3777482
 [50] -0.9695975 -0.6884003 -1.3106532 -0.9089572 -1.7970221 -0.6553532 -0.2785981
 [57] -0.9336035 -1.6773840 -1.0612469 -0.6002046  0.3070840 -1.0792818 -0.3905096
 [64] -0.7643933 -1.5264474 -0.3540782 -1.9662361 -0.6233462 -0.9594732  0.1381904
 [71] -0.1852506 -1.3285226 -0.5803871 -1.4863370 -1.7103169 -0.8629584 -1.0247360
 [78] -0.4806361 -0.7059812 -0.9927834 -1.5921652 -0.8216154 -0.3559753 -2.1285959
 [85] -0.5727712 -0.8732135 -0.1023033 -1.3436125 -0.9225435 -1.0979780 -1.3155360
 [92] -0.2955632 -0.6027357 -0.4422999 -0.5086040 -0.9827529 -1.2780067 -1.5405946
 [99] -1.5093126 -1.3319693

length(y)
[1] 100
\end{lstlisting}
\textbf{Ans:}\\
The length of the vector $y$ is 100.\\
$\beta_0 = -1$ and $\beta_1 = 0.5$

\subsection*{(d)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
\end{lstlisting}
\textbf{Result:}
\begin{figure*}[!h]
\begin{center}
\includegraphics[width=0.45\textwidth]{HW1_P3_d.png}
\end{center}
\label{fig:HW1_P3_d}
\end{figure*}\\
\textbf{Ans:}\\
There is an approximate linear relationship between Y and X.

\subsection*{(e)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> lm.fit = lm(y~x)
> summary(lm.fit)
> plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
> abline(lm.fit)
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max
-0.46921 -0.15344 -0.03487  0.13485  0.58654

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.00942    0.02425  -41.63   <2e-16 ***
x            0.49973    0.02693   18.56   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2407 on 98 degrees of freedom
Multiple R-squared:  0.7784,	Adjusted R-squared:  0.7762
F-statistic: 344.3 on 1 and 98 DF,  p-value: < 2.2e-16
\end{lstlisting}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.5\textwidth]{HW1_P3_e.png}
\end{center}
\label{fig:HW1_P3_e}
\end{figure}
\textbf{Ans:}\\
$\hat{\beta}_0 = -1.00942$ \quad $\hat{\beta}_1 = 0.49973$ \qquad $\beta_0 = -1$ \quad $\beta_1 = 0.5$\\
With the $\mathrm{Pr}(>|t|) < 2e-16$, we will reject $H_0$ and say $\hat{\beta}_0$ and $\hat{\beta}_1$ are not 0.\\
With p-value $< 2.2e-16$ and $R^2 = 0.7784$, we can say there exist relationship between Y and X and our linear model fits Y and X well. \\
The estimated value of $\beta_0$ and $\beta_1$ are very close to their true value.

\subsection*{(f)}
\textbf{Code:}
\begin{lstlisting}[language=R]
> par(col='black')
> plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
> abline(lm.fit, col='blue', lty=5)
> abline(a=-1, b=0.5, col='red', lty=1)
> legend('topleft', inset=0.05, c('least squares line', 'population regression line'), lty=c(5, 1), col=c('blue', 'red'), bty = "o")
\end{lstlisting}
\textbf{Result:}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.7\textwidth]{HW1_P3_f.png}
\end{center}
\label{fig:HW1_P3_f}
\end{figure}

\subsection*{(g)}
\textbf{Code:}
\begin{lstlisting}[language=R]
# polynomial regression
Poly_fit = lm(y ~ poly(x,2))
# show regression result
summary(Poly_fit)
# creat points to draw fitting line
xlims = range(x)
x.grid = seq(from=xlims[1], to=xlims[2])
preds = predict(Poly_fit, newdata=list(x=x.grid), se=TRUE)
# use standard error to creat wrapper line
se.bands = cbind(preds$fit+2*preds$se.fit, preds$fit-2*preds$se.fit)
# draw
plot(x, y, xlab="X", ylab="Y", main='Degree-2 Polynomial', col="black")
lines(x.grid, preds$fit, lwd=2, col="blue", lty=1)
matlines(x.grid, se.bands, lwd=1, col="red", lty=5)
legend('topleft', inset=0.05, c('polynomial regression line', 'SE wrapper line'), lwd=c(2, 1), lty=c(1, 5), col=c('blue', 'red'), bty = "o")
\end{lstlisting}
\textbf{Result:}
\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\textwidth]{HW1_P3_g.png}
\end{center}
\label{fig:HW1_P3_g}
\end{figure}
\begin{lstlisting}[language=R]
Call:
lm(formula = y ~ poly(x, 2))

Residuals:
    Min      1Q  Median      3Q     Max
-0.4913 -0.1563 -0.0322  0.1451  0.5675

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.95501    0.02395 -39.874   <2e-16 ***
poly(x, 2)1  4.46612    0.23951  18.647   <2e-16 ***
poly(x, 2)2 -0.33602    0.23951  -1.403    0.164
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2395 on 97 degrees of freedom
Multiple R-squared:  0.7828,	Adjusted R-squared:  0.7784
F-statistic: 174.8 on 2 and 97 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Ans:}\\
There is limited evidence that the quadratic term improves the model fit. \\
The $\mathrm{Pr}(>|t|)$ of the quadratic term equals 0.164. So we still can say there's no relation between Y and $X^2$ with the probability equalling 0.164, which is not a small enough probability. \\
Also, from the figure above, the polynomial regression line is very close to the least squares line in linear model. \\
Therefore, the quadratic term hardly improves the model fit.

\subsection*{(h)}
\textbf{Code:}
\begin{lstlisting}[language=R]
# a
set.seed(1) # ensure consistent results
x=rnorm(100, mean=0, sd=1) # feature X

# b
eps=rnorm(100, mean=0, sd=0.1)

# c
y = -1 + 0.5*x + eps
length(y)

# d scatterplot
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")

# e least squares linear model
lm.fit = lm(y~x)
summary(lm.fit)
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
abline(lm.fit)

# f
par(col='black')
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
abline(lm.fit, col='blue', lty=5) # least squares line
abline(a=-1, b=0.5, col='red', lty=1) # population regression line
legend('topleft', inset=0.05, c('least squares line', 'population regression line'), lty=c(5, 1), col=c('blue', 'red'), bty = "o")
\end{lstlisting}
\textbf{Result:}
\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\textwidth]{HW1_P3_h.png}
\end{center}
\label{fig:HW1_P3_h}
\end{figure}
\begin{lstlisting}[language=R]
Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max
-0.18768 -0.06138 -0.01395  0.05394  0.23462

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.003769   0.009699  -103.5   <2e-16 ***
x            0.499894   0.010773    46.4   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.09628 on 98 degrees of freedom
Multiple R-squared:  0.9565,	Adjusted R-squared:  0.956
F-statistic:  2153 on 1 and 98 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Ans:}\\
With less noise in the data, the observation points are more near to the population regression line. The estimated value of $\beta_0$ and $\beta_1$ are more close to their true value. $R^2 = 0.9565$ means the linear model gives a better fit.

\subsection*{(i)}
\textbf{Code:}
\begin{lstlisting}[language=R]
# a
set.seed(1) # ensure consistent results
x=rnorm(100, mean=0, sd=1) # feature X

# b
eps=rnorm(100, mean=0, sd=1)

# c
y = -1 + 0.5*x + eps
length(y)

# d scatterplot
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")

# e least squares linear model
lm.fit = lm(y~x)
summary(lm.fit)
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
abline(lm.fit)

# f
par(col='black')
plot(x, y, xlab="X", ylab="Y", main="Relationship between X and Y")
abline(lm.fit, col='blue', lty=5) # least squares line
abline(a=-1, b=0.5, col='red', lty=1) # population regression line
legend('topleft', inset=0.05, c('least squares line', 'population regression line'), lty=c(5, 1), col=c('blue', 'red'), bty = "o")
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max
-1.8768 -0.6138 -0.1395  0.5394  2.3462

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.03769    0.09699 -10.699  < 2e-16 ***
x            0.49894    0.10773   4.632 1.12e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.9628 on 98 degrees of freedom
Multiple R-squared:  0.1796,	Adjusted R-squared:  0.1712
F-statistic: 21.45 on 1 and 98 DF,  p-value: 1.117e-05
\end{lstlisting}
\begin{figure}[htb]
\begin{center}
\includegraphics[width=0.7\textwidth]{HW1_P3_i.png}
\end{center}
\label{fig:HW1_P3_i}
\end{figure}
\textbf{Ans:}\\
With more noise in the data, the observation points are more far to the population regression line. The estimated value of $\beta_0$ and $\beta_1$ are more different to their true value. $R^2 = 0.1796$ means the linear model gives a worse fit.

\subsection*{(j)}
\textbf{Code:}
\begin{lstlisting}[language=R]
confint(lm.fit)
\end{lstlisting}
\textbf{Result:}\\
$\epsilon \thicksim N(0, 0.25)$
\begin{lstlisting}[language=R]
                 2.5 %     97.5 %
(Intercept) -1.0575402 -0.9613061
x            0.4462897  0.5531801
\end{lstlisting}
$\epsilon \thicksim N(0, 0.1)$
\begin{lstlisting}[language=R]
                 2.5 %     97.5 %
(Intercept) -1.0230161 -0.9845224
x            0.4785159  0.5212720
\end{lstlisting}
$\epsilon \thicksim N(0, 1)$
\begin{lstlisting}[language=R]
                 2.5 %     97.5 %
(Intercept) -1.2301607 -0.8452245
x            0.2851588  0.7127204
\end{lstlisting}
\textbf{Ans:}\\
With less noise, the confidence interval for $\beta_0$ and $\beta_1$ are shorter, meaning the estimation are more accurate to the true value. \\
With more noise, the confidence interval for $\beta_0$ and $\beta_1$ are longer, meaning the estimation are less accurate to the true value.

\newpage

\section*{P4}
\textbf{Code:}
\begin{lstlisting}[language=R]
Advertising = read.csv("/Users/yangchenye/Downloads/Advertising.csv",header=T,na.strings="?")
dim(Advertising)
Advertising=na.omit(Advertising) # remove incomplete cases
dim(Advertising)
names(Advertising)
attach(Advertising)

# TV
TV_fit = lm(sales~TV) # least squares model
TV_CI = confint(TV_fit, level = 0.92) # 92% confidence intervals

plot(TV, sales, xlab="TV advertising", ylab="Sales", main="Relationship between Sales and TV advertising") # scatterplot
abline(TV_fit, col='blue', lty=1) # least squares line
abline(a=TV_CI[1,1], b=TV_CI[2,1], col='red', lty=5) # 92% confidence intervals line
abline(a=TV_CI[1,2], b=TV_CI[2,2], col='red', lty=5) # 92% confidence intervals line
legend('topleft', inset=0.05, c('least squares line', '92% confidence intervals line'), lty=c(1, 5), col=c('blue', 'red'), bty = "o")

# Radio
Radio_fit = lm(sales~radio) # least squares model
Radio_CI = confint(Radio_fit, level = 0.92) # 92% confidence intervals

plot(radio, sales, xlab="Radio advertising", ylab="Sales", main="Relationship between Sales and Radio advertising") # scatterplot
abline(Radio_fit, col='blue', lty=1) # least squares line
abline(a=Radio_CI[1,1], b=Radio_CI[2,1], col='red', lty=5) # 92% confidence intervals line
abline(a=Radio_CI[1,2], b=Radio_CI[2,2], col='red', lty=5) # 92% confidence intervals line
legend('topleft', inset=0.05, c('least squares line', '92% confidence intervals line'), lty=c(1, 5), col=c('blue', 'red'), bty = "o")

# Newspaper
Newspaper_fit = lm(sales~newspaper) # least squares model
Newspaper_CI = confint(Newspaper_fit, level = 0.92) # 92% confidence intervals

plot(newspaper, sales, xlab="Newspaper advertising", ylab="Sales", main="Relationship between Sales and Newspaper advertising") # scatterplot
abline(Newspaper_fit, col='blue', lty=1) # least squares line
abline(a=Newspaper_CI[1,1], b=Newspaper_CI[2,1], col='red', lty=5) # 92% confidence intervals line
abline(a=Newspaper_CI[1,2], b=Newspaper_CI[2,2], col='red', lty=5) # 92% confidence intervals line
legend('bottomright', inset=0.05, c('least squares line', '92% confidence intervals line'), lty=c(1, 5), col=c('blue', 'red'), bty = "o")
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
> TV_CI
                   4 %       96 %
(Intercept) 6.22691926 7.83826784
TV          0.04280193 0.05227135
> Radio_CI
                  4 %       96 %
(Intercept) 8.3210922 10.3021840
radio       0.1665776  0.2384139
> Newspaper_CI
                    4 %        96 %
(Intercept) 11.25788302 13.44493112
newspaper    0.02552451  0.08386169
\end{lstlisting}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.6\textwidth]{HW1_P4_1.png}
\end{center}
\newpage
\label{fig:HW1_P4_1}
\end{figure}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.6\textwidth]{HW1_P4_2.png}
\end{center}
\label{fig:HW1_P4_2}
\end{figure}
\newpage
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.6\textwidth]{HW1_P4_3.png}
\end{center}
\label{fig:HW1_P4_3}
\end{figure}

\newpage

\section*{P5}
\subsection*{(a)}
\textbf{Code:}
\begin{lstlisting}[language=R]
Auto = read.csv("/Users/yangchenye/Downloads/Auto.csv",header=T,na.strings="?")
dim(Auto)
Auto=na.omit(Auto) # remove incomplete cases
dim(Auto)
names(Auto)
attach(Auto)
pairs(Auto)
\end{lstlisting}
\textbf{Result:}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=0.88\textwidth]{HW1_P5_a.png}
\end{center}
\label{fig:HW1_P5_a}
\end{figure}

\subsection*{(b)}
\textbf{Code:}
\begin{lstlisting}[language=R]
CRM = cor(Auto[,1:8])
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R, basicstyle=\tiny]
                    mpg  cylinders displacement horsepower     weight acceleration       year     origin
mpg           1.0000000 -0.7776175   -0.8051269 -0.7784268 -0.8322442    0.4233285  0.5805410  0.5652088
cylinders    -0.7776175  1.0000000    0.9508233  0.8429834  0.8975273   -0.5046834 -0.3456474 -0.5689316
displacement -0.8051269  0.9508233    1.0000000  0.8972570  0.9329944   -0.5438005 -0.3698552 -0.6145351
horsepower   -0.7784268  0.8429834    0.8972570  1.0000000  0.8645377   -0.6891955 -0.4163615 -0.4551715
weight       -0.8322442  0.8975273    0.9329944  0.8645377  1.0000000   -0.4168392 -0.3091199 -0.5850054
acceleration  0.4233285 -0.5046834   -0.5438005 -0.6891955 -0.4168392    1.0000000  0.2903161  0.2127458
year          0.5805410 -0.3456474   -0.3698552 -0.4163615 -0.3091199    0.2903161  1.0000000  0.1815277
origin        0.5652088 -0.5689316   -0.6145351 -0.4551715 -0.5850054    0.2127458  0.1815277  1.0000000
\end{lstlisting}

\subsection*{(c)}
\textbf{Code:}
\begin{lstlisting}[language=R]
mlm_fit = lm(mpg~cylinders + displacement + horsepower + weight + acceleration + year + origin)
summary(mlm_fit)
\end{lstlisting}
\textbf{Result:}
\begin{lstlisting}[language=R]
Call:
lm(formula = mpg ~ cylinders + displacement + horsepower + weight +
    acceleration + year + origin)

Residuals:
    Min      1Q  Median      3Q     Max
-9.5903 -2.1565 -0.1169  1.8690 13.0604

Coefficients:
               Estimate Std. Error t value Pr(>|t|)
(Intercept)  -17.218435   4.644294  -3.707  0.00024 ***
cylinders     -0.493376   0.323282  -1.526  0.12780
displacement   0.019896   0.007515   2.647  0.00844 **
horsepower    -0.016951   0.013787  -1.230  0.21963
weight        -0.006474   0.000652  -9.929  < 2e-16 ***
acceleration   0.080576   0.098845   0.815  0.41548
year           0.750773   0.050973  14.729  < 2e-16 ***
origin         1.426141   0.278136   5.127 4.67e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.328 on 384 degrees of freedom
Multiple R-squared:  0.8215,	Adjusted R-squared:  0.8182
F-statistic: 252.4 on 7 and 384 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Ans:}\\
\textbf{i. Is there a relationship between the predictors and the response?}\\
With the p-value $< 2.2e-16$, we should reject $H_0$, namely, admit that there exist a relationship between predictors and response. And $R^2=0.8215$ shows that this linear model fits the data well. \\
\textbf{ii. Which predictors appear to have a statistically significant relationship to the response?}\\
The $\mathrm{Pr}(>|t|)$ of cylinders, horsepower and acceleration are not small enough to say they have significant relationship to the response, while the $\mathrm{Pr}(>|t|)$ of other predictors are small enough. That is to say, displacement, weight, year and origin have a statistically significant relationship to the response mpg. \\
\textbf{iii. What does the coefficient for the year variable suggest?}\\
The estimated coefficient for the year variable is 0.750773, suggesting that with one unit increase in year, the mpg will increases about 0.75 unit. The year has a positive relationship to mpg.

\subsection*{(d)}
\textbf{Code:}
\begin{lstlisting}[language=R]
mlm_fit_log = lm(log10(mpg)~log10(cylinders) + log10(displacement) +log10(horsepower) + log10(weight) + log10(acceleration) + log10(year) + log10(origin))
summary(mlm_fit_log)

mlm_fit_sqrt = lm(sqrt(mpg)~sqrt(cylinders) + sqrt(displacement) +sqrt(horsepower) + sqrt(weight) + sqrt(acceleration) + sqrt(year) + sqrt(origin))
summary(mlm_fit_sqrt)

mlm_fit_power = lm(mpg^2~cylinders^2 + displacement^2 + horsepower^2 + weight^2 + acceleration^2 + year^2 + origin^2)
summary(mlm_fit_power)
\end{lstlisting}
\textbf{Result with the transformation of $\mathrm{log}(x)$:}
\begin{lstlisting}[language=R]
Call:
lm(formula = log10(mpg) ~ log10(cylinders) + log10(displacement) +
    log10(horsepower) + log10(weight) + log10(acceleration) +
    log10(year) + log10(origin))

Residuals:
      Min        1Q    Median        3Q       Max
-0.179356 -0.030825  0.000238  0.026708  0.171685

Coefficients:
                     Estimate Std. Error t value Pr(>|t|)
(Intercept)         -0.067485   0.281523  -0.240  0.81068
log10(cylinders)    -0.082815   0.061429  -1.348  0.17841
log10(displacement)  0.006625   0.056970   0.116  0.90748
log10(horsepower)   -0.294389   0.057652  -5.106 5.18e-07 ***
log10(weight)       -0.569666   0.082397  -6.914 1.98e-11 ***
log10(acceleration) -0.179239   0.059536  -3.011  0.00278 **
log10(year)          2.243989   0.131661  17.044  < 2e-16 ***
log10(origin)        0.044848   0.018821   2.383  0.01767 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.04935 on 384 degrees of freedom
Multiple R-squared:  0.8903,	Adjusted R-squared:  0.8883
F-statistic: 445.3 on 7 and 384 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Result with the transformation of $\sqrt{x}$:}
\begin{lstlisting}[language=R]
Call:
lm(formula = sqrt(mpg) ~ sqrt(cylinders) + sqrt(displacement) +
    sqrt(horsepower) + sqrt(weight) + sqrt(acceleration) + sqrt(year) +
    sqrt(origin))

Residuals:
     Min       1Q   Median       3Q      Max
-0.98667 -0.17280 -0.00315  0.16145  1.02245

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)
(Intercept)        -1.949286   0.847481  -2.300 0.021979 *
sqrt(cylinders)    -0.108552   0.141968  -0.765 0.444964
sqrt(displacement)  0.019707   0.021182   0.930 0.352752
sqrt(horsepower)   -0.090896   0.028428  -3.197 0.001502 **
sqrt(weight)       -0.061414   0.007292  -8.422 7.48e-16 ***
sqrt(acceleration) -0.107258   0.077048  -1.392 0.164699
sqrt(year)          1.266015   0.079308  15.963  < 2e-16 ***
sqrt(origin)        0.272324   0.070883   3.842 0.000143 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2964 on 384 degrees of freedom
Multiple R-squared:  0.8662,	Adjusted R-squared:  0.8638
F-statistic: 355.1 on 7 and 384 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Result with the transformation of $x^2$:}
\begin{lstlisting}[language=R]
Call:
lm(formula = mpg^2 ~ cylinders^2 + displacement^2 + horsepower^2 +
    weight^2 + acceleration^2 + year^2 + origin^2)

Residuals:
    Min      1Q  Median      3Q     Max
-483.45 -141.87  -19.62  103.58 1042.84

Coefficients:
               Estimate Std. Error t value Pr(>|t|)
(Intercept)  -1.878e+03  2.928e+02  -6.412 4.22e-10 ***
cylinders    -1.436e+01  2.038e+01  -0.704  0.48157
displacement  1.328e+00  4.738e-01   2.802  0.00534 **
horsepower   -3.587e-01  8.693e-01  -0.413  0.68009
weight       -3.522e-01  4.111e-02  -8.567 2.62e-16 ***
acceleration  9.278e+00  6.232e+00   1.489  0.13740
year          4.081e+01  3.214e+00  12.698  < 2e-16 ***
origin        9.509e+01  1.754e+01   5.422 1.04e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 209.8 on 384 degrees of freedom
Multiple R-squared:  0.7292,	Adjusted R-squared:  0.7243
F-statistic: 147.8 on 7 and 384 DF,  p-value: < 2.2e-16
\end{lstlisting}
\textbf{Ans:}\\
Different transformations of variables will lead to different estimate value of parameters in a same linear model. For example, the intercepts corresponding with different transformation are −17.218435, −0.067485, −1.949286 and −1.878e+03. \\
Also, transformations will change the relationship between variables. For example, "displacement" has relationship with "mpg" in $x$ and $x^2$ transformation situation, while little relationship in $\mathrm{log}(x)$ and $\sqrt{x}$ situation.\\
Moreover, transformation on variables will also change the property of the linear model, which can be seen in the difference of $R^2$, RSE and F - statistic.

\newpage

\section*{P6}
\textbf{Ans:}\\
From the given constrain, we can calculate the following values:\\
\begin{equation}
    \begin{aligned}
    &\bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i = \frac{1}{20} \sum_{i=1}^{20} x_i = \frac{1}{20} \times 8.552 = 0.4276\\
    &\bar{y} = \frac{1}{n} \sum_{i=1}^{n} y_i = \frac{1}{20} \sum_{i=1}^{20} y_i = \frac{1}{20} \times 398.2 = 19.91\\
    \end{aligned}
\end{equation}{}
\begin{equation}
    \begin{aligned}
    \sum_{i=1}^n (x_i - \bar{x}) (y_i - \bar{y}) &= \sum_{i=1}^{20} (x_i y_i - \bar{x} y_i - x_i \bar{y} + \bar{x}\bar{y})\\
    &= \sum_{i=1}^{20} (x_i y_i) - \bar{x} \sum_{i=1}^{20} y_i - \bar{y} \sum_{i=1}^{20} x_i + 20\bar{x}\bar{y}\\
    &= 216.6 - 0.4276\times 398.2 - 19.91\times 8.552 + 20 \times 0.4276 \times 19.91\\
    &= 46.33\\
    \sum_{i=1}^n (x_i - \bar{x})^2 &= \sum_{i=1}^{20} (x_i^2 - 2\bar{x} x_i  + \bar{x}^2)\\
    &= \sum_{i=1}^{20} x_i^2 - 2\bar{x} \sum_{i=1}^{20} x_i  + 20\bar{x}^2\\
    &= 5.196 - 2\times0.4276\times8.552 + 20\times0.4276^2\\
    &= 1.539\\
    \sum_{i=1}^n (y_i - \bar{y})^2 &= \sum_{i=1}^{20} (y_i^2 - 2\bar{y} y_i  + \bar{y}^2)\\
    &= \sum_{i=1}^{20} y_i^2 - 2\bar{y} \sum_{i=1}^{20} y_i  + 20\bar{y}^2\\
    &= 9356 - 2\times19.91\times398.2 + 20\times19.91^2\\
    &= 1428
    \end{aligned}
\end{equation}{}\\
Thus:\\
\begin{equation}
    \begin{aligned}
    \hat{\beta}_1 &= \frac{\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^n (x_i-\bar{x})^2} = \frac{46.33}{1.539} = 30.10\\
    \hat{\beta}_0 &= \bar{y} - \hat{\beta}_1\bar{x} = 19.91 - 30.10\times 0.4276 = 7.039\\
    \mathrm{RSS} &= \sum_{i=1}^n (y_i-\hat{y}_i)^2 = \sum_{i=1}^n (y_i - \hat{\beta}_0 - \hat{\beta}_1 x_i)^2 = \sum_{i=1}^n (y_i - \bar{y} + \hat{\beta}_1 \bar{x} - \hat{\beta}_1 x_i)^2 \\
    &= \sum_{i=1}^n (y_i - \bar{y})^2 - \hat{\beta}_1 \sum_{i=1}^n (y_i - \bar{y}) (x_i - \bar{x}) = 1428 - 30.10\times 46.33 = 33.47
    \end{aligned}
\end{equation}{}\\
\begin{equation}
\begin{aligned}
R^2 &= 1 - \frac{\mathrm{RSS}}{\sum_{i=1}^n (y_i-\bar{y})^2} = 1-\frac{33.47}{1428} = 0.9766\\
\hat{\sigma}^2 &= \frac{\mathrm{RSS}}{n-2}  = \frac{\mathrm{33.47}}{20-2} = 1.859
\end{aligned}
\end{equation}{}\\

When $x = 0.5$:\\
$$\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x = 7.039 + 30.10\times 0.5 = 22.09$$\\

\newpage

\section*{P7}
\textbf{Code:}
\begin{lstlisting}[language=Matlab]
fcdf(1.89, 6, 38, 'upper') % matlab
\end{lstlisting}{}
\textbf{Ans:}\\
The null hypothesis test is performed by computing the F-statistic:\\
\begin{equation}
\begin{aligned}
&F = \frac{(\mathrm{TSS}-\mathrm{RSS})/p}{\mathrm{RSS}/(n-p-1)} = \frac{(11.62-8.95)/6}{8.95/(45-6-1)} = 1.89\\
&\mathrm{Pr} (F_{6,38} > 1.89) = 0.11
\end{aligned}
\end{equation}{}\\
The $p$-value for the null hypothesis is 0.11

\newpage

\section*{E1}
\textbf{Ans:}\\
The definition of gamma function is :
$$\Gamma(x) = \int_{0}^{+\infty} t^{x-1} e^{-t} dt $$
If a random variable $X \thicksim \Gamma(\alpha, \theta) $, then the probability density function of $X$ has the following format:\\
\begin{equation}
    \centering
    f_X (x)=
    \left\{
             \begin{aligned}
             \frac{1}{\theta^\alpha \Gamma(\alpha)}x^{\alpha-1}e^{-x/\theta}&, x > 0 \\
             0\qquad\qquad&, \mathrm{else}
             \end{aligned}
    \right.
\end{equation}{}\\
Let the density of $X$ be $f_X (x)$, and let the density of $Y=X^2$ be $f_Y (y)$, we have:\\
\begin{equation}
    \centering
    f_Y (y)=
    \left\{
             \begin{aligned}
             \frac{1}{2\sqrt{y}}[f_X(\sqrt{y}) + f_X(-\sqrt{y})]&, y > 0 \\
             0\qquad\qquad\qquad&, \mathrm{else}
             \end{aligned}
    \right.
\end{equation}{}\\
Therefore, as for random variable $X \thicksim N(0,1)$ with density $\varphi (x)=\frac{1}{\sqrt{2\pi}}e^{-x^2/2}\ (-\infty<x<\infty)$, let $Y=X^2$, the density of $Y$ is:
\begin{equation}
    \centering
    \begin{aligned}
    f_Y (y)&=
    \left\{
             \begin{aligned}
             \frac{1}{2\sqrt{y}}[\frac{1}{\sqrt{2\pi}}e^{-y/2} + \frac{1}{\sqrt{2\pi}}e^{-y/2}]&, y > 0 \\
             0\qquad\qquad\qquad&, \mathrm{else}
             \end{aligned}
    \right. \\
    &=
    \left\{
             \begin{aligned}
             \frac{1}{\sqrt{2\pi}}\frac{1}{\sqrt{y}} e^{-y/2}&, y > 0 \\
             0\qquad&, \mathrm{else}
             \end{aligned}
    \right. \\
    \end{aligned}
\end{equation}{}\\
Thus, $Y=X^2 \thicksim \Gamma(\frac{1}{2}, 2)$.\\
Because $X_i,\ i=1,...,n$ are independent, and as a result $X_i^2,\ i=1,...,n$ are also independent, Chi-squared distribution $\chi_n^2 = X_1^2 + X_2^2 + ... + X_n^2$ has additivity. Namely:\\
$$\chi_n^2 = \sum_{i=1}^{n} X_i^2 \thicksim \Gamma(\frac{n}{2}, 2)$$
Therefore, the density of $\chi_n^2$ is given by:
\begin{equation}
    \centering
    g_n (x)=
    \left\{
             \begin{aligned}
             \frac{1}{2^{n/2}\Gamma(n/2)} x^{n/2-1} e^{-x/2}&, x > 0 \\
             0\qquad\qquad\qquad&, x \leq 0
             \end{aligned}
    \right.
\end{equation}{}\\

\newpage

\section*{E2}
\textbf{Ans:}\\
(The first half of the proof is the same as P1(d), I just copy and paste.)\\
Let $Z_i = \frac{X_i -\mu}{\sigma}\ i=1,2,...,n$, thus $Z_1, Z_2,..., Z_n$ are independent and $Z_i\thicksim N(0,1)\ i=1,2,...,n$. Also:\\
\begin{equation}
    \begin{aligned}
    \bar{Z} &= \frac{1}{n}\sum_{i=1}^{n} Z_i = \frac{1}{n}\sum_{i=1}^{n} \frac{X_i -\mu}{\sigma} = \frac{\bar{X}-\mu}{\sigma}\\
    \frac{(n-1)S^2}{\sigma^2} &= \frac{\sum_{i=1}^{n} (X_i - \bar{X})^2}{\sigma^2} = \sum_{i=1}^{n} [\frac{ (X_i - \mu) - (\bar{X} - \mu)}{\sigma }]^2\\
    &= \sum_{i=1}^{n} (Z_i - \bar{Z})^2 = \sum_{i=1}^{n} (Z_i^2 - 2Z_i\bar{Z} + \bar{Z}^2)\\
    &= \sum_{i=1}^{n}Z_i^2 - 2\bar{Z}\sum_{i=1}^{n}Z_i + n\bar{Z}^2 = \sum_{i=1}^{n}Z_i^2 - n\bar{Z}^2
    \end{aligned}
\end{equation}
Choose a matrix $\bm{A} = (a_{ij})$, in which all the entries in the first row are $1/\sqrt{n}$, and each row vector of the matrix should be orthogonal. Do:\\
$$\bm{Y} = \bm{AZ}$$
In which $$\bm{Y} = (Y_1, Y_2,...,Y_n)^T$$ $$\bm{Z} = (Z_1, Z_2,...,Z_n)^T$$ $$\bm{A} = (\bm{a_1}^T, \bm{a_2}^T,...,\bm{a_n}^T)^T$$
Because $Y_i$ is the linear combination of $Z_i$ and $Z_i\thicksim N(0,1)\ i=1,2,...,n$, therefore $Y_i\ i=1,2,...,n$ have normal/Gaussian distribution and:
\begin{equation}
\begin{aligned}
\mathbb{E}(Y_i) &= \mathbb{E}(\sum_{j=1}^{n} a_{ij}Z_j) = \sum_{j=1}^{n} a_{ij}\mathbb{E}(Z_j) = 0\\
\mathrm{Var}(Y_i) &= \mathrm{Var}(\sum_{j=1}^{n} a_{ij}Z_j) = \sum_{j=1}^{n} a_{ij}^2 = <\bm{a_i}, \bm{a_i}> = 1
\end{aligned}
\end{equation}\\
Because $\mathrm{Cov}(Z_k, Z_h)$ equals 1 when $k=h$ and equals 0 when $k\neq h$:
\begin{equation}
    \begin{aligned}
    \mathrm{Cov}(Y_i, Y_j) &= \mathrm{Cov}(\sum_{k=1}^{n} a_{ik}Z_k, \sum_{h=1}^{n} a_{jh}Z_h) \\
    &= \sum_{k=1}^{n} \sum_{h=1}^{n} a_{ik} a_{jh} \mathrm{Cov}(Z_k, Z_h) \\
    &= \sum_{k=1}^{n} a_{ik} a_{jk}\\
    &= <\bm{a_i}, \bm{a_j}>\\
    &=
    \left\{
             \begin{aligned}
             1\qquad&, i=j \\
             0\qquad&, i\neq j
             \end{aligned}
    \right. \\
    &= \mathrm{Cov}(Z_i, Z_j)
    \end{aligned}
\end{equation}\\
Therefore, $Y_i\ i=1,2,...,n$ has no correlation with each other. Considering $Y_i \thicksim N(0,1)$, thus $Y_1, Y_2,..., Y_n$ are independent.
Also:\\
\begin{equation}
\begin{aligned}
&Y_1 = \sum_{j=1}^{n} a_{1j}Z_j = \sum_{j=1}^{n}\frac{1}{\sqrt{n}}Z_j = \sqrt{n}\bar{Z}\\
&\sum_{i=1}^{n}Y_i^2 = \bm{Y}^T\bm{Y} = (\bm{AZ})^T(\bm{AZ}) = \bm{Z}^T (\bm{A} \bm{A}^T)^T \bm{Z} = \bm{Z}^T \bm{I} \bm{Z} = \sum_{i=1}^{n}Z_i^2
\end{aligned}
\end{equation}\\
Therefore:
\begin{equation}
\begin{aligned}
\frac{(n-1)S^2}{\sigma^2} = \sum_{i=1}^{n}Z_i^2 - n\bar{Z}^2 = \sum_{i=1}^{n}(Y_i^2) - Y_1^2 = \sum_{i=2}^{n}(Y_i^2)
\end{aligned}
\end{equation}\\
Because $Y_2, Y_3,..., Y_n$ are independent and $Y_i \thicksim N(0,1)\ i=2,3,...,n$, thus $\sum_{i=2}^{n}(Y_i^2)\thicksim \chi_{n-1}^2$. \\
Thus:
$$\frac{(n-1)S^2}{\sigma^2}\thicksim \chi_{n-1}^2$$
Namely:
$$\frac{(n-1)S^2}{\sigma^2}\overset{d}{=} \chi_{n-1}^2$$

\newpage

\section*{E3}
\textbf{Ans:}\\

\newpage

\section*{E4}
\textbf{Ans:}\\

\newpage

\end{document}


The previous chapter introduced motion planning problems that are formulated with respect to the robot's configuration space ($C$-space). In particular, two specific approaches for motion planning in $C$-space were discussed: grid-based methods and combinatorial planning methods. Grid-based methods discretize the continuous $C$-space into a grid, and then use graph search methods such as A* to compute paths through the grid. Combinatorial planners compute a \textit{roadmap} that consists of a finite set of points in the $C$-space, but avoids the use of a rigid grid structure. Planning with the roadmap then consists of connecting the initial configuration and desired configuration to the roadmap, and then performing a graph search to find a path along the roadmap.

Generally speaking, grid-based methods suffer from the rigidity of the discretization that is performed. In contrast, combinatorial planners have much more flexibility because \textit{any} configuration $q$ can be a part of the roadmap. However, both types of planners require a complete characterization of the free configuration space (e.g. points in the configuration space that don't result in a collision with obstacles) in advance. In this chapter, a class of motion planning algorithms is presented which builds a roadmap that is similar to combinatorial planners, but without requiring a full characterization of the free configuration space. Instead, these algorithms build roadmaps one point at a time by sampling a point in the configuration space, and then querying an independent module to determine if the sample is admissible. This class of planners are referred to as \textit{sampling-based methods}\cite{LaValle2006}.

\notessection{Sampling-Based Motion Planning}
In contrast to the search-based motion planners discussed in the last chapter, sampling-based methods leverage an independent module that can be queried to determine if a configuration is admissible. In the context of robotics, an admissible configuration in motion planning problems is often one that is collision-free and therefore this module is often referred to as a collision detection module (or simply a \textit{collision checker}).
The collision detection module is used to probe and incrementally build a roadmap in the configuration space, rather than attempting to completely characterize the free space in advance (as is done in combinatorial planners).

Sampling-based algorithms are a common choice for practical applications as they are conceptually simple, flexible, relatively easy to implement, and can be extended beyond the geometric case (i.e. they can consider differential constraints). The disadvantages of the approach are typically with respect to theoretical guarantees, for example these approaches cannot certify that a solution doesn't exist. In this chapter the focus will be on two popular sampling-based methods: probabilistic roadmaps (PRM) and the rapidly-exploring random trees (RRT) algorithm. Additional techniques such as the fast-marching tree algorithm (FMT*), kinodynamic planning, and deterministic sampling-based methods will also be briefly mentioned.

\subsection{Probabilistic Roadmap (PRM)}
It is easiest to start with the probabilistic roadmap algorithm because it is conceptually quite similar to combinatorial planners from the previous section. In particular, the PRM algorithm also generates a topological graph $G$ called a \textit{roadmap} where the vertices are configurations $q$ in the free part of the configuration space $C_{free}$, and edges connect the vertices (and must also entirely lie in $C_{free}$). Once the roadmap is generated, a motion plan can be found for a given initial configuration $q_I$ and goal configuration $q_G$ by first connecting them to the roadmap, and then using a graph-search algorithm (e.g. A*) to find a path along the roadmap graph $G$. The difference between PRM and combinatorial planners lies in the method in which the roadmap is generated.

The key insight of the PRM algorithm is that a complete characterization of the free configuration space (which is computationally expensive) can be avoided by sampling configurations $q$ at random and then using a collision checker to validate if $q \in C_{free}$. The general outline of the algorithm follows:
\begin{enumerate}
    \item Randomly sample $n$ configurations $q_i$ from the configuration space.
    \item Query a collision checker for each $q_i$ to determine if $q_i \in C_{free}$, if $q_i \not\in C_{free}$ then it is removed from the sample set.
    \item Create a graph $G = (V,E)$ with vertices from the sampled configurations $q_i \in C_{free}$. Define a radius $r$ and create edges for every pair of vertices $q$ and $q'$ where: (i) $\lVert q - q' \rVert \leq r$ and (ii) the straight line path between $q$ and $q'$ is also collision free.
\end{enumerate}
An example of the roadmap resulting from applying this algorithm is shown in Figure \ref{fig:prm-graph}.
\begin{figure}[ht]
  \centering
   \includegraphics[width=0.65\textwidth]{tex/figs/ch06_figs/prm.png}
  \caption{Example solution found via the PRM algorithm. The black dots represent the randomly sampled vertices of the graph, and the grey lines represent the edges created between vertices within a predefined radius $r$ of each other. The initial configuration $q_{start}$ and goal configuration $q_{goal}$, are connected through this roadmap along the pink line, which is found by a graph-search algorithm.}
  \label{fig:prm-graph}
\end{figure}
Note that using the \textit{connectivity radius} $r$ is a simple and efficient way of connecting the sampled vertices without having a burdensome number of edges. This is desirable because having too many edges is unnecessary, will make the graph-search more challenging, and will require more collision checks to be made\footnote{Edge validation is usually performed by densely sampling the edge and checking for collisions at each.}. On the flip side, making the radius $r$ too small could mean not enough connections are made.

The downside of PRM is that finding good solutions may require a large number of samples $n$ to sufficiently cover the configuration space. Similar to why having too many edges is not good, having too many samples will require a lot of queries of the collision checker, which may be costly. However, there are some scenarios where building a roadmap that completely covers the space $C_{free}$ is beneficial, namely in \textit{multi-query} planning problems. In multi-query problems, it is assumed that the motion planner will be called many times for different initial $q_I$ and goal $q_G$ configurations. In this case the PRM graph can be built once to cover $C_{free}$, and then it can be reused as many times as needed. In other words, the costly sampling and collision checking only needs to be done once at the start, so it may be worth the ``investment''. Note however that this only works if the environment stays the same in between each query of the motion planner. If the environment changes, the entire PRM roadmap would have to be rebuilt from scratch!

\subsection{Rapidly-exploring Random Trees (RRT)}
In multi-query problems where the environment does not change in between each query, the probabilistic roadmap (PRM) algorithm offers the advantage of front-loading some work to provide efficient queries later. However, many problems in robotics are alternatively classified as \textit{single-query} problems, where it is assumed that only a single query will be made to the motion planner. A common single-query planning scenario arises from changing environments, such as if there is a moving obstacle. In this case building up a roadmap over the entire free configuration space $C_{free}$ may result in wasted effort. The RRT algorithm solves this problem by incrementally sampling and building the graph, starting at the initial configuration $q_I$, until the goal configuration $q_G$ is reached. Additionally, the graph is built as a \textit{tree}, which is a special type of graph that has only one path between any two vertices in the graph.

In general, the RRT algorithm begins by initializing a tree\footnote{The tree is a graph, however since it has special structure it is denoted as $T$ rather than $G$.} $T = (V,E)$ with a vertex at the initial configuration (i.e. $V = \{q_I\}$). At each iteration the RRT algorithm then performs the following steps:
\begin{enumerate}
    \item Randomly sample a configuration $q \in C$.
    \item Find the vertex $q_{near} \in V$ that is closest to the sampled configuration $q$.
    \item Compute a new configuration $q_{new}$ that lies on the line connecting $q_{near}$ and $q$ such that the entire line from $q_{near}$ to $q_{new}$ is contained in the free configuration space $C_{free}$.
    \item Add a vertex $q_{new}$ and an edge $(q_{near}, q_{new})$  to the tree $T$.
\end{enumerate}
Thus after each iteration only a single point is sampled and potentially added to the tree. Additionally, every so often the sampled point $q$ can be set to be the goal configuration $q_G$. Then, if the nearest point $q_{near}$ can be connected to $q_G$ by a collision-free line the search can be terminated. Intuitively, this approach works because of a phenomenon referred to as the \textit{Voronoi bias}, which essentially describes the fact that there is more ``empty space'' near the nodes on the frontier of the tree. Therefore, a randomly sampled point is more likely to be drawn in this ``empty space'', causing the frontier to be extended (and therefore driving exploration).

Note that variations on this standard algorithm exist, in particular there exist different ways of connecting a sampled point to the existing tree. One popular variant that modifies the way a sampled point is connected to the tree is known as RRT* (pronounced RRT star). This modified RRT algorithm introduces a notion of optimality into the planner and will in fact return an optimal solution as the number of samples approaches infinity. Another variant of RRT is called RRT-Connect, which simultaneously builds a tree from both the initial configuration $q_I$ and the goal configuration $q_G$ and tries to connect them.

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.65\textwidth]{tex/figs/ch06_figs/rrt.png}
  \caption{Example exploration tree by the RRT algorithm. The black dots represent points sampled at each iteration of the algorithm, which are connected to the nearest vertex that is currently part of the tree.}
  \label{fig:rrt-graph}
\end{figure}

\subsection{Theoretical Results for PRM and RRT}
One of the main challenges of sampling-based motion planning is that it is unclear how many samples are needed to find a solution. However, some theoretical guarantees can be provided regarding their asymptotic behavior (i.e. behavior as number of samples $n \xrightarrow{} \infty$). In particular, both PRM\footnote[][-8\baselineskip]{With a constant connectivity radius $r$.} and RRT are guaranteed\footnote[][-8\baselineskip]{These guarantees also require an assumption that the configuration space is bounded, for example if $C$ is the $d$-dimensional unit hypercube with $2 \leq d \leq \infty$.} to eventually find a solution if it exists \cite[-4\baselineskip]{LaValle1998}\cite[-\baselineskip]{KavrakiSvestkaEtAl1996}.
Regarding solution \textit{quality}, it has been shown that PRM (with the appropriate choice of the radius $r$) can find optimal paths as the number of samples $n \xrightarrow{} \infty$. However, RRT can be arbitrarily bad with non-negligible probability \cite{KaramanFrazzoli2011}.

\subsection{Fast Marching Tree Algorithm (FMT*)}
As previously mentioned, PRM is an asymptotically optimal algorithm which means that with enough samples it will find good paths. However, in practice PRM with a large number of samples also requires a lot of collision checks and is therefore costly. On the other hand, RRT is fast but in general will not find good paths.
FMT* is a an advanced sampling-based motion planning algorithm that maintains the advantages of both of these algorithms (i.e. fast and asymptotically optimal) \cite{JansonSchmerlingEtAl2015}.

FMT* builds a tree structured graph in the same way RRT does (which maintains the efficiency of RRT), but makes connections in a way that allows for asymptotic optimality. In particular, the technique used for making new connections is referred to as \textit{dynamic programming}. Dynamic programming can be used to find the best paths with respect to a \textit{cost-of-arrival}, denoted $c(q)$, which represents the cost to move from the initial configuration $q_I$ to the configuration $q$. An example of a common metric is simply the Euclidean distance, which would result in a ``shortest'' path. In the context of motion planning, dynamic programming leverages Bellman's \textit{principle of optimality}, which states that the optimal paths satisfy:
\begin{equation}
\label{eq:bellmancost}
c(v) = \min_{u:\lVert u - v\rVert < r_n} \text{Cost}(u, v) + c(u),
\end{equation}
where $u$ are nodes within radius $r_n$ of node $v$, $\text{Cost}(u, v)$ is the cost of an edge between $u$ and $v$, and $c(u)$ is the cost-to-arrive at $u$. In words, this relationship says that the cost-of-arrival at any configuration $v$ on the optimal path is defined by searching over all local neighboring configurations to find which would result in the best path. FMT* uses this principle repeatedly every time it needs to connect a new sample to the tree.
However, in practice using the condition \eqref{eq:bellmancost} is complicated by the fact that the resulting edge may result in a collision. FMT* handles this by ignoring obstacles when using the condition \eqref{eq:bellmancost} to connect a new sample to the tree, and then if a collision occurs from the resulting connection it is simply skipped and the algorithm moves on to a new sample. Therefore this application of dynamic programming is referred to as \textit{lazy} because it only checks for collisions after the fact. It turns out that this substantially reduces the total amount of collision checks required, and only leads to sub-optimality in rare cases.

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.75\textwidth]{tex/figs/ch06_figs/fmt.png}
  \caption{Example of a step in FMT*. Suppose the sample $v$ has been selected to be the next point to be added to the tree. The candidate costs $\text{Cost}(u_1, v) + c(u_1)$ and $\text{Cost}(u_2, v) + c(u_2)$ are evaluated to see which connection would minimize $c_v$. Suppose $u_2$ was selected by this criteria (i.e. $u_2$ satisfies \eqref{eq:bellmancost}), then the collision checker would see that the edge $(u_2,v)$ results in a collision and the sample $v$ would be skipped (but could be added later).}
  \label{fig:plot_idea}
\end{figure}

\subsection{Kinodynamic Planning}
The geometric motion planning algorithms previously considered assume that the robot does not have any constraints on its motion and only a collision-free solution is required. This makes the planning task easier because two configurations $q$ and $q'$ can be simply connected by the planner with a \textit{straight line}. However, robots do typically have kinematic/dynamical constraints on their motion, and for some motion planning problems it is desirable or even necessary to take those constraints into account.
The problem of planning a path through the free configuration space $C_{free}$ that satisfies a given set of differential constraints is referred to as \textit{kinodynamic motion planning}\cite{SchmerlingJansonEtAl2015}.

Similar to previous chapter, it is assumed that the robot operates in a state space $X\subseteq R^n$ and can apply controls $\bu \in U \subseteq \R^m$, and that the motion constraints are given by the differential model (i.e. from kinematic or dynamics constraints):
\begin{equation} \label{eq:dmodel}
\dot{\x} = f(\x,\bu),
\end{equation}
where $\x \in \R^n$ and $\bu \in \R^m$. Note that the state space $X$ is not necessarily the same as the configuration space $C$, but the configuration $q$ is derivable from the state $x$. As was previously mentioned, the configuration space is something that can be chosen to capture the information that is necessary for obstacle avoidance. However to include dynamics constraints it is required that the motion planning now be done in the state space $X$.

The RRT algorithm can be extended to the kinodynamic case with relative simplicity. In particular, a random state $x$ is sampled from the state space $X$ and its nearest neighbor $x_{near}$ on the current tree $T$ is found. Instead of connecting $x$ and $x_{near}$ with a straight line (which is likely not dynamically feasible), a random control $u \in U$ and random time $t$ are sampled. Then, the state is propagated forward by integrating the differential equations \eqref{eq:dmodel} with the chosen $u$ for a time $t$ and initial condition $x_{near}$. The resulting state $x_{new}$ is then added to the tree if the path from $x_{near}$ to $x_{new}$ is collision free. This is referred to as a \textit{forward-propagation-based} approach.

Another approach to kinodynamic planning leverage \textit{steering-based} algorithms. In these approaches, the planner selects two points in the state space $x$ and $x'$ and then uses a steering subroutine to find a feasible trajectory to connect these points. Crucially, these approaches only work well if the steering subroutine is \textit{efficient}. This approach is be particularly well suited for differential flat systems.

\begin{figure}[ht]
    \centering
    \begin{subfigure}[b]{0.4\linewidth}
      \includegraphics[width=\linewidth]{tex/figs/ch06_figs/driftless.png}
      \caption{Reeds-Shepp car}
    \end{subfigure}
    \begin{subfigure}[b]{0.4\linewidth}
      \includegraphics[width=\linewidth]{tex/figs/ch06_figs/drift.png}
      \caption{Double integrator system}
    \end{subfigure}
    \caption{Results from a kinodynamic planner called Differential FMT* (DFMT*) (Schmerling et al.). The figure on the left shows the results for a Reeds-Shepp car model, and on the right is a double integrator model.}
    \label{fig:kino}
\end{figure}

\subsection{Deterministic Sampling-Based Motion Planning}

Probabilistic sampling-based algorithms, such as the probabilistic roadmap (PRM) and the rapidly exploring random tree (RRT) algorithms, have been quite successful in practice for robotic motion planning and often have nice theoretical properties (e.g. in terms of probabilistic completeness or even asymptotic optimality). Such algorithms are \textit{probabilistic} because they compute a path by connecting independently and identically distributed (i.i.d.) random points in the configuration space. However, this randomization introduces several challenges for practical use, including certification for safety-critical applications and the ability to use offline computation to improve real-time execution. Hence, it is important to ask whether similar (or better) theoretical guarantees and practical performance could be obtained by considering \textit{deterministic} approaches.

An important metric for answering this question is referred to as the $l_2$-dispersion.
\begin{definition}[$l_2$-dispersion]
For a finite set $S$ of points contained in $X \subset R^d$, its $l_2$-dispersion $D(S)$ is defined as:
\begin{equation}
    D(S) := \underset{x\in X}{\text{sup}}\: \underset{ s \in S}{\text{min}} \lVert s - x \rVert_2.
\end{equation}
\end{definition}
Intuitively, the $l_2$-dispersion of $S$ quantifies how well a space is covered by the set of points in $S$ in terms of the largest Euclidean ball that touches and contains none of the points. For a fixed number of samples, a small $l_2$-dispersion (only a small radius ball can be fit among the points of $S$ without touching or containing any) means that the points are more uniformly distributed.

To create a deterministic sampling based motion planning algorithm, it is desirable to generate a set of samples $S$ with low-dispersion. In fact, low-dispersion sampling sequences exist that give sets $S$ with $l_2$-dispersion $D(S)$ on the order of $O(n^{-1/d})$ where $d$ is the dimension of the space. Additionally, for $d=2$ it is possible to create sequences of points $S$ that \textit{minimize} the $l_2$-dispersion. Then, if the set $S$ of $n$ samples has $l_2$-dispersion that satisfies
\begin{equation*}
    D(S) \leq \gamma n^{-1/d},
\end{equation*}
for some $\gamma > 0$, and if $\lim_{n\rightarrow\infty}n^{1/d}r_n = \infty$, then the arc length of the path $c_n$ returned will converge to the optimal path $c^*$ as $n \xrightarrow{} \infty$.

In summary, deterministic sampling can be used to generate motion planning algorithms. These deterministic algorithms still maintain the asymptotic optimality guarantees that probabilistic planners do, and can even use a smaller connection radius $r_n$.

\subsection{Exercises}
All exercises for this chapter can be found in the online repository:

\vspace{\baselineskip}

\url{https://github.com/PrinciplesofRobotAutonomy/AA274A_HW2}.

\subsubsection{Rapidly-Exploring Random Trees}
Complete \textit{Problem 2: Rapidly-Exploring Random Trees (RRT)}, where you will implement the RRT sample-based motion planning algorithm to plan paths in simple 2D environments. Additionally, in this problem you will start with a simple geometric planner that does not consider robot dynamics, but will then extend the RRT algorithm to consider a wheeled robot modeled with Dubins car dynamics.

\subsubsection{Motion Planning \& Control}
Complete \textit{Problem 3: Motion Planning \& Control}, where you will combine an A* planner with a differential flatness-based tracking controller and a pose stabilization controller to enable a unicycle robot to autonomously move through a 2D environment. Note that this problem requires exercises from previous chapters to be completed first.

\subsubsection{Bi-Directional Sampling-based Motion Planning}
Complete \textit{Extra Problem: Bi-Directional Sampling-based Motion Planning}, where you will implement a variation of the RRT algorithm known as RRT-Connect, which uses a bi-direction approach to building the RRT tree. This algorithm will be implemented for both a simple geometric path planner as well as for a Dubins car robot.


\documentclass[10pt]{report}

\usepackage{enumerate} % for enumerate counter
\usepackage{subcaption} % for subfigures
\usepackage{amsthm} % for QED
\usepackage{mathtools} % for delimiter

\usepackage{listings} % for code
\lstset{
	language=R,
	basicstyle=\footnotesize\ttfamily,
	numbers=none,
	stepnumber=1,
	numbersep=8pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	frame=single,
	tabsize=2,
	captionpos=t,
	breaklines=true,
	breakatwhitespace=false
}

\usepackage{float} % for figure [H]
\usepackage{booktabs} % for tabular
\usepackage{caption} % for \caption*
\usepackage[export]{adjustbox} % for valign=t
\usepackage{array} % for column type m
\usepackage{verbatim}
\usepackage{graphicx}
%\graphicspath{ {imgs/} }
\usepackage{fancyhdr}
\usepackage{amssymb}
\usepackage{amsmath}

%%%%%%Pagination
\setlength{\topmargin}{-.3 in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textheight}{9.in}
\setlength{\textwidth}{6.5in}

%Cover
\newcommand{\hwTitle}{Homework \#5}
\newcommand{\hwCourse}{Applied Statistics}
\newcommand{\hmClassInstructor}{Professor Lulu Kang}

\title{
	\vspace{2in}
	\textmd{\textbf{\hwCourse\\\hwTitle}}\\
	\vspace{0.3in}\large{\textit{\hmClassInstructor}}
	\vspace{3in}
}
\author{\textbf{Zhihao Ai}}
\date{}

%Header
\pagestyle{fancy}
\fancyhead[L]{Zhihao Ai}
\fancyhead[C]{Math 484}
\fancyhead[R]{Homework 5}
%%%%%%

%Global settings
%\everymath{\displaystyle}
\setlength\parindent{0pt}

%Custom commands
\newcommand{\ds}{\displaystyle}
\newcommand{\ts}{\textstyle}

\newcolumntype{N}{>$ c <$}
\newcolumntype{M}[1]{>{\centering\arraybackslash $}m{#1}<{$}}

\newcommand{\abs}[1] {\left| #1 \right|}

\DeclarePairedDelimiter\autoparen{(}{)}
\newcommand{\pa}[1]{\autoparen*{#1}}

\newcommand{\var} {\text{var}}

\newcommand{\m}[1] {\mathbf{#1}}

\begin{document}

\maketitle

\section*{Problem 1}
(Ex. 9.13) \textbf{Lung pressure}
\begin{enumerate}[a.]
	\item
	Prepare separate dot plots for each of the three predictor variables. Are there any noteworthy features in these plots? Comment.
	\begin{figure}[H]
		\centering
		\begin{subfigure}[b]{.3\linewidth}
			\includegraphics[width=\linewidth]{p1/13a_x1.png}
		\end{subfigure}
		\begin{subfigure}[b]{.3\linewidth}
			\includegraphics[width=\linewidth]{p1/13a_x2.png}
		\end{subfigure}
		\begin{subfigure}[b]{.3\linewidth}
			\includegraphics[width=\linewidth]{p1/13a_x3.png}
		\end{subfigure}
	\end{figure}
	Both $X_1$ and $X_2$ are relatively concentrated in the lower part of each range, whereas $X_3$ is randomly scattered in its range. There are several outliers in dot plots of $X_1$ and $X_2$.

	\item
	Obtain the scatter plot matrix. Also obtain the correlation matrix of the $X$ variables. What do the scatter plots suggest about the nature of the functional relationship between $Y$ and each of the predictor variables? Are any serious multicollinearity problems evident? Explain.

	Scatter plot matrix:
	\begin{figure}[H]
		\centering
		\vspace{-4ex}
		\includegraphics[width=.5\linewidth]{p1/13b.png}
		\vspace{-5ex}
	\end{figure}
	Correlation matrix:
	\lstinputlisting{p1/13b.txt}
	The scatter plots suggest that there exists negative correlation between $Y$ and both $X_1$ and $X_2$, and no clear correlation between $Y$ and $X_3$ is shown. The correlation coefficient between $X_1$ and $X_2$ is 0.65, indicating a moderate correlation between them, which could results in muticollinearity problems.

	\item
	Fit the multiple regression function containing the three predictor variables as first-order terms. Does it appear that all predictor variables should be retained?
	\lstinputlisting{p1/13c.txt}
	The $p$-values of $\hat{\beta_1}$ and $\hat{\beta_3}$ are not significant, so it appears not all predictor variables should be retained.
\end{enumerate}

\section*{Problem 2}
(Ex. 9.14) Refer to \textbf{Lung pressure} Problem 9.13
\begin{enumerate}[a.]
	\item
	Using first-order and second-order terms for each of the three predictor variables (centered around the mean) in the pool of potential $X$ variables (including cross products of the first-order terms), find the three best hierarchical subset regression models according to the $R_{a,p}^2$ criterion.
	\begin{figure}[H]
		\centering
		\includegraphics[width=.5\linewidth]{p2/14a.png}
	\end{figure}
	The three best subsets are
	\lstinputlisting{p2/14a.txt}
	The $R_{a,p}^2$'s of the models containing $\{X_1, X_2, X_1^2, X_2^2\}$, $\{X_1, X_2, X_1 X_2\}$ and $\{X_1, X_3, X_1^2, X_2 X_3\}$ are 0.7506701, 0.7506631 and 0.7485086, correspondingly.

	\item
	Is there much difference in $R_{a,p}^2$ for the three best subset models?

	There is little difference between them.
\end{enumerate}

\section*{Problem 3}
(Ex. 10.13) \textbf{Cosmetics sales}
\begin{enumerate}[a.]
	\item
	State the regression model to be employed and fit it to the data.

	First order multiple regression model is to be employed.
	\lstinputlisting{p3/13a.txt}
	Fitting the model to the data, we have $\hat{Y}=1.0233 + 0.9657 x_1 + 0.6292 x_2 + 0.6760 x_3$.

	\item
	Test whether there is a regression relation between sales and the three predictor variables; use $\alpha = .05$. State the alternatives, decision rule, and conclusion.

	The alternatives are
	\begin{align*}
		H_0 &: \beta_1 = \beta_2 = \beta_3 = 0\\
		H_a &: \text{not all } \beta_i=0, i=1,2,3
	\end{align*}
	The test statistic is
	\[
	F^* = \frac{MSR}{MSE}
	\]
	The decision rule is
	\begin{align*}
		\text{If } F^* \le F(1-\alpha; p-1, n-p) = 2.838745, \text{ conclude } H_0\\
		\text{If } F^* > F(1-\alpha; p-1, n-p) = 2.838745, \text{ conclude } H_a
	\end{align*}
	The ANOVA table is shown below:
	\lstinputlisting{p3/13b.txt}
	So, the test statistic $F^* = 38.28 > 2.838745$, we conclude $H_a$, that not all $\beta_i$ is zero.

	\item
	Test for each of the regression coefficients $\beta_k (k=1,2,3)$ individually whether or not $\beta_k=0$; use $\alpha = .05$ each time. Do the conclusions of these tests correspond to that obtained in part (b)?

	The alternatives are
	\begin{align*}
		H_0 &: \beta_k = 0\\
		H_a &: \beta_k \ne 0
	\end{align*}
	The test statistic is
	\[
	t^* = \frac{\hat{\beta}_k}{s\{\hat{\beta}_k\}}
	\]
	The decision rule is
	\begin{align*}
		\text{If } \abs{t^*} \le t(1-\alpha/2; n-p) = 2.021075, \text{ conclude } H_0\\
		\text{If } \abs{t^*} > t(1-\alpha/2; n-p) = 2.021075, \text{ conclude } H_a
	\end{align*}
	\lstinputlisting{p3/13c.txt}
	According to the table above, $t^*$ for each $\beta_k$ is smaller than 2.021075. Therefore, for each $\beta_k$, we conclude $H_0$. It doesn't correspond to the conclusion obtain in part (b).

	\item
	Obtain the correlation matrix of the $X$ variables.
	\lstinputlisting{p3/13d.txt}

	\item
	What do the results in parts (b), (c), and (d) suggest about the suitability of the data for the research objective?

	The results suggest that there exists serious multicollinearity in the data, so it's not suitable for the research objective.
\end{enumerate}

\section*{Problem 4}
(Ex. 10.20) Refer to \textbf{Lung pressure} Problem 9.13 and 9.14. The subset regression model containing first-order terms for $X_1$ and $X_2$ and the cross-product term $X_1 X_2$ is to be evaluated in detail.
\begin{enumerate}[a.]
	\item
	Obtain the residuals and plot them separately against $\hat{Y}$ and each of the three predictor variables. On the basis of these plots, should any further modifications of the regression model be attempted?
	\begin{figure}[H]
		\centering
		\begin{subfigure}[b]{.25\linewidth}
			\includegraphics[width=\linewidth]{p4/20a_yhat.png}
		\end{subfigure}%%
		\begin{subfigure}[b]{.25\linewidth}
			\includegraphics[width=\linewidth]{p4/20a_x1.png}
		\end{subfigure}%%
		\begin{subfigure}[b]{.25\linewidth}
			\includegraphics[width=\linewidth]{p4/20a_x2.png}
		\end{subfigure}%%
		\begin{subfigure}[b]{.25\linewidth}
			\includegraphics[width=\linewidth]{p4/20a_x1x2.png}
		\end{subfigure}
	\end{figure}
	The residual variance may not be constant as shown in the plots above. Transformation on some of the variables is necessary. Also, there seem to be outliers in the data.

	\item
	Prepare a normal probability plot of the residuals. Also obtain the coefficient of correlation between the ordered residuals and their expected values under normality. Does the normality assumption appear to be reasonable here?
	\begin{figure}[H]
		\centering
		\includegraphics[width=.4\linewidth]{p4/20b.png}
	\end{figure}
	The normal quantiles are calculated as
	\[
	z_i = \Phi^{-1} \pa{\frac{i-0.375}{n+0.25}}
	\]
	where $\Phi^{-1}(\cdot)$ is the inverse CDF of standard normal distribution. Then with the ordered residuals, we obtain 0.9633751 as the coefficient of correlation between them. Using table B.6, the critical value when $n=19$ using a 0.01 significance level is about 0.923. Since $0.9633751 > 0.923$, the normality assumption appear to be reasonable.

	\item
	Obtain the variance inflation factors. Are there any indications that serious multicollinearity problems are present? Explain.

	Calling \verb|vif| on the model, we have
	\lstinputlisting{p4/20c.txt}
	Since $\max{VIF} = 22.47 > 10$, multicollinearity problems exist.

	\item
	Obtain the studentized deleted residuals and identify any outlying $Y$ observations. Use the Bonferroni outlier test procedure with $\alpha = .05$. State the decision rule and conclusion.

	The alternatives are
	\begin{align*}
	H_0 &: \text{$Y_i$ is not an outlier}\\
	H_a &: \text{$Y_i$ is an outlier}
	\end{align*}
	Studentized deleted residual is given by
	\[
	t_i = e_i \pa{\frac{n-p-1}{SSE(1-h_{ii})-e_i^2}}^{1/2}
	\]
	The decision rule is
	\begin{align*}
	\text{If } \abs{t_i} \le t(1-\alpha/2n; n-p-1) = 3.64871, \text{ conclude } H_0\\
	\text{If } \abs{t_i} > t(1-\alpha/2n; n-p-1) = 3.64871, \text{ conclude } H_a
	\end{align*}
	The $t_i$'s are %rstudent()
	\lstinputlisting{p4/20d.txt}
	The absolute values of the $t_i$'s are all smaller than 3.64871. Therefore, we conclude $H_0$, that there is no outlying $Y$ observation at the $\alpha=0.05$ level. However, $t_7$, the largest residual among them, is close to 3.64871, and actually it would be identified as outlier with respect to $Y$ if $\alpha = .1$.

	\item
	Obtain the diagonal elements of the hat matrix. Using the rule of thumb in the text, identify any outlying $X$ observations. Are your findings consistent with those in Problem 9.13a? Should they be? Discuss.

	The diagonal elements of $H$ are %hatvalues()
	\lstinputlisting{p4/20e.txt}
	If $h_{ii}$ is larger than $2p/n = 0.4210526$, the corresponding observation is outlying in terms of $X$ values. Therefore, cases 3, 8, and 15 are outlying $X$ observations. The findings are consistent with those in Problem 9.13a and they should be. Because from the dot plots, we can see cases 3 and 8 are outlying in terms of $X_1$ and case 15 is outlying in terms of $X_2$.

	\item
	Cases 3, 8, and 15 are moderately far outlying with respect to their $X$ values, and case 7 is relatively far outlying with respect to its $Y$ value. Obtain $DFFITS$, $DFBETAS$, and Cook's distance values for these cases to assess their influence. What do you conclude?

	The $DFFITS$ values of these cases are %dffits()
	\lstinputlisting{p4/20f_dffits.txt}
	According to the guideline for small to medium sized data set, $\abs{DFFITS_7}$ exceeds 1 but is close enough to 1 so the case may not be influential enough; $\abs{DFFITS_8}$ indicates that case 8 may be influential enough to require remedial action.

	The $DFBETAS$ values of these cases are %dfbetas()
	\lstinputlisting{p4/20f_dfbetas.txt}
	As shown above, case 7 exceeds the guideline of 1 for small to medium sized data set for $\hat{\beta}_1$, but not by too much, indicating it's potentially influential but not influential enough to require remedial action. Case 8 exceeds 1 for each estimated coefficient so remedial action is clearly required.

	The Cook's distance values for these cases are %cooks.distance()
	\lstinputlisting{p4/20f_cook.txt}
	The percentile values of the corresponding $D_i$ from the $F(4, 15)$ distribution are %pf()
	\lstinputlisting{p4/20f_cookpf.txt}
	$D_8$ is the 99th($\gg$50th) percentile of the distribution so case 8 has a major influence on the regression fit.
\end{enumerate}

\end{document}


% --------------------------------------------------------------
% This is all preamble stuff that you don't have to worry about.
% Head down to where it says "Start here"
% --------------------------------------------------------------

\documentclass[12pt,answers]{exam}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{float}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{verbatim}
\usepackage{listings}
\usepackage[comma, authoryear]{natbib}
\bibliographystyle{plainnat}
\usepackage{mathtools}
\usepackage[shortlabels]{enumitem}
\usepackage{pythontex}
\usepackage[makeroom]{cancel}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{darkblue}{rgb}{0.1,0.1,1}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{darkblue},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=1pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\lstset{style=mystyle}

\graphicspath{{../figures/}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand\tab[1][1cm]{\hspace*{#1}}
\newcommand{\K}{\mathbb{K}}
\newcommand{\zero}{\mathbb{O}}

\newenvironment{theorem}[2][Theorem]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{lemma}[2][Lemma]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{exercise}[2][Exercise]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{reflection}[2][Reflection]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{proposition}[2][Proposition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{corollary}[2][Corollary]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}

\begin{document}

% --------------------------------------------------------------
%                         Start here
% --------------------------------------------------------------

%\renewcommand{\qedsymbol}{\filledbox}

\title{\textbf{Advanced Numerical Methods for PDEs}\\ \Large{Homework 1}}%replace X with the appropriate number
\author{Francisco Castillo}

\maketitle
Consider the initial - boundary value problem for the scalar advection diffusion equation
\begin{align}\label{eq:PDE}
\partial_tu(x,t)+a\partial_xu(x,t)-b\partial_x^2u(x,t)=0,~~~~
u(x,t=0)=u^I(x),
\end{align}
on the interval $x\in[-1, 1]$ with periodic boundary conditions $u(x + 2, t) = u(x, t), \forall x, t$.
Consider the explicit difference method
\begin{align}\label{eq:discrPDE}
U(x, t+\Delta t) = U(x, t)-\frac{a\Delta t}{ 2\Delta x}(T-T^{-1})U(x,t)+\frac{b\Delta t}{\Delta x^2}(T-2+T^{-1})U(x, t)
\end{align}
for the problem (1).
\section*{Problem 1}
\input{problem1}
\newpage
\section*{Problem 2}
\input{problem2}
\newpage
\section*{Problem 3}
\input{problem3}
\newpage
\section*{Problem 4}
\input{problem4}
\newpage
\section*{Appendix}
\input{appendix}

\end{document}


\documentclass[../PHYS306Notes.tex]{subfiles}

\begin{document}
\subsection{Worksheet - Rotating Coordinate Systems}
\begin{center}
    \includegraphics[scale=0.75]{Lecture-15/w15-img1.png}
\end{center}
\begin{p}
What is the time rate of change, $\od{\v{r}}{t} = \v{v}$ of a vector $\v{r}$ to a point in a body that is rotating about an axis $O$ with angular velocity $\omega$?
\end{p}
\begin{s}
We define the rotation vector:
\[\bm{\omega} = \omega\hat{\v{u}}\]
Where $\hat{\v{u}}$ points along the rotation axis (the sign is determined by the RHR). Geometry tells us that the distance from the rotation axis $\rho$ is given by:
\[\rho = r\sin\theta\]
Hence the velocity of this point is given by:
\[v = \rho\omega = r\sin\theta\omega\]
Generalizing this to the vector form, we have:
\[\v{v} = \bm{\omega} \times \v{r} = \dot{\v{r}}\]
\end{s}

\begin{p}
Consider a body rotating in a reference frame that is itself rotating with respect to a fixed reference frame. Show that angular velocities add just like linear velocities.
\end{p}
\begin{s}
This is a consequence of the linearity of the cross product. Suppose we have:
\[\v{v}_{31} = \bm{\omega}_{31}\times \v{r} = \v{v}_{32} + \v{v}_{21} = \bm{\omega}_{32}\times \v{r} + \bm{\omega}_{21}\times \v{r} = (\bm{\omega}_{32} + \bm{\omega}_{21})\times \v{r}\]
Hence:
\[\bm{\omega}_{31} = \bm{\omega}_{32} + \bm{\omega}_{21}\]
So we can see that we can add angular velocities just like linear velocities.
\end{s}

\begin{p}
Consider a vector $\v{Q}$, which may be a position, velocity, or force vector. Show that the time rate of change of $\v{Q}$, $\od{\v{Q}}{t}$ in a fixed frame of reference $S_0$ is related to $\od{\v{Q}}{t}$ in a rotating frame of reference $S$ by:
\[\left(\dod{\v{Q}}{t}\right)_{S_0} = \left(\dod{\v{Q}}{t}\right)_{S} + \v{\Omega} \times \v{Q}\]
\end{p}
\begin{s}
\begin{center}
    \includegraphics[scale=0.75]{Lecture-15/w15-img2.png}
\end{center}
We express the unit vectors in the rotating frame by transforming the unit vectors in the fixed frame:
\[\xhat = \xhat_0\cos\omega t + \yhat_0\sin\omega t\]
\[\yhat = \yhat_0\cos\omega t - \xhat_0\sin\omega t\]
So expressing an arbitrary vector $\v{Q}$ in the rotating frame, we have:
\[\v{Q} = Q_x\xhat + Q_y\yhat + Q_z\zhat\]
Now expressing this in terms of the unit vectors in the stationary frame, we have:
\[\v{Q} = (Q_x\cos\omega t - Q_y\sin\omega t)\xhat_0 + (Q_x\sin\omega t + Q_y\cos\omega t)\yhat_0 + Q_z\zhat_0\]
We pick $\zhat$ to be the rotation axis, so $\zhat = \zhat_0$ by our choice of coordinate system. Looking at the rate of change w.r.t the lab frame, we have:
\[\left(\dod{\v{Q}}{t}\right)_{S_0} = \left(-\omega Q_x\sin\omega t - \omega Q_y\cos\omega t\right)\xhat_0 + \left(\omega Q_x\cos\omega t - \omega Q_y \sin \omega t\right)\yhat_0 \]
Where the unit vectors do not have time dependence, only the $\cos\omega t$ and $\sin\omega t$ terms do. Looking at this expression, we can see that this can be written as the expression:
\[\left(\dod{\v{Q}}{t}\right)_{S_0} = \omega(\zhat_0 \times \v{Q}) = \bm{\omega} \times \v{Q}\]
Now, we take the derivative in the rotating frame:
\[\left(\dod{\v{Q}}{t}\right)_{S} = \dot{Q}_x\xhat + \dot{Q}_y \yhat + \dot{Q}_z \zhat\]
Now, we consider what is $\left(\od{Q}{t}\right)_{S_0, x}$. By the chain rule:
\[\left(\od{Q}{t}\right)_{S_0, x} = \dod{}{t}\left(Q_x\cos\omega t - Q_y\sin\omega t\right)\xhat_0\]
\[\left(\od{Q}{t}\right)_{S_0, x} = \left[\dot{Q}_x\cos\omega t - \dot{Q}_y\sin\omega t) + (-\omega Q_x\sin\omega t - \omega Q_y\cos\omega t)\right]\xhat_0\]
Doing the same for the $x$ and $y$ components, we get the simple formula:
\[\left(\dod{\v{Q}}{t}\right)_{S_0} = \left(\dod{\v{Q}}{t}\right)_{S} + \bm{\omega}\times\v{Q}\]
\end{s}

\begin{p}
Now find the acceleration in a fixed frame of reference, and thus write the modified Newton’s law for a constantly rotating frame of reference, as we’d experience on the earth for example.
\end{p}
\begin{s}
The goal is to find the second derivative of the position vector in the $S$ (rotating) frame. First calculating $\left(\od[2]{\v{r}}{t}\right)_{S_0}$, we have:
\[\left(\dod[2]{\v{r}}{t}\right)_{S_0} = \left(\dod{}{t}\right)_{S_0}\left(\dod{\v{r}}{t}\right)_{S_0}\]
So applying the result from above twice, we have:
\[\left(\dod[2]{\v{r}}{t}\right)_{S_0} = \left(\dod{}{t}\right)_{S_0}\left(\left(\dod{\v{r}}{t}\right)_S + \bm{\Omega} \times \v{r}\right)\]
\[\left(\dod[2]{\v{r}}{t}\right)_{S_0} = \left(\dod{}{t}\right)_{S}\left(\left(\dod{\v{r}}{t}\right)_S + \bm{\Omega} \times \v{r}\right) + \bm{\Omega}\times \left(\left(\dod{\v{r}}{t}\right)_S + \bm{\Omega} \times \v{r}\right)\]
We now simplify this expression. Henceforth, we will use the dot notation to refer to a derivative in $S$. \[\left(\dod[2]{\v{r}}{t}\right)_{S_0} =
\ddot{\v{r}} + \dot{\bm{\Omega}}\times \v{r} + \bm{\Omega} \times \dot{\v{r}} + \bm{\Omega} \times \dot{\v{r}} + \bm{\Omega} \times (\bm{\Omega} \times \v{r})\]
Multiplying both sides by $m$, and simplifying using the antisymmetry of the cross product ($\v{a} \times \v{b} = -\v{b} \times \v{a}$) have:
\[m\left(\dod[2]{\v{r}}{t}\right)_{S_0} = \v{F} =
m\ddot{\v{r}} - 2m\dot{\v{r}}\times\bm{\Omega} - m(\dot{\v{r}}\times \dot{\bm{\Omega}}) - m(\bm{\Omega} \times \v{r}) \times \bm{\Omega}\]
Where we use the identity that the LHS is just the total force on the system by Newton's second law. Rearranging this, we have:
\[m\ddot{\v{r}} = \v{F} + 2m\dot{\v{r}}\times \bm{\Omega} + m(\bm{\Omega} \times \v{r})\times \bm{\Omega} + m(\v{r} \times \dot{\bm{\Omega}})\]
Where the first term is the sum of the (real) forces on the system, the second term is the coriolois force, the third term is the centrifugal force, and the fourth term (which is nonzero only if the angular velocity is changing in time) is the Euler force. We may write this as:
\[m\ddot{\v{r}} = \v{F} + \v{F}_{cor} + \v{F}_{cent} + \v{F}_{euler}\]
We note that we can also derive this using the Lagrangian approach! If we write the Lagrangian with the correct velocity, i.e.:
\[\LL = \frac{m}{2}\left|\left(\dod{r}{t}\right)_{S_0}\right|^2 - U(\v{r})\]
Then we will find that we recover the same result.

\end{s}
\end{document}


\section{The real projective spaces}

\subsection{The type of \texorpdfstring{$2$}{2}-element sets}

\begin{thm}
  The type
  \begin{equation*}
    \sm{X:\UU_\bool}X
  \end{equation*}
  is contractible.
\end{thm}

\begin{cor}
  For any $2$-element type $X$, the map
  \begin{equation*}
    (\bool=X)\to X
  \end{equation*}
  given by $p\mapsto\tr_{\mathcal{T}}(p,\btrue)$ is an equivalence.
\end{cor}

\subsection{Classifying real line bundles}

\subsection{The finite dimensional real projective spaces}