An Optimization Walkthrough in Python
When a breach hits, the quality of your response is largely decided before the incident starts. Every security team has a finite number of analysts and a finite tool budget, and these have to be split across four phases: Detection, Containment, Recovery, and Notification. Spread them evenly and you waste money on phases that need little. Starve the wrong phase and the damage compounds while the clock runs.
In this post we turn that question into a small optimization problem, solve it exactly by brute force, cross-check it with a continuous optimizer, and visualize the result with 2D and 3D plots.
The Problem Setup
A mid-sized company has:
- 20 responders to assign across the four phases (at least 1 per phase)
- $100k of tool budget to allocate, in $5k increments
We want the split that minimizes the expected loss from an incident.
Phase duration model
More people and better tooling shorten each phase, but with diminishing returns. For phase $i$ with $n_i$ staff and $m_i$ (in $k) of tools:
$$
T_i(n_i, m_i) = T^{\min}_i + \frac{T^{0}_i - T^{\min}_i}{1 + g_i , n_i^{\alpha} \left(\dfrac{m_i}{10}\right)^{\beta}}
$$
- $T^{0}_i$ is the duration with essentially no investment.
- $T^{\min}_i$ is the floor that no amount of investment can break (for example, regulators’ review windows).
- $g_i$ is how responsive the phase is to investment.
- $\alpha = 0.6$ and $\beta = 0.4$ control the diminishing returns on people and tools.
| Phase | $T^0$ (h) | $T^{\min}$ (h) | $g$ |
|---|---|---|---|
| Detection | 120 | 1 | 0.50 |
| Containment | 48 | 2 | 0.60 |
| Recovery | 96 | 8 | 0.30 |
| Notification | 60 | 6 | 0.80 |
Loss model
There are three incident scenarios with probabilities $p = (0.40, 0.35, 0.25)$: data theft, ransomware, and insider misuse. Each has a different cost rate while the attacker is undetected or uncontained ($a_s$), while systems are down ($b_s$), and a different regulatory penalty weight. Averaging over scenarios gives
$$
\bar a = \sum_s p_s a_s = 9.25, \qquad \bar b = \sum_s p_s b_s = 13.75, \qquad \bar P = 500 \sum_s p_s \pi_s = 297.5
$$
in $k per hour (or $k for the penalty). The expected loss is
$$
L(\mathbf n, \mathbf m) = \bar a ,(T_D + T_C) + \bar b ,(T_C + T_R) + \bar P \cdot \sigma!\left(T_D + T_C + T_N - 72\right)
$$
where the notification deadline is 72 hours and $\sigma$ is a smooth penalty (a scaled softplus):
$$
\sigma(x) = \frac{6}{72},\ln!\left(1 + e^{x/6}\right)
$$
The full optimization problem is
$$
\min_{\mathbf n,, \mathbf m} ; L(\mathbf n, \mathbf m) \quad \text{s.t.} \quad \sum_{i=1}^{4} n_i = 20,;; n_i \ge 1,;; \sum_{i=1}^{4} m_i = 100,;; m_i \in {0, 5, 10, \dots}
$$
The Source Code
The search space contains $969 \times 1771 = 1{,}716{,}099$ combinations. A naive nested Python loop would take minutes, so the code below evaluates all combinations at once with NumPy broadcasting.
1 | import itertools |
Code Walkthrough
1. Parameters
The first block defines everything about the scenario: the base durations T0, the floors TMIN, the responsiveness G, and the exponents ALPHA and BETA. The scenario arrays p, a, b, and pen are collapsed into the three scalars A_BAR, B_BAR, and P_BAR using the probability-weighted averages shown earlier. Because the loss is linear in the scenario parameters, averaging first gives exactly the same expected loss as averaging at the end, and it is much cheaper.
2. The model functions
phase_times(n, m)implements $T_i(n_i, m_i)$ directly. It works on a single allocation of shape(4,)and also on huge arrays, because every operation is element-wise and the last axis always holds the four phases.soft_excess(x)implements $\sigma(x)$.np.logaddexp(0, z)computes $\ln(1+e^{z})$ without overflow, which matters when the optimizer wanders into extreme regions.loss_from_times(T)slices the last axis (T[..., 0]and so on), so it accepts any array shape. This is what lets us evaluate 1.7 million allocations in a single call.
I chose a smooth penalty rather than a hard max(0, x) on purpose. The continuous optimizer (SLSQP) relies on gradients, and a smooth function keeps it well-behaved while still being very close to the hard deadline rule.
3. Enumerating every allocation
compositions(total, parts, minimum) lists every way to split total units into parts non-negative integers (after reserving minimum for each part) using the classic stars-and-bars construction. For 20 staff in 4 phases with at least 1 each, that is 969 rows. For $100k in $5k units, that is 1,771 rows.
4. The speed-up: broadcasting instead of loops
The key lines are:
1 | E = np.power(S[:, None, :], ALPHA) * np.power(Mt[None, :, :] / 10.0, BETA) |
S[:, None, :] has shape (969, 1, 4) and Mt[None, :, :] has shape (1, 1771, 4). NumPy broadcasts them into a (969, 1771, 4) array, which is every staff split paired with every tool split, without a single Python loop. A nested loop would call the Python interpreter 1.7 million times; broadcasting pushes the entire computation into compiled C code, which brings the run time down from minutes to a fraction of a second. np.unravel_index(np.argmin(L_all), L_all.shape) then converts the flat position of the minimum back into a (staff row, tool row) pair.
5. Baseline and continuous optimum
The “equal split” baseline gives 5 people and $25k to every phase. For the continuous solution, solve_continuous runs SLSQP with two equality constraints (total staff and total budget) and box bounds. It starts from several initial points (one uniform, the rest random draws from a Dirichlet distribution) and keeps the best feasible result, so a single unlucky start cannot trap it in a poor local minimum. Feasibility of the constraints is verified explicitly before a result is accepted. The continuous optimum is a lower bound for the integer problem, so it tells us how much we lose by having to use whole people and $5k steps.
6. Surfaces for the 3D plots
Panel (d) slices the problem: it varies detection staff and detection tools on a grid, and distributes the remaining people and budget over the other three phases in the same proportions as the optimal solution. Panel (e) re-solves the continuous problem for a grid of total staff and total budget values, showing how the best achievable loss improves as you scale up resources.
Execution Results
Search space size: 1716099 combinations, solved in 0.21 s
Phase Staff (equal) Staff (optimal) Tools $k (equal) Tools $k (optimal) Hours (equal) Hours (optimal)
Detection 5.0 7.0 25.0 35.0 42.1 33.6
Containment 5.0 6.0 25.0 25.0 16.1 15.0
Recovery 5.0 6.0 25.0 35.0 49.2 43.9
Notification 5.0 1.0 25.0 5.0 19.4 39.6
Expected loss, equal split : 1466.2 ($k)
Expected loss, continuous opt. : 1326.0 ($k)
Expected loss, integer optimum : 1328.1 ($k)
Reduction vs equal split : 9.4 %
Total time, equal / optimal : 126.7 h / 132.1 h

Interpreting the Results
The optimum beats the even split by roughly 9%. The brute-force search over all 1,716,099 allocations finds an expected loss of about $1,328k, against about $1,466k for the equal split. The continuous optimum is about $1,326k, so rounding to whole people and $5k steps costs only about $2k. That is a strong sign the integer solution is essentially optimal.
The best allocation is strongly uneven. Detection ends up with 7 people and $35k, Containment with 6 people and $25k, Recovery with 6 people and $35k, and Notification with just 1 person and $5k. Detection and Recovery attract the largest budgets, while Notification is pushed to the minimum.
Why Notification gets so little. Look at the marginal economics. An extra hour in Detection or Containment costs $\bar a = 9.25$ $k, and Containment and Recovery hours each carry an additional $\bar b = 13.75$ $k. A late notification, by contrast, only incurs the deadline penalty, whose slope is about $\bar P / 72 \approx 4.1$ $k per hour past the deadline. Spending scarce resources on shaving notification hours buys less than spending them upstream. Panel (c) shows the consequence: the optimal plan has a longer Notification phase than the equal split, and it spends more of its time over the 72-hour line, but the total loss is still lower.
Minimizing time is not the same as minimizing loss. The optimal plan’s total duration is actually slightly longer than that of the equal split (about 132 h versus about 127 h). The optimizer does not try to make every phase fast; it makes the expensive hours fast. This is the central lesson for pre-allocation: weigh each phase by what an hour of delay actually costs, not by how long the phase takes.
The 3D surface in panel (d) shows diminishing returns. Starting from the corner with little detection staff and little tooling, the loss falls steeply, then flattens as both inputs increase. The red marker sits in the flat basin. Pushing detection resources further would take them from the other phases, whose loss then rises faster than detection’s falls. This is what a well-balanced allocation looks like: no phase can gain without another losing more.
The 3D surface in panel (e) shows how the best achievable loss changes with total resources. It decreases in both directions, but the slope shrinks as the totals grow, because of the exponents $\alpha = 0.6$ and $\beta = 0.4$ and the duration floors $T^{\min}_i$. This is useful for budgeting: if you are choosing between hiring two more analysts and buying more tooling, this surface lets you read off which lever lowers the optimal loss more at your current operating point.
Panel (f) confirms the ordering: equal split, then continuous optimum, then integer optimum, with the last two nearly identical.
Takeaways
- Model each phase with diminishing returns and a hard floor. Real incident work does not scale linearly with headcount.
- Allocate by cost per hour of delay, not by phase length.
- For small discrete problems like this one, exhaustive search with NumPy broadcasting gives a guaranteed global optimum in well under a second, and a continuous solver provides an independent check.
- Re-run the model with your own numbers. Changing the scenario probabilities or the deadline penalty can shift the optimum noticeably, which is exactly why this analysis is worth doing before the incident rather than during it.











