Multi-model price forecasting, anomaly detection, price elasticity, and seasonality analysis
Project description
PriceProphet
Multi-model price forecasting, anomaly detection, price elasticity, and seasonality analysis for Python.
Installation
pip install priceprophet
Quick Start
from priceprophet import PriceProphet
import pandas as pd
pp = PriceProphet()
df = pd.DataFrame({
'date': pd.date_range('2025-01-01', periods=180),
'price': [100 + i*0.3 + (i % 7)*2 for i in range(180)]
})
# Auto-select best model
forecast = pp.forecast(df, periods=30, model="auto")
print(forecast.head())
# Detect price anomalies
anomalies = pp.detect_anomalies(df)
print(anomalies[anomalies['is_anomaly']])
# Market shock simulation
impact = pp.simulate_impact(current_price=150.0, shock_magnitude=0.15, shock_type="competitor")
print(impact)
Features at a Glance
| Feature | Description |
|---|---|
| 4 Forecasting Models | Linear, Ridge, Polynomial (deg 1–5), EMA |
| Auto Model Selection | Picks the best model via train/test split |
| Model Comparison | Compare all models — MAE, RMSE, R² table |
| Price Elasticity | Arc and point elasticity with revenue simulation |
| Seasonality Detection | Weekly, monthly, FFT-based dominant cycle |
| Anomaly Detection | Z-score with configurable threshold |
| Shock Simulation | competitor / supply / demand / regulation shocks |
Forecasting Models
from priceprophet import PriceProphet
import pandas as pd, numpy as np
pp = PriceProphet()
df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=365),
'price': 100 + np.cumsum(np.random.randn(365) * 0.5)
})
# Linear regression (fastest, good for stable trends)
fc_linear = pp.forecast(df, periods=30, model="linear")
# Ridge (L2-regularized, robust to outliers)
fc_ridge = pp.forecast(df, periods=30, model="ridge", alpha=0.5)
# Polynomial (captures curve, degree=3 for S-curves)
fc_poly = pp.forecast(df, periods=30, model="polynomial", degree=3)
# EMA (exponential moving average, follows recent trend)
fc_ema = pp.forecast(df, periods=30, model="ema", span=14)
# Auto-select: runs comparison, picks lowest MAE model
fc_auto = pp.forecast(df, periods=30, model="auto")
print(fc_auto[['Date', 'Predicted_Value', 'Lower_Bound', 'Upper_Bound']].tail())
Model Comparison
comparison = pp.compare_models(df, cv_split=0.8)
print(comparison)
# model MAE RMSE R2
# 0 ridge 1.832 2.341 0.972
# 1 linear 1.944 2.501 0.969
# 2 polynomial_deg2 2.103 2.788 0.961
# 3 ema 2.891 3.672 0.943
Price Elasticity
from priceprophet import PriceElasticity
import pandas as pd
prices = pd.Series([10.0, 11.0, 12.0, 11.5, 13.0, 12.5])
demands = pd.Series([100, 90, 80, 88, 70, 83])
pe = PriceElasticity()
result = pe.calculate(prices, demands)
print(result)
# Price Elasticity: -1.247
# Interpretation: Elastic — demand changes more than proportionally to price
# A 10% price increase → 12.5% demand drop
# Revenue Impact: Raising price DECREASES revenue
# Simulate a specific price change
sim = pe.simulate_price_change(
current_price=12.0,
current_demand=80,
price_change_pct=0.10, # +10%
elasticity=result.elasticity,
)
print(sim)
# {'new_price': 13.2, 'new_demand': 69.8, 'revenue_change_pct': -2.7, ...}
Seasonality Detection
from priceprophet import SeasonalityDetector
import pandas as pd, numpy as np
df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=365),
'price': [100 + 20 * np.sin(2 * np.pi * i / 7) for i in range(365)]
})
sd = SeasonalityDetector()
result = sd.detect(df)
print(result)
# Weekly seasonality : Yes
# Monthly seasonality : No
# Peak day of week : Wednesday
# Dominant cycle : ~7 days
Anomaly Detection
df_with_spike = df.copy()
df_with_spike.loc[45, 'price'] = 999 # inject spike
anomalies = pp.detect_anomalies(df_with_spike, threshold=2.5)
spikes = anomalies[anomalies['is_anomaly']]
print(f"Found {len(spikes)} anomalies")
print(spikes[['date', 'price', 'z_score']])
Market Shock Simulation
for shock_type in ["competitor", "supply", "demand", "regulation"]:
result = pp.simulate_impact(
current_price=100.0,
shock_magnitude=0.20,
shock_type=shock_type
)
print(f"{shock_type:12s}: price {result['new_price']:.1f} | "
f"recovery {result['estimated_recovery_days']} days")
# competitor : price 86.0 | recovery 14 days
# supply : price 76.0 | recovery 30 days
# demand : price 82.0 | recovery 21 days
# regulation : price 70.0 | recovery 60 days
Full Analysis Pipeline
from priceprophet import PriceProphet
import pandas as pd, numpy as np
pp = PriceProphet()
# Generate realistic price series
np.random.seed(42)
n = 365
trend = np.linspace(100, 140, n)
weekly = 8 * np.sin(2 * np.pi * np.arange(n) / 7)
noise = np.random.randn(n) * 2
prices = trend + weekly + noise
df = pd.DataFrame({'date': pd.date_range('2025-01-01', periods=n), 'price': prices})
# 1. Seasonality
season = pp.seasonality(df)
print("Peak day:", season.peak_day)
# 2. Best model forecast
fc = pp.forecast(df, periods=90, model="auto")
print(f"90-day forecast: {fc['Predicted_Value'].mean():.2f} avg")
# 3. Anomalies
anom = pp.detect_anomalies(df, threshold=2.5)
print(f"Anomalies: {anom['is_anomaly'].sum()}")
# 4. Model comparison
print(pp.compare_models(df).to_string(index=False))
License
MIT — Teyfik Öz
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
priceprophet-1.1.0.tar.gz
(14.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file priceprophet-1.1.0.tar.gz.
File metadata
- Download URL: priceprophet-1.1.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bee3caf54aec724bac5e50e5790722ae9ffea3c4e2c81f16fbfb3723a51b2c3
|
|
| MD5 |
7585828273513907b58c3a0c29df47ba
|
|
| BLAKE2b-256 |
d639c9b4680ea4e507ac36fd11b7ce423ae4c9e50a6c7c557f6282e7ec812215
|
File details
Details for the file priceprophet-1.1.0-py3-none-any.whl.
File metadata
- Download URL: priceprophet-1.1.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7b988567a6c4f7334f107c2f07dd0f1c923ab609ed654d8f0a56537c9bdf3ec
|
|
| MD5 |
085869b1bcddb482a557312e580b56ab
|
|
| BLAKE2b-256 |
194239281d8e7e801acbf353b0170d05a07b118f975138376f60808341d6d788
|