How to Use Code to Understand Anything
By: Alex Lamb
Tags: Wisdom, Coding, Simulation, Science, Business

Image by David Bruyland from Pixabay

In his terrific book, How Not To Be Wrong, Jordan Ellenberg describes math as a way to boost your intuition. He frames it as a kind of magic tool that enables you to make great choices just by manipulating numbers and symbols. I love this way of thinking about the subject and he’s absolutely right. But I don’t think math is the only tool that’s capable of boosting a person’s effective intelligence. Arguably, there’s another technique that’s easier to use and more broadly applicable to everyday life. Chances are, you’re using a device that facilitates that kind of mental superpower right now to read this article.

From the title of this piece, you’ve no doubt guessed that I’m talking about coding. But the idea I want to share with you isn’t just about ordinary programming. I’m referring to a particularly easy form of coding that delivers disproportionately huge insights. I’m talking about simulation.

Good simulations often comprise just a page or two of code, require no advanced software skills, and can take you as far as the frontier of human knowledge in the course of an afternoon. You can use tools as simple as Scratch, the software language they use to teach middle school kids. And yet, amazingly, few people know how to use this technique or what it can achieve. There are seemingly more people in the world who tinker with the mysteries of deep learning, despite the fact that simulation can do everything from predicting customer behavior, to informing political choices, to helping design products.

You might be wondering what I mean by simulation, exactly. You might have heard of flight simulators or the kind of simulations people use to predict the weather. Neither of these are particularly easy to understand or build, so if you’re feeling skeptical, that reaction is warranted. What I’m talking about is what people refer to as an agent-based model. Think of the simplest computer game you can, perhaps something 2D and scrolling. Now take away any complex graphical programming. Now take away the player. Now take away any requirement that the code run smoothly or fast. What’s left is close to the kind of code you need.

The next question you may have is what the hell use is something like that? The answer is they’re useful for understanding pretty much everything. I’ve used these programs to plumb the depths of quantum mechanics, unravel how evolution works, peer hundreds of years into the future, and help large companies better structure their projects. This skill has enabled me to work with scientists in over half a dozen disciplines, everywhere from national labs to the Institute for Advanced Study, without having a PhD. Right now, simulations I built at Princeton in 2013 to understand politics are proving alarmingly, uncannily accurate, six years later.

Simulations operate at a different scale to math skills. Math is great for telling you things like what kind of investments to make or how seriously to take the results of a medical test. Sims don’t give you that kind of concrete information. Instead, they offer you guidance. For instance: how to build effective teams, how public policies are likely to pan out, or which tech companies are likely to be doing well five years from now. A good sim is a bit like a mountain-top guru who refuses to answer direct questions but bestows life-changing wisdom.

While simulations can help you grasp almost any scientific idea you like, perhaps most pragmatic use for them is to check our intuition. The basic idea is that you take your mental model for how something works, turn it into code, and then play it forwards to see if your assumptions produce the kind of results you expect to see. If they don’t then, chances are, your intuition needs repair. And you’d be surprised at how often that’s true. Politics particularly seems to be full of people asserting that if they do X, then Y will happen, based apparently on nothing but hunches and hope.

How do we turn mental models into programs? It’s rather like writing a story. First, you come up with your characters. In the case of a sim, your characters are agents. Instead of writing down their personal traits, you write the rules they follow when things happen, rather like non-player-characters in a game.

Second, you come up with a world for your characters to live in. In this case, that world is an environment that determines how your agents meet each other, and what kind of external factors can influence their behavior. Lastly, you come up with a narrative. For a simulation, this can be extremely simple. Really it’s just a framework that sets up the initial conditions of the story, lets the agents play out their choices, and measures what happens.

That’s pretty much it. I’ll include some examples at the end of this article where you can see this technique in action. But by now you may be wondering how it’s possible to pick the right behaviors to put into agents so that they do anything even slightly like their analogs in the real world. And how can you tell if you’re getting it wrong?

The answer is that if your intuition has been successfully checked, it doesn’t matter whether your agents behave exactly like the people, animals or companies you’re emulating. If you’ve given them rules to behave by that you think are realistic, and they don’t act the way you expect, then you’ve learned something. You can then use your model to figure out what kind of rules must be governing the real world by tweaking your sim until you see patterns you recognize. This approach isn’t perfect, of course. It’s still possible to make mistakes. But it’s remarkable how much insight can be gained just by trying.

Another important point about simulations is that the simpler they are, the more likely they are to be useful. People have a habit when designing models of squeezing in details in the hopes that the outcomes will be truer to life. This is generally the opposite of what happens. You can never get exact answers out of a sim because, often, the systems you’re modeling are chaotic. Tiny changes in the initial conditions can have big impacts in the results. Instead, the more bells and whistles you add, the more assumptions your model makes, and the more potentially biased it becomes.

By contrast, if you take detail out, your model becomes more general. If it says anything predictive at all, the answers are far more likely to be correct. Usually, the best way to proceed is to run a simple model hundreds of times with different parameter settings to look at the spread of answers that get generated.

So far, so good, but I’ve admittedly not said a word yet about perhaps the biggest reason that people shy away from using simulation: they simply don’t believe they can work. The world around us is complicated and apparently unpredictable. So how can building tiny, ultra-simplified models of it possibly say more than our intuition?

This belief reveals one of the deepest problems we humans have when trying to understand nature. We assume that because our environment is full of complex people full of hopes and dreams and whims, that the world at scales above the individual must be even more complicated. But this belief is just wrong.

Gerd Gigerenzer, world expert on heuristics and decision making, has a lovely story about this. He points out that when Google’s deep-learning-based flu prediction engine wasn’t performing as hoped, the engineers did the obvious thing and added in dozens more variables to boost its capabilities. They had little effect. Meanwhile, Gerd’s own team was able to create a model that out-predicted Google’s by instead reducing the number of variables to three. As he points out: when it comes to prediction, less is often more.

Another great example is traffic jams. Behind the wheel of every car is a human with free will, their own life, culture, passions, and reasons to travel. And yet, every traffic jam on Earth has the same emergent structure. That’s because the constraints of driving a car surrounded by other cars limit the field of expression of the humans inside, so only certain patterns of collective action can arise.

Structural limits like the ones that shape traffic jams are the rule, not the exception. It’s just that a lot of the time, it’s hard to spot those limits before you start modeling. Generally speaking, the bigger the system you’re looking at, the more likely it is to be governed by simple laws. That’s because as you increase the number of variables in a system, it’s ever more likely that just a few of them are going to have a disproportionate impact. Similarly, it’s much easier to model events on long timelines than it is on short ones, which is why it’s far easier to predict climate trends than next week’s weather.

Simulations aren’t a panacea, or course. The lack of precise answers frustrates many, and you can never get the precise understanding that a mathematical proof provides. The upside, though, is that with simulations, you don’t need to know how something works before you start modeling. Understanding comes after you’ve watched the code run many times. And computer models can describe natural phenomena that analytical models never can. In complex systems research where these tools are most often used, mathematical understanding often lags decades behind the creation of simple programs. And sometimes, the math never catches up. (Or at least, hasn’t yet!)

I’m passionate about simulations because they’ve informed the course of my career. I never would have been able to do so much interesting work without them. So as a parting gift, here are some models I’ve shared online that will hopefully clarify both how easy this kind of coding is, how it can be done, and how deep the lessons can be.

This first model describes the Prisoner’s Dilemma — one of the most profound topics in game theory — with just a couple of pages of code. The second shows a simple extension of the same game that’s used in evolutionary biology to explain the rise of altruism. The third shows another extension that I came up with while doing research at Princeton that sheds light on how the poorest people in society often end up voting against their own interests. As you can see, the program is tiny but the implications of the work are potentially huge. The round time from me having the idea and getting to test it was measured in hours. The frontiers of human knowledge really are right next door, and with still so few people working on agent-based models, the chances of a citizen scientist noticing something important are greater than in just about any other field.

If you’re hungry for more, here is the first in a series of posts I wrote a couple of years ago about how wealth inequality happens. To see the model code, just open the developer options in your browser and look at the page source.

In other posts, I’ll aim to cover how anyone can start doing the kind of background research necessary to generate models like these. In the meantime, I hope you have as much fun exploring simulations as I have. Happy coding!