---
title: "ch2 Distributions"
output: pdf_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

# Uniform(a,b) distribution
Let $a< b$ be real numbers. The Uniform distribution has p.d.f. 
$$
f(x)=\begin{cases}\frac{1}{b-a} & a<x<b\\ 0 & \text{otherwise}\\\end{cases}
$$

## p.d.f.
```{r}
x=seq(0,4,by = 0.01)
plot(x,dunif(x,1,3))
```

## c.d.f.
```{r}
plot(x,punif(x,1,3))
```

## quantiles
```{r}
q=seq(0,1,by = 0.01)
plot(q,qunif(q,1,3))
```

## Random sample
```{r}
x=runif(100,1,3)
plot(x)
x=runif(10000,1,3)
hist(x,10)
```


# Exponential($\lambda$) distribution
Let $\lambda>0$ be a positive number. The exponential($\lambda$) distribution has p.d.f. 
$$
f(x)=\begin{cases} \lambda \exp(-\lambda x)& x>0\\ 0 & \text{otherwise}\\\end{cases}
$$

## p.d.f.
```{r}
x=seq(0,4,by = 0.01)
plot(x,dexp(x,rate=2))
plot(x,dexp(x,rate=2,log=T))
```

## c.d.f.
```{r}
plot(x,pexp(x,2))
```

## quantiles
```{r}
q=seq(0,1,by = 0.01)
plot(q,qexp(q,2))
```

## Random sample
```{r}
x=rexp(100,2)
plot(x)
x=rexp(10000,2)
hist(x,10)
```



# Normal($\mu,\sigma$) distribution
Let $\mu$ be a number and let $\sigma$ be a positive number. The N($\mu,\sigma^2$) distribution has p.d.f. 
$$
f(x)=\frac{1}{\sigma\sqrt{2\pi}}\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)
$$

## p.d.f.
```{r}
x=seq(0,4,by = 0.01)
plot(x,dnorm(x,2,0.3))
plot(x,dnorm(x,2,0.3,log=T))
```

## c.d.f.
```{r}
plot(x,pnorm(x,2,0.3))
```

## quantiles
```{r}
q=seq(0,1,by = 0.01)
plot(q,qnorm(q,2,0.3))
```

## Random sample
```{r}
x=rnorm(100,2,0.3)
plot(x)
x=rexp(10000,2)
hist(x,10)
```

## qq plot
```{r}
x=rnorm(100,2,0.3)
q=seq(1/200,199/200,by=1/100)
plot(qnorm(q,2,0.3),sort(x))
abline(0,1)
qqnorm(x)
qqline(x)
```

## Brownian motion
```{r}
x=rnorm(1000,0,0.1)
plot(seq(0,length.out =1000, by=0.1),cumsum(x))
```

## Biased Brownian motion
```{r}
x=rnorm(1000,0.03,0.1)
plot(seq(0,length.out =1000, by=0.1),cumsum(x))
```

# Binomial($n,p$) distribution
Let $n$ be a positive integer $p\in[0,1]$. The Bin($n,p$) distribution has p.m.f
$$
f(k)=\binom{n}{k} p^k (1-p)^{n-k}, \qquad k=0,1,\dots,n.
$$

## p.m.f.
```{r}
x=0:10
plot(x,dbinom(x,10,0.25))
```

## c.d.f.
```{r}
plot(x,pbinom(x,10,0.25))
```

## quantiles
```{r}
q=seq(0,1,by = 0.001)
plot(q,qbinom(q,10,0.25))
```

## Random sample
```{r}
x=rbinom(100,10,0.25)
plot(x)
x=rbinom(10000,10,0.25)
plot(table(x))
```


# Poisson($\lambda$) distribution
Let $n$ be a positive integer $p\in[0,1]$. The Bin($n,p$) distribution has p.m.f
$$
f(k)=\exp(-\lambda)\frac{\lambda^k}{k!}
$$

## p.m.f.
```{r}
x=0:10
plot(x,dpois(x,3))
```

## c.d.f.
```{r}
plot(x,ppois(x,3))
```

## quantiles
```{r}
q=seq(0,1,by = 0.001)
plot(q,qpois(q,3))
```

## Random sample
```{r}
x=rpois(100,3)
plot(table(x))
x=rpois(10000,3)
plot(table(x))
```

##Arrival process
```{r}
x=rpois(1000,0.01)
plot(cumsum(x))
```
