Skip to main content
Editor

SciPy Statistics: Distributions, Hypothesis Tests, and Correlations

Advanced30 min6 exercises120 XP
0/6 exercises

A pharmaceutical company develops a new headache medicine. They give it to 100 patients and a placebo to another 100. The treatment group reports slightly fewer headaches. But is that difference real, or just random chance? This is the question statistics answers.

SciPy's stats module gives you the tools to answer these questions with mathematical rigor. You'll learn to describe data with summary statistics, model it with probability distributions, test hypotheses with t-tests and chi-square tests, and measure relationships with correlations.

How Do You Summarize Data with Descriptive Statistics?

Before running any fancy tests, you need to understand your data. Descriptive statistics condense thousands of data points into a handful of numbers that tell you what's typical, how spread out the data is, and whether it's skewed.

Descriptive statistics with SciPy
import numpy as np
from scipy import stats
# Exam scores for a class of 30 students
np.random.seed(42)
scores = np.random.normal(loc=75, scale=10, size=30).astype(int)
# Central tendency
print('Mean:', round(np.mean(scores), 1))
print('Median:', round(np.median(scores), 1))
mode_result = stats.mode(scores, keepdims=True)
print('Mode:', mode_result.mode[0])
# Spread
print('\nStd deviation:', round(np.std(scores, ddof=1), 1))
print('Variance:', round(np.var(scores, ddof=1), 1))
print('Range:', np.ptp(scores))
print('IQR:', round(stats.iqr(scores), 1))
# Shape
print('\nSkewness:', round(stats.skew(scores), 2))
print('Kurtosis:', round(stats.kurtosis(scores), 2))
Output
Mean: 72.5
Median: 72.0
Mode: 72

Std deviation: 9.1
Variance: 82.3
Range: 35
IQR: 9.8

Skewness: 0.12
Kurtosis: -0.37

Let's break down what each measurement tells you:

  • Mean: the average. Sensitive to outliers.
  • Median: the middle value. Robust to outliers.
  • Std deviation: how far values typically are from the mean.
  • IQR (interquartile range): the range of the middle 50% of data.
  • Skewness: 0 means symmetric, positive means tail on the right, negative means tail on the left.
  • Kurtosis: how heavy the tails are. Positive means more outliers than a normal distribution.
  • What Is the Normal Distribution and Why Does It Matter?

    The normal distribution (the "bell curve") is the most important distribution in statistics. Heights, test scores, measurement errors, and countless natural phenomena follow it. It's defined by just two parameters: the mean (center) and standard deviation (width).

    SciPy lets you work with normal distributions programmatically. You can calculate probabilities, find percentiles, and generate random samples.

    Working with the normal distribution
    from scipy import stats
    # Create a normal distribution: mean=100, std=15 (like IQ scores)
    iq_dist = stats.norm(loc=100, scale=15)
    # What percentage of people have IQ above 130?
    prob_above_130 = 1 - iq_dist.cdf(130)
    print(f'IQ above 130: {prob_above_130:.1%}')
    # What percentage have IQ between 85 and 115?
    prob_middle = iq_dist.cdf(115) - iq_dist.cdf(85)
    print(f'IQ between 85-115: {prob_middle:.1%}')
    # What IQ is at the 95th percentile?
    iq_95th = iq_dist.ppf(0.95)
    print(f'95th percentile IQ: {iq_95th:.1f}')
    # The 68-95-99.7 rule
    for n_std in [1, 2, 3]:
    prob = iq_dist.cdf(100 + n_std*15) - iq_dist.cdf(100 - n_std*15)
    print(f'Within {n_std} std dev: {prob:.1%}')
    Output
    IQ above 130: 2.3%
    IQ between 85-115: 68.3%
    95th percentile IQ: 124.7
    Within 1 std dev: 68.3%
    Within 2 std dev: 95.4%
    Within 3 std dev: 99.7%

    How Does Hypothesis Testing Work?

    Hypothesis testing is like a courtroom trial. You start by assuming the "defendant" (your hypothesis) is innocent (no effect). Then you look at the evidence (data). If the evidence is strong enough, you reject the assumption of innocence.

    In statistics, the "innocent until proven guilty" assumption is called the null hypothesis (H0). The alternative hypothesis (H1) is what you're trying to prove. The p-value is the probability of seeing your data (or something more extreme) if the null hypothesis were true.

    The Independent Samples T-Test

    The t-test compares the means of two groups. "Is there a real difference between group A and group B, or is the difference just random noise?" This is the workhorse of A/B testing.

    A/B testing with the t-test
    import numpy as np
    from scipy import stats
    np.random.seed(42)
    # A/B test: does the new checkout page increase spending?
    control_group = np.random.normal(loc=50, scale=12, size=100) # Old page
    treatment_group = np.random.normal(loc=55, scale=12, size=100) # New page
    print('Control mean: ${:.2f}'.format(np.mean(control_group)))
    print('Treatment mean: ${:.2f}'.format(np.mean(treatment_group)))
    print('Difference: ${:.2f}'.format(np.mean(treatment_group) - np.mean(control_group)))
    print()
    # Run the t-test
    t_stat, p_value = stats.ttest_ind(control_group, treatment_group)
    print(f'T-statistic: {t_stat:.3f}')
    print(f'P-value: {p_value:.4f}')
    print()
    # Interpret the result
    alpha = 0.05
    if p_value < alpha:
    print(f'P-value ({p_value:.4f}) < alpha ({alpha})')
    print('Result: REJECT null hypothesis')
    print('The new checkout page significantly affects spending.')
    else:
    print(f'P-value ({p_value:.4f}) >= alpha ({alpha})')
    print('Result: FAIL TO REJECT null hypothesis')
    print('No significant difference detected.')
    Output
    Control mean: $48.75
    Treatment mean: $55.27
    Difference: $6.51
    
    T-statistic: -4.122
    P-value: 0.0001
    
    P-value (0.0001) < alpha (0.05)
    Result: REJECT null hypothesis
    The new checkout page significantly affects spending.

    When Do You Use the Chi-Square Test?

    The t-test works for numerical data (amounts, scores, times). But what about categorical data? "Did more people choose option A or option B?" "Is product preference independent of age group?" That's where the chi-square test comes in.

    Chi-square test for categorical data
    import numpy as np
    from scipy import stats
    # Survey: Do people prefer product A, B, or C?
    # If there's no preference, each should get 1/3 of votes.
    observed = np.array([45, 35, 20]) # Actual votes
    expected_equal = np.array([100/3, 100/3, 100/3]) # Expected if no preference
    # Chi-square goodness of fit test
    chi2, p_value = stats.chisquare(observed)
    print('Observed votes:', observed)
    print('Expected (equal):', [round(e, 1) for e in expected_equal])
    print(f'\nChi-square statistic: {chi2:.2f}')
    print(f'P-value: {p_value:.4f}')
    print()
    if p_value < 0.05:
    print('People DO have a significant preference.')
    print(f'Product A is the most popular with {observed[0]} votes.')
    else:
    print('No significant preference detected.')
    Output
    Observed votes: [45 35 20]
    Expected (equal): [np.float64(33.3), np.float64(33.3), np.float64(33.3)]
    
    Chi-square statistic: 9.50
    P-value: 0.0087
    
    People DO have a significant preference.
    Product A is the most popular with 45 votes.

    You can also use chi-square to test whether two categorical variables are independent. For example: "Is there a relationship between gender and product preference?"

    Chi-square test of independence
    import numpy as np
    from scipy import stats
    # Contingency table: product preference by age group
    # Product A Product B Product C
    contingency = np.array([
    [30, 10, 5], # Age 18-30
    [20, 25, 15], # Age 31-50
    [10, 15, 30], # Age 50+
    ])
    print('Contingency table:')
    print(' Prod A Prod B Prod C')
    labels = ['18-30 ', '31-50 ', '50+ ']
    for label, row in zip(labels, contingency):
    print(f' {label} {row[0]:5d} {row[1]:5d} {row[2]:5d}')
    # Chi-square test of independence
    chi2, p_value, dof, expected = stats.chi2_contingency(contingency)
    print(f'\nChi-square: {chi2:.2f}')
    print(f'Degrees of freedom: {dof}')
    print(f'P-value: {p_value:.6f}')
    if p_value < 0.05:
    print('\nProduct preference IS related to age group.')
    else:
    print('\nProduct preference is NOT related to age group.')
    Output
    Contingency table:
             Prod A  Prod B  Prod C
      18-30      30     10      5
      31-50      20     25     15
      50+        10     15     30
    
    Chi-square: 35.64
    Degrees of freedom: 4
    P-value: 0.000000
    
    Product preference IS related to age group.

    How Do You Measure the Relationship Between Two Variables?

    Correlation measures how strongly two variables move together. Do taller people tend to weigh more? Does more advertising lead to more sales? Correlation gives you a number between -1 and +1 that quantifies the relationship.

  • +1 = perfect positive correlation (as one goes up, the other always goes up)
  • 0 = no linear relationship
  • -1 = perfect negative correlation (as one goes up, the other always goes down)
  • SciPy offers two types: Pearson (for linear relationships) and Spearman (for any monotonic relationship, even non-linear).

    Pearson and Spearman correlations
    import numpy as np
    from scipy import stats
    np.random.seed(42)
    # Study hours and exam scores (strong positive correlation)
    hours = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    scores = hours * 7 + np.random.normal(0, 3, 10) + 30
    # Pearson correlation (linear)
    r_pearson, p_pearson = stats.pearsonr(hours, scores)
    print('Pearson correlation:')
    print(f' r = {r_pearson:.3f}')
    print(f' p-value = {p_pearson:.4f}')
    print(f' Interpretation: {", ".join(["strong" if abs(r_pearson) > 0.7 else "moderate" if abs(r_pearson) > 0.4 else "weak", "positive" if r_pearson > 0 else "negative"])} correlation')
    print()
    # Spearman rank correlation (monotonic)
    r_spearman, p_spearman = stats.spearmanr(hours, scores)
    print('Spearman correlation:')
    print(f' r = {r_spearman:.3f}')
    print(f' p-value = {p_spearman:.4f}')
    print(f'\nPearson assumes linear relationship.')
    print(f'Spearman only assumes monotonic (always increasing or decreasing).')
    Output
    Pearson correlation:
      r = 0.995
      p-value = 0.0000
      Interpretation: strong, positive correlation
    
    Spearman correlation:
      r = 1.000
      p-value = 0.0000
    
    Pearson assumes linear relationship.
    Spearman only assumes monotonic (always increasing or decreasing).

    What Are Confidence Intervals?

    A single number (like a sample mean) is a point estimate. But how precise is it? A confidence interval gives you a range: "We're 95% confident the true mean is between X and Y." Wider intervals mean less certainty.

    Confidence intervals
    import numpy as np
    from scipy import stats
    np.random.seed(42)
    # Customer satisfaction scores (1-10 scale)
    scores = np.random.normal(loc=7.5, scale=1.5, size=50)
    mean = np.mean(scores)
    sem = stats.sem(scores) # Standard error of the mean
    n = len(scores)
    print(f'Sample size: {n}')
    print(f'Sample mean: {mean:.2f}')
    print(f'Standard error: {sem:.3f}')
    print()
    # 95% confidence interval
    ci_95 = stats.t.interval(0.95, df=n-1, loc=mean, scale=sem)
    print(f'95% CI: ({ci_95[0]:.2f}, {ci_95[1]:.2f})')
    print(f'Width: {ci_95[1] - ci_95[0]:.2f}')
    print()
    # 99% confidence interval (wider)
    ci_99 = stats.t.interval(0.99, df=n-1, loc=mean, scale=sem)
    print(f'99% CI: ({ci_99[0]:.2f}, {ci_99[1]:.2f})')
    print(f'Width: {ci_99[1] - ci_99[0]:.2f}')
    print()
    print('Higher confidence = wider interval (less precise)')
    Output
    Sample size: 50
    Sample mean: 7.16
    Standard error: 0.198
    
    95% CI: (6.76, 7.56)
    Width: 0.80
    
    99% CI: (6.63, 7.69)
    Width: 1.06
    
    Higher confidence = wider interval (less precise)

    Practice Exercises

    Compute Descriptive Statistics
    Write Code

    Given the salaries array, compute and print the mean, median, and standard deviation (with ddof=1), each rounded to 1 decimal place. Also compute and print the IQR (interquartile range) using scipy.stats.iqr(), rounded to 1 decimal place. Print each on a separate line.

    import numpy as np
    from scipy import stats
    salaries = np.array([45000, 48000, 52000, 55000, 58000,
    62000, 65000, 70000, 85000, 120000])
    # Compute and print mean, median, std, iqr
    Work with the Normal Distribution
    Write Code

    Battery life for a phone model follows a normal distribution with mean 10 hours and standard deviation 1.5 hours. Using scipy.stats.norm, calculate: (1) the probability that a random phone lasts more than 12 hours (print as percentage rounded to 1 decimal), (2) the battery life at the 90th percentile (rounded to 2 decimal places). Print each on a separate line.

    from scipy import stats
    # Battery life: mean=10, std=1.5
    battery = stats.norm(loc=10, scale=1.5)
    # Probability of lasting more than 12 hours (%)
    # 90th percentile battery life
    Run an A/B Test with a T-Test
    Write Code

    You ran an A/B test on email subject lines. Group A (old subject) and Group B (new subject) have different open rates. Run an independent samples t-test using stats.ttest_ind(). Print the t-statistic rounded to 2 decimal places, the p-value rounded to 4 decimal places, and whether the result is 'Significant' or 'Not significant' at alpha=0.05.

    import numpy as np
    from scipy import stats
    np.random.seed(123)
    group_a = np.random.normal(loc=22, scale=5, size=50) # Old subject
    group_b = np.random.normal(loc=25, scale=5, size=50) # New subject
    # Run t-test
    # Print t-statistic (2 decimals)
    # Print p-value (4 decimals)
    # Print Significant or Not significant
    Test Survey Results with Chi-Square
    Write Code

    A survey asked 200 people to choose their favorite season. The results were: Spring=60, Summer=70, Fall=45, Winter=25. If there were no preference, each season would get 50 votes. Use scipy.stats.chisquare() to test whether the distribution is uniform. Print the chi-square statistic (rounded to 1 decimal) and whether preferences are 'Significant' or 'Not significant' at alpha=0.05.

    import numpy as np
    from scipy import stats
    observed = np.array([60, 70, 45, 25])
    # Run chi-square goodness of fit test
    # Print chi-square statistic (1 decimal)
    # Print significance
    Calculate and Interpret Correlation
    Write Code

    Calculate the Pearson correlation between ad_spend and revenue. Print the correlation coefficient rounded to 3 decimal places. Then print the interpretation: 'Strong positive' if r > 0.7, 'Moderate positive' if r > 0.4, 'Weak positive' if r > 0, 'Negative' if r <= 0.

    from scipy import stats
    import numpy as np
    ad_spend = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
    revenue = np.array([15, 28, 38, 52, 55, 68, 72, 89, 95, 110])
    # Calculate Pearson correlation
    # Print r value (3 decimals)
    # Print interpretation
    Compute a Confidence Interval
    Write Code

    Given the response_times array, compute the 95% confidence interval for the mean using stats.t.interval(). Print the lower bound and upper bound, each rounded to 2 decimal places, on separate lines.

    import numpy as np
    from scipy import stats
    np.random.seed(99)
    response_times = np.random.normal(loc=250, scale=30, size=40) # milliseconds
    mean = np.mean(response_times)
    sem = stats.sem(response_times)
    n = len(response_times)
    # Compute 95% confidence interval
    # Print lower and upper bounds (2 decimals each)

    Harder problems

    More problems on this topic: Math

    Related Tutorials

    Was this tutorial helpful?