Installs into .claude/skills of the current project.
Are you the author of Ds Confidence Intervals?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/paulpas-ds-confidence-intervals)
---
name: ds-confidence-intervals
compatibility: opencode
completeness: 95
content-types:
- code
- guidance
- do-dont
- examples
description: '"Provides Constructs confidence intervals using bootstrap, analytical
methods, and uncertainty quantification for parameter estimation"'
license: MIT
maturity: stable
metadata:
domain: coding
output-format: code
related-skills: ds-bayesian-inference, ds-hypothesis-testing, ds-monte-carlo
role: implementation
scope: implementation
triggers: confidence intervals, bootstrap, uncertainty quantification, confidence
bounds, credible intervals
archetypes:
- tactical
- generation
anti_triggers:
- brainstorming
- vague ideation
- code golf
- over-engineering
response_profile:
verbosity: low
directive_strength: high
abstraction_level: operational
version: "1.0.0"
---
# Confidence Intervals
Comprehensive guide to confidence intervals in machine learning and data science workflows.
## When to Use This Skill
- Solving real-world statistical inference problems
- Building machine learning pipelines with confidence intervals
- Implementing best practices for confidence intervals
- Optimizing model performance using confidence intervals techniques
- Learning industry-standard approaches to confidence intervals
## When NOT to Use This Skill
- When using pre-built libraries without understanding underlying concepts
- For toy problems that don't require confidence intervals rigor
- When domain expertise in specific problem requires different approach
- If your problem doesn't require the complexity this skill provides
## Purpose and Key Concepts
Confidence Intervals is a critical component of the machine learning workflow. This skill covers:
1. **Theoretical foundations** — Mathematical principles and statistical concepts
2. **Practical implementation** — Working code examples and patterns
3. **Common pitfalls** — Mistakes to avoid and how to recover from them
4. **Best practices** — Industry-standard approaches and optimization techniques
## Core Workflow
1. **Understand the problem** — Clearly define what you're solving for
2. **Select approach** — Choose the right technique for your data and constraints
3. **Implement solution** — Write clean, tested code following best practices
4. **Validate results** — Verify your implementation with tests and validation
5. **Optimize performance** — Improve efficiency and accuracy incrementally
## Implementation Patterns
### Pattern 1: Basic Confidence Intervals
```python
import numpy as np
import pandas as pd
from scipy import stats
def calculate_analytical_ci(data: np.ndarray, confidence: float = 0.95) -> tuple[float, float]:
"""Calculate analytical confidence interval using t-distribution."""
if not isinstance(data, np.ndarray):
data = np.asarray(data)
n = len(data)
if n < 2:
raise ValueError("Sample size must be at least 2")
mean = np.mean(data)
std_err = stats.sem(data)
margin = stats.t.ppf((1 + confidence) / 2, df=n - 1) * std_err
return (mean - margin, mean + margin)
def calculate_bootstrap_ci(data: np.ndarray, confidence: float = 0.95, n_resamples: int = 1000) -> tuple[float, float]:
"""Calculate bootstrap confidence interval via resampling."""
rng = np.random.default_rng(42)
bootstrap_means = np.empty(n_resamples)
for i in range(n_resamples):
sample = rng.choice(data, size=len(data), replace=True)
bootstrap_means[i] = np.mean(sample)
alpha = 1 - confidence
lower = np.percentile(bootstrap_means, 100 * alpha / 2)
upper = np.percentile(bootstrap_means, 100 * (1 - alpha / 2))
return (lower, upper)
```
### Pattern 2: Production-Ready Confidence Intervals
```python
import logging
import numpy as np
import pandas as pd
from typing import Any, Dict, Literal
logger = logging.getLogger(__name__)
class ConfidenceIntervalCalculator:
"""Production-grade calculator for confidence intervals."""
def __init__(self, confidence: float = 0.95, method: Literal['analytical', 'bootstrap'] = 'analytical', n_resamples: int = 1000):
self.confidence = confidence
self.method = method
self.n_resamples = n_resamples
logger.info(f"Initialized CI calculator with method={method}, confidence={confidence}")
def execute(self, data: pd.DataFrame, column: str) -> Dict[str, Any]:
"""Execute confidence interval calculation on specified column."""
if column not in data.columns:
raise KeyError(f"Column '{column}' not found in DataFrame")
values = data[column].dropna().values
if len(values) < 2:
raise ValueError("Insufficient non-null data for CI calculation")
try:
if self.method == 'analytical':
n = len(values)
mean = np.mean(values)
std_err = stats.sem(values)
margin = stats.t.ppf((1 + self.confidence) / 2, df=n - 1) * std_err
lower, upper = mean - margin, mean + margin
else:
rng = np.random.default_rng(42)
boots = np.array([np.mean(rng.choice(values, size=len(values), replace=True))
for _ in range(self.n_resamples)])
alpha = 1 - self.confidence
lower, upper = np.percentile(boots, 100 * alpha / 2), np.percentile(boots, 100 * (1 - alpha / 2))
logger.info(f"Calculated CI: [{lower:.4f}, {upper:.4f}]")
return {
'status': 'success'
'lower_bound': float(lower)
'upper_bound': float(upper)
'point_estimate': float(np.mean(values))
'method': self.method
'sample_size': int(n if self.method == 'analytical' else len(values))
}
except Exception as e:
logger.error(f"CI calculation failed: {e}")
return {'status': 'error', 'message': str(e)}
```
## Best Practices
- ✅ Always validate your implementation on test data
- ✅ Document your assumptions and methodology
- ✅ Use version control for reproducibility
- ✅ Monitor performance metrics in production
- ✅ Periodically review and update your approach
- ✅ Test with edge cases and outliers
- ✅ Log all significant operations for debugging
- ✅ Follow PEP 8 and SOLID principles for maintainable statistical code
## Common Pitfalls
| Pitfall | Problem | Solution |
|
---
---
## Constraints
### MUST DO
- Validate all data preprocessing steps are fit-only on training data, never on validation or test sets
- Implement reproducible pipelines with fixed random seeds and deterministic operations where possible
- Report model performance with confidence intervals via bootstrapping or cross-validation across multiple runs
- Log all experiments with parameters, metrics, and artifacts using MLflow or equivalent tracking system
### MUST NOT DO
- Do not evaluate a model on the same data used for training — always hold out a proper test set
- Avoid overfitting to the validation set by limiting hyperparameter search iterations
- Never use features that can only be computed at inference time (look-ahead bias)
- Do not report single-run accuracy without statistical significance testing or error bars
## Live References
> Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.
- [Confidence Interval — Wikipedia](https://en.wikipedia.org/wiki/Confidence_interval)
- [SciPy Stats — Statistical Distributions](https://docs.scipy.org/doc/scipy/reference/stats.html)
- [Statistical Intervals (NIST Handbook)](https://itl.nist.gov/div898/handbook/prc/section2/prc211.htm)
- [Confidence Interval Calculator (GraphPad)](https://www.graphpad.com/quickcalcs/confint1/)
- [Bayesian Credible Intervals — PyMC docs](https://docs.pymc.io/en/stable/notebooks/posterior_interpretation.html)