Sleep No More - Caffeine

post @ endlesspint.com


In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import datetime

plt.style.use('ggplot')
%matplotlib inline

In [2]:
half_life = 5.0
half_hour_decay = np.exp(-np.log(2) * (0.5 / half_life))

half_hour_decay


Out[2]:
0.9330329915368074

In [3]:
# toy sample

t = np.zeros(10)
c = np.array([0, 0, 0, 30, 0, 0, 10, 0, 0, 0])

for i in range(2, len(t)):
    t[i] += t[i-1] * half_hour_decay + c[i-1]
    
plt.plot(t)


Out[3]:
[<matplotlib.lines.Line2D at 0x140247cc358>]

In [4]:
intake = pd.read_excel("11day_intake.xlsx")
intake.head(10)


Out[4]:
Day Time Substance Amount (g) Caffeine (mg)
0 1 09:30:00 coffee 220 88
1 1 11:00:00 coffee 220 88
2 2 09:00:00 espresso 80 170
3 2 10:00:00 espresso 80 170
4 2 12:30:00 dark choc (60-85%) 35 14
5 3 09:00:00 coffee 220 88
6 3 10:00:00 coffee 110 44
7 3 16:30:00 dark choc (60-85%) 35 14
8 4 08:30:00 coffee 220 88
9 5 08:30:00 espresso 40 85

In [5]:
intake_ts_day = pd.pivot_table(intake, values="Caffeine (mg)", index="Time", columns="Day").fillna(0)
intake_ts_day


Out[5]:
Day 1 2 3 4 5 6 7 8 9 10 11
Time
08:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 88.0 88.0
08:30:00 0.0 0.0 0.0 88.0 85.0 0.0 0.0 0.0 88.0 0.0 0.0
09:00:00 0.0 170.0 88.0 0.0 0.0 0.0 0.0 0.0 0.0 88.0 0.0
09:30:00 88.0 0.0 0.0 0.0 85.0 0.0 170.0 0.0 0.0 0.0 170.0
10:00:00 0.0 170.0 44.0 0.0 0.0 0.0 0.0 88.0 88.0 0.0 0.0
10:30:00 0.0 0.0 0.0 0.0 0.0 170.0 170.0 0.0 0.0 170.0 0.0
11:00:00 88.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 64.0
11:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 24.0
12:30:00 0.0 14.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
13:30:00 0.0 0.0 0.0 0.0 85.0 0.0 0.0 0.0 0.0 0.0 0.0
14:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 51.0 0.0 170.0 0.0
15:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 12.0 0.0
16:30:00 0.0 0.0 14.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
19:00:00 0.0 0.0 0.0 0.0 0.0 14.0 0.0 0.0 0.0 0.0 0.0
19:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 12.0 0.0 0.0

In [6]:
intake_ts_day[2][datetime.time(9,0)]


Out[6]:
170.0

In [7]:
# dummy Y/M/D
six_am = datetime.datetime(2019, 1, 1, 6, 0) 

time_idx = []

for i in range(48):
    step = 30 * i
    time_idx.append((six_am + datetime.timedelta(minutes = step)).time())
    
print(time_idx[:10])

df_time_idx = pd.DataFrame(np.zeros(len(time_idx)), index=time_idx)
df_time_idx.head()


[datetime.time(6, 0), datetime.time(6, 30), datetime.time(7, 0), datetime.time(7, 30), datetime.time(8, 0), datetime.time(8, 30), datetime.time(9, 0), datetime.time(9, 30), datetime.time(10, 0), datetime.time(10, 30)]
Out[7]:
0
06:00:00 0.0
06:30:00 0.0
07:00:00 0.0
07:30:00 0.0
08:00:00 0.0

In [8]:
df = pd.concat([intake_ts_day, df_time_idx], axis=1, sort=True).fillna(0)
df_intake_ts_day = df.loc[time_idx, :11]
df_intake_ts_day.columns = ['day_' + str(i) for i in df_intake_ts_day.columns]
df_intake_ts_day.head(20)


Out[8]:
day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11
06:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
06:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
07:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
07:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
08:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 88.0 88.0
08:30:00 0.0 0.0 0.0 88.0 85.0 0.0 0.0 0.0 88.0 0.0 0.0
09:00:00 0.0 170.0 88.0 0.0 0.0 0.0 0.0 0.0 0.0 88.0 0.0
09:30:00 88.0 0.0 0.0 0.0 85.0 0.0 170.0 0.0 0.0 0.0 170.0
10:00:00 0.0 170.0 44.0 0.0 0.0 0.0 0.0 88.0 88.0 0.0 0.0
10:30:00 0.0 0.0 0.0 0.0 0.0 170.0 170.0 0.0 0.0 170.0 0.0
11:00:00 88.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 64.0
11:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 24.0
12:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
12:30:00 0.0 14.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
13:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
13:30:00 0.0 0.0 0.0 0.0 85.0 0.0 0.0 0.0 0.0 0.0 0.0
14:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
14:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 51.0 0.0 170.0 0.0
15:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 12.0 0.0
15:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

In [9]:
caff_down = {}

for col in df_intake_ts_day.columns:
    dayy = np.zeros(len(time_idx))
    caff = df_intake_ts_day[col].values
    
    for i in range(2, len(dayy)):
        dayy[i] += dayy[i-1] * half_hour_decay + caff[i-1]
        
    caff_down[col] = dayy

df_caff_down = pd.DataFrame(caff_down, index=time_idx)
df_caff_down


Out[9]:
day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11
06:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
06:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
07:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
07:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
08:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
08:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 88.000000 88.000000
09:00:00 0.000000 0.000000 0.000000 88.000000 85.000000 0.000000 0.000000 0.000000 88.000000 82.106903 82.106903
09:30:00 0.000000 170.000000 88.000000 82.106903 79.307804 0.000000 0.000000 0.000000 82.106903 164.608450 76.608450
10:00:00 88.000000 158.615609 82.106903 76.608450 158.996798 0.000000 170.000000 0.000000 76.608450 153.585114 241.478211
10:30:00 82.106903 317.993596 120.608450 71.478211 148.349258 0.000000 158.615609 88.000000 159.478211 143.299978 225.307137
11:00:00 76.608450 296.698516 112.531663 66.691529 138.414752 170.000000 317.993596 82.106903 148.798432 303.703608 210.218993
11:30:00 159.478211 276.829504 104.995754 62.225397 129.145530 158.615609 296.698516 76.608450 138.833846 283.365486 260.141255
12:00:00 148.798432 258.291060 97.964502 58.058348 120.497040 147.993596 276.829504 71.478211 129.536559 264.389347 266.720374
12:30:00 138.833846 240.994081 91.404113 54.170354 112.427714 138.082907 258.291060 66.691529 120.861883 246.683983 248.858908
13:00:00 129.536559 238.855428 85.283053 50.542728 104.898766 128.835908 240.994081 62.225397 112.768124 230.164295 232.193572
13:30:00 120.861883 222.859994 79.571902 47.158032 97.874010 120.208153 224.855428 58.058348 105.216380 214.750880 216.644263
14:00:00 112.768124 207.935727 74.243209 44.000000 176.319680 112.158172 209.797533 54.170354 98.170354 200.369656 202.136245
14:30:00 105.216380 194.010894 69.271364 41.053452 164.512079 104.647275 195.748019 50.542728 91.596179 186.951500 188.599785
15:00:00 98.170354 181.018565 64.632468 38.304225 153.495197 97.639360 182.639360 98.158032 85.462257 344.431917 175.969822
15:30:00 91.596179 168.896293 60.304225 35.739105 143.216083 91.100744 170.408549 91.584683 79.739105 333.366342 164.185649
16:00:00 85.462257 157.585813 56.265831 33.345764 133.625330 85.000000 158.996798 85.451530 74.399216 311.041795 153.190627
16:30:00 79.739105 147.032763 52.497877 31.112698 124.676841 79.307804 148.349258 79.729097 69.416923 290.212257 142.931909
17:00:00 74.399216 137.186419 62.982251 29.029174 116.327606 73.996798 138.414752 74.389878 64.768279 270.777610 133.360187
17:30:00 69.416923 127.999455 58.764518 27.085177 108.537495 69.041454 129.145530 69.408210 60.430942 252.644444 124.429454
18:00:00 64.768279 119.427714 54.829234 25.271364 101.269063 64.417954 120.497040 64.760150 56.384062 235.725601 116.096786
18:30:00 60.430942 111.429997 51.157484 23.579016 94.487377 60.104076 112.427714 60.423357 52.608190 219.939763 108.322131
19:00:00 56.384062 103.967864 47.731621 22.000000 88.159840 56.079086 104.898766 56.376985 49.085177 205.211055 101.068122
19:30:00 52.608190 97.005447 44.535177 20.526726 82.256039 66.323638 97.874010 52.601587 45.798090 191.468684 94.299892
20:00:00 49.085177 90.509282 41.552789 19.152112 76.747598 61.882142 91.319680 49.079016 54.731129 178.646599 87.984911
20:30:00 45.798090 84.448146 38.770123 17.869553 71.608041 57.738080 85.204274 45.792341 51.065949 166.683171 82.092825
21:00:00 42.731129 78.792907 36.173804 16.672882 66.812665 53.871534 79.498399 42.725765 47.646215 155.520898 76.595314
21:30:00 39.869553 73.516381 33.751353 15.556349 62.338421 50.263918 74.174629 39.864548 44.455490 145.106128 71.465955
22:00:00 37.199608 68.593209 31.491126 14.514587 58.163803 46.897894 69.207376 37.194939 41.478439 135.388805 66.680093
22:30:00 34.708462 63.999727 29.382259 13.542589 54.268747 43.757282 64.572765 34.704105 38.700752 126.322222 62.214727
23:00:00 32.384140 59.713857 27.414617 12.635682 50.634532 40.826988 60.248520 32.380075 36.109079 117.862801 58.048393
23:30:00 30.215471 55.714999 25.578742 11.789508 47.243689 38.092927 56.213857 30.211678 33.690962 109.969881 54.161066
00:00:00 28.192031 51.983932 23.865810 11.000000 44.079920 35.541957 52.449383 28.188493 31.434779 102.605527 50.534061
00:30:00 26.304095 48.502723 22.267588 10.263363 41.128020 33.161819 48.937005 26.300794 29.329686 95.734342 47.149946
01:00:00 24.542589 45.254641 20.776395 9.576056 38.373799 30.941071 45.659840 24.539508 27.365564 89.323300 43.992455
01:30:00 22.899045 42.224073 19.385062 8.934776 35.804021 28.869040 42.602137 22.896171 25.532974 83.341586 41.046412
02:00:00 21.365564 39.396453 18.086902 8.336441 33.406333 26.935767 39.749199 21.362883 23.823107 77.760449 38.297657
02:30:00 19.934776 36.758191 16.875676 7.778175 31.169210 25.131959 37.087314 19.932274 22.227745 72.553064 35.732977
03:00:00 18.599804 34.296605 15.745563 7.257294 29.081902 23.448947 34.603688 18.597469 20.739220 67.694403 33.340047
03:30:00 17.354231 31.999864 14.691130 6.771294 27.134374 21.878641 32.286383 17.352053 19.350376 63.161111 31.107364
04:00:00 16.192070 29.856928 13.707309 6.317841 25.317266 20.413494 30.124260 16.190038 18.054539 58.931400 29.024196
04:30:00 15.107735 27.857499 12.789371 5.894754 23.621844 19.046463 28.106928 15.105839 16.845481 54.984941 27.080533
05:00:00 14.096016 25.991966 11.932905 5.500000 22.039960 17.770979 26.224692 14.094246 15.717389 51.302764 25.267031
05:30:00 13.152048 24.251362 11.133794 5.131681 20.564010 16.580909 24.468502 13.150397 14.664843 47.867171 23.574973

In [10]:
df_caff_down['day_split'] = 'A - '
df_caff_down.loc[time_idx[-12:], 'day_split'] = 'B - '
df_caff_down['day_time'] = [str(i)[:5] for i in df_caff_down.index]
df_caff_down['day_time_split'] =df_caff_down.day_split + df_caff_down.day_time
df_caff_down.tail(15)


Out[10]:
day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11 day_split day_time day_time_split
22:30:00 34.708462 63.999727 29.382259 13.542589 54.268747 43.757282 64.572765 34.704105 38.700752 126.322222 62.214727 A - 22:30 A - 22:30
23:00:00 32.384140 59.713857 27.414617 12.635682 50.634532 40.826988 60.248520 32.380075 36.109079 117.862801 58.048393 A - 23:00 A - 23:00
23:30:00 30.215471 55.714999 25.578742 11.789508 47.243689 38.092927 56.213857 30.211678 33.690962 109.969881 54.161066 A - 23:30 A - 23:30
00:00:00 28.192031 51.983932 23.865810 11.000000 44.079920 35.541957 52.449383 28.188493 31.434779 102.605527 50.534061 B - 00:00 B - 00:00
00:30:00 26.304095 48.502723 22.267588 10.263363 41.128020 33.161819 48.937005 26.300794 29.329686 95.734342 47.149946 B - 00:30 B - 00:30
01:00:00 24.542589 45.254641 20.776395 9.576056 38.373799 30.941071 45.659840 24.539508 27.365564 89.323300 43.992455 B - 01:00 B - 01:00
01:30:00 22.899045 42.224073 19.385062 8.934776 35.804021 28.869040 42.602137 22.896171 25.532974 83.341586 41.046412 B - 01:30 B - 01:30
02:00:00 21.365564 39.396453 18.086902 8.336441 33.406333 26.935767 39.749199 21.362883 23.823107 77.760449 38.297657 B - 02:00 B - 02:00
02:30:00 19.934776 36.758191 16.875676 7.778175 31.169210 25.131959 37.087314 19.932274 22.227745 72.553064 35.732977 B - 02:30 B - 02:30
03:00:00 18.599804 34.296605 15.745563 7.257294 29.081902 23.448947 34.603688 18.597469 20.739220 67.694403 33.340047 B - 03:00 B - 03:00
03:30:00 17.354231 31.999864 14.691130 6.771294 27.134374 21.878641 32.286383 17.352053 19.350376 63.161111 31.107364 B - 03:30 B - 03:30
04:00:00 16.192070 29.856928 13.707309 6.317841 25.317266 20.413494 30.124260 16.190038 18.054539 58.931400 29.024196 B - 04:00 B - 04:00
04:30:00 15.107735 27.857499 12.789371 5.894754 23.621844 19.046463 28.106928 15.105839 16.845481 54.984941 27.080533 B - 04:30 B - 04:30
05:00:00 14.096016 25.991966 11.932905 5.500000 22.039960 17.770979 26.224692 14.094246 15.717389 51.302764 25.267031 B - 05:00 B - 05:00
05:30:00 13.152048 24.251362 11.133794 5.131681 20.564010 16.580909 24.468502 13.150397 14.664843 47.867171 23.574973 B - 05:30 B - 05:30

different attempts at plotting with time on x-axis


In [11]:
df_caff_down2 = df_caff_down.reset_index().set_index('day_time_split')
df_caff_down2.head()


Out[11]:
index day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11 day_split day_time
day_time_split
A - 06:00 06:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 A - 06:00
A - 06:30 06:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 A - 06:30
A - 07:00 07:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 A - 07:00
A - 07:30 07:30:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 A - 07:30
A - 08:00 08:00:00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 A - 08:00

In [12]:
df_caff_down2[df_caff_down2.columns[1:-2]].plot(figsize=(18,10))


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x14024991c18>

In [13]:
df_caff_down.reset_index()[df_caff_down.columns[0:11]].plot(figsize=(18,10))


Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x14024e89160>

In [14]:
plt.figure(figsize=(18, 10))
plt.xticks(rotation='vertical')

days = ['day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6', 'day_7', 'day_8',
       'day_9', 'day_10', 'day_11']

for col in days:

    plt.plot( 'day_time_split', col, data=df_caff_down)

# plt.axvline(x='A - 22:00')
# plt.axhline(y=50)
plt.legend()

plt.title("Caffeine Half-Life", size=20)
plt.savefig("caff_hl.jpg")



In [15]:
df_caff_down['day_q25'] = df_caff_down.quantile(q=0.25, axis=1)
df_caff_down['day_q50'] = df_caff_down.quantile(q=0.50, axis=1)
df_caff_down['day_q75'] = df_caff_down.quantile(q=0.75, axis=1)
df_caff_down.head(15)


Out[15]:
day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11 day_split day_time day_time_split day_q25 day_q50 day_q75
06:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 A - 06:00 A - 06:00 0.000000 0.000000 0.000000
06:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 A - 06:30 A - 06:30 0.000000 0.000000 0.000000
07:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 A - 07:00 A - 07:00 0.000000 0.000000 0.000000
07:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 A - 07:30 A - 07:30 0.000000 0.000000 0.000000
08:00:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 A - 08:00 A - 08:00 0.000000 0.000000 0.000000
08:30:00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 88.000000 88.000000 A - 08:30 A - 08:30 0.000000 0.000000 0.000000
09:00:00 0.000000 0.000000 0.000000 88.000000 85.000000 0.000000 0.000000 0.000000 88.000000 82.106903 82.106903 A - 09:00 A - 09:00 0.000000 0.000000 82.106903
09:30:00 0.000000 170.000000 88.000000 82.106903 79.307804 0.000000 0.000000 0.000000 82.106903 164.608450 76.608450 A - 09:30 A - 09:30 0.000000 77.958127 82.106903
10:00:00 88.000000 158.615609 82.106903 76.608450 158.996798 0.000000 170.000000 0.000000 76.608450 153.585114 241.478211 A - 10:00 A - 10:00 76.608450 85.053452 158.615609
10:30:00 82.106903 317.993596 120.608450 71.478211 148.349258 0.000000 158.615609 88.000000 159.478211 143.299978 225.307137 A - 10:30 A - 10:30 85.053452 131.954214 158.615609
11:00:00 76.608450 296.698516 112.531663 66.691529 138.414752 170.000000 317.993596 82.106903 148.798432 303.703608 210.218993 A - 11:00 A - 11:00 97.319283 143.606592 210.218993
11:30:00 159.478211 276.829504 104.995754 62.225397 129.145530 158.615609 296.698516 76.608450 138.833846 283.365486 260.141255 A - 11:30 A - 11:30 117.070642 148.724727 260.141255
12:00:00 148.798432 258.291060 97.964502 58.058348 120.497040 147.993596 276.829504 71.478211 129.536559 264.389347 266.720374 A - 12:00 A - 12:00 109.230771 138.765077 258.291060
12:30:00 138.833846 240.994081 91.404113 54.170354 112.427714 138.082907 258.291060 66.691529 120.861883 246.683983 248.858908 A - 12:30 A - 12:30 101.915913 129.472395 240.994081
13:00:00 129.536559 238.855428 85.283053 50.542728 104.898766 128.835908 240.994081 62.225397 112.768124 230.164295 232.193572 A - 13:00 A - 13:00 95.090909 120.802016 230.164295

In [16]:
plt.figure(figsize=(18, 10))
plt.xticks(rotation='vertical')

for col in zip(['day_q25', 'day_q50', 'day_q75'],['skyblue', 'darkblue', 'skyblue']):

    plt.plot( 'day_time_split', col[0], data=df_caff_down, color=col[1], linewidth=4)

# plt.axvline(x='A - 22:00')
# plt.axhline(y=50)

plt.title("Caffeine Half-Life, IQR", size=20)
plt.savefig("caff_hl_iqr.jpg")
# plt.legend()



In [17]:
intake_ts_day.sum().describe()


Out[17]:
count     11.000000
mean     249.454545
std      129.805519
min       88.000000
25%      161.000000
50%      188.000000
75%      343.000000
max      528.000000
dtype: float64

In [18]:
intake_ts_day.sum().hist()
plt.title("Hist")

plt.show()



In [19]:
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import math

mu = intake_ts_day.sum().describe()['mean']
sigma = intake_ts_day.sum().describe()['std']
x = np.linspace(mu - 2*sigma, mu + 2*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))

plt.title("Norm")
plt.show()



In [20]:
from scipy.stats import poisson

mu = intake_ts_day.sum().describe()['mean']
x_p = np.arange(poisson.ppf(0.01, mu),
                poisson.ppf(0.99, mu))
plt.plot(x_p, poisson.pmf(x_p, mu), 'bo', ms=8, label='poisson pmf')
plt.title("Poisson")

plt.show()



In [21]:
# summary charts, of a sort

f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18,6))


ax1.hist(intake_ts_day.sum())
ax1.set_title("Hist")

ax2.plot(x, stats.norm.pdf(x, mu, sigma))
ax2.set_title("Norm")

ax3.plot(x_p, poisson.pmf(x_p, mu), 'bo', ms=8, label='poisson pmf')
ax3.set_title("Poisson")

plt.tight_layout()
plt.savefig("caff_intake_summaries.jpg")


the caffeine comedown when extending half-life to 7 hours


In [22]:
half_life = 7.0
half_hour_decay = np.exp(-np.log(2) * (0.5 / half_life))

half_hour_decay


Out[22]:
0.9516951530106196

In [23]:
caff_down = {}

for col in df_intake_ts_day.columns:
    dayy = np.zeros(len(time_idx))
    caff = df_intake_ts_day[col].values
    
    for i in range(2, len(dayy)):
        dayy[i] += dayy[i-1] * half_hour_decay + caff[i-1]
        
    caff_down[col] = dayy

df_caff_down7 = pd.DataFrame(caff_down, index=time_idx)


df_caff_down7['day_split'] = 'A - '
df_caff_down7.loc[time_idx[-12:], 'day_split'] = 'B - '
df_caff_down7['day_time'] = [str(i)[:5] for i in df_caff_down7.index]
df_caff_down7['day_time_split'] =df_caff_down7.day_split + df_caff_down7.day_time


df_caff_down7['day_q25'] = df_caff_down7.quantile(q=0.25, axis=1)
df_caff_down7['day_q50'] = df_caff_down7.quantile(q=0.50, axis=1)
df_caff_down7['day_q75'] = df_caff_down7.quantile(q=0.75, axis=1)
df_caff_down7.tail(15)


Out[23]:
day_1 day_2 day_3 day_4 day_5 day_6 day_7 day_8 day_9 day_10 day_11 day_split day_time day_time_split day_q25 day_q50 day_q75
22:30:00 55.132651 104.196849 45.820025 23.116646 83.615173 64.839644 103.743183 51.086527 59.303457 187.621742 101.325183 A - 22:30 A - 22:30 53.109589 62.071550 101.325183
23:00:00 52.469476 99.163636 43.606696 22.000000 79.576155 61.707575 98.731885 48.618800 56.438812 178.558702 96.430686 A - 23:00 A - 23:00 50.544138 59.073194 96.430686
23:30:00 49.934946 94.373552 41.500281 20.937293 75.732241 58.726800 93.962656 46.270276 53.712544 169.933451 91.772616 A - 23:30 A - 23:30 48.102611 56.219672 91.772616
00:00:00 47.522846 89.814852 39.495616 19.925921 72.074007 55.890011 89.423804 44.035198 51.117968 161.724842 87.339554 B - 00:00 B - 00:00 45.779022 53.503989 87.339554
00:30:00 45.227263 85.476359 37.587787 18.963402 68.592483 53.190253 85.104201 41.908084 48.648722 153.912748 83.120630 B - 00:30 B - 00:30 43.567673 50.919487 83.120630
01:00:00 43.042567 81.347437 35.772114 18.047378 65.279133 50.620906 80.993256 39.883721 46.298753 146.478016 79.105501 B - 01:00 B - 01:00 41.463144 48.459829 79.105501
01:30:00 40.963402 77.417961 34.044148 17.175602 62.125835 48.175671 77.080889 37.957144 44.062299 139.402418 75.284322 B - 01:30 B - 01:30 39.460273 46.118985 75.284322
02:00:00 38.984671 73.678298 32.399651 16.345937 59.124856 45.848552 73.357508 36.123630 41.933876 132.668606 71.647724 B - 02:00 B - 02:00 37.554150 43.891214 71.647724
02:30:00 37.101523 70.119280 30.834590 15.556349 56.268839 43.633845 69.813985 34.378683 39.908267 126.260069 68.186792 B - 02:30 B - 02:30 35.740103 41.771056 68.186792
03:00:00 35.309339 66.732178 29.345130 14.804902 53.550781 41.526119 66.441631 32.718026 37.980504 120.161096 64.893039 B - 03:00 B - 03:00 34.013683 39.753311 64.893039
03:30:00 33.603727 63.508691 27.927618 14.089754 50.964019 39.520206 63.232178 31.137587 36.145862 114.356732 61.758391 B - 03:30 B - 03:30 32.370657 37.833034 61.758391
04:00:00 31.980504 60.440913 26.578579 13.409150 48.502210 37.611188 60.177758 29.633490 34.399841 108.832748 58.775161 B - 04:00 B - 04:00 30.806997 36.005515 58.775161
04:30:00 30.435691 57.521324 25.294705 12.761423 46.159318 35.794386 57.270880 28.202049 32.738162 103.575599 55.936036 B - 04:30 B - 04:30 29.318870 34.266274 55.936036
05:00:00 28.965499 54.742765 24.072848 12.144985 43.929599 34.065343 54.504419 26.839754 31.156750 98.572395 53.234055 B - 05:00 B - 05:00 27.902626 32.611047 53.234055
05:30:00 27.566325 52.098424 22.910013 11.558323 41.807586 32.419822 51.871592 25.543263 29.651728 93.810871 50.662592 B - 05:30 B - 05:30 26.554794 31.035775 50.662592

In [41]:
plt.figure(figsize=(18, 10))
plt.xticks(rotation='vertical')

plt.plot( 'day_time_split', 'day_q50', data=df_caff_down, color='skyblue', linewidth=4, label='5hr HL')
plt.plot( 'day_time_split', 'day_q50', data=df_caff_down7, color='deepskyblue', linewidth=4, label='7hr HL')

plt.title("Median Caffeine Intake, 5 v 7 hrs Half-Life", size=20)

plt.legend()
plt.savefig("caff_diff_hl.jpg")
plt.show()


self v others


In [42]:
mu = intake_ts_day.sum().describe()['mean']
sigma = intake_ts_day.sum().describe()['std']
x = np.linspace(mu - 2*sigma, mu + 5*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma), label="ep8")

mu_jcsm = 319.0
sigma_jcsm = 181.0

plt.plot(x, stats.norm.pdf(x, mu_jcsm, sigma_jcsm), label="JCSM")

plt.title("Comp")

plt.legend()
plt.savefig("caff_comp.jpg")
plt.show()



In [ ]: