DSA LABData Science Applications
Experiment 10 · CO1

Reading the
Future in Data

Time-series analysis splits a signal into trend, seasonality and noise — then extends them to forecast what comes next. Tune each component and watch the forecast and its confidence band respond.

Course IT416
Outcome CO1
Method Decomposition
Goal Forecast

Signal → forecast

LIVE FORECAST
0
Components
0
Observations
0
Forecast steps
0
Course outcome
01

Lab Manual

Problem
Perform time-series analysis and forecast the behaviour of a time series over a period of time, using statistical methods to identify patterns, trends and seasonality in the data.
Aim
To analyse a time series by decomposing it into trend, seasonal and residual components, and to forecast future values with a statistical model.
Objectives
  • Plot and inspect a time series; check for trend and seasonality.
  • Test/achieve stationarity (differencing) where needed.
  • Decompose the series into trend + seasonal + residual.
  • Fit a forecasting model (e.g. ARIMA/SARIMA) and predict ahead with intervals.
Software
Python 3.x · Jupyter / Colab · Pandas, statsmodels, Matplotlib.
Theory
A time series is data indexed in time order. Classical analysis models it as level + trend (long-run direction) + seasonality (repeating cycle) + residual (noise). Forecasting extends the estimated trend and seasonal pattern forward; uncertainty grows with the horizon, shown as a widening confidence band.
Outcome
CO1 — analysing a real problem and producing a forecast for decision-making.
02

Three Pieces of Every Series

Trend

The long-run rise or fall — sales growing year on year, temperatures drifting up. Captured by a smooth line through the data.

Seasonality

A pattern that repeats on a fixed cycle — higher every December, busier every weekend. Same shape, again and again.

Residual (noise)

What's left after removing trend and season — random wobble. Good models leave residuals that look like pure noise.

03

Build a Series, Forecast It

The cyan line is the observed series. The model fits a trend and a repeating seasonal pattern, then projects them forward as a forecast with a shaded confidence band. Tune the components and horizon, or toggle the trend/season lines on the chart.

Trend + Seasonality + Forecastshaded purple = forecast uncertainty
Trend slope0.4
Seasonal amplitude8
Noise level3
Forecast horizon12
Watch the band: the purple forecast continues the trend and repeats the seasonal shape, but its confidence band widens the further ahead you predict — honest forecasting always shows growing uncertainty. Crank up the noise and the band balloons.
04

Watch the Concepts

Two starting points to understand decomposition and ARIMA forecasting. These open YouTube searches so you can pick the explainer that suits you.

Concept · decomposition Time-series components & stationarityTrend, seasonality, residuals and why we make a series stationary before modelling. Open on YouTube ↗
Method · forecasting ARIMA / SARIMA forecastingHow AR, differencing and MA terms combine to forecast — with prediction intervals. Open on YouTube ↗

These are YouTube search links (not embeds), so you can choose a current, high-quality explainer for your level.

05

The Program

Load the series, decompose it, check stationarity, fit a model and forecast with intervals.

1 — load_decompose.py
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

s = pd.read_csv("series.csv", index_col="date", parse_dates=True)["value"]

# Split into trend + seasonal + residual
result = seasonal_decompose(s, model="additive", period=12)
result.plot(); plt.show()
2 — stationarity.py
from statsmodels.tsa.stattools import adfuller

def adf(x):
    stat, p = adfuller(x)[:2]
    print("ADF p-value:", round(p,4),
          "-> stationary" if p < 0.05 else "-> non-stationary")

adf(s)
adf(s.diff().dropna())     # difference to remove trend
3 — forecast_arima.py
from statsmodels.tsa.statespace.sarimax import SARIMAX

model = SARIMAX(s, order=(1,1,1),
                seasonal_order=(1,1,1,12)).fit()

fc = model.get_forecast(steps=12)
mean = fc.predicted_mean
ci   = fc.conf_int()              # prediction interval

mean.plot(label="forecast")
plt.fill_between(ci.index, ci.iloc[:,0], ci.iloc[:,1], alpha=.3)
plt.legend(); plt.show()
06

Procedure & Result

Steps followed

1 · Plot
Visualise the series; spot trend/seasonality.
2 · Decompose
Separate trend, seasonal, residual.
3 · Stationarity
ADF test; difference if needed.
4 · Model
Fit ARIMA/SARIMA (choose p,d,q).
5 · Forecast
Predict ahead with intervals.
6 · Validate
Check residuals look like noise.

Result

The time series was decomposed into trend, seasonal and residual parts; stationarity was checked and addressed by differencing; and a SARIMA model produced a forecast with prediction intervals, identifying the patterns and projecting future behaviour — demonstrating CO1.

Conclusion: Forecasting is pattern + honesty. By isolating trend and seasonality we capture the structure, and by showing a widening confidence band we admit that the future is uncertain. The further ahead we look, the less precise — a forecast without intervals hides its own risk.
07

Viva Questions

What is a time series?
A sequence of observations recorded at successive, usually equally spaced, points in time — e.g. monthly sales or daily temperature. Order matters, because each value can depend on previous ones.
What are the components of a time series?
Level, trend (long-run direction), seasonality (fixed repeating cycle), and residual/irregular noise. Additive or multiplicative decomposition separates them.
What is stationarity and why does it matter?
A stationary series has constant mean, variance and autocorrelation over time. Many models (like ARIMA) assume stationarity, so we difference or transform the data to achieve it first.
What does ARIMA stand for?
AutoRegressive (AR, past values) Integrated (I, differencing for stationarity) Moving Average (MA, past errors). Parameters (p,d,q) set the order of each part; SARIMA adds seasonal terms.
How do you test for stationarity?
Plot the series and rolling statistics, and run the Augmented Dickey-Fuller (ADF) test — a small p-value indicates stationarity. ACF/PACF plots also help choose model orders.
What is the difference between trend and seasonality?
Trend is a long-term, non-repeating movement up or down. Seasonality is a regular pattern that repeats over a fixed period (daily, weekly, yearly).
Why do forecast confidence intervals widen with the horizon?
Uncertainty accumulates: each step ahead adds error, and errors compound. So the further into the future you predict, the wider the prediction interval becomes.