learn
/
mmm
Marketing Mix Modeling Python: Nevergrad Auto-tuning Guide
Learn how to auto-tune Adstock and diminishing returns hyperparameters in Marketing Mix Modeling using Python and Nevergrad. Step-by-step guide with code.


Get a weekly dose of insightful people strategy content
If you have ever built a Marketing Mix Model in Python, you have probably encountered this problem: you want to transform your media variables with AdStock and Diminishing Returns transformations but the hyperparameters need to be inserted manually.
This manual process causes a lot of inaccuracy in most models because humans are biased by their experience and cannot optimize objectively. We analyzed the best libraries available to solve this problem and on Facebook Robyn we found a solution: Nevergrad.
What is Nevergrad?
Nevergrad is a Python library developed by Facebook that uses AI to perform derivative-free and evolutionary optimization. Despite the technical-sounding name, Nevergrad is easy to use — in just a few lines of code you can run your first optimization.
In this guide, we use it to solve one of the most common problems in Marketing Mix Modeling with Python.
Why Use Nevergrad for Marketing Mix Modeling?
In Marketing Mix Modeling there are parameters we need to pass to our model in order for it to work. These variables are called Hyperparameters.
Let's say you want to calculate your media variables' Adstock and Saturation effects. Based on the specific formula you use, you generally have to define 2 to 3 hyperparameters per channel.
Now say you are modeling 5 different media channels, plus 2 offline channels, with a breakdown making them a total of 10 channels.
The math: 10 channels × 2 hyperparameters = 20 hyperparameters to define before starting the modeling process. These are floating point numbers within large boundaries, meaning an enormous search space to test manually.
That is exactly what Nevergrad is made for: finding the best possible combination of hyperparameters to minimize your model error or maximize its accuracy.
How Nevergrad Works
Nevergrad offers a wide range of derivative-free algorithms, including evolution strategies, differential evolution, particle swarm optimization, Cobyla, and Bayesian optimization.
At its core: you pass a function to Nevergrad that returns a specific value (such as MAPE, NRMSE, or any KPI that matters to you) and tell Nevergrad which hyperparameters to optimize. You specify the algorithm and number of trials, then let Nevergrad do the work. The output is a set of optimally tuned hyperparameters.
How Optimization Works (Optional)
This section is for the more curious. Feel free to skip to the coding section.
If you are new to function optimization, consider a function made up of two parameters X and Y that describes the error of your model. Starting from a given position (e.g. X=5, Y=2), the algorithm takes a step in a random direction, observes whether the error increases or decreases, and adjusts accordingly — iterating until it finds a local minimum.
This is an oversimplified explanation. A great starting point on optimization problems is this YouTube Playlist.
Step-by-Step: MMM Auto-tuning with Nevergrad in Python
Let's model an eCommerce company advertising on Facebook (Meta) and Google.
We use Ridge Regression to handle multi-collinearity between channels. First, define custom AdStock and Saturation transformation functions:
The variable x is your channel input (e.g. Google Ads spend). The variables theta and beta are the hyperparameters to optimize per channel.
Next, define one function containing both variable transformations and Ridge Regression. This function must return the value Nevergrad will minimize — in this case, MAPE:
Now set up Nevergrad:
1. Install Nevergrad (first time only):
2. Import:
3. Define the Instrumentation object with the hyperparameters to optimize:
See Nevergrad parametrization docs for all variable types.
4. Define the optimizer. NGOpt is Nevergrad's meta-optimizer and a solid default:
See Nevergrad's algorithm guide for alternatives.
5. Run the optimizer:
The recommendation variable contains your optimal hyperparameters.
Extra Tip
To make Nevergrad outputs easier to use, extend build_model() to return additional values:
Then rebuild the full model using the optimal hyperparameters in one line:
All the code from this example is available in this GitHub Repository.
Start with MMM Today: Try Cassandra
Cassandra is the first self-service software to model your media mix effectiveness on the cloud and receive media plans that maximize your sales. No Python required — try Cassandra for free.
Frequently Asked Questions
What is Nevergrad and why is it useful for MMM?
Nevergrad is a Python library by Facebook that performs derivative-free optimization. In Marketing Mix Modeling, it automates the search for optimal Adstock and Saturation hyperparameters, replacing error-prone manual tuning with an objective optimization process.
How many hyperparameters does a typical MMM require?
It depends on the number of channels and the transformation formula used. A model with 10 channels and 2 hyperparameters per channel requires 20 hyperparameters — too many to tune manually with any reliability.
What algorithm should I use in Nevergrad for MMM?
NGOpt (Nevergrad's meta-optimizer) is the recommended default. It selects the best underlying algorithm based on the problem structure. For advanced use cases, Nevergrad also offers CMA-ES, differential evolution, particle swarm optimization, and Bayesian methods.
Do I need to know Python to do Marketing Mix Modeling?
If you want to use open-source libraries like Robyn, LightweightMMM, or Nevergrad, Python knowledge is required. If you prefer a no-code approach, platforms like Cassandra let you build calibrated MMM models without writing any code.
