### Install MMM Proto Schema from Source Source: https://github.com/google/meridian/blob/main/proto/README.md Clone the repository and install the package locally with the schema dependencies. ```sh git clone https://github.com/google/meridian.git cd meridian pip install .[schema] ``` -------------------------------- ### Install Meridian and Verify Environment Source: https://github.com/google/meridian/blob/main/demo/ROI_mROI_Response_Curves.ipynb Installs the Meridian library and checks for GPU availability. A GPU runtime is required for model training. ```python # Install meridian: from PyPI @ latest release !pip install --upgrade google-meridian[colab,and-cuda] from meridian.analysis import analyzer from meridian.analysis import visualizer from meridian.data import data_frame_input_data_builder from meridian.model import model import numpy as np import pandas as pd # check if GPU is available from psutil import virtual_memory import tensorflow as tf ram_gb = virtual_memory().total / 1e9 print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb)) print( 'Num GPUs Available: ', len(tf.config.experimental.list_physical_devices('GPU')), ) print( 'Num CPUs Available: ', len(tf.config.experimental.list_physical_devices('CPU')), ) ``` -------------------------------- ### Install Meridian with Scenario Planner and CUDA Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Installs the google-meridian library with scenario planner and CUDA features. This step can take 3-7 minutes to complete. ```python # @markdown Then we can install meridian library with scenario planner feature, this step can take 3-7 mins !pip install google-meridian[scenarioplanner,and-cuda] ``` -------------------------------- ### Install Meridian from GitHub Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Installs the Meridian library from the GitHub repository at the HEAD commit. This is useful for testing the latest development version. ```python # Install meridian: from GitHub @HEAD ``` -------------------------------- ### Install Meridian from PyPI Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Install the latest version of Meridian from PyPI, including Colab, CUDA, and schema support. ```python # Install meridian: from PyPI @ latest release !pip install --upgrade google-meridian[colab,and-cuda,schema] ``` -------------------------------- ### Install Meridian from PyPI Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Installs the Meridian package from PyPI. Use the `[colab,and-cuda]` extra to include dependencies for Colab and CUDA support. A specific version can be installed by appending `==`. For the latest development version, install from GitHub. ```python # Install meridian: from PyPI @ latest release !pip install --upgrade google-meridian[colab,and-cuda] ``` ```python # Install meridian: from PyPI @ specific version # !pip install google-meridian[colab,and-cuda]==1.0.3 ``` ```python # Install meridian: from GitHub @HEAD # !pip install --upgrade "google-meridian[colab,and-cuda] @ git+https://github.com/google/meridian.git" ``` -------------------------------- ### Install MMM Proto Schema via PIP Source: https://github.com/google/meridian/blob/main/proto/README.md Use this command to install or upgrade the package using the Python package manager. ```sh pip install --upgrade mmm-proto-schema ``` -------------------------------- ### Install Google Meridian Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Installs the Google Meridian library with specific dependencies for Colab, CUDA, and schema support from a Git repository. ```bash # !pip install --upgrade "google-meridian[colab,and-cuda,schema] @ git+https://github.com/google/meridian.git@main" ``` -------------------------------- ### Install Meridian library Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Install the Meridian package with required dependencies for Colab and CUDA support. ```python # Install meridian: from PyPI @ latest release !pip install --upgrade google-meridian[colab,and-cuda] # Install meridian: from PyPI @ specific version # !pip install google-meridian[colab,and-cuda]==1.0.3 # Install meridian: from GitHub @HEAD # !pip install --upgrade "google-meridian[colab,and-cuda] @ git+https://github.com/google/meridian.git" ``` -------------------------------- ### Install Google Meridian Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Install the Google Meridian library with optional dependencies for Colab, CUDA, and schema support. This command installs the latest version from the main branch of the GitHub repository. ```bash #!pip install --upgrade "google-meridian[colab,and-cuda,schema] @ git+https://github.com/google/meridian.git@main" ``` -------------------------------- ### Add Media Data to Builder Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Incorporates media-related data, including impressions and spend, for specified channels. This example defines channels and their corresponding media columns. ```python channels = ["Channel0", "Channel1", "Channel2", "Channel3", "Channel4"] builder = builder.with_media( df, media_cols=[f"{channel}_impression" for channel in channels], media_spend_cols=[f"{channel}_spend" for channel in channels], media_channels=channels, ) ``` -------------------------------- ### Install Google Meridian with MLflow and CUDA Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Use this command to install the Google Meridian library with support for MLflow and CUDA. Ensure you have the necessary build tools and environment set up. ```python # !pip install --upgrade "google-meridian[colab,and-cuda,mlflow] @ git+https://github.com/google/meridian.git" ``` -------------------------------- ### Install Meridian with MLflow from PyPI Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Installs the latest version of the Meridian library with MLflow and Colab dependencies. Ensure you are using a GPU runtime. ```python # Install meridian with mlflow: from PyPI @ latest release !pip install --upgrade google-meridian[colab,and-cuda,mlflow] ``` -------------------------------- ### Install Meridian with MLflow from PyPI (Specific Version) Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Installs a specific version (1.1.3 or later) of the Meridian library with MLflow and Colab dependencies. Ensure you are using a GPU runtime. ```python # Install meridian with mlflow: from PyPI @ specific version # !pip install google-meridian[colab,and-cuda,mlflow]==1.1.3 ``` -------------------------------- ### Install Unreleased Meridian for CPU from GitHub Source: https://github.com/google/meridian/blob/main/README.md Install the most recent, unreleased version of Meridian from GitHub for CPU users. This version is suitable for macOS and general CPU environments. ```sh pip install --upgrade git+https://github.com/google/meridian.git ``` -------------------------------- ### Install Unreleased Meridian with GPU Support from GitHub Source: https://github.com/google/meridian/blob/main/README.md Install the most recent, unreleased version of Meridian with CUDA support from GitHub for GPU users. A CUDA toolchain and compatible GPU device are necessary. ```sh pip install --upgrade "google-meridian[and-cuda] @ git+https://github.com/google/meridian.git" ``` -------------------------------- ### Output Model Results Summary Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Generate and save a two-page HTML summary of model results for a specified time duration. Requires the Summarizer object, filename, filepath, start date, and end date. ```python filepath = meridian_root start_date = '2021-01-25' end_date = '2024-01-15' mmm_summarizer.output_model_results_summary( 'summary_output.html', filepath, start_date, end_date ) ``` -------------------------------- ### Enable MLflow Autologging Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Initializes MLflow autologging for the Meridian library. Call this once before starting any model runs to automatically log parameters, metrics, and models. ```python from meridian.mlflow import autolog autolog.autolog(log_metrics=True) # Metric logging is not enabled by default. ``` -------------------------------- ### Install Meridian with GPU Support Source: https://github.com/google/meridian/blob/main/README.md Install the latest release of Meridian with CUDA support for Linux-GPU users. A CUDA toolchain and compatible GPU device are necessary. ```sh pip install --upgrade google-meridian[and-cuda] ``` -------------------------------- ### Simulate Time-Varying Intercepts (mu_t) Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Simulates time-varying intercepts (mu_t) using Meridian's knot system. This example uses full knots, equivalent to simulating directly from a Normal distribution. ```python # Initialize Meridian's knots object (we need the weights) knots_object = knots.get_knot_info(n_times, n_knots_simul, False) # From simulated knots, get mu_t mu_t = tfp.distributions.Deterministic( tf.einsum( '...k,kt->...t', knots_k, tf.convert_to_tensor(knots_object.weights), ) ).sample() ``` ```python mu_t = tfp.distributions.Normal(0, 2.0).sample(n_times). ``` -------------------------------- ### Run Budget Optimization Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Instantiate the BudgetOptimizer and execute the default ROI maximization scenario. ```python %%time budget_optimizer = optimizer.BudgetOptimizer(mmm) optimization_results = budget_optimizer.optimize() ``` -------------------------------- ### Initialize Budget Optimizer Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Instantiate the BudgetOptimizer class with the model object to prepare for budget optimization. This runs the default Fixed Budget Scenario to maximize ROI. ```python budget_optimizer = optimizer.BudgetOptimizer(mmm) optimization_results = budget_optimizer.optimize() ``` -------------------------------- ### Configure Input Data Components Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Populates the builder with KPI, revenue, population, control, media, and reach/frequency data. ```python builder = ( builder.with_kpi(df, kpi_col="conversions") .with_revenue_per_kpi(df, revenue_per_kpi_col="revenue_per_conversion") .with_population(df) .with_controls( df, control_cols=[ "sentiment_score_control", "competitor_activity_score_control", ], ) ) channels = ["Channel0", "Channel1", "Channel2"] builder = builder.with_media( df, media_cols=[f"{channel}_impression" for channel in channels], media_spend_cols=[f"{channel}_spend" for channel in channels], media_channels=channels, ).with_reach( df, reach_cols=["Channel3_reach"], frequency_cols=["Channel3_frequency"], rf_spend_cols=["Channel3_spend"], rf_channels=["Channel3"], ) ``` -------------------------------- ### Prepare Input Data Source: https://github.com/google/meridian/blob/main/demo/ROI_mROI_Response_Curves.ipynb Configures the input data builder with KPI, revenue, population, control, and media channel information. ```python df = pd.read_csv( "https://raw.githubusercontent.com/google/meridian/refs/heads/main/meridian/data/simulated_data/csv/geo_all_channels.csv" ) builder = data_frame_input_data_builder.DataFrameInputDataBuilder( kpi_type='non_revenue', default_kpi_column='conversions', default_revenue_per_kpi_column='revenue_per_conversion', ) builder = ( builder.with_kpi(df) .with_revenue_per_kpi(df) .with_population(df) .with_controls( df, control_cols=["sentiment_score_control", "competitor_sales_control"] ) ) channels = ["Channel0", "Channel1", "Channel2", "Channel3", "Channel4"] builder = builder.with_media( df, media_cols=[f"{channel}_impression" for channel in channels], media_spend_cols=[f"{channel}_spend" for channel in channels], media_channels=channels, ) data = builder.build() ``` -------------------------------- ### Import Meridian modules and verify hardware Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Import necessary Meridian modules and verify the availability of GPU and CPU resources. ```python import IPython from meridian import constants from meridian.analysis import optimizer from meridian.analysis import summarizer from meridian.analysis import visualizer from meridian.data import data_frame_input_data_builder from meridian.model import model from meridian.model import prior_distribution from meridian.model import spec import pandas as pd # check if GPU is available from psutil import virtual_memory import tensorflow as tf import tensorflow_probability as tfp ram_gb = virtual_memory().total / 1e9 print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb)) print( 'Num GPUs Available: ', len(tf.config.experimental.list_physical_devices('GPU')), ) print( 'Num CPUs Available: ', len(tf.config.experimental.list_physical_devices('CPU')), ) ``` -------------------------------- ### Initialize Meridian Model Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Sets up the model with custom ROI priors and the prepared input data. ```python roi_rf_mu = 0.2 # Mu for ROI prior for each RF channel. roi_rf_sigma = 0.9 # Sigma for ROI prior for each RF channel. prior = prior_distribution.PriorDistribution( roi_rf=tfp.distributions.LogNormal( roi_rf_mu, roi_rf_sigma, name=constants.ROI_RF ) ) model_spec = spec.ModelSpec(prior=prior) mmm = model.Meridian(input_data=data, model_spec=model_spec) ``` -------------------------------- ### Install Meridian for CPU Users Source: https://github.com/google/meridian/blob/main/README.md Install the latest release of Meridian for macOS and general CPU users. This version does not include official GPU support for macOS. ```sh pip install --upgrade google-meridian ``` -------------------------------- ### Run Budget Optimization Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Optimizes media spend allocation based on prepared data tensors, a specified budget, and percentage of spend distribution. The output includes warnings for unsupported arguments. ```python # Default values for `budget` and `pct_of_spend` are derived from the `new_data`, # but these values can be overridden without modifying the `new_data` itself. hypothetical_optimization_results = budget_optimizer.optimize( new_data=data_tensors, budget=50_000_000, pct_of_spend=[.2, .1, .2, .2, .3] ) ``` -------------------------------- ### Load Model from Google Drive Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Mounts Google Drive to access and load a saved .pkl or .binpb model file. ```python from google.colab import drive drive_mount = '/content/drive' drive.mount(drive_mount) # @markdown Provide complete path to the file(For mounted Google Drive, a trained model is stored in `MyDrive/.binpb`): model_path= ''# @param {"type":"string","placeholder": "Drive path of your saved model"} path = f'{drive_mount}/{model_path}' meridian = None if model_path.endswith('.pkl'): meridian = model.load_mmm(path) meridian = model.Meridian(input_data=meridian.input_data, model_spec= meridian.model_spec, inference_data=meridian.inference_data) else: meridian = meridian_serde.load_meridian(path) ``` -------------------------------- ### Importing the Meridian Backend Source: https://github.com/google/meridian/blob/main/meridian/backend/README.md The entry point for accessing unified backend operations. ```python from meridian import backend ``` -------------------------------- ### Initialize Summarizer Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Instantiate the Summarizer class with the model object. ```python mmm_summarizer = summarizer.Summarizer(mmm) ``` -------------------------------- ### Display HTML Summary Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Display the generated two-page HTML summary output in a notebook environment. ```python IPython.display.HTML(filename=f'{meridian_root}/summary_output.html') ``` -------------------------------- ### Import Meridian and TensorFlow Dependencies Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Imports necessary libraries and modules for Meridian, TensorFlow Probability, and Pandas. This cell tests if all dependencies are installed appropriately. ```python # @markdown Now, run this cell and we can test if all dependencies are appropratly installed import tensorflow_probability as tfp import pandas as pd from meridian import constants from meridian.data import data_frame_input_data_builder from meridian.model import model from meridian.model import prior_distribution from meridian.model import spec from meridian.schema.processors import model_fit_processor from meridian.schema.processors import marketing_processor from meridian.schema.processors import budget_optimization_processor from meridian.schema.utils import date_range_bucketing from meridian.schema.serde import meridian_serde from scenarioplanner.converters import sheets from scenarioplanner.converters.dataframe import dataframe_model_converter from scenarioplanner import mmm_ui_proto_generator as mmm_ui_gen from scenarioplanner.linkingapi import url_generator ``` -------------------------------- ### Check System Resources Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Checks for available RAM, GPUs, and CPUs. Useful for understanding the environment before running ML tasks. ```python import pandas as pd import tensorflow as tf import tensorflow_probability as tfp from meridian import constants from meridian.data import data_frame_input_data_builder from meridian.model import model from meridian.model import spec from meridian.model import prior_distribution # check if GPU is available from psutil import virtual_memory ram_gb = virtual_memory().total / 1e9 print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb)) print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) print("Num CPUs Available: ", len(tf.config.experimental.list_physical_devices('CPU'))) ``` -------------------------------- ### Sample Prior and Posterior Distributions Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Use sample_prior() and sample_posterior() to obtain samples from the model's distributions. This step may take a significant amount of time on certain runtimes. ```python mmm.sample_prior(500) mmm.sample_posterior( n_chains=10, n_adapt=2500, n_burnin=500, n_keep=1000, seed=0, unrolled_leapfrog_steps=5, ) ``` -------------------------------- ### Initialize and Train the Model Source: https://github.com/google/meridian/blob/main/demo/ROI_mROI_Response_Curves.ipynb Initializes the Meridian model and samples from the posterior distribution. ```python # Initialize and run the model mmm = model.Meridian(input_data=data) mmm.sample_posterior( n_chains=10, n_adapt=2000, n_burnin=500, n_keep=1000, seed=0 ) ``` -------------------------------- ### Check GPU and CPU Availability Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Verifies the number of available GPUs and CPUs on your runtime environment. Displays available RAM. ```python from psutil import virtual_memory import tensorflow as tf import tensorflow_probability as tfp ram_gb = virtual_memory().total / 1e9 print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb)) print( 'Num GPUs Available: ', len(tf.config.experimental.list_physical_devices('GPU')), ) print( 'Num CPUs Available: ', len(tf.config.experimental.list_physical_devices('CPU')), ) ``` -------------------------------- ### Initialize Meridian Model with Custom Specification Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Initializes the Meridian model with the prepared input data and a custom model specification. This includes setting up ROI priors using Lognormal distribution. ```python roi_mu = 0.2 # Mu for ROI prior for each media channel. roi_sigma = 0.9 # Sigma for ROI prior for each media channel. prior = prior_distribution.PriorDistribution( roi_m=tfp.distributions.LogNormal(roi_mu, roi_sigma, name=constants.ROI_M) ) model_spec = spec.ModelSpec(prior=prior, enable_aks=True) mmm = model.Meridian(input_data=data, model_spec=model_spec) ``` -------------------------------- ### Initialize DataFrameInputDataBuilder Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Creates an instance of DataFrameInputDataBuilder to construct the InputData object. Configure default KPI and revenue columns. ```python builder = data_frame_input_data_builder.DataFrameInputDataBuilder( kpi_type='non_revenue', default_kpi_column='conversions', default_revenue_per_kpi_column='revenue_per_conversion', ) ``` -------------------------------- ### Prepare Data for Optimization Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Prepare multi-dimensional arrays from loaded data to construct a DataTensors instance for optimization. The BudgetOptimizer.create_optimization_tensors method can simplify this process. ```python n_geos = mmm.n_geos n_media_channels = mmm.n_media_channels n_non_media_channels = mmm.n_non_media_channels n_organic_media_channels = mmm.n_organic_media_channels ``` -------------------------------- ### Configure Model Analyses and Budget Optimizations Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Define specifications for model analyses and budget optimizations. This includes setting the spreadsheet name, optimization name, and whether to include non-paid channels. Date ranges and time breakdowns (yearly, quarterly, monthly) can also be configured. ```python mmm=meridian # @title Define specs for model analyses and budget optimizations # @markdown ## General setup spreadsheet_name = "Meridian Looker Studio Small Demo Data" # @param {"type":"string"} optimization_name = "Colab demo" # @param {"type":"string"} include_non_paid_channels = True # @param {"type":"boolean","placeholder":"Whether to include non-paid channels in the analysis."} start_date = None end_date = None # @markdown ## Time breakdown setup # @markdown By default, the analysis results and budget scenarios are based on # @markdown the whole time period in the dataset. The following enables you to # @markdown generate additional results for more granular time frames. # @markdown Breakdowns: yearly = False # @param {"type":"boolean","placeholder":"Whether to optimize budget for every year"} quarterly = True # @param {"type":"boolean","placeholder":"Whether to optimize budget for every quarter"} monthly = False # @param {"type":"boolean","placeholder":"Whether to optimize budget for every month"} ``` ```python # @markdown ## Interactive budget optimization setup # @markdown The following controls the size of the optimization grid. The larger # @markdown the grid, the more adjustment can be done during the interactive # @markdown budget optimization when using the Meridian Looker Studio report. # @markdown * `min_spend_shift_ratio` determines the minimum available spend by # @markdown reducing the original spend by the specified proportion. # @markdown * `max_spend_shift_ratio` determines the maximum available spend by # @markdown increasing the original spend by the specified proportion. min_spend_shift_ratio = 1.0 # @param {"type":"raw","placeholder":"From 0 to 1."} max_spend_shift_ratio = 1.0 # @param {"type":"raw","placeholder":"A number greater than 0."} # @markdown If the input data contains reach and frequency channels, you can choose to use optimal frequency during budget optimization: use_optimal_frequency = True # @param {"type":"boolean","placeholder":"Whether to optimize budget with optimal frequency data"} # @markdown If the frequency range is too wide, this cell would take longer time to finish, please consider limit the max frequency if you experienced a timeout issue: max_frequency = 10.0 # @param {"type":"raw","placeholder":"A number greater than 0."} ``` -------------------------------- ### Accessing Probabilistic Distributions Source: https://github.com/google/meridian/blob/main/meridian/backend/README.md Use the backend.tfd alias to ensure compatibility with the correct TFP substrate. ```python import tensorflow_probability as tfp tfd = tfp.distributions my_dist = tfd.LogNormal(loc=0.2, scale=0.9) ``` ```python from meridian import backend my_dist = backend.tfd.LogNormal(loc=0.2, scale=0.9) ``` -------------------------------- ### Add KPI, Revenue, Population, and Controls to Builder Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Adds core data components like KPIs, revenue, population, and control variables to the builder. Control columns for sentiment and competitor sales are specified. ```python builder = ( builder.with_kpi(df) .with_revenue_per_kpi(df) .with_population(df) .with_controls( df, control_cols=["sentiment_score_control", "competitor_sales_control"] ) ) ``` -------------------------------- ### Initialize DataFrameInputDataBuilder Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Creates an instance of the builder with the specified KPI type. ```python builder = data_frame_input_data_builder.DataFrameInputDataBuilder( kpi_type='non_revenue' ) ``` -------------------------------- ### Plotting Response Curves with MediaEffects Source: https://github.com/google/meridian/blob/main/demo/ROI_mROI_Response_Curves.ipynb Initializes the MediaEffects visualizer and generates plots for channel response curves, including credible intervals. ```python # Create a MediaEffects visualizer object media_effects = visualizer.MediaEffects(mmm) # Plot the response curves for each channel media_effects.plot_response_curves( plot_separately=True, include_ci=True # Shows the credible interval on the curve ) ``` -------------------------------- ### Display Optimization Report Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Render the optimization summary HTML report. ```python IPython.display.HTML(filename='/content/drive/MyDrive/optimization_output.html') ``` -------------------------------- ### Collect Simulated Parameters (KPI_per_capita) Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Collects simulated parameters on the raw scale of KPI_per_capita, including various alpha, beta, ec, eta, gamma, mu, sigma, slope, tau, and xi values. ```python # @title Collecting simulated parameters (on raw scale of KPI_per_capita) dict_simul_param_on_raw_scale = { 'alpha_m': alpha_m.numpy(), 'alpha_rf': alpha_rf.numpy(), 'beta_gm': beta_gm.numpy()[:, :n_imp_channels], 'beta_grf': beta_gm.numpy()[:, -n_rf_channels:], 'beta_m': beta_m.numpy()[:n_imp_channels], 'beta_rf': beta_m.numpy()[-n_rf_channels:], 'ec_m': ec_m.numpy(), 'ec_rf': ec_rf.numpy(), 'eta_m': eta_m.numpy()[:n_imp_channels], 'eta_rf': eta_m.numpy()[-n_rf_channels:], 'gamma_c': gamma_c.numpy(), 'gamma_gc': gamma_gc.numpy(), 'mu_t': mu_t.numpy(), 'sigma': sigma.numpy(), 'slope_m': slope_m.numpy(), 'slope_rf': slope_rf.numpy(), 'tau_g': tau_g.numpy(), 'xi_c': xi_c.numpy(), 'intercept_gt': (tau_g[..., tf.newaxis] + mu_t[tf.newaxis, ...]).numpy(), } ``` -------------------------------- ### Sample Prior and Posterior Distributions Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Executes MCMC sampling for the model parameters. Note that this process can be time-intensive depending on hardware. ```python %%time mmm.sample_prior(500) mmm.sample_posterior( n_chains=10, n_adapt=2000, n_burnin=500, n_keep=1000, seed=1 ) ``` -------------------------------- ### Add Non-Media Treatments and Organic Media to Builder Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Includes non-media treatment data and organic media data. Specifies columns for non-media treatments and organic media impressions and channels. ```python builder = builder.with_non_media_treatments( df, non_media_treatment_cols=['Promo'] ).with_organic_media( df, organic_media_cols=['Organic_channel0_impression'], organic_media_channels=['Organic_channel0'], ) ``` -------------------------------- ### Simulate Impressions Per Channel Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Simulates media impressions by combining channel-specific, time-specific, and geo-specific factors, then rounding to the nearest whole number. It also calculates and prints the sparsity percentage for impression per capita for each channel. ```python # @title Simulate impression per channel u_m = tfp.distributions.Normal(1, 0.5).sample(n_total_paid_channels) u_tm = tfp.distributions.Normal(0.8, 0.5).sample( [n_times, n_total_paid_channels] ) u_gtm = tfp.distributions.Normal(0, 0.5).sample( (n_geos, n_times, n_total_paid_channels) ) # impression per capita per channel ipc_gtm = np.maximum(u_m + u_tm + u_gtm, 0.0) # impression per channel impression_gtm = tf.round(ipc_gtm * p_gtm) # Check percentage or sparsity ipc_sparsity = np.sum(ipc_gtm == 0.0, axis=(0, 1)) / (n_geos * n_times) ipc_sparsity_percentage = [f'{s * 100:.2f}%' for s in ipc_sparsity] print( 'percentage of sparsity of impression_per_capita for each channel:' f' {ipc_sparsity_percentage}' ) ``` -------------------------------- ### Export Optimization Summary Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Save the optimization results to an HTML report. ```python filepath = '/content/drive/MyDrive' optimization_results.output_optimization_summary( 'optimization_output.html', filepath ) ``` -------------------------------- ### Import Packages and Check Environment Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Imports essential libraries for Meridian and data analysis, including TensorFlow, TensorFlow Probability, and Xarray. It also checks for GPU availability and reports RAM and CPU counts. ```python import datetime import matplotlib.pyplot as plt import numpy as np import tensorflow as tf import tensorflow_probability as tfp import xarray as xr import pickle from meridian.model import model from meridian.model import prior_distribution from meridian.model import transformers from meridian.model import knots # check if GPU is available from psutil import virtual_memory ram_gb = virtual_memory().total / 1e9 print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb)) print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) print("Num CPUs Available: ", len(tf.config.experimental.list_physical_devices('CPU'))) ``` -------------------------------- ### Run Meridian Model Sampling with MLflow Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Initializes the Meridian model within an MLflow run context and samples from the prior and posterior distributions. This step may take a significant amount of time, especially on CPU runtimes. ```python import mlflow with mlflow.start_run(run_name="my-run"): mmm = model.Meridian(input_data=data, model_spec=model_spec) mmm.sample_prior(500) mmm.sample_posterior(n_chains=7, n_adapt=1000, n_burnin=500, n_keep=1000, seed=1) ``` -------------------------------- ### Create Analyzer Instance Source: https://github.com/google/meridian/blob/main/demo/ROI_mROI_Response_Curves.ipynb Instantiates the Analyzer class to extract business metrics from the trained model. ```python # Create an instance of the Analyzer class analyzer_instance = analyzer.Analyzer( model_context=mmm.model_context, inference_data=mmm.inference_data ) ``` -------------------------------- ### Offer Components to Data Builder Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Adds various data components to the builder, including KPI, revenue, population, controls, and media data. Components can be added all at once or piecewise. ```python builder = ( builder.with_kpi(df, kpi_col="conversions") .with_revenue_per_kpi(df, revenue_per_kpi_col="revenue_per_conversion") .with_population(df) .with_controls( df, control_cols=["sentiment_score_control", "competitor_sales_control"] ) ) channels = ["Channel0", "Channel1", "Channel2", "Channel3", "Channel4"] builder = builder.with_media( df, media_cols=[f"{channel}_impression" for channel in channels], media_spend_cols=[f"{channel}_spend" for channel in channels], media_channels=channels, ) data = builder.build() ``` -------------------------------- ### Build InputData Instance Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Finalizes the builder configuration to produce the InputData object. ```python data = builder.build() ``` -------------------------------- ### Display Optimization Report Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Display the generated two-page HTML optimization report in a notebook environment. ```python IPython.display.HTML(filename=f'{meridian_root}/optimization_output.html') ``` -------------------------------- ### Mount Google Drive for Colab Free/Pro Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Mounts your Google Drive to the specified path for use as external storage. This is recommended for organizing distinct execution runs. ```python # @markdown If you are using Colab Free, Colab Pro, run this cell to mount your Google Drive. from google.colab import drive drive_mount = '/content/drive' drive.mount(drive_mount, force_remount=True) subfolder = '' # @param {"type":"string","placeholder": "Optional, specifying a subfolder is recommended for organizing distinct execution runs."} # Change this "MyDrive" to other share folders name if you would like to use a different drive. meridian_root = f'{drive_mount}/MyDrive/{subfolder}' is_enterprise_user=False ``` -------------------------------- ### Load Model from Local Storage Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Loads a model file from a local path after it has been downloaded from third-party storage. ```python path = "" # @param {"type":"string","placeholder": "/content/....binpb"} meridian = None if path.endswith('.pkl'): meridian = model.load_mmm(path) meridian = model.Meridian(input_data=meridian.input_data, model_spec= meridian.model_spec, inference_data=meridian.inference_data) else: meridian = meridian_serde.load_meridian(path) ``` -------------------------------- ### Initialize Meridian with Model Specification Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Initialize the Meridian class with loaded data and a customized model specification, including ROI priors. ```python roi_mu = 0.2 # Mu for ROI prior for each media channel. roi_sigma = 0.9 # Sigma for ROI prior for each media channel. prior = prior_distribution.PriorDistribution( roi_m=tfp_jax.distributions.LogNormal(roi_mu, roi_sigma, name=constants.ROI_M) ) model_spec = spec.ModelSpec(prior=prior, enable_aks=True) mmm = model.Meridian(input_data=data, model_spec=model_spec) ``` -------------------------------- ### Create xarray DataArray for Media Impressions Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Initializes an xarray DataArray to hold media impression data. Requires pre-defined variables for impression data, dimensions, and coordinate names. ```python media_data = xr.DataArray( impression_gtm, dims=[GEO_DIM_NAME, TIME_DIM_NAME, CHANNEL_DIM_NAME], coords={ GEO_DIM_NAME: GEO_NAMES, TIME_DIM_NAME: TIME_NAMES, CHANNEL_DIM_NAME: CHANNEL_NAMES, }, name=IMPRESSIONS_COL_SUFFIX, ) ``` -------------------------------- ### Performing Standardized Numerical Operations Source: https://github.com/google/meridian/blob/main/meridian/backend/README.md Use backend-agnostic functions like einsum and concatenate instead of framework-specific imports. ```python import tensorflow as tf result = tf.einsum('gc,gtc->gt', gamma_gc, controls_scaled) concatenated = tf.concat([part1, part2], axis=-1) ``` ```python from meridian import backend result = backend.einsum('gc,gtc->gt', gamma_gc, controls_scaled) concatenated = backend.concatenate([part1, part2], axis=-1) ``` -------------------------------- ### Load Model from Google Cloud Storage Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Authenticates with GCP and uses gcsfuse to mount a bucket for loading model files. ```python import os from google.colab import auth auth.authenticate_user() # @markdown [Google Cloud Storage] If you are an Enterprise user and would like to load model(pkl or proto file) from GCS, run this cell with your bucket name and path. If you are not an Enterprise user, but would like to use Cloud Storage, please open this cell accordingly before excution. project_id = ""# @param {"type":"string","placeholder": "Cloud project id"} bucket_name = "" # @param {"type":"string","placeholder": "GCS bucket that contains your model"} model_path = "" # @param {"type":"string","placeholder": "/"} os.environ['GOOGLE_CLOUD_PROJECT'] = project_id !gcloud config set project {project_id} !mkdir /content/{bucket_name} # Uncomment below if you don't have gcsfuse installed #!echo "deb https://packages.cloud.google.com/apt gcsfuse-`lsb_release -c -s` main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list #!curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - #!apt-get update #!apt-get install gcsfuse !gcsfuse --implicit-dirs {bucket_name} /content/{bucket_name} path = f'/content/{bucket_name}/{model_path}' meridian = None if model_path.endswith('.pkl'): meridian = model.load_mmm(path) meridian = model.Meridian(input_data=meridian.input_data, model_spec= meridian.model_spec, inference_data=meridian.inference_data) else: meridian = meridian_serde.load_meridian(path) !fusermount -u /content/{bucket_name} ``` -------------------------------- ### Train a New Meridian Model Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Initializes and trains a model using the DataFrameInputDataBuilder. Requires GPU for optimal performance. ```python df = pd.read_csv( "https://raw.githubusercontent.com/google/meridian/refs/heads/main/meridian/data/simulated_data/csv/geo_all_channels.csv" ) media_channels = ["Channel0", "Channel1", "Channel2", "Channel3", "Channel4"] non_media_channels = ["Promo"] organic_channels = ["Organic_channel0"] builder = ( data_frame_input_data_builder.DataFrameInputDataBuilder( kpi_type='non_revenue') .with_kpi(df, kpi_col="conversions") .with_revenue_per_kpi(df, revenue_per_kpi_col="revenue_per_conversion") .with_population(df) .with_controls( df, control_cols=["sentiment_score_control", "competitor_sales_control"]) .with_media( df, media_cols=[f"{channel}_impression" for channel in media_channels], media_spend_cols=[f"{channel}_spend" for channel in media_channels], media_channels=media_channels) .with_non_media_treatments(df,non_media_treatment_cols=non_media_channels) .with_organic_media( df, organic_media_cols=[f"{channel}_impression" for channel in organic_channels], organic_media_channels=organic_channels, media_time_col=constants.TIME) ) data = builder.build() roi_mu = 0.2 # Mu for ROI prior for each media channel. roi_sigma = 0.9 # Sigma for ROI prior for each media channel. prior = prior_distribution.PriorDistribution( roi_m=tfp.distributions.LogNormal(roi_mu, roi_sigma, name=constants.ROI_M) ) model_spec = spec.ModelSpec(prior=prior) meridian = model.Meridian(input_data=data, model_spec=model_spec) meridian.sample_prior(500) meridian.sample_posterior( n_chains=10, n_adapt=2000, n_burnin=500, n_keep=1000, seed=1 ) ``` -------------------------------- ### Configure and Export Meridian Data Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Configures time breakdown generators and channel constraints, creates a budget optimization spec, and uploads the resulting dataframes to a Google Spreadsheet. ```python time_breakdown_generators = [] if yearly: time_breakdown_generators.append( date_range_bucketing.YearlyDateRangeGenerator ) if quarterly: time_breakdown_generators.append( date_range_bucketing.QuarterlyDateRangeGenerator ) if monthly: time_breakdown_generators.append( date_range_bucketing.MonthlyDateRangeGenerator ) channel_constraints = [] if min_spend_shift_ratio is not None and max_spend_shift_ratio is not None: for channel in mmm.input_data.get_all_paid_channels(): channel_constraints.append( budget_optimization_processor.ChannelConstraintRel( channel_name=channel, spend_constraint_lower=min_spend_shift_ratio, spend_constraint_upper=max_spend_shift_ratio, ) ) grid_name_prefix = ("-").join(optimization_name.lower().split(" ")) budgetOptSpec = budget_optimization_processor.BudgetOptimizationSpec( start_date=start_date, end_date=end_date, optimization_name=optimization_name, grid_name=grid_name_prefix, constraints=channel_constraints, use_optimal_frequency=use_optimal_frequency, max_frequency=max_frequency, ) print("Creating the proto data.") summary_spec = marketing_processor.MediaSummarySpec( include_non_paid_channels=include_non_paid_channels) mmm_proto = mmm_ui_gen.create_mmm_ui_data_proto( mmm=mmm, specs=[ model_fit_processor.ModelFitSpec(), marketing_processor.MarketingAnalysisSpec( media_summary_spec=summary_spec, ), budgetOptSpec, ], time_breakdown_generators=time_breakdown_generators, ) print("Converting the proto to a dataframe") converter = dataframe_model_converter.DataFrameModelConverter(mmm_proto) dataframes = converter() print("Uploading data to a Google spreadsheet.") if spreadsheet_name: spreadsheet = sheets.upload_to_gsheet( dataframes, credentials, spreadsheet_name=spreadsheet_name ) else: spreadsheet = sheets.upload_to_gsheet(dataframes, credentials) print(f"Spreadsheet URL: {spreadsheet.url}") ``` -------------------------------- ### Add Components to DataFrameInputDataBuilder Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Adds various data components like KPI, revenue, population, controls, and media to the builder. Media columns are defined using a list of channel names. ```python builder = ( builder.with_kpi(df) .with_revenue_per_kpi(df) .with_population(df) .with_controls( df, control_cols=["sentiment_score_control", "competitor_sales_control"] ) ) channels = ["Channel0", "Channel1", "Channel2", "Channel3", "Channel4"] builder = builder.with_media( df, media_cols=[f"{channel}_impression" for channel in channels], media_spend_cols=[f"{channel}_spend" for channel in channels], media_channels=channels, ) ``` -------------------------------- ### Sample from Prior Distributions Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Obtains samples from the prior distributions of the model parameters using the sample_prior() method. This is useful for understanding the prior beliefs of the model. ```python mmm.sample_prior(500) ``` -------------------------------- ### Assess Model Fit Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Visualizes the model fit by comparing expected versus actual sales. ```python model_fit = visualizer.ModelFit(mmm) model_fit.plot_model_fit() ``` -------------------------------- ### Generate Looker Studio Report Link Source: https://github.com/google/meridian/blob/main/demo/Meridian_Scenario_Planner_Beta.ipynb Generates a clickable HTML link to access the Meridian Scenario Planner report in Looker Studio. ```python # @markdown Run this cell to access the Meridian Scenario Planner in Looker Studio url = url_generator.create_report_url(spreadsheet) html_link = f'Click to create a report' from IPython.display import HTML HTML(html_link) ``` -------------------------------- ### Display HTML Output Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Render the generated HTML report within the notebook environment. ```python IPython.display.HTML(filename='/content/drive/MyDrive/summary_output.html') ``` -------------------------------- ### Helper Functions for Naming Arrays Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Provides utility functions for generating sample names and times, and for manipulating string suffixes. These are useful for organizing and labeling data arrays. ```python def _sample_names(prefix: str, n_names: int | None = None) -> list[str]: """Generates a list of sample names. It concatenates the same prefix with consecutive numbers to generate a list of strings that can be used as sample names of columns/arrays/etc. """ res = [prefix + str(n) for n in range(n_names)] if n_names else None return res ``` ```python def _sample_times( n_times: int, start_date: datetime.date = datetime.date(2021, 1, 25) ) -> list[str]: """Generates sample `time`s.""" res = [ (start_date + datetime.timedelta(weeks=w)).strftime('%Y-%m-%d') for w in range(n_times) ] return res ``` ```python def add_suffix(string: str, suffix: str) -> str: return '_'.join([string, suffix]) def remove_suffix(string: str, suffix: str) -> str: return string.replace('_' + suffix, '') ``` -------------------------------- ### Prepare Model Specification Source: https://github.com/google/meridian/blob/main/demo/Meridian_MLflow_Demo.ipynb Defines the prior distributions for model parameters, including ROI priors for media channels. This sets up the `ModelSpec` object required for Meridian. ```python roi_mu = 0.2 # Mu for ROI prior for each media channel. roi_sigma = 0.9 # Sigma for ROI prior for each media channel. prior = prior_distribution.PriorDistribution( roi_m=tfp.distributions.LogNormal(roi_mu, roi_sigma, name=constants.ROI_M) ) model_spec = spec.ModelSpec(prior=prior) ``` -------------------------------- ### Output Optimization Summary Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Export the two-page HTML optimization report, which includes optimized spend allocations and ROI. Requires the optimization results object, filename, and filepath. ```python filepath = meridian_root optimization_results.output_optimization_summary( 'optimization_output.html', filepath ) ``` -------------------------------- ### Export Optimization Report Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Exports the results of the budget optimization to an HTML file. This allows for easy viewing and sharing of the optimization summary. ```python filepath = meridian_root hypothetical_optimization_results.output_optimization_summary( 'hypothetical_optimization_output.html', filepath ) ``` -------------------------------- ### Generate and Save EDA Report Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started.ipynb Initializes MeridianEDA with a Meridian model object and generates an HTML report. Requires prior samples; automatically runs prior sampling if needed. ```python eda = meridian_eda.MeridianEDA(mmm) filename = 'eda_report.html' eda.generate_and_save_report(filename=filename, filepath=meridian_root) IPython.display.HTML(filename=f'{meridian_root}/{filename}') ``` -------------------------------- ### Assess Model Convergence Source: https://github.com/google/meridian/blob/main/demo/Meridian_RF_Demo.ipynb Generates r-hat statistics to verify MCMC convergence. ```python model_diagnostics = visualizer.ModelDiagnostics(mmm) model_diagnostics.plot_rhat_boxplot() ``` -------------------------------- ### Import os Module Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Import the os module. This is a standard Python library for interacting with the operating system. ```python import os ``` -------------------------------- ### Load Hypothetical Data Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Load hypothetical future scenario data from a CSV file into a Pandas DataFrame for budget optimization. ```python df = pd.read_csv( "https://raw.githubusercontent.com/google/meridian/refs/heads/main/meridian/data/simulated_data/csv/hypothetical_geo_all_channels.csv" ) ``` -------------------------------- ### Plot Model Fit Comparison Source: https://github.com/google/meridian/blob/main/demo/Meridian_Getting_Started_Jax.ipynb Visualize the model's fit by comparing expected sales against actual sales. This helps in assessing how well the model represents the data. ```python model_fit = visualizer.ModelFit(mmm) model_fit.plot_model_fit() ``` -------------------------------- ### Simulate Cost and Unit Value Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb Simulates Cost Per Mille (CPM) for paid channels and unit values across geos and times. It then plots histograms of the resulting spend per channel. ```python # @title Simulate cost and unit value CPM_m = tfp.distributions.Uniform(0.011, 0.012).sample(n_total_paid_channels) cost_gtm = impression_gtm * CPM_m unit_value = tfp.distributions.Uniform(0.0345, 0.0355).sample((n_geos, n_times)) ``` -------------------------------- ### Generate KPI and Revenue Source: https://github.com/google/meridian/blob/main/demo/RF_Data_Simulation_for_Meridian.ipynb This snippet calculates KPI per capita and total KPI, then derives revenue. It also includes plotting functionality for the averaged KPI over time. Ensure necessary libraries like tensorflow and matplotlib are imported. ```python # KPI per capita KPI_per_capita_gt = tf.maximum( tau_g[..., tf.newaxis] + tf.einsum("gtm,gm->gt", transformed_control_gtc, gamma_gc) + eps_gt + mu_t[tf.newaxis, ...], 0.0, ) + tf.einsum("gtm,gm->gt", media_rf_transformed, beta_gm) # KPI and revenue KPI_gt = KPI_per_capita_gt * p_g[..., tf.newaxis] Revenue_gt = KPI_gt * unit_value # plot plt.plot(np.mean(KPI_gt, axis=(0))) plt.ylabel("KPI averaged over geos") plt.xlabel("Time") plt.show() ```