import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ファイルの読み込み
file_path = 'path_to_your_local_file/合成用資料_1.xlsx' # ローカルファイルのパスに置き換えてください
data = pd.read_excel(file_path)
期間定義
periods = {
'2014-2015': ('2014-04-30', '2015-12-31'),
'2016-2019': ('2016-01-01', '2019-12-31'),
'2020-2021': ('2020-01-01', '2021-12-31'),
'2022-2024': ('2022-01-01', '2024-04-30')
}
ファクターポートフォリオの構築と評価
factor_names = ['BPR', 'EST_SENTIMENT', 'EST_LIQUIDITY', 'INCM_NORM', 'ROA', 'Sales Growth', 'Price Momentum']
results = {factor: [] for factor in factor_names}
for period, (start, end) in periods.items():
period_data = data[(data['TDATE'] >= pd.to_datetime(start)) & (data['TDATE'] <= pd.to_datetime(end))]
for factor in factor_names:
factor_portfolio = period_data.sort_values(by=factor, ascending=False).iloc[:int(len(period_data) * 0.1)]
cumulative_return = (factor_portfolio['ROR1MF'] / 100 + 1).prod() - 1
average_return = factor_portfolio['ROR1MF'].mean()
risk = factor_portfolio['ROR1MF'].std()
sharpe_ratio = average_return / risk if risk != 0 else np.nan
results[factor].append({
'Period': period,
'Cumulative Return': cumulative_return,
'Average Return': average_return,
'Risk': risk,
'Sharpe Ratio': sharpe_ratio
})
結果の集計と視覚化
results_df = pd.DataFrame([(factor, period, res['Cumulative Return'], res['Average Return'], res['Risk'], res['Sharpe Ratio'])
for factor, values in results.items()
for period, res in zip(periods.keys(), values)],
columns=['Factor', 'Period', 'Cumulative Return', 'Average Return', 'Risk', 'Sharpe Ratio'])
各指標の棒グラフ作成
for metric in ['Cumulative Return', 'Average Return', 'Risk', 'Sharpe Ratio']:
fig, ax = plt.subplots(figsize=(12, 8))
for factor in factor_names:
subset = results_df[results_df['Factor'] == factor]
ax.bar(subset['Period'], subset[metric], label=factor)
ax.set_title(f'{metric} by Factor and Period')
ax.set_xlabel('Period')
ax.set_ylabel(metric)
ax.legend(title='Factor')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()