πŸ”¬ analysis.vifΒΆ

Variance Inflation Factor (VIF) is a key diagnostic tool in regression analysis, used to detect multicollinearity among explanatory variables. High VIF values indicate that a feature is highly collinear with other features, which can adversely affect model interpretability and stability.

The VIFAnalyzer class provides a convenient interface to compute VIF values for selected columns in a pandas DataFrame. It supports configurable precision and optional color-coding for visualization, making it easy to identify problematic features.

OverviewΒΆ

The module exposes:

  • A parameter dataclass for flexible configuration

  • An analyzer class that:

    • Accepts a pandas DataFrame

    • Computes VIF values for selected features (excluding the target column)

    • Supports configurable precision and color thresholds

    • Returns structured output for downstream analysis or visualization

Class ReferenceΒΆ

class owlmix.analysis.vif.VIFParamsΒΆ

Dataclass for specifying VIF analysis parameters.

Parameters:
  • target_column (str) – The name of the target column to exclude from VIF calculation.

  • features (Optional[List[str]]) – List of feature column names to include in the analysis. If None, all numeric columns are used.

  • precision (int) – Number of decimal places to round VIF values.

  • color_thresholds (Optional[List[Tuple[float, str]]]) – List of (threshold, color) tuples for color-coding VIF values. If None, no color-coding is applied.

class owlmix.analysis.vif.VIFAnalyzer(df, params)ΒΆ

Calculates the Variance Inflation Factor (VIF) for specified features in a pandas DataFrame.

Parameters:
  • df (pandas.DataFrame) – Input DataFrame containing the data.

  • params (VIFParams) – Configuration parameters for VIF analysis.

compute()ΒΆ

Calculates VIF values for each specified feature.

Returns:

A dictionary with keys: - feature: List of feature names analyzed. - vif: List of VIF values (rounded to specified precision). - color: List of color codes for each VIF value (if color_thresholds provided).

Return type:

dict[str, list]

add_colors(vif_values)ΒΆ

Assigns colors to VIF values based on the defined color thresholds.

Parameters:

vif_values (List[float]) – List of VIF values.

Returns:

List of color strings corresponding to each VIF value.

Return type:

List[str]

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)ΒΆ

Prints the results in a human-readable tabular format.

Parameters:

results (list[dict], optional) – The results to print. If None, uses the computed results.

Usage ExampleΒΆ

Below is a simple example of how to use the analyzer:

import pandas as pd
from owlmix.analysis.vif import VIFAnalyzer, VIFParams

df = pd.DataFrame({
    "y": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    "x1": [2, 3, 2, 5, 7, 8, 6, 5, 4, 3],
    "x2": [5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
    "x3": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
})

vif_params = VIFParams(
    target_column="y",
    features=["x1", "x2", "x3"],
    precision=2,
    color_thresholds=[(5, "orange"), (10, "red")]
)
analyzer = VIFAnalyzer(df=df, params=vif_params)
result = analyzer.compute()
print(result)

Result Example

{
    "feature": ["x1", "x2", "x3"],
    "vif": [1.23, 8.45, 4.56],
    "color": ["orange", "red", "orange"]
}

NotesΒΆ

  • The target column is always excluded from VIF calculation.

  • If fewer than two features are provided, VIF is not defined and NaN is returned.

  • Only numeric columns are processed; non-numeric columns are ignored.

  • Missing values are automatically dropped before computation.

  • Color-coding is optional and controlled via the color_thresholds parameter.

DependenciesΒΆ

  • pandas

  • numpy

  • statsmodels

ReferencesΒΆ

See AlsoΒΆ

Back to Home