Python:帝人とグーグルの1年後の株価を予測する

その買うを、もっとハッピーに。|ハピタス

このサイトを参考にして、1年後のグーグルと帝人の株価を予測する。大統領再選の年の、1980年、1984年、1992年、1996年、2004年、2012年は、1980年と1992年を除いて全て好景気の真っ只中だったので、来年は3分の2の確率で好景気になる。

スポンサーリンク

グーグル株価データの準備

先ず、必要なモジュールをインポートする。

# For division
from __future__ import division
#Let's go ahead and start with some imports
import pandas as pd
from pandas import Series,DataFrame
import numpy as np

# For Visualization
import matplotlib.pyplot as plt

%matplotlib inline

# For reading stock data from yahoo
import pandas_datareader as web

# For time stamps
from datetime import datetime

yahoo financeからグーグルの株価を引っ張ってくる。

# The tech stocks we'll use for this analysis
ticker = ['GOOGL']

# To Set up End and Start times for data grab
end = datetime.now()
start = datetime(end.year - 40,end.month,end.day)


# Creating loop for grabing yahoo finance data and setting as a dataframe

for stock in ticker:   
    
    #To set DataFrame as the Stock Ticker
    
    globals()[stock] = web.DataReader(stock,'yahoo',start,end)['Adj Close']
GOOGL.head()
Date
2004-08-19    50.220219
2004-08-20    54.209209
2004-08-23    54.754753
2004-08-24    52.487488
2004-08-25    53.053055
Name: Adj Close, dtype: float64
GOOGL.tail()
Date
2019-08-21    1191.579956
2019-08-22    1191.520020
2019-08-23    1153.579956
2019-08-26    1171.180054
2019-08-27    1170.819946
Name: Adj Close, dtype: float64
GOOGL.plot(figsize=(15,10),legend=True, linestyle='--', marker='o');
GOOGL.describe()
count    3782.000000
mean      477.523152
std       325.609655
min        50.055054
25%       236.291290
50%       313.133148
75%       710.414978
max      1296.199951
Name: Adj Close, dtype: float64
df = GOOGL
df = df.reset_index()
df[['ds','y']] = df[['Date' ,'Adj Close']]
df = df[['ds','y']]
df.head()
ds y
0 2004-08-19 50.220219
1 2004-08-20 54.209209
2 2004-08-23 54.754753
3 2004-08-24 52.487488
4 2004-08-25 53.053055
スポンサーリンク

prophetでグーグル株価予測

import fbprophet
m = fbprophet.Prophet()
m.fit(df)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
<fbprophet.forecaster.Prophet at 0x7fbe70fe29b0>
future = m.make_future_dataframe(periods=365)
future.tail()
ds
4142 2020-08-22
4143 2020-08-23
4144 2020-08-24
4145 2020-08-25
4146 2020-08-26
forecast = m.predict(future)
forecast.tail()
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
4142 2020-08-22 1385.538230 1317.489502 1451.976040 1344.660685 1436.218256 -5.112305 -5.112305 -5.112305 -2.689929 -2.689929 -2.689929 -2.422375 -2.422375 -2.422375 0.0 0.0 0.0 1380.425925
4143 2020-08-23 1385.933447 1316.189344 1453.138430 1344.816140 1436.834903 -5.506142 -5.506142 -5.506142 -2.689928 -2.689928 -2.689928 -2.816214 -2.816214 -2.816214 0.0 0.0 0.0 1380.427304
4144 2020-08-24 1386.328663 1320.163781 1453.304163 1344.978900 1437.452221 -2.694460 -2.694460 -2.694460 0.469093 0.469093 0.469093 -3.163553 -3.163553 -3.163553 0.0 0.0 0.0 1383.634203
4145 2020-08-25 1386.723880 1316.665670 1459.588784 1345.141659 1438.032475 -2.274549 -2.274549 -2.274549 1.187720 1.187720 1.187720 -3.462269 -3.462269 -3.462269 0.0 0.0 0.0 1384.449331
4146 2020-08-26 1387.119097 1319.400190 1454.660957 1345.462991 1438.612730 -2.357253 -2.357253 -2.357253 1.353419 1.353419 1.353419 -3.710672 -3.710672 -3.710672 0.0 0.0 0.0 1384.761843
plt.rcParams["font.size"] = "25"
fig, ax = plt.subplots(figsize=(20,10))
pd.plotting.register_matplotlib_converters()
m.plot(forecast,ax=ax);

グーグル株価は来年も上昇トレンドが続くようである。

plt.rcParams["font.size"] = "13.3"
m.plot_components(forecast);
スポンサーリンク

帝人株価データの準備

df = web.DataReader('3401.JP','stooq')
df.head()
Open High Low Close Volume
Date
2019-08-27 1864.0 1869.0 1852.0 1859.0 705700
2019-08-26 1835.0 1852.0 1829.0 1841.0 863700
2019-08-23 1862.0 1889.0 1862.0 1875.0 962000
2019-08-22 1850.0 1850.0 1837.0 1847.0 865300
2019-08-21 1834.0 1843.0 1826.0 1839.0 711100
df.tail()
Open High Low Close Volume
Date
2010-01-08 1390.81 1390.81 1358.90 1358.90 1037249
2010-01-07 1372.56 1377.15 1363.48 1372.56 515116
2010-01-06 1363.48 1377.15 1358.90 1372.56 507221
2010-01-05 1390.81 1395.39 1363.48 1363.48 443407
2010-01-04 1372.56 1395.39 1368.06 1368.06 408322

2010年以前のデータは取得できないようである。

df = df.reset_index()
df[['ds','y']] = df[['Date' ,'Close']]
df = df[['ds','y']]
df.head()
ds y
0 2019-08-27 1859.0
1 2019-08-26 1841.0
2 2019-08-23 1875.0
3 2019-08-22 1847.0
4 2019-08-21 1839.0
スポンサーリンク

prophetで帝人株価予測

m = fbprophet.Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=365)
future.tail()
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
ds
2723 2020-08-22
2724 2020-08-23
2725 2020-08-24
2726 2020-08-25
2727 2020-08-26
forecast = m.predict(future)
forecast.tail()
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
2723 2020-08-22 1580.866277 1222.683171 1935.813909 1228.730786 1901.896136 -8.434259 -8.434259 -8.434259 61.896037 61.896037 61.896037 -70.330296 -70.330296 -70.330296 0.0 0.0 0.0 1572.432018
2724 2020-08-23 1580.258466 1196.277079 1927.020919 1226.317182 1902.293160 -11.170570 -11.170570 -11.170570 61.896040 61.896040 61.896040 -73.066611 -73.066611 -73.066611 0.0 0.0 0.0 1569.087896
2725 2020-08-24 1579.650656 1099.210380 1809.976241 1224.461806 1902.599889 -101.492637 -101.492637 -101.492637 -25.861847 -25.861847 -25.861847 -75.630790 -75.630790 -75.630790 0.0 0.0 0.0 1478.158019
2726 2020-08-25 1579.042846 1109.628677 1832.008074 1222.262288 1902.892797 -104.213405 -104.213405 -104.213405 -26.207309 -26.207309 -26.207309 -78.006097 -78.006097 -78.006097 0.0 0.0 0.0 1474.829440
2727 2020-08-26 1578.435035 1119.617057 1823.077494 1219.831863 1903.185704 -103.849279 -103.849279 -103.849279 -23.672899 -23.672899 -23.672899 -80.176379 -80.176379 -80.176379 0.0 0.0 0.0 1474.585757
plt.rcParams["font.size"] = "25"
fig, ax = plt.subplots(figsize=(20,10))
m.plot(forecast,ax=ax);

帝人株は来年は下降トレンドに入るようである。

plt.rcParams["font.size"] = "13.3"
m.plot_components(forecast);
スポンサーリンク

帝人ADRの株価データを準備

df1 = web.DataReader('TINLY','yahoo')
df1.head()
High Low Open Close Volume Adj Close
Date
2009-12-31 16.250000 16.250000 16.250000 16.250000 400.0 11.844674
2010-01-04 16.250000 16.250000 16.250000 16.250000 0.0 11.844674
2010-01-05 16.350000 16.350000 16.350000 16.350000 400.0 11.917563
2010-01-06 16.405001 16.405001 16.405001 16.405001 400.0 11.957653
2010-01-07 16.190001 16.190001 16.190001 16.190001 600.0 11.800939
# The tech stocks we'll use for this analysis
ticker = ['TINLY']

# To Set up End and Start times for data grab
end = datetime.now()
start = datetime(end.year - 40,end.month,end.day)


# Creating loop for grabing yahoo finance data and setting as a dataframe

for stock in ticker:   
    
    #To set DataFrame as the Stock Ticker
    
    globals()[stock] = web.DataReader(stock,'yahoo',start,end)['Adj Close']
df1 = TINLY
df1.head()
Date
1996-11-18    14.631307
1996-11-19    14.555104
1996-11-20    14.783714
1996-11-21    14.631307
1996-11-22    14.555104
Name: Adj Close, dtype: float64
df1 = df1.reset_index()
df1[['ds','y']] = df1[['Date' ,'Adj Close']]
df1 = df1[['ds','y']]
df1.head()
ds y
0 1996-11-18 14.631307
1 1996-11-19 14.555104
2 1996-11-20 14.783714
3 1996-11-21 14.631307
4 1996-11-22 14.555104
スポンサーリンク

prophetで帝人ADR株価予測

m = fbprophet.Prophet()
m.fit(df1)
future = m.make_future_dataframe(periods=365)
future.tail()
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
ds
6091 2020-08-22
6092 2020-08-23
6093 2020-08-24
6094 2020-08-25
6095 2020-08-26
forecast = m.predict(future)
forecast.tail()
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
6091 2020-08-22 20.181508 17.509937 22.918175 18.676749 22.499465 -0.047019 -0.047019 -0.047019 0.249363 0.249363 0.249363 -0.296383 -0.296383 -0.296383 0.0 0.0 0.0 20.134489
6092 2020-08-23 20.184206 17.722457 22.860727 18.666945 22.525237 -0.066491 -0.066491 -0.066491 0.249363 0.249363 0.249363 -0.315854 -0.315854 -0.315854 0.0 0.0 0.0 20.117715
6093 2020-08-24 20.186904 17.253842 22.550568 18.653910 22.536484 -0.430290 -0.430290 -0.430290 -0.094607 -0.094607 -0.094607 -0.335683 -0.335683 -0.335683 0.0 0.0 0.0 19.756614
6094 2020-08-25 20.189601 17.336770 22.428507 18.646617 22.547618 -0.459636 -0.459636 -0.459636 -0.104014 -0.104014 -0.104014 -0.355622 -0.355622 -0.355622 0.0 0.0 0.0 19.729965
6095 2020-08-26 20.192299 17.291634 22.520095 18.643681 22.558136 -0.480084 -0.480084 -0.480084 -0.104685 -0.104685 -0.104685 -0.375400 -0.375400 -0.375400 0.0 0.0 0.0 19.712214
plt.rcParams["font.size"] = "25"
fig, ax = plt.subplots(figsize=(20,10))
m.plot(forecast,ax=ax);

帝人ADRの方は、来年は上昇局面に入るようである。

plt.rcParams["font.size"] = "13.3"
m.plot_components(forecast);
スポンサーリンク
スポンサーリンク