NelworksNelworks

ML & AI / Season 1 / EP07

THE LINEAR DELUSION (REGRESSION & AUTOCORRELATION)

Shez

And as you can see, based on my machine learning model, our Q4 sales will exceed the GDP of China!

Kurumi

(Spinning in the chair) And we will soon sell more units than Earth population by November?

Shez

It's math, Kurumi! **Linear Regression**. y = mx + b.

Shez

The R^2 is 0.98!

Kurumi

You fitted a straight line to a cumulative time series.

Shez

Yeah! The trend is up!

Kurumi

You didn't find a pattern. You found an angle. You assumed the world is a ramp.

Kurumi

This is reality. It fluctuates. It has cycles.

Shez

Sure, but the *general* trend is up.

Kurumi

Your Linear Regression sees this. It minimizes the squared error.

Kurumi

Even if the market crashes today, your line will dutifully predict a record high and create a porcupine chart!

Shez

But... I used 'TimeSeriesSplit'! I trained on the past to predict the future!

Kurumi

You validated correctly, but your **Model Assumption** is trash.

Kurumi

OLS Regression assumes the errors are independent.

Shez

So?

Kurumi

Independent means: The result of this coin flip does not care about the last coin flip.

Shez

That's the **Gamblers Fallacy**! I've heard of it!

Kurumi

But Sales are not coin flips. Sales are dominoes.

Kurumi

If Sales were high yesterday, they are likely to be high today. That is **Autocorrelation**. **Memory** in your data.

Kurumi

Your model assumes today is independent of yesterday. It assumes the deviation from the line is random noise.

Kurumi

Look at your residuals (the distance between the dots and the line).

Shez

They... look like waves.

Kurumi

Random noise looks like dust. Your residuals look like a snake.

Kurumi

That means your model left all the interesting information (the cycle) in the trash can.

Shez

But the R^2 is high! That proves correlation!

Kurumi

Look. Eating cheese causes bedsheet strangulation.

Shez

...really?

Kurumi

NO, YOU FOOL.

Kurumi

This is **Spurious Regression**.

Kurumi

Both variables are increasing over time. Anything that goes up will correlate with anything else that goes up.

Shez

So... my sales prediction is just...

Kurumi

A coincidence of trends. It has no predictive power for turning points.

Kurumi

If the trend changes, your model will be the last to know. It will keep pointing at the sky even if the company burns.

Shez

Okay. So I can't use Linear Regression on raw sales data.

Kurumi

Not on raw data. You have to kill the trend first.

Kurumi

A Stationary time series has a constant mean and variance. It doesn't wander off to infinity.

Kurumi

You can't predict where the balloon will be in an hour (it could be in space). But you *can* predict where the dog will be (around the stake).

Shez

But I want to predict the balloon! I want Sales to go up!

Kurumi

Then don't predict the *Position* (y_t). Predict the **Step** (y_t - y_t-1).

Kurumi

Instead of predicting "Sales will be $1,000,000," predict "Sales will change by +$500."

Shez

If I predict the change... the trend disappears?

Kurumi

If sales grow by $10 every day, the *change* is a constant line at 10. That is stationary. Linear Regression can handle that.

Shez

'df['diff'] = df['sales'].diff()'.

Shez

'model.fit(X, df['diff'])'.

Shez

This looks... messy. It's not a straight line anymore.

Kurumi

It's the truth. It shows the volatility.

Kurumi

Now, if you want to be a professional, you use a professional model built for this.

Kurumi

**A**uto**R**egressive **I**ntegrated **M**oving **A**verage.

Shez

Sounds like an anime protagonist name.

Kurumi

**AR (AutoRegressive):** Uses past values to predict future (y_t-1 predicts y_t). "If it rained yesterday, it might rain today."

Kurumi

**I (Integrated):** The Differencing we just talked about. Handles the trend.

Kurumi

**MA (Moving Average):** Uses past *errors* to correct the forecast. "I was wrong by 5 units yesterday, let me adjust."

Shez

So it learns from its own mistakes?

Kurumi

Yes. It has a feedback loop. Linear Regression is stubborn; it never looks back at its residuals.

Shez

'AutoARIMA' is finding the parameters... '(p=1, d=1, q=1)'.

Shez

It... it stops going up forever. It flattens out.

Kurumi

That is a conservative, realistic forecast. It says "The trend might continue, or it might stabilize."

Shez

And this grey cone?

Kurumi

That's the Confidence Interval. It grows wider the further you look. It admits "I don't know."

Kurumi

Time series is a rabbit hole to get into. Wait till we get into cross-validation, train test splits, regime detections.

Shez

Next time, please...