@@ -706,7 +706,10 @@ pub fn compute_roc_and_metrics_from_value(
706706 FitFunction :: ppv => ppv ( tp_init, fp_init) ,
707707 FitFunction :: g_mean => g_mean ( sens_init, spec_init) ,
708708 // Regression metrics — threshold optimization not applicable
709- FitFunction :: spearman | FitFunction :: rmse | FitFunction :: mutual_information => 0.0 ,
709+ FitFunction :: spearman
710+ | FitFunction :: pearson
711+ | FitFunction :: rmse
712+ | FitFunction :: mutual_information => 0.0 ,
710713 } ;
711714
712715 if obj_init > best_objective {
@@ -765,7 +768,10 @@ pub fn compute_roc_and_metrics_from_value(
765768 FitFunction :: npv => npv ( tn, fn_count) ,
766769 FitFunction :: ppv => ppv ( tp, fp) ,
767770 FitFunction :: g_mean => g_mean ( sensitivity, specificity) ,
768- FitFunction :: spearman | FitFunction :: rmse | FitFunction :: mutual_information => 0.0 ,
771+ FitFunction :: spearman
772+ | FitFunction :: pearson
773+ | FitFunction :: rmse
774+ | FitFunction :: mutual_information => 0.0 ,
769775 } ;
770776
771777 if objective > best_objective {
@@ -956,6 +962,40 @@ pub fn spearman_correlation(x: &[f64], y: &[f64]) -> f64 {
956962 cov / ( var_x. sqrt ( ) * var_y. sqrt ( ) )
957963}
958964
965+ /// Compute Pearson correlation coefficient between two vectors.
966+ ///
967+ /// Returns r ∈ [-1, 1]. Higher = stronger linear association.
968+ /// Unlike Spearman (rank-based), Pearson measures linear correlation on raw values.
969+ pub fn pearson_correlation ( x : & [ f64 ] , y : & [ f64 ] ) -> f64 {
970+ assert_eq ! ( x. len( ) , y. len( ) , "Pearson: vectors must have equal length" ) ;
971+ let n = x. len ( ) ;
972+ if n < 2 {
973+ return 0.0 ;
974+ }
975+
976+ let n_f = n as f64 ;
977+ let mean_x: f64 = x. iter ( ) . sum :: < f64 > ( ) / n_f;
978+ let mean_y: f64 = y. iter ( ) . sum :: < f64 > ( ) / n_f;
979+
980+ let mut cov = 0.0 ;
981+ let mut var_x = 0.0 ;
982+ let mut var_y = 0.0 ;
983+
984+ for i in 0 ..n {
985+ let dx = x[ i] - mean_x;
986+ let dy = y[ i] - mean_y;
987+ cov += dx * dy;
988+ var_x += dx * dx;
989+ var_y += dy * dy;
990+ }
991+
992+ if var_x == 0.0 || var_y == 0.0 {
993+ return 0.0 ;
994+ }
995+
996+ cov / ( var_x. sqrt ( ) * var_y. sqrt ( ) )
997+ }
998+
959999/// Compute negative Root Mean Squared Error between predictions and targets.
9601000///
9611001/// Returns -RMSE so that higher = better (consistent with other fit functions).
0 commit comments