In this paper we develop a new approach for finding optimal government policies in economies with heterogeneous agents. Using the calculus of variations, we present three classes of equilibrium conditions from government's and individual agent's optimization problems: 1) the first order conditions: the government's Lagrange-Euler equation and the individual agent's Euler equation; 2) the stationarity condition on the distribution function; and, 3) the aggregate market clearing conditions.
These conditions form a system of functional equations which we solve numerically. The solution takes into account simultaneously the effect of the government policy on individual allocations, the resulting optimal distribution of agents in the steady state and, therefore, equilibrium prices.
We illustrate the methodology on a Ramsey problem with heterogeneous agents, finding the optimal limiting tax on total income.