1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! # PKBoost: Shannon-Guided Gradient Boosting
//!
//! [](https://crates.io/crates/pkboost)
//! [](https://docs.rs/pkboost)
//! [](https://www.gnu.org/licenses/gpl-3.0)
//!
//! PKBoost (**P**erformance-Based **K**nowledge **Boost**er) is an adaptive gradient boosting
//! library built from scratch in Rust, specifically designed for **extreme class imbalance**
//! and **concept drift** scenarios.
//!
//! ## Key Features
//!
//! - **Extreme Imbalance Handling**: Outperforms XGBoost/LightGBM on datasets with <5% minority class
//! - **Drift Detection & Adaptation**: Automatically detects concept drift and triggers model adaptation
//! - **Shannon Entropy Guidance**: Splits optimized using information theory for minority class
//! - **Auto-Tuning**: No hyperparameter tuning required - auto-configures based on data
//! - **Multi-Task Support**: Binary classification, multi-class, and regression
//! - **Built-in Metrics**: PR-AUC, ROC-AUC, F1, RMSE, R², and more
//!
//! ## Quick Start
//!
//! ### Binary Classification (Recommended for Imbalanced Data)
//!
//! ```rust,no_run
//! use pkboost::{OptimizedPKBoostShannon, calculate_pr_auc, calculate_roc_auc};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Your data: Vec<Vec<f64>> for features, Vec<f64> for labels (0.0 or 1.0)
//! let x_train: Vec<Vec<f64>> = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
//! let y_train: Vec<f64> = vec![0.0, 1.0];
//! let x_test: Vec<Vec<f64>> = vec![vec![1.5, 2.5]];
//! let y_test: Vec<f64> = vec![0.0];
//!
//! // Create model with auto-tuning (recommended)
//! let mut model = OptimizedPKBoostShannon::auto(&x_train, &y_train);
//!
//! // Train with optional validation set for early stopping
//! model.fit(&x_train, &y_train, None, true)?;
//!
//! // Predict probabilities
//! let predictions = model.predict_proba(&x_test)?;
//!
//! // Evaluate
//! let pr_auc = calculate_pr_auc(&y_test, &predictions);
//! let roc_auc = calculate_roc_auc(&y_test, &predictions);
//! println!("PR-AUC: {:.4}, ROC-AUC: {:.4}", pr_auc, roc_auc);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Multi-Class Classification
//!
//! ```rust,no_run
//! use pkboost::MultiClassPKBoost;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let x_train: Vec<Vec<f64>> = vec![/* your data */];
//! let y_train: Vec<f64> = vec![0.0, 1.0, 2.0]; // Class labels: 0, 1, 2, ...
//! let x_test: Vec<Vec<f64>> = vec![/* test data */];
//!
//! // Specify number of classes
//! let mut model = MultiClassPKBoost::new(3);
//!
//! // Train
//! model.fit(&x_train, &y_train, None, true)?;
//!
//! // Get class probabilities [n_samples, n_classes]
//! let probs = model.predict_proba(&x_test)?;
//!
//! // Or get predicted class indices
//! let predictions = model.predict(&x_test)?;
//!
//! Ok(())
//! }
//! ```
//!
//! ### Regression
//!
//! ```rust,no_run
//! use pkboost::{PKBoostRegressor, calculate_rmse, calculate_r2};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let x_train: Vec<Vec<f64>> = vec![/* your data */];
//! let y_train: Vec<f64> = vec![/* continuous targets */];
//! let x_test: Vec<Vec<f64>> = vec![/* test data */];
//! let y_test: Vec<f64> = vec![/* test targets */];
//!
//! // Create regressor with auto configuration
//! let mut model = PKBoostRegressor::auto(&x_train, &y_train);
//!
//! // Train
//! model.fit(&x_train, &y_train, None, true)?;
//!
//! // Predict
//! let predictions = model.predict(&x_test)?;
//!
//! // Evaluate
//! let rmse = calculate_rmse(&y_test, &predictions);
//! let r2 = calculate_r2(&y_test, &predictions);
//! println!("RMSE: {:.4}, R²: {:.4}", rmse, r2);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Adaptive Model with Drift Detection
//!
//! For streaming data or scenarios where data distribution changes over time:
//!
//! ```rust,no_run
//! use pkboost::AdversarialLivingBooster;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let x_train: Vec<Vec<f64>> = vec![/* initial training data */];
//! let y_train: Vec<f64> = vec![/* initial labels */];
//!
//! // Create adaptive model
//! let mut model = AdversarialLivingBooster::new(&x_train, &y_train);
//!
//! // Initial training
//! model.fit_initial(&x_train, &y_train, None, true)?;
//!
//! // As new data arrives, observe it (model adapts automatically)
//! let x_new: Vec<Vec<f64>> = vec![/* new batch */];
//! let y_new: Vec<f64> = vec![/* new labels */];
//! model.observe_batch(&x_new, &y_new, true)?;
//!
//! // Check model state
//! println!("Vulnerability score: {:.4}", model.get_vulnerability_score());
//! println!("Metamorphosis count: {}", model.get_metamorphosis_count());
//!
//! Ok(())
//! }
//! ```
//!
//! ## Builder Pattern (Advanced Configuration)
//!
//! For fine-grained control over hyperparameters:
//!
//! ```rust,no_run
//! use pkboost::OptimizedPKBoostShannon;
//!
//! let model = OptimizedPKBoostShannon::builder()
//! .n_estimators(200)
//! .learning_rate(0.05)
//! .max_depth(6)
//! .min_samples_split(10)
//! .reg_lambda(1.0)
//! .gamma(0.1)
//! .subsample(0.8)
//! .colsample_bytree(0.8)
//! .early_stopping_rounds(20)
//! .histogram_bins(32)
//! .mi_weight(0.1) // Mutual information weight for imbalance
//! .scale_pos_weight(5.0) // Weight for positive class
//! .build();
//! ```
//!
//! ## Core Types
//!
//! | Type | Description |
//! |------|-------------|
//! | [`OptimizedPKBoostShannon`] | Binary classification with Shannon entropy guidance |
//! | [`MultiClassPKBoost`] | Multi-class classification via One-vs-Rest |
//! | [`PKBoostRegressor`] | Regression with MSE, Huber, or Poisson loss |
//! | [`AdversarialLivingBooster`] | Adaptive model with drift detection |
//!
//! ## Metrics
//!
//! | Function | Description |
//! |----------|-------------|
//! | [`calculate_pr_auc`] | Precision-Recall AUC (best for imbalanced data) |
//! | [`calculate_roc_auc`] | Receiver Operating Characteristic AUC |
//! | [`calculate_rmse`] | Root Mean Squared Error |
//! | [`calculate_mae`] | Mean Absolute Error |
//! | [`calculate_r2`] | R² coefficient of determination |
//!
//! ## Model Serialization
//!
//! PKBoost models implement `serde::Serialize` and `serde::Deserialize`:
//!
//! ```rust,no_run
//! use pkboost::OptimizedPKBoostShannon;
//!
//! // Save model
//! let model = OptimizedPKBoostShannon::auto(&x_train, &y_train);
//! let json = serde_json::to_string(&model)?;
//! std::fs::write("model.json", json)?;
//!
//! // Load model
//! let json = std::fs::read_to_string("model.json")?;
//! let model: OptimizedPKBoostShannon = serde_json::from_str(&json)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## When to Use PKBoost
//!
//! **✅ Good fit:**
//! - Extreme class imbalance (<5% minority class)
//! - Fraud detection, anomaly detection, rare event prediction
//! - Data that evolves over time (concept drift)
//! - When you want good results without hyperparameter tuning
//!
//! **❌ Consider alternatives for:**
//! - Perfectly balanced datasets (XGBoost may be faster)
//! - Very small datasets (<1,000 samples)
//!
//! ## Author
//!
//! **Pushp Kharat** - [GitHub](https://github.com/Pushp-Kharat1/pkboost)
//!
//! ## License
//!
//! This project is licensed under the GPL-3.0 License.
// Re-exports for convenient access
pub use AdversarialEnsemble;
pub use ;
pub use *;
pub use OptimizedHistogramBuilder;
pub use HuberLoss;
pub use AdversarialLivingBooster;
pub use ;
pub use ;
pub use FeatureMetabolism;
pub use ;
pub use OptimizedPKBoostShannon;
pub use MultiClassPKBoost;
pub use CachedHistogram;
pub use TransposedData;
pub use ;
pub use ;
pub use ;
pub use ;