One Washington evening two summers ago, my wife and I were strolling down 15th Street NW, licking ice cream cones and chatting as our 13-month-old daughter wriggled in a carrier on my back. Then a scream from across the street snapped us out of our conversation. Looking up, I saw a man chasing a woman around a car that had come to a sudden halt in the middle of the street.

For a moment I thought it was all in fun until the woman fell and the man punched her, hard. A quicker-thinking bystander sprinted through traffic and barreled into the assailant, momentarily knocking him away from the prone woman. I handed the baby to my wife and trotted across the street. Up close, I realized I might be making a big mistake: All three people in the struggle were slick with blood.

My main contribution was an undignified yank on the knifeman's clothes. No matter: Within a few seconds, half a dozen men were sitting on the attacker. I crouched beside the young victim, looking at her eyes and the awful knife wounds on her arm and in her belly. I could hardly believe she might survive.

She did. The neighborhood itself had saved her.

These days I live in London, near a park where recently a young American woman out for her morning run also suffered a knife attack. Nobody helped, she died, and the murderer has not been caught.

Two wholly random assaults — the D.C. attacker told police he didn't know his victim and had been taking drugs to celebrate his 21st birthday — with equally random outcomes. Life and death seemed arbitrary.

I found the experience infiltrating my life as a writer and an economist. I was writing a book about how economics can help us find the hidden logic in aspects of life that seem complex and unpredictable. Could economics say anything about what makes our neighborhoods defend us or abandon us?

I discovered that it could. Economists can now tell us why neighborhoods go through dramatic transitions from dangerous to safe or rich to poor; they have established a clear link between urban architecture and crime; they can even shed some light on whether local crime is contagious. And they can tell us what difference law enforcement really makes when the streets are peopled by those who try to kill for no reason.

Cities frequently fall into a sharply defined patchwork of thriving areas and struggling ones, often divided along racial lines. It is easy to see this as the result of bitter prejudice. But the Nobel Prize-winning economist Thomas Schelling proved decades ago that the motivations that lead to segregation may be less entrenched than you might suppose.

In the days before computer simulations, Schelling demonstrated his theory with a game played with randomly distributed pennies and nickels on a checkerboard. He invented a simple rule for how the coins moved: A nickel could be happy as long as it was touching two or more other nickels. But if it touched only one other nickel, it would hop elsewhere, leaving its former neighbor isolated. One coin after another would move in a chain reaction. Schelling's game seemed to make possible a mixed checkerboard, but the result was always segregation.

The lesson? Even if everyone were comfortable living in a mixed neighborhood, extreme segregation — by race, class or income — could still emerge from people's mild preferences not to be outnumbered. Countless individually rational decisions can snowball into a socially regrettable outcome.

City streets can be unsettlingly empty or reassuringly thronged with passers-by. The safer and livelier the streets feel, the safer and livelier they become. This virtuous circle means a small catalyst can transform a neighborhood from struggling to thriving.

Architecture matters, too, something we feel intuitively but find hard to prove or quantify. Think of high-rise apartments. Do they make a city safer by packing more people into an area and giving the streets a greater bustle? Or are cities safer if most buildings are low-rise, so people feel a connection to the street?

Two new-wave economists, Edward Glaeser of Harvard and Bruce Sacerdote of Dartmouth, matched crime figures with data on building height and discovered that the residents of high-rise apartments are much more likely to be crime victims, specifically street crime. The effect remains similar after statistically adjusting for poverty, demographics and public housing: It's the height of the building itself that matters.

This backs up urbanist Jane Jacobs' persuasive, but unproven, insight that "eyes on the street" make a neighborhood safe. The hero who sprinted over to knock the knife away was sitting on his stoop, not gazing out his penthouse window.

We also know much more than we did 10 years ago about law enforcement's role in keeping our cities safe. Steven Levitt of the University of Chicago, now famous as the co-author of "Freakonomics," used state-by-state differences in the juvenile punishment system to show that teenage delinquents paid close attention to the threat of punishment.

The day they passed the age of majority was the day they cleaned up their act — an effect that was more pronounced in states where the adult penal system was dramatically harsher than the juvenile one.

Economists Alex Tabarrok and Jonathan Klick recently found that crime dropped noticeably in Washington when extra police were placed on duty because of terrorism alerts. These discoveries are a mere handful of jigsaw pieces from a very large puzzle, but they are credible explanations discovered in the face of the apparent randomness of everyday life. While the economics profession won't rescue the inner cities by itself, policymakers should make decisions about policing, public housing and planning in light of the evidence.

Tim Harford, a Financial Times columnist, is the author of "The Logic of Life: The Rational Economics of an Irrational World."