Skip to content

Instantly share code, notes, and snippets.

@jakelevi1996
Last active January 19, 2024 12:11
Show Gist options
  • Save jakelevi1996/7fd98e2980c9202e429c21505e3466b8 to your computer and use it in GitHub Desktop.
Save jakelevi1996/7fd98e2980c9202e429c21505e3466b8 to your computer and use it in GitHub Desktop.
Compare capture/recapture statistics for different sample sizes and population sizes
from jutility import plotting
import numpy as np
rng = np.random.default_rng(0)
# plotting.set_latex_params()
combined_output_list = []
for population_size in [100, 200, 500, 1000, 2000, 5000, 10000]:
subplots = []
step = int(population_size / 10)
bins = np.linspace(0, 2 * population_size, 50)
num_trials = 1000
for sample_size in range(step, population_size, step):
observation_size = np.zeros(num_trials)
population = np.zeros(population_size)
population[:sample_size] = 1
for trial in range(num_trials):
observation = rng.choice(population, sample_size, replace=False)
observation_size[trial] = observation.sum()
observation_size = observation_size[observation_size > 0]
population_estimate = sample_size * sample_size / observation_size
sp = plotting.Subplot(
plotting.Hist(np.round(population_estimate), bins=bins, color="b"),
axis_properties=plotting.AxisProperties(
xlim=[0, 2*population_size],
title="Sample size = %i" % sample_size,
grid=False,
),
)
subplots.append(sp)
mp = plotting.MultiPlot(
*subplots,
figure_properties=plotting.FigureProperties(
figsize=[8, 6],
title="Population size = %i" % population_size,
top_space=0.15,
),
)
mp.save(
plot_name="Population size = %05i" % population_size,
dir_name=".",
)
mp_image = mp.get_image_array()
sp = plotting.Subplot(plotting.ImShow(mp_image, axis_off=True))
combined_output_list.append(sp)
mp.close()
mp = plotting.MultiPlot(*combined_output_list)
mp.save("Combined output", dir_name=".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment