Loading problem...
In real-world energy management systems, predicting future consumption is essential for maintaining grid stability and ensuring adequate power supply. However, raw consumption data often contains periodic fluctuations caused by daily, weekly, or seasonal patterns that must be separated from the underlying trend before making accurate forecasts.
You are a data scientist working on energy management for a growing remote settlement. Your task is to forecast energy consumption for day 15 based on 10 days of historical usage data. The challenge is that the measured consumption includes a known periodic oscillation pattern superimposed on the actual usage trend.
The periodic fluctuation follows the formula:
$$f_i = 10 \times \sin\left(\frac{2\pi \times i}{10}\right)$$
where i is the day number (ranging from 1 to 10).
Implement a function that performs the following pipeline:
Linear regression finds the best-fit line y = mx + b through data points by minimizing the sum of squared residuals. For this problem:
The slope represents the daily change in consumption, while the intercept represents the baseline consumption level.
daily_consumption = [150, 165, 185, 195, 210, 225, 240, 260, 275, 290]403Step 1: Calculate periodic fluctuations for days 1-10
For each day i, compute: f_i = 10 × sin(2π × i / 10)
| Day | sin(2π×i/10) | Fluctuation |
|---|---|---|
| 1 | 0.588 | 5.88 |
| 2 | 0.951 | 9.51 |
| 3 | 0.951 | 9.51 |
| 4 | 0.588 | 5.88 |
| 5 | 0.000 | 0.00 |
| 6 | -0.588 | -5.88 |
| 7 | -0.951 | -9.51 |
| 8 | -0.951 | -9.51 |
| 9 | -0.588 | -5.88 |
| 10 | 0.000 | 0.00 |
Step 2: Detrend the data
Subtract each fluctuation from the observed consumption to get the base consumption trend.
Step 3: Fit linear regression
Apply ordinary least squares to find the line of best fit through the detrended points.
Step 4: Predict day 15 base consumption
Use the equation: predicted = slope × 15 + intercept
Step 5: Add day 15 fluctuation
f_15 = 10 × sin(2π × 15 / 10) = 10 × sin(3π) ≈ 0
Step 6: Apply 5% safety margin and convert to integer
Final forecast = int(predicted × 1.05) = 403
daily_consumption = [100, 120, 140, 160, 180, 200, 220, 240, 260, 280]417This dataset shows perfectly linear growth with 20 units increase per day.
After removing the sinusoidal fluctuation and fitting linear regression, the model captures this linear trend accurately.
The prediction for day 15, with fluctuation restoration and 5% safety margin, yields 417.
daily_consumption = [200, 200, 200, 200, 200, 200, 200, 200, 200, 200]228This is a constant consumption scenario where all values are 200.
After detrending, the underlying pattern still fluctuates slightly due to the sinusoidal subtraction, but the linear regression will find essentially a flat trend.
The base prediction for day 15 plus the day 15 fluctuation (which is approximately 0), multiplied by 1.05 gives approximately 228.
Constraints