10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Monte Carlo Simulation

R0[i,j]=sp.random.normal(loc=mean_0[j],scale=std_0[j],size=1)

if(R0.any()<=-1.0):

print ('Error: return is <=-100%')

#

# Step 4: generate correlated return matrix: Cholesky

R=np.dot(R0,U)

R=np.array(R)

#

# Step 5: define a few functions

def objFunction(W, R, target_ret):

stock_mean=np.mean(R,axis=0)

port_mean=np.dot(W,stock_mean)

# portfolio mean

cov=np.cov(R.T)

# var-covar matrix

port_var=np.dot(np.dot(W,cov),W.T) # portfolio variance

penalty = 2000*abs(port_mean-target_ret) # penalty 4 deviation

return np.sqrt(port_var) + penalty # objective function

#

# Step 6: estimate optimal portfolo for a given return

out_mean,out_std,out_weight=[],[],[]

stockMean=np.mean(R,axis=0)

#

for r in np.linspace(np.min(stockMean), np.max(stockMean), num=100):

W = sp.ones([nStocks])/nStocks

# starting:equal w

b_ = [(0,1) for i in range(nStocks)] # bounds

c_ = ({'type':'eq', 'fun': lambda W: sum(W)-1. })# constraint

result=minimize(objFunction,W,(R,r),method='SLSQP',constraints=c_,

bounds=b_)

if not result.success:

# handle error

raise BaseException(result.message)

out_mean.append(round(r,4))

# a few decimal places

std_=round(np.std(np.sum(R*result.x,axis=1)),6)

out_std.append(std_)

out_weight.append(result.x)

#

# Step 7: plot the efficient frontier

plt.title('Simulation for an Efficient Frontier: '+str(nStocks)+'

stocks')

plt.xlabel('Standard Deviation of the Porfolio')

plt.ylabel('Return of the2-stock portfolio')

plt.plot(out_std,out_mean,'--',linewidth=3)

plt.show()

[ 458 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!