Script to make data_pops.csv table
authorJustin Kitzes <jkitzes@berkeley.edu>
Tue, 13 Nov 2012 04:00:44 +0000 (20:00 -0800)
committerW. Trevor King <wking@tremily.us>
Fri, 1 Nov 2013 15:32:11 +0000 (08:32 -0700)
python/make_data_pops.py [new file with mode: 0644]

diff --git a/python/make_data_pops.py b/python/make_data_pops.py
new file mode 100644 (file)
index 0000000..68ae79f
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/python
+
+'''
+Make and save logistic growth population data
+'''
+
+from __future__ import division
+import numpy as np
+
+def logistic_growth(r, K, n0, p=0.1):
+    # Set up population array
+    n = np.zeros((npops, nt))
+    n[:,0] = n0
+
+    # Loop through all time steps
+    steps = np.arange(1, nt)
+    for i in steps:
+        cat = (np.random.rand(npops) < p)  # Random catastrophe 10% of time
+        ni = np.round(n[:,i-1] + r*n[:,i-1]*(1 - n[:,i-1]/K))
+        ni[cat] = n0
+        n[:,i] = ni
+    
+    return n
+
+if __name__ == '__main__':
+    nt = 15
+    npops = 50
+    result = logistic_growth(0.6, 100, 10)
+    np.savetxt('data_pops.csv', result, '%i', ',')