import pandas as pd
import matplotlib. pyplot as plt
import statsmodels. api as sm
from statsmodels. tsa. arima_model import ARMA
from statsmodels. graphics. api import qqplot
data = [ 3821 , 4236 , 3758 , 6783 , 4664 , 2589 , 2538 , 3542 , 4626 , 5886 , 6233 , 4199 , 3561 , 2335 , 5636 , 3524 ,
4327 , 6064 , 3912 , 1356 , 4305 , 4379 , 4592 , 4233 , 4281 , 1613 , 1233 , 4514 , 3431 , 2159 , 2322 , 4239 , 4733 ,
2268 , 5397 , 5821 , 6115 , 6631 , 6474 , 4134 , 2728 , 5753 , 7130 , 7860 , 6991 , 7499 , 5301 , 2808 , 6755 , 6658 ,
6944 , 6372 , 8380 , 7366 , 6352 , 8333 , 8281 , 11548 , 10823 , 13642 , 9973 , 6723 , 13416 , 12205 , 13942 , 9590 ,
11693 , 9276 , 6519 , 6863 , 8237 , 10122 , 8646 , 9749 , 5346 , 4836 , 9806 , 7502 , 9387 , 11078 , 9832 , 6886 , 4285 ,
8351 , 9725 , 11844 , 12387 , 10666 , 7072 , 6429 ]
data= pd. Series( data)
data_index = sm. tsa. datetools. dates_from_range( '1901' , '1990' )
data. index = pd. Index( data_index)
data. plot( figsize= ( 12 , 8 ) )
plt. show( )
arma = ARMA( data, ( 7 , 0 ) ) . fit( )
print ( 'AIC: %0.4lf' % arma. aic)
predict_y = arma. predict( '1990' , '2000' )
fig, ax = plt. subplots( figsize= ( 12 , 8 ) )
ax = data. ix[ '1901' : ] . plot( ax= ax)
predict_y. plot( ax= ax)
plt. show( )
stock_arima
import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
from statsmodels. tsa. arima_model import ARIMA
import statsmodels. api as sm
import warnings
from itertools import product
from datetime import datetime, timedelta
import calendar
warnings. filterwarnings( 'ignore' )
plt. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
df = pd. read_csv( './shanghai_index_1990_12_19_to_2020_03_12.csv' )
df = df[ [ 'Timestamp' , 'Price' ] ]
df. Timestamp = pd. to_datetime( df. Timestamp)
df. index = df. Timestamp
print ( df. head( ) )
df_month = df. resample( 'M' ) . mean( )
df_Q = df. resample( 'Q-DEC' ) . mean( )
df_year = df. resample( 'A-DEC' ) . mean( )
print ( df_month)
ps = range ( 0 , 5 )
qs = range ( 0 , 5 )
ds = range ( 1 , 2 )
parameters = product( ps, ds, qs)
parameters_list = list ( parameters)
results = [ ]
best_aic = float ( "inf" )
for param in parameters_list:
try :
model = sm. tsa. statespace. SARIMAX( df_month. Price,
order= ( param[ 0 ] , param[ 1 ] , param[ 2 ] ) ,
enforce_stationarity= False ,
enforce_invertibility= False ) . fit( )
except ValueError:
print ( '参数错误:' , param)
continue
aic = model. aic
if aic < best_aic:
best_model = model
best_aic = aic
best_param = param
results. append( [ param, model. aic] )
print ( '最优模型: ' , best_model. summary( ) )
df_month2 = df_month[ [ 'Price' ] ]
future_month = 3
last_month = pd. to_datetime( df_month2. index[ len ( df_month2) - 1 ] )
date_list = [ ]
for i in range ( future_month) :
year = last_month. year
month = last_month. month
if month == 12 :
month = 1
year = year+ 1
else :
month = month + 1
next_month_days = calendar. monthrange( year, month) [ 1 ]
last_month = last_month + timedelta( days= next_month_days)
date_list. append( last_month)
print ( 'date_list=' , date_list)
future = pd. DataFrame( index= date_list, columns= df_month. columns)
df_month2 = pd. concat( [ df_month2, future] )
df_month2[ 'forecast' ] = best_model. get_prediction( start= 0 , end= len ( df_month2) ) . predicted_mean
plt. figure( figsize= ( 30 , 7 ) )
df_month2. Price. plot( label= '实际指数' )
df_month2. forecast. plot( color= 'r' , ls= '--' , label= '预测指数' )
plt. legend( )
plt. title( '沪市指数(月)' )
plt. xlabel( '时间' )
plt. ylabel( '指数' )
plt. show( )
stock_prophet
import pandas as pd
from fbprophet import Prophet
import matplotlib. pyplot as plt
% matplotlib inline
df = pd. read_csv( './shanghai_index_1990_12_19_to_2020_03_12.csv' )
df. rename( columns= { 'Timestamp' : 'ds' , 'Price' : 'y' } , inplace= True )
print ( df. head( ) )
model = Prophet( )
model. fit( df)
future = model. make_future_dataframe( periods= 365 )
forecast = model. predict( future)
print ( forecast[ [ 'ds' , 'yhat' , 'yhat_lower' , 'yhat_upper' ] ] . tail( ) )
model. plot( forecast)
plt. show( )
model. plot_components( forecast)
print ( forecast. columns)
stock_tsa
import statsmodels. api as sm
import matplotlib. pyplot as plt
import pandas as pd
data = pd. read_csv( 'shanghai_index_1990_12_19_to_2020_03_12.csv' , usecols= [ 'Timestamp' , 'Price' ] )
print ( data. head( ) )
data. Timestamp = pd. to_datetime( data. Timestamp)
data = data. set_index( 'Timestamp' )
data[ 'Price' ] = data[ 'Price' ] . apply ( pd. to_numeric, errors= 'ignore' )
print ( data. Price. shape)
data. Price. interpolate( inplace= True )
print ( data. head( ) )
result = sm. tsa. seasonal_decompose( data. Price, freq= 288 )
result. plot( )
plt. show( )
stock_prophet.py
import pandas as pd
from fbprophet import Prophet
import matplotlib. pyplot as plt
get_ipython( ) . run_line_magic( 'matplotlib' , 'inline' )
df = pd. read_csv( './shanghai_index_1990_12_19_to_2020_03_12.csv' )
df. rename( columns= { 'Timestamp' : 'ds' , 'Price' : 'y' } , inplace= True )
print ( df. head( ) )
model = Prophet( )
model. fit( df)
future = model. make_future_dataframe( periods= 365 )
forecast = model. predict( future)
print ( forecast[ [ 'ds' , 'yhat' , 'yhat_lower' , 'yhat_upper' ] ] . tail( ) )
model. plot( forecast)
plt. show( )
model. plot_components( forecast)
print ( forecast. columns)