Skill 寫法圖鑑

statistical-analysis

Anthropic | 資料與分析 | 原文 | Apache-2.0

描述性統計、趨勢、離群值和假設檢定,以及哪些統計宣稱要小心

中文翻譯

---
name: statistical-analysis
description: 套用統計方法,包括描述性統計、趨勢分析、離群值偵測和假設檢定。適用於分析分布、檢定顯著性、偵測異常、計算相關性,或解讀統計結果。
user-invocable: false
---

# 統計分析 Skill

描述性統計、趨勢分析、離群值偵測、假設檢定,以及什麼時候對統計宣稱要保持謹慎的指引。

## 描述性統計方法

### 集中趨勢

依資料選對中心的度量:

| Situation | Use | Why |
|---|---|---|
| 對稱分布、沒有離群值 | 平均數(Mean) | 最有效率的估計量 |
| 偏態分布 | 中位數(Median) | 對離群值穩健 |
| 類別或次序資料 | 眾數(Mode) | 非數值資料唯一的選擇 |
| 高度偏態且有離群值(例如每位使用者的營收) | 中位數 + 平均數 | 兩個都報告;差距就顯示偏態 |

**商業指標一定要同時報告平均數和中位數。** 如果兩者差很多,資料就是偏態的,只看平均數會有誤導。

### 離散程度與變異性

- **標準差(Standard deviation)**:數值通常離平均數多遠。用於常態分布的資料。
- **四分位距(IQR)**:p25 到 p75 的距離。對離群值穩健。用於偏態資料。
- **變異係數(CV)**:標準差 / 平均數。用來比較不同尺度的指標之間的變異性。
- **全距(Range)**:最大值減最小值。對離群值敏感,但能快速掌握資料的範圍。

### 商業情境下的百分位數

報告關鍵百分位數,比只看平均數能講出更完整的故事:

```
p1:   最低 1%(下限/典型的最小值)
p5:   正常範圍的低端
p25:  第一四分位數
p50:  中位數(典型使用者)
p75:  第三四分位數
p90:  前 10%/重度使用者
p95:  正常範圍的高端
p99:  前 1%/極端使用者
```

**範例說法**:「工作階段時長的中位數是 4.2 分鐘,但前 10% 的使用者每次待超過 22 分鐘,把平均數拉高到 7.8 分鐘。」

### 描述分布

你分析的每個數值分布都要描述特徵:

- **形狀**:常態、右偏、左偏、雙峰、均勻、厚尾
- **中心**:平均數和中位數(以及兩者的差距)
- **離散**:標準差或 IQR
- **離群值**:有多少個、多極端
- **界線**:有沒有自然的下限(零)或上限(100%)?

## 趨勢分析與預測

### 辨識趨勢

用**移動平均**平滑雜訊:
```python
# 7-day moving average (good for daily data with weekly seasonality)
df['ma_7d'] = df['metric'].rolling(window=7, min_periods=1).mean()

# 28-day moving average (smooths weekly AND monthly patterns)
df['ma_28d'] = df['metric'].rolling(window=28, min_periods=1).mean()
```

**期間對期間的比較**:
- 週對週(WoW):跟上週同一天比
- 月對月(MoM):跟前一個月比
- 年對年(YoY):有季節性的事業的黃金標準
- 去年同一天:比較特定的日曆日

**成長率**:
```
Simple growth: (current - previous) / previous
CAGR: (ending / beginning) ^ (1 / years) - 1
Log growth: ln(current / previous)  -- better for volatile series
```

### 季節性偵測

檢查週期性的模式:
1. 畫出原始時間序列,先用眼睛看
2. 計算星期幾的平均值:有沒有明顯的週模式?
3. 計算月份的平均值:有沒有年度週期?
4. 比較期間時,一律用 YoY 或同期比較,避免把趨勢和季節性混為一談

### 預測(簡單方法)

對商業分析師(不是資料科學家),用簡單直接的方法:

- **Naive 預測**:明天 = 今天。當作基準線。
- **Seasonal naive**:明天 = 上週/去年的同一天。
- **線性趨勢**:對歷史資料擬合一條線。只用於明顯線性的趨勢。
- **移動平均預測**:用往前的平均值當作預測。

**一定要說明不確定性**。給一個範圍,不要給單點估計:
- 「根據近 3 個月的趨勢,我們預期下個月有 1 萬到 1.2 萬個註冊」
- 不要說「下個月我們會剛好得到 11,234 個註冊」

**何時升級給資料科學家**:非線性趨勢、多重季節性、外部因素(行銷花費、假日),或是預測準確度會影響資源配置的時候。

## 離群值與異常偵測

### 統計方法

**Z-score 法**(用於常態分布的資料):
```python
z_scores = (df['value'] - df['value'].mean()) / df['value'].std()
outliers = df[abs(z_scores) > 3]  # More than 3 standard deviations
```

**IQR 法**(對非常態分布穩健):
```python
Q1 = df['value'].quantile(0.25)
Q3 = df['value'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df[(df['value'] < lower_bound) | (df['value'] > upper_bound)]
```

**百分位數法**(最簡單):
```python
outliers = df[(df['value'] < df['value'].quantile(0.01)) |
              (df['value'] > df['value'].quantile(0.99))]
```

### 處理離群值

絕對不要自動移除離群值。改成:

1. **調查**:這是資料錯誤、真正的極端值,還是不同的母體?
2. **資料錯誤**:修正或移除(例如負的年齡、年份是 1970 的時間戳記)
3. **真正的極端值**:保留,但考慮用穩健統計量(用中位數而不是平均數)
4. **不同的母體**:把它們分出來另外分析(例如企業客戶 vs. 中小企業客戶)

**報告你做了什麼**:「我們排除了 47 筆(0.3%)交易金額 >$50K 的紀錄,這些是大宗企業訂單,另外分析。」

### 時間序列異常偵測

偵測時間序列裡的異常值:

1. 計算預期值(移動平均或去年同期)
2. 計算與預期值的偏差
3. 標記超過閾值的偏差(通常是殘差的 2-3 個標準差)
4. 區分點異常(單一個異常值)和變化點(持續的偏移)

## 假設檢定基礎

### 何時使用

需要判斷觀察到的差異可能是真的,還是可能只是隨機機率造成時,用假設檢定。常見情境:

- A/B 測試結果:B 版本真的比 A 好嗎?
- 前後比較:產品的改動真的推動了指標嗎?
- 區隔比較:企業客戶的留存率真的比較高嗎?

### 框架

1. **虛無假設(H0)**:沒有差異(預設的假設)
2. **對立假設(H1)**:有差異
3. **選擇顯著水準(alpha)**:通常是 0.05(5% 的偽陽性機率)
4. **計算檢定統計量和 p 值**
5. **解讀**:如果 p < alpha,拒絕 H0(有真實差異的證據)

### 常見檢定

| Scenario | Test | When to Use |
|---|---|---|
| 比較兩組平均數 | t 檢定(獨立樣本) | 常態資料、兩組 |
| 比較兩組比例 | 比例的 z 檢定 | 轉換率、二元結果 |
| 比較成對的測量值 | 成對 t 檢定 | 同一批對象的前後比較 |
| 比較 3 組以上的平均數 | ANOVA | 多個區隔或版本 |
| 非常態資料、兩組 | Mann-Whitney U 檢定 | 偏態指標、次序資料 |
| 類別之間的關聯 | 卡方檢定 | 兩個類別變數 |

### 實務顯著性 vs. 統計顯著性

**統計顯著性**是指這個差異不太可能是機率造成的。

**實務顯著性**是指這個差異大到足以影響商業決策。

一個差異可能統計上顯著,但實務上沒有意義(大樣本很常見)。一定要報告:
- **效果量**:差異有多大?(例如「B 版本把轉換率提高了 0.3 個百分點」)
- **信賴區間**:合理的真實效果範圍是多少?
- **商業影響**:這換算成營收、使用者或其他商業用語是多少?

### 樣本數考量

- 樣本小的話,結果不可靠,即使 p 值顯著
- 比例的經驗法則:每組至少要 30 個事件才有基本的可靠度
- 要偵測小效果(例如轉換率變動 1%),可能每組需要數千個觀察值
- 樣本小就直說:「每組只有 200 個觀察值,偵測小於 X% 的效果能力有限」

## 什麼時候要對統計宣稱保持謹慎

### 相關不等於因果

發現相關性時,要明確考慮:
- **反向因果**:也許是 B 造成 A,不是 A 造成 B
- **干擾變數**:也許是 C 同時造成 A 和 B
- **巧合**:變數夠多的話,虛假相關不可避免

**你可以說**:「使用功能 X 的使用者留存率高 30%」
**沒有更多證據就不能說**:「功能 X 造成留存率高 30%」

### 多重比較問題

測試很多個假設時,有些會因為機率而「顯著」:
- 用 p=0.05 測 20 個指標,大約會有 1 個偽顯著
- 如果你看了很多區隔才找到一個不同的,要註明這件事
- 用 Bonferroni 校正(把 alpha 除以檢定次數)來調整多重比較,或報告總共跑了幾個檢定

### 辛普森悖論

彙總資料的趨勢,在資料分區隔之後可能會反轉:
- 一定要檢查結論在關鍵區隔裡是否也成立
- 例如:整體轉換率上升,但每個區隔的轉換率都下降,因為組成比例往轉換率較高的區隔偏移

### 倖存者偏差

你只能分析「倖存」到出現在資料集裡的對象:
- 分析活躍使用者,會忽略已經流失的人
- 分析成功的公司,會忽略失敗的公司
- 一定要問:「這個資料集裡少了誰?把他們納入會改變結論嗎?」

### 生態謬誤

彙總層級的趨勢不一定適用於個體:
- 「X 較高的國家,Y 也較高」**不代表**「X 較高的個人,Y 也較高」
- 把群體層級的發現套用到個案時要小心

### 錨定在特定數字上

小心虛假的精確度:
- 「下一季流失率會是 4.73%」暗示的確定性超過合理範圍
- 偏好用範圍:「根據歷史模式,我們預期流失率在 4-6% 之間」
- 適當四捨五入:「大約 5%」通常比「4.73%」更誠實

英文原文

---
name: statistical-analysis
description: Apply statistical methods including descriptive stats, trend analysis, outlier detection, and hypothesis testing. Use when analyzing distributions, testing for significance, detecting anomalies, computing correlations, or interpreting statistical results.
user-invocable: false
---

# Statistical Analysis Skill

Descriptive statistics, trend analysis, outlier detection, hypothesis testing, and guidance on when to be cautious about statistical claims.

## Descriptive Statistics Methodology

### Central Tendency

Choose the right measure of center based on the data:

| Situation | Use | Why |
|---|---|---|
| Symmetric distribution, no outliers | Mean | Most efficient estimator |
| Skewed distribution | Median | Robust to outliers |
| Categorical or ordinal data | Mode | Only option for non-numeric |
| Highly skewed with outliers (e.g., revenue per user) | Median + mean | Report both; the gap shows skew |

**Always report mean and median together for business metrics.** If they diverge significantly, the data is skewed and the mean alone is misleading.

### Spread and Variability

- **Standard deviation**: How far values typically fall from the mean. Use with normally distributed data.
- **Interquartile range (IQR)**: Distance from p25 to p75. Robust to outliers. Use with skewed data.
- **Coefficient of variation (CV)**: StdDev / Mean. Use to compare variability across metrics with different scales.
- **Range**: Max minus min. Sensitive to outliers but gives a quick sense of data extent.

### Percentiles for Business Context

Report key percentiles to tell a richer story than mean alone:

```
p1:   Bottom 1% (floor / minimum typical value)
p5:   Low end of normal range
p25:  First quartile
p50:  Median (typical user)
p75:  Third quartile
p90:  Top 10% / power users
p95:  High end of normal range
p99:  Top 1% / extreme users
```

**Example narrative**: "The median session duration is 4.2 minutes, but the top 10% of users spend over 22 minutes per session, pulling the mean up to 7.8 minutes."

### Describing Distributions

Characterize every numeric distribution you analyze:

- **Shape**: Normal, right-skewed, left-skewed, bimodal, uniform, heavy-tailed
- **Center**: Mean and median (and the gap between them)
- **Spread**: Standard deviation or IQR
- **Outliers**: How many and how extreme
- **Bounds**: Is there a natural floor (zero) or ceiling (100%)?

## Trend Analysis and Forecasting

### Identifying Trends

**Moving averages** to smooth noise:
```python
# 7-day moving average (good for daily data with weekly seasonality)
df['ma_7d'] = df['metric'].rolling(window=7, min_periods=1).mean()

# 28-day moving average (smooths weekly AND monthly patterns)
df['ma_28d'] = df['metric'].rolling(window=28, min_periods=1).mean()
```

**Period-over-period comparison**:
- Week-over-week (WoW): Compare to same day last week
- Month-over-month (MoM): Compare to same month prior
- Year-over-year (YoY): Gold standard for seasonal businesses
- Same-day-last-year: Compare specific calendar day

**Growth rates**:
```
Simple growth: (current - previous) / previous
CAGR: (ending / beginning) ^ (1 / years) - 1
Log growth: ln(current / previous)  -- better for volatile series
```

### Seasonality Detection

Check for periodic patterns:
1. Plot the raw time series -- visual inspection first
2. Compute day-of-week averages: is there a clear weekly pattern?
3. Compute month-of-year averages: is there an annual cycle?
4. When comparing periods, always use YoY or same-period comparisons to avoid conflating trend with seasonality

### Forecasting (Simple Methods)

For business analysts (not data scientists), use straightforward methods:

- **Naive forecast**: Tomorrow = today. Use as a baseline.
- **Seasonal naive**: Tomorrow = same day last week/year.
- **Linear trend**: Fit a line to historical data. Only for clearly linear trends.
- **Moving average forecast**: Use trailing average as the forecast.

**Always communicate uncertainty**. Provide a range, not a point estimate:
- "We expect 10K-12K signups next month based on the 3-month trend"
- NOT "We will get exactly 11,234 signups next month"

**When to escalate to a data scientist**: Non-linear trends, multiple seasonalities, external factors (marketing spend, holidays), or when forecast accuracy matters for resource allocation.

## Outlier and Anomaly Detection

### Statistical Methods

**Z-score method** (for normally distributed data):
```python
z_scores = (df['value'] - df['value'].mean()) / df['value'].std()
outliers = df[abs(z_scores) > 3]  # More than 3 standard deviations
```

**IQR method** (robust to non-normal distributions):
```python
Q1 = df['value'].quantile(0.25)
Q3 = df['value'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df[(df['value'] < lower_bound) | (df['value'] > upper_bound)]
```

**Percentile method** (simplest):
```python
outliers = df[(df['value'] < df['value'].quantile(0.01)) |
              (df['value'] > df['value'].quantile(0.99))]
```

### Handling Outliers

Do NOT automatically remove outliers. Instead:

1. **Investigate**: Is this a data error, a genuine extreme value, or a different population?
2. **Data errors**: Fix or remove (e.g., negative ages, timestamps in year 1970)
3. **Genuine extremes**: Keep them but consider using robust statistics (median instead of mean)
4. **Different population**: Segment them out for separate analysis (e.g., enterprise vs. SMB customers)

**Report what you did**: "We excluded 47 records (0.3%) with transaction amounts >$50K, which represent bulk enterprise orders analyzed separately."

### Time Series Anomaly Detection

For detecting unusual values in a time series:

1. Compute expected value (moving average or same-period-last-year)
2. Compute deviation from expected
3. Flag deviations beyond a threshold (typically 2-3 standard deviations of the residuals)
4. Distinguish between point anomalies (single unusual value) and change points (sustained shift)

## Hypothesis Testing Basics

### When to Use

Use hypothesis testing when you need to determine whether an observed difference is likely real or could be due to random chance. Common scenarios:

- A/B test results: Is variant B actually better than A?
- Before/after comparison: Did the product change actually move the metric?
- Segment comparison: Do enterprise customers really have higher retention?

### The Framework

1. **Null hypothesis (H0)**: There is no difference (the default assumption)
2. **Alternative hypothesis (H1)**: There is a difference
3. **Choose significance level (alpha)**: Typically 0.05 (5% chance of false positive)
4. **Compute test statistic and p-value**
5. **Interpret**: If p < alpha, reject H0 (evidence of a real difference)

### Common Tests

| Scenario | Test | When to Use |
|---|---|---|
| Compare two group means | t-test (independent) | Normal data, two groups |
| Compare two group proportions | z-test for proportions | Conversion rates, binary outcomes |
| Compare paired measurements | Paired t-test | Before/after on same entities |
| Compare 3+ group means | ANOVA | Multiple segments or variants |
| Non-normal data, two groups | Mann-Whitney U test | Skewed metrics, ordinal data |
| Association between categories | Chi-squared test | Two categorical variables |

### Practical Significance vs. Statistical Significance

**Statistical significance** means the difference is unlikely due to chance.

**Practical significance** means the difference is large enough to matter for business decisions.

A difference can be statistically significant but practically meaningless (common with large samples). Always report:
- **Effect size**: How big is the difference? (e.g., "Variant B improved conversion by 0.3 percentage points")
- **Confidence interval**: What's the range of plausible true effects?
- **Business impact**: What does this translate to in revenue, users, or other business terms?

### Sample Size Considerations

- Small samples produce unreliable results, even with significant p-values
- Rule of thumb for proportions: Need at least 30 events per group for basic reliability
- For detecting small effects (e.g., 1% conversion rate change), you may need thousands of observations per group
- If your sample is small, say so: "With only 200 observations per group, we have limited power to detect effects smaller than X%"

## When to Be Cautious About Statistical Claims

### Correlation Is Not Causation

When you find a correlation, explicitly consider:
- **Reverse causation**: Maybe B causes A, not A causes B
- **Confounding variables**: Maybe C causes both A and B
- **Coincidence**: With enough variables, spurious correlations are inevitable

**What you can say**: "Users who use feature X have 30% higher retention"
**What you cannot say without more evidence**: "Feature X causes 30% higher retention"

### Multiple Comparisons Problem

When you test many hypotheses, some will be "significant" by chance:
- Testing 20 metrics at p=0.05 means ~1 will be falsely significant
- If you looked at many segments before finding one that's different, note that
- Adjust for multiple comparisons with Bonferroni correction (divide alpha by number of tests) or report how many tests were run

### Simpson's Paradox

A trend in aggregated data can reverse when data is segmented:
- Always check whether the conclusion holds across key segments
- Example: Overall conversion goes up, but conversion goes down in every segment -- because the mix shifted toward a higher-converting segment

### Survivorship Bias

You can only analyze entities that "survived" to be in your dataset:
- Analyzing active users ignores those who churned
- Analyzing successful companies ignores those that failed
- Always ask: "Who is missing from this dataset, and would their inclusion change the conclusion?"

### Ecological Fallacy

Aggregate trends may not apply to individuals:
- "Countries with higher X have higher Y" does NOT mean "individuals with higher X have higher Y"
- Be careful about applying group-level findings to individual cases

### Anchoring on Specific Numbers

Be wary of false precision:
- "Churn will be 4.73% next quarter" implies more certainty than is warranted
- Prefer ranges: "We expect churn between 4-6% based on historical patterns"
- Round appropriately: "About 5%" is often more honest than "4.73%"