# Forecasting Application

This folder contains the out-of-sample inflation density forecasting exercise, forecast evaluation, and the comparison with Survey of Professional Forecasters (SPF) disagreement measures.

---

## Running for All Inflation Aggregates

All scripts are configured by default for **Core PCE (excl. food & energy)** but can be run for any of the four aggregates by changing the `load(...)` statement. Pre-processed data files for all four aggregates are included in `data/`:

| Aggregate | Data file |
|---|---|
| Core PCE (excl. food & energy) | `data_for_server_20250429_PCEx.mat` *(default)* |
| Headline PCE | `data_for_server_20250429_PCE.mat` |
| Core CPI (excl. food & energy) | `data_for_server_20250429_CPIx.mat` |
| Headline CPI | `data_for_server_20250429_CPI.mat` |

No other code changes are needed — output file names, evaluation metrics, and figure exports adapt automatically via the `infl_id` variable.

---

## Main Scripts & Execution Order

Run in the order listed below. Step 1 is the most computationally intensive; steps 2–3 use pre-computed results.

### 1. `MAIN_forecasting_exercise_pipe.m` — Recursive Forecasting

| Aspect | Detail |
|---|---|
| **Loads** | `data/data_for_server_20250429_PCEx.mat` (default; swap for other aggregates) |
| **Method** | Expanding-window recursive forecasting (first 50 % of the sample as burn-in). Three models estimated per horizon × vintage: **(1)** Bayesian Density Regression (DR) via `VB_BDR_pipe`, **(2)** Quantile Regression (QR, Mitchell-style) via `VBTVPQR`, **(3)** UC-SV via `VB_DVS` |
| **Horizons** | *h* = 1, …, 12 quarters ahead |
| **Evaluation metrics** | CRPS (energy & PWM), quantile scores (τ = 0.05–0.95), MSPE (mean & median), PITs, tail probability exceedances (2 % and 5 %) |
| **Saves** | `results/<SERIES>_H_5_priorM_2_priorK_0_cluster_.mat` (e.g. `PCEPILFE_H_5_…_.mat`) |
| **LaTeX output** | Prints evaluation tables (Rossi–Sekhposyan tests, quantile scores, RMSE, DM tests) as LaTeX to the console |
| **Uses `parfor`** | Yes — over the evaluation samples. Parallel Computing Toolbox recommended. |

### 2. `quantilechart.m` — Quantile Chart (Online Appendix, Figure E2)

| Aspect | Detail |
|---|---|
| **Loads** | `data/data_for_server_20250429_PCEx.mat` |
| **Processing** | Full-sample estimation for all three models across *h* = 1…12; extracts 10th, 50th, 90th percentile paths |
| **Generates** | `OS_Figure3_quantile_chart.pdf` and `.eps` |
| **Saves** | Optionally `results/quantile_chart.mat` (uncomment the `save` line) |

### 3. `SPF_comparison.m` — Cumulative Quantile Scores vs. SPF Disagreement (Main Paper, Figure 2)

| Aspect | Detail |
|---|---|
| **Loads** | `data/SPFmicrodata.xlsx` (Sheets: `COREPCE`, `CPI`) — individual forecaster-level SPF micro data; `results/PCEPILFE_H_5_priorM_2_priorK_0_cluster_.mat` — pre-computed forecasting results |
| **Processing** | Computes cross-sectional dispersion (IQR, MAD, STD), skewness (moment & Bowley), kurtosis (moment & Moors) from SPF point forecasts; overlays with cumulative quantile score differentials |
| **Generates** | `Main_Figure2_CQS_10_.{pdf,eps}`, `Main_Figure2_CQS_50_.{pdf,eps}`, `Main_Figure2_CQS_90_.{pdf,eps}` |

---

## Data Files (`data/`)

| File | Description |
|---|---|
| `data_for_server_20250429_PCEx.mat` | Pre-processed Core PCE excl. food & energy (default) |
| `data_for_server_20250429_PCE.mat` | Pre-processed headline PCE |
| `data_for_server_20250429_CPI.mat` | Pre-processed headline CPI |
| `data_for_server_20250429_CPIx.mat` | Pre-processed Core CPI |
| `FREDQD_new.xlsx` | FRED-QD quarterly database |
| `SPFmicrodata.xlsx` | Survey of Professional Forecasters micro data |
| `data.xlsx`, `indices.xlsx` | Additional raw series and commodity/financial indices |
| `load_data.m` | Helper function to reconstruct `.mat` from raw xlsx files |

## Pre-Computed Results (`results/`)

| File | Contents |
|---|---|
| `PCEPILFE_H_5_priorM_2_priorK_0_cluster_.mat` | Forecasting output for Core PCE excl. food & energy |
| `CPILFESL_H_5_priorM_2_priorK_0_cluster_.mat` | Forecasting output for Core CPI |
| `CPIAUCSL_H_5_priorM_2_priorK_0_cluster_.mat` | Forecasting output for headline CPI |
| `PCEPI_H_5_priorM_2_priorK_0_cluster_.mat` | Forecasting output for headline PCE |
| `quantile_chart.mat` | Saved quantile fan chart data |

---

## Key Estimation Settings

| Parameter | Value | Meaning |
|---|---|---|
| `H_max` | 5 | Max mixture components |
| `init_method` | `'cluster'` | K-means initialisation |
| `prior_mixing` | 2 | Horseshoe prior |
| `prior_kernel` | 0 | Normal prior |
| `max_iter` | 10 000 | Max VB iterations |
| `tol` | 1e-6 | Convergence tolerance |
| `R` | 1 000 | Posterior simulation draws |
| `first_per` | 0.5 | Burn-in fraction of the sample |
| `nhor` | 12 | Forecast horizons evaluated |

---

## Active Local Dependencies (`functions/`)

- Core estimators: `VB_BDR_pipe`, `VBTVPQR`, `VB_DVS`
- Sampling: `f_sample_sel`, `f_simul_VB`
- Prediction: `f_predict`, `f_predict2`
- Evaluation: `f_CRPS_nrg`, `f_CRPS_pwm`, `dmtest`, `rs_test_adj`, `significanceStarsMatrix`
- QR helpers: `QR1_adjusted`
- External: `RossiSekhposyan/` (correct specification tests), `VulnerableGrowth/` (skew-*t* interpolation)

---

## Computational Notes

- **`MAIN_forecasting_exercise_pipe.m`** completes in roughly **20 minutes** per aggregate on a modern workstation, thanks to the efficient variational Bayes implementation.
- **`quantilechart.m`** runs a full-sample (non-recursive) estimation and takes roughly **5–10 minutes**.
- **`SPF_comparison.m`** is fast (< 1 minute) since it only processes pre-computed results and SPF data.
- Repeating all three scripts for all four aggregates takes approximately **1–1.5 hours** in total.
