EEVblog 1658 - TUTORIAL: Mean vs Median - Page 1
Forum section: electronics / general

author: EEVblog
author_rank: Unknown
timestamp: December 28, 2024, 08:42:03 pm
content: It's important to understand the difference between Mean and Median, not just for engineering, but for everyday life, so you don't get manipulated by people with bad intentions.UBS Global Wealth Report:https://www.ubs.com/content/dam/assets/wm/global/insights/doc/global-wealth-report.pdf

author: schmitt trigger
author_rank: Super Contributor
timestamp: December 28, 2024, 09:06:59 pm
content: Definitely love Dave’s engineering basics videos.

author: golden_labels
author_rank: Super Contributor
timestamp: December 29, 2024, 05:28:59 am
content: What is the average number of whiteboards falling in a video?Dave hit my pet peeve. I expected as much after I saw what was on the Fallen Whiteboard. I became certain when the Patreon email arrived. And now I’m struggling with writing a post without making it a wall of text nobody is going to read.I’ll post the comment later, if I manage to make it fit for consumption.Meanwhile, while trying to assemble what I wanted to say, a note more objective in its nature. I believe there is an error in this video: the example with current measurement. The total energy consumed includes the peaks. It’s the integral of current over time, and that is exactly equal to time multiplied by the mean, not the median. They may of course contribute close to nothing. True. But if this is the case, there will also be close to no difference between the mean and the median.

author: EEVblog
author_rank: Unknown
timestamp: December 29, 2024, 07:05:48 am
content: Quote from: golden_labels on December 29, 2024, 05:28:59 amIf you have a product with a predictable repeatable current pattern, then yes, that would be true.My point was that if you measured a bunch of data points to find a base current consumption of your widget, and it happened to include a random large current spike, then that could eronerously increase the overwise "base current" specification.Now that I think about it, not the clearest example, I nuked it.

author: Kleinstein
author_rank: Super Contributor
timestamp: December 29, 2024, 09:14:21 am
content: It depends on the application if the arithmetic average or the median is more suitable. Both have there merits. One point is that if there are no outliers, the arithmetic average gives the lowest scattering / noise with normal distributed data.

author: golden_labels
author_rank: Super Contributor
timestamp: December 29, 2024, 02:23:56 pm
content: Quote from: EEVblog on December 29, 2024, 07:05:48 amIf that’s just a random spike in data, it indeed changes things. I assumed we’re talking about a device, that would exhibit that repeated pattern.The signal in the example is unfortunate for one more reason. I get, what you meant. But the graph shows something, where the median takes a particularly bad value. It’s equal to where the bottom segments are.

author: golden_labels
author_rank: Super Contributor
timestamp: December 29, 2024, 05:28:13 pm
content: Ok, I guess this is what the example was meant to show.Let’s say we have a microcontroller and want to measure how much power it consumes. The power consumption varies a bit, of course, but is fairly stable. However, during measurement we also capture some spikes. They may be accidental, nonrepetitive events. Or a normal occurance, but coming from another device on the same power line. In other words: they are unwanted outliers.Taking mean of all the data may give a wrong number. The ideal solution is to ignore them and calculate the mean from the remaining data. But that is not always convenient in a lab setup. So to get a rough estimate, we may use the median. The value is still wrong, but more useful in this scenario.A simple example below, using simulated, random data:The median is obviously much closer to what we want to measure (“Good value”, 3rd chart) than the mean. But I have chosen the seed(1)to not make the case too nice. The median is certainly better here: at least it lies within the current actually drawn and not hanging somewhere in the air. But still notably off.This isn’t a quirk. It’s a consequence of the second problem. Unlike the mean, the median is confined to values actually observed. Usually it will coincide with levels at which the data often sits. This is why I mentioned Dave’s example is unfortunate. The median there is bound to the lowest level. How could this be a problem? Imagine the microcontroller sleeps for 3/4 of the time and the remaining 1/4 is responsible for 99.99% of the power draw. The median will act as if the chip never drawn energy. This is why checking graphs instead of blindly looking at the numbers is so important.(2)The third and fourth observations: the median isn’t “most common value”. It’s easy to fall into this trap, if all we see is nice, gaussian-ish curves. And human brains are not that good at comparing sizes. You’d swear the two bumps right of 1.4 are larger than that single, only minimally taller bump on the left. Wouldn’t you?(1)2619361415; python 3.13.1, numpy 2.2.1.(2)I’m happy to have any hair left, given how many I wanted to tear my hair seeing people use e.g.SASwithout even considering looking at the charts.Mandatory Xkcd.Python script used to generate this:Code:#!/usr/bin/env python# SPDX-License-Identifier: CC0-1.0# Copyright © 2024 mpan; <https://mpan.pl/>import matplotlib.pyplot as pltimport numpy as npimport randomimport timedef generate_mcu_curve(*, n=1000, vscale=0.75, noise=0.1, reps=4,extras_scale=8, extras_width=0.01, extras_n=8):# n: number of points# vscale: scale signal shifts# noise: scale of the random noise to add, None for none# reps: signal repetitions# extras_*: scale, width and number of extra peaksnTotal = nn = n // repsx = np.linspace(0, 1, nTotal)y = np.ones(n)for i in range(0, 4):t0 = int(n * random.random())t1 = t0 + n // 2r = random.gauss(0, vscale / 3)if t1 < n:y[t0:t1] += relse:y[t0:] += ry[:t1-n] += ry = np.concatenate( * reps)for i in range(0, extras_n):t1 = nTotalwhile t1 >= nTotal:t0 = int(nTotal * random.random())t1 = t0 + int(extras_width * nTotal)y[t0:t1] = extras_scaleif None is not noise:y += np.random.normal(0, noise / 3, nTotal)return (x, y)def run():seed = time.monotonic_ns() % 2**32random.seed(seed)np.random.seed(seed)extrasScale = 10n = 1000x, y = generate_mcu_curve(n=n, extras_scale=extrasScale)mean = np.mean(y)q2 = np.median(y)total = sum(y) / ncutoff = extrasScale / 2goodTotal = sum(v for v in y if v < cutoff) / nprint(f'Seed: {seed}')print(f'Mean: {mean:.3f}')print(f'Median: {q2:.3f}')print(f'Cutoff: {cutoff:.3f}')print(f'Sum: {total:.3f}')print(f'Good sum: {goodTotal:.3f}')plots = plt.figure().subplot_mosaic([['sig'], ['hist'], ['histZoom']])sigPlt = plots['sig']histPlt = plots['hist']histZoomPlt = plots['histZoom']sigPlt.set_title('Mean vs median in a signal with outliers')sigPlt.set_xlabel('Time (no unit)')sigPlt.set_ylabel('Value (no unit)')sigPlt.set_ylim([0, max(y) * 1.05])sigPlt.plot(x, y, c='gray', label='Signal')sigPlt.plot([0, 0.25], [mean, mean], ls=':', c='red', label=f'Mean {mean:.3f}')sigPlt.plot([0, 0.25], [q2, q2], ls='--', c='blue', label=f'Median {q2:.3f}')sigPlt.legend()counts, b, p = histPlt.hist(y, bins=300, color='gray',label='Histogram (detail below)')countsMax = max(counts)histPlt.set_ylabel('Count')histPlt.set_xlim([0, max(y) * 1.05])histPlt.plot([mean, mean], [0, countsMax], c='red', ls=':', label=None)histPlt.plot([q2, q2], [0, countsMax], c='blue', ls='--', label=None)histPlt.legend()ySub = [v for v in y if v < cutoff]counts, b, p = histZoomPlt.hist(ySub, bins=50, color='gray', label=None)countsMax = max(counts)histZoomPlt.set_xlabel('Signal value')histZoomPlt.set_ylabel('Count')histZoomPlt.set_xlim([min(ySub) * 0.95, max(max(ySub), mean) * 1.05])histZoomPlt.plot([mean, mean], [0, countsMax], c='red', ls=':', label=None)histZoomPlt.plot([q2, q2], [0, countsMax], c='blue', ls='--', label=None)histZoomPlt.plot([goodTotal, goodTotal], [countsMax / 2, countsMax / 2],'x', c='blue', label='Good value')histZoomPlt.legend()plt.tight_layout()plt.show()if '__main__' == __name__:run()

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 30, 2024, 01:28:39 am
content: I've found I rarely use "average" to describe any discrete one-dimensional data sets anymore, and tend to use "minimum", "maximum", and some set ofk-th percentiles, usually including "median" (k=50 if the number of data points is odd).(If you sortNmeasurements into entries 1 toN, inclusive, you can find the approximatek-th percentile measurement at entryround(N·k/100). IfNis odd, thenk=50 is median, exactly.)k-th percentiles can be expanded into error bars around median. One possible notation is \$\left.m\right.^{+a}_{-b} (\text{p}\%)\$, where \$m\$ is the median value (k=50), \$a\$ is the difference between value atk=(100+p)/2 and median, \$b\$ the difference between median and value atk=(100-p)/2, and p is the percentage of data points the entire range covers, centered on median. Most often the parenthesized range of included results is omitted, and instead the error bars shown are described generally as 1σ ("one sigma error bars", corresponding to p=68.3), 2σ ("two-sigma error bars", p=95.4), or 3σ ("three-sigma error bars", p=99.7), derived from the fact that in a standard/Gaussian distribution 68.3% of values are within one standard deviation of the median, 95.4% of values are within two standard deviations of the median, and 99.7% of values are within three standard deviations of the median. If p is not explicitly or implicitly defined, the error bars are 1σ by convention, and the range bracketed by the error bars cover 68.3% of all samples. If \$a ≃ b\$, it is common to use \$m \pm a\$.Note that such error bars do not assume that the shape of the distribution is Gaussian, though: they can be used with any distribution, and even with discrete sets of samples where you do not know (or examine) the shape at all. In fact, I've found error bars the most useful measure ofstatistical dispersion(=variability,scatter,spread) for discrete data sets both in physics and in numerical computation in general.If you look at your results as a histogram or graph, it can be useful to pick p by hand so that it optimally includes valid samples but excludes invalid/erroneous/suspicious samples. The larger p is, the more reproducible your test/measurement setup is –– but the larger the error bars, the less specific the result is. By picking p by hand, you choose the balance between the two, and readers can assume you did so to the best of your ability, balancing the usefulness of the results with your trust in the measurement setup/run/iteration count.

author: Xena E
author_rank: Frequent Contributor
timestamp: December 30, 2024, 01:03:55 pm
content: Good topic.As an aside statisticalmanipulationstudy, can provide very useful, and surprisingly accurate, otherwise hidden information. Particularly if you are a politician.For example:From the figures on Daves whiteboard of his resistor samples I determine that approximately 9.1% of his stock of resistors, (ie the last sample), are old Allen Bradley carbon composition types, which without a doubt means that he is a closet audiophile that likes building his own period correct valve amplifiers out of noisy crap old resistors that have drifted way high in value.The figures dont lie, 😁

author: rsjsouza
author_rank: Super Contributor
timestamp: December 30, 2024, 01:12:00 pm
content: I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"Great video, Dave!

author: golden_labels
author_rank: Super Contributor
timestamp: December 30, 2024, 01:25:28 pm
content: Quote from: rsjsouza on December 30, 2024, 01:12:00 pmNoooooo!First of all, the video is fine!That single example at the end didn’t really work too well. But that’s the only problem and my long post above is, I hope, explaining what was the goal.The comment you referenced was about a small pet peeve of mine: the “median vs mean” topic. I was expecting having to comment on that, when the video is finally released. And hopefully I will, if I manage to put it into a reasonably concise post.

author: Xena E
author_rank: Frequent Contributor
timestamp: December 30, 2024, 02:13:31 pm
content: Quote from: golden_labels on December 30, 2024, 01:25:28 pmJust a joke, play on words! From someone who put it in the comments of the video.The replies that Kleinstein, you and Nominal Animal have posted are also very enlightening, good additions...X

author: coppice
author_rank: Super Contributor
timestamp: December 30, 2024, 02:19:00 pm
content: Quote from: rsjsouza on December 30, 2024, 01:12:00 pmPeople just can't resist returning to the mean. The joke comment was spoiled by using mean instead of average.

author: Xena E
author_rank: Frequent Contributor
timestamp: December 30, 2024, 02:26:38 pm
content: Quote from: coppice on December 30, 2024, 02:19:00 pmThat would be better but could be misinterpreted as being mean.

author: rsjsouza
author_rank: Super Contributor
timestamp: December 30, 2024, 06:10:00 pm
content: Indeed sarcasm and humor do not translate well in writing...That was a joke, folks. And I appreciated the word "mean" being used instead of "average" for the additional shock factor. I suspect it was indeed effective.

author: TimFox
author_rank: Super Contributor
timestamp: December 30, 2024, 08:10:32 pm
content: According to one of my statistics textbooks, there is a useful approximate empirical relationship:(mean - mode) = 3 x (mean - median).For a normal distribution (Gaussian), of course, mean = median = mode .

author: golden_labels
author_rank: Super Contributor
timestamp: December 30, 2024, 08:37:11 pm
content: Xena E,rsjsouza: no problem. Miscommunication happens. I took the fragment about expectation as a reference to my post, and “the guy” me.I’m not offended. I just didn’t want to have my post taken as being an overwhelmingly negative reaction to Dave’s video.My language use makes people often interpret it contrary to what my intention was or what mood I expressed.TimFox: everything agrees for the normal distribution too. Look: (µ - µ) = 3 · (µ - µ).

author: TimFox
author_rank: Super Contributor
timestamp: December 30, 2024, 08:40:29 pm
content: Yes.

author: EEVblog
author_rank: Unknown
timestamp: December 31, 2024, 10:43:41 pm
content: Quote from: golden_labels on December 30, 2024, 01:25:28 pmI agree, which is why I removed that part.

author: EEVblog
author_rank: Unknown
timestamp: December 31, 2024, 10:49:23 pm
content: Quote from: rsjsouza on December 30, 2024, 01:12:00 pmLots of good mean jokes

author: thm_w
author_rank: Super Contributor
timestamp: December 31, 2024, 10:50:48 pm
content: https://ledidi.com/academy/measures-of-central-tendency-mean-median-and-mode

author: schmitt trigger
author_rank: Super Contributor
timestamp: January 01, 2025, 12:42:06 am
content: “Mean” is one of those English word that is very suitable for wordplay, thanks to its different mean-ings.Mean: averageMean: nastyMean: identify

author: Psi
author_rank: Super Contributor
timestamp: January 01, 2025, 01:01:15 am
content: yeah, it always annoys me when people talk about their salary vs the mean/avg salary, instead of the mode or median which is a lot more useful.

author: ballsystemlord
author_rank: Frequent Contributor
timestamp: January 01, 2025, 02:16:23 am
content: So Dave was a math teacher, not an EE, all along? Who knew!

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 02, 2025, 09:52:21 am
content: I finally realized a good example of mean, median and mode: when designing a high-efficiency power supply for a black-box-like device.Average or mean power draw describes the cost of running the device. It does not actually tell what kind of power the supply must provide.Median power draw is the power that suffices for half the time. (While it is not that useful here, extending it tok-th percentiles, median being the 50th percentile of the power draw, is.)To size the power supply, you might look at say 99.7%k-th percentile (to find the power that suffices 99.7% of time), then add suitable margin. (In practice, you'd also look at the power draw "spikiness", and compensate with bulk capacitance if possible, or by increasing the power provided otherwise.)Mode of the power draw is the most typical power draw. One might consider optimizing this when designing a high-efficiency supply. If the power draw is such that there is no single typical power draw (value) at all, then the mode is undefined. Not all statistical distributions have a mode, after all.

author: iMo
author_rank: Super Contributor
timestamp: January 02, 2025, 10:07:07 am
content: I've been using the median for on the fly filtering the incoming data from my 34401A. It removes outliers like random EMI spikes etc and does low-passing as well.Btw there are industrial sectors (like Health) where using the "average/mean" is almost considered a swear word (the Health sector is The World of Outliers)..Also - not sure the median is less compute intensive compared to average/mean (as it has been said in the video).Try to calculate the average/mean of 1000 floating point numbers vs. median of 1000 fp numbers (the sorting is pretty expensive exercise compared to 1000 adds and a single divide).

author: golden_labels
author_rank: Super Contributor
timestamp: January 02, 2025, 12:56:44 pm
content: Being introduced to statistics with gussian-ish distributions leads to developing some poor, or outright wrong, intuition. Took me years to recover, and I guess I’ll never be completely cured.One of the mistakes is unconditional belief in the mode ≤ median ≤ mean (or mirrored) rule. It doesn’t hold universally. Multimodal distributions often shred it to pieces:But unimodal ones may also produce unexpected results:Distribution design by Glen_B of StackOverflow(1)Another common, but wrong intuition is with long tails. With the normal distribution we’re used to interpreting extreme values as an irrelevant. Similar to how a cone filled with liquid holds most of it in the widest section. While this is true for the normal distribution, it is not in general. A tail may look unimpressive on a graph. But it may contain a significant part of the population.(1)Both smooth and empirical distributions with that property also exist. But they are either not showing the effect to that extent, or are empirical data that with unknown underlying model. I chosen Glen_B’s one, as it’s readily available, simple, and the effect is dramatic.

author: coppice
author_rank: Super Contributor
timestamp: January 02, 2025, 04:41:25 pm
content: Quote from: golden_labels on January 02, 2025, 12:56:44 pmDon't most people start with linear distributions, like tossing a die, then work up to the idea that munging a lot of those together tends towards a Gaussian distribution, often with its tails trimmed by the linear distribution having limits?Quote from: golden_labels on January 02, 2025, 12:56:44 pmThey do tend to show some poorly chosen examples in books and presentations, but it should be obvious that the mode can be practically anywhere. Many things combine a strong tendency to one value, while everything which isn't that value is more or less Gaussian. You see these narrow spikes imposed on a bell like distribution all over the place.Quote from: golden_labels on January 02, 2025, 12:56:44 pmPeople are pretty weak on the effect of the tails, especially when there is asymmetry in them.Let's say you use the common practice of producing a Gaussian like distribution by summing 12 random integers, and dividing by 12. The tails are very much trimmed. Try using that to model a rare event and you will probably conclude the rare event never actually occurs. You have none of the tail areas which show up many of those rare events.Asymmetry in the tails gets really interesting. If you can get to zero on one side, but the other is unbounded you often tend towards a Pareto distribution instead of a Gaussian one. If people have studied enough stats to get beyond the intuitive idea that munging a lot of distributions together tends towards Gaussian, and learn the central limit theorem properly, they should have noted that the requirement towards Gaussian requires a stable mean. No stable mean, means the distribution will probably be something funky, Pareto being one of the interesting forms of funkiness.

author: Picuino
author_rank: Super Contributor
timestamp: January 02, 2025, 05:26:27 pm
content: It is especially different in measurements that are bounded on one side, but not bounded on the other. For example, in wages.http://news.bbc.co.uk/2/hi/uk_news/magazine/7581120.stm

author: TimFox
author_rank: Super Contributor
timestamp: January 02, 2025, 06:52:47 pm
content: An interesting example of the statistics of resistor production lots: Two reports cited on p. 56 of D SelfSmall signal audio designFocal press 2015.(His discussion of improving statistical accuracy with multiple resistors.)1. He cites H Kroeze, data from 211 10 k\$\Omega\$ 1% metal film resistors (from the same batch): mean 9995(0.05% low), apparently Gaussian distribution with standard deviation 10(0.1%).2. His own data from 100 cheap Yageo 10001% metal film resistors: mean 997.66, std dev 2.10(0.21%), one outlier at 0.7%, all others within 0.5%, again apparently Gaussian.His conclusion is that the manufacturer doesn't select the best parts from a production run to sell at tighter tolerance. He suspects that old-style carbon composition resistors may be subject to that selection.

author: golden_labels
author_rank: Super Contributor
timestamp: January 03, 2025, 12:08:07 pm
content: Quote from: coppice on January 02, 2025, 04:41:25 pmIntroduction toprobabilisticsis indeed the binomial and uniform distributions. But instatisticsI’d say it’s the normal distribution. But you have a point: for some people it might be the binomial distribution, that builds the concept of irrelevant tails.Quote from: coppice on January 02, 2025, 04:41:25 pmI would argue it’s not obvious, in particular for unimodal distributions. All common distributions obey the inequality and most well-known do too. While in strict sense they’re not rare, it’s actually hard to find a theoretical distribution that doesn’t observe the mean-median-mode inequality, if you want it to be elegant or, worse, smooth and not complicated. This is why I didn’t even try, but used somebody else’s work.Quote from: TimFox on January 02, 2025, 06:52:47 pmThe notched normal distribution in resistors has been a common belief in the past. Unfortunately I don’t know, if that was true or not. I was hoping to test it one day. But it is too late now. For a long time 1% and 5% ranges come from different processes, not from binning. Now we would need to measure banana’s curvature.(1)(1)This common anti-EU disinformation was indeed a distorted presentation of thebinning process used to mark bananas for wholesale market.

author: coppice
author_rank: Super Contributor
timestamp: January 03, 2025, 12:32:27 pm
content: Quote from: golden_labels on January 03, 2025, 12:08:07 pmAll common distributions relate to one driving force, and they do not generally have funky modal values. However, in the real world many things combine multiple distributions you might find in a book. For example, you might measure all the distances walked by all employees in their day at work. You'll probably find a fairly Gaussian mix of short journeys around the campus, but one strong line much longer than that, which is the distance from the local train station where they arrive in the morning and leave at night. The overall distribution you are seeing is a mix of two (or more) fairly Gaussian distributions. This is extremely common, and you have to be pretty dumb not to realise this blending of distributions is going to happen all over the place.

author: golden_labels
author_rank: Super Contributor
timestamp: January 03, 2025, 12:53:09 pm
content: Some interesting quirks in distributions.Maturais in Poland an exam you may take after finishing high school. There are three mandatory subjects, Polish language, maths, and a foreign language. You may also take additional subjects, as well as pass some exams at “extended” level. To pass, you need to collect 1/3 of points for each subject. Some histograms.First, the most famous one, the basic Polish exam (horizontal scale is the number of points (score)).The pronounced anomaly around 1/3 points (the threshold): you can guess yourself, where this came from. If you need a hint: those are written essays, judged subjectively by the teachers.Another one, from extended Polish exam. You can still see a small anomaly by the threshold, but what is more interesting are the ends:This is a great example of the truncated gaussian distribution. With many other cases we only assume gaussian-like behavior, while the underlying model doesn’t allow for longer tails. Or it does, but the histogram is cut to shape (as with Picuino’s example). But here it may be an actual case of the rating process collecting all extra-good students into a single, final bin. And of course the 0, which collected not only poor performance, but also all people who for one reason or another should really be on the negative side.Now let’s discuss some weirder occurances. You’d assume normality or at least something like Poisson distribution, right? Here is the basic maths level:Other than once more teachers trying to squeeze out points to push students over the thresholds and the “genius collector” bin, what on Earth is this?You think it may be Poisson overlaid with normal distribution from very good maths pupils, who didn’t choose the extended exam for some reason? Well, then look at the extended level:I don’t even know, what happens here. In particular note the mode is below the threeshold, despite this is an exam which pupils choose themselves.Finally, if you think that all exams produce the hump, possibly left-skewed, a surprise. This is basic English:I have a hint to the examining agency: maybe, just maybe the English exam is too easy, if it captures only the far low side of the actual distribution?Source (in Polish):Grabowska, Grabowska et al. “Osiągnięcia maturzystów w 2011 roku: sprawozdanie z egzaminu maturalnego w 2011 roku”, Centralna Komisja Egzaminacyjna 2011