.chapter20<-function(i=0){ " i Chapter 20: VaR (Value at Risk) i Explanations - ------------------------------------- -- --------------------------------- 1 What is VaR? 21 How to generate a n-day return (excel) 2 Several simple examples 22 3 Review: standard deviation 23 4 Review: normal distribution 24 5 Review: density vs. cumulative dist 25 6 variance conversion 26 7 left tail 27 8 Confidence level, left tail/probability 28 9 Method #1 to calculate VaR (normality) 29 10 The simplest formula for a short-period 30 11 Method #2: ranking of returns 31 12 Formula to estimate Chi square 32 13 Critical value for a Chi square test 33 14 Decision rule 34 15 Normality tests 35 16 Excel functions for 4 moments 36 17 Modified VaR 37 18 An easy way to download daily/monthly data 38 19 Videos 39 20 Links 40 Example #1:>.c20 # find out the list Example #2:>.c20() # the same as the above Example #3:>.c20(1) # see the first explanation ";.zchapter20(i)} .c20<-.chapter20 .n20chapter<-40 .zchapter20<-function(i){ if(i==0){ print(.c20) }else{ .printEachQ(20,i,.n20chapter) } } .C20EXPLAIN1<-"What is VaR? /////////////////////////// The maximum loss for a given confidence level over a fixed period. /////////////////////////// " .C20EXPLAIN2<-"A few simple examples /////////////////////////// Today, I own 300 shares of IBM stocks worth $2,942. The maximum loss tomorrow is $ 239 with a 99% confidence level. Our mutual fund has a value of $10m today. The maximum loss over next 3-month is 0.5m with 95% confidence. The value of our bank is $350m. The VaR of our bank is $10m with 99% confidence over the next 6 months. /////////////////////////// " .C20EXPLAIN3<-"Review: variance vs. standard deviation /////////////////////////// For a given set of returns, we have the following formulae to estimate variance and standard deviation. Assume that our a set of n returns: R1, R2, ... Rn R1+ R2 + ... + Rn Step 1: mean= ------------------------ n (R1-mean)^2 + (R2-mean)^2 + ... + (Rn-mean)^2 var = ------------------------------------------- n-1 std = sqrt(var) Note : usually, when the IBM's volatility is 0.2 means that its annualized standard deviation of returns is 0.2. /////////////////////////// " .C20EXPLAIN4<-"Review: normal distribution /////////////////////////// The standard normal distribution is defined by the following equation: 1 f(x) = --------- * exp(-0.5 *x^2) sqrt(2*pi) where f(x) is the density function, x is the input pi is 3.1415926 Manually, we could calculate a few values. x=0 -> f(x) = 1/sqrt(2*3.1415925) -> 0.3989423 x=1 -> f(1) = 1/sqrt(2*3.1415926)*exp(-0.5) -> 0.2419707 x=-1 -> f(-1) = 1/sqrt(2*3.1415926)*exp(-0.5*(-1)^2) -> 0.2419707 /////////////////////////// " .C20EXPLAIN5<-"Review: density vs. cumulative distribution /////////////////////////// Cumulative density has a cumulative property. Discrete cases -------------- Case #1: Toss a dice with values from 1 to 6 Each number has 1/6 probability What is the prob. to get a number less than 4? 1/6 + 1/6 + 1/6 = 3/6 =1/2 1/6 is probability of each number, while 1/2 is the cumulative probability (distribution) Continuous cases ---------------- Case #2: uniform distribution: assume density function is 1/a and choose a=5 from 1 to 5, the density is 1/5. total sum is 5*1/5 =1 what does it mean that the density function is 1/5 (0.2)? Take a value between 0 and 5, such as x=1.5 and choose a small number about called deltaX. The probability that a random number falls into this small strip = f(x) * deltaX= 0.2*detlaX /////////////////////////// " .C20EXPLAIN6<-"variance conversion /////////////////////////// we have the following formulae to convert variances and standard deviations based on daily or monthly returns. var(annual) = 252 * var(daily) var(annual) = 12 * var(monthly) std(annual) = sqrt(252) * std(daily) var(annual) = sqrt(12 ) * std(monthly) var(10day) = 10 *var(daily) std(10day) = sqrt(10) std(daily /////////////////////////// " .C20EXPLAIN7<-"Review: left tail /////////////////////////// Since the negative is on the left-hand side, we use the left tail to represent loss or risk. The loss of one 1% is the area of the left tail of a normal distribution (1%). /////////////////////////// " .C20EXPLAIN8<-"Review: Confidence level vs. significant level /////////////////////////// Confidence level = 1 - significance level If significant level =1% -> confidence level is 99% /////////////////////////// " .C20EXPLAIN9<-"Method #1 to calculate VaR (assume a normal distribution) /////////////////////////// VaR = p * (mean - z * std ) (1) VaR : is our var P : is our position, such as value of our portfolio mean : is our period mean return z : depend on confidence level std : is the period standard deviation z=2.326 when the confidence is 99% =1.64 When the confidence is 5% Sign issue ---------- The Excel function for the z value is =Excel(confidence,0,1) VaR = p * (mean - z * std ) (2) z = > qnorm(1-0.01) [1] 2.326348 > qnorm(1-0.05) [1] 1.644854 /////////////////////////// " .C20EXPLAIN10<-"The simplest formula for VaR /////////////////////////// When the time period is very short such as one day VaR = p * z * std VaR : is our value at risk P : is our position, such as value of our portfolio z : depend on confidence level std : is the period standard deviation /////////////////////////// " .C20EXPLAIN11<-"Method #2 to calculate VaR (ranking) /////////////////////////// Step 1: calculate returns Step 2: sort all return from the lowest to highest Step 5: n = # of returns Step 6: VaR will be VaR = ret(k) *Position k=as.integer(n *significanceLevel) /////////////////////////// " .C20EXPLAIN12<-"Formula to estimate Chi square /////////////////////////// Assume that we have three frequency columns # ourFrequency theoryFreq 1 f1 t1 2 f2 t2 3 f3 t3 ... ... ... n fn tn Step 1: estimate the squared percentage deviation [(f1-t1)/t1]^2 Step 2: chi square = summation of above squared percentage deviation /////////////////////////// " .C20EXPLAIN13<-"Critical value of a Chi square test /////////////////////////// =CHISQ.INV.RT(probability,deg_freedom) input #1: probability is the probability #2: deg_freedom is the degree of freedom n-1 For example, =CHISQ.INV.RT(0.05,8) -> 15.50731306 /////////////////////////// " .C20EXPLAIN14<-"Decision rule /////////////////////////// Hypothesis: the returns follow a specific distribution If Chi square > critical value -> Reject the null Chi square < critical value -> Accept the null /////////////////////////// " .C20EXPLAIN15<-"Normality tests /////////////////////////// Issue: we have a column of returns. Do they follow a normal distribution? General distribution for two columns: one for data & one for known distribution Question: do observations presented by our column follow a given distribution? Step 1: generate a return column Step 2: calculate mean and standard deviation Step 3: calculate the adjusted return adjRet=(R-mean)/std Step 4: Estimate max and min and generate a bin column. For example, if max =-0.3 and max=0.4 and we plan to have 20 intervals. we could generate another column called \"bin\" -0.3 ->C1 c1+interval c2+interval .... 0.4 where interval= (max-min)/20 Step 5: click \"Data\",\"Data Analysis\", \"Histogram\" Enter your return column as \"Input Range\" Enter you \"bin\" as \"bin Range\" Step 6: Calculate frequency for each bin Step 7: calculate density function for normaldistribution For example, for -0.3 -> NORMSIN(-0.3) Step 8: calculate squared percentage difference [f1/n1-1)^2 where f1 : frequency for the 1st bin n1 : density for a standard normal distribution Step 9: summation = Chi square Step 10: compare it with the critical value Chi square critical value = CHIQIINV(probability, deg_freedom) e.g., = CHIQIINV(0.05,30) -> 43.77297183 /////////////////////////// " .C20EXPLAIN16<-"Excel functions for 4 moments /////////////////////////// Excel 4 moments 1) the 1st moment: mean -> =average() 2) the 2nd moment: std -> =stdev() 3) the 3rd moment: skewness -> =skew() 4) the 4th moment: kurtosis -> =kurt() /////////////////////////// " .C20EXPLAIN17<-"Modified VaR (non-normal distribution) /////////////////////////// VaR consider the first two moments ---------------------------------- VaR = P * (mean - z *sigma) (1) where P : position mean: mean period return z : =abs(NORMSINV(1-confidence)) sigma: volatility of period returns Note: a sign issue VaR = P * (mean + z *sigma) (2) z : =NORMSINV(1-confidence) mVaR (VaR consider the first 4 moments) ---------------------------------- VaR = P * (mean - tt*sigma) (3) where P : position mean: mean period return z : =abs(NORMSINV(1-confidence)) tt : =z + 1/6*(z^2-1)S +1/24*(z^3-3z)K -1/36*(2*z^3-5z)S^2 S : skewness of returns K : kurtosis of returns sigma: volatility of period returns /////////////////////////// " .C20EXPLAIN18<-"An easy way to download daily/monthly data /////////////////////////// For example, to get daily price data for IBM -------------------------------------------- > x<-getDailyPrice(\"ibm\") > saveYan(x,\"c:/temp/ibmDaily.csv\") [1] \"Your saved file is ==>c:/temp/ibmDaily.csv\" To get monthly price data for Walmart ------------------------------------- > x<-getDailyPrice(\"wmt\") > saveYan(x,\"c:/temp/wmt.csv\") [1] \"Your saved file is ==>c:/temp/wmt.csv\" /////////////////////////// " .C20EXPLAIN19<-"Videos /////////////////////////// VaR (Value at Risk), explained (6m29s) https://www.youtube.com/watch?v=L_EZQhLrwAU Value at Risk - Introduction (9m31s) https://www.youtube.com/watch?v=OGCRrZAZGDc CAIIB - Value at Risk (9m24s) https://www.youtube.com/watch?v=c9el1wL0KSs Value At Risk, VAR (18m32s) https://www.youtube.com/watch?v=F6_vGfDwkfk 2015 - FRM : VAR Methods Part I (of 2) (40m28s, 9m54s) https://www.youtube.com/watch?v=y88vSlGgDaU https://www.youtube.com/watch?v=Vs4tq2Sn9OU /////////////////////////// " .C20EXPLAIN20<-"Links /////////////////////////// Wikipedia,https://en.wikipedia.org/wiki/Value_at_risk Investopedia http://www.investopedia.com/terms/v/var.asp Definition http://www.hedgefund-index.com/d_var.asp Financial Time, Definition of value at risk - VaR http://lexicon.ft.com/Term?term=value-at-risk-_-VaR Prof. Adamodar, NYC http://people.stern.nyu.edu/adamodar/pdfiles/papers/VAR.pdf /////////////////////////// " .C20EXPLAIN21<-"How to generate a n-day return /////////////////////////// Assume that we have downloaded daily price data for IBM, see the above explanation. A few lines of observations are show below. Date,Open,High,Low,Close,Volume,Adj.Close 2017-04-18,170.789993,171.690002,169.830002,170.050003,6194800,170.050003 2017-04-17,169.75,171.300003,169.619995,171.100006,3676500,171.100006 2017-04-13,169.919998,171.360001,169.529999,169.529999,3203900,169.529999 2017-04-12,171.039993,171.199997,170.020004,170.660004,3276900,170.660004 2017-04-11,170.649994,171.229996,168.979996,170.580002,4890200,170.580002 2017-04-10,172.529999,172.559998,171,171.199997,3715300,171.199997 Step 1: calculate log return, assume it is column H with a name of \"logDailyRet\" =ln(G1/G2) # G column is the daily price Step 2: generate a column called \"i\" Assume it is I column =1 # the first value =I1+1 # the second value Step 3: generate a variable called group (\"J\" column =int(I1/10) =int(I2/10) The first 10 value would be 0, while the next 10 values will be 1, and so on Step 4: Click \"Insert\", \"Pivot Table \" Step 5: Use Group as \"Row Labels\", DailyLogRet as value choose \"Sum of dailyLogRet\". We have 10-day log return Step 6: convert log returns to percentage returns R(%) = exp(R_log) -1 /////////////////////////// "