Stats methods

From vm0-ai/vm0-skills · View interactive version

Stats methods is an AI agent skill from the GitHub repository vm0-ai/vm0-skills. Apply statistics for summaries, tests, distributions, experiments, outliers, and trends.

How to use this skill

Skill file

Summarizing Numeric Data

Choosing a Center Metric

| Data Characteristic | Recommended Measure | Rationale |

|---|---|---|

| Numeric, meaningful additive scale | Mean | Reflects the total per observation; sensitive to extremes |

| Skewed numeric data | Median and relevant quantiles | Describes the middle and tails without letting extremes dominate |

| Nominal categories | Counts, proportions, mode | Category labels have no numeric order or mean |

| Ordinal responses | Distribution, median, ordered quantiles | Preserves ranking without assuming equal spacing |

| Revenue per user or other skewed economic data | Mean for economics; median/quantiles for typical experience | Different summaries answer different questions |

Choose summaries for the question and measurement scale. Do not average IDs or category codes, or require both mean and median for every KPI. A mean-median gap can suggest asymmetry but does not diagnose the distribution on its own.

Quantifying Variability

Telling the Story with Percentiles

Choose percentiles relevant to the decision; extreme quantiles in small samples are unstable and depend on interpolation. The following are positions, not guaranteed boundaries of "typical" values:

p1:   First percentile (not the minimum)
p5:   Fifth percentile
p25:  First quartile
p50:  Median
p75:  Third quartile
p90:  Top 10% threshold (heavy users, premium tier)
p95:  Ninety-fifth percentile
p99:  Extreme top 1%

Sample insight: "Half of all sessions last under 4.2 minutes, yet the top decile exceeds 22 minutes, which pushes the average to 7.8 minutes."

Characterizing Distributions

For numeric measures relevant to the question, examine:

Trend Analysis and Projection

Smoothing Noisy Time Series

# Weekly smoother — useful for daily data with weekday/weekend cycles
df['smooth_7'] = df['metric'].rolling(window=7, min_periods=1).mean()

# Four-week smoother — reduces short-term noise; does not remove all seasonality
df['smooth_28'] = df['metric'].rolling(window=28, min_periods=1).mean()

Period Comparisons

Measuring Growth

Simple rate:   (current - prior) / prior
CAGR:          (final / initial) ^ (1 / n_years) - 1
Log rate:      ln(current / prior)

Simple growth is undefined at a zero base and often misleading for negative bases; show absolute change and explain the sign convention. Use CAGR and log growth for strictly positive values, with a valid elapsed interval. Do not silently replace zero or negative inputs. Log growth is additive over time, not a general fix for volatility.

Spotting Seasonal Cycles

  1. Visually inspect the raw series first
  2. Aggregate by day-of-week to surface weekly rhythms
  3. Aggregate by calendar month to surface annual rhythms
  4. Use matched-period comparisons where informative; holiday shifts and changing business conditions can still confound them

Lightweight Forecasting Approaches

For analysts who need quick projections rather than full modeling:

Report a conditional point forecast when supported and explain the method, horizon, and assumptions. Add a prediction interval only when a model, validation errors, or another stated method supports its coverage. A sensitivity scenario varies explicit assumptions; it is not a confidence or prediction interval. If uncertainty cannot be quantified from the available data, say so instead of inventing bounds. Backtest against a simple baseline when sufficient history is available.

Hand off to a specialist when the pattern is non-linear, multiple seasonal cycles overlap, external drivers (ad spend, holidays) matter, or when forecast precision drives resource decisions.

Detecting and Handling Outliers

Identification Techniques

Z-score approach (assumes approximate normality):

z = (df['val'] - df['val'].mean()) / df['val'].std()
outliers = df[abs(z) > 3]  # beyond 3 standard deviations

IQR fence approach (a descriptive flag, not a distribution-free error detector):

q1 = df['val'].quantile(0.25)
q3 = df['val'].quantile(0.75)
iqr = q3 - q1
lo = q1 - 1.5 * iqr
hi = q3 + 1.5 * iqr
outliers = df[(df['val'] < lo) | (df['val'] > hi)]

Percentile cutoff approach (selects tails even in valid data; not proof of anomalies):

outliers = df[(df['val'] < df['val'].quantile(0.01)) |
              (df['val'] > df['val'].quantile(0.99))]

What to Do with Outliers

Never strip outliers automatically. Follow this decision process:

  1. Diagnose: Is this a recording error, a legitimately extreme observation, or a sign of a separate population?
  2. Errors: Verify against source definitions before correcting or excluding (e.g., an impossible age; an epoch timestamp may be valid or a missing-value sentinel)
  3. Legitimate extremes: Retain them and preserve the quantity being estimated. If the question concerns a mean or total (such as ARPU or total revenue), keep that metric and add median/IQR or other distribution summaries to describe typical experience rather than replacing the target metric
  4. Distinct populations: Analyze separately (e.g., enterprise accounts vs. self-serve)

Document every exclusion: "We set aside 47 records (0.3% of the dataset) with order values above $50K; these bulk enterprise transactions are covered in a separate section."

Detecting Anomalies in Time Series

  1. Establish an expected baseline (rolling average or year-ago value)
  2. Compute the residual: actual minus expected
  3. Choose an alert rule using the residual distribution, seasonality, dependence, and acceptable false alarms; a 2-3 standard-deviation rule is only a heuristic
  4. Differentiate one-off spikes (point anomalies) from lasting shifts (change points)

Hypothesis Testing Essentials

When It Applies

Use formal testing when a sampling or experimental design and its assumptions support inference:

Step-by-Step Process

  1. Define the estimand, unit of assignment/observation, sampling design, and any pairing or clustering
  2. State H0 and H1 (including direction) before inspecting results; a no-difference null is one possible choice
  3. Choose a test and significance level appropriate to the decision; alpha controls a procedure's Type I error under H0 and its assumptions, not the probability this finding is false
  4. Check assumptions and calculate the estimate, uncertainty, test statistic, and p-value as appropriate
  5. Interpret evidence and practical consequences together; non-rejection is not proof of no effect or equivalence

Selecting the Right Test

| Question | Appropriate Test | Conditions |

|---|---|---|

| Two independent group means differ? | Welch t-test | Independent observations; adequate behavior of the mean's sampling distribution; small, heavy-tailed samples need care |

| Two independent conversion proportions differ? | Proportions z-test or suitable exact method | Defined binary trials; check expected successes and failures for the approximation; repeated users need dependence-aware methods |

| Same entities measured twice? | Paired t-test | Correct pairing, independent pairs, suitable distribution of within-pair differences; pairing does not remove time confounding |

| Three or more independent group means? | ANOVA or Welch ANOVA | Independence; classical ANOVA additionally assumes equal variances; assess residual behavior and planned comparisons |

| Two independent ordinal or continuous distributions differ? | Mann-Whitney U | A rank/distribution test; not a generic test of medians. Location/median interpretations require additional shape assumptions; handle ties and exact/asymptotic choice explicitly |

| Two categorical variables related? | Chi-squared or suitable exact test | Independent counts, mutually exclusive cells, sufficient expected counts for the chi-squared approximation |

For clustered, repeated, censored, or time-dependent data, use a design-aware method rather than treating every row as an independent observation. See the SciPy Mann-Whitney documentation for its specific null, alternatives, and implementation assumptions.

Beyond p-values: Practical Impact

A p-value measures how incompatible the observed test statistic is with a specified null model, through its probability of an equally or more extreme statistic under that model. It is not the probability that H0 is true or that chance alone produced the result. Statistical significance does not measure effect size, practical importance, or causality. See the ASA statement on p-values. Accompany test results with:

Sample Size Awareness

Guarding Against Statistical Pitfalls

Correlation vs. Causation

Whenever a correlation surfaces, explicitly evaluate:

Safe phrasing: "Users who adopt feature X exhibit 30% higher retention"

Unsafe phrasing: "Feature X causes 30% higher retention" (requires experimental evidence)

The Multiple Testing Trap

Running many tests inflates false positives:

Simpson's Paradox

An overall trend can invert when you break the data into subgroups:

Survivorship Bias

Your dataset only contains entities that persisted long enough to be recorded:

Ecological Fallacy

Group-level patterns may not describe individuals:

Illusory Precision

Overly specific numbers suggest unjustified confidence:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.