I want to compare previous data points in my plot (see image).The red dots are my data points, the black vertical lines are intervals. Now I want my algorithm to have a "mind" and also compare the previous data points to create improved intervals (blue lines) which start at the beginning of the deviation and end afterwards.
I was imagining doing so by using a for loop (going through all the points) and/or with an if-else statement (comparing the points) and trying to keep it general, so other data points can be used. I was thinking of using maybe the standard deviation or sth similar. I honestly dont know how to compare all the points and then set the intervals. Below is my desired output. It would be really great if I could get some help/advice on this.