File size: 4,319 Bytes
e9ddb89
558db1e
 
 
 
e9ddb89
 
 
558db1e
e9ddb89
558db1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: Portfolio Engine
emoji: 📈
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---
# Portfolio Engine

An institutional-grade portfolio allocation and backtesting engine for quantitative finance research.

## Features

- **Walk-Forward Cross-Validation**: Rigorous expanding window out-of-sample backtesting to prevent look-ahead bias and overfitting.
- **Advanced Asset Allocation Models**:
  - CAPM (Capital Asset Pricing Model)
  - Black-Litterman (Market Equilibrium Prior)
  - Bayesian Shrinkage (James-Stein)
  - Fama-French 3-Factor + Momentum
  - Machine Learning Stacking Ensemble (XGBoost + ElasticNet + Ridge Meta-Learner)
  - End-to-End Differentiable Optimization (SPO+)
  - Regime-Adaptive Factor Blend
- **Deep Learning Forecasting**:
  - Noise-Filtered Transformer with Conv1D temporal smoothing
  - Cross-asset training with sliding window sequence datasets
  - Integrated into the meta-learner ensemble pipeline
- **Alternative Data Ingestion**:
  - Real-time options flow: Put/Call volume ratios and Implied Volatility skew
  - Seamless injection into the ML feature generation pipeline
- **Three Allocation Engines**:
  - Mean-Variance Optimization (CVXPY) with 7-stage constraint relaxation cascade
  - Hierarchical Risk Parity (HRP) with tax-aware blending
  - Exact True Risk Parity (ERC) via Spinu logarithmic barrier formulation
- **Cardinality Constraints**: Iterative pruning heuristic for maximum-asset-count limits
- **Econometric Validation & Statistical Rigor**: 
  - Model Confidence Sets (MCS) for screening optimal models
  - Diebold-Mariano Tests for predictive accuracy vs benchmarks
  - Probabilistic Sharpe Ratio (PSR) for non-normality
  - Deflated Sharpe Ratio (DSR) to account for multiple testing bias
  - Christoffersen Conditional Coverage for dynamic risk limit validation
- **Robust Execution**:
  - Differentiable optimization via `cvxpylayers`
  - Realistic transaction cost and Almgren-Chriss market impact modeling
  - Dynamic API rate-limiting for automated data pipeline tasks
  - Structured JSON logging for production monitoring

## Documentation

Comprehensive documentation is available in the `docs/` directory:

| Document | Description |
|----------|-------------|
| [**ARCHITECTURE.md**](docs/ARCHITECTURE.md) | **Master reference** — complete codebase walkthrough with file map |
| [PIPELINE.md](docs/PIPELINE.md) | Pipeline stages, data flow, and execution model |
| [MODELS.md](docs/MODELS.md) | Deep dive into all return forecasting and risk models |
| [ALLOCATION_ENGINES.md](docs/ALLOCATION_ENGINES.md) | Mean-Variance, HRP, and Exact Risk Parity engines |
| [RELAXATION_CASCADE.md](docs/RELAXATION_CASCADE.md) | 7-stage constraint relaxation for infeasible solves |
| [TESTS.md](docs/TESTS.md) | Test suite, mocking strategy, and property-based testing |
| [OUTPUT.md](docs/OUTPUT.md) | Output artefacts and directory structure |
| [DEPLOY.md](docs/DEPLOY.md) | Docker, Helm, CI/CD, and Kubernetes deployment |
| [RESEARCH.md](docs/RESEARCH.md) | Experimental modules: PID controller, Dreamer RL |

## Usage

Configure the engine via `config.py` and execute the core pipeline:
```bash
python main.py
```

### Reporting
Run the integration testing suite using pytest:
```bash
pytest tests/test_simulate.py -v
```

The engine automatically constructs comprehensive HTML reports displaying true equity curves, allocation drift, and marginal risk attributions.

## Deployment

The engine supports robust Kubernetes deployments via Helm.

### Deploying via Helm

1. Configure your API keys in `deploy/helm/portfolio-engine/values.yaml` or pass them via command line.
2. Install the chart:
```bash
helm install portfolio-engine ./deploy/helm/portfolio-engine --set env[0].value="YOUR_API_KEY"
```

3. Ensure the `DATABASE_URL` is configured if you're using a managed PostgreSQL instance for the database layer.

## Results

Validation tests confirm the mathematical stability of the solver across all core models. The walk-forward CV confirms the strategy returns are stable and survive multiple testing deflations (DSR).

## Safety & Rate Limiting

The data fetching module incorporates thread-safe rate limits (max 2 requests per second globally) when fetching assets to ensure safety during automated deployments and prevent API bans.