← Back to featured projects
Data Science · Forecasting · Pharmacy operations

FarmaStock AI — Predictive Pharmacy Stock Optimization

FarmaStock AI is an end-to-end data science project that transforms operational data exported from Nixfarma into stock metrics, demand forecasts and explainable replenishment recommendations for community pharmacy.

Python Pandas Forecasting Streamlit Data Modeling EDA
Final Master's Thesis project evaluated with 9.5/10. Real pharmacy data was used during development, but no sensitive or identifiable data is included in the public repository.
FarmaStock AI stock optimization cover

Business problem

Community pharmacies need to balance two opposite risks: stockouts, which can reduce service quality and cause lost sales, and overstock, which immobilizes capital, occupies space and increases expiry risk.

The goal of this project is to support replenishment decisions using operational data, demand behaviour, stock coverage and explainable rules.

What this project shows

  • End-to-end data pipeline from raw Nixfarma exports to processed analytical tables.
  • Stock reconstruction using movement history and stock deltas.
  • Demand analysis, rotation, coverage and stockout/overstock risk indicators.
  • Demand forecasting with baseline validation and supervised model comparison.
  • Rule-based replenishment recommendations with explainable reasons.
  • Streamlit app for operational exploration and product-level detail.

End-to-end pipeline

The project is structured as a reproducible notebook pipeline. Each step produces validated intermediate or processed outputs used by the following stage.

01 · Raw ingestion

Inventory of raw files, robust reading and source traceability for sales, movements and formal purchases.

02 · Cleaning

Column standardization, type conversion, controlled parsing and validation of Nixfarma exports.

03 · Reconciliation

Operational stock reconstruction using movement chronology, previous stock, final stock and delta stock.

04 · Data modeling

Creation of analytical tables such as product daily metrics, product dimension and enriched facts.

05 · Stock metrics EDA

Demand, rotation, stock coverage, replenishment behaviour, stockout risk and overstock review signals.

06 · Forecasting

Demand forecast using a weighted recent-demand baseline, backtesting and supervised challenger models.

07 · Recommendations

Rule-based replenishment recommendations combining forecast, stock, coverage, safety stock and review flags.

08 · App review

Preparation of app KPIs, filters, labels and Streamlit-oriented review outputs.

Data sources

The project uses three Nixfarma data families with different roles:

  • Movements: main source for stock, operational demand, entries, exits and adjustments.
  • Sales: commercial enrichment for PVP, sales type, operators, entities and amounts.
  • Formal purchases: documentary and supplier-related contrast, not automatic stock input.

Reconciliation logic

The project does not assume that all sources match. Stock and demand are reconstructed primarily from movements.

In movement data, the stock column is interpreted as final stock after each operation:

  • stock_prev: previous movement stock for the same product.
  • stock_after: stock after the current movement.
  • delta_stock: net stock variation between movements.

This is especially important for manual stock modifications, recounts and stock adjustments where original units may not represent real stock movement.

Forecasting approach

The forecasting layer estimates expected operational demand for 7, 14 and 30 days. The main model for version 1 is an explainable weighted recent-demand baseline.

  • Demand series are converted into continuous daily product-level data.
  • Products are classified by forecastability and confidence level.
  • The baseline is backtested before being accepted as the final forecast version.
  • Supervised models are trained as challengers, not automatic replacements.
  • Metrics include MAE, RMSE, sMAPE, WAPE and bias.

Replenishment recommendations

Recommendations are generated using a rule-based and interpretable policy. The system converts forecast and stock context into operational actions.

  • recommended_order_qty: initial suggested order quantity.
  • replenishment_action: action category for each product.
  • replenishment_urgency_level: urgency based on risk and quantity.
  • replenishment_reason: explanation of the recommendation.
  • Manual review is prioritized when the data does not support automatic ordering.

Streamlit app

The project includes a Streamlit app designed to explore stock recommendations visually and operationally.

Executive overview

Global KPIs, recommendation volume, product status and action distribution.

Replenishment priorities

Products to order or review, with quantity, urgency and explanation.

Risk and coverage

Stockout risk, estimated coverage and forecast context.

Overstock review

Products where no order is recommended due to overstock or low demand signals.

Product detail

Individual product card with stock, forecast, recommendation and decision rationale.

Operational assistant

Guided interpretation of recommendations and supplier campaign scenarios.

The public repository includes code, notebooks, documentation and screenshots. Real processed pharmacy data is not included for confidentiality reasons.

Final outputs

  • fact_stock_movements_clean
  • fact_sales_enriched
  • fact_formal_purchases
  • dim_products
  • product_daily_metrics
  • product_stock_metrics
  • product_demand_forecast
  • product_replenishment_recommendations

Why it matters

This project connects pharmacy domain knowledge with data engineering, forecasting and decision-support design.

Instead of focusing only on model complexity, the project prioritizes traceability, validation, operational interpretation and business usefulness.

The final result is a structured data product that can support better stock decisions in a pharmacy context.

Limitations

  • Real data cannot be published due to confidentiality.
  • Lead times and safety stock are rule-based initial assumptions.
  • The app requires locally generated processed data to run with real outputs.
  • Supplier constraints, package multiples, expiry dates and economic margins are not yet included.
  • Recommendations are decision-support outputs, not blind purchase orders.

Related project

This stock optimization pipeline is complemented by a separate generative AI project:

Knowledge Assistant — Expert RAG Agent, an assistant for answering pharmacy stock optimization questions using Gemini, ChromaDB, LangGraph and Streamlit.