0
$\begingroup$

Forgive me as I am no mathematician.

What would be appropriate ways of defining “almost flat” mathematically ?

I have a rolling series of values that are the output of a moving average function.

The goal is to identify when the trend is flat or “almost flat”.

We are also talking about what are numerically small changes (i.e. behind the decimal point).

For example, something like this would be classified as a light down trend :

1.22122
1.22058
1.22019
1.21970
1.21930

However, something like this would be “almost flat”:

1.21564
1.21550
1.21552
1.21543
1.21578

I have tried to do a little research on the matter, and once possibility I’ve come up with is linear regression slope, but given the small changes between values, from my limited understanding I think that would be better suited to more significant changes

  • 2
    Your second example is different from your first in two ways: the successive differences are smaller, and the values fluctuate rather than descend. Are both these differences important? Just one of them?2017-01-12
  • 0
    One option is to look at relative changes. Let $a_i$ be the moving average after having seen $i$ values. Then, one way to define the relative change is $r_i = \frac{a_{i + 1} - a_i}{a_i}$. Clearly, depending on what you think is important, you can add absolute values in the numerator and/or denominator. Then, you could call it `almost flat' if, for example, $\sum_{j = i - 5}^i | r_j | < \epsilon$, where you have picked $\epsilon$ beforehand.2017-01-12
  • 0
    @MeesdeVries Thank you for your questions. In my case, I am always looking at the most recent X values (e.g. the last 10 values). Within that series of values, I don't mind if there are fluctuations, as long as the overall tendency is "almost flat".2017-01-12
  • 0
    @Ritz Interesting idea. Your post reminded me of something. Given the small numerical changes involved, would I be right in thinking an option would be to use log diff (e.g. ret=log(a/a-1) )2017-01-12
  • 1
    @LittleCode, for instance, would you consider the sequence $0,1,0,1,0,1,0,1,0,1$ "almost flat"? What about the sequence $0.999, 0.998, 0.997, 0.996, 0.995$?2017-01-12
  • 0
    @MeesdeVries Your binary sequence, yes, because it is ultimatley remaining within a defined range. The decimal sequence, strictly speaking there is a clear decline in the second half of your sequence (0.997,0.996,0.995). However for the purposes of my application, the last decimal place could be ignored to a reasonable extent and so yes that would be "almost flat". I need to allow for a little scope around the realities of life and that a perfect range behaviour is highly unlikely ! Hope this makes sense !2017-01-12
  • 1
    @LittleCode, that makes total sense. With those two answers I am pretty sure your initial conclusion was correct: I think you should compute the slope of the linear regression, and then decide what counts as "flat enough". For example, the first of the two sequences in my previous comment will give slope 0; the second will give slope $-0.001$. So perhaps a good threshold for you will be something like: it is flat if the absolute value of the slope is below $0.0005$.2017-01-12
  • 1
    The only thing I would add to that is that you might want to scale the slope by the values you are working with. For example, you might consider $10000, 9999, 9998, 9997, 9996$ "almost flat" but not $10, 9, 8, 7, 6$ although they have the same slope (namely $-1$). In that case, you might want to divide the slope by the average value -- in that case, the first one has a scaled slope of slightly less than $-.0001$, while the second has a scaled slope of $-0.125$.2017-01-12
  • 0
    @MeesdeVries Thanks for bearing with me and helping come to a conclusion. I will investigate slopes again along the lines you suggest, sounds great !2017-01-12

0 Answers 0