papilon 0.2.0__tar.gz → 0.3.0__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 (36) hide show
  1. papilon-0.3.0/LICENSE +201 -0
  2. papilon-0.3.0/NOTICE +13 -0
  3. papilon-0.3.0/PKG-INFO +183 -0
  4. papilon-0.3.0/README.md +145 -0
  5. papilon-0.3.0/papilon/__init__.py +27 -0
  6. {papilon-0.2.0 → papilon-0.3.0}/papilon/causal.py +25 -25
  7. papilon-0.3.0/papilon/datasets.py +75 -0
  8. {papilon-0.2.0 → papilon-0.3.0}/papilon/energy.py +5 -5
  9. {papilon-0.2.0 → papilon-0.3.0}/papilon/entropy.py +10 -10
  10. papilon-0.3.0/papilon/mmm/__init__.py +6 -0
  11. papilon-0.3.0/papilon/mmm/model.py +380 -0
  12. papilon-0.3.0/papilon/mmm/transforms.py +45 -0
  13. {papilon-0.2.0 → papilon-0.3.0}/papilon/optimizer.py +17 -17
  14. {papilon-0.2.0 → papilon-0.3.0}/papilon/relationships.py +32 -32
  15. {papilon-0.2.0 → papilon-0.3.0}/papilon/simulation.py +10 -10
  16. {papilon-0.2.0 → papilon-0.3.0}/papilon/states.py +10 -10
  17. papilon-0.3.0/papilon/topology.py +23 -0
  18. papilon-0.3.0/papilon.egg-info/PKG-INFO +183 -0
  19. {papilon-0.2.0 → papilon-0.3.0}/papilon.egg-info/SOURCES.txt +11 -2
  20. papilon-0.3.0/papilon.egg-info/requires.txt +12 -0
  21. papilon-0.3.0/pyproject.toml +55 -0
  22. papilon-0.3.0/setup.cfg +4 -0
  23. papilon-0.3.0/tests/test_datasets.py +26 -0
  24. papilon-0.3.0/tests/test_mmm.py +76 -0
  25. papilon-0.3.0/tests/test_transforms.py +22 -0
  26. papilon-0.3.0/tests/test_v020_tools.py +23 -0
  27. papilon-0.2.0/LICENSE +0 -21
  28. papilon-0.2.0/PKG-INFO +0 -74
  29. papilon-0.2.0/README.md +0 -57
  30. papilon-0.2.0/papilon/__init__.py +0 -7
  31. papilon-0.2.0/papilon.egg-info/PKG-INFO +0 -74
  32. papilon-0.2.0/papilon.egg-info/requires.txt +0 -6
  33. papilon-0.2.0/pyproject.toml +0 -29
  34. papilon-0.2.0/setup.cfg +0 -19
  35. {papilon-0.2.0 → papilon-0.3.0}/papilon.egg-info/dependency_links.txt +0 -0
  36. {papilon-0.2.0 → papilon-0.3.0}/papilon.egg-info/top_level.txt +0 -0
papilon-0.3.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
papilon-0.3.0/NOTICE ADDED
@@ -0,0 +1,13 @@
1
+ Papilon
2
+ Copyright 2024-2026 Brian Curry / Vector1 Research
3
+
4
+ This product includes software developed by Brian Curry / Vector1 Research
5
+ (https://vector1.ai).
6
+
7
+ "Papilon", "Vector1" and "Vector1 Research" are trademarks of Brian Curry.
8
+ The Apache License, Version 2.0 does not grant permission to use these names
9
+ or marks, except as required to describe the origin of the work (see Section 6
10
+ of the License).
11
+
12
+ Papilon depends on, but does not include, NumPy, SciPy, pandas, scikit-learn,
13
+ NetworkX, Matplotlib and seaborn, each distributed under its own license.
papilon-0.3.0/PKG-INFO ADDED
@@ -0,0 +1,183 @@
1
+ Metadata-Version: 2.4
2
+ Name: papilon
3
+ Version: 0.3.0
4
+ Summary: Bayesian marketing mix modeling with gated importance sampling, plus causal data simulation and complex-systems tools
5
+ Author-email: Brian Curry <brian@vector1.ai>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://vector1.ai
8
+ Project-URL: Repository, https://github.com/Bodhi8/papilon
9
+ Project-URL: Issues, https://github.com/Bodhi8/papilon/issues
10
+ Project-URL: Changelog, https://github.com/Bodhi8/papilon/blob/main/CHANGELOG.md
11
+ Keywords: marketing mix modeling,MMM,media mix model,bayesian,adstock,importance sampling,causal inference,budget optimization,statistical mechanics
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Scientific/Engineering
21
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
22
+ Requires-Python: >=3.10
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ License-File: NOTICE
26
+ Requires-Dist: numpy>=1.24
27
+ Requires-Dist: pandas>=2.0
28
+ Requires-Dist: scipy>=1.10
29
+ Requires-Dist: scikit-learn>=1.3
30
+ Requires-Dist: networkx>=3.0
31
+ Requires-Dist: matplotlib>=3.7
32
+ Requires-Dist: seaborn>=0.13
33
+ Provides-Extra: dev
34
+ Requires-Dist: pytest>=7.0; extra == "dev"
35
+ Requires-Dist: build>=1.0; extra == "dev"
36
+ Requires-Dist: twine>=5.0; extra == "dev"
37
+ Dynamic: license-file
38
+
39
+ # Papilon
40
+
41
+ <p align="center">
42
+ <img src="https://img.shields.io/badge/version-0.3.0-blue.svg" alt="Version 0.3.0">
43
+ <img src="https://img.shields.io/badge/python-3.10+-green.svg" alt="Python 3.10+">
44
+ <img src="https://img.shields.io/badge/license-Apache%202.0-lightgrey.svg" alt="Apache 2.0 license">
45
+ </p>
46
+
47
+ <p align="center"><em>"Does the flap of a butterfly's wings in Brazil set off a tornado in Texas?" Edward Lorenz</em></p>
48
+
49
+ Papilon (French for butterfly) is a Python library for marketing mix modeling and for
50
+ studying how small changes move complex systems. Version 0.3.0 adds a Bayesian marketing
51
+ mix model that chooses its own carryover shape by Bayes factor and reports when its
52
+ posterior can't be trusted. It also includes the statistical-mechanics and simulation
53
+ tools first released in 0.2.0.
54
+
55
+ ## Install
56
+
57
+ ```bash
58
+ pip install papilon
59
+ ```
60
+
61
+ ## Quickstart: a marketing mix model with a known truth
62
+
63
+ `make_synthetic_mmm()` creates three years of weekly sales for a brand running four
64
+ digital channels, and returns the parameters that generated it, so you can check every
65
+ estimate against the truth.
66
+
67
+ ```python
68
+ import papilon as pp
69
+
70
+ df, truth = pp.make_synthetic_mmm()
71
+
72
+ model = pp.MMM(
73
+ data=df,
74
+ target="sales",
75
+ media=["paid_search", "paid_social", "online_video", "display"],
76
+ date_col="date",
77
+ controls=["price_index", "promo"],
78
+ yearly_seasonality=2,
79
+ seed=11,
80
+ )
81
+
82
+ # Does online video's effect peak after the week it runs? Let the data decide.
83
+ families = model.compare_carryover({
84
+ "all_geometric": [],
85
+ "video_delayed": ["online_video"],
86
+ })
87
+
88
+ roi = model.get_roi() # ROAS with 90% intervals
89
+ plan = model.optimize_budget(total=df[model.media].sum(axis=1).mean())
90
+ ```
91
+
92
+ Output from `examples/mmm_quickstart.py`, abridged (spend and sales in $ thousands):
93
+
94
+ ```
95
+ log_evidence log_bayes_factor_vs_best probability ess_fraction verdict
96
+ all_geometric 241.223 -18.72 0.0 0.134 ACCEPT
97
+ video_delayed 259.943 0.00 1.0 0.197 ACCEPT
98
+
99
+ spend roas_mean roas_p05 roas_p95 mroas_mean true_roas
100
+ paid_search 4729.95 0.64 0.03 1.65 0.32 1.25
101
+ paid_social 7678.08 1.02 0.47 1.79 0.49 1.50
102
+ online_video 7369.53 1.76 1.50 2.05 0.88 1.57
103
+ display 3497.10 0.71 0.08 1.68 0.35 0.74
104
+ ```
105
+
106
+ The model picks the delayed carryover shape for online video and puts its peak at 2.0
107
+ weeks, the true value. Every true ROAS falls inside its 90% interval. The point
108
+ estimates for paid search and paid social are low: search spend in this dataset follows
109
+ seasonal demand, which makes it hard to separate from seasonality with three years of
110
+ weekly data. That is the job lift tests do, and calibration to experiments is on the
111
+ roadmap.
112
+
113
+ ## How the model is fit
114
+
115
+ `pp.MMM` follows the inference path in
116
+ [RFC 001, amortized structural inference](papilon-amortized-inference-design.md):
117
+
118
+ 1. **Proposal.** A distribution over the media parameters (carryover, saturation,
119
+ effect size). In 0.3.0, a Laplace fit at the posterior mode stands in for the
120
+ trained amortized network the RFC describes.
121
+ 2. **Importance sampling** against the exact posterior. Baseline, trend, seasonality,
122
+ controls and noise are integrated out in closed form, so sampling covers only the
123
+ media parameters.
124
+ 3. **Effective-sample-size gate.** If the proposal matches the posterior well, it is
125
+ accepted. If not, it is refined from the weighted draws and tried again, or the fit
126
+ is escalated. `model.gate_results` records every step.
127
+ 4. **Bayes factors.** The same importance weights estimate each family's marginal
128
+ likelihood, so `compare_carryover()` can choose between geometric and delayed
129
+ adstock per channel.
130
+
131
+ Channel effects are constrained to be positive. The single-channel prototypes behind
132
+ this design, with their results, are in [`prototypes/`](prototypes/).
133
+
134
+ ## Other tools (from 0.2.0)
135
+
136
+ ```python
137
+ import papilon as pp
138
+
139
+ pp.shannon_entropy(values) # information content
140
+ pp.simulate_kde_scenarios(df, columns, n_samples=1000) # scenario simulation
141
+ pp.analyze_relationships(df, target="sales") # correlation and mutual information
142
+ pp.infer_causal_structure(df, threshold=0.01) # pairwise dependency screen
143
+ pp.grid_search_optimize(df, feature_ranges, objective_fn, constraints)
144
+ ```
145
+
146
+ `infer_causal_structure` tests each pair of variables for a significant linear
147
+ relationship. It is a screen for dependencies, not causal discovery: it does not orient
148
+ edges or adjust for confounders.
149
+
150
+ The notebook `Papilon_Marketing_Optimization_Workflow.ipynb` walks through these tools.
151
+
152
+ ## Tests
153
+
154
+ ```bash
155
+ pip install -e ".[dev]"
156
+ pytest -m "not slow" # about 30 seconds
157
+ pytest # includes the full Bayes-factor recovery test, a few minutes
158
+ ```
159
+
160
+ ## Roadmap
161
+
162
+ These are planned and **not in the released package**:
163
+
164
+ - The trained amortized network from RFC 001 in place of the Laplace proposal
165
+ - Calibration to lift tests and geo experiments
166
+ - Geo-level models with partial pooling
167
+ - Weibull adstock and logistic saturation
168
+ - Causal discovery and effect estimation beyond the dependency screen
169
+
170
+ ## License
171
+
172
+ Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
173
+
174
+ ## Citation
175
+
176
+ If you use Papilon in research, please cite:
177
+
178
+ ```
179
+ Curry, B. (2026). Papilon: Bayesian marketing mix modeling with gated importance
180
+ sampling (Version 0.3.0). https://github.com/Bodhi8/papilon
181
+ ```
182
+
183
+ Built by [Brian Curry](https://vector1.ai), Vector1 Research.
@@ -0,0 +1,145 @@
1
+ # Papilon
2
+
3
+ <p align="center">
4
+ <img src="https://img.shields.io/badge/version-0.3.0-blue.svg" alt="Version 0.3.0">
5
+ <img src="https://img.shields.io/badge/python-3.10+-green.svg" alt="Python 3.10+">
6
+ <img src="https://img.shields.io/badge/license-Apache%202.0-lightgrey.svg" alt="Apache 2.0 license">
7
+ </p>
8
+
9
+ <p align="center"><em>"Does the flap of a butterfly's wings in Brazil set off a tornado in Texas?" Edward Lorenz</em></p>
10
+
11
+ Papilon (French for butterfly) is a Python library for marketing mix modeling and for
12
+ studying how small changes move complex systems. Version 0.3.0 adds a Bayesian marketing
13
+ mix model that chooses its own carryover shape by Bayes factor and reports when its
14
+ posterior can't be trusted. It also includes the statistical-mechanics and simulation
15
+ tools first released in 0.2.0.
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ pip install papilon
21
+ ```
22
+
23
+ ## Quickstart: a marketing mix model with a known truth
24
+
25
+ `make_synthetic_mmm()` creates three years of weekly sales for a brand running four
26
+ digital channels, and returns the parameters that generated it, so you can check every
27
+ estimate against the truth.
28
+
29
+ ```python
30
+ import papilon as pp
31
+
32
+ df, truth = pp.make_synthetic_mmm()
33
+
34
+ model = pp.MMM(
35
+ data=df,
36
+ target="sales",
37
+ media=["paid_search", "paid_social", "online_video", "display"],
38
+ date_col="date",
39
+ controls=["price_index", "promo"],
40
+ yearly_seasonality=2,
41
+ seed=11,
42
+ )
43
+
44
+ # Does online video's effect peak after the week it runs? Let the data decide.
45
+ families = model.compare_carryover({
46
+ "all_geometric": [],
47
+ "video_delayed": ["online_video"],
48
+ })
49
+
50
+ roi = model.get_roi() # ROAS with 90% intervals
51
+ plan = model.optimize_budget(total=df[model.media].sum(axis=1).mean())
52
+ ```
53
+
54
+ Output from `examples/mmm_quickstart.py`, abridged (spend and sales in $ thousands):
55
+
56
+ ```
57
+ log_evidence log_bayes_factor_vs_best probability ess_fraction verdict
58
+ all_geometric 241.223 -18.72 0.0 0.134 ACCEPT
59
+ video_delayed 259.943 0.00 1.0 0.197 ACCEPT
60
+
61
+ spend roas_mean roas_p05 roas_p95 mroas_mean true_roas
62
+ paid_search 4729.95 0.64 0.03 1.65 0.32 1.25
63
+ paid_social 7678.08 1.02 0.47 1.79 0.49 1.50
64
+ online_video 7369.53 1.76 1.50 2.05 0.88 1.57
65
+ display 3497.10 0.71 0.08 1.68 0.35 0.74
66
+ ```
67
+
68
+ The model picks the delayed carryover shape for online video and puts its peak at 2.0
69
+ weeks, the true value. Every true ROAS falls inside its 90% interval. The point
70
+ estimates for paid search and paid social are low: search spend in this dataset follows
71
+ seasonal demand, which makes it hard to separate from seasonality with three years of
72
+ weekly data. That is the job lift tests do, and calibration to experiments is on the
73
+ roadmap.
74
+
75
+ ## How the model is fit
76
+
77
+ `pp.MMM` follows the inference path in
78
+ [RFC 001, amortized structural inference](papilon-amortized-inference-design.md):
79
+
80
+ 1. **Proposal.** A distribution over the media parameters (carryover, saturation,
81
+ effect size). In 0.3.0, a Laplace fit at the posterior mode stands in for the
82
+ trained amortized network the RFC describes.
83
+ 2. **Importance sampling** against the exact posterior. Baseline, trend, seasonality,
84
+ controls and noise are integrated out in closed form, so sampling covers only the
85
+ media parameters.
86
+ 3. **Effective-sample-size gate.** If the proposal matches the posterior well, it is
87
+ accepted. If not, it is refined from the weighted draws and tried again, or the fit
88
+ is escalated. `model.gate_results` records every step.
89
+ 4. **Bayes factors.** The same importance weights estimate each family's marginal
90
+ likelihood, so `compare_carryover()` can choose between geometric and delayed
91
+ adstock per channel.
92
+
93
+ Channel effects are constrained to be positive. The single-channel prototypes behind
94
+ this design, with their results, are in [`prototypes/`](prototypes/).
95
+
96
+ ## Other tools (from 0.2.0)
97
+
98
+ ```python
99
+ import papilon as pp
100
+
101
+ pp.shannon_entropy(values) # information content
102
+ pp.simulate_kde_scenarios(df, columns, n_samples=1000) # scenario simulation
103
+ pp.analyze_relationships(df, target="sales") # correlation and mutual information
104
+ pp.infer_causal_structure(df, threshold=0.01) # pairwise dependency screen
105
+ pp.grid_search_optimize(df, feature_ranges, objective_fn, constraints)
106
+ ```
107
+
108
+ `infer_causal_structure` tests each pair of variables for a significant linear
109
+ relationship. It is a screen for dependencies, not causal discovery: it does not orient
110
+ edges or adjust for confounders.
111
+
112
+ The notebook `Papilon_Marketing_Optimization_Workflow.ipynb` walks through these tools.
113
+
114
+ ## Tests
115
+
116
+ ```bash
117
+ pip install -e ".[dev]"
118
+ pytest -m "not slow" # about 30 seconds
119
+ pytest # includes the full Bayes-factor recovery test, a few minutes
120
+ ```
121
+
122
+ ## Roadmap
123
+
124
+ These are planned and **not in the released package**:
125
+
126
+ - The trained amortized network from RFC 001 in place of the Laplace proposal
127
+ - Calibration to lift tests and geo experiments
128
+ - Geo-level models with partial pooling
129
+ - Weibull adstock and logistic saturation
130
+ - Causal discovery and effect estimation beyond the dependency screen
131
+
132
+ ## License
133
+
134
+ Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
135
+
136
+ ## Citation
137
+
138
+ If you use Papilon in research, please cite:
139
+
140
+ ```
141
+ Curry, B. (2026). Papilon: Bayesian marketing mix modeling with gated importance
142
+ sampling (Version 0.3.0). https://github.com/Bodhi8/papilon
143
+ ```
144
+
145
+ Built by [Brian Curry](https://vector1.ai), Vector1 Research.
@@ -0,0 +1,27 @@
1
+ """Papilon: marketing mix modeling, causal data simulation and complex-systems tools."""
2
+
3
+ from .causal import infer_causal_structure
4
+ from .datasets import make_synthetic_mmm
5
+ from .energy import energy_score
6
+ from .entropy import joint_entropy, shannon_entropy
7
+ from .mmm import MMM
8
+ from .optimizer import grid_search_optimize
9
+ from .relationships import analyze_relationships
10
+ from .simulation import simulate_kde_scenarios
11
+ from .states import count_microstates, macrostate_entropy
12
+
13
+ __version__ = "0.3.0"
14
+
15
+ __all__ = [
16
+ "MMM",
17
+ "make_synthetic_mmm",
18
+ "shannon_entropy",
19
+ "joint_entropy",
20
+ "simulate_kde_scenarios",
21
+ "energy_score",
22
+ "count_microstates",
23
+ "macrostate_entropy",
24
+ "analyze_relationships",
25
+ "infer_causal_structure",
26
+ "grid_search_optimize",
27
+ ]
@@ -1,25 +1,25 @@
1
- import pandas as pd
2
- import networkx as nx
3
- from sklearn.feature_selection import f_regression
4
-
5
- def infer_causal_structure(df, threshold=0.01, visualize=True):
6
- numeric_df = df.select_dtypes(include='number').dropna()
7
- features = numeric_df.columns.tolist()
8
- G = nx.DiGraph()
9
-
10
- for target in features:
11
- for predictor in [col for col in features if col != target]:
12
- X = numeric_df[[predictor]]
13
- y = numeric_df[target]
14
- _, pval = f_regression(X, y)
15
- if pval[0] < threshold:
16
- G.add_edge(predictor, target, weight=1 - pval[0])
17
-
18
- if visualize:
19
- import matplotlib.pyplot as plt
20
- pos = nx.spring_layout(G, seed=42)
21
- nx.draw(G, pos, with_labels=True, node_size=1500, font_size=10, edge_color='gray')
22
- plt.title('Inferred Causal Graph')
23
- plt.show()
24
-
25
- return G
1
+ import pandas as pd
2
+ import networkx as nx
3
+ from sklearn.feature_selection import f_regression
4
+
5
+ def infer_causal_structure(df, threshold=0.01, visualize=True):
6
+ numeric_df = df.select_dtypes(include='number').dropna()
7
+ features = numeric_df.columns.tolist()
8
+ G = nx.DiGraph()
9
+
10
+ for target in features:
11
+ for predictor in [col for col in features if col != target]:
12
+ X = numeric_df[[predictor]]
13
+ y = numeric_df[target]
14
+ _, pval = f_regression(X, y)
15
+ if pval[0] < threshold:
16
+ G.add_edge(predictor, target, weight=1 - pval[0])
17
+
18
+ if visualize:
19
+ import matplotlib.pyplot as plt
20
+ pos = nx.spring_layout(G, seed=42)
21
+ nx.draw(G, pos, with_labels=True, node_size=1500, font_size=10, edge_color='gray')
22
+ plt.title('Inferred Causal Graph')
23
+ plt.show()
24
+
25
+ return G