efficient_pca 0.1.8

Principal component computation using SVD and covariance matrix trick
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
// Global allocator setup for jemalloc
#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
use jemallocator::Jemalloc;

#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use efficient_pca::PCA;
use ndarray::Array2;
use rand::distributions::Uniform;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::time::Instant;

#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
use jemalloc_ctl::{epoch, stats};

// Enum to specify the type of data source for benchmarks.
#[derive(Clone, Debug)]
enum DataSource {
    Dense012,
    Sparse012(f64), // Parameter is sparsity level (e.g., 0.95 for 95% zeros)
    LowVariance012 {
        fraction_low_var_feats: f64, // Proportion of features that are low-variance
        majority_val_in_low_var_feat: f64, // In a low-var feature, probability of seeing the 'majority value'
                                           // The 'majority value' itself will be fixed (e.g. 0.0) for simplicity in this generator.
                                           // Minority values will be 1.0 or 2.0, split equally.
    },
}

/// Generates random data of shape (n_samples x n_features) with values 0, 1, or 2 (as f64), seeded for reproducibility.
fn generate_random_data(n_samples: usize, n_features: usize, seed: u64) -> Array2<f64> {
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let distribution = Uniform::new_inclusive(0, 2);
    Array2::from_shape_fn((n_samples, n_features), |_| rng.sample(distribution) as f64)
}

/// Generates sparse random data of shape (n_samples x n_features).
/// Values are 0.0 (with probability `sparsity`), or 1.0/2.0 (with equal probability for the remainder).
fn generate_sparse_random_data(
    n_samples: usize,
    n_features: usize,
    sparsity: f64,
    seed: u64,
) -> Array2<f64> {
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let value_distribution = Uniform::new_inclusive(1, 2); // For 1.0 or 2.0

    Array2::from_shape_fn((n_samples, n_features), |_| {
        if rng.gen_range(0.0..1.0) < sparsity {
            0.0
        } else {
            rng.sample(value_distribution) as f64
        }
    })
}

/// Generates low-variance random data.
/// For a `fraction_low_var_feats` of columns, values are `0.0` with probability `majority_val_in_low_var_feat_freq`,
/// or `1.0`/`2.0` (equal chance) otherwise. Other columns are standard 0,1,2 random.
fn generate_low_variance_data(
    n_samples: usize,
    n_features: usize,
    fraction_low_var_feats: f64,
    majority_val_in_low_var_feat_freq: f64,
    seed: u64,
) -> Array2<f64> {
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let standard_dist = Uniform::new_inclusive(0, 2);
    let minority_val_dist = Uniform::new_inclusive(1, 2);

    let mut data_vec = Vec::with_capacity(n_samples * n_features);

    for _j in 0..n_features {
        let is_low_var_feature = rng.gen::<f64>() < fraction_low_var_feats;
        for _i in 0..n_samples {
            if is_low_var_feature {
                if rng.gen::<f64>() < majority_val_in_low_var_feat_freq {
                    data_vec.push(0.0);
                } else {
                    data_vec.push(rng.sample(minority_val_dist) as f64);
                }
            } else {
                data_vec.push(rng.sample(standard_dist) as f64);
            }
        }
    }
    Array2::from_shape_vec((n_samples, n_features), data_vec)
        .expect("Shape mismatch in generate_low_variance_data")
}

/// Runs `fit` or `rfit` on the provided data, returns (time_secs, memory_kb) measured.
fn benchmark_pca(
    use_rfit: bool,
    data: &Array2<f64>,
    n_components_override: Option<usize>,
    n_oversamples_for_rfit: usize,
    seed_for_rfit: u64,
) -> (f64, u64, u64) {
    // Memory stats using jemalloc_ctl
    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    epoch::advance().unwrap();
    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    let resident_before = stats::resident::read().unwrap();
    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    let active_before = stats::active::read().unwrap(); // active is closer to virtual memory used by application

    // Fallback for non-jemalloc or msvc builds - RSS and Virt will be 0
    #[cfg(not(all(feature = "jemalloc", not(target_env = "msvc"))))]
    let resident_before = 0;
    #[cfg(not(all(feature = "jemalloc", not(target_env = "msvc"))))]
    let active_before = 0;

    let start_time = Instant::now();

    let mut pca = PCA::new();
    let transformed_data: Array2<f64>;

    let n_components_to_use_for_rfit = n_components_override
        .unwrap_or_else(|| std::cmp::min(data.nrows(), data.ncols()).min(30).max(2));

    if use_rfit {
        transformed_data = pca
            .rfit(
                data.clone(),
                n_components_to_use_for_rfit,
                n_oversamples_for_rfit,
                Some(seed_for_rfit),
                None,
            )
            .expect("rfit failed");
        assert_eq!(
            transformed_data.ncols(),
            n_components_to_use_for_rfit,
            "RFIT: Transformed data column count should match requested components for rfit."
        );
    } else {
        pca.fit(data.clone(), None).expect("fit failed");
        transformed_data = pca.transform(data.clone()).expect("transform failed");
        let actual_fit_components = pca.rotation().map_or(0, |r| r.ncols());
        assert_eq!(transformed_data.ncols(), actual_fit_components, "FIT: Transformed data column count should match actual components in the model after fit.");
    }

    assert_eq!(
        transformed_data.nrows(),
        data.nrows(),
        "Transformed data should have same number of rows as input."
    );

    let duration = start_time.elapsed().as_secs_f64();

    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    epoch::advance().unwrap();
    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    let resident_after = stats::resident::read().unwrap();
    #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
    let active_after = stats::active::read().unwrap();

    #[cfg(not(all(feature = "jemalloc", not(target_env = "msvc"))))]
    let resident_after = 0;
    #[cfg(not(all(feature = "jemalloc", not(target_env = "msvc"))))]
    let active_after = 0;

    let rss_delta_bytes = resident_after.saturating_sub(resident_before);
    let virt_delta_bytes = active_after.saturating_sub(active_before);

    (
        duration,
        (rss_delta_bytes / 1024) as u64,
        (virt_delta_bytes / 1024) as u64,
    ) // Convert bytes to KB
}

fn write_raw_data_to_tsv(
    raw_data: &[RawBenchDataPoint],
    filename: &str,
) -> Result<(), std::io::Error> {
    let file = File::create(filename)?;
    let mut writer = BufWriter::new(file);

    // Write header
    writeln!(
        writer,
        "ScenarioName	NumSamples	NumFeatures	BackendName	Iteration	RunType	TimeSec	RSSDeltaKB	VirtDeltaKB	NumComponentsOverride"
    )?;

    // Write data
    for point in raw_data {
        let n_comp_str = point
            .n_components_override
            .map_or_else(|| "None".to_string(), |k| k.to_string());
        writeln!(
            writer,
            "{}	{}	{}	{}	{}	{}	{:.6}	{}	{}	{}", // Using {:.6} for TimeSec for precision
            point.scenario_name,
            point.n_samples,
            point.n_features,
            point.backend_name,
            point.iteration_idx,
            point.run_type,
            point.time_sec,
            point.rss_delta_kb,
            point.virt_delta_kb,
            n_comp_str
        )?;
    }
    Ok(())
}

#[derive(Debug, Clone)]
struct RawBenchDataPoint {
    scenario_name: String,
    n_samples: usize,
    n_features: usize,
    backend_name: String,
    iteration_idx: u64, // Criterion's iteration count (from 0 to iters-1)
    run_type: String,   // "fit" or "rfit"
    time_sec: f64,
    rss_delta_kb: u64,
    virt_delta_kb: u64,
    n_components_override: Option<usize>,
}

fn determine_appropriate_sample_size(
    scenario_name_short: &str, // e.g., "Large", "Wide", "Small"
    is_rfit: bool,
    _n_samples: usize, // _ to indicate potentially unused for now, but good for context
    n_features: usize,
) -> usize {
    if !is_rfit {
        // Fit method
        match scenario_name_short {
            "Large" | "Square" | "Sparse-W" => return 10,
            "Wide" | "LowVar-W" | "Wide-k10" | "Wide-k50" | "Wide-k200" if n_features >= 10000 => {
                return 10
            }
            "Wide-XL" if n_features >= 100000 => return 10,
            "Wide-L" if n_features >= 50000 => return 20,
            "Medium" | "Tall" => return 50,
            _ => return 100, // For "Small" and other faster scenarios
        }
    } else {
        // Rfit method
        match scenario_name_short {
            "Wide-k200" if n_features >= 10000 => return 20,
            "Wide-XL" if n_features >= 100000 => return 30,
            "Large" | "Wide-L" | "Sparse-W" => return 50,
            _ => return 100, // For other faster rfit scenarios
        }
    }
}

fn criterion_benchmark_runner(c: &mut Criterion) {
    let mut all_raw_data = Vec::<RawBenchDataPoint>::new();
    let current_backend_name = if cfg!(feature = "backend_faer") {
        "faer".to_string()
    } else {
        "ndarray".to_string()
    };

    let scenarios = vec![
        ("Small", 100, 50, 1234, DataSource::Dense012, None),
        ("Medium", 1000, 500, 1234, DataSource::Dense012, None),
        ("Large", 5000, 2000, 1234, DataSource::Dense012, None),
        ("Square", 2000, 2000, 1234, DataSource::Dense012, None),
        ("Tall", 10000, 500, 1234, DataSource::Dense012, None),
        ("Wide", 500, 10000, 1234, DataSource::Dense012, None),
        ("Wide-L", 100, 50000, 1234, DataSource::Dense012, None),
        ("Wide-XL", 88, 100000, 1234, DataSource::Dense012, None),
        (
            "Sparse-W",
            500,
            20000,
            1234,
            DataSource::Sparse012(0.95),
            None,
        ),
        (
            "LowVar-W",
            500,
            10000,
            1234,
            DataSource::LowVariance012 {
                fraction_low_var_feats: 0.5,
                majority_val_in_low_var_feat: 0.95,
            },
            None,
        ),
        ("Wide-k10", 500, 10000, 1234, DataSource::Dense012, Some(10)),
        ("Wide-k50", 500, 10000, 1234, DataSource::Dense012, Some(50)),
        (
            "Wide-k200",
            500,
            10000,
            1234,
            DataSource::Dense012,
            Some(200),
        ),
    ];

    for (name, n_samples, n_features, seed, data_source_type, n_components_override) in scenarios {
        // Clone data_source_type if it's captured by multiple closures or used after move.
        let data = match data_source_type.clone() {
            DataSource::Dense012 => generate_random_data(n_samples, n_features, seed),
            DataSource::Sparse012(s) => generate_sparse_random_data(n_samples, n_features, s, seed),
            DataSource::LowVariance012 {
                fraction_low_var_feats,
                majority_val_in_low_var_feat,
            } => generate_low_variance_data(
                n_samples,
                n_features,
                fraction_low_var_feats,
                majority_val_in_low_var_feat,
                seed,
            ),
        };

        let oversamples_for_rfit = 0;

        // --- FIT Benchmark ---

        // --- FIT Benchmark ---
        let fit_group_name = format!("fit/{}", name);
        let fit_sample_size = determine_appropriate_sample_size(name, false, n_samples, n_features);
        let mut fit_group = c.benchmark_group(fit_group_name);
        fit_group.sample_size(fit_sample_size);
        let input_size_bytes = (n_samples * n_features * std::mem::size_of::<f64>()) as u64;
        fit_group.throughput(Throughput::Bytes(input_size_bytes));

        let _fit_benchmark_id = BenchmarkId::new(
            "fit", // Use "fit" as function_id
            format!(
                "{}_s{}_f{}_c{:?}",
                name, n_samples, n_features, n_components_override
            ), // Parameter string
        );

        fit_group.bench_with_input(_fit_benchmark_id, &data.clone(), |b, data_to_bench| {
            b.iter_custom(|iters| {
                let mut total_duration = std::time::Duration::new(0, 0);
                for i in 0..iters {
                    // Use 'i' for iteration_idx
                    let (time_taken, rss_mem_used, virt_mem_used) = benchmark_pca(
                        false,
                        data_to_bench,
                        n_components_override,
                        oversamples_for_rfit,
                        seed,
                    );
                    total_duration += std::time::Duration::from_secs_f64(time_taken);

                    all_raw_data.push(RawBenchDataPoint {
                        scenario_name: name.to_string(),
                        n_samples,
                        n_features,
                        backend_name: current_backend_name.clone(),
                        iteration_idx: i,
                        run_type: "fit".to_string(),
                        time_sec: time_taken,
                        rss_delta_kb: rss_mem_used,
                        virt_delta_kb: virt_mem_used,
                        n_components_override,
                    });
                }
                total_duration
            });
        });
        fit_group.finish();

        // --- RFIT Benchmark ---
        let rfit_group_name = format!("rfit/{}", name);
        let rfit_sample_size = determine_appropriate_sample_size(name, true, n_samples, n_features);
        let mut rfit_group = c.benchmark_group(rfit_group_name);
        rfit_group.sample_size(rfit_sample_size);
        rfit_group.throughput(Throughput::Bytes(input_size_bytes));

        let rfit_benchmark_id = BenchmarkId::new(
            "rfit", // Use "rfit" as function_id
            format!(
                "{}_s{}_f{}_c{:?}",
                name, n_samples, n_features, n_components_override
            ), // Parameter string
        );

        rfit_group.bench_with_input(rfit_benchmark_id, &data.clone(), |b, data_to_bench| {
            b.iter_custom(|iters| {
                let mut total_duration = std::time::Duration::new(0, 0);
                for i in 0..iters {
                    // Use 'i' for iteration_idx
                    let (time_taken, rss_mem_used, virt_mem_used) = benchmark_pca(
                        true,
                        data_to_bench,
                        n_components_override,
                        oversamples_for_rfit,
                        seed,
                    );
                    total_duration += std::time::Duration::from_secs_f64(time_taken);

                    all_raw_data.push(RawBenchDataPoint {
                        scenario_name: name.to_string(),
                        n_samples,
                        n_features,
                        backend_name: current_backend_name.clone(),
                        iteration_idx: i,
                        run_type: "rfit".to_string(),
                        time_sec: time_taken,
                        rss_delta_kb: rss_mem_used,
                        virt_delta_kb: virt_mem_used,
                        n_components_override,
                    });
                }
                total_duration
            });
        });
        rfit_group.finish();
    }

    if let Err(e) = write_raw_data_to_tsv(&all_raw_data, "benchmark_raw_results.tsv") {
        eprintln!("Failed to write raw benchmark data to TSV: {}", e);
    }
}

criterion_group!(benches, criterion_benchmark_runner);
criterion_main!(benches);