Having run my np.random numbers through a Cholesky decomposition, I'm having a problem using those numbers in a loop find the value of the assets
I have used a for loop, but it continuously gives an error. I want the initial value "s0" to continually multiply for 12 times. (Finding the 12-month return) and get the average of those 10000 simulations of the portfolio.
import numpy as np
import pandas as pd
def cholesky(rho):
L = [[0.0] * len(rho) for b in range(len(rho))]
for i in range(len(rho)):
for j in range(i+1):
s = sum(L[i][k] * L[j][k] for k in range(j))
L[i][j] = ((rho[i][i] - s)**(1/2)) if (i == j) else \
(1.0 / L[j][j] * (rho[i][j] - s))
return L
rho = [[1],[0.5, 1],[0.1, 0.8,1]]
a = cholesky(rho)
np.random.seed(42)
normalrands=np.random.normal(0,1,[10000,3])
corrnormalrands=[]
for i in range(0,len(rho)):
corrrow=[]
for j in range (0,10000):
if i ==0:
corrrow.append(a[0][0]*normalrands[j,0])
elif i==1:
corrrow.append(a[1][0]*normalrands[j,0]+a[1][1]*normalrands[j,1])
else:
corrrow.append(a[2][0]*normalrands[j,0]+a[2] [1]*normalrands[j,1]+a[2][2]*normalrands[j,2])
corrnormalrands.append(corrrow)
def portfoliostock(s0,mu,sigma,months):
x = corrnormalrands
price=[]
for i in range [0,10000]:
stockvalue=s0
for j in range (0,months):
stockvalue=stockvalue*np.exp(mu+sigma*x(i,j))
price.append(stockvalue)
return price
stocks =portfoliostock(50000,0.005833,0.057733,12)
print ("stock value =" , np.mean(stocks))
for i in range corrnormalrands[0,10000]:
^
SyntaxError: invalid syntax
Aucun commentaire:
Enregistrer un commentaire