Would you return a lost wallet filled with cash? Many people would, even if no one could know they pocketed the money. It’s obvious why we adhere to moral principles in public—we’ll get punished or shamed if others see us breaking the rules. But why are we so willing to do the right thing when no one is watching?

Over the past several years, with collaborators at University College London, I’ve been probing this question in the laboratory. In our studies we asked people to decide whether to anonymously inflict electric shocks on themselves or strangers in exchange for money. These shocks were tailored to each participant’s own pain threshold in order to be moderately painful but not intolerably so. The decisions were completely private, so concerns about punishment or reputation could not be a strong motivating factor.

Across several studies, we’ve found that most people would rather shock themselves than others for money, despite the choices being secret. On the whole, we avoid harming others for our own gain, even when there’s no way to get caught.

What remained unclear, however, was why people behave this way, and solving this puzzle is challenging for several reasons. We can’t just ask people why they forgo profits to spare others pain—they might rattle off a number of reasons, but these may be unreliable because people often lack insight into why they behave the way they do. And even when people do know the reasons behind their choices, there are strong incentives to share only the virtuous ones. Where else, then, can we turn?

Our research team looked to the brain for clues. Brain imaging provides a toolkit for measuring aspects of moral decision-making that are inaccessible to conscious awareness. So in a new study, published recently in the journal Nature Neuroscience, we again asked participants to trade off money and pain inside a brain scanner. They faced a range of choices that varied in how much pain would be inflicted per dollar earned. For example, in some choices, gaining ten dollars came at the cost of ten extra shocks. In other choices, the exchange rate was more cruel: delivering ten shocks would yield only a few pennies.

By looking at how the brain weighed up these varying amounts of money and pain during decision-making, we were able to solve part of the mystery about why we might not take money from a lost wallet—ill-gotten gains seem less valuable to us.

When we make decisions, a network of brain areas calculates how valuable each option is. This network includes the striatum, a fist-shaped structure buried deep in the center of the brain. In our new study, we found the striatum responds less to money gained from shocking others than to money gained from shocking oneself. Importantly, this was only true for the people who behaved morally—the people who were least willing to profit from others’ pain showed the weakest striatum responses to ill-gotten gains.

If we’re moral when no one’s watching because we value the prospective spoils less, is the pain itself having any impact? Surprisingly, how the brain responded to others’ pain was unrelated to differences in moral behavior. It’s not that the brain was indifferent to the amount of pain at stake – in fact, we observed activity in areas that typically respond when people empathize with others’ suffering. But those people who showed the strongest brain responses to others’ pain were no less likely to actually shock someone for money.

There’s a further question, though: why is it that the value of money seems to be so sensitive to the way it was earned? We identified a region that may be responsible in the prefrontal cortex, a more recently evolved part of the brain that is engaged when we judge others for misbehaving. In our study, this region tracked the blameworthiness of harming others for profit, responding most strongly when people had the option to inflict a lot of pain for a trivial amount of money. And when people refused to profit from harming others, the prefrontal cortex increased its communication with the striatum.

These findings echo centuries-old ideas about human morality. In his Theory of Moral Sentiments, the philosopher Adam Smith described the conscience as an “impartial spectator”, guiding our choices by directing inwards the spotlight we regularly shine on others’ misdeeds. Our research suggests that this is exactly what our prefrontal cortex might be doing—when we’re making a moral decision, like whether to take money from a lost wallet, our prefrontal cortex simulates how others might blame us and adjusts the values of our options accordingly, even when no one is watching. The construction of moral values in the brain seems to incorporate the anticipated or internalized judgments of others.

Our findings paint an optimistic view of human nature, but also raise an obvious question: how can we explain moral failures? To answer this question, we need to understand how the brain comes to represent those moral principles that often (but not always) guide our choices. The brain circuitry we identified plays a key role in learning, which underscores the importance of education and social influence in shaping our moral values. Doing good in private may, ironically, hinge on the details of how we all behave in public.