AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Anomaly Detection: Finding Outliers

📚 Machine Learning⏱️ 23 min read🎓 Grade 10
✍️ AI Computer Institute Editorial Team Updated: September 2026 CBSE-aligned · Peer-reviewed · 23 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

It is 3:14 AM. Your phone buzzes with a bank SMS: ₹2,500 has been debited from your account via UPI to a merchant you have never heard of. You were asleep. You did not authorise this. Yet somewhere inside your bank's fraud-detection system, software had already compared this transaction against thousands of others you have made over the past few months, decided within milliseconds that it did not belong, flagged it, and fired off the alert you just read — all without a human being anywhere near your account at that hour. Out of the billions of UPI transactions flowing across India every month, how did a machine single out this one payment, at this exact second, as worth a second look?

The answer lies in a branch of machine learning called anomaly detection — the science of teaching a computer what "normal" looks like so precisely that anything unusual stands out on its own, without ever being told in advance what the unusual thing will be. This chapter builds that idea from the ground up: what counts as an anomaly, how to measure "how unusual" something is with actual numbers, and how to trace, step by step, the exact calculation a system like your bank's might run.

What Makes a Data Point "Anomalous"?

You may already have met the word "outlier" while studying box-and-whisker plots or analysing a set of exam scores — a value sitting far outside the rest of the data. Anomaly detection takes that familiar statistical idea and turns it into an automated, real-time decision system. Formally, an anomaly (also called an outlier) is a data point that differs so significantly from the rest of the data that it looks like it was generated by a different process altogether. Notice what this definition does not say: it does not say "an anomaly is bad" or "an anomaly is fraud." A ₹50,000 UPI transfer is not inherently suspicious — if you make it once a year to pay a semester's college fees, it is a normal part of your spending pattern. The same ₹50,000 transfer to a brand-new payee at 3 AM, on an account that has never seen a transaction above ₹2,000, is a different story. Anomaly detection is not about recognising specific bad events in advance; it is about recognising statistical rarity relative to an established pattern.

This is what separates anomaly detection from the classification problems you may already have studied, such as spam-versus-not-spam email filters. A spam classifier learns from thousands of labelled examples of both spam and non-spam messages. But genuine fraud, machine failures, and network intrusions are rare almost by definition — a bank might process a normal transaction pattern for years before encountering a genuinely new kind of fraud it has never labelled before. Because of this, anomaly detection is usually framed as an unsupervised or semi-supervised problem: the system is never shown examples labelled "fraud," only a large history of data assumed to be normal, and it must decide for itself what counts as a meaningful departure from that history.

This single idea shows up across a surprising number of Indian industries. A telecom operator watches call records for a sudden burst of international calls from a SIM that has only ever made local calls — a sign of a cloned SIM. A bottling plant measures the fill volume of every soft-drink bottle on its production line; a bottle filled to 150 ml when every other bottle that hour was 300 ml ± 5 ml points to a malfunctioning filling nozzle, not a design choice. A hospital's continuous patient monitor watches heart rate and oxygen saturation, and a sudden unexplained spike or dip triggers an alert to the nursing staff before a human would have noticed the chart. A login-security system watches account sign-ins and flags "impossible travel" — the same account logging in from Mumbai and then, nine minutes later, from a location physically impossible to reach in that time — a strong signal that a password has been stolen. In every one of these cases, the underlying question is identical: given what "normal" looks like here, does this new data point belong?

The stakes of getting this right are not symmetrical. Missing a genuine anomaly — a fraudulent transaction that slips through, a cracked component on an assembly line that ships anyway, a patient's vital signs drifting dangerously without anyone noticing — can be expensive or even dangerous. But flagging too aggressively has its own cost: a bottling plant that halts the line for every bottle within a few millilitres of target wastes production time on nothing, and a bank that blocks every slightly-larger-than-usual purchase trains its customers to distrust its own alerts. Good anomaly detection is not about catching every unusual point; it is about drawing the line between "normal" and "worth investigating" wherever it best balances these two costs for the specific problem at hand.

Three Flavours of Anomalies

Not every anomaly looks the same, and it helps to name the differences before building detection methods.

  • Point anomalies are single data instances that are unusual all on their own, regardless of context. A single UPI transaction of ₹2,500 sitting among a history of ₹50–90 payments is a point anomaly — you do not need to know anything else about the account to suspect it.
  • Contextual anomalies are values that are only unusual given a specific context, such as time or location. A temperature reading of 40°C is completely unremarkable for Delhi in June, but the same 40°C reading from a sensor in Shimla in December would demand investigation. Spending ₹5,000 on new clothes during Diwali week is ordinary; the identical ₹5,000 spend at 3 AM on an otherwise quiet Tuesday is not — the value is the same, but the context around it changes whether it counts as normal.
  • Collective anomalies are groups of data points that are anomalous together, even though no single point in the group looks suspicious in isolation. This is exactly how a common attack called card-testing fraud works: an attacker who has stolen payment credentials sends a rapid sequence of very small charges — ₹1, ₹2, ₹5 — to dozens of different merchants within a few seconds, just to check which stolen numbers still work before attempting a large charge. Not one of those tiny transactions looks alarming by itself. It is the pattern — many small charges, many merchants, almost no time gap — that is the anomaly.

Most of the rest of this chapter focuses on detecting point anomalies, since they are the easiest to build first-principles intuition for, and the same statistical machinery becomes a building block for detecting the other two.

The Z-Score Method: Measuring "How Unusual" With a Number

To decide whether a data point is unusual, you first need a precise, numerical description of "usual." Two quantities from statistics do this job: the mean (the average value, often written μ) and the standard deviation (a measure of how spread out the values typically are, written σ). A small standard deviation means most values sit close to the mean; a large one means values are scattered widely.

Standard deviation is calculated in four steps. First, find the mean. Second, for every data point, find its deviation — how far it sits from the mean. Third, square every deviation (this removes negative signs and, usefully, punishes large deviations far more than small ones) and average those squared deviations to get the variance. Fourth, take the square root of the variance to bring the units back to the original scale — this final number is the standard deviation.

Once you have a mean and a standard deviation for a "normal" dataset, you can describe any new value's position using a z-score:

z = (x - mean) / standard_deviation

The z-score answers one clean question: how many standard deviations away from the average is this point? A z-score of 0 means the point sits exactly at the mean. A z-score of 1 means it is one standard deviation above average; -2 means two standard deviations below. For data that follows a roughly bell-shaped normal distribution, statisticians rely on the empirical rule: about 68% of values fall within 1 standard deviation of the mean, about 95% fall within 2, and about 99.7% fall within 3. This is why |z| > 3 is such a common rule of thumb for flagging an anomaly — a point that far out has less than roughly a 0.3% chance of occurring under normal behaviour, making it statistically rare enough to be worth a second look.

Worked Example: Catching a Suspicious UPI Transaction

Suppose a UPI app has logged nine of your recent small daily payments — auto-rickshaw fares, canteen bills, printouts — in rupees:

60, 80, 70, 90, 50, 75, 65, 85, 55

This is the baseline: the historical profile of what "normal" spending looks like for this account. Now trace the calculation exactly as a fraud-detection system would.

Step 1 — Find the mean. Add all nine values and divide by 9.

60 + 80 + 70 + 90 + 50 + 75 + 65 + 85 + 55 = 630
mean = 630 / 9 = 70

Step 2 — Find each deviation and square it. Subtract the mean (70) from every value, then square the result.

value  deviation  squared
  60      -10        100
  80       10        100
  70        0          0
  90       20        400
  50      -20        400
  75        5         25
  65       -5         25
  85       15        225
  55      -15        225

Step 3 — Average the squared deviations to get the variance.

100+100+0+400+400+25+25+225+225 = 1500
variance = 1500 / 9 ≈ 166.67

Step 4 — Take the square root to get the standard deviation.

standard_deviation = √166.67 ≈ 12.91

The baseline profile is now fully described: on a typical day, this account spends around ₹70, usually swinging by about ₹12.91 in either direction.

Step 5 — Score the new transaction. At 3:14 AM, a ₹2,500 payment arrives. Compute its z-score against the baseline just built:

z = (2500 - 70) / 12.91 = 2430 / 12.91 ≈ 188

A z-score of roughly 188 means this transaction sits about 188 standard deviations above the account's normal spending — more than sixty times past the usual |z| > 3 threshold used to flag anomalies. There is no ambiguity in this number: under any reasonable model of this account's normal behaviour, a payment this large is not a coincidence. The system flags it and fires off the alert — all before you have even picked up your phone.

For contrast, suppose the next transaction instead is a slightly larger ₹95 canteen bill. Its z-score is (95 - 70) / 12.91 = 25 / 12.91 ≈ 1.94 — noticeably above the account's average, but comfortably inside the |z| > 3 threshold, so it passes through without a flag, exactly as an ordinarily generous meal should.

Cross-Checking with the IQR Method

The z-score method has one honest weakness: it leans on the mean and standard deviation, both of which assume the data is roughly bell-shaped and both of which are themselves sensitive to extreme values. A second, more robust technique sidesteps this by using quartiles — the three values that split sorted data into four equal-sized groups — instead of the mean. This robustness comes from how quartiles are computed: the median and quartiles depend only on the relative ordering of values, not their exact magnitude, so one extreme value — however large — cannot drag Q1, Q2, or Q3 far from where the bulk of the data actually sits. The mean, by contrast, is pulled directly by the size of every value, including outliers.

Sort the same nine baseline values in increasing order:

50, 55, 60, 65, 70, 75, 80, 85, 90

The middle value, Q2 (the median), is 70. Splitting the data at the median gives a lower half (50, 55, 60, 65) and an upper half (75, 80, 85, 90). Q1, the median of the lower half, is the average of 55 and 60: 57.5. Q3, the median of the upper half, is the average of 80 and 85: 82.5. The interquartile range (IQR) is the width of the middle 50% of the data:

IQR = Q3 - Q1 = 82.5 - 57.5 = 25

A widely used convention, often called Tukey's fences after the statistician who popularised it, marks anything more than 1.5 × IQR beyond Q1 or Q3 as an outlier:

lower fence = Q1 - 1.5 × IQR = 57.5 - 37.5 = 20
upper fence = Q3 + 1.5 × IQR = 82.5 + 37.5 = 120

Any transaction below ₹20 or above ₹120 falls outside the fence. The ₹2,500 payment is more than twenty times past the upper fence of ₹120 — a second, independent method, built on entirely different machinery, arrives at the same verdict as the z-score. When two different statistical tests agree this strongly, confidence in the flag goes up substantially.

Anomaly Detection in Code

The entire calculation above is short enough to write as a single reusable function. The code below mirrors the worked example step by step, so you can check every intermediate value against the hand calculation.

def is_anomaly(baseline, new_value, threshold=3):
    n = len(baseline)
    mean = sum(baseline) / n

    squared_diffs = []
    for x in baseline:
        squared_diffs.append((x - mean) ** 2)
    variance = sum(squared_diffs) / n
    std_dev = variance ** 0.5

    z_score = (new_value - mean) / std_dev
    flagged = abs(z_score) > threshold
    return mean, std_dev, z_score, flagged


daily_spends = [60, 80, 70, 90, 50, 75, 65, 85, 55]
new_transaction = 2500

mean, std_dev, z, flagged = is_anomaly(daily_spends, new_transaction)

print(f"Baseline mean: Rs.{mean:.2f}")
print(f"Baseline std dev: Rs.{std_dev:.2f}")
print(f"z-score of new transaction: {z:.2f}")
print(f"Flagged as anomaly: {flagged}")

Running this program prints:

Baseline mean: Rs.70.00
Baseline std dev: Rs.12.91
z-score of new transaction: 188.23
Flagged as anomaly: True

Notice what the is_anomaly function deliberately does not do: it does not compute the mean and standard deviation from a dataset that already includes the ₹2,500 transaction. If it did, that single huge value would drag the mean and standard deviation upward with it, diluting its own z-score and making it harder to detect — a real trap in naive implementations. Instead, is_anomaly builds its profile of "normal" strictly from historical baseline data, then tests each new point against that fixed profile, exactly the way a production fraud-detection pipeline keeps a customer's spending history separate from the live transaction it is currently scoring.

There is a computer-science reason this approach is so widely used in production systems, beyond its statistical simplicity. Computing a mean and a standard deviation from the baseline data takes a fixed number of passes over the list — an O(n) operation — and once that baseline profile is stored, scoring each new transaction against it takes only a subtraction, a division, and a comparison: O(1), regardless of how large the baseline history grows. That is fast enough to score every single transaction in real time, at a scale of millions of transactions a day, without ever becoming the bottleneck in the payment pipeline.

Beyond Simple Thresholds

Z-scores and IQR fences work well when you are watching a single number, like a transaction amount. Real-world anomaly detection often has to watch dozens of features simultaneously — transaction amount, time of day, merchant category, device fingerprint, location — and spotting an outlier across that many dimensions at once needs more machinery. A transaction can look unremarkable on every individual feature — an ordinary amount, an ordinary time, a familiar merchant category — and still be highly unusual as a combination of all of them together, which is exactly the kind of pattern a single-feature z-score can never catch.

Three ideas extend the same core principle into that setting, and are worth knowing by name even before you study them in depth. Isolation Forest builds many random decision trees that repeatedly split the data on random features; because anomalies are rare and different, they tend to get separated from the rest of the data in far fewer splits than normal points do, so a short average path length becomes the anomaly signal. DBSCAN, a clustering algorithm, groups data points that sit in dense neighbourhoods together and simply labels anything left over in a low-density region as noise. Autoencoders, a type of neural network, learn to compress and then reconstruct normal data accurately; when a genuinely unusual input is fed in, the network reconstructs it poorly, and that reconstruction error becomes the anomaly score. Every one of these techniques, however different the mechanics, is still answering the same question this chapter opened with: given a model of what normal looks like, how far does this point sit from it?

Why Anomaly Detection Isn't Foolproof

No threshold is perfect, and real systems are built around this fact rather than pretending it away. Go back to the baseline account, with its mean of ₹70 and standard deviation of ₹12.91. At the standard threshold of |z| > 3, only transactions outside roughly ₹31 to ₹109 get flagged. Loosen the threshold to |z| > 5 and the accepted range widens to roughly ₹5 to ₹135 — a genuinely fraudulent transaction sitting around ₹120 would now slip through unflagged, a false negative. Tighten it too far, to |z| > 1.5, and the accepted range shrinks to roughly ₹51 to ₹89 — narrow enough that ₹90 and ₹50, two perfectly ordinary values from the account's own baseline history, would themselves now be flagged as suspicious: a false positive. Legitimate transactions like a one-time laptop purchase or wedding-season shopping start getting blocked, and users learn to ignore alerts altogether out of frustration, a problem security teams call alert fatigue. Choosing the threshold is therefore a genuine engineering decision, not a fixed constant — it trades the cost of missed fraud against the cost of annoying honest customers, and different products tune it differently on purpose.

There is also a structural difficulty unique to this field: anomalies are rare by definition, so there is rarely enough labelled fraud data to train an ordinary classifier the way you would train a handwriting-recognition model on thousands of balanced examples. This is precisely why the mean-and-deviation approach in this chapter, which needs no labelled fraud examples at all, remains so useful in practice.

This is also why your bank's system almost never blocks a flagged transaction outright. Instead, an anomaly score typically triggers a lighter-weight response: an SMS alert, a temporary hold, or a request for an OTP before a further payment completes. The statistics narrow millions of transactions down to the handful worth extra scrutiny; a human — you, confirming or denying the alert — makes the final call. That 3:14 AM message was never a machine deciding you had been defrauded. It was a z-score of about 188 doing exactly what it is built to do: noticing, with numbers, that a data point did not belong — and asking you to look before anything more happened.

Think About It

Think about this: How would you explain anomaly detection: finding outliers to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind anomaly detection: finding outliers, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← t-SNE and UMAP: Beautiful Data VisualizationARIMA: Time Series Forecasting →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn