This worksheet looks at VaR calculations for the two cases of single and multiple equity asset. This is the last lab worksheet, other than the extra credit assignment.
Remember to always set your working directory to the source file location. Go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Read carefully the below and follow the instructions to complete the tasks and answer any questions. Submit your work to RPubs as detailed in previous notes.
Always read carefully the instructions on Sakai. For clarity, tasks/questions to be completed/answered are highlighted in red color (visible in preview) and numbered according to their particular placement in the task section. Quite often you will need to add your own code chunk.
Execute all code chunks, preview, publish, and submit link on Sakai follwoing the naming convention. Make sure to add comments to your code where appropriate. Use own language!
#Install package quantmod
install.packages("quantmod")
library("quantmod")
Consider the time series of a stock of your choice (other than AAPL and your colleague!) and for the time-period from Jan 1, 2015 to present.
We will first generate the historical time series of daily log returns, and calculate the mean and standard deviation of the time series.
##### 1A) Calculate the historical daily log returns, mean and standard deviation
getSymbols('NKE',src="yahoo")
[1] "NKE"
NikeLog = periodReturn(NKE, period = 'daily', type = 'log', subset='2015::')
NikeSd=sd(NikeLog)
NikeMean=mean(NikeLog)
NikeLog
daily.returns
2015-01-02 -1.171688e-02
2015-01-05 -1.623117e-02
2015-01-06 -5.899765e-03
2015-01-07 2.044588e-02
2015-01-08 2.282177e-02
2015-01-09 -1.108532e-02
2015-01-12 -1.355205e-03
2015-01-13 -8.906623e-03
2015-01-14 -1.377726e-02
2015-01-15 -9.650536e-03
2015-01-16 1.937568e-03
2015-01-20 6.645276e-03
2015-01-21 -1.068812e-03
2015-01-22 2.471604e-02
2015-01-23 3.229064e-03
2015-01-26 1.973862e-03
2015-01-27 -1.938754e-02
2015-01-28 -1.267257e-02
2015-01-29 1.658029e-02
2015-01-30 -2.800527e-02
2015-02-02 -3.583671e-03
2015-02-03 1.543724e-02
2015-02-04 -8.714865e-03
2015-02-05 8.607714e-03
2015-02-06 -1.674534e-02
2015-02-09 -6.777486e-03
2015-02-10 1.718183e-02
2015-02-11 -1.564741e-02
2015-02-12 7.528288e-03
2015-02-13 4.346881e-04
2015-02-17 -1.957586e-03
2015-02-18 1.897838e-02
2015-02-19 8.540893e-04
2015-02-20 1.304163e-02
2015-02-23 8.423924e-04
2015-02-24 4.306009e-03
2015-02-25 1.456541e-02
2015-02-26 1.754275e-03
2015-02-27 1.339488e-03
2015-03-02 1.664406e-02
2015-03-03 -7.930074e-03
2015-03-04 -4.603886e-03
2015-03-05 9.592930e-03
2015-03-06 -1.586762e-02
2015-03-09 5.351417e-03
2015-03-10 -9.487521e-03
2015-03-11 -1.219724e-02
2015-03-12 1.767386e-02
2015-03-13 -1.275622e-02
2015-03-16 6.554031e-03
2015-03-17 1.036356e-03
2015-03-18 9.997527e-03
2015-03-19 8.272508e-03
2015-03-20 3.654929e-02
2015-03-23 -5.112123e-03
2015-03-24 -8.015460e-03
2015-03-25 -1.663036e-02
2015-03-26 3.428846e-03
2015-03-27 5.521785e-03
2015-03-30 1.006137e-02
2015-03-31 -5.566042e-03
2015-04-01 -7.804702e-03
2015-04-02 1.104362e-03
2015-04-06 7.021415e-04
2015-04-07 -1.204013e-03
2015-04-08 1.227250e-02
2015-04-09 -1.190653e-03
2015-04-10 -7.474249e-03
2015-04-13 -7.026791e-03
2015-04-14 2.615716e-03
2015-04-15 3.009673e-03
2015-04-16 7.009463e-04
2015-04-17 -1.360563e-02
2015-04-20 1.450609e-02
2015-04-21 2.995828e-03
2015-04-22 -1.396907e-03
2015-04-23 9.539951e-03
2015-04-24 -1.583728e-03
2015-04-27 -9.900441e-05
2015-04-28 -1.075710e-02
2015-04-29 3.099518e-03
2015-04-30 -1.336638e-02
2015-05-01 1.943756e-02
2015-05-04 4.960468e-04
2015-05-05 -4.074580e-03
2015-05-06 -1.993581e-03
2015-05-07 1.219824e-02
2015-05-08 9.711254e-03
2015-05-11 3.410791e-03
2015-05-12 -4.094352e-03
2015-05-13 -2.053470e-03
2015-05-14 1.245150e-02
2015-05-15 1.477814e-02
2015-05-18 -2.193335e-03
2015-05-19 -1.050671e-03
2015-05-20 -2.867384e-04
2015-05-21 -2.009455e-03
2015-05-22 2.872959e-04
2015-05-26 -9.718643e-03
2015-05-27 -5.624004e-03
2015-05-28 -5.264631e-03
2015-05-29 -6.177457e-03
2015-06-01 8.848646e-04
2015-06-02 3.433544e-03
2015-06-03 3.909711e-03
2015-06-04 -5.477917e-03
2015-06-05 7.844103e-04
2015-06-08 -5.799358e-03
2015-06-09 -8.876572e-04
2015-06-10 1.944463e-02
2015-06-11 5.211883e-03
2015-06-12 -9.631129e-04
2015-06-15 -4.151988e-03
2015-06-16 1.163978e-02
2015-06-17 1.815509e-03
2015-06-18 1.186254e-02
2015-06-19 4.987523e-03
2015-06-22 2.437672e-03
2015-06-23 3.831922e-03
2015-06-24 -9.183778e-03
2015-06-25 -9.459019e-03
2015-06-26 4.178711e-02
2015-06-29 -1.876955e-02
2015-06-30 3.245383e-03
2015-07-01 1.296872e-02
2015-07-02 4.012791e-03
2015-07-06 -2.731419e-04
2015-07-07 1.311468e-02
2015-07-08 -1.831756e-02
2015-07-09 1.554625e-03
2015-07-10 9.819992e-03
2015-07-13 1.695647e-02
2015-07-14 -6.229856e-04
2015-07-15 -3.924318e-03
2015-07-16 3.924318e-03
2015-07-17 4.086423e-03
2015-07-20 2.921208e-03
2015-07-21 -1.238281e-03
2015-07-22 9.688274e-03
2015-07-23 -7.891272e-04
2015-07-24 -8.899147e-03
2015-07-27 -1.121402e-02
2015-07-28 1.545322e-02
2015-07-29 1.269771e-02
2015-07-30 7.828818e-04
2015-07-31 1.824263e-03
2015-08-03 -3.390570e-03
2015-08-04 8.152638e-03
2015-08-05 8.429467e-03
2015-08-06 -1.405982e-02
2015-08-07 -5.312907e-03
2015-08-10 7.395501e-03
2015-08-11 -7.832257e-03
2015-08-12 -1.230678e-02
2015-08-13 4.764855e-03
2015-08-14 6.667879e-03
2015-08-17 5.406868e-03
2015-08-18 -1.392550e-03
2015-08-19 1.218557e-03
2015-08-20 -2.341034e-02
2015-08-21 -4.956074e-02
2015-08-24 -2.847302e-02
2015-08-25 -3.278730e-03
2015-08-26 4.928223e-02
2015-08-27 3.487570e-02
2015-08-28 -1.066116e-03
2015-08-31 -6.688988e-03
2015-09-01 -2.831664e-02
2015-09-02 1.887621e-02
2015-09-03 1.354114e-03
2015-09-04 -1.051969e-02
2015-09-08 1.860601e-02
2015-09-09 -1.705741e-02
2015-09-10 4.722112e-03
2015-09-11 1.296150e-02
2015-09-14 6.258102e-04
2015-09-15 1.727767e-02
2015-09-16 1.516903e-02
2015-09-17 -2.685780e-03
2015-09-18 -1.910317e-03
2015-09-21 1.303929e-02
2015-09-22 -5.074591e-03
2015-09-23 -4.667269e-03
2015-09-24 -5.559914e-03
2015-09-25 8.520937e-02
2015-09-28 -2.314581e-02
2015-09-29 -2.042999e-02
2015-09-30 2.720250e-02
2015-10-01 6.969234e-03
2015-10-02 1.108265e-02
2015-10-05 -7.535742e-03
2015-10-06 -8.728690e-03
2015-10-07 -8.887530e-03
2015-10-08 2.275315e-02
2015-10-09 2.401281e-04
2015-10-12 1.185516e-02
2015-10-13 -4.915979e-03
2015-10-14 2.384105e-04
2015-10-15 2.317190e-02
2015-10-16 1.296021e-02
2015-10-19 2.078354e-02
2015-10-20 -6.325874e-03
2015-10-21 7.552183e-04
2015-10-22 -4.530202e-04
2015-10-23 -1.430013e-02
2015-10-26 6.490848e-03
2015-10-27 -4.271524e-03
2015-10-28 3.586178e-03
2015-10-29 -1.295667e-03
2015-10-30 -6.866146e-04
2015-11-02 1.220380e-03
2015-11-03 1.599391e-03
2015-11-04 -7.946206e-03
2015-11-05 1.144090e-02
2015-11-06 -6.069034e-04
2015-11-09 -9.913923e-03
2015-11-10 -3.685477e-03
2015-11-11 -2.004575e-02
2015-11-12 -1.136551e-02
2015-11-13 -3.325035e-02
2015-11-16 1.304437e-02
2015-11-17 -7.153323e-03
2015-11-18 2.577044e-02
2015-11-19 0.000000e+00
2015-11-20 5.317970e-02
2015-11-23 -1.433277e-03
2015-11-24 3.773106e-04
2015-11-25 1.215144e-02
2015-11-27 1.489965e-03
2015-11-30 -1.537860e-02
2015-12-01 7.756355e-03
2015-12-02 -4.963193e-03
2015-12-03 -2.697186e-02
2015-12-04 2.455664e-02
2015-12-07 -5.531753e-03
2015-12-08 7.595139e-04
2015-12-09 -2.195528e-02
2015-12-10 -1.553488e-03
2015-12-11 -1.843696e-02
2015-12-14 1.235507e-02
2015-12-15 5.848654e-03
2015-12-16 1.787820e-02
2015-12-17 -5.437425e-03
2015-12-18 -1.314078e-02
2015-12-21 9.910270e-03
2015-12-22 1.567012e-02
2015-12-23 -2.410310e-02
2015-12-24 -1.842688e-02
2015-12-28 9.922138e-03
2015-12-29 7.027451e-03
2015-12-30 -1.584226e-02
2015-12-31 -1.192857e-02
2016-01-04 -1.580423e-02
2016-01-05 1.388240e-02
2016-01-06 -1.437013e-02
2016-01-07 -2.703319e-02
2016-01-08 -1.650979e-02
2016-01-11 1.148467e-02
2016-01-12 6.527766e-03
2016-01-13 -1.954240e-02
2016-01-14 -4.603998e-03
2016-01-15 -1.636975e-02
2016-01-19 1.311719e-02
2016-01-20 1.227011e-02
2016-01-21 2.541943e-02
2016-01-22 5.270104e-03
2016-01-25 -2.961015e-03
2016-01-26 6.731821e-03
2016-01-27 -2.535555e-02
2016-01-28 2.682723e-02
2016-01-29 1.314842e-02
2016-02-01 1.837556e-02
2016-02-02 -9.704968e-03
2016-02-03 -9.596450e-04
2016-02-04 -3.783272e-02
2016-02-05 -5.114461e-02
2016-02-08 -3.796904e-02
2016-02-09 1.156080e-02
2016-02-10 3.059749e-02
2016-02-11 -2.486681e-02
2016-02-12 7.471979e-03
2016-02-16 2.399210e-02
2016-02-17 6.553956e-03
2016-02-18 7.364939e-03
2016-02-19 1.204328e-02
2016-02-22 1.439591e-02
2016-02-23 6.645789e-04
2016-02-24 3.150703e-03
2016-02-25 3.225559e-02
2016-02-26 3.520519e-03
2016-02-29 -1.626573e-02
2016-03-01 2.136453e-02
2016-03-02 -1.118754e-02
2016-03-03 -1.212724e-02
2016-03-04 -3.422198e-03
2016-03-07 -3.336129e-02
2016-03-08 9.407108e-03
2016-03-09 -2.505631e-02
2016-03-10 9.384916e-03
2016-03-11 2.017555e-02
2016-03-14 1.207722e-02
2016-03-15 9.655602e-03
2016-03-16 7.463916e-03
2016-03-17 2.111401e-02
2016-03-18 -3.011780e-03
2016-03-21 2.709427e-02
2016-03-22 2.777366e-03
2016-03-23 -3.864157e-02
2016-03-24 -1.273282e-02
2016-03-28 -5.041103e-03
2016-03-29 2.442434e-03
2016-03-30 8.904762e-03
2016-03-31 -9.230087e-03
2016-04-01 1.950253e-03
2016-04-04 -2.665507e-02
2016-04-05 -5.015106e-03
2016-04-06 1.066860e-02
2016-04-07 -1.658569e-04
2016-04-08 -1.470125e-02
2016-04-11 -2.556812e-02
2016-04-12 1.081836e-02
2016-04-13 1.743891e-02
2016-04-14 -1.511716e-03
2016-04-15 1.680477e-04
2016-04-18 1.175779e-03
2016-04-19 -1.678670e-04
2016-04-20 1.006847e-03
2016-04-21 7.685969e-03
2016-04-22 -1.087789e-02
2016-04-25 -3.708720e-03
2016-04-26 5.557943e-03
2016-04-27 -4.545097e-03
2016-04-28 -1.239298e-02
2016-04-29 6.809662e-03
2016-05-02 1.096781e-02
2016-05-03 -1.175384e-03
2016-05-04 -6.912259e-03
2016-05-05 -1.568663e-02
2016-05-06 4.115955e-03
2016-05-09 4.950945e-03
2016-05-10 8.141157e-03
2016-05-11 -3.769490e-02
2016-05-12 1.704396e-02
2016-05-13 -1.179547e-02
2016-05-16 -2.970767e-03
2016-05-17 -7.002977e-04
2016-05-18 -1.731184e-02
2016-05-19 1.626056e-02
2016-05-20 -9.866200e-03
2016-05-23 -8.713454e-03
2016-05-24 1.065915e-02
2016-05-25 -1.065915e-02
2016-05-26 -2.682681e-03
2016-05-27 6.248329e-03
2016-05-31 -1.741356e-02
2016-06-01 -5.265577e-03
2016-06-02 -7.308657e-03
2016-06-03 -2.000433e-02
2016-06-06 1.577755e-02
2016-06-07 -1.390843e-02
2016-06-08 1.058801e-02
2016-06-09 1.503788e-02
2016-06-10 3.089544e-03
2016-06-13 -3.635725e-03
2016-06-14 -1.449170e-02
2016-06-15 3.504606e-03
2016-06-16 -1.558762e-02
2016-06-17 4.478418e-03
2016-06-20 1.202942e-02
2016-06-21 7.513991e-03
2016-06-22 -3.658318e-03
2016-06-23 -8.280496e-03
2016-06-24 -2.867780e-02
2016-06-27 -1.339991e-02
2016-06-28 2.286251e-02
2016-06-29 3.770546e-02
2016-06-30 1.268921e-03
2016-07-01 7.400088e-03
2016-07-05 -7.400088e-03
2016-07-06 1.081088e-02
2016-07-07 2.148211e-03
2016-07-08 1.420484e-02
2016-07-11 1.937491e-03
2016-07-12 2.141259e-02
2016-07-13 -1.206359e-03
2016-07-14 8.585217e-03
2016-07-15 -1.065674e-02
2016-07-18 -2.248889e-03
2016-07-19 3.462511e-04
2016-07-20 0.000000e+00
2016-07-21 -1.342061e-02
2016-07-22 -4.572678e-03
2016-07-25 7.201208e-03
2016-07-26 -1.356729e-02
2016-07-27 -1.034242e-02
2016-07-28 -6.112928e-03
2016-07-29 9.012889e-04
2016-08-01 -1.622938e-03
2016-08-02 -8.882538e-03
2016-08-03 -3.100176e-03
2016-08-04 7.303451e-04
2016-08-05 1.916178e-02
2016-08-08 2.324994e-03
2016-08-09 -3.758394e-03
2016-08-10 -1.154204e-02
2016-08-11 2.860912e-02
2016-08-12 -4.947863e-03
2016-08-15 5.652709e-03
2016-08-16 1.935786e-03
2016-08-17 -3.516793e-04
2016-08-18 6.136567e-03
2016-08-19 2.911243e-02
2016-08-22 -4.083061e-03
2016-08-23 1.623301e-02
2016-08-24 1.001347e-02
2016-08-25 -1.640752e-02
2016-08-26 -4.059579e-03
2016-08-29 -6.290916e-03
2016-08-30 -1.080352e-02
2016-08-31 -6.226257e-03
2016-09-01 1.549354e-02
2016-09-02 -8.922520e-03
2016-09-06 -1.056927e-02
2016-09-07 5.385247e-03
2016-09-08 -2.722098e-02
2016-09-09 -1.506748e-02
2016-09-12 1.221492e-02
2016-09-13 -1.149230e-02
2016-09-14 -4.343862e-03
2016-09-15 6.148301e-03
2016-09-16 -5.241783e-03
2016-09-19 -4.176870e-03
2016-09-20 -1.456966e-03
2016-09-21 8.529242e-03
2016-09-22 1.264109e-03
2016-09-23 -4.703301e-03
2016-09-26 -1.369259e-02
2016-09-27 1.713178e-02
2016-09-28 -3.849817e-02
2016-09-29 -2.068189e-02
2016-09-30 9.350359e-03
2016-10-03 3.797190e-04
2016-10-04 -1.011361e-02
2016-10-05 -3.836563e-04
2016-10-06 -1.728277e-03
2016-10-07 -4.623356e-03
2016-10-10 0.000000e+00
2016-10-11 5.790754e-04
2016-10-12 1.189346e-02
2016-10-13 -7.849184e-03
2016-10-14 -7.911281e-03
2016-10-17 -1.149550e-02
2016-10-18 3.716425e-03
2016-10-19 1.126003e-02
2016-10-20 1.735944e-03
2016-10-21 -2.315243e-03
2016-10-24 1.929738e-03
2016-10-25 -1.593504e-02
2016-10-26 1.786112e-02
2016-10-27 -1.540574e-03
2016-10-28 2.502186e-03
2016-10-31 -3.601172e-02
2016-11-01 -1.122258e-02
2016-11-02 2.013329e-03
2016-11-03 2.010860e-04
2016-11-04 4.614293e-03
2016-11-07 1.883670e-02
2016-11-08 3.333702e-03
2016-11-09 7.827202e-04
2016-11-10 -1.438307e-02
2016-11-11 7.512906e-03
2016-11-14 -1.109144e-02
2016-11-15 -1.594539e-03
2016-11-16 1.091170e-02
2016-11-17 1.779650e-02
2016-11-18 -9.543397e-03
2016-11-21 3.516335e-03
2016-11-22 8.350374e-03
2016-11-23 -7.180992e-03
2016-11-25 3.499906e-03
2016-11-28 -9.948429e-03
2016-11-29 -7.477347e-03
2016-11-30 -1.112228e-02
2016-12-01 1.151724e-02
2016-12-02 -3.758347e-03
2016-12-05 2.717397e-02
2016-12-06 -2.499638e-02
2016-12-07 2.980640e-02
2016-12-08 -1.061272e-02
2016-12-09 3.292382e-03
2016-12-12 -3.486349e-03
2016-12-13 1.463813e-02
2016-12-14 -9.799252e-03
2016-12-15 -9.701279e-03
2016-12-16 -7.240086e-03
2016-12-19 -1.375651e-03
2016-12-20 1.831702e-02
2016-12-21 9.799252e-03
2016-12-22 -3.063963e-03
2016-12-23 -4.420939e-03
2016-12-27 -1.201563e-02
2016-12-28 -5.278108e-03
2016-12-29 7.837187e-04
2016-12-30 -4.514661e-03
2017-01-03 2.237226e-02
2017-01-04 2.075277e-02
2017-01-05 -1.884293e-04
2017-01-06 1.589262e-02
2017-01-09 -9.879827e-03
2017-01-10 -5.070909e-03
2017-01-11 -7.939588e-03
2017-01-12 -5.519036e-03
2017-01-13 9.874672e-03
2017-01-17 1.370020e-02
2017-01-18 -7.108185e-03
2017-01-19 -6.403035e-03
2017-01-20 5.088129e-03
2017-01-23 7.516159e-04
2017-01-24 3.936625e-03
2017-01-25 7.641450e-03
2017-01-26 -3.906600e-03
2017-01-27 -8.611116e-03
2017-01-30 -2.070143e-03
2017-01-31 -3.396870e-03
2017-02-01 2.265824e-03
2017-02-02 -4.158029e-03
2017-02-03 -8.368212e-03
2017-02-06 8.368212e-03
2017-02-07 1.894139e-04
2017-02-08 2.005878e-02
2017-02-09 2.619437e-02
2017-02-10 1.631884e-02
2017-02-13 -2.315040e-03
2017-02-14 6.397765e-03
2017-02-15 3.360123e-03
2017-02-16 -6.198514e-03
2017-02-17 8.138739e-03
2017-02-21 1.069156e-02
2017-02-22 1.522522e-02
2017-02-23 -1.470238e-02
2017-02-24 8.156262e-03
2017-02-27 -2.942486e-03
2017-02-28 -9.229478e-03
2017-03-01 1.372623e-02
2017-03-02 -2.591829e-03
2017-03-03 -1.921453e-02
2017-03-06 1.233789e-03
2017-03-07 -3.882832e-03
2017-03-08 -7.076066e-04
2017-03-09 -2.657873e-03
2017-03-10 1.241227e-03
2017-03-13 4.244003e-03
2017-03-14 1.070657e-02
2017-03-15 6.612187e-03
2017-03-16 -1.041159e-03
2017-03-17 3.466225e-03
2017-03-20 1.511019e-02
2017-03-21 -1.148358e-02
2017-03-22 -7.311395e-02
2017-03-23 2.653648e-02
2017-03-24 1.772179e-02
2017-03-27 -7.658796e-03
2017-03-28 1.226135e-02
2017-03-29 1.059153e-03
2017-03-30 -1.135568e-02
2017-03-31 -5.547138e-03
2017-04-03 -3.055066e-03
2017-04-04 -9.403324e-03
2017-04-05 -1.090729e-03
2017-04-06 2.905905e-03
2017-04-07 -5.441814e-04
2017-04-10 -3.817821e-03
2017-04-11 3.641595e-04
2017-04-12 1.176594e-02
2017-04-13 -4.147513e-03
2017-04-17 1.613231e-02
2017-04-18 -2.314215e-03
2017-04-19 -4.465489e-03
2017-04-20 9.620616e-03
2017-04-21 -9.799704e-03
2017-04-24 -6.827138e-03
2017-04-25 -3.606203e-04
2017-04-26 -5.243679e-03
2017-04-27 5.604299e-03
2017-04-28 -1.082269e-03
2017-05-01 -7.608696e-03
2017-05-02 1.453716e-03
2017-05-03 -9.854113e-03
2017-05-04 -1.100881e-03
2017-05-05 -9.592400e-03
2017-05-08 6.466498e-03
2017-05-09 1.080696e-02
2017-05-10 -6.030132e-03
2017-05-11 -5.882351e-03
2017-05-12 -7.377536e-04
2017-05-15 -7.965236e-03
2017-05-16 -1.858338e-02
2017-05-17 -1.874218e-02
2017-05-18 -2.319270e-03
2017-05-19 1.739971e-03
2017-05-22 -3.870723e-03
2017-05-23 1.214241e-02
2017-05-24 -3.646545e-03
2017-05-25 6.515930e-03
2017-05-26 4.574088e-03
2017-05-30 6.255314e-03
2017-05-31 1.321953e-03
2017-06-01 -1.310690e-02
2017-06-02 1.291813e-02
2017-06-05 5.660534e-04
2017-06-06 -1.004839e-02
2017-06-07 1.419000e-02
2017-06-08 -5.637320e-04
2017-06-09 4.875277e-03
2017-06-12 1.060574e-02
2017-06-13 5.168961e-03
2017-06-14 6.423790e-03
2017-06-15 -3.272880e-02
2017-06-16 -3.461892e-02
2017-06-19 1.784380e-02
2017-06-20 -8.882063e-03
2017-06-21 1.977979e-02
2017-06-22 -3.619358e-03
2017-06-23 8.551045e-03
2017-06-26 8.103333e-03
2017-06-27 -6.212917e-03
2017-06-28 7.713329e-03
2017-06-29 -3.567130e-03
2017-06-30 1.040432e-01
2017-07-03 -5.949835e-03
2017-07-05 -1.875971e-02
2017-07-06 -6.973546e-03
2017-07-07 1.424377e-02
2017-07-10 1.285255e-02
2017-07-11 -9.409016e-03
2017-07-12 6.873024e-04
2017-07-13 -4.820980e-03
2017-07-14 1.035035e-03
2017-07-17 -3.627243e-03
2017-07-18 -2.425486e-03
2017-07-19 2.079328e-03
2017-07-20 2.276128e-02
2017-07-21 1.428001e-02
2017-07-24 -1.682125e-02
2017-07-25 7.436201e-03
2017-07-26 -1.749510e-02
2017-07-27 -3.604827e-03
2017-07-28 8.902575e-03
2017-07-31 6.456037e-03
2017-08-01 1.328981e-02
2017-08-02 -8.358940e-04
2017-08-03 5.836721e-03
2017-08-04 -6.338654e-03
2017-08-07 5.019323e-04
2017-08-08 -5.534639e-03
2017-08-09 7.372738e-03
2017-08-10 -1.395336e-02
2017-08-11 -1.694324e-03
2017-08-14 1.364228e-02
2017-08-15 -2.061925e-02
2017-08-16 -3.415884e-04
2017-08-17 -1.862126e-02
2017-08-18 -4.466534e-02
2017-08-21 -2.468806e-02
2017-08-22 9.652943e-03
2017-08-23 -9.652943e-03
2017-08-24 4.281049e-03
2017-08-25 1.113830e-03
2017-08-28 -3.159010e-03
2017-08-29 -1.878695e-02
2017-08-30 -3.229160e-03
2017-08-31 4.745192e-03
2017-09-01 1.036083e-02
2017-09-05 -6.580883e-03
2017-09-06 -4.727247e-03
2017-09-07 -7.037576e-03
2017-09-08 -3.633200e-03
2017-09-11 1.577526e-02
2017-09-12 6.953011e-03
2017-09-13 2.244632e-03
2017-09-14 3.916089e-03
2017-09-15 2.602213e-03
2017-09-18 -6.892064e-03
2017-09-19 -3.182592e-03
2017-09-20 4.303477e-03
2017-09-21 -6.932150e-03
2017-09-22 9.396411e-04
2017-09-25 -1.878839e-04
2017-09-26 8.790873e-03
2017-09-27 -1.936702e-02
2017-09-28 -7.596772e-04
2017-09-29 -1.493142e-02
2017-10-02 3.856730e-04
2017-10-03 -7.741436e-03
2017-10-04 1.178190e-02
2017-10-05 1.918243e-03
2017-10-06 4.588880e-03
2017-10-09 -1.731808e-02
2017-10-10 1.940611e-04
2017-10-11 -9.750467e-03
2017-10-12 -3.926905e-03
2017-10-13 2.946628e-03
2017-10-16 7.620926e-03
2017-10-17 1.218939e-02
2017-10-18 5.752633e-03
2017-10-19 7.429313e-03
2017-10-20 6.997702e-03
2017-10-23 1.124448e-02
2017-10-24 -4.482675e-03
2017-10-25 2.805649e-02
2017-10-26 3.347072e-02
2017-10-27 -1.507525e-02
2017-10-30 -1.240687e-02
2017-10-31 -5.078879e-03
2017-11-01 1.453716e-03
2017-11-02 9.075053e-04
2017-11-03 1.064704e-02
2017-11-06 5.906093e-03
2017-11-07 -1.600896e-02
2017-11-08 1.099995e-02
2017-11-09 6.613716e-03
2017-11-10 -7.129032e-04
2017-11-13 -3.214288e-03
2017-11-14 1.251229e-03
2017-11-15 1.154441e-02
2017-11-16 1.053934e-02
2017-11-17 3.367436e-02
2017-11-20 1.013188e-03
2017-11-21 2.360065e-03
2017-11-22 -5.402664e-03
2017-11-24 4.223336e-03
2017-11-27 5.212303e-03
2017-11-28 -8.388391e-04
2017-11-29 1.300667e-02
2017-11-30 9.934924e-04
2017-12-01 -8.977567e-03
2017-12-04 3.667232e-03
2017-12-05 5.310335e-03
2017-12-06 -1.165315e-02
2017-12-07 1.462787e-02
2017-12-08 1.148497e-02
2017-12-11 9.901891e-03
2017-12-12 4.190819e-03
2017-12-13 3.368714e-02
2017-12-14 3.570539e-03
2017-12-15 4.021069e-03
2017-12-18 3.085957e-04
2017-12-19 -8.833843e-03
2017-12-20 -1.016981e-02
2017-12-21 1.838626e-02
2017-12-22 -2.311513e-02
2017-12-26 5.672002e-03
2017-12-27 -1.105858e-02
2017-12-28 0.000000e+00
2017-12-29 -6.374555e-03
2018-01-02 1.491622e-02
2018-01-03 -1.575490e-04
2018-01-04 -6.303341e-04
2018-01-05 8.475973e-03
2018-01-08 8.869629e-03
2018-01-09 -7.151881e-03
2018-01-10 2.026421e-03
2018-01-11 1.089409e-03
2018-01-12 5.893271e-03
2018-01-16 -1.951815e-02
2018-01-17 6.130696e-03
2018-01-18 4.690440e-03
2018-01-19 4.722166e-02
2018-01-22 -1.227560e-02
2018-01-23 1.123355e-02
2018-01-24 1.272773e-02
2018-01-25 -4.273840e-03
2018-01-26 4.861917e-03
2018-01-29 -6.783671e-03
2018-01-30 -3.706179e-03
2018-01-31 1.313186e-02
2018-02-01 -8.390407e-03
2018-02-02 -6.376547e-03
2018-02-05 -4.301251e-02
2018-02-06 1.280786e-02
2018-02-07 6.266677e-03
2018-02-08 -4.902629e-02
2018-02-09 4.689085e-02
2018-02-12 7.454283e-03
2018-02-13 -1.668563e-03
2018-02-14 3.123613e-02
2018-02-15 4.844076e-03
2018-02-16 1.464529e-04
2018-02-20 -1.193040e-02
2018-02-21 -6.540755e-03
2018-02-22 1.192339e-03
2018-02-23 1.522695e-02
2018-02-26 2.162479e-02
2018-02-27 -2.353396e-02
2018-02-28 -1.480850e-02
2018-03-01 -1.064875e-02
2018-03-02 -6.504841e-03
2018-03-05 -1.283042e-02
2018-03-06 2.916496e-03
2018-03-07 -1.653697e-02
2018-03-08 1.454238e-02
2018-03-09 1.811178e-02
2018-03-12 7.812494e-03
2018-03-13 -9.775279e-03
2018-03-14 4.532598e-04
2018-03-15 2.866010e-03
2018-03-16 -7.256192e-03
2018-03-19 -3.039130e-03
2018-03-20 1.645202e-02
2018-03-21 -6.759395e-03
2018-03-22 -2.951962e-02
2018-03-23 3.254540e-03
2018-03-26 1.945982e-02
2018-03-27 4.088686e-03
2018-03-28 -1.109343e-02
2018-03-29 1.516559e-02
2018-04-02 -3.554294e-02
2018-04-03 3.944853e-02
2018-04-04 2.546024e-02
2018-04-05 1.695567e-02
2018-04-06 -2.975271e-02
2018-04-09 -5.492525e-03
2018-04-10 -2.682965e-03
2018-04-11 -2.540508e-03
2018-04-12 1.396746e-02
2018-04-13 -7.702556e-03
2018-04-16 -2.829307e-03
2018-04-17 6.688053e-03
2018-04-18 -1.959535e-02
2018-04-19 -7.124930e-03
2018-04-20 5.461901e-03
2018-04-23 1.188253e-02
2018-04-24 1.344849e-03
2018-04-25 -4.489726e-03
2018-04-26 2.048773e-02
2018-04-27 2.194689e-02
2018-04-30 -1.696306e-02
2018-05-01 -4.249417e-03
2018-05-02 2.346789e-03
2018-05-03 -2.012498e-02
2018-05-04 1.777819e-02
2018-05-07 1.804470e-02
2018-05-08 -1.277226e-02
2018-05-09 -7.477522e-03
2018-05-10 -4.415839e-04
2018-05-11 7.480812e-03
2018-05-14 5.973588e-03
2018-05-15 9.541839e-03
2018-05-16 2.613037e-02
2018-05-17 -5.622646e-03
2018-05-18 5.342315e-03
2018-05-21 8.408830e-04
2018-05-22 -9.811340e-04
2018-05-23 4.205819e-04
2018-05-24 1.170588e-02
2018-05-25 9.693278e-04
2018-05-29 -1.857987e-02
2018-05-30 1.830306e-02
2018-05-31 -5.970996e-03
2018-06-01 1.328186e-02
2018-06-04 1.459880e-02
2018-06-05 2.975401e-03
2018-06-06 9.408631e-03
2018-06-07 1.337971e-04
2018-06-08 1.870908e-03
2018-06-11 -4.147521e-03
2018-06-12 -4.030030e-03
2018-06-13 -2.425877e-03
2018-06-14 7.929563e-03
2018-06-15 1.514575e-02
2018-06-18 -3.037242e-03
2018-06-19 -1.801609e-02
2018-06-20 6.175332e-03
2018-06-21 -1.049383e-02
2018-06-22 -6.921409e-03
2018-06-25 -1.481715e-02
2018-06-26 2.898353e-03
2018-06-27 -1.681646e-02
2018-06-28 4.893390e-03
2018-06-29 1.055279e-01
2018-07-02 -1.683267e-02
2018-07-03 -2.677518e-02
2018-07-05 3.533394e-03
2018-07-06 -9.148533e-04
2018-07-09 1.040587e-02
2018-07-10 3.745578e-03
2018-07-11 -2.710890e-03
2018-07-12 1.292833e-04
2018-07-13 1.291632e-04
2018-07-16 4.770241e-03
2018-07-17 -3.607774e-03
2018-07-18 -1.142431e-02
2018-07-19 4.689354e-03
2018-07-20 1.299721e-04
2018-07-23 -4.819326e-03
2018-07-24 -1.393655e-02
2018-07-25 2.135132e-02
2018-07-26 1.274884e-02
2018-07-27 -1.625427e-02
2018-07-30 -1.216894e-02
2018-07-31 1.242909e-02
2018-08-01 8.157986e-03
2018-08-02 1.421371e-02
2018-08-03 1.143605e-03
2018-08-06 9.731565e-03
2018-08-07 1.274695e-02
2018-08-08 -3.725890e-04
2018-08-09 9.396731e-03
2018-08-10 -6.543625e-03
2018-08-13 -7.210387e-03
2018-08-14 -1.248113e-04
2018-08-15 -7.137955e-03
2018-08-16 6.014340e-03
2018-08-17 -3.754735e-03
2018-08-20 3.001522e-02
2018-08-21 9.326017e-03
2018-08-22 -3.744168e-03
2018-08-23 3.261918e-03
2018-08-24 -5.563718e-03
2018-08-27 2.422836e-03
2018-08-28 -8.473038e-04
2018-08-29 2.539749e-03
2018-08-30 -1.693200e-02
2018-08-31 9.779968e-03
2018-09-04 -3.214120e-02
2018-09-05 4.012042e-03
2018-09-06 5.988092e-03
2018-09-07 -1.244543e-03
2018-09-10 2.216833e-02
2018-09-11 6.434782e-03
2018-09-12 4.467833e-03
2018-09-13 5.646690e-03
2018-09-14 2.395424e-04
2018-09-17 -2.758574e-03
2018-09-18 2.373717e-02
2018-09-19 -9.782646e-03
2018-09-20 1.107200e-02
2018-09-21 2.106249e-03
2018-09-24 -1.507514e-02
2018-09-25 6.151729e-03
2018-09-26 -1.293868e-02
2018-09-27 9.985865e-03
2018-09-28 2.126906e-03
2018-10-01 -3.073675e-03
2018-10-02 -2.021240e-02
2018-10-03 -3.267347e-03
2018-10-04 -2.852419e-02
2018-10-05 -7.485590e-04
2018-10-08 1.371999e-03
2018-10-09 2.365329e-03
2018-10-10 -7.057506e-02
2018-10-11 -5.754449e-03
2018-10-12 1.861511e-02
2018-10-15 -1.219374e-02
2018-10-16 3.266513e-02
2018-10-17 -1.299057e-02
2018-10-18 -1.157305e-02
2018-10-19 -1.855736e-02
2018-10-22 9.255019e-03
2018-10-23 -2.091144e-02
2018-10-24 -1.691104e-02
2018-10-25 1.349690e-02
2018-10-26 -1.419047e-02
2018-10-29 1.663691e-03
2018-10-30 3.163181e-02
2018-10-31 7.087947e-03
2018-11-01 2.305312e-02
2018-11-02 -2.738464e-03
2018-11-05 1.232897e-02
2018-11-06 -1.245959e-02
2018-11-07 1.811880e-02
2018-11-08 -2.439834e-03
2018-11-09 -1.842530e-02
2018-11-12 -1.663848e-02
2018-11-13 1.330659e-03
2018-11-14 0.000000e+00
2018-11-15 -1.163653e-02
2018-11-16 5.500732e-03
2018-11-19 -3.015305e-02
2018-11-20 -1.949371e-02
2018-11-21 1.742326e-02
2018-11-23 -1.223434e-02
2018-11-26 1.692136e-02
2018-11-27 -8.563630e-03
2018-11-28 3.502924e-02
2018-11-29 -4.295416e-03
2018-11-30 1.043776e-02
2018-12-03 3.685245e-02
2018-12-04 -2.797295e-02
2018-12-06 -3.304040e-03
2018-12-07 -2.955622e-02
2018-12-10 -1.138160e-02
2018-12-11 1.451282e-02
2018-12-12 1.014276e-02
2018-12-13 -1.888002e-02
2018-12-14 -5.499821e-03
2018-12-17 -3.693458e-02
2018-12-18 1.772467e-02
2018-12-19 -3.111871e-02
2018-12-20 -2.109971e-02
[ reached getOption("max.print") -- omitted 30 rows ]
NikeSd
[1] 0.01509771
NikeMean
[1] 0.0005285341
Given a time series we can generate a histogram and mark the quantile value that corresponds to the 95% confidence level. The quantile value in this case is the critical value for which 95% of the data is on the right (higher) of the critical value as represented by the histogram. The remaining 5% will be on the left. To find the quantile value you will need to use the function quantile() with the proper arguments. For example quantile(xts,probs=0.01) applies to a time series xts and return the critical value correspondng to 99% confidence level.
##### 1B) Plot a histogram and mark the quantile value that corresponds to 95% confidence level. A mark can be added to a plot by using the function abline().
NikeLine=quantile(NikeLog,probs=.05)
hist(NikeLog,breaks="FD",xlab='Log Returns',main='Nike Log Returns 2015 - Present')
abline(v=NikeLine)
Given the above, we can calculate a VaR value for a given investment and a time horizon.
##### 1C) Assume $1000 investment in the stock, calculate the 95% VaR for 3 days’ time horizon. Explain the meaning of VaR in practical terms.
1000*(NikeMean-1.645*NikeSd*sqrt(3))
[1] -42.50965
The VaR is the maximum amount of money that we can expect to lose over a certain time period that will only be exceeded a certain percent of the time. It is expected that a 5% VaR will be exceeded 5% of the time.
##### 1D) How does the VaR calculated in 1C changes if we assumed simple returns instead of the log returns.
NikeSimple = periodReturn(NKE, period = 'daily', type = 'arithmetic', subset='2015::')
NikeSimpleSd=sd(NikeSimple)
NikeSimpleMean=mean(NikeSimple)
1000*(NikeSimpleMean-1.645*NikeSimpleSd*sqrt(3))
[1] -42.65342
There is little change between the two since the log and simple returns are similar at low values.
Calculations so far have been on the histogram of log returns. We can instead, looking at the histogram, assume a normal distribution of the log returns. The normal return assumption will have a mean and standard deviation equal to those calculated from the histogram, and properly scaled by the horizon time. Note that the mean is linearly scaled with time and volatility with square-root of time.
##### 1E) Repeat the calculation in 1D assuming instead a normal distribution. How the two results compare and why so?
NikeQnorm=qnorm(0.05, mean=NikeSimpleMean, sd =NikeSimpleSd) # calculates the 5% quantile for a normal distribution with given mean and standard deviation
1000*NikeQnorm*sqrt(3)
[1] -42.18596
hist(NikeSimple,breaks="FD")
The results are similar because the simple returns follow a near normal distribution.
##### 1F) Write down the mathematical representation corresponding to the VaR calculation in 1E. Explain what each variable in the equation represents, and provide the corresponding numerical value.
\(VaR_{.95} = V*(\mu - Z_{.95}*\sigma)\) V is the investment amount 1000. \(\mu\) is the mean of the simple returns 0.0006332948. \(\sigma\) is the standard deviation of the simple returns 0.01519246. \(Z_{.95}\) is the 95th percent confidence level -1.645.
Consider the times series of the three stocks DIS, TWTR, and NFLX for the time-period from Jan 1,2015 to present. Assume an investment of $100,000 equally distributed among all three stocks.
To calculate the portfolio VaR we will follow the methodolgy described by the variance-covariance. First the covariance matrix needs to be computed. We then calculate the variance or volatility of the portolio as expressed in the varaince-covariance method taking into the weights associated with each asset in the portfolio. Fiinally we compute the mean or expected return of the portfolio taking also into account the weights. Given the expected return and volatility we should be able to compute the VaR of the portfolio. The assumption is we have a normal distribution of log returns.
First we calculate the overall portfolio VaR.
##### 2A) Calculate the portfolio 99% VaR for 1 day, and 3 days. Specify whether using log-returns or simple returns.
symbols=c('DIS','TWTR','NFLX')
getSymbols(symbols,src="yahoo")
[1] "DIS" "TWTR" "NFLX"
disRd = periodReturn(DIS, period = 'daily', type = 'log', subset='2015::')
twtrRd = periodReturn(TWTR, period = 'daily', type = 'log', subset='2015::')
nflxRd = periodReturn(NFLX, period = 'daily', type = 'log', subset='2015::')
dismean=mean(disRd)
dissd=sd(disRd)
twtrmean=mean(twtrRd)
twtrsd=sd(twtrRd)
nflxmean=-mean(nflxRd)
nflxsd=sd(nflxRd)
M = cbind(disRd,twtrRd,nflxRd) # create a matrix for three time series of returns
cor(M) # correlation matrix
daily.returns daily.returns.1
daily.returns 1.0000000 0.2132424
daily.returns.1 0.2132424 1.0000000
daily.returns.2 0.2860884 0.2683348
daily.returns.2
daily.returns 0.2860884
daily.returns.1 0.2683348
daily.returns.2 1.0000000
cov(M) # covariance matrix
daily.returns daily.returns.1
daily.returns 1.459938e-04 8.651425e-05
daily.returns.1 8.651425e-05 1.127441e-03
daily.returns.2 9.361749e-05 2.440134e-04
daily.returns.2
daily.returns 9.361749e-05
daily.returns.1 2.440134e-04
daily.returns.2 7.334650e-04
w = rep(1/3, 3) # creates three equal weights of 1/3 each
var_p = w %*% cov(M) %*% w # Calculates variance of portfolio.
mu_p = colMeans(M) %*% w # Calculates mean of portfolio
portfolioVar1=qnorm(.01,mean=mu_p,sd=(sqrt(var_p)))*100000
portfolioVar3=qnorm(.01,mean=mu_p,sd=(sqrt(var_p)))*100000*sqrt(3)
print(portfolioVar1)
[1] -4074.829
print(portfolioVar3)
[1] -7057.811
Log returns were used.
We then describe the mathetical formulation useds in the code to perform the calculations
##### 2B) Write down the mathematical representations used in the code to calculate the VaR. Label properly each variable, what it represents, and the numerical value corresponding to in the code.
\(VaR_{.99} = V*Z_{.99}*\sigma_\rho*\sqrt{t}\) V is the investment amount 100000. \(\sigma\) is the standard deviation of the simple returns 0.01781133 \(Z_{.99}\) is the 95th percent confidence level -2.326.
We now calculated individual VaR corresponding to each asst in the portfolio
##### 2C) Calculate the three-individual asset 99% VaR for 1 day.
disVar=qnorm(.01,mean=dismean,sd=dissd)*100000/3
nflxVar=qnorm(.01,mean=nflxmean,sd=nflxsd)*100000/3
twtrVar=qnorm(.01,mean=twtrmean,sd=twtrsd)*100000/3
print(disVar)
[1] -931.6871
print(nflxVar)
[1] -2164.922
print(twtrVar)
[1] -2606.635
Finally we compare the overall VaR to the individual VaR calculations
##### 2D) Compare the overall portfolio VaR to the sum of the individual asset VaR for the 1 day case. Share observations.
sum(disVar,nflxVar,twtrVar)
[1] -5703.244
print(portfolioVar1)
[1] -4077.172
As expected, the VaR for the portfolio is much lower than the sum of the individual stocks due to diversification. The correlation matrix shows that none of these stocks are perfectly correlated with each other, so \(VaR_p = \sqrt{{VaR_1}^2+{VaR_2}^2+{VaR_3}^2-3_{\rho12}Var_1Var_2Var_3}\) *http://computationalfinance.lsi.upc.edu