0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09The fraud alarm that is wrong 81% of the time
Brightcart's fraud detector just flagged an order. The detector catches ninety-five percent of real fraud. So the order is frozen, and the customer is waiting. Here's the question. Given that alarm, what's the chance this order really is fraud? Ninety-five percent? Fifty? Or nineteen? Most people say ninety-five. Even many professionals say ninety-five. The real answer, for Brightcart's numbers, is about nineteen percent. More than four out of five alarms are false. If that surprises you, this episode is for you. Machine learning is built on probability.
Models don't just give answers; they give how sure they are. To build them, and to trust them, we have to speak this language fluently.
1:00Map flight: probability and statistics
Season One, Module One, final stop: probability and statistics.
1:07Probability, events and distributions
A probability is a number between zero and one. Zero means impossible, one means certain, and a half is a coin flip. If one Brightcart delivery in ten arrives late, the probability that a delivery is late is zero point one. Then the probability that it's not late must be zero point nine. Something either happens or it doesn't, so the two always add to one. A quantity whose value depends on chance is called a random variable. "Is this delivery late?" is a random variable with two outcomes, one for late and zero for on time.
A single yes-or-no event like this has a name we'll use a lot: a Bernoulli variable, with probability p. A distribution lists every possible outcome and how likely it is. Here's Brightcart's distribution for the number of items in an order. Thirty percent of orders have one item, twenty-five percent have two, and so on, and all the bars add up to exactly one. Something has to happen. Two quick rules. If two events are independent, so one doesn't affect the other, the chance of both is the product of their chances. And the chance of either one happening is their chances added together, minus the overlap, so we don't count it twice.
2:28Mean, variance and standard deviation
Statistics gives us a few numbers that summarise a whole pile of data. Here are thirteen normal Brightcart orders, in dollars. The first summary is the mean, the average. Add them up and divide by how many there are: forty-seven dollars and fifteen cents. Picture it as the balance point. If the orders were weights on a seesaw, this is where it would balance. But the mean doesn't tell us how spread out the orders are. For that we use variance. Measure how far each order is from the mean, square that distance, and average the squares.
We square for the same reason we did in our cost-functions video: so that distances above and below don't cancel each other out. Variance is measured in squared dollars, which is awkward, so we usually take its square root. That's the standard deviation: here, about six dollars and twenty-nine cents. It's a typical distance from the mean. Look: nine of these thirteen orders, about sixty-nine percent, lie within one standard deviation of the mean. Hold on to that number. Two datasets can have the same mean but very different spreads.
Same average order, but in one shop everybody spends about the same and in the other the amounts swing wildly. The standard deviation tells them apart. One small detail for later. When we use a sample to estimate the spread of a whole population, statisticians divide by m minus one instead of m. It's a correction for the fact that a sample tends to underestimate spread. For describing data we already have, dividing by m is fine.
4:13The normal distribution
If we collect ten thousand orders and draw a histogram, then make the bars thinner and thinner, a smooth shape appears. This bell shape is the normal distribution, and it's everywhere in nature and in business: heights, measurement errors, and many averages and totals, like daily sales. It's described by just two numbers: the mean, which sets its centre, and the standard deviation, which sets its width. And there's a famous rule of thumb. About sixty-eight percent of values fall within one standard deviation of the mean, about ninety-five percent within two, and ninety-nine point seven percent within three.
Remember our sixty-nine percent of real orders? That's the rule showing up in actual data. For a smooth distribution, probability is area under the curve. For orders averaging forty-seven dollars with a standard deviation of six, the chance an order falls between forty and fifty-four dollars is about seventy-six percent: this shaded area. Why is the bell curve so common? Watch this board. Each ball is knocked left or right, at random, by many pegs. Lots of small, independent random effects add up, and the result is a bell shape.
That idea, called the central limit theorem, is why the normal distribution keeps appearing. And notice the formula: its heart is the squared distance from the mean. We'll see why that matters in a moment.
5:41Conditional probability and Bayes' theorem
Now we can solve the fraud mystery. The trick is to stop thinking in percentages and count instead. Picture ten thousand orders. Fraud is rare: only one percent, so a hundred of these orders are fraud and nine thousand nine hundred are honest. The detector catches ninety-five percent of the fraud, so ninety-five of the hundred get flagged. But it isn't perfect on honest orders either. It wrongly flags four percent of them. Four percent of nine thousand nine hundred is three hundred and ninety-six false alarms. Now gather every flagged order: ninety-five real frauds and three hundred and ninety-six innocent customers.
Four hundred and ninety-one alarms, and only ninety-five are real. That's about nineteen percent. The detector isn't bad. Fraud is just rare, so even a small false-alarm rate on a huge number of honest orders swamps the real cases. What we just did is called conditional probability, the chance of one thing given that another has happened, written with a vertical bar. And the formula that does it in one line is Bayes' theorem. The posterior, our updated belief, equals the likelihood times the prior, divided by the evidence.
Ignoring the prior, how rare something is to begin with, is one of the most common mistakes in data work. It matters when we judge any classifier, and we'll come back to it when we measure precision and recall in Module Four.
7:15Expected value
One more tool: the expected value, the average result you'd get if you repeated something many, many times. If one delivery in ten is late, and each late delivery costs Brightcart an eight-dollar refund, then the expected refund per order is zero times nought point nine, plus eight times nought point one. Eighty cents. Small per order, but across ten thousand orders a day, about eight thousand dollars. That's a number Dev would definitely care about. And here's a connection you'll see again. A cost function, like mean squared error, is an average loss over our data.
That's our best estimate of the expected loss on the kind of data the model will face in the real world.
8:03Deep dive: likelihood and cost functions
Deep dive. If you'd rather skip, jump to the next chapter. I'll sum it up at the end. Brightcart watched ten customers: three left and seven stayed. If each customer leaves with some unknown probability p, which value of p best explains what we saw? For any guess of p, the probability of seeing exactly this result is p cubed times one minus p to the seventh. As a function of p, this is called the likelihood. Slide p from zero to one and watch it rise and fall. It peaks at exactly zero point three. Three out of ten.
The data's own proportion is the most likely explanation. Choosing the parameter that makes the observed data most probable is called maximum likelihood, and it's one of the deepest ideas in machine learning. In practice we take the logarithm, which turns products into sums and keeps the peak in the same place. Then we flip the sign, so the peak becomes the bottom of a bowl, and maximising turns into minimising. And this negative log-likelihood is exactly binary cross-entropy, the cost function for yes-or-no classification.
The same thing happens with regression. If we assume a model's errors follow a normal distribution, maximising the likelihood turns into minimising the squared errors, because of that squared distance inside the bell curve's formula. That's mean squared error. So, in one plain sentence: the most popular cost functions aren't arbitrary choices. They come straight from asking which parameters make the data we saw most likely.
9:45Try it yourself: Bayes with spam reviews
Let's use Bayes' theorem on a different problem. Suppose twenty percent of reviews on Brightcart are spam. A detector catches ninety percent of spam reviews, and wrongly flags five percent of genuine ones. A review gets flagged. How likely is it to be spam? Pause and count.
Imagine a thousand reviews: two hundred spam, eight hundred genuine. Ninety percent of the spam, a hundred and eighty, get flagged. Five percent of the genuine ones, forty, are flagged by mistake. So of two hundred and twenty flagged reviews, a hundred and eighty are spam: about eighty-two percent. Compare that with the fraud example, where a similar-looking detector gave only nineteen percent. The difference is the base rate: spam is common here, fraud was rare. Same kind of test, completely different meaning. That's the single most useful lesson in this episode.
10:45Mean vs median, and the power of samples
Here's a statistic trap that appears in business reports constantly. Brightcart's fourteen orders, including one unusual four-hundred-and-twenty-dollar order, have a mean of about seventy-four dollars. But the median, the middle value, is forty-seven fifty. Which better describes a typical order? The median: one large order dragged the mean far away from the crowd. For skewed data like incomes, order values and delays, report the median, along with the spread. Another key idea: you rarely need everyone to learn about everyone.
If one delivery in ten is late, the running average of late deliveries wanders at first, but as you count more and more, it settles close to ten percent. That's the law of large numbers. It's why a well-chosen random sample of a thousand customers can tell you a lot about all of them. But the sample must be random. Ask only customers who complained, and you'll get a badly distorted picture.
11:50Correlation: how two things move together
Finally, a measure we'll use again and again: correlation. It describes how two quantities move together. When customers who visit more also tend to spend more, the two are positively correlated. Correlation runs from minus one, perfectly opposite, through zero, no straight-line relationship, to plus one, perfectly together. For six Brightcart customers, visits and spend have a correlation of zero point nine nine three: almost perfectly in step, which we'll exploit in episode eighteen. Two cautions. Correlation only captures straight-line relationships.
A strong U-shaped pattern can have a correlation near zero. And correlation is not causation, a warning we'll explore properly in episode nine.
12:40Expected value as a decision tool
Expected value isn't just a statistic. It's a way to make decisions under uncertainty. Should Brightcart send a five-dollar coupon to a particular customer? Let's make two business assumptions, just for illustration. A loyal customer is worth about sixty dollars of future profit. And if a customer is about to leave, the coupon persuades them to stay half the time. If the probability that this customer is leaving is p, the expected benefit of sending the coupon is p, times a half, times sixty: thirty dollars times p.
The cost is five dollars. So the coupon is worth sending when thirty times p is bigger than five, which means when p is above about zero point one seven. A customer with a ten percent chance of leaving? Not worth it. A customer with a forty percent chance? Definitely worth it. Notice what just happened: a probability became a business rule. In Module Four, our classifiers will produce exactly these probabilities, and this is how they turn into action.
13:46Myth vs Fact: model confidence
Myth: if a model says it's ninety percent sure, it'll be right ninety percent of the time. Not necessarily. Many models, including large neural networks, can be overconfident, saying ninety percent when they're right far less often. Fact: a model's probabilities are only trustworthy if they're calibrated, and that's something we have to check on test data, not assume. We'll do exactly that later in the course.
14:15Recap
Let's recap. A probability lives between zero and one. Distributions list every outcome and how likely it is. The mean tells us the centre of the data and the standard deviation tells us its spread. The normal curve puts about sixty-eight percent of values within one standard deviation and ninety-five percent within two. Bayes' theorem updates a belief with evidence, and it reminds us never to forget the prior. And maximum likelihood is where our favourite cost functions come from. One sentence to remember: always ask, how rare was it to begin with?
14:53Next: gradient descent
Mission complete. Brightcart can now put numbers on uncertainty. A typical order is forty-seven dollars, give or take about six. And a fraud alarm is only about nineteen percent likely to be real, so Sana's team should review flagged orders, not cancel them automatically. And with that, Module One, the maths toolkit, is complete. Vectors, gradients and probability: you now have the tools. Next, we put them to work. We know which way to turn the dials, so now we'll turn them, step by step, until the model learns. It's gradient descent, the engine of machine learning.
See you on the map.