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.

leftTail=int(n*(1-confidence_level))

print(leftTail)

#

VaR2=position*ret2[leftTail]

print("Holding=",position, "VaR=", round(VaR2,4), "tomorrow")

('Holding=', 26503.070499999998, 'VaR=', -816.7344, 'tomorrow')

The result shows that the 1-day VaR is $817. Recall that the VaR based on the

normality is $648. If the second method is more accurate, the first method

underestimates our potential loss by 20%. This is a huge number in terms of risk

evaluation! The following codes are for an n-day period based on sorting:

[ 407 ]

Chapter 11

ret = x.aclose[1:]/x.aclose[:-1]-1

position=n_shares*x.close[0]

#

# Method 1: based on normality

mean=np.mean(ret)

std=np.std(ret)

meanNdays=(1+mean)**nDays-1

stdNdays=std*np.sqrt(nDays)

z=norm.ppf(confidence_level)

VaR1=position*z*stdNdays

print("Holding=",position, "VaR1=", round(VaR1,0), "in ", nDays,

"Days")

#

# method 2: calculate 10 day returns

ddate=[]

d0=x.date

for i in range(0,np.size(logret)):

ddate.append(int(i/nDays))

y=pd.DataFrame(logret,index=ddate,columns=['retNdays'])

logRet=y.groupby(y.index).sum()

retNdays=np.exp(logRet)-1

#

VaR2=position*z*np.std(retNdays)

print("Holding=",position, "VaR2=", round(VaR2,0), "in ", nDays,

"Days")

#

# Method III

ret2=np.sort(retNdays)

n=np.size(ret2)

leftTail=int(n*(1-confidence_level))

print(leftTail)

#

VaR3=position*ret2[leftTail]

print("Holding=",position, "VaR=", round(VaR3,0), "in ",nDays, "Days")

('Holding=', 24853.456000000002, 'VaR1=', 2788.0, 'in ', 10, 'Days')

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

Saved successfully!

Ooh no, something went wrong!