Skip to main content

correlation

Function correlation 

Source
pub fn correlation(x: &[f64], y: &[f64]) -> StatsResult<f64>
Expand description

Calculate Pearson correlation coefficient between two datasets

Examples found in repository?
examples/basic_usage.rs (line 34)
3fn main() {
4    println!("=== Statify Statistics Library Demo ===\n");
5
6    // Basic descriptive statistics
7    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
8    
9    println!("Dataset: {:?}\n", data);
10    println!("Descriptive Statistics:");
11    println!("  Mean: {:.2}", data.mean().unwrap());
12    println!("  Median: {:.2}", data.median().unwrap());
13    println!("  Std Dev: {:.2}", data.std_dev().unwrap());
14    println!("  Variance: {:.2}", data.variance().unwrap());
15    println!("  Min: {:.2}", data.min().unwrap());
16    println!("  Max: {:.2}", data.max().unwrap());
17    println!("  Range: {:.2}", data.range().unwrap());
18    println!("  Sum: {:.2}", data.sum().unwrap());
19
20    // Percentiles and quartiles
21    println!("\nPercentiles:");
22    println!("  25th percentile (Q1): {:.2}", data.quartile_1().unwrap());
23    println!("  50th percentile (Median): {:.2}", data.percentile(50.0).unwrap());
24    println!("  75th percentile (Q3): {:.2}", data.quartile_3().unwrap());
25    println!("  IQR: {:.2}", data.iqr().unwrap());
26
27    // Correlation and covariance
28    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
29    let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];
30    
31    println!("\nCorrelation Analysis:");
32    println!("  X: {:?}", x);
33    println!("  Y: {:?}", y);
34    println!("  Correlation: {:.4}", correlation(&x, &y).unwrap());
35    println!("  Covariance: {:.4}", covariance(&x, &y).unwrap());
36
37    // Z-scores
38    let sample = vec![10.0, 20.0, 30.0, 40.0, 50.0];
39    let z_vals = z_scores(&sample).unwrap();
40    
41    println!("\nZ-Scores:");
42    println!("  Data: {:?}", sample);
43    println!("  Z-scores: {:?}", z_vals.iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
44
45    // Real-world example: test scores
46    let test_scores = vec![
47        85.0, 92.0, 78.0, 95.0, 88.0, 76.0, 89.0, 94.0, 81.0, 87.0,
48    ];
49    
50    println!("\n=== Real-World Example: Test Scores ===");
51    println!("Scores: {:?}", test_scores);
52    println!("  Average Score: {:.2}", test_scores.mean().unwrap());
53    println!("  Median Score: {:.2}", test_scores.median().unwrap());
54    println!("  Std Deviation: {:.2}", test_scores.std_dev().unwrap());
55    println!("  Lowest Score: {:.2}", test_scores.min().unwrap());
56    println!("  Highest Score: {:.2}", test_scores.max().unwrap());
57    println!("  Score Range: {:.2}", test_scores.range().unwrap());
58}
More examples
Hide additional examples
examples/advanced_statistics.rs (line 54)
6fn main() {
7    println!("=== Advanced Statistics Demo ===\n");
8
9    // Sample dataset: student test scores
10    let scores = vec![
11        85.0, 92.0, 78.0, 95.0, 88.0, 76.0, 89.0, 94.0, 81.0, 87.0, 90.0, 83.0, 86.0, 91.0, 79.0,
12    ];
13
14    println!("📊 Student Test Scores: {:?}\n", scores);
15
16    // Basic statistics
17    println!("Basic Statistics:");
18    println!("  Mean: {:.2}", scores.mean().unwrap());
19    println!("  Median: {:.2}", scores.median().unwrap());
20    println!("  Std Dev: {:.2}", scores.std_dev().unwrap());
21    println!("  Coefficient of Variation: {:.2}%", coefficient_of_variation(&scores).unwrap());
22    println!("  Standard Error: {:.2}", standard_error(&scores).unwrap());
23
24    // Distribution shape
25    println!("\n📈 Distribution Shape:");
26    println!("  Skewness: {:.4}", skewness(&scores).unwrap());
27    println!("  Kurtosis: {:.4}", kurtosis(&scores).unwrap());
28
29    // Normalization
30    println!("\n🔄 Normalized Scores (0-1 scale):");
31    let normalized = normalize_min_max(&scores).unwrap();
32    println!("  First 5: {:?}", &normalized[0..5].iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
33
34    // Grade scaling (60-100 scale)
35    println!("\n📏 Scaled to 60-100:");
36    let scaled = normalize_range(&scores, 60.0, 100.0).unwrap();
37    println!("  First 5: {:?}", &scaled[0..5].iter().map(|&x| format!("{:.2}", x)).collect::<Vec<_>>());
38
39    // Linear regression: study hours vs test scores
40    println!("\n📉 Linear Regression: Study Hours vs Test Scores");
41    let study_hours = vec![2.0, 3.5, 1.5, 5.0, 3.0, 1.0, 3.5, 4.5, 2.5, 3.0, 4.0, 2.0, 3.0, 4.0, 1.5];
42    
43    let regression = linear_regression(&study_hours, &scores).unwrap();
44    println!("  Slope: {:.2} (points per hour)", regression.slope);
45    println!("  Intercept: {:.2}", regression.intercept);
46    println!("  R²: {:.4}", regression.r_squared);
47    
48    // Predict score for 6 hours of study
49    let predicted_score = regression.predict(6.0);
50    println!("  Predicted score for 6 hours of study: {:.2}", predicted_score);
51
52    // Correlation analysis
53    println!("\n🔗 Correlation Analysis:");
54    let corr = correlation(&study_hours, &scores).unwrap();
55    println!("  Correlation between study hours and scores: {:.4}", corr);
56
57    // Normal distribution analysis
58    println!("\n📊 Normal Distribution Analysis:");
59    let mean = scores.mean().unwrap();
60    let std_dev = scores.std_dev().unwrap();
61    
62    println!("  Using mean={:.2}, std_dev={:.2}", mean, std_dev);
63    
64    // Probability of scoring exactly 85
65    let pdf_85 = normal_pdf(85.0, mean, std_dev).unwrap();
66    println!("  PDF at score 85: {:.6}", pdf_85);
67    
68    // Probability of scoring 85 or less
69    let cdf_85 = normal_cdf(85.0, mean, std_dev).unwrap();
70    println!("  Probability of scoring ≤85: {:.2}%", cdf_85 * 100.0);
71    
72    // Probability of scoring 90 or less
73    let cdf_90 = normal_cdf(90.0, mean, std_dev).unwrap();
74    println!("  Probability of scoring ≤90: {:.2}%", cdf_90 * 100.0);
75    
76    // Probability of scoring above 90
77    println!("  Probability of scoring >90: {:.2}%", (1.0 - cdf_90) * 100.0);
78
79    // Real-world interpretation
80    println!("\n💡 Insights:");
81    if regression.r_squared > 0.7 {
82        println!("  ✓ Strong correlation between study hours and test scores!");
83    } else if regression.r_squared > 0.4 {
84        println!("  ≈ Moderate correlation between study hours and test scores.");
85    } else {
86        println!("  ✗ Weak correlation between study hours and test scores.");
87    }
88    
89    let skew_val = skewness(&scores).unwrap();
90    if skew_val.abs() < 0.5 {
91        println!("  ✓ The score distribution is approximately symmetric.");
92    } else if skew_val > 0.0 {
93        println!("  ⚠ The distribution is right-skewed (more low scores).");
94    } else {
95        println!("  ⚠ The distribution is left-skewed (more high scores).");
96    }
97    
98    let cv = coefficient_of_variation(&scores).unwrap();
99    if cv < 10.0 {
100        println!("  ✓ Low variability: scores are consistent.");
101    } else if cv < 20.0 {
102        println!("  ≈ Moderate variability in scores.");
103    } else {
104        println!("  ⚠ High variability: scores are widely spread.");
105    }
106}