"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["9363"],{1998:function(e,s,a){a.r(s),a.d(s,{assets:function(){return h},contentTitle:function(){return l},default:function(){return d},frontMatter:function(){return r},metadata:function(){return t},toc:function(){return m}});var t=a(2208),n=a(5893),i=a(65);let r={slug:"2016/04/tick-tock",title:"Tick tock...",date:new Date("2016-04-06T12:00:00.000Z"),authors:["bspeice"],tags:[]},l=void 0,h={authorsImageUrls:[void 0]},m=[{value:"2.5 Billion",id:"25-billion",level:2},{value:"Statistical Considerations",id:"statistical-considerations",level:2},{value:"Getting the data",id:"getting-the-data",level:2},{value:"Registering an application",id:"registering-an-application",level:2},{value:"Handling OAuth 2",id:"handling-oauth-2",level:3},{value:"Requesting the data",id:"requesting-the-data",level:3},{value:"Wild Extrapolations from Small Data",id:"wild-extrapolations-from-small-data",level:2},{value:"How many heartbeats so far?",id:"how-many-heartbeats-so-far",level:3},{value:"How many heartbeats longer?",id:"how-many-heartbeats-longer",level:3},{value:"Summary",id:"summary",level:2}];function c(e){let s={a:"a",annotation:"annotation",code:"code",h2:"h2",h3:"h3",li:"li",math:"math",mi:"mi",mo:"mo",mrow:"mrow",mstyle:"mstyle",msub:"msub",mtable:"mtable",mtd:"mtd",mtr:"mtr",ol:"ol",p:"p",pre:"pre",semantics:"semantics",span:"span",strong:"strong",ul:"ul",...(0,i.a)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(s.p,{children:"If all we have is a finite number of heartbeats left, what about me?"}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsx)(s.h2,{id:"25-billion",children:"2.5 Billion"}),"\n",(0,n.jsxs)(s.p,{children:["If ",(0,n.jsx)(s.a,{href:"http://www.pbs.org/wgbh/nova/heart/heartfacts.html",children:"PBS"})," is right, that's the total number of heartbeats we get. Approximately once every second that number goes down, and down, and down again..."]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"total_heartbeats = 2500000000\n"})}),"\n",(0,n.jsx)(s.p,{children:"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?"}),"\n",(0,n.jsx)(s.h2,{id:"statistical-considerations",children:"Statistical Considerations"}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsxs)(s.ol,{children:["\n",(0,n.jsxs)(s.li,{children:["The concept of 2.5 billion as an agreed-upon number is tenuous at best. I've seen anywhere from ",(0,n.jsx)(s.a,{href:"http://gizmodo.com/5982977/how-many-heartbeats-does-each-species-get-in-a-lifetime",children:"2.21 billion"})," to ",(0,n.jsx)(s.a,{href:"http://wonderopolis.org/wonder/how-many-times-does-your-heart-beat-in-a-lifetime/",children:"3.4 billion"})," 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."]}),"\n",(0,n.jsx)(s.li,{children:"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."}),"\n",(0,n.jsx)(s.li,{children:"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."}),"\n"]}),"\n",(0,n.jsxs)(s.p,{children:["So while the ending number is ",(0,n.jsx)(s.strong,{children:"not useful in any medical context"}),", it is still an interesting project to work with the data I have on hand."]}),"\n",(0,n.jsx)(s.h2,{id:"getting-the-data",children:"Getting the data"}),"\n",(0,n.jsxs)(s.p,{children:[(0,n.jsx)(s.a,{href:"https://www.fitbit.com/",children:"Fitbit"})," has an ",(0,n.jsx)(s.a,{href:"https://dev.fitbit.com/",children:"API available"})," for people to pull their personal data off the system. It requires registering an application, authentication with OAuth, and some other complicated things. ",(0,n.jsxs)(s.strong,{children:["If you're not interested in how I fetch the data, skip ",(0,n.jsx)(s.a,{href:"#Wild-Extrapolations-from-Small-Data",children:"here"})]}),"."]}),"\n",(0,n.jsx)(s.h2,{id:"registering-an-application",children:"Registering an application"}),"\n",(0,n.jsxs)(s.p,{children:["I've already ",(0,n.jsx)(s.a,{href:"https://dev.fitbit.com/apps/new",children:"registered a personal application"})," with Fitbit, so I can go ahead and retrieve things like the client secret from a file."]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"# Import all the OAuth secret information from a local file\nfrom secrets import CLIENT_SECRET, CLIENT_ID, CALLBACK_URL\n"})}),"\n",(0,n.jsx)(s.h3,{id:"handling-oauth-2",children:"Handling OAuth 2"}),"\n",(0,n.jsx)(s.p,{children:"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:"}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:"Application: I've got a user here who wants to use my application, but I need their data.\nFitbit: OK, what data do you need access to, and for how long?\nApplication: I need all of these scopes, and for this amount of time.\nFitbit: OK, let me check with the user to make sure they really want to do this.\n\nFitbit: User, do you really want to let this application have your data?\nUser: I do! And to prove it, here's my password.\nFitbit: OK, everything checks out. I'll let the application access your data.\n\nFitbit: Application, you can access the user's data. Use this special value whenever you need to request data from me.\nApplication: Thank you, now give me all the data.\n"})}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsxs)(s.p,{children:["It does make handling small requests a bit challenging, but I'll go through the steps here. We'll be using the ",(0,n.jsx)(s.a,{href:"https://dev.fitbit.com/docs/oauth2/",children:"Implicit Grant"})," workflow, as it requires fewer steps in processing."]}),"\n",(0,n.jsx)(s.p,{children:"First, we need to set up the URL the user would visit to authenticate:"}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"import urllib\n\nFITBIT_URI = 'https://www.fitbit.com/oauth2/authorize'\nparams = {\n # If we need more than one scope, must be a CSV string\n 'scope': 'heartrate',\n 'response_type': 'token',\n 'expires_in': 86400, # 1 day\n 'redirect_uri': CALLBACK_URL,\n 'client_id': CLIENT_ID\n}\n\nrequest_url = FITBIT_URI + '?' + urllib.parse.urlencode(params)\n"})}),"\n",(0,n.jsxs)(s.p,{children:["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 ",(0,n.jsx)(s.code,{children:"CLIENT_ID"})," 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."]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"# The `response_url` variable contains the full URL that\n# FitBit sent back to us, but most importantly,\n# contains the token we need for authorization.\naccess_token = dict(urllib.parse.parse_qsl(response_url))['access_token']\n"})}),"\n",(0,n.jsx)(s.h3,{id:"requesting-the-data",children:"Requesting the data"}),"\n",(0,n.jsxs)(s.p,{children:["Now that we've actually set up our access via the ",(0,n.jsx)(s.code,{children:"access_token"}),", it's time to get the actual ",(0,n.jsx)(s.a,{href:"https://dev.fitbit.com/docs/heart-rate/",children:"heart rate data"}),". I'll be using data from January 1, 2016 through March 31, 2016, and extrapolating wildly from that."]}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"from requests_oauthlib import OAuth2Session\nimport pandas as pd\nfrom datetime import datetime\n\nsession = OAuth2Session(token={\n 'access_token': access_token,\n 'token_type': 'Bearer'\n })\n\nformat_str = '%Y-%m-%d'\nstart_date = datetime(2016, 1, 1)\nend_date = datetime(2016, 3, 31)\ndr = pd.date_range(start_date, end_date)\n\nurl = 'https://api.fitbit.com/1/user/-/activities/heart/date/{0}/1d/1min.json'\nhr_responses = [session.get(url.format(d.strftime(format_str))) for d in dr]\n\ndef record_to_df(record):\n if 'activities-heart' not in record:\n return None\n date_str = record['activities-heart'][0]['dateTime']\n df = pd.DataFrame(record['activities-heart-intraday']['dataset'])\n \n df.index = df['time'].apply(\n lambda x: datetime.strptime(date_str + ' ' + x, '%Y-%m-%d %H:%M:%S'))\n return df\n\nhr_dataframes = [record_to_df(record.json()) for record in hr_responses]\nhr_df_concat = pd.concat(hr_dataframes)\n\n\n# There are some minutes with missing data, so we need to correct that\nfull_daterange = pd.date_range(hr_df_concat.index[0],\n hr_df_concat.index[-1],\n freq='min')\nhr_df_full = hr_df_concat.reindex(full_daterange, method='nearest')\n\nprint(\"Heartbeats from {} to {}: {}\".format(hr_df_full.index[0],\n hr_df_full.index[-1],\n hr_df_full['value'].sum()))\n"})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" Heartbeats from 2016-01-01 00:00:00 to 2016-03-31 23:59:00: 8139060\n"})}),"\n",(0,n.jsxs)(s.p,{children:["And now we've retrieved all the available heart rate data for January 1",(0,n.jsx)("sup",{children:"st"})," through March 31",(0,n.jsx)("sup",{children:"st"}),"! Let's get to the actual analysis."]}),"\n",(0,n.jsx)(s.h2,{id:"wild-extrapolations-from-small-data",children:"Wild Extrapolations from Small Data"}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsx)(s.h3,{id:"how-many-heartbeats-so-far",children:"How many heartbeats so far?"}),"\n",(0,n.jsxs)(s.p,{children:["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",(0,n.jsx)("sup",{children:"st"}),", 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."]}),"\n",(0,n.jsx)(s.p,{children:"Between January 1992 and January 2016 there were 96 quarters, and 6 leap days. The number we're looking for is:"}),"\n",(0,n.jsx)(s.span,{className:"katex-display",children:(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mtable,{rowspacing:"0.16em",columnspacing:"1em",children:(0,n.jsx)(s.mtr,{children:(0,n.jsx)(s.mtd,{children:(0,n.jsx)(s.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"q"})]}),(0,n.jsx)(s.mo,{children:"\u22C5"}),(0,n.jsx)(s.mi,{children:"n"}),(0,n.jsx)(s.mo,{children:"\u2212"}),(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"d"})]}),(0,n.jsx)(s.mo,{children:"\u22C5"}),(0,n.jsx)(s.mo,{stretchy:"false",children:"("}),(0,n.jsx)(s.mi,{children:"n"}),(0,n.jsx)(s.mo,{children:"\u2212"}),(0,n.jsx)(s.mi,{children:"m"}),(0,n.jsx)(s.mo,{stretchy:"false",children:")"})]})})})})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\begin{equation*}\nhr_q \\cdot n - hr_d \\cdot (n-m)\n\\end{equation*}"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"1.2em",verticalAlign:"-0.35em"}}),(0,n.jsx)(s.span,{className:"mord",children:(0,n.jsx)(s.span,{className:"mtable",children:(0,n.jsx)(s.span,{className:"col-align-c",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.85em"},children:(0,n.jsxs)(s.span,{style:{top:"-3.01em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"3em"}}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.1514em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",style:{marginRight:"0.03588em"},children:"q"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.2861em"},children:(0,n.jsx)(s.span,{})})})]})})]}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u22C5"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u2212"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.3361em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",children:"d"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.15em"},children:(0,n.jsx)(s.span,{})})})]})})]}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u22C5"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mopen",children:"("}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u2212"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,n.jsx)(s.span,{className:"mclose",children:")"})]})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.35em"},children:(0,n.jsx)(s.span,{})})})]})})})})]})})]})}),"\n",(0,n.jsxs)(s.ul,{children:["\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"q"})]})]}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"hr_q"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.9805em",verticalAlign:"-0.2861em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.1514em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",style:{marginRight:"0.03588em"},children:"q"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.2861em"},children:(0,n.jsx)(s.span,{})})})]})})]})]})})]}),": Number of heartbeats per quarter"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"d"})]})]}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"hr_d"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.8444em",verticalAlign:"-0.15em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.3361em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",children:"d"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.15em"},children:(0,n.jsx)(s.span,{})})})]})})]})]})})]}),": Number of heartbeats on leap day"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mrow,{children:(0,n.jsx)(s.mi,{children:"n"})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"n"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]}),": Number of quarters, in this case 96"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mrow,{children:(0,n.jsx)(s.mi,{children:"m"})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"m"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"m"})]})})]}),": Number of leap days, in this case 6"]}),"\n"]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"quarterly_count = hr_df_full['value'].sum()\nleap_day_count = hr_df_full[(hr_df_full.index.month == 2) &\n (hr_df_full.index.day == 29)]['value'].sum()\nnum_quarters = 96\nleap_days = 6\n\njan_92_jan_16 = quarterly_count * num_quarters - leap_day_count * (num_quarters - leap_days)\njan_92_jan_16\n"})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" 773609400\n"})}),"\n",(0,n.jsxs)(s.p,{children:["So between January 1992 and January 2016 I've used ",(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mrow,{children:(0,n.jsx)(s.mo,{children:"\u2248"})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\approx"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.4831em"}}),(0,n.jsx)(s.span,{className:"mrel",children:"\u2248"})]})})]})," 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."]}),"\n",(0,n.jsx)(s.p,{children:"For privacy purposes I'll put the code here that I'm using, but without any identifying information:"}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:'minute_mean = hr_df_full[\'value\'].mean()\n# Don\'t you wish you knew?\n# birthday_minutes = ???\n\nbirthday_heartbeats = birthday_minutes * minute_mean\n\nheartbeats_until_2016 = int(birthday_heartbeats + jan_92_jan_16)\nremaining_2016 = total_heartbeats - heartbeats_until_2016\n\nprint("Heartbeats so far: {}".format(heartbeats_until_2016))\nprint("Remaining heartbeats: {}".format(remaining_2016))\n'})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" Heartbeats so far: 775804660\n Remaining heartbeats: 1724195340\n"})}),"\n",(0,n.jsxs)(s.p,{children:["It would appear that my heart has beaten 775,804,660 times between my moment of birth and January 1",(0,n.jsx)("sup",{children:"st"})," 2016, and that I have 1.72 billion left."]}),"\n",(0,n.jsx)(s.h3,{id:"how-many-heartbeats-longer",children:"How many heartbeats longer?"}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsx)(s.p,{children:"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:"}),"\n",(0,n.jsx)(s.span,{className:"katex-display",children:(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mtable,{rowspacing:"0.16em",columnspacing:"1em",children:(0,n.jsx)(s.mtr,{children:(0,n.jsx)(s.mtd,{children:(0,n.jsx)(s.mstyle,{scriptlevel:"0",displaystyle:"true",children:(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"q"})]}),(0,n.jsx)(s.mo,{children:"\u22C5"}),(0,n.jsx)(s.mi,{children:"n"}),(0,n.jsx)(s.mo,{children:"\u2212"}),(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"d"})]}),(0,n.jsx)(s.mo,{children:"\u22C5"}),(0,n.jsx)(s.mo,{stretchy:"false",children:"("}),(0,n.jsx)(s.mi,{children:"n"}),(0,n.jsx)(s.mo,{children:"\u2212"}),(0,n.jsx)(s.mi,{children:"m"}),(0,n.jsx)(s.mo,{stretchy:"false",children:")"})]})})})})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"\\begin{equation*}\nhr_q \\cdot n - hr_d \\cdot (n - m)\n\\end{equation*}"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"1.2em",verticalAlign:"-0.35em"}}),(0,n.jsx)(s.span,{className:"mord",children:(0,n.jsx)(s.span,{className:"mtable",children:(0,n.jsx)(s.span,{className:"col-align-c",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.85em"},children:(0,n.jsxs)(s.span,{style:{top:"-3.01em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"3em"}}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.1514em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",style:{marginRight:"0.03588em"},children:"q"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.2861em"},children:(0,n.jsx)(s.span,{})})})]})})]}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u22C5"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u2212"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.3361em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",children:"d"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.15em"},children:(0,n.jsx)(s.span,{})})})]})})]}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u22C5"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mopen",children:"("}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mbin",children:"\u2212"}),(0,n.jsx)(s.span,{className:"mspace",style:{marginRight:"0.2222em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"m"}),(0,n.jsx)(s.span,{className:"mclose",children:")"})]})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.35em"},children:(0,n.jsx)(s.span,{})})})]})})})})]})})]})}),"\n",(0,n.jsxs)(s.ul,{children:["\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"q"})]})]}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"hr_q"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.9805em",verticalAlign:"-0.2861em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.1514em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",style:{marginRight:"0.03588em"},children:"q"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.2861em"},children:(0,n.jsx)(s.span,{})})})]})})]})]})})]}),": Heartbeats per quarter"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsxs)(s.mrow,{children:[(0,n.jsx)(s.mi,{children:"h"}),(0,n.jsxs)(s.msub,{children:[(0,n.jsx)(s.mi,{children:"r"}),(0,n.jsx)(s.mi,{children:"d"})]})]}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"hr_d"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.8444em",verticalAlign:"-0.15em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"h"}),(0,n.jsxs)(s.span,{className:"mord",children:[(0,n.jsx)(s.span,{className:"mord mathnormal",style:{marginRight:"0.02778em"},children:"r"}),(0,n.jsx)(s.span,{className:"msupsub",children:(0,n.jsxs)(s.span,{className:"vlist-t vlist-t2",children:[(0,n.jsxs)(s.span,{className:"vlist-r",children:[(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.3361em"},children:(0,n.jsxs)(s.span,{style:{top:"-2.55em",marginLeft:"-0.0278em",marginRight:"0.05em"},children:[(0,n.jsx)(s.span,{className:"pstrut",style:{height:"2.7em"}}),(0,n.jsx)(s.span,{className:"sizing reset-size6 size3 mtight",children:(0,n.jsx)(s.span,{className:"mord mathnormal mtight",children:"d"})})]})}),(0,n.jsx)(s.span,{className:"vlist-s",children:"\u200B"})]}),(0,n.jsx)(s.span,{className:"vlist-r",children:(0,n.jsx)(s.span,{className:"vlist",style:{height:"0.15em"},children:(0,n.jsx)(s.span,{})})})]})})]})]})})]}),": Heartbeats per leap day"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mrow,{children:(0,n.jsx)(s.mi,{children:"n"})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"n"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"n"})]})})]}),": Number of quarters = 16"]}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsxs)(s.span,{className:"katex",children:[(0,n.jsx)(s.span,{className:"katex-mathml",children:(0,n.jsx)(s.math,{xmlns:"http://www.w3.org/1998/Math/MathML",children:(0,n.jsxs)(s.semantics,{children:[(0,n.jsx)(s.mrow,{children:(0,n.jsx)(s.mi,{children:"m"})}),(0,n.jsx)(s.annotation,{encoding:"application/x-tex",children:"m"})]})})}),(0,n.jsx)(s.span,{className:"katex-html","aria-hidden":"true",children:(0,n.jsxs)(s.span,{className:"base",children:[(0,n.jsx)(s.span,{className:"strut",style:{height:"0.4306em"}}),(0,n.jsx)(s.span,{className:"mord mathnormal",children:"m"})]})})]}),": Number of leap days = 1"]}),"\n"]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"heartbeats_4year = quarterly_count * 16 - leap_day_count * (16 - 1)\nheartbeats_4year\n"})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" 128934900\n"})}),"\n",(0,n.jsx)(s.p,{children:"Now, I can fast forward from 2016 the number of periods of 4 years I have left."}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:'four_year_periods = remaining_2016 // heartbeats_4year\nremaining_4y = remaining_2016 - four_year_periods * heartbeats_4year\n\nprint("Four year periods remaining: {}".format(four_year_periods))\nprint("Remaining heartbeats after 4 year periods: {}".format(remaining_4y))\n'})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" Four year periods remaining: 13\n Remaining heartbeats after 4 year periods: 48041640\n"})}),"\n",(0,n.jsx)(s.p,{children:"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."}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:'remaining_leap = remaining_4y - leap_day_count\n# Ignore leap day in the data set\nheartbeats_quarter = hr_df_full[(hr_df_full.index.month != 2) &\n (hr_df_full.index.day != 29)][\'value\'].sum()\nquarters_left = remaining_leap // heartbeats_quarter\nremaining_year = remaining_leap - quarters_left * heartbeats_quarter\n\nprint("Quarters left starting 2068: {}".format(quarters_left))\nprint("Remaining heartbeats after that: {}".format(remaining_year))\n'})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" Quarters left starting 2068: 8\n Remaining heartbeats after that: 4760716\n"})}),"\n",(0,n.jsxs)(s.p,{children:["So, that analysis gets me through until January 1",(0,n.jsx)("sup",{children:"st"})," 2070. Final step, using that minute estimate to figure out how many minutes past that I'm predicted to have:"]}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{className:"language-python",children:"from datetime import timedelta\n\nbase = datetime(2070, 1, 1)\nminutes_left = remaining_year // minute_mean\n\nkaput = timedelta(minutes=minutes_left)\nbase + kaput\n"})}),"\n",(0,n.jsx)(s.pre,{children:(0,n.jsx)(s.code,{children:" datetime.datetime(2070, 2, 23, 5, 28)\n"})}),"\n",(0,n.jsxs)(s.p,{children:["According to this, I've got until February 23",(0,n.jsx)("sup",{children:"rd"}),", 2070 at 5:28 PM in the evening before my heart gives out."]}),"\n",(0,n.jsx)(s.h2,{id:"summary",children:"Summary"}),"\n",(0,n.jsxs)(s.p,{children:["Well, that's kind of a creepy date to know. As I said at the top though, ",(0,n.jsx)(s.strong,{children:"this number is totally useless in any medical context"}),". 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."]}),"\n",(0,n.jsxs)(s.p,{children:["Even still, I think philosophically humans have a desire to know how much time we have left in the world. ",(0,n.jsx)(s.a,{href:"https://www.biblegateway.com/passage/?search=psalm+144&version=ESV",children:"Man is but a breath"}),", 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?"]}),"\n",(0,n.jsx)(s.p,{children:"Thanks for sticking with me on this one, I promise it will be much less depressing next time!"})]})}function d(e={}){let{wrapper:s}={...(0,i.a)(),...e.components};return s?(0,n.jsx)(s,{...e,children:(0,n.jsx)(c,{...e})}):c(e)}},65:function(e,s,a){a.d(s,{Z:function(){return l},a:function(){return r}});var t=a(7294);let n={},i=t.createContext(n);function r(e){let s=t.useContext(i);return t.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function l(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(n):e.components||n:r(e.components),t.createElement(i.Provider,{value:s},e.children)}},2208:function(e){e.exports=JSON.parse('{"permalink":"/2016/04/tick-tock","source":"@site/blog/2016-04-06-tick-tock/index.mdx","title":"Tick tock...","description":"If all we have is a finite number of heartbeats left, what about me?","date":"2016-04-06T12:00:00.000Z","tags":[],"readingTime":10.125,"hasTruncateMarker":true,"authors":[{"name":"Bradlee Speice","socials":{"github":"https://github.com/bspeice"},"key":"bspeice","page":null}],"frontMatter":{"slug":"2016/04/tick-tock","title":"Tick tock...","date":"2016-04-06T12:00:00.000Z","authors":["bspeice"],"tags":[]},"unlisted":false,"lastUpdatedAt":1730863976000,"prevItem":{"title":"The unfair casino","permalink":"/2016/05/the-unfair-casino"},"nextItem":{"title":"Tweet like me","permalink":"/2016/03/tweet-like-me"}}')}}]);