
EXERCÍCIOS DE RISCO - PARTE 2
Value-at-Risk (VaR) and Expected Shortfall (ES) focus on large losses and are more direct indications of financial risk
For most assets, future returns cannot be known exactly and therefore are random variables. Risk means uncertainty in future returns from an investment, in particular, that the investment could earn less than the expected return and even result in a loss, that is, a negative return. Risk is often measured by the standard deviation of the return, which we also call the volatility. Recently there has been a trend toward measuring risk by value-at-risk (VaR) and expected shortfall (ES). These focus on large losses and are more direct indications of financial risk than the standard deviation of the return. Because risk depends upon the probability distribution of a return, probability and statistics are fundamental tools for finance.
Part of Introduction of the book by David Ruppert and David S. Matteson, “Statistics and Data Analysis for Financial Engineering - with R examples”, Publisher: Springer; 2 edition (April 21, 2015).
VaR Histórico e o VaR Híbrido

Questions 3 and 4 from "Chapter 3 - Market Risk: Value at Risk" by M. B. Miller, “Quantitative Financial Risk Management”, Wiley, 2019.
CVaR Theory

M. B. Miller, “Quantitative Financial Risk Management”, Wiley, 2019.
CVaR Problem

M. B. Miller, “Quantitative Financial Risk Management”, Wiley, 2019.
Stress Tests and Black Swan
VAR should be complemented by stress-testing, which aims at identifying situations that could create extraordinary losses but plausible losses. One drawback of stress tests is that they are more subjective than VAR measures. A VAR number reflects realized risk. An extreme scenario, by contrast, may be more difficult to accept by senior management if it does not reflect an actual observation and if it looks too excessive.
In the case of our hedge fund with a $4 billion position short the yen, we have seen that the daily VAR at the 95 percent level of confidence is on the order of $42 million. In addition, it would be informative to see how worse the loss could be. Going back over the last twenty years, for example, the worst movement in the exchange rate was a loss of -5.4% on October 7, 1998. This leads to a stress loss of $215 million. Such a loss is plausible.

Philippe Jorion, Financial Risk Manager Handbook - FRM PART I / PART II, 6th Edition, Wiley Finance, 2011.
Stress Testing and Scenario Analysis
Stress testing, discussed in Chapter 16, is now a formal requirement of Basel III and the Dodd-Frank Act and has become a much more prominent part of the risk manager’s toolkit.
Properly applied, stress testing is a critical diagnostic and risk identification tool, but it can be counterproductive if it becomes too mechanical or consumes resources unproductively.
Firms need to ensure that stress testing methodologies and policies are consistently applied throughout the firm, take into account multiple risk factors, and adequately deal with correlations between risk factors.
Results must have a meaningful impact on business decisions.
M. Crouhy, D. Galai and R. Mark, "The Essentials of Risk Management", 2nd Edit, 2014.

Monte Carlo Simulation of Geometric Random Walk
In this section you will use simulations to see how stock prices evolve when the log-returns are i.i.d. normal, which implies that the price series is a geometric random walk.
Run the following R code. The set.seed() command insures that everyone using this code will have the same random numbers and will obtain the same price series. There are 253 trading days per year, so you are simulating 1 year of daily returns nine times. The price starts at 120.
The code par(mfrow=c(3,3)) on line 3 opens a graphics window with three rows and three columns and rnorm() on line 6 generates normally distributed random numbers.
1 set.seed(2012)
2 n = 253
3 par(mfrow=c(3,3))
4 for (i in (1:9))
5 {
6 log_return = rnorm(n, 0.05 / 253, 0.2 / sqrt(253))
7 price = c(120, 120 * exp(cumsum(log_return)))
8 plot(price, type = "b")
9 }

Problem 9 - In this simulation, what are the mean and standard deviation of the log-returns for 1 year?
David Ruppert and David S. Matteson, “Statistics and Data Analysis for Financial Engineering - with R examples”, Publisher: Springer; 2 edition (April 21, 2015).
Segue abaixo o mesmo código em Pyhton:
import numpy as np
import matplotlib.pyplot as plt
#
np.random.seed(2012)
number_simulations = 253 # number of simulations
valor_medio = 0.05 / 253 # 5%ao_ano
desvio_padrao = 0.2 / np.sqrt(253) # 0.2 ao_ano
z2 = np.random.normal(loc=valor_medio, scale=desvio_padrao, size=number_simulations)
# The probability density function of the normal distribution
out_sum = 120*(np.exp(np.cumsum(z2)))
eixo_x = np.arange(1, 253 + 1, 1)
# Values are generated within the half-open interval [start, stop)
# (in other words, the interval including start but excluding stop).
# For integer arguments the function is equivalent to the Python built-in
# range function, but returns an ndarray rather than a list.
plt.plot(eixo_x, out_sum, '-', lw=2)
plt.xlabel('number of simulations')
plt.ylabel('Price')
plt.title('Sorteios Aleatórios dentro da Probability Density Function Gaussian')
plt.grid(True)

Returns on a Stock
Suppose that the daily log returns on a stock are independent and normally distributed with mean 0.001 and standard deviation 0.015.
Suppose you buy $1,000 worth of this stock.
(a) What is the probability that after one trading day your investment is worth less than $990?
(b) What is the probability that after five trading days your investment is worth less than $990?
David Ruppert and David S. Matteson, “Statistics and Data Analysis for Financial Engineering - with R examples”, Publisher: Springer; 2 edition (April 21, 2015).
The Process for a Stock Price
Example 14.3 da "Seção 14.3 - The Process for a Stock Price", do "Chapter 14. - Wiener processes and Itoˆ ’s lemma", do livro do John C. Hull, “Options, Futures, and Other Derivatives”, 10th Edition, Publisher: Pearson, 2018.
The model of stock price behavior we have developed is known as geometric Brownian motion. The discrete-time version of the model is

The variable ∆S is the change in the stock price S in a small time interval ∆t, and as before ε has a standard normal distribution (i.e., a normal distribution with a mean of zero and standard deviation of 1.0). The parameter μ is the expected rate of return per unit of time from the stock. The parameter σ is the stock price volatility. In this chapter we will assume these parameters are constant.
The left-hand side of equation (14.7) is the discrete approximation to the return provided by the stock in a short period of time, ∆t. The term μ∆t is the expected value of this return, and the term σε√∆t is the stochastic component of the return. The variance of the stochastic component (and, therefore, of the whole return) is (σ**2)∆t. This is consistent with the definition of the volatility σ given in Section 13.7; that is, σ is such that σ√∆t is the standard deviation of the return in a short time period ∆t.
Equation (14.7) shows that ∆S/S is approximately normally distributed with mean μ∆t and standard deviation σ√∆t. In other words,

Example 14.3
Consider a stock that pays no dividends, has a volatility of 30% per annum, and provides an expected return of 15% per annum with continuous compounding. In this case, μ = 0.15 and σ = 0.30. The process for the stock price is

If S is the stock price at a particular time and ∆S is the increase in the stock price in the next small interval of time, the discrete approximation to the process is

where ε has a standard normal distribution. Consider a time interval of 1 week, or 0.0192 year (1/52 ~= 0.0192), so that ∆t = 0.0192. Then the approximation gives

By repeatedly simulating movements in the stock price, a complete probability distribution of the stock price at the end of this time (o horizonte final dele era 10 semanas) is obtained. Monte Carlo simulation is discussed in more detail in Chapter 21.
Segue abaixo o código em Pyhton:
#
import numpy as np
import matplotlib.pyplot as plt
#
step_number = 10
Sprice = np.zeros(10)
Sprice[0] = 100.0
# The initial stock price is assumed to be $100.
for i in range(1,step_number):
z = np.random.normal()
delta_Sprice = (0.15*0.0192*Sprice[i-1]) + (0.3*np.sqrt(0.0192)*Sprice[i-1]*z)
Sprice[i] = delta_Sprice + Sprice[i-1]
#}
plt.plot(Sprice)