A
A
Anton2019-10-27 21:10:25
Mathematics
Anton, 2019-10-27 21:10:25

How to calculate the overall trend?

for example given a list [29, 21, 40, 23, 19, 50, 34, 10, 42, 59]
how to get a general trend like 'up', 'down' or 'flat' ?
Help find the formula

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2019-10-28
@anttoon

Build a linear regression line. By the sign of the coefficient a (in some books it is called the coefficient b1, in general - this is the coefficient at X ) an increasing or decreasing trend in the set is determined.
In Python, the sklearn library has the LinearRegression function for this purpose.
In fact, the hardest part is defining what you obviously called "flat" - i.e. no trend. To do this, it is not enough to determine the indicated coefficient itself, but it is necessary to calculate its confidence interval and understand whether it captures 0.

A
Anton, 2019-10-28
@anttoon

Wrote such a function, for my purposes what you need

from sklearn.linear_model import LinearRegression


def get_trand(l):
    data = [[i] for i in l]
    x = [[i] for i in range(len(l))]
    lr = LinearRegression().fit(x, data)
    a = lr.coef_[0][0]
    a = round(a, ndigits=1)

    if a > 0:
        return 'up'
    elif a < 0:
        return 'down'
    else:
        return 'flat'

l = [29, 21, 40, 23, 19, 50, 34, 10, 42, 59]
print(get_trand(l))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question