Loading...

vastorbit.machine_learning.model_selection.statistical_tests.tsa.durbin_watson

vastorbit.machine_learning.model_selection.statistical_tests.tsa.durbin_watson(input_relation: Annotated[str | VastFrame, ''], eps: str, ts: str, by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None) float

Durbin Watson test (residuals autocorrelation).

Parameters:
  • input_relation (SQLRelation) – Input relation.

  • eps (str) – Input residual VastColumn.

  • 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.

Returns:

Durbin Watson statistic.

Return type:

float

Examples

Initialization

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

  • A value of interest that has noise related to time

  • Time-stamp data

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
days = list(range(N))
y_val = [2 * x + np.random.normal(scale = 4 * x * x) for x in days]

# VastFrame
vdf = vo.VastFrame(
    {
        "day": days,
        "y1": y_val,
    }
)

Model Fitting

Next, we can fit a Linear Model. To do that we need to first import the model and intialize:

from vastorbit.machine_learning.vast.linear_model import LinearRegression

model = LinearRegression()

Next we can fit the model:

model.fit(vdf, X = "day", y = "y1")

We can create a column in the VastFrame that has the predictions:

model.predict(vdf, X = "day", name = "y_pred")
123
day
Integer
123
y1
Decimal(20, 15)
123
y_pred
Decimal(29, 16)
114-1786.349640175681824.95078885364371
232-3392.6819214570687-11.621391402404718
317-422.535866355896418.855425477635638
426-3010.36939307836160.5693353496114244
545-18647.41757952407-38.034632698439694
622324.415240710511458.696486517622187
78213.4522939910061737.141515605659855
835-7585.859000880063-17.716754778412792
936-1444.9615939848813-19.74854257041548
10421108.3970083845666-31.939269322431624
11273706.6768336821206-1.4624524423912661
12333512.2281729342913-13.653179194407409
13434264.268936826552-33.971057114434316
1429-1257.1959336519483-5.526028026396648
157242.165710681357539.17330339766254
16417505.971897571791-29.907481530428935
1744-1454.8836658618152-36.002844906437005
184616906.09270757043-40.06642049044239
199-180.4904240976054335.109727813657166
2019-860.07716128788614.791849893630259
2128-879.3176730588347-3.4942402343939567
2225652.61357855109172.601123141614115
236-34.22793236092777441.20509118966524
24391740.5175715195064-25.843905946423554
25492653.6882423137154-46.161783866450456
26231640.1447748430366.6646987256194965
27305556.850333818485-7.557815818399338
2811288.059092748946531.046152229651785
2924-365.39768500313374.632910933616806
3047-6963.54718764456-42.09820828244508
31220.1963804768063449.332242357676
3234-2182.7779910863865-15.6849669864101
3348-5370.178926362465-44.12999607444777
3418-1677.530959373948216.82363768563295
35385251.8320231696935-23.81211815442086
36312255.7910254483477-9.589603610402028
3720168.9638112053198412.760062101627568
38372545.763654427294-21.780330362418173
3940-4338.557601209795-27.875693738426243
40212290.39238791469210.728274309624878
415-82.4264053148709943.23687898166793
42161633.59104052069320.88721326963833
4316.31386837452741251.36403014967869
4415-719.392999987194722.919001061641023
453-16.44917102265960647.300454565673306
4613-1259.340050631432226.982576645646404
474-103.685305399859145.26866677367062
4810-130.4441985863413733.07794002165448
4912-141.4394684063190429.014364437649093
5000.053.39581794168138
Rows: 1-50 | Columns: 3

Then we can calculate the residuals i.e. eps:

vdf["eps"] = vdf["y1"] - vdf["y_pred"]

We can plot the residuals to see the trend:

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

Test

Now we can apply the Durbin Watson Test:

from vastorbit.machine_learning.model_selection.statistical_tests import durbin_watson

durbin_watson(input_relation = vdf, ts = "day", eps = "eps")

We can see that the Durbin-Watson statistic is not equal to 2. This shows the presence of autocorrelation.

Note

The Durbin-Watson statistic values can be interpretted as such:

Approximately 2: No significant autocorrelation.

Less than 2: Positive autocorrelation (residuals are correlated positively with their lagged values).

Greater than 2: Negative autocorrelation (residuals are correlated negatively with their lagged values).