Showing posts with label diffusion processes. Show all posts
Showing posts with label diffusion processes. Show all posts

Wednesday, February 12, 2020

Contagion, coronavirus, and charlatans

This post is an illustration of a simple epidemiological model and why some of the ad-hoc modeling of coronavirus that some charlatans are spreading on social media platforms is a nonsensical distraction.


Math of contagion: the SIR-1 model


A simple model for infectious diseases, the SIR-1 model (also known as Kendrick-McCormack model), is too simple for the coronavirus, but contains some of the basic behavior of any epidemic.

The model uses a fixed population, with no deaths, no natural immunity, no latent period for the disease (when a person is exposed but not infectious; not to be mistaken for what happens with the coronavirus, where people are infectious but asymptomatic), and a simple topology (the population is in a single homogeneous pool, instead of different cities and countries sparsely connected).

There are three states that a given individual can be in: susceptible (fraction on the population in this state represented by $S$), infectious (fraction represented by $I$), and recovered (fraction represented by $R$); recovered means immune, so there isn't recurrence of an infection.

There are two parameters: $\beta$, the contagiousness of the disease, and $\gamma$, the recovery rate. To illustrate using discretized time, $\beta= 0.06$ means that any infectious individual has a 6% chance of infecting another individual in the next period (say, a day); $\gamma= 0.03$ means that any infectious individual has a 3% chance of recovering in the next period.

The dynamics of the model are described by three differential equations:

$\dot S = - \beta S I$;
$\dot I = (\beta S - \gamma) I$;
$\dot R = \gamma I$.

The ratio $R_0 = \beta/\gamma$ is critical to the behavior of an epidemic: if lower than one, the infection dies off without noticeable expansion, if much higher than one, it becomes a large epidemic.

There is no analytic solution to the differential equations, but they're easy enough to simulate and to fit data to. Here are some results for a discretized, 200-period simulation for some values of the parameters $(\beta, \gamma)$, starting with an initial infected population of 1%.

First, a model with an $R_0=2$, illustrating the three processes:


Note that although a large percentage of the population is eventually infected (if we continue to run the model, it will converge to 100%), the number of people infectious at a given time (and presumably also feeling the symptoms of the disease) is much lower, and this is a very important metric, as the number of people sick at a given time determines how effectively health providers can deal with the disease.

Next, a model of runaway epidemic (the $R_0 = 24$ is beyond any epidemic I've known; used here only to make the point in a short 200 periods):


In this case, the number of sick people grows very fast, which makes it difficult for the health system to cope with the disease, plus the absence of the sick people from the workforce leads to second-order problems, including stalled production, insufficient logistics to distribute needed supplies, and lack of services and support for necessary infrastructure.

Finally, a model closer to non-epidemic diseases, like the seasonal flu (as opposed to epidemic flu), though the $(\beta,\gamma)$ are too high for that disease; this was necessary for presentation purposes, in order to make the 200-period chart more than three flat lines.


Note how low the number of people infected at any time is, which is why these things tend to die off, instead of growing into epidemics, once people start taking precautions and that $\beta$ becomes smaller than $\gamma$ which leads to a $R_0 < 1$, a condition for the disease to die off eventually.


The problem with estimating ad-hoc models


One of the problems with ignoring the elements of these epidemiological models and calibrating statistical models on early data can be seen when we take the first example above ($\beta=0.06,\gamma=0.03$) and use the first 50 data points to calibrate a statistical model for forecasting the evolution of the epidemic:


As a general rule of thumb, models for processes that follow a S-shaped curve are extremely difficult to calibrate on early data; any data set that doesn't extend at least some periods into the concave region of the model is going to be of questionable value, especially if there are errors in measurement (as is always the case).

Consider that the failure of that estimation is for the simplest model (SIR-1), without the complexities of topology (multiple populations in different locations, each with a $(\beta,\gamma)$ of their own, connected by a network of transportation with different levels of quarantine and preventative measures, etc.), possible obfuscation of some data due to political concerns, misdiagnosis and under-reporting due to latency, changes to the $\beta$ and $\gamma$ as people's behavior adapts and health services adapt, and many other complications of a real-world epidemic including second-order effects on health services and essential infrastructure, which change people's behavior as well.

No, that forecasting error comes simply from that rule of thumb, that until the process passes the inflection point, it's almost certain that estimates based on aggregate numbers (as opposed to clinical measures of $\beta$ and $\gamma$, based on analysis of clinical cases; these are what epidemiologists use, by the way) will give nonsensical predictions.

But those nonsensical predictions get retweets, YouTube video views, and SuperChat money.

Saturday, February 8, 2020

Fun with numbers for February 8, 2020

Some collected twitterage and other nerditude from the interwebs.

Converting California to EVs: we're going to need a bigger boat grid


I like how silent electric vehicles are, but if California is to convert a significant number of FF cars to electric (50-80%), its grid will need to deliver 11-18% more energy (we already import around 1/3 of that energy and our grid is not exactly underutilized).




Playing around with diffusion models to avoid thinking about coronavirus


Playing around with some diffusion models of infection, not really sophisticated enough to deal with the topological complexities of coronavirus given air travel but better than people who believe you get that virus from drinking too much Corona beer… 🤯




Better choose winners of the past or the new thing?


Based on the following tweet by TJIC, author of Prometheus Award winning hard scifi books (first, second) about homesteading the Moon, with uplifted (genetically engineered, intelligent) Dogs and sentient AI,


I decided to create a simple model and just run with it. For laughs only.

We need to have some sort of metric of quality, $x$, and we'll assume that since people can stop reading a novel if it's too bad, $x \ge 0$. We also know Sturgeon's law, that 90% of everything is dross, so we'll need a distribution with a long left tail. For now we're okay with the exponential distribution $f_X(x) = \lambda \exp(-\lambda x)$, and we'll go with a $\lambda = 1$ to start.

Instead of changing the average quality of the novels for different years, we'll change the sample size from which the winners are chosen; what we're interested in is, therefore, $M(x)_N = E\left[\max\{x_1,\ldots,x_N\}\right]$ for different $N$, the number of novels. Assuming that $10 < N < 100000$, we can use a simple simulation to find those $M(x)_N$:


The results are

$M(x)_{10} = 2.899432$
$M(x)_{100} = 5.230011$
$M(x)_{1000} = 7.512119$
$M(x)_{10000} =  9.750539$
$M(x)_{100000} =   12.122326$

Let's say there are between 100 and 1000 scifi novels worthy of that name in any given year of the last 100 years. So, unless the new novels have on average between 5.2 and 7.5 times the average quality of those in the previous 100 years, one is better off picking a winner at random from those 100 years than a random new novel.

(Yes, there's a lot of nonsense in this model, but the idea is just to show that when there's a long left tail, which comes from Sturgeon's law --- and this one isn't even that steep --- randomly picking past winners is a better choice than randomly picking new novels even if the quality improved a bit relative to the past.)



No numbers, just Bay Area seamanship





Live long and prosper.

Friday, October 4, 2019

Fun with numbers for October 4, 2019

It's flu season, let's talk product diffusion


One of the classic marketing models people learn in innovation classes is basically a SIR(1) model without the R part: the Bass model of product diffusion.

The idea is that some fraction $a$ of the consumers are "innovators" who adopt a product without social pressure, while another fraction $b$ are "imitators" who adopt a product when they see others with it. The fraction $x$ of the market that has adopted the product at a given time is given by the following differential equation

$\dot x = (a  + b x)(1-x)$, 

and the behavior looks like a traditional product life-cycle curve (an S-shaped curve):




The process for a viral infection is similar: some people get the virus from the environment (those would be the $a$ fraction), some get it from contact with other people (those would be the $b$); the infection process has a third element, recovery, which we ignored here.



Growth confusion and punditry, part 1


Pundits throwing around growth numbers seem to be unaware that there are significant differences even with very small growth numbers.




Growth confusion and punditry, part 2


A pundit: "it's important to get the economics high-growth first, so that the slower growth starts from a higher number." (Paraphrased.)

Me: Gah! Multiplication is transitive. The order doesn't matter, what matters is that the high-growth period be the longer period.

Consider two periods, with $t_1$ and $t_2$, with associated growth rates $r_1$ and $r_2$. Starting from some value $x_0$, the result of period 1 before period 2 is:

$\left( x_0 \, e^{r_1 t_1} \right) \, e^{r_2 t_2}$,

and the result of period 2 before period 1 is

$\left( x_0 \, e^{r_2 t_2} \right) \, e^{r_1 t_1}$,

in other words, the same result.

These pundits get paid to go on television and say these things and to write them in Op-Eds. And influential people take them seriously. The innumeracy is staggering.



Having some fun with Tesla data


Downloaded some historical data from Yahoo Finance (yes, I have other better sources, but this one is public and can be shared) and played around with smoothing. Here's a nice view of the TSLA closing price for the last year using the same triangular smoothing I did for my bodyweight (in other words, a second-order moving average of (5,5)):



Throughout the first half of 2019 Tesla boosters on Twitter were fully convinced that this would be the year that heralded the end of the internal combustion engine car. In reality, this seems to be the year in which Tesla's financial shenanigans are likely to bring its valuation to a more appropriate level.

CYA statement: I have no personal position on Tesla and will not initiate one in the next 72 hours. This is not intended as financial advice and represents my personal views (of making fun of Tesla boosters) not those of my employer or our clients.

Also:

(Yes, it's sarcastic.Very, very sarcastic.)



Yet another infrastructure photo