Skip to main content

Monte Carlo Power Analysis for Statistical Models

Project description

MCPower

Simple Monte Carlo power analysis for complex models. Find the sample size you need or check if your study has enough power - even with complex models that traditional power analysis can't handle.

Why MCPower?

Traditional power analysis breaks down with interactions, correlated predictors, or non-normal data. MCPower uses simulation instead of formulas - it generates thousands of datasets exactly like yours, then sees how often your analysis finds real effects.

Works with complexity: Interactions, correlations, any distribution
R-style formulas: outcome = treatment + covariate + treatment*covariate
Two simple commands: Find sample size or check power
Scenario analysis: Test robustness under realistic conditions
Minimal math required: Just specify your model and effects

Get Started in 2 Minutes

Install

pip install mcpower

Update to the latest version (every few days).

pip install --upgrade mcpower

Your First Power Analysis

# 0. First initialization could take a few seconds, due to compilation (package is not distributed yet with compiled files)
import mcpower

# 1. Define your model (just like R)
model = mcpower.LinearRegression("satisfaction = treatment + motivation")

# 2. Set effect sizes (how big you expect effects to be)
model.set_effects("treatment=0.5, motivation=0.3")

# 3. Change the treatment to "binary" (people receive treatment or not).
model.set_variable_type("treatment=binary")

# 4. Find the sample size you need
model.find_sample_size(target_test="treatment", from_size=50, to_size=200)

Output: "You need N=75 for 80% power to detect the treatment effect"

That's it! 🎉

🎯 Scenario Analysis: Test Your Assumptions

Real studies rarely match perfect assumptions. MCPower's scenario analysis tests how robust your power calculations are under realistic conditions.

# Test robustness with scenario analysis
model.find_sample_size(
    target_test="treatment", 
    from_size=50, to_size=300,
    scenarios=True  # 🔥 The magic happens here
)

Output:

SCENARIO SUMMARY
================================================================================

Uncorrected Sample Sizes:
Test                                     Optimistic   Realistic    Doomer      
-------------------------------------------------------------------------------
treatment                                75           85           100         
================================================================================

What each scenario means:

  • Optimistic: Your ideal conditions (original settings)
  • Realistic: Moderate real-world complications (small effect variations, mild assumption violations)
  • Doomer: Conservative estimate (larger effect variations, stronger assumption violations)

💡 Pro tip: Use the Realistic scenario for planning. If Doomer is acceptable, you're really safe!

Understanding Effect Sizes

Effect sizes tell you how much the outcome changes when predictors change.

  • Effect size = 0.5 means the outcome increases by 0.5 standard deviations when:
    • Continuous variables: Predictor increases by 1 standard deviation
    • Binary variables: Predictor changes from 0 to 1 (e.g., control → treatment)

Practical examples:

model.set_effects("treatment=0.5, age=0.3, income=0.2")
  • treatment=0.5: Treatment increases outcome by 0.5 SD (medium-large effect)
  • age=0.3: Each 1 SD increase in age → 0.3 SD increase in outcome
  • income=0.2: Each 1 SD increase in income → 0.2 SD increase in outcome

Effect size guidelines:

  • 0.1 = Small effect (detectable but modest)
  • 0.25 = Medium effect (clearly noticeable)
  • 0.4 = Large effect (substantial impact)

Effect size guidelines (binary variables):

  • 0.2 = Small effect (detectable but modest)
  • 0.5 = Medium effect (clearly noticeable)
  • 0.8 = Large effect (substantial impact)

Your uploaded data is automatically standardized (mean=0, SD=1) so effect sizes work the same way whether you use synthetic or real data.

Copy-Paste Examples for Common Studies

Randomized Controlled Trial

import mcpower

# RCT with treatment + control variables
model = mcpower.LinearRegression("outcome = treatment + age + baseline_score")
model.set_effects("treatment=0.6, age=0.2, baseline_score=0.8")
model.set_variable_type("treatment=binary")  # 0/1 treatment

# Find sample size for treatment effect with scenario analysis
model.find_sample_size(target_test="treatment", from_size=100, to_size=500, 
                      by=50, scenarios=True)

A/B Test with Interaction

import mcpower

# Test if treatment effect depends on user type
model = mcpower.LinearRegression("conversion = treatment + user_type + treatment*user_type")
model.set_effects("treatment=0.4, user_type=0.3, treatment:user_type=0.5")
model.set_variable_type("treatment=binary, user_type=binary")

# Check power robustness for the interaction
model.find_power(sample_size=400, target_test="treatment:user_type", scenarios=True)

Survey with Correlated Predictors

import mcpower

# Predictors are often correlated in real data
model = mcpower.LinearRegression("wellbeing = income + education + social_support")
model.set_effects("income=0.4, education=0.3, social_support=0.6")
model.set_correlations("corr(income, education)=0.5, corr(income, social_support)=0.3")

# Find sample size for any effect
model.find_sample_size(target_test="all", from_size=200, to_size=800, 
                      by=100, scenarios=True)

Customize for Your Study

Different Variable Types

# Binary (0/1), skewed, or other distributions
model.set_variable_type("treatment=binary, income=right_skewed, age=normal")

# Binary with custom proportions (30% get treatment)
model.set_variable_type("treatment=(binary,0.3)")

Your Own Data (be careful, not yet well tested)

import pandas as pd

# Use your pilot data for realistic simulations
df = pd.read_csv('examples/cars.csv')
model.upload_own_data(df)  # Automatically preserves correlations

Multiple Testing

# Testing multiple effects? Control false positives
model.find_power(
    sample_size=200, 
    target_test="treatment,covariate,treatment:covariate",
    correction="Benjamini-Hochberg",
    scenarios=True  # Test robustness too!
)

Test the single violation of assumptions.

# Customize how much "messiness" to add in scenarios
model.set_heterogeneity(0.2)        # Effect sizes vary between people
model.set_heteroskedasticity(0.15)  # Violation of equal variance assumption

# Then run scenario analysis
model.find_sample_size(target_test="treatment", scenarios=False)

More precision

# To make a more precise estimation, consider increasing the number of simulations.
model.set_simulations(10000)

# MCPower is already heavily optimized, but there is old code that allows for parallelization. Use it to speed up your largest simulations.
model.set_parallel(True)

Quick Reference

Want to... Use this
Find required sample size model.find_sample_size(target_test="effect_name")
Check power for specific N model.find_power(sample_size=150, target_test="effect_name")
Test robustness Add scenarios=True to either method
Test overall model target_test="overall"
Test multiple effects target_test="effect1, effect2" or "all"
Binary variables model.set_variable_type("var=binary")
Correlated predictors model.set_correlations("corr(var1, var2)=0.4")
Multiple testing correction Add correction="FDR", or "Holm" pr "Bonferroni"

When to Use MCPower

✅ Use MCPower when you have:

  • Interaction terms (treatment*covariate)
  • Binary or non-normal variables
  • Correlated predictors
  • Multiple effects to test
  • Need to test assumption robustness
  • Complex models where traditional power analysis fails

✅ Use Scenario Analysis when:

  • Planning important studies (grants, dissertations)
  • Working with messy real-world data
  • Effect sizes are uncertain
  • Want conservative sample size estimates
  • Stakeholders need confidence in your numbers

❌ Use traditional power analysis for:

  • For models that are not yet implemented
  • When all assumptions are clearly met

What Makes Scenarios Different? (Be careful, unvalidated, preliminary scenarios)

Traditional power analysis assumes perfect conditions. MCPower's scenarios add realistic "messiness":

Scenario What's Different When to Use
Optimistic Your exact settings Best-case planning
Realistic Mild effect variations, small assumption violations Recommended for most studies
Doomer Larger effect variations, stronger assumption violations Conservative/worst-case planning

Behind the scenes, scenarios randomly vary:

  • Effect sizes between participants
  • Correlation strengths
  • Variable distributions
  • Assumption violations

This gives you a range of realistic outcomes instead of a single optimistic estimate. ⚠️ Important: Scenario analysis and uploaded data features are experimental. Use with caution for critical decisions.

📚 Advanced Features (Click to expand)

Advanced Options

All Variable Types

model.set_variable_type("""
    treatment=binary,           # 0/1 with 50% split
    ses=(binary,0.3),          # 0/1 with 30% split  
    age=normal,                # Standard normal (default)
    income=right_skewed,       # Positively skewed
    depression=left_skewed,    # Negatively skewed
    response_time=high_kurtosis, # Heavy-tailed
    rating=uniform             # Uniform distribution
""")

Complex Correlation Structures

import numpy as np

# Full correlation matrix for 3 variables
corr_matrix = np.array([
    [1.0, 0.4, 0.6],    # Variable 1 with others
    [0.4, 1.0, 0.2],    # Variable 2 with others
    [0.6, 0.2, 1.0]     # Variable 3 with others
])
model.set_correlations(corr_matrix)

Performance Tuning

# Adjust for your needs
model.set_power(90)           # Target 90% power instead of 80%
model.set_alpha(0.01)         # Stricter significance (p < 0.01)
model.set_simulations(10000)  # High precision (slower)

Formula Syntax

# These are equivalent:
"y = x1 + x2 + x1*x2"        # Assignment style
"y ~ x1 + x2 + x1*x2"        # R-style formula  
"x1 + x2 + x1*x2"            # Predictors only

# Interactions:
"x1*x2"         # Main effects + interaction (x1 + x2 + x1:x2)
"x1:x2"         # Interaction only
"x1*x2*x3"      # All main effects + all interactions

Correlation Syntax

# String format (recommended)
model.set_correlations("corr(x1, x2)=0.3, corr(x1, x3)=-0.2")

# Shorthand format  
model.set_correlations("(x1, x2)=0.3, (x1, x3)=-0.2")

Requirements

  • Python ≥ 3.7
  • NumPy, SciPy, scikit-learn, matplotlib
  • joblib (optional, for parallel processing)

Need Help?

Aim for future (waiting for suggestions)

  • ✅ Linear Regression
  • ✅ Scenarios, robustness analysis
  • 🚧 Logistic Regression (coming soon)
  • 🚧 ANOVA (and factor as variables) (coming soon)
  • 🚧 Guide about methods, corrections (coming soon)
  • 📋 Rewriting to Cython (backend change)
  • 📋 Mixed Effects Models
  • 📋 2 groups comparision with alternative tests
  • 📋 Robust regression methods

License & Citation

GPL v3. If you use MCPower in research, please cite:

@software{mcpower2025,
  author = {Pawel Lenartowicz},
  title = {MCPower: Monte Carlo Power Analysis for Statistical Models},
  year = {2025},
  url = {https://github.com/pawlenartowicz/MCPower}
}

🚀 Ready to start? Copy one of the examples above and adapt it to your study!

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

mcpower-0.3.1.tar.gz (59.5 kB view details)

Uploaded Source

Built Distributions

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

mcpower-0.3.1-cp312-cp312-win_amd64.whl (421.1 kB view details)

Uploaded CPython 3.12Windows x86-64

mcpower-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

mcpower-0.3.1-cp312-cp312-macosx_11_0_arm64.whl (586.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mcpower-0.3.1-cp311-cp311-win_amd64.whl (300.0 kB view details)

Uploaded CPython 3.11Windows x86-64

mcpower-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mcpower-0.3.1-cp311-cp311-macosx_11_0_arm64.whl (409.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

mcpower-0.3.1-cp310-cp310-win_amd64.whl (178.9 kB view details)

Uploaded CPython 3.10Windows x86-64

mcpower-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (611.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

mcpower-0.3.1-cp310-cp310-macosx_11_0_arm64.whl (233.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file mcpower-0.3.1.tar.gz.

File metadata

  • Download URL: mcpower-0.3.1.tar.gz
  • Upload date:
  • Size: 59.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpower-0.3.1.tar.gz
Algorithm Hash digest
SHA256 e6b90939b170f90e5b9f300476b2b5c044c95deb8507f624cdbebfcec77e41f0
MD5 0af2a36125abcce7f274f24302e7f3a3
BLAKE2b-256 c7204a697220535a28397a2bffedcfd893c0c3497dcbd9cffe00b0909bc0b0b2

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: mcpower-0.3.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 421.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpower-0.3.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bebbf7469076212d8130ea0284d68abefa324aede73c1e208c2bb80e1b17856c
MD5 cfa248a6ca0b3145c63d15b79f363f0a
BLAKE2b-256 2f60f24eb7f492268da1eae6129c3cba87735f311568c58214b6951fa4fcc2cf

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e3b66003b9a9795f815631e45d714b0faf2510dc101acb715e85606266500d8a
MD5 20679570cceb5b6bffb0ca6ef5d237b9
BLAKE2b-256 8b59760ffb1d61d3096b9ed617cac1d190fcd738eb0872a1eea5b5a10a249f1e

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b64d064ccd1b134a26abbb40bbe285c465b247199beeb612acc37a6fc48407f
MD5 dd9b63ec0390f8b71ab801f7301ae585
BLAKE2b-256 69b9fe1a7562b3201eb352cc5bfb6e6e8d89320e1bdcbb42877e351450effe77

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: mcpower-0.3.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 300.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpower-0.3.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc41da2c257966ec1c693321807d88fb9b79ea7ba1ff42d41f4afe62c6b19e04
MD5 e6a8b0263fe23185780af4170d2b0ac5
BLAKE2b-256 d67500a63c0cb68ee4720b096c9ebfde1184ee11e91fe0f8fd9cdf95a4054700

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1582b6854502950e96705cb5817b46469db2f1630c7021c1b56b5dfcb938be39
MD5 8851a8257b8ff5e08630b1722f232be6
BLAKE2b-256 768e9536271b200744f5cc2feb4db35e553d4378928e10f10f6253d3bf7e0ed5

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8c7e5619b84d9fe1cbe2ff9e0d007b8ed9bc88f94e55754f9070e03a68870f2
MD5 526b8037cfcc5de7376dca32ea070ba0
BLAKE2b-256 bd1d742736446c55d0def0a1416327fb3928b49eed98483f00c6589d4f135c83

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: mcpower-0.3.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 178.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpower-0.3.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a80b64525f6eec24679db6c33d484b6a3929be2a2ad44709f5778fca1d7c512b
MD5 077364c9345a0a77594a89c4633fc722
BLAKE2b-256 19b7654a2f9575b03792dab156886738eb39eb97e3bb07a8fa277d9002cdcb97

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d0ce7453d2015b864dcfaa3db6e7321ac9ffcc10fac03d69848656069e4609d
MD5 81b25d6b8fb201035857f45d24a62846
BLAKE2b-256 631334046b7bb503815e2d6f48b4bddaf870c253392831826ab08ed2c9f127f8

See more details on using hashes here.

File details

Details for the file mcpower-0.3.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcpower-0.3.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ee43fc8c0b4dcf69d5c9bdaeb0c428f134bc65a035833689681752f08b914ce4
MD5 d9637177fcca01d75004cff199494760
BLAKE2b-256 25e9bf2847063c30b6060d8788cb39eed73f22fb4499fd7799f913e12192b75a

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