# Theil-Sen Regression

**Theil-Sen Regression** is a non-parametric alternative to Simple Linear Regression. It estimates the slope as the **median of all pairwise slopes** between data points, making it highly robust to outliers.

---

## 📌 When to Use
- One continuous predictor, one continuous outcome
- Data contains extreme outliers
- Residuals are clearly non-normal
- Robust slope estimate is preferred over OLS

> ✅ No normality assumption required.

---

## 📐 Method

The slope is estimated as:

$$
\hat{\beta}_1 = \text{median}\left(\frac{Y_j - Y_i}{X_j - X_i}\right) \quad \forall\, i < j
$$

The intercept is:

$$
\hat{\beta}_0 = \text{median}(Y) - \hat{\beta}_1 \cdot \text{median}(X)
$$

---

## 🔬 What StatInsight Reports
- Estimated slope (median of pairwise slopes)
- 95% CI for slope
- Intercept
- Kendall's τ with p-value (measure of monotone association)
- Significance: whether the 95% CI for slope excludes 0

---

## 🧪 Assumptions
- Monotone (not necessarily linear) relationship between X and Y
- Independent observations
- No requirement for normality of residuals

---

## 🧠 Interpretation
- **Slope CI excludes 0**: statistically significant directional relationship
- **Kendall's τ**: strength and direction of the monotone association (range −1 to +1)
- **Robust to outliers**: single extreme values do not distort the slope estimate

---

## 🐍 Python Implementation

```python
from scipy.stats import theilslopes, kendalltau

res = theilslopes(y, x, alpha=0.05)
print(f"Slope: {res.slope:.4f}  CI: [{res.low_slope:.4f}, {res.high_slope:.4f}]")

tau, p = kendalltau(x, y)
print(f"Kendall tau={tau:.3f}  p={p:.4f}")
```

---

## 📎 Related Tests

[Simple Linear Regression](simple-linear.md) — parametric counterpart

[Spearman Correlation](/statistics/correlation/spearman.md) — non-parametric monotone association
