# prize35-9 Exercise 3: a diversity index across disturbance rates import math, random random.seed(2) def shannon(p, patches=2000, years=1000): ages = [0] * patches for _ in range(years): ages = [0 if random.random() < p else age + 1 for age in ages] bands = [sum(a < 10 for a in ages), sum(10 <= a < 40 for a in ages), sum(a >= 40 for a in ages)] shares = [b / patches for b in bands if b > 0] return -sum(s * math.log(s) for s in shares) best = None print("disturbance/yr Shannon index") for p in (0.5, 0.2, 0.1, 0.05, 0.03, 0.02, 0.01, 0.005, 0.001): h = shannon(p) print(f"{p:14.3f} {h:13.2f}") if best is None or h > best[1]: best = (p, h) print("highest index at", best[0], "per year:", round(best[1], 2), "(the maximum possible for 3 bands is", round(math.log(3), 2), ")") # Output: # disturbance/yr Shannon index # 0.500 0.01 # 0.200 0.33 # 0.100 0.70 # 0.050 0.98 # 0.030 1.08 # 0.020 1.04 # 0.010 0.81 # 0.005 0.60 # 0.001 0.18 # highest index at 0.03 per year: 1.08 (the maximum possible for 3 bands is 1.1 ) # Reading it: the index is lowest when disturbance is very frequent or very rare and highest at moderate rates (about 0.03 per year here). # This is a toy version of the intermediate disturbance idea: moderate disturbance keeps young, middle and old patches side by side. Real ecosystems are messier, and the idea is debated.