k = np.random.rand(n)
return k / sum(k)
if (weights == ""):
opt_weight = np.asmatrix(generate_weigths(returns.shape[0]))
else:
z=weights
we = []
for x in range(0,len(df_opt)):
we.append(z)
opt_weight = np.asmatrix(we)
opt_cov = np.asmatrix(np.cov(returns))
mu = opt_weight * opt_mean.T
sigma = np.sqrt(opt_weight * opt_cov * opt_weight.T)
if sigma > 2:
return portfolio(returns, weights)
return mu, sigma
def optimizate_portfolio(returns):
n = len(returns)
returns = np.asmatrix(returns)
N = 100
mus = [10**(5.0 * t/N - 1.0) for t in range(N)]
plt.scatter(results_df.Std,results_df.Return,c=results_df.Sharpe,cmap='afmhot')
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.colorbar()
#plot red star to highlight position of portfolio with highest Sharpe Ratio
plt.scatter(max_sharpe.iloc[:,1],max_sharpe.iloc[:,0],marker=(5,1,0),color='r',s=10
00)
#plot green star to highlight position of minimum variance portfolio
plt.scatter(min_sharpe.iloc[:,1],min_sharpe.iloc[:,0],marker=(5,1,0),color='g',s=10
00)
plt.show()
for i in range(100):
custom_returns=np.random.normal(mu/T,vol/sqrt(T),T)+1
price_list = [S]
for x in custom_returns:
price_list.append(price_list[-1]*x)
plt.plot(price_list)
plt.yticks(np.arange(0, 500, 50))
plt.title("Random Walk")
result.append(price_list[-1])
plt.show()
plt.hist(result,bins=50)
plt.show()
plt.hist(result,bins=50, color="orange")
plt.axvline(np.percentile(result,5), color='r', linestyle='dotted', linewidth=2)
plt.axvline(np.percentile(result,95), color='r', linestyle='dotted', linewidth=2)
plt.title("Monte Carlo with Random Walk")
plt.show()