End-to-end Prescriptive Analytics using Machine Learning
Simulates price elasticity and competitor data to prescribe optimal pricing strategies for revenue maximization.
๐ฎ Live Demo โข Features โข Installation โข Usage
The Retail Price Optimization Engine is a machine learning solution designed to shift retailers from reactive pricing to Prescriptive Analytics. Unlike traditional forecasting, this system doesn't just predict sales it calculates the Optimal Price Point.
By leveraging Random Forest Regression and other ensemble methods, the system learns the non-linear relationship between Price and Demand (Elasticity), factoring in competitor pricing to recommend strategies that maximize Total Revenue.
Most retailers still rely on gut feeling or simple cost-plus formulas for pricing. This leads to:
- Leaving money on the table (pricing too low)
- Losing customers (pricing too high)
This engine uses actual data to find that sweet spot where revenue is maximized.
- Elasticity Modeling: Captures non-linear demand response to price changes
- Competitor Benchmarking: Real-time tracking of price gaps against competitors
- Revenue Maximization: Simulation algorithm tests 50+ price points per SKU
- Confidence Intervals: Uncertainty estimation for reliable recommendations
- Single & Batch Optimization: Optimize one product or entire categories
- Real-time Inference: Sub-second prediction for pricing scenarios
- Visual Analytics: Interactive Plotly charts showing Revenue Curves
- Export to Excel/CSV: Download optimization results
- Strategic Insights: Automated recommendations (raise/lower/maintain price)
- 75 Unit Tests: Comprehensive test coverage
- Input Validation: Robust error handling and data validation
- CI/CD Pipeline: Automated testing with GitHub Actions
- Docker Support: Easy deployment with containerization
graph LR
A[Raw Data CSV] --> B(Feature Engineering)
B --> C{ML Model}
C --> D[Price Simulation]
D --> E[Optimization Engine]
E --> F[Streamlit Dashboard]
Pipeline Steps:
- Data Ingestion - Load historical sales and competitor pricing data
- Feature Engineering - Create features like price differences/ratios vs competitors
- Model Training - Random Forest learns demand response to price changes
- Price Simulation - Test different price points (50+ scenarios per product)
- Optimization - Pick the price with highest predicted revenue
- Dashboard - Visualize everything in the Streamlit app
retail-price-optimization/
โโโ .github/
โ โโโ workflows/
โ โโโ test.yml # CI/CD pipeline
โโโ data/
โ โโโ processed/ # Cleaned data for app demo
โ โโโ raw/ # (Gitignored) Raw source data
โโโ models/
โ โโโ pricing_model.pkl # Trained Random Forest model
โโโ notebooks/
โ โโโ 01_eda_pricing_logic.ipynb
โ โโโ 02_feature_engineering.ipynb
โ โโโ 03_modeling.ipynb
โ โโโ 04_price_optimization.ipynb
โโโ tests/
โ โโโ test_app.py # Validation & elasticity tests
โ โโโ test_model_training.py # Model tests
โ โโโ test_utils.py # Utility function tests
โโโ .gitignore
โโโ app.py # Main Streamlit Application
โโโ config.py # Centralized configuration
โโโ model_training.py # Model training with CV & tuning
โโโ utils.py # Utility functions & validation
โโโ requirements.txt # Production dependencies
โโโ requirements-dev.txt # Development dependencies
โโโ Dockerfile # Container configuration
โโโ docker-compose.yml # Docker Compose setup
โโโ README.md # Documentation
| File | Description |
|---|---|
app.py |
Main Streamlit dashboard with batch optimization, export |
config.py |
Centralized configuration with environment variable support |
model_training.py |
Train models with cross-validation & hyperparameter tuning |
utils.py |
Validation functions, elasticity calculation, formatting |
- Python 3.9 or higher
- Git
# Clone the repository
git clone https://github.com/mugnihidayah/retail-price-optimization.git
cd retail-price-optimization
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Mac/Linux
# Install dependencies
pip install -r requirements.txt
# Run the app
streamlit run app.pyThe app will open in your browser at http://localhost:8501.
streamlit run app.py- Open the app in your browser
- Select a product category from the sidebar
- Choose Single Product or Batch Optimization mode
- Click "Run AI Optimization"
- Review results and download recommendations
- Revenue Curve: How revenue changes at different price points
- Optimal Price: AI-recommended price with confidence interval
- Revenue Uplift: Potential revenue increase
- Competitor Comparison: Your position vs the market
- Action: Should you increase, decrease, or maintain price?
# Install dev dependencies
pip install -r requirements-dev.txt
# Run tests
pytest tests/ -v
# Run with coverage
pytest tests/ -v --cov=. --cov-report=html| Test File | Tests | Coverage |
|---|---|---|
test_utils.py |
33 | Utility functions, validation |
test_model_training.py |
18 | PricingModel class |
test_app.py |
24 | Validation, elasticity |
| Total | 75 | ~85% |
# Build image
docker build -t retail-price-optimization .
# Run container
docker run -p 8501:8501 retail-price-optimizationdocker-compose upAccess the app at http://localhost:8501.
| Metric | Performance | Notes |
|---|---|---|
| Model Rยฒ | 0.62 | Solid for noisy retail data |
| Revenue Uplift | 15-20% | On underpriced products |
| Inference Speed | <0.5s | Real-time capable |
| Test Coverage | 75 tests | All passing |
We found products with inelastic demand where customers don't care about small price changes. For these items, we could increase prices significantly (up to 98% revenue boost!) without losing customers.
- 15-20% more revenue on average (just by pricing smarter)
- Instant recommendations (no more spreadsheet gymnastics)
- Better competitive positioning (know where you stand)
- Clear action items (the model tells you what to do)
pip install -r requirements-dev.txt# Basic training
python model_training.py
# With hyperparameter tuning
python model_training.py --tune# Linting
flake8 .
# Formatting
black .
# Type checking
mypy .- Fork the repo
- Create a branch (
git checkout -b feature/cool-stuff) - Make your changes
- Run tests (
pytest tests/ -v) - Push and open a PR
- Add Neural Network model option
- Build REST API with FastAPI
- Add A/B testing capability
- Improve visualizations
- Add more competitor data sources
MIT License. Do whatever you want with it.
โญ Star this repo if you find it useful!