Machine Learning Load Forecasting
الانحدار الخطي والتدريب والتنبؤ وتقييم الخطأ
🎯 Lab Objectives
- Understand load forecasting as a supervised-learning problem.
- Construct a simple electrical-load dataset.
- Understand linear regression mathematically.
- Estimate model parameters in MATLAB.
- Predict electrical load from temperature.
- Calculate MAE, MSE, and RMSE.
- Visualize actual and predicted load.
🎯 أهداف المختبر
- فهم التنبؤ بالحمل كمسألة تعلم خاضع للإشراف.
- إنشاء مجموعة بيانات بسيطة للحمل الكهربائي.
- فهم الانحدار الخطي رياضيًا.
- تقدير معاملات النموذج باستخدام MATLAB.
- التنبؤ بالحمل اعتمادًا على درجة الحرارة.
- حساب MAE وMSE وRMSE.
- رسم الحمل الحقيقي والمتنبأ به.
1. Load Forecasting Problem
Electrical load forecasting estimates future power demand using historical measurements and explanatory variables.
In this first example, we intentionally use only one input:
1. مسألة التنبؤ بالحمل
يهدف التنبؤ بالحمل الكهربائي إلى تقدير الطلب المستقبلي على القدرة اعتمادًا على البيانات التاريخية والعوامل المؤثرة.
سنبدأ بنموذج بسيط يستخدم درجة الحرارة كدخل واحد.
2. Supervised Learning
For each training sample, we know both the input and the desired output.
In this laboratory:
2. التعلم الخاضع للإشراف
لكل عينة تدريب نعرف قيمة الدخل والقيمة الصحيحة المطلوبة.
3. Example Dataset
| Sample | Temperature [°C] | Load [MW] |
|---|---|---|
| 1 | 10 | 105 |
| 2 | 12 | 110 |
| 3 | 14 | 116 |
| 4 | 16 | 121 |
| 5 | 18 | 128 |
| 6 | 20 | 134 |
| 7 | 22 | 141 |
| 8 | 24 | 147 |
| 9 | 26 | 154 |
| 10 | 28 | 162 |
3. مجموعة البيانات
تمثل كل عينة درجة حرارة مقاسة والحمل الكهربائي المقابل لها.
4. Enter the Data in MATLAB
temperature = [10 12 14 16 18 20 22 24 26 28]';
loadMW = [105 110 116 121 128 134 141 147 154 162]';
The apostrophe converts each vector into a column vector.
4. إدخال البيانات إلى MATLAB
نخزن درجات الحرارة والحمل في متجهين عموديين بحيث تمثل كل صفّة عينة تدريب واحدة.
5. Linear Regression Model
Assume a linear relationship:
where:
- ŷ = predicted electrical load
- x = temperature
- β₀ = intercept
- β₁ = slope
5. نموذج الانحدار الخطي
حيث β₀ هو الجزء الثابت، وβ₁ يمثل مقدار تغير الحمل المتوقع عند تغير درجة الحرارة بمقدار درجة واحدة.
6. Prediction Error
For sample i:
A good model should make these residual errors small.
6. خطأ التنبؤ
يمثل الخطأ الفرق بين الحمل الحقيقي والحمل الذي تنبأ به النموذج.
7. Least-Squares Principle
Linear regression selects the parameters that minimize the sum of squared errors:
Therefore:
7. مبدأ المربعات الصغرى
نبحث عن β₀ وβ₁ بحيث يصبح مجموع مربعات أخطاء التنبؤ أصغر ما يمكن.
8. Matrix Form
Write the model for all samples:
with:
and:
8. الصيغة المصفوفية
يمكن كتابة جميع معادلات عينات التدريب بصورة مختصرة:
العمود الأول من X يخص المعامل الثابت β₀، والعمود الثاني يحتوي على قيم درجة الحرارة.
9. Deriving the Optimal Parameters
The squared-error function is:
Differentiate with respect to β:
At the minimum:
therefore:
which gives the normal-equation solution:
9. اشتقاق معاملات النموذج
نشتق دالة الخطأ بالنسبة إلى معاملات النموذج، ثم نساوي المشتقة بالصفر لأننا نبحث عن القيمة الدنيا.
ومنها:
10. MATLAB Implementation
X = [ones(size(temperature)) temperature];
beta = X \ loadMW;
MATLAB's backslash operator solves the least-squares problem directly and is generally preferable to explicitly computing a matrix inverse.
beta0 = beta(1);
beta1 = beta(2);
10. التنفيذ في MATLAB
نبني المصفوفة X ثم نستخدم العامل \ لحساب معاملات الانحدار بطريقة عددية مستقرة.
beta = X \ loadMW;
11. Make Predictions
Once β₀ and β₁ are known:
MATLAB:
predictedLoad = X * beta;
11. إجراء التنبؤ
بعد تعلم معاملات النموذج يمكن حساب الحمل المتوقع لكل درجة حرارة.
predictedLoad = X * beta;
12. Mean Absolute Error — MAE
MAE = mean(abs(loadMW - predictedLoad));
MAE gives the average magnitude of the prediction error in MW.
12. متوسط الخطأ المطلق MAE
إذا كان MAE=2 MW مثلًا، فهذا يعني أن مقدار خطأ التنبؤ المتوسط يقارب 2 MW.
13. Mean Squared Error — MSE
MSE = mean((loadMW - predictedLoad).^2);
Squaring makes larger errors more influential.
13. متوسط مربع الخطأ MSE
يتم تربيع الخطأ قبل حساب المتوسط، ولذلك تحصل الأخطاء الكبيرة على وزن أكبر.
14. Root Mean Squared Error — RMSE
RMSE = sqrt(MSE);
14. الجذر التربيعي لمتوسط مربع الخطأ RMSE
الميزة المهمة أن وحدة RMSE هي MW نفسها، مما يجعل تفسيرها أسهل.
15. Actual vs Predicted Load
figure
plot(temperature,loadMW,'o','LineWidth',1.6)
hold on
plot(temperature,predictedLoad,'-','LineWidth',1.8)
grid on
xlabel('Temperature [°C]')
ylabel('Electrical Load [MW]')
legend('Actual Load','Predicted Load','Location','best')
title('Electrical Load Forecasting')
15. الحمل الحقيقي مقابل الحمل المتنبأ به
تمثل النقاط الحمل الحقيقي، بينما يمثل الخط خرج نموذج الانحدار الخطي.
16. Forecast a New Operating Point
Suppose tomorrow's expected temperature is:
The predicted load is:
Tnew = 30;
forecastLoad = beta0 + beta1*Tnew;
fprintf('Forecast at %.1f C = %.2f MW\n', ...
Tnew,forecastLoad);
16. التنبؤ بحالة جديدة
إذا كانت درجة الحرارة المتوقعة غدًا 30°C، يمكن استخدام النموذج المتعلم للتنبؤ بالحمل دون وجود قيمة الحمل مسبقًا.
17. Complete MATLAB Program
clear; clc; close all;
% -----------------------------------------------
% Machine Learning Load Forecasting
% Linear Regression
% -----------------------------------------------
% Dataset
temperature = [10 12 14 16 18 20 22 24 26 28]';
loadMW = [105 110 116 121 128 134 141 147 154 162]';
% -----------------------------------------------
% Build regression matrix
% y = beta0 + beta1*x
% -----------------------------------------------
X = [ones(size(temperature)) temperature];
% Least-squares training
beta = X \ loadMW;
beta0 = beta(1);
beta1 = beta(2);
% -----------------------------------------------
% Prediction on training data
% -----------------------------------------------
predictedLoad = X * beta;
% Residual error
error = loadMW - predictedLoad;
% -----------------------------------------------
% Performance indices
% -----------------------------------------------
MAE = mean(abs(error));
MSE = mean(error.^2);
RMSE = sqrt(MSE);
% -----------------------------------------------
% Display model
% -----------------------------------------------
fprintf('Learned model:\n')
fprintf('Load = %.4f + %.4f * Temperature\n\n', ...
beta0,beta1)
fprintf('MAE = %.4f MW\n',MAE)
fprintf('MSE = %.4f MW^2\n',MSE)
fprintf('RMSE = %.4f MW\n\n',RMSE)
% -----------------------------------------------
% Forecast new temperature
% -----------------------------------------------
Tnew = 30;
forecastLoad = beta0 + beta1*Tnew;
fprintf('Forecast for T = %.1f C: %.2f MW\n', ...
Tnew,forecastLoad)
% -----------------------------------------------
% Plot actual and predicted load
% -----------------------------------------------
figure
plot(temperature,loadMW,'o','LineWidth',1.6)
hold on
plot(temperature,predictedLoad,'-','LineWidth',1.8)
grid on
xlabel('Temperature [°C]')
ylabel('Electrical Load [MW]')
legend('Actual Load','Predicted Load', ...
'Location','best')
title('Machine Learning Load Forecasting')
% -----------------------------------------------
% Plot prediction errors
% -----------------------------------------------
figure
stem(temperature,error,'LineWidth',1.5)
grid on
xlabel('Temperature [°C]')
ylabel('Prediction Error [MW]')
title('Load Forecasting Residuals')
17. البرنامج الكامل في MATLAB
ينفذ البرنامج جميع مراحل المسألة:
أي: البيانات ← التدريب ← النموذج ← التنبؤ ← تقييم الأداء.
18. What Does β₁ Mean Physically?
Its approximate unit is:
For this educational dataset, a positive β₁ means that load tends to increase as temperature increases.
18. ماذا يعني β₁ فيزيائيًا؟
أي أنه يمثل حساسية الحمل تجاه تغير درجة الحرارة، ووحدته التقريبية:
19. Training Data vs Test Data
Evaluating a model only on the same data used for training can give an overly optimistic impression.
19. بيانات التدريب وبيانات الاختبار
لا يكفي اختبار النموذج على البيانات نفسها التي استخدمناها في التدريب.
20. Simple Train/Test Experiment
Use the first seven samples for training and the last three for testing:
xTrain = temperature(1:7);
yTrain = loadMW(1:7);
xTest = temperature(8:10);
yTest = loadMW(8:10);
Xtrain = [ones(size(xTrain)) xTrain];
betaTest = Xtrain \ yTrain;
Xtest = [ones(size(xTest)) xTest];
yPrediction = Xtest * betaTest;
testError = yTest - yPrediction;
RMSEtest = sqrt(mean(testError.^2));
fprintf('Test RMSE = %.4f MW\n',RMSEtest)
20. تجربة بسيطة للتدريب والاختبار
نستخدم العينات السبع الأولى لتدريب النموذج، ثم نختبره على ثلاث عينات لم تدخل في عملية التدريب.
21. Extending the Model
Real load forecasting rarely depends on temperature alone.
A more realistic input vector may be:
The model becomes:
21. تطوير النموذج
في التطبيقات الحقيقية لا يعتمد الحمل عادةً على درجة الحرارة وحدها.
يمكن إضافة الساعة، ونوع اليوم، والحمل السابق، والطقس وغيرها من المتغيرات.
22. Experiment 1 — Add Measurement Noise
Replace the original load data by:
loadNoisy = loadMW + 3*randn(size(loadMW));
Train the model again and observe how the regression line and RMSE change.
22. التجربة 1 — إضافة ضجيج للقياسات
أضف ضجيجًا عشوائيًا إلى بيانات الحمل، ثم أعد التدريب وقارن النتائج.
23. Experiment 2 — Remove Training Samples
Train using only a small number of samples and compare the prediction.
For example:
temperatureSmall = temperature(1:4);
loadSmall = loadMW(1:4);
23. التجربة 2 — تقليل بيانات التدريب
استخدم أربع عينات فقط في التدريب ثم قارن النموذج بالنموذج الأصلي.
الهدف هو فهم أهمية كمية البيانات المستخدمة في التعلم.
24. Experiment 3 — Nonlinear Load Behavior
Suppose the real relationship becomes nonlinear:
A straight line may no longer represent the relationship sufficiently well.
24. التجربة 3 — علاقة غير خطية
إذا أصبحت العلاقة غير خطية، فقد لا يكون الانحدار الخطي كافيًا لتمثيلها.
25. Lab Tasks
- Enter the temperature and load dataset.
- Plot the raw data.
- Construct the regression matrix X.
- Calculate β₀ and β₁.
- Explain the physical meaning of β₁.
- Calculate predicted load values.
- Calculate MAE, MSE, and RMSE.
- Forecast the load at 30°C.
- Perform the train/test experiment.
- Add measurement noise and repeat the training.
- Explain when linear regression may become inadequate.
25. مهام المختبر
- أدخل بيانات درجة الحرارة والحمل.
- ارسم البيانات الأصلية.
- أنشئ مصفوفة الانحدار X.
- احسب β₀ وβ₁.
- اشرح المعنى الفيزيائي للمعامل β₁.
- احسب قيم الحمل المتنبأ بها.
- احسب MAE وMSE وRMSE.
- تنبأ بالحمل عند 30°C.
- نفذ تجربة التدريب والاختبار.
- أضف ضجيجًا إلى القياسات وأعد التدريب.
- اشرح متى يصبح الانحدار الخطي غير كافٍ.
📝 Review Questions
- Why is load forecasting a supervised-learning problem?
- What do β₀ and β₁ represent?
- Why do we minimize squared errors?
- How is the least-squares solution obtained?
- What is the difference between MAE, MSE, and RMSE?
- Why should test data not be used for training?
- What does a large test RMSE indicate?
- Why can temperature alone be insufficient for realistic load forecasting?
- When would a nonlinear model be preferable?
📝 أسئلة المراجعة
- لماذا يعد التنبؤ بالحمل مسألة تعلم خاضع للإشراف؟
- ماذا يمثل β₀ وβ₁؟
- لماذا نقلل مجموع مربعات الأخطاء؟
- كيف نحصل على حل المربعات الصغرى؟
- ما الفرق بين MAE وMSE وRMSE؟
- لماذا لا يجب استخدام بيانات الاختبار في التدريب؟
- ماذا تعني قيمة RMSE كبيرة على بيانات الاختبار؟
- لماذا لا تكفي درجة الحرارة وحدها في التنبؤ الحقيقي بالحمل؟
- متى نحتاج إلى نموذج غير خطي؟