π¬ analysis.acf_pacfΒΆ
Autocorrelation and partial autocorrelation are essential tools in time series analysis, helping to identify patterns, seasonality, and the appropriate lag structure for modeling.
The AcfPacfAnalyzer class provides an easy interface to compute the Autocorrelation Function (ACF)
and Partial Autocorrelation Function (PACF) for specified columns in a pandas DataFrame.
It leverages the statsmodels library for time series analysis and supports configurable lag and precision settings.
It is particularly useful in identifying lag relationships and temporal dependencies in MMM (Market Mix Modeling) datasets.
OverviewΒΆ
The module exposes a calculator class that:
Accepts a pandas DataFrame
Computes ACF and PACF values for selected columns
Supports configurable lag values
Returns structured output for downstream analysis or visualization
Class ReferenceΒΆ
- class owlmix.analysis.acf_pacf.AcfPacfParams(columns=None, n_lags=10, precision=4)ΒΆ
Dataclass for specifying ACF/PACF analysis parameters.
- Parameters:
columns (
Optional[list[str]]) β List of column names to include in the analysis. If None, all numeric columns are used.n_lags (
int) β Number of lag values to compute for ACF and PACF. Default is 10.precision (
int) β Number of decimal places to round ACF and PACF values. Default is 4.
- class owlmix.analysis.acf_pacf.AcfPacfAnalyzer(df, params)ΒΆ
Calculates the Autocorrelation Function (ACF) and Partial Autocorrelation Function (PACF) for specified columns in a pandas DataFrame.
- Parameters:
df (
pandas.DataFrame) β Input DataFrame containing time series data.params (
AcfPacfParams) β Configuration parameters for ACF/PACF analysis.
- generate()ΒΆ
Calculates ACF and PACF for each specified column.
- Returns:
A dictionary with a βdataβ key containing a list of results for each column.
- Return type:
dict[str, list[dict]]
- Each result dictionary contains:
column: Name of the column analyzed.n_obs: Number of non-null observations.lags: List of lag indices.acf: List of ACF values (rounded to specified precision).pacf: List of PACF values (rounded to specified precision).
- print_results_json(results=None, indent=2)ΒΆ
Prints the results in JSON format.
- Parameters:
results (
list[dict], optional) β The results to print. If None, uses the computed results.indent (
int) β Indentation level for pretty-printing the JSON.
- print_results(results=None, include_outliers=False)ΒΆ
Prints the results in a human-readable tabular format.
- Parameters:
results (
list[dict], optional) β The results to print. If None, uses the computed results.include_outliers (
bool) β Whether to include the list of outlier values in the table. Default isFalse.
Usage ExampleΒΆ
Below is a simple example of how to use the calculator:
import pandas as pd
from owlmix.utils.sample_data_generator import create_sample_data
from owlmix.analysis.acf_pacf import AcfPacfAnalyzer, AcfPacfParams
# Sample data
num_rows = 100
df = create_sample_data(n=100)
params = AcfPacfParams(
columns=["sales", "radio_spend", "digital_spend"],
n_lags=5,
precision=4
)
analyzer = AcfPacfAnalyzer(df, params)
result = analyzer.compute()
analyzer.print_results_json(result)
analyzer.print_results(result)
Result Example
Result Output - analyzer.print_results_json(result)
[
{
"column": "digital_spend",
"n_obs": 100,
"lags": [
0,
1,
2,
3,
4,
5
],
"acf": [
1.0,
0.079,
0.0208,
0.1941,
-0.1845,
0.0838
],
"pacf": [
1.0,
0.0798,
0.0149,
0.1986,
-0.2338,
0.1396
]
},
{
"column": "radio_spend",
"n_obs": 94,
"lags": [
0,
1,
2,
3,
4,
5
],
"acf": [
1.0,
-0.0166,
-0.0108,
0.1119,
-0.0763,
0.0992
],
"pacf": [
1.0,
-0.0167,
-0.0113,
0.1153,
-0.0771,
0.1075
]
},
{
"column": "sales",
"n_obs": 100,
"lags": [
0,
1,
2,
3,
4,
5
],
"acf": [
1.0,
-0.0274,
0.0339,
-0.1039,
0.0685,
0.0103
],
"pacf": [
1.0,
-0.0277,
0.0338,
-0.1054,
0.0657,
0.0209
]
}
]
Result Output - analyzer.print_results(result, include_outliers=False)
Column: digital_spend (n_obs=100)
Lag ACF PACF
----- ------- -------
0 1.0000 1.0000
1 0.0790 0.0798
2 0.0208 0.0149
3 0.1941 0.1986
4 -0.1845 -0.2338
5 0.0838 0.1396
Column: radio_spend (n_obs=94)
Lag ACF PACF
----- ------- -------
0 1.0000 1.0000
1 -0.0166 -0.0167
2 -0.0108 -0.0113
3 0.1119 0.1153
4 -0.0763 -0.0771
5 0.0992 0.1075
Column: sales (n_obs=100)
Lag ACF PACF
----- ------- -------
0 1.0000 1.0000
1 -0.0274 -0.0277
2 0.0339 0.0338
3 -0.1039 -0.1054
4 0.0685 0.0657
5 0.0103 0.0209
NotesΒΆ
Only numeric columns are processed; non-numeric columns are skipped.
Missing values are automatically dropped before computation.
The class inherits from
ColumnMixinfor flexible column selection.
DependenciesΒΆ
pandas
numpy
statsmodels