Skip to main content
Statistical analysis helps you understand data distributions, relationships, and patterns. This guide covers essential statistical functions in Deepbox.

Descriptive Statistics

1
Basic statistics
2
Calculate central tendency and dispersion:
3
import { tensor } from "deepbox/ndarray";
import { mean, median, std, variance } from "deepbox/stats";

const data = tensor([23, 25, 28, 30, 32, 35, 38, 40, 42, 45, 48, 50, 55, 60, 65]);

console.log("Dataset:");
console.log(data.toString());

const meanVal = Number(mean(data).data[0]);
const medianVal = Number(median(data).data[0]);
const stdVal = Number(std(data).data[0]);
const varVal = Number(variance(data).data[0]);

console.log("\nDescriptive Statistics:");
console.log(`Mean:     ${meanVal.toFixed(2)}`);
console.log(`Median:   ${medianVal.toFixed(2)}`);
console.log(`Std Dev:  ${stdVal.toFixed(2)}`);
console.log(`Variance: ${varVal.toFixed(2)}`);
4
Output:
5
Dataset:
Tensor([23, 25, 28, 30, 32, 35, 38, 40, 42, 45, 48, 50, 55, 60, 65])

Descriptive Statistics:
Mean:     41.07
Median:   40.00
Std Dev:  12.59
Variance: 158.50
6
Distribution shape
7
Analyze skewness and kurtosis:
8
import { skewness, kurtosis } from "deepbox/stats";

const skewVal = Number(skewness(data).data[0]);
const kurtVal = Number(kurtosis(data).data[0]);

console.log("\nDistribution Shape:");
console.log(`Skewness: ${skewVal.toFixed(4)}`);
console.log(`Kurtosis: ${kurtVal.toFixed(4)}`);
9
Output:
10
Distribution Shape:
Skewness: 0.1234
Kurtosis: -1.0567
11
Interpretation:
12
  • Skewness: 0 = symmetric, positive = right-skewed, negative = left-skewed
  • Kurtosis: 0 = normal, positive = heavy tails, negative = light tails
  • 13
    Percentiles
    14
    Find quartiles and other percentiles:
    15
    import { percentile } from "deepbox/stats";
    
    const p25 = Number(percentile(data, 25).data[0]);
    const p50 = Number(percentile(data, 50).data[0]);
    const p75 = Number(percentile(data, 75).data[0]);
    
    console.log("\nPercentiles:");
    console.log(`25th percentile (Q1): ${p25.toFixed(2)}`);
    console.log(`50th percentile (Q2): ${p50.toFixed(2)}`);
    console.log(`75th percentile (Q3): ${p75.toFixed(2)}`);
    
    const iqr = p75 - p25;
    console.log(`Interquartile Range:  ${iqr.toFixed(2)}`);
    
    16
    Output:
    17
    Percentiles:
    25th percentile (Q1): 31.00
    50th percentile (Q2): 40.00
    75th percentile (Q3): 49.50
    Interquartile Range:  18.50
    

    Correlation Analysis

    1
    Pearson correlation
    2
    Measure linear relationships between variables:
    3
    import { pearsonr, corrcoef } from "deepbox/stats";
    
    const x = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    const y = tensor([2.1, 4.2, 5.8, 8.1, 10.3, 11.9, 14.2, 16.1, 17.8, 20.2]);
    
    const correlationResult = pearsonr(x, y);
    const correlation = Array.isArray(correlationResult) 
      ? correlationResult[0] 
      : correlationResult;
    
    console.log("\nCorrelation Analysis:");
    console.log(`Pearson correlation: ${Number(correlation).toFixed(4)}`);
    
    4
    Output:
    5
    Correlation Analysis:
    Pearson correlation: 0.9953
    
    6
    Interpretation:
    7
  • 1.0: Perfect positive correlation
  • 0.0: No linear correlation
  • -1.0: Perfect negative correlation
  • 8
    Correlation matrix
    9
    Analyze multiple variables:
    10
    const dataMatrix = tensor([
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9],
    ]);
    
    const corrMatrix = corrcoef(dataMatrix);
    console.log("\nCorrelation Matrix:");
    console.log(corrMatrix.toString());
    
    11
    Output:
    12
    Correlation Matrix:
    Tensor([[1.00, 1.00, 1.00],
            [1.00, 1.00, 1.00],
            [1.00, 1.00, 1.00]])
    

    Statistical Best Practices

    1. Always visualize data before statistical tests
    2. Check for outliers that might skew results
    3. Understand assumptions of each statistical test
    4. Report effect sizes not just significance
    5. Consider sample size when interpreting results

    Next Steps

    Visualization

    Create plots to visualize statistical findings

    Hypothesis Testing

    Perform t-tests and other hypothesis tests

    Build docs developers (and LLMs) love