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 Solutions: Demand Forecast App

Open In Colab

Lab 8 Solutions: Demand Forecast App

These are the worked solutions of Lab 8, with the outputs. Try the lab yourself first.

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.

Loading...

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).

Loading...
✅ A1 three years, four branches
✅ A1 every branch grows from year 1 to year 3

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?

<Figure size 800x350 with 1 Axes>

The summer break is the deep dip at the start and the end of each year line (weeks 47 to 4, late June to late August): bean use falls from about 50 kg to under 20 kg. There are two smaller dips: the winter break around week 25 (mid-January), which stays in place, and Ramadan in weeks 31 to 38, which comes earlier every year. The peaks just before the summer and in December are exam weeks.

Part B: A fair test

B1. Split

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

✅ B1 148 training weeks
✅ B1 the test starts on 2 June 2025

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.

✅ B2 moving average of the last 4 weeks
✅ B2 same week last year
✅ B2 Holt-Winters forecasts the summer drop at University

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.

Loading...
✅ B3 naive MAE is about 15.7 kg
✅ B3 Holt-Winters has the lowest RMSE
✅ B3 RMSE is never below MAE

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.)

Loading...

The test weeks are the start of the summer and of the campus break; the last week and the moving average carry the busy spring level forward, so they over-forecast every week (by 47% on average for the naive rule). The two seasonal methods are close on most weeks, but copying last year makes a few large misses, the largest at Olaya in the week of 9 June: 80.0 kg forecast against 105.0 kg, because Eid al-Adha fell in that week in 2024 but a week earlier in 2025. RMSE squares the errors, so those few big misses weigh heavily (7.65 against 5.72 kg), while MAE and MAPE treat every kilo or percent the same.

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.

Overwriting forecast_app.py
✅ C1 the app runs without an error
✅ C1 holdout MAE and RMSE metrics
✅ C1 an order metric
University order: 314 kg
✅ C1 the University order is about 314 kg with Holt-Winters and z = 1.28

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.

Order 814 kg for Olaya, 735 kg for Corniche, 512 kg for Al Faisaliyah and 314 kg for the University branch: the Holt-Winters forecast for 28 July to 15 September plus a buffer of 1.28 × RMSE × √8, about 90% sure to cover the 8 weeks. On the last 8 weeks, which we hid from the model, Holt-Winters missed by 4.7 kg a week on average and had the lowest RMSE (5.7 kg), against 15.7 kg for repeating last week, which would order only 148 kg for the University branch just before the students come back. The model only knows the calendar of past years, so check the coming weeks for anything new, such as a promotion, a supply delay or a change in the university’s term dates, and adjust the order by hand.

★ 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.

Loading...
method      Holt-Winters  Last week  Moving average  Same week last year
origin                                                                  
2025-03-10          8.97      11.69            8.07                 7.43
2025-04-07          5.46       3.39           13.12                 6.20
2025-05-05          4.69       5.82            5.93                 6.54
2025-06-02          4.67      15.73           14.93                 5.35 

mean MAE
 method
Holt-Winters            5.95
Same week last year     6.38
Last week               9.16
Moving average         10.51
dtype: float64
2023-03-20 full Ramadan weeks vs the 4 weeks before: -26.5%
2024-03-11 full Ramadan weeks vs the 4 weeks before: -25.1%
2025-02-24 full Ramadan weeks vs the 4 weeks before: -29.4%
about -3.9% per Ramadan day