Join the newly launched Discord
community for real-time discussions, peer support, and direct interaction with the Meridian team!
meridian.analysis.visualizer.ModelFit
Stay organized with collections
Save and categorize content based on your preferences.
Generates model fit plots from the Meridian model fitting.
meridian
.
analysis
.
visualizer
.
ModelFit
(
meridian
:
meridian
.
model
.
model
.
Meridian
,
confidence_level
:
float
=
c
.
DEFAULT_CONFIDENCE_LEVEL
)
Calculates the expected versus actual outcome with the confidence level over
time, and plots graphs to compare the values.
Media mix model with the raw data from the model fitting.
Confidence level for expected outcome credible intervals
represented as a value between zero and one. Default is 0.9
.
Dataset holding the expected, actual, and baseline outcome over time.
The dataset contains the following:
- Coordinates:
geo
, time
, metric
( mean
, ci_hi
, ci_lo
)
- Data variables:
expected
, baseline
, actual
(outcome)
Methods
plot_model_fit
View source
plot_model_fit
(
selected_times
:
(
Sequence
[
str
]
|
None
)
=
None
,
selected_geos
:
(
Sequence
[
str
]
|
None
)
=
None
,
n_top_largest_geos
:
(
int
|
None
)
=
None
,
show_geo_level
:
bool
=
False
,
include_baseline
:
bool
=
True
,
include_ci
:
bool
=
True
)
->
alt
.
Chart
Plots the expected versus actual outcome over time.
selected_times
Optional list of a subset of time dimensions to include.
By default, all times are included. Times should match the time
dimensions from meridian.InputData
.
selected_geos
Optional list of a subset of geo dimensions to include. By
default, all geos are included. Geos should match the geo dimension
names from meridian.InputData
. Set either selected_geos
or n_top_largest_geos
, do not set both.
n_top_largest_geos
Optional number of largest geos by population to
include. By default, all geos are included. Set either selected_geos
or n_top_largest_geos
, do not set both.
show_geo_level
If True
, plots at the geo-level instead of one national
level plot. Only available if selected_geos
or n_top_largest_geos
is
provided.
include_baseline
If True
, shows the expected baseline outcome without
any media execution.
include_ci
If True
, shows the credible intervals for the expected
outcome.
An Altair plot showing the model fit.
update_confidence_level
View source
update_confidence_level
(
confidence_level
:
float
)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-09-05 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-05 UTC."],[[["\u003cp\u003eThis module generates plots to visualize the fit of a Meridian model by comparing expected versus actual outcomes over time, with an optional confidence level.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eModelFit\u003c/code\u003e class takes a Meridian model and a confidence level as input, calculating the expected outcome, actual outcome, and baseline outcome across the dataset.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003emodel_fit_data\u003c/code\u003e attribute stores the calculated expected, actual, and baseline outcomes, along with confidence intervals, for each geo, time, and metric.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eplot_model_fit\u003c/code\u003e method allows users to generate plots of the model fit, with options to filter by time and geography, show geo-level data, and include baseline and confidence intervals.\u003c/p\u003e\n"],["\u003cp\u003eThe confidence level used for plotting can be updated by using the \u003ccode\u003eupdate_confidence_level\u003c/code\u003e method, changing the level of the credible intervals.\u003c/p\u003e\n"]]],["The `ModelFit` class generates plots comparing expected versus actual outcomes from a Meridian model. It calculates these values, along with confidence intervals, over time. Key actions include initializing with a Meridian model and a confidence level, then generating the plot using `plot_model_fit`. This method allows selecting specific time periods and geographic regions, displaying the geo level, and optionally including baseline outcomes and confidence intervals. It returns an Altair plot. The confidence level can be updated through `update_confidence_level`. The `model_fit_data` attribute contains the relevant datasets.\n"],null,["\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L359-L628) |\n\nGenerates model fit plots from the Meridian model fitting. \n\n meridian.analysis.visualizer.ModelFit(\n meridian: ../../../meridian/model/model/Meridian.md,\n confidence_level: float = c.DEFAULT_CONFIDENCE_LEVEL\n )\n\nCalculates the expected versus actual outcome with the confidence level over\ntime, and plots graphs to compare the values.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------------------|-------------------------------------------------------------------------------------------------------------------------|\n| `meridian` | Media mix model with the raw data from the model fitting. |\n| `confidence_level` | Confidence level for expected outcome credible intervals represented as a value between zero and one. Default is `0.9`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ||\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `model_fit_data` | Dataset holding the expected, actual, and baseline outcome over time. \u003cbr /\u003e The dataset contains the following: - **Coordinates:** `geo`, `time`, `metric` (`mean`, `ci_hi`, `ci_lo`) - **Data variables:** `expected`, `baseline`, `actual` (outcome) |\n\nMethods\n\n`plot_model_fit`\n\n[View source](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L400-L521) \n\n plot_model_fit(\n selected_times: (Sequence[str] | None) = None,\n selected_geos: (Sequence[str] | None) = None,\n n_top_largest_geos: (int | None) = None,\n show_geo_level: bool = False,\n include_baseline: bool = True,\n include_ci: bool = True\n ) -\u003e alt.Chart\n\nPlots the expected versus actual outcome over time.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `selected_times` | Optional list of a subset of time dimensions to include. By default, all times are included. Times should match the time dimensions from `meridian.InputData`. |\n| `selected_geos` | Optional list of a subset of geo dimensions to include. By default, all geos are included. Geos should match the geo dimension names from `meridian.InputData`. Set either `selected_geos` or `n_top_largest_geos`, do not set both. |\n| `n_top_largest_geos` | Optional number of largest geos by population to include. By default, all geos are included. Set either `selected_geos` or `n_top_largest_geos`, do not set both. |\n| `show_geo_level` | If `True`, plots at the geo-level instead of one national level plot. Only available if `selected_geos` or `n_top_largest_geos` is provided. |\n| `include_baseline` | If `True`, shows the expected baseline outcome without any media execution. |\n| `include_ci` | If `True`, shows the credible intervals for the expected outcome. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| An Altair plot showing the model fit. ||\n\n\u003cbr /\u003e\n\n`update_confidence_level`\n\n[View source](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L395-L398) \n\n update_confidence_level(\n confidence_level: float\n )"]]