【Python】【経済指標】米国の雇用統計と新規失業保険申請件数のデータを取得して描画する
米国雇用統計は、機関投資家のみならず個人投資家にとっても、投資をする上でとても注目度の高い経済指標と言われています。ではなぜなのでしょうか?それは米国の中央銀行である連邦準備制度理事会 FRBが金融政策を決める上でとても重視している経済統計だからです。
FRBの役割として特に重要なのは、国民の雇用を最大化することと、物価を安定させることです。FRBが重視する米国雇用統計は、自国の金融政策の方向を左右するのです。
また、新規失業保険申請件数は、特に近年になって注目されるようになった米国の経済指標です。申請された失業保険の件数をトラックしたものです。
ここでは、雇用統計や新規失業保険申請件数に関するデータを取得し描画していきます。
なお、最低限のポイントのみの説明にするため、Pythonライブラリ、モジュール等のインストール方法については割愛させて頂きます。お使いのPC環境等に合わせてインストールしてもらえればと思います。
1.米国の雇用統計と新規失業保険申請件数データを取得する
米国の雇用統計や新規失業保険申請件数のデータを分析するには、pandasのDataFrameオブジェクトにデータを用意する必要があります。ここでは、非農業部門雇用者数、失業率、労働参加率、平均時給、新規失業保険申請件数のデータをFREDから取得していきます。FREDは米国のセントルイス連銀が運営するサイトで一般的には十分な世界中の公的な経済データが手に入ります。データの取得期間は、2018年1月1日から現在の日付までです。
import pandas_datareader.data as web
import datetime
# 2018/1/1から現在まで
start = '2018-01-01'
end = datetime.date.today()
# 米非農業部門雇用者数の取得
df_payems = web.DataReader(['PAYEMS'], 'fred', start, end)
df_payems = df_payems.dropna() # 空データ削除
# 失業率
df_unrate = web.DataReader(["UNRATE"], "fred", start, end)
df_unrate = df_unrate.dropna() # 空データ削除
# 労働参加率
df_civpart = web.DataReader(["CIVPART"], "fred", start, end)
df_civpart = df_civpart.dropna() # 空データ削除
# 平均時給
df_hourly_earnings = web.DataReader(["CES0500000003"], "fred", start, end)
df_hourly_earnings = df_hourly_earnings.dropna() # 空データ削除
# 新規失業保険申請件数
df_icsa = web.DataReader(["ICSA"], "fred", start, end)
df_icsa = df_icsa.dropna() # 空データ削除
https://note.com/scilabcafe/n/n0060466ef5c3
2.グラフの描画
ここでは、plotlyを使って、米非農業部門雇用者数、失業率、労働参加率、平均時給、新規失業保険申請件数の変化をグラフに表示していきます。plotlyについては、以前に描画方法と見た目に関わる設定について投稿しているので参考にしてもらえればと思います。
●plotlyでグラフを描画
import plotly.graph_objects as go # グラフ表示関連ライブラリ
import plotly.io as pio # 入出力関連ライブラリ
pio.renderers.default = 'iframe'
# subplot
from plotly.subplots import make_subplots
# グラフの実体となる trace オブジェクトを生成
payems_trace = go.Scatter(
x=df_payems.index,
y=df_payems['PAYEMS'],
mode='lines',
line={'color':'red', 'width':4},
name='米非農業部門雇用者数'
)
unrate_trace = go.Scatter(
x=df_unrate.index,
y=df_unrate['UNRATE'],
mode='lines',
line={'color':'red', 'width':4},
name='失業率'
)
civpart_trace = go.Scatter(
x=df_civpart.index,
y=df_civpart['CIVPART'],
mode='lines',
line={'color':'red', 'width':4},
name='労働参加率'
)
hourly_earnings_trace = go.Scatter(
x=df_hourly_earnings.index,
y=df_hourly_earnings['CES0500000003'],
mode='lines',
line={'color':'red', 'width':4},
name='平均時給'
)
icsa_trace = go.Scatter(
x=df_icsa.index,
y=df_icsa['ICSA'],
mode='lines',
line={'color':'blue', 'width':4},
name='新規失業保険申請件数'
)
subplots_fig = make_subplots(
rows=5,
cols=1,
start_cell='top-left',
subplot_titles=[
'米非農業部門雇用者数',
'失業率',
'労働参加率',
'平均時給',
'新規失業保険申請件数'
],
horizontal_spacing=0.15,
vertical_spacing=0.08,
)
# 描画領域である figure オブジェクトの作成
subplots_fig.add_trace(payems_trace, row=1, col=1)
subplots_fig.add_trace(unrate_trace, row=2, col=1)
subplots_fig.add_trace(civpart_trace, row=3, col=1)
subplots_fig.add_trace(hourly_earnings_trace, row=4, col=1)
subplots_fig.add_trace(icsa_trace, row=5, col=1)
# レイアウトの更新
subplots_fig.update_layout(
# 凡例は消去
showlegend=False,
# 幅と高さの設定
width=900,height=1000,
title='経済指標(米国雇用統計)',
plot_bgcolor='white', # 背景色を白に設定
),
# 軸の設定
subplots_fig.update_yaxes(title='Thousands of Persons', row=1, col=1)
subplots_fig.update_yaxes(title='Percent(%)', row=2, col=1)
subplots_fig.update_yaxes(title='Percent(%)', row=3, col=1)
subplots_fig.update_yaxes(title='Dollars per Hour', row=4, col=1)
subplots_fig.update_yaxes(title='Number', row=5, col=1)
# linecolorを設定して、ラインをミラーリング(mirror=True)して枠にする
subplots_fig.update_xaxes(linecolor='black', linewidth=1, mirror=True, row=1, col=1)
subplots_fig.update_yaxes(linecolor='black', linewidth=1, mirror=True, row=1, col=1)
subplots_fig.update_xaxes(linecolor='black', linewidth=1, mirror=True, row=2, col=1)
subplots_fig.update_yaxes(linecolor='black', linewidth=1, mirror=True, row=2, col=1)
subplots_fig.update_xaxes(linecolor='black', linewidth=1, mirror=True, row=3, col=1)
subplots_fig.update_yaxes(linecolor='black', linewidth=1, mirror=True, row=3, col=1)
subplots_fig.update_xaxes(linecolor='black', linewidth=1, mirror=True, row=4, col=1)
subplots_fig.update_yaxes(linecolor='black', linewidth=1, mirror=True, row=4, col=1)
subplots_fig.update_xaxes(linecolor='black', linewidth=1, mirror=True, row=5, col=1)
subplots_fig.update_yaxes(linecolor='black', linewidth=1, mirror=True, row=5, col=1)
# ticks='inside':目盛り内側, tickcolor:目盛りの色, tickwidth:目盛りの幅、ticklen:目盛りの長さ
subplots_fig.update_xaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=1, col=1)
subplots_fig.update_yaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=1, col=1)
subplots_fig.update_xaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=2, col=1)
subplots_fig.update_yaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=2, col=1)
subplots_fig.update_xaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=3, col=1)
subplots_fig.update_yaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=3, col=1)
subplots_fig.update_xaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=4, col=1)
subplots_fig.update_yaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=4, col=1)
subplots_fig.update_xaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=5, col=1)
subplots_fig.update_yaxes(ticks='inside', tickcolor='black', tickwidth=1, ticklen=5, row=5, col=1)
# gridcolor:グリッドの色, gridwidth:グリッドの幅、griddash='dot':破線
subplots_fig.update_xaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=1, col=1)
subplots_fig.update_yaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=1, col=1)
subplots_fig.update_xaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=2, col=1)
subplots_fig.update_yaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=2, col=1)
subplots_fig.update_xaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=3, col=1)
subplots_fig.update_yaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=3, col=1)
subplots_fig.update_xaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=4, col=1)
subplots_fig.update_yaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=4, col=1)
subplots_fig.update_xaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=5, col=1)
subplots_fig.update_yaxes(gridcolor='lightgrey', gridwidth=1, griddash='dot', row=5, col=1)
# tick0:初期軸目盛り, dtick:軸目盛り間隔
subplots_fig.update_xaxes(dtick='M6') # 6カ月ごと
# 軸の文字サイズ変更
subplots_fig.update_xaxes(tickfont=dict(size=14, color='grey'))
subplots_fig.update_yaxes(tickfont=dict(size=16, color='grey'))
# show()メソッドでグラフを描画
subplots_fig.show()
今回は、個人投資家にとっても、投資をする上でとても注目度の高い経済指標と言われている、雇用統計や新規失業保険申請件数に関するデータを取得して、plotlyを用いてデータの可視化を行いました。今後は、他の経済指標のデータ取得や他の経済指標と合わせた分析を行っていきたいと思います。
この記事が気に入ったらサポートをしてみませんか?