Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a sequence of numbers has a increasing/decreasing trend in C++

What's the best way to check if a sequence of numbers has an increasing or decreasing trend?

I know that I could pick the first and last value of the sequence, and check their difference, but I'd like a somewhat more robust check. This means that I want to be able to tolerate a minority of increasing values within a mostly decreasing sequence, and viceversa.

More specifically, the numbers are stored as

vector<int> mySequence;

A few more details about the number sequences that I am dealing with:

  • All the numbers within the sequence have the same order of magnitude. This means that no sequence like the following can appear: [45 38 320 22 12 6].
  • By descending trend I mean that most or all the numbers within the sequence are lesser than the previous one. (The opposite applies for ascending trend). As a consequence, the following sequence is to be considered as descending: [45 42 38 32 28 34 26 20 12 8 48]
like image 855
INElutTabile Avatar asked Dec 12 '22 09:12

INElutTabile


2 Answers

I would accumulate the number of increases vs number of decreases, which should give you an idea of whether there's an overall trend to increase or decrease.

like image 159
Kristian D'Amato Avatar answered Dec 13 '22 22:12

Kristian D'Amato


You probably could look into trend estimation and some type of regression like linear regression.

It depends of course on the specific application of yours, but in general it sounds like a fitting problem.

like image 20
murrekatt Avatar answered Dec 13 '22 21:12

murrekatt