# Unit 1: Practice Exam

## Data

Run this code to get started.

```{r, message = F}
library(tidyverse)
library(maxLik)

data <- read_csv("https://raw.githubusercontent.com/cobriant/teaching-datasets/refs/heads/main/logit.csv")
```

**Question 1: Write code to explore the data set.** 

Write code to view the data set in a new tab, to find the column names, and find the number of rows.

```{r}

```

**Question 2: Dplyr. Write code to answer each of these questions:**

- How many people did we offer a discount of 20% to?
- Among those people, what fraction made a purchase?
- For each discount level, what fraction made a purchase?

```{r}

```

**Question 3: Linear Probability Model**

Visualize the linear probability model for the data set above, estimate it, and interpret your estimates.

```{r}

```

- What is the main problem with the linear probability model?
- The estimate for the probability of purchase when we offer a discount of 0 is: ____, which (is/is not) statistically significantly different from 0.
- When the discount increases by 1%, the probability of purchase increases by ____, which (is/is not) statistically significantly different from 0.

**Question 4: Logit**

Visualize the logit for the data set above, estimate it, and interpret your estimates.

```{r}
# data %>%
#   ggplot(aes(x = ____, y = ____)) +
#   geom_jitter(width = 8, height = .1) +
#   geom_smooth(method = "glm", method.args = list(family = "binomial"), se = F) +
#   labs(title = "Logit")
```

Recall that the logistic function is:

$$p(x) = \frac{1}{1 + e^{-(\beta_0 + \beta_1 x)}}$$

And the log likelihood of seeing the data that we did given a guess for $\beta_0$ and $\beta_1$ is:

$$LL(\beta_0, \beta_1) = \sum_i y_i \log(p_i) + (1 - y_i) \log(1 - p_i)$$

```{r}
# LL <- function(beta) {
#   b0 <- beta[1]
#   b1 <- beta[2]
# 
#   x <- data %>% pull(____)
#   y <- data %>% pull(____)
# 
#   p <- ____
# 
#   sum(____)
# }

# maxLik(
#   logLik = ____,
#   start = c(0, 0)
# )
```

Your estimates for $\beta_0$ and $\beta_1$: 
b0: ____
b1: ____

What is the estimated probability of purchase when we give a discount of 50%?

What is the estimated probability of purchase when we give a discount of 100%?

**Question 5: Centipede Game**

Consider this centipede game with 2 players (A and B). If a player takes (T), the game ends and the payoffs are distribted (A, B). If the player passes, the game continues. Find the subgame perfect equilibrium.

A ●
  \ T → (2,1)
   \
    ● B
      \ T → (1,2)
       \
        ● A
          \ T → (3,3)
           \
            ● B
              \ T → (2,4)

SPE:

- In the first decision node, player A ____.
- In the second decision node, player B ____.
- In the third decision node, player A ____.
- In the fourth decision node, player B ____.

**Question 6: GridWorld**

Consider this GridWorld. Write down the policy function and the value function, assuming agents value payoffs one step ahead at 90% of the value of present payoffs.

┌───┬───┬───┐
│ 0 │ 2 │ 0 │
├───┼───┼───┤
│ 0 │ 1 │ 0 │
├───┼───┼───┤
│ 0 │ 0 │ 0 │
└───┴───┴───┘

| Cell             | Payoff | Policy | Value |
|------------------|--------|--------|-------|
| 1 (upper left)   | 0      |        |       |
| 2 (upper middle) | 2      |        |       |
| 3 (upper right)  | 0      |        |       |
| 4 (middle left)  | 0      |        |       |
| 5 (center)       | 1      |        |       |
| 6 (middle right) | 0      |        |       |
| 7 (bottom left)  | 0      |        |       |
| 8 (bottom middle)| 0      |        |       |
| 9 (bottom right) | 0      |        |       |
