Sharpe Ratio: ฟีเจอร์และเป้าหมาย

Machine Learning สำหรับการเงินด้วย Python

Nathan George

Data Science Professor

พอร์ตโฟลิโอที่ดีที่สุด

Machine Learning สำหรับการเงินด้วย Python

พอร์ตโฟลิโอที่ดีที่สุดพร้อมจุด Sharpe

Machine Learning สำหรับการเงินด้วย Python

สมการ Sharpe Ratio

Machine Learning สำหรับการเงินด้วย Python

คำนวณ Sharpe Ratio

# empty dictionaries for sharpe ratios and best sharpe indexes by date
sharpe_ratio, max_sharpe_idxs = {}, {}

# loop through dates and get sharpe ratio for each portfolio for date in portfolio_returns.keys(): for i, ret in enumerate(portfolio_returns[date]): volatility = portfolio_volatility[date][i] sharpe_ratio.setdefault(date,[]).append(ret / volatility) # get the index of the best sharpe ratio for each date max_sharpe_idxs[date] = np.argmax(sharpe_ratio[date])
Machine Learning สำหรับการเงินด้วย Python

สร้างฟีเจอร์

# calculate exponentially-weighted moving average of daily returns
ewma_daily = returns_daily.ewm(span=30).mean()

# resample daily returns to first business day of the month
ewma_monthly = ewma_daily.resample('BMS').first()

# shift ewma 1 month forward
ewma_monthly = ewma_monthly.shift(1).dropna()
Machine Learning สำหรับการเงินด้วย Python

คำนวณฟีเจอร์และเป้าหมาย

targets, features = [], []

# create features from price history and targets as ideal portfolio for date, ewma in ewma_monthly.iterrows(): # get the index of the best sharpe ratio best_idx = max_sharpe_idxs[date] targets.append(portfolio_weights[date][best_idx]) features.append(ewma) targets = np.array(targets) features = np.array(features)
Machine Learning สำหรับการเงินด้วย Python
# latest date
date = sorted(covariances.keys())[-1]

cur_returns = portfolio_returns[date] cur_volatility = portfolio_volatility[date]
plt.scatter(x=cur_volatility, y=cur_returns, alpha=0.1, color='blue') best_idx = max_sharpe_idxs[date] plt.scatter(cur_volatility[best_idx], cur_returns[best_idx], marker='x', color='orange') plt.xlabel('Volatility') plt.ylabel('Returns') plt.show()
Machine Learning สำหรับการเงินด้วย Python

Efficient Frontier พร้อม Sharpe

Machine Learning สำหรับการเงินด้วย Python

มาฝึกกันเถอะ!

Machine Learning สำหรับการเงินด้วย Python

Preparing Video For Download...