Skip to content

Instantly share code, notes, and snippets.

@OddExtension5
Last active January 19, 2020 18:14
Show Gist options
  • Save OddExtension5/996740205059aed745bf1e1b363676b1 to your computer and use it in GitHub Desktop.
Save OddExtension5/996740205059aed745bf1e1b363676b1 to your computer and use it in GitHub Desktop.
Time Series Analysis : Stationary, Trend, Seasonal, Decomposition, Differences, Windowing, Running Values, Exponential Smoothing, Fill Methods, Resampling, Jackknife Estimation, Bootstrapping, Fourier Transform , Filtering, Correlation, Auto-Correlation, Partial Auto-Correlation, Random Walks, Dickey-Fuller Test, ARIMA Models

Time Series

  • A set of values measured sequentially in time
  • Values are typically (but not always) measured at equal interval x1, x2, x3, etc..
  • Values can be:
    • Continuous
    • discrete or symbolic (words)
  • Associated with empirical observation of time varying phenomena:
    • Stock market price (day, hour, minute, tick etc...)
    • Temperature (day, minute, second, etc..)
    • Number of paitents (week, month, etc..)
    • GDP (quarter, year, etc..)
  • Forecasting requires predicting future values based on past behaviour

Mathematical Conventions

  • The value of the time series at time t is given by Xt
  • The values at a given lag l are given by Xt-l
  • The mean of the overall signal is $\mu $ and the corresponding running value is $\mu_{t}^{w}$
  • The variance of the overall signal is $\sigma $ and the corresponding running value is $\sigma_{t}^{w}$
  • Running values are calculated over window of width w

Time Series Analysis

  • The Fundamental assumption is that previous values determine the current value:
    xt+1 = f(xt,xt-1,...)

ts

Three Fundamental Behaviors

ts2

Stationarity

  • A time series is said to be stationary if its basic statistical properties are independent of time
  • In particular:
    • Mean Average value stays constant
    • Variance : The width of the curve is bounded
    • Covariance: Correlation between points is indepedent of time
  • Stationary processes are easier to analyze
  • Many time series analysis algorithms assume the time series to be stationary
  • Several rigorous tests for stationarity have been developed such as the (Augmented) Dickey-Fuller and Hurst Exponent
  • Typically, the first step of any analysis is to transform the series to make it stationary

ts3

Trends

  • Many time series have a clear trend or tendency:
    • Stock market indices tend to go up over time
    • Number of cases of preventable diseases tends to go down over time
  • Trends can be additive or multiplicative:

ts4

  • Trends can be removed by subtraction or division of the correct values
  • One simple way to determine the trend is to calculate a running average over the series
def running_average(x, order):
  current = x[:order].sum()
  running = []
  
  for i in range(order, x.shape[0]):
    current += x[i]
    current -= x[i-order]
    running.append(current/order)
  
  return np.array(running)

ts5

Seasonality

  • Many of the phenomena we might be interested in varying in time in a cyclical or seasonal fashion

    • Ice-cream sales peak in the summer and drop in the winter
    • Number of cell phone calls made is larger during the day than at night
    • Many types of crime are more frequent at night than during the day
    • Visits to museums are more frequent in the weekend than in weekdays
    • The stock market grows during bull periods and shrinks during bear periods
  • Understanding the seasonality of a time series provides important information about its long term behavior and is extremely useful in predicting future values

  • If the period is fixed it’s called seasonality, while if the period is irregular it's called cyclical

ts6

ts7

ts8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment