Filter Model Results with selected_geos and selected_times

When analyzing your fitted Meridian model, the Analyzer, Visualizer, and BudgetOptimizer classes allow one to scope the analysis to specific geographical regions or time periods. These arguments allow you to calculate metrics (like ROI or incremental outcome), generate visualizations, and perform budget optimization based on subsets of your input data.

Use selected_geos argument for scoping to geos. Use selected_times argument for scoping to time periods for the Analyzer and Visualizer classes. To define the time window for BudgetOptimizer, use start_date and end_date arguments (typically provided in yyyy-mm-dd string format).

By default, these are set to include all the geographical units and all of the time periods respectively. The boolean options aggregate_times and aggregate_geos available in some methods lets you to either segment results be time and geo respectively, or to aggregate the selected times and geos.

Example code

Several hypothetical scenarios are presented in this section for how to use selected_geos and selected_times.

Scenario 1: Calculate summary metrics across a specific time period

# Scenario 1: Calculate summary metrics for Q4 2024 only, aggregated across geos.

from meridian.analysis import analyzer
from meridian.model import model

mmm = model.Meridian(...)
mmm.sample_posterior(...)
analyzer_instance = analyzer.Analyzer(mmm)

selected_q4_times = [
    '2024-10-01',
    '2024-10-08',
    '2024-10-15',
    '2024-10-22',
    '2024-10-29',
    '2024-11-05',
    '2024-11-12',
    '2024-11-19',
    '2024-11-26',
    '2024-12-03',
    '2024-12-10',
    '2024-12-17',
    '2024-12-24',
    '2024-12-31',
]

q4_national_summary = analyzer_instance.summary_metrics(
    selected_times=selected_q4_times,
)

Scenario 2: Calculate incremental outcome for 2 hypothetical geos

# Scenario 2: Calculate incremental outcome for 2 hypothetical geos, 'Geo_A', 'Geo_B' broken down weekly.

from meridian.analysis import analyzer
from meridian.model import model

mmm = model.Meridian(...)
mmm.sample_posterior(...)
analyzer_instance = analyzer.Analyzer(mmm)

# Note: Incremental Outcome can be aggregated by time or shown weekly.
geo_a_weekly_impact = analyzer_instance.incremental_outcome(
    selected_geos=['Geo_A', 'Geo_B'],
    aggregate_geos=False,  # Keep the geo dimension separate.
    aggregate_times=False,  # Show incremental outcome week-by-week.
)

Scenario 3: Filtering Model Fit Plot

# Scenario 3: Filtering Model Fit Plot
# You can restrict the ModelFit.plot_model_fit() visualization to a specific
# region and ensure results are shown at that detailed geo level.
# This example assumes weekly aggregation.

from meridian.model import model
from meridian.analysis import visualizer

mmm = model.Meridian(...)
mmm.sample_posterior(...)
# Assume 'mmm' is fitted
model_fit = visualizer.ModelFit(mmm)

model_fit_chart = model_fit.plot_model_fit(
    selected_geos=['Geo_A'],
    # Plotting January 2025
    selected_times=[
        '2025-01-01',
        '2025-01-08',
        '2025-01-15',
        '2025-01-22',
        '2025-01-29',
    ],
    # Display Geo_A's plot separately (relevant for multi-geo models)
    show_geo_level=True,
    include_baseline=True,
)