Essays on macroeconomics with heterogeneous agents