pymargins 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. pymargins-0.0.1/.claude/settings.local.json +12 -0
  2. pymargins-0.0.1/.gitignore +33 -0
  3. pymargins-0.0.1/.python-version +1 -0
  4. pymargins-0.0.1/LICENSE +21 -0
  5. pymargins-0.0.1/PKG-INFO +131 -0
  6. pymargins-0.0.1/README.md +67 -0
  7. pymargins-0.0.1/demo/williams_2012_data.csv +5001 -0
  8. pymargins-0.0.1/demo/williams_2012_demo.py +303 -0
  9. pymargins-0.0.1/demo/williams_2012_demo_marginaleffects_r.R +283 -0
  10. pymargins-0.0.1/demo/williams_2012_demo_scales.py +158 -0
  11. pymargins-0.0.1/demo/williams_2012_demo_scales_marginaleffects_r.R +69 -0
  12. pymargins-0.0.1/demo/williams_2012_demo_scales_statsmodels.py +114 -0
  13. pymargins-0.0.1/demo/williams_2012_demo_statsmodels.py +296 -0
  14. pymargins-0.0.1/docs/_static/custom.css +10 -0
  15. pymargins-0.0.1/docs/api.rst +78 -0
  16. pymargins-0.0.1/docs/conf.py +73 -0
  17. pymargins-0.0.1/docs/demos.rst +58 -0
  18. pymargins-0.0.1/docs/explanations/adapter_pattern.md +58 -0
  19. pymargins-0.0.1/docs/explanations/delta_sim_bootstrap.md +48 -0
  20. pymargins-0.0.1/docs/explanations/gradient_backend.md +44 -0
  21. pymargins-0.0.1/docs/explanations/inference_scale.md +57 -0
  22. pymargins-0.0.1/docs/explanations/kappa_diagnostic.md +68 -0
  23. pymargins-0.0.1/docs/explanations/scenarios_model.md +91 -0
  24. pymargins-0.0.1/docs/explanations/session_precommitment.md +61 -0
  25. pymargins-0.0.1/docs/explanations/vs_marginaleffects.md +79 -0
  26. pymargins-0.0.1/docs/howto/ame_age.joblib +0 -0
  27. pymargins-0.0.1/docs/howto/ame_results.csv +2 -0
  28. pymargins-0.0.1/docs/howto/ame_results.parquet +0 -0
  29. pymargins-0.0.1/docs/howto/ame_results.xlsx +0 -0
  30. pymargins-0.0.1/docs/howto/bootstrap.md +72 -0
  31. pymargins-0.0.1/docs/howto/cluster_block_bootstrap.md +63 -0
  32. pymargins-0.0.1/docs/howto/contrasts.md +227 -0
  33. pymargins-0.0.1/docs/howto/contrasts_vs_evaluate.md +139 -0
  34. pymargins-0.0.1/docs/howto/custom_adapter.md +107 -0
  35. pymargins-0.0.1/docs/howto/diff_in_diff.md +75 -0
  36. pymargins-0.0.1/docs/howto/discrete_changes.md +81 -0
  37. pymargins-0.0.1/docs/howto/elasticities.md +120 -0
  38. pymargins-0.0.1/docs/howto/evaluate.md +193 -0
  39. pymargins-0.0.1/docs/howto/exporting_results.md +80 -0
  40. pymargins-0.0.1/docs/howto/formula_interface.md +115 -0
  41. pymargins-0.0.1/docs/howto/grid_predictions.md +87 -0
  42. pymargins-0.0.1/docs/howto/kappa_fallback.md +91 -0
  43. pymargins-0.0.1/docs/howto/matching.md +178 -0
  44. pymargins-0.0.1/docs/howto/plotting.md +135 -0
  45. pymargins-0.0.1/docs/howto/robust_clustered_ses.md +78 -0
  46. pymargins-0.0.1/docs/howto/scenarios_helpers.md +98 -0
  47. pymargins-0.0.1/docs/howto/simultaneous_ci.md +41 -0
  48. pymargins-0.0.1/docs/index.rst +89 -0
  49. pymargins-0.0.1/docs/intro.rst +191 -0
  50. pymargins-0.0.1/docs/math.rst +227 -0
  51. pymargins-0.0.1/docs/requirements.txt +20 -0
  52. pymargins-0.0.1/docs/tutorials/aft_survival.md +100 -0
  53. pymargins-0.0.1/docs/tutorials/contrasts_and_did.md +102 -0
  54. pymargins-0.0.1/docs/tutorials/cox_survival.md +99 -0
  55. pymargins-0.0.1/docs/tutorials/gee.md +146 -0
  56. pymargins-0.0.1/docs/tutorials/getting_started.md +160 -0
  57. pymargins-0.0.1/docs/tutorials/glm_logit.md +129 -0
  58. pymargins-0.0.1/docs/tutorials/glm_poisson.md +88 -0
  59. pymargins-0.0.1/docs/tutorials/inference_methods.md +105 -0
  60. pymargins-0.0.1/docs/tutorials/iv_2sls.md +63 -0
  61. pymargins-0.0.1/docs/tutorials/mixed_effects.md +135 -0
  62. pymargins-0.0.1/docs/tutorials/mnlogit.md +103 -0
  63. pymargins-0.0.1/docs/tutorials/ols_linear.md +84 -0
  64. pymargins-0.0.1/docs/tutorials/panel_fe.md +83 -0
  65. pymargins-0.0.1/docs/tutorials/scales_and_kappa.md +62 -0
  66. pymargins-0.0.1/docs/tutorials/sklearn_models.md +137 -0
  67. pymargins-0.0.1/pymargins/__init__.py +103 -0
  68. pymargins-0.0.1/pymargins/_adapter.py +748 -0
  69. pymargins-0.0.1/pymargins/_adapters/__init__.py +648 -0
  70. pymargins-0.0.1/pymargins/_adapters/_common.py +217 -0
  71. pymargins-0.0.1/pymargins/_adapters/lifelines_aalen_additive.py +192 -0
  72. pymargins-0.0.1/pymargins/_adapters/lifelines_cox_timevarying.py +216 -0
  73. pymargins-0.0.1/pymargins/_adapters/lifelines_coxph.py +163 -0
  74. pymargins-0.0.1/pymargins/_adapters/lifelines_coxph_survival.py +209 -0
  75. pymargins-0.0.1/pymargins/_adapters/lifelines_coxtimevarying.py +168 -0
  76. pymargins-0.0.1/pymargins/_adapters/lifelines_crc_spline.py +195 -0
  77. pymargins-0.0.1/pymargins/_adapters/lifelines_crc_spline_hr.py +179 -0
  78. pymargins-0.0.1/pymargins/_adapters/lifelines_generalized_gamma.py +196 -0
  79. pymargins-0.0.1/pymargins/_adapters/lifelines_loglogistic_aft.py +208 -0
  80. pymargins-0.0.1/pymargins/_adapters/lifelines_lognormal_aft.py +211 -0
  81. pymargins-0.0.1/pymargins/_adapters/lifelines_piecewise_exponential.py +185 -0
  82. pymargins-0.0.1/pymargins/_adapters/lifelines_weibull_aft.py +209 -0
  83. pymargins-0.0.1/pymargins/_adapters/linearmodels_absorbing.py +194 -0
  84. pymargins-0.0.1/pymargins/_adapters/linearmodels_iv.py +221 -0
  85. pymargins-0.0.1/pymargins/_adapters/linearmodels_panel.py +197 -0
  86. pymargins-0.0.1/pymargins/_adapters/sklearn_bootstrap.py +341 -0
  87. pymargins-0.0.1/pymargins/_adapters/statsmodels_discrete_binary.py +261 -0
  88. pymargins-0.0.1/pymargins/_adapters/statsmodels_discrete_count.py +287 -0
  89. pymargins-0.0.1/pymargins/_adapters/statsmodels_gee.py +286 -0
  90. pymargins-0.0.1/pymargins/_adapters/statsmodels_glm.py +293 -0
  91. pymargins-0.0.1/pymargins/_adapters/statsmodels_mixedlm.py +206 -0
  92. pymargins-0.0.1/pymargins/_adapters/statsmodels_mnlogit.py +232 -0
  93. pymargins-0.0.1/pymargins/_adapters/statsmodels_nominal_gee.py +306 -0
  94. pymargins-0.0.1/pymargins/_adapters/statsmodels_ols.py +239 -0
  95. pymargins-0.0.1/pymargins/_adapters/statsmodels_ordered.py +188 -0
  96. pymargins-0.0.1/pymargins/_adapters/statsmodels_ordinal_gee.py +280 -0
  97. pymargins-0.0.1/pymargins/_adapters/statsmodels_phreg.py +196 -0
  98. pymargins-0.0.1/pymargins/_adapters/statsmodels_phreg_survival.py +216 -0
  99. pymargins-0.0.1/pymargins/_adapters/statsmodels_quantreg.py +133 -0
  100. pymargins-0.0.1/pymargins/_adapters/statsmodels_rlm.py +131 -0
  101. pymargins-0.0.1/pymargins/_adapters/statsmodels_zi.py +394 -0
  102. pymargins-0.0.1/pymargins/_delta.py +562 -0
  103. pymargins-0.0.1/pymargins/_estimands.py +684 -0
  104. pymargins-0.0.1/pymargins/_formula.py +195 -0
  105. pymargins-0.0.1/pymargins/_gradients.py +729 -0
  106. pymargins-0.0.1/pymargins/_inference/__init__.py +20 -0
  107. pymargins-0.0.1/pymargins/_inference/_bootstrap.py +1017 -0
  108. pymargins-0.0.1/pymargins/_inference/_config.py +101 -0
  109. pymargins-0.0.1/pymargins/_inference/_delta.py +92 -0
  110. pymargins-0.0.1/pymargins/_inference/_dispatch.py +178 -0
  111. pymargins-0.0.1/pymargins/_inference/_simulation.py +106 -0
  112. pymargins-0.0.1/pymargins/_kappa.py +558 -0
  113. pymargins-0.0.1/pymargins/_result/__init__.py +17 -0
  114. pymargins-0.0.1/pymargins/_result/_diagnostic.py +65 -0
  115. pymargins-0.0.1/pymargins/_result/_export.py +459 -0
  116. pymargins-0.0.1/pymargins/_result/_margins.py +1982 -0
  117. pymargins-0.0.1/pymargins/_result/_test.py +82 -0
  118. pymargins-0.0.1/pymargins/_result/_text.py +15 -0
  119. pymargins-0.0.1/pymargins/_scenarios.py +388 -0
  120. pymargins-0.0.1/pymargins/_tabular.py +327 -0
  121. pymargins-0.0.1/pymargins/margins/__init__.py +11 -0
  122. pymargins-0.0.1/pymargins/margins/_atoms.py +172 -0
  123. pymargins-0.0.1/pymargins/margins/_estimands.py +277 -0
  124. pymargins-0.0.1/pymargins/margins/_inference_glue.py +216 -0
  125. pymargins-0.0.1/pymargins/margins/_session.py +1154 -0
  126. pymargins-0.0.1/pymargins/matching.py +179 -0
  127. pymargins-0.0.1/pymargins/scenarios.py +460 -0
  128. pymargins-0.0.1/pyproject.toml +96 -0
  129. pymargins-0.0.1/requirements.txt +22 -0
  130. pymargins-0.0.1/tests/_generate_survival_reference.R +14 -0
  131. pymargins-0.0.1/tests/conftest.py +27 -0
  132. pymargins-0.0.1/tests/test_adapter_common.py +370 -0
  133. pymargins-0.0.1/tests/test_adapter_lifelines_aalen_additive.py +101 -0
  134. pymargins-0.0.1/tests/test_adapter_lifelines_cox_timevarying.py +110 -0
  135. pymargins-0.0.1/tests/test_adapter_lifelines_coxph.py +280 -0
  136. pymargins-0.0.1/tests/test_adapter_lifelines_coxph_survival.py +186 -0
  137. pymargins-0.0.1/tests/test_adapter_lifelines_coxtimevarying.py +93 -0
  138. pymargins-0.0.1/tests/test_adapter_lifelines_crc_spline.py +101 -0
  139. pymargins-0.0.1/tests/test_adapter_lifelines_crc_spline_hr.py +97 -0
  140. pymargins-0.0.1/tests/test_adapter_lifelines_generalized_gamma.py +97 -0
  141. pymargins-0.0.1/tests/test_adapter_lifelines_loglogistic_aft.py +250 -0
  142. pymargins-0.0.1/tests/test_adapter_lifelines_lognormal_aft.py +250 -0
  143. pymargins-0.0.1/tests/test_adapter_lifelines_piecewise_exponential.py +98 -0
  144. pymargins-0.0.1/tests/test_adapter_lifelines_weibull_aft.py +275 -0
  145. pymargins-0.0.1/tests/test_adapter_linearmodels_absorbing.py +174 -0
  146. pymargins-0.0.1/tests/test_adapter_linearmodels_famamacbeth.py +82 -0
  147. pymargins-0.0.1/tests/test_adapter_linearmodels_iv.py +224 -0
  148. pymargins-0.0.1/tests/test_adapter_linearmodels_ols.py +75 -0
  149. pymargins-0.0.1/tests/test_adapter_linearmodels_panel.py +285 -0
  150. pymargins-0.0.1/tests/test_adapter_registry.py +183 -0
  151. pymargins-0.0.1/tests/test_adapter_sklearn.py +154 -0
  152. pymargins-0.0.1/tests/test_adapter_statsmodels_discrete_count.py +233 -0
  153. pymargins-0.0.1/tests/test_adapter_statsmodels_gee.py +351 -0
  154. pymargins-0.0.1/tests/test_adapter_statsmodels_glm.py +392 -0
  155. pymargins-0.0.1/tests/test_adapter_statsmodels_mixedlm.py +265 -0
  156. pymargins-0.0.1/tests/test_adapter_statsmodels_mnlogit.py +271 -0
  157. pymargins-0.0.1/tests/test_adapter_statsmodels_nominal_gee.py +242 -0
  158. pymargins-0.0.1/tests/test_adapter_statsmodels_ols.py +368 -0
  159. pymargins-0.0.1/tests/test_adapter_statsmodels_ordered.py +223 -0
  160. pymargins-0.0.1/tests/test_adapter_statsmodels_ordinal_gee.py +310 -0
  161. pymargins-0.0.1/tests/test_adapter_statsmodels_phreg.py +261 -0
  162. pymargins-0.0.1/tests/test_adapter_statsmodels_phreg_survival.py +188 -0
  163. pymargins-0.0.1/tests/test_adapter_statsmodels_quantreg.py +180 -0
  164. pymargins-0.0.1/tests/test_adapter_statsmodels_rlm.py +180 -0
  165. pymargins-0.0.1/tests/test_adapter_statsmodels_zi.py +278 -0
  166. pymargins-0.0.1/tests/test_audit_coverage_gaps.py +418 -0
  167. pymargins-0.0.1/tests/test_bootstrap_block.py +202 -0
  168. pymargins-0.0.1/tests/test_bootstrap_ci_methods.py +223 -0
  169. pymargins-0.0.1/tests/test_bootstrap_cluster.py +191 -0
  170. pymargins-0.0.1/tests/test_bootstrap_jit_cache.py +710 -0
  171. pymargins-0.0.1/tests/test_bootstrap_parallel.py +212 -0
  172. pymargins-0.0.1/tests/test_bug_fixes.py +716 -0
  173. pymargins-0.0.1/tests/test_correctness_linearmodels.py +305 -0
  174. pymargins-0.0.1/tests/test_correctness_mixed.py +297 -0
  175. pymargins-0.0.1/tests/test_correctness_survival.py +703 -0
  176. pymargins-0.0.1/tests/test_delta.py +710 -0
  177. pymargins-0.0.1/tests/test_end_to_end.py +382 -0
  178. pymargins-0.0.1/tests/test_end_to_end_ols.py +228 -0
  179. pymargins-0.0.1/tests/test_estimands.py +198 -0
  180. pymargins-0.0.1/tests/test_formula_interface.py +249 -0
  181. pymargins-0.0.1/tests/test_gradients.py +511 -0
  182. pymargins-0.0.1/tests/test_inference.py +190 -0
  183. pymargins-0.0.1/tests/test_kappa.py +445 -0
  184. pymargins-0.0.1/tests/test_matching.py +294 -0
  185. pymargins-0.0.1/tests/test_methodological_completeness.py +1242 -0
  186. pymargins-0.0.1/tests/test_result_formatting.py +251 -0
  187. pymargins-0.0.1/tests/test_result_to_frame.py +183 -0
  188. pymargins-0.0.1/tests/test_scenarios.py +240 -0
  189. pymargins-0.0.1/tests/test_strict_mode.py +139 -0
  190. pymargins-0.0.1/tests/test_tabular_polars.py +284 -0
  191. pymargins-0.0.1/tests/test_williams_2012_correctness.py +462 -0
@@ -0,0 +1,12 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(python -c \"import ast; [ast.parse\\(open\\(f\\).read\\(\\)\\) for f in ['pymargins/_adapter.py','pymargins/margins.py','pymargins/_result.py']]; print\\('OK'\\)\")",
5
+ "Bash(python *)",
6
+ "Bash(grep *)",
7
+ "Bash(git clone *)",
8
+ "Bash(sed -n '295,320p' tests/test_end_to_end.py)",
9
+ "Bash(sed -n '78,95p' tests/test_audit_coverage_gaps.py)"
10
+ ]
11
+ }
12
+ }
@@ -0,0 +1,33 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # pyenv
7
+ # .python-version is intentionally tracked for reproducibility
8
+
9
+ # Environments
10
+ .env
11
+ .venv
12
+ env/
13
+ venv/
14
+
15
+ # IDEs
16
+ .vscode/
17
+ .idea/
18
+
19
+ # Testing
20
+ .pytest_cache/
21
+ .coverage
22
+
23
+ # Build artifacts
24
+ *.egg-info/
25
+ dist/
26
+ build/
27
+
28
+ notes/
29
+
30
+ # Sphinx docs
31
+ docs/_build/
32
+ docs/_autosummary/
33
+ docs/jupyter_execute/
@@ -0,0 +1 @@
1
+ 3.12.13
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hunter Mills
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,131 @@
1
+ Metadata-Version: 2.4
2
+ Name: pymargins
3
+ Version: 0.0.1
4
+ Summary: Expert-mode marginal effects for Python with session-level analytical pre-commitment.
5
+ Project-URL: Homepage, https://github.com/huntermills707/pymargins
6
+ Project-URL: Documentation, https://pymargins.readthedocs.io
7
+ Project-URL: Repository, https://github.com/huntermills707/pymargins
8
+ Author: Hunter Mills
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Classifier: Development Status :: 2 - Pre-Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: jax>=0.4.20
22
+ Requires-Dist: jaxlib>=0.4.20
23
+ Requires-Dist: numpy>=1.24
24
+ Requires-Dist: pandas>=2.0
25
+ Requires-Dist: scipy>=1.10
26
+ Requires-Dist: threadpoolctl>=3.0
27
+ Provides-Extra: docs
28
+ Requires-Dist: joblib>=1.0; extra == 'docs'
29
+ Requires-Dist: lifelines>=0.30; extra == 'docs'
30
+ Requires-Dist: linearmodels>=5.0; extra == 'docs'
31
+ Requires-Dist: matplotlib>=3.5; extra == 'docs'
32
+ Requires-Dist: myst-nb>=1.0; extra == 'docs'
33
+ Requires-Dist: openpyxl>=3.0; extra == 'docs'
34
+ Requires-Dist: patsy>=0.5; extra == 'docs'
35
+ Requires-Dist: polars>=1.0; extra == 'docs'
36
+ Requires-Dist: pyarrow>=10.0; extra == 'docs'
37
+ Requires-Dist: pysmatch>=2.1; extra == 'docs'
38
+ Requires-Dist: scikit-learn>=1.3; extra == 'docs'
39
+ Requires-Dist: sphinx-design>=0.5; extra == 'docs'
40
+ Requires-Dist: sphinx>=7.0; extra == 'docs'
41
+ Requires-Dist: statsmodels>=0.14; extra == 'docs'
42
+ Provides-Extra: lifelines
43
+ Requires-Dist: lifelines>=0.30; extra == 'lifelines'
44
+ Provides-Extra: linearmodels
45
+ Requires-Dist: linearmodels>=5.0; extra == 'linearmodels'
46
+ Provides-Extra: matching
47
+ Requires-Dist: pysmatch>=2.1; extra == 'matching'
48
+ Provides-Extra: polars
49
+ Requires-Dist: polars>=1.0; extra == 'polars'
50
+ Requires-Dist: pyarrow>=10.0; extra == 'polars'
51
+ Provides-Extra: sklearn
52
+ Requires-Dist: scikit-learn>=1.3; extra == 'sklearn'
53
+ Provides-Extra: statsmodels
54
+ Requires-Dist: patsy>=0.5; extra == 'statsmodels'
55
+ Requires-Dist: statsmodels>=0.14; extra == 'statsmodels'
56
+ Provides-Extra: test
57
+ Requires-Dist: hypothesis; extra == 'test'
58
+ Requires-Dist: polars>=1.0; extra == 'test'
59
+ Requires-Dist: pyarrow>=10.0; extra == 'test'
60
+ Requires-Dist: pysmatch>=2.1; extra == 'test'
61
+ Requires-Dist: pytest-cov; extra == 'test'
62
+ Requires-Dist: pytest>=7.0; extra == 'test'
63
+ Description-Content-Type: text/markdown
64
+
65
+ # pymargins
66
+
67
+ Expert-mode marginal effects for Python. Session-level analytical
68
+ pre-commitment, JAX-native autodiff, κ-driven simulation fallback.
69
+
70
+ ## Status
71
+
72
+ Alpha. End-to-end usable with statsmodels GLM and OLS/WLS/GLS through
73
+ auto-detection. Additional adapters (sklearn, linearmodels, mixed
74
+ models), cluster/block bootstrap, and reporting polish remain on the
75
+ roadmap.
76
+
77
+ ## Quick example
78
+
79
+ ```python
80
+ import statsmodels.formula.api as smf
81
+ import statsmodels.api as sm
82
+ from pymargins import Margins
83
+
84
+ # Fit a model
85
+ fit = smf.glm(
86
+ "outcome ~ treatment + age + sex",
87
+ data=df,
88
+ family=sm.families.Binomial(),
89
+ ).fit()
90
+
91
+ # Wrap in a session, committing to log-scale analysis
92
+ m = Margins.log_scale(fit, vcov="HC3", level=0.95)
93
+ print(m.summary()) # methods-section paragraph
94
+
95
+ # Pre-flight diagnostic: is delta reliable here?
96
+ print(m.diagnose().summary())
97
+
98
+ # Compute a relative risk contrast
99
+ rr = m.contrasts(
100
+ scenarios=[
101
+ {"atexog": {"treatment": 1}, "label": "treated"},
102
+ {"atexog": {"treatment": 0}, "label": "control"},
103
+ ],
104
+ contrasts=[+1, -1],
105
+ )
106
+ print(rr.summary()) # estimate, asymmetric CI, κ, etc.
107
+ ```
108
+
109
+ ## Performance notes
110
+
111
+ - **Bootstrap with `n_jobs > 1`**: Parallel bootstrap uses a
112
+ `ThreadPoolExecutor` for model refitting, but JAX evaluation is
113
+ always serial in the main thread. This avoids XLA compilation race
114
+ conditions. BLAS threads are limited to 1 per worker to prevent
115
+ oversubscription.
116
+
117
+ - **Large scenario grids**: `expand_scenario` creates one block of rows
118
+ per grid point. For a 10-point grid over a 1M-row dataset this
119
+ materialises 10M rows. Use smaller representative samples (e.g.
120
+ `at="typical"`) or pass explicit `data=` overrides when exploring
121
+ high-dimensional counterfactuals.
122
+
123
+ - **Memory retention**: `MarginsResult` objects hold references to the
124
+ parent session, design matrices, and gradients. Call
125
+ `result.materialize()` promptly on results you intend to store
126
+ long-term; this drops the heavy machinery while preserving estimates,
127
+ standard errors, and confidence intervals.
128
+
129
+ ## License
130
+
131
+ MIT
@@ -0,0 +1,67 @@
1
+ # pymargins
2
+
3
+ Expert-mode marginal effects for Python. Session-level analytical
4
+ pre-commitment, JAX-native autodiff, κ-driven simulation fallback.
5
+
6
+ ## Status
7
+
8
+ Alpha. End-to-end usable with statsmodels GLM and OLS/WLS/GLS through
9
+ auto-detection. Additional adapters (sklearn, linearmodels, mixed
10
+ models), cluster/block bootstrap, and reporting polish remain on the
11
+ roadmap.
12
+
13
+ ## Quick example
14
+
15
+ ```python
16
+ import statsmodels.formula.api as smf
17
+ import statsmodels.api as sm
18
+ from pymargins import Margins
19
+
20
+ # Fit a model
21
+ fit = smf.glm(
22
+ "outcome ~ treatment + age + sex",
23
+ data=df,
24
+ family=sm.families.Binomial(),
25
+ ).fit()
26
+
27
+ # Wrap in a session, committing to log-scale analysis
28
+ m = Margins.log_scale(fit, vcov="HC3", level=0.95)
29
+ print(m.summary()) # methods-section paragraph
30
+
31
+ # Pre-flight diagnostic: is delta reliable here?
32
+ print(m.diagnose().summary())
33
+
34
+ # Compute a relative risk contrast
35
+ rr = m.contrasts(
36
+ scenarios=[
37
+ {"atexog": {"treatment": 1}, "label": "treated"},
38
+ {"atexog": {"treatment": 0}, "label": "control"},
39
+ ],
40
+ contrasts=[+1, -1],
41
+ )
42
+ print(rr.summary()) # estimate, asymmetric CI, κ, etc.
43
+ ```
44
+
45
+ ## Performance notes
46
+
47
+ - **Bootstrap with `n_jobs > 1`**: Parallel bootstrap uses a
48
+ `ThreadPoolExecutor` for model refitting, but JAX evaluation is
49
+ always serial in the main thread. This avoids XLA compilation race
50
+ conditions. BLAS threads are limited to 1 per worker to prevent
51
+ oversubscription.
52
+
53
+ - **Large scenario grids**: `expand_scenario` creates one block of rows
54
+ per grid point. For a 10-point grid over a 1M-row dataset this
55
+ materialises 10M rows. Use smaller representative samples (e.g.
56
+ `at="typical"`) or pass explicit `data=` overrides when exploring
57
+ high-dimensional counterfactuals.
58
+
59
+ - **Memory retention**: `MarginsResult` objects hold references to the
60
+ parent session, design matrices, and gradients. Call
61
+ `result.materialize()` promptly on results you intend to store
62
+ long-term; this drops the heavy machinery while preserving estimates,
63
+ standard errors, and confidence intervals.
64
+
65
+ ## License
66
+
67
+ MIT