ファイナンス機械学習:交差検証によるハイパーパラメータの調整 練習問題 インサンプルのシャープレシオと評価関数
上記の前記事で行ったグリッドサーチの結果から、インサンプルのシャープレシオを計算する。
シャープレシオは、グリッドサーチで見つけられた最適パラメータでのCVの平均スコアとその分散で与えられるから、グリッドサーチの結果を使って次のように実装する。
def getSharpeRatioIS(clf):
best = np.argmin(clf.cv_results_['rank_test_score'])
mean_score = clf.cv_results_['mean_test_score'][best]
std_score = clf.cv_results_['std_test_score'][best]
if mean_score < 0:
return -mean_score / std_score
else:
return mean_score / std_score
Grid SearchCV neg_log_loss
clf = GridSearchCV(estimator = svm,
param_grid = param_grid,
scoring='neg_log_loss',
n_jobs=-1,
refit=True,
cv= cv,
verbose=10)
clf.fit(X,y['bin'],sample_weight = None)
print(f'Sharpe ratio: {getSharpeRatioIS(clf)}')
Grid SearchCV accuracy
clf = GridSearchCV(estimator = svm,
param_grid = param_grid,
scoring='accuracy',
n_jobs=-1,
refit=True,
cv= cv,
verbose=10)
clf.fit(X,y['bin'],sample_weight = None)
print(f'Sharpe ratio: {getSharpeRatioIS(clf)}')
RadomizedSearchCV with neg_log_loss
param_distributions = {'C':loguniform(a = 1e-2, b= 1e2),
'gamma':loguniform(a = 1e-2, b= 1e2)}
Rclf = RandomizedSearchCV(estimator = svm,
param_distributions = param_distributions,
n_iter = 25,
scoring='neg_log_loss',
n_jobs=None,
refit=True,
cv= cv,
verbose=10)
Rclf.fit(X,y['bin'],sample_weight = None)
print(f'Sharpe ratio: {getSharpeRatioIS(Rclf)}')
RadomizedSearchCV with accuracy
Rclf = RandomizedSearchCV(estimator = svm,
param_distributions = param_distributions,
n_iter = 25,
scoring='accuracy',
n_jobs=None,
refit=True,
cv= cv,
verbose=10)
Rclf.fit(X,y['bin'],sample_weight = None)
print(f'Sharpe ratio: {getSharpeRatioIS(Rclf)}')
この記事が気に入ったらサポートをしてみませんか?