algorithms Module
Numba-accelerated simulation algorithms and helper functions.
Overview
The algorithms module provides Numba-accelerated helper functions for computing mating/sperm matrices, updating sperm storage, generating offspring distributions, and other population genetics operations.
Complete Module Reference
natal.algorithms
Simulation helpers used by cohort-based (absolute population size) population simulations.
This module provides Numba-accelerated helper functions for computing
mating/sperm matrices, updating sperm storage and occupancy, generating
offspring distributions, and other population genetics operations. All
functions are written to be shape-defensive and to integrate with the
PopulationState data structures.
compute_mating_probability_matrix
compute_mating_probability_matrix(sexual_selection_matrix: Annotated[NDArray[float64], 'shape=(g,g)'], male_counts: Annotated[NDArray[float64], 'shape=(g,)'], n_genotypes: int) -> Annotated[NDArray[np.float64], 'shape=(g,g)']
Compute a row-normalized mating probability matrix.
The function computes A = alpha * diag(M) (implemented as column-wise scaling) and returns a row-normalized matrix P where each row sums to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sexual_selection_matrix
|
Annotated[NDArray[float64], 'shape=(g,g)']
|
Preference weights with shape |
required |
male_counts
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male counts vector with shape |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
Returns:
| Type | Description |
|---|---|
Annotated[NDArray[float64], 'shape=(g,g)']
|
np.ndarray: Row-normalized mating probability matrix |
Source code in src/natal/algorithms.py
sample_mating
sample_mating(female_counts: Annotated[NDArray[float64], 'shape=(A,g)'], sperm_store: Annotated[NDArray[float64], 'shape=(A,g,g)'], mating_prob: Annotated[NDArray[float64], 'shape=(g,g)'], female_mating_rates_by_age: Annotated[NDArray[float64], 'shape=(A,)'], sperm_displacement_rate: float, adult_start_idx: int, n_ages: int, n_genotypes: int, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> Annotated[NDArray[np.float64], 'shape=(A,g,g)']
Vectorized version: batch sampling of mating events (monogamous). (67.0x speedup)
Assumption: Each female mates at most once per tick. Sampling process consists of two steps: 1. Determine how many females of each genotype participate in mating (Binomial) 2. These mating females choose which male genotype to mate with (Multinomial)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_counts
|
Annotated[NDArray[float64], 'shape=(A,g)']
|
Female counts array with shape (A, g) where A is number of ages |
required |
sperm_store
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage array with shape (A, g, g) tracking mated females by male genotype |
required |
mating_prob
|
Annotated[NDArray[float64], 'shape=(g,g)']
|
Mating probability matrix with shape (g, g) |
required |
female_mating_rates_by_age
|
Annotated[NDArray[float64], 'shape=(A,)']
|
Age-specific female mating rates with shape (A,) |
required |
sperm_displacement_rate
|
float
|
Rate controlling remating displacement.
The effective remating probability is
|
required |
adult_start_idx
|
int
|
Starting age index for adults |
required |
n_ages
|
int
|
Total number of age classes |
required |
n_genotypes
|
int
|
Number of genotypes g |
required |
is_stochastic
|
bool
|
If True, use stochastic sampling; if False, use deterministic expectations |
True
|
use_continuous_sampling
|
bool
|
If True and is_stochastic=True, use Dirichlet distribution instead of discrete sampling. Currently not implemented (will use discrete). |
False
|
Returns:
| Type | Description |
|---|---|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Updated sperm storage array with shape (A, g, g) containing mated female allocations |
Note
S[a, gf, :]is interpreted as a partition of mated females of (age=a, female_genotype=gf) by male genotype. - Virgins are represented implicitly as:virgins = female_count[a, gf] - sum_gm(S[a, gf, gm])- Historical sperm storage is preserved across ticks. Remating displaces an expected fraction of existing sperm allocation, then adds newly formed matings.- Previous implementation used a single scalar mating rate for all
adult ages. Now we use age-specific female mating rates:
p_mating(age) = female_mating_rates_by_age[age].
Source code in src/natal/algorithms.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
compute_offspring_probability_tensor
compute_offspring_probability_tensor(meiosis_f: Annotated[NDArray[float64], 'shape=(g,hl)'], meiosis_m: Annotated[NDArray[float64], 'shape=(g,hl)'], haplo_to_genotype_map: Annotated[NDArray[float64], 'shape=(hl,hl,g)'], n_genotypes: int, n_haplogenotypes: int, n_glabs: int = 1) -> Annotated[NDArray[np.float64], 'shape=(g,g,g)']
Precompute offspring genotype probabilities for all (gf, gm) pairs.
Constructs the tensor P[gf, gm, g_off] where each entry represents the probability of offspring genotype g_off from the cross (gf × gm).
The computation leverages tensor operations to compute all gamete pairs simultaneously: P(g_off | gf, gm) = Σ_hf,hm P(hf | gf) × P(hm | gm) × I[hf ⊗ hm = g_off]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meiosis_f
|
Annotated[NDArray[float64], 'shape=(g,hl)']
|
Female meiosis probability tensor with shape (g, hl), where entry [g, h] = P(haplotype h | genotype g) for females. |
required |
meiosis_m
|
Annotated[NDArray[float64], 'shape=(g,hl)']
|
Male meiosis probability tensor with shape (g, hl), where entry [g, h] = P(haplotype h | genotype g) for males. |
required |
haplo_to_genotype_map
|
Annotated[NDArray[float64], 'shape=(hl,hl,g)']
|
Haplotype-pair to diploid-genotype mapping with shape (hl, hl, g). Entry [h1, h2, g] = 1 if haplotypes h1, h2 combine to form genotype g, else 0. |
required |
n_genotypes
|
int
|
Number of diploid genotypes. |
required |
n_haplogenotypes
|
int
|
Number of haploid genotypes. |
required |
n_glabs
|
int
|
Number of gamete-label variants per haplotype (default 1). If > 1, the total haplotype space is hl = n_haplogenotypes * n_glabs. |
1
|
Returns:
| Type | Description |
|---|---|
Annotated[NDArray[float64], 'shape=(g,g,g)']
|
Offspring probability tensor with shape (g, g, g), where |
Annotated[NDArray[float64], 'shape=(g,g,g)']
|
out[gf, gm, g_off] = P(g_off | gf, gm). |
Source code in src/natal/algorithms.py
fertilize_with_precomputed_offspring_probability_and_age_specific_reproduction
fertilize_with_precomputed_offspring_probability_and_age_specific_reproduction(female_counts: Annotated[NDArray[float64], 'shape=(A,g)'], sperm_storage_by_male_genotype: Annotated[NDArray[float64], 'shape=(A,g,g)'], fertility_f: Annotated[NDArray[float64], 'shape=(g,)'], fertility_m: Annotated[NDArray[float64], 'shape=(g,)'], offspring_probability: Annotated[NDArray[float64], 'shape=(g,g,g)'], average_eggs_per_wt_female: float, adult_start_idx: int, n_ages: int, n_genotypes: int, n_haplogenotypes: int, female_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], male_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], female_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], male_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], n_glabs: int = 1, age_based_reproduction_rates: Optional[NDArray[float64]] = None, female_age_based_relative_fertility: Optional[NDArray[float64]] = None, fixed_eggs: bool = False, sex_ratio: float = 0.5, has_sex_chromosomes: bool = False, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> tuple[Annotated[NDArray[np.float64], 'shape=(g,)'], Annotated[NDArray[np.float64], 'shape=(g,)']]
Public interface for fertilization with age-specific reproduction rates.
This function ensures consistency between equilibrium inference and actual reproduction by using age-specific reproduction rates and relative fertility rates directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_counts
|
Annotated[NDArray[float64], 'shape=(A,g)']
|
Female counts array (unused, for API compatibility). |
required |
sperm_storage_by_male_genotype
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage array. |
required |
fertility_f
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female fertility rates. |
required |
fertility_m
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male fertility rates. |
required |
offspring_probability
|
Annotated[NDArray[float64], 'shape=(g,g,g)']
|
Precomputed offspring probability tensor. |
required |
average_eggs_per_wt_female
|
float
|
Expected eggs per wild-type female. |
required |
adult_start_idx
|
int
|
First reproductive age class. |
required |
n_ages
|
int
|
Total number of age classes. |
required |
n_genotypes
|
int
|
Number of diploid genotypes. |
required |
n_haplogenotypes
|
int
|
Unused parameter for API compatibility. |
required |
female_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female sex-compatibility weights. |
required |
male_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male sex-compatibility weights. |
required |
female_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Female-only genotype mask. |
required |
male_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Male-only genotype mask. |
required |
n_glabs
|
int
|
Unused parameter for API compatibility. |
1
|
age_based_reproduction_rates
|
Optional[NDArray[float64]]
|
Age-specific reproduction participation rates. |
None
|
female_age_based_relative_fertility
|
Optional[NDArray[float64]]
|
Age-specific relative fertility rates. |
None
|
fixed_eggs
|
bool
|
Whether to use fixed egg counts. |
False
|
sex_ratio
|
float
|
Offspring female ratio. |
0.5
|
has_sex_chromosomes
|
bool
|
Whether offspring sex is genotype-constrained. |
False
|
is_stochastic
|
bool
|
Whether to sample stochastically. |
True
|
use_continuous_sampling
|
bool
|
Whether to use continuous sampling. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple containing female and male offspring counts. |
Source code in src/natal/algorithms.py
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 | |
fertilize_with_mating_genotype
fertilize_with_mating_genotype(female_counts: Annotated[NDArray[float64], 'shape=(A,g)'], sperm_storage_by_male_genotype: Annotated[NDArray[float64], 'shape=(A,g,g)'], fertility_f: Annotated[NDArray[float64], 'shape=(g,)'], fertility_m: Annotated[NDArray[float64], 'shape=(g,)'], meiosis_f: Annotated[NDArray[float64], 'shape=(g,hl)'], meiosis_m: Annotated[NDArray[float64], 'shape=(g,hl)'], haplo_to_genotype_map: Annotated[NDArray[float64], 'shape=(hl,hl,g)'], average_eggs_per_wt_female: float, adult_start_idx: int, n_ages: int, n_genotypes: int, n_haplogenotypes: int, female_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], male_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], female_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], male_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], n_glabs: int = 1, age_based_reproduction_rates: Optional[NDArray[float64]] = None, female_age_based_relative_fertility: Optional[NDArray[float64]] = None, fixed_eggs: bool = False, sex_ratio: float = 0.5, has_sex_chromosomes: bool = False, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> tuple[Annotated[NDArray[np.float64], 'shape=(g,)'], Annotated[NDArray[np.float64], 'shape=(g,)']]
Fertilization using meiosis matrices (on-the-fly probability computation).
Vectorized offspring generation with batch multinomial sampling. This variant computes offspring probabilities on-the-fly from meiosis matrices, then delegates to the core _fertilize_with_precomputed_offspring_probability kernel.
Achieves ~60.9x speedup through vectorization: - Pre-compute expected egg counts per mating pair - Batch Poisson sampling to avoid individual per-pair sampling - Single multinomial draw per viable egg count - Vectorized accumulation of genotype counts
Sex-chromosome compatibility is inferred from meiosis row sums: genotypes that cannot produce gametes of one sex are marked as sex-incompatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_counts
|
Annotated[NDArray[float64], 'shape=(A,g)']
|
Female genotype counts, shape (A, g) (unused, for API compatibility). |
required |
sperm_storage_by_male_genotype
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage reservoir, shape (A, g, g). |
required |
fertility_f
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female fertility rates relative to wild-type, shape (g,). |
required |
fertility_m
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male fertility rates relative to wild-type, shape (g,). |
required |
meiosis_f
|
Annotated[NDArray[float64], 'shape=(g,hl)']
|
Female meiosis probabilities (genotype → haplotype), shape (g, hl). Row sums indicate whether a genotype can produce female gametes. |
required |
meiosis_m
|
Annotated[NDArray[float64], 'shape=(g,hl)']
|
Male meiosis probabilities (genotype → haplotype), shape (g, hl). Row sums indicate whether a genotype can produce male gametes. |
required |
haplo_to_genotype_map
|
Annotated[NDArray[float64], 'shape=(hl,hl,g)']
|
Haplotype pair → genotype membership, shape (hl, hl, g). |
required |
average_eggs_per_wt_female
|
float
|
Expected eggs per reproducing wild-type female. |
required |
adult_start_idx
|
int
|
First reproductive age class. |
required |
n_ages
|
int
|
Total age classes. |
required |
n_genotypes
|
int
|
Number of diploid genotypes. |
required |
n_haplogenotypes
|
int
|
Number of haploid genotypes. |
required |
female_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female compatibility weight per genotype. |
required |
male_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male compatibility weight per genotype. |
required |
female_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Precomputed female-only genotype mask. |
required |
male_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Precomputed male-only genotype mask. |
required |
n_glabs
|
int
|
Gamete-label variants per haplotype (default 1). |
1
|
age_based_reproduction_rates
|
Optional[NDArray[float64]]
|
Age-specific reproduction rates, shape (age,). |
None
|
female_age_based_relative_fertility
|
Optional[NDArray[float64]]
|
Age-specific relative fertility rates, shape (age,). |
None
|
fixed_eggs
|
bool
|
Use deterministic eggs if True, Poisson if False. |
False
|
sex_ratio
|
float
|
Offspring female fraction (used if no sex-chromosomes). |
0.5
|
has_sex_chromosomes
|
bool
|
Whether offspring sex is genotype-constrained. |
False
|
is_stochastic
|
bool
|
Use sampling if True, deterministic if False. |
True
|
use_continuous_sampling
|
bool
|
Use Beta/Dirichlet if True, Binomial/Multinomial if False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple (n_offspring_female, n_offspring_male) with shape (g,) each. |
Source code in src/natal/algorithms.py
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | |
fertilize_with_precomputed_offspring_probability
fertilize_with_precomputed_offspring_probability(female_counts: Annotated[NDArray[float64], 'shape=(A,g)'], sperm_storage_by_male_genotype: Annotated[NDArray[float64], 'shape=(A,g,g)'], fertility_f: Annotated[NDArray[float64], 'shape=(g,)'], fertility_m: Annotated[NDArray[float64], 'shape=(g,)'], offspring_probability: Annotated[NDArray[float64], 'shape=(g,g,g)'], average_eggs_per_wt_female: float, adult_start_idx: int, n_ages: int, n_genotypes: int, n_haplogenotypes: int, female_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], male_genotype_compatibility: Annotated[NDArray[float64], 'shape=(g,)'], female_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], male_only_by_sex_chrom: Annotated[NDArray[bool_], 'shape=(g,)'], n_glabs: int = 1, fixed_eggs: bool = False, sex_ratio: float = 0.5, has_sex_chromosomes: bool = False, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> tuple[Annotated[NDArray[np.float64], 'shape=(g,)'], Annotated[NDArray[np.float64], 'shape=(g,)']]
Fertilization wrapper using externally precomputed offspring probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_counts
|
Annotated[NDArray[float64], 'shape=(A,g)']
|
Female counts array with shape (A, g). Reserved for API compatibility with the non-precomputed variant. |
required |
sperm_storage_by_male_genotype
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage array with shape (A, g, g). |
required |
fertility_f
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female fertility rates with shape (g,). |
required |
fertility_m
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male fertility rates with shape (g,). |
required |
offspring_probability
|
Annotated[NDArray[float64], 'shape=(g,g,g)']
|
Precomputed offspring tensor (g, g, g). |
required |
average_eggs_per_wt_female
|
float
|
Average eggs produced per wild-type female. |
required |
adult_start_idx
|
int
|
Starting age index for adults. |
required |
n_ages
|
int
|
Total number of age classes. |
required |
n_genotypes
|
int
|
Number of genotypes. |
required |
n_haplogenotypes
|
int
|
Unused here; kept for signature parity. |
required |
female_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female-compatible weight per genotype.
If sex-chromosome constraints are present, this overrides global
|
required |
male_genotype_compatibility
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male-compatible weight per genotype.
If sex-chromosome constraints are present, this overrides global
|
required |
female_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Precomputed female-only genotype mask. |
required |
male_only_by_sex_chrom
|
Annotated[NDArray[bool_], 'shape=(g,)']
|
Precomputed male-only genotype mask. |
required |
n_glabs
|
int
|
Unused here; kept for signature parity. |
1
|
fixed_eggs
|
bool
|
Whether to use fixed egg counts. |
False
|
sex_ratio
|
float
|
Offspring female ratio. Used only when has_sex_chromosomes is False. |
0.5
|
has_sex_chromosomes
|
bool
|
Whether offspring sex is genotype-constrained. |
False
|
is_stochastic
|
bool
|
Whether to sample stochastically. |
True
|
use_continuous_sampling
|
bool
|
Whether to use continuous sampling. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple containing female and male offspring counts with shape (g,). |
Source code in src/natal/algorithms.py
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | |
compute_age_based_survival_rates
compute_age_based_survival_rates(female_survival_rates: Annotated[NDArray[float64], 'shape=(A,)'], male_survival_rates: Annotated[NDArray[float64], 'shape=(A,)'], n_ages: int) -> Tuple[Annotated[NDArray[np.float64], 'shape=(A,)'], Annotated[NDArray[np.float64], 'shape=(A,)']]
Return age-specific survival rate arrays (no sampling).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)']
|
Female survival rates shape (n_ages,) |
required |
male_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)']
|
Male survival rates shape (n_ages,) |
required |
n_ages
|
int
|
Number of ages |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(A,)'], Annotated[NDArray[float64], 'shape=(A,)']]
|
Tuple[survival_rates_f, survival_rates_m]: Two arrays with shape (n_ages,) |
Source code in src/natal/algorithms.py
compute_viability_survival_rates
compute_viability_survival_rates(female_viability_rates: Annotated[NDArray[float64], 'shape=(g,)'], male_viability_rates: Annotated[NDArray[float64], 'shape=(g,)'], n_genotypes: int, target_age: int, n_ages: int) -> Tuple[Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g)']]
Return viability survival rate matrices (non-zero only at target age).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
female_viability_rates
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Female viability genotype-specific rates shape (g,) |
required |
male_viability_rates
|
Annotated[NDArray[float64], 'shape=(g,)']
|
Male viability genotype-specific rates shape (g,) |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
target_age
|
int
|
Age index where viability is applied |
required |
n_ages
|
int
|
Total number of ages |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']]
|
Tuple[survival_rates_f, survival_rates_m]: Two matrices with shape (n_ages, n_genotypes), all rows are 1.0 except target_age row |
Source code in src/natal/algorithms.py
apply_survival_rates_deterministic
apply_survival_rates_deterministic(population: Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']], female_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], male_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], n_genotypes: int, n_ages: int) -> Tuple[Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g)']]
Deterministically apply survival rates (direct multiplication, no sampling).
Supports two input formats: - 1D array shape (A,): Apply by age, broadcast to all genotypes - 2D array shape (A,g): Directly apply to each (age, genotype)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']]
|
(female, male) tuple |
required |
female_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Female survival rates |
required |
male_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Male survival rates |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
n_ages
|
int
|
Number of ages |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']]
|
Tuple[female_new, male_new]: Population multiplied by survival rates |
Source code in src/natal/algorithms.py
apply_survival_rates_deterministic_with_sperm_storage
apply_survival_rates_deterministic_with_sperm_storage(population: Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']], sperm_store: Annotated[NDArray[float64], 'shape=(A,g,g)'], female_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], male_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], n_genotypes: int, n_ages: int) -> Tuple[Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g,g)']]
Deterministically apply survival rates with consistent scaling of sperm storage (no sampling).
Key: sperm storage is scaled by the same survival rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']]
|
(female, male) tuple |
required |
sperm_store
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage array shape (n_ages, n_genotypes, n_genotypes) |
required |
female_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Female survival rates (supports 1D or 2D) |
required |
male_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Male survival rates (supports 1D or 2D) |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
n_ages
|
int
|
Number of ages |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g,g)']]
|
Tuple[female_new, male_new, sperm_store_new] |
Source code in src/natal/algorithms.py
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 | |
sample_survival_with_sperm_storage
sample_survival_with_sperm_storage(population: Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']], sperm_store: Annotated[NDArray[float64], 'shape=(A,g,g)'], female_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], male_survival_rates: Annotated[NDArray[float64], 'shape=(A,)|(A,g)'], n_genotypes: int, n_ages: int, use_continuous_sampling: bool = False) -> Tuple[Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g)'], Annotated[NDArray[np.float64], 'shape=(A,g,g)']]
Randomly apply survival rates with consistent sampling of sperm storage.
Key: For each (age, gf) pair, use the same sampling result to update individual counts and sperm storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)']]
|
(female, male) tuple |
required |
sperm_store
|
Annotated[NDArray[float64], 'shape=(A,g,g)']
|
Sperm storage array shape (n_ages, n_genotypes, n_genotypes) |
required |
female_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Female survival rates (supports 1D or 2D) |
required |
male_survival_rates
|
Annotated[NDArray[float64], 'shape=(A,)|(A,g)']
|
Male survival rates (supports 1D or 2D) |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
use_continuous_sampling
|
bool
|
If True, use Dirichlet distribution instead of discrete sampling. Currently not implemented (will use discrete). |
False
|
n_ages
|
int
|
Number of ages |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g)'], Annotated[NDArray[float64], 'shape=(A,g,g)']]
|
Tuple[female_new, male_new, sperm_store_new] |
Important implementation notes
- Invariant check for
n_virginsuses raw floating-point mass:n_virgins_raw = F_raw - sum(S_raw). This avoids false negatives from per-cell rounding in stochastic mode. - Discrete sampling still rounds each category to integer trial counts before binomial draws, but only after the raw-mass invariant check.
Source code in src/natal/algorithms.py
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 | |
recruit_juveniles_sampling
recruit_juveniles_sampling(age_0_juvenile_counts: Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']], carrying_capacity: int, n_genotypes: int, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> Tuple[Annotated[NDArray[np.float64], 'shape=(g,)'], Annotated[NDArray[np.float64], 'shape=(g,)']]
Cohort-mode recruitment to carrying capacity.
If total juveniles <= carrying_capacity, returns float copies. If
greater, deterministically scale down to K while preserving genotype proportions
(with remainder distribution), unless is_stochastic is True in which case
exactly K juveniles are sampled by multinomial.
Returns float64 arrays (containing integral values if stochastic).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
age_0_juvenile_counts
|
Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple of (female_0, male_0) age-0 juvenile counts |
required |
carrying_capacity
|
int
|
Carrying capacity K |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
is_stochastic
|
bool
|
If True, use stochastic sampling; if False, use deterministic scaling |
True
|
use_continuous_sampling
|
bool
|
If True and is_stochastic=True, use Dirichlet distribution instead of discrete sampling |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple[female_new, male_new]: Recruited juvenile counts with shape (g,) each |
Source code in src/natal/algorithms.py
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 | |
recruit_juveniles_given_scaling_factor_sampling
recruit_juveniles_given_scaling_factor_sampling(age_0_juvenile_counts: Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']], scaling_factor: float, n_genotypes: int, is_stochastic: bool = True, use_continuous_sampling: bool = False) -> Tuple[Annotated[NDArray[np.float64], 'shape=(g,)'], Annotated[NDArray[np.float64], 'shape=(g,)']]
Scale age-0 juveniles by scaling_factor.
If is_stochastic is True, sample exactly round(total * scaling_factor)
juveniles by multinomial according to genotype-by-sex proportions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
age_0_juvenile_counts
|
Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple of (female_0, male_0) age-0 juvenile counts |
required |
scaling_factor
|
float
|
Scaling factor to apply to total juvenile count |
required |
n_genotypes
|
int
|
Number of genotypes |
required |
is_stochastic
|
bool
|
If True, use stochastic sampling; if False, use deterministic scaling |
True
|
use_continuous_sampling
|
bool
|
If True and is_stochastic=True, use Dirichlet distribution instead of discrete sampling. Currently not implemented (will use discrete). |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[Annotated[NDArray[float64], 'shape=(g,)'], Annotated[NDArray[float64], 'shape=(g,)']]
|
Tuple[female_new, male_new]: Scaled juvenile counts with shape (g,) each. Returns float64 arrays (containing integral values if stochastic). |
Source code in src/natal/algorithms.py
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 | |
compute_equilibrium_metrics
compute_equilibrium_metrics(carrying_capacity: float, expected_eggs_per_female: float, age_based_survival_rates: NDArray[float64], age_based_mating_rates: NDArray[float64], female_age_based_relative_fertility: NDArray[float64], relative_competition_strength: NDArray[float64], sex_ratio: float, new_adult_age: int, n_ages: int, age_based_reproduction_rates: Optional[NDArray[float64]] = None, equilibrium_individual_count: Optional[NDArray[float64]] = None, external_expected_eggs: Optional[float] = None) -> Tuple[float, float]
Calculate competition strength and survival rate metrics under equilibrium.
These metrics are used for LOGISTIC and BEVERTON_HOLT density-dependent modes.
The equilibrium distribution (from equilibrium_individual_count or built from
carrying_capacity) is always used for expected_competition_strength.
The expected_survival_rate is computed as total_age_1 / (produced_age_0 * s_0_avg).
When external_expected_eggs is given, it replaces produced_age_0 in the survival
rate formula but NOT in the competition strength formula (competition uses the actual
equilibrium distribution's egg production).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrying_capacity
|
float
|
Total carrying capacity K based on age=1 |
required |
expected_eggs_per_female
|
float
|
Basic offspring count |
required |
age_based_survival_rates
|
NDArray[float64]
|
Survival rate matrix (2, n_ages) |
required |
age_based_mating_rates
|
NDArray[float64]
|
Mating rate matrix (2, n_ages) |
required |
age_based_reproduction_rates
|
Optional[NDArray[float64]]
|
Female age-specific reproduction participation
rates with shape (n_ages,). If None, falls back to
|
None
|
female_age_based_relative_fertility
|
NDArray[float64]
|
Female age-dependent relative fertility (n_ages,) |
required |
relative_competition_strength
|
NDArray[float64]
|
Competition weights for each age (n_ages,) |
required |
sex_ratio
|
float
|
Sex ratio (female proportion) |
required |
new_adult_age
|
int
|
Adult starting age |
required |
n_ages
|
int
|
Total number of ages |
required |
equilibrium_individual_count
|
Optional[NDArray[float64]]
|
Optional user-provided equilibrium distribution (2, n_ages) |
None
|
external_expected_eggs
|
Optional[float]
|
If provided, overrides |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple[expected_competition_strength, expected_survival_rate] |
Source code in src/natal/algorithms.py
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 | |
compute_scaling_factor_fixed
Calculate scaling factor for FIXED mode.
When total_age_0 > K, scale down proportionally to K; otherwise keep unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_age_0
|
float
|
Total age-0 larvae count |
required |
carrying_capacity
|
float
|
Carrying capacity K |
required |
Returns:
| Type | Description |
|---|---|
float
|
scaling_factor = min(1.0, K / total) |
Source code in src/natal/algorithms.py
compute_actual_competition_strength
compute_actual_competition_strength(juvenile_counts_by_age: NDArray[float64], relative_competition_strength: NDArray[float64], new_adult_age: int) -> float
Compute current total competition strength metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
juvenile_counts_by_age
|
NDArray[float64]
|
Juvenile counts by age with shape (n_ages,) |
required |
relative_competition_strength
|
NDArray[float64]
|
Competition weights for each age with shape (n_ages,) |
required |
new_adult_age
|
int
|
Starting age index for adults |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total competition strength as weighted sum of juvenile counts |
Source code in src/natal/algorithms.py
compute_scaling_factor_logistic
compute_scaling_factor_logistic(actual_competition_strength: float, expected_competition_strength: float, expected_survival_rate: float, low_density_growth_rate: float) -> float
Compute LOGISTIC (LINEAR) mode scaling factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual_competition_strength
|
float
|
Current competition strength |
required |
expected_competition_strength
|
float
|
Expected competition strength at equilibrium |
required |
expected_survival_rate
|
float
|
Expected survival rate at equilibrium |
required |
low_density_growth_rate
|
float
|
Growth rate at low population density |
required |
Returns:
| Type | Description |
|---|---|
float
|
Scaling factor for larval recruitment in LOGISTIC mode |
Source code in src/natal/algorithms.py
compute_scaling_factor_beverton_holt
compute_scaling_factor_beverton_holt(actual_competition_strength: float, expected_competition_strength: float, expected_survival_rate: float, low_density_growth_rate: float) -> float
Compute BEVERTON_HOLT (CONCAVE) mode scaling factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual_competition_strength
|
float
|
Current competition strength |
required |
expected_competition_strength
|
float
|
Expected competition strength at equilibrium |
required |
expected_survival_rate
|
float
|
Expected survival rate at equilibrium |
required |
low_density_growth_rate
|
float
|
Growth rate at low population density |
required |
Returns:
| Type | Description |
|---|---|
float
|
Scaling factor for larval recruitment in BEVERTON_HOLT mode |