Skip to main content

Budget optimizer for nested MMMs

Project description

budget_optimizer

Matthew Reda

This library is to help wrap custom models for use in budget optimization. It is designed to work with nested MMMs, where the budget is allocated to different media channels and the performance is measured by multiple KPIs which are fed into a downstream revenue model.

For example in a typical MMM, the budget is allocated to different media channels and the performance is measured by sales, website visits, and brand awareness. Website visits and brand awareness impact sales, website visits are impacted by brand awareness. So the effects of changing the budget flow through the nested KPIs.

The library is designed to work with any model that can be wrapped in a Model class. The Model class should have needs a predict method that takes a dictionary of parameters and returns an xarray dataset with the model prediction for that model’s kpi. These model classes can be composed in a NestedModel class which will flow the predictions into the next stage of the model.

Model loading and functions to define how budget translates into model inputs must be defined in a seperate file. Included in the folder with the model artifacts in a file called model_config.py which should contain the following functions:

  • model_loader - a function that takes a path and returns a Model object
  • budget_to_model_inputs - a function that a budget and model object and returns a dataset of model inputs

[!NOTE]

How to define the model_config.py file

See the example in the example_files folder for an example of how to define these functions for a simple model.

Developer Guide

If you are new to using nbdev here are some useful pointers to get you started.

Install budget_optimizer in Development mode

# make sure budget_optimizer package is installed in development mode
$ pip install -e .

# make changes under nbs/ directory
# ...

# compile to have changes apply to budget_optimizer
$ nbdev_prepare

Usage

Installation

Install latest from the GitHub repository:

$ pip install git+https://github.com/redam94/budget_optimizer.git

or from pypi

$ pip install budget_optimizer

Documentation

Documentation can be found hosted on this GitHub repository’s pages. Additionally you can find package manager specific guidelines on conda and pypi respectively.

How to use

## file: example_files/model_1/model_config.py
import xarray as xr
from pathlib import Path
import numpy as np
from budget_optimizer.utils.model_helpers import AbstractModel, BudgetType

INITIAL_BUDGET: BudgetType = dict(a=2., b=3.)

class SimpleModel(AbstractModel):
  """
  Simple model that just adds the two variables a and b.
  This can be as complex as you want as long as it has a predict method
  that takes an xarray Dataset and returns an xarray DataArray and 
  a contributions method that takes an xarray Dataset and returns an xarray Dataset.
  
  Ideally, the model should also have data that defines the initial data that the
  model was trained on. You can wrap cutom models or functions in a class like this.
  """
  def __init__(self, data: xr.Dataset = None):
    self.data = data
    
  def predict(self, x: xr.Dataset) -> xr.DataArray:
    x = x.copy()
    x["prediction"] = x["a"] + x["b"]
    return x["prediction"]
  
  def contributions(self, x: xr.Dataset) -> xr.Dataset:
    return x

def budget_to_data(budget: BudgetType, model: AbstractModel) -> xr.Dataset:
    data = model.data.copy()
    for key, value in budget.items():
        data[key] = value/INITIAL_BUDGET[key]*data[key]
    return data
  
def model_loader(path: Path) -> AbstractModel:
    rng = np.random.default_rng(42)
    data_a = xr.DataArray(np.exp(1+rng.normal(0, .1, size=156)), dims='time', coords={"time": np.arange(1, 157)})
    data_b = xr.DataArray(np.exp(2+rng.normal(0, .1, size=156)), dims='time', coords={"time": np.arange(1, 157)})
    return SimpleModel(data = xr.Dataset({"a": data_a, "b": data_b}))
class RevenueModel(BaseBudgetModel):
    def __init__(self, model_name: str, model_kpi: str, model_path: str):
        super().__init__(model_name, model_kpi, model_path)
MODEL_NAME = "Revenue Model"
MODEL_KPI = "Revenue"
MODEL_PATH = "../example_files/model_1"
model = RevenueModel(MODEL_NAME, MODEL_KPI, MODEL_PATH)
budget_1 = dict(a=1, b=2)
budget_2 = dict(a=2, b=1)
outcome_budget_1 = model.predict(budget_1)
outcome_budget_2 = model.predict(budget_2)

Figure 1: Revenue Performance of Budget 1 and Budget 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

budget_optimizer-0.0.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

budget_optimizer-0.0.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file budget_optimizer-0.0.2.tar.gz.

File metadata

  • Download URL: budget_optimizer-0.0.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for budget_optimizer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 25c1ca8ea67ad5eab48d5bf0a0fe4a08f831e2e3efaa8d89148a6d1c7d3d657c
MD5 53e98cdfd06d5b5aa98e245fc96dfabb
BLAKE2b-256 55c2620dc7842a64eb7cb0ab2108a85520564ff73dc5e78aa3f221ea1c7cca9a

See more details on using hashes here.

File details

Details for the file budget_optimizer-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for budget_optimizer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eee7199a6423a0b363b7e856936cc611cd2ced424092f2331ca50c2433d3ae7c
MD5 b77c3ec5da467bf2605e30e5f6962024
BLAKE2b-256 532dd88ea47ffe816cb63dd9f0ff4a04744ebba35235879610d91b05400e71d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page