AuthorNate Lemoine

I’ve been getting more into theoretical/analytical modeling lately, and I’ve been playing around with software to help me do some of the more complicated calculus involved (for two reasons: 1) I’m lazy, 2) My skills are very rusty). Python (of course) provides excellent symbolic capabilities through the Sympy module. I played around with it to try and get a feel for how it works, but I couldn’t find any help online, nor anyone who has posted a tutorial, on analyzing basic biological/ecological models with Sympy. So here is my version. Below, I solve both the exponential growth and logistic growth models using Sympy, then plot the results. Here is a step-by-step tutorial for Bio-Sympy.

Exponential Growth

Exponential growth is defined by the differential equation

\( \frac{dy(t)}{dt} = k*y(t) \)

and this ODE has the analytical solution of

\( y(t) = Y_0 e^{kt} \)

So how do we use Sympy to go from the ODE to the general solution? Well, like this.

First, gotta import the modules we need, and then initialize pretty printing just so the output is readable (see the Sympy docs for this):

Next, define the variables k (the intrinsic growth rate) and t (for time). Then, make y a function of t

from sympy.abc import t, k
y = sm.Function('y')(t)

Then, define the derivative of y with respect to t (the left-hand side of the ODE), and then define the right-hand side of the ODE.

dy = y.diff(t)
rhs = k*y

We need to set these two quantities equal to one another, as in the ODE above, which we can do using a Sympy Equality

eq = sm.Eq(dy, rhs)

If you print eq, it should give you the differential equation. Now that we have the differential equation set up, all we need to do is solve. Since this is a simple one, Sympy can do it on its own with no hints or guesses:

sol = sm.dsolve(eq)
sol

Where sol should give you the analytical solution \( y(t) = C_1 e^{kt} \). But \( C_1 \) here is just a constant, we want to put it in terms of the initial conditions. (I know this is trivial in this case, but bear with me). First, we need to find the initial conditions. We do so by substituting 0 in for t, and then setting that equal to n0.

# find out what C is in terms of y0
t0 = sol.args[1].subs({'t':0})
n0 = sm.symbols('n0')
eq_init = sm.Eq(n0, t0)
# it takes a little more work to isolate C1 here
# try each step for yourself to see what it does
C1 = t0.args[2].args[0].args[0]
t0_sol = sm.solve(eq_init, C1)

The initial conditions are also rather complex:

\( C_1 =\frac{\log \left ( \frac{-n_0)}{K-n_0} \right )}{K} \)

But we can substitute that back into the original solution to get the general solution in terms of the initial conditions

# substitute the expression for C1 back into the equation
final = sol.args[1].subs(C1, t0_sol[0])

Insect herbivores play an important role in plant populations. The can determine population size by consuming and killing plants. They determine plant fitness, either by consuming seeds, reducing photosynthesis rates by consuming leaves, or by forcing plants to invest limited resources into defensive chemistry rather than seed production.

Warming, caused by climate change, affects both plants and herbivores. Rising temperatures increase plant growth rates, affect the production of plant defensive chemicals, and can also affect plant seed production. On top of that, warming often increases herbivore metabolic rates, causing herbivores to increase their consumption of plant material. Such increased consumption rates may strengthen the effect of herbivores on plants, but the direct effects of warming on plants (i.e. higher growth rates), could offset the increased herbivore damage. My co-authors and I just published a new paper in the journal Ecology, titled “Responses of plant phenology, growth, defense, and reproduction to interactive effects of warming and insect herbivory“, that examines whether rising temperatures might disrupt the ability of insects to influence plant life history.

What we found surprised us. At normal temperatures, plants were able to compensate for herbivory. Sure, plants exposed to insects were shorter and produced fewer fruits, but each fruit was packed with more seeds. As a result, total seed biomass was unaffected by insects (although seeds were smaller).

Warming changed all that. Plants were shorter and they produced more fruits. However, fruits were smaller, and packed with many small seeds. It was quite astonishing, however, because herbivores no longer had any effect. Despite high leaf damage, plant life history was unaffected by the presence of herbivores.

Why would this happen, why would warming negate any influence of insects on plants? We don’t know for sure, but we suspect that warming reduced soil moisture (a common effect of rising temperatures). Low soil water content appears to have induced a stress response, causing stunted growth in the plants and altering their life history strategy. Interestingly, the stress response to low soil moisture appears to have superseded any effect of herbivores.

Herbivores play an incredibly important role in plant ecology; they drive plant population dynamics and evolution. Our results suggest that climate warming might drastically alter the role of insects in plant communities.

In my last post, I discussed how to get predictions from Gaussian Processes in STAN quickly using the analytical solution. I was able to get it down to 3.8 seconds, which is pretty quick. But I can do better.

One of this issues you may or may not have noticed is, using the model wherein posterior predictions are generated quantities, your computer bogs down with anything over 1000 iterations (I know mine froze up). The issue here is that generated quantities saves all the variables into memory, including those large matrices \( \boldsymbol{K}_{obs}, \boldsymbol{K}_{obs}^{*}, \boldsymbol{K}^{*} \) . We can resolve this issue, and speed things up, using functions to calculate the predictive values while storing the matrices only as local variables within the function (they are not saved in memory).

One obvious solution is to not calculate them within STAN, but to do it externally. Python can do this relatively quickly with judicious use of numpy (whose linear algebra functions are all written in C and so very fast) and numba/just-in-time compilers. However, since STAN compiles everything into C++, and many of its linear algebra functions are optimized for speed, it makes sense to do it all in STAN. Fortunately, we can create our own functions within STAN to do this, which are then compiled and executed in C++. The code is as follows:

There are a couple of tricks here. I used a diagonal matrix \(\boldsymbol{I}\sigma_n\) to remove a for-loop in calculating the noise variance. It has the added benefit of cleaning up the code (I find for-loops to be messy to read, I like things tidy). Second, by using functions, the generated quantities only stores the predictions, not all the extra matrices, freeing up tons of memory.

One other thing to note is that the prediction function ends with the suffix ‘_rng’. This is because it is a random number generator, drawing random observations from the posterior distribution. Using the ‘_rng’ suffix allows the function to access other ‘_rng’ functions, chiefly the normal_rng function which draws random normal deviates. That’s necessary for the cholesky trick to turn N(0,1) numbers into the posterior distribution (see the last post). However, normal_rng only returns ONE number, so you have to repeat it for however many observations you need, hence the rep_vector( ) wrapper.

This code is extremely fast. After compilation, it executes 4 chains, 1000 iterations in roughly 1 second (compared to just over three from the previous post). Further, since I no longer have memory issues, I can run more iterations. Whereas 5000 iterations froze my computer before, now it executes in 5 seconds. 10,000 iterations, previously unimaginable on my laptop, runs in 10 seconds.

As described in an earlier post, Gaussian process models are a fast, flexible tool for making predictions. They’re relatively easy to program if you happen to know the parameters of your covariance function/kernel, but what if you want to estimate them from the data? There are several methods available, but my favorite so far is STAN. True, it requires programming the kernel by hand, but I actually find this easier to understand than trying to parse out the kernel functions from, say, scikit-learn.

STAN can fit GP models quickly, but there are certain tricks you can do that make it lightning fast and accurate. I’ve had trouble getting scikit to converge on a stable/accurate solution, but STAN does this with no problem. Plus, the Hamiltonian Monte-Carlo sampler is very quick for GPs (see the STAN User Manual for more).

Here’s a quick tutorial on how to fit GPs in STAN, and how to speed them up. First, let’s import our modules and simulate some fake data:

This model fits the parameters of the kernel. On my computer, it does so in about 0.15 seconds total (1000 iterations).

Now that we know what the hyperparameters are, we’d like to predict new values, but we also want to do so incorporating full uncertainty in the hyperparameters. The way the STAN manual says to go about this is to make a second vector containing the locations you’d like to predict, paste those together to the X values you have, make a vector of prediction points as parameters, paste those to the Y values you have, and feed those into the multivariate normal distribution as one big mush. The issue here is that the covariance matrix, Sigma, gets very large very fast. Large covariance matrices take a while to invert in the multivariate probability density, and so slows down the sampler.

Here’s the model, after making a vector of 100 prediction points to get a smooth line:

This is extremely slow, taking about 81 seconds on my computer (up from less than one second). Taking the cholesky decompose of Sigma an using multi_normal_cholesky didn’t speed things up, either.

We can speed this up by taking advantage of the analytical form of the solution. That is, once we know the hyperparameters of the kernel from the observed data, we can directly calculate the multivariate normal distribution of the predicted data:

We can calculate those quantities directly within STAN. Then, as an added trick, we can take advantage of the Cholesky decomposition to generate random samples of \( Y_p \) within STAN as well. Here’s the annotated model:

Gaussian Processes for Machine Learning by Rasmussen and Williams has become the quintessential book for learning Gaussian Processes. They kindly provide their own software that runs in MATLAB or Octave in order to run GPs. However, I find it easiest to learn by programming on my own, and my language of choice is Python. This is the first in a series of posts that will go over GPs in Python and how to produce the figures, graphs, and results presented in Rasmussen and Williams.

Figure 2.2

We want to make smooth lines to start, so make 100 evenly spaced \(x\) values:

N_star = 101
x_star = np.linspace(-5, 5, N_star)

Next we have to calculate the covariances between all the observations and store them in the matrix \(\boldsymbol{K}\). Here, we use the squared exponential covariance: \(\text{exp}[-\frac{1}{2}(x_i – x_j)^2]\)

We now need to calculate the covariance between our unobserved data (x_star) and our observed data (x_obs), as well as the covariance among x_obs points as well. The first for loop calculates observed covariances. The second for loop calculates observed-new covariances.

This may not look exactly like the Rasmussen and Williams Fig. 2.2b because I guessed at the data points and they may not be quite right. As the authors point out, we can actually plot what the covariance looks like for difference x-values, say \(x=-1,2,3\).

In this case, however, we’ve forced the scale to be equal to 1, that is you have to be at least one unit away on the x-axis before you begin to see large changes \(y\). We can incorporate a scale parameter \(\lambda\) to change that. We can use another parameter \(\sigma_f^2\) to control the noise in the signal (that is, how close to the points does the line have to pass) and we can add further noise by assuming measurement error \(\sigma_n^2\).

I’d like to congratulate my REU from last summer, Maddi Rode, whose paper “Prospective evidence for independent nitrogen and phosphorus limitation of grasshopper (Chorthippus curtipennis) growth in a tallgrass prairie” was just published in PLOS One!

Maddi spent the summer at Konza Prairie Biological Station doing just that. She enriched plots of Andropogon gerardii, or big bluestem, with nitrogen, phosphorus, or a combination of the two. We then tracked the growth of the marsh meadow grasshopper, Chorthippus curtipennis, under all conditions.

Chorthippus curtipennis. http://bugguide.net/node/view/699668

She found that nitrogen enrichment led to higher grasshopper growth rates. Surprisingly (or unsurprisingly to us), phosphorus enrichment stimulated grasshopper growth by nearly the exact same amount as nitrogen enrichment.

This work adds to the building body of literature that grasshoppers, and indeed most terrestrial insects, are limited by a suite of nutrients beyond simply phosphorus. What this means for herbivore feeding behavior and climate change remains to be seen…

Nutrient-sharing mutualisms occur when two species cooperate to exchange necessary nutrients between them. The most common example is plants and mycorrhizal fungi. These fungi live on or in the roots of plants and are very good at scavenging rare nutrients, such as nitrogen or phosphorus, from the soils. However, they are not very good at obtaining sugars, carbohydrates, and other carbon-based nutrients. Plants have the opposite problem. They fix sunlight and carbon dioxide into sugars and carbohydrates, but often cannot take up enough nitrogen from the soils. Mycorrhizae and plants share nutrients, with fungi receiving sugars in exchange for nitrogen, to the benefit of both parties. Corals and algae are another common example, wherein corals provide nitrogen to their photosynthetic algae partners in exchange for sugars.

Beneath the snow and dirt lies a vast network of roots and fungi that support this Ponderosa pine forest

The stability of these mutualisms hinges on the relative rarity of important nutrients, in this case nitrogen and phosphorus. There is some evidence that the plant, or more generally the photosynthesizing partner, regulates the amount of nutrients in the trade. Theory predicts that if limiting nutrients, like nitrogen, suddenly become non-limiting, then the mutualism should fall apart. After all, why should the plant continue to trade away valuable sugars when it no longer needs the nitrogen from the fungi?

Andy and I tested this prediction using a meta-analysis, which is a quantitative synthesis of published results. Sure enough, we found that nutrient-sharing mutualisms subject to nutrient enrichment showed signs of collapse. That is, when soils or water were fertilized with nitrogen or phosphorus, the heterotrophic partner (corals, fungi) suffered lower performance because the phototrophic partner (plants, algae) ceased trading sugars for nutrients.

What does this mean? Well, nutrient-sharing symbioses form the foundation of most ecosystems: most plants harbor mutualistic fungi, corals cannot exist with their algal symbionts, and lichens exist as a mutualism between fungi and algae. If these mutualisms weaken, then the foundation of most communities also weakens, although just how much remains to be seen.

As you may be aware, reading science papers can be difficult. Language is verbose and cluttered, the vocabulary is full of jargon, and trying to make sense of it can make your head hurt. I feel that way and I spend my life reading research papers, so I can’t even imagine how non-scientists feel when they attempt to read primary literature. Actually, I can, because I remember being confused as hell reading papers as an undergraduate. This has led some reputable scientists to explain why most written science is difficult to follow. Of course, other equally reputable scientists had some alternative ideas. I actually agree with Dr. Gelman, author of the blog in the second link. It isn’t just most science writing that stinks (to use Dr. Pinker’s phrasing). Most writing stinks in general. Writing is hard. I’ve spent years honing my skills and they still aren’t great. Here are a few simple tips, in no particular order, I’ve picked up on how to write a clear and understandable research paper.

Write in the active voice. As an undergraduate, I learned that science was to be written in the passive voice at all times (I did that on purpose). I’ve learned as a PhD. student that passive voice is boring and slow. Write actively wherever possible; it’s more engaging and interesting. For example, reword the first sentence of this paragraph: ‘As an undergraduate in the sciences, I learned to write in passive voice at all times’. Much better.

First person is fine. I also learned in undergrad never to use first person. This archaic rule has fallen completely out of style. Feel free to say ‘I did this’ or ‘We did that’. If you have co-authors, always use ‘we’ even if you did all the physical work. Often your co-authors (see advisors) had a stronger guiding influence than you suspect.

Delete the word ‘the’. ‘The’ has to be one of the most overused words in writing. Read your sentence multiple times, both with and without ‘the’. See if it reads just as clearly without it, then delete it. This is a BIG one.

Delete the words ‘of’ and ‘that’. Same as above. Don’t write ‘even if you did all of the physical work’. Instead, write ‘even if you did all the physical work’. Sounds better, simpler, more active. Instead of ‘a few tips that I’ve picked up’, try ‘a few tips I’ve picked up’.

Use as few words as possible. I think this is good advice for giving public presentations, writing, and talking in general. Don’t write ‘small changes in temperature’ when ‘small temperature changes’ will do.

Know what ‘As such’ really means. Please look this up, because this has to be pretty high on the list of misused phrases. ‘As such’ commonly appears as a transition, e.g. “Temperatures increase metabolic rates. As such, growth and respiration increase as well”. That is wrong. ‘As such’ directly refers to the subject of the previous sentence, e.g. “I am a Phd. candidate. As such, I’ll be unemployed as soon as I graduate”. If you’re confused, replace ‘As such’ with the subject or description from the previous sentence, “As a PhD. candidate, I’ll be unemployed as soon as I graduate’. If it works, you’ve used ‘As such’ correctly. If not, try a different word. ‘Accordingly’ is good, but make sure your paper isn’t covered with ‘Accordingly’s.

It’s OK to start sentences with ‘However’ and ‘Therefore’. Technically it isn’t (another rule I learned as an undergraduate). However, for impact, I prefer it. First, the technically correct way: “I agree with my advisor on most things. I find, however, that I strongly disagree with him on others.’ Second: “I agree with my advisor on most things. However, I strongly disagree with him on others”. I like the second one better, it gets the point across.

Check your paragraphs. The first sentence of a paragraph is the intro. The last is the outro. You should be able to remove everything in between and get your major point across. The stuff in the middle is just details. Try it with the lead-in paragraph to this post. If you can’t remove the middle without sacrificing several important points, then you have too many main ideas in one paragraph.

Following from #8, keep paragraphs short. Good god, please don’t write a page-long paragraph. Get your point across quickly.

Most importantly, use short and simple sentences. My PhD. advisor is king of this, and it works very well. Write like Hemmingway. You don’t need to show off your incredible vocabulary or complex, Faulknerian trains of thought. Save that for the Archer fan fiction you keep in binders on your shelf. The stuff you want people to understand needs to be written clearly and concisely in simple language.

Honorable Mentions: COMMA SPLICING! Please cut down on commas. I reviewed a number of papers of both friends and anonymous authors and people like to put commas everywhere. Only put them where they belong. Also, write with a dog on your lap. A dog on the lap makes everything better.

Over the past few weeks, Blue Atala caterpillars have been out in force. A single coontie plant in the park by my house could have 15 – 20 caterpillars. A week ago, the caterpillars all formed their chrysalids, so the coontie plants look like Christmas trees, evergreen shrubs with little red ornaments.

Atala chrysalids hanging from a coontie plant.

I can’t wait for the next week or so, when the park will be swarming with Atala butterflies.

As you may or may not know, scientists don’t just get money for research given to them by federal or state governments, agencies, or universities. We have to write proposals and submit them for review whenever there is funding available. Only the best proposals get funded, and the odds of getting your proposal funded for any given opportunity is <10% (usually somewhere between 2 – 5% get funded, depending on the funding source and competition). I though I’d post my NSF Doctoral Dissertation Improvement Grant that was successfully funded (on the first try!) for anyone who wants to to see. This is in keeping with a new philosophy of science transparency, and there are some excellent lists of publicly available grant proposals. Hopefully, this helps you understand what goes into the beginnings of a research project, after all, no research is possible without funding, and getting funding isn’t easy. If you’re applying to one of these grants, hopefully you find this and can look it over to see what successful DDIG grants look like.