Hacker News new | past | comments | ask | show | jobs | submit login
Where to wait for an elevator (2010) (johndcook.com)
105 points by bumbledraven on July 6, 2022 | hide | past | favorite | 107 comments



"Since you can’t move without increasing the average distance, you must have started at the best spot."

Technically this isn't a fully sound inference. All this proves is that you're at a local optimum. So you'd also need to know or show that there is only one optimum/the problem is convex/local=global or any variant.

Of course that is the case here, but it's always worth noting the specific properties of a problem that make a neat solution possible.


The author addressed this point in a comment on the article:

> True, it’s not a rigorous proof. You can find a rigorous proof here:

> “A Simple Noncalculus Proof That the Median Minimizes the Sum of the Absolute Deviations.” Neil C. Schwertman, A. J. Gilks, and J. Cameron. The American Statistician, Vol. 44, No. 1 (Feb., 1990), pp. 38-39.

That paper fits on one page, not counting references. Here's a link: https://tommasorigon.github.io/StatI/approfondimenti/Schwert...


> "Since you can’t move without increasing the average distance, you must have started at the best spot."

> Technically this isn't a fully sound inference. All this proves is that you're at a local optimum. So you'd also need to know or show that there is only one optimum/the problem is convex/local=global or any variant.

You can't move away from the median without increasing the average distance over all doors, when you're standing on the median.

But that's just a special case of the also-easy-to-prove fact that you can't move away from the median without increasing the average distance over all doors, no matter where you're standing. You can show that by exactly the same argument used in the post - if you move away from the median, your total distance to all doors will increase, because you moved away from more than half of the doors.

This suffices to show that -- if there is a global optimum superior to the local optimum at the median door -- that global optimum cannot be located in the neighborhood of any point on the real number line, which is to say it cannot be located at any finite distance from the median. Or in other words, no such global optimum can exist.


The best real-world optimization: stand aside to make room for anyone exiting the elevator before crowding the doorway trying to walk in too quickly!


Great, you just made the problem more complicated.

That's is because if there are people in the elevator and you right in front of it, you have to move away, wait, and go back. Not ideal, it makes more sense to wait on the side of the elevator, possibly at a distance proportional to the expected time it takes for the passengers to leave.

That would make the ideal waiting spot somewhere between the middle elevator and the other one that is the furthest away from the middle elevator, but not necessarily the mean.


Depends a bit on assumptions, but it's not quite what I expected, actually:

    import math
    import random

    B = 1000
    elevators = [-6, 0, 3]

    def time_to_enter_distribution(position):
        for _ in range(0, B):
            elevator = random.choice(elevators)
            distance = elevator - position
            # What matters is not how many people are in the elevator, but how
            # quickly they can evacuate. Approximate Poisson distribution.
            evac_time = [0, 1, 2, 3, 3, 4, 5][random.randint(0, 6)]

            # While you walk to the elevator, you give time for people to evacuate
            # and your time passes.
            evac_time -= abs(distance)
            time = abs(distance)

            # If people are still evacuating when you have arrived, you have to wait
            # for that to end by the side of the elevator, and then take the final
            # 0.5 metres to step inside.
            if evac_time > 0:
                time += evac_time + 0.5

            yield time

    def time_to_enter_summary(position):
        distr = list(time_to_enter_distribution(position))
        sum_time = sum(distr)
        sum_sq_time = sum(t**2 for t in distr)
        mean = sum_time/B
        sd = (sum_sq_time - sum_time**2/B)/(B-1)
        se = sd/math.sqrt(B)
        return (mean, se)

    if __name__ == '__main__':
        print(f'{"pos":5s}  {"t":4s} {"se":3s}')
        for p in (i/4 for i in range(-6*4, 3*4)):
            (t, se) = time_to_enter_summary(p)
            print(f'{p:5.2f}: {t:4.2f} ({se:3.2f})')


Oh my god, this.


Might be a bit off-topic…

But: The one big question I have with elevator UX is this: why isn’t it possible to deselect a choice? So pressed 10th floor but wanted 11th. There is no way to press button 10 and then button 11 to correct that mistake.

Will I ever solve this mystery?


Because in a busy environment (where consistently more than one person uses the elevator) the scenario of making a wrong decision and having the wish to correct it occurs far less than the scenario where someone does not pay attention to which floors have been selected already and deselects your destination. At the worst case this means that you both miss your stop because you both did not notice.


Might also get confusing if the LED indicating the button's been pressed stops working


This comment right here is an example of thinking like a good designer. No amount of sleek surfaces and cool lighting can beat this.


Leave the button recessed after it's been selected, even if the LED fails (how often does this actually happen?) it's still a visual and tactile indicator.

To deselect the floor, simply press in again.


Those buttons are expensive and fail relatively quickly. It's a good idea, though.


Remove the buttons entirely and replace with a QRcode/NFC thing where each rider can vote/unvote where he personally wants to go. This can also work outside before the elevator arrives so a routing system can decide to bundle up a bunch of people going to the same floor in one express elevator. Haven't thought out how the ux would look to keep the wrong people out of that one elevator.


Many elevators in tall buildings have you select the floor when you arrive at the elevator bank, then it assigns you an elevator (usually denoted with letters A, B, C, D etc). When that elevator opens you get in (along with other people who were assigned that elevator), and it will stop at your floor (as well as other floors for other people).


Otis has a touch-screen system where you enter the exact destination: https://www.otis.com/documents/256045/11235683/22032_Compass...


> Remove the buttons entirely and replace with a QRcode/NFC thing where each rider can vote/unvote where he personally wants to go.

My local pizza place plays music based on customer voting through a smartphone app. It's not necessary to be there in order to vote - you just need the app.

This elevator design could be fun in similar ways.


Yeah, that's why I added NFC to at least approximate presence.


Now you made the buttons even more complicated, requiring some sort of magnet or motor in addition to a simple LED. And what do you do when that fails?


Also, malicious fighting over selecting and deselecting destinations


Yes, some elevator environments are busy. But many aren't.


There are two cases where deselecting a floor would complicate the logic:

1. When the currently targeted floor is deselected and the remaining selected floors are all in the other direction: Then the elevator would have to either reverse course without stopping — a situation which otherwise doesn’t occur and would be unexpected and possibly dangerous for the passengers — or stop on a non-selected floor before changing direction, which would be confusing as well.

2. The deselected floor was the only selected floor: The elevator would then have to choose a floor to stop on by itself — e.g. the next one it can safely stop on — which is also a situation that doesn’t otherwise occur. In addition, deselecting the only selected floor doesn’t seem to be a practical use case.

So instead of having to define, implement and test a behavior for those odd situations, it’s safer and less costly to just disallow deselection.


I didn't think elevators permitted scenario 1 to occur at all; I thought they refused to allow selecting floors not in the direction of travel.

(I don't usually board elevators traveling in the wrong direction, ofc., so, I'm not that sure.)


I haven't done it in a while but my recollection is that if you press a "wrong-direction" floor it will just go there after having completed all the previously-selected stops.


not just previously selected but it will wait until there are no stops in the current direction, then switch directions and go in the reverse order


Yeah I wasn't sure what will happen if more people add in stops in the "right" direction afterwards, I don't think I've ever been in that situation, but that makes sense.


The one in my building will allow you to select any floor at any time. it always goes to the last stop in the current direction before reversing as far as i have noticed


I've experienced both kinds of elevators. Some don't let you select a floor that's in the wrong direction, so you can notice you entered the wrong elevator and get back out.


Rather than testing these scenarios, couldn't one simply just(tm) not allow deselection in these scenarios?


Then you’d have to test that. And it would still be confusing to users that deselecting only sometimes works.


Right.

HN is not representative of the average person, or even a standard cross section of society. A elevator has to be legible to an 8 year old child, an 80 year old retiree, someone who is blind, a person who is deaf, etc.

When designing for such a broad audience, KISS (keep it simple, stupid)


I imagine that's by design. Otherwise you could unselect other people's choices.


Yes of course it’s by design. I just wonder why.


In Marc-Uwe Kling's "Qualityland", reversing elevators, turning around subway cars, and flicking traffic lights is a level-skill obtained by accumulating social credit points.

[1] https://www.amazon.com/Qualityland-Visit-Tomorrow-Marc-Uwe-K...


Not worth complicating the UX to address such a minor issue.


This is it exactly. It opens up the door to even more complexity in the design...

Now that you can unselect floors, if the elevator is on its way to the 5th floor and you unselect it, should it skip that floor mid-movement?

If there's only one floor selected, we'll probably want to disallow unselecting it while the elevator is moving, but what if you just hopped on and it hasn't moved yet?

This would probably make for an interesting interview question.


Minor to you... Introducing a great experience is the job of good UX. Not simply limiting options and saying 'done'. I am sure there are better options to handle this. Seems like no-one has put the effort into it (yet).


Unnecessarily stopping at a floor for a few seconds is minor to literally everyone riding in a public elevator.

But yeah, I'm sure that at all of the various elevator companies, over all of the years that elevators have had electronic controls, nobody has bothered to put any thought into this.


Limiting options is exactly the point. Elevators are supposed to be easy to use for the entire population, including the very young, the very old, those with disabilities, those not able to read or write the dominant language, etc.

In these cases, it is simply the best UX to keep everything really simple.


Because you could deselect other peoples choices.


This isn't an argument, as you can also select other people's non-choices.


There's some incentive not to do that, however, because in many cases it will inconvenience you just as much as everyone else. But if you could just deselect all the floors between the current one and your destination, there's a pretty strong incentive there.

Until someone punches you out, of course, because it'd be hard to hide and you're in a confined space with them...


Elevators sometimes have an anti-mischief feature where they will deselect every floor if all the floors are selected in a brief period of time. The cab will slow to a stop if no new buttons are pressed. It’s kind of eerie, actually.


Neat! How do you know this?


The elevators at my office were upgraded a few years ago and one of the elevator techs happened to mention it to me. I tried it out while alone, for obvious reasons.


Cool. I was hoping for something other than "I exhaustively test all new elevators".


That's a one way ratchet. Once selected, the fight is over.

If you can go back and forth, it would probably get violent.


That doesn't prevent people from getting to their destination.


It isn’t an argument. It’s a statement of fact.


How do I go about deselecting other people's choices, if that's my specific goal? I can get whatever I want in Jira, but nothing in elevators. It's fucked.


Maybe ask a woman?


Elevators in Japan often support this; usually either a double-tap or press-and-hold the button to deselect.


Some elevators are clever enough to detect kids messing with them. If you select enough floors, they'll just deselect everything soon, including your errant selection.

Admittedly, that's not very helpful unless you somehow knew that the elevator you were in had been configured in such a way.


> But: The one big question I have with elevator UX is this: why isn’t it possible to deselect a choice? So pressed 10th floor but wanted 11th. There is no way to press button 10 and then button 11 to correct that mistake.

In many elevators, one can double-press a button to deselect it.


Now I have a lot of testing in front of me.


The elevators in my building do support this. People get confused all the time by accidentally deselecting the floor because they didn't notice it was already lit up. Also, the "G" light is broken in one of the elevators, which causes even more confusion (until you realise that it's not broken on the second, lower set of buttons for people in wheelchairs).


I suspect you are thinking like a lawful-good individual engineer. You are solving a niggle, without considering the human and engineering compromises you would discover if you worked in the industry.

You are perhaps trying to optimise for a one-person elevator trip, rather than thinking of what is the optimum for a repeated game with many concurrent trip players that includes both cooperator players and defector players. An elevator is a common good to its users.

If people could cancel a destination, then there is less incentive for people to have to prethink their destination before getting on the elevator. Leading to more likely to waste concurrent elevator users’ time in the long run?

If you could cancel a destination, defector players could cancel your destination to speed up their trip (with resulting policing and status meta-games etcetera).

Elevators mostly have the lowest-common-denominator for a UI. Where is the incentive for elevator purchasers (building procurement or facilities) to demand an improved UI? Would a manufacturer get any profit by providing an improved UI? Would such a UI be accessible to the blind? Discoverable? Usable? Cleanable? Predictable (e.g. elevator arriving indicators on destination floors)? Safety? Complexity?

There are a huge number of factors to building anything complicated.

Also see https://danluu.com/sounds-easy/ and https://fs.blog/chestertons-fence/ and all of the elevator wiki starting here https://elevation.fandom.com/wiki/Fire_service_mode_(EFS)

Disclaimer: I’m not an elevator engineer. I feel your pain: I want a government department of niggles which catalogues minor complaints and gets them fixed. Although I admit I am pessimistic enough to think it would only end up increasing the total number of niggles due to second-order effects.


Every single elevator in Korea allows you to deselect a floor by simply pushing the button again.

You'd be surprised how often it comes in handy and have never had an issue with anyone deselecting someone else's floor.


It seems like every lift I've been in in China ( been here for 8 years) has this functionality where if you press the same button in rapid succession about 5 to 15 times, it will depress.


In the Philippines (tested in Manila) high rises this seemed to be a common feature: Double clicking a selected floor would deselect.


If I were to guess, people enter the elevator and subconsciously just press which floor they want to go to. If you allow deselecting, someone will inadvertently deselect where they want to go to if that button is already selected.


I agree. Should be a "hold the button for 5 seconds to deselect"


In smaller installations (e.g. private homes) this is the functionality provided by some lifts: push once to select, double push to de-select a floor.

In larger/public settings, offering this would be counterproductive, for the reasons listed by sibling comments.


Some elevators have this! The one I encountered was, press the button until it starts flashing, release and press again to deselect the floor.


I have been to a few elevators in India where you can deselect a choice like you described. However they are not rare.


Every elevator in Korea works this way. Pressing once selects, pressing again deselects.


Perhaps you could answer the two questions here, preferably relating to a named elevator model: https://news.ycombinator.com/item?id=32005810

And maybe answer my question too: how do blind people know if they are selecting or deselecting?

Watching foreign button-mashers must be amusing!


clearly they aren't using "destination dispatch" where the elevator tells you where to wait after you tell it where you want to go.

https://en.wikipedia.org/wiki/Destination_dispatch

I've only seen this in one building. It was a little weird, but it supposedly is coming soon to more and more elevators.


I live in a building that has this. As a resident - it works.

Whoever made this building made a whole lot of mistakes though:

- They put this into a building with public spaces (e.g. a doctor's office).

- When standing in front of the elevators, nowhere does it say which office is on which floor (it says so in the lobby which you walked by to get to the elevators)

- The screens where riders have to select floors automatically turn off after not being used for a few minutes. At that point, to people who have never used them before, they do not look like they have anything to do with the elevators at all.

- When these screens do turn on, they list the floors in reverse order (i.e. highest first). That makes sense because the people on the highest floors are the most likely to use the elevators, right? Well, all the offices are on floors 1 through 4. They're on page two of the screen. The button for "next page" is not labelled as such. You have to know it's there.

- The screens are resistive touch screens and not well calibrated. The buttons to select a floor are approximately the same height as the tip of your index finger. A first time user won't hit the correct floor the first time, guaranteed.

- At the point where you are standing in front of the elevators, you are so deep into the building that you have 0 cell reception. I have randomly encountered food delivery people be stranded there because they could not call their customers to figure out how to use the elevator.

Every single week I have to rescue a poor 80 year old person who needs to go to the doctor and is horribly lost and confused, and it just breaks my heart how the building's owner (which I've contacted about this to no avail) chose "oh the salesperson said this was more efficient and trendy" and now a large percentage of users are so much worse off for that choice.


This is such a bad use for touch screens. I worked in a building that installed them on the elevators and it was such a terrible experience. They were properly calibrated capacitive touch screens, but it was still very likely that you wouldn't get the selection you wanted. Humans can't actually aim their fingers that accurately.


I wonder if there is any improving that system without replacing it.


There are some easy fixes to the things I described - like adding a list of offices and floor numbers above the touchpads, or adding a phone signal boosting indoors antenna.

Apart from that, the first thing I wondered when I moved in was "oh, as a resident, does that mean I just get a keyfob I can wave at this screen that knows I live on the X-th floor?". That's also feasible, to the degree that the Wikipedia article linked above refers to this.

The problem with all these fixes is of course that they all cost more than $0.

And neither the company owning this building nor any landlord company involved in its operation is even based in the same ZIP code as the building. So why would they care?


> So why would [the landlord] care?

A complaint from one of many residents may be futile, but a complaint from the retail tenant with the largest space would carry more weight. Surely showing that this elevator decreases the value of their building would be something they'd care about, if delivered correctly.


Most of the problem described here is the input mechanism. The building I work in has "destination dispatch" too but instead of a touchscreen, it has a large numeric keypad that you type your floor number on, with physical buttons that look like normal elevator buttons. We do also have some floors that are open to the public and i've only seen people confused by it a couple of times in several years of working here.


If you want a fun challenge, try making your own elevator algorithm! https://play.elevatorsaga.com/


At the end of the workday, not having several full elevators stop on their way down to the lobby to try (and fail) to take you down from the second floor is a godsend to everyone involved.


2nd floor? You need the exercise.


I stayed at a hotel that used this. It's a very nice, efficient system. Much better than everybody cramming into the next elevator that arrives and selecting a bunch of floors.


I worked in a building at NYC that did this. It was confusing at first as I just immediately jumped into the first elevator door that opened since it was my first day and didn't realize there were no buttons inside. After the initial and very short learning curve due to all the stairs I had to climb that day I would say it was much more efficient than any other building I had been in.


I encountered one that did this, but the screen for selecting your floor was a temperamental touch screen, so trying to type "13" would often select "1" and "11" before 13, consigning you to riding and stopping at those floors, also.

Then I found the secret wheel-chair button assortment, which was genuine buttons.


> standing at the mean minimizes the average squared distance

This is true, but I find it more intuitive to think about the signed distance: if you define signed_dist(x, y) = x - y, then the mean is the value that makes the average signed distance closest to 0. Of course in reality distance is unsigned – if you walk 1m left then 1m right, you've walked 1m + 1m = 2m, not 1m + -1m = 0m, so the signed distance is not appropriate.

Here is a practical example where the aim is to get the average signed difference to be closest to 0: You want to count a large number of nails by weighing them. To do that, you need to know the weight of a single nail. But the nails have slightly different weights, so you need to pick some representative value. Question: Should you pick the mean or the median? Answer: You should pick the mean, because when you weigh all the nails together the differences will cancel out.

So the rule is:

- To minimise the average value of the absolute difference, pick the median.

- To minimise the absolute value of the average difference, pick the mean.


> But the nails have slightly different weights, so you need to pick some representative value. Question: Should you pick the mean or the median? Answer: You should pick the mean, because when you weigh all the nails together the differences will cancel out.

Well, yes, that is the definition of the mean.

But you can't know what the mean is without having already answered your original question of "how many nails are there?", so as a formal matter this is completely useless advice.

What you actually want to do is pick out a number of nails that you do know exactly, say 10, weigh them, and use the mean weight of that sample to estimate the mean weight of the entire supply of nails.


> But you can't know what the mean is without having already answered your original question of "how many nails are there?", so as a formal matter this is completely useless advice.

Yes, obviously you'd estimate the mean/median by taking a small sample (or using a published value). I left that detail out because it's not relevant to the choice between mean and median.


I still don't really see the point of the observation. It's weird to say "You should pick the mean, because when you weigh all the nails together the differences will cancel out". That's true because the mean is defined as the value that achieves that result. It's like answering the Monty Hall problem by saying "you should pick the door with a car behind it, because that door has a car behind it".

You're asking what the mean weight of a bundle of nails is and then pointing out that the mean weight is a better estimate of the mean weight than the median weight is. Why is that worth noting?


I was trying to come up with a scenario where you have to choose between the mean and the median, and the correct choice is the mean due to signedness (in contrast with the elevator example, where the correct choice is the median because the distance is unsigned). Do you have a better example?

It should be obvious that the mean is the correct representative value for weighing nails. My point is that there are cases that call for the mean and cases that call for the median, and the distinction can be explained by considering the signedness (without having to consider squared distances).

Maybe the following variation on the nail example would help: Suppose the manufacturer of the nails must publish a reference value for the weight of a nail, and will be fined for every nail that is not exactly the correct weight. The fine is proportional to the absolute error. Question: Should the manufacturer publish the mean or the median in order to minimise fines? Answer: The median.

Why is the mean the correct representative value for counting nails, but not for minimising fines? I'm arguing that it's because when the nails are weighed together the errors are signed, but when the fines are calculated the errors are unsigned (in the same way that the distances to the elevators are unsigned).


It is an interesting example, but after some thought it's basically identical to the elevator example. It is so similar that I have some doubts about whether I could correctly extrapolate a principle based on those two examples.

> Why is the mean the correct representative value for counting nails, but not for minimising fines? I'm arguing that it's because when the nails are weighed together the errors are signed, but when the fines are calculated the errors are unsigned (in the same way that the distances to the elevators are unsigned).

And here, there are two points that bother me.

First, the mean is the correct representative for counting nails because that's how it's defined. A mean is just the answer to any question of the form "if all of these data points were equal to each other, what is the value they would all share?". That's the question you're asking about the weight of the nails.

Second, I find it difficult to believe in your stated explanation of why you might choose the mean over the median (or vice versa), because we've already observed that the mean minimizes the sum of squared errors, and squared errors are all unsigned. You're relying on the assumption that, in the above example, the fine (which should be minimized) is directly proportional to difference from the estimate. But that assumption doesn't appear in the explanation of why one metric or the other will minimize the fine.


> You're relying on the assumption that, in the above example, the fine (which should be minimized) is directly proportional to difference from the estimate. But that assumption doesn't appear in the explanation of why one metric or the other will minimize the fine.

Yes, my argument is specifically for the case where the fine etc. is directly proportional to the difference (or absolute difference). I'm pointing out that the two cases are similar apart from the signedness.

To put it mathematically, I'm just observing that if you have a set {x_i}, then:

- avg{|x_i - y|} is minimised by setting y to median{x_i}

- |avg{x_i - y}| is minimised by setting y to avg{x_i}

The excerpt in the article says "the mean minimizes the average squared distance" (i.e. setting y to avg{x_i} minimises avg{(x_i - y)^2}), which is correct but not obvious (the proof is non-trivial, and if you were to ask the students who chose the mean why they chose it I bet none of them would say "because it minimises the squared distance"). So I find it odd to include that statement. If I was the author I would have said "the mean minimizes the average signed distance", which I find more intuitive and think is a better explanation of why those students chose the mean. (Also, I like how "average value of the absolute difference" and "absolute value of the average difference" are the same words just in a different order.)


Why is it not the first elevator in my direction? I’ll need to walk the same distance if it ends up being the third. If it is the first I saved walking to the median and back.


I thought the same thing - the way the problem is stated doesn't explicitly discount the distance walked before reaching any of the elevators, so logically the answer will depend on the configuration of the lobby and where you first enter it...or at least the position of the elevator call button. E.g. if the call button is to the left of all 3 elevators, it definitely makes sense to walk to the first of those, but any further is adding distance that will be doubled if that leftmost elevator is the first to arrive. I'm pretty sure even if the next two elevators are 100s of metres away, the mean distance walked from the moment you press the call button will go up if you start walking towards them, but haven't tried proving it.


> So standing in front of the second elevator minimizes the expected distance to the next elevator, assuming all three elevators are equally likely to arrive next.

> What if you want to minimize the worst case instead of the average case? Stand half way between the first and third elevators.

What is the difference between "in front of second" and "halfway between first and third"?


The elevators are not evenly spaced. This point is in the article but could have been belabored a bit more. If they are evenly spaced, there is no difference.


In the first paragraph he says the elevators aren’t evenly spaced, and then again, it is mentioned in the James Hadley quote.

In this scenario, the second elevator isn’t centered between the first and the third elevators.

If I understand the description properly, the layout may be something like this:

[]__[]____[]


From the problem statement (first two sentences of the post):

> Imagine a bank of three elevators along a wall. The elevators are in a straight line but they are not evenly spaced.


I assume this must mean that the elevators are not equidistant from eachother. As in 1 is 10 feet from 2 is 20 feet from three.


They’re not necessarily evenly spaced. The middle elevator might be a lot closer to one side than the other.


Reminds me of this: https://en.wikipedia.org/wiki/Rectilinear_Steiner_tree#Singl...

The best "trunk" to minimize total distance to all points is the median of their positions.


Is there any intuitive explanation for why the mean minimizes the squared error? I know it IS true - this property is used for linear regressions etc - but I couldn’t actually explain it.

It seems like whatever minimizes a squared error should itself have some squares in it.


Distances are squares, actually square roots of squares, in Euclidian space, so it makes sense to find squares when we deal with distances.

An intuitive way to look at it is that square function is the simplest function that goes down until it reaches a minimum, and then back up again. If you want this property, you can expect to find squares somewhere. Distance have this property: if you move in a straight line, the distance to your target will go down as you are closing in, then you reach it, then if goes back up again after you pass it.

That's the general intuition for where the squares can be. Let's be a bit more formal.

Let's flip the problem and try to minimize the sum of the squared distances (or squared error, same thing). In 1D, the sum of the squared distances to a and b is (a-x)^2 + (b-x)^2 = a^2 - 2ax + x^2 + b^2 - 2bx + x^2 = a^2 + b^2 - 2(a+b)x + 2x^2

It is a parabola, the minimum is where the derivative is zero. The derivative is -2(a+b) + 4x, solve -2(a+b) + 4x = 2 for x and you have x = (a+b)/2, which is your average. Of course, it works with more than 2 distances.

Another intuition: to minimize something, usually, we take a derivative, and when we take the derivative of something squared, the square tends to disappear, so it shouldn't surprising that the minimum of something squared has no squares in it.


> An intuitive way to look at it is that square function is the simplest function that goes down until it reaches a minimum, and then back up again

By what measure of simplicity is x^2 simpler than abs(x)?

I mean, if you added the caveat “with a continuous derivative”, sure.


By what measure of simplicity is x^2 simpler than abs(x)?

Its description is shorter in English and in most programming languages, mainly because it doesn't have a conditional. (Which is related to it being differentiable everywhere).


If you know about inertia, the grabiy center is where inertia is minimized. But it's just a way to rephrase it.


Suppose there are a bunch of points on a number line (elevators in the example) and you are standing somewhere between them. You decide to minimize the sum of squared differences by the greedy algorithm - make a small move either left or right, whichever makes it smaller, then re-evaluate and repeat.

First just consider one point, A. The squared distance from that point is a parabola with its apex at A, or as a formula (x - A)^2. The derivative of that is 2x - 2A. So if 2x - 2A > 0, moving to the right makes the squared distance go up, moving to the left makes it go down. If it's < 0 then the opposite is true. If it equals 0 then you have minimized the squared distance to A. All this is trivial, in the case of a single point, because x = A is obviously the solution (the mean of a single value is that value).

But you want to minimize the sum of the squared differences. So you are checking sum(2x - 2Ai) where Ai are all the different points. This means comparing 2nx with sum(2Ai), if n is the number of points. Or equivalently, you can divide by 2n and compare x with sum(Ai)/n. This formula is just the mean of the points. If this is greater than x, you should move a bit to the right. If it is smaller, you should move to the left. If equal, you have minimized the sum of squared differences.

We didn't prove uniqueness but we've established that the derivative of the sum of squared distances has only linear terms in it, so a little more calculus will easily allow us to check that.

tl;dr: To minimize a function requires an equation in that function's derivative. To minimize sum of squared errors we solve an equation with only linear terms.


Well ok, but if you listen carefully you can hear which elevator is coming/closest and always be standing directly in front of the one that is arriving.


Whelp, now I'm gonna think about minimizing the distance to every elevator every time I'm in front of more than 2 elevators... thanks


So why is it that if you select 2 and 3 you don't get to floor 5?


You mean 6?


I mean 8




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: