<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Viktor Kärnstrand on Medium]]></title>
        <description><![CDATA[Stories by Viktor Kärnstrand on Medium]]></description>
        <link>https://medium.com/@viktor.karnstrand?source=rss-6dff6c04871------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BUwJcqHEoW1DI7ea</url>
            <title>Stories by Viktor Kärnstrand on Medium</title>
            <link>https://medium.com/@viktor.karnstrand?source=rss-6dff6c04871------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 06:59:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@viktor.karnstrand/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Estimating the effect of social distancing in Sweden]]></title>
            <link>https://medium.com/data-science/estimating-the-effect-of-social-distancing-in-sweden-c6c1e606c8f9?source=rss-6dff6c04871------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6c1e606c8f9</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[kalman-filter]]></category>
            <category><![CDATA[covid19]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <category><![CDATA[sweden]]></category>
            <dc:creator><![CDATA[Viktor Kärnstrand]]></dc:creator>
            <pubDate>Sat, 11 Apr 2020 22:50:39 GMT</pubDate>
            <atom:updated>2020-04-12T22:35:06.062Z</atom:updated>
            <content:encoded><![CDATA[<h3>Estimating the effect of social distancing</h3><h4>A Bayesian approach to estimate parameters for the dynamics of a pandemic</h4><p>In my last post, I created a GUI to simulate the dynamics of the Covid-19 outbreak using a deterministic SIR model. I then mentioned something about creating a fancy method to estimate the infection rate <em>β </em>in the model over time<em>. </em>Today I have taken <em>β, </em>added it as a state in the SIR model, modeled it as a stochastic process and thereby created a <strong>Stochastic SIR model</strong>. Then I used something fancy called <strong>Unscented Kalman Filtering </strong>and something even fancier called <strong>Unscented Rauch-Tung-Striebel Smoothing</strong> to estimate the state of the process in general, and <em>β </em>in particular to see if there is any evidence in the data that social distancing actually works.</p><p>Those of you who are not into math can just skip the technicalities and look at the pretty images. If you are into math, and perhaps into Bayesian estimation in particular, forgive me for any stupid approximations I make. Every time I pretend something is Gaussian or approximately linear it is just me trying to get by. But please weight in here if you have better approximations or methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gdQFf793fWKDhDQ0kxa-Ew.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@uniqueton?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Anton</a> on <a href="https://unsplash.com/s/photos/statistics?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In my last post, I used the number of reported cases, since there were not many fatalities in Sweden at that time. Looking at the data now it seems that reported cases is not a very reliable metric since it can be affected by policies and behavioral changes. Unfortunately, there are now quite a few fatalities per day, and that data is not subject to the same temporal circumstances and thus I have chosen to use that data for this study.</p><p>Last time I also left out the actual math behind the <a href="https://en.wikipedia.org/wiki/Mathematical_modelling_of_infectious_disease">SIR model</a>, which is actually pretty simple. The three groups <strong>S</strong>usceptible, <strong>I</strong>nfected and <strong>R</strong>esistent are coupled as the following differential equation describes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/0*i4JYi4SDy-y4pGMl.png" /></figure><p>It is also worth mentioning that N ≡ S + I + R is constant. So currently we have a deterministic model where the <strong>infection rate β </strong>and the <strong>recovery rate γ </strong>are constants. I will also stick to my dubious claim that γ=0.2, but now we put β into the model state and add two equations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/0*NQjFgikntpvrJIVt.png" /></figure><p>So we say that we cannot deterministically model β but we say that it changes with some velocity and the time derivative of that velocity is <strong>white </strong>and <strong>normally </strong>distributed around zero with some standard deviation <em>q</em>. The term white in this context means that the process <em>v(t)</em> has no memory, meaning any knowledge about the state at one time does not add any information when predicting any future state, as opposed to β and its time derivative which we say are <a href="https://en.wikipedia.org/wiki/Markov_chain"><strong>Markov</strong></a>. Meaning the process has a memory and the current state is sufficient to describe the process going forward, any prior information about the process is redundant.</p><p>Ok, so now we have a stochastic SIR model which is pretty cool. But what are we going to do with it? Well, this model provides us with some information about the dynamics of the outbreak, now we want to <strong>fuse </strong>that with the data to estimate the current <strong>state of the outbreak</strong>. Meaning how many Susceptible, Infected and Resistant there currently are in the population, and also what the infection rate is. Ultimately we also would want to go back in time and tell the state of the outbreak in retrospect.</p><p>As it turns out there is this classical method for estimating a process state using measurement data given that you have a <strong>linear</strong> process model with <strong>white Gaussian process noise</strong> and that we can describe the measurements with a linear model that has <strong>white gaussian measurement errors</strong>. Gaussian means normally distributed, and normally distributed white process noise we do have - check. Our model is not exactly linear, but we will have to solve that later. Firstly we have to come up with a measurement model. Here is one now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cnG1gwJUC6u4DAgl.png" /></figure><p>So the number of fatalities on day <em>k </em>is <strong>Poisson distributed</strong> with a rate that is proportional to the number of infected individuals scaled by λ, the <strong>fatality rate</strong>. If the number of fatalities is large enough, say &gt;30, then the normal distribution is a fairly good approximation. For the cases where the number of fatalities are <em>&lt;30 </em>we will just have to pretend the approximation is OK. The measurement error in this model is white though, as opposed to if you use any numbers that are aggregated over time which is a common mistake when starting to play with Kalman filters.</p><p>Now, if our model was linear we could basically have used the Kalman Filter by now, but this is not the case so we will have to do something about that. If the model is fairly linear it is an option to just linearize it using the estimate at each time (which is then referred to as the <strong>Extended Kalman Filter</strong>), but this model is not that nice, it turns out. For all you math nerds out there curiously wondering how I know, it is because the Jacobian of the time derivative is not <a href="https://en.wikipedia.org/wiki/Nilpotent">nilpotent</a>. 🤓</p><p>So instead we use something called the <strong>Unscented Transform, </strong>which essentially means to discretize the distribution into a number of points. These points can then be propagated using the non-linear model and can then be used to calculate the predicted distribution and the very crucial matrix called the <strong>Kalman Gain</strong>. This version of the Kalman Filter is called the <a href="https://en.wikipedia.org/wiki/Kalman_filter#Unscented_Kalman_filter"><strong>Unscented Kalman Filter </strong></a>and is very good for a number of additional reasons. It does not require a lot of analytical calculations, the model works pretty much as is. It typically also has a better numerical stability than the Extended Kalman Filter.</p><p>Now, all we have to do is to formulate a <strong>non-informative prior</strong> distribution for our outbreak parameters at day zero. This means that we describe our knowledge of the state parameters as a multivariate normal distribution with some initial guess as mean value and some pretty inflated variances to reflect that we really do not know much about the system at this point. Then we can predict a prior distribution for day one, update that with the data using the Kalman Gain, predict a prior for day two etc. At some point, we get to the last day <em>N </em>that we have data, at which point we will have an <strong>unbiased optimal estimate</strong> for that day… Given that we do not remember any of the cheating and approximations that we have made along the way. 🤔</p><p>The problem now is that the estimates for all the earlier days <em>k&lt;N </em>are outdated, but we want the utilize all information to estimate the change in β that has happened due to increasing government regulations and recommendations. Luckily three clever German-American rocket scientists at Lockheed Missile Company came up with an extension to the Kalman Filter to go back in time and account for information “from the future”. These guys where called Rauch, Tung and Striebel and hence the method is called <a href="https://en.wikipedia.org/wiki/Kalman_filter#Rauch%E2%80%93Tung%E2%80%93Striebel"><strong>Rauch-Tung-Striebel Smoothing</strong></a>.</p><p>Yet another problem though, this method does not work out of the box with the Unscented Kalman Filter. But not more than ten years ago a Finnish researcher called Simo Särkkä came up with a <a href="https://users.aalto.fi/~ssarkka/pub/uks-preprint.pdf">solution</a>. So now we have our models, we have the data, we can assert some initial guess, estimate the state for each day using the Unscented Kalman Filter, and when we get to the end we go back using the Unscented Rauch-Tung-Striebel smoother. 👌</p><p>Before getting to the results I should also mention that I did some pre-smoothing of the data for two reasons. Primarily to mitigate that some weekdays have consistently lower or higher numbers and in general, a higher variability than they should due to shortcomings in the way health care authorities report dates for fatalities. Also because of the fact that the Gaussian assumption is not all that good for low fatality numbers. There are better methods to deal with this, but it had to do for now.</p><p>I should also mention that I actually complicated things a bit by adding another group of infected to the SIR model. The individuals that are infected are transferred to the first group where they can affect others, after which they transition to the second group where they contribute to the fatality predictions. I made this choice because there seems to be a delay between reported cases and fatalities which is not reflected in the standard SIR model. Also, I worked for a while with combining data from reported cases and fatalities and in that case, it was necessary, then it so happened that I did not remove it from the model. The transition rate between these groups is the same as the recovery rate γ in the examples.</p><p>Also, the fatality rate λ is not known, and there are no reliable estimates out there yet so I will provide examples for 0.5%, 0.2% and 1% mortality. The fatality rate λ can then be calculated from the total mortality rate and the assumed recovery rate γ. The examples contain estimates with standard deviations for β over time and also predictions from the 10th of April going forward where there is a best guess illustrated as a continuous line and dashed lines representing one standard deviation off the β estimate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1_l5E8GYcI2ppxRZeBSmPQ.png" /><figcaption>Mortality Rate = 0.5%</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XxPfZMWkUEUnAkUh5ma7rA.png" /><figcaption>Mortality Rate = 0.2%</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9a4jLzO9e3NFgMYy1S7y3Q.png" /><figcaption>Mortality Rate = 1%</figcaption></figure><p>These results are at least an indication that social distancing policy has had an effect on the infection rate β, which seems to have dropped from 0.5 to 0.27±0.03 in a month time.It also seems that the fatality peak will be at the <strong>end of April</strong> given that the actual mortality is around <em>0.2%</em> and that the infection rate is kept close to <em>0.2</em>, in which case only approximately <strong>8.000</strong> swedes will lose their lives.</p><p>If, on the other hand, the actual mortality is closer to 1% the peak will happen during the <strong>turn from May to June</strong> and between <strong>30.000 and 60.000 </strong>swedes will lose their lives depending on the continued success of social distancing.</p><p>It is a delicate act of balance that the authorities will have to pull off, with limited data and blunt tools to intervene. And the stakes are apparently high.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6c1e606c8f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/estimating-the-effect-of-social-distancing-in-sweden-c6c1e606c8f9">Estimating the effect of social distancing in Sweden</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Covid-19 Prediction for Sweden]]></title>
            <link>https://medium.com/@viktor.karnstrand/covid-19-prediction-for-sweden-a338dbf2a3b8?source=rss-6dff6c04871------2</link>
            <guid isPermaLink="false">https://medium.com/p/a338dbf2a3b8</guid>
            <category><![CDATA[sweden]]></category>
            <category><![CDATA[covid19]]></category>
            <category><![CDATA[coronavirus]]></category>
            <category><![CDATA[epidemiology]]></category>
            <category><![CDATA[corona]]></category>
            <dc:creator><![CDATA[Viktor Kärnstrand]]></dc:creator>
            <pubDate>Wed, 01 Apr 2020 17:35:13 GMT</pubDate>
            <atom:updated>2020-04-01T19:37:59.021Z</atom:updated>
            <content:encoded><![CDATA[<h3>COVID-19 Prediction for Sweden</h3><p>Since Corona cleared my schedule for the near future I had to find a hobby. Epidemiology perhaps? That seems hot now. I sat down last night and I though I would share with you my first semi-scientific prediction for the development of <strong>COVID-19 </strong>in Sweden. Also I will touch on Swedens’ much debated policies, which are far less restrictive at this point that those for similar states. But first a disclaimer.</p><p><strong>Disclaimer: </strong>I do not have any qualifications in epidemiology whatsoever</p><p>Alright, so if you want to cite me do so at your own risk. Anyway, I found some data on this site <a href="https://ourworldindata.org/coronavirus-source-data">https://ourworldindata.org/coronavirus-source-data</a>… I had some initial ideas about stochastic simulations and spatial simulations. But I started to play with a deterministic zero dimensional macro model and after some research I decided to use the well established <strong>SIR </strong>model. <a href="https://en.wikipedia.org/wiki/Mathematical_modelling_of_infectious_disease">https://en.wikipedia.org/wiki/Mathematical_modelling_of_infectious_disease</a></p><p>The model has a three dimensional state space representing the three groups <strong>Susceptible</strong>, <strong>Infectious</strong> and <strong>Recovered</strong>. The data I found contained the total number of confirmed cases for each day with Februari 28th as day zero. This would represent Infectious + Recovered in the model.</p><p>The model has two parameters. Parameter <strong>β</strong> representing the number of people exposed by one infectious individual per day. And Parameter <strong>γ </strong>representing the recovery rate or the inverse of the period of time an individual is infectious. After some experimentation with the model and the data I could conclude that policies seems to have altered the parameters. Logic would dictate that government policies such as social isolation, closing down events and other restrictions would affect β most, an assumption that seemed to hold when comparing model and data. I therefor chose to keep γ constant at a rate of 0.2 (I will cite Google search on that one). I then built a GUI in Matlab so that I could alter β over time and compare with the data as I went along. I suppose some fancy parameter optimization would be better, but that will be something for later.</p><p>In the GUI β and γ can be adjusted over time using linear interpolation in the two lower (interactive) plots. The main plot is logarithmic and displays the data points as black rings and the model as:</p><ul><li>Blue Line = Susceptible</li><li>Red Line = Infectious</li><li>Green Line = Infectious + Recovered</li></ul><p>Starting out I trusted the data blindly, meaning I made the assumption that those cases reported by the health care are the only cases and that there are no unrecorded cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uYXk7OkBRZ183itlMQe6Hg.png" /><figcaption>If Sweden would keep the current policy for a total period of 200 days after day zero (Februari 28) 54% of the population would eventually get COVID-19 and we would reach a peak of half million infected individuals after 128 days (Juli 6).</figcaption></figure><p>Those results does not seems totally unrealistic, but since I figured not everyone infected go and have themselves tested I also added a real cases / recorded cases ratio parameter. Some studies suggest that a large portion of those affected are totally asymptomatic so the number of unrecorded cases might be quite substantial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y82-zgLxLIV9YVmQEfMyIw.png" /><figcaption>Assuming actual cases are about five times as many as observed and Sweden would keep the current policy for a total period of 200 days after day zero (Februari 28) 53% of the population would eventually get COVID-19 and we would reach a peak of 477.000 infected individuals after 107 days (June 14).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WvqtxYRNXhvv69Lfxxh2uw.png" /><figcaption>Assuming actual cases are about ten times as many as observed and Sweden would keep the current policy for a total period of 200 days after day zero (Februari 28) 53% of the population would eventually get COVID-19and we would reach a peak of 466.000 infected individuals after 100 days (June 7).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zKv9TxvBteR6EQEdtwKdZw.png" /><figcaption>Assuming actual cases are about 100 times as many as observed and Sweden would keep the current policy for a total period of 200 days after day zero (Februari 28) 50% of the population would eventually get COVID-19 and we would reach a peak of 421.000 infected individuals after 65 days (May 4).</figcaption></figure><p>It turns out it would not be all that bad if we have a lot of unrecorded cases of COVID-19, not even if the actual number is 100 times greater than the recorded cases. It seems good to keep the process going in the beginning to reach the peak faster, and when that sharp exponential growth is approaching enforce policies that mitigates the peak. For the remainder I have assumed that the unrecorded case factor is 60, which would mean that about 300.000 people in Sweden has at some point been infected by COVID-19.</p><p>This state of emergency will not go on forever, but when can we start to go <strong>back to normal</strong>? Let us assume that the transition back to normal will happen continuously over the course of 30 days, then when can we start?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBGkOUihzk36xYzWqHR21w.png" /><figcaption>Assuming actual cases are about 60 times as many as observed and Sweden would phase out emergency measures 60 (April 29) days after day zero (Februari 28) 83% of the population would eventually get Covid-19 and we would reach a peak of over 1.000.000 infected individuals after 88 days (May 26).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YeXEHCENh70dYLAvP4yL3w.png" /><figcaption>Assuming actual cases are about 60 times as many as observed and Sweden would phase out emergency measures 80 (May 18) days after day zero (Februari 28) 75% of the population would eventually get Covid-19 and we would reach a peak of over 446.000 infected individuals after 106 days (June 13).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4x9K7WO-NXcYQUdn_va0fg.png" /><figcaption>Assuming actual cases are about 60 times as many as observed and Sweden would phase out emergency measures 100 (June 8) days after day zero (Februari 28) 73% of the population would eventually get Covid-19 and we would reach a peak of over 400.000 infected individuals after 72 days (May 10).</figcaption></figure><p>If these assumptions are approximately correct a first peak will be reached in the mid of May and swedes might begin the transition to normal some time during June or possibly even the end of May.</p><p>So far it seems that stricter policies and very little exposure are not always for the best. But what would have happened if Sweden <strong>took no action</strong> at all?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NJYy9tNwhjMGDgpc0MmmGg.png" /><figcaption>Assuming Sweden took no actions at all 89% of the population would eventually get Covid-19 and we would reach a peak of over 2.300.000 infected individuals after 35 days (April 3).</figcaption></figure><p>Ok, so apparently that is not an optimal strategy, what about a very <strong>strict policy</strong> as advocated by many countries such as China or South Korea?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nrLz29-xXcITL_H0YhKjMQ.png" /><figcaption>Assuming Sweden adopted a strict policy when going into Mars and started going back to normal in the end of June 84% of the population would eventually get Covid-19 and we would reach a peak of approximately 2.300.000 infected individuals after 181 days (August 28).</figcaption></figure><p>It also seems you do not get rid of the problem by simply closing everything down, that is if no one comes up with a vaccine during that time. At some point people will have to go back to business and that is when the shit hits the fan. It seems that the Swedish policy is right on the money at this point.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a338dbf2a3b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>