Loading...

vastorbit.machine_learning.model_selection.statistical_tests.ols.het_white

vastorbit.machine_learning.model_selection.statistical_tests.ols.het_white(input_relation: Annotated[str | VastFrame, ''], eps: str, X: Annotated[str | list[str], 'STRING representing one column or a list of columns']) tuple[float, float, float, float]

White’s Lagrange Multiplier Test for Heteroscedasticity.

Parameters:
  • input_relation (SQLRelation) – Input relation.

  • eps (str) – Input residual VastColumn.

  • X (str) – Exogenous Variables to test the Heteroscedasticity on.

Returns:

Lagrange Multiplier statistic, LM pvalue, F statistic, F pvalue

Return type:

tuple

Examples

Initialization

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

  • x (a predictor)

  • y (the response)

  • Random noise

Note

This metric requires eps, which represents the difference between the predicted value and the true value. If you already have eps available, you can directly use it instead of recomputing it, as demonstrated in the example below.

Before we begin we can import the necessary libraries:

import vastorbit as vo
import numpy as np
from vastorbit.machine_learning.vast.linear_model import LinearRegression

Example 1: Homoscedasticity

Next, we can create some values with random noise:

y_vals = [0, 2, 4, 6, 8, 10] + np.random.normal(0, 0.4, 6)

We can use those values to create the VastFrame:

vdf = vo.VastFrame(
    {
        "x": [0, 1, 2, 3, 4, 5],
        "y": y_vals,
    }
)

We can initialize a regression model:

model = LinearRegression()

Fit that model on the dataset:

model.fit(input_relation = vdf, X = "x", y = "y")

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

model.predict(vdf, X = "x", name = "y_pred")

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

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

We can plot the residuals to see the trend:

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

Notice the randomness of the residuals with respect to x. This shows that the noise is homoscedestic.

To test its score, we can import the test function:

from vastorbit.machine_learning.model_selection.statistical_tests import het_white

And simply apply it on the VastFrame:

lm_statistic, lm_pvalue, f_statistic, f_pvalue = het_white(vdf, eps = "eps", X = "x")
print(lm_statistic, lm_pvalue, f_statistic, f_pvalue)

As the noise was not heteroscedestic, we got higher p_value scores and lower statistics score.

Note

A p_value in statistics represents the probability of obtaining results as extreme as, or more extreme than, the observed data, assuming the null hypothesis is true. A smaller p-value typically suggests stronger evidence against the null hypothesis i.e. the test data does not have a heteroscedestic noise in the current case.

However, small is a relative term. And the choice for the threshold value which determines a “small” should be made before analyzing the data.

Generally a p-value less than 0.05 is considered the threshold to reject the null hypothesis. But it is not always the case - read more

Note

F-statistics tests the overall significance of a model, while LM statistics tests the validity of linear restrictions on model parameters. High values indicate heterescedestic noise in this case.

Example 2: Heteroscedasticity

We can contrast the above result with a dataset that has heteroscedestic noise below:

# y values
y_vals = np.array([0, 2, 4, 6, 8, 10])

# Adding some heteroscedestic noise
y_vals = y_vals + [0.5, 0.3, 0.2, 0.1, 0.05, 0]
vdf = vo.VastFrame(
    {
        "x": [0, 1, 2, 3, 4, 5],
        "y": y_vals,
    }
)

We can intialize a regression model:

model = LinearRegression()

Fit that model on the dataset:

model.fit(input_relation = vdf, X = "x", y = "y")

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

model.predict(vdf, X = "x", name = "y_pred")

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

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

We can plot the residuals to see the trend:

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

Notice the relationship of the residuals with respect to x. This shows that the noise is heteroscedestic.

Now we can perform the test on this dataset:

lm_statistic, lm_pvalue, f_statistic, f_pvalue = het_white(vdf, eps = "eps", X = "x")
print(lm_statistic, lm_pvalue, f_statistic, f_pvalue)

Note

Notice the contrast of the two test results. In this dataset, the noise was heteroscedestic so we got very low p_value scores and higher statistics score. Thus confirming that the noise was in fact heteroscedestic.