<metaname="description"content="If all we have is a finite number of heartbeats left, what about me? Warning: this one is a bit creepier. But that's what you get when you come up with data science ideas as you're drifting off to ...">
<p>If all we have is a finite number of heartbeats left, what about me?</p>
<hr>
<p>Warning: this one is a bit creepier. But that's what you get when you come up with data science ideas as you're drifting off to sleep.</p>
<h1id="2.5-Billion">2.5 Billion<aclass="anchor-link"href="#2.5-Billion">¶</a></h1><p>If <ahref="http://www.pbs.org/wgbh/nova/heart/heartfacts.html">PBS</a> is right, that's the total number of heartbeats we get. Approximately once every second that number goes down, and down, and down again...</p>
<p>I got a Fitbit this past Christmas season, mostly because I was interested in the data and trying to work on some data science projects with it. This is going to be the first project, but there will likely be more (and not nearly as morbid). My idea was: If this is the final number that I'm running up against, how far have I come, and how far am I likely to go? I've currently had about 3 months' time to estimate what my data will look like, so let's go ahead and see: given a lifetime 2.5 billion heart beats, how much time do I have left?</p>
<h1id="Statistical-Considerations">Statistical Considerations<aclass="anchor-link"href="#Statistical-Considerations">¶</a></h1><p>Since I'm starting to work with health data, there are a few considerations I think are important before I start digging through my data.</p>
<ol>
<li>The concept of 2.5 billion as an agreed-upon number is tenuous at best. I've seen anywhere from <ahref="http://gizmodo.com/5982977/how-many-heartbeats-does-each-species-get-in-a-lifetime">2.21 billion</a> to <ahref="http://wonderopolis.org/wonder/how-many-times-does-your-heart-beat-in-a-lifetime/">3.4 billion</a> so even if I knew exactly how many times my heart had beaten so far, the ending result is suspect at best. I'm using 2.5 billion because that seems to be about the midpoint of the estimates I've seen so far.</li>
<li>Most of the numbers I've seen so far are based on extrapolating number of heart beats from life expectancy. As life expectancy goes up, the number of expected heart beats goes up too.</li>
<li>My estimation of the number of heartbeats in my life so far is based on 3 months worth of data, and I'm extrapolating an entire lifetime based on this.</li>
</ol>
<p>So while the ending number is <strong>not useful in any medical context</strong>, it is still an interesting project to work with the data I have on hand.</p>
<h1id="Getting-the-data">Getting the data<aclass="anchor-link"href="#Getting-the-data">¶</a></h1><p><ahref="https://www.fitbit.com/">Fitbit</a> has an <ahref="https://dev.fitbit.com/">API available</a> for people to pull their personal data off the system. It requires registering an application, authentication with OAuth, and some other complicated things. <strong>If you're not interested in how I fetch the data, skip <ahref="#Wild-Extrapolations-from-Small-Data">here</a></strong>.</p>
<h2id="Registering-an-application">Registering an application<aclass="anchor-link"href="#Registering-an-application">¶</a></h2><p>I've already <ahref="https://dev.fitbit.com/apps/new">registered a personal application</a> with Fitbit, so I can go ahead and retrieve things like the client secret from a file.</p>
<h2id="Handling-OAuth-2">Handling OAuth 2<aclass="anchor-link"href="#Handling-OAuth-2">¶</a></h2><p>So, all the people that know what OAuth 2 is know what's coming next. For those who don't: OAuth is how people allow applications to access other data without having to know your password. Essentially the dialog goes like this:</p>
<pre><code>Application: I've got a user here who wants to use my application, but I need their data.
Fitbit: OK, what data do you need access to, and for how long?
Application: I need all of these scopes, and for this amount of time.
Fitbit: OK, let me check with the user to make sure they really want to do this.
Fitbit: User, do you really want to let this application have your data?
User: I do! And to prove it, here's my password.
Fitbit: OK, everything checks out. I'll let the application access your data.
Fitbit: Application, you can access the user's data. Use this special value whenever you need to request data from me.
Application: Thank you, now give me all the data.</code></pre>
<p>Effectively, this allows an application to gain access to a user's data without ever needing to know the user's password. That way, even if the other application is hacked, the user's original data remains safe. Plus, the user can let the data service know to stop providing the application access any time they want. All in all, very secure.</p>
<p>It does make handling small requests a bit challenging, but I'll go through the steps here. We'll be using the <ahref="https://dev.fitbit.com/docs/oauth2/">Implicit Grant</a> workflow, as it requires fewer steps in processing.</p>
<p>First, we need to set up the URL the user would visit to authenticate:</p>
<p>Now, here you would print out the request URL, go visit it, and get the full URL that it sends you back to. Because that is very sensitive information (specifically containing my <code>CLIENT_ID</code> that I'd really rather not share on the internet), I've skipped that step in the code here, but it happens in the background.</p>
<h2id="Requesting-the-data">Requesting the data<aclass="anchor-link"href="#Requesting-the-data">¶</a></h2><p>Now that we've actually set up our access via the <code>access_token</code>, it's time to get the actual <ahref="https://dev.fitbit.com/docs/heart-rate/">heart rate data</a>. I'll be using data from January 1, 2016 through March 31, 2016, and extrapolating wildly from that.</p>
<p>Fitbit only lets us fetch intraday data one day at a time, so I'll create a date range using pandas and iterate through that to pull down all the data.</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Heartbeats from </span><spanclass="si">{}</span><spanclass="s2"> to </span><spanclass="si">{}</span><spanclass="s2">: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">hr_df_full</span><spanclass="o">.</span><spanclass="n">index</span><spanclass="p">[</span><spanclass="mi">0</span><spanclass="p">],</span>
<p>And now we've retrieved all the available heart rate data for January 1<sup>st</sup> through March 31<sup>st</sup>! Let's get to the actual analysis.</p>
<h1id="Wild-Extrapolations-from-Small-Data">Wild Extrapolations from Small Data<aclass="anchor-link"href="#Wild-Extrapolations-from-Small-Data">¶</a></h1><p>A fundamental issue of this data is that it's pretty small. I'm using 3 months of data to make predictions about my entire life. But, purely as an exercise, I'll move forward.</p>
<h2id="How-many-heartbeats-so-far?">How many heartbeats so far?<aclass="anchor-link"href="#How-many-heartbeats-so-far?">¶</a></h2><p>The first step is figuring out how many of the 2.5 billion heartbeats I've used so far. We're going to try and work backward from the present day to when I was born to get that number. The easy part comes first: going back to January 1<sup>st</sup>, 1992. That's because I can generalize how many 3-month increments there were between now and then, account for leap years, and call that section done.</p>
<p>Between January 1992 and January 2016 there were 96 quarters, and 6 leap days. The number we're looking for is:</p>
<p>So between January 1992 and January 2016 I've used $\approx$ 774 million heartbeats. Now, I need to go back to my exact birthday. I'm going to first find on average how many heartbeats I use in a minute, and multiply that by the number of minutes between my birthday and January 1992.</p>
<p>For privacy purposes I'll put the code here that I'm using, but without any identifying information:</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Heartbeats so far: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">heartbeats_until_2016</span><spanclass="p">))</span>
<p>It would appear that my heart has beaten 775,804,660 times between my moment of birth and January 1<sup>st</sup> 2016, and that I have 1.72 billion left.</p>
<h2id="How-many-heartbeats-longer?">How many heartbeats longer?<aclass="anchor-link"href="#How-many-heartbeats-longer?">¶</a></h2><p>Now comes the tricky bit. I know how many heart beats I've used so far, and how many I have remaining, so I'd like to come up with a (relatively) accurate estimate of when exactly my heart should give out. We'll do this in a few steps, increasing in granularity.</p>
<p>First step, how many heartbeats do I use in a 4-year period? I have data for a single quarter including leap day, so I want to know:</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Four year periods remaining: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">four_year_periods</span><spanclass="p">))</span>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Remaining heartbeats after 4 year periods: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">remaining_4y</span><spanclass="p">))</span>
<p>Given that there are 13 four-year periods left, I can move from 2016 all the way to 2068, and find that I will have 48 million heart beats left. Let's drop down to figuring out how many quarters that is. I know that 2068 will have a leap day (unless someone finally decides to get rid of them), so I'll subtract that out first. Then, I'm left to figure out how many quarters exactly are left.</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Quarters left starting 2068: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">quarters_left</span><spanclass="p">))</span>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Remaining heartbeats after that: </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">remaining_year</span><spanclass="p">))</span>
<p>So, that analysis gets me through until January 1<sup>st</sup> 2070. Final step, using that minute estimate to figure out how many minutes past that I'm predicted to have:</p>
<p>According to this, I've got until February 23<sup>rd</sup>, 2070 at 5:28 PM in the evening before my heart gives out.</p>
<h1id="Summary">Summary<aclass="anchor-link"href="#Summary">¶</a></h1><p>Well, that's kind of a creepy date to know. As I said at the top though, <strong>this number is totally useless in any medical context</strong>. It ignores the rate at which we continue to get better at making people live longer, and is extrapolating from 3 months' worth of data the rest of my life. Additionally, throughout my time developing this post I made many minor mistakes. I think they're all fixed now, but it's easy to mix a number up here or there and the analysis gets thrown off by a couple years.</p>
<p>Even still, I think philosophically humans have a desire to know how much time we have left in the world. <ahref="https://www.biblegateway.com/passage/?search=psalm+144&version=ESV">Man is but a breath</a>, and it's scary to think just how quickly that date may be coming up. This analysis asks an important question though: what are you going to do with the time you have left?</p>
<p>Thanks for sticking with me on this one, I promise it will be much less depressing next time!</p>