# prize35-10 Exercise 2: fit a species-area relationship S = c * A**z import math areas = [2, 5, 20, 60, 250, 900] # island areas in km2 (invented) species = [9, 12, 20, 27, 41, 59] # species counted (invented) # Least-squares straight line through (ln A, ln S): ln S = ln c + z ln A xs = [math.log(a) for a in areas] ys = [math.log(s) for s in species] n = len(xs) mx, my = sum(xs) / n, sum(ys) / n z = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs) c = math.exp(my - z * mx) print(f"z = {z:.2f}, c = {c:.2f}") def predict(area): return c * area ** z for area in (5, 50, 500): print(f"predicted species on {area:3} km2: {predict(area):5.1f}") print(f"area cut by half: species fall by {(1 - 0.5 ** z) * 100:.1f}%") print(f"area cut to 10%: species fall by {(1 - 0.1 ** z) * 100:.1f}%") # Output: # z = 0.31, c = 7.47 # predicted species on 5 km2: 12.3 # predicted species on 50 km2: 25.0 # predicted species on 500 km2: 50.8 # area cut by half: species fall by 19.2% # area cut to 10%: species fall by 50.8% # Reading it: z is about 0.31, a little above the 0.25 used in the chapter. Halving the area costs about a fifth of the species and # cutting it to a tenth costs about half. The data are invented, so the exact figures only show how the method works.