diff --git a/doc/pub/How2ReadData/html/._How2ReadData-bs000.html b/doc/pub/How2ReadData/html/._How2ReadData-bs000.html index f9424edda..2e49e32ac 100644 --- a/doc/pub/How2ReadData/html/._How2ReadData-bs000.html +++ b/doc/pub/How2ReadData/html/._How2ReadData-bs000.html @@ -45,17 +45,66 @@ Automatically generated HTML file from DocOnce source ('Different algorithms', 2, None, '___sec2'), ('Software and needed installations', 2, None, '___sec3'), ('Python installers', 2, None, '___sec4'), - ('Installing R and C++', 2, None, '___sec5'), + ('Installing R, C++, cython or Julia', 2, None, '___sec5'), ('Introduction to Jupyter notebook and available tools', 2, None, '___sec6'), - ('Representing data, overarching aims', 2, None, '___sec7'), - ('Representing data, more examples', 2, None, '___sec8')]} + ('Doing it in R', 2, None, '___sec7'), + ('Representing data, overarching aims', 2, None, '___sec8'), + ('Representing data, more examples', 2, None, '___sec9'), + ('Binning of data', 2, None, '___sec10'), + ('Adding model complexity: Predator-Prey model from ecology', + 2, + None, + '___sec11'), + ('Case study from Hudson bay', 2, None, '___sec12'), + ('Hudson bay data', 2, None, '___sec13'), + ('Plotting the data', 2, None, '___sec14'), + ('Hares and lynx in Hudson bay from 1900 to 1920', + 2, + None, + '___sec15'), + ('Why now create a computer model for the hare and lynx ' + 'populations?', + 2, + None, + '___sec16'), + ('The traditional (top-down) approach', 2, None, '___sec17'), + ("The ``new'' discrete bottom-up approach", 2, None, '___sec18'), + ('Basic (computer-friendly) mathematics notation', + 2, + None, + '___sec19'), + ('Basic dynamics of the population of hares', + 2, + None, + '___sec20'), + ('Basic dynamics of the population of lynx', 2, None, '___sec21'), + ('Evolution equations', 2, None, '___sec22'), + ('Adapt the model to the Hudson Bay case', 2, None, '___sec23'), + ('The program', 2, None, '___sec24'), + ('The plot', 2, None, '___sec25')]} end of tocinfo -->
+ + + + + + +-
@@ -139,6 +205,8 @@ end of tocinfo -->
-
@@ -139,6 +205,8 @@ end of tocinfo -->
-
@@ -232,7 +248,7 @@ we recommend that you install the following Python packages via pip as For Python3, replace pip with pip3.
-For OSX user we recommend also, after having installed Xcode, to install brew. Brew allows +For OSX users we recommend also, after having installed Xcode, to install brew. Brew allows for a seamless installation of additional software via for example
You will also find it convenient to utilize R. Say more about R. @@ -282,6 +298,13 @@ To add more entropy, cython can also be used when running your notebooks. setup allows you to integrate widely popular softwares and tools for scientific computing. With its versatility, including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook can easily be converted into a nicely rendered PDF file or a Latex file for further processing. + +
+This never ends, + +
+If you se the light mark-up language doconce you can convert a standard ascii text file into various HTML +formats, ipython notebooks, latex files, pdf files etc.
@@ -320,7 +349,7 @@ display(data_pandas)
@@ -348,6 +377,445 @@ plt.show()
+
+
+The population dynamics of a simple predator-prey system is a
+classical example shown in many biology textbooks when ecological
+systems are discussed. The system contains all elements of the
+scientific method:
+
+
+
+Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why?
+We shall demonstrate the scientific method by
+
+
+
+% if FORMAT == 'ipynb':
+Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle.
+
+
+One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920.
+% endif
+
+
+
+
+
+
+
+% if FORMAT != 'ipynb':
+
+
+% if FORMAT == 'ipynb':
+We see from the plot that there are indeed fluctuations.
+We would like to create a mathematical model that explains these
+population fluctuations. Ecologists have predicted that in a simple
+predator-prey system that a rise in prey population is followed (with
+a lag) by a rise in the predator population. When the predator
+population is sufficiently high, then the prey population begins
+dropping. After the prey population falls, then the predator
+population falls, which allows the prey population to recover and
+complete one cycle of this interaction. Thus, we see that
+qualitatively oscillations occur. Can a mathematical model predict
+this? What causes cycles to slow or speed up? What affects the
+amplitude of the oscillation or do you expect to see the oscillations
+damp to a stable equilibrium? The models tend to ignore factors like
+climate and other complicating factors. How significant are these?
+% else:
+
+
+
+% endif
+
+
+The classical way (in all books) is to present the Lotka-Volterra equations:
+
+
+Here,
+
+
+
+Most books quickly establish the model and then use considerable space on
+discussing the qualitative properties of this nonlinear system of
+ODEs (which cannot be solved)
+
+
+
+The population of hares evolves due to births and deaths exactly as a bacteria population:
+
+
+
+We assume that the primary growth for the lynx population depends on sufficient food for raising lynx kittens, which implies an adequate source of nutrients from predation on hares. Thus, the growth of the lynx population does not only depend of how many lynx there are, but on how many hares they can eat.
+In a time interval \( \Delta t HL \) hares and lynx can meet, and in a
+fraction \( b\Delta t HL \) the lynx eats the hare. All of this does not
+contribute to the growth of lynx, again just a fraction of
+\( b\Delta t HL \) that we write as
+\( d\Delta t HL \). In addition, lynx die just as in the population
+dynamics with one isolated animal population, leading to a loss
+\( -c\Delta t L \).
+
+
+The accounting of lynx then looks like
+
+
+By writing up the definition of \( \Delta H \) and \( \Delta L \), and putting
+all assumed known terms \( H^n \) and \( L^n \) on the right-hand side, we have
+
+
+Note:
+
+
+
+
+
+
+
+% if FORMAT != 'ipynb':
+
+
+% else:
+If we perform a least-square fitting, we can find optimal values for the parameters \( a \), \( b \), \( d \), \( c \). The optimal parameters are \( a=0.4807 \), \( b=0.02482 \), \( d=0.9272 \) and \( c=0.02756 \). These parameters result in a slightly modified initial conditions, namely \( H(0) = 34.91 \) and \( L(0)=3.857 \). With these parameters we are now ready to solve the equations and plot these data together with the experimental values.
+% endif
+Representing data, more examples
+Representing data, more examples
Binning of data
+Use the Hudson bay model to load data and make plot in R and with python
+Adding model complexity: Predator-Prey model from ecology
+
+
+
+Case study from Hudson bay
+
+
+
+Hudson bay data
+
+
+
+
+
+
+
+
+Year Hares (x1000) Lynx (x1000)
+ 1900 30.0 4.0
+ 1901 47.2 6.1
+ 1902 70.2 9.8
+ 1903 77.4 35.2
+ 1904 36.3 59.4
+ 1905 20.6 41.7
+ 1906 18.1 19.0
+ 1907 21.4 13.0
+ 1908 22.0 8.3
+ 1909 25.4 9.1
+ 1910 27.1 7.4
+ 1911 40.3 8.0
+ 1912 57 12.3
+ 1913 76.6 19.5
+ 1914 52.3 45.7
+ 1915 19.5 51.1
+ 1916 11.2 29.7
+ 1917 7.6 15.8
+ 1918 14.6 9.7
+ 1919 16.2 10.1
+
+ 1920 24.7 8.6 Plotting the data
+
+import numpy as np
+from matplotlib import pyplot as plt
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.dat', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+year = data[:,0]
+hares = data[:,1]
+lynx = data[:,2]
+
+plt.plot(year, hares ,'b-+', year, lynx, 'r-o')
+plt.axis([1900,1920,0, 100.0])
+plt.xlabel(r'Year')
+plt.ylabel(r'Numbers of hares and lynx ')
+plt.legend(('Hares','Lynx'), loc='upper right')
+plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')
+plt.savefig('Hudson_Bay_data.pdf')
+plt.savefig('Hudson_Bay_data.png')
+plt.show()
+
Hares and lynx in Hudson bay from 1900 to 1920
+
+
+% endif
+Why now create a computer model for the hare and lynx populations?
+
+
+The traditional (top-down) approach
+
+
+$$
+\begin{align*}
+\frac{dH}{dt} &= H(a - b L)\\
+\frac{dL}{dt} &= - L(d - c H)
+\end{align*}
+$$
+
+
+
+
+The "new" discrete bottom-up approach
+
+
+
+
+
+Basic (computer-friendly) mathematics notation
+
+
+Basic dynamics of the population of hares
+
+
+$$
+\Delta H = a \Delta t H^n
+$$
+
+
+However, hares have an additional loss in the population because
+they are eaten by lynx.
+All the hares and lynx can form
+\( H\cdot L \) pairs in total. When such pairs meet during a time
+interval \( \Delta t \), there is some
+small probablity that the lynx will eat the hare.
+So in fraction \( b\Delta t HL \), the lynx eat hares. This
+loss of hares and must be accounted for:
+subtracted in the equation for hares:
+
+
+$$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
+
+Basic dynamics of the population of lynx
+
+
+$$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$
+
+Evolution equations
+
+
+$$ H^{n+1} = H^n + a\Delta t H^n - b\Delta t H^n L^n $$
+
+
+
+$$ L^{n+1} = L^n + d\Delta t H^nL^n - c\Delta t L^n $$
+
+
+
+
+
+ (which we can get from the data)Adapt the model to the Hudson Bay case
+
+
+
+The program
+
+import numpy as np
+import matplotlib.pyplot as plt
+
+def solver(m, H0, L0, dt, a, b, c, d, t0):
+ """Solve the difference equations for H and L over m years
+ with time step dt (measured in years."""
+
+ num_intervals = int(m/float(dt))
+ t = np.linspace(t0, t0 + m, num_intervals+1)
+ H = np.zeros(t.size)
+ L = np.zeros(t.size)
+
+ print 'Init:', H0, L0, dt
+ H[0] = H0
+ L[0] = L0
+
+ for n in range(0, len(t)-1):
+ H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n]
+ L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n]
+ return H, L, t
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+t_e = data[:,0]
+H_e = data[:,1]
+L_e = data[:,2]
+
+# Simulate using the model
+H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1,
+ a=0.4807, b=0.02482, c=0.9272, d=0.02756,
+ t0=1900)
+
+# Visualize simulations and data
+plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--')
+plt.xlabel('Year')
+plt.ylabel('Numbers of hares and lynx')
+plt.axis([1900, 1920, 0, 140])
+plt.title(r'Population of hares and lynx 1900-1920 (x1000)')
+plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left')
+plt.savefig('Hudson_Bay_sim.pdf')
+plt.savefig('Hudson_Bay_sim.png')
+plt.show()
+
The plot
+
+
+
+
-
@@ -185,7 +234,7 @@ we recommend that you install the following Python packages via pip as
For Python3, replace pip with pip3.
-For OSX user we recommend also, after having installed Xcode, to install brew. Brew allows +For OSX users we recommend also, after having installed Xcode, to install brew. Brew allows for a seamless installation of additional software via for example
You will also find it convenient to utilize R. Say more about R. @@ -233,6 +282,13 @@ setup allows you to integrate widely popular softwares and tools for scientific including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook can easily be converted into a nicely rendered PDF file or a Latex file for further processing. +
+This never ends, + +
+If you se the light mark-up language doconce you can convert a standard ascii text file into various HTML +formats, ipython notebooks, latex files, pdf files etc. +
@@ -241,7 +297,13 @@ can easily be converted into a nicely rendered PDF file or a Latex file f
-
+
+
+
@@ -272,7 +334,7 @@ display(data_pandas)
-
@@ -300,6 +362,451 @@ plt.show()
+
+
+
+
+
+
+
+The population dynamics of a simple predator-prey system is a +classical example shown in many biology textbooks when ecological +systems are discussed. The system contains all elements of the +scientific method: + +
+
+
+
+
+Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why? +We shall demonstrate the scientific method by + +
+
+
+
+
+% if FORMAT == 'ipynb': +Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle. + +
+One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920. +% endif + +
+
| Year | Hares (x1000) | Lynx (x1000) |
|---|---|---|
| 1900 | 30.0 | 4.0 |
| 1901 | 47.2 | 6.1 |
| 1902 | 70.2 | 9.8 |
| 1903 | 77.4 | 35.2 |
| 1904 | 36.3 | 59.4 |
| 1905 | 20.6 | 41.7 |
| 1906 | 18.1 | 19.0 |
| 1907 | 21.4 | 13.0 |
| 1908 | 22.0 | 8.3 |
| 1909 | 25.4 | 9.1 |
| 1910 | 27.1 | 7.4 |
| 1911 | 40.3 | 8.0 |
| 1912 | 57 | 12.3 |
| 1913 | 76.6 | 19.5 |
| 1914 | 52.3 | 45.7 |
| 1915 | 19.5 | 51.1 |
| 1916 | 11.2 | 29.7 |
| 1917 | 7.6 | 15.8 |
| 1918 | 14.6 | 9.7 |
| 1919 | 16.2 | 10.1 |
| 1920 | 24.7 | 8.6 |
+
+
+
+
+
+ + +
import numpy as np
+from matplotlib import pyplot as plt
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.dat', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+year = data[:,0]
+hares = data[:,1]
+lynx = data[:,2]
+
+plt.plot(year, hares ,'b-+', year, lynx, 'r-o')
+plt.axis([1900,1920,0, 100.0])
+plt.xlabel(r'Year')
+plt.ylabel(r'Numbers of hares and lynx ')
+plt.legend(('Hares','Lynx'), loc='upper right')
+plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')
+plt.savefig('Hudson_Bay_data.pdf')
+plt.savefig('Hudson_Bay_data.png')
+plt.show()
+
+% if FORMAT != 'ipynb':
+
+
+
+

+
+
+
+% if FORMAT == 'ipynb': +We see from the plot that there are indeed fluctuations. +We would like to create a mathematical model that explains these +population fluctuations. Ecologists have predicted that in a simple +predator-prey system that a rise in prey population is followed (with +a lag) by a rise in the predator population. When the predator +population is sufficiently high, then the prey population begins +dropping. After the prey population falls, then the predator +population falls, which allows the prey population to recover and +complete one cycle of this interaction. Thus, we see that +qualitatively oscillations occur. Can a mathematical model predict +this? What causes cycles to slow or speed up? What affects the +amplitude of the oscillation or do you expect to see the oscillations +damp to a stable equilibrium? The models tend to ignore factors like +climate and other complicating factors. How significant are these? +% else: + +
+
+
+
+
+The classical way (in all books) is to present the Lotka-Volterra equations: + +$$ +\begin{align*} +\frac{dH}{dt} &= H(a - b L)\\ +\frac{dL}{dt} &= - L(d - c H) +\end{align*} +$$ + +
+Here, + +
+
+
+
+
+ + +
+
+
+
+ +
+
+
+
+
+The population of hares evolves due to births and deaths exactly as a bacteria population: + +$$ +\Delta H = a \Delta t H^n +$$ + +However, hares have an additional loss in the population because +they are eaten by lynx. +All the hares and lynx can form +\( H\cdot L \) pairs in total. When such pairs meet during a time +interval \( \Delta t \), there is some +small probablity that the lynx will eat the hare. +So in fraction \( b\Delta t HL \), the lynx eat hares. This +loss of hares and must be accounted for: +subtracted in the equation for hares: + +$$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$ +
+
+
+
+
+We assume that the primary growth for the lynx population depends on sufficient food for raising lynx kittens, which implies an adequate source of nutrients from predation on hares. Thus, the growth of the lynx population does not only depend of how many lynx there are, but on how many hares they can eat. +In a time interval \( \Delta t HL \) hares and lynx can meet, and in a +fraction \( b\Delta t HL \) the lynx eats the hare. All of this does not +contribute to the growth of lynx, again just a fraction of +\( b\Delta t HL \) that we write as +\( d\Delta t HL \). In addition, lynx die just as in the population +dynamics with one isolated animal population, leading to a loss +\( -c\Delta t L \). +
+
+The accounting of lynx then looks like +$$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$ +
+
+
+
+
+By writing up the definition of \( \Delta H \) and \( \Delta L \), and putting +all assumed known terms \( H^n \) and \( L^n \) on the right-hand side, we have + +$$ H^{n+1} = H^n + a\Delta t H^n - b\Delta t H^n L^n $$ + + +$$ L^{n+1} = L^n + d\Delta t H^nL^n - c\Delta t L^n $$ + +
+Note: + +
+
+
+
+
+ +
+
+
+
+
+
+ + +
import numpy as np
+import matplotlib.pyplot as plt
+
+def solver(m, H0, L0, dt, a, b, c, d, t0):
+ """Solve the difference equations for H and L over m years
+ with time step dt (measured in years."""
+
+ num_intervals = int(m/float(dt))
+ t = np.linspace(t0, t0 + m, num_intervals+1)
+ H = np.zeros(t.size)
+ L = np.zeros(t.size)
+
+ print 'Init:', H0, L0, dt
+ H[0] = H0
+ L[0] = L0
+
+ for n in range(0, len(t)-1):
+ H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n]
+ L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n]
+ return H, L, t
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+t_e = data[:,0]
+H_e = data[:,1]
+L_e = data[:,2]
+
+# Simulate using the model
+H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1,
+ a=0.4807, b=0.02482, c=0.9272, d=0.02756,
+ t0=1900)
+
+# Visualize simulations and data
+plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--')
+plt.xlabel('Year')
+plt.ylabel('Numbers of hares and lynx')
+plt.axis([1900, 1920, 0, 140])
+plt.title(r'Population of hares and lynx 1900-1920 (x1000)')
+plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left')
+plt.savefig('Hudson_Bay_sim.pdf')
+plt.savefig('Hudson_Bay_sim.png')
+plt.show()
+
+% if FORMAT != 'ipynb':
+
+
+
+

+% else: +If we perform a least-square fitting, we can find optimal values for the parameters \( a \), \( b \), \( d \), \( c \). The optimal parameters are \( a=0.4807 \), \( b=0.02482 \), \( d=0.9272 \) and \( c=0.02756 \). These parameters result in a slightly modified initial conditions, namely \( H(0) = 34.91 \) and \( L(0)=3.857 \). With these parameters we are now ready to solve the equations and plot these data together with the experimental values. +% endif +
diff --git a/doc/pub/How2ReadData/html/How2ReadData.html b/doc/pub/How2ReadData/html/How2ReadData.html index eb101e92a..6a1b851c5 100644 --- a/doc/pub/How2ReadData/html/How2ReadData.html +++ b/doc/pub/How2ReadData/html/How2ReadData.html @@ -70,17 +70,66 @@ div { text-align: justify; text-justify: inter-word; } ('Different algorithms', 2, None, '___sec2'), ('Software and needed installations', 2, None, '___sec3'), ('Python installers', 2, None, '___sec4'), - ('Installing R and C++', 2, None, '___sec5'), + ('Installing R, C++, cython or Julia', 2, None, '___sec5'), ('Introduction to Jupyter notebook and available tools', 2, None, '___sec6'), - ('Representing data, overarching aims', 2, None, '___sec7'), - ('Representing data, more examples', 2, None, '___sec8')]} + ('Doing it in R', 2, None, '___sec7'), + ('Representing data, overarching aims', 2, None, '___sec8'), + ('Representing data, more examples', 2, None, '___sec9'), + ('Binning of data', 2, None, '___sec10'), + ('Adding model complexity: Predator-Prey model from ecology', + 2, + None, + '___sec11'), + ('Case study from Hudson bay', 2, None, '___sec12'), + ('Hudson bay data', 2, None, '___sec13'), + ('Plotting the data', 2, None, '___sec14'), + ('Hares and lynx in Hudson bay from 1900 to 1920', + 2, + None, + '___sec15'), + ('Why now create a computer model for the hare and lynx ' + 'populations?', + 2, + None, + '___sec16'), + ('The traditional (top-down) approach', 2, None, '___sec17'), + ("The ``new'' discrete bottom-up approach", 2, None, '___sec18'), + ('Basic (computer-friendly) mathematics notation', + 2, + None, + '___sec19'), + ('Basic dynamics of the population of hares', + 2, + None, + '___sec20'), + ('Basic dynamics of the population of lynx', 2, None, '___sec21'), + ('Evolution equations', 2, None, '___sec22'), + ('Adapt the model to the Hudson Bay case', 2, None, '___sec23'), + ('The program', 2, None, '___sec24'), + ('The plot', 2, None, '___sec25')]} end of tocinfo -->
+ + + + + + + @@ -102,7 +151,7 @@ end of tocinfo -->-
@@ -190,7 +239,7 @@ we recommend that you install the following Python packages via pip as
For Python3, replace pip with pip3.
-For OSX user we recommend also, after having installed Xcode, to install brew. Brew allows +For OSX users we recommend also, after having installed Xcode, to install brew. Brew allows for a seamless installation of additional software via for example
You will also find it convenient to utilize R. Say more about R. @@ -238,6 +287,13 @@ setup allows you to integrate widely popular softwares and tools for scientific including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook can easily be converted into a nicely rendered PDF file or a Latex file for further processing. +
+This never ends, + +
+If you se the light mark-up language doconce you can convert a standard ascii text file into various HTML +formats, ipython notebooks, latex files, pdf files etc. +
@@ -246,7 +302,13 @@ can easily be converted into a nicely rendered PDF file or a Latex file f
-
+
+
+
@@ -277,7 +339,7 @@ display(data_pandas)
-
@@ -305,6 +367,451 @@ plt.show()
+
+
+
+
+
+
+
+The population dynamics of a simple predator-prey system is a +classical example shown in many biology textbooks when ecological +systems are discussed. The system contains all elements of the +scientific method: + +
+
+
+
+
+Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why? +We shall demonstrate the scientific method by + +
+
+
+
+
+% if FORMAT == 'ipynb': +Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle. + +
+One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920. +% endif + +
+
| Year | Hares (x1000) | Lynx (x1000) |
|---|---|---|
| 1900 | 30.0 | 4.0 |
| 1901 | 47.2 | 6.1 |
| 1902 | 70.2 | 9.8 |
| 1903 | 77.4 | 35.2 |
| 1904 | 36.3 | 59.4 |
| 1905 | 20.6 | 41.7 |
| 1906 | 18.1 | 19.0 |
| 1907 | 21.4 | 13.0 |
| 1908 | 22.0 | 8.3 |
| 1909 | 25.4 | 9.1 |
| 1910 | 27.1 | 7.4 |
| 1911 | 40.3 | 8.0 |
| 1912 | 57 | 12.3 |
| 1913 | 76.6 | 19.5 |
| 1914 | 52.3 | 45.7 |
| 1915 | 19.5 | 51.1 |
| 1916 | 11.2 | 29.7 |
| 1917 | 7.6 | 15.8 |
| 1918 | 14.6 | 9.7 |
| 1919 | 16.2 | 10.1 |
| 1920 | 24.7 | 8.6 |
+
+
+
+
+
+ + +
import numpy as np
+from matplotlib import pyplot as plt
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.dat', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+year = data[:,0]
+hares = data[:,1]
+lynx = data[:,2]
+
+plt.plot(year, hares ,'b-+', year, lynx, 'r-o')
+plt.axis([1900,1920,0, 100.0])
+plt.xlabel(r'Year')
+plt.ylabel(r'Numbers of hares and lynx ')
+plt.legend(('Hares','Lynx'), loc='upper right')
+plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')
+plt.savefig('Hudson_Bay_data.pdf')
+plt.savefig('Hudson_Bay_data.png')
+plt.show()
+
+% if FORMAT != 'ipynb':
+
+
+
+

+
+
+
+% if FORMAT == 'ipynb': +We see from the plot that there are indeed fluctuations. +We would like to create a mathematical model that explains these +population fluctuations. Ecologists have predicted that in a simple +predator-prey system that a rise in prey population is followed (with +a lag) by a rise in the predator population. When the predator +population is sufficiently high, then the prey population begins +dropping. After the prey population falls, then the predator +population falls, which allows the prey population to recover and +complete one cycle of this interaction. Thus, we see that +qualitatively oscillations occur. Can a mathematical model predict +this? What causes cycles to slow or speed up? What affects the +amplitude of the oscillation or do you expect to see the oscillations +damp to a stable equilibrium? The models tend to ignore factors like +climate and other complicating factors. How significant are these? +% else: + +
+
+
+
+
+The classical way (in all books) is to present the Lotka-Volterra equations: + +$$ +\begin{align*} +\frac{dH}{dt} &= H(a - b L)\\ +\frac{dL}{dt} &= - L(d - c H) +\end{align*} +$$ + +
+Here, + +
+
+
+
+
+ + +
+
+
+
+ +
+
+
+
+
+The population of hares evolves due to births and deaths exactly as a bacteria population: + +$$ +\Delta H = a \Delta t H^n +$$ + +However, hares have an additional loss in the population because +they are eaten by lynx. +All the hares and lynx can form +\( H\cdot L \) pairs in total. When such pairs meet during a time +interval \( \Delta t \), there is some +small probablity that the lynx will eat the hare. +So in fraction \( b\Delta t HL \), the lynx eat hares. This +loss of hares and must be accounted for: +subtracted in the equation for hares: + +$$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$ +
+
+
+
+
+We assume that the primary growth for the lynx population depends on sufficient food for raising lynx kittens, which implies an adequate source of nutrients from predation on hares. Thus, the growth of the lynx population does not only depend of how many lynx there are, but on how many hares they can eat. +In a time interval \( \Delta t HL \) hares and lynx can meet, and in a +fraction \( b\Delta t HL \) the lynx eats the hare. All of this does not +contribute to the growth of lynx, again just a fraction of +\( b\Delta t HL \) that we write as +\( d\Delta t HL \). In addition, lynx die just as in the population +dynamics with one isolated animal population, leading to a loss +\( -c\Delta t L \). +
+
+The accounting of lynx then looks like +$$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$ +
+
+
+
+
+By writing up the definition of \( \Delta H \) and \( \Delta L \), and putting +all assumed known terms \( H^n \) and \( L^n \) on the right-hand side, we have + +$$ H^{n+1} = H^n + a\Delta t H^n - b\Delta t H^n L^n $$ + + +$$ L^{n+1} = L^n + d\Delta t H^nL^n - c\Delta t L^n $$ + +
+Note: + +
+
+
+
+
+ +
+
+
+
+
+
+ + +
import numpy as np
+import matplotlib.pyplot as plt
+
+def solver(m, H0, L0, dt, a, b, c, d, t0):
+ """Solve the difference equations for H and L over m years
+ with time step dt (measured in years."""
+
+ num_intervals = int(m/float(dt))
+ t = np.linspace(t0, t0 + m, num_intervals+1)
+ H = np.zeros(t.size)
+ L = np.zeros(t.size)
+
+ print 'Init:', H0, L0, dt
+ H[0] = H0
+ L[0] = L0
+
+ for n in range(0, len(t)-1):
+ H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n]
+ L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n]
+ return H, L, t
+
+# Load in data file
+data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1)
+# Make arrays containing x-axis and hares and lynx populations
+t_e = data[:,0]
+H_e = data[:,1]
+L_e = data[:,2]
+
+# Simulate using the model
+H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1,
+ a=0.4807, b=0.02482, c=0.9272, d=0.02756,
+ t0=1900)
+
+# Visualize simulations and data
+plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--')
+plt.xlabel('Year')
+plt.ylabel('Numbers of hares and lynx')
+plt.axis([1900, 1920, 0, 140])
+plt.title(r'Population of hares and lynx 1900-1920 (x1000)')
+plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left')
+plt.savefig('Hudson_Bay_sim.pdf')
+plt.savefig('Hudson_Bay_sim.png')
+plt.show()
+
+% if FORMAT != 'ipynb':
+
+
+
+

+% else: +If we perform a least-square fitting, we can find optimal values for the parameters \( a \), \( b \), \( d \), \( c \). The optimal parameters are \( a=0.4807 \), \( b=0.02482 \), \( d=0.9272 \) and \( c=0.02756 \). These parameters result in a slightly modified initial conditions, namely \( H(0) = 34.91 \) and \( L(0)=3.857 \). With these parameters we are now ready to solve the equations and plot these data together with the experimental values. +% endif +
diff --git a/doc/pub/How2ReadData/ipynb/How2ReadData.ipynb b/doc/pub/How2ReadData/ipynb/How2ReadData.ipynb index c658f51e7..7e6ae0e87 100644 --- a/doc/pub/How2ReadData/ipynb/How2ReadData.ipynb +++ b/doc/pub/How2ReadData/ipynb/How2ReadData.ipynb @@ -10,7 +10,7 @@ " \n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", "\n", - "Date: **Nov 27, 2017**\n", + "Date: **Dec 1, 2017**\n", "\n", "Copyright 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", @@ -88,7 +88,7 @@ "\n", "For Python3, replace **pip** with **pip3**.\n", "\n", - "For OSX user we recommend also, after having installed Xcode, to install **brew**. Brew allows \n", + "For OSX users we recommend also, after having installed Xcode, to install **brew**. Brew allows \n", "for a seamless installation of additional software via for example\n", "1. brew install python3\n", "\n", @@ -105,7 +105,7 @@ "\n", "2. Enthought canopy\n", "\n", - "## Installing R and C++\n", + "## Installing R, C++, cython or Julia\n", "\n", "You will also find it convenient to utilize R. Say more about R. \n", "Jupyter/Ipython notebook allows you run **R** code interactively in your browser. The software library **R** is \n", @@ -120,9 +120,15 @@ "including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook \n", "can easily be converted into a nicely rendered **PDF** file or a Latex file for further processing.\n", "\n", + "This never ends, \n", + "\n", + "If you se the light mark-up language **doconce** you can convert a standard ascii text file into various HTML \n", + "formats, ipython notebooks, latex files, pdf files etc. \n", "\n", "## Introduction to Jupyter notebook and available tools\n", "\n", + "## Doing it in R\n", + "Use the Hudson bay model to load data and make plot in R and with python\n", "\n", "\n", "## Representing data, overarching aims" @@ -188,6 +194,449 @@ "plt.plot(line, regline.predict(line), label= \"Linear Rgression\")\n", "plt.show()" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Binning of data\n", + "Use the Hudson bay model to load data and make plot in R and with python\n", + "\n", + "\n", + "\n", + "\n", + "## Adding model complexity: Predator-Prey model from ecology\n", + "\n", + "The population dynamics of a simple predator-prey system is a\n", + "classical example shown in many biology textbooks when ecological\n", + "systems are discussed. The system contains all elements of the\n", + "scientific method:\n", + "\n", + " * The set up of a specific hypothesis combined with\n", + "\n", + " * the experimental methods needed (one can study existing data or perform experiments)\n", + "\n", + " * analyzing and interpreting the data and performing further experiments if needed\n", + "\n", + " * trying to extract general behaviors and extract eventual laws or patterns\n", + "\n", + " * develop mathematical relations for the uncovered regularities/laws and test these by per forming new experiments\n", + "\n", + "\n", + "\n", + "\n", + "## Case study from Hudson bay\n", + "\n", + "Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why?\n", + "We shall demonstrate the scientific method by\n", + "\n", + "1. plotting the data\n", + "\n", + "2. derive a simple model for the population dynamics\n", + "\n", + "3. (fitting parameters in the model to the data)\n", + "\n", + "4. using the model predict the evolution other predator-pray systems\n", + "\n", + "\n", + "\n", + "## Hudson bay data\n", + "\n", + "\n", + "% if FORMAT == 'ipynb':\n", + "Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle.\n", + "\n", + "One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920.\n", + "% endif\n", + "\n", + "
| Year | Hares (x1000) | Lynx (x1000) |
|---|---|---|
| 1900 | 30.0 | 4.0 |
| 1901 | 47.2 | 6.1 |
| 1902 | 70.2 | 9.8 |
| 1903 | 77.4 | 35.2 |
| 1904 | 36.3 | 59.4 |
| 1905 | 20.6 | 41.7 |
| 1906 | 18.1 | 19.0 |
| 1907 | 21.4 | 13.0 |
| 1908 | 22.0 | 8.3 |
| 1909 | 25.4 | 9.1 |
| 1910 | 27.1 | 7.4 |
| 1911 | 40.3 | 8.0 |
| 1912 | 57 | 12.3 |
| 1913 | 76.6 | 19.5 |
| 1914 | 52.3 | 45.7 |
| 1915 | 19.5 | 51.1 |
| 1916 | 11.2 | 29.7 |
| 1917 | 7.6 | 15.8 |
| 1918 | 14.6 | 9.7 |
| 1919 | 16.2 | 10.1 |
| 1920 | 24.7 | 8.6 |
\n",
+ "\n",
+ "\n",
+ "\n",
+ "% endif\n",
+ "\n",
+ "\n",
+ "## Why now create a computer model for the hare and lynx populations?\n",
+ "% if FORMAT == 'ipynb':\n",
+ "We see from the plot that there are indeed fluctuations.\n",
+ "We would like to create a mathematical model that explains these\n",
+ "population fluctuations. Ecologists have predicted that in a simple\n",
+ "predator-prey system that a rise in prey population is followed (with\n",
+ "a lag) by a rise in the predator population. When the predator\n",
+ "population is sufficiently high, then the prey population begins\n",
+ "dropping. After the prey population falls, then the predator\n",
+ "population falls, which allows the prey population to recover and\n",
+ "complete one cycle of this interaction. Thus, we see that\n",
+ "qualitatively oscillations occur. Can a mathematical model predict\n",
+ "this? What causes cycles to slow or speed up? What affects the\n",
+ "amplitude of the oscillation or do you expect to see the oscillations\n",
+ "damp to a stable equilibrium? The models tend to ignore factors like\n",
+ "climate and other complicating factors. How significant are these?\n",
+ "% else:\n",
+ " * We see oscillations in the data\n",
+ "\n",
+ " * What causes cycles to slow or speed up?\n",
+ "\n",
+ " * What affects the amplitude of the oscillation or do you expect to see the oscillations damp to a stable equilibrium?\n",
+ "\n",
+ " * With a model we can better *understand the data*\n",
+ "\n",
+ " * More important: we can understand the ecology dynamics of\n",
+ " predator-pray populations\n",
+ "\n",
+ "% endif\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "## The traditional (top-down) approach\n",
+ "\n",
+ "The classical way (in all books) is to present the Lotka-Volterra equations:"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\begin{align*}\n",
+ "\\frac{dH}{dt} &= H(a - b L)\\\\\n",
+ "\\frac{dL}{dt} &= - L(d - c H)\n",
+ "\\end{align*}\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Here,\n",
+ "\n",
+ " * $H$ is the number of preys\n",
+ "\n",
+ " * $L$ the number of predators\n",
+ "\n",
+ " * $a$, $b$, $d$, $c$ are parameters\n",
+ "\n",
+ "Most books quickly establish the model and then use considerable space on\n",
+ "discussing the qualitative properties of this *nonlinear system of\n",
+ "ODEs* (which cannot be solved)\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "## The \"new\" discrete bottom-up approach\n",
+ "\n",
+ "**The bottom-up approach.**\n",
+ "\n",
+ " * Start with experimental data and discuss the methods which have been used to collect the data, the assumptions, the electronic devices, the aims etc. That is, expose the students to the theory and assumptions behind the data that have been collected and motivate for the scientific method.\n",
+ "\n",
+ " * Where appropriate the students should do the experiment(s) needed to collect the data.\n",
+ "\n",
+ " * The first programming tasks are to read and visualize the data to see if there are patterns or regularities. This strengthens a research-driven intuition.\n",
+ "\n",
+ " * Now we want to increase the understanding through modeling.\n",
+ "\n",
+ " * Most of the biology lies in the *derivation* of the model. We shall\n",
+ " focus on an intuitive discrete approach that leads to difference\n",
+ " equations that can be programmed *and solved* directly.\n",
+ "\n",
+ "\n",
+ "\n",
+ "## Basic (computer-friendly) mathematics notation\n",
+ " * Time points: $t_0,t_1,\\ldots,t_m$\n",
+ "\n",
+ " * Uniform distribution of time points: $t_n=n\\Delta t$\n",
+ "\n",
+ " * $H^n$: population of hares at time $t_n$\n",
+ "\n",
+ " * $L^n$: population of lynx at time $t_n$\n",
+ "\n",
+ " * We want to model the changes in populations, $\\Delta H=H^{n+1}-H^n$\n",
+ " and $\\Delta L=L^{n+1}-L^n$ during a general time interval $[t_{n+1},t_n]$\n",
+ " of length $\\Delta t=t_{n+1}-t_n$\n",
+ "\n",
+ "\n",
+ "\n",
+ "## Basic dynamics of the population of hares\n",
+ "\n",
+ "The population of hares evolves due to births and deaths exactly as a bacteria population:"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\Delta H = a \\Delta t H^n\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "However, hares have an additional loss in the population because\n",
+ "they are eaten by lynx.\n",
+ "All the hares and lynx can form\n",
+ "$H\\cdot L$ pairs in total. When such pairs meet during a time\n",
+ "interval $\\Delta t$, there is some\n",
+ "small probablity that the lynx will eat the hare.\n",
+ "So in fraction $b\\Delta t HL$, the lynx eat hares. This\n",
+ "loss of hares and must be accounted for:\n",
+ "subtracted in the equation for hares:"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\Delta H = a\\Delta t H^n - b \\Delta t H^nL^n\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Basic dynamics of the population of lynx\n",
+ "\n",
+ "We assume that the primary growth for the lynx population depends on sufficient food for raising lynx kittens, which implies an adequate source of nutrients from predation on hares. Thus, the growth of the lynx population does not only depend of how many lynx there are, but on how many hares they can eat.\n",
+ "In a time interval $\\Delta t HL$ hares and lynx can meet, and in a\n",
+ "fraction $b\\Delta t HL$ the lynx eats the hare. All of this does not\n",
+ "contribute to the growth of lynx, again just a fraction of\n",
+ "$b\\Delta t HL$ that we write as\n",
+ "$d\\Delta t HL$. In addition, lynx die just as in the population\n",
+ "dynamics with one isolated animal population, leading to a loss\n",
+ "$-c\\Delta t L$.\n",
+ "\n",
+ "\n",
+ "\n",
+ "The accounting of lynx then looks like"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\Delta L = d\\Delta t H^nL^n - c\\Delta t L^n\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Evolution equations\n",
+ "\n",
+ "By writing up the definition of $\\Delta H$ and $\\Delta L$, and putting\n",
+ "all assumed known terms $H^n$ and $L^n$ on the right-hand side, we have"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "H^{n+1} = H^n + a\\Delta t H^n - b\\Delta t H^n L^n\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "L^{n+1} = L^n + d\\Delta t H^nL^n - c\\Delta t L^n\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Note:\n",
+ "\n",
+ " * These equations are ready to be implemented!\n",
+ "\n",
+ " * But to start, we need $H^0$ and $L^0$ \n",
+ " (which we can get from the data)\n",
+ "\n",
+ " * We also need values for $a$, $b$, $d$, $c$\n",
+ "\n",
+ "\n",
+ "\n",
+ "## Adapt the model to the Hudson Bay case\n",
+ "\n",
+ " * As always, models tend to be general - as here, applicable\n",
+ " to \"all\" predator-pray systems\n",
+ "\n",
+ " * The critical issue is whether the *interaction* between hares and lynx\n",
+ " is sufficiently well modeled by $\\hbox{const}HL$\n",
+ "\n",
+ " * The parameters $a$, $b$, $d$, and $c$ must be\n",
+ " estimated from data\n",
+ "\n",
+ " * Measure time in years\n",
+ "\n",
+ " * $t_0=1900$, $t_m=1920$\n",
+ "\n",
+ "\n",
+ "\n",
+ "## The program"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt\n",
+ "\n",
+ "def solver(m, H0, L0, dt, a, b, c, d, t0):\n",
+ " \"\"\"Solve the difference equations for H and L over m years\n",
+ " with time step dt (measured in years.\"\"\"\n",
+ "\n",
+ " num_intervals = int(m/float(dt))\n",
+ " t = np.linspace(t0, t0 + m, num_intervals+1)\n",
+ " H = np.zeros(t.size)\n",
+ " L = np.zeros(t.size)\n",
+ "\n",
+ " print 'Init:', H0, L0, dt\n",
+ " H[0] = H0\n",
+ " L[0] = L0\n",
+ "\n",
+ " for n in range(0, len(t)-1):\n",
+ " H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n]\n",
+ " L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n]\n",
+ " return H, L, t\n",
+ "\n",
+ "# Load in data file\n",
+ "data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1)\n",
+ "# Make arrays containing x-axis and hares and lynx populations\n",
+ "t_e = data[:,0]\n",
+ "H_e = data[:,1]\n",
+ "L_e = data[:,2]\n",
+ "\n",
+ "# Simulate using the model\n",
+ "H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1,\n",
+ " a=0.4807, b=0.02482, c=0.9272, d=0.02756,\n",
+ " t0=1900)\n",
+ "\n",
+ "# Visualize simulations and data\n",
+ "plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--')\n",
+ "plt.xlabel('Year')\n",
+ "plt.ylabel('Numbers of hares and lynx')\n",
+ "plt.axis([1900, 1920, 0, 140])\n",
+ "plt.title(r'Population of hares and lynx 1900-1920 (x1000)')\n",
+ "plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left')\n",
+ "plt.savefig('Hudson_Bay_sim.pdf')\n",
+ "plt.savefig('Hudson_Bay_sim.png')\n",
+ "plt.show()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "% if FORMAT != 'ipynb':\n",
+ "## The plot\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "
\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "% else:\n",
+ "If we perform a least-square fitting, we can find optimal values for the parameters $a$, $b$, $d$, $c$. The optimal parameters are $a=0.4807$, $b=0.02482$, $d=0.9272$ and $c=0.02756$. These parameters result in a slightly modified initial conditions, namely $H(0) = 34.91$ and $L(0)=3.857$. With these parameters we are now ready to solve the equations and plot these data together with the experimental values.\n",
+ "% endif"
+ ]
}
],
"metadata": {},
diff --git a/doc/pub/How2ReadData/ipynb/ipynb-How2ReadData-src.tar.gz b/doc/pub/How2ReadData/ipynb/ipynb-How2ReadData-src.tar.gz
index 152d5a1b3..0fa3383aa 100644
Binary files a/doc/pub/How2ReadData/ipynb/ipynb-How2ReadData-src.tar.gz and b/doc/pub/How2ReadData/ipynb/ipynb-How2ReadData-src.tar.gz differ
diff --git a/doc/pub/How2ReadData/pdf/How2ReadData-beamer-handouts2x3.pdf b/doc/pub/How2ReadData/pdf/How2ReadData-beamer-handouts2x3.pdf
index e40bd1f11..87483f064 100644
Binary files a/doc/pub/How2ReadData/pdf/How2ReadData-beamer-handouts2x3.pdf and b/doc/pub/How2ReadData/pdf/How2ReadData-beamer-handouts2x3.pdf differ
diff --git a/doc/pub/How2ReadData/pdf/How2ReadData-beamer.pdf b/doc/pub/How2ReadData/pdf/How2ReadData-beamer.pdf
index c9a67f747..e09e901d6 100644
Binary files a/doc/pub/How2ReadData/pdf/How2ReadData-beamer.pdf and b/doc/pub/How2ReadData/pdf/How2ReadData-beamer.pdf differ
diff --git a/doc/pub/How2ReadData/pdf/How2ReadData-minted.pdf b/doc/pub/How2ReadData/pdf/How2ReadData-minted.pdf
index 41487143a..9648da49b 100644
Binary files a/doc/pub/How2ReadData/pdf/How2ReadData-minted.pdf and b/doc/pub/How2ReadData/pdf/How2ReadData-minted.pdf differ
diff --git a/doc/src/How2ReadData/How2ReadData.do.txt b/doc/src/How2ReadData/How2ReadData.do.txt
index 63f0d7751..70ec63b82 100644
--- a/doc/src/How2ReadData/How2ReadData.do.txt
+++ b/doc/src/How2ReadData/How2ReadData.do.txt
@@ -168,6 +168,280 @@ plt.show()
!eblock
+!split
+===== Binning of data =====
+Use the Hudson bay model to load data and make plot in R and with python
+
+
+
+
+!split
+===== Adding model complexity: Predator-Prey model from ecology =====
+
+!bblock
+The population dynamics of a simple predator-prey system is a
+classical example shown in many biology textbooks when ecological
+systems are discussed. The system contains all elements of the
+scientific method:
+
+ * The set up of a specific hypothesis combined with
+ * the experimental methods needed (one can study existing data or perform experiments)
+ * analyzing and interpreting the data and performing further experiments if needed
+ * trying to extract general behaviors and extract eventual laws or patterns
+ * develop mathematical relations for the uncovered regularities/laws and test these by per forming new experiments
+!eblock
+
+
+!split
+===== Case study from Hudson bay =====
+
+!bblock
+Lots of data about populations of hares and lynx collected from furs in Hudson Bay, Canada, are available. It is known that the populations oscillate. Why?
+We shall demonstrate the scientific method by
+
+ o plotting the data
+ o derive a simple model for the population dynamics
+ o (fitting parameters in the model to the data)
+ o using the model predict the evolution other predator-pray systems
+!eblock
+
+!split
+===== Hudson bay data =====
+
+
+!bblock
+% if FORMAT == 'ipynb':
+Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle.
+
+One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920.
+% endif
+
+|------------------------------------------------------|
+| Year | Hares (x1000) | Lynx (x1000)|
+|---------l-----------------------r--------------r------|
+| 1900 | 30.0 | 4.0 |
+| 1901 | 47.2 | 6.1 |
+| 1902 | 70.2 | 9.8 |
+| 1903 | 77.4 | 35.2 |
+| 1904 | 36.3 | 59.4 |
+| 1905 | 20.6 | 41.7 |
+| 1906 | 18.1 | 19.0 |
+| 1907 | 21.4 | 13.0 |
+| 1908 | 22.0 | 8.3 |
+| 1909 | 25.4 | 9.1 |
+| 1910 | 27.1 | 7.4 |
+| 1911 | 40.3 | 8.0 |
+| 1912 | 57 | 12.3 |
+| 1913 | 76.6 | 19.5 |
+| 1914 | 52.3 | 45.7 |
+| 1915 | 19.5 | 51.1 |
+| 1916 | 11.2 | 29.7 |
+| 1917 | 7.6 | 15.8 |
+| 1918 | 14.6 | 9.7 |
+| 1919 | 16.2 | 10.1 |
+| 1920 | 24.7 | 8.6 |
+|------------------------------------------------------|
+
+!eblock
+
+
+
+!split
+===== Plotting the data =====
+
+!bblock
+@@@CODE src/plot_Hudson.py
+!eblock
+
+% if FORMAT != 'ipynb':
+!split
+===== Hares and lynx in Hudson bay from 1900 to 1920 =====
+
+FIGURE: [fig/Hudson_Bay_data, width=700 frac=0.9]
+% endif
+
+
+!split
+===== Why now create a computer model for the hare and lynx populations? =====
+!bblock
+% if FORMAT == 'ipynb':
+We see from the plot that there are indeed fluctuations.
+We would like to create a mathematical model that explains these
+population fluctuations. Ecologists have predicted that in a simple
+predator-prey system that a rise in prey population is followed (with
+a lag) by a rise in the predator population. When the predator
+population is sufficiently high, then the prey population begins
+dropping. After the prey population falls, then the predator
+population falls, which allows the prey population to recover and
+complete one cycle of this interaction. Thus, we see that
+qualitatively oscillations occur. Can a mathematical model predict
+this? What causes cycles to slow or speed up? What affects the
+amplitude of the oscillation or do you expect to see the oscillations
+damp to a stable equilibrium? The models tend to ignore factors like
+climate and other complicating factors. How significant are these?
+% else:
+ * We see oscillations in the data
+ * What causes cycles to slow or speed up?
+ * What affects the amplitude of the oscillation or do you expect to see the oscillations damp to a stable equilibrium?
+ * With a model we can better *understand the data*
+ * More important: we can understand the ecology dynamics of
+ predator-pray populations
+% endif
+!eblock
+
+
+!split
+===== The traditional (top-down) approach =====
+
+!bblock
+The classical way (in all books) is to present the Lotka-Volterra equations:
+
+!bt
+\begin{align*}
+\frac{dH}{dt} &= H(a - b L)\\
+\frac{dL}{dt} &= - L(d - c H)
+\end{align*}
+!et
+
+Here,
+
+ * $H$ is the number of preys
+ * $L$ the number of predators
+ * $a$, $b$, $d$, $c$ are parameters
+
+Most books quickly establish the model and then use considerable space on
+discussing the qualitative properties of this *nonlinear system of
+ODEs* (which cannot be solved)
+!eblock
+
+
+!split
+===== The ``new'' discrete bottom-up approach =====
+
+!bblock The bottom-up approach
+!bpop
+ * Start with experimental data and discuss the methods which have been used to collect the data, the assumptions, the electronic devices, the aims etc. That is, expose the students to the theory and assumptions behind the data that have been collected and motivate for the scientific method.
+ * Where appropriate the students should do the experiment(s) needed to collect the data.
+ * The first programming tasks are to read and visualize the data to see if there are patterns or regularities. This strengthens a research-driven intuition.
+ * Now we want to increase the understanding through modeling.
+ * Most of the biology lies in the *derivation* of the model. We shall
+ focus on an intuitive discrete approach that leads to difference
+ equations that can be programmed *and solved* directly.
+!epop
+!eblock
+
+!split
+===== Basic (computer-friendly) mathematics notation =====
+!bblock
+ * Time points: $t_0,t_1,\ldots,t_m$
+ * Uniform distribution of time points: $t_n=n\Delta t$
+ * $H^n$: population of hares at time $t_n$
+ * $L^n$: population of lynx at time $t_n$
+ * We want to model the changes in populations, $\Delta H=H^{n+1}-H^n$
+ and $\Delta L=L^{n+1}-L^n$ during a general time interval $[t_{n+1},t_n]$
+ of length $\Delta t=t_{n+1}-t_n$
+!eblock
+
+!split
+===== Basic dynamics of the population of hares =====
+
+!bblock
+The population of hares evolves due to births and deaths exactly as a bacteria population:
+
+!bt
+\[
+\Delta H = a \Delta t H^n
+\]
+!et
+However, hares have an additional loss in the population because
+they are eaten by lynx.
+All the hares and lynx can form
+$H\cdot L$ pairs in total. When such pairs meet during a time
+interval $\Delta t$, there is some
+small probablity that the lynx will eat the hare.
+So in fraction $b\Delta t HL$, the lynx eat hares. This
+loss of hares and must be accounted for:
+subtracted in the equation for hares:
+
+!bt
+\[ \Delta H = a\Delta t H^n - b \Delta t H^nL^n\]
+!et
+!eblock
+
+!split
+===== Basic dynamics of the population of lynx =====
+
+!bblock
+We assume that the primary growth for the lynx population depends on sufficient food for raising lynx kittens, which implies an adequate source of nutrients from predation on hares. Thus, the growth of the lynx population does not only depend of how many lynx there are, but on how many hares they can eat.
+In a time interval $\Delta t HL$ hares and lynx can meet, and in a
+fraction $b\Delta t HL$ the lynx eats the hare. All of this does not
+contribute to the growth of lynx, again just a fraction of
+$b\Delta t HL$ that we write as
+$d\Delta t HL$. In addition, lynx die just as in the population
+dynamics with one isolated animal population, leading to a loss
+$-c\Delta t L$.
+!eblock
+
+!bblock
+The accounting of lynx then looks like
+!bt
+\[ \Delta L = d\Delta t H^nL^n - c\Delta t L^n\]
+!et
+!eblock
+
+!split
+===== Evolution equations =====
+
+!bblock
+By writing up the definition of $\Delta H$ and $\Delta L$, and putting
+all assumed known terms $H^n$ and $L^n$ on the right-hand side, we have
+
+!bt
+\[ H^{n+1} = H^n + a\Delta t H^n - b\Delta t H^n L^n \]
+!et
+
+!bt
+\[ L^{n+1} = L^n + d\Delta t H^nL^n - c\Delta t L^n \]
+!et
+
+Note:
+
+ * These equations are ready to be implemented!
+ * But to start, we need $H^0$ and $L^0$