Prophetで予測した任天堂株価をPlotlyでグラフ化

前回は、fbprophetが予測したグーグル・帝人株価をmatplotlibでグラフ化したが、今回は、prophetが予測した一年後の任天堂の株価をplotlyを使ってグラフ化して視覚効果を飛躍的に高める。

スポンサーリンク

任天堂株価データの準備

from __future__ import division
import pandas as pd
import numpy as np
from datetime import datetime
from pandas import Series,DataFrame
import pandas_datareader as web
import fbprophet
from fbprophet.plot import plot_plotly
from plotly.offline import plot,iplot
import matplotlib.pyplot as plt
%matplotlib inline
df = web.DataReader('7974.JP','stooq')
df.head()
Open High Low Close Volume
Date
2019-08-27 40200.0 40350.0 39950.0 40250.0 1106800
2019-08-26 40200.0 40440.0 39680.0 39820.0 1494400
2019-08-23 40220.0 41010.0 40140.0 40980.0 1400500
2019-08-22 40380.0 40600.0 40050.0 40180.0 949000
2019-08-21 39800.0 40280.0 39670.0 40230.0 872200
df.tail()
Open High Low Close Volume
Date
2010-01-08 24400.37 24811.02 24142.52 24429.03 1502609
2010-01-07 23731.88 24639.13 23731.88 24371.72 1835488
2010-01-06 22910.57 23474.01 22690.92 23397.63 1653814
2010-01-05 21860.07 22051.06 21745.46 21898.26 864918
2010-01-04 21115.16 21630.87 21115.16 21602.22 657901
df = df.reset_index()
df[['ds','y']] = df[['Date' ,'Close']]
df = df[['ds','y']]
df.head()
ds y
0 2019-08-27 40250.0
1 2019-08-26 39820.0
2 2019-08-23 40980.0
3 2019-08-22 40180.0
4 2019-08-21 40230.0

prophetで任天堂株価予測

m = fbprophet.Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=365)
future.tail()
INFO:numexpr.utils:Note: NumExpr detected 12 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
INFO:numexpr.utils:NumExpr defaulting to 8 threads.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
ds
2707 2020-08-22
2708 2020-08-23
2709 2020-08-24
2710 2020-08-25
2711 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
2707 2020-08-22 30538.972523 21692.332770 35438.743608 24628.364944 36971.351402 -2598.624556 -2598.624556 -2598.624556 -2600.855891 -2600.855891 -2600.855891 2.231335 2.231335 2.231335 0.0 0.0 0.0 27940.347967
2708 2020-08-23 30529.841165 21037.784168 34867.178579 24602.113619 36989.893750 -2584.107710 -2584.107710 -2584.107710 -2600.855845 -2600.855845 -2600.855845 16.748135 16.748135 16.748135 0.0 0.0 0.0 27945.733455
2709 2020-08-24 30520.709807 24635.107065 39196.378986 24575.270588 37008.436098 1067.904107 1067.904107 1067.904107 1037.757804 1037.757804 1037.757804 30.146303 30.146303 30.146303 0.0 0.0 0.0 31588.613914
2710 2020-08-25 30511.578450 25064.572727 38441.113816 24547.061486 37026.978446 1065.292864 1065.292864 1065.292864 1023.257935 1023.257935 1023.257935 42.034929 42.034929 42.034929 0.0 0.0 0.0 31576.871313
2711 2020-08-26 30502.447092 25440.035114 38792.791238 24518.852384 37045.520794 1088.492638 1088.492638 1088.492638 1036.373822 1036.373822 1036.373822 52.118816 52.118816 52.118816 0.0 0.0 0.0 31590.939729
fig = plot_plotly(m, forecast)  # This returns a plotly Figure
layout = dict(title = '任天堂株価予測',
    title_font=dict(size=24, family='Courier', color='black'),
    yaxis=dict(title='株価',title_font=dict(size=22)
                ,tickfont=dict(size=20)),
    xaxis=dict(title='年',title_font=dict(size=22),tickfont=dict(size=20)),
    autosize=False,width=800, height=640,
    hovermode= 'x',
    hoverlabel=dict(font=dict(size=24)),
    legend=dict(x=-.001,y=1,font=dict(size=21,color='black'),bgcolor='rgba(0,0,0,0)'),
    legend_orientation="v"
              )
fig.layout.update(layout)
plot(fig,show_link=False,filename="stock_price_nintendo.html",include_plotlyjs=False)

任天堂株は来年は下降局面に入るようである。

任天堂ADR株価データの準備

end = datetime.now()
start = datetime(end.year - 40,end.month,end.day)

df1 = web.DataReader('NTDOY','yahoo',start,end)['Adj Close']
df1.head()
Date
1996-11-18    6.180383
1996-11-19    6.180383
1996-11-20    6.223302
1996-11-21    6.180383
1996-11-22    6.266223
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 6.180383
1 1996-11-19 6.180383
2 1996-11-20 6.223302
3 1996-11-21 6.180383
4 1996-11-22 6.266223

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
6092 2020-08-23
6093 2020-08-24
6094 2020-08-25
6095 2020-08-26
6096 2020-08-27
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
6092 2020-08-23 57.084252 50.781390 63.768872 52.456848 61.523961 0.276235 0.276235 0.276235 0.241934 0.241934 0.241934 0.034302 0.034302 0.034302 0.0 0.0 0.0 57.360487
6093 2020-08-24 57.105031 49.966056 64.463916 52.443909 61.596453 -0.055273 -0.055273 -0.055273 -0.113155 -0.113155 -0.113155 0.057882 0.057882 0.057882 0.0 0.0 0.0 57.049758
6094 2020-08-25 57.125810 50.252397 63.790615 52.432105 61.637206 -0.011651 -0.011651 -0.011651 -0.093765 -0.093765 -0.093765 0.082114 0.082114 0.082114 0.0 0.0 0.0 57.114160
6095 2020-08-26 57.146590 50.831000 63.680583 52.426801 61.673495 0.038490 0.038490 0.038490 -0.068230 -0.068230 -0.068230 0.106720 0.106720 0.106720 0.0 0.0 0.0 57.185080
6096 2020-08-27 57.167369 50.117449 64.153498 52.407436 61.709783 0.029835 0.029835 0.029835 -0.101609 -0.101609 -0.101609 0.131444 0.131444 0.131444 0.0 0.0 0.0 57.197204
fig = plot_plotly(m, forecast)  # This returns a plotly Figure
layout = dict(title = '任天堂ADR株価予測',
    title_font=dict(size=24, family='Courier', color='black'),
    yaxis=dict(title='株価',title_font=dict(size=22)
                ,tickfont=dict(size=20)),
    xaxis=dict(title='年',title_font=dict(size=22),tickfont=dict(size=20)),
    autosize=False,width=800, height=640,
    hovermode= 'x',
    hoverlabel=dict(font=dict(size=24)),
    legend=dict(x=-.001,y=1,font=dict(size=21,color='black'),bgcolor='rgba(0,0,0,0)'),
    legend_orientation="v"
              )
fig.layout.update(layout)
plot(fig,show_link=False,filename="stock_price_nintendo_adr.html",include_plotlyjs=False)

任天堂ADRの場合、来年は上昇局面に入り、2018年3月の高値を超えるようである。