Module: schema.processors.model_kernel_processor

Module for transforming a Meridian model into a structured MMM schema.

This module provides the ModelKernelProcessor, which is responsible for transforming the internal state of a trained Meridian model object into a structured and portable format defined by the MmmKernel protobuf message.

The "kernel" includes essential information about the model, such as:

  • Model specifications and hyperparameters.
  • Inferred parameters distributions (as a serialized ArViz inference data).
  • MMM-agnostic marketing data (i.e. input data to the model).

This serialized representation allows the model to be saved, loaded, and analyzed across different environments or by other tools that understand the MmmKernel schema.

The serialization logic is primarily handled by the MeridianSerde class from the schema.serde package.

Key Classes:

  • ModelKernelProcessor: The processor class that takes a Meridian model instance and populates an MmmKernel message.

Example Usage:

import meridian
from meridian.model import model
from mmm.v1 import mmm_pb2
from schema.processors import model_kernel_processor
import semver

# Assuming 'mmm' is a `meridian.model.Meridian` object.
# Example:
# mmm = meridian.model.Meridian(...)
# mmm.sample_prior(...)
# mmm.sample_posterior(...)

processor = model_kernel_processor.ModelKernelProcessor(
    meridian_model=mmm,
    model_id="my_model_v1",
)

# Create an output Mmm proto message
output_proto = mmm_pb2.Mmm()

# Populate the mmm_kernel field
processor(output_proto)

# Now output_proto.mmm_kernel contains the serialized model.
# This can be saved to a file, sent over a network, etc.
print(f"Model Kernel ID: {output_proto.mmm_kernel.model_id}")
print(f"Meridian Version: {output_proto.mmm_kernel.meridian_version}")
# Access other fields within output_proto.mmm_kernel as needed.

Classes

class ModelKernelProcessor: Transcribes a model's stats into an "MmmKernel message.