DEV Community

Cover image for The One Idea Quietly Running Every Prediction App on Your Phone
Sohaib Hassan
Sohaib Hassan

Posted on Originally published at sohaib-hasan.github.io

The One Idea Quietly Running Every Prediction App on Your Phone

Open your phone and look at three numbers. A weather app saying tomorrow will be 34 degrees. A property site showing what a house in your area is roughly worth. A ride app quoting your fare before the driver has even arrived.

Every number like that comes from a model that learned from past data. Real products use far more sophisticated models than what I am about to show you. But the simplest, oldest member of that family is called linear regression, and almost everything fancier is built on the same core move. Understand that move once, in plain words, and you will start noticing it behind every prediction you see.

I teach undergraduate mathematics, and I wrote this the way I would explain it to a friend who has never opened a statistics book. If you already write code, stay for the last third, there is a short Python section for you.

You Already Do This, You Just Never Named It

Think about the last time you sold an old phone on OLX or eBay. You did not open a calculator. You looked at what three or four similar phones sold for, same model, similar condition, and landed on a number somewhere in between. You did the same thing if you ever guessed what a plot of land was worth from the sales next door, or guessed how a student would do in a final exam after seeing the first two tests.

Each time, your brain did one specific thing. It looked at a handful of known examples, noticed the pattern connecting them, and stretched that pattern forward to guess something it had never seen. Draw a line through what you know, then use it to predict what you do not. That is linear regression. You already had the instinct. A computer simply does it with numbers instead of a gut feeling.

Where This Family of Ideas Shows Up

Once you know what to look for, it appears anywhere someone needs to predict a number from past data.

A property site can estimate a home's price by learning how size, location and age related to price in thousands of past sales. A retailer can plan stock for Eid or Black Friday by learning how sales behaved in the weeks before it in past years. A bank scoring a loan applicant uses a close cousin of the same idea, usually logistic regression, which predicts a probability instead of a number. In my own classroom, I notice how revision hours relate to test scores, and I use that pattern to tell a new student roughly what to expect.

The real systems behind big apps combine many more inputs and use more elaborate models than a single straight line. That is exactly the point: a straight line is the simplest version, and the elaborate versions are built on the same foundation.

Why a Gut Feeling Needed Math

If people have guessed this way for centuries, why did it become a formal piece of mathematics?

Because instinct breaks when the problem gets big or the stakes get real. Ask ten people to price a plot from the same five examples and you will get ten different answers, because everyone draws the pattern a little differently in their head. That is fine for a phone resale. It is not fine when a system makes a million decisions a day.

Math replaces "roughly in between" with one exact rule for finding the single best line through any data: the line that keeps the total mistake, across every example, as small as it can be. The method behind it, least squares, was first published by Legendre in 1805. It does not change with who runs it, does not get tired, and does not care whether there are five data points or five million. That is the real power math adds. Not a smarter guess, but a guess that can be trusted, repeated, and scaled.

For Developers: The Whole Thing in Five Lines

Here is a tiny version with four students: hours studied against marks scored.

import numpy as np

hours = np.array([1, 2, 3, 4])
marks = np.array([1, 3, 2, 4])

m, b = np.polyfit(hours, marks, 1)
print(round(m, 2), round(b, 2))   # 0.8 0.5
print(round(m * 5 + b, 2))        # 4.5, the predicted marks for 5 hours
Enter fullscreen mode Exit fullscreen mode

The best line is marks = 0.8 * hours + 0.5. Every extra hour of study is associated with 0.8 extra marks in this toy data, and a student who studied 5 hours is predicted to score 4.5. Those two numbers, the slope and the intercept, are the entire trained model.

Two things are worth knowing as a developer:

  • There are two ways to find those numbers. For a straight line there is a closed-form answer, which is what polyfit uses. You can also start with a random line and nudge it downhill on the error, step by step, which is gradient descent.
  • That second way is how neural networks learn. A single neuron with no activation function, trained to minimize squared error, is exactly this model. Stack many of them with nonlinear bends in between and you get the networks behind modern AI. The training loop looks conceptually the same: measure the error, nudge the parameters so the error shrinks, repeat.

So when people say linear regression is the grandparent of machine learning, it is not just a nice phrase. It is the smallest working example of how learning from data actually works.

Want to See the Math by Hand?

Most readers do not need to see a formula to use what they just learned. But if you are a student, or you simply want to watch the best line get calculated step by step with real numbers, I built a complete animated walkthrough using the same four point example:

It covers why the mistakes are measured the way they are, what the error looks like when you plot it, and how the two numbers above, 0.8 and 0.5, are worked out from scratch.

The Next Time You See a Prediction

When an app gives you a price, a fare or an estimate before you have told it much, you now know the shape of what is behind it: look at what happened before, find the pattern, and stretch it forward. The models get bigger and the code gets cleverer, but that core idea has barely changed in over two hundred years.


About the author

I'm Sohaib Hasan. I have 9+ years of teaching experience in advanced mathematics and statistics, and I'm currently a section leader for Stanford's Probability for AI course, selected out of 2,700 participants from 70 countries to teach students from around the world. After each class, I explain the same ideas again through Manim animations, and I'm glad that Stanford professors and fellow section leaders have appreciated them.

On my YouTube channel, PlotLab Algorithms, I teach data science, machine learning, cryptography, math, DSA, and statistics visually, in the style made popular by 3Blue1Brown. Shorter clips live on Facebook (80K+ followers) and Instagram (15K+ followers).

Want an animation like this for your own work? If you have a project, a research article, a thesis defense, a YouTube video, or any idea that is hard to explain with slides alone, I can turn it into a clear, professional animation. Reach out through my Work With Me page and tell me what you are working on. If you would rather learn to build animations yourself, start with my Manim CE Beginner's Handbook. And I'd like to hear what you think of this post, drop a comment.

Top comments (0)