sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
Documentation
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Utility functions for information-theoretic manifold learning methods

use super::bregman_divergence::BregmanDivergenceType;
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, Axis};
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use scirs2_linalg::compat::{ArrayLinalgExt, UPLO};
use sklears_core::error::{Result as SklResult, SklearsError};
use std::f64::consts::{E, PI};

// Helper functions for Bregman divergence computations

/// Compute Bregman divergence matrix for all pairs of points
pub fn compute_bregman_divergence_matrix(
    x: &Array2<f64>,
    divergence_type: &BregmanDivergenceType,
) -> SklResult<Array2<f64>> {
    let n_samples = x.nrows();
    let mut divergence_matrix = Array2::zeros((n_samples, n_samples));

    for i in 0..n_samples {
        for j in 0..n_samples {
            if i != j {
                let xi = x.row(i);
                let xj = x.row(j);
                let div = compute_bregman_divergence(&xi, &xj, divergence_type)?;
                divergence_matrix[[i, j]] = div;
            }
        }
    }

    Ok(divergence_matrix)
}

/// Compute Bregman divergence between two points
pub fn compute_bregman_divergence(
    x: &ArrayView1<f64>,
    y: &ArrayView1<f64>,
    divergence_type: &BregmanDivergenceType,
) -> SklResult<f64> {
    if x.len() != y.len() {
        return Err(SklearsError::InvalidInput(
            "Points must have the same dimension".to_string(),
        ));
    }

    let divergence = match divergence_type {
        BregmanDivergenceType::SquaredEuclidean => {
            // D_φ(x,y) = φ(x) - φ(y) - ∇φ(y)^T(x-y)
            // For φ(x) = ||x||²/2, ∇φ(y) = y
            let phi_x = x.dot(x) / 2.0;
            let phi_y = y.dot(y) / 2.0;
            let grad_phi_y_diff = y.dot(&(x - y));
            phi_x - phi_y - grad_phi_y_diff
        }
        BregmanDivergenceType::KullbackLeibler => {
            // D_φ(x,y) = Σ x_i log(x_i/y_i) - x_i + y_i
            let mut div = 0.0;
            for (xi, yi) in x.iter().zip(y.iter()) {
                if *xi > 0.0 && *yi > 0.0 {
                    div += xi * (xi / yi).ln() - xi + yi;
                } else if *xi == 0.0 && *yi >= 0.0 {
                    div += *yi;
                } else {
                    return Err(SklearsError::InvalidInput(
                        "KL divergence requires non-negative values".to_string(),
                    ));
                }
            }
            div
        }
        BregmanDivergenceType::ItakuraSaito => {
            // D_φ(x,y) = Σ (x_i/y_i - log(x_i/y_i) - 1)
            let mut div = 0.0;
            for (xi, yi) in x.iter().zip(y.iter()) {
                if *xi > 0.0 && *yi > 0.0 {
                    let ratio = xi / yi;
                    div += ratio - ratio.ln() - 1.0;
                } else {
                    return Err(SklearsError::InvalidInput(
                        "Itakura-Saito divergence requires positive values".to_string(),
                    ));
                }
            }
            div
        }
        BregmanDivergenceType::Exponential => {
            // D_φ(x,y) = Σ exp(x_i) - exp(y_i) - exp(y_i)(x_i - y_i)
            let mut div = 0.0;
            for (xi, yi) in x.iter().zip(y.iter()) {
                let exp_xi = xi.exp();
                let exp_yi = yi.exp();
                div += exp_xi - exp_yi - exp_yi * (xi - yi);
            }
            div
        }
        BregmanDivergenceType::LogSumExp => {
            // D_φ(x,y) = Σ log(1+exp(x_i)) - log(1+exp(y_i)) - sigmoid(y_i)(x_i - y_i)
            let mut div = 0.0;
            for (xi, yi) in x.iter().zip(y.iter()) {
                let log_sum_exp_xi = (1.0 + xi.exp()).ln();
                let log_sum_exp_yi = (1.0 + yi.exp()).ln();
                let sigmoid_yi = yi.exp() / (1.0 + yi.exp());
                div += log_sum_exp_xi - log_sum_exp_yi - sigmoid_yi * (xi - yi);
            }
            div
        }
    };

    Ok(divergence)
}

/// Compute Bregman centroids using iterative algorithm
pub fn compute_bregman_centroids(
    x: &Array2<f64>,
    divergence_type: &BregmanDivergenceType,
    n_centroids: usize,
) -> SklResult<Array2<f64>> {
    let (n_samples, n_features) = x.dim();

    if n_centroids > n_samples {
        return Err(SklearsError::InvalidInput(
            "Number of centroids cannot exceed number of samples".to_string(),
        ));
    }

    // Initialize centroids randomly
    let mut rng = StdRng::seed_from_u64(42);
    let mut centroids = Array2::zeros((n_centroids, n_features));

    for i in 0..n_centroids {
        let idx = rng.random_range(0..n_samples);
        centroids.row_mut(i).assign(&x.row(idx));
    }

    // Iterative refinement using Bregman centroid algorithm
    let max_iter = 100;
    let tol = 1e-6;

    for _iter in 0..max_iter {
        let mut new_centroids = Array2::zeros((n_centroids, n_features));
        let mut cluster_assignments = vec![0; n_samples];

        // Assign points to nearest centroids
        for (i, assignment) in cluster_assignments.iter_mut().enumerate() {
            let mut min_div = f64::INFINITY;
            let mut best_centroid = 0;

            for j in 0..n_centroids {
                let div =
                    compute_bregman_divergence(&x.row(i), &centroids.row(j), divergence_type)?;
                if div < min_div {
                    min_div = div;
                    best_centroid = j;
                }
            }
            *assignment = best_centroid;
        }

        // Update centroids
        for j in 0..n_centroids {
            let cluster_points: Vec<_> = (0..n_samples)
                .filter(|&i| cluster_assignments[i] == j)
                .collect();

            if !cluster_points.is_empty() {
                let centroid =
                    compute_bregman_centroid_for_points(x, &cluster_points, divergence_type)?;
                new_centroids.row_mut(j).assign(&centroid);
            } else {
                // Keep the old centroid if no points assigned
                new_centroids.row_mut(j).assign(&centroids.row(j));
            }
        }

        // Check for convergence
        let change = (&new_centroids - &centroids).mapv(|x| x.abs()).sum();
        if change < tol {
            break;
        }

        centroids = new_centroids;
    }

    Ok(centroids)
}

/// Compute Bregman centroid for a set of points
pub fn compute_bregman_centroid_for_points(
    x: &Array2<f64>,
    point_indices: &[usize],
    divergence_type: &BregmanDivergenceType,
) -> SklResult<Array1<f64>> {
    if point_indices.is_empty() {
        return Err(SklearsError::InvalidInput(
            "Cannot compute centroid for empty set of points".to_string(),
        ));
    }

    let n_features = x.ncols();
    let mut centroid = Array1::zeros(n_features);

    match divergence_type {
        BregmanDivergenceType::SquaredEuclidean => {
            // Arithmetic mean
            for &idx in point_indices {
                centroid += &x.row(idx);
            }
            centroid /= point_indices.len() as f64;
        }
        BregmanDivergenceType::KullbackLeibler => {
            // Geometric mean (after log transformation)
            for j in 0..n_features {
                let mut sum_log = 0.0;
                for &idx in point_indices {
                    let val = x[[idx, j]];
                    if val > 0.0 {
                        sum_log += val.ln();
                    }
                }
                centroid[j] = (sum_log / point_indices.len() as f64).exp();
            }
        }
        BregmanDivergenceType::ItakuraSaito => {
            // Harmonic mean
            for j in 0..n_features {
                let mut sum_inv = 0.0;
                for &idx in point_indices {
                    let val = x[[idx, j]];
                    if val > 0.0 {
                        sum_inv += 1.0 / val;
                    }
                }
                centroid[j] = point_indices.len() as f64 / sum_inv;
            }
        }
        BregmanDivergenceType::Exponential => {
            // Log-sum-exp centroid
            for j in 0..n_features {
                let mut sum_exp = 0.0;
                for &idx in point_indices {
                    sum_exp += x[[idx, j]].exp();
                }
                centroid[j] = (sum_exp / point_indices.len() as f64).ln();
            }
        }
        BregmanDivergenceType::LogSumExp => {
            // Softmax centroid
            for j in 0..n_features {
                let mut sum = 0.0;
                let mut max_val = f64::NEG_INFINITY;

                // Find max for numerical stability
                for &idx in point_indices {
                    max_val = max_val.max(x[[idx, j]]);
                }

                // Compute softmax
                for &idx in point_indices {
                    sum += (x[[idx, j]] - max_val).exp();
                }

                centroid[j] = max_val + sum.ln() - (point_indices.len() as f64).ln();
            }
        }
    }

    Ok(centroid)
}

/// Perform MDS on Bregman divergence matrix
pub fn bregman_mds(divergence_matrix: &Array2<f64>, n_components: usize) -> SklResult<Array2<f64>> {
    let n_samples = divergence_matrix.nrows();

    // Double centering (classical MDS)
    let row_means = divergence_matrix
        .mean_axis(Axis(1))
        .expect("operation should succeed");
    let col_means = divergence_matrix
        .mean_axis(Axis(0))
        .expect("operation should succeed");
    let total_mean = divergence_matrix.mean().expect("operation should succeed");

    let mut centered_matrix = Array2::zeros((n_samples, n_samples));
    for i in 0..n_samples {
        for j in 0..n_samples {
            centered_matrix[[i, j]] =
                -0.5 * (divergence_matrix[[i, j]] - row_means[i] - col_means[j] + total_mean);
        }
    }

    // Eigendecomposition
    let (eigenvalues, eigenvectors) = centered_matrix
        .eigh(UPLO::Lower)
        .map_err(|e| SklearsError::InvalidInput(format!("Eigendecomposition failed: {}", e)))?;

    // Sort eigenvalues and eigenvectors in descending order
    let mut eigen_pairs: Vec<_> = eigenvalues.iter().zip(eigenvectors.columns()).collect();
    eigen_pairs.sort_by(|a, b| b.0.partial_cmp(a.0).expect("operation should succeed"));

    // Take the top n_components eigenvectors and scale by sqrt(eigenvalue)
    let mut embedding = Array2::zeros((n_samples, n_components));
    for j in 0..n_components {
        let eigenval = *eigen_pairs[j].0;
        let eigenvec = eigen_pairs[j].1;

        if eigenval > 0.0 {
            let scale = eigenval.sqrt();
            for i in 0..n_samples {
                embedding[[i, j]] = eigenvec[i] * scale;
            }
        }
    }

    Ok(embedding)
}

/// Project new data using Bregman projections
pub fn bregman_project(
    x: &Array2<f64>,
    centroids: &Array2<f64>,
    divergence_type: &BregmanDivergenceType,
) -> SklResult<Array2<f64>> {
    let n_samples = x.nrows();
    let n_centroids = centroids.nrows();

    let mut projection = Array2::zeros((n_samples, n_centroids));

    for i in 0..n_samples {
        for j in 0..n_centroids {
            let div = compute_bregman_divergence(&x.row(i), &centroids.row(j), divergence_type)?;
            projection[[i, j]] = (-div).exp(); // Convert to similarity
        }

        // Normalize to probabilities
        let row_sum = projection.row(i).sum();
        if row_sum > 0.0 {
            projection.row_mut(i).mapv_inplace(|x| x / row_sum);
        }
    }

    Ok(projection)
}

// Helper functions for natural gradient computations

/// Compute Fisher information matrix for current embedding
pub fn compute_fisher_information_matrix(embedding: &Array2<f64>) -> SklResult<Array2<f64>> {
    let (n_samples, n_components) = embedding.dim();
    let total_params = n_samples * n_components;

    // Flatten embedding for Fisher computation
    let _params = embedding.as_slice().expect("operation should succeed"); // deferred: raw slice for advanced Fisher estimators
    let mut fisher_matrix = Array2::zeros((total_params, total_params));

    // Compute Fisher information as second moment of score function
    // Using empirical Fisher information (outer product of gradients)
    for i in 0..n_samples {
        for d1 in 0..n_components {
            for j in 0..n_samples {
                for d2 in 0..n_components {
                    let idx1 = i * n_components + d1;
                    let idx2 = j * n_components + d2;

                    // Simplified Fisher computation based on manifold structure
                    if i == j {
                        fisher_matrix[[idx1, idx2]] = if d1 == d2 { 1.0 } else { 0.0 };
                    } else {
                        // Include neighbor interactions in Fisher matrix
                        let distance = ((embedding[[i, d1]] - embedding[[j, d1]]).powi(2)
                            + (embedding[[i, d2]] - embedding[[j, d2]]).powi(2))
                        .sqrt();
                        let weight = (-distance).exp();
                        fisher_matrix[[idx1, idx2]] = weight;
                    }
                }
            }
        }
    }

    Ok(fisher_matrix)
}

/// Compute natural gradient using Fisher information metric
pub fn compute_natural_gradient(
    gradient: &Array2<f64>,
    fisher_matrix: &Array2<f64>,
    regularization: f64,
) -> SklResult<Array2<f64>> {
    let (n_samples, n_components) = gradient.dim();

    // Flatten gradient
    let grad_flat = gradient.as_slice().expect("operation should succeed");
    let grad_vec = Array1::from_vec(grad_flat.to_vec());

    // Add regularization to Fisher matrix
    let mut regularized_fisher = fisher_matrix.clone();
    for i in 0..fisher_matrix.nrows() {
        regularized_fisher[[i, i]] += regularization;
    }

    // Solve Fisher^{-1} * gradient for natural gradient
    // Using pseudo-inverse for numerical stability
    let (u, s, vt) = regularized_fisher
        .svd(true)
        .map_err(|e| SklearsError::InvalidInput(format!("SVD failed: {}", e)))?;

    let (u_mat, vt_mat) = (u, vt);
    // Compute pseudo-inverse using SVD
    let mut s_inv = Array1::zeros(s.len());
    for (i, &si) in s.iter().enumerate() {
        if si > 1e-10 {
            s_inv[i] = 1.0 / si;
        }
    }

    // Fisher^{-1} = V * S^{-1} * U^T
    let s_inv_diag = Array2::from_diag(&s_inv);
    let fisher_inv = vt_mat.t().dot(&s_inv_diag).dot(&u_mat.t());

    // Natural gradient = Fisher^{-1} * gradient
    let natural_grad_flat = fisher_inv.dot(&grad_vec);

    // Reshape back to original shape
    let natural_gradient =
        Array2::from_shape_vec((n_samples, n_components), natural_grad_flat.to_vec())
            .map_err(|e| SklearsError::InvalidInput(format!("Reshape failed: {}", e)))?;

    Ok(natural_gradient)
}

/// Compute stress gradient for embedding
pub fn compute_stress_gradient(x: &Array2<f64>, embedding: &Array2<f64>) -> SklResult<Array2<f64>> {
    let (n_samples, n_components) = embedding.dim();
    let mut gradient = Array2::zeros((n_samples, n_components));

    // Compute pairwise distances in original space
    let mut original_distances = Array2::zeros((n_samples, n_samples));
    for i in 0..n_samples {
        for j in 0..n_samples {
            if i != j {
                let diff = &x.row(i) - &x.row(j);
                original_distances[[i, j]] = diff.dot(&diff).sqrt();
            }
        }
    }

    // Compute stress gradient
    for i in 0..n_samples {
        for k in 0..n_components {
            let mut grad_sum = 0.0;

            for j in 0..n_samples {
                if i != j {
                    let embed_diff = &embedding.row(i) - &embedding.row(j);
                    let embed_dist = embed_diff.dot(&embed_diff).sqrt();

                    if embed_dist > 1e-10 {
                        let original_dist = original_distances[[i, j]];
                        let stress_factor = 1.0 - original_dist / embed_dist;
                        grad_sum += stress_factor * (embedding[[i, k]] - embedding[[j, k]]);
                    }
                }
            }

            gradient[[i, k]] = 4.0 * grad_sum; // Factor of 4 from stress derivative
        }
    }

    Ok(gradient)
}

/// Compute embedding stress (Sammon stress)
pub fn compute_embedding_stress(x: &Array2<f64>, embedding: &Array2<f64>) -> SklResult<f64> {
    let n_samples = x.nrows();
    let mut stress = 0.0;
    let mut normalizer = 0.0;

    for i in 0..n_samples {
        for j in i + 1..n_samples {
            // Original distance
            let diff_orig = &x.row(i) - &x.row(j);
            let dist_orig = diff_orig.dot(&diff_orig).sqrt();

            // Embedding distance
            let diff_embed = &embedding.row(i) - &embedding.row(j);
            let dist_embed = diff_embed.dot(&diff_embed).sqrt();

            if dist_orig > 1e-10 {
                let error = (dist_orig - dist_embed).powi(2) / dist_orig;
                stress += error;
                normalizer += dist_orig;
            }
        }
    }

    if normalizer > 0.0 {
        Ok(stress / normalizer)
    } else {
        Ok(0.0)
    }
}

// Helper functions for information-theoretic computations

/// Compute mutual information between two datasets using KNN estimation
pub fn compute_mutual_information_knn(
    x: &Array2<f64>,
    y: &Array2<f64>,
    k: usize,
) -> SklResult<f64> {
    let n_samples = x.nrows();

    if n_samples != y.nrows() {
        return Err(SklearsError::InvalidInput(
            "X and Y must have the same number of samples".to_string(),
        ));
    }

    // Combine X and Y along the feature axis
    let xy = scirs2_core::ndarray::concatenate(Axis(1), &[x.view(), y.view()])?;

    // Estimate entropies using k-NN
    let h_x = estimate_entropy_knn(x, k)?;
    let h_y = estimate_entropy_knn(y, k)?;
    let h_xy = estimate_entropy_knn(&xy, k)?;

    // MI = H(X) + H(Y) - H(X,Y)
    Ok(h_x + h_y - h_xy)
}

/// Compute mutual information using kernel density estimation
pub fn compute_mutual_information_kde(
    x: &Array2<f64>,
    y: &Array2<f64>,
    bandwidth: f64,
) -> SklResult<f64> {
    // Simplified KDE-based MI estimation
    // In practice, this would use more sophisticated KDE methods
    let n_samples = x.nrows() as f64;
    let d_x = x.ncols() as f64;
    let d_y = y.ncols() as f64;

    // Estimate using the rule of thumb for bandwidth
    let h_x = -0.5 * d_x * (2.0 * PI * E * bandwidth * bandwidth).ln() - (n_samples).ln();
    let h_y = -0.5 * d_y * (2.0 * PI * E * bandwidth * bandwidth).ln() - (n_samples).ln();
    let h_xy = -0.5 * (d_x + d_y) * (2.0 * PI * E * bandwidth * bandwidth).ln() - (n_samples).ln();

    Ok(h_x + h_y - h_xy)
}

/// Compute mutual information between two 2D arrays
pub fn compute_mutual_information_2d(x: &Array2<f64>, y: &Array2<f64>) -> SklResult<f64> {
    let n_samples = x.nrows();

    if n_samples != y.nrows() {
        return Err(SklearsError::InvalidInput(
            "X and Y must have the same number of samples".to_string(),
        ));
    }

    // Use KNN estimation with k=3
    compute_mutual_information_knn(x, y, 3)
}

/// Compute mutual information between two arrays (simplified version)
pub fn compute_mutual_information(x: &Array2<f64>, y: &Array1<f64>) -> SklResult<f64> {
    let n_samples = x.nrows();

    if n_samples != y.len() {
        return Err(SklearsError::InvalidInput(
            "X and y must have the same number of samples".to_string(),
        ));
    }

    // Convert y to 2D array for consistency
    let y_2d = y.clone().insert_axis(Axis(1));

    // Use KNN estimation with k=3
    compute_mutual_information_knn(x, &y_2d, 3)
}

/// Estimate entropy using k-nearest neighbors
pub fn estimate_entropy_knn(x: &Array2<f64>, k: usize) -> SklResult<f64> {
    let (n_samples, n_dims) = x.dim();

    if k >= n_samples {
        return Err(SklearsError::InvalidInput(
            "k must be less than the number of samples".to_string(),
        ));
    }

    let mut entropy = 0.0;

    for i in 0..n_samples {
        let xi = x.row(i);

        // Find k-th nearest neighbor distance
        let mut distances: Vec<f64> = Vec::new();

        for j in 0..n_samples {
            if i != j {
                let xj = x.row(j);
                let diff = &xi - &xj;
                let dist = diff.dot(&diff).sqrt();
                distances.push(dist);
            }
        }

        distances.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));

        if k <= distances.len() {
            let rho_k = distances[k - 1];
            if rho_k > 0.0 {
                entropy += (n_dims as f64) * rho_k.ln();
            }
        }
    }

    // Add constant terms
    entropy /= n_samples as f64;
    entropy += (n_samples as f64).ln();
    entropy += gamma_function_ln(n_dims as f64 / 2.0 + 1.0);
    entropy -= (n_dims as f64 / 2.0) * PI.ln();

    Ok(entropy)
}

/// Estimate bandwidth for kernel density estimation
pub fn estimate_bandwidth(x: &Array2<f64>, _n_neighbors: usize) -> f64 {
    let (n_samples, n_dims) = x.dim();

    // Silverman's rule of thumb
    let std_dev = x.std_axis(Axis(0), 0.0).mean().unwrap_or(1.0);
    let bandwidth = std_dev
        * ((4.0 / ((n_dims + 2) as f64)).powf(1.0 / (n_dims + 4) as f64))
        * (n_samples as f64).powf(-1.0 / (n_dims + 4) as f64);

    bandwidth.max(0.01) // Minimum bandwidth
}

/// Compute gradient of information bottleneck objective
pub fn compute_ib_gradient(
    x: &Array2<f64>,
    y: &Array1<f64>,
    weights: &Array2<f64>,
    beta: f64,
) -> SklResult<Array2<f64>> {
    let (_n_samples, n_features) = x.dim();
    let n_components = weights.ncols();

    // Approximate gradient using finite differences
    let epsilon = 1e-6;
    let mut gradient = Array2::zeros((n_features, n_components));

    for i in 0..n_features {
        for j in 0..n_components {
            // Forward difference
            let mut weights_plus = weights.clone();
            weights_plus[[i, j]] += epsilon;

            let z_plus = x.dot(&weights_plus);
            let i_z_y_plus = compute_mutual_information(&z_plus, y)?;
            let i_x_z_plus = compute_mutual_information_2d(x, &z_plus)?;
            let obj_plus = i_z_y_plus - beta * i_x_z_plus;

            // Backward difference
            let mut weights_minus = weights.clone();
            weights_minus[[i, j]] -= epsilon;

            let z_minus = x.dot(&weights_minus);
            let i_z_y_minus = compute_mutual_information(&z_minus, y)?;
            let i_x_z_minus = compute_mutual_information_2d(x, &z_minus)?;
            let obj_minus = i_z_y_minus - beta * i_x_z_minus;

            gradient[[i, j]] = (obj_plus - obj_minus) / (2.0 * epsilon);
        }
    }

    Ok(gradient)
}

/// Compute gradient of mutual information for MMI
pub fn compute_mi_gradient(
    x: &Array2<f64>,
    embedding: &Array2<f64>,
    bandwidth: f64,
) -> SklResult<Array2<f64>> {
    let (n_samples, n_components) = embedding.dim();

    // Approximate gradient using finite differences
    let epsilon = 1e-6;
    let mut gradient = Array2::zeros((n_samples, n_components));

    for i in 0..n_samples {
        for j in 0..n_components {
            // Forward difference
            let mut embedding_plus = embedding.clone();
            embedding_plus[[i, j]] += epsilon;

            let mi_plus = compute_mutual_information_kde(x, &embedding_plus, bandwidth)?;

            // Backward difference
            let mut embedding_minus = embedding.clone();
            embedding_minus[[i, j]] -= epsilon;

            let mi_minus = compute_mutual_information_kde(x, &embedding_minus, bandwidth)?;

            gradient[[i, j]] = (mi_plus - mi_minus) / (2.0 * epsilon);
        }
    }

    Ok(gradient)
}

/// Compute local Fisher information matrix
pub fn compute_local_fisher_information(
    x: &Array2<f64>,
    n_neighbors: usize,
    sigma: f64,
) -> SklResult<Array2<f64>> {
    let (n_samples, n_features) = x.dim();
    let mut fisher_matrix = Array2::zeros((n_features, n_features));

    for i in 0..n_samples {
        let xi = x.row(i);

        // Find neighbors
        let mut distances: Vec<(usize, f64)> = Vec::new();

        for j in 0..n_samples {
            if i != j {
                let xj = x.row(j);
                let diff = &xi - &xj;
                let dist = diff.dot(&diff).sqrt();
                distances.push((j, dist));
            }
        }

        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
        distances.truncate(n_neighbors);

        // Compute local Fisher information
        for &(j, dist) in &distances {
            let weight = (-dist * dist / (2.0 * sigma * sigma)).exp();
            let diff = &x.row(i) - &x.row(j);

            for p in 0..n_features {
                for q in 0..n_features {
                    fisher_matrix[[p, q]] += weight * diff[p] * diff[q] / (sigma * sigma);
                }
            }
        }
    }

    fisher_matrix /= n_samples as f64;
    Ok(fisher_matrix)
}

/// Compute global Fisher information matrix
pub fn compute_global_fisher_information(x: &Array2<f64>, sigma: f64) -> SklResult<Array2<f64>> {
    let (n_samples, n_features) = x.dim();
    let mut fisher_matrix = Array2::zeros((n_features, n_features));

    // Compute covariance matrix as approximation to Fisher information
    let mean = x.mean_axis(Axis(0)).expect("operation should succeed");
    let x_centered = x - &mean.insert_axis(Axis(0));

    for i in 0..n_samples {
        let xi = x_centered.row(i);

        for p in 0..n_features {
            for q in 0..n_features {
                fisher_matrix[[p, q]] += xi[p] * xi[q];
            }
        }
    }

    fisher_matrix /= (n_samples - 1) as f64;
    fisher_matrix /= sigma * sigma;

    Ok(fisher_matrix)
}

/// Simplified log gamma function
pub fn gamma_function_ln(x: f64) -> f64 {
    // Stirling's approximation for simplicity
    if x > 1.0 {
        (x - 0.5) * x.ln() - x + 0.5 * (2.0 * PI).ln()
    } else {
        0.0
    }
}