Metadata-Version: 2.4
Name: meta-robyn-py
Version: 0.1.0
Summary: Python port of the Meta Robyn Marketing Mix Modeling (MMM) package
Author-email: Robyn Team <robyn@meta.com>
License: 
        MIT License
        
        Copyright (c) Meta Platforms, Inc. and its affiliates.
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/facebookexperimental/Robyn
Project-URL: Bug Tracker, https://github.com/facebookexperimental/Robyn/issues
Keywords: marketing,mmm,econometrics,causal-inference,data-science
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24.0
Requires-Dist: pandas>=2.0.0
Requires-Dist: scipy>=1.10.0
Requires-Dist: scikit-learn>=1.2.0
Requires-Dist: prophet>=1.1.0
Requires-Dist: nevergrad>=1.0.0
Requires-Dist: matplotlib>=3.7.0
Requires-Dist: seaborn>=0.12.0
Requires-Dist: tqdm>=4.65.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: flake8; extra == "dev"
Dynamic: license-file


# Robyn Python

A Python implementation of **Robyn**, an experimental, automated and open-sourced Marketing Mix Modeling (MMM) code by Meta Marketing Science.

This package aims to be a feature-complete port of the original R package, providing the same automated hyperparameter optimization, ridge regression modeling, and budget allocation capabilities.

## Features

*   **Automated Hyperparameter Optimization**: Uses `Nevergrad` to efficiently explore adstock, saturation, and regularization parameters.
*   **Ridge Regression**: Constrained ridge regression to ensure valid coefficient signs.
*   **Feature Engineering**: Integrated Prophet decomposition for trend, seasonality, and holiday effects.
*   **Multi-Objective Optimization**: Optimizes for NRMSE and DECOMP.RSSD (Business Logic) to find Pareto-optimal models.
*   **Budget Allocation**: Non-linear optimization to maximize response given budget constraints.

## Installation

```bash
pip install meta-robyn-py
```
*(Note: If not yet on PyPI, install locally)*
```bash
pip install .
```

## Quick Start

```python
import pandas as pd
from robyn import robyn_inputs, robyn_run, robyn_outputs, robyn_allocator

# 1. Load Data
dt_input = pd.read_csv("your_data.csv")
dt_holidays = pd.read_csv("holidays.csv")

# 2. Configure Inputs
input_collect = robyn_inputs(
    dt_input=dt_input,
    dt_holidays=dt_holidays,
    date_var="DATE",
    dep_var="revenue",
    dep_var_type="revenue",
    prophet_vars=["trend", "season", "holiday"],
    prophet_country="DE",
    context_vars=["competitor_sales", "events"],
    paid_media_spends=["tv_S", "facebook_S"],
    paid_media_vars=["tv_S", "facebook_I"],
    organic_vars=["newsletter"],
    window_start="2016-11-23",
    window_end="2018-08-22",
    adstock="geometric"
)

# 3. Add Hyperparameters
hyperparameters = {
    "tv_S_alphas": [0.5, 3], "tv_S_gammas": [0.3, 1], "tv_S_thetas": [0, 0.3],
    "facebook_I_alphas": [0.5, 3], "facebook_I_gammas": [0.3, 1], "facebook_I_thetas": [0, 0.3],
    "train_size": [0.5, 0.8]
    # ... add for others
}
input_collect = robyn_inputs(InputCollect=input_collect, hyperparameters=hyperparameters)

# 4. Run Model
output_models = robyn_run(
    InputCollect=input_collect,
    iterations=2000,
    trials=5,
    outputs=False
)

# 5. Export Results & Clusters
output_collect = robyn_outputs(
    input_collect=input_collect,
    output_models=output_models,
    pareto_fronts="auto",
    clusters=True,
    export=True
)

# 6. Budget Allocation
best_model = output_collect['clusters']['models'].iloc[0]['solID']
allocator = robyn_allocator(
    input_collect=input_collect,
    output_collect=output_collect,
    select_model=best_model,
    scenario="max_response",
    channel_constr_low=0.7,
    channel_constr_up=1.2
)
```

## Comparisons with R version

This Python port has been benchmarked against the R version using standard simulation data.

| Metric | Python Port | R Reference |
| :--- | :--- | :--- |
| **NRMSE** | **0.0442** | ~0.04 - 0.06 |
| **Speed** | ~96 iter/s | ~50 - 100 iter/s |

See `COMPARISON_REPORT.md` for full details.

## License

MIT
