Author Topic: EEVblog 1658 - TUTORIAL: Mean vs Median  (Read 11341 times)

0 Members and 3 Guests are viewing this topic.

Offline EEVblogTopic starter

  • Administrator
  • *****
  • Posts: 42146
  • Country: au
    • EEVblog
EEVblog 1658 - TUTORIAL: Mean vs Median
« on: December 28, 2024, 08:42:03 pm »
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

 
The following users thanked this post: Xena E

Offline schmitt trigger

  • Super Contributor
  • ***
  • Posts: 2500
  • Country: mx
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #1 on: December 28, 2024, 09:06:59 pm »
Definitely love Dave’s engineering basics videos.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2430
  • Country: pl
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #2 on: December 29, 2024, 05:28:59 am »
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.  :palm: 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.
Why 📎 | We live in times when half of people have IQ below 100.
 
The following users thanked this post: EEVblog

Offline EEVblogTopic starter

  • Administrator
  • *****
  • Posts: 42146
  • Country: au
    • EEVblog
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #3 on: December 29, 2024, 07:05:48 am »
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.

If 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.
« Last Edit: December 29, 2024, 07:16:42 am by EEVblog »
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 17194
  • Country: de
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #4 on: December 29, 2024, 09:14:21 am »
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.
 
The following users thanked this post: Xena E

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2430
  • Country: pl
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #5 on: December 29, 2024, 02:23:56 pm »
If 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.
If 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.  :)




« Last Edit: December 29, 2024, 02:28:11 pm by golden_labels »
Why 📎 | We live in times when half of people have IQ below 100.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2430
  • Country: pl
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #6 on: December 29, 2024, 05:28:13 pm »
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. SAS without even considering looking at the charts. Mandatory Xkcd.


Python script used to generate this:
Code: [Select]
#!/usr/bin/env python
# SPDX-License-Identifier: CC0-1.0
# Copyright © 2024 mpan; <https://mpan.pl/>
import matplotlib.pyplot as plt
import numpy as np
import random
import time

def 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 peaks
   
    nTotal = n
    n = n // reps
    x = np.linspace(0, 1, nTotal)
    y = np.ones(n)
   
    for i in range(0, 4):
        t0 = int(n * random.random())
        t1 = t0 + n // 2
        r = random.gauss(0, vscale / 3)
        if t1 < n:
            y[t0:t1] += r
        else:
            y[t0:] += r
            y[:t1-n] += r
   
    y = np.concatenate([y] * reps)
   
    for i in range(0, extras_n):
        t1 = nTotal
        while t1 >= nTotal:
            t0 = int(nTotal * random.random())
            t1 = t0 + int(extras_width * nTotal)
        y[t0:t1] = extras_scale
   
    if None is not noise:
        y += np.random.normal(0, noise / 3, nTotal)
   
    return (x, y)

def run():
    seed = time.monotonic_ns() % 2**32
    random.seed(seed)
    np.random.seed(seed)
   
    extrasScale = 10
    n = 1000
   
    x, y = generate_mcu_curve(n=n, extras_scale=extrasScale)
   
    mean = np.mean(y)
    q2 = np.median(y)
    total = sum(y) / n
    cutoff = extrasScale / 2
    goodTotal = sum(v for v in y if v < cutoff) / n
    print(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()
« Last Edit: December 29, 2024, 05:29:44 pm by golden_labels »
Why 📎 | We live in times when half of people have IQ below 100.
 
The following users thanked this post: EEVblog, thm_w, Jacon, Xena E

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #7 on: December 30, 2024, 01:28:39 am »
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 of k-th percentiles, usually including "median" (k=50 if the number of data points is odd).

(If you sort N measurements into entries 1 to N, inclusive, you can find the approximate k-th percentile measurement at entry round(N·k/100).  If N is odd, then k=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 at k=(100+p)/2 and median, \$b\$ the difference between median and value at k=(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 of statistical 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.
« Last Edit: December 30, 2024, 01:34:34 am by Nominal Animal »
 
The following users thanked this post: EEVblog, golden_labels, Xena E

Offline Xena E

  • Super Contributor
  • ***
  • Posts: 1309
  • Country: gb
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #8 on: December 30, 2024, 01:03:55 pm »
Good topic. :-+

As an aside statistical manipulation study, 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, 😁
« Last Edit: December 30, 2024, 01:06:38 pm by Xena E »
 

Offline rsjsouza

  • Super Contributor
  • ***
  • Posts: 6196
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #9 on: December 30, 2024, 01:12:00 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"

Great video, Dave!
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 
The following users thanked this post: EEVblog

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2430
  • Country: pl
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #10 on: December 30, 2024, 01:25:28 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"
Noooooo! :(:(:(

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.
Why 📎 | We live in times when half of people have IQ below 100.
 

Offline Xena E

  • Super Contributor
  • ***
  • Posts: 1309
  • Country: gb
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #11 on: December 30, 2024, 02:13:31 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"
Noooooo! :(:(:(

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.


Just 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
« Last Edit: December 30, 2024, 02:22:44 pm by Xena E »
 

Offline coppice

  • Super Contributor
  • ***
  • Posts: 10289
  • Country: gb
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #12 on: December 30, 2024, 02:19:00 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"

Great video, Dave!
People just can't resist returning to the mean. The joke comment was spoiled by using mean instead of average.
 

Offline Xena E

  • Super Contributor
  • ***
  • Posts: 1309
  • Country: gb
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #13 on: December 30, 2024, 02:26:38 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"

Great video, Dave!
People just can't resist returning to the mean. The joke comment was spoiled by using mean instead of average.

That would be better but could be misinterpreted as being mean.

 :o
 

Offline rsjsouza

  • Super Contributor
  • ***
  • Posts: 6196
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #14 on: December 30, 2024, 06:10:00 pm »
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.
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Online TimFox

  • Super Contributor
  • ***
  • Posts: 11888
  • Country: us
  • Retired, now restoring antique test equipment
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #15 on: December 30, 2024, 08:10:32 pm »
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 .
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2430
  • Country: pl
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #16 on: December 30, 2024, 08:37:11 pm »
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 · (µ - µ). ;)


« Last Edit: December 30, 2024, 08:39:31 pm by golden_labels »
Why 📎 | We live in times when half of people have IQ below 100.
 

Online TimFox

  • Super Contributor
  • ***
  • Posts: 11888
  • Country: us
  • Retired, now restoring antique test equipment
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #17 on: December 30, 2024, 08:40:29 pm »
Yes.
 

Offline EEVblogTopic starter

  • Administrator
  • *****
  • Posts: 42146
  • Country: au
    • EEVblog
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #18 on: December 31, 2024, 10:43:41 pm »
First of all, the video is fine! That single example at the end didn’t really work too well.

I agree, which is why I removed that part.
 

Offline EEVblogTopic starter

  • Administrator
  • *****
  • Posts: 42146
  • Country: au
    • EEVblog
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #19 on: December 31, 2024, 10:49:23 pm »
I loved the comment where the guy says: "I was expecting an awesome video, but overall it was quite mean"

Lots of good mean jokes  :-+
 
The following users thanked this post: rsjsouza

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 9764
  • Country: ca
  • Non-expert
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline schmitt trigger

  • Super Contributor
  • ***
  • Posts: 2500
  • Country: mx
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #21 on: January 01, 2025, 12:42:06 am »
“Mean” is one of those English word that is very suitable for wordplay, thanks to its different mean-ings.

Mean: average
Mean: nasty
Mean: identify
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12590
  • Country: nz
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #22 on: January 01, 2025, 01:01:15 am »
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.
« Last Edit: January 01, 2025, 01:03:31 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline ballsystemlord

  • Frequent Contributor
  • **
  • Posts: 482
  • Country: us
  • Student
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #23 on: January 01, 2025, 02:16:23 am »
So Dave was a math teacher, not an EE, all along? Who knew! ;D
« Last Edit: January 01, 2025, 02:27:18 am by ballsystemlord »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: EEVblog 1658 - TUTORIAL: Mean vs Median
« Reply #24 on: January 02, 2025, 09:52:21 am »
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 to k-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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf