How to create a scoring algorithm based on the interactions of two trends?

57 Views Asked by At

I have two variables represented as a time-series with monthly observations. I am attempting to create a scoring system -1 to 1 where: 1 represents a strong shift in trend from B to A and -1 represents a strong shift in trend from A to B. Ultimately, I am looking to score the magnitude of trend change / momentum from A to B (vice-versa).

For example, given the sample data points in Example 1 below, the trend in A drops instantly and the trend in B increases. So the score should be close to -1.

Example 1

A = {20, 0, 0, 0, 0}
B = {0, 20, 15, 18, 30}

Conversely, the sample data points in Example 2 below illustrates the trend in B dropping instantly and the trend in A increases. So the score should be close to 1.

Example 2

A = {0, 5, 10, 12, 8}
B = {5, 0, 0, 0, 0}

Lastly, the sample data points in Example 3 below is mixed and the score would be negative but not as negative as in Example 1.

Example 3

A = {5, 8, 7, 4, 3}
B = {0, 0, 2, 5, 10}

I tried perhaps utilizing correlation or Granger causality, but did not quite address the requirements. Tried to search around but could not quite hone down to a feasible solution. Is there a general formula for scoring the trend changes between A and B?

1

There are 1 best solutions below

0
Scriddie On

The first thing that comes to mind would be the Pearson Correlation Coefficient, also called Pearson's R. It is a measure of linear correlation and takes values between -1 and 1. You can compute it in Python for example like this:

from scipy.stats import pearsonr

# Sample data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

# Calculate Pearson's correlation
corr, _ = pearsonr(x, y)

print(f"Pearson correlation coefficient: {corr}")

Now I see that this does not fit the description of your examples (Pearsons R would be close to -1 in all three cases), but I'm not sure there is anything that would fit it, and developing a new measure of correlation would be far beyond the scope of an answer here. Besides this, perhaps it's a good idea to think if the behavior you're describing is really desirable and consistent:

  • In case 1, Pearson's R is close to what you want it to be.
  • In case 2, the correlation is negative, but you'd like to see a positive value because B is declining, instead of A as in the first example. That would be easy to achieve, just multiply with -1 if B is declining. But are you sure that's what you want? There are many more possibilities of how A and B can be dependent.
  • In case 3 I don't see a mix of the others, I just see something entirely different, and I imagine there are many other possibilities for which I'm not sure what you'd like the outcome to be.

In summary, the three examples are most likely not a sufficient basis to identify a correlation measure that would do exactly what you want it to. The field of assessing variable dependence is vast and it's easy to get lost in the intricacies. I'd recommend you take something simple and well-understood like Pearson's R and adapt it if really necessary, but make sure you know what exactly you need before you do that.