calculating slope for a series trendline in Pandas

Dmitry B.

Is there an idiomatic way of getting the slope for linear trend line fitting values in a DataFrame column? The data is indexed with DateTime index.

piRSquared

This should do it:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(100, 5), pd.date_range('2012-01-01', periods=100))

def trend(df):
    df = df.copy().sort_index()
    dates = df.index.to_julian_date().values[:, None]
    x = np.concatenate([np.ones_like(dates), dates], axis=1)
    y = df.values
    return pd.DataFrame(np.linalg.pinv(x.T.dot(x)).dot(x.T).dot(y).T,
                        df.columns, ['Constant', 'Trend'])


trend(df)

enter image description here

Using the same df above for its index:

df_sample = pd.DataFrame((df.index.to_julian_date() * 10 + 2) + np.random.rand(100) * 1e3, df.index)

coef = trend(df_sample)
df_sample['trend'] = (coef.iloc[0, 1] * df_sample.index.to_julian_date() + coef.iloc[0, 0])
df_sample.plot(style=['.', '-'])

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

calculating slope for a series trendline in Pandas

From Dev

Calculating the slope between each value and time step - pandas

From Java

Calculating Slope in Tableau

From Dev

calculating slope values from splines

From Dev

Calculating a cumulative deviation from mean monthly value in pandas series

From Dev

Calculating daily average from irregular time series using pandas

From Dev

Calculating averages of time series data using pandas dataframe

From Dev

Calculating nested radical series

From Dev

Calculating Aroon Indicator Series

From Dev

Calculating Fourier series in SciPy

From Dev

Calculating the slope of each row in a large data set using R

From Dev

matlab - plot trendline with specific condition(e.g. slope at specific x value, must past through specific point,etc)

From Dev

Calculating multiindex series / dataframes with conditions

From Dev

calculating the autocorrelation of a time series in R

From Dev

Calculating relative time series in R

From Dev

Python / Pandas - Calculating ratio

From Dev

Slope or Trend calculation for series or each vector (multiple columns)

From Dev

Taylor Series Calculating Program in C compiled with GCC

From Dev

Algorithm for calculating a series of future dates based on a frequency

From Dev

R xts calculating percentage index on multiple series

From Dev

Python: calculating log returns of a time series

From Dev

a daily time series - calculating totals in R

From Dev

Calculating the duration an event in a time series python

From Dev

Calculating slope and associated stats by rows using multiple categories in dataframe that has NAs

From Java

Calculating percentage in Python Pandas library

From Dev

Calculating the number of years in a pandas dataframe

From Dev

Calculating and plotting count ratios with Pandas

From Dev

Calculating the mean of groups in python/pandas

From Dev

Calculating cumulative returns with pandas dataframe

Related Related

  1. 1

    calculating slope for a series trendline in Pandas

  2. 2

    Calculating the slope between each value and time step - pandas

  3. 3

    Calculating Slope in Tableau

  4. 4

    calculating slope values from splines

  5. 5

    Calculating a cumulative deviation from mean monthly value in pandas series

  6. 6

    Calculating daily average from irregular time series using pandas

  7. 7

    Calculating averages of time series data using pandas dataframe

  8. 8

    Calculating nested radical series

  9. 9

    Calculating Aroon Indicator Series

  10. 10

    Calculating Fourier series in SciPy

  11. 11

    Calculating the slope of each row in a large data set using R

  12. 12

    matlab - plot trendline with specific condition(e.g. slope at specific x value, must past through specific point,etc)

  13. 13

    Calculating multiindex series / dataframes with conditions

  14. 14

    calculating the autocorrelation of a time series in R

  15. 15

    Calculating relative time series in R

  16. 16

    Python / Pandas - Calculating ratio

  17. 17

    Slope or Trend calculation for series or each vector (multiple columns)

  18. 18

    Taylor Series Calculating Program in C compiled with GCC

  19. 19

    Algorithm for calculating a series of future dates based on a frequency

  20. 20

    R xts calculating percentage index on multiple series

  21. 21

    Python: calculating log returns of a time series

  22. 22

    a daily time series - calculating totals in R

  23. 23

    Calculating the duration an event in a time series python

  24. 24

    Calculating slope and associated stats by rows using multiple categories in dataframe that has NAs

  25. 25

    Calculating percentage in Python Pandas library

  26. 26

    Calculating the number of years in a pandas dataframe

  27. 27

    Calculating and plotting count ratios with Pandas

  28. 28

    Calculating the mean of groups in python/pandas

  29. 29

    Calculating cumulative returns with pandas dataframe

HotTag

Archive