[Python] Statistical analysis of the time series

Source: Internet
Author: User
Tags stock prices

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 are good to buy which day are good for sell, we can use Bollinger bands.

Bollinger Bands:

ImportOSImportPandas as PDImportMatplotlib.pyplot as PltdefTest_run (): start_date='2017-01-01'End_data='2017-12-15'dates=Pd.date_range (start_date, End_data)#Create an empty data frameDF=PD. DataFrame (index=dates) Symbols=['SPY','AAPL','IBM','GOOG','GLD']     forSymbolinchsymbols:temp=getadjcloseforsymbol (symbol) DF=df.join (temp, how='Inner')    returnDFif __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's 2 times STD value.

"""Bollinger Bands."""ImportOSImportPandas as PDImportMatplotlib.pyplot as PltdefSymbol_to_path (Symbol, base_dir="Data"):    """Return CSV file path given ticker symbol."""    returnOs.path.join (Base_dir,"{}.csv". Format (str ))defget_data (symbols, dates):"""Read stock Data (adjusted close) for given symbols from CSV files."""DF= PD. DataFrame (index=dates)if 'SPY'  not inchSymbols#add SPY for reference, if absentSymbols.insert (0,'SPY')     forSymbolinchsymbols: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)ifSymbol = ='SPY':#Drop dates SPY did not tradeDF = Df.dropna (subset=["SPY"])    returnDFdefPlot_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 ()defGet_rolling_mean (values, window):"""Return rolling mean of given values, using specified window size."""    returnValues.rolling (window=window). Mean ()defget_rolling_std (values, window):"""Return rolling Standard deviation of given values, using specified window size."""    #Todo:compute and return rolling standard deviation    returnValues.rolling (window=window). STD ()defget_bollinger_bands (rm, RSTD):"""Return Upper and lower Bollinger bands."""    #todo:compute Upper_band and Lower_bandUpper_band = RSTD * 2 +RM Lower_band= RM-RSTD * 2returnUpper_band, Lower_banddefTest_run ():#Read DataDates = Pd.date_range ('2012-01-01','2012-12-31') Symbols= ['SPY'] DF=get_data (symbols, dates)#Compute Bollinger Bands    #1. Compute rolling meanRm_spy = Get_rolling_mean (df['SPY'], window=20)    #2. Compute Rolling Standard deviationRstd_spy = GET_ROLLING_STD (df['SPY'], window=20)    #3. Compute Upper and lower bandsUpper_band, Lower_band =get_bollinger_bands (Rm_spy, Rstd_spy)#Plot Raw SPY values, rolling mean and Bollinger bandsAx = 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 legendAx.set_xlabel ("Date") Ax.set_ylabel (" Price") ax.legend (Loc='Upper Left') plt.show ()if __name__=="__main__": Test_run ()

[Python] Statistical analysis of the time series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.