Essays on macroeconomics with heterogeneity