リターンを評価する
次に、ポートフォリオの選択結果が SPY への投資と比べてどのようなパフォーマンスを示すか確認しましょう。R\(^2\) の値が低くても、予測結果が有望かどうかを見極めるために行います。
初期投資額を1000ドルに設定し、予測結果と SPY それぞれのリターンをループ処理します。ポートフォリオ選択と SPY の月次リターンを初期残高に適用することで、月ごとの投資状況を把握できます。そして、予測結果全体と SPY のパフォーマンスを比較します。最後に、予測に基づくポートフォリオをグラフにプロットして SPY と比較しましょう。
この演習はコースの一部です
Python による金融のための Machine Learning
演習の手順
algo_cashとspy_cashの両方のリストの最初の要素を、同じ金額(cash)に設定します。test_returnsのループ内で、cashに1 + rを掛けてリターンをcashに反映します。test_returnsのループと同様に、SPY パフォーマンスのループ内でも1 + rを掛けた後にcashをspy_cashに追加します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Calculate the effect of our portfolio selection on a hypothetical $1k investment
cash = 1000
algo_cash, spy_cash = [cash], ____ # set equal starting cash amounts
for r in test_returns:
cash *= 1 + r
algo_cash.append(cash)
# Calculate performance for SPY
cash = 1000 # reset cash amount
for r in returns_monthly['SPY'].iloc[train_size:]:
cash ____ ____
____
print('algo returns:', (algo_cash[-1] - algo_cash[0]) / algo_cash[0])
print('SPY returns:', (spy_cash[-1] - spy_cash[0]) / spy_cash[0])