Loading...

verticapy.machine_learning.model_selection.statistical_tests.tsa.durbin_watson

verticapy.machine_learning.model_selection.statistical_tests.tsa.durbin_watson(input_relation: Annotated[str | vDataFrame, ''], 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 vDataColumn.

ts: str

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

by: SQLColumns, optional

vDataColumns used in the partition.

Returns

float

Durbin Watson statistic.

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 verticapy as vp

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]

# vDataFrame
vdf = vp.vDataFrame(
    {
        "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 verticapy.machine_learning.vertica.linear_model import LinearRegression

model = LinearRegression()

Next we can fit the model:

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


=======
details
=======
predictor|coefficient| std_err |t_value |p_value 
---------+-----------+---------+--------+--------
Intercept|-638.30828 |880.82131|-0.72467| 0.47217
   day   | 43.03305  |30.97760 | 1.38917| 0.17119


==============
regularization
==============
type| lambda 
----+--------
none| 1.00000


===========
call_string
===========
linear_reg('"public"."_verticapy_tmp_linearregression_v_mldb_a30bde9a979511efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_a34c2fd6979511efa8720242ac120002_"', '"y1"', '"day"'
USING PARAMETERS optimizer='newton', epsilon=1e-06, max_iterations=100, regularization='none', lambda=1, alpha=0.5, fit_intercept=true)

===============
Additional Info
===============
       Name       |Value
------------------+-----
 iteration_count  |  1  
rejected_row_count|  0  
accepted_row_count| 50  

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

model.predict(vdf, X = "day", name = "y_pred")
123
day
Integer
123
y1
Numeric(23)
123
y_pred
Float(22)
100.0-638.308276681683
211.4718141238459528-595.275229236085
329.563767147206292-552.242181790486
43-80.59173869539234-509.209134344888
54-59.011448637908856-466.176086899289
65-85.4664615740646-423.143039453691
76-98.16387107457986-380.109992008092
87128.19569314466747-337.076944562494
98-296.77209056013197-294.043897116895
109-240.93297393180575-251.010849671297
111024.24745121101933-207.977802225698
1211186.63950560486157-164.9447547801
1312-287.06175497856964-121.911707334501
1413-2004.4320601125016-78.8786598889025
15143.193073644676552-35.845612443304
1615-296.03289660886827.18743500229448
1716672.495528045630950.220482447893
1817897.270714838228193.2535298934915
19181032.9040446063786136.28657733909
2019-4.8059967885965875179.319624784688
2120-1279.0420313279094222.352672230287
2221-1111.8508309081021265.385719675886
23223983.0165848275396308.418767121484
2423-1315.7761168096056351.451814567083
2524-459.9123496010786394.484862012681
262572.0841069518651437.51790945828
2726-386.85885049056213480.550956903878
2827-2417.42276003794523.584004349477
2928-65.36336128519035566.617051795075
3029-6360.333842282093609.650099240674
3130-714.2076595577226652.683146686272
32312118.740010485263695.716194131871
3332-3685.4304092921357738.749241577469
34332274.592916047279781.782289023068
35341239.603942295978824.815336468666
36358113.194942855282867.848383914265
37368460.654677364753910.881431359863
38372439.636266448762953.914478805462
3938-5836.372157175366996.94752625106
4039668.48089679300481039.98057369666
41402016.00191025074291083.01362114226
42412800.83145337853331126.04666858786
4342-1703.53344993885931169.07971603345
4443387.266030724629841212.11276347905
45443430.7919466235751255.14581092465
4645-1441.426577777031298.17885837025
47464586.5017946987231341.21190581585
484712457.6993455746381384.24495326145
4948-4247.5612023736721427.27800070705
5049-2726.6462390933691470.31104815264
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 verticapy.machine_learning.model_selection.statistical_tests import durbin_watson

durbin_watson(input_relation = vdf, ts = "day", eps = "eps")
Out[12]: 1.79438889005769

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