---
title: "Math 339SP -- R Markdown template"
author: "Tim Chumley"
date: "January 22, 2040"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tinytex)
library(expm)
```
# Markdown tips
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r chunk1}
# this is a comment, no code gets run on this line
set.seed(1) # setting the seed guarantees we all get the same "random" number
sample(1:6, size = 2, replace = TRUE) # simulation of rolling a die twice
sample(1:6, size = 2, replace = FALSE) # simulation of sampling without replacement
```
## Including Plots
You can also embed plots, for example:
```{r chunk2, echo=FALSE}
plot(x = 1:10, y = (1:10)^2, xlab = "input", ylab = "output")
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
# Matrix tips
We'll spend time working with matrices and powers of matrices this semester, so it will be useful
to know how to enter them into R and multiply them.
```{r}
p = 0.3
q = 0.2
A = matrix(c(1-p-q, p, q,
q, 1-p-q, p,
p, q, 1-p-q), nrow = 3, ncol = 3, byrow = TRUE)
A # print out A
A %*% A # multiply A*A
A %^% 2 # equivalently, raise A to the second power
A %^% 100 # raise A to the 10th power
```
# \LaTeX tips
Markdown documents support \LaTeX, a syntax for writing mathematical notation. Here are some useful commands for writing math.
This is how you can use math mode within a sentence,
which is sometimes called in-line mode: $A\subseteq \Omega$.
We can also make math appear centered and on its own line.
This is often called a *displayed* equation: $$P(A) = \frac{1}{2}.$$
Remember, when writing math, punctuation matters.
Even displayed equations should have a period if they end a sentence.
For example, we have $$\int_{-\infty}^{\infty} e^{-x^2/2} = \sqrt{2\pi}.$$
Notice that
you need
a blank space in between lines of code to start a new paragraph.
## Typesetting matrices
Here is some syntax for typesetting a transition matrix $P$:
$$P = \begin{bmatrix}
0.1 & 0.3 & 0.6 \\
0 & 0.4 & 0.6 \\
0.3 & 0.2 & 0.5 \\
\end{bmatrix}$$
The \& symbols are used to separate the entries and the slash symbols make a new row.
There are special packages for labeling the rows and columns but we'll get to those as needed.
## Align environment
For equations that span several lines, we use the align environment. The \texttt{\&} symbol tells where to anchor each line of the equation. The double backslash makes a new line.
We claim that when $X \sim Bin(n,p)$ is binomially distributed with parameters $n$ and $p$, its expection is given by $E[X] = np$. Indeed, observe that
\begin{align*}
E[X] &= \sum_{k = 1}^n k P(X = k) \\
&= \sum_{k = 1}^n k {n \choose k} p^k (1-p)^{n-k} \\
&= \sum_{k = 1}^n \frac{n!}{(k-1)!(n-k)!} p^k (1-p)^{n-k} \\
&= np.
\end{align*}
### Long equations
Sometimes you have an expression that's too long and cannot fit on one line.
The align environment is good for putting it on two separate lines and having
everything line up nicely. Notice that the command \texttt{\char`\\hspace\char`\{1em}} inserts a horizontal space that is as wide as the capital M letter.
\begin{align*}
P(A \cup B \cup C)
&= P(A) + P(B) - P(AB)\\
&\hspace{1em} + P(C) - [P(AC) + P(BC) - P(ABC)]
\end{align*}
## Equation labels
For equations that you'd like to label and refer back to, there are a couple of options. The first is the \texttt{equation} environment, along with the \texttt{label} and \texttt{eqref} commands:
\begin{equation}
|\sin x|\leq 1 \label{eq:sine-bound}
\end{equation}
The displayed expression \eqref{eq:sine-bound} gets labeled with a numbered value, so that you can refer back to in the main body discussion.
You can do this with the \texttt{align} environment as well. Notice that leaving out the asterisk introduces number labels to each line:
\begin{align}
|a+b|^2 &= (a+b)^2 \label{eq:square-abs-val} \\
&= a^2 + 2ab + b^2 \nonumber \\
&= |a|^2 + 2ab + |b|^2 \nonumber \\
&\leq |a|^2 + 2|a||b| + |b|^2 \label{eq:abs-val-inequality} \\
&= (|a|+|b|)^2. \nonumber
\end{align}
This way, you can again reference expressions \eqref{eq:abs-val-inequality} and \eqref{eq:square-abs-val} and not worry about keeping track of the numbers.
\newpage
## Problem ABC
You can start a new page with the \texttt{\char`\\newpage} command.