1. Promise

One-sentence promise

After this lecture, you can transform time series data into returns, differences, trends, and scaled features in a time-consistent way, and you can explain why each transformation exists.

End tag line

Transformations are part of the model.


2. Learning objectives

After reading this lecture, you will be able to:

  • Explain why prices are often non-stationary and why returns are used instead.
  • Compute simple returns and log returns and interpret them.
  • Apply differencing and de-trending and state what problem they solve.
  • Choose a scaling method and fit it on training data only.
  • Detect “peeking” leakage created by normalisation on the full sample.
  • Communicate transformations in econometrics language: stationarity, unit roots, drift, volatility.

3. Prerequisites and setup

Required maths

  • Logarithms: log⁡(ab)=log⁡a+log⁡b\log(ab)=\log a+\log b.
  • Mean and variance.

Python

  • Python 3.9+.
  • numpy, pandas, matplotlib.

4. Conceptual core

4.1 Prices vs returns

4.1.1 Why prices are tricky

Many asset prices behave approximately like a random walk with drift: Pt=Pt−1exp⁡(μ+σεt),εt∼𝒩(0,1). P_t = P_{t-1}\exp(\mu + \sigma \varepsilon_t), \quad \varepsilon_t \sim \mathcal{N}(0,1).

Then the level PtP_t is typically non-stationary: - mean changes with time, - variance grows with time.

Models trained on non-stationary levels often learn “time” rather than “signal”.

4.1.2 Simple returns

Simple return over one step: Rt=Pt−Pt−1Pt−1. R_t = \frac{P_t - P_{t-1}}{P_{t-1}}.

Interpretation: - Rt=0.01R_t = 0.01 means +1%.

Useful properties: - Intuitive for P&L approximations. - Not additive over time.

4.1.3 Log returns

Log return: rt=log⁡(PtPt−1)=log⁡Pt−log⁡Pt−1. r_t = \log\left(\frac{P_t}{P_{t-1}}\right) = \log P_t - \log P_{t-1}.

Why log returns are popular: - Additive across time: ∑k=1nrt−k+1=log⁡(PtPt−n). \sum_{k=1}^n r_{t-k+1} = \log\left(\frac{P_t}{P_{t-n}}\right). - For small returns: rt≈Rtr_t \approx R_t. - Often closer to stationary than price levels.

Practical rule - Use returns for prediction tasks. - Use prices when the decision is explicitly about levels (e.g., barrier options), but then you must model non-stationarity explicitly.

Validator sentence If you fit on prices, you must justify why the level is the right object.


4.2 Differencing

Differencing is the operation: Δxt=xt−xt−1. \Delta x_t = x_t - x_{t-1}.

It removes a unit root component in many cases.

4.2.1 Differencing prices and logs

  • Difference of prices: ΔPt=Pt−Pt−1\Delta P_t = P_t - P_{t-1}.
  • Difference of log prices: Δ(log⁡Pt)=log⁡Pt−log⁡Pt−1=rt, \Delta(\log P_t) = \log P_t - \log P_{t-1} = r_t, which is exactly the log return.

So “log returns” are “first differences of log prices”.

4.2.2 When differencing is useful

  • To remove drift/trend from a series such as:
    • load demand,
    • interest rate levels,
    • spreads that trend.

But differencing can remove long-run information. It can also amplify noise.

Validator sentence Differencing is a bias–variance trade-off. It reduces non-stationarity but can increase noise.


4.4 Scaling (normalisation) — fit scaler on train only

Scaling transforms a feature xx to a normalised version zz.

A common scaling is standardisation: z=x−μσ. z = \frac{x - \mu}{\sigma}.

Critical rule Compute μ\mu and σ\sigma on the training set only: μtrain=1ntrain∑t∈trainxt,σtrain2=1ntrain∑t∈train(xt−μtrain)2. \mu_{\text{train}} = \frac{1}{n_{\text{train}}}\sum_{t\in \text{train}} x_t,\quad \sigma_{\text{train}}^2 = \frac{1}{n_{\text{train}}}\sum_{t\in \text{train}}(x_t-\mu_{\text{train}})^2.

Then apply those parameters unchanged to validation/test.

If you compute μ\mu and σ\sigma on the full sample, you leak future distribution information into training.

This is “peeking via normalisation”.

Validator sentence A scaler is a model component. It must be trained like a model.


5. Control lens

5.1 Analogue and failure mode

In control, you normalise signals based on what the controller can know online.

If you scale using future statistics, you are designing a controller with privileged information. It will fail on the real plant.

Finance is the same.


6. Worked examples (micro narratives)

6.1 Equity price series

  • Prices trend upward.
  • Returns fluctuate around a small mean.
  • A model trained on prices may learn time and drift.
  • A model trained on returns targets the incremental move.

6.2 Load demand series

  • Load has daily and weekly seasonality and long-term trend.
  • Use de-trending and seasonal adjustment.
  • Use differencing if you target short-run changes.

6.3 Scaling risk features

  • Volatility regime shifts change feature distribution.
  • Fit scaler on train only.
  • Monitor drift in production.

7. Practical checklist (what to say in interviews)

  1. What object do you model: price, return, spread, volatility?
  2. Is it stationary enough for your method?
  3. Do you need differencing or de-trending?
  4. Are your transforms causal (only use past information)?
  5. Is scaling fit on training only?
  6. Did you document every transformation and its motivation?

8. Common mistakes (and how to correct them)

  1. Training on price levels without justification
    • Correction: use returns or include explicit trend terms and regime tests.
  2. Differencing everything mechanically
    • Correction: difference only where it removes a real non-stationarity and does not destroy signal.
  3. Scaling on full data
    • Correction: fit scaler on train only and treat scaler parameters as artefacts.
  4. Using centred moving averages that look into the future
    • Correction: use trailing windows only.
  5. Mixing time zones and trading calendars
    • Correction: align timestamps and market sessions before transformations.

9. Mini-exercises

Exercise 1

Show that the sum of log returns equals the log price ratio.

Solution ∑k=1n(log⁡Pt−k+1−log⁡Pt−k)=log⁡Pt−log⁡Pt−n. \sum_{k=1}^{n}(\log P_{t-k+1}-\log P_{t-k})=\log P_t-\log P_{t-n}.


Exercise 2 (trap)

You standardise a feature using mean and std from 2018–2024, then test on 2024. Is it leakage?

Solution Yes. The scaler used 2024 information. Fit on training only.


Exercise 3

When is de-trending more appropriate than differencing?

Solution When the trend is deterministic (e.g., linear growth) and you want to preserve low-frequency structure.


10. Summary

  • Prices are often non-stationary; returns are often closer to stationary.
  • Log returns are differences of log prices and are additive.
  • Differencing removes stochastic trends but can increase noise.
  • De-trending removes deterministic trends.
  • Scaling must be fit on training only to prevent leakage.

11. Out of scope (for this sub-lecture)

  • Unit root tests (ADF, KPSS) and full ARIMA theory.
  • Cointegration and error-correction models.
  • Advanced filters and state-space models.