Search code examples
rtime-seriesstockstandard-deviationvolatility

Stocks - Calculating Volatility of a Time Series


I have this simple dataset of a stock price, where Col1 is for Dates, Col2 is for Returns (Close Price D / Close Price D-1, the same as the pct_change in Python) and Col3 for the Ewma Volatility. I'm working with projections and were ploting the Returns vs Ewma Volatility, but reading some articles I realized that I need to plot the Daily Volatility x Ewma Volatility so I can see the predictions more clearly

enter image description here

So I wanted to see a graph like these, but in Red I have Returns and instead of it I need the red line to be Volatility calculated by normal methods (simple Vol). In black is the EwmaVol calculated by the MTS::EWMAvol package.

Here's the data:

structure(list(date = structure(c(18515, 18516, 18520, 18521, 
18522, 18523, 18526, 18527, 18528, 18529, 18530, 18533, 18534, 
18535, 18536, 18537, 18540, 18541, 18542, 18543, 18544, 18547, 
18548, 18549, 18550, 18551, 18554, 18555, 18556, 18557, 18558, 
18561, 18562, 18563, 18564, 18565, 18568, 18569, 18570, 18571, 
18572, 18575, 18576, 18577, 18578, 18579, 18582, 18583, 18584, 
18585, 18586, 18589, 18590, 18591, 18592, 18593, 18596, 18597, 
18598, 18599, 18600, 18603, 18604, 18605, 18606, 18607, 18610, 
18611, 18612, 18613, 18614, 18617, 18618, 18619, 18620, 18621, 
18624, 18625, 18626, 18627, 18628, 18631, 18632, 18633, 18634, 
18635, 18638, 18639, 18640, 18641, 18642, 18645, 18646, 18647, 
18648, 18649, 18652, 18653, 18654, 18655, 18656, 18659, 18660, 
18661, 18662, 18663, 18666, 18667, 18668, 18669, 18670, 18673, 
18674, 18675, 18676, 18677, 18680, 18681, 18682, 18683, 18684, 
18687, 18688, 18689, 18690, 18691, 18694, 18695, 18696, 18697, 
18698, 18701, 18702, 18703, 18704, 18705, 18708, 18709, 18710, 
18711, 18712, 18715, 18716, 18717, 18718, 18719, 18722, 18723, 
18724, 18725, 18726, 18729, 18730, 18731, 18732, 18733, 18736, 
18737, 18738, 18739, 18740, 18743, 18744, 18745, 18746, 18747, 
18750, 18751, 18752, 18753, 18754, 18757, 18758, 18759, 18760, 
18761, 18764, 18765, 18766, 18767, 18768, 18771, 18772, 18773, 
18774, 18775, 18778, 18779, 18780, 18781, 18782, 18785, 18786, 
18787, 18788, 18789, 18792, 18793, 18794, 18795, 18796, 18799, 
18800, 18801, 18802, 18803, 18806, 18807, 18808, 18809, 18810, 
18813, 18814, 18815, 18816, 18817, 18820, 18821, 18822, 18823, 
18824, 18827, 18828, 18829, 18830, 18831, 18834, 18835, 18836, 
18837, 18838, 18841, 18842, 18843, 18844, 18845, 18848, 18849, 
18850, 18851, 18852, 18855, 18856, 18857, 18858, 18859, 18862, 
18863, 18864, 18865, 18866, 18869, 18870, 18871, 18872, 18873, 
18876, 18877, 18878, 18879, 18880, 18883, 18884), class = "Date"), 
    Retorno = c(-0.675993561014219, 0.00727122549799243, 0.0200646939724594, 
    -0.309572944244751, -0.240552943912361, -0.22033250472985, 
    -0.361838491629178, 0.425326793664922, -0.467655896347618, 
    0.184647131258419, 0.289993731622618, -0.247917340719569, 
    -0.266792070525439, 0.408121976560422, 0.432634049112388, 
    -0.526936711212769, 0.255443464814248, -0.277205195648874, 
    0.237634551321446, 0.416255267136204, 0.182381045120273, 
    0, 0.559328565933268, 0.22352526750161, 0.0344661931237814, 
    -0.0114765743015762, -0.340659866250595, 0.518121135418651, 
    0.124920612225829, 0.060477879624518, -0.132268000351469, 
    -0.419044186141948, -0.0468465628669677, -1.24962920487573, 
    0.519314480623082, -0.83241356610111, 0, 0.677072797328314, 
    0.770362872443658, 0.779540076578319, -0.128308411797546, 
    0.208939823146876, -0.313737425389519, 0.218322415089412, 
    -0.69034693281341, 0.812758658841942, 0.167317931481811, 
    -0.208730841632264, -0.387374906435024, 0.145599821661092, 
    0.107177528022895, 0.381307385634918, 0.386256536259558, 
    0.160935467100042, 0.0370218344118541, 0.241511701069377, 
    -0.33376949245172, 0.162429122410115, 0.189409487756105, 
    -0.136073192422632, 0.221928418309387, -0.294157383734931, 
    0.293123696475209, -0.308081743475511, -0.0160523988860364, 
    0.200830763685225, 0.215786923768921, 0.467113206724325, 
    0.405886001764272, 0.105544246268935, -0.128010524950925, 
    -0.195222854432975, 0.202533534363647, 0.36037155474833, 
    0.0195551377992495, 0, 0.386574054270276, -0.0520687967679869, 
    0.134723231050616, -0.0105062746232602, 0, -0.0867476700402434, 
    0.0959051831633806, -0.792631919080304, 0.714335671518634, 
    1.00993938236837, -0.129261214344111, 0.124684691358913, 
    -0.183903752322858, -0.0658608900095052, -0.160979643574251, 
    0.19290543725798, 0.251171599859635, 0.230012814573936, 0.00867119071736477, 
    -0.00849157305596641, -0.0662645178280147, -0.325684851940853, 
    -0.63526581976692, 0.987392935674623, -0.71566845639813, 
    0.823448032203283, 0.364849774654681, 0.493861329996562, 
    0.429481174502912, 0.392845558392575, 0.18687659266044, -0.0496962834358631, 
    -0.351476870541759, 0.325056528852312, 0.0540948459475099, 
    0, 0, 0.298660972132666, -0.129176783491061, 0.0936264785031788, 
    -0.717102311834032, 0.0579970685670885, 0.2387869198789, 
    -0.800077283233545, -0.346606771876278, 0.652393650772446, 
    0.019543204729203, -0.186097235985271, -0.401338954104801, 
    0.770468206437961, -1.16143407489768, 0.287520645270323, 
    -0.180982644002469, 0.636164450379635, -0.167036272711984, 
    0.273933259257009, 0.0018679680615165, 0.401511529829268, 
    -1.0714528798559, 0.225507468509173, -0.0998873705026633, 
    -0.357468921258203, -0.453208884721668, 0.584325924664598, 
    0.294490804371817, -0.071870245489955, 0.227731039838296, 
    0.0272554279847483, 0.282891030886757, 0, 0.444572787367717, 
    0.107513540207911, 0.204070787509094, 0.290177231896655, 
    0.314121770459557, 0.294143400791366, 0.0423074542633505, 
    -0.111752005968369, 0.191432961182637, 0.107012720814761, 
    -0.221435538762791, -0.287455618822024, 0, -0.504458868407395, 
    0.576189122256037, 0.0513721502072407, -0.0878347180967137, 
    0.0305926251264597, 0.0409984249091759, -0.0507344439032554, 
    0.127910054528995, -0.33110581208469, 0.244321197674869, 
    -0.153002648530627, 0.550660961815173, -0.39137091346195, 
    0.00655796886117349, -1.07175015625675, 0.47234018769347, 
    0.457477961736975, 0.194987471696463, -0.192289243894243, 
    -0.100966714379365, 0.295535636777222, 0.0497612954361652, 
    0.424646083712068, -0.0235816065166123, 0.148925361065251, 
    0.0833499347084376, 0.162437322657595, 0.216822541199244, 
    -0.0266349635355871, -0.0203718557462801, 0, 0.163246069746882, 
    0.0540313182825264, -0.132667149729679, -0.0106752473427232, 
    0.0966877877911841, -0.0135235065561488, 0.286749500028446, 
    -0.123925251554195, -0.0750149494882748, -0.00131099907860658, 
    0.0788369651503004, 0.310420308737175, 0.0738549668615309, 
    -0.0388124917441666, 0.156992583882095, -0.158341400377075, 
    0.138832514569506, 0.0484161074075555, 0.0644817162954546, 
    -0.075831329755838, 0.594706467883038, 0.0582157771824145, 
    -0.224095914091399, 0.433246293201151, -0.340500528636388, 
    0.0408415756500143, 0.45961172993559, 0.136168616599819, 
    0.0264246421863121, -0.0041223988663561, -0.273325208808114, 
    -0.429764244359346, 0.318952765660581, 0.152571137442228, 
    0.378021240701817, -0.0502434439840156, -0.284587147638002, 
    -0.549313963668678, 0.024940657477001, 0.106379612006064, 
    -0.666213312763818, 0.101187879104207, 0.212900264239358, 
    -0.319050618049279, 0.151814926005171, 0.165719848192862, 
    0.111396078192044, -0.192914594549868, -0.0785555116791173, 
    -0.0633138947667031, -0.100685336741663, -0.70572669007387, 
    -0.636169672803793, -0.0256706550272056, 0.568449636874711, 
    0.431844471002692, -0.0884432149109783, 0.507974104651909, 
    0.253656436799725, -0.551492331609423, 0.473346006073304, 
    -0.230628440542497, -0.36475396210053, 0.0553706780422077, 
    -0.458282184750842, 0.127876503287846, 0.329153343062354, 
    0, -0.640854284349025, 0.314530520275267, -0.547495384943616, 
    0.532252306040703, 0), EwmaVol = c(0.401545880470543, 
    0.37957402999614, 0.357002237552554, 0.336710024448518, 0.318760036517056, 
    0.316732889747995, 0.306462909109581, 0.288335641955465, 
    0.285974951159514, 0.27897153978688, 0.26226816859379, 0.285059883553795, 
    0.272374799205532, 0.268378720050093, 0.262349047774286, 
    0.258978405250914, 0.253148786729867, 0.238963348264941, 
    0.225541112253008, 0.234992747615912, 0.222414110918961, 
    0.209069264263823, 0.206230071488549, 0.20121898142488, 0.189404201517459, 
    0.179625218204264, 0.169480509616124, 0.174732838517365, 
    0.164762013463212, 0.156538802813315, 0.153370969102807, 
    0.145432704491324, 0.14287274912088, 0.21547817272594, 0.214028935828116, 
    0.234147750804581, 0.220098885756306, 0.21209598867207, 0.242287370393438, 
    0.261087909529102, 0.248230680408422, 0.243595317635679, 
    0.230164613514584, 0.217002608357163, 0.22967146650814, 0.248916351205143, 
    0.239617766167996, 0.225423319002878, 0.218347622569303, 
    0.206598225427181, 0.196302279540574, 0.185030606442786, 
    0.188266218750961, 0.181245068143354, 0.17041364058184, 0.161095631643361, 
    0.160389804076746, 0.156316917381599, 0.14852488563309, 0.142019675657794, 
    0.133567051476024, 0.125838895726541, 0.11969455573153, 0.119360323208544, 
    0.11750261880258, 0.112148663686316, 0.108243847740347, 0.115163834443589, 
    0.116212022405643, 0.109409013330819, 0.104496900700937, 
    0.10935285581462, 0.105502302791016, 0.101242444242857, 0.09516847810389, 
    0.0894583694176566, 0.0866069786756536, 0.0817380126900134, 
    0.0773367431654428, 0.0726971569867781, 0.0683353275675715, 
    0.0697812759496627, 0.067691476973121, 0.129966833467446, 
    0.126803399533392, 0.171461998066648, 0.171739783002973, 
    0.197564652471901, 0.189022309519642, 0.179950836609547, 
    0.170183058064679, 0.164266188880656, 0.155101252531341, 
    0.148693223428185, 0.14012494879068, 0.133740625026294, 0.125716772617523, 
    0.119105020311106, 0.117086850187538, 0.145347969102275, 
    0.152463434186314, 0.158687203287356, 0.152449871680205, 
    0.166738452722435, 0.158612506047059, 0.160075907173878, 
    0.151254857380356, 0.142343596709454, 0.142934873378322, 
    0.142665594947683, 0.134145123063853, 0.126096415680022, 
    0.118530630739221, 0.112499802745561, 0.110940338230715, 
    0.104545755177033, 0.135780351022354, 0.133312910743497, 
    0.125726130222031, 0.153200898521969, 0.159065047312098, 
    0.155282128215572, 0.146884947547679, 0.138117117632574, 
    0.130622632663405, 0.152186093616364, 0.253518876239024, 
    0.238323024716563, 0.227569746334944, 0.248057416617638, 
    0.234104106541416, 0.221001436475852, 0.208519869481825, 
    0.209685706242652, 0.239728740398097, 0.234167233383272, 
    0.224220956969507, 0.216318345630569, 0.223196193500169, 
    0.224922152734894, 0.212452796555951, 0.200672435018375, 
    0.197518356826799, 0.188183960822706, 0.177785419155766, 
    0.16711829400642, 0.169717068257208, 0.166026638555189, 0.156957479697871, 
    0.153622766010829, 0.14469286668481, 0.138471748094382, 0.131579951121295, 
    0.124018474660646, 0.120182498675561, 0.113234218642543, 
    0.107149115926987, 0.101721766522229, 0.095618460530895, 
    0.0999442416592745, 0.101574073875603, 0.0957296378477016, 
    0.093118066436934, 0.0914320009168463, 0.0871185787307817, 
    0.0823469513367038, 0.0811192489506884, 0.0825047887866796, 
    0.0860049643565581, 0.0808493443261423, 0.0961929240668569, 
    0.0911638648549433, 0.0857640508042613, 0.104905012906323, 
    0.114721311495374, 0.109831409732472, 0.104612358275864, 
    0.0993594406351853, 0.0946255646854852, 0.0895721816070949, 
    0.0845410520311026, 0.0838606211253721, 0.0789729132909391, 
    0.0747186193653219, 0.0705734402792115, 0.0688594831676326, 
    0.0657400666843101, 0.0634447557051325, 0.0633681690044842, 
    0.0595660788642152, 0.0603886782791013, 0.0587833071039431, 
    0.057511124086288, 0.0543325953300019, 0.0512261329106077, 
    0.0491856952988916, 0.0510463463396217, 0.0487190552131411, 
    0.047891041488347, 0.0467019683194386, 0.0466241053525344, 
    0.0456515495748067, 0.0431468387328283, 0.041317889619408, 
    0.0452423873993231, 0.0486383165176941, 0.0479392218342959, 
    0.0454006830162697, 0.0435713468777161, 0.0419873301594818, 
    0.0536531567181674, 0.0506716342092926, 0.0562970018467919, 
    0.0684016291611351, 0.0674546466335945, 0.0634076483792872, 
    0.067163694596286, 0.0640197920338736, 0.061921707762386, 
    0.0589219002148625, 0.0567269880819515, 0.0678661716944311, 
    0.0660541475889322, 0.062251261441256, 0.0624556318584788, 
    0.0635712086121672, 0.0630732335813346, 0.0691297797235716, 
    0.0653096157302932, 0.0636744930080208, 0.0944386419928364, 
    0.0915130579751466, 0.0863295417576338, 0.0867829530305672, 
    0.0839894020970701, 0.0791825005794653, 0.0766098726649444, 
    0.0753903595328056, 0.0733451437270916, 0.0693926680771279, 
    0.0664728433228, 0.0956887810320563, 0.0954558159033218, 
    0.0922581275391543, 0.09091195032168, 0.0929899977659706, 
    0.091341181932747, 0.131126103751573, 0.129655613187944, 
    0.148447176829185, 0.153225134955549, 0.153634505982304, 
    0.160933771494317, 0.151965634321099, 0.167395816107282, 
    0.160837085230127, 0.167419121889831, 0.157373974576441, 
    0.217309648664878, 0.211280213400328, 0.212032193587911, 
    0.22982849323927, 0.216038783644914)), class = "data.frame", row.names = c(NA, 
-263L))

Now, I need to have a column with the daily volatility of the assets.

What's the simpliest way of doing it in R?


Solution

  • Volatility is the (typically annualized) standard deviation of returns over a given period. The exact definition of volatility depends on some conventions (percentage returns vs log returns, day count conventions) which will depend a lot on your context.

    We don't quite know what we're talking about when we talk about volatility

    From your data, it is possible to calculate some metric of volatility by using the zoo function rollapply. Please not that the example here does not conform to most conventions (you'd normally multiply with sqrt(daycount/horizon) to annualize)

    library(zoo)
    library(ggplot2)
    #Define a horizon for volatility calculation, e.g. 20 days
    horizon = 20
    
    #Note: volatility is not annualized!
    data['vols'] = c(rep(NA, horizon-1), rollapply(as.vector(data$Retorno), horizon, FUN = function(x) sd(x)))
    
    #Alternatively, calculate some measure of day-to-day change in return that is not a volatility
    data['not_a_vol'] = abs(data$Retorno - lag(data$Retorno))
    

    You can then plot the results as:

    ggplot() + 
      geom_bar(aes(x=data$date, y=data$Retorno), stat = 'Identity') + 
      geom_line(aes(x = data$date, y= data$vols, color = 'red'), lwd=1.5)
    

    Example plot

    Your analysis seems to be going in the direction of volatility analysis, so you might find GARCH models or exponential moving average of volatility interesting.