Professional Documents
Culture Documents
_A_Comprehensive_Guide_for_Measuring_Investment_Risks_1712104631
_A_Comprehensive_Guide_for_Measuring_Investment_Risks_1712104631
## stap by stap A Comprehensive Guide to Python Code for Measuring Investment Risks
# Fetch historical price data for the asset and benchmark from a reliable data source
asset_data = yf.download(ticker, start_date, end_date)
benchmark_data = yf.download(benchmark_ticker, start_date, end_date)
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['AAPL']: ValueError("time data '2024–03–21' does not match format '%Y-%m-%d'")
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['^GSPC']: ValueError("time data '2024–03–21' does not match format '%Y-%m-%d'")
# Standard Deviation
standard_deviation = asset_returns.std()
# Sharpe Ratio
risk_free_rate = 0.03 # Assume a risk-free rate of 3%
sharpe_ratio = (average_return - risk_free_rate) / standard_deviation
## To calculate the Sortino ratio, we need to consider only negative returns. Here’s an example code snippet:
# Sortino Ratio
downside_returns = asset_returns[asset_returns < 0]
average_downside_return = downside_returns.mean()
sortino_ratio = (average_return - risk_free_rate) / downside_returns.std()
# 4.3 Beta
model = sm.OLS(asset_returns, sm.add_constant(benchmark_returns)).fit()
beta = model.params[1]
### 4.4 Max Drawdown: Assessing the Largest Decline in Investment Value
## To visualize and analyze max drawdown, we can plot a cumulative returns chart and calculate the maximum
# drawdown. Here’s an example code snippet:
# Max Drowdown
cum_returns = (asset_returns + 1).cumprod()
cum_max = cum_returns.cummax()
drawdown = (cum_max - cum_returns) / cum_max
plt.plot(drawdown)
plt.title('Max Drawdown')
plt.xlabel('Date')
plt.ylabel('Drawdown')
plt.show()
max_drawdown = drawdown.max()
## To calculate VaR, we can utilize historical simulation assuming future returns follow the historical
# distribution. Here’s an example code snippet:
# To calculate CVaR, we can use the tail portion of returns beyond the VaR level. Here’s an example code
# snippet:
# To calculate R-squared, we can perform linear regression between the asset’s returns and the benchmark
# returns. Here’s an example code snippet:
# R-squared
model = sm.OLS(asset_returns, sm.add_constant(benchmark_returns)).fit()
r_squared = model.rsquared
#5 Complet code
start_date = dt.datetime.today() - dt.timedelta(days=365) # Start date for obtaining historical price data
end_date = dt.datetime.today() # End date for obtaining historical price data
# Fetch historical price data for the asset and benchmark from a reliable data source
asset_data = yf.download(ticker, start_date, end_date)
benchmark_data = yf.download(benchmark_ticker, start_date, end_date)
# Standard Deviation
standard_deviation = asset_returns.std()
# Sharpe Ratio
risk_free_rate = 0.03 # Assume a risk-free rate of 3%
sharpe_ratio = (average_return - risk_free_rate) / standard_deviation
# Sortino Ratio
downside_returns = asset_returns[asset_returns < 0]
average_downside_return = downside_returns.mean()
sortino_ratio = (average_return - risk_free_rate) / downside_returns.std()
# Beta
model = sm.OLS(asset_returns, sm.add_constant(benchmark_returns)).fit()
beta = model.params[1]
# Max Drowdown
cum_returns = (asset_returns + 1).cumprod()
cum_max = cum_returns.cummax()
drawdown = (cum_max - cum_returns) / cum_max
plt.plot(drawdown)
plt.title('Max Drawdown')
plt.xlabel('Date')
plt.ylabel('Drawdown')
plt.show()
max_drawdown = drawdown.max()
# Value at Risk (VaR)
confidence_level = 0.95
var = np.percentile(asset_returns, 100 - confidence_level * 100)
# R-squared
model = sm.OLS(asset_returns, sm.add_constant(benchmark_returns)).fit()
r_squared = model.rsquared
[*********************100%%**********************] 1 of 1 completed
[*********************100%%**********************] 1 of 1 completed
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js