Skip to main content

A Python package for calculating Cohen's d effect size

Project description

Cohen's d Effect Size Calculator

PyPI version Python versions License: BSD-3-Clause Tests

A Python package for calculating Cohen's d effect size with comprehensive options for handling missing data, different axes, and pooled vs unpooled standard deviations.

Features

  • One-sample and two-sample Cohen's d: Calculate effect sizes for single samples against zero or between two independent groups
  • Flexible data handling: Support for 1D and multi-dimensional arrays with axis specification
  • Missing data policies: Choose how to handle NaN values (propagate, raise, or omit)
  • Pooled and unpooled variance: Option to use pooled standard deviation or first sample's standard deviation
  • NumPy compatibility: Full integration with NumPy arrays and broadcasting rules
  • Comprehensive testing: Extensive test suite ensuring numerical accuracy and edge case handling

Installation

Install from PyPI:

pip install cohens-d-effect-size

Install from source:

git clone https://github.com/DawitLam/cohens-d-scipy.git
cd cohens-d
pip install -e .

Quick Start

import numpy as np
from cohens_d import cohens_d

# Two-sample Cohen's d
group1 = np.array([1, 2, 3, 4, 5])
group2 = np.array([2, 3, 4, 5, 6])
effect_size = cohens_d(group1, group2)
print(f"Cohen's d: {effect_size:.3f}")

# One-sample Cohen's d (against zero)
sample = np.array([0.5, 1.0, 1.5, 2.0, 2.5])
effect_size = cohens_d(sample)
print(f"One-sample Cohen's d: {effect_size:.3f}")

Parameters

  • x: First sample or the sample to compare against zero
  • y: Second sample (optional). If not provided, calculates one-sample Cohen's d
  • axis: Axis along which to compute the effect size (default: None)
  • nan_policy: How to handle NaN values ('propagate', 'raise', 'omit')
  • ddof: Delta degrees of freedom for standard deviation calculation (default: 1)
  • keepdims: Whether to keep reduced dimensions as size 1 (default: False)
  • alternative: Alternative hypothesis ('two-sided', 'less', 'greater')
  • pooled: Whether to use pooled standard deviation for two-sample case (default: True)

Examples

Basic Usage

import numpy as np
from cohens_d import cohens_d

# Create sample data
np.random.seed(42)
control = np.random.normal(0, 1, 100)
treatment = np.random.normal(0.5, 1, 100)

# Calculate Cohen's d
d = cohens_d(control, treatment)
print(f"Effect size: {d:.3f}")

Multi-dimensional Arrays

# 2D arrays - calculate along different axes
data_2d = np.random.normal(0, 1, (10, 5))
treatment_2d = np.random.normal(0.3, 1, (10, 5))

# Along rows (axis=0)
d_rows = cohens_d(data_2d, treatment_2d, axis=0)
print(f"Effect sizes along axis 0: {d_rows}")

# Along columns (axis=1)  
d_cols = cohens_d(data_2d, treatment_2d, axis=1)
print(f"Effect sizes along axis 1: {d_cols}")

Handling Missing Data

# Data with missing values
data_with_nan = np.array([1, 2, np.nan, 4, 5])
control_with_nan = np.array([2, 3, 4, np.nan, 6])

# Omit missing values
d_omit = cohens_d(data_with_nan, control_with_nan, nan_policy='omit')
print(f"Effect size (omit NaN): {d_omit:.3f}")

# Raise error on missing values
try:
    d_raise = cohens_d(data_with_nan, control_with_nan, nan_policy='raise')
except ValueError as e:
    print(f"Error: {e}")

Pooled vs Unpooled Standard Deviation

# Using pooled standard deviation (default)
d_pooled = cohens_d(group1, group2, pooled=True)

# Using first sample's standard deviation only
d_unpooled = cohens_d(group1, group2, pooled=False)

print(f"Pooled: {d_pooled:.3f}, Unpooled: {d_unpooled:.3f}")

Interpretation

Cohen's d effect size interpretation:

  • Small effect: d ≈ 0.2
  • Medium effect: d ≈ 0.5
  • Large effect: d ≈ 0.8
def interpret_cohens_d(d):
    \"\"\"Interpret Cohen's d effect size.\"\"\"
    abs_d = abs(d)
    if abs_d < 0.2:
        return "negligible"
    elif abs_d < 0.5:
        return "small"
    elif abs_d < 0.8:
        return "medium"
    else:
        return "large"

d = cohens_d(control, treatment)
interpretation = interpret_cohens_d(d)
print(f"Effect size: {d:.3f} ({interpretation})")

Requirements

  • Python ≥ 3.8
  • NumPy ≥ 1.19.0

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

git clone https://github.com/DawitLam/cohens-d.git
cd cohens-d
pip install -e ".[dev]"

Running Tests

pytest tests/

Code Formatting

black cohens_d/ tests/
flake8 cohens_d/ tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

References

  1. Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum Associates.
  2. Lakens, D. (2013). Calculating and reporting effect sizes to facilitate cumulative science: a practical primer for t-tests and ANOVAs. Frontiers in Psychology, 4, 863.

Changelog

0.1.0 (2024-01-XX)

  • Initial release
  • Support for one-sample and two-sample Cohen's d
  • Comprehensive parameter options
  • Full NumPy compatibility
  • Extensive test coverage

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cohens_d_effect_size-1.2.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cohens_d_effect_size-1.2.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file cohens_d_effect_size-1.2.0.tar.gz.

File metadata

  • Download URL: cohens_d_effect_size-1.2.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for cohens_d_effect_size-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a2f3c0b5a5b4a404b24bdff7634c74b88583b0b8e454fa52333d78d0291cb214
MD5 fbad7a2a5f3b6f7dee12786c5b1f8654
BLAKE2b-256 34f99213668eeef00055d9c6df7be6eb696c127ede4b7848b75eec1ae072c6c1

See more details on using hashes here.

File details

Details for the file cohens_d_effect_size-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cohens_d_effect_size-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 346698d79fe4163e3a776f872c9f9ea9299bb2682d01fa9521563da959125d4f
MD5 82fd26c0b8c1d39cafcaf9c500bb3448
BLAKE2b-256 8108e426ed4d836ee4d71e28e1e03a091694cae735bc30af7d0495300790e6ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page