Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 8: Demand Forecast App

Open In Colab

Lab 8: Demand Forecast App

In this lab you forecast the coffee beans of each Qahwa House branch, test your forecasts fairly, and put the result in a Streamlit app the manager can use to place the order.

The question. It is the end of July 2025, and each branch orders its beans for the next 8 weeks at once. Too few and the branch runs out; too many and the beans go stale. How many kilograms should each branch order, and which forecasting method should the app use?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part C, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb, plus your forecast_app.py.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

kg has one row per week (156 weeks, 1 August 2022 to 21 July 2025) and one column per branch: the kilograms of beans used that week. The original table demand also has ramadan_days (days of the week in Ramadan) and holiday.

Part A: Look before you forecast

A1. Trend

Compute the mean kg per week of each branch in each of the three years. The rows of kg are in order, 52 weeks per year, so the year number is np.repeat([1, 2, 3], 52). Store the table in yearly (3 rows, 4 branches).

A2. Seasonality

Plot the University branch’s three years on top of each other (one line per year, x = week 1 to 52). Which weeks are the summer break? Is there a second, smaller dip?

Your answer here (double-click to edit).

Part B: A fair test

B1. Split

Hide the last 8 weeks: train is every week but the last 8, test the last 8.

B2. Four forecasting methods

Complete the functions. Each takes a series s (one branch) and a horizon h and returns an array of h forecasts.

B3. Score them

Write scores(actual, forecast) that returns a Series with the MAE, RMSE and MAPE (in %) of all the errors of a table of actuals and a table of forecasts (stack() puts every branch and week in one column). Then score the four methods over the four branches.

B4. In two or three sentences: why do the first two methods miss so badly on these 8 weeks? And why does the gap between the two seasonal methods look small on MAE and MAPE but large on RMSE? (Hint: compare both forecasts with the actual values at Olaya, week by week, and look at the holiday column of demand.)

Your answer here (double-click to edit).

Part C: The demand forecast app

C1. Fill in the app

The next cell writes a Streamlit app to forecast_app.py: the manager chooses a branch, a method and a service level; the app tests the method on the last 8 weeks, forecasts the next 8 and computes the order, forecast total + z × RMSE × √8, as in the reading. Fill in the five gaps marked ..., run the cell, then run the test cell under it.

C2. See it in a browser

Colab cannot show a Streamlit page, so open it in GitHub Codespaces (free with GitHub Education) or on your own computer:

  1. Put forecast_app.py in a folder, open a terminal there and run pip install streamlit pandas statsmodels.

  2. Run streamlit run forecast_app.py; a browser tab opens at http://localhost:8501.

  3. To share it, push the file and a requirements.txt (streamlit, pandas, statsmodels) to a GitHub repository and deploy it on Streamlit Community Cloud (share.streamlit.io).

The course’s finished version, with all branches at once and more methods, is week08/app/app.py in the course repository.

C3. Your recommendation

Use your app with each branch and method. Then write three sentences for the manager: how much each branch should order (with the method and service level you chose), why you trust that method (with a number from the holdout), and one thing in the coming 8 weeks the model cannot know about.

Write your recommendation here (double-click to edit).

★ Extended challenges

E1. Multiplicative seasonality. Refit Holt-Winters with seasonal="mul" and score it on the holdout. Which branches gain, and why might multiplicative seasonality suit the University branch?

E2. Rolling-origin test. One holdout of 8 weeks is a small test. Repeat the split with the test ending 4, 8 and 12 weeks earlier, and average the MAE of each method. Does the ranking hold?

E3. Moving holidays. In the coming 8 weeks there is no Eid, but in February 2026 Ramadan starts on about 18 February, 11 days earlier than in 2025. Using ramadan_days, estimate how much bean use falls per Ramadan day at Olaya (compare Ramadan weeks with the weeks just before), and describe how you would correct a Holt-Winters forecast for the weeks of February and March 2026.

E4. The app. Add the moving average and the average to your app’s methods, and show a table of the holdout MAE, RMSE and MAPE of every method for the chosen branch.

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.