## Feature Engineering & Generation

Features comprise the backbone of this solution. Both the sparsity of data and the temporal nature of historical actions that must be strictly followed for predicting future engagement lead to variations in the features built into this solution, such as rolling windows for past engagement actions, cross-channel patterns, and trend detection to enrich sparse signals.

All features are computed per HCP × channel using data up to T−1 only. The same logic is applied at inference time - no train/serve skew.

### 1. HCP Profile Features (Static)

- `segment` (e.g., high_value / medium_value / low_value derived from Rx quantiles)
- `specialty` (e.g., Cardiology, Dermatology, Oncology)
- `practice_type` (e.g., hospital, private_practice, clinic, cancer_center)
- `account_tenure` (years since first recorded engagement)
- `channel_type` (the channel being scored for this HCP × channel pair)

### 2. Rolling Window Engagement Features (Time-Aware)

Computed per HCP × channel, respecting train/test temporal order.

**Attempt and success counts**
- `channel_attempts_1w`, `channel_attempts_4w`, `channel_attempts_12w`
- `channel_successes_1w`, `channel_successes_4w`, `channel_successes_12w`
- `total_channel_attempt` (lifetime)
- `total_successful_channel_engagement` (lifetime)

**Success rates (smoothed to avoid 0/0 division)**
- `sr_4w` = (successes_last_4w + α) / (attempts_last_4w + β), α=1, β=2
- `sr_12w` = same logic over 12-week window
- `trend_score` = sr_4w − sr_12w (positive = improving, negative = declining)

**Recency**
- `days_since_last_channel_attempt`
- `days_since_last_channel_success`
- `previous_engagement_channel`

**Cross-channel features**
- `total_attempts_4w`, `total_attempts_12w` (all channels combined)
- `total_successes_4w`, `total_successes_12w` (all channels combined)
- `successes_other_channels_12w` (successes on channels other than current)

### 3. Rx / Prescribing Features

- `prescriptions_last_week`
- `prescriptions_last_4w_sum`
- `rx_trend`: compare sum(Rx last 4w) vs sum(Rx prior 4w), categorised as increasing / stable / decreasing

### Feature Pipeline Summary

| Step | What happens |
|---|---|
| Parse & align | Dates parsed, week/month/year extracted, engagement and Rx datasets aligned to the same weekly grain |
| Consolidate | Engagement History + HCP Master + KPI Metrics joined to create HCP × channel grain |
| Rolling windows | Window recipes compute all time-aware features per HCP × channel, strictly using only data before each week |
| Final dataset | Feature families combined, HCP × channel combinations with no target label dropped, train/test split applied |

**Critical detail**: For the test set, all features must be computed using only data before the test period to prevent leakage. The same computation logic is reused at scoring time on the inference grid.