LSTM time series forecasting for pension contributions using PyTorch with FastAPI and Streamlit
Type: Sequence Modeling | Algorithm: LSTM (PyTorch) | Domain: Financial Forecasting
Cash flow unpredictability creates significant challenges for:
Solution: Deploy an LSTM-based forecasting model to predict monthly contribution amounts 6 months in advance, enabling proactive decision-making.
monthly_contributions_clp - Monthly contribution amounts in CLPnew_customers - New customer acquisitionsmarket_index - Market performance indicatorunemployment_rate - Chile unemployment rate (%)gdp_growth - GDP growth rate (%)Type: Stacked LSTM
Layers: 2
Hidden Units: 128
Dropout: 0.2
Sequence Length: 12 months (lookback window)
Forecast Horizon: 6 months (direct multi-step)
Output: 6 values (one per forecast month)
# Clone repository
cd 07-lstm-time-series-forecasting
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
python data/generate_synthetic_data.py
python scripts/train.py
# Start FastAPI server
bash scripts/run_api.sh
# API will be available at http://localhost:8000
# API docs at http://localhost:8000/docs
# Start Streamlit dashboard
bash scripts/run_dashboard.sh
# Dashboard will be available at http://localhost:8501
Generate 6-month forecast with confidence intervals.
Request:
{
"historical_data": {
"date": ["2022-01-01", "2022-02-01", ...],
"monthly_contributions_clp": [150000000, 155000000, ...],
"new_customers": [520, 535, ...],
"market_index": [1050, 1060, ...],
"unemployment_rate": [0.072, 0.071, ...],
"gdp_growth": [0.031, 0.032, ...]
}
}
Response:
{
"forecast": [158000000, 162000000, ...],
"confidence_intervals": {
"lower": [152000000, 156000000, ...],
"upper": [164000000, 168000000, ...]
},
"forecast_dates": ["2023-01-01", "2023-02-01", ...],
"model_info": {
"model_type": "LSTM",
"sequence_length": 12,
"forecast_horizon": 6,
"confidence_level": 0.95
}
}
Scenario analysis by adjusting economic variables.
Request:
{
"adjustments": {
"unemployment_rate_delta": 0.01, # +1%
"gdp_growth_delta": -0.005, # -0.5%
"market_index_multiplier": 0.95 # -5%
}
}
Forecast Explorer
Scenario Analysis
Model Performance
Data Explorer
# Run all tests
pytest
# Run with coverage
pytest --cov=src --cov-report=html
# Run specific test file
pytest tests/test_model.py
# Build and start all services
cd docker
docker-compose up --build
# Services:
# - API: http://localhost:8000
# - Dashboard: http://localhost:8501
# API only
docker build -f docker/Dockerfile.api -t lstm-forecast-api .
docker run -p 8000:8000 lstm-forecast-api
# Dashboard only
docker build -f docker/Dockerfile.dashboard -t lstm-forecast-dashboard .
docker run -p 8501:8501 lstm-forecast-dashboard
07-lstm-time-series-forecasting/
βββ config.yaml # Configuration file
βββ requirements.txt # Dependencies
βββ README.md # This file
βββ data/
β βββ generate_synthetic_data.py # Data generation script
β βββ raw/ # Raw data
β βββ processed/ # Train/test/val splits
β βββ external/ # External data sources
βββ notebooks/ # Jupyter notebooks
βββ src/
β βββ models/ # LSTM model & training
β βββ features/ # Feature engineering
β βββ api/ # FastAPI endpoints
β βββ visualization/ # Plotting utilities
β βββ utils/ # Helper functions
β βββ dashboard/ # Streamlit app
βββ docker/ # Docker files
βββ scripts/ # Execution scripts
βββ tests/ # Unit tests
βββ docs/ # Documentation
βββ models/ # Saved model checkpoints
Instead of recursive prediction (where errors compound), our model outputs all 6 months directly, improving accuracy.
By enabling dropout during inference and running multiple predictions, we estimate uncertainty quantiles without additional models.
MIT License - feel free to use this project for learning and development.
Created as part of a 10-project ML/DL portfolio showcasing end-to-end machine learning systems.
Technologies: Python, PyTorch, FastAPI, Streamlit, Docker
Domain: Financial Time Series Forecasting
Date: 2024