Join the newly launched Discord
community for real-time discussions, peer support, and direct interaction with the Meridian team!
meridian.analysis.visualizer.ModelDiagnostics
Stay organized with collections
Save and categorize content based on your preferences.
Generates model diagnostics plots from the Meridian model fitting.
meridian
.
analysis
.
visualizer
.
ModelDiagnostics
(
meridian
:
meridian
.
model
.
model
.
Meridian
)
Methods
plot_prior_and_posterior_distribution
View source
plot_prior_and_posterior_distribution
(
parameter
:
str
=
'roi_m'
,
num_geos
:
int
=
3
,
selected_times
:
(
list
[
str
]
|
None
)
=
None
)
->
(
alt
.
Chart
|
alt
.
FacetChart
)
Plots prior and posterior distributions for a model parameter.
parameter
Model parameter name to plot. By default, the ROI parameter is
shown if a name is not specified.
num_geos
Number of largest geos by population to show in the plots for
the geo-level parameters. By default, only the top three largest geos
are shown.
selected_times
List of specific time periods to plot for time-level
parameters. These times must match the time periods from the data. By
default, the first three time periods are plotted.
An Altair plot showing the parameter distributions.
NotFittedModelError
The model hasn't been fitted.
ValueError
A parameter
is not a Meridian model parameter.
plot_rhat_boxplot
View source
plot_rhat_boxplot
()
->
alt
.
Chart
Plots the R-hat box plot.
Visual summary of the Gelman & Rubin (1992) potential scale reduction for
chain convergence, commonly referred to as R-hat. It is a convergence
diagnostic measure that measures the degree to which variance (of the means)
between chains exceeds what you would expect if the chains were identically
distributed. Values close to 1.0 indicate convergence. R-hat < 1.2 indicates
approximate convergence and is a reasonable threshold for many problems
(Brooks & Gelman, 1998).
There is a single R-hat value for each model parameter. The box plot
summarizes the distribution of R-hat values across indices. For example, the
box corresponding to beta_gm
summarizes the distribution of R-hat values
across both the geo index g
and the channel index m
.
The R-hat is not defined for any parameters that have deterministic priors,
so these parameters are not shown on the boxplot.
Andrew Gelman and Donald B. Rubin. Inference from Iterative Simulation
Using Multiple Sequences. Statistical Science, 7(4):457-472, 1992.
Stephen P. Brooks and Andrew Gelman. General Methods for Monitoring
Convergence of Iterative Simulatio
An Altair plot showing the R-hat boxplot per parameter.
NotFittedModelError
The model hasn't been fitted.
MCMCSamplingError
The MCMC sampling did not converge.
predictive_accuracy_table
View source
predictive_accuracy_table
(
selected_geos
:
(
Sequence
[
str
]
|
None
)
=
None
,
selected_times
:
(
Sequence
[
str
]
|
None
)
=
None
,
column_var
:
(
str
|
None
)
=
None
,
batch_size
:
int
=
c
.
DEFAULT_BATCH_SIZE
)
->
pd
.
DataFrame
Displays the predictive accuracy of the DataFrame.
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.
selected_times
Optional list of a subset of time dimensions to include.
By default, all times are included. Times must match the time dimensions
from meridian.InputData
.
column_var
Optional string that indicates whether to pivot the table by metric
, geo_granularity
or evaluation_set
. By default, column_var=None
indicates that the metric
, geo_granularity
and value
(along with evaluation_set
when holdout_id
isn't None
)
columns are displayed in the returning unpivoted DataFrame.
batch_size
Integer representing the number of maximum draws per chain in
each batch. The calculation is run in batches to avoid memory
exhaustion. If a memory error occurs, try reducing batch_size
. The
calculation will generally be faster with larger batch_size
values.
A DataFrame containing the computed R_Squared
, MAPE
and wMAPE
values. If holdout_id
exists, the data is split into Train
, Test
,
and All Data
subsections, and evaluation_set
is included as a column
in the transformation from Dataset to DataFrame.
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 webpage documents the \u003ccode\u003eModelDiagnostics\u003c/code\u003e class, which is used to generate diagnostic plots from the Meridian model fitting process.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eplot_prior_and_posterior_distribution\u003c/code\u003e method visualizes the prior and posterior distributions for a selected model parameter, allowing customization for specific geos and time periods.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eplot_rhat_boxplot\u003c/code\u003e method provides a visual summary of the R-hat values, which are a convergence diagnostic measure for the model parameters.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003epredictive_accuracy_table\u003c/code\u003e method displays a table which includes metrics such as \u003ccode\u003eR_Squared\u003c/code\u003e, \u003ccode\u003eMAPE\u003c/code\u003e, and \u003ccode\u003ewMAPE\u003c/code\u003e, to evaluate the predictive accuracy of the model, also allowing the selection of specific geos, time periods and pivoting the table by metrics.\u003c/p\u003e\n"]]],["The `ModelDiagnostics` class generates plots and tables for model analysis. Key actions include: plotting prior and posterior distributions of model parameters, allowing selection of specific parameters, geographies, and time periods. It also generates an R-hat box plot to assess chain convergence, indicating values close to 1.0 are desirable. Additionally, it provides a table displaying predictive accuracy using metrics like R-squared, MAPE, and wMAPE, with options to filter by geographies and time periods.\n"],null,["\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L46-L356) |\n\nGenerates model diagnostics plots from the Meridian model fitting. \n\n meridian.analysis.visualizer.ModelDiagnostics(\n meridian: ../../../meridian/model/model/Meridian.md\n )\n\nMethods\n\n`plot_prior_and_posterior_distribution`\n\n[View source](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L151-L270) \n\n plot_prior_and_posterior_distribution(\n parameter: str = 'roi_m',\n num_geos: int = 3,\n selected_times: (list[str] | None) = None\n ) -\u003e (alt.Chart | alt.FacetChart)\n\nPlots prior and posterior distributions for a model parameter.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `parameter` | Model parameter name to plot. By default, the ROI parameter is shown if a name is not specified. |\n| `num_geos` | Number of largest geos by population to show in the plots for the geo-level parameters. By default, only the top three largest geos are shown. |\n| `selected_times` | List of specific time periods to plot for time-level parameters. These times must match the time periods from the data. By default, the first three time periods are plotted. |\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 parameter distributions. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|-----------------------|--------------------------------------------------|\n| `NotFittedModelError` | The model hasn't been fitted. |\n| `ValueError` | A `parameter` is not a Meridian model parameter. |\n\n\u003cbr /\u003e\n\n`plot_rhat_boxplot`\n\n[View source](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L272-L356) \n\n plot_rhat_boxplot() -\u003e alt.Chart\n\nPlots the R-hat box plot.\n\nVisual summary of the Gelman \\& Rubin (1992) potential scale reduction for\nchain convergence, commonly referred to as R-hat. It is a convergence\ndiagnostic measure that measures the degree to which variance (of the means)\nbetween chains exceeds what you would expect if the chains were identically\ndistributed. Values close to 1.0 indicate convergence. R-hat \\\u003c 1.2 indicates\napproximate convergence and is a reasonable threshold for many problems\n(Brooks \\& Gelman, 1998).\n\nThere is a single R-hat value for each model parameter. The box plot\nsummarizes the distribution of R-hat values across indices. For example, the\nbox corresponding to `beta_gm` summarizes the distribution of R-hat values\nacross both the geo index `g` and the channel index `m`.\n\nThe R-hat is not defined for any parameters that have deterministic priors,\nso these parameters are not shown on the boxplot.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| References ||\n|---|---|\n| Andrew Gelman and Donald B. Rubin. Inference from Iterative Simulation Using Multiple Sequences. Statistical Science, 7(4):457-472, 1992. Stephen P. Brooks and Andrew Gelman. General Methods for Monitoring Convergence of Iterative Simulatio ||\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 R-hat boxplot per parameter. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|-----------------------|-------------------------------------|\n| `NotFittedModelError` | The model hasn't been fitted. |\n| `MCMCSamplingError` | The MCMC sampling did not converge. |\n\n\u003cbr /\u003e\n\n`predictive_accuracy_table`\n\n[View source](https://github.com/google/meridian/blob/v1.2.0/meridian/analysis/visualizer.py#L88-L149) \n\n predictive_accuracy_table(\n selected_geos: (Sequence[str] | None) = None,\n selected_times: (Sequence[str] | None) = None,\n column_var: (str | None) = None,\n batch_size: int = c.DEFAULT_BATCH_SIZE\n ) -\u003e pd.DataFrame\n\nDisplays the predictive accuracy of the DataFrame.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\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| `selected_times` | Optional list of a subset of time dimensions to include. By default, all times are included. Times must match the time dimensions from `meridian.InputData`. |\n| `column_var` | Optional string that indicates whether to pivot the table by `metric`, `geo_granularity` or `evaluation_set`. By default, `column_var=None` indicates that the `metric`, `geo_granularity` and `value` (along with `evaluation_set` when `holdout_id` isn't `None`) columns are displayed in the returning unpivoted DataFrame. |\n| `batch_size` | Integer representing the number of maximum draws per chain in each batch. The calculation is run in batches to avoid memory exhaustion. If a memory error occurs, try reducing `batch_size`. The calculation will generally be faster with larger `batch_size` values. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A DataFrame containing the computed `R_Squared`, `MAPE` and `wMAPE` values. If `holdout_id` exists, the data is split into `Train`, `Test`, and `All Data` subsections, and `evaluation_set` is included as a column in the transformation from Dataset to DataFrame. ||\n\n\u003cbr /\u003e"]]