---
title: "Classification Lab (stub)"
format: html
---

Stub of the Classification Lab page: the exercises, without the solutions. Solutions: https://morrison-lab.github.io/lds/chapters/classification-lab.html

#### Lab: the share of up days

Use the [`Smarket` data](#dat-smarket).
What [proportion](https://morrison-lab.github.io/sds/classification.html#def-prevalence) of the trading days has `Direction` equal to `Up`?

::: {.callout-tip}

#### Hint

The [counting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) tally each value of a column,
and a tally divided by the number of rows is a [proportion](https://morrison-lab.github.io/sds/classification.html#def-prevalence).

:::

```{python}
# your code here
```

#### Lab: correlations among the lagged returns

Use the [`Smarket` data](#dat-smarket).
Among every two different columns from `Lag1` to `Lag5`,
what is the largest [correlation](https://morrison-lab.github.io/pds/variance-covariance.html#def-correlation) in absolute value?

::: {.callout-tip}

#### Hint

The [summary functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) of a data frame include its [correlation](https://morrison-lab.github.io/pds/variance-covariance.html#def-correlation) [matrix](https://morrison-lab.github.io/mds/linear-algebra-matrices.html#def-matrix).
Each pair of columns appears twice in it,
and each column has correlation $1$ with itself.

:::

```{python}
# your code here
```

#### Lab: scatterplots of the lagged returns

Use the [`Smarket` data](#dat-smarket).
Draw a [scatterplot](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-scatterplot) of every two different columns from `Lag1` to `Lag5`,
with the points colored by `Direction`.

::: {.callout-tip}

#### Hint

In R, the `ggpairs()` [function](https://morrison-lab.github.io/mcs/functions.html#def-function) of the `GGally` package draws all the [scatterplots](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-scatterplot) of a data frame's columns in one grid.
In Python, the `scatter_matrix()` function of `pandas.plotting` does the same.

:::

```{python}
# your code here
```

#### Lab: a histogram of the previous day's return

Use the [`Smarket` data](#dat-smarket).
Draw a histogram of `Lag1`.

::: {.callout-tip}

#### Hint

The [plotting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) draw a histogram of one column.

:::

```{python}
# your code here
```

#### Lab: trading volume by year

Use the [`Smarket` data](#dat-smarket).
In which `Year` is the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of `Volume` the largest?

::: {.callout-tip}

#### Hint

[Split a column by the values of another](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) before taking the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean),
then find the group with the largest result.

:::

```{python}
# your code here
```

#### Lab: trading volume over time

Use the [`Smarket` data](#dat-smarket).
Plot `Volume` against the position of the trading day in the data, from the first day to the last.

::: {.callout-tip}

#### Hint

The [plotting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) draw a scatterplot,
and the [row numbers](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidyverse-more) are a sequence of whole numbers from the first row to the last.

:::

```{python}
# your code here
```

#### Lab: the previous day's return by direction

Use the [`Smarket` data](#dat-smarket).
Compute the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of `Lag1` within each value of `Direction`.

::: {.callout-tip}

#### Hint

[Split a column by the values of another](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) before taking the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of each group.

:::

```{python}
# your code here
```

#### Lab: boxplots of the previous day's return by direction

Use the [`Smarket` data](#dat-smarket).
Draw side-by-side boxplots of `Lag1`, one for each value of `Direction`.

::: {.callout-tip}

#### Hint

The [boxplot methods](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-matplotlib-more) take one array of values for each group,
and you can [select the rows of each group with a mask](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas).

:::

```{python}
# your code here
```

#### Lab: logistic regression on the stock market data

Use the [`Smarket` data](#dat-smarket).
[Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a [logistic regression](https://morrison-lab.github.io/lds/chapters/logistic-regression.html#def-logistic-regression) for the probability that `Direction` is `Up`, using all the days.
Take `Lag1` to `Lag5` and `Volume` as the [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature).
Report each [coefficient's](https://morrison-lab.github.io/lds/chapters/logistic-regression.html#def-logistic-regression) [estimate](https://morrison-lab.github.io/sds/estimation.html#def-estimate), [standard error](https://morrison-lab.github.io/sds/estimation.html#def-SE), and [$p$-value](https://morrison-lab.github.io/sds/inference.html#def-p-value).

::: {.callout-tip}

#### Hint

The [`family` argument](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) chooses the [logistic regression](https://morrison-lab.github.io/lds/chapters/logistic-regression.html#def-logistic-regression),
and the [summary of the fit](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) holds each [estimate](https://morrison-lab.github.io/sds/estimation.html#def-estimate), [standard error](https://morrison-lab.github.io/sds/estimation.html#def-SE), and $p$-value.

:::

```{python}
# your code here
```

#### Lab: logistic regression on the stock market data (part 2)

Use the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) of the [logistic regression](https://morrison-lab.github.io/lds/chapters/logistic-regression.html#def-logistic-regression) in Exercise 9.
Which [predictor](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) has the smallest [$p$-value](https://morrison-lab.github.io/sds/inference.html#def-p-value)?

::: {.callout-tip}

#### Hint

The [summary of the fit](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) holds one $p$-value for each [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter).

:::

```{python}
# your code here
```

#### Lab: test the six predictors together

Use the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) of Exercise 9.
Take the drop from the [null deviance](https://morrison-lab.github.io/sds/logistic-inference.html#def-null-deviance) to the [deviance](https://morrison-lab.github.io/sds/logistic-inference.html#def-deviance) of that fit.
What is the $p$-value of the drop, under the [distribution of the drop](https://morrison-lab.github.io/sds/logistic-inference.html#thm-deviance-drop)?

::: {.callout-tip}

#### Hint

The [fitted results](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) hold the [deviance](https://morrison-lab.github.io/sds/logistic-inference.html#def-deviance) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) and its [null deviance](https://morrison-lab.github.io/sds/logistic-inference.html#def-null-deviance),
and the [distribution of the drop](https://morrison-lab.github.io/sds/logistic-inference.html#thm-deviance-drop) is a chi-squared distribution,
whose [tail probability](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-scipy) SciPy computes.
In R, `pchisq()` plays that role.

:::

```{python}
# your code here
```

#### Lab: a logistic regression with fewer predictors

Use the split of Exercise 16.
[Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a [logistic regression](https://morrison-lab.github.io/lds/chapters/logistic-regression.html#def-logistic-regression) for `Direction` on the days before $2005$,
with only `Lag1` and `Lag2` as [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature).
[Predict](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) `Up` on a day in $2005$ when the [predicted probability](https://morrison-lab.github.io/sds/classification.html#def-predicted-probability) is above $0.5$.
What is the [accuracy](https://morrison-lab.github.io/lds/chapters/first-model.html#def-accuracy) on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set)?

::: {.callout-tip}

#### Hint

[Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) the [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) with the [`family` argument](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) set to the binomial family,
then compare the [predicted probabilities](https://morrison-lab.github.io/sds/classification.html#def-predicted-probability) on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) with $0.5$.

:::

```{python}
# your code here
```

#### Lab: the confusion matrix on the test set

Use the [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) of Exercise 12.
Tabulate the [predicted](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) `Direction` against the actual `Direction` on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set), as a [confusion matrix](https://morrison-lab.github.io/lds/chapters/metrics.html#def-confusion-matrix).

::: {.callout-tip}

#### Hint

The [counting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) tabulate each pair of values of two columns.
The [confusion matrix](https://morrison-lab.github.io/lds/chapters/metrics.html#def-confusion-matrix) lists the [predicted](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) [classes](https://morrison-lab.github.io/sds/classification.html#def-class) against the actual ones.

:::

```{python}
# your code here
```

#### Lab: the always-`Up` rule on the test set

Use the split of Exercise 16.
What is the [accuracy](https://morrison-lab.github.io/lds/chapters/first-model.html#def-accuracy) on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) of the rule that [predicts](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) `Up` on every day?

::: {.callout-tip}

#### Hint

A [mask on a column](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) of the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) counts the days
on which `Direction` is `Up`.
The [accuracy](https://morrison-lab.github.io/lds/chapters/first-model.html#def-accuracy) is the share of days
on which the [prediction](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) is right.

:::

```{python}
# your code here
```

#### Lab: predict at chosen values

Use the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) of Exercise 12.
What is the [predicted probability](https://morrison-lab.github.io/sds/classification.html#def-predicted-probability) that `Direction` is `Up` on a day with `Lag1` $= 1.2$ and `Lag2` $= 1.1$?

::: {.callout-tip}

#### Hint

The [`predict` function](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) takes a data frame with the same columns as the [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature)
and returns [predicted probabilities](https://morrison-lab.github.io/sds/classification.html#def-predicted-probability), not [log-odds](https://morrison-lab.github.io/pds/probability-basics.html#def-log-odds).

:::

```{python}
# your code here
```

#### Lab: split the Smarket data by year

Split the `Smarket` data into a [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) of the days before $2005$ and a [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) of the days in $2005$.
Keep `Lag1` and `Lag2` as the [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) and `Direction` as the [outcome](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable).

::: {.callout-tip}

#### Hint

A [mask on a column](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) keeps the matching rows,
and a [selection of columns](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) gives the [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) and the [outcome](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable).

:::

```{python}
# your code here
```
