Skip to content

mixdyn.data

The names the top-level mixdyn package lists are some of this package's, imported from it, so they are documented here.

The data layer: a typed media panel on one regular calendar, and the boundary that reads it.

NATIONAL = Geo('national') module-attribute

The geo of a panel read without a geo column.

Channel = NewType('Channel', str) module-attribute

Geo = NewType('Geo', str) module-attribute

KpiUnit = Revenue | Count

ColumnData = Mapping[str, Any] | ColumnFrame

Combine

Bases: Enum

How the controls of the geos below a node make the node's.

SUM = 'sum' class-attribute instance-attribute

A count or an amount, which adds up.

POPULATION_MEAN = 'population_mean' class-attribute instance-attribute

A rate or a level, such as a price or a temperature: the mean weighted by population.

Issue(code, severity, message, geo=None, column=None, dates=(), channel=None) dataclass

One finding of read_panel: what it is, how severe, and where.

geo and column are None when the finding is not about a single one. dates holds every date involved; message lists only the first few. channel names the channel whose spend the finding is about, by the panel's name for it, which need not be its column's: read_panel's spend maps a channel to a column of any name. It is None for every other finding, a spend column missing or given two roles among them, which name the column alone.

code instance-attribute

severity instance-attribute

message instance-attribute

geo = None class-attribute instance-attribute

column = None class-attribute instance-attribute

dates = () class-attribute instance-attribute

channel = None class-attribute instance-attribute

IssueCode

Bases: Enum

What a finding is about; its value is the code a document of ISSUES writes.

MISSING_COLUMN = 'missing_column' class-attribute instance-attribute

BAD_SPEC = 'bad_spec' class-attribute instance-attribute

The reading instructions contradict themselves or the data: a column in two roles, unequal column lengths, no rows.

BAD_DATE = 'bad_date' class-attribute instance-attribute

BAD_GEO = 'bad_geo' class-attribute instance-attribute

NOT_NUMERIC = 'not_numeric' class-attribute instance-attribute

NON_FINITE = 'non_finite' class-attribute instance-attribute

NEGATIVE_SPEND = 'negative_spend' class-attribute instance-attribute

MISSING_KPI = 'missing_kpi' class-attribute instance-attribute

OFF_GRID = 'off_grid' class-attribute instance-attribute

A date is not on the cadence's grid that the other dates share.

DUPLICATE = 'duplicate' class-attribute instance-attribute

MISSING_PERIOD = 'missing_period' class-attribute instance-attribute

A geo has no row for a period inside the panel's calendar.

CONSTANT = 'constant' class-attribute instance-attribute

UNIT = 'unit' class-attribute instance-attribute

HIERARCHY = 'hierarchy' class-attribute instance-attribute

POPULATION = 'population' class-attribute instance-attribute

CADENCE = 'cadence' class-attribute instance-attribute

The dates are spaced finer or coarser than the cadence named: reading does not resample.

FEW_OBSERVATIONS = 'few_observations' class-attribute instance-attribute

A geo has fewer observed periods per effect, channel or control, than an effect needs.

ENDOGENOUS_SPEND = 'endogenous_spend' class-attribute instance-attribute

A channel's spend reads the KPI's last period beyond its own last period and the calendar (mixdyn.data.feedback.spend_feedback): a model that reads the spend as set apart from demand credits the channel with the demand.

PanelError(issues)

Bases: ValueError

The data cannot be read as a panel. issues holds every error found, and the warnings.

MAX_LISTED = 40 class-attribute instance-attribute

issues = issues instance-attribute

Severity

Bases: Enum

Whether a finding stops reading.

ERROR = 'error' class-attribute instance-attribute

Reading fails.

WARNING = 'warning' class-attribute instance-attribute

Reading succeeds; the panel carries the finding in issues.

Allotment

Bases: Enum

How a channel bought nationally is split over the geos, period by period.

POPULATION = 'population' class-attribute instance-attribute

In proportion to each geo's population.

Baseline

Bases: Enum

A lever's counterfactual level read off the panel: the least or the most the lever was observed at, over every geo's periods that were not filled.

MIN = 'min' class-attribute instance-attribute

MAX = 'max' class-attribute instance-attribute

Cadence

Bases: Enum

The spacing of consecutive periods. A monthly period is labelled by its month's first day.

DAY = 'day' class-attribute instance-attribute

WEEK = 'week' class-attribute instance-attribute

MONTH = 'month' class-attribute instance-attribute

Count(unit) dataclass

The KPI counts events (orders, installs, sign-ups) named by unit.

A return on spend is then a cost per event, not a ratio.

unit instance-attribute

Lever(kind, baseline=Baseline.MIN) dataclass

A control the panel declares a lever: its kind, and the level its contribution is measured against, baseline, the lever's observed least or most or a level given in the control's own units.

A lever enters a model as its control does, with no adstock or curve. Its contribution at a period is its coefficient times its departure from the baseline, so the KPI at the baseline is the base, and a planner never allocates it: it has no spend.

Raises:

Type Description
ValueError

on a kind that is not a LeverKind, or a baseline neither a Baseline nor a finite number.

kind instance-attribute

baseline = Baseline.MIN class-attribute instance-attribute

LeverKind

Bases: Enum

What a lever is: a column of the panel, not media spend, that moves the KPI.

PRICE = 'price' class-attribute instance-attribute

A price, or a discount off it.

PROMOTION = 'promotion' class-attribute instance-attribute

A promotion's depth or intensity, or a flag that one ran.

ORGANIC = 'organic' class-attribute instance-attribute

Activity bought with no media spend: email sends, organic posts, unpaid search.

MediaPanel(dates, cadence, geos, channels, controls, kpi, kpi_unit, spend, currency, control_values, filled, hierarchy=(lambda: MappingProxyType({}))(), population=None, national=(lambda: MappingProxyType({}))(), issues=(), levers=(lambda: MappingProxyType({}))(), events=(lambda: MappingProxyType({}))()) dataclass

A KPI, media spend and controls for every geo and period of a regular calendar.

Arrays are read-only, indexed by geo, then period, then channel or control:

  • kpi, float64 (geo, period): NaN where a period has no observation.
  • spend, float64 (geo, period, channel): finite, non-negative, in currency.
  • control_values, float64 (geo, period, control): finite except on filled steps.
  • filled, bool (geo, period): True on a step the input had no row for. Such a step has no KPI observation, zero spend in every channel and NaN controls.

hierarchy maps each geo to its parent and, when not empty, is one tree whose leaves are exactly geos. population, float64 (geo,), is each geo's, finite and positive, or None when not known. national names the channels bought nationally and how each was split over the geos: every period, a geo's spend on one is its share of the period's national spend. issues holds the warnings raised while reading.

levers declares some controls levers (Lever): price, promotion or organic activity whose contribution is measured against a counterfactual level, counterfactual. events names calendar events, holidays among them, each by its days, every one inside the calendar: an event's column (event_column) is 1 in each period one of its days falls in, the same in every geo, and 0 elsewhere, and a model reads it by the event's name as it reads a control's (regressors).

dates instance-attribute

cadence instance-attribute

geos instance-attribute

channels instance-attribute

controls instance-attribute

kpi instance-attribute

kpi_unit instance-attribute

spend instance-attribute

currency instance-attribute

control_values instance-attribute

filled instance-attribute

hierarchy = field(default_factory=lambda: MappingProxyType({})) class-attribute instance-attribute

population = None class-attribute instance-attribute

national = field(default_factory=lambda: MappingProxyType({})) class-attribute instance-attribute

issues = () class-attribute instance-attribute

levers = field(default_factory=lambda: MappingProxyType({})) class-attribute instance-attribute

events = field(default_factory=lambda: MappingProxyType({})) class-attribute instance-attribute

counterfactual(lever)

The level lever's contribution is measured against: its Lever.baseline, or the least or the most of its values over every geo's periods that were not filled.

Raises:

Type Description
ValueError

on a name the panel does not declare a lever.

event_column(event)

event's column over the calendar, (period,): 1 in each period one of its days falls in, the last period labelled on or before the day, and 0 elsewhere.

Raises:

Type Description
ValueError

on a name that is not one of the panel's events.

regressors(names, geo, periods=None)

The values of the controls and events names in geo at the periods periods marks, every period by default, (periods, names): a control's as the panel holds them, an event's its column, the same in every geo.

Raises:

Type Description
ValueError

on a geo the panel does not have, or a name that is neither a control nor an event of the panel.

counterfactuals(controls)

Each of controls' counterfactual level, (controls,): a lever's counterfactual, and 0 for a control that is not a lever, whose contribution is measured against nothing.

Revenue() dataclass

The KPI is revenue in the panel's currency: a return on spend is a ratio of one currency.

Gap

Bases: Enum

What a period becomes when a geo has no row for it.

  • ERROR: reading fails, naming each geo and the periods it lacks.
  • STEP: the period stays as a step with no KPI observation, zero spend in every channel and NaN controls, and reading warns. The zero is a claim about the missing period, and adstock carries it into the periods after: choose STEP only where nothing was spent, or where that does not matter.

ERROR = 'error' class-attribute instance-attribute

STEP = 'step' class-attribute instance-attribute

RecurringEvents(names, weeks, posterior, separation) dataclass

The events recurring_events reads: each one's weeks of the year, a run of consecutive weeks from 1 to 52, named in names; each week of the year's posterior probability of the event class, (52,), the prior's where the panel holds no week of it; and separation, the one class's Bayesian information criterion less the two classes', above 0 where two read better and the events are read.

names instance-attribute

weeks instance-attribute

posterior instance-attribute

separation instance-attribute

columns(dates)

Each event's column over the weekly dates, (dates,): 1 in each week whose week of the year is one of the event's, and 0 elsewhere; a calendar's weeks or those past it.

days(dates)

Each event's days over the weekly dates, as MediaPanel.events holds them: the label of each week of the event.

aggregate(panel, nodes, controls=None)

The panel at nodes of its hierarchy, each node a geo of the result.

nodes must cut the hierarchy: every geo sits at or below exactly one of them. A node's KPI, spend and population are the sums of its geos'; its KPI is missing in a period any of them misses, and its step is filled where all of theirs are. Each control combines as controls says. Channels bought nationally stay so, split as before. The hierarchy keeps the nodes above the cut.

Raises:

Type Description
ValueError

on nodes that do not cut the hierarchy, a control with no rule or a rule for no control, or a population mean without a population.

allot_national(panel, channel, spend, by=Allotment.POPULATION)

The panel with channel added, bought nationally: each period's national spend split over the geos by their population.

Raises:

Type Description
ValueError

on a channel the panel has, national spend that is not one finite, non-negative value a period, a panel with no population, or national spend in a period some geo has no row for, which the panel holds at zero spend.

read_panel(data, *, date, kpi, spend, cadence, kpi_unit, currency, geo=None, controls=(), hierarchy=None, population=None, gaps=Gap.ERROR, levers=None, events=None)

Check one row per geo and period, then read it into a MediaPanel.

data is a mapping of column name to values, or a frame with .columns (pandas, polars). spend maps each channel's name to its spend column; the panel keeps that order. Geos are sorted. Without geo the panel has one geo, NATIONAL. hierarchy maps every geo, and every region above one, to its parent. Nodes with no geo of the data below them are dropped, with a warning. population maps every geo to its population, one number a geo, and a geo not in the data is dropped, with a warning; or it names a column that holds each row's geo's population, which must be the same on every row of a geo. levers declares some of controls levers, each with the level its contribution is measured against (Lever). events names calendar events, holidays among them, each by its days, which must fall inside the calendar read.

Channels are read by name: the order of the data's columns moves nothing, and the panel holds the channels in spend's order with each channel's own column. The dates must be spaced as cadence says: daily dates read at a weekly cadence are refused, not resampled, so data finer than the cadence is summed to it before reading. A geo with fewer than 15 observed periods per channel and control warns that its effects are barely identified.

Raises PanelError listing every error found. Warnings ride on the panel's issues and go to this module's logger.

recurring_events(panel, indicators, *, geo=NATIONAL, prefix='recurring')

The events that recur in panel's weeks of the year, read off its controls indicators, each with its direction, 1 where an event raises it and -1 where it lowers it; see the module. An event is named prefix and its weeks, as "recurring weeks 2-4".

Raises:

Type Description
ValueError

on a panel that is not weekly, a geo it does not have, no indicator, a name that is not one of its controls, a direction that is not 1 or -1, an indicator that does not vary over the weeks observed, or fewer observed weeks than a year's.