Unit 12 · Exercises
Unit 12 exercises
Each exercise describes a project design or a project report with one or more flaws. Identify the flaws, explain their effect on the conclusions, and propose a corrected design. The worked answers explain each flaw, its effect on the conclusions and a corrected design.
Exercise 1. A detector that is almost perfect
A project trains a random forest on a public intrusion dataset collected over three weeks. The author removes no duplicate records, shuffles all records, uses 80% for training and 20% for testing, and reports accuracy 99.8% and F1 0.97. The conclusion is that the detector "is ready for deployment".
Show the answer
Three flaws are clear. First, the random split of time-ordered records lets the model learn from records later than those it is tested on, which is temporal leakage in the sense of Kapoor and Narayanan (2023) and temporal bias in the sense of Pendlebury et al. (2019); any attack family that appears late is seen in training. Second, duplicates are not removed, so identical records can sit on both sides of the split and part of the test measures memorization instead of generalization. Third, accuracy and F1 on a test set with the dataset's own share of attacks say little about deployment, where the base rate is usually far lower (Lesson 1.2); the report needs precision, recall and alerts per day at the deployment base rate and alert budget.
A corrected design deduplicates first, splits by time (for example, the first two weeks for training and the third for testing, with a validation period for the threshold), chooses the threshold on validation data for a stated alert budget, reports precision and recall at that threshold with intervals, reports per-family recall, and compares with a hand-written rule and a logistic regression. "Ready for deployment" would also need a test on the target network's own traffic, since an evaluation on a public dataset alone is a lab-only evaluation, pitfall P9 of Arp et al. (2022).
Exercise 2. The best of twenty
A project compares a new feature set with an old one. For each, the author tries 20 combinations of hyperparameters and decision thresholds and reports the combination with the highest F1 on the test set. The new features win, 0.81 against 0.78.
Show the answer
Choosing hyperparameters and thresholds on the test set is data snooping and biased parameter selection, pitfalls P3 and P5 of Arp et al. (2022). The reported scores are maxima over 20 noisy evaluations, so both are optimistic, and the difference of 0.03 may be produced entirely by the selection, which favors whichever variant's 20 scores happen to vary more. The corrected design selects hyperparameters and thresholds by validation (a time-ordered validation period or grouped cross-validation on the training data), freezes them, evaluates each feature set once on the test period, and repeats over \(k\) seeds with paired differences and the interval \(\bar{m} \pm t_{0.975,\,k-1}\,s/\sqrt{k}\) of Lesson 12.1, where \(\bar{m}\) and \(s\) are the mean and sample standard deviation of the \(k\) per-seed differences and \(t_{0.975,\,k-1}\) is the 97.5% quantile of the \(t\) distribution with \(k - 1\) degrees of freedom.
Exercise 3. Precision on the benchmark and in the network
An anomaly detector achieves a true positive rate of 0.95 and a false positive rate of 0.03 on a benchmark in which 20% of records are attacks, and the report states "precision 0.89". The organization that wants to use it estimates that 0.1% of its connections are malicious. Compute the precision the organization should expect, and say what the report should have contained.
Show the answer
On the benchmark, the share of records that are detected attacks is \(0.95 \times 0.2 = 0.190\) and the share that are false alarms is \(0.03 \times 0.8 = 0.024\), so precision is
which matches the report. At a base rate of 0.001 the two shares are \(0.95 \times 0.001 = 0.00095\) and \(0.03 \times 0.999 = 0.02997\), and precision becomes
so about 31 alerts in a thousand are real and the rest are false. The report should have stated the benchmark's base rate, reported the true and false positive rates (which do not depend on the base rate), and converted them to precision and alerts per day at a stated deployment base rate. This is the spatial bias of Pendlebury et al. (2019) and the base-rate fallacy, pitfall P8 of Arp et al. (2022).
Exercise 4. A language model that reads phishing
A project prompts a language model to classify 50 emails from a public phishing corpus as "phishing" or "legitimate", runs the prompt once at the default temperature, and reports 92% accuracy. It concludes that the model "outperforms traditional filters".
Show the answer
The flaws compound. Fifty emails give a wide interval; by the Wilson formula of Lesson 10.2, 46 correct out of 50 gives a 95% interval of roughly 0.81 to 0.97. A single run at nonzero temperature measures one draw; repeated runs and prompt variations are needed to see how stable the result is. No baseline was run, so "outperforms traditional filters" is unsupported; a URL reputation check or a simple text classifier evaluated on the same emails is the minimum. A public corpus may be in the model's training data, so emails from after the model's training cutoff are needed to reduce the chance that it recalls them. Accuracy on a corpus whose phishing share is unknown or balanced does not translate to precision in a mailbox. Finally, the evaluation ignores the adversary. Phishing emails are written by attackers, who can include text aimed at the model (indirect prompt injection, Lesson 10.2), so the project should test emails with injected instructions and report how often the verdict changes.
Exercise 5. A fairness audit with an invented group
To audit a detector for unequal treatment, a project defines two groups of hosts by whether a host's value of the feature count is above or below its median, and reports the true positive rate of each group. In the "below median" group the test set contains no attacks, and the report shows its true positive rate as 0.
Show the answer
The group is derived from a model feature, so it says nothing about any population whose treatment matters; it partitions the input space where the model's behavior is expected to differ by construction, and any "gap" mostly restates how the feature relates to the label. A fairness audit needs groups defined independently of the model, such as sites, business units, user populations or, where lawful, demographic categories, chosen because the people in them bear the consequences of errors (Lesson 11.2). The true positive rate of a group with no positives is undefined (the denominator \(TP + FN\) is zero), and reporting it as 0 is an error that will be read as a failure to detect. The audit should report "undefined (no attacks in this group in the test period)", the counts behind every rate, and intervals, and should choose a test period or a grouping in which each group contains enough positives to estimate the rate at all.
Exercise 6. The best seed
A project trains a neural detector with five random seeds and obtains average precision of 0.71, 0.74, 0.69, 0.80 and 0.72. The report states "average precision 0.80". Compute what should be reported instead.
Show the answer
Reporting the maximum over seeds is selection on noise. The five values sum to 3.66, so the mean is \(\bar{m} = 0.732\). The sample standard deviation is \(s = 0.0421\), and with \(t_{0.975,4} = 2.776\) the 95% interval \(\bar{m} \pm t_{0.975,4}\,s/\sqrt{5}\) has half-width
so the interval is \(0.732 \pm 0.052\). The report should state 0.732 with that interval, mention that one seed reached 0.80, and, if the spread is surprising, investigate why training is unstable (for example, the learning rate or class imbalance) instead of keeping the lucky run.
Exercise 7. A finding published on the day it was made
While testing a commercial email gateway with a free trial account, a project discovers that a crafted attachment bypasses its machine-learning filter. The author publishes the attachment and a write-up on a public repository the same day, "so that defenders can protect themselves".
Show the answer
Publishing a working bypass before the vendor can fix it exposes every customer of the gateway to attackers who read the repository, while defenders cannot patch a product they do not control. Coordinated vulnerability disclosure, as described in the CERT guide (Householder et al., 2017), would report the finding privately through the vendor's security contact, agree on a timeline, and publish after a fix is available or the agreed time has passed, following principles such as reducing harm and avoiding surprise. The author should also check that the trial's terms of use allowed the testing that was done. The project report can still describe the class of weakness and its measured rate; the exact attachment belongs in the report only after the coordinated disclosure.
References
- Arp, D., Quiring, E., Pendlebury, F., Warnecke, A., Pierazzi, F., Wressnegger, C., Cavallaro, L., and Rieck, K. (2022). Dos and don'ts of machine learning in computer security. Proceedings of the 31st USENIX Security Symposium, 3971-3988. https://www.usenix.org/conference/usenixsecurity22/presentation/arp
- Householder, A. D., Wassermann, G., Manion, A., and King, C. (2017). The CERT guide to coordinated vulnerability disclosure. Software Engineering Institute, Carnegie Mellon University. Special report CMU/SEI-2017-SR-022, 15 August 2017. https://doi.org/10.1184/R1/12367340.v1
- Kapoor, S., and Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9), 100804. https://doi.org/10.1016/j.patter.2023.100804
- Pendlebury, F., Pierazzi, F., Jordaney, R., Kinder, J., and Cavallaro, L. (2019). TESSERACT: Eliminating experimental bias in malware classification across space and time. Proceedings of the 28th USENIX Security Symposium, 729-746. https://www.usenix.org/conference/usenixsecurity19/presentation/pendlebury