Tests before fitting#

Stationarity#

check_stationarity(data, *, signif=0.05, regression='c')[source]#

Classify each column as I(0), I(1) or I(2) with the ADF test.

Parameters:
data

Time series, shape (T, n). A pandas.DataFrame or a 2-D array. Column names are used when available.

signif

Significance level. A p-value below this rejects the unit root.

regression

Deterministic terms in the test on the levels:

  • "c": constant (default).

  • "ct": constant and linear trend. Use this when a series could be stationary around a straight line.

  • "n": none.

The test on the first difference drops the trend, so "ct" becomes "c" there.

Returns:
StationarityResult
Raises:
ValueError

If data fails validation, a column is constant, signif is not strictly between 0 and 1, or regression is not recognised.

class StationarityResult(table, signif, regression)[source]#

Result of check_stationarity().

Attributes:
tablepandas.DataFrame

One row per column of the input, with columns:

  • variable: column name.

  • p_level: ADF p-value for the levels.

  • p_diff: ADF p-value for the first difference.

  • order: "I(0)", "I(1)" or "I(2)". "I(2)" means two or more.

  • role: "exog", "endog" or "difference first".

signiffloat

Significance level used for both tests.

regressionstr

Deterministic terms in the levels test.

property endog#

Names of the I(1) columns.

property exog#

Names of the I(0) columns.

Granger pretest#

granger_pretest(endog, exog=None, *, lags=None, max_lags=8, extra_lags=1, seasonal=None, alpha=0.05, contemporaneous=None, excluded=None)[source]#

Test which series help predict which, before choosing the rank.

Parameters:
endog

Brand metrics in levels (logs), shape (T, K). A DataFrame gives the variable names.

exog

Drivers in levels, shape (T, m). They enter the VAR as equals of the brand metrics so that feedback can be tested. None tests the brand metrics only.

lags

Lags to test. None picks them with the Hannan-Quinn criterion on a levels VAR, at least 1.

max_lags

Largest lag order the criterion considers. It is capped so that the test regression keeps at least half its rows as residual degrees of freedom. Near that limit the criterion picks long lags by chance.

extra_lags

Lags added and not tested. 1 for series that are at most I(1).

seasonal

Period for centred seasonal dummies, e.g. 12 for monthly data. None for no dummies.

alpha

Significance level used in the printed verdicts.

contemporaneous

Same-week links to test, as {effect: [causes]} with brand metric names, for example {"brand_consideration": ["brand_awareness"]}. None skips the same_week table.

excluded

Drivers with no direct same-week effect on a brand metric, as {effect: [drivers]}. They are the instruments for that effect’s links. An effect needs at least as many as it has causes.

Returns:
GrangerPretestResult
Raises:
ValueError

If the inputs fail validation, contemporaneous or excluded name an unknown series, or the VAR has too few degrees of freedom.

class GrangerPretestResult(tests, delay, same_period, lags, extra_lags, n_obs, alpha, seasonal, same_week=None)[source]#

Toda-Yamamoto Granger tests and same-period correlations.

Printing the result gives the tests grouped into drivers, links and feedback. Each attribute below is a pandas.DataFrame for further work.

Attributes:
testspandas.DataFrame

One row per ordered pair. Columns cause, effect, group, f_stat, df_num, df_den and p_value. group is one of "driver -> metric", "metric -> metric", "metric -> driver" or "driver -> driver".

delaypandas.DataFrame or None

Same columns as tests for each driver and brand metric pair, with group set to "delay". The metric’s equation also holds the current values of every driver. None without exog.

same_periodpandas.DataFrame

One row per unordered pair. Columns variable_1, variable_2, group, corr and p_value. The correlation is between the two VAR residuals.

lagsint

Lags tested. Chosen by the Hannan-Quinn criterion unless given.

extra_lagsint

Lags added and left untested.

n_obsint

Observations used in the regression after lags are dropped.

alphafloat

Tests with p_value below this are reported as links.

seasonalint or None

Period of the seasonal dummies, if any.

same_weekpandas.DataFrame or None

One row per same-week link in contemporaneous. Columns cause, effect, ols, estimate (2SLS), std_err, p_value, first_stage_f, n_instruments, sargan, sargan_p and verdict. The verdict is one of "same-week link", "no clear evidence", "weak instruments" (first-stage F below 10), "instruments disagree" (Sargan rejects) or "not identified" (fewer excluded drivers than causes). None without contemporaneous.

Cointegration rank#

select_coint_rank(endog, *, k_ar_diff=1, det_order=0)[source]#

Select the cointegration rank via the Johansen trace test.

Wraps statsmodels.tsa.vector_ar.vecm.select_coint_rank() and returns a CointRankResult with a readable summary table and the recommended rank.

Parameters:
endog

Endogenous time-series data. Accepts the same inputs as validate_endog(): a 2-D numpy array or any array-like with a .to_numpy() method (e.g. a pandas.DataFrame). Shape (T, K) — rows are time periods, columns are variables. At least two variables required.

k_ar_diff

Number of lagged-difference blocks in the VECM (same as the k_ar_diff you intend to pass to BayesianVECM). Passed as the k_ar_diffs argument to statsmodels.

det_order

Deterministic-term order for the Johansen test: -1 — no constant/trend, 0 — constant outside cointegrating relation (default), 1 — constant inside + trend outside. This controls the test’s critical values; it does not need to match the deterministic code you use for fitting (though it should be in the same ballpark).

Returns:
CointRankResult

Contains the recommended rank, test statistics, critical values, and a printable summary table.

Raises:
ValueError

If endog fails validation or det_order is not in {-1, 0, 1}.

class CointRankResult(rank, test_stats, crit_vals, variable_names, det_order, k_ar_diff)[source]#

Result of a Johansen trace-test rank-selection procedure.

Attributes:
rankint

Recommended cointegration rank — the smallest \(r\) for which the trace statistic falls below the 5% critical value. If the statistic exceeds the critical value at all \(r < K\), the procedure recommends the full rank K (all series stationary in levels — unlikely in practice for a cointegrated system).

test_statsnp.ndarray, shape (K,)

Trace test statistics for each null hypothesis \(H_0: r \leq 0, 1, \dots, K-1\).

crit_valsnp.ndarray, shape (K,)

Corresponding 5% critical values.

variable_nameslist[str] | None

Column names from the input, if available.

det_orderint

Deterministic-term order passed to the Johansen test (-1, 0, or 1).

k_ar_diffint

Number of lagged-difference blocks used in the test.