Introduction
Knowing that more staff improves patient throughput is a prediction; knowing exactly how many nurses and beds to allocate within budget to maximize satisfaction is a decision — and that's the gap between predictive and prescriptive analytics. ConductLinearOptimization closes it by fitting a linear regression to estimate how your input variables actually drive the output, then feeding those coefficients into SciPy's linprog solver to find the exact combination of inputs that maximizes or minimizes the output, subject to constraints you specify (or the observed data range if you don't).
Linear optimization is essential for:
- Resource allocation in healthcare (e.g., staffing vs. patient throughput)
- Optimizing vaccine distribution logistics to maximize population coverage
- Minimizing operating costs in epidemiological field operations
- Maximizing intelligence collection coverage with limited sensor assets
- Marketing budget optimization across multiple advertising channels
- Manufacturing production planning under raw material constraints
- Portfolio optimization in financial risk management
- Nutritional optimization (e.g., meeting dietary needs at minimum cost)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
dataframerequired | pd.DataFrame | — | DataFrame containing the input and output variables. |
output_variablerequired | str | — | Name of the target column to be optimized (the dependent variable). |
list_of_input_variablesrequired | list | — | List of column names to be used as inputs for optimization (the independent variables). |
optimization_type | str | 'maximize' | Whether to 'maximize' or 'minimize' the output variable. Defaults to 'maximize'. |
input_constraints | dict | None | Dictionary mapping input variable names to (min, max) tuples. Use None for no bound: e.g., {'var1': (0, 100), 'var2': (None, 50)}. If None, defaults to the observed data range in dataframe. Defaults to None. |
print_optimization_summary | bool | True | Whether to print a detailed text summary of the model coefficients and optimal results. Defaults to True. |
print_constraint_summary | bool | True | Whether to print the min/max bounds applied to each input variable. Defaults to True. |
data_source_for_plot | str | None | Text describing the data source, displayed in the plot caption. Defaults to None. |
plot_optimization_results | bool | True | Whether to generate a bar chart showing optimal input values and the resulting output. Defaults to True. |
dot_fill_color | str | '#999999' | Hex color code for the input variables bar chart. Defaults to '#999999'. |
line_color | str | '#b0170c' | Hex color code for the output variable bar chart. Defaults to '#b0170c'. |
figure_size_for_optimization_plot | tuple | (10, 6) | Width and height of the generated plot in inches. Defaults to (10, 6). |
title_for_optimization_plot | str | 'Linear Optimization Results' | Main title text for the optimization plot. |
subtitle_for_optimization_plot | str | 'Optimal input values and resulting output value.' | Subtitle text for the optimization plot. |
caption_for_optimization_plot | str | None | Additional caption text to display at the bottom of the plot. Defaults to None. |
Returns
A dictionary with success (whether the solver found a valid solution), optimization_type, output_variable, input_variables, coefficients and intercept (from the underlying regression), optimal_inputs (the calculated optimal values for each input), optimal_output (the resulting maximized/minimized output value), constraints (the bounds applied), and model (the fitted sklearn.linear_model.LinearRegression).
Example
from analysistoolbox.prescriptive_analytics import ConductLinearOptimization
import pandas as pd
# Healthcare: optimize hospital resource allocation to maximize patient satisfaction
hospital_data = pd.DataFrame({
'nurses_on_shift': [10, 15, 20, 25, 30],
'beds_available': [50, 60, 70, 80, 90],
'avg_wait_time_minutes': [45, 30, 20, 15, 10],
'patient_satisfaction_score': [65, 75, 85, 90, 95]
})
results = ConductLinearOptimization(
dataframe=hospital_data,
output_variable='patient_satisfaction_score',
list_of_input_variables=['nurses_on_shift', 'beds_available'],
optimization_type='maximize',
input_constraints={'nurses_on_shift': (10, 40), 'beds_available': (50, 100)}
)
print(f"Optimal satisfaction score: {results['optimal_output']:.1f}")