Suppose you want to find $k$ that minimises your cost function $J_D(k)$ for the whole dataset $D$. We may want to apply batch gradient descent or stochastic gradient descent. Let's deliberately initialise $k$ with the same number $k_0 = 1$ for both BGD and SGD to see the difference in their behavior.
If you apply BGD, the whole process may look like this:
On the other hand if you apply SGD, this optimisation may look like this:
In both pictures the blue solid curve represents the cost function $J_D(k)$. But in the second picture there are also dotted curves. In my experiment I used batch size which was $10\text%$ of the whole dataset $D$. So each dotted curve represents the cost function $J_B(k)$ for the current batch $B$. From these dotted curves you can see that the gradient $\nabla J_B\left(k_0\right)$ happened to be large multiple times in a row for different batches $B$. That's why the point was "pushed" to the deeper minimum.
As I understand we use SGD hoping that there is such a big $\nabla J_B\left(k_0\right)$ for some batch $B$ so that the red point is "pushed" to jump out of this local minimum for another chance at arriving at a better minimum.
But I'm stuck here.
Why does stochastic gradient descent lead us to a minimum at all? Why can't it escape all the minima?
Why do we think that another local minimum is going to be deeper than the initial one? I don't believe that we just hope that our new minimum is going to be good enough. With the same success we could randomly choose a value for $k$.
If we don't think that another local minimum is going to be deeper, then how is SGD supposed to avoid local minima problem? Our red point can end up in a minimum that is shallower (higher) than the initial one, e.g.:
If BGD looks for the nearest minimum, then what kind of minimum does SGD look for?
How do we know for certain that it's not going to escape a deeper minimum? How deep should it be?
What kind of minima do we expect stochastic gradient descent to get stuck on and why do we think it's going to be deeper than the minimum we can obtain with a normal gradient descent?
How SGD is supposed to avoid local minima problem if all it can is just push us to jump out of a local minimum? I mean it doesn't look for a better one but only wandering along the curvature.
As a side note, $J_D(k) = \frac 1n\sum_{i=1}^n\left(\sin\left(kx_i\right) - y_i\right)^2$.
EDIT 1:
Need some clarification of @WhoDatBoy's answer.
Since we randomly selecting each batch, the single batch's distribution is going to be similar to that of the whole dataset. And this distribution uniquely determines the distribution of residuals of each batch. That's why each batch's gradient is going to be similar to that of the whole dataset. Is that right?
Now, I perfectly understand why the red point can't usually escape from wide minima: it's very unlikely to select a batch with gradient that differs from the whole dataset multiple times in a row.
However there is still a thing that confuses me. You said that SGD was not invented to be robust against local minima. But it's told to be likely to reach a better minimum than the initial one. And I can see why in the case when the red point was initialised in some local minimum near a wide minimum: there's a chance that some batch's gradient will push it from the shallow minimum towards the wider one. But what if our cost function looks like this:
Some batch can push the point to the left in the shallower minimum. The point can stay there for a while and after that it can be pushed again towards even shallower minimum.
Question 1: Is it highly unlikely case, since there are always batches seeking to push the point to the right?
Or consider the following situation:
Despite the fact that the initial minimum is deeper, but it's very narrow. So, the point can be easily pushed out of it towards the shallower minimum. In both these cases SGD can fail.
Question 2: I'm not sure about the first one but in the second plot we definitely can't say that the red point is likely to find a better minimum, right? I mean, all the minima are too narrow for the point to stay there.
Question 3: Is it true that only wide minima can hold the point (no matter deep or not)? And how wide should it be depends on the batch size we choose?
Question 4: And since we don't know it in advance, we just try to guess its size, right?
Question 5: It turns out then, the depth of the minimum doesn't play much role in holding the red point. It's the width of the minimum that matters?
Question 6: Do we assume something before applying SGD? If yes, then what exactly? I mean is there some kind of assumption of the form: "SGD is likely to find a better minimum if the curvature does not have only narrow minima and is not too hilly".
EDIT 2:
All over this edit I assume that we have the same batch size and the same learning rate and, for the sake of simplicity, assume that all those minima $A$, $B$ and $C$ (denoted below) have the same width (but different depth).
CONFUSION 1: In your Question 5 answer you said that the depth is important. Doesn't it mean that the deeper the minimum is, the harder it is for the red point to escape that minimum? Thus, we can conclude that the red point is likely to stuck in relatively deep minima when using SGD. The word "relatively" is used because the depth that is able to hold the red point depends on the batch size and the learning rate: the smaller the batch size and the bigger the learning rate, the deeper minima the red point is looking for. By "looking for" I mean that the red point is going to get stuck in such minima. However, we don't know how deep the minimum has to be in absolute value.
CONFUSION 2: It's still unclear why the red point is likely to get stuck in deeper minima. Suppose, for the sake of example, that we have a dataset of $100$ observation and 3 minima in our cost function curvature: $A$, $B$ and $C$. The respective errors (values of our cost function) at those minima are: $\mathrm{error}(A) = 100$, $\mathrm{error}(B) = 10$ and $\mathrm{error}(C) = 0$.
Now, when the red point gets into the minimum $C$, then each of $100$ observations has $0$ error and therefore $0$ gradient. So, whatever batch you choose it's going to have $0$ gradient, since its gradient is the sum of gradients of the observations the batch consists of. Consequently, it's impossible for the red point to escape from the minimum $C$.
And here is my main confusion. Why is the red point less likely to escape the minimum $B$ (the deeper one) than the minimum $A$? It would be nice to explain it in the following way: "since the $\mathrm{error}(A) > \mathrm{error}(B)$, then the gradient of each observation is smaller in the minimum $B$ and therefore every batch now has smaller gradient which causes smaller ability to push the red point out of the minimum $B$". But the problem is that we can NOT claim such a thing, since the error reduction in the minimum $B$ compared to the minimum $A$ could be caused by a single observation. I mean, if the error of a single observation, say the first one out of our 100 observations, reduced significantly, then it would cause a reduction in the error of our cost function. But the rest of the observations has the same error as before and therefore the same gradient. And since we're randomly picking the batch on each iteration, our red point can still be pushed out of the minimum $B$ with the same probability (am I wrong in here regarding the same probability?).
CONFUSION 3: It becomes even more confusing when the reduction in the error of a single observation is not the case, and the error of our cost function is reduced due to the fact that overall error reduced in some observations is greater than the overall error raised in other observations. The minimum would be deeper in this case, but how to show that the red point is now less likely to escape from this new deeper minimum?
I want to note here that in my understanding, the reduction in the cost function does not mean either the reduction in the gradient of the whole dataset or the reduction in the gradient of some individual batch. Then how on earth can the reduction in the cost function (and this is exactly what the deeper minimum means) mean smaller ability to escape the minimum?