
---
title: "STAT340 HW1: Random Variables and R Review"
author: "Keith Levin and Bi Cheng Wu"
date: "September 2021"
output: html_document
---

[link to source](hw01.Rmd)

## Problem 1 (10 points): Generating random variables

Let's se R to generate a single Binomial, Geometric, and Poisson random variable.

 You can choose the parameter of the distribution to be anything you like.  

Here are the function names: `rbinom, rgeom, rpois`.
Remember that the "r" stands for "Random".

### 1a) Read the help files for these functions. What are the 8 headings in each of the help files? 

Reading the help files is hard at first, but it is an essential skill.  Have a look at the help files for these functions.  What are the 8 headings?
```{r}
?rbinom
?rgeom
?rpois
```

You can type your (short!) answer here:

### 1b) Generate a single copy of each random variable. Once again, you may set parameters however you like.

```{r}

# generate a single Binomial random variable:
rbinom(n = 1, size = 10, prob = .3)

# generate a single Geometric random variable:

# generate a single Poisson random variable:
```


### 1c) Generate ten copies of each random variable.

```{r}
# generate 10 Binomial random variables:

# generate 10 Geometric random variables:
rgeom(n=10, p=0.25)

# generate 10 Poisson random variables:

```

## Problem 2 (10 points): Making histograms

One way to think about random variables is through their "distribution".
To see what the distributions of different random variables look like, generate 1,000 copies of some random variables (indicated in the code below) and put them in a histogram:

```{r}
# Exponential:
hist(rexp(n = 1000, rate=1/5))
# or 
library(magrittr)
rexp(10000, rate=1/5) %>% hist

# Binomial:


# Normal:


```

Repeat the last step, but change the value of the parameter that you put in.  

```{r}

# Exponential:
hist(rexp(n = 1000, rate = 1))
# or 
library(magrittr)
rexp(1000, rate=1) %>% hist

# Binomial: (change p, not the size parameter)


# Binomial: (change the size parameter, not p)


# Normal: (change the mean, not the standard deviation)


# Normal: (change the standard deviation, not the mean)


```


## Problem 3 (5 points): What happens to the histogram in Problem 2 above when the parameter gets bigger or smaller?  

Answer in words:

1) Exponential: the histogram "expands out to the right" as the rate decreases.
2) Binomial (changing p): ...
3) Binomial (changing n): ...
4) Normal (changing mu): ...
5) Normal (changing sigma): ...

## Problem 4 (10 points): Evaluating probability mass/density functions

Now let's answer the same question, but this time using the built-in R functions for evaluating probability distributions.
That is, instead of generating random variables to approximate the true distribution, let's evaluate the probability mass or density exactly.

```{r}
# Exponential, for values k=0,1,2,...,20:
# 
k <- seq(0,20);
plot( k, dexp(k,rate=1) )

# Binomial(n=10,p=0.3), for values k=0,1,2,...,9,10:

#TODO: write code here.

# Normal(mean=0,sd=1), for x in [-3,3] in increments of size 0.1
x <- seq(-3,3,0.1);
# Note that this is a density, not a mass function,
# but for plotting purposes, we can't evaluate the pdf at
# EVERY point, so we'll just choose a few.
plot( x, dnorm(x,mean=0,sd=1))

# Normal(mean=1,sd=2), for x in [-5,7] in increments of size 0.1

#TODO: write code here.
```



## Problem 5 (15 points): Defining some simple functions in R.

This is not a programming course, but it's important that you become familiar with some basic programming concepts this semester.
Toward that end, let's close by getting some practice with function definition in R.

```{r}
# 5a) Define a function sum_of_normals that takes a single argument n (you may assume that n is a non-negative integer), generates n independent normals with mean 0 and variance 1, and returns their sum.
# Hint: pay attention to the case n=0. What should be the sum of zero normals?
sum_of_normals <- function( n ){
  #TODO: CODE GOES HERE.
}

# 5b) Define a function sum_of_poissons that takes two arguments: a non-negative integer n and a positive real lambda.
# Your function should generate n independent Poisson random variables with shared parameter lambda, and return their sum.
# You may assume that n is a non-negative integer and lambda is a positive real (i.e., numeric)
# n and lambda should both default to 1, so that sum_of_poissons(5) returns the sum of five Pois(1) random variables.
sum_of_poissons <- function( TODO: arguments! ) {
  # TODO: CODE GOES HERE!
}

# 5c) Define a function max_of_normals that takes three arguments:
# a non-negative integer n
# a real number (i.e., numeric) mu
# a positive real (i.e., positive numeric) sigma
# Your function should generate n independent normals with mean and sd given by mu and sigma, respectively, and return the maximum of those n normal RVs.
# mu should default to 0 and sigma should default to 1. There should be no default value for n.
# Hint: be careful of the case n=0. By convention, the maximum of an empty set (e.g., the maximum of a collection of zero normals) 
# By convention, the maximum of an empty set is negative infinity.

#TODO: define the function max_of_normals.
```
