ESSAYS IN FINANCE AND MACROECONOMICS WITH HETEROGENEOUS AGENTS