Beingamanforever

PCA Benchmarking

Mar 8th, 2026
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 4.67 KB | None | 0 0
  1. ## Benchmark script for pca.m vectorization optimization
  2. ## Compares pca_original.m vs optimized pca.m
  3.  
  4. addpath("inst");
  5. addpath("src");
  6.  
  7. ## Configuration
  8. num_runs = 10;
  9.  
  10. ## Test configurations: [n, d]
  11. test_configs = [
  12.   1000, 20;
  13.   2000, 30;
  14.   5000, 50;
  15.   8000, 50;
  16. ];
  17.  
  18. printf("=== PCA Benchmark ===\n\n");
  19.  
  20. ## Initialize results
  21. results = {};
  22. result_idx = 1;
  23.  
  24. for cfg_idx = 1:rows(test_configs)
  25.   n = test_configs(cfg_idx, 1);
  26.   d = test_configs(cfg_idx, 2);
  27.  
  28.   printf("Dataset: n=%d, d=%d\n", n, d);
  29.  
  30.   ## Generate test data with 10% missing values
  31.   rand("seed", 42);
  32.   randn("seed", 42);
  33.   X = randn(n, d);
  34.   missing_count = round(0.1 * numel(X));
  35.   missing_idx = randperm(numel(X), missing_count);
  36.   X(missing_idx) = NaN;
  37.  
  38.   ## Also generate weights for weighted test
  39.   weights = rand(n, 1) + 0.5;  # positive weights
  40.  
  41.   ## Test 1: With missing values (rows='pairwise') - coeff only
  42.   printf("  Testing with missing values (pairwise)...\n");
  43.  
  44.   original_times = zeros(1, num_runs);
  45.   for run = 1:num_runs
  46.     tic;
  47.     coeff1 = pca_original(X, "Algorithm", "eig", "Rows", "pairwise");
  48.     original_times(run) = toc;
  49.   endfor
  50.   original_mean = mean(original_times);
  51.  
  52.   optimized_times = zeros(1, num_runs);
  53.   for run = 1:num_runs
  54.     tic;
  55.     coeff2 = pca(X, "Algorithm", "eig", "Rows", "pairwise");
  56.     optimized_times(run) = toc;
  57.   endfor
  58.   optimized_mean = mean(optimized_times);
  59.  
  60.   speedup = original_mean / optimized_mean;
  61.  
  62.   ## Validate correctness
  63.   coeff_err = norm(coeff1 - coeff2);
  64.   if (coeff_err > 1e-10)
  65.     printf("    WARNING: Results differ! coeff_err=%e\n", coeff_err);
  66.   endif
  67.  
  68.   printf("    original=%.4fs, optimized=%.4fs, speedup=%.2fx\n", ...
  69.          original_mean, optimized_mean, speedup);
  70.  
  71.   results{result_idx, 1} = n;
  72.   results{result_idx, 2} = d;
  73.   results{result_idx, 3} = "missing_pairwise";
  74.   results{result_idx, 4} = original_mean;
  75.   results{result_idx, 5} = optimized_mean;
  76.   results{result_idx, 6} = speedup;
  77.   result_idx += 1;
  78.  
  79.   ## Test 2: With missing values and weights - coeff only
  80.   printf("  Testing with missing values + weights...\n");
  81.  
  82.   original_times = zeros(1, num_runs);
  83.   for run = 1:num_runs
  84.     tic;
  85.     coeff1 = pca_original(X, "Algorithm", "eig", "Rows", "pairwise", "Weights", weights);
  86.     original_times(run) = toc;
  87.   endfor
  88.   original_mean = mean(original_times);
  89.  
  90.   optimized_times = zeros(1, num_runs);
  91.   for run = 1:num_runs
  92.     tic;
  93.     coeff2 = pca(X, "Algorithm", "eig", "Rows", "pairwise", "Weights", weights);
  94.     optimized_times(run) = toc;
  95.   endfor
  96.   optimized_mean = mean(optimized_times);
  97.  
  98.   speedup = original_mean / optimized_mean;
  99.  
  100.   printf("    original=%.4fs, optimized=%.4fs, speedup=%.2fx\n", ...
  101.          original_mean, optimized_mean, speedup);
  102.  
  103.   results{result_idx, 1} = n;
  104.   results{result_idx, 2} = d;
  105.   results{result_idx, 3} = "missing_weighted";
  106.   results{result_idx, 4} = original_mean;
  107.   results{result_idx, 5} = optimized_mean;
  108.   results{result_idx, 6} = speedup;
  109.   result_idx += 1;
  110.  
  111.   ## Test 3: Without missing values (baseline)
  112.   printf("  Testing without missing values...\n");
  113.  
  114.   X_clean = randn(n, d);
  115.  
  116.   original_times = zeros(1, num_runs);
  117.   for run = 1:num_runs
  118.     tic;
  119.     [coeff1, score1, latent1] = pca_original(X_clean);
  120.     original_times(run) = toc;
  121.   endfor
  122.   original_mean = mean(original_times);
  123.  
  124.   optimized_times = zeros(1, num_runs);
  125.   for run = 1:num_runs
  126.     tic;
  127.     [coeff2, score2, latent2] = pca(X_clean);
  128.     optimized_times(run) = toc;
  129.   endfor
  130.   optimized_mean = mean(optimized_times);
  131.  
  132.   speedup = original_mean / optimized_mean;
  133.  
  134.   printf("    original=%.4fs, optimized=%.4fs, speedup=%.2fx\n", ...
  135.          original_mean, optimized_mean, speedup);
  136.  
  137.   results{result_idx, 1} = n;
  138.   results{result_idx, 2} = d;
  139.   results{result_idx, 3} = "no_missing";
  140.   results{result_idx, 4} = original_mean;
  141.   results{result_idx, 5} = optimized_mean;
  142.   results{result_idx, 6} = speedup;
  143.   result_idx += 1;
  144.  
  145.   printf("\n");
  146. endfor
  147.  
  148. ## Save results to CSV
  149. fid = fopen("pca_benchmark_results.csv", "w");
  150. fprintf(fid, "n,d,test_type,original_time,optimized_time,speedup\n");
  151. for i = 1:size(results, 1)
  152.   fprintf(fid, "%d,%d,%s,%.6f,%.6f,%.2f\n", ...
  153.           results{i, 1}, results{i, 2}, results{i, 3}, ...
  154.           results{i, 4}, results{i, 5}, results{i, 6});
  155. endfor
  156. fclose(fid);
  157.  
  158. printf("Results saved to pca_benchmark_results.csv\n\n");
  159.  
  160. ## Summary
  161. speedups = cell2mat(results(:, 6));
  162. printf("=== Summary ===\n");
  163. printf("Average speedup: %.2fx\n", mean(speedups));
  164. printf("Max speedup: %.2fx\n", max(speedups));
  165. printf("Min speedup: %.2fx\n", min(speedups));
  166.  
Advertisement
Add Comment
Please, Sign In to add comment