There’s a homeless lady who lives by the station in my bustling South-East London neighborhood. I wonder what her story is. What brought her here to eke out an existence on this grimy London street? I imagine myself in her shoes and wonder how I’d cope. Most of the time she’s asleep, bundled under a filthy duvet. Occasionally, when she’s awake, I give her money.

Acts of kindness—even costly ones—occur across the animal kingdom, but they are most common within families. These are straightforward to explain, since the altruist has a vested interest in the beneficiary’s genetic success. But such acts are not limited to families. Elsewhere, costly helpful behaviors are predicated on future benefits—what evolutionary biologist Robert Trivers calls “reciprocal altruism.” This golden rule of social interaction assures individuals that their costly efforts will be repaid: You scratch my back now and I’ll scratch yours some other time.

Yet many of us are willing—and happy—to help people who are less fortunate than us and from whom we expect no repayment. Human helping behavior is as diverse as it is frequent: We hold doors open for people, let cars out at junctions, donate money and time to charities, and in extreme cases, we may even risk our lives to help others. Why are we so helpful?

Many of us are willing—and happy—to help people who are less fortunate than us and from whom we expect no repayment.

Many people, if asked, would insist that their helpful actions stem from a genuine concern for the plight of others. Indeed, we know that these feelings of empathy are real and that helping others is psychologically pleasurable.

The urge to help others seems to be strongest when we are faced with a single “identifiable victim,” rather than many needy individuals. The reasons for this are not fully known, but plausible explanations include the desire to have a meaningful, defined impact, or that we feel we can make a difference to one individual in need (whereas the plight of thousands overwhelms us).

But more recent research adds nuance to this story. One study shows that the identity of the victim matters: Individuals from low-ranking social groups are less likely to be helped. In another study, people were not more likely to donate when a single animal was presented as the recipient.

Why are we more likely to help individuals in some cases but not others? An evolutionary perspective can clarify. Evolution is a strict accountant. For a behavior to be selected for, costly actions must yield ultimate benefits.

While appealing to genuine empathy or the warm fuzzy glow of helping others explains why we help in one sense, it also poses a deeper question: Why are our brains wired to make us feel good about doing costly things? For the most part, our brains try to stop us from doing things that impose a long-term cost. Putting your hand in a fire will reliably trigger brain alarms—coded as pain—to alert you to the problem.

Evolution is a strict accountant. For a behavior to be selected for, costly actions must yield ultimate benefits.

So why do we have a brain that is a product of evolution but also incentivizes costly things like helping other people?

It may be that the downstream benefits of acquiring and maintaining a good reputation are large enough to outweigh the short-term costs of helping others. Reputation-based economies, like eBay and AirBnB, highlight the importance of maintaining a good image and the penalties of not doing so. Reputational concern is not a dot-com concept, however. The importance of virtue signaling has deep evolutionary roots, including among fish. On tropical coral reefs, small “cleaner” fish rely on their reputations in order to attract other fish—the “clients”—to their cleaning stations. Clients watch their intended partner cleaning another individual and only patronize that cleaning station if it looks like the cleaner is doing a good job. Even more amazingly, cleaners are sensitive to being watched and raise their cleaning game when they have an audience.

Reputation is the oil that lubricates social interactions.

As in cleaner fish, so it is in humans. Reputation is the oil that lubricates social interactions. People with a reputation for helping others are preferred as partners and have stronger and more supportive social networks. Sometimes, people even prize a reputation for fairness and kindness over other winning attributes, such as wealth. Though they may not consciously recognize their motives for helping other, people are implicitly aware of the potential for reputational gains when they interact with others, and are more helpful when their reputation is at stake. And in the same way that the brain alerts them to physical pain, actions that result in ostracism from a group also activate these same pain detection areas.

In fact, reputation can be more powerful than money at incentivizing good behavior. In a recent study aimed at increasing participation in an energy reduction program, customers were four times as likely to participate when their actions were observable compared to when they were incentivized with a $25 voucher. Also, people sometimes compete to have the best reputation: An analysis of donations to fundraising pages for the 2014 London marathon found that men compete with other men when donating to attractive female fundraisers, contributing on average nearly £30 more than they otherwise would.

Despite yielding these benefits, there are times when ostentatious acts of generosity or displays of superior morality backfire. All of us can think of a time when we have been on the receiving end of a humblebrag (the more astute readers may have spotted a humblebrag surreptitiously planted in this article). Rather than being filled with admiration and respect for that person, we might instead secretly hanker for some unfortunate incident to bring them down a peg or two. Walking the reputation tightrope isn’t easy.

This presents a paradox: How is it that doing good things is essential to improving our reputation, but we don’t necessarily think people who do good things are good people? The answer lies in our unique human ability to mind read. We seldom take behavior at face value but instead reflexively look under the hood to try and figure out why people behave the way they do.

How is it that doing good things is essential to improving our reputation, but we don’t necessarily think people who do good things are good people?

We know that doing something nice for others can improve our reputation. But we also know that other people know this. This means that when we see overtly generous or striking displays of helping behavior, our instinct is to question the actor’s motives: Is he doing that because he’s really a nice person, or is he doing that to show off? The competitive nature of reputation means that other people’s grandiose displays make our own efforts look paltry. Extremely cooperative individuals can be shunned by their social group. As a result, when donating online, people are more likely to give anonymously if they are making an extremely generous donation.

One might ask, therefore, why do people ever bother to give large anonymous donations? Instead, why not give smaller donations publicly and reap the reputational benefits? One possibility is that large donors are later “outed” for their donations, thereby reaping the reputation benefits while avoiding the question marks over their integrity. After all, as Oscar Wilde once remarked, “The best feeling in the world is to do a good deed anonymously—and have somebody find out.”

Another possibility is that since most people who donate on social networks are loosely connected to one another, they might be aware of the individuals who can afford to give a bit more. These richer members might therefore be stuck between a rock and a hard place: damned for being stingy if they give the same as everyone else but accused of showing off if they make large, public donations.

What can result from this more complete understanding of the reasons why we help? Interestingly, understanding our selfish motivations allows us to leverage them to encourage more helping behavior. For example, the competing motives of wanting to be seen to be doing good, while at the same time avoiding “do-gooder derogation,” have already been put to great use in fundraising. JustGiving, a site that taps into existing social networks to help their users fundraise online, knew that a donor sharing their donation on Facebook yielded, on average, an additional £4.50 per share. But donors—perhaps aware of the costs of humblebragging—were reticent to share their good deeds online. Simply by changing the framing of the message to “help your friend raise even more money,” JustGiving overcame this natural hesitation. This small change (together with increasing the size of the buttons on the webpage) resulted in a massive 28 percent increase in sharing and an estimated £3 million extra in donations in one year alone.

So while it is correct to assert that helpful actions can be motivated by genuine altruistic motives, this is not the full story. Next time you see someone—like me—stopping to give money to a homeless person, you also know that evolution has shaped our brain to make us enjoy doing things that are to our own eventual benefit.