simace.simulation¶
simulate¶
simace.simulation.simulate
¶
ACE pedigree simulation.
Simulates multi-generational pedigrees with: - A: Additive genetic component - C: Common/shared environment component - E: Unique environment component
Supports single-trait and two-trait (bivariate) modes with configurable cross-trait correlations for genetic (rA) and common environment (rC) components.
resolve_per_gen_param
¶
Resolve a variance-component parameter to a per-generation list.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
scalar (constant across all generations) or dict mapping generation index → value. Missing generation keys are forward-filled from the most recent earlier key.
TYPE:
|
G
|
total number of generations to resolve for (indices 0..G-1).
TYPE:
|
name
|
parameter name, used in error messages.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[float]
|
List of length G with the resolved value for each generation. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if any resolved value is negative or if a dict has no key <= 0 (so generation 0 would be undefined). |
Source code in simace/simulation/simulate.py
generate_correlated_components
¶
Generate two correlated normal variables via multivariate normal.
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
numpy random generator
TYPE:
|
n
|
number of samples
TYPE:
|
sd1
|
standard deviation for component 1
TYPE:
|
sd2
|
standard deviation for component 2
TYPE:
|
correlation
|
correlation between components
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(comp1, comp2)
|
tuple of arrays, each shape (n,) |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if sd1 or sd2 is negative, or correlation is outside [-1, 1] |
Source code in simace/simulation/simulate.py
generate_mendelian_noise
¶
Generate correlated Mendelian sampling noise for two traits.
Under the infinitesimal model, the Mendelian sampling variance is 0.5 * Var(A) for each trait, so sd_noise = sd_A / sqrt(2) (Bulmer, 1971, Am. Nat., 105, 201-211).
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
numpy random generator
TYPE:
|
n
|
number of offspring
TYPE:
|
sd_A1
|
standard deviation of A1 (sqrt of A1 variance)
TYPE:
|
sd_A2
|
standard deviation of A2 (sqrt of A2 variance)
TYPE:
|
rA
|
genetic correlation between traits
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(noise1, noise2)
|
tuple of arrays, each shape (n,) |
Source code in simace/simulation/simulate.py
draw_mating_counts
¶
Draw zero-truncated Poisson mating counts for n individuals.
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
numpy random generator
TYPE:
|
n
|
number of individuals
TYPE:
|
mating_lambda
|
Poisson lambda for the ZTP distribution
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of shape |
Source code in simace/simulation/simulate.py
balance_mating_slots
¶
Balance total mating slots between males and females.
Takes T = min(sum(male), sum(female)) and randomly trims the larger
side so both sum to T.
| RETURNS | DESCRIPTION |
|---|---|
tuple[ndarray, ndarray]
|
|
Source code in simace/simulation/simulate.py
pair_partners
¶
Create mating pairs via random bipartite matching.
Expands each sex into slot arrays using np.repeat, shuffles one side,
and pairs positionally. Duplicate (mother, father) pairs are resolved
by swapping conflicting entries.
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
|
Source code in simace/simulation/simulate.py
allocate_offspring
¶
Distribute N offspring across n_matings matings via multinomial.
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of shape |
Source code in simace/simulation/simulate.py
assign_twins
¶
Decide which matings produce an MZ twin pair.
Only matings with >= 2 offspring are eligible. At most one twin pair per mating.
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Boolean mask of shape |
Source code in simace/simulation/simulate.py
mating
¶
mating(rng, parental_sex, mating_lambda, p_mztwin, pheno=None, assort1=0.0, assort2=0.0, rho_w=0.0, assort_matrix=None)
Generate parent-offspring pairings via a modular mating pipeline.
- Separate males/females and draw ZTP(mating_lambda) mating counts.
- Balance slots so both sexes have equal total.
- Pair partners randomly (or assortatively if assort1/assort2 != 0).
- Allocate N offspring across matings via multinomial.
- Assign MZ twins to eligible matings.
- Build output arrays.
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
numpy random generator
TYPE:
|
parental_sex
|
array of sex values (0=female, 1=male) for parents
TYPE:
|
mating_lambda
|
Poisson lambda for zero-truncated mating count distribution
TYPE:
|
p_mztwin
|
probability of a mating producing MZ twins (if >= 2 offspring)
TYPE:
|
pheno
|
(n, 6) array of [A1, C1, E1, A2, C2, E2]; required when assort1 or assort2 is nonzero
TYPE:
|
assort1
|
target mate correlation on trait 1 liability
TYPE:
|
assort2
|
target mate correlation on trait 2 liability
TYPE:
|
rho_w
|
within-person cross-trait liability correlation, used to derive off-diagonal entries of the mate correlation matrix
TYPE:
|
assort_matrix
|
optional full 2x2 mate correlation matrix R_mf
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
parent_idxs
|
(N, 2) array of [mother_idx, father_idx] for each offspring
TYPE:
|
twins
|
(m, 2) array of [twin1_idx, twin2_idx] pairs for MZ twins
TYPE:
|
household_ids
|
(N,) array mapping each offspring to a household index
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if assort is nonzero but pheno is None |
Source code in simace/simulation/simulate.py
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 | |
reproduce
¶
reproduce(rng, pheno, parents, twins, household_ids, sd_A1, sd_E1, sd_C1, sd_A2, sd_E2, sd_C2, rA, rC, rE=0.0)
Simulate offspring phenotypes from parents for two correlated traits.
Additive genetic values are inherited as midparent + Mendelian noise. Common environment (C) is drawn freshly per household — it is NOT inherited from parents but represents the offspring's own shared rearing environment (siblings share C; parents and children do not). Unique environment (E) is drawn independently per individual.
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
numpy random generator
TYPE:
|
pheno
|
(n, 6) array of [A1, C1, E1, A2, C2, E2] for parents
TYPE:
|
parents
|
(n, 2) array of [mother_idx, father_idx]
TYPE:
|
twins
|
array of MZ twin index pairs
TYPE:
|
household_ids
|
(n,) array mapping each offspring to a household
TYPE:
|
sd_A1
|
standard deviation of A for trait 1
TYPE:
|
sd_E1
|
standard deviation of E for trait 1
TYPE:
|
sd_C1
|
standard deviation of C for trait 1
TYPE:
|
sd_A2
|
standard deviation of A for trait 2
TYPE:
|
sd_E2
|
standard deviation of E for trait 2
TYPE:
|
sd_C2
|
standard deviation of C for trait 2
TYPE:
|
rA
|
genetic correlation between traits
TYPE:
|
rC
|
common environment correlation between traits
TYPE:
|
rE
|
unique environment correlation between traits
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
offspring
|
(n, 6) array of [A1, C1, E1, A2, C2, E2]
TYPE:
|
sex_offspring
|
(n,) array of sex values (0=female, 1=male)
TYPE:
|
Source code in simace/simulation/simulate.py
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 860 861 862 863 864 865 866 867 868 869 870 871 | |
add_to_pedigree
¶
Add a generation to the pedigree DataFrame (backward-compatible wrapper).
The internal simulation loop uses pre-allocated arrays for performance. This function is retained for external callers and tests.
Source code in simace/simulation/simulate.py
run_simulation
¶
run_simulation(*, seed, N, G_ped, mating_lambda, p_mztwin, A1, C1, A2, C2, rA, rC, rE=0.0, E1, E2, G_sim=None, assort1=0.0, assort2=0.0, assort_matrix=None)
Run the full ACE simulation for two correlated traits.
Variance components A (additive genetic), C (shared environment), and E (unique environment) are specified as absolute variances. A is constant across generations; C and E may be specified per-generation via a dict mapping generation index to value (forward-filled for missing keys).
| PARAMETER | DESCRIPTION |
|---|---|
seed
|
Random seed
TYPE:
|
N
|
Population size per generation (positive integer)
TYPE:
|
G_ped
|
Number of generations to record in pedigree (integer >= 1)
TYPE:
|
mating_lambda
|
Poisson lambda for zero-truncated mating count distribution (> 0)
TYPE:
|
p_mztwin
|
Probability of a mating producing MZ twins, in [0, 1)
TYPE:
|
A1
|
Trait 1 additive genetic variance (>= 0).
TYPE:
|
C1
|
Trait 1 shared-environment variance (>= 0).
TYPE:
|
A2
|
Trait 2 additive genetic variance (>= 0).
TYPE:
|
C2
|
Trait 2 shared-environment variance (>= 0).
TYPE:
|
rA
|
Genetic correlation between traits, in [-1, 1]
TYPE:
|
rC
|
Common environment correlation between traits, in [-1, 1]
TYPE:
|
rE
|
Unique environment correlation between traits, in [-1, 1]. Default 0 (independent E across traits).
TYPE:
|
E1
|
Trait 1 unique-environment variance. Scalar (constant) or dict mapping generation index → value (forward-filled).
TYPE:
|
E2
|
Trait 2 unique-environment variance. Same format as E1.
TYPE:
|
G_sim
|
Total generations to simulate (default: G_ped). First G_sim - G_ped generations are burn-in and discarded from output.
TYPE:
|
assort1
|
Target mate Pearson correlation on trait 1 liability, in [-1, 1].
TYPE:
|
assort2
|
Target mate Pearson correlation on trait 2 liability, in [-1, 1].
TYPE:
|
assort_matrix
|
Optional full 2x2 mate correlation matrix R_mf. Overrides assort1/assort2 diagonal with matrix diagonal.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Pedigree DataFrame with columns id, sex, mother, father, twin, |
DataFrame
|
generation, household_id, A1, C1, E1, liability1, A2, C2, E2, |
DataFrame
|
liability2. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if any parameter is outside its valid range |
Source code in simace/simulation/simulate.py
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 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 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 1230 1231 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 | |
cli
¶
Command-line interface for running ACE simulations.
Source code in simace/simulation/simulate.py
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 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 | |
mate_correlation¶
simace.simulation.mate_correlation
¶
Theoretical expected mate liability correlation matrix.
Companion to the generative _assortative_pair_partners in
simace.simulation.simulate: given the same assortative-mating parameters
and ACE variance components passed to the simulator, this module returns the
expected R_mf matrix that the simulator should produce. Used by
validation and plotting code to compare observed vs. expected mate
correlations. Change one, change the other.
expected_mate_corr_matrix
¶
expected_mate_corr_matrix(assort1, assort2, rA, rC, A1, C1, A2, C2, assort_matrix=None, rE=0.0, E1=0.0, E2=0.0)
Compute the 2x2 expected mate liability correlation matrix.
Returns E[corr(F_i, M_j)] for i,j in {1,2} given assortative mating parameters and ACE variance components.
With the 4-variate copula algorithm, assort1 and assort2 are target Pearson mate correlations. The cross-mate cross-trait correlation follows from the mechanistic path: c = rho_w * sqrt(|r1r2|) * sign(r1r2), where rho_w is the within-person cross-trait liability correlation.
When assort_matrix is provided, it is returned directly (the user
has specified the full R_mf).
| PARAMETER | DESCRIPTION |
|---|---|
assort1
|
Target mate Pearson correlation for trait 1.
TYPE:
|
assort2
|
Target mate Pearson correlation for trait 2.
TYPE:
|
rA
|
Genetic correlation between traits.
TYPE:
|
rC
|
Shared-environment correlation between traits.
TYPE:
|
A1
|
Additive genetic variance for trait 1.
TYPE:
|
C1
|
Shared-environment variance for trait 1.
TYPE:
|
A2
|
Additive genetic variance for trait 2.
TYPE:
|
C2
|
Shared-environment variance for trait 2.
TYPE:
|
assort_matrix
|
If provided, returned directly as the full R_mf matrix.
TYPE:
|
rE
|
Unique-environment correlation between traits.
TYPE:
|
E1
|
Unique-environment variance for trait 1.
TYPE:
|
E2
|
Unique-environment variance for trait 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
2x2 array of expected mate liability correlations |