---
title: "3.2 The Method of Maximum Likelihood"
format:
  html:
    self-contained: TRUE
---

## Understanding `dnorm()`

<iframe width="573" height="398" src="https://www.youtube.com/embed/HJPBNSTdgwg" title="ML 3.2.1 dnorm() in R" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

https://youtu.be/HJPBNSTdgwg

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
1) Let $X$ be a random variable with a normal distribution, mean 0, standard deviation 1 ($X \sim N(0, 1)$). Calculate the PDF of $X$ evaluated at 0. To do this, use two methods to check your work: one method is to use `dnorm()` in R, and the other method is to use the equation for the pdf of the normal distribution: $f(X) = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp \left( \frac{(X - \mu)^2}{-2\sigma^2} \right)$, where the mean $\mu = 0$ and the variance $\sigma^2 = 1$.
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
</div></br>

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
2) Use `dnorm()` to show that .0965 is the *joint probability density* for two independent random variables $X_1$ and $X_2$, both distributed $N(0, 1)$, where $X_1 = 0$ and $X_2 = 1$.
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
</div>

## Maximum Likelihood

<iframe width="573" height="398" src="https://www.youtube.com/embed/ZLnf6YFtmP0" title="ML 3.2.2 Maximum Likelihood Intuition/Proof" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

https://youtu.be/ZLnf6YFtmP0

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
3) Again let $X \sim N(\mu, 1)$. Suppose X was observed to take on the values 0 and 2. Find, using all the math steps for the method of maximum likelihood, the most likely value for $\mu$.
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
\begin{align}
\end{align}
</div>

## Logit Estimation

<iframe width="573" height="398" src="https://www.youtube.com/embed/OuYUcYmL6nM" title="ML 3.2.3 Log Likelihood Function" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

https://youtu.be/OuYUcYmL6nM

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
4) What is the log likelihood of seeing these six data points given $\beta_0 = -1$ and $\beta_1 = 0.05$? Is it more or less likely than $\beta_0 = 1$ and $\beta_1 = 2$? You can use my LL function:

```{r}
LL <- function(params) {
  
  b0 <- params[1]
  b1 <- params[2]
  
  log(1 / (1 + exp(b0))) +
    log(1 / (1 + exp(b0 + b1 * 20))) +
    log(exp(b0 + b1 * 40) / (1 + exp(b0 + b1 * 40))) +
    log(exp(b0 + b1 * 60) / (1 + exp(b0 + b1 * 60))) +
    log(1 / (1 + exp(b0 + b1 * 80))) +
    log(exp(b0 + b1*100) / (1 + exp(b0 + b1 * 100)))
}
```
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
</div>

To finish the logit estimation, we'll let the function `maxLik()` handle searching for the values of $\hat{\beta_0}$ and $\hat{\beta_1}$ that give the largest likelihood of seeing the data. You'll need to install maxLik before using it: `install.packages("maxLik")`, but make sure to not include code to install a package in your document: it will prevent the compilation of the document.

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
5) `maxLik` takes as arguments a log likelihood function and a starting point for its search. Fill in the blank and run the code. Does our procedure match the logit that the function `glm()` finds?
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
```{r, message = F}
library(maxLik)
library(tidyverse)


```

```{r}
tibble(
    discount = seq(0, 100, by = 20),
    purchase = c(0, 0, 1, 1, 0, 1)
) %>%
  glm(purchase ~ discount, data = ., family = "binomial") %>%
  broom::tidy(conf.int = TRUE)
```
</div></br>

<div style="background-color:#a4f5ed; border-width:3px; border-style:solid; border-color:#a4f5ed; padding: 1em;">
6) Imperative programming practice: Complete Project Euler problem 2: even fibonacci numbers.
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
```{r}
```
</div>

