### Local DynamoDB

I’m working on a side project that uses DynamoDB. I got tired of waiting for my DynamoDB table changes to complete, so I decided to try…

Note: I used KaTeX to render the equations here. There are a couple issues with the CSS that I haven’t had the time to work through, so the equations appear a bit messed up.

In a previous post, I mentioned that I’d integrate the formula so that coffee consumption could be considered an event of finite duration instead of a point event. Even if you bolt down a cup of coffee, it’s not instant. (If you really want to, you can use a Dirac delta function, also known as an impulse function, to model instant consumption. The following equations will still hold.)

I was going to simply integrate, but after rereading Animate Your Way to Glory I decided to derive it as a filter instead. This should come out to the exact same result, but with a method that’s a bit easier to grasp intuitively.

In the second demo, slide 66, the following equation appears:

$p _ i = q _ i \otimes r _ i = \sum _ {k=-\infty}^{\infty} q _ k\, r _ {i-k}$This represents a convolved function, and as Wittens describes and demonstrates, convolution effectively “slides” one function along the mirror image of the other. In this case, $q _ i$ is the *filter* and $r _ i$ is the signal.

The unique thing about our application is that we’re not looking for a signal of similar magnitude to the input; we’re literally just summing events. Wittens says that the area under the curve for $q _ i$ needs to equal 1, but in our case that’s not so.

Converting this from discrete events into a continuum is easy; just switch around the notation:

$p(t) = q(t) \otimes r(t) = \int_{-\infty}^{\infty}q(u)\;r(t-u)\;du$$p(t)$ represents the output we want: the amount of caffeine in the body at time $t$. $r(t)$ is easy; that’s our consumption, or rate of input of coffee. For one cup consumed between time $t _ {1 _ i}$ and $t _ {2 _ i}$, with $a _ i$ being amount of caffeine in the cup:

$r(t) = \begin{cases} \frac{a _ i}{t _ {2 _ i}-t _ {1 _ i}}, & t _ {1 _ i}\le t \le t _ {2 _ i}\\ 0\; & \text{elsewhere} \end{cases}$Now, though, we must deal with the annoying fact that we don’t actually know what our filter is. Let’s derive it starting with our original formula.

$\sum_i \left( \frac{ a _ i \exp\left({\frac{-(t-t _ i)\ln 2}{\lambda}}\right) \left( 1 - \exp\left(\frac{(t-t _ i)\ln 0.05}{\alpha}\right) \right)} {ML} \right)$That sigma is annoying; let’s convert it to an integral. Letting $a_u$ represent rate of caffeine consumption:

$p(t) = \frac 1 {ML} \int_{-\infty}^{\infty} { a(u)\; e^{\left( \frac{-(t-u)\ln 2} {\lambda} \right)} \left( 1 - e^{\left( \frac{(t-u)\ln 0.05}{\alpha} \right)} \right) } \;du$This is actually our answer, but I want to pick it apart a little bit before I call it a day. If you consider the consumption events used in our previous model as point events, you can model them with a Dirac delta function and the above formula still works.

And look, the $(t-u)$ terms stayed intact! Meaning that our filter is simply:

$q(t) = e^{\left(\frac{-t\ln 2}{\lambda}\right)} \left(1-e^{\left(\frac{t\ln 0.05}{\alpha}\right)} \right)$We can do one better though. If we define the following:

$\begin{array}{rcl} A &= &\frac a {t_2 - t_1} \\ k_1 &= &-\frac {\ln 2}\lambda \\ k_2 &= &\frac{\ln 0.05}{\alpha} \\ L &= &\text{liters of blood per kg of mass in the typical human} \\ &\approx & 0.007 \frac{\mathrm{L}}{\mathrm{kg}} \\ M &= &\text{body mass} \\ \lambda &=& \text{half-life of caffeine in the body} \\ &\approx & 5 \text{ hours} \\ \alpha &=& \text{absorption rate} \\ &\approx & 1\text{ hour} \end{array}$Then a cup of coffee containing amount $a$ of caffeine consumed between times $t _ 1$ and $t _ 2$ will, at time $t$, cause the following amount of caffeine to remain in the body:

$p(t) = \frac A {ML} \left[ \frac{e^{(k _ 2+k _ 1)(t-u)}}{k _ 2+k _ 1} - \frac{e^{k _ 1(t-u)}}{k _ 1} \right] _ {u=t _ 1}^{t _ 2}$For multiple cups, we simply need to evaluate the above expression once for each cup. I don’t think we can really get any more accurate without a cup that constantly measures the level of liquid in it. Even if we tried this, it’s likely that the model would begin to break down. When you’re measuring on such small timescales, you’d need to keep track of things like esophagaus movement and salivation. Our model is about as good as it gets without delving into some of the crazier realms.