Facebook has agreed to invest $20 million in affordable housing initiatives after facing intense criticism for failing to help low-income residents in Silicon Valley where the technology boom has exacerbated displacement and gentrification.