Zero Labs
Industry News / AI

From Probability to Loss — How MLE Builds Machine Learning: Part 1

Source · Towards AI By Manish Patil

From the publisher’s feed, collected in the ZeroLabs newsroom.

Loss functions aren’t magic. They aren’t some arbitrary design choice that researchers pulled out of thin air. Every loss function you’ve ever seen has a story, a logical, step-by-step derivation rooted in probability theory. By the end of this piece, you won’t just use loss functions. You’ll understand exactly where they come from and why they have to be the way they are.What you’ll learn in this part:How probability and data distributions connect to the idea of “fitting a model”What likelihood actually means — and why it’s different from probabilityWhy we maximize likelihood, and what that looks like mathematicallyWhy we take the log, and why we then flip the sign to get a negativeHow all of this leads us, naturally, to a loss functionThe central questionHere’s the one question that sits at the heart of all of machine learning:Given a dataset, how do we pick model parameters that make the observed data as plausible as possible?In other words, you have data. You want to build a function that “makes sense” of it, so that when new data arrives, your model can make good predictions. How do you find the right parameters for that function?That’s exactly what we’re going to unpack.1: A quick probability refresherBefore we go anywhere, let’s align on a few foundational ideas.Random variables are variables whose values come from some underlying process, like the height of a randomly picked person, or whether a coin flip lands heads.Conditional probability is the probability of something happening given that something else has already happened. Written as P(A | B) : “the probability of A, given B.”Joint probability is the probability of two things happening together. Written as P(A, B).Now, one rule that’ll come back to haunt us in a good way: when two events are independent :- meaning one doesn’t affect the other their joint probability is simply the product of their individual probabilities:P(A, B) = P(A) × P(B)Keep this in the back of your mind. When we get to fitting a model over an entire dataset, we’re going to assume each data point is independent and identically distributed (i.i.d.) — and that multiplication rule is going to be the key step. More on that soon.2: How data shapes distributionsBefore we talk about fitting distributions to data, let’s talk about the distributions themselves.A distribution is a mathematical description of how data is spread — it tells you which values are likely, which are rare, and what shape the data takes overall. Different kinds of real-world data naturally follow different distributions.We’ll focus on four:Bernoulli / Binomial DistributionFor data that is binary — yes/no, heads/tails, spam/not spam. The Bernoulli distribution models a single trial with two outcomes. Extend it to multiple trials and you get the Binomial.Formula: P(X = k) = p^k × (1−p)^(1−k), where p is the probability of success.Bernoulli Distribution2. Gaussian / Normal DistributionThe classic bell curve. Most naturally occurring measurements — heights, weights, test scores — cluster around a mean, with fewer and fewer values as you move away from the center.Formula: P(x) = (1 / σ√2π) × exp(−(x−μ)² / 2σ²)Where μ is the mean and σ is the standard deviation.Normal Distribution3. Poisson DistributionFor count data — how many events happen in a fixed interval of time or space. Think: how many emails arrive per hour, or how many cars pass a checkpoint per minute.Formula: P(X = k) = (λ^k × e^−λ) / k!Where λ is the average rate of events.Poisson Distribution for different λ values4. Exponential DistributionModels the time between events. If a Poisson distribution tells you how many buses arrive per hour, the exponential distribution tells you how long you wait between buses.Formula: P(x) = λ × e^(−λx)Exponential distribution for different λ valuesHow the shape of a distribution changesWithin any distribution, a few parameters control the shape:The mean (μ) shifts the whole curve left or right, it’s where the center of mass sits.The standard deviation (σ) controls how spread out the data is. A small σ gives you a tall, narrow spike. A large σ gives you a flat, wide curve.Outliers pull the mean and flatten the curve, increasing variance.Three normal distributions with different means and standard deviations, showing how the curve shifts and flattens3: Probability vs. Likelihood — the most important distinctionThis is the section where everything starts to click. These two words “Probability” and “Likelihood” are used interchangeably in everyday life. In machine learning, they mean something fundamentally different and confusing them will make the rest of this field feel like magic tricks instead of logical steps.Let’s build the intuition carefully.Probability: the distribution is known. You’re asking about the data.Picture a bowl. You know exactly what’s inside — 70 red balls and 30 blue balls. You close your eyes, reach in, and pull one out.What’s the probability it’s red? 0.7. Easy.You could answer that because you already knew the bowl. You knew the distribution. The parameters were fixed — 70% red, 30% blue and you were reasoning forward: given this distribution, what data might I see?This is probability.Fixed parameters → reason about data. P(data | θ)The parameters here are mean and varianceLikelihood: you have the data. You’re asking about the distribution.Now flip probability entirely.Someone walks up and hands you a fistful of balls — 8 red, 2 blue. They say: “I pulled these from a bowl. What do you think is in the bowl?”You don’t know the distribution. You’re working backwards from the evidence you were given, the data, to make your best guess about the parameters.Maybe the bowl is 80% red. Maybe 90%. You’re trying to figure out which parameter value makes the data you observed most plausible.This is likelihood.Fixed data → reason about parameters. L(θ | data)Here’s the thing that trips people up: it’s the same mathematical expression. P(data | θ) and L(θ | data) use the same formula. What changes is your perspective — what you’re treating as known, and what you’re solving for.So which one is machine learning?Always likelihood. Without exception.Here’s why. When you’re training a model, you have a dataset in front of you. Those are your fixed, observed facts. What you don’t have — what you’re trying to find — are the parameters: the weights, the means, whatever knobs your model has.You’re not predicting what data you might see. You’re asking: given the data I already have, what parameters best explain it?That’s the likelihood question. Every single time.How likelihood was actually introduced into machine learningThis is worth slowing down on, because it answers a question most textbooks skip: why did we even go down this path?Early on, people knew they wanted to fit a model to data. But “fit” was vague. Fit how? By what measure? You could draw infinitely many lines through a scatter plot. How do you pick the right one?The answer that emerged from statistics was this: pick the parameters that make your observed data as probable as possible under your model. Don’t just find a line that passes near the points, instead find the line under which those exact points were most likely to have been generated.This idea is called Maximum Likelihood Estimation, or MLE.The word “maximum” is doing a lot of work here. You’re not just estimating parameters, you’re finding the specific parameters that maximize the probability of having seen your data.Think of it visually. You have a scatter of data points. You can imagine a distribution sitting on top of them — a curve, a bell, whatever shape fits. As you slide the parameters around, that curve shifts. MLE says: stop sliding when the data points sit as high up under the curve as possible. That’s your best distribution.Lets understand this with the help of a visualization:https://youtu.be/7EU8c8CNSEEIn the animation above, two things are being shown simultaneously:Distribution Fit (left) — how the estimated distribution changes as more data points are observed.Log-Likelihood (right) — how the likelihood of the model evolves as we update our estimate.First, notice the green dashed curve. This represents the true distribution — the ideal distribution that actually generated the data. In this case, it is a normal distribution, and our goal is to estimate its parameters from the observed samples.We begin the Maximum Likelihood Estimation process with very little data.When there is only a single data point, the estimated mean is exactly at that point. Since there is no information about variability yet, the estimated variance is extremely small. As a result, the bell curve appears very narrow and sharply peaked.At this stage, the model is clearly a poor estimate of the true distribution, which is reflected in the high loss (low likelihood) shown in the log-likelihood plot.Adding More Data PointsWhen the second data point is introduced, the estimated distribution begins to adjust.The mean shifts slightly to better represent both observations.The variance increases, causing the bell curve to broaden.This widening of the curve reflects increasing uncertainty about where the true distribution lies.As more data points are added, two important things happen:The estimated mean gradually moves toward the true mean of the underlying distribution.The variance stabilizes, allowing the bell curve to better match the shape of the true distribution.At the same time, the log-likelihood curve decreases, indicating that the model parameters are improving and the model is becoming more consistent with the observed data.ConvergenceEventually, as enough samples are observed, the estimated distribution closely aligns with the true distribution.At this point:The mean and variance stabilize.The distribution fit stops changing significantly.The log-likelihood reaches a steady value, indicating that the model parameters have converged.This is the key idea behind Maximum Likelihood Estimation:We adjust the parameters of a distribution so that the probability of observing the given data becomes as large as possible.In other words, we are finding the parameters that make the observed data most likely.That’s the entire idea. Simple in principle. Now let’s see what happens when you actually try to compute it.Section 4: The journey from likelihood to lossNow we follow the math.You have a dataset of n data points: x₁, x₂, …, xₙ. You want to find parameter θ that best explains this data.Step 1 — Write the likelihood of the whole datasetEach data point has some probability of being generated by your model with parameters θ. And since we assume the data points are independent of each other (the i.i.d. assumption from Section 1 — told you it would come back ;), the probability of seeing the entire dataset is the product of each individual probability:L(θ) = P(x₁|θ) × P(x₂|θ) × … × P(xₙ|θ)In words- The likelihood of parameter θ is the product of probabilities of observing each data point given θ.Or in compact notation: L(θ) = ∏ P(xᵢ|θ)This is the likelihood function. You want to find the θ that makes this as large as possible (maximize).Step 2 — The problem with multiplying small numbersEach P(xᵢ|θ) is a number between 0 and 1. Now multiply hundreds or thousands of those together. You get something like:0.7 × 0.65 × 0.71 × 0.68 × … (1000 times)The result is a number so close to zero that a computer can’t reliably represent it. Floating point arithmetic breaks down. You get rounding errors. The whole calculation becomes numerically unstable.We need a smarter approach.Step 3 — Take the logHere’s the fix: apply a logarithm.The log of a product becomes a sum:log(A × B × C) = log(A) + log(B) + log(C)So:log L(θ) = Σ log P(xᵢ|θ)This is the log-likelihood. Sums are stable. Sums are easy. And crucially, this doesn’t change your answer. Because log is a monotonically increasing function, the θ that maximizes L(θ) is the exact same θ that maximizes log L(θ). You get the same optimal parameters either way.Step 4 — Flip the sign, because optimizers minimizeNow you want to maximize the log-likelihood. But every optimizer in machine learning — gradient descent, Adam, RMSProp, all of them is built to minimize. They walk downhill. That’s the only direction they know.So we flip the sign:NLL(θ) = − Σ log P(xᵢ|θ)Minimizing this negative log-likelihood is mathematically identical to maximizing the log-likelihood. Same answer, different direction.And this — the Negative Log-Likelihood — is a loss function. We’ve arrived at something you can hand to an optimizer and say: “minimize this.”Step 5 — The deeper reason this works: KL DivergenceHere’s a connection that makes everything feel inevitable rather than arbitrary.When you minimize NLL, what are you actually doing? You’re pushing your model’s distribution closer to the real distribution of your data. You’re closing a gap.That gap has a name: KL divergence (Kullback–Leibler divergence). It’s a measure of how different two probability distributions are from each other. If your model’s distribution perfectly matches the data distribution, KL divergence is zero. The further apart they are, the larger it gets.Now here’s the key result:Maximizing expected log-likelihood is mathematically equivalent to minimizing KL divergence between your model distribution and the empirical data distribution.This means the entire MLE procedure isn’t just a numerical trick. It has a geometric meaning: every training step is your model’s distribution inching closer to the true distribution of your data.Minimizing NLL = Minimizing KL divergence. They’re the same objective, written two different ways. Keep this in the back of your mind it will resurface when we talk about cross-entropy and generative models later.Let’s make it concrete — deriving MSE from scratchEnough abstraction. Let’s see the whole thing play out with a real distributional assumption.Suppose you’re doing regression — predicting a continuous value. A very natural assumption is that for each input xᵢ, your model outputs a prediction μᵢ, and the true value yᵢ is scattered around that prediction according to a Gaussian distribution:P(yᵢ | xᵢ, θ) = (1 / σ√2π) × exp(−(yᵢ − μᵢ)² / 2σ²)Now let’s build the NLL step by step.Start with the log-likelihood:log L(θ) = Σ log [ (1 / σ√2π) × exp(−(yᵢ − μᵢ)² / 2σ²) ]Split the log:= Σ [ log(1/σ√2π) + log(exp(−(yᵢ − μᵢ)² / 2σ²)) ]= Σ [ −log(σ√2π) − (yᵢ − μᵢ)² / 2σ² ]Now flip the sign to get the NLL:NLL = Σ [ log(σ√2π) + (yᵢ − μᵢ)² / 2σ² ]Here’s the key move: if you assume σ is constant (same variance everywhere), then log(σ√2π) and 1/2σ² are just constants. They don’t change as you adjust θ. So minimizing NLL is equivalent to minimizing just:Σ (yᵢ − μᵢ)²That’s Mean Squared Error.Not invented. Not chosen arbitrarily. Derived — directly from the assumption that your data follows a Gaussian distribution, and from the principle of maximum likelihood.The next time you use MSE in a regression problem, you’ll know exactly why it’s there.The broader point: nothing in machine learning is arbitrary. Every loss function, every training objective, every regularization term — there’s a probabilistic story behind it. Once you see how MSE falls out of the Gaussian assumption, you start wondering: what falls out of other assumptions? What loss function does a Poisson assumption give you? What about a Laplacian?That’s exactly what Part 2 will cover.From Probability to Loss — How MLE Builds Machine Learning: Part 1 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.