Skip to main content

Understanding Z-Test and P-Value with ML Use Cases

Learn about z-test and p-value in statistics with detailed examples and Python code. Understand how they apply to Machine Learning and Deep Learning for model evaluation.

What is a P-Value?

The p-value is a probability that measures the strength of the evidence against the null hypothesis. Specifically, it is the probability of observing a test statistic (like the z-score) at least as extreme as the one computed from your sample, assuming that the null hypothesis is true.

A smaller p-value indicates stronger evidence against the null hypothesis. Common thresholds to reject the null hypothesis are:

  • p < 0.05: statistically significant
  • p < 0.01: highly significant

Python Example of Z-Test

Let’s assume we want to test whether the mean of a sample differs from a known population mean:


import numpy as np
from scipy import stats

# Sample data
sample = [2.9, 3.0, 2.5, 3.2, 3.8, 3.5]
mu = 3.0       # Population mean
sigma = 0.5    # Population std deviation
n = len(sample)
x_bar = np.mean(sample)

# Calculate z-score
z = (x_bar - mu) / (sigma / np.sqrt(n))
p_value = 2 * (1 - stats.norm.cdf(abs(z)))

print("Z-score:", z)
print("P-value:", p_value)
  

Using Z-Test and P-Value in ML/DL

In Machine Learning (ML) and Deep Learning (DL), z-tests and p-values help validate experimental results, such as whether a new model significantly outperforms a baseline model. Without statistical testing, we might mistake random fluctuations in performance for real improvements.

  • Compare two models: Test if the performance difference between two models (e.g., accuracy) is statistically significant.
  • A/B testing: Evaluate changes in algorithms, UI components, or features based on user interactions.
  • Feature selection: Check whether the mean of a feature differs between classes significantly, which may indicate predictive power.

Example: Comparing Two Models

Let’s compare the accuracy of two models over multiple runs:


acc_model_a = [0.83, 0.85, 0.82, 0.84, 0.86]
acc_model_b = [0.79, 0.78, 0.80, 0.77, 0.81]

mean_a = np.mean(acc_model_a)
mean_b = np.mean(acc_model_b)
sd = np.std(acc_model_a + acc_model_b, ddof=1)
n = len(acc_model_a)

z = (mean_a - mean_b) / (sd * np.sqrt(2/n))
p = 2 * (1 - stats.norm.cdf(abs(z)))

print("Z-Score:", z)
print("P-Value:", p)
  

Conclusion

The z-test and p-value are essential statistical tools for validating model improvements, experimental hypotheses, and performance evaluations. Especially in ML/DL pipelines, applying these tests ensures that your decisions are backed by robust statistical evidence rather than randomness.

References

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

ZeRO: Deep Memory Optimization for Training Trillion-Parameter Models

In 2020, Microsoft researchers introduced ZeRO (Zero Redundancy Optimizer) via their paper "ZeRO: Memory Optimization Towards Training Trillion Parameter Models" (arXiv:1910.02054). ZeRO is a memory optimization technique that eliminates redundancy in distributed training, enabling efficient scaling to trillion-parameter models. This provides an in-depth technical breakdown of ZeRO's partitioning strategies, memory usage analysis, and integration with DeepSpeed. 1. What is ZeRO? ZeRO eliminates redundant memory copies of model states across GPUs. Instead of replicating parameters, gradients, and optimizer states across each GPU, ZeRO partitions them across all devices. This results in near-linear memory savings as the number of GPUs increases. 2. Limitations of Traditional Data Parallelism In standard data-parallel training, every GPU maintains: Model Parameters $\theta$ Gradients $\nabla \theta$ Optimizer States $O(\theta)$ This causes memory usage ...