[機械学習・進化計算による株式取引最適化] No.03-03 収益グラフの表示

このプログラムの目的

このプログラムはdqn.pyなどの学習によって得られたエージェントの収益グラフ,もしくはその学習過程をグラフとして出力するものです.

work_share
├02_get_stock_price
└03_dqn_learning
  ├Dockerfile
  ├docker-compose.yml
  └src
    ├draw_graph
    |  └draw_tools.py (これを作成)
    ├enviroment
    |  └stock_env.py
    ├reinforcement_learning
    |  └dqn.py
    ├result(自動生成)
    └experiment01.py

保存したデータのグラフ化

分けて話すようなことでもないので,一括で載せます.
plot_total_assets_graphとplot_money_graphは実は統合できます.ほとんど中身が同じです.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

def plot_log(log, save_path, key):
    x = log['i']
    y = log[key]
    plt.cla()
    plt.clf()
    plt.plot(x, y)
    plt.title(key)
    plt.savefig(save_path, bbox_inches='tight')

def plot_total_assets_graph(log, save_path, title):
    plt.cla()
    plt.clf()

    df = log.groupby('date').mean()
    df = df.reset_index()
    df['date'] = pd.to_datetime(df['date'])
    df = df.sort_values('date').reset_index(drop=True)

    fig, ax = plt.subplots()
    ax.plot(df['date'], df['total_assets'])
    ax.set_xlabel('date')
    ax.set_title(title)
    ax.xaxis.set_tick_params(rotation=30)
    ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
    ax.ticklabel_format(style="sci",  axis="y",scilimits=(0,0))

    plt.savefig(save_path, bbox_inches='tight')

def plot_money_graph(log, save_path, title, key):
    plt.cla()
    plt.clf()

    df = log.groupby('date').mean()
    df = df.reset_index()
    df['date'] = pd.to_datetime(df['date'])
    df = df.sort_values('date').reset_index(drop=True)

    fig, ax = plt.subplots()
    ax.plot(df['date'], df[key])
    ax.set_xlabel('date')
    ax.set_title(key)
    ax.xaxis.set_tick_params(rotation=30)
    ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
    ax.ticklabel_format(style="sci",  axis="y",scilimits=(0,0))

    plt.savefig(save_path, bbox_inches='tight')
タイトルとURLをコピーしました