[Python] Statistical analysis of time series

Global Statistics:

Common seen methods as such

1. Mean

2. Median

3. Standard deviation:  the larger the number means it various a lot.

4. Sum.

Rolling Statistics:

It use a time window, moving forward each day to calculate the mean value of those window periods.

To find which day is good to buy which day is good for sell, we can use Bollinger bands.

Bollinger bands:

import os
import pandas as pd
import matplotlib.pyplot as plt

def test_run():
    start_date=‘2017-01-01‘
    end_data=‘2017-12-15‘
    dates=pd.date_range(start_date, end_data)

    # Create an empty data frame
    df=pd.DataFrame(index=dates)

    symbols=[‘SPY‘, ‘AAPL‘, ‘IBM‘, ‘GOOG‘, ‘GLD‘]
    for symbol in symbols:
        temp=getAdjCloseForSymbol(symbol)
        df=df.join(temp, how=‘inner‘)

    return df  

if __name__ == ‘__main__‘:
    df=test_run()
    # data=data.ix[‘2017-12-01‘:‘2017-12-15‘, [‘IBM‘, ‘GOOG‘]]
    # df=normalize_data(df)
    ax = df[‘SPY‘].plot(title="SPY rolling mean", label=‘SPY‘)
    rm = df[‘SPY‘].rolling(20).mean()
    rm.plot(label=‘Rolling mean‘, ax=ax)
    ax.set_xlabel(‘Date‘)
    ax.set_ylabel(‘Price‘)
    ax.legend(loc="upper left")
    plt.show()

Now we can calculate Bollinger bands, it is 2 times std value.

"""Bollinger Bands."""

import os
import pandas as pd
import matplotlib.pyplot as plt

def symbol_to_path(symbol, base_dir="data"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if ‘SPY‘ not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, ‘SPY‘)

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col=‘Date‘,
                parse_dates=True, usecols=[‘Date‘, ‘Adj Close‘], na_values=[‘nan‘])
        df_temp = df_temp.rename(columns={‘Adj Close‘: symbol})
        df = df.join(df_temp)
        if symbol == ‘SPY‘:  # drop dates SPY did not trade
            df = df.dropna(subset=["SPY"])

    return df

def plot_data(df, title="Stock prices"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12)
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    plt.show()

def get_rolling_mean(values, window):
    """Return rolling mean of given values, using specified window size."""
    return values.rolling(window=window).mean()

def get_rolling_std(values, window):
    """Return rolling standard deviation of given values, using specified window size."""
    # TODO: Compute and return rolling standard deviation
    return values.rolling(window=window).std()

def get_bollinger_bands(rm, rstd):
    """Return upper and lower Bollinger Bands."""
    # TODO: Compute upper_band and lower_band
    upper_band = rstd * 2 + rm
    lower_band =  rm - rstd * 2
    return upper_band, lower_band

def test_run():
    # Read data
    dates = pd.date_range(‘2012-01-01‘, ‘2012-12-31‘)
    symbols = [‘SPY‘]
    df = get_data(symbols, dates)

    # Compute Bollinger Bands
    # 1. Compute rolling mean
    rm_SPY = get_rolling_mean(df[‘SPY‘], window=20)

    # 2. Compute rolling standard deviation
    rstd_SPY = get_rolling_std(df[‘SPY‘], window=20)

    # 3. Compute upper and lower bands
    upper_band, lower_band = get_bollinger_bands(rm_SPY, rstd_SPY)

    # Plot raw SPY values, rolling mean and Bollinger Bands
    ax = df[‘SPY‘].plot(title="Bollinger Bands", label=‘SPY‘)
    rm_SPY.plot(label=‘Rolling mean‘, ax=ax)
    upper_band.plot(label=‘upper band‘, ax=ax)
    lower_band.plot(label=‘lower band‘, ax=ax)

    # Add axis labels and legend
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    ax.legend(loc=‘upper left‘)
    plt.show()

if __name__ == "__main__":
    test_run()

时间: 2024-08-03 11:01:52

[Python] Statistical analysis of time series的相关文章

Python.Data.Analysis(PACKT,2014)pdf

下载地址:网盘下载 Finding great data analysts is difficult. Despite the explosive growth of data in industries ranging from manufacturing and retail to high technology, finance, and healthcare, learning and accessing data analysis tools has remained a challe

pandas: powerful Python data analysis toolkit

pandas.read_csv pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, fal

python data analysis | python数据预处理(基于scikit-learn模块)

原文:http://www.jianshu.com/p/94516a58314d Dataset transformations| 数据转换 Combining estimators|组合学习器 Feature extration|特征提取 Preprocessing data|数据预处理 1 Dataset transformations scikit-learn provides a library of transformers, which may clean (see Preproce

Machine and Deep Learning with Python

Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstitions cheat sheet Introduction to Deep Learning with Python How to implement a neural network How to build and run your first deep learning network Neur

用 Python 通过马尔可夫随机场(MRF)与 Ising Model 进行二值图降噪

前言 这个降噪的模型来自 Christopher M. Bishop 的 Pattern Recognition And Machine Learning (就是神书 PRML……),问题是如何对一个添加了一定椒盐噪声(Salt-and-pepper Noise)(假设噪声比例不超过 10%)的二值图(Binary Image)去噪. 原图 添加 10% 椒盐噪声的图 建模 下文中的数学表示: yi:噪声图中的像素 xi:原图中的像素,对应噪声图中的 yi 既然噪声图是从原图添加噪声而来,我们拥

Python兵器谱

转载自:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/C++,但平时的很多文本数据处理任务都交给了Python.离开腾讯创业后,第一个作品课程图谱也是选择了Python系的Flask框架,渐渐的将自己的绝大部分工作交给了Python.这些年来,接触和使用了很多Python工具包,特别是在文本处理,科学计算,机器学习和数据挖掘领域,有很多很

Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)

原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/C++,但平时的很多文本数据处理任务都交给了Python.离开腾讯创业后,第一个作品课程图谱也是选择了Python系的Flask框架,渐渐的将自己的绝大部分工作交给了Python.这些年来,接触和使用了很多Python工具包,特别是在文本处理,科学计算,机器学习和数据挖掘领域,有很多很多

python and 我爱自然语言处理

曾经因为NLTK的 缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/C++,但平时的很多文本数据处理任务都交给了Python.离 开腾讯创业后,第一个作品课程图谱也 是选择了Python系的Flask框架,渐渐的将自己的绝大部分工作交给了Python.这些年来,接触和使用了很多Python工具包,特别是在文本 处理,科学计算,机器学习和数据挖掘领域,有很多很多优秀的Python工具包可供使用,所以作为Pythoner,也是相当幸福的.其实如果仔细留意微 博,你

【Python】Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

好文 mark http://www.52nlp.cn/python-%E7%BD%91%E9%A1%B5%E7%88%AC%E8%99%AB-%E6%96%87%E6%9C%AC%E5%A4%84%E7%90%86-%E7%A7%91%E5%AD%A6%E8%AE%A1%E7%AE%97-%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0-%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工