Loading...

vastorbit.machine_learning.model_selection.statistical_tests.tsa.ljungbox

vastorbit.machine_learning.model_selection.statistical_tests.tsa.ljungbox(input_relation: Annotated[str | VastFrame, ''], column: str, ts: str, by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, p: int = 1, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 0.05, box_pierce: bool = False) TableSample

Ljung–Box test (whether any of a group of autocorrelations of a time series are different from zero).

Parameters:
  • input_relation (SQLRelation) – Input relation.

  • column (str) – Input VastColumn to test.

  • ts (str) – VastColumn used as timeline to order the data. It can be a numerical or date-like type (date, datetime, timestamp…) VastColumn.

  • by (SQLColumns, optional) – VastColumns used in the partition.

  • p (int, optional) – Number of lags to consider in the test.

  • alpha (PythonNumber, optional) – Significance Level. Probability to accept H0.

  • box_pierce (bool) – If set to True, the Box-Pierce statistic is used.

Returns:

result of the test.

Return type:

TableSample

Examples

Initialization

Let’s try this test on a dummy dataset that has the following elements:

  • Time-stamp data

  • Some columns related to time

  • Some columns independent of time

Before we begin we can import the necessary libraries:

import vastorbit as vo
import numpy as np

Data

Now we can create the dummy dataset:

# Initialization
N = 50 # Number of Rows.
day = list(range(N))
x_val_1 = [2 * x + np.random.normal(scale = 4) for x in day]
x_val_2 = np.random.normal(0, 4, N)

# VastFrame
vdf = vo.VastFrame(
    {
        "day": day,
        "x1": x_val_1,
        "x2": x_val_2,
    }
)

Note that in the above dataset we have create two columns x1 and x2.

  • x1:

    It is related to day

  • x2:

    It is independent of day

Data Visualization

We can visualize ther relationship with the help of a scatter plot:

vdf.scatter(["day", "x1"])

We can see that the variable x1 seems to be correalted with time. Now let us check the other variable x2.

vdf.scatter(["day", "x2"])

Above we observe that there is no apparent correlation with time.

Test

Now we can apply the Ljung-Box test Test:

from vastorbit.machine_learning.model_selection.statistical_tests import ljungbox
ljungbox(vdf, "x1", ts = "day")

The test confirms that there is indeed a relationship.

Now, we can test the other independent column as well:

from vastorbit.machine_learning.model_selection.statistical_tests import ljungbox
ljungbox(vdf, "x2", ts = "day")

We can confirm that x2 is indeed independent of time. The results are consistent with our earlier visual observation.