Skip to content

Feature for ML - #4

Open
faux66 wants to merge 19 commits into
mainfrom
feat/mlstrategy
Open

Feature for ML#4
faux66 wants to merge 19 commits into
mainfrom
feat/mlstrategy

Conversation

@faux66

@faux66 faux66 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

1. Mathematical Foundation Expansion

  • Implemented 8 continuous probability distributions: Normal, Exponential, Weibull, Gamma, LogNormal, Student's t, Beta, and Uniform

2. Update GenericCriterion

  • Implemented Dynamic Parameter Injection: The adapter now automatically maps distribution fitting results from scipy.stats (e.g., shape, mu) to specific engine attributes required by pysatl-criterion (e.g., a, loc, df) using a comprehensive alias map.
  • Introduced Signature Introspection: Using the inspect module, the system now determines if a statistic requires cdf_vals. This allows for Lazy Evaluation, calculating the theoretical CDF only when explicitly requested by the test engine

3. Automated Discovery (DynamicCriterionSelector)

  • Developed selection mechanism that dynamically scans the pysatl-criterion library at runtime. This eliminates manual class mapping.

4. Feature for ML (FeatureVector)

  • Developed selection mechanism that dynamically scans the pysatl-criterion library at runtime. This eliminates manual class mapping and allows the system to automatically support new statistical tests as they are added to the external library.
  • Implemented Missing Value Encoding: If a test is mathematically inapplicable (e.g., negative values for Exponential) or fails, it is assigned a value of -1.0.

@f1i3g3 f1i3g3 added the enhancement New feature or request label Apr 14, 2026
@f1i3g3
f1i3g3 self-requested a review April 14, 2026 21:19

@f1i3g3 f1i3g3 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Выглядит круто

@faux66

faux66 commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Overview

This Pull Request introduces the core Machine Learning Expert System for pysatl-expert. It replaces the legacy empirical heuristics with a data-driven 2-stage Hierarchical Random Forest Classifier (HierarchicalExpertModel) powered by a high-performance SQLite Critical Value Cache (CVCache and MLStrategy).

The system automatically identifies empirical sample distributions across 8 candidate laws (Normal, Exponential, Weibull, Uniform, Student, Gamma, Beta, LogNormal) with over 95% accuracy, supporting sample sizes N between 30 and 1000 and location shifts (loc != 0).


Architectural Breakdown & Core Components

1. 2-Stage Hierarchical Classifier (HierarchicalExpertModel)

  • Stage 1 (Family Model): Predicts distribution families (Family 1, Family 2, Family 3) obtained via Ward hierarchical clustering.
  • Stage 2 (Sub-Family Models): Dedicated Random Forest classifiers predict the exact probability distribution within the identified family.
  • Feature Selection: Automatically selects top 30 Stage 1 features and top 20 Stage 2 intra-family features during model fitting to optimize accuracy and execution speed.

2. Fast Critical Value Cache and ML Strategy (CVCache, MLStrategy)

  • Microsecond Critical Value Lookup: CVCache preloads compressed header and zstd-compressed float32 blobs from expert_cv_database.sqlite and performs O(1) linear interpolation (np.interp) over sample size N.
  • Feature Binarization: Binarizes raw Goodness-of-Fit (GoF) test values (1.0 if statistic <= CV_95%, else 0.0) to form robust decision boundaries.

3. Feature Space & Dynamic Criteria Selector (FeatureVector, DynamicCriterionSelector)

  • Fixed 192-Dimensional Feature Vector: Aggregates 8 descriptive sample statistics (min, max, sample_size, skew, kurtosis, coef_of_variation, relative_iqr, entropy) and 184 binarized GoF test scores.
  • Single Canonical Blacklist: Consolidates computationally heavy or missing database criteria (bhs, kl_int, kl_sup, cq*, rs, ahs, hp, graph metrics) into FeatureVector.BLACKLIST to guarantee complete schema synchronization across criteria selectors and feature extractors.

4. Facade API and Visual Report Generator (app.py, visual_report.py)

  • Facade Function: evaluate_sample(data) provides a top-level single-function interface that automatically wires pipeline components, fits parameters, executes identification, and formats output.
  • Visual Diagnostics: Generates 4-panel PNG report figures containing Empirical Histogram vs Fitted PDF, ECDF vs Theoretical CDF, Q-Q Plot, and Candidate Distribution Rankings.

5. Workspace Reorganization

  • scripts/ Directory: Contains workflow utilities including generate_ml_dataset.py (multiprocessed 16,000-sample dataset generator), cluster_distributions.py (Ward hierarchical clustering script), and train_model.py (model training script).

@f1i3g3
f1i3g3 self-requested a review August 10, 2026 18:53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Документация же такая же, как в experiment?

Comment on lines +55 to +58
for target in potential_targets:
if hasattr(self.engine, target):
setattr(self.engine, target, p_value)
break

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь установит первый попавшийся, так и должно быть?

"lilliefors",
"cramer_von_mises",
"gini_index",
"moran_test",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сунуть в БД в будущем? Если релизить в прод.

self.stage1_model = RandomForestClassifier(
n_estimators=200, max_depth=12, random_state=42, n_jobs=2
)
self.stage1_model.fit(X_df[self.stage1_features], y_families)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, здесь снова обучается на всех данных. Так и должно быть?



class CVCache:
"""Thread-safe cache for critical values queried from SQLite database.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

threading.Lock не увидел.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Точно кластеризация следует за цветами на дендрограмме?

Comment thread pyproject.toml

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пометка на потом: починить criterion.

@faux66
faux66 requested a review from alex98247 August 13, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants