validmind 2.2.5__py3-none-any.whl → 2.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- validmind/__version__.py +1 -1
- validmind/{ai.py → ai/test_descriptions.py} +127 -69
- validmind/ai/utils.py +104 -0
- validmind/api_client.py +70 -31
- validmind/client.py +5 -5
- validmind/logging.py +38 -32
- validmind/models/foundation.py +10 -6
- validmind/models/function.py +3 -1
- validmind/models/metadata.py +1 -1
- validmind/test_suites/__init__.py +1 -7
- validmind/test_suites/regression.py +0 -16
- validmind/test_suites/statsmodels_timeseries.py +1 -1
- validmind/tests/data_validation/ACFandPACFPlot.py +36 -27
- validmind/tests/{model_validation/statsmodels → data_validation}/ADF.py +42 -13
- validmind/tests/data_validation/BivariateScatterPlots.py +38 -41
- validmind/tests/{model_validation/statsmodels → data_validation}/DFGLSArch.py +67 -11
- validmind/tests/data_validation/HeatmapFeatureCorrelations.py +1 -1
- validmind/tests/data_validation/HighPearsonCorrelation.py +12 -3
- validmind/tests/data_validation/IsolationForestOutliers.py +2 -2
- validmind/tests/{model_validation/statsmodels → data_validation}/KPSS.py +64 -11
- validmind/tests/{model_validation/statsmodels → data_validation}/PhillipsPerronArch.py +65 -11
- validmind/tests/data_validation/ScatterPlot.py +1 -1
- validmind/tests/data_validation/SeasonalDecompose.py +12 -7
- validmind/tests/data_validation/TabularDateTimeHistograms.py +29 -33
- validmind/tests/data_validation/WOEBinPlots.py +1 -1
- validmind/tests/data_validation/WOEBinTable.py +1 -1
- validmind/tests/{model_validation/statsmodels → data_validation}/ZivotAndrewsArch.py +65 -11
- validmind/tests/data_validation/nlp/CommonWords.py +1 -1
- validmind/tests/data_validation/nlp/Hashtags.py +1 -1
- validmind/tests/data_validation/nlp/Mentions.py +1 -1
- validmind/tests/data_validation/nlp/PolarityAndSubjectivity.py +2 -1
- validmind/tests/data_validation/nlp/Punctuations.py +1 -1
- validmind/tests/data_validation/nlp/Sentiment.py +1 -1
- validmind/tests/data_validation/nlp/TextDescription.py +5 -1
- validmind/tests/data_validation/nlp/Toxicity.py +1 -1
- validmind/tests/decorator.py +1 -1
- validmind/tests/model_validation/FeaturesAUC.py +5 -3
- validmind/tests/model_validation/embeddings/CosineSimilarityComparison.py +4 -0
- validmind/tests/model_validation/embeddings/CosineSimilarityHeatmap.py +4 -0
- validmind/tests/model_validation/embeddings/EuclideanDistanceComparison.py +4 -0
- validmind/tests/model_validation/embeddings/EuclideanDistanceHeatmap.py +4 -0
- validmind/tests/model_validation/embeddings/PCAComponentsPairwisePlots.py +4 -0
- validmind/tests/model_validation/embeddings/TSNEComponentsPairwisePlots.py +4 -0
- validmind/tests/model_validation/ragas/AnswerCorrectness.py +3 -3
- validmind/tests/model_validation/ragas/AnswerRelevance.py +5 -4
- validmind/tests/model_validation/ragas/AnswerSimilarity.py +5 -4
- validmind/tests/model_validation/ragas/AspectCritique.py +14 -8
- validmind/tests/model_validation/ragas/ContextEntityRecall.py +3 -4
- validmind/tests/model_validation/ragas/ContextPrecision.py +4 -5
- validmind/tests/model_validation/ragas/ContextRecall.py +3 -4
- validmind/tests/model_validation/ragas/ContextRelevancy.py +5 -4
- validmind/tests/model_validation/ragas/Faithfulness.py +6 -5
- validmind/tests/model_validation/ragas/utils.py +35 -9
- validmind/tests/model_validation/sklearn/ClusterPerformance.py +2 -2
- validmind/tests/model_validation/sklearn/ClusterPerformanceMetrics.py +1 -1
- validmind/tests/model_validation/sklearn/ModelsPerformanceComparison.py +6 -8
- validmind/tests/model_validation/sklearn/RegressionErrors.py +1 -1
- validmind/tests/model_validation/sklearn/RegressionModelsPerformanceComparison.py +14 -8
- validmind/tests/model_validation/sklearn/RegressionR2Square.py +1 -1
- validmind/tests/model_validation/statsmodels/DurbinWatsonTest.py +1 -1
- validmind/tests/model_validation/statsmodels/GINITable.py +1 -1
- validmind/tests/model_validation/statsmodels/JarqueBera.py +1 -1
- validmind/tests/model_validation/statsmodels/KolmogorovSmirnov.py +1 -1
- validmind/tests/model_validation/statsmodels/LJungBox.py +1 -1
- validmind/tests/model_validation/statsmodels/Lilliefors.py +1 -1
- validmind/tests/model_validation/statsmodels/RegressionCoeffsPlot.py +4 -0
- validmind/tests/model_validation/statsmodels/RegressionFeatureSignificance.py +9 -4
- validmind/tests/model_validation/statsmodels/RegressionModelsCoeffs.py +2 -2
- validmind/tests/model_validation/statsmodels/RunsTest.py +1 -1
- validmind/tests/model_validation/statsmodels/ShapiroWilk.py +1 -1
- validmind/tests/prompt_validation/Bias.py +14 -11
- validmind/tests/prompt_validation/Clarity.py +14 -11
- validmind/tests/prompt_validation/Conciseness.py +14 -11
- validmind/tests/prompt_validation/Delimitation.py +14 -11
- validmind/tests/prompt_validation/NegativeInstruction.py +14 -11
- validmind/tests/prompt_validation/Robustness.py +11 -11
- validmind/tests/prompt_validation/Specificity.py +14 -11
- validmind/tests/prompt_validation/ai_powered_test.py +53 -75
- validmind/unit_metrics/composite.py +2 -1
- validmind/utils.py +4 -49
- validmind/vm_models/dataset/dataset.py +17 -3
- validmind/vm_models/dataset/utils.py +2 -2
- validmind/vm_models/model.py +1 -1
- validmind/vm_models/test/metric.py +1 -8
- validmind/vm_models/test/result_wrapper.py +27 -34
- validmind/vm_models/test/test.py +3 -0
- validmind/vm_models/test/threshold_test.py +1 -1
- validmind/vm_models/test_suite/runner.py +12 -6
- validmind/vm_models/test_suite/summary.py +18 -7
- validmind/vm_models/test_suite/test.py +13 -20
- {validmind-2.2.5.dist-info → validmind-2.3.1.dist-info}/METADATA +1 -1
- {validmind-2.2.5.dist-info → validmind-2.3.1.dist-info}/RECORD +95 -104
- validmind/tests/data_validation/DefaultRatesbyRiskBandPlot.py +0 -114
- validmind/tests/data_validation/PiTCreditScoresHistogram.py +0 -150
- validmind/tests/data_validation/PiTPDHistogram.py +0 -152
- validmind/tests/model_validation/statsmodels/ADFTest.py +0 -88
- validmind/tests/model_validation/statsmodels/FeatureImportanceAndSignificance.py +0 -198
- validmind/tests/model_validation/statsmodels/PDRatingClassPlot.py +0 -151
- validmind/tests/model_validation/statsmodels/RegressionModelInsampleComparison.py +0 -146
- validmind/tests/model_validation/statsmodels/RegressionModelOutsampleComparison.py +0 -144
- validmind/tests/model_validation/statsmodels/RegressionModelsPerformance.py +0 -127
- validmind/tests/model_validation/statsmodels/ResidualsVisualInspection.py +0 -130
- {validmind-2.2.5.dist-info → validmind-2.3.1.dist-info}/LICENSE +0 -0
- {validmind-2.2.5.dist-info → validmind-2.3.1.dist-info}/WHEEL +0 -0
- {validmind-2.2.5.dist-info → validmind-2.3.1.dist-info}/entry_points.txt +0 -0
@@ -1,130 +0,0 @@
|
|
1
|
-
# Copyright © 2023-2024 ValidMind Inc. All rights reserved.
|
2
|
-
# See the LICENSE file in the root of this repository for details.
|
3
|
-
# SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial
|
4
|
-
|
5
|
-
from dataclasses import dataclass
|
6
|
-
|
7
|
-
import matplotlib.pyplot as plt
|
8
|
-
import pandas as pd
|
9
|
-
import seaborn as sns
|
10
|
-
from scipy import stats
|
11
|
-
from statsmodels.graphics.tsaplots import plot_acf
|
12
|
-
from statsmodels.tsa.seasonal import seasonal_decompose
|
13
|
-
|
14
|
-
from validmind.vm_models import Figure, Metric
|
15
|
-
|
16
|
-
|
17
|
-
@dataclass
|
18
|
-
class ResidualsVisualInspection(Metric):
|
19
|
-
"""
|
20
|
-
Provides a comprehensive visual analysis of residuals for regression models utilizing various plot types.
|
21
|
-
|
22
|
-
**Purpose**: The main purpose of this metric is to visualize and analyze the residuals (the differences between the
|
23
|
-
observed and predicted values) of a regression problem. It allows for a graphical exploration of the model's
|
24
|
-
errors, helping to identify statistical patterns or anomalies that may indicate a systematic bias in the model's
|
25
|
-
predictions. By inspecting the residuals, we can check how well the model fits the data and meets the assumptions
|
26
|
-
of the model.
|
27
|
-
|
28
|
-
**Test Mechanism**: The metric generates four common types of residual plots which are: a histogram with kernel
|
29
|
-
density estimation, a quantile-quantile (Q-Q) plot, a residuals series dot plot, and an autocorrelation function
|
30
|
-
(ACF) plot.
|
31
|
-
|
32
|
-
- The residuals histogram with kernel density estimation visualizes the distribution of residuals and allows to
|
33
|
-
check if they are normally distributed.
|
34
|
-
- Q-Q plot compares the observed quantiles of the data to the quantiles of a standard normal distribution, helping
|
35
|
-
to assess the normality of residuals.
|
36
|
-
- A residuals dot plot indicates the variation in residuals over time, which helps in identifying any time-related
|
37
|
-
pattern in residuals.
|
38
|
-
- ACF plot visualizes the correlation of an observation with its previous observations, helping to pinpoint any
|
39
|
-
seasonality effect within residuals.
|
40
|
-
|
41
|
-
**Signs of High Risk**:
|
42
|
-
|
43
|
-
- Skewness or asymmetry in the histogram or a significant deviation from the straight line in the Q-Q plot, which
|
44
|
-
indicates that the residuals aren't normally distributed.
|
45
|
-
- Large spikes in the ACF plot, indicating that the residuals are correlated, in violation of the assumption that
|
46
|
-
they are independent.
|
47
|
-
- Non-random patterns in the dot plot of residuals, indicating potential model misspecification.
|
48
|
-
|
49
|
-
**Strengths**:
|
50
|
-
|
51
|
-
- Visual analysis of residuals is a powerful yet simple way to understand a model’s behavior across the data set
|
52
|
-
and to identify problems with the model's assumptions or its fit to the data.
|
53
|
-
- The test is applicable to any regression model, irrespective of complexity.
|
54
|
-
- By exploring residuals, we might uncover relationships that were not captured by the model, revealing
|
55
|
-
opportunities for model improvement.
|
56
|
-
|
57
|
-
**Limitations**:
|
58
|
-
|
59
|
-
- Visual tests are largely subjective and can be open to interpretation. Clear-cut decisions about the model based
|
60
|
-
solely on these plots may not be possible.
|
61
|
-
- The metrics from the test do not directly infer the action based on the results; domain-specific knowledge and
|
62
|
-
expert judgement is often required to interpret the results.
|
63
|
-
- These plots can indicate a problem with the model but they do not necessarily reveal the nature or cause of the
|
64
|
-
problem.
|
65
|
-
- The test assumes that the error terms are identically distributed, which might not always be the case in
|
66
|
-
real-world scenarios.
|
67
|
-
"""
|
68
|
-
|
69
|
-
name = "residuals_visual_inspection"
|
70
|
-
required_inputs = ["dataset"]
|
71
|
-
metadata = {
|
72
|
-
"task_types": ["regression"],
|
73
|
-
"tags": ["statsmodels", "visualization"],
|
74
|
-
}
|
75
|
-
|
76
|
-
@staticmethod
|
77
|
-
def residual_analysis(residuals, variable_name, axes):
|
78
|
-
residuals = residuals.dropna().reset_index(
|
79
|
-
drop=True
|
80
|
-
) # drop NaN values and reset index
|
81
|
-
|
82
|
-
# QQ plot
|
83
|
-
stats.probplot(residuals, dist="norm", plot=axes[0, 1])
|
84
|
-
axes[0, 1].set_title(f"Residuals Q-Q Plot ({variable_name})")
|
85
|
-
|
86
|
-
# Histogram with KDE
|
87
|
-
sns.histplot(residuals, kde=True, ax=axes[0, 0])
|
88
|
-
axes[0, 0].set_xlabel("Residuals")
|
89
|
-
axes[0, 0].set_title(f"Residuals Histogram ({variable_name})")
|
90
|
-
|
91
|
-
# Residual series dot plot
|
92
|
-
sns.lineplot(data=residuals, linewidth=0.5, color="red", ax=axes[1, 0])
|
93
|
-
axes[1, 0].set_title(f"Residual Series Dot Plot ({variable_name})")
|
94
|
-
|
95
|
-
# ACF plot
|
96
|
-
n_lags = min(100, len(residuals) - 1) # Adjust the number of lags
|
97
|
-
plot_acf(residuals, ax=axes[1, 1], lags=n_lags, zero=False) # Added zero=False
|
98
|
-
axes[1, 1].set_title(f"ACF Plot of Residuals ({variable_name})")
|
99
|
-
|
100
|
-
def run(self):
|
101
|
-
x_train = self.inputs.dataset.df
|
102
|
-
figures = []
|
103
|
-
|
104
|
-
# TODO: specify which columns to plot via params
|
105
|
-
for col in x_train.columns:
|
106
|
-
sd = seasonal_decompose(x_train[col], model="additive")
|
107
|
-
|
108
|
-
# Remove NaN values from the residuals and reset the index
|
109
|
-
residuals = pd.Series(sd.resid).dropna().reset_index(drop=True)
|
110
|
-
|
111
|
-
# Create subplots
|
112
|
-
fig, axes = plt.subplots(nrows=2, ncols=2)
|
113
|
-
fig.suptitle(f"Residuals Inspection for {col}", fontsize=24)
|
114
|
-
|
115
|
-
self.residual_analysis(residuals, col, axes)
|
116
|
-
|
117
|
-
# Adjust the layout
|
118
|
-
plt.tight_layout()
|
119
|
-
|
120
|
-
# Do this if you want to prevent the figure from being displayed
|
121
|
-
plt.close("all")
|
122
|
-
|
123
|
-
figures.append(
|
124
|
-
Figure(
|
125
|
-
for_object=self,
|
126
|
-
key=self.key,
|
127
|
-
figure=fig,
|
128
|
-
)
|
129
|
-
)
|
130
|
-
return self.cache_results(figures=figures)
|
File without changes
|
File without changes
|
File without changes
|