Optimizing Labor and Capital in Python
Every firm faces the same fundamental question: how much labor and how much capital should it employ to maximize profit? When production follows a Cobb-Douglas technology, this question turns into a clean, well-behaved optimization problem — one that’s perfect for illustrating both the economics and the numerical methods behind it. In this article, we’ll set up a two-variable profit maximization problem, solve it with a closed-form derivation and a numerical solver, and visualize the profit landscape in 3D.
The Economic Setup
Consider a firm that produces output $Q$ using labor $L$ and capital $K$ according to a Cobb-Douglas production function:
$$
Q(L, K) = A , L^{\alpha} K^{\beta}
$$
where $A$ is total factor productivity, and $\alpha, \beta \in (0,1)$ are the output elasticities of labor and capital. When $\alpha + \beta < 1$, the technology exhibits decreasing returns to scale, which guarantees a well-defined, interior profit-maximizing point (rather than a corner solution or an unbounded profit).
The firm sells output at price $p$, pays wage $w$ per unit of labor, and pays rental rate $r$ per unit of capital. Profit is:
$$
\pi(L, K) = p , A , L^{\alpha} K^{\beta} - wL - rK
$$
The firm’s problem is:
$$
\max_{L > 0,, K > 0} ; \pi(L, K)
$$
First-Order Conditions
Taking partial derivatives and setting them to zero gives the classic marginal-revenue-product-equals-input-price conditions:
$$
\frac{\partial \pi}{\partial L} = p A \alpha L^{\alpha - 1} K^{\beta} - w = 0
$$
$$
\frac{\partial \pi}{\partial K} = p A \beta L^{\alpha} K^{\beta - 1} - r = 0
$$
Dividing the first equation by the second eliminates $p$ and $A$, yielding a simple ratio between the optimal capital-labor ratio and the relative input prices:
$$
\frac{K^*}{L^*} = \frac{\beta w}{\alpha r}
$$
Substituting this back into the first FOC and solving for $L^*$ gives a closed-form solution:
$$
L^* = \left[ \frac{w}{pA\alpha \left(\dfrac{\beta w}{\alpha r}\right)^{\beta}} \right]^{\frac{1}{\alpha + \beta - 1}}, \qquad K^* = \frac{\beta w}{\alpha r} , L^*
$$
This is exactly what we’ll implement in Python, then cross-check numerically with scipy.optimize.
Concrete Numerical Example
We’ll use the following parameters:
- $A = 8$ (productivity)
- $\alpha = 0.35$ (labor elasticity)
- $\beta = 0.25$ (capital elasticity)
- $p = 10$ (output price)
- $w = 6$ (wage rate)
- $r = 4$ (capital rental rate)
Since $\alpha + \beta = 0.6 < 1$, the profit function is strictly concave in $(L, K)$, so it has a unique interior maximum.
Source Code
1 | import numpy as np |
=== Profit Maximization: Cobb-Douglas Production === Parameters: A=8.0, alpha=0.35, beta=0.25, p=10.0, w=6.0, r=4.0 Closed-form analytical solution: L* = 49.118372 K* = 52.626828 Numerical solution (scipy L-BFGS-B): L* = 49.118528 K* = 52.626637 Q* = 84.202941 Profit* = 336.811696
Code Walkthrough
Dark theme setup. The plt.rcParams block configures every plot to use a dark background with white text, matching the blog’s visual style, and applies to all three figures automatically.
production(L, K) implements the Cobb-Douglas function $Q = AL^{\alpha}K^{\beta}$ using np.power, which works cleanly whether L and K are scalars or full meshgrid arrays — this lets the same function serve both the optimizer and the plotting code.
profit(L, K) computes $\pi = pQ - wL - rK$ directly from the production function.
neg_profit(x) wraps profit for scipy.optimize.minimize, which only performs minimization. It also guards against non-positive inputs by returning a very large penalty value, keeping the optimizer inside the economically meaningful region $L, K > 0$.
analytical_solution() implements the closed-form formula derived above. It first computes the optimal capital-labor ratio ratio = (β·w)/(α·r), then solves for $L^*$ using the derived exponent formula, and finally recovers $K^*$ from the ratio. This gives an exact answer with no iterative solver involved.
scipy.optimize.minimize cross-checks the analytical result numerically using the L-BFGS-B algorithm, which handles the box constraints ($L, K > 0$) efficiently. Starting from [1.0, 1.0], it converges to the same optimum as the closed-form solution — a good sanity check that both the math and the code agree.
Figure 1 (3D surface + contour) visualizes the entire profit landscape. The 3D surface shows profit as a dome-shaped peak — a direct consequence of decreasing returns to scale making the profit function strictly concave. The contour map on the right shows the same landscape from above, with concentric rings collapsing toward the optimal point marked in cyan.
Figure 2 (profit slices) cuts through the 3D surface along each axis, holding the other input fixed at its optimal value. Both curves are single-peaked, confirming that $L^*$ and $K^*$ are each true local maximizers along their respective directions — a visual confirmation of the first-order conditions.
Figure 3 (comparative statics) explores how the optimum responds to a change in the output price p, holding A, α, β, w, r fixed. Because the capital-labor ratio K*/L* depends only on α, β, w, r (not on p), both L* and K* scale up together as p rises — the firm expands scale but keeps its input mix constant. Maximum profit π* grows even faster than either input, since revenue rises with both price and quantity simultaneously.
Interpreting the Results
The closed-form and numerical solutions should match to several decimal places, confirming the correctness of both derivations. Economically, the result illustrates a core insight of Cobb-Douglas theory: the ratio of capital to labor at the optimum is pinned down entirely by relative factor prices and output elasticities ($\beta w / \alpha r$), while the scale of production is what responds to the output price. This decomposition — between “input mix” and “input scale” — is one of the reasons Cobb-Douglas functions remain a workhorse in microeconomics and production theory.



Wrapping Up
This example shows how a two-input profit maximization problem can be solved two independent ways — analytically via the first-order conditions, and numerically via constrained optimization — with both approaches converging to the same answer. The 3D visualization makes the concavity of the profit function tangible, while the comparative statics plot reveals how the firm’s optimal scale (but not its input mix) responds to market prices. The same framework extends naturally to more complex production technologies, multiple outputs, or additional constraints such as a fixed budget for total input spending.



























