Mathemathinking Jekyll 2017-12-06T19:17:12+00:00 http://CorySimon.github.io/ Cory Simon http://CorySimon.github.io/ CoryMSimon@gmail.com <![CDATA[Relating the binomial and Poisson distributions]]> http://CorySimon.github.io/articles/poisson-binomial 2017-05-14T00:00:00+00:00 2017-05-14T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>The Poisson and binomial distributions become equivalent under a limit. Understanding this lends a helpful interpretation of the Poisson distribution. In this post, we will build our way up to the Poisson distribution under the context of owning of a coffee shop in Seattle.</p> <h1 id="the-bernoulli-distribution">The Bernoulli distribution</h1> <p>A given pedestrian is approaching our coffee shop. :runner:</p> <p>Let <script type="math/tex">X</script> be the binary random variable that indicates whether that person <br />(i) comes in to buy coffee (<script type="math/tex">X=</script>:coffee:) or <br />(ii) passes by and continues walking in the rain (<script type="math/tex">X=</script>:umbrella:). [This is Seattle!]</p> <p>The Bernoulli distribution specifies the probabilities <script type="math/tex">Pr(\cdot)</script> of outcome (i) as <script type="math/tex">p</script> and outcome (ii) as <script type="math/tex">1-p</script>:</p> <p><script type="math/tex">Pr(X=</script>:coffee:<script type="math/tex">) = p</script></p> <p><script type="math/tex">Pr(X=</script>:umbrella:<script type="math/tex">) = 1-p.</script></p> <h1 id="the-binomial-distribution">The binomial distribution</h1> <p>Consider today when many pedestrians are walking by our coffee shop. Let us model the outcome of whether or not each pedestrian stops by our coffee shop with the Bernoulli distribution above (same <script type="math/tex">p</script> for each pedestrian). Assume each pedestrian behaves independently of what the others do.</p> <p>Given <script type="math/tex">n</script> :runner:’s walk by our coffee shop today, what is the probability that <script type="math/tex">k</script> of them stop in to buy a coffee :coffee:? That’s the binomial distribution.</p> <p>The probability of observing a <em>particular</em> sequence of <script type="math/tex">k</script> coffee buyers and <script type="math/tex">n-k</script> passerbys is <script type="math/tex">p^k(1-p)^{n-k}</script>. For example, for <script type="math/tex">n=3</script> and <script type="math/tex">k=1</script>, the probability of the sequence :umbrella: :umbrella: :coffee: is <script type="math/tex">(1-p)^2p</script>.</p> <p>For a given <script type="math/tex">n</script> and <script type="math/tex">k</script>, there are <script type="math/tex">\binom{n}{k}</script> different ways to arrange <script type="math/tex">k</script> :coffee:’s and <script type="math/tex">n-k</script> :umbrella:’s into a sequence. See previous post <a href="http://corysimon.github.io/articles/counting/">here</a>. For <script type="math/tex">n=3</script> and <script type="math/tex">k=1</script>, there are <script type="math/tex">\binom{3}{1}=3</script> ways: (1) :umbrella: :umbrella: :coffee: (2) :umbrella: :coffee: :umbrella: and (3) :coffee: :umbrella: :umbrella:.</p> <p>Accounting for these different arrangements, the binomial distribution is:</p> <p><script type="math/tex">Pr(k</script> :coffee:’s, <script type="math/tex">n-k</script> :umbrella:’s<script type="math/tex">) = \binom{n}{k}p^k(1-p)^{n-k}.</script></p> <p>You can work out the expected value of the Bernoulli distribution to be <script type="math/tex">np</script>. This is intuitive: in the long run, we expect a fraction <script type="math/tex">p</script> of the <script type="math/tex">n</script> pedestrians to stop in and buy a coffee.</p> <h1 id="the-poisson-distribution">The Poisson distribution</h1> <p>Now, imagine instead that we’re interested in modeling the random variable <script type="math/tex">C</script> that is the count of pedestrians :runner: that walk by our coffee shop in a given hour. Suppose that on average, we see <script type="math/tex">E(C)= 6</script> people walk by our coffee shop each hour.</p> <p>Our first modeling approach might be to break up the hour into intervals of 5 minutes. There are 12 such 5 minute time intervals in an hour. Then, for each interval of 5 minutes, we can model the binary outcome of a pedestrian walking by (or not) as a Bernoulli random variable with probability <script type="math/tex">p</script>.</p> <p>The count of pedestrians walking by in an hour would then follow a binomial distribution with <script type="math/tex">n=12</script> Bernoulli trials and <script type="math/tex">p=1/2</script>, since we have on average <script type="math/tex">E(C) = np=6</script> pedestrians each hour.</p> <p>While breaking time into discrete, consecutive intervals yields the correct average behavior, it is an inadequate way of modeling the pedestrian count since it does not allow for the possibility of: <br />(i) more than one pedestrian walking by in a given 5 minute interval or <br />(ii) greater than 12 pedestrians walking by in the hour.</p> <p>We can mitigate each of the two issues above by breaking time into even smaller intervals. Breaking the hour into <script type="math/tex">n=60</script> intervals, where each minute is a Bernoulli trial, we would set <script type="math/tex">p=1/10</script> to maintain that <script type="math/tex">E(C)= np=6</script>. Since <script type="math/tex">p</script> is smaller than when we had <script type="math/tex">n=12</script> intervals, we mitigate the possibility of (i) and account for up to 60 pedestrians walking by our coffee shop to address (ii).</p> <p>We can make the time intervals as small as we want under this binomial distribution framework, as long as we also make <script type="math/tex">p</script> small enough such that <script type="math/tex">np=E(C)=6</script>. In the limit of infinitely small time intervals, when <script type="math/tex">n \rightarrow \infty</script>, holding the average <script type="math/tex">\lambda:=np</script> fixed, we recover the Poisson distribution:</p> <p><script type="math/tex">Pr(C=c)=e^{-\lambda}\frac{\lambda^c}{c!}</script> for <script type="math/tex">c=0,1,2,3,...</script></p> <p>So, the Poisson distribution is a stochastic model of event counts in a given time interval. It partitions the time interval into infinitely small subintervals and models the binary outcome of the event of interest occurring in each interval as an independent, Bernoulli trial. To maintain an expected value of event counts of <script type="math/tex">\lambda</script>, we simultaneously need <script type="math/tex">p\rightarrow0</script> for each Bernoulli trail in the infinitely small subintervals. Below, we formalize this.</p> <p>This interpretation of the Poisson process as an infinite series of identical, independent Bernoulli trials is helpful in understanding why the Poisson process is memoryless: the previous events do not affect the outcome of the future. See my previous post <a href="http://corysimon.github.io/articles/the-poisson-process/">here</a>.</p> <h1 id="the-math-of-the-poisson-limit-theorem">The math of the Poisson limit theorem</h1> <p>For a random variable <script type="math/tex">X</script> that follows the binomial distribution with <script type="math/tex">n</script> trials:</p> <script type="math/tex; mode=display">Pr(X=k) = \binom{n}{k}p^k(1-p)^{n-k}.</script> <p>Our goal is to show that we recover the Poisson distribution with average <script type="math/tex">\lambda:= np</script> when we take the limit <script type="math/tex">n\rightarrow \infty</script> while holding <script type="math/tex">np</script> fixed. To achieve this, we specify that <em>both</em> <script type="math/tex">n\rightarrow \infty</script> and <script type="math/tex">p\rightarrow 0</script> at rates such that the average <script type="math/tex">np</script> remains fixed. That is, we will show that:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | np \text{ fixed}} \binom{n}{k}p^k(1-p)^{n-k} = e^{-\lambda}\frac{\lambda^k}{k!}.</script> <p>First, write the limit in terms of <script type="math/tex">\lambda:=np</script> and write the binomial term as factorials:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \frac{n!}{(n-k)!k!} \left( \frac{\lambda}{n} \right)^k \left( 1-\frac{\lambda}{n} \right)^{n-k}.</script> <p>Then, pull out the <script type="math/tex">n^k</script> term:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \frac{n!}{(n-k)! n^k} \frac{1}{k!}\left( \lambda \right)^k \left( 1-\frac{\lambda}{n} \right)^{n-k},</script> <p>and split up the <script type="math/tex">(\cdot)^{n-k}</script> term into two products:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \frac{\lambda^k}{k!} \frac{n!}{(n-k)! n^k} \left( 1-\frac{\lambda}{n} \right)^{n}\left( 1-\frac{\lambda}{n} \right)^{-k}.</script> <p>It suffices to show that:</p> <script type="math/tex; mode=display">\lim_{n \rightarrow \infty | \lambda \text{ fixed}} \frac{n!}{(n-k)! n^k} \left( 1-\frac{\lambda}{n} \right)^{n}\left( 1-\frac{\lambda}{n} \right)^{-k}=e^{-\lambda}.</script> <p>since then we recover the Poisson distribution.</p> <p>First, focus on the limit:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \frac{n!}{(n-k)! n^k}</script> <p>The <script type="math/tex">(n-k)!</script> term in the denominator cancels out terms in the product <script type="math/tex">n!</script> in the numerator.</p> <script type="math/tex; mode=display">\frac{n!}{(n-k)! n^k} = \frac{n(n-1)(n-2)\cdots(n-k-1)}{n^k}.</script> <p>The numerator is thus a <script type="math/tex">k</script>th order polynomial in <script type="math/tex">n</script>. As <script type="math/tex">n\rightarrow\infty</script>, the dominant term in the numerator is <script type="math/tex">n^k</script>; the sizes of the lower order terms pale in comparison. So, as <script type="math/tex">n\rightarrow \infty</script>, this term behaves like <script type="math/tex">n^k / n^k</script> and consequently:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \frac{n!}{(n-k)! n^k} = 1</script> <p>Next, focus on the limit:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \left( 1-\frac{\lambda}{n} \right)^{-k} = 1/\left( 1-\frac{\lambda}{n} \right)^{k}</script> <p>As <script type="math/tex">n\rightarrow\infty</script>, the <script type="math/tex">\lambda / n \rightarrow 0</script> since <script type="math/tex">\lambda</script> is held fixed. The inside of the parentheses then approaches 1. Multiplying 1 by itself <script type="math/tex">k</script> times is 1 (<script type="math/tex">k</script> is under no limit here). Consequently:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \left( 1-\frac{\lambda}{n} \right)^{-k} = 1/\left( 1-\frac{\lambda}{n} \right)^{k}=1</script> <p>Finally, focus on the limit:</p> <script type="math/tex; mode=display">\lim_{n\rightarrow\infty | \lambda \text{ fixed}} \left( 1-\frac{\lambda}{n} \right)^{n}.</script> <p>This one is famous. As directly above, the inside of the parenthesis goes to 1, but the numerator also goes to infinity. So we are multiplying a number a little more than 1 an infinite number of times. One student in the room might say the limit is equal to 1 as directly above. Another might say the limit is <script type="math/tex">\infty</script> because we are multiplying a number [slightly] greater than one by itself many times. Both students are wrong– this limit amazingly goes to the mathematical constant <script type="math/tex">e^{-\lambda}</script>, where <script type="math/tex">e \approx 2.71828</script>. This is in fact a way to define the exponential constant <script type="math/tex">e</script>:</p> <script type="math/tex; mode=display">e:=\lim_{n\rightarrow\infty} \left(1+\frac{1}{n}\right)^n.</script> <p>See <a href="https://en.wikipedia.org/wiki/E_(mathematical_constant)">Wikipedia</a>. You might recognize this as continuously compounding interest.</p> <p>As the limit of a product of functions is the product of the limits when the limits are finite, we have proven that:</p> <script type="math/tex; mode=display">\lim_{n \rightarrow \infty | \lambda \text{ fixed}} \frac{n!}{(n-k)! n^k} \left( 1-\frac{\lambda}{n} \right)^{n}\left( 1-\frac{\lambda}{n} \right)^{-k}=e^{-\lambda},</script> <p>and shown the connection between the Poisson and binomial distributions.</p> <p><a href="http://CorySimon.github.io/articles/poisson-binomial/">Relating the binomial and Poisson distributions</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on May 14, 2017.</p> <![CDATA[Recall, precision, specificity, and sensitivity]]> http://CorySimon.github.io/articles/classification-metrics 2017-02-22T00:00:00+00:00 2017-02-22T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>When working with classification algorithms, I consistently need to remind myself of the definition of recall, precision, specificity, and sensitivity. So, I created an illustration for reference.</p> <p>Imagine you’re on a fishing boat in the <a href="https://en.wikipedia.org/wiki/Great_Pacific_garbage_patch">Great Pacific garbage patch</a>. You cast your fishing net and retrieve a mix of fish and plastic bottles. A robot on the boat is equipped with a machine learning algorithm to classify each catch as a fish, defined as a positive (+), or a plastic bottle, defined as a negative (-).</p> <figure class="center"> <img src="/images/classification_metrics/fish_bottles.png" alt="image" /> </figure> <p>The fish/bottle classification algorithm makes mistakes. To quantify its performance, we define recall, precision, specificity, and selectivity. Each are conditional probabilities. Note that the sensitivity (= recall) and specificity are each conditioned on the true class label.</p> <figure class="center"> <img src="/images/classification_metrics/classify.png" alt="image" /> </figure> <p>Consider below the test set of eight fish and four plastic bottles. The algorithm classifies the catches highlighted in green as fish (+’s) and in red as bottles (-‘s). The sensitivity (= recall), specificity, and precision are written for this test outcome.</p> <figure class="center"> <img src="/images/classification_metrics/metric_example.png" alt="image" /> </figure> <p><a href="http://CorySimon.github.io/articles/classification-metrics/">Recall, precision, specificity, and sensitivity</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on February 22, 2017.</p> <![CDATA[Periodic functions as art]]> http://CorySimon.github.io/articles/periodicvis 2016-09-03T00:00:00+00:00 2016-09-03T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>I came across this interesting piece of art by Paul Brand at the Norwegian Museum of Contemporary Art in Oslo, Norway. Vials are evenly arranged on a circle, and each vial contains a different amount of blue liquid.</p> <figure class="center"> <img src="/images/DSC_0077.JPG" alt="image" /> </figure> <p>This is essentially a visualization of a periodic function <script type="math/tex">f(\theta)</script> with a period of <script type="math/tex">2\pi</script>. The liquid level in each vial represents the function value at that particular polar coordinate, <script type="math/tex">\theta</script>.</p> <p>Note the larger difference in liquid level between the top-most vial and the vial to the left of it. This suggests that the artist may have chosen a <em>discontinuous</em> <script type="math/tex">2\pi</script>-periodic function to visualize here. But, in reality, the behavior of the function is unknown between each vial with this visualization strategy.</p> <p><a href="http://CorySimon.github.io/articles/periodicvis/">Periodic functions as art</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on September 03, 2016.</p> <![CDATA[Noria, a wheely Xe-selective solid]]> http://CorySimon.github.io/articles/noria 2016-07-05T00:00:00+00:00 2016-07-05T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p><a href="http://CorySimon.github.io/articles/noria/">Noria, a wheely Xe-selective solid</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on July 05, 2016.</p> <![CDATA[Computationally-inspired material discovery]]> http://CorySimon.github.io/articles/sbmof-1 2016-06-26T00:00:00+00:00 2016-06-26T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p><a href="http://CorySimon.github.io/articles/sbmof-1/">Computationally-inspired material discovery</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on June 26, 2016.</p> <![CDATA[So you think you can count?]]> http://CorySimon.github.io/articles/counting 2016-02-06T00:00:00+00:00 2016-02-06T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>Think you can count? We’re in a cafeteria with a fruit basket.</p> <p>:banana: :strawberry: :watermelon: :green_apple:</p> <p>An arrangement of objects into a sequence, where order matters, is a <em>permutation</em>. A collection of objects, when the order does not matter, is a <em>combination</em>.</p> <h1 id="counting-permutations">Counting permutations</h1> <p>Here, we consider the choices of fruit among our three friends, Christina, Joaquin, and Ezra. Because Christina, Joaquin, and Ezra are in line at the cafeteria, the order matters.</p> <h5 id="case-1-christina-joaquin-and-ezra-each-select-one-fruit-there-are-plenty-of-bananas-banana-strawberries-strawberry-watermelons-watermelon-and-green-apples-green_apple-in-the-cafeteria-how-many-different-outcomes-are-there-eg-one-outcome-is-christina-and-ezra-select-a-banana-and-joaquin-selects-a-watermelon">Case 1. Christina, Joaquin, and Ezra each select one fruit. There are plenty of bananas :banana:, strawberries :strawberry:, watermelons :watermelon:, and green apples :green_apple: in the cafeteria. How many different outcomes are there? e.g., one outcome is Christina and Ezra select a banana and Joaquin selects a watermelon.</h5> <p>Each person individually has four different outcomes. Since there are three people and any one person’s choice does not change the possible outcomes of the others, the number of outcomes is <script type="math/tex">4\times4\times4=4^3</script>.</p> <p>More generally, when <script type="math/tex">k</script> people each have a choice of <script type="math/tex">n</script> fruits, the number of outcomes is <script type="math/tex">n^k</script>. This is called a permutation <em>with repetition</em> since people may choose the same fruits.</p> <h5 id="case-2-christina-joaquin-and-ezra-each-select-one-fruit-now-however-there-is-only-a-single-banana-banana-green-apple-green_apple-and-strawberry-strawberry-remaining-in-the-fruit-basket-how-many-different-outcomes-are-there">Case 2. Christina, Joaquin, and Ezra each select one fruit. Now, however, there is only a single banana :banana:, green apple :green_apple:, and strawberry :strawberry: remaining in the fruit basket. How many different outcomes are there?</h5> <p>Now, since Christina is in line first, she has a choice between all three fruits. However, after she chooses one, there are only two choices remaining for Joaquin; Ezra gets one choice. So, here the number of different outcomes is <script type="math/tex">3\times 2 \times 1</script>.</p> <p>More generally, given <script type="math/tex">n</script> people lined up to select <script type="math/tex">n</script> distinguishable fruits, the number of outcomes is: <script type="math/tex">n(n-1)(n-2)\cdots (3)(2)(1).</script></p> <p>This is called the <em>factorial</em> of <script type="math/tex">n</script>, denoted as <script type="math/tex">n!</script>.</p> <h5 id="case-3-christina-joaquin-and-ezra-each-select-one-fruit-now-however-there-is-a-banana-banana-green-apple-green_apple-strawberry-strawberry-and-watermelon-watermelon-remaining-in-the-fruit-basket-how-many-different-outcomes-are-there">Case 3. Christina, Joaquin, and Ezra each select one fruit. Now, however, there is a banana :banana:, green apple :green_apple:, strawberry :strawberry:, <em>and</em> watermelon :watermelon: remaining in the fruit basket. How many different outcomes are there?</h5> <p>There are four fruits for three people. One fruit will be left behind. Following the logic above, Christina has four choices, Joaquin has three choices, and Ezra has two choices. So there are <script type="math/tex">4\times 3 \times 2</script> outcomes.</p> <p>More generally, when <script type="math/tex">k</script> people each choose one of <script type="math/tex">n</script> distinguishable fruits, for <script type="math/tex">n\geq k</script>, the number of outcomes is: <script type="math/tex">n(n-1)(n-2) \cdots (n-k+1).</script></p> <p>There are <script type="math/tex">k</script> factors here, one for each person. Note that this can be written as:</p> <script type="math/tex; mode=display">\frac{n!}{(n-k)!}.</script> <p>This scenario, where we seek the number of orderings of a subset of <script type="math/tex">k</script> objects from a set of <script type="math/tex">n</script> distinguishable objects is called a <script type="math/tex">k</script>-permutation of <script type="math/tex">n</script>.</p> <h5 id="case-4-christina-joaquin-and-ezra-each-selected-one-fruit-we-know-that-all-together-christina-joaquin-and-ezra-selected-two-bananas-and-a-watermelon-but-we-dont-know-who-selected-which-fruit-how-many-different-possibilities-are-there-eg-one-possibility-is-that-christina-selected-the-watermelon-and-joaquin-and-ezra-each-selected-a-banana">Case 4. Christina, Joaquin, and Ezra each selected one fruit. We know that, all together, Christina, Joaquin, and Ezra selected two bananas and a watermelon, but we don’t know who selected which fruit. How many different possibilities are there? e.g., one possibility is that Christina selected the watermelon and Joaquin and Ezra each selected a banana.</h5> <p>Here, we seek how many different ways we can distribute two bananas and a watermelon among Christina, Ezra, and Joaquin, under the constraint that each receives one fruit. This seems similar to Case 2, where we counted the permutations of three different, <em>distinguishable</em> fruits. But, in this scenario, the two bananas are <em>indistinguishable</em>. i.e., if we could label the bananas with 1 and 2, the assignment:</p> <p>Christina: :watermelon:, Ezra: :banana:<script type="math/tex">_1</script>, Joaquin: :banana:<script type="math/tex">_2</script></p> <p>is the same as the assignment:</p> <p>Christina: :watermelon:, Ezra: :banana:<script type="math/tex">_2</script>, Joaquin: :banana:<script type="math/tex">_1</script></p> <p>since both describe the scenario where Christina gets the watermelon and Joaquin and Ezra get a banana.</p> <p>That is, <script type="math/tex">3!</script> overcounts the number of possibilities because, upon switching the two labeled bananas in each assignment, the assignment represents the same outcome. Consequently, we must then divide the answer for the <em>distinguishable</em> fruit case (<script type="math/tex">3!</script>) by the number of ways we can permute the bananas. Since there are two bananas, we need to divide by <script type="math/tex">2!=2</script>. Thus, there are <script type="math/tex">3!/2! =3</script> outcomes here:</p> <ol> <li>Christina: :watermelon:, Ezra: :banana:, Joaquin: :banana:</li> <li>Christina: :banana:, Ezra: :watermelon:, Joaquin: :banana:</li> <li>Christina: :banana:, Ezra: :banana:, Joaquin: :watermelon:</li> </ol> <p>From the list above, we see that we could also reduce this problem to: who gets the watermelon? Then, there are only 3 choices.</p> <p>More generally, consider when we have <script type="math/tex">n</script> fruits consisting of <script type="math/tex">k</script> different (<em>distinguishable</em>) kinds fruit. Let <script type="math/tex">n_i</script> be the number of fruits of type <script type="math/tex">i</script> (e.g. here, <script type="math/tex">n</script>:banana:<script type="math/tex">=2</script> and <script type="math/tex">n</script>:watermelon:<script type="math/tex">=1</script>); we have <script type="math/tex">k</script> of these variables, and a constraint here is:</p> <script type="math/tex; mode=display">n_1 + n_2 + \cdots n_k = n.</script> <p>The number of permutations is</p> <script type="math/tex; mode=display">\frac{n!}{n_1! n_2! \cdots n_k!}.</script> <p>The <script type="math/tex">k</script> numbers in the denominator– one for each type of fruit– are to account for the indistinguishability of these fruits, as we did for the bananas in this case. This is called a <em>multiset permutation</em>.</p> <h1 id="counting-combinations">Counting combinations</h1> <p>Here, we will see examples where the ordering of the fruits does not matter by considering only Christina’s selection of fruit from the basket.</p> <h5 id="case-5-christina-selects-two-different-fruits-from-a-fruit-basket-that-contains-a-banana-banana-a-strawberry-strawberry-a-watermelon-watermelon-and-a-green-apple-green_apple-how-many-different-outcomes-are-there-eg-one-outcome-is-that-she-selects-a-watermelon-and-a-strawberry-here-the-ordering-of-the-fruit-does-not-matter">Case 5. Christina selects two <em>different</em> fruits from a fruit basket that contains a banana :banana:, a strawberry :strawberry:, a watermelon :watermelon:, and a green apple :green_apple:. How many different outcomes are there? e.g., one outcome is that she selects a watermelon and a strawberry. Here, the ordering of the fruit does not matter.</h5> <p>If ordering <em>did</em> matter, we would count the <script type="math/tex">k=2</script> permutations of <script type="math/tex">n=4</script>; Christina has four choices for the first fruit and then three remaining choices for the second fruit she picks, giving <script type="math/tex">4\times 3</script> outcomes. However, we need to correct for the fact that, here, since ordering does not matter,</p> <p>{ :strawberry:, :watermelon: }</p> <p>is the same as</p> <p>We can correct for this overcounting by dividing by the number of permutations (reorderings) of the two fruits that Christina selected, which in this case is <script type="math/tex">2!=2</script>. There are thus <script type="math/tex">4 \times 3 / 2 = 6</script> different outcomes here.</p> <p>We will use the brackets {} to denote a <em>set</em> of objects where ordering does not matter. All outcomes here are then:</p> <ol> <li>{:strawberry:, :watermelon:}</li> <li>{:strawberry:, :banana:}</li> <li>{:strawberry:, :green_apple:}</li> <li>{:watermelon:, :banana:}</li> <li>{:watermelon:, :green_apple:}</li> <li>{:green_apple:, :banana:}</li> </ol> <p>More generally, following the math for <script type="math/tex">k</script>-permutations of <script type="math/tex">n</script> in Case 3, the number of <em>combinations</em> of <script type="math/tex">k</script> fruits from a basket of <script type="math/tex">n</script> distinguishable fruits (<script type="math/tex">k=2</script>, <script type="math/tex">n=4</script> in this case) is:</p> <script type="math/tex; mode=display">\frac{n!}{(n-k)! k!}.</script> <p>So the formula gives the number of <script type="math/tex">k</script>-permutations of <script type="math/tex">n</script> divided by <script type="math/tex">k!</script>, the number of ways to permute/rearrange an ordered sequence of <script type="math/tex">k</script> fruits, to correct for the order being inconsequential.</p> <p>The number of combinations of <script type="math/tex">k</script> objects from a set of <script type="math/tex">n</script> distinguishable objects has a special notation:</p> <script type="math/tex; mode=display">\binom{n}{k} := \frac{n!}{(n-k)! k!},</script> <p>read out loud as “<script type="math/tex">n</script> choose <script type="math/tex">k</script>”.</p> <h5 id="case-6-christina-selects-two-fruits-from-a-fruit-basket-with-four-different-types-of-fruit-bananas-banana-strawberries-strawberry-watermelons-watermelon-and-green-apples-green_apple-however-we-now-relax-the-constraint-in-case-5-by-allowing-duplicates-christina-can-select-two-of-the-same-fruit-if-she-desires-how-many-outcomes-are-there-again-ordering-does-not-matter">Case 6. Christina selects two fruits from a fruit basket with four different types of fruit (bananas :banana:, strawberries :strawberry:, watermelons :watermelon:, and green apples :green_apple:). However, we now relax the constraint in Case 5 by allowing duplicates; Christina can select two of the same fruit if she desires. How many outcomes are there? Again, ordering does not matter.</h5> <p>There is a visual way to understand this problem. To represent a particular outcome, let’s list the fruits that Christina chooses in a special way, using cookies :cookie: from the cafeteria. We place <script type="math/tex">n-1=3</script> cookies in a row on the table:</p> <p>:cookie: :cookie: :cookie:</p> <p>When Christina chooses two fruits, we group them together by fruit type (e.g. bananas with bananas) and place them in between the cookies, using the cookies as <em>separators</em>. Our convention is to place the fruits in the following order: bananas, strawberries, watermelons, and then green apples.</p> <p>For example, if she chooses a banana and a watermelon, we represent this as:</p> <p>:banana: :cookie: :cookie: :watermelon: :cookie:</p> <p>If she chooses two watermelons:</p> <p>:cookie: :cookie: :watermelon: :watermelon: :cookie:</p> <p>If she chooses a watermelon and a green apple:</p> <p>:cookie: :cookie: :watermelon: :cookie: :green_apple:</p> <p>That is, we keep a cookie :cookie: to separate the fruit type even if there were no fruits of that type. This way, no matter what the outcome, there are <em>always</em> <script type="math/tex">k=2</script> fruits and <script type="math/tex">n-1=3</script> cookies in our representation of an outcome, for a total of <script type="math/tex">k+n-1=5</script> foods representing the outcome.</p> <p>From this representation of outcomes, we see that the number of outcomes is equal to the number of ways that we can arrange the <script type="math/tex">n-1=3</script> cookies in <script type="math/tex">k+n-1=5</script> positions (the rest of the positions will be occupied by fruits). This is a <em>multiset permutation</em> as in Case 4, where we are looking for the number of permutations of <script type="math/tex">k+n-1</script> symbols, of which <script type="math/tex">n-1</script> are indistinguishable cookies and the other <script type="math/tex">k</script> symbols are fruits, which we also view as indistinguishable since the positions of the cookies distinguish the fruits implicitly by our convention of ordering the fruit.</p> <p>The number of ways we can arrange the <script type="math/tex">n-1</script> cookies among the <script type="math/tex">k+n-1</script> positions is:</p> <script type="math/tex; mode=display">\frac{(k+n-1)!}{k! (n-1)!} = \binom{k+n-1}{k}.</script> <p>In this particular case, <script type="math/tex">\binom{2+4-1}{2}=10</script>. We now check by enumerating the possibilities. We have four more possibilities than in Case 5 above, since we now allow for duplicates:</p> <ol> <li>{:strawberry:, :watermelon:}</li> <li>{:strawberry:, :banana:}</li> <li>{:strawberry:, :green_apple:}</li> <li>{:watermelon:, :banana:}</li> <li>{:watermelon:, :green_apple:}</li> <li>{:green_apple:, :banana:}</li> <li>{:strawberry:, :strawberry:}</li> <li>{:banana:, :banana:}</li> <li>{:watermelon:, :watermelon:}</li> <li>{:green_apple:, :green_apple:}</li> </ol> <p>This scenario is counting the number of <script type="math/tex">k</script> combinations of <script type="math/tex">n</script> objects <em>with repetitions</em>. This type of counting also has a special notation:</p> <script type="math/tex; mode=display">\left( \binom{n}{k} \right):= \frac{(k+n-1)!}{k! (n-1)!} = \binom{k+n-1}{k}</script> <p><a href="http://CorySimon.github.io/articles/counting/">So you think you can count?</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on February 06, 2016.</p> <![CDATA[Nested loops via recursion]]> http://CorySimon.github.io/articles/recursion 2016-02-05T00:00:00+00:00 2016-02-05T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>I encountered a problem where I needed to program a nested loop and wanted to share (code is in Julia).</p> <p><em>Problem:</em> We have a lattice with <code class="highlighter-rouge">n</code> ordered lattice sites, each of which are in state <code class="highlighter-rouge">-1</code> or <code class="highlighter-rouge">1</code>. There are thus <script type="math/tex">2^n</script> distinct lattice states. My goal is to exhaustively enumerate each of these lattice states.</p> <p>We can store the state of the lattice in an <code class="highlighter-rouge">n</code>-dimensional array of integers, <code class="highlighter-rouge">lattice_state</code>. For <code class="highlighter-rouge">n=2</code>, we can use a nested for loop to enumerate all lattice states. Each for loop represents a particular lattice site, or entry of <code class="highlighter-rouge">lattice_state</code>.</p> <div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">n</span> <span class="o">=</span> <span class="mi">2</span> <span class="n">lattice_state</span> <span class="o">=</span> <span class="n">zeros</span><span class="x">(</span><span class="kt">Int</span><span class="x">,</span> <span class="n">n</span><span class="x">)</span> <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">=</span> <span class="n">i</span> <span class="k">for</span> <span class="n">j</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">=</span> <span class="n">j</span> <span class="n">println</span><span class="x">(</span><span class="n">lattice_state</span><span class="x">)</span> <span class="k">end</span> <span class="k">end</span> <span class="c"># =&gt;</span> <span class="c"># [-1,-1]</span> <span class="c"># [-1,1]</span> <span class="c"># [1,-1]</span> <span class="c"># [1,1]</span> </code></pre></div></div> <p>When <code class="highlighter-rouge">n</code> gets large, this looks ugly because we need to write <code class="highlighter-rouge">n</code> nested loops:</p> <div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">n</span> <span class="o">=</span> <span class="mi">4</span> <span class="n">lattice_state</span> <span class="o">=</span> <span class="n">zeros</span><span class="x">(</span><span class="kt">Int</span><span class="x">,</span> <span class="n">n</span><span class="x">)</span> <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">1</span><span class="x">]</span> <span class="o">=</span> <span class="n">i</span> <span class="k">for</span> <span class="n">j</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">2</span><span class="x">]</span> <span class="o">=</span> <span class="n">j</span> <span class="k">for</span> <span class="n">k</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">3</span><span class="x">]</span> <span class="o">=</span> <span class="n">k</span> <span class="k">for</span> <span class="n">l</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="n">lattice_state</span><span class="x">[</span><span class="mi">4</span><span class="x">]</span> <span class="o">=</span> <span class="n">l</span> <span class="n">println</span><span class="x">(</span><span class="n">lattice_state</span><span class="x">)</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span> <span class="c"># =&gt;</span> <span class="c"># [-1,-1,-1,-1]</span> <span class="c"># [-1,-1,-1,1]</span> <span class="c"># [-1,-1,1,-1]</span> <span class="c"># [-1,-1,1,1]</span> <span class="c"># [-1,1,-1,-1]</span> <span class="c"># [-1,1,-1,1]</span> <span class="c"># [-1,1,1,-1]</span> <span class="c"># [-1,1,1,1]</span> <span class="c"># [1,-1,-1,-1]</span> <span class="c"># [1,-1,-1,1]</span> <span class="c"># [1,-1,1,-1]</span> <span class="c"># [1,-1,1,1]</span> <span class="c"># [1,1,-1,-1]</span> <span class="c"># [1,1,-1,1]</span> <span class="c"># [1,1,1,-1]</span> <span class="c"># [1,1,1,1]</span> </code></pre></div></div> <p>We can use a recursive algorithm instead to write a function for a general <code class="highlighter-rouge">n</code>:</p> <div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">n</span> <span class="o">=</span> <span class="mi">8</span> <span class="n">lattice_state</span> <span class="o">=</span> <span class="n">zeros</span><span class="x">(</span><span class="kt">Int</span><span class="x">,</span> <span class="n">n</span><span class="x">)</span> <span class="k">function</span><span class="nf"> nested_loop</span><span class="x">(</span><span class="n">loop_level</span><span class="o">::</span><span class="kt">Int</span><span class="x">)</span> <span class="s">""" Recursive algorithm to perform nested loop: for i = [-1, 1] lattice_state = i for j = [-1, 1] lattice_state = j .... n times end end Prints all possible lattice_state arrays. Parameters: loop_level, Int: level of nested loop we are in. Nested loop loop_level modifies entry loop_level in lattice_state. """</span> <span class="c"># if we are the last loop,</span> <span class="c"># print the array and return (do not go on)</span> <span class="k">if</span> <span class="x">(</span><span class="n">loop_level</span> <span class="o">&gt;</span> <span class="n">n</span><span class="x">)</span> <span class="n">println</span><span class="x">(</span><span class="n">lattice_state</span><span class="x">)</span> <span class="k">return</span> <span class="k">end</span> <span class="c"># if we made it past this point, </span> <span class="c"># we need to continue going in the nested loop</span> <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="x">[</span><span class="o">-</span><span class="mi">1</span><span class="x">,</span> <span class="mi">1</span><span class="x">]</span> <span class="c"># modify entry loop_level</span> <span class="n">lattice_state</span><span class="x">[</span><span class="n">loop_level</span><span class="x">]</span> <span class="o">=</span> <span class="n">i</span> <span class="c"># with entry 1, 2, ..., loop_level fixed, </span> <span class="c"># call function again to go to next level in nested loop</span> <span class="n">nested_loop</span><span class="x">(</span><span class="n">loop_level</span> <span class="o">+</span> <span class="mi">1</span><span class="x">)</span> <span class="k">end</span> <span class="k">end</span> </code></pre></div></div> <p>Now, I call the <code class="highlighter-rouge">nested_loop</code> function with <code class="highlighter-rouge">loop_level=1</code> to start at the first loop in the nest. It will then print all <script type="math/tex">2^n</script> configurations of the lattice of length <code class="highlighter-rouge">n=8</code>, represented by unique <code class="highlighter-rouge">lattice_state</code> arrays:</p> <div class="language-julia highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">nested_loop</span><span class="x">(</span><span class="mi">1</span><span class="x">)</span> </code></pre></div></div> <p><a href="http://CorySimon.github.io/articles/recursion/">Nested loops via recursion</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on February 05, 2016.</p> <![CDATA[On funding curiosity-driven research]]> http://CorySimon.github.io/articles/curiosity-driven-research 2016-01-18T00:00:00+00:00 2016-01-18T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>The discovery of CRISPR and its exploitation to edit/manipulate genomes is revolutionizing biology. If you haven’t heard of CRISPR, see the video below.</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/2pp17E4E-O8" frameborder="0" allowfullscreen=""></iframe> <p>We can extract some important lessons from the history of how CRISPR was discovered and technology exploiting it was developed. I passionately believe that the story of this scientific advance epitomizes how science realistically progresses. Although I am a young scientist, the story aligns with my own experiences as well. As this story of scientific advance has important implications for how we fund science, it is important to disseminate it.</p> <p>The full article in Cell is <a href="http://www.cell.com/cell/fulltext/S0092-8674(15)01705-5">here</a>. I want to highlight a few points here.</p> <ul> <li>Contrarty to the stereotype that scientists are lone wolfs working solo in a lab, the discovery of CRISPR, its significance, and its exploitation to manipulate genomes was a gradual, building process with many different groups involved.</li> </ul> <blockquote> <p>Finally, the narrative underscores that scientific breakthroughs are rarely eureka moments. They are typically ensemble acts, played out over a decade or more, in which the cast becomes part of something greater than what any one of them could do alone. Eric S. Lander. “The Heroes of CRISPR.” Cell (2016) <a href="http://dx.doi.org/10.1016/j.cell.2015.12.041">DOI</a></p> </blockquote> <ul> <li>When CRISPR was first discovered, these scientists did not set out to find a way to manipulate genomes and cure disease. Instead, they were curious about microbes that could survive in salty water. Many policy-makers criticize research funds that go into basic, curiosity-driven research as wasteful compared to applied research, but this story of CRISPR epitomizes that imporant discoveries often come about from basic research that, at first, has very little to do with a direct application.</li> </ul> <blockquote> <p>The most important is that medical breakthroughs often emerge from completely unpredictable origins. The early heroes of CRISPR were not on a quest to edit the human genome—or even to study human disease. Their motivations were a mix of personal curiosity (to understand bizarre repeat sequences in salt-tolerant microbes), military exigency (to defend against biological warfare), and industrial application (to improve yogurt production). Eric S. Lander. “The Heroes of CRISPR.” Cell (2016) <a href="http://dx.doi.org/10.1016/j.cell.2015.12.041">DOI</a></p> </blockquote> <ul> <li>A critical breakthrough in discovering the significance of CRISPR came about when a scientist found certain DNA sequences associated with CRISPR. He then searched a database to find that these DNA sequences matched that of certain viruses, leading to the discovery that “CRISPR loci must encode the instructions for an adaptive immune system that protected microbes against specific infections”. The point is that this database of DNA sequences was built as a platform to advance science. Storing this data in one place and making it widely available to researchers can lead to advances and discoveries that were not specifically envisioned when such a database is initiated, which Lander calls “hypothesis-free” science:</li> </ul> <blockquote> <p>The history also illustrates the growing role in biology of “hypothesis-free” discovery based on big data. The discovery of the CRISPR loci, their biological function, and the tracrRNA all emerged not from wet-bench experiments but from open-ended bioinformatic exploration of large-scale, often public, genomic datasets. “Hypothesis-driven” science of course remains essential, but the 21st century will see an increasing partnership between these two approaches. Eric S. Lander. “The Heroes of CRISPR.” Cell (2016) <a href="http://dx.doi.org/10.1016/j.cell.2015.12.041">DOI</a></p> </blockquote> <ul> <li>Finally, it is interesting to note that some of the seminal papers on CRISPR were rejected from the leading scientific journals; one of the seminal papers took a year to get published.</li> </ul> <blockquote> <p>Their seminal papers were often rejected by leading journals—appearing only after considerable delay and in less prominent venues. These observations may not be a coincidence: the settings may have afforded greater freedom to pursue less trendy topics but less support about how to overcome skepticism by journals and reviewers. Eric S. Lander. “The Heroes of CRISPR.” Cell (2016) <a href="http://dx.doi.org/10.1016/j.cell.2015.12.041">DOI</a></p> </blockquote> <p><strong>UPDATE</strong></p> <p>As Michael Eisen pointed out in his <a href="http://www.michaeleisen.org/blog/?p=1825">blog post</a>, “The Heroes of CRISPR” article was written with a conflict of interest and may not be the most impartial story of how CRISPR was discovered. That said, the points I made here still hold!</p> <p><a href="http://CorySimon.github.io/articles/curiosity-driven-research/">On funding curiosity-driven research</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on January 18, 2016.</p> <![CDATA[Computing the heat of adsorption in a grand canonical Monte Carlo simulation]]> http://CorySimon.github.io/articles/hoa 2016-01-17T00:00:00+00:00 2016-01-17T00:00:00+00:00 Cory Simon and Efrem Braun http://CorySimon.github.io <p>The isosteric heat of adsorption is a thermodynamic quantity that characterizes the enthalpy change associated with the adsorption of a molecule on a surface. This can be measured in the laboratory in two ways. First, one can use calorimetry to measure the heat released upon the adsorption of a given amount of gas. Second, one can look at the sensitivity of the adsorption isotherms with temperature; the Clausius-Clapeyron equation relates the isosteric heat of adsorption to where on the pressure-temperature plane an adsorbed phase in equilibrium with a gas phase lies (at a given amount of adsorption).</p> <p>In molecular simulations, we typically simulate the adsorption of molecules on a surface using the Metropolis-Hastings Monte Carlo algorithm in the context of the grand canonical statistical ensemble. In this ensemble, we consider a given volume/area of the adsorbent surface in equlibrium with a bulk gas phase at constant chemical potential and temperature. This is connected to experiment by an equation of state that relates the gas phase pressure in the experiment to the chemical potential of the gas.</p> <p>During the Monte Carlo simulations of the grand canonical ensemble (GCMC simulations), we compute the energy of the adsorbate molecules, <script type="math/tex">E</script>, at each sample. One might postulate that the isosteric heat of adsorption is related to the average value of <script type="math/tex">E/N</script> during the simulation, where <script type="math/tex">N</script> is the number of adsorbates in the system. This is incorrect because it is a biased estimate of the energy per adsorbed molecule; when there are fewer particles in the system, the energy of these particles are weighted more in the average.</p> <p>Instead, we define the average energy of adsorption <script type="math/tex">q</script> as the difference in the average energy between two systems that differ only by the average number of particles. By making the average number of particles between these two systems differ by one, this is the average energy difference upon the addition of a single particle, the average energy of adsorption:</p> <script type="math/tex; mode=display">q = \langle E\rangle (\langle N \rangle+1) - \langle E \rangle (\langle N \rangle).</script> <p>Here, we are looking at <script type="math/tex">\langle E\rangle</script> as a function of <script type="math/tex">\langle N\rangle</script>.</p> <p>Dividing by one, which is also <script type="math/tex">N+1-N</script>, we see that this is approximately the derivative of <script type="math/tex">\langle E\rangle</script> with respect to <script type="math/tex">\langle N \rangle</script>:</p> <script type="math/tex; mode=display">q \approx \frac{\partial \langle E\rangle}{\partial \langle N \rangle}.</script> <p>To get a more useful formula for estimating <script type="math/tex">q</script> from GCMC simulations, we need to bring in the grand canonical partition function <script type="math/tex">\Xi(\beta, \mu, V)</script>, where <script type="math/tex">\beta=1/T</script>, <script type="math/tex">\mu</script> is the chemical potential of the gas, and <script type="math/tex">V</script> is the volume of adsorbent considered (corresponds to a given area of surface). Although a bit abstract, we can write the partition function as a sum over particles in the system <script type="math/tex">N</script> and microstates <script type="math/tex">\nu</script> with that particle number:</p> <script type="math/tex; mode=display">\Xi = \displaystyle \sum_{N=0}^{\infty} \displaystyle \sum_{\nu} e^{-\beta E_{\nu} + \beta \mu N}</script> <p>Here, <script type="math/tex">E_{\nu}</script> is the energy of that particular microstate <script type="math/tex">\nu</script>.</p> <p>The probability of a microstate <script type="math/tex">\nu</script> with <script type="math/tex">N</script> particles is then <script type="math/tex">e^{-\beta E_{\mu} + \beta \mu N} / \Xi</script>. From this and the expectation value formula from elementary probability theory, we can work out the relationship between the partition function and the average energy and particle number:</p> <script type="math/tex; mode=display">\langle E\rangle = - \frac{\partial \log \Xi}{\partial \beta}</script> <script type="math/tex; mode=display">\langle N\rangle = \frac{\partial \log \Xi}{\partial (\beta \mu)}</script> <p>These two relations give us a nice way to relate <script type="math/tex">\langle E\rangle</script> and <script type="math/tex">\langle N\rangle</script> through the partition function:</p> <script type="math/tex; mode=display">\frac{ \partial \langle E\rangle}{\partial (\beta \mu)} = - \frac{\partial }{\partial(\beta \mu)} \frac{\partial \log \Xi}{\partial \beta} = -\frac{\partial{\langle N\rangle}}{\partial \beta}.</script> <p>Looking back at our equation for the average energy of adsorption <script type="math/tex">q</script>, we bring this quantity into <script type="math/tex">q</script> using the chain rule:</p> <script type="math/tex; mode=display">q \approx \frac{\partial \langle E\rangle}{\partial \langle N \rangle} = \frac{\partial \langle E\rangle}{\partial (\beta \mu)} \frac{\partial (\beta \mu)}{\partial \langle N \rangle}.</script> <p>We immediately recognize the derivative on the right from the fluctuation theorem in statistical mechanics. Again, this can be seen from elementary probability theory by taking the derivative of <script type="math/tex">\langle N\rangle = \frac{\partial \log \Xi}{\partial (\beta \mu)}</script> once more and writing out the sum for the partition function.</p> <script type="math/tex; mode=display">\frac{\partial \langle N \rangle}{\partial (\beta \mu)}= \langle N ^2 \rangle - \langle N \rangle ^2 = \mathrm{Var}(N)</script> <p>As for the first derivative, we use the relationship we derived:</p> <script type="math/tex; mode=display">\frac{\partial \langle E\rangle}{\partial (\beta \mu)} = -\frac{\partial \langle N\rangle}{\partial \beta} = -\frac{\partial}{\partial \beta} \displaystyle \sum_{N=0}^{\infty} \displaystyle \sum_{\nu} N \frac{e^{-\beta E_{\nu} + \beta \mu N}}{\Xi}.</script> <p>Here, we wrote out the expectation of <script type="math/tex">N</script> from elementary probability theory using the probability of observing a given value of <script type="math/tex">N</script>, <script type="math/tex">p(N) = \displaystyle \sum_{\nu} \frac{e^{-\beta E_{\nu} + \beta \mu N}}{\Xi}</script>.</p> <p>Actually taking this derivative WRT <script type="math/tex">\beta</script> using the product rule (<script type="math/tex">\Xi</script> is a function of <script type="math/tex">\beta</script> too!),</p> <script type="math/tex; mode=display">-\frac{\partial}{\partial \beta} \displaystyle \sum_{N=0}^{\infty} \displaystyle \sum_{\nu} N \frac{e^{-\beta E_{\nu} + \beta \mu N}}{\Xi} = -\displaystyle \sum_{N=0}^{\infty} \displaystyle \sum_{\nu} \left (-N E_{\nu} \frac{e^{-\beta E_{\nu} + \beta \mu N}}{\Xi} - N \frac{e^{-\beta E_{\nu} + \beta \mu N}}{\Xi^2} \frac{\partial \Xi}{\partial \beta} \right).</script> <p>You may be wondering why we did not consider the <script type="math/tex">e^{\beta \mu N}</script> term during differentiation, but actually we are viewing <script type="math/tex">\Xi</script> as <script type="math/tex">\Xi(\beta, \beta \mu, V)</script> here.</p> <p>We can recognize the first sum as the expected value of <script type="math/tex">EN</script>, <script type="math/tex">\langle E N \rangle</script>. The second sum involves the term <script type="math/tex">\frac{1}{\Xi} \frac{\partial \Xi}{\partial \beta}=-\langle E \rangle</script>. Thus,</p> <script type="math/tex; mode=display">\frac{\partial \langle E\rangle}{\partial (\beta \mu)} = \langle E N \rangle - \langle E \rangle \langle N \rangle.</script> <p>Finally, we arrive at the average energy of adsoption as:</p> <script type="math/tex; mode=display">q = \frac{\langle E N \rangle - \langle E \rangle \langle N \rangle}{\mathrm{Var}(N)},</script> <p>which are all statistical quantities we can easily measure from samples during our Monte Carlo simulations. This formula is fascinating because it relates the average energy of adsorption to fluctuations in the particle number and the covariance of the energy and particle number (<script type="math/tex">\mathrm{Cov}(E,N)=\langle EN \rangle - \langle E \rangle \langle N \rangle</script>); this is another fluctuation theorem in statistical mechanics.</p> <p>The isosteric heat of adsorption follows from <script type="math/tex">q</script> by <script type="math/tex">q_{st}= -q + RT</script> since we need to account for the work to push the gas into the gas phase when it desorbs.</p> <p><a href="http://CorySimon.github.io/articles/hoa/">Computing the heat of adsorption in a grand canonical Monte Carlo simulation</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on January 17, 2016.</p> <![CDATA[Matplotlib markers]]> http://CorySimon.github.io/articles/matplotlib-markers 2015-12-03T00:00:00+00:00 2015-12-03T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>The description of possible markers on the Matplotlib description <a href="http://matplotlib.org/api/markers_api.html">here</a> does not show what each marker looks like, so I made the visualization below to aid in choosing a marker for scatter plots. The left column displays the marker; the right column shows the corresponding marker label in Matplotlib.</p> <p>e.g. the code below will use square markers.</p> <figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'s'</span><span class="p">)</span></code></pre></figure> <figure> <img src="/images/MatplotlibMarkers.png" alt="image" /> </figure> <p><a href="http://CorySimon.github.io/articles/matplotlib-markers/">Matplotlib markers</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on December 03, 2015.</p> <![CDATA[Bravais lattice and its reciprocal lattice]]> http://CorySimon.github.io/articles/bravais 2015-11-09T00:00:00+00:00 2015-11-09T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>The unit cell of many crystals, for example a metal-organic framework, can be described by three basis vectors <script type="math/tex">a_1</script>, <script type="math/tex">a_2</script>, and <script type="math/tex">a_3</script> that form a three-dimensional parallelpiped. One can conveniently express any point <script type="math/tex">x</script> inside the unit cell of the crystal as a linear combination of these vectors:</p> <p><script type="math/tex">x = f_1 a_1 + f_2 a_2 + f_3 a_3</script>,</p> <p>where <script type="math/tex">f_i \in [0, 1]</script> are <em>fractional coordinates</em>. For example, in two dimensions, the shaded gray area formed by the parallelogram of the two vectors <script type="math/tex">a_1</script> and <script type="math/tex">a_2</script> in Fig. 1 could describe a unit cell of a crystal.</p> <figure class="center"> <img src="/images/Bravais/2dversion.png" alt="image" /> <figcaption>Figure 1.</figcaption> </figure> <p>The point <script type="math/tex">x</script> in Fig. 1 can be described as <script type="math/tex">\frac{1}{2}a_1+ \frac{1}{2}a_2</script>.</p> <p>To mimick an infinite crystal during molecular simulations, we can consider only the home unit cell and impose periodic boundary conditions. For example, the electrostatic potential <script type="math/tex">\phi(x)</script> inside the pores of a metal-organic framework is a periodic function since we model the crystal as infinitely repeating. Using the basis vectors <script type="math/tex">a_1</script>, <script type="math/tex">a_2</script>, and <script type="math/tex">a_3</script>, we can express the periodicity of <script type="math/tex">\phi(x)</script> as:</p> <script type="math/tex; mode=display">\phi(x + n_1 a_1 + n_2 a_2 + n_3 a_3) = \phi(x),</script> <p>where <script type="math/tex">n_i\in \mathbb{Z}</script>, that is <script type="math/tex">n_i</script> are integers. Fig. 2 shows a point <script type="math/tex">x</script> in the home unit cell formed by <script type="math/tex">a_1</script> and <script type="math/tex">a_2</script> and its periodic images <script type="math/tex">x+n_1 a_1 + n_2 a_2</script> for our two-dimensional example (here we show <script type="math/tex">n_1\in\{-1,0,1\}</script> and <script type="math/tex">n_2\in\{-1,0,1\}</script>).</p> <figure class="center"> <img src="/images/Bravais/periodic.png" alt="image" /> <figcaption>Figure 2. A point in the home unit cell and its periodic images. </figcaption> </figure> <p>The points <script type="math/tex">n_1a_1 +n_2a_2+n_3a_3</script> for <script type="math/tex">n_i \in \mathbb{Z}</script> that form the corners of the unit cells are called a <em>Bravais lattice</em>.</p> <p>As the electrostatic potential <script type="math/tex">\phi(x)</script> has the same periodicity as the Bravais lattice describing the unit cell, we can expand it in a discrete Fourier series:</p> <script type="math/tex; mode=display">\phi(x)=\phi(f_1, f_2, f_3) = \displaystyle \sum_{n_1=-\infty}^{\infty} \displaystyle \sum_{n_2=-\infty}^{\infty} \displaystyle \sum_{n_3=-\infty}^{\infty}c(n_1, n_2, n_3) e^{2\pi i (n_1 f_1 + n_2 f_2 + n_3 f_3)},</script> <p>essentially viewing <script type="math/tex">\phi(x)</script> as a function of fractional coordinates <script type="math/tex">(f_1, f_2, f_3)</script>, which each have a periodicity of 1.0. The Fourier coefficients are denoted as <script type="math/tex">c(n_1, n_2, n_3)</script>.</p> <p>We can instead express the above Fourier series as a sum over vectors <script type="math/tex">k</script>:</p> <script type="math/tex; mode=display">\phi(x)= \displaystyle \sum_{k} c(k) e^{i k \cdot x},</script> <p>where vectors <script type="math/tex">k</script> are in the set:</p> <script type="math/tex; mode=display">k = n_1 b_1 + n_2 b_2 + n_3 b_3.</script> <p>The vectors <script type="math/tex">b_i</script> are <em>reciprocal lattice vectors</em>, defined as:</p> <script type="math/tex; mode=display">b_i \cdot a_j = 2 \pi \delta_{ij}.</script> <p>This way,</p> <script type="math/tex; mode=display">k \cdot x = (n_1 b_1 + n_2 b_2 + n_3 b_3) \cdot (f_1 a_1 + f_2 a_2 + f_3 a_3) = 2 \pi (n_1 f_1 + n_2 f_2 + n_3 f_3),</script> <p>as in the first time we wrote the Fourier series expansion of <script type="math/tex">\phi(x)</script>.</p> <p>We can obtain the reciprocal lattice vectors from a given Bravais lattice by solving the matrix equation <script type="math/tex">[a_1 a_2 a_3]^T [b_1 b_2 b_3] = 2\pi I</script>.</p> <p>In conclusion, functions that have the same periodicity as the Bravais lattice can be expressed as a Fourier series over the reciprocal lattice vectors.</p> <p><a href="http://CorySimon.github.io/articles/bravais/">Bravais lattice and its reciprocal lattice</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on November 09, 2015.</p> <![CDATA[An argument for basic research]]> http://CorySimon.github.io/articles/basic-research 2015-11-09T00:00:00+00:00 2015-11-09T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>On the Bill Maher show, Neil DeGrasse Tyson explains why funding <a href="https://en.wikipedia.org/wiki/Basic_research">basic research</a> is important:</p> <iframe width="420" height="315" src="https://www.youtube.com/embed/a0SUpwIAjQs?start=61" frameborder="0" allowfullscreen=""></iframe> <p><a href="http://CorySimon.github.io/articles/basic-research/">An argument for basic research</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on November 09, 2015.</p> <![CDATA[Water is the wrong focus for water conservation]]> http://CorySimon.github.io/articles/water-conservation 2015-10-24T00:00:00+00:00 2015-10-24T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>California is experiencing a severe drought. Governor Brown proclaimed a State of Emergency to manage and conserve our freshwater supply . The detrimental impacts of the drought include an increased frequency and severity of forest fires , disrupted ecosystems , diminished drinking water quality , and a suffering economy. As the largest component of the latter, consider that agriculture in California raked in \$54 billion in 2014 . These farms need water to grow their crops; less rainfall combined with scarce and consequently expensive freshwater drive up the price to produce a given crop. This results in rising food prices for consumers — even outside of California.</p> <blockquote> California’s agricultural abundance includes more than 400 commodities. The state produces nearly half of US-grown fruits, nuts and vegetables. Across the nation, US consumers regularly purchase several crops produced solely in California. <br /> - California Department of Food and Agriculture </blockquote> <p>It is thus imperative to conserve freshwater. The average person in the US incurs a water footprint of 2,000 gallons of water per day . This number seems incredulously large at first thought. Given the flow rate of a typical shower head, 2.5 gallons per minute, this amounts to taking a ~14 hour shower daily. But, if we fixate on the water that we lucidly see flowing in our homes — through our sink, toilet, and shower— we only consider a small fraction of our individual water footprint (3%). By far, the largest share of our water footprint implicitly comes from the food that we eat. “Virtual water.” Much of this food is grown in drought-stricken California.</p> <figure class="center"> <img src="/images/water_conservation/wateruse.png" alt="image" /> <figcaption>This pie chart shows the percentage of the global water footprint attributed to industrial products (4%), domestic consumption (3%), and agriculture (92%). Examples: using a disposable coffee cup, washing dishes, and eating a hamburger, respectively. </figcaption> </figure> <p>Following our discussion about the economic impact of the drought on agriculture, in hindsight, it is not surprising that most of our water footprint comes from the food that we eat. Collectively, as consumers, our demand for food drives the demand of the agricultural sector for water.</p> <p>The pie chart above shows that, as individual consumers, if we want to take the most significant measure to reduce our water footprint, we should focus less on how long we spend in the shower and more on the food that we eat. The easiest and most impactful effort to conserve water is to refrain from wasting food.</p> <p>The next effort we can make to conserve water is in our choice of foods that we eat. Different foods incur different water footprints. <a href="http://waterfootprint.org/en/resources/interactive-tools/product-gallery/">This product gallery</a> compares how much water is required to produce different foods and drinks. Note the trend that meat generally requires much more water than fruits, vegetables, and grains. By far one of the most water-intensive foods is beef, requiring 1,850 gallons of water per pound. Beef incurs such a large water footprint because we have to grow the feed for the cow and give it water to drink for its lifetime; raising a cow is a very inefficient process to put calories on our plate compared to e.g. directly eating the corn and soybeans that we feed the cow. Generally, following this reasoning, eating less meat can significantly reduce our water footprint.</p> <p>On a mass basis, chicken is more water-efficient than beef by a factor of 3.5. Consider substituting chicken for beef.</p> <p>Of course, many factors together determine the water footprint of a given food, leading to large variations in the water footprint of a given food, e.g. depending on the region in which the crop is grown. Furthermore, growing crops in a region with a large amount of natural rainfall reduces required irrigation from rivers, lakes, and reservoirs. Unfortunately, much of our nation’s crops are grown in drought-stricken California.</p> <p>My motivation for writing this article came while eating at a Thai restaurant in San Francisco. Instead of automatically refilling my empty glass of water, a card on the table (see cover image) encourages water refills only by request. Albeit an admirable intention, this will have a much less significant impact on our water footprint than if the focus were on the food beside the glass of water. Instead, I suggest a sign that reads:</p> <ul> <li>Save your leftover food for tomorrow’s lunch instead of throwing it in the trash.<sup>1</sup></li> <li>Try omitting meat.</li> <li>Consider substituting chicken for beef.</li> </ul> <p>The above are my conclusions.</p> <p>Finally, I must editorialize that unnecessary water use, such as watering urban lawns, is irking. Brown is the new green!</p> <hr /> <p>An engaging, interactive visualization of our water footprint: <a href="http://www.angelamorelli.com/water/">here</a>.</p> <p>More data visualizations regarding water footprints of food and drinks are on <a href="http://corysimon.github.io/articles/water/">my blog</a>, but the above is better.</p> <p>A detail: note in the product gallery that we can distinguish between green water, blue water, and gray water in the water footprint of a given product. See a description on Wikipedia <a href="https://en.wikipedia.org/wiki/Water_use#Water_Footprint_Network_.28WFN.29">here</a>.</p> <p><sup>1</sup> I imagine that the production of the disposable plastic container that you use to take your food home consumes less water than to grow/produce your leftover food.</p> <h3 id="references">References</h3> <p> <a href="http://ca.gov/drought/">http://ca.gov/drought/</a></p> <p> (a) <a href="http://www.nytimes.com/interactive/2015/07/15/us/california-fire-season-drought.html?_r=0">http://www.nytimes.com/interactive/2015/07/15/us/california-fire-season-drought.html?_r=0</a>; (b) Dale, V. H., Joyce, L. A., McNulty, S., Neilson, R. P., Ayres, M. P., Flannigan, M. D., … &amp; Michael Wotton, B. (2001). Climate change and forest disturbances: climate change can affect forests by altering the frequency, intensity, duration, and timing of fire, drought, introduced species, insect and pathogen outbreaks, hurricanes, windstorms, ice storms, or landslides. BioScience, 51(9), 723–734.</p> <p> <a href="http://www.nature.com/news/native-ecosystems-blitzed-by-drought-1.15707">http://www.nature.com/news/native-ecosystems-blitzed-by-drought-1.15707</a></p> <p> Centers for Disease Control and Prevention. <a href="http://www.cdc.gov/features/drought/">http://www.cdc.gov/features/drought/</a>; My personal testimony in Berkeley: <a href="http://www.berkeleyside.com/2015/03/30/berkeley-residents-complain-about-horrible-water-taste/">http://www.berkeleyside.com/2015/03/30/berkeley-residents-complain-about-horrible-water-taste/</a></p> <p> California Department of Food and Agriculture. <a href="https://www.cdfa.ca.gov/statistics/">https://www.cdfa.ca.gov/statistics/</a></p> <p> Hoekstra, A. Y., &amp; Mekonnen, M. M. (2012). The water footprint of humanity.Proceedings of the national academy of sciences, 109(9), 3232–3237.</p> <p><a href="http://CorySimon.github.io/articles/water-conservation/">Water is the wrong focus for water conservation</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on October 24, 2015.</p> <![CDATA[Accelerating Materials Discovery with CUDA]]> http://CorySimon.github.io/articles/cuda-materials 2015-10-12T00:00:00+00:00 2015-10-12T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>See the post on NVidia’s blog <a href="http://devblogs.nvidia.com/parallelforall/accelerating-materials-discovery-cuda/">here</a>.</p> <p><a href="http://CorySimon.github.io/articles/cuda-materials/">Accelerating Materials Discovery with CUDA</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on October 12, 2015.</p> <![CDATA[Honey Bees and Materials Science]]> http://CorySimon.github.io/articles/bees-mof74 2015-09-17T00:00:00+00:00 2015-09-17T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>:honeybee: :honeybee: :honeybee: :honeybee:</p> <p>Honey bees build honeycombs of wax that exhibit a hexagonal tiling. Why hexagons? As this Ted Ed video explains, the hexagonal structure utilizes space most efficiently and uses the minimal amount of wax to create a given number of columns per volume.</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/QEzlsjAqADA" frameborder="0" allowfullscreen=""></iframe> <p>In materials science, the crystal structure of a well-studied nanoporous material, MOF-74 , resembles a honeycomb; the structure consists of 1-dimensional channels with a hexagonal cross-section.</p> <figure class="center"> <img src="/images/mof74/mof74.png" alt="image" /> </figure> <p>The crystal structure of MOF-74 constructs a surface that, like the honeycomb, most efficiently utilizes a volume to create a given number of channels, while minimizing the amount of material required.</p> <p>Instead of storing honey, MOF-74 can be used to store or separate gas molecules. For example, MOF-74 is a promising candidate to selectively capture the greenhouse gas carbon dioxide from the exhaust of coal-fired power plants . The carbon dioxide can then be compressed and pumped underground to be stored in geological formations, where it cannot instigate climate change.</p> <figure class="center"> <img src="/images/mof74/mof74_bee.png" alt="image" /> </figure> <p> Rosi, N. L., Kim, J., Eddaoudi, M., Chen, B., O’Keeffe, M., &amp; Yaghi, O. M. (2005). Rod packings and metal-organic frameworks constructed from rod-shaped secondary building units. Journal of the American Chemical Society, 127(5), 1504-1518.</p> <p> Britt, D., Furukawa, H., Wang, B., Glover, T. G., &amp; Yaghi, O. M. (2009). Highly efficient separation of carbon dioxide by a metal-organic framework replete with open metal sites. Proceedings of the National Academy of Sciences, 106(49), 20637-20640.</p> <p><a href="http://CorySimon.github.io/articles/bees-mof74/">Honey Bees and Materials Science</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on September 17, 2015.</p> <![CDATA[Computer-aided search for materials to store natural gas for vehicles]]> http://CorySimon.github.io/articles/methane-storage 2015-08-31T00:00:00+00:00 2015-08-31T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>We wrote an article in a scientific journal for kids, Frontiers for Young Minds, describing our research on using sponge-like materials to store natural gas onboard vehicles. The article is a nice read for adults as well!</p> <p>You can read it here: <a href="http://kids.frontiersin.org/article/10.3389/frym.2015.00011">http://kids.frontiersin.org/article/10.3389/frym.2015.00011</a></p> <figure class="center"> <img src="/images/fig2_beaker.png" alt="image" /> </figure> <p>This is a translation of the Perspective in Energy &amp; Environmental Science here: <a href="http://pubs.rsc.org/en/content/articlehtml/2015/ee/c4ee03515a">http://pubs.rsc.org/en/content/articlehtml/2015/ee/c4ee03515a</a></p> <figure class="center"> <img src="/images/eescover.png" alt="image" /> </figure> <p><a href="http://CorySimon.github.io/articles/methane-storage/">Computer-aided search for materials to store natural gas for vehicles</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on August 31, 2015.</p> <![CDATA[How a parameter affects a function in the IJulia Notebook]]> http://CorySimon.github.io/articles/interact-in-julia 2015-08-18T00:00:00+00:00 2015-08-18T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>This blog post illustrates a simple way to visualize how the shape of a function changes as we tune a parameter in the function. We use the <a href="https://github.com/JuliaLang/IJulia.jl">IJulia Notebook</a> with the <a href="https://github.com/dcjones/Gadfly.jl">Gadfly</a> and <a href="https://github.com/JuliaLang/Interact.jl">Interact</a> packages.</p> <p>As an example, the Langmuir equation is a ubiquitous model in chemical engineering to describe the adsorption of molecules on a surface as a function of concentration in the bulk phase. The Langmuir equation is:</p> <script type="math/tex; mode=display">N(P) = M \frac{KP}{1+KP}.</script> <p>The variables in this function <script type="math/tex">N(P)</script> are:</p> <ul> <li><script type="math/tex">N</script>: the number of molecules adsorbed on the surface</li> <li><script type="math/tex">P</script>: pressure of gas phase in contact with the surface</li> </ul> <p>The parameters in the funtion <script type="math/tex">N(P)</script> are:</p> <ul> <li><script type="math/tex">M</script>: the total number of adsorption sites on the surface</li> <li><script type="math/tex">K</script>: Langmuir constant describing affinity of the molecule for the surface</li> </ul> <p>We are interested in how the parameters <script type="math/tex">M</script> and <script type="math/tex">K</script> affect the shape of the Langmuir equation <script type="math/tex">N(P)</script>. Using Gadfly, we start by plotting <script type="math/tex">N(P)</script> for a given value of <script type="math/tex">K</script> and <script type="math/tex">M</script>.</p> <figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">using</span> <span class="n">Gadfly</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="c"># make a pretty theme</span><br data-jekyll-commonmark-ghpages="" /><span class="n">plot_theme</span> <span class="o">=</span> <span class="n">Theme</span><span class="x">(</span><span class="n">line_width</span><span class="o">=</span><span class="mi">1</span><span class="n">mm</span><span class="x">,</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">default_color</span><span class="o">=</span><span class="n">color</span><span class="x">(</span><span class="s">"green"</span><span class="x">),</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">panel_fill</span><span class="o">=</span><span class="n">color</span><span class="x">(</span><span class="s">"lightgray"</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">grid_color</span><span class="o">=</span><span class="n">color</span><span class="x">(</span><span class="s">"white"</span><span class="x">));</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="c"># create an array of pressures</span><br data-jekyll-commonmark-ghpages="" /><span class="n">P</span> <span class="o">=</span> <span class="n">linspace</span><span class="x">(</span><span class="mi">0</span><span class="x">,</span> <span class="mi">1</span><span class="x">)</span> <span class="c"># bar</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="c"># define parameters</span><br data-jekyll-commonmark-ghpages="" /><span class="n">M</span> <span class="o">=</span> <span class="mf">3.0</span> <span class="c"># (mmol/g)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">K</span> <span class="o">=</span> <span class="mf">5.0</span> <span class="c"># (1/bar)</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="c"># plot N(P)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">plot</span><span class="x">(</span><span class="n">x</span><span class="o">=</span><span class="n">P</span><span class="x">,</span> <span class="n">y</span><span class="o">=</span><span class="n">M</span><span class="o">*</span><span class="n">K</span><span class="o">*</span><span class="n">P</span><span class="o">./</span><span class="x">(</span><span class="mi">1</span><span class="o">+</span><span class="n">K</span><span class="o">*</span><span class="n">P</span><span class="x">),</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">Geom</span><span class="o">.</span><span class="n">line</span><span class="x">,</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">Scale</span><span class="o">.</span><span class="n">y_continuous</span><span class="x">(</span><span class="n">maxvalue</span><span class="o">=</span><span class="mi">3</span><span class="x">),</span> <span class="n">Scale</span><span class="o">.</span><span class="n">x_continuous</span><span class="x">(</span><span class="n">maxvalue</span><span class="o">=</span><span class="mi">1</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">Guide</span><span class="o">.</span><span class="n">xlabel</span><span class="x">(</span><span class="s">"P (bar)"</span><span class="x">),</span> <span class="n">Guide</span><span class="o">.</span><span class="n">ylabel</span><span class="x">(</span><span class="s">"N (mmol/g)"</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">Guide</span><span class="o">.</span><span class="n">title</span><span class="x">(</span><span class="s">"Langmuir isotherm"</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">plot_theme</span><span class="x">)</span></code></pre></figure> <figure> <img src="/images/interact/ex.png" alt="image" /> </figure> <p>To get an idea of how <script type="math/tex">M</script> and <script type="math/tex">K</script> affect <script type="math/tex">N(P;K,M)</script>, we could manually change <script type="math/tex">M</script> and <script type="math/tex">K</script> in the above code and plot <script type="math/tex">N(P;K,M)</script> several times. With the Julia package Interact, there is a much nicer way. The Interact package creates a slider bar with the parameters <script type="math/tex">M</script> and <script type="math/tex">K</script> so that we can interact with the Gadfly plot and see how <script type="math/tex">N(P;K,M)</script> changes somewhat continuously.</p> <p>The <code class="highlighter-rouge">@manipulate</code> macro in the Interact package creates the interactive plot with sliders to change the parameters. We can choose the parameter space of <script type="math/tex">M</script> and <script type="math/tex">K</script> to explore in the interactive plot. The code below explores <script type="math/tex">K \in [.1,100]</script> and <script type="math/tex">M \in [1,3]</script> in increments of 0.1.</p> <figure class="highlight"><pre><code class="language-julia" data-lang="julia"><span class="n">using</span> <span class="n">Gadfly</span><br data-jekyll-commonmark-ghpages="" /><span class="n">using</span> <span class="n">Interact</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="nd">@manipulate</span> <span class="k">for</span> <span class="n">K</span> <span class="o">=</span> <span class="o">.</span><span class="mi">1</span><span class="x">:</span><span class="o">.</span><span class="mi">1</span><span class="x">:</span><span class="mi">100</span><span class="x">,</span> <span class="n">M</span> <span class="o">=</span> <span class="mf">1.</span><span class="x">:</span><span class="o">.</span><span class="mi">1</span><span class="x">:</span><span class="mf">3.</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">plot</span><span class="x">(</span><span class="n">x</span><span class="o">=</span><span class="n">P</span><span class="x">,</span> <span class="n">y</span><span class="o">=</span><span class="n">M</span><span class="o">*</span><span class="n">K</span><span class="o">*</span><span class="n">P</span><span class="o">./</span><span class="x">(</span><span class="mi">1</span><span class="o">+</span><span class="n">K</span><span class="o">*</span><span class="n">P</span><span class="x">),</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">Geom</span><span class="o">.</span><span class="n">line</span><span class="x">,</span> <br data-jekyll-commonmark-ghpages="" /> <span class="n">Scale</span><span class="o">.</span><span class="n">y_continuous</span><span class="x">(</span><span class="n">maxvalue</span><span class="o">=</span><span class="mi">3</span><span class="x">),</span> <span class="n">Scale</span><span class="o">.</span><span class="n">x_continuous</span><span class="x">(</span><span class="n">maxvalue</span><span class="o">=</span><span class="mi">1</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">Guide</span><span class="o">.</span><span class="n">xlabel</span><span class="x">(</span><span class="s">"P (bar)"</span><span class="x">),</span> <span class="n">Guide</span><span class="o">.</span><span class="n">ylabel</span><span class="x">(</span><span class="s">"N (mmol/g)"</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">Guide</span><span class="o">.</span><span class="n">title</span><span class="x">(</span><span class="s">"Langmuir isotherm"</span><span class="x">),</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">plot_theme</span><span class="x">)</span><br data-jekyll-commonmark-ghpages="" /><span class="k">end</span></code></pre></figure> <p>A screenshot of the result is below.</p> <figure> <img src="/images/interact/interactive.png" alt="image" /> </figure> <p>In the IJulia Notebook, we can play with the sliders to gain insight into how the parameters <script type="math/tex">K</script> and <script type="math/tex">M</script> affect the shape of the Langmuir equation. The IJulia notebook with this example is <a href="https://github.com/CorySimon/CorySimon.github.io/blob/master/notebooks/Langmuir.ipynb">here</a>.</p> <p>At a high enough pressure, <script type="math/tex">N \rightarrow M</script> as the adsorption sites become filled with molecules. Intuitively, <script type="math/tex">M</script> determines the value of <script type="math/tex">N</script> at large pressures.</p> <p>The Langmuir constant <script type="math/tex">K</script> determines the initial slope of the adsorption curve at low pressures <script type="math/tex">P</script> for a given <script type="math/tex">M</script>. In other words, it determines the pressure <script type="math/tex">P</script> at which the adsorption curve starts to saturate. If we note that <script type="math/tex">N(P=K^{-1})=\frac{1}{2}M</script>, <script type="math/tex">K</script> is inversely related to the pressure at which the adsorption sites are half-filled with molecules. Molecules with a high affinity with the surface will fill the adsorption sites at a relatively low pressure <script type="math/tex">P</script>; their adsorption curve will exhibit a high slope at low pressures. Therefore, a high affinity of the adsorbing molecule for the surface implies a high <script type="math/tex">K</script> in the Langmuir model.</p> <p><a href="http://CorySimon.github.io/articles/interact-in-julia/">How a parameter affects a function in the IJulia Notebook</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on August 18, 2015.</p> <![CDATA[We eat most of the water that we consume]]> http://CorySimon.github.io/articles/water 2015-05-02T00:00:00+00:00 2015-05-02T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>In the US, water is thought of as a cheap and abundant resource. The current drought in California has challenged this mindset and reminded us that a relatively local supply of water is imperative to sustain a prosperous economy. In the face of climate change, natural variations in weather, and a growing population, everyone needs to be more cognizant about our water supply and use.</p> <p>The average person in the US uses 2,057 gallons of fresh water per day. Given the flow rate of a typical shower head, 2.5 gallons per minute, this amounts to taking a ~14 hour shower!</p> <p>However, we do not see most of the water that we consume each day flowing through the pipes in our homes. Below is a Sankey diagram showing the breakdown of our water use. Domestic consumption – the water we use to shower, flush the toilet, cook, do laundry, etc. – accounts for only 4% of our water use. We also <em>implicitly</em> use water when we, for example, use a paper cup, since it took water to produce it. The water used to produce the industrial products that we use accounts for 12% of our water footprint. The rest, and by far the largest fraction, is from agriculture.</p> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization", "1.1", {packages:["sankey", "corechart", "bar"]}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('string', 'From'); data.addColumn('string', 'To'); data.addColumn('number', 'Gallons/day/person'); data.addRows([ [ 'Water', 'Domestic consumption', 80. ], [ 'Water', 'Industrial products', 242. ], [ 'Water', 'Agriculture', 1735. ] ]); // Mekonnen, M.M. and Hoekstra, A.Y. (2011) National water footprint accounts: The green, blue and grey water footprint of production and consumption, Value of Water Research Report Series No. 50, UNESCO-IHE, Delft, the Netherlands. // Sets chart options. var options = { title: 'US water footprint per capita', width: 600, sankey: { node: { label: { fontSize:16 } } }, }; // Instantiates and draws our chart, passing in some options. var chart = new google.visualization.Sankey(document.getElementById('sankey_basic')); chart.draw(data, options); } </script> </head> <body> <div id="sankey_basic" style="width: 900px; height: 300px;"></div> </body> </html> <p>Consider the question “How much water does it take to make 1 cup of coffee?”. The graphic below shows that the water that you see in the cup of coffee is only a small fraction of the water used to grow, process, and transport the coffee beans. Get ready to scroll! Each drop represents 1 cup of water.</p> <p class="center"><img src="/images/coffee_viz.jpg" align="middle" alt="" width="600" /></p> <p>Thus, for water conservation efforts, we should think less about shortening our shower and more about the food that we waste and the type of foods that we choose to eat. Some foods take more water to produce than others. The bar chart below shows much water it takes to produce different foods on a mass basis. Eating meat invokes the largest water footprint. Water is needed to grow the crops to feed the farm animals, which is inefficient since we could use this water to grow crops that we can eat directly. Further, we need to give the animals water to drink and wash them.</p> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.setOnLoadCallback(drawBasic); function drawBasic() { var data = google.visualization.arrayToDataTable([ ['Product', 'Water to produce (gallon/lb)'], ['Chocolate', 2060], ['Beef', 1847], ['Sheep meat', 1247], ['Pork', 717], ['Chicken meat', 518], ['Cheese', 381], ['Rice', 299], ['Dry pasta', 222], ['Sugar (cane)', 214], ['Wheat bread', 192], ['Corn', 146], ['Apples', 98], ['Bananas', 95], ['Oranges', 67], ['Potatoes', 34], ['Tomato', 25] ]); var options = { title: 'Water footprints of food', chartArea: {width: '50%'}, legend: {position: 'none'}, colors: ['green'], hAxis: { title: 'Water footprint (gal/lb)', minValue: 0 }, vAxis: { title: 'Food Product' } }; var chart = new google.visualization.BarChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <div id="chart_div" style="width: 900px; height: 800px;"></div> </body> </html> <p>In summary, most of the water that we consume day-to-day, we do not see. It is hidden in the food that we eat and industrial products that we use. Different foods take different amounts of water to produce. Meat has the largest water footprint.</p> <p>The data in the Sankey diagram is from: Mekonnen, M.M. and Hoekstra, A.Y. (2011) National water footprint accounts: The green, blue and grey water footprint of production and consumption, Value of Water Research Report Series No. 50, UNESCO-IHE, Delft, the Netherlands.</p> <p>The data in the bar plot is from: Mekonnen, M.M. and Hoekstra, A.Y. (2011) The green, blue and grey water footprint of crops and derived crop products, Hydrology and Earth System Sciences, 15(5): 1577-1600.</p> <p>An interactive gallery of the water footprint of different products and more information can be found at <a href="http://waterfootprint.org/en/resources/interactive-tools/product-gallery/">waterfootprint.org</a>.</p> <p><a href="http://CorySimon.github.io/articles/water/">We eat most of the water that we consume</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on May 02, 2015.</p> <![CDATA[Using standard atom colors in Matplotlib]]> http://CorySimon.github.io/articles/atom-coloring-in-matplotlib 2015-03-29T00:00:00+00:00 2015-03-29T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>Imagine we are interested in the following chemical structure, which is composed of atom types oxygen (O), hydrogen (H), and carbon (C).</p> <p class="center"><img src="/images/atomcolors/linker.jpeg" align="middle" alt="" height="100" width="100" /></p> <p>The colors of the atoms in the structure visualization, which I made in <a href="https://wci.llnl.gov/simulation/computer-codes/visit/">VisIt visualization tool</a>, correspond to the standard colors used in <a href="http://jmol.sourceforge.net/">JMol</a>. A data visualization regarding the atom types in this chemical structure should utilize the corresponding colors for consistency.</p> <p>As a toy example, say we desire a bar plot that counts the number of atom types in the structure. We can make this in Python’s plotting library Matplotlib. What follows is a tutorial of how to make the colors in a Matplotlib visualization correspond to the colors in JMol. The final product:</p> <p class="center"><img src="/images/atomcolors/toy.png" align="middle" alt="" height="450" width="450" /></p> <p>A list of the RGB (red, green, blue) tuples that are used for the color of each atom is on the <a href="http://jmol.sourceforge.net/jscolors/">JMol website</a>. I put a .csv of these colors on <a href="https://github.com/CorySimon/JMolColors">GitHub</a>.</p> <p>Using Pandas to load these RGB tuples into a DataFrame, I wrote a function that takes an atom type as an input and returns an RGB tuple that we will send to the plotting library Matplotlib:</p> <figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span><br data-jekyll-commonmark-ghpages="" /><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span><br data-jekyll-commonmark-ghpages="" /><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="c"># d/l here: https://github.com/CorySimon/JMolColors</span><br data-jekyll-commonmark-ghpages="" /><span class="n">df_colors</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s">"jmolcolors.csv"</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="k">def</span> <span class="nf">get_color</span><span class="p">(</span><span class="n">atom</span><span class="p">):</span><br data-jekyll-commonmark-ghpages="" /> <span class="s">"""<br data-jekyll-commonmark-ghpages="" /> Returns RGB color tuple corresponding to atom<br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /> :param str atom: symbol for atom<br data-jekyll-commonmark-ghpages="" /> """</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">r</span> <span class="o">=</span> <span class="n">df_colors</span><span class="p">[</span><span class="n">df_colors</span><span class="p">[</span><span class="s">'atom'</span><span class="p">]</span> <span class="o">==</span> <span class="n">atom</span><span class="p">][</span><span class="s">'R'</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="mf">255.0</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">g</span> <span class="o">=</span> <span class="n">df_colors</span><span class="p">[</span><span class="n">df_colors</span><span class="p">[</span><span class="s">'atom'</span><span class="p">]</span> <span class="o">==</span> <span class="n">atom</span><span class="p">][</span><span class="s">'G'</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="mf">255.0</span><br data-jekyll-commonmark-ghpages="" /> <span class="n">b</span> <span class="o">=</span> <span class="n">df_colors</span><span class="p">[</span><span class="n">df_colors</span><span class="p">[</span><span class="s">'atom'</span><span class="p">]</span> <span class="o">==</span> <span class="n">atom</span><span class="p">][</span><span class="s">'B'</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="mf">255.0</span><br data-jekyll-commonmark-ghpages="" /> <span class="k">return</span> <span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><br data-jekyll-commonmark-ghpages="" /><span class="n">get_color</span><span class="p">(</span><span class="s">"C"</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><span class="c"># returns the RGB color tuple for carbon (0.564, 0.564, 0.564)</span></code></pre></figure> <p>I created the data that we seek to visualize regarding atom counts:</p> <figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">df_toy</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span><br data-jekyll-commonmark-ghpages="" /><span class="n">df_toy</span><span class="p">[</span><span class="s">"Atoms"</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s">"C"</span><span class="p">,</span> <span class="s">"H"</span><span class="p">,</span> <span class="s">"O"</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">df_toy</span><span class="p">[</span><span class="s">"Counts"</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">8</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span> <span class="c"># atom counts</span></code></pre></figure> <p>To make the bar plot, we need to define the array of colors corresponding to the atoms in <code class="highlighter-rouge">df_toy</code>. We can do this with a list comprehension calling <code class="highlighter-rouge">get_color()</code>:</p> <figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="n">get_color</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">df_toy</span><span class="p">[</span><span class="s">'Atoms'</span><span class="p">]]</span></code></pre></figure> <p>Now, <code class="highlighter-rouge">colors</code> is a list of RGB color tuples corresponding to <code class="highlighter-rouge">C</code>, <code class="highlighter-rouge">H</code>, and <code class="highlighter-rouge">O</code>. We can pass this list of colors to Matplotlib to make the bar plot shown above:</p> <figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span><br data-jekyll-commonmark-ghpages="" /><span class="n">ind</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df_toy</span><span class="p">[</span><span class="s">'Atoms'</span><span class="p">]))</span><br data-jekyll-commonmark-ghpages="" /><span class="n">ax</span><span class="o">.</span><span class="n">bar</span><span class="p">(</span><span class="n">ind</span> <span class="o">+</span> <span class="mf">0.25</span><span class="p">,</span> <span class="n">df_toy</span><span class="p">[</span><span class="s">'Counts'</span><span class="p">],</span> <span class="mf">0.5</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">colors</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s">'k'</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">ind</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'Count'</span><span class="p">)</span><br data-jekyll-commonmark-ghpages="" /><span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">df_toy</span><span class="p">[</span><span class="s">'Atoms'</span><span class="p">])</span><br data-jekyll-commonmark-ghpages="" /><span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span><br data-jekyll-commonmark-ghpages="" /><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></code></pre></figure> <p><a href="http://CorySimon.github.io/articles/atom-coloring-in-matplotlib/">Using standard atom colors in Matplotlib</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on March 29, 2015.</p> <![CDATA[How big is the tip of the iceberg?]]> http://CorySimon.github.io/articles/tip-of-the-icerberg 2015-03-08T00:00:00+00:00 2015-03-08T00:00:00+00:00 Cory Simon http://CorySimon.github.io CoryMSimon@gmail.com <p>The phrase ‘tip of the iceberg’ originates from the fact that most of an iceberg floating in the ocean is <em>beneath</em> the surface of the water. With a back-of-the-envelope calculation, we can estimate what fraction of a free-floating iceberg is above the surface.</p> <p>Consider when an object (the iceberg) is immersed in a fluid (the salty water of the ocean). Gravity exerts a downward force on the object. As not all objects sink, an opposing force on the object is at work. This force that is strong enough to cause some objects to float is called a <em>buoyant force</em>.</p> <p>A buoyant force is the force exerted on the immersed object <em>by</em> the fluid, in the opposite direction of gravity. Actually, this force is a consequence of gravity too– the exertion of gravity on the <em>fluid</em>. Gravity pulls down on the molecules of the fluid; molecules deep in the fluid can “feel” the weight of the molecules on top of them. As a consequence, the <em>pressure</em> in the fluid increases with depth. You can feel the higher pressure on your ears when you swim to the bottom of a swimming pool. Similarly, as the air is a fluid, your ears pop because of the lesser pressures at high elevations, like in an airplane or on a mountain.</p> <p>How does this increase of pressure with depth give rise to the buoyant force? Imagine that we tie a green marble to a thread and suspend it in a glass of water:</p> <p class="center"><img src="/images/iceberg/object.png" align="middle" alt="" height="250" width="250" /></p> <p>The fluid would exert a force on the marble at every point on its surface in a direction perpendicular to the surface (red arrows)– this is the pressure of the fluid at work. However, the pressure at the bottom of the fluid is <em>greater</em> than the pressure at the top of the fluid. As a consequence, the fluid is exerting a net upward force on the object. The arrows at the bottom are thicker than the arrows at the top to represent the magnitude of the force at the greater depth to be larger. Whether an object will float or sink depends on how the magnitude of the buoyant force compares with the downward gravitational force on the object.</p> <p><em>Archimedes’ principle</em> quantifies the strength of this buoyant force, and it depends only upon the volume of fluid that the object displaces when it is submerged (partially or fully). I derive Archimedes’ principle at the end.</p> <blockquote> <p>An object that is fully or patially submerged in a fluid experiences an upward force by the fluid equal to the weight of the fluid that it displaces.</p> <p>-- Archimedes’ Principle</p> </blockquote> <p>Back to the iceberg.</p> <p>The <a href="http://en.wikipedia.org/wiki/Ice">density of ice</a> is <script type="math/tex">\rho_{ice} \approx 917</script> kg/m<sup>3</sup>, and the <a href="http://en.wikipedia.org/wiki/Seawater">density of salty seawater</a> is around <script type="math/tex">\rho_{seawater} \approx 1025</script> kg/m<sup>3</sup>. Since seawater has a higher density than ice, the weight of the ice is less than that of the equivalent volume of seawater; Archimedes’ Principle tells us that an iceberg will float, then, since the buoyant force on the iceberg when fully submerged is greater than the weight of the iceberg.</p> <p>Let’s finally calculate the fraction of the iceberg that will be submerged when it floats. The iceberg will be at equilibrium when the buoyant force by the seawater is equal to the gravitational force on the iceberg itself. Let <script type="math/tex">V</script> be the volume of the iceberg and <script type="math/tex">\eta</script> be the fraction of the iceberg that is beneath the surface, for which we will solve. So <script type="math/tex">\eta V</script> is the volume of iceberg that is submerged.</p> <p>The force due to gravity on the iceberg is <script type="math/tex">\rho_{ice}V g</script>, its mass <script type="math/tex">\rho_{ice}V</script> times the acceleration due to gravity <script type="math/tex">g=9.8</script> m/s<script type="math/tex">^2</script>. The buoyant force on the iceberg is <script type="math/tex">\rho_{seawater} \eta V g</script>, since this is the weight of the seawater displaced by the iceberg with our submerged volume of ice <script type="math/tex">\eta V</script>.</p> <p>Equating these two forces and solving for <script type="math/tex">\eta</script>, we get the fraction of the iceberg that is submerged as the ratio of the density of ice to the density of seawater.</p> <script type="math/tex; mode=display">\eta = \rho_{ice} / \rho_{seawater} = 0.89</script> <p><strong>So 89% of a free-floating iceberg is submerged beneath the surface of the ocean; we only see 11% of it above the surface of the ocean.</strong> That the tip of the iceberg is just 11% is a result of a simple calculation.</p> <p>As an intuition check, if we instead consider an object with a density larger than that of seawater, we would get <script type="math/tex">\eta >1</script>, indicating that the object would sink since we can’t submerge more than 100% of an object!</p> <h2 id="mathematical-derivation-of-archimedes-principle">Mathematical derivation of Archimedes’ Principle</h2> <p class="center"><img src="/images/iceberg/noobject.png" align="middle" alt="" height="250" width="250" /></p> <p>Consider a hydrostatic fluid with a pressure <script type="math/tex">P(x,y,z)</script> and a closed volume <script type="math/tex">\Omega</script> of fluid, drawn with dashed lines in the figure above. The pressure increases with depth <script type="math/tex">z</script> according to the following equation:</p> <script type="math/tex; mode=display">P(x,y,z) = \rho g z.</script> <p>Here, <script type="math/tex">\rho</script> is the density of the fluid.</p> <p>As pressure acts normal to the surface of the volume, the net buoyant force on the volume of fluid by the surrounding fluid is a surface integral over the boundary <script type="math/tex">\partial \Omega</script> of the volume:</p> <script type="math/tex; mode=display">\mathbf{F} = - \int \int_{|\partial \Omega| } P \mathbf{n} dS</script> <p>Here, <script type="math/tex">\mathbf{n}</script> is the outward unit normal vector to the surface; the negative sign is to account for pressure acting in the opposite direction of <script type="math/tex">\mathbf{n}</script>. Gauss’s divergence theorem relates this surface integral to the volume integral:</p> <script type="math/tex; mode=display">\mathbf{F} = - \int \int_{|\partial \Omega| } P \mathbf{n} dS = -\int \int \int_{\Omega} \mathbf{\nabla} P dV.</script> <p>Now, we are integrating over the entire volume <script type="math/tex">\Omega</script>. Using the equation for <script type="math/tex">P(x,y,z)</script> above, <script type="math/tex">% <![CDATA[ \mathbf{\nabla} P = <0, 0, \rho g> %]]></script> and the integral over the volume becomes:</p> <script type="math/tex; mode=display">% <![CDATA[ F = <0, 0, -\rho g \int \int \int_{\Omega} dV>, %]]></script> <p>if the density <script type="math/tex">\rho</script> of the fluid is spatially uniform. The value <script type="math/tex">\int \int \int_{\Omega} dV</script> is just the volume of <script type="math/tex">\Omega</script>!</p> <p>From the perspective of the fluid outside of the volume that we considered here, it does not matter if an object is inside <script type="math/tex">\Omega</script> or if this is fluid<sup>1</sup>. Thus, this is the buoyant force exerted on an object immersed in a fluid with density <script type="math/tex">\rho</script>: an upward force equal to <span><script type="math/tex">\rho g | \Omega |</script></span>.</p> <p><sup>1</sup> Yeah, yeah, we are ignoring surface tension here, but I think this force is negligible on an iceberg.</p> <p>Thanks to <a href="https://www.linkedin.com/in/abradle2">Aaron Bradley</a> for the idea for this blog post!</p> <p><a href="http://CorySimon.github.io/articles/tip-of-the-icerberg/">How big is the tip of the iceberg?</a> was originally published by Cory Simon at <a href="http://CorySimon.github.io">Mathemathinking</a> on March 08, 2015.</p>