Skip to main content

A practical, transparent demand forecasting method that treats each calendar month independently.

Project description

MonthWise Forecast

A practical, transparent demand forecasting method built for real-world messy data.

MonthWise was born from frustration with traditional forecasting models (ETS, Prophet, ARIMA) that fail silently across hundreds of materials with different history lengths, sparse data, and seasonal shifts. Instead of fitting one model to an entire time series, MonthWise treats each calendar month as its own independent forecasting problem.

Installation

pip install monthwise-forecast

Or install from source:

git clone https://github.com/your-repo/monthwise-forecast.git
cd monthwise-forecast
pip install .

Quick Start

from monthwise_forecast import MonthWiseForecaster
import pandas as pd

# Create monthly data
dates = pd.date_range("2023-01-01", periods=24, freq="MS")
values = [10, 12, 8, 15, 20, 18, 11, 14, 9, 16, 22, 19,
          12, 15, 10, 17, 24, 21, 13, 16, 11, 18, 26, 23]
series = pd.Series(values, index=dates)

# Forecast next 18 months
f = MonthWiseForecaster()
result = f.forecast(series)
print(result)

# Forecast 36 months in 6-month chunks
f = MonthWiseForecaster(forecast_horizon=36)
chunks = f.forecast_chunked(series, chunk_size=6)
print(chunks)

How It Works

For each future month, MonthWise counts how many same-calendar-month data points exist in the history and applies the appropriate rule:

Rule 1: One Historical Value

When only one same-month value exists, apply incremental growth:

  • 1st future occurrence: value × (1 + growth_step)
  • 2nd future occurrence: value × (1 + growth_step × 2)
  • 3rd future occurrence: value × (1 + growth_step × 3)

Default growth_step is 0.10 (10%). This is the only configurable rule parameter.

Example (growth_step=0.10, March 2025 = 10):

Future Month Calculation Forecast
March 2026 10 × 1.10 11
March 2027 10 × 1.20 12
March 2028 10 × 1.30 13

Rule 2: Two Historical Values

Calculate the percentage change between the two values, cap at ±50%, and compound forward.

Example (April 2024 = 10, April 2025 = 12, pct_change = +20%):

Future Month Calculation Forecast
April 2026 12 × 1.20 14
April 2027 14.4 × 1.20 17

If the older value is 0, falls back to Rule 1 logic.

Rule 3: Three or More Historical Values

Fit a simple linear regression (year vs value) and project the line forward.

  • Capped at 1.5× the maximum historical value for that month
  • Floored at 0

Global Rules

  • Floor at 0: No forecast is ever negative
  • Float math: All calculations use floats; rounding to nearest integer happens only at the very end
  • Independent months: Each calendar month determines its own rule based on its own data count
  • Minimum 12 months: Will not forecast with less than 12 months of history

API Reference

MonthWiseForecaster(forecast_horizon=18, rule1_growth_step=0.10)

Parameters:

  • forecast_horizon (int): Months to forecast. Default: 18
  • rule1_growth_step (float): Growth increment per step for Rule 1. Default: 0.10

.forecast(series) → pd.DataFrame

Forecast a single time series. Input: pd.Series with datetime index. Output: DataFrame with month_start and forecast columns.

.forecast_chunked(series, chunk_size=6) → pd.DataFrame

Forecast and aggregate into chunks. Output: Single-row DataFrame with columns like 1-6, 7-12, etc.

.forecast_batch(df, group_cols, date_col, value_col) → pd.DataFrame

Forecast multiple groups in a long-format DataFrame. Groups with < 12 months are skipped.

.forecast_batch_chunked(df, group_cols, date_col, value_col, chunk_size=6) → pd.DataFrame

Forecast multiple groups and return chunked aggregates.

Requirements

  • Python >= 3.8
  • numpy >= 1.20
  • pandas >= 1.3
  • scipy >= 1.7

License

MIT

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

monthwise_forecast-1.0.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

monthwise_forecast-1.0.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file monthwise_forecast-1.0.0.tar.gz.

File metadata

  • Download URL: monthwise_forecast-1.0.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for monthwise_forecast-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0083fd17e90fbf2001c9ffe787af10e56e43a347fa42b6dd49c2a690848575b6
MD5 0a8b22c341ba8f1b13e59c170ecbd2da
BLAKE2b-256 6eb4d44cf6e8e9c5b4285b809686f5d7640456702ef1b2d602ca0ca4183cc666

See more details on using hashes here.

File details

Details for the file monthwise_forecast-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for monthwise_forecast-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 179ade3803740032055955435164ca80c5f1ce94273d9292fe3ae2f504d71585
MD5 b46d1a78fa00f702f88c220801d66c07
BLAKE2b-256 9aae4b4c4b57324681f8b90c0bfbfe450ab9405463f930d368188952d0d62822

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page