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

17,146

Solution 1

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.

Solution 2

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.

Solution 3

I think you can simply calculate the median of your sequence and check if it is greater than the first value.
This is ONE way, not THE way.

Another way, always considering average medium, you can check the number of ascending and descending values in the sequence.

int trend = 0;
int avg = mySequence[0]; 
int size = mySequence.size();
for (int i=0; i < size - 1; ++i) {
  if(i > 0) { 
   avg = (avg + mySequence[i]) / 2; 
  }
  (mySequence[i+1] - avg) > 0 ? ++trend; --trend;    
}
Share:
17,146

Related videos on Youtube

INElutTabile
Author by

INElutTabile

Ph.D. in computer science earned at the Université Toulouse III - Paul Sabatier, in international partnership with Sapienza Università di Roma. My main field of expertise is Human-Computer Interaction, Image Processing, Augmented Reality and Web Design and Development. I'm also very fond of User Interface Study and Automation, Sign Language, and Videogames!

Updated on August 22, 2022

Comments

  • INElutTabile
    INElutTabile over 1 year

    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]
    • juanchopanza
      juanchopanza over 10 years
      For this to be a programming question, you would have to specify your algorithm to determine whether the sequence is increasing/decreasing, then try to implement it, then ask a question if you have any problems.
    • Pete Becker
      Pete Becker over 10 years
      Note that this question is not about C++, but about defining "trend".
    • frickskit
      frickskit
      Would your sequence still be descending if instead of 34 you had, say, 200? ie. do the sizes matter or are you just comparing the left number with its right number for all numbers?