Psychology, biology, and mathematics have come together to show that the occurrence of altruism and spite — helping or harming others at a cost to oneself — depends on similarity not just between two interacting individuals but also to the rest of their neighbors.