Categories for Epidemiology

4 May, 2023

Xiaoyan Li, Sophie Libkind, Nathaniel D. Osgood, Eric Redekopp and I have been creating software for modeling the spread of disease… with the help of category theory!

Lots of epidemiologists use “stock-flow diagrams” to describe ordinary differential equation (ODE) models of disease dynamics. We’ve created two tools to help them.

The first, called StockFlow.jl, is based on category theory and written in AlgebraicJulia, a framework for programming with categories that many people at or associated with Topos have been developing. The second, called ModelCollab, runs on web browsers and serves as a graphical user interface for StockFlow.jl.

Using ModelCollab requires no knowledge of Julia or category theory! This feature should be useful in “participatory modeling”, an approach where models are built with the help of diverse stakeholders. However, as we keep introducing new features in StockFlow.jl, it takes time to implement them in ModelCollab.

But what’s a stock-flow diagram, and what does our software let you do with them?

The picture here shows an example: a simple disease model where Susceptible people become Infective, then Recovered, then Susceptible again.

The boxes are “stocks” and the double-edged arrows are “flows”. There are also blue “links” from stocks to “variables”, and from stocks and variables to flows. This picture doesn’t show the formulas that say exactly how the variables depend on stocks, and how the flows depend on stocks and variables. So, this picture doesn’t show the whole thing. It’s really just what they call a “system structure diagram”: a stock-flow diagram missing the quantitative information that you need to get a system of ODEs from it. A stock-flow diagram, on the other hand, uniquely specifies a system of first-order ODEs.

Modelers often regard diagrams as an informal step toward a mathematically rigorous formulation of a model in terms of ODEs. However, we’ve shown that stock-flow diagrams have a precise mathematical syntax! They are objects in a category \mathsf{StockFlow}, while “open” stock-flow diagrams, where things can flow in and out of the whole system, are horizontal 1-cells in a double category \mathbb{O}\mathbf{pen}(\mathsf{StockFlow}). If you know category theory you can read a paper we wrote with Evan Patterson where we explain this:

• John C. Baez, Xiaoyan Li, Sophie Libkind, Nathaniel D. Osgood and Evan Patterson, Compositional modeling with stock and flow diagrams. To appear in Proceedings of Applied Category Theory 2022.

If you don’t, we have a gentler paper for you:

• John C. Baez, Xiaoyan Li, Sophie Libkind, Nathaniel D. Osgood and Evan Redekopp, A categorical framework for modeling with stock and flow diagrams, to appear in Mathematics for Public Health, Springer, Berlin.

Why does it help to formalize the syntax of stock-flow diagrams using category theory? There are many reasons, but here are three:

1. Functorial Semantics

Our software lets modelers separate the syntax of stock and flow diagrams from their semantics: that is, the various uses to which these diagrams are put. Different choices of semantics are described via different functors. This idea, called “functorial semantics”, goes back to Lawvere and is popular in certain realms of theoretical computer science.

Besides the ODE semantics, we have implemented functors that turn stock-flow diagrams into other widely used diagrams: “system structure diagrams”, which I already explained, and “causal loop diagrams”. It doesn’t really matter much here, but a causal loop diagram ignores the distinction between stocks, flows and variables, lumps them all together, and has arrows saying what affects what:

These other forms of semantics capture purely qualitative features of stock and flow models. In the future, people can implement still more forms of semantics, like stochastic differential equation models!

So, instead of a single monolithic model, we have something much more flexible.

2. Composition

ModelCollab provides a structured way to build complex stock-flow diagrams from small reusable pieces. These pieces are open stock-flow diagrams, and sticking together amounts to composing them.

ModelCollab lets users save these diagrams and retrieve them for reuse as parts of various larger models. Since ModelCollab can run on multiple web browsers, it lets members of a modeling team compose models collaboratively. This is a big advance on current systems, which are not optimized for collaborative work.

This picture shows two small stock-flow diagrams being composed in ModelCollab:

Some of the underlying math here was developed in earlier work using categories and epidemiological modeling, which was also done by people at Topos and their collaborators:

• Sophie Libkind, Andrew Baas, Micah Halter, Evan Patterson and James P. Fairbanks, An algebraic framework for structured epidemic modelling, Philosophical Transactions of the Royal Society A 380 (2022), 20210309.

3. Stratification

Our software also allows users to “stratify” models: that is, refine them by subdividing a single population (stock) into several smaller populations with distinct features. For example, you might take a disease model and break each stock into different age groups.

In contrast to the global changes commonly required to stratify stock-flow diagrams, our software lets users build a stratified diagram as a “pullback” of simpler diagrams, which can be saved for reuse. Pullbacks are a concept from category theory, and here we are using pullbacks in the category whose objects are system structure diagrams. Remember, these are like stock and flow diagrams, but lacking the quantitative information describing the rates of flows. After a system structure diagram has been constructed, this information can be added to obtain a stock and flow diagram.

This picture shows two different models stratified in two different ways, creating four larger models. I won’t try to really explain this here. But at least you can get a tiny glimpse of how complicated these models get. They get a lot bigger! That’s why we need software based on good math to deal with them efficiently.


[AJ] AlgebraicJulia: Bringing compositionality to technical computing.

[B1] John C. Baez, Xiaoyan Li, Sophie Libkind, Nathaniel D. Osgood and Evan Patterson, Compositional modeling with stock and flow diagrams. To appear in Proceedings of Applied Category Theory 2022.

[B2] John C. Baez, Xiaoyan Li, Sophie Libkind, Nathaniel D. Osgood and Eric Redekopp, A categorical framework for modeling with stock and flow diagrams, to appear in Mathematics for Public Health, Springer, Berlin.

[H] P. S. Hovmand, Community Based System Dynamics, Springer, Berlin, 2014.

[L] Sophie Libkind, Andrew Baas, Micah Halter, Evan Patterson and James P. Fairbanks, An algebraic framework for structured epidemic modelling, Philosophical Transactions of the Royal Society A 380 (2022), 20210309.

[MC] ModelCollab: A web-based application for collaborating on simulation models in real-time using Firebase.

[SF] Stockflow.jl.

Compositional Modeling with Decorated Cospans

27 June, 2022

It’s finally here: software that uses category theory to let you build models of dynamical systems! We’re going to train epidemiologists to use this to model the spread of disease. My first talk on this will be on Wednesday June 29th. You’re invited!

Compositional modeling with decorated cospans, Graph Transformation Theory and Practice (GReTA) seminar, 19:00 UTC, Wednesday 29 June 2022.

You can attend live on Zoom if you click here. You can also watch it live on YouTube, or later recorded, here:

Abstract. Decorated cospans are a general framework for composing open networks and mapping them to dynamical systems. We explain this framework and illustrate it with the example of stock and flow diagrams. These diagrams are widely used in epidemiology to model the dynamics of populations. Although tools already exist for building these diagrams and simulating the systems they describe, we have created a new software package called StockFlow which uses decorated cospans to overcome some limitations of existing software. Our approach cleanly separates the syntax of stock and flow diagrams from the semantics they can be assigned. We have implemented a semantics where stock and flow diagrams are mapped to ordinary differential equations, although others are possible. We illustrate this with code in StockFlow that implements a simplified version of a COVID-19 model used in Canada. This is joint work with Xiaoyan Li, Sophie Libkind, Nathaniel Osgood and Evan Patterson.

My talk is at a seminar on graph rewriting, so I’ll explain how the math applies to graphs before turning to ‘stock-flow diagrams’, like this one here:

Stock-flow diagrams are used to create models in epidemiology. There’s a functor mapping them to dynamical systems.

But the key idea in our work is ‘compositional modeling’. This lets different teams build different models and then later assemble them into a larger model. The most popular existing software for stock-flow diagrams does not allow this. Category theory to the rescue!

This work would be impossible without the right team! Brendan Fong developed decorated cospans and then started the Topos Institute. My coauthors Evan Patterson and Sophie Libkind work there, and they know how to program using category theory.

Evan started a seminar on epidemiological modeling – and my old grad school pal Nate Osgood showed up, along with his grad student Xiaoyan Li! Nate is a computer scientist who now runs the main COVID model for the government of Canada.

So, all together we have serious expertise in category theory, computer science, and epidemiology. Any two parts alone would not be enough for this project.

And I’m not even listing all the people whose work was essential. For example, Kenny Courser and Christina Vasilakopoulou helped modernize the theory of decorated cospans in a way we need here. James Fairbanks, Evan and others designed AlgebraicJulia, the software environment that our package StockFlow relies on. And so on!

Moral: to apply category theory to real-world problems, you need a team.

And we’re just getting started!

Banning Lead in Wetlands

27 September, 2020

An European Union commission has voted to ban the use of lead ammunition near wetlands and waterways! The proposal now needs to be approved by the European Parliament and Council. They are expected to approve the ban. If so, it will go into effect in 2022. The same commission, called REACH, may debate a complete ban on lead ammunition and fishing weights later this year.

Why does this matter? The European Chemicals Agency has estimated that as many as 1.5 million aquatic birds die annually from lead poisoning because they swallow some of the 5000 tonnes of lead shot that land in European wetlands each year. Water birds are more likely to be poisoned by lead because they mistake small lead shot pellets for stones they deliberately ingest to help grind their food.

In fact, about 20,000 tonnes of lead shot is fired each year in the EU, and 60,000 in the US. Eating game shot with lead is not good for you—but also, even low levels of lead in the environment can cause health damage and negative changes in behavior.

How much lead is too much? This is a tricky question, so I’ll just give some data. In the U.S., the geometric mean of the blood lead level among adults was 1.2 micrograms per deciliter (μg/dL) in 2009–2010. Blood lead concentrations in poisoning victims ranges from 30-80 µg/dL in children exposed to lead paint in older houses, 80–100 µg/dL in people working with pottery glazes, 90–140 µg/dL in individuals consuming contaminated herbal medicines, 110–140 µg/dL in indoor shooting range instructors and as high as 330 µg/dL in those drinking fruit juices from glazed earthenware containers!

The amount of lead that US children are exposed to has been dropping, thanks to improved regulations:

However, what seem like low levels now may be high in the grand scheme of things. The amount of lead has increased by a factor of about 300 in the Greenland ice sheet during the past 3000 years. Most of this is due to industrial emissions:

• Amy Ng and Clair Patterson, Natural concentrations of lead in ancient Arctic and Antarctic ice, Geochimica et Cosmochimica Acta 45 (1981), 2109–2121.

Good News (Part 3)

26 December, 2015


Malaria is a nasty disease, caused by mosquitoes infected with parasites. If you catch malaria, you get feverish and tired. You have headaches, and vomit. If you’re unlucky you may have seizures, fall into a coma and even die.

Almost 200 million people got malaria in 2013. Somewhere between 500,000 and a million died. 90% of these people lived in Africa.

This is good news???

Yes, it is! Since 2000, malaria funding has increased nearly tenfold. From 2000 to 2015, cases of malaria in Africa dropped by 40%. Thanks to this, over 600 million cases of malaria have been avoided!

The main reason? Insecticide-treated nets. If you sleep with one of these over your bed, you’re less likely to get bitten by a mosquito.

And how are people getting these nets? The Global Fund to Fight AIDS, Tuberculosis and Malaria, founded in 2002, has distributed 548 million of them. They provide about half the international funding for malaria control worldwide.

The Bill and Melinda Gates Foundation is helping. They’ve spent almost $2 billion to fight malaria. They’ve also contributed $1.6 billion to the Global Fund.

The war against malaria is far from won. One of the main drugs used to fight it is artemisinin. But a strain that’s resistant to artemisinin is spreading near the border of Thailand and Cambodia.

A vaccine would be great. But there’s no vaccine yet. And it’s not easy: malaria is actually caused by several different organisms. Still, stopping just a few of the main culprits would be great.

New technology can change the game. On November 23rd, something amazing happened.

A team of scientists from the University of California announced that they had gotten mosquitoes to pass on malaria resistance genes to almost all their children—not just half, as you’d normally expect!

With this method, malaria resistance could spread through the mosquito population like wildfire.

This method is called a gene drive, and it was implemented using a system called CRISPR. If you haven’t heard about these things, it’s time to do some reading! I’ll give you some links below.

Being sensible and cautious, the scientists have not tested this method in the wild yet. They could do it in less than a year—but they’re in no rush. Said Anthony James:

It’s not going to go anywhere until the social science advances to the point where we can handle it. We’re not about to do anything foolish.

It may be good to test it on a remote island, where mosquitoes can’t fly to another place.

Gene drives are simultaneously very promising and quite scary. If we used one to spread malaria resistance among mosquitoes we could save half a million lives each year – and let poor countries spend their resources on something better.

We are gaining the power to do many things. We just need some wisdom to go along with this power. In fact, many of us have that wisdom. We just need to get better at making it prevail.

For more

On the CRISPR method for spreading malaria resistance:

• Heidi Ledford and Ewen Callaway, ‘Gene drive’ mosquitoes engineered to fight malaria, Nature News, 23 November 2015.

For more on CRISPR:

• Sarah Zhang, Everything you need to know about CRISPR, the new tool that edits DNA, Gizmodo, 6 May 2015.

For the new discovery:

• Valentino M. Gantza, Nijole Jasinskiene, Olga Tatarenkova, Aniko Fazekas, Vanessa M. Macias, Ethan Bier and Anthony A. James, Highly efficient Cas9-mediated gene drive for population modification of the malaria vector mosquito Anopheles stephensi, Proceedings of the National Academy of Sciences 112 (2015).

Good News (Part 2)

25 December, 2015


When I visited Cambodia I went to Kompong Phluk, a village where all the houses are on stilts, and everyone knows how to swim. The villagers raise fish in the Tonlé Sap, which is the largest freshwater lake in southeast Asia. During the dry season, from November to May, this lake drains into the Mekong River near Phnom Penh. But during the monsoons, water flows back from the Mekong into the lake, and it grows six-fold in area! We took a boat ride down this muddy river into the Tonlé Sap and saw the fish farms.

In 2008, a Canadian student named Christopher Charles was working in rural Cambodia. He was living in a house on stilts. He had no electricity or running water, but lots time to sit around and think.

He started thinking about anemia.

Anemia is often caused by an iron deficiency. It makes you tired and weak. You have trouble thinking clearly. Almost half of Cambodia’s population suffers from this disease! In fact, over 3.5 billion people on our planet have anemia, and the World Bank estimates that it’s a $50 billion drain on the global GDP.

You can cure anemia with iron supplements—but they taste bad, and they often cause stomach pains, constipation, and even more disgusting problems.

So Charles had another idea: give villagers little blocks of iron to drop into their cooking pots. The iron gets released slowly as the water boils.

But at first, people hated them. They thought the iron blocks where ugly. They thought the iron blocks would scratch their pots. So they turned them into doorstops.

He kept trying. He needed a second idea: one that could make the first idea work.

He realized that in rural Cambodia almost everything revolves around fish. Fish from the Tonlé Sap provide Cambodians with 60% of their protein intake. People earn lots of their money fishing, they’re important in Khmer folklore. Even their currency—the riel—is named after a fish!

So, he made iron into “lucky fish” , shown here:

Now people are happy to put one into the pot when cooking.

One of those who has been using the fish is Sot Mot, a 60-year-old grandmother who lives just outside Phnom Penh. She drops the fish into boiling water as she chops up garlic, ginger and lemongrass for Khmer chicken soup. “Before, I felt tired and lazy and my chest shook when I was tired,” she says. “But after I use the fish, I have strength and energy to work and I sleep well, too.”

One of her grand-daughters seems to be improving, too. “Before, when I went to school I felt tired, and I didn’t do well at math, maybe the sixth in the class,” says 15-year-old Danai. “Now,” she says proudly, “I’m No. 1.”

Of course, this idea needs to be tested with scientific studies. And here’s one such study:

• Christopher V. Charles et al, Iron-deficiency anaemia in rural Cambodia: community trial of a novel iron supplementation technique, The European Journal of Public Health, 28 January 2010.

More studies are coming up.

No matter what the result finally is, it shows that paying attention to local culture can work wonders when trying to help people.

Large parts of this story are paraphrased from the following radio show, which is definitely worth listening to:

• Michael Sullivan, In Cambodia, ‘lucky’ iron fish in the pot could help fight anemia, Morning Edition, National Public Radio, 25 December 2015.

Good News (Part 1)

24 December, 2015


As we head toward the new year, I’d like to talk about some good news!

Most news is bad news. Why? Because the news media are like the immune system: their job is to warn us of threats. But an over-sensitive immune system can actually cause diseases, like allergies and auto-immune disorders. And the same thing can happen to the body politic when the news media exaggerates threats!

You’ve already heard more than enough bad news. But you may not have heard this good news:

• From 1990 and 2015, the proportion of the world’s population that is undernourished has almost halved, dropping from 19% to 11%.

• Global child mortality from all causes has more than halved since 1990. So, 6.7 million fewer children under the age of five are dying each year now than in 1990.

• Violent crime has declined both in the US and globally: there has been a 35% decline in overall violent crime in the US from 1995 to 2014, and a 6% decline in global homicide rates over from 2000 to 2012.

• There hasn’t been a single case of polio detected in Africa in almost a year and a half! Polio is now known to exist only in Pakistan and Afghanistan. A disease that used to be a major killer, with 350,000 cases in 1988, is now almost extinct.

• Since 2000, worldwide cases of measles have dropped by more than two-thirds, saving more than 17 million lives—largely thanks to vaccination.

• Ebola seems to have been defeated: between November 1st and December 16th, there were only 4 confirmed cases of the disease. Trials of an Ebola vaccine this spring indicated that it was 100% effective.

• Worldwide , the number of children not going to primary school has fallen from 100 million in 2000 to a projected 57 million in 2015.

• This September, for the first time ever, less than 10% of the global population lived in extreme poverty, defined as less than $1.90 per day. This is down from 37% in 1990.

Of course not everything is going well—that’s not what I’m trying to say. Humanity faces a lot of tough problems. But I think many of us will be better at solving these problems if we can learn to be more optimistic. That’s true of me, at least.

(There seem to be people who are too optimistic—but strangely, I don’t know them personally. Am I just avoiding them, or are they avoiding me?)

These facts were taken from here:

• Charles Kenny, 2015: The best year in history for the average human being, The Atlantic, 18 December 2015.

Some are paraphrased from Alexander Kruel, a cool dude who pointed out this article on Google+. You can see the sources for the facts above in the article.

I would like to know if $1.90 is in constant dollars, but I didn’t have the patience to dig through the report where this fact came from:

Global Monitoring Report 2015/2016: Development Goals in an Era of Demographic Change, World Bank, Washington DC, 2015.

Can you find out?