<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Cheryl Kouadio</title>
<link>https://cheryl-kdio.github.io/posts.html</link>
<atom:link href="https://cheryl-kdio.github.io/posts.xml" rel="self" type="application/rss+xml"/>
<description>Cheryl Kouadio is a graduate student in stochastic modeling and quantitative finance at Université Paris Cité. Her academic interests focus on probability theory, stochastic processes, data analysis, and financial risk modeling. She studies how rigorous statistical and mathematical methods can be applied to quantify uncertainty and support decision-making in finance and risk management.</description>
<image>
<url>https://cheryl-kdio.github.io/files/profile/kouadio_profile.jpeg</url>
<title>Cheryl Kouadio</title>
<link>https://cheryl-kdio.github.io/posts.html</link>
</image>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Sat, 20 Jun 2026 22:00:00 GMT</lastBuildDate>
<item>
  <title>Particle method for stochastic local volatility valibration</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/vol/implied_vol.html</link>
  <description><![CDATA[ 




<p>Derivatives are financial instruments whose value depends on one or several underlying risk factors. Their contractual cash flows are determined by a payoff function, possibly path-dependent, which specifies how future payments depend on the evolution of the underlying asset. As market prices are unpredictable, models are needed to attempt to understand the explicable structure of price formation while accounting for its unexplainable part. This can, for example, help to hedge efficiently.</p>
<p>A foundational model in option pricing is the Black–Scholes model <span class="citation" data-cites="black1973pricing">(Black and Scholes 1973)</span>, introduced in 1973. It assumes that the underlying asset follows a geometric Brownian motion with constant volatility <img src="https://latex.codecogs.com/png.latex?%5Csigma">, deterministic interest rate <img src="https://latex.codecogs.com/png.latex?r">, and standard Brownian motion <img src="https://latex.codecogs.com/png.latex?W_t">. This framework yields closed-form formulae for European call and put options, making it possible to infer an implied volatility from observed option prices. Through this model, one can try to understand the effect of many factors on price formation. This is what is referred to as the Greeks.</p>
<p>One major limitation of the Black–Scholes model is its constant volatility assumption, meaning volatility has a constant impact on price formation. However, market option prices imply volatilities that vary across strikes and maturities, forming smiles and skews that are inconsistent with the use of a single constant volatility parameter. To address this shortcoming, several classes of models have been developed: local volatility models, where volatility depends on time and the underlying price; stochastic volatility models, where volatility follows its own random dynamics; and stochastic local volatility models, which combine both approaches. However, the central difficulty remains the calibration of these richer volatility models to the observed implied volatility surface.</p>
<p>In this article, we focus on the particle method proposed by Guyon and Henry-Labordère <span class="citation" data-cites="guyon2011smile">(Guyon and Henry-Labordere 2011)</span> for the calibration of stochastic local volatility models. More precisely, the method aims at estimating the leverage function that allows the model to reproduce the market smile while preserving a chosen stochastic volatility dynamics. We first trace the evolution from Black–Scholes to stochastic local volatility models. We then present the particle method and its implementation on a numerical example.</p>
<section id="from-blackscholes-constant-volatility-to-stochastic-local-volatility-a-brief-theoretical-background" class="level2">
<h2 class="anchored" data-anchor-id="from-blackscholes-constant-volatility-to-stochastic-local-volatility-a-brief-theoretical-background">From Black–Scholes Constant Volatility to Stochastic Local Volatility: a brief theoretical background</h2>
<section id="blackscholes-model" class="level3">
<h3 class="anchored" data-anchor-id="blackscholes-model">Black–Scholes model</h3>
<p>The Black–Scholes model <span class="citation" data-cites="black1973pricing">(Black and Scholes 1973)</span>, introduced in 1973, is a foundational model in option pricing. Its main idea is to model the underlying asset price through a lognormal dynamics. Under the risk-neutral measure, the asset price follows the stochastic differential equation</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20rS_tdt%20+%20%5Csigma%20S_tdW_t,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is a constant volatility, <img src="https://latex.codecogs.com/png.latex?r"> is a deterministic risk-free rate, and <img src="https://latex.codecogs.com/png.latex?W_t"> is a standard Brownian motion, that is normally distributed with law <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,t)">.</p>
<p>That being said, one can obtain an explicit expression for the spot price <img src="https://latex.codecogs.com/png.latex?S_t%20=%20S_0%20%5Cexp%5Cleft(%5Cleft(r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%5Cright)t%20+%20%5Csigma%20W_t%5Cright)">.</p>
<p>Taking a close look at this formulae, we can identify a log normal structure of spot price that allows European option prices to be computed explicitly. For example, for a European call option with strike <img src="https://latex.codecogs.com/png.latex?K"> and maturity <img src="https://latex.codecogs.com/png.latex?T">, the risk-neutral price at time <img src="https://latex.codecogs.com/png.latex?t"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5CPi_%7B%5Cmathrm%7BCall%7D%7D(t)%20&amp;=%20e%5E%7B-r(T-t)%7D%20%5Cmathbb%7BE%7D%5E%7B%5Cmathbb%7BQ%7D%7D%20%5Cleft%5B%20%5Cmax(S_T-K,0)%20%5Cmid%20%5Cmathcal%7BF%7D_t%20%5Cright%5D%20%5C%5C%0A&amp;=%20S_t%5CPhi(d_+)%20-%20Ke%5E%7B-r%5Ctau%7D%5CPhi(d_-).%0A%5Cend%7Balign*%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%20T-t">, <img src="https://latex.codecogs.com/png.latex?%5CPhi"> is the cumulative distribution function of the standard normal distribution, and $ d_= $.</p>
<p>The main limitation of this model is the assumption that volatility is constant. In practice, market option prices imply different volatilities for different strikes and maturities, producing the well-known implied volatility smile or skew. This observation motivates the introduction of richer volatility models.</p>
</section>
<section id="local-volatility-dupire-model" class="level3">
<h3 class="anchored" data-anchor-id="local-volatility-dupire-model">Local Volatility: Dupire model</h3>
<p>To address the empirical fact that volatility is not constant across strikes and maturities, Bruno Dupire introduced the local volatility model in 1994 <span class="citation" data-cites="dupire1994pricing">(Dupire et al. 1994)</span>. In this framework, the volatility is no longer constant, but is instead a deterministic function of time and the underlying asset price. The asset price dynamics are given by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20rS_tdt%20+%20%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D(t,S_t)S_tdW_t.%0A"></p>
<!-- This framework preserves market completeness in the idealized Black–Scholes sense, since the only source of randomness is the Brownian motion driving the underlying asset. As a result, contingent claims can in principle be replicated by dynamic trading in the underlying asset and the risk-free asset. -->
<p>Allowing the volatility to depend on both <img src="https://latex.codecogs.com/png.latex?t"> and <img src="https://latex.codecogs.com/png.latex?S_t"> is particularly useful because it gives enough flexibility to match the full implied volatility surface observed in the market. However, market quotes are not directly expressed as functions of the future spot process <img src="https://latex.codecogs.com/png.latex?S_t">. Instead, European option prices are observed as a function of strike <img src="https://latex.codecogs.com/png.latex?K"> and maturity <img src="https://latex.codecogs.com/png.latex?T">. We can therefore view market call prices as a surface <img src="https://latex.codecogs.com/png.latex?%5CPi_%7B%5Cmathrm%7BCall%7D%7D(t)%20=%20C(K,T)."></p>
<p>By differentiating this surface with respect to strike and maturity, Dupire derived a formula linking the observed option price surface to the local volatility function. Assuming constant interest rate <img src="https://latex.codecogs.com/png.latex?r">, the local volatility is given by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D%5E2(K,T)%20=%20%5Cfrac%7B%20%5Cpartial_T%20C(K,T)%20+%20r%20K%5Cpartial_K%20C(K,T)%7D%7B%0A%20%20%20%20%5Cfrac%7B1%7D%7B2%7DK%5E2%5Cpartial_%7BKK%7DC(K,T)%7D.%0A"></p>
<p>This formula shows that, under suitable regularity and no-arbitrage conditions, the local volatility surface can be recovered from European call prices. The local volatility model can therefore be calibrated exactly to the market implied volatility surface at the initial calibration date. Its main drawback, however, is dynamic. Although the model perfectly reproduces today’s vanilla option prices, the future evolution of the smile generated by the model is generally not consistent with the forward smile dynamics observed in the market. This limitation motivates the introduction of stochastic volatility models and, ultimately, stochastic local volatility models.</p>
</section>
<section id="stochastic-volatility-models" class="level3">
<h3 class="anchored" data-anchor-id="stochastic-volatility-models">Stochastic volatility models</h3>
<p>Stochastic volatility models were introduced to better reproduce the empirical dynamics of financial markets by allowing volatility itself to evolve randomly over time. Unlike the Black–Scholes model, where volatility is constant, these models introduce additional state variables that generate richer smile dynamics and more realistic volatility behavior. However, by adding a second source of randomness, they generally lead to market incompleteness. This mean, we could not perfectly hedge a derivative with the spot as underlying. Moreover, when specified with a finite number of parameters, they usually cannot fit an arbitrary implied volatility surface exactly.</p>
<p>In such models, the spot price typically follows a dynamics of the form : <img src="https://latex.codecogs.com/png.latex?dS_t%20=%20rS_tdt%20+%20%5Csigma_t%20S_tdW_t">, where <img src="https://latex.codecogs.com/png.latex?%5Csigma_t"> is now a stochastic process. Several stochastic volatility models have been proposed in the literature. One of the most widely used is the Heston model, introduced by Steven Heston. Under the risk-neutral measure, its dynamics are given by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20rS_tdt%20+%20%5Csqrt%7Bv_t%7DS_tdW_t%5ES,%20%5C%5C%0Adv_t%20=%20%5Ckappa(%5Ctheta-v_t)dt%20+%20%5Cxi%5Csqrt%7Bv_t%7DdW_t%5Ev,%20%5C%5C%0Ad%5Clangle%20W%5ES,%20W%5Ev%5Crangle_t%20=%20%5Crho%20dt,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?v_t"> denotes the instantaneous variance. The parameter <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> is the mean-reversion speed, <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is the long-term variance level, <img src="https://latex.codecogs.com/png.latex?%5Cxi"> is the volatility of volatility, and <img src="https://latex.codecogs.com/png.latex?%5Crho"> is the instantaneous correlation between the Brownian motion driving the asset price and the Brownian motion driving the variance. Through these parameters, the model captures the stylized features of volatility dynamics.</p>
<!-- The parameters $\kappa$ and $\theta$ encode the assumption that variance tends to revert toward a long-run level $\theta$. The larger $\kappa$ is, the faster this mean reversion occurs. The parameter $\xi$ controls the magnitude of random fluctuations in variance. Finally, the correlation parameter $\rho$ plays a crucial role in shaping the implied volatility skew. A negative value of $\rho$ captures the leverage effect: downward shocks in the spot price tend to be associated with upward shocks in variance, which typically produces a negative skew in implied volatilities. -->
</section>
<section id="stochastic-local-volatility-models" class="level3">
<h3 class="anchored" data-anchor-id="stochastic-local-volatility-models">Stochastic local volatility models</h3>
<p>Knowing the respective advantages and drawbacks of local and stochastic volatility models, the stochastic local volatility framework was developed to combine the exact calibration capability of local volatility models with the richer smile dynamics generated by stochastic volatility models. A stochastic local volatility model can be written as follows :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20rS_tdt%20+%20a_t%20l(t,S_t)S_tdW_t%5ES,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?a_t"> is a stochastic volatility factor and <img src="https://latex.codecogs.com/png.latex?l(t,S)"> is a deterministic function called the leverage function.</p>
<p>The goal of introducing the stochastic factor <img src="https://latex.codecogs.com/png.latex?a_t"> is to capture featurs of stochastic volatility models. That is to reproduce more realistic forward smile dynamics and volatility clustering effects observed in the market, while retaining enough flexibility to calibrate the model to vanilla option prices. The leverage function <img src="https://latex.codecogs.com/png.latex?l(t,S)"> is then chosen so that the SLV model matches the market prices of European vanilla options, or equivalently, the market implied volatility surface.</p>
<p>More precisely, the leverage function is calibrated so that the one-dimensional marginal distributions of the SLV model coincide with those implied by the corresponding local volatility model. This condition leads to the relation</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al%5E2(t,S)%20=%20%5Cfrac%7B%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D%5E2(t,S)%7D%7B%5Cmathbb%7BE%7D%5Cleft%5Ba_t%5E2%20%5Cmid%20S_t=S%5Cright%5D%7D,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D(t,S)"> denotes the local volatility surface obtained from Dupire’s formula.</p>
<p>Hence, the local volatility component ensures exact calibration to European vanilla option prices, while the stochastic volatility factor provides more realistic dynamics for exotic derivatives and forward smiles. The main numerical difficulty lies in the conditional expectation appearing in the denominator, since it depends on the joint distribution of the spot and the stochastic volatility factor. This is precisely where the particle method of Guyon and Henry-Labordère becomes useful.</p>
<p>The calibration problem is therefore transformed into the numerical estimation of a conditional expectation. Since this expectation depends on the law of the process being calibrated, the resulting dynamics are nonlinear in the McKean sense. Guyon and Henry-Labordère propose a particle method to approximate this nonlinear dynamics and recover the leverage function iteratively.</p>
</section>
</section>
<section id="the-particle-method-for-smile-calibration" class="level2">
<h2 class="anchored" data-anchor-id="the-particle-method-for-smile-calibration">The Particle Method for Smile Calibration</h2>
<p>As shown in the previous section, the stochastic local volatility framework combines both local and stochastic volatility components. It can be written as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20rS_tdt%20+%20a_t%20l(t,S_t)S_tdW_t%5ES,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?a_t"> is a stochastic volatility factor and <img src="https://latex.codecogs.com/png.latex?l(t,S)"> is the deterministic leverage function. This framework contains some simpler models as limiting cases. If <img src="https://latex.codecogs.com/png.latex?l(t,S)=1">, the model reduces to a pure stochastic volatility model. Equivalently, if <img src="https://latex.codecogs.com/png.latex?a_t%20%5Cequiv%201">, it reduces to a pure local volatility model.</p>
<p>The particle method of Guyon and Henry-Labordère is mainly designed for the calibration of stochastic local volatility models. More precisely, its purpose is to estimate the leverage function <img src="https://latex.codecogs.com/png.latex?l(t,S)"> so that the SLV model reproduces the market implied volatility surface while keeping the chosen stochastic volatility dynamics.</p>
<p>The key calibration relation is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al%5E2(t,S)%0A=%0A%5Cfrac%7B%0A%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D%5E2(t,S)%0A%7D%7B%0A%5Cmathbb%7BE%7D%5Cleft%5Ba_t%5E2%20%5Cmid%20S_t=S%5Cright%5D%0A%7D,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D(t,S)"> is the local volatility surface obtained from Dupire’s formula. The main numerical difficulty comes from the conditional expectation in the denominator. Indeed, this expectation depends on the joint distribution of <img src="https://latex.codecogs.com/png.latex?(S_t,a_t)">, which itself depends on the leverage function that we are trying to compute. This circular dependence makes the problem nonlinear in the McKean sense.</p>
<p>The idea of the particle method is to approximate the law of the process by simulating a large number of interacting particles. At each time step, the conditional expectation <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5Cleft%5Ba_t%5E2%20%5Cmid%20S_t=S%5Cright%5D"> is estimated from the simulated particles using a kernel regression method, typically a Nadaraya–Watson estimator. The leverage function is then updated on a spot grid and used to simulate the next time step.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?%5C%7Bt_k%5C%7D_%7Bk=0%7D%5E%7BM%7D"> denote a time discretization of <img src="https://latex.codecogs.com/png.latex?%5B0,T%5D">, with <img src="https://latex.codecogs.com/png.latex?t_k%20=%20k%5CDelta%20t."></p>
<p>The particle algorithm proceeds as follows.</p>
<ol type="1">
<li><p>Initialize the algorithm at <img src="https://latex.codecogs.com/png.latex?t_0=0">. Since all particles start from the same initial spot <img src="https://latex.codecogs.com/png.latex?S_0"> and the same initial volatility factor <img src="https://latex.codecogs.com/png.latex?a_0">, the leverage function is initialized by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al_N(0,S)%0A=%0A%5Cfrac%7B%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D(0,S)%7D%7Ba_0%7D.%0A"></p>
<p>In practice, this value is used on the first time interval <img src="https://latex.codecogs.com/png.latex?%5Bt_0,t_1%5D">.</p></li>
<li><p>Simulate <img src="https://latex.codecogs.com/png.latex?N"> particles</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7BS_t%5E%7Bi,N%7D,a_t%5E%7Bi,N%7D%5Cright%5C%7D_%7B1%5Cleq%20i%5Cleq%20N%7D%0A"></p>
<p>from <img src="https://latex.codecogs.com/png.latex?t_%7Bk-1%7D"> to <img src="https://latex.codecogs.com/png.latex?t_k"> using a time discretization scheme, for example a log-Euler scheme for the spot process. Each particle represents one simulated trajectory of the stochastic local volatility model.</p></li>
<li><p>At time <img src="https://latex.codecogs.com/png.latex?t_k">, estimate the conditional expectation <img src="https://latex.codecogs.com/png.latex?m(t_k,S)%20=%20%5Cmathbb%7BE%7D%5Cleft%5Ba_%7Bt_k%7D%5E2%20%5Cmid%20S_%7Bt_k%7D=S%5Cright%5D"> on a spot grid <img src="https://latex.codecogs.com/png.latex?G_%7BS,t_k%7D">. This can be done with a Nadaraya–Watson kernel estimator:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7Bm%7D(t_k,S)%20=%20%5Cfrac%7B%5Cdisplaystyle%5Csum_%7Bi=1%7D%5E%7BN%7D%20K_h%5Cleft(S_%7Bt_k%7D%5E%7Bi,N%7D-S%5Cright)%20%5Cleft(a_%7Bt_k%7D%5E%7Bi,N%7D%5Cright)%5E2%0A%7D%7B%0A%5Cdisplaystyle%5Csum_%7Bi=1%7D%5E%7BN%7D%20K_h%5Cleft(S_%7Bt_k%7D%5E%7Bi,N%7D-S%5Cright)%0A%7D,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?K_h(x)=K%5Cleft(%5Cfrac%7Bx%7D%7Bh%7D%5Cright)"> and <img src="https://latex.codecogs.com/png.latex?h"> is the bandwidth parameter. Typically, the bandwidth associated with the discretization date <img src="https://latex.codecogs.com/png.latex?t_k"> is chosen as <img src="https://latex.codecogs.com/png.latex?h_%7Bt_k,N%7D%20=%20%5Ckappa%20S_0%20%5Csigma_%7B%5Cmathrm%7BVS%7D%7D(t_k)%20%5Csqrt%7B%5Cmax(t_k,t_%7B%5Cmin%7D)%7DN%5E%7B-1/5%7D,"> where <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> is a tuning constant, <img src="https://latex.codecogs.com/png.latex?S_0"> is the initial spot price, <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7BVS%7D%7D(t_k)"> denotes the variance swap volatility at maturity <img src="https://latex.codecogs.com/png.latex?t_k">, and <img src="https://latex.codecogs.com/png.latex?t_%7B%5Cmin%7D"> is a lower time threshold used to avoid an excessively small bandwidth near the initial date.</p>
<p>The leverage function is then computed on the grid through</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al_N%5E2(t_k,S)%20=%20%5Cfrac%7B%20%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D%5E2(t_k,S)%20%7D%7B%20%5Cwidehat%7Bm%7D(t_k,S)%7D.%0A"></p>
<p>Equivalently,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al_N(t_k,S)%20=%20%5Cfrac%7B%20%5Csigma_%7B%5Cmathrm%7Bloc%7D%7D(t_k,S)%20%7D%7B%5Csqrt%7B%5Cwidehat%7Bm%7D(t_k,S)%7D%7D.%0A"></p>
<p>The values of <img src="https://latex.codecogs.com/png.latex?l_N(t_k,S)"> are then interpolated on the spot grid, for example using cubic splines. Outside the grid, one can use flat extrapolation to avoid unstable values in regions where few particles are available. Finally, the leverage function is frozen on the next interval:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al_N(t,S)%20=%20l_N(t_k,S),%20%5Cqquad%20t%20%5Cin%20%5Bt_k,t_%7Bk+1%7D%5D.%0A"></p></li>
<li><p>Set <img src="https://latex.codecogs.com/png.latex?k:=k+1"> and repeat steps 2 and 3 until the final maturity <img src="https://latex.codecogs.com/png.latex?T">.</p></li>
</ol>
</section>
<section id="illustrative-example" class="level2">
<h2 class="anchored" data-anchor-id="illustrative-example">Illustrative example</h2>
<p>In order to illustrate the particle method, let us consider the following stochastic local volatility (SLV) model:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Barray%7D%7Bl%7D%0AdS_t%20=%20a_t%20l(t,S_t)S_t%20dW_t%5E%7B(1)%7D,%20%5C%5C%5B4pt%5D%0Aa_t%20=%20%5Csigma_0%20e%5E%7BY_t%7D,%20%5C%5C%5B4pt%5D%0AdY_t%20=%20-%5Ckappa%20Y_tdt%20+%20%5Cgamma%20dW_t%5E%7B(2)%7D,%20%5C%5C%5B4pt%5D%0Ad%5Clangle%20W%5E%7B(1)%7D,W%5E%7B(2)%7D%5Crangle_t%20=%20%5Crho%20dt.%0A%5Cend%7Barray%7D%0A"></p>
<p>For simplicity, we work here with zero interest rate and zero dividend yield. Equivalently, the spot process can be interpreted as a forward price process under the risk-neutral measure.</p>
<p>The numerical values for the model parameters are <img src="https://latex.codecogs.com/png.latex?T%20=%201,%5C%20S_0%20=%20100,%5C%20%5Csigma_0%20=%2015%5C%25,%5C%20Y_0%20=%200,%5C%20%5Crho%20=%20-50%5C%25,%5C%20%5Cgamma%20=%2050%5C%25,%5C%20%5Ckappa%20=%201."></p>
<p>The goal is to find a leverage function <img src="https://latex.codecogs.com/png.latex?l(t,S)"> such that the SLV model matches the market prices of European vanilla options. For the sake of simplicity, we assume that the market implied volatility surface is flat, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7BMarket%7D%7D(t,K)%20%5Cequiv%2015%5C%25">. In this case, the corresponding Dupire local volatility is also constant: <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7BDup%7D%7D(t,S)%20%5Cequiv%2015%5C%25">.</p>
<section id="monte-carlo-scheme" class="level3">
<h3 class="anchored" data-anchor-id="monte-carlo-scheme">Monte carlo scheme</h3>
<p>We now describe a Monte Carlo simulation scheme for the SLV model. Let <img src="https://latex.codecogs.com/png.latex?(t_i)_%7B0%5Cleq%20i%5Cleq%20n%7D"> be a discretization of the interval <img src="https://latex.codecogs.com/png.latex?%5B0,T%5D">, with time steps <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t_i%20=%20t_i%20-%20t_%7Bi-1%7D,%5C%201%5Cleq%20i%5Cleq%20100">. In order to simulate both volatility and the spot process, we will use respectively the conditional moments of the euler discretisation and the log-euler discretisation scheme knowing <img src="https://latex.codecogs.com/png.latex?F_t-1"> since the processes followed a normal distribution. In order to take in account the correlation between both processes, we use the conditional correlation between <img src="https://latex.codecogs.com/png.latex?(Y_%7Bt_i%7D,%5Clog%20S_%7Bt_i%7D)%20%5Cmid%20(S_%7Bt_%7Bi-1%7D%7D,Y_%7Bt_%7Bi-1%7D%7D)"> by using a cholesky decomposition which is : <img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%7B%5Crho%7D%20=%20%5Crho%20%5Csqrt%7B%20%5Cfrac%7B%202(1-e%5E%7B-%5Ckappa%5CDelta%20t_i%7D)%7D%7B%20%5Ckappa%5CDelta%20t_i(1+e%5E%7B-%5Ckappa%5CDelta%20t_i%7D)%7D%7D.%0A"></p>
<p>Hence, one can jointly simulate <img src="https://latex.codecogs.com/png.latex?(%5Clog%20S_%7Bt_i%7D,Y_%7Bt_i%7D)"> by generating two independent standard Gaussian random variables <img src="https://latex.codecogs.com/png.latex?Z_1"> and <img src="https://latex.codecogs.com/png.latex?Z_2">.</p>
<p>The simulation scheme is then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Clog%20S_%7Bt_i%7D%20=%20%5Clog%20S_%7Bt_%7Bi-1%7D%7D%20-%20%5Cfrac12%20%5Csigma_0%5E2%20e%5E%7B2Y_%7Bt_%7Bi-1%7D%7D%7D%20l%5E2(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%20%5CDelta%20t_i%20+%0A%20%20%20%20%5Csigma_0%20e%5E%7BY_%7Bt_%7Bi-1%7D%7D%7D%20l(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%20%5Csqrt%7B%5CDelta%20t_i%7D%20%5Cleft(%20%5Csqrt%7B1-%5Cbar%7B%5Crho%7D%5E2%7DZ_1+%5Cbar%7B%5Crho%7DZ_2%20%5Cright),%20%5C%5C%5B8pt%5D%0AY_%7Bt_i%7D%20=%20e%5E%7B-%5Ckappa%5CDelta%20t_i%7DY_%7Bt_%7Bi-1%7D%7D%20+%20%5Cgamma%20%5Csqrt%7B%20%5Cfrac%7B1-e%5E%7B-2%5Ckappa%5CDelta%20t_i%7D%7D%7B2%5Ckappa%7D%20%7D%20Z_2.%0A%5Cend%7Bcases%7D%0A"></p>
<p>This procedure is repeated sequentially over all time intervals <img src="https://latex.codecogs.com/png.latex?%5Bt_%7Bi-1%7D,t_i%5D"> in order to generate Monte Carlo trajectories of the SLV model.</p>
The following figure shows 100 simulated paths of the spot price and the stochastic volatility factor <img src="https://latex.codecogs.com/png.latex?a_t">:
<p align="center">
<img src="https://cheryl-kdio.github.io/posts/m2mo/vol/slv_paths.png" width="800">
</p>
</section>
<section id="results" class="level3">
<h3 class="anchored" data-anchor-id="results">Results</h3>
<p>To calibrate the leverage function, we simulate <img src="https://latex.codecogs.com/png.latex?N=10,000"> paths. For the minimum and maximum values of <img src="https://latex.codecogs.com/png.latex?G_%7Bt_k%7D">, take the 0.1% and 99.9% quantiles of the risk-neutral distribution of <img src="https://latex.codecogs.com/png.latex?S"> at <img src="https://latex.codecogs.com/png.latex?t_k">, i.e., since the smile is flat at <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Ctextrm%7BMarket%7D%7D">, of the distribution of <img src="https://latex.codecogs.com/png.latex?S_0%5Cexp(%5Csigma_%7B%5Ctextrm%7BMarket%7D%7D%5Csqrt%7Bt_k%7DG%20-%20%5Cfrac12%20%5Csigma_%7B%5Ctextrm%7BMarket%7D%7D%5E2%20t_k)">, with <img src="https://latex.codecogs.com/png.latex?G"> a standard Gaussian random variable. We suggest that the grid <img src="https://latex.codecogs.com/png.latex?G_%7Bt_k%7D"> contain 30 equally spaced spot values. We also use the quartic kernel to perform kernel regression to estimate the conditional expectation, while fine-tuning the dimensionless bandwidth parameter <img src="https://latex.codecogs.com/png.latex?%5Ckappa">. The kernel is defined as follows: <img src="https://latex.codecogs.com/png.latex?%0AK(x)=(x+1)%5E2(1-x)%5E2%5Cquad%5Ctext%7Bfor%20%7D-1%5Cleq%20x%5Cleq%201%5Cquad%5Ctext%7Band%7D%5Cquad0%5Cquad%5Ctext%7B%20elsewhere%7D%0A"></p>
<p>To check that the resulting model is indeed calibrated to the market implied volatilities <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Ctextrm%7BMarket%7D%7D%20%5Cequiv%2015%5C%25">, we compute estimates of the call prices (maturity <img src="https://latex.codecogs.com/png.latex?T=1">) in the calibrated model for strikes <img src="https://latex.codecogs.com/png.latex?70,%2080,%2090,%20100,%20110,%20120,%20130,%20140">, and invert the Black-Scholes formula to get the corresponding estimation of the implied volatilities <img src="https://latex.codecogs.com/png.latex?%5Chat%5Csigma(T,K)">. To estimate the call prices in the calibrated model, we simulated a new set of independent paths with the calibrated leverage function <img src="https://latex.codecogs.com/png.latex?l"> and <img src="https://latex.codecogs.com/png.latex?N_2%20=%20100,000"> paths.</p>
<p>As shown in the following results, the SLV model calibrated via the particle method reproduces vanilla option prices with an implied volatility close to the market input. The mean absolute error is 12.3 bp, with the largest deviations occurring at deep in-the-money and out-of-the-money strikes, as expected.</p>
<p align="center">
<img src="https://cheryl-kdio.github.io/posts/m2mo/vol/result_particle_slv.png" width="500">
</p>
<p>The leverage function <img src="https://latex.codecogs.com/png.latex?l(t,%20K)"> (blue) is strongly increasing with the spot price, ranging from approximately 0.5 near <img src="https://latex.codecogs.com/png.latex?S_t%20=%2060"> to values above 1.5 at the right tail. This shape reflects the correction that the leverage function must apply to compensate for the stochastic volatility factor <img src="https://latex.codecogs.com/png.latex?a_t">: since <img src="https://latex.codecogs.com/png.latex?a_t"> (orange) is itself decreasing with the spot level, <img src="https://latex.codecogs.com/png.latex?l(t,%20K)"> must increase to ensure that their product remains consistent with the market implied volatility surface.</p>
<p>The total volatility <img src="https://latex.codecogs.com/png.latex?%5Csigma(t,%20K)%20=%20l(t,%20K)%20%5Ccdot%20a_t"> (green) remains remarkably flat across the entire spot range, hovering around 0.15. This is precisely the calibration objective: the particle method has successfully determined a leverage function such that the SLV model reproduces a flat implied volatility surface consistent with the market input <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cmathrm%7Bmkt%7D%7D%20=%2015%5C%25">.</p>
<p align="center">
<img src="https://cheryl-kdio.github.io/posts/m2mo/vol/slv_decomp.png" width="800">
</p>
</section>
</section>
<section id="appendices" class="level2">
<h2 class="anchored" data-anchor-id="appendices">Appendices</h2>
<section id="python-codes" class="level3">
<h3 class="anchored" data-anchor-id="python-codes">Python codes</h3>
<section id="kernel-regression-implementation" class="level4">
<h4 class="anchored" data-anchor-id="kernel-regression-implementation">Kernel regression implementation</h4>
<p>The following code implements the Nadaraya–Watson regression used to estimate the conditional expectation <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5Ba_t%5E2%20%5Cmid%20S_t=S%5D">, together with a leave-one-out cross-validation procedure for selecting the bandwidth parameter. The leave-one-out step avoids using a particle to predict itself when selecting the bandwidth. Once the optimal multiplier <img src="https://latex.codecogs.com/png.latex?%5Ckappa_%7B%5Cmathrm%7Bbw%7D%7D"> is obtained, the bandwidth <img src="https://latex.codecogs.com/png.latex?h"> is computed and used in the Nadaraya–Watson estimator to approximate the conditional expectation appearing in the denominator of the leverage function.</p>
<div id="aaef3879" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize_scalar</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> quartic_kernel(x):</span>
<span id="cb1-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Quartic kernel used in the Nadaraya-Watson regression.</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Values outside [-1, 1] are clipped, which gives zero weight at the boundary.</span></span>
<span id="cb1-9">    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.clip(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bandwidth(kappa_bw, tk, sigma_tk, S0, N):</span>
<span id="cb1-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Compute the kernel bandwidth in spot-price units.</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> kappa_bw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> S0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(tk, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span>
<span id="cb1-18"></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cv_bandwidth(S_particles,a2_particles,tk,sigma_tk,S0,N,kernel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quartic_kernel,bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>)):</span>
<span id="cb1-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Select the bandwidth multiplier by leave-one-out cross-validation.</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-24"></span>
<span id="cb1-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cv_loss(kappa_bw):</span>
<span id="cb1-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert the bandwidth multiplier into an actual bandwidth.</span></span>
<span id="cb1-27">        h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bandwidth(kappa_bw, tk, sigma_tk, S0, N)</span>
<span id="cb1-28"></span>
<span id="cb1-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute pairwise normalized distances between particles.</span></span>
<span id="cb1-30">        u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (S_particles[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, :] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> S_particles[:, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> h</span>
<span id="cb1-31"></span>
<span id="cb1-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kernel weights between particles.</span></span>
<span id="cb1-33">        W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kernel(u)</span>
<span id="cb1-34"></span>
<span id="cb1-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exclude each particle from its own regression estimate.</span></span>
<span id="cb1-36">        np.fill_diagonal(W, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)</span>
<span id="cb1-37"></span>
<span id="cb1-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nadaraya-Watson numerator and denominator.</span></span>
<span id="cb1-39">        num_loo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> a2_particles</span>
<span id="cb1-40">        den_loo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-41"></span>
<span id="cb1-42">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the global mean if too few neighbours are available.</span></span>
<span id="cb1-43">        valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> den_loo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span></span>
<span id="cb1-44">        m_hat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full_like(a2_particles, a2_particles.mean(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb1-45">        m_hat[valid] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> num_loo[valid] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> den_loo[valid]</span>
<span id="cb1-46"></span>
<span id="cb1-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cross-validation error.</span></span>
<span id="cb1-48">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.mean((a2_particles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m_hat) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-49"></span>
<span id="cb1-50">    result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize_scalar(cv_loss, bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bounds, method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounded"</span>)</span>
<span id="cb1-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result.x</span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> kernel_regression(S_particles, a2_particles, grid, h, kernel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quartic_kernel):</span>
<span id="cb1-54">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Estimate E[a_t^2 | S_t = s] on a spot grid.</span></span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-57">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute normalized distances between grid points and particles.</span></span>
<span id="cb1-58">    u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (S_particles[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, :] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> grid[:, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> h</span>
<span id="cb1-59"></span>
<span id="cb1-60">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kernel weights around each grid point.</span></span>
<span id="cb1-61">    W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kernel(u)</span>
<span id="cb1-62"></span>
<span id="cb1-63">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nadaraya-Watson numerator and denominator.</span></span>
<span id="cb1-64">    num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> a2_particles</span>
<span id="cb1-65">    denom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-66"></span>
<span id="cb1-67">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the global mean when no particle contributes to a grid point.</span></span>
<span id="cb1-68">    valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> denom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-12</span></span>
<span id="cb1-69">    cond_exp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full_like(grid, a2_particles.mean(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb1-70">    cond_exp[valid] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> num[valid] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom[valid]</span>
<span id="cb1-71"></span>
<span id="cb1-72">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Avoid division by zero later in the leverage function.</span></span>
<span id="cb1-73">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.maximum(cond_exp, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>)</span></code></pre></div>
</div>
</section>
<section id="particle-method-implementation-validation" class="level4">
<h4 class="anchored" data-anchor-id="particle-method-implementation-validation">Particle method implementation + validation</h4>
<div id="3f1ba3ad" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.interpolate <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> CubicSpline</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> spot_grid(tk, S0, sigma_mkt, n_points<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>):</span>
<span id="cb2-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Build the spot grid G_{t_k} from lognormal quantiles.</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># At time zero, all particles start from the same initial spot.</span></span>
<span id="cb2-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb2-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.array([S0])</span>
<span id="cb2-12"></span>
<span id="cb2-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lower bound of the grid: 0.1% lognormal quantile.</span></span>
<span id="cb2-14">    S_min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp( sigma_mkt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(tk) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma_mkt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> tk)</span>
<span id="cb2-15"></span>
<span id="cb2-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Upper bound of the grid: 99.9% lognormal quantile.</span></span>
<span id="cb2-17">    S_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp( sigma_mkt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(tk) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma_mkt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> tk)</span>
<span id="cb2-18"></span>
<span id="cb2-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.linspace(S_min, S_max, n_points)</span>
<span id="cb2-20"></span>
<span id="cb2-21"></span>
<span id="cb2-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> particle_method( S0, Y0, sigma0, rho, gamma, kappa, sigma_mkt, N, n_steps, Dt, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>):</span>
<span id="cb2-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calibrate the leverage function l(t_k, S) with the Guyon-Henry-Labordere</span></span>
<span id="cb2-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    particle method.</span></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-27">    np.random.seed(seed)</span>
<span id="cb2-28"></span>
<span id="cb2-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial stochastic volatility factor and initial leverage value.</span></span>
<span id="cb2-30">    a0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(Y0)</span>
<span id="cb2-31">    l0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_mkt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> a0</span>
<span id="cb2-32"></span>
<span id="cb2-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize all particles at the same spot and volatility state.</span></span>
<span id="cb2-34">    S_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full(N, S0, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb2-35">    Y_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full(N, Y0, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb2-36"></span>
<span id="cb2-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial spot grid and initial constant leverage spline.</span></span>
<span id="cb2-38">    K_grid_init <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spot_grid(Dt, S0, sigma_mkt, n_points<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb2-39">    spline_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CubicSpline(K_grid_init, np.full(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, l0))</span>
<span id="cb2-40">    K_grid_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K_grid_init</span>
<span id="cb2-41"></span>
<span id="cb2-42">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store the calibrated leverage splines and their associated grids.</span></span>
<span id="cb2-43">    splines <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb2-44">    Stoch_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb2-45">    K_grids <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb2-46"></span>
<span id="cb2-47">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n_steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb2-48">        tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt</span>
<span id="cb2-49"></span>
<span id="cb2-50">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evaluate the previous leverage function at current particle spots.</span></span>
<span id="cb2-51">        l_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spline_prev( np.clip(S_tk, K_grid_prev[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], K_grid_prev[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) )</span>
<span id="cb2-52"></span>
<span id="cb2-53">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effective correlation induced by exact simulation of the OU factor.</span></span>
<span id="cb2-54">        rho_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(</span>
<span id="cb2-55">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt))</span>
<span id="cb2-56">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt)))</span>
<span id="cb2-57">        )</span>
<span id="cb2-58"></span>
<span id="cb2-59">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Independent Gaussian variables used to generate correlated increments.</span></span>
<span id="cb2-60">        Z1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.randn(N)</span>
<span id="cb2-61">        Z2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.randn(N)</span>
<span id="cb2-62"></span>
<span id="cb2-63">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exact simulation of the Ornstein-Uhlenbeck volatility factor.</span></span>
<span id="cb2-64">        Y_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-65">            np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Y_tk</span>
<span id="cb2-66">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gamma</span>
<span id="cb2-67">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa))</span>
<span id="cb2-68">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z2</span>
<span id="cb2-69">        )</span>
<span id="cb2-70"></span>
<span id="cb2-71">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effective volatility of each particle over the current time step.</span></span>
<span id="cb2-72">        sig_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(Y_tk) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> l_prev</span>
<span id="cb2-73"></span>
<span id="cb2-74">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-Euler update of the spot process.</span></span>
<span id="cb2-75">        log_S_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-76">            np.log(np.maximum(S_tk, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>))</span>
<span id="cb2-77">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sig_eff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt</span>
<span id="cb2-78">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sig_eff</span>
<span id="cb2-79">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(Dt)</span>
<span id="cb2-80">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho_bar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rho_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z2)</span>
<span id="cb2-81">        )</span>
<span id="cb2-82"></span>
<span id="cb2-83">        S_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp(log_S_new)</span>
<span id="cb2-84">        a_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(Y_new)</span>
<span id="cb2-85"></span>
<span id="cb2-86">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build the spot grid at the current time.</span></span>
<span id="cb2-87">        K_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spot_grid(tk, S0, sigma_mkt, n_points<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb2-88"></span>
<span id="cb2-89">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Squared stochastic volatility factor used in the conditional expectation.</span></span>
<span id="cb2-90">        a2_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a_new<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-91"></span>
<span id="cb2-92">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select the bandwidth parameter once, then reuse it at later dates.</span></span>
<span id="cb2-93">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb2-94">            kappa_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cv_bandwidth(S_new, a2_new, tk, sigma_mkt, S0, N, kernel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quartic_kernel)</span>
<span id="cb2-95"></span>
<span id="cb2-96">        h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bandwidth(kappa_opt, tk, sigma_mkt, S0, N)</span>
<span id="cb2-97"></span>
<span id="cb2-98">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimate E[a_t^2 | S_t = S] on the spot grid.</span></span>
<span id="cb2-99">        cond_exp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kernel_regression(S_new, a2_new, K_grid, h, kernel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quartic_kernel)</span>
<span id="cb2-100"></span>
<span id="cb2-101">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the leverage function from the calibration identity.</span></span>
<span id="cb2-102">        l_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_mkt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.sqrt(cond_exp)</span>
<span id="cb2-103"></span>
<span id="cb2-104">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpolate the leverage function on the current spot grid.</span></span>
<span id="cb2-105">        spline_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CubicSpline(K_grid, l_values)</span>
<span id="cb2-106"></span>
<span id="cb2-107">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store current leverage spline and grid.</span></span>
<span id="cb2-108">        splines[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spline_curr</span>
<span id="cb2-109">        K_grids[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K_grid</span>
<span id="cb2-110">        Stoch_vol[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(cond_exp)</span>
<span id="cb2-111"></span>
<span id="cb2-112">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move to the next time step.</span></span>
<span id="cb2-113">        spline_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spline_curr</span>
<span id="cb2-114">        K_grid_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K_grid</span>
<span id="cb2-115">        S_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S_new</span>
<span id="cb2-116">        Y_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y_new</span>
<span id="cb2-117"></span>
<span id="cb2-118">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Optimal bandwidth multiplier: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>kappa_opt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-119"></span>
<span id="cb2-120">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> splines, K_grids, Stoch_vol</span>
<span id="cb2-121"></span>
<span id="cb2-122"></span>
<span id="cb2-123"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> simulate_calibrated(S0, Y0, sigma0, rho, gamma, kappa, sigma_mkt, N2, n_steps, Dt, splines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, K_grids<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>):</span>
<span id="cb2-124">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-125"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Simulate paths under the calibrated SLV model.</span></span>
<span id="cb2-126"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-127">    np.random.seed(seed)</span>
<span id="cb2-128"></span>
<span id="cb2-129">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize validation particles.</span></span>
<span id="cb2-130">    S_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full(N2, S0, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb2-131">    Y_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.full(N2, Y0, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb2-132"></span>
<span id="cb2-133">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store simulated spot paths.</span></span>
<span id="cb2-134">    S_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((n_steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N2))</span>
<span id="cb2-135">    S_paths[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S_tk</span>
<span id="cb2-136"></span>
<span id="cb2-137">    Y_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((n_steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N2))</span>
<span id="cb2-138">    Y_paths[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y_tk</span>
<span id="cb2-139"></span>
<span id="cb2-140">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n_steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb2-141"></span>
<span id="cb2-142">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use calibrated leverage if provided; otherwise simulate pure stochastic volatility.</span></span>
<span id="cb2-143">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> splines <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> K_grids <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb2-144">            l_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.ones(N2)</span>
<span id="cb2-145">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb2-146">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the leverage function calibrated at the previous time step.</span></span>
<span id="cb2-147">            spline_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> splines[k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> splines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-148">            K_grid_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K_grids[k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> K_grids[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-149"></span>
<span id="cb2-150">            l_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spline_prev(</span>
<span id="cb2-151">                np.clip(S_tk, K_grid_prev[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], K_grid_prev[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-152">            )</span>
<span id="cb2-153"></span>
<span id="cb2-154">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effective correlation for the joint simulation of log-spot and OU factor.</span></span>
<span id="cb2-155">        rho_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(</span>
<span id="cb2-156">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt))</span>
<span id="cb2-157">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt)))</span>
<span id="cb2-158">        )</span>
<span id="cb2-159"></span>
<span id="cb2-160">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Independent Gaussian variables.</span></span>
<span id="cb2-161">        Z1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.randn(N2)</span>
<span id="cb2-162">        Z2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.randn(N2)</span>
<span id="cb2-163"></span>
<span id="cb2-164">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exact update of the OU volatility factor.</span></span>
<span id="cb2-165">        Y_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-166">            np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Y_tk</span>
<span id="cb2-167">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gamma</span>
<span id="cb2-168">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa))</span>
<span id="cb2-169">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z2</span>
<span id="cb2-170">        )</span>
<span id="cb2-171"></span>
<span id="cb2-172">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effective volatility under the calibrated SLV model.</span></span>
<span id="cb2-173">        sig_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(Y_tk) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> l_prev</span>
<span id="cb2-174"></span>
<span id="cb2-175">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-Euler update of the spot process.</span></span>
<span id="cb2-176">        log_S_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-177">            np.log(np.maximum(S_tk, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>))</span>
<span id="cb2-178">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sig_eff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Dt</span>
<span id="cb2-179">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sig_eff</span>
<span id="cb2-180">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(Dt)</span>
<span id="cb2-181">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho_bar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rho_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z2)</span>
<span id="cb2-182">        )</span>
<span id="cb2-183"></span>
<span id="cb2-184">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store new spot values and update the volatility factor.</span></span>
<span id="cb2-185">        S_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp(log_S_new)</span>
<span id="cb2-186">        S_paths[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S_tk</span>
<span id="cb2-187">        Y_tk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y_new</span>
<span id="cb2-188">        Y_paths[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y_tk</span>
<span id="cb2-189"></span>
<span id="cb2-190">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> S_paths, Y_paths</span></code></pre></div>
</div>
</section>
<section id="black-scholes-price-implementation" class="level4">
<h4 class="anchored" data-anchor-id="black-scholes-price-implementation">Black scholes price implementation</h4>
<div id="b0e1f8d9" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> py_vollib.black_scholes.implied_volatility <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> implied_volatility</span>
<span id="cb3-2">vec_find_vol_rat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.vectorize(implied_volatility)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a better implied vol estimator based on "P Jäckel Let's be rational"</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> blackscholes_price(K, T, S, vol, r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, callput<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'call'</span>):</span>
<span id="cb3-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Compute the call/put option price in Black-Scholes model</span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb3-8">    F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.exp((r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>q)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>T)</span>
<span id="cb3-9">    w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>T</span>
<span id="cb3-10">    d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (np.log(F<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>K)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>w)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>np.sqrt(w)</span>
<span id="cb3-11">    d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.sqrt(w)</span>
<span id="cb3-12">    callput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> callput.lower()</span>
<span id="cb3-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> callput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'call'</span>:</span>
<span id="cb3-14">        opttype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> callput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'put'</span>:</span>
<span id="cb3-16">        opttype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The value of callput must be either "call" or "put".'</span>)</span>
<span id="cb3-19">    price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (opttype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>F<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>norm.cdf(opttype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>opttype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>norm.cdf(opttype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(d2)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>T)</span>
<span id="cb3-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> price</span></code></pre></div>
</div>
</section>
<section id="application-of-the-particle-method" class="level4">
<h4 class="anchored" data-anchor-id="application-of-the-particle-method">Application of the particle method</h4>
<div id="84c81229" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Paramètres du modèle</span></span>
<span id="cb4-2">T       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb4-3">S0      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span></span>
<span id="cb4-4">sigma0  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span></span>
<span id="cb4-5">Y0      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb4-6">rho     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span></span>
<span id="cb4-7">gamma   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span></span>
<span id="cb4-8">kappa   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb4-9">sigma_mkt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># smile plat</span></span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Paramètres numériques</span></span>
<span id="cb4-12">Dt       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb4-13">n_steps  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> Dt)</span>
<span id="cb4-14">N_calib  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-15">N_valid  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100_000</span></span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strikes pour la validation</span></span>
<span id="cb4-18">strikes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">110</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>], dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Étape A : Calibration ─────────────────────────────────────────────</span></span>
<span id="cb4-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb4-22"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"CALIBRATION  N=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_calib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  Nt=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_steps<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-23">splines, K_grids, Stoch_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> particle_method(</span>
<span id="cb4-24">    S0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S0, Y0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Y0, sigma0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma0, rho<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rho,</span>
<span id="cb4-25">    gamma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>gamma, kappa<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>kappa,</span>
<span id="cb4-26">    sigma_mkt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_mkt,</span>
<span id="cb4-27">    N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N_calib, n_steps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n_steps, Dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Dt, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span></span>
<span id="cb4-28">)</span>
<span id="cb4-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Calibration terminée.</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-30"></span>
<span id="cb4-31"></span>
<span id="cb4-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Étape B : Simulation avec levier calibré</span></span>
<span id="cb4-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb4-34"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VALIDATION   N=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_valid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-35">S_paths, Y_paths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> simulate_calibrated(</span>
<span id="cb4-36">    S0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S0, Y0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Y0, sigma0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma0, rho<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rho,</span>
<span id="cb4-37">    gamma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>gamma, kappa<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>kappa, sigma_mkt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_mkt,</span>
<span id="cb4-38">    N2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N_valid, n_steps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n_steps, Dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Dt,</span>
<span id="cb4-39">    splines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>splines, K_grids<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K_grids, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span></span>
<span id="cb4-40">)</span></code></pre></div>
</div>
</section>
<section id="results-layouts" class="level4">
<h4 class="anchored" data-anchor-id="results-layouts">Results layouts</h4>
<div id="9e564563" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Show simulation paths</span></span>
<span id="cb5-4">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb5-5">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].plot(sigma0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(Y_paths[:, :<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>]))</span>
<span id="cb5-6">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$a_t$ paths"</span>)</span>
<span id="cb5-7"></span>
<span id="cb5-8">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"$S_t$ paths"</span>)</span>
<span id="cb5-9">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(S_paths[:, :<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>])</span>
<span id="cb5-10"></span>
<span id="cb5-11">plt.tight_layout()</span>
<span id="cb5-12">plt.show()</span></code></pre></div>
</div>
<div id="5a4e3e7e" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Decomposition of leverage functions</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=======================</span></span>
<span id="cb6-3"></span>
<span id="cb6-4">K_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K_grids[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(K_grids.keys())]</span>
<span id="cb6-5">l_vals <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> splines[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(splines.keys())](K_grid)</span>
<span id="cb6-6">a_vals <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Stoch_vol[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(Stoch_vol.keys())]</span>
<span id="cb6-7"></span>
<span id="cb6-8">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb6-9">plt.plot(K_grid, l_vals, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'$l(t, K)$ — leverage function'</span>)</span>
<span id="cb6-10">plt.plot(K_grid, a_vals, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'$a_t$ — stochastic volatility factor'</span>)</span>
<span id="cb6-11">plt.plot(K_grid, l_vals <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> a_vals, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'$\sigma(t, K) = l(t,K)\cdot a_t$ — total volatility'</span>)</span>
<span id="cb6-12">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spot price $S_t$"</span>)</span>
<span id="cb6-13">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatility"</span>)</span>
<span id="cb6-14">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decomposition of the fitted SLV volatility"</span>)</span>
<span id="cb6-15">plt.legend()</span>
<span id="cb6-16">plt.show()</span></code></pre></div>
</div>
<div id="50c9cb1b" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Étape C : Prix des calls et volatilités implicites</span></span>
<span id="cb7-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;8}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Call MC'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Call BS'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'IV hat'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Erreur (bp)'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>)</span>
<span id="cb7-4"></span>
<span id="cb7-5">S_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S_paths[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,:] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spot prices at T</span></span>
<span id="cb7-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> K <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> strikes:</span>
<span id="cb7-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix Monte-Carlo</span></span>
<span id="cb7-8">    call_mc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.maximum(S_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb7-9"></span>
<span id="cb7-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix Black-Scholes à sigma_mkt</span></span>
<span id="cb7-11">    call_bs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> blackscholes_price(K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S0,vol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_mkt,callput<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'call'</span>)</span>
<span id="cb7-12"></span>
<span id="cb7-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Volatilité implicite</span></span>
<span id="cb7-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-15">        iv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vec_find_vol_rat(price<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>call_mc, S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S0, K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K, t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T, r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>)        </span>
<span id="cb7-16">        err_bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(iv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> sigma_mkt)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span></span>
<span id="cb7-17">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>K<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;8.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_mc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_bs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>iv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>err_bp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;+12.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb7-19">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>K<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;8.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_mc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_bs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  N/A'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  N/A'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-20"></span>
<span id="cb7-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>)</span>
<span id="cb7-22">ivs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb7-23"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> K <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> strikes:</span>
<span id="cb7-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-25">        price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.maximum(S_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb7-26">        ivs.append(vec_find_vol_rat(price, S0, K, T, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>))</span>
<span id="cb7-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb7-28">        ivs.append(np.nan)</span>
<span id="cb7-29">ivs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(ivs)</span>
<span id="cb7-30">valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>np.isnan(ivs)</span>
<span id="cb7-31">mae_bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(ivs[valid] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> sigma_mkt)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span></span>
<span id="cb7-32"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Erreur absolue moyenne : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mae_bp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> bp"</span>)</span>
<span id="cb7-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span></code></pre></div>
</div>
</section>
</section>
<section id="mathematical-details-of-simulation" class="level3">
<h3 class="anchored" data-anchor-id="mathematical-details-of-simulation">Mathematical details of simulation</h3>
<section id="simulation-of-the-volatility-factor" class="level4">
<h4 class="anchored" data-anchor-id="simulation-of-the-volatility-factor">Simulation of the volatility factor</h4>
<p>The Ornstein–Uhlenbeck process <img src="https://latex.codecogs.com/png.latex?Y"> admits the explicit representation</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY_%7Bt_i%7D%0A=%0Ae%5E%7B-%5Ckappa%20%5CDelta%20t_i%7DY_%7Bt_%7Bi-1%7D%7D%0A+%0A%5Cint_%7Bt_%7Bi-1%7D%7D%5E%7Bt_i%7D%0A%5Cgamma%20e%5E%7B-%5Ckappa(t_i-s)%7DdW_s%5E%7B(2)%7D.%0A"></p>
<p>Conditionally on <img src="https://latex.codecogs.com/png.latex?Y_%7Bt_%7Bi-1%7D%7D">, the random variable <img src="https://latex.codecogs.com/png.latex?Y_%7Bt_i%7D"> is Gaussian with conditional moments</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5Cleft%5B%0AY_%7Bt_i%7D%5Cmid%20Y_%7Bt_%7Bi-1%7D%7D%0A%5Cright%5D%0A=%0Ae%5E%7B-%5Ckappa%5CDelta%20t_i%7DY_%7Bt_%7Bi-1%7D%7D,%0A"></p>
<p>and</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Coperatorname%7BVar%7D%5Cleft%5B%0AY_%7Bt_i%7D%5Cmid%20Y_%7Bt_%7Bi-1%7D%7D%0A%5Cright%5D%0A=%0A%5Cfrac%7B%5Cgamma%5E2%7D%7B2%5Ckappa%7D%0A%5Cleft(%0A1-e%5E%7B-2%5Ckappa%5CDelta%20t_i%7D%0A%5Cright).%0A"></p>
<p>Hence, the volatility factor can be simulated exactly on each time interval.</p>
</section>
<section id="simulation-of-the-spot-process" class="level4">
<h4 class="anchored" data-anchor-id="simulation-of-the-spot-process">Simulation of the spot process</h4>
<p>To simulate the spot process, we use a log-Euler discretization scheme. Over the interval <img src="https://latex.codecogs.com/png.latex?%5Bt_%7Bi-1%7D,t_i%5D">, the volatility factor and the leverage function are frozen at their left-endpoint values. Thus,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20S_%7Bt_i%7D-%5Clog%20S_%7Bt_%7Bi-1%7D%7D%0A=%0A-%5Cfrac12%0A%5Csigma_0%5E2%20e%5E%7B2Y_%7Bt_%7Bi-1%7D%7D%7D%0Al%5E2(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5CDelta%20t_i%0A+%0A%5Csigma_0%20e%5E%7BY_%7Bt_%7Bi-1%7D%7D%7D%0Al(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5Cint_%7Bt_%7Bi-1%7D%7D%5E%7Bt_i%7DdW_t%5E%7B(1)%7D.%0A"></p>
<p>Conditionally on <img src="https://latex.codecogs.com/png.latex?(S_%7Bt_%7Bi-1%7D%7D,Y_%7Bt_%7Bi-1%7D%7D)">, the pair <img src="https://latex.codecogs.com/png.latex?(%5Clog%20S_%7Bt_i%7D,Y_%7Bt_i%7D)"> is jointly Gaussian.</p>
<p>Its conditional expectation for the log-spot is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%0A%5Cleft%5B%0A%5Clog%20S_%7Bt_i%7D%0A%5Cmid%0AS_%7Bt_%7Bi-1%7D%7D,Y_%7Bt_%7Bi-1%7D%7D%0A%5Cright%5D%0A=%0A%5Clog%20S_%7Bt_%7Bi-1%7D%7D%0A-%0A%5Cfrac12%0A%5Csigma_0%5E2%20e%5E%7B2Y_%7Bt_%7Bi-1%7D%7D%7D%0Al%5E2(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5CDelta%20t_i.%0A"></p>
<p>Its conditional variance is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Coperatorname%7BVar%7D%0A%5Cleft%5B%0A%5Clog%20S_%7Bt_i%7D%0A%5Cmid%0AS_%7Bt_%7Bi-1%7D%7D,Y_%7Bt_%7Bi-1%7D%7D%0A%5Cright%5D%0A=%0A%5Csigma_0%5E2%20e%5E%7B2Y_%7Bt_%7Bi-1%7D%7D%7D%0Al%5E2(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5CDelta%20t_i.%0A"></p>
<p>Finally, the conditional covariance between <img src="https://latex.codecogs.com/png.latex?Y_%7Bt_i%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Clog%20S_%7Bt_i%7D"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Coperatorname%7BCov%7D%0A%5Cleft%5B%0AY_%7Bt_i%7D,%5Clog%20S_%7Bt_i%7D%0A%5Cmid%0AS_%7Bt_%7Bi-1%7D%7D,Y_%7Bt_%7Bi-1%7D%7D%0A%5Cright%5D%0A=%0A%5Csigma_0%20e%5E%7BY_%7Bt_%7Bi-1%7D%7D%7D%0Al(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5Cfrac%7B%5Cgamma%5Crho%7D%7B%5Ckappa%7D%0A%5Cleft(%0A1-e%5E%7B-%5Ckappa%5CDelta%20t_i%7D%0A%5Cright).%0A"></p>
<p>Equivalently, the corresponding conditional correlation is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%7B%5Crho%7D%0A=%0A%5Crho%0A%5Csqrt%7B%0A%5Cfrac%7B%0A2(1-e%5E%7B-%5Ckappa%5CDelta%20t_i%7D)%0A%7D%7B%0A%5Ckappa%5CDelta%20t_i(1+e%5E%7B-%5Ckappa%5CDelta%20t_i%7D)%0A%7D%0A%7D.%0A"></p>
</section>
<section id="numerical-scheme" class="level4">
<h4 class="anchored" data-anchor-id="numerical-scheme">Numerical scheme</h4>
<p>Using the previous conditional moments, one can jointly simulate <img src="https://latex.codecogs.com/png.latex?(%5Clog%20S_%7Bt_i%7D,Y_%7Bt_i%7D)"> by generating two independent standard Gaussian random variables <img src="https://latex.codecogs.com/png.latex?Z_1"> and <img src="https://latex.codecogs.com/png.latex?Z_2">.</p>
<p>The simulation scheme is then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Clog%20S_%7Bt_i%7D%0A=%0A%5Clog%20S_%7Bt_%7Bi-1%7D%7D%0A-%0A%5Cfrac12%0A%5Csigma_0%5E2%20e%5E%7B2Y_%7Bt_%7Bi-1%7D%7D%7D%0Al%5E2(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5CDelta%20t_i%0A+%0A%5Csigma_0%20e%5E%7BY_%7Bt_%7Bi-1%7D%7D%7D%0Al(t_%7Bi-1%7D,S_%7Bt_%7Bi-1%7D%7D)%0A%5Csqrt%7B%5CDelta%20t_i%7D%0A%5Cleft(%0A%5Csqrt%7B1-%5Cbar%7B%5Crho%7D%5E2%7DZ_1+%5Cbar%7B%5Crho%7DZ_2%0A%5Cright),%0A%5C%5C%5B8pt%5D%0AY_%7Bt_i%7D%0A=%0Ae%5E%7B-%5Ckappa%5CDelta%20t_i%7DY_%7Bt_%7Bi-1%7D%7D%0A+%0A%5Cgamma%0A%5Csqrt%7B%0A%5Cfrac%7B1-e%5E%7B-2%5Ckappa%5CDelta%20t_i%7D%7D%7B2%5Ckappa%7D%0A%7D%0AZ_2.%0A%5Cend%7Bcases%7D%0A"></p>
<p>This procedure is repeated sequentially over all time intervals <img src="https://latex.codecogs.com/png.latex?%5Bt_%7Bi-1%7D,t_i%5D"> in order to generate Monte Carlo trajectories of the SLV model.</p>



</section>
</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-black1973pricing" class="csl-entry">
Black, Fischer, and Myron Scholes. 1973. <span>“The Pricing of Options and Corporate Liabilities.”</span> <em>Journal of Political Economy</em> 81 (3): 637–54.
</div>
<div id="ref-dupire1994pricing" class="csl-entry">
Dupire, Bruno et al. 1994. <span>“Pricing with a Smile.”</span> <em>Risk</em> 7 (1): 18–20.
</div>
<div id="ref-guyon2011smile" class="csl-entry">
Guyon, Julien, and Pierre Henry-Labordere. 2011. <span>“The Smile Calibration Problem Solved.”</span> <em>Available at SSRN 1885032</em>.
</div>
</div></section></div> ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>options</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/vol/implied_vol.html</guid>
  <pubDate>Sat, 20 Jun 2026 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Options made simple: what they are and why they matter and how do we price them?</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/derivatives/03_options_made_simple.html</link>
  <description><![CDATA[ 




<p>In a trading floor, exchanges take place continuously between different counterparties ,states, corporations, financial institutions, and individual investors, etc. ,with the aim of transferring, managing, or valuing risks related to price, rate, or currency fluctuations or even more simple : to make money.</p>
<p>These exchanges involve derivatives, i.e.&nbsp;contracts concluded between two parties that define rights and monetary obligations. Each derivative has a price, determined by market conditions and the expectations of market participants.</p>
<p>There are generally three main categories of derivative instruments: forwards, options, and swaps. In this article, we focus on options, and more specifically on call options and put options, in order to understand how they are priced, what factors influence their value, and how they are used for hedging or speculation.</p>
<section id="what-is-an-option" class="level1">
<h1>What is an option ?</h1>
<p>An option is a financial contract concluded between a buyer and a seller, which gives the buyer a right ,but not an obligation ,to buy or sell an underlying asset at a price fixed in advance (called the exercise price or strike), at a predetermined future date (in the case of a European option) or up to that date (in the case of an American option). It is important to emphasize that it is not an obligation, meaning that the buyer can decide not to apply the contract ,“exercise” in the jargon. However, the seller has the obligation to respect the contract if the buyer decides to exercise their right.</p>
<p>There are two main categories of options:</p>
<ul>
<li><p>A <strong>call option</strong> gives its holder the right to buy the underlying asset at a pre-agreed price. The seller (or writer) of the call has the obligation to sell the asset if the buyer decides to exercise their right. It is advantageous for the buyer to exercise their right only when the asset price is greater than the strike.</p></li>
<li><p>A <strong>put option</strong> gives its holder the right to sell the underlying asset at a predetermined price. The seller of the put has the obligation to buy the asset if the buyer decides to exercise their right. It is advantageous for the buyer to exercise their right only when the asset price is lower than the strike.</p></li>
</ul>
</section>
<section id="what-is-an-option-used-for" class="level1">
<h1>What is an option used for?</h1>
<p>An option can be used to hedge against risk, speculate, or exploit arbitrage opportunities. To better understand the uses of an option, we illustrate each case with a call option example.</p>
<section id="hedging" class="level4">
<h4 class="anchored" data-anchor-id="hedging">Hedging</h4>
<p>A French company needs to purchase oil in three months to supply its factories, but fears that the price per barrel may rise in the meantime. To protect itself, it buys a call option on oil with a strike price fixed today at $80.</p>
<ul>
<li>If in three months the barrel price rises to $95, the company exercises its call and buys at $80. Hence, the company has avoided the price increase.</li>
<li>If the price stays at $75, it does not exercise the option and buys directly on the market, meaning the company has only lost the option premium.</li>
</ul>
</section>
<section id="speculation" class="level4">
<h4 class="anchored" data-anchor-id="speculation">Speculation</h4>
<p>An investor anticipates a rise in a given stock, currently at $200. Rather than buying the stock outright, they buy a call with a strike of $210, which costs $8.</p>
<ul>
<li>If the stock rises to $230, they exercise the option: they buy at $210 what is worth $230. This represents a gain of $20 for an initial outlay of only $8, illustrating the leverage effect of options.</li>
<li>If the stock stays below $210, they do not exercise and only lose the $8 premium.</li>
</ul>
<p>The option allows one to benefit from an anticipated rise without investing the full amount, while capping the maximum loss at the premium paid.</p>
</section>
<section id="arbitrage" class="level4">
<h4 class="anchored" data-anchor-id="arbitrage">Arbitrage</h4>
<p>Consider a stock worth €105, with a call and a put both struck at €100, priced at €12 and €6 respectively, and a zero risk-free rate. The call–put parity states that:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCall%7D%20-%20%5Ctext%7BPut%7D%20=%20S%20-%20K%0A"></p>
<p>In our case, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCall%7D%20-%20%5Ctext%7BPut%7D%20=%2012%20-%206%20=%206">, but <img src="https://latex.codecogs.com/png.latex?S%20-%20K%20=%20105%20-%20100%20=%205">. There is a €1 discrepancy: the call is overpriced. An investor can therefore build the following arbitrage portfolio today:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Action</th>
<th>Cash flow today</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Sell the call</td>
<td>+€12</td>
</tr>
<tr class="even">
<td>Buy the put</td>
<td>−€6</td>
</tr>
<tr class="odd">
<td>Buy the stock</td>
<td>−€105</td>
</tr>
<tr class="even">
<td>Borrow €100 at zero rate</td>
<td>+€100</td>
</tr>
<tr class="odd">
<td><strong>Net</strong></td>
<td><strong>+€1</strong></td>
</tr>
</tbody>
</table>
<p>At maturity, the stock is delivered against the strike of €100, repaying the loan exactly, and the portfolio closes flat regardless of the final stock price. The investor locks in a risk-free gain of €1 today ,a textbook arbitrage.</p>
</section>
</section>
<section id="how-do-we-price-an-option" class="level1">
<h1>How do we price an option?</h1>
<p>To price an option, i.e.&nbsp;assign it a value today, we first determine what is called the <strong>payoff</strong> ,the cash flow received by the buyer at maturity <img src="https://latex.codecogs.com/png.latex?T">.</p>
<p>In the case of a call option, when the price of the underlying <img src="https://latex.codecogs.com/png.latex?S_T"> exceeds the strike <img src="https://latex.codecogs.com/png.latex?K">, the buyer exercises their right and receives <img src="https://latex.codecogs.com/png.latex?(S_T%20-%20K)">. Otherwise (<img src="https://latex.codecogs.com/png.latex?S_T%20%5Cleq%20K">), the buyer does not exercise and receives nothing. The payoff is therefore:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D_%7B%5Ctext%7BCall%7D%7D%20=%20%5Cmax(S_T%20-%20K,%5C%200).%0A"></p>
<p>Symmetrically, the payoff of a put option is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D_%7B%5Ctext%7BPut%7D%7D%20=%20%5Cmax(K%20-%20S_T,%5C%200).%0A"></p>
<p>Note that in both cases the payoff is always non-negative ,the buyer can never be forced to take a loss, which is precisely what distinguishes an option from a forward contract.</p>
<p>From the seller’s perspective, writing such a contract implies bearing a potentially unlimited loss. The seller will therefore only agree to enter the transaction in exchange for a <strong>premium</strong> ,the price of the option paid upfront by the buyer.</p>
<p>To determine this premium, one must compute the present value of the expected future payoff. However, this expectation is not taken under the real-world probability measure, but under the <strong>risk-neutral measure</strong> <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D"> ,the probability measure under which all assets grow at the risk-free rate <img src="https://latex.codecogs.com/png.latex?r">, ensuring the absence of arbitrage:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPi_%7B%5Ctext%7BCall%7D%7D(t)%20=%20e%5E%7B-r(T-t)%7D%5C,%20%5Cmathbb%7BE%7D%5E%7B%5Cmathbb%7BQ%7D%7D%5C!%5Cleft%5B%5Cmax(S_T%20-%20K,%5C%200)%5C%20%5Cmiddle%7C%5C%20%5Cmathcal%7BF%7D_t%5Cright%5D.%0A"></p>
<p>In order to compute this expectation, it is necessary to know the law of <img src="https://latex.codecogs.com/png.latex?S_T">. A famous model used for <img src="https://latex.codecogs.com/png.latex?S_t"> is the so-called Black-Scholes model.</p>
<section id="black-scholes-model" class="level2">
<h2 class="anchored" data-anchor-id="black-scholes-model">Black scholes model</h2>
<p>The Black-Scholes model <span class="citation" data-cites="black1973pricing">(Black and Scholes 1973)</span> was published in 1973 by Fischer Black and Myron Scholes to provide a theoretical valuation formula for options. Under this model, the underlying asset is assumed to follow a geometric Brownian motion:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20%5Cmu%20S_t%5C,%20dt%20+%20%5Csigma%20S_t%5C,%20dW_t,%0A"></p>
<p>under the historical probability measure <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D">, where <img src="https://latex.codecogs.com/png.latex?%5Cmu%20%5Cin%20%5Cmathbb%7BR%7D"> is the drift and <img src="https://latex.codecogs.com/png.latex?%5Csigma%20%3E%200"> is the constant volatility.</p>
<p>By Girsanov’s theorem, there exists a risk-neutral probability measure <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D"> under which the discounted asset price is a martingale. The change of measure is characterized by the market price of risk <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%20%5Cfrac%7B%5Cmu%20-%20r%7D%7B%5Csigma%7D">, and under <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D"> the dynamics become:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20r%20S_t%5C,%20dt%20+%20%5Csigma%20S_t%5C,%20d%5Ctilde%7BW%7D_t,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BW%7D_t%20=%20W_t%20+%20%5Clambda%20t"> is a Brownian motion under <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D">, and <img src="https://latex.codecogs.com/png.latex?r"> is the risk-free rate.</p>
<p>This means that under <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D">, the stock grows on average at the risk-free rate <img src="https://latex.codecogs.com/png.latex?r"> — not because investors are assumed to be risk-neutral, but because the change of measure absorbs the risk premium, making discounted prices martingales for pricing purposes. The remaining <img src="https://latex.codecogs.com/png.latex?%5Csigma%20S_t%5C,%20d%5Ctilde%7BW%7D_t"> term captures the random fluctuations of the stock price.</p>
<p>By applying Itô’s formula to <img src="https://latex.codecogs.com/png.latex?%5Cln%20S_t">, we obtain an analytical expression for <img src="https://latex.codecogs.com/png.latex?S_t">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AS_t%20=%20S_0%20%5Cexp%5C!%5Cleft(%20%5Cleft(r%20-%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%5Cright)t%20+%20%5Csigma%20%5Ctilde%7BW%7D_t%20%5Cright).%0A"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BW%7D_t"> is a Brownian motion under <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D">, it follows a normal distribution <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,%20t)">. Hence, <img src="https://latex.codecogs.com/png.latex?%5Cln%20S_t"> is normally distributed:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cln%20S_t%20%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cleft(%20%5Cln%20S_0%20+%20%5Cleft(r%20-%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%5Cright)t,%5C%20%5Csigma%5E2%20t%20%5Cright),%0A"></p>
<p>and <img src="https://latex.codecogs.com/png.latex?S_t"> follows a log-normal distribution.</p>
<p>Knowing this, the price of a call option can be computed explicitly :</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5CPi_%7B%5Ctext%7BCall%7D%7D(t)%20&amp;=%20e%5E%7B-r(T-t)%7D%5C,%20%5Cmathbb%7BE%7D%5E%7B%5Cmathbb%7BQ%7D%7D%5C!%5Cleft%5B%5Cmax(S_T%20-%20K,%5C%200)%5C%20%5Cmiddle%7C%5C%20%5Cmathcal%7BF%7D_t%5Cright%5D%20%5C%5C%0A&amp;=%20S_t%5C,%20%5CPhi(d_+)%20-%20K%20e%5E%7B-r%5Ctau%7D%5C,%20%5CPhi(d_-),%0A%5Cend%7Balign*%7D"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5CPhi"> is the cumulative distribution function of the standard normal distribution, and:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad_%5Cpm%20=%20%5Cfrac%7B%5Cln(S_t%20/%20K)%20+%20%5Cleft(r%20%5Cpm%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%5Cright)%5Ctau%7D%7B%5Csigma%5Csqrt%7B%5Ctau%7D%7D,%0A"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Good to know
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5CPhi(d_-)"> can be interpreted as the risk-neutral probability that the option expires in the money, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D(S_T%20%3E%20K%20%5Cmid%20%5Cmathcal%7BF%7D_t)">.</li>
<li><img src="https://latex.codecogs.com/png.latex?S_t%5C,%5CPhi(d_+)"> represents the present value of receiving the stock conditionally on exercising the option.</li>
<li><img src="https://latex.codecogs.com/png.latex?Ke%5E%7B-r%5Ctau%7D%5CPhi(d_-)"> represents the present value of paying the strike conditionally on exercising the option.</li>
</ul>
</div>
</div>



</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-black1973pricing" class="csl-entry">
Black, Fischer, and Myron Scholes. 1973. <span>“The Pricing of Options and Corporate Liabilities.”</span> <em>Journal of Political Economy</em> 81 (3): 637–54.
</div>
</div></section></div> ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>options</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/derivatives/03_options_made_simple.html</guid>
  <pubDate>Sat, 02 May 2026 22:00:00 GMT</pubDate>
  <media:content url="https://cheryl-kdio.github.io/posts/m2mo/derivatives/featured.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Interest rates made simple</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/yield_curves/interest_rates_made_simple.html</link>
  <description><![CDATA[ 




<section id="interest-rates-bonds-xx-year-treasury-yield-what-does-all-that-mean" class="level2">
<h2 class="anchored" data-anchor-id="interest-rates-bonds-xx-year-treasury-yield-what-does-all-that-mean">Interest rates, bonds, XX-year treasury yield — what does all that mean?</h2>
<p>Interest rates are key indicators that reveal a great deal about what is happening in the world. For example, during the 2008 financial crisis, interest rates rose significantly. In contrast, during the COVID-19 crisis in 2020, we observed a drastic decline in interest rates, with some even turning negative. More recently, during tensions between Iran and the United States, movements in 10-year bond yields raised concerns about potential inflation.</p>
<p>Until recently, you may have been unfamiliar with terms such as “bonds,” “XX-year bond yields,” and “interest rates,” as well as their impact on the economy. If so, you are in the right place. In this article, we aim to demystify the concept of interest rates.</p>
<p>According to the concept of time value of money, a dollar today is worth more than a dollar tomorrow. This can be explained by different mechanisms. For example, since the future is uncertain, a dollar promised tomorrow carries more risk than a dollar received today. Hence, what can justify lending money — and thus forgoing its immediate use? Interest rates.</p>
<p>An interest rate can be seen as the cost of borrowing or the return on lending. For example, suppose a counterparty A lends money today to B, who must reimburse it in five days. To compensate for the renunciation of liquidity now, B reimburses A, at what we call the maturity date, the initial amount plus a fraction of the sum borrowed. This fraction is the interest rate.</p>
<p>Another instrument illustrates the same mechanism: the zero-coupon bond. Here, rather than specifying an interest rate explicitly, the compensation for waiting is embedded in the price. A zero-coupon bond is a contract that guarantees its holder one dollar at maturity date <img src="https://latex.codecogs.com/png.latex?T"> — with no intermediate payments. Since that dollar is received in the future, the buyer pays less than one dollar for it today. The difference between the price paid and the dollar received at <img src="https://latex.codecogs.com/png.latex?T"> is precisely what compensates the lender for the time and risk of waiting.</p>
</section>
<section id="the-link-between-bonds-and-interest-rates" class="level2">
<h2 class="anchored" data-anchor-id="the-link-between-bonds-and-interest-rates">The link between bonds and interest rates</h2>
<p>The link between bonds and interest rates is quite simple. In order to understand this, let’s consider two investment strategies that should, in a no-arbitrage world, yield the same return.</p>
<p>To formalize the example, let’s denote <img src="https://latex.codecogs.com/png.latex?r(t,T)"> the simple interest rate fixed at time <img src="https://latex.codecogs.com/png.latex?t"> and applying until <img src="https://latex.codecogs.com/png.latex?T">. A sum <img src="https://latex.codecogs.com/png.latex?X"> invested at time <img src="https://latex.codecogs.com/png.latex?t"> will therefore be worth <img src="https://latex.codecogs.com/png.latex?X%20+%20X%20%5Ccdot%20r(t,T)%20%5Ccdot%20(T-t)"> at time <img src="https://latex.codecogs.com/png.latex?T">.</p>
<p>Now consider a zero-coupon bond: we pay <img src="https://latex.codecogs.com/png.latex?B(t,T)"> at time <img src="https://latex.codecogs.com/png.latex?t"> to receive <img src="https://latex.codecogs.com/png.latex?1"> at maturity <img src="https://latex.codecogs.com/png.latex?T">. Alternatively, we can invest the same amount <img src="https://latex.codecogs.com/png.latex?B(t,T)"> in a money market account that pays the simple spot rate <img src="https://latex.codecogs.com/png.latex?r(t,T)">. At maturity <img src="https://latex.codecogs.com/png.latex?T">, this second strategy yields <img src="https://latex.codecogs.com/png.latex?B(t,T)%20%5Ccdot%20(1%20+%20(T-t)%20%5Ccdot%20r(t,T))">.</p>
<p>Since we are in a no-arbitrage framework, both strategies must produce the same payoff at maturity <img src="https://latex.codecogs.com/png.latex?T">, which gives:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A1%20=%20B(t,T)%20%5Ccdot%20(1%20+%20(T-t)%20%5Ccdot%20r(t,T))%20%5Cquad%20%5CLongleftrightarrow%20%5Cquad%20r(t,T)%20=%20%5Cfrac%7B1%7D%7BT-t%7D%20%5Cleft(%20%5Cfrac%7B1%7D%7BB(t,T)%7D%20-%201%20%5Cright).%0A"></p>
<p>In practice, continuous compounding is generally preferred. To emphasize this change in convention, we will denote <img src="https://latex.codecogs.com/png.latex?R(t,T)"> the continuously compounded rate, as opposed to the simple rate <img src="https://latex.codecogs.com/png.latex?r(t,T)">. Writing <img src="https://latex.codecogs.com/png.latex?B(t,T)%20=%20e%5E%7B-(T-t)%20R(t,T)%7D"> and identifying with the expression above, we obtain the following equivalent relationship:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A1%20=%20B(t,T)%20%5Ccdot%20e%5E%7B(T-t)%20R(t,T)%7D%20%5Cquad%20%5CLongleftrightarrow%20%5Cquad%20R(t,T)%20=%20-%5Cfrac%7B1%7D%7BT-t%7D%20%5Cln%20B(t,T).%0A"></p>
<p>As one can see, there is an inversely proportional relationship between the interest rate and the zero-coupon bond price. Under normal conditions, we tend to observe an increasing term structure of interest rates, meaning that lenders are willing to lend their money over the long term only if they receive a higher rate of return as compensation for time and risk. Consequently, the zero-coupon bond price curve will be decreasing and convex, meaning that bond prices decline as maturity increases, but at a decreasing rate.</p>
<p>Since both <img src="https://latex.codecogs.com/png.latex?r(t,T)"> and <img src="https://latex.codecogs.com/png.latex?B(t,T)"> depend on <img src="https://latex.codecogs.com/png.latex?t"> and <img src="https://latex.codecogs.com/png.latex?T">, in order to obtain a curve one needs to fix one of the two parameters. The most common convention is to fix the observation date <img src="https://latex.codecogs.com/png.latex?t"> and let the maturity <img src="https://latex.codecogs.com/png.latex?T"> vary — this gives rise to what is called the <strong>yield curve</strong> (or term structure of interest rates), which represents the interest rate as a function of maturity <span class="citation" data-cites="ecbyieldcurve">(European Central Bank, n.d.b)</span>.</p>
<p>Conversely, fixing <img src="https://latex.codecogs.com/png.latex?T"> and varying <img src="https://latex.codecogs.com/png.latex?t"> gives the time evolution of the rate for a given maturity, giving rise to what is called a <img src="https://latex.codecogs.com/png.latex?T">-year bond rate curve <span class="citation" data-cites="ecbsr10y">(European Central Bank, n.d.a)</span>. It is called the <img src="https://latex.codecogs.com/png.latex?T">-year Treasury yield when the bond used as reference is a US government bond (issued by the US Department of the Treasury).</p>
</section>
<section id="why-is-it-important-to-model-the-interest-rate-curve" class="level2">
<h2 class="anchored" data-anchor-id="why-is-it-important-to-model-the-interest-rate-curve">Why is it important to model the interest rate curve?</h2>
<p>It is important to model the interest rate curve because it is a fundamental input in almost every area of finance. It allows us to price and value a wide range of financial instruments — from simple bonds to complex interest rate derivatives — by providing the discount rates needed to bring future cash flows back to their present value. Knowing that 1 euro today will be worth <img src="https://latex.codecogs.com/png.latex?1%20+%20r%20%5Ccdot%20T"> in <img src="https://latex.codecogs.com/png.latex?T"> years, we can derive the present value of 1 euro received at time <img src="https://latex.codecogs.com/png.latex?T">, which is simply <img src="https://latex.codecogs.com/png.latex?1/(1%20+%20r%20%5Ccdot%20T)"> today. One of the famous model used for interest rate is the vasicek model for the short rate process <img src="https://latex.codecogs.com/png.latex?r_t">, which is basically the interest rate <img src="https://latex.codecogs.com/png.latex?r(t,%20t+dt)"> applied over an infinitesimally small period of time <img src="https://latex.codecogs.com/png.latex?dt">.</p>
<p>Depending on the conditions of the economy, we will observe various types of interest rate curves:</p>
<ul>
<li><p><strong>Normal curve</strong>: the yield curve is upward sloping, meaning long-term rates are higher than short-term rates. This reflects the typical expectation that lenders demand higher compensation for tying up their money over longer periods, and that the economy is in a healthy growth regime.</p></li>
<li><p><strong>Inverted curve</strong>: short-term rates exceed long-term rates, producing a downward sloping curve. This is often interpreted as a signal that markets expect economic slowdown or recession ahead, as investors anticipate that central banks will cut rates in the future and therefore want higher compensation now.</p></li>
<li><p><strong>Steepening curve</strong>: the difference between long-term and short-term rates widens, meaning that investors demand higher compensation for long-term lending. This typically occurs when markets expect stronger growth or higher inflation ahead, causing long-term rates to rise faster than short-term rates.</p></li>
<li><p><strong>Flattening curve</strong>: the difference between long-term and short-term rates narrows. This often signals that the market expects a slowdown, typically occurring when central banks raise short-term rates aggressively to cool the economy while long-term rates remain relatively stable.</p></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/yield_curves/covers/yield_curves.png" class="img-fluid figure-img"></p>
<figcaption>Inversion, Steepening and Flattening of the Yield Curve</figcaption>
</figure>
</div>
</section>
<section id="references" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-ecbsr10y" class="csl-entry">
European Central Bank. n.d.a. <span>“10-Year Spot Rate for Euro Area.”</span> <a href="https://data.ecb.europa.eu/data/datasets/YC/YC.B.U2.EUR.4F.G_N_A.SV_C_YM.SR_10Y">https://data.ecb.europa.eu/data/datasets/YC/YC.B.U2.EUR.4F.G_N_A.SV_C_YM.SR_10Y</a>.
</div>
<div id="ref-ecbyieldcurve" class="csl-entry">
———. n.d.b. <span>“Euro Area Yield Curves.”</span> <a href="https://www.ecb.europa.eu/stats/financial_markets_and_interest_rates/euro_area_yield_curves/html/index.en.html">https://www.ecb.europa.eu/stats/financial_markets_and_interest_rates/euro_area_yield_curves/html/index.en.html</a>.
</div>
</div>


</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>rates</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/yield_curves/interest_rates_made_simple.html</guid>
  <pubDate>Sat, 02 May 2026 22:00:00 GMT</pubDate>
  <media:content url="https://cheryl-kdio.github.io/posts/m2mo/yield_curves/covers/yield_curves.png" medium="image" type="image/png" height="107" width="144"/>
</item>
<item>
  <title>Interest rates market - Key instruments</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/yield_curves/00_intro_yield_curve.html</link>
  <description><![CDATA[ 




<p>The interest rate market is one of the largest and most liquid segments of the financial markets.<br>
It encompasses a wide range of instruments, including government bonds, corporate bonds, interest rate swaps, and options on interest rates. The modeling of interest rates is crucial for pricing and managing the risks associated with these instruments. To understand the dynamics of interest rates, we first introduce the different types of interest rates that are used in creating and pricing interest rate derivatives, as well as the concept of the yield curve, which is a fundamental tool for analyzing the term structure of interest rates. Moreover, we introduce several interest rate derivatives currently traded in the market.</p>
<section id="different-types-of-interest-rates" class="level1">
<h1>Different types of interest rates</h1>
<p>An interest rate can be seen as the cost of borrowing or the return on lending money. For example, when a counterparty A borrows at time <img src="https://latex.codecogs.com/png.latex?t"> a certain amount of cash <img src="https://latex.codecogs.com/png.latex?N"> from a counterparty B with maturity <img src="https://latex.codecogs.com/png.latex?T"> and interest rate <img src="https://latex.codecogs.com/png.latex?r">. Then, at maturity <img src="https://latex.codecogs.com/png.latex?T">, A must pay back <img src="https://latex.codecogs.com/png.latex?N(1+r(T-t))%20=%20N(1+r%5Ctau)"> with <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%20T-t"> the time to maturity, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?N%20%5Cmapsto%20N(1+r%5Ctau)">. This is the case of a simple interest rate.</p>
<p>In practice, one can differentiate two types of interest rates:</p>
<ul>
<li><p>the simple interest rate. In continuous time, we write <img src="https://latex.codecogs.com/png.latex?N%20%5Cmapsto%20N%20e%5E%7Br%5Ctau%7D">.</p></li>
<li><p>the compound interest rate. At time <img src="https://latex.codecogs.com/png.latex?t">, A borrows an amount <img src="https://latex.codecogs.com/png.latex?N"> from B with maturity <img src="https://latex.codecogs.com/png.latex?T"> and compound interest rate <img src="https://latex.codecogs.com/png.latex?r">. Then at maturity <img src="https://latex.codecogs.com/png.latex?T">, A pays back <img src="https://latex.codecogs.com/png.latex?N(1+r)%5E%7B%5Ctau%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%20T-t">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?N%20%5Cmapsto%20N(1+r)%5E%7B%5Ctau%7D">.</p></li>
</ul>
<p>It is also important to distinguish between the different types of interest rates used in the market:</p>
<ul>
<li><p>the <strong>spot interest rate</strong> is the rate that applies to a loan or deposit that starts immediately and has maturity <img src="https://latex.codecogs.com/png.latex?T">.</p></li>
<li><p>the <strong>forward interest rate</strong> is the rate that applies to a loan or deposit that starts at a future date <img src="https://latex.codecogs.com/png.latex?T"> and has maturity <img src="https://latex.codecogs.com/png.latex?S">.</p></li>
</ul>
</section>
<section id="link-between-the-zero-coupon-bond-price-and-the-interest-rates" class="level1">
<h1>Link between the Zero-coupon bond price and the interest rates</h1>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
What is a zero-coupon bond?
</div>
</div>
<div class="callout-body-container callout-body">
<p>A zero-coupon bond is a debt security that does not pay periodic interest (coupons). Instead, it is issued at a discount to its face value and pays its full face value at maturity. If the face value is <img src="https://latex.codecogs.com/png.latex?1">, its price at time <img src="https://latex.codecogs.com/png.latex?t"> for maturity <img src="https://latex.codecogs.com/png.latex?T"> is denoted <img src="https://latex.codecogs.com/png.latex?B(t,T)">.</p>
</div>
</div>
<section id="zero-coupon-bond-price-and-simple-spot-interest-rate" class="level2">
<h2 class="anchored" data-anchor-id="zero-coupon-bond-price-and-simple-spot-interest-rate">Zero-coupon bond price and simple spot interest rate</h2>
<p>Assume we pay <img src="https://latex.codecogs.com/png.latex?B(t,T)"> at time <img src="https://latex.codecogs.com/png.latex?t"> to buy a zero-coupon bond that will pay <img src="https://latex.codecogs.com/png.latex?1"> at maturity <img src="https://latex.codecogs.com/png.latex?T">. Hence, <img src="https://latex.codecogs.com/png.latex?B(t,T)%20%5Cmapsto%201"> at maturity <img src="https://latex.codecogs.com/png.latex?T">. Alternatively, we can invest <img src="https://latex.codecogs.com/png.latex?B(t,T)"> in the money market account that pays the simple spot rate <img src="https://latex.codecogs.com/png.latex?L(t,T)">. At maturity <img src="https://latex.codecogs.com/png.latex?T">, we obtain <img src="https://latex.codecogs.com/png.latex?B(t,T)(1%20+%20(T-t)L(t,T))">. Since we are in a no-arbitrage framework, we must have the same payoff at maturity <img src="https://latex.codecogs.com/png.latex?T"> for both strategies, which implies <img src="https://latex.codecogs.com/png.latex?1%20=%20B(t,T)(1%20+%20(T-t)L(t,T))">. Hence, we deduce the following link between the zero-coupon bond price and the simple spot interest rate:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AL(t,T)%20=%20%5Cfrac%7B1%7D%7BT-t%7D%20%5Cleft(%20%5Cfrac%7B1%7D%7BB(t,T)%7D%20-%201%20%5Cright).%0A"></p>
<p>In continuous compounding, we have <img src="https://latex.codecogs.com/png.latex?1%20=%20B(t,T)%20e%5E%7B(T-t)%20R(t,T)%7D">, which gives :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR(t,T)%20=%20-%5Cfrac%7B1%7D%7BT-t%7D%20%5Cln(B(t,T)).%0A"></p>
<p>Therefore, the short-term interest rate is defined as <img src="https://latex.codecogs.com/png.latex?%5Clim_%7BT%20%5Cto%20t%7D%20L(t,T)%20=%20%5Clim_%7BT%20%5Cto%20t%7D%20R(t,T)%20=%20r_t">.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
The yield curve or the term structure of interest rates
</div>
</div>
<div class="callout-body-container callout-body">
<p>The yield curve is a graphical representation of the relationship between interest rates and maturities. It is the function <img src="https://latex.codecogs.com/png.latex?T%20%5Cmapsto%20L(t,T)"> for a given date <img src="https://latex.codecogs.com/png.latex?t">. For each date <img src="https://latex.codecogs.com/png.latex?t">, we can build a yield curve using market instruments (bonds, FRA, swaps, etc.) and use it to price interest rate derivatives.</p>
</div>
</div>
</section>
<section id="zero-coupon-bond-price-and-simple-forward-interest-rate" class="level2">
<h2 class="anchored" data-anchor-id="zero-coupon-bond-price-and-simple-forward-interest-rate">Zero-coupon bond price and simple forward interest rate</h2>
<p>The forward rate is the rate that can be locked at time <img src="https://latex.codecogs.com/png.latex?t"> for a loan starting at date <img src="https://latex.codecogs.com/png.latex?T"> and ending at date <img src="https://latex.codecogs.com/png.latex?S">. At time <img src="https://latex.codecogs.com/png.latex?t">, one can invest in a zero-coupon bond maturing at <img src="https://latex.codecogs.com/png.latex?S"> and obtain <img src="https://latex.codecogs.com/png.latex?B(t,S)%20%5Cmapsto%201"> at maturity <img src="https://latex.codecogs.com/png.latex?S">. Alternatively, one can invest in a zero-coupon bond maturing at <img src="https://latex.codecogs.com/png.latex?T">, obtain <img src="https://latex.codecogs.com/png.latex?B(t,T)%20%5Cmapsto%201"> at time <img src="https://latex.codecogs.com/png.latex?T">, and then reinvest the proceeds from <img src="https://latex.codecogs.com/png.latex?T"> to <img src="https://latex.codecogs.com/png.latex?S"> at the forward rate <img src="https://latex.codecogs.com/png.latex?L(t;T,S)">. At maturity <img src="https://latex.codecogs.com/png.latex?S">, this yields <img src="https://latex.codecogs.com/png.latex?1%20+%20(S-T)L(t;T,S)">. Under no-arbitrage, we must have <img src="https://latex.codecogs.com/png.latex?B(t,S)%20=%20B(t,T)%5Cfrac%7B1%7D%7B1%20+%20(S-T)L(t;T,S)%7D">. Hence, <img src="https://latex.codecogs.com/png.latex?%0AL(t;%20T,S)%20=%20%5Cfrac%7B1%7D%7BS-T%7D%20%5Cleft(%20%5Cfrac%7BB(t,T)%7D%7BB(t,S)%7D%20-%201%20%5Cright).%0A"></p>
<p>The instantaneous forward rate satisfies <img src="https://latex.codecogs.com/png.latex?f(t,T)%20=%20-%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20T%7D%20%5Cln%20B(t,T)">. Moreover,<img src="https://latex.codecogs.com/png.latex?B(t,T)%20=%20e%5E%7B-%5Cint_t%5ET%20f(t,u)%20du%7D."></p>
</section>
</section>
<section id="rates-derivatives" class="level1">
<h1>Rates derivatives</h1>
<section id="fra" class="level2">
<h2 class="anchored" data-anchor-id="fra">FRA</h2>
<p>A FRA (Forward Rate Agreement) is a contract that fixes at time <img src="https://latex.codecogs.com/png.latex?t"> an interest rate <img src="https://latex.codecogs.com/png.latex?K"> for the period <img src="https://latex.codecogs.com/png.latex?%5BT,S%5D"> on a nominal <img src="https://latex.codecogs.com/png.latex?N">. Its payoff at time <img src="https://latex.codecogs.com/png.latex?S"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN(S-T)(L(T,S)%20-%20K).%0A"></p>
<p>Its price at time <img src="https://latex.codecogs.com/png.latex?t"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPi%5E%7B%5Ctext%7BFRA%7D%7D(t,T,S,K,N)%20=%20N%20B(t,S)%20(S-T)%20(L(t;T,S)%20-%20K).%0A"></p>
</section>
<section id="swap" class="level2">
<h2 class="anchored" data-anchor-id="swap">Swap</h2>
<p>A swap exchanges fixed and floating interest rates between two counterparties over dates <img src="https://latex.codecogs.com/png.latex?0%20%5Cle%20T_0%20%3C%20T_1%20%3C%20%5Cdots%20%3C%20T_n"> on a nominal <img src="https://latex.codecogs.com/png.latex?N">. The fixed leg pays <img src="https://latex.codecogs.com/png.latex?N%20R%20(T_i%20-%20T_%7Bi-1%7D)">. The floating leg pays <img src="https://latex.codecogs.com/png.latex?N%20L(T_%7Bi-1%7D,T_i)(T_i%20-%20T_%7Bi-1%7D)">. The swap can be seen as a sum of FRA. Its price is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5CPi%5E%7B%5Ctext%7BSW%7D%7D%20(t,T_0,T_n,R,N)%0A&amp;=%20%5Csum_%7Bi=1%7D%5En%20%5CPi%5E%7B%5Ctext%7BFRA%7D%7D%20(t,T_%7Bi-1%7D,T_i,R,N)%20%5C%5C%0A&amp;=%20N%20%5Cleft(B(t,T_0)%20-%20B(t,T_n)%20-%20%5Csum_%7Bi=1%7D%5En%20B(t,T_i)(T_i%20-%20T_%7Bi-1%7D)R%20%5Cright)%0A%5Cend%7Balign%7D%0A"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Swap rate
</div>
</div>
<div class="callout-body-container callout-body">
<p>The swap rate <img src="https://latex.codecogs.com/png.latex?S(t,T_0,T_n)"> is the rate that makes the swap price equal to zero:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AS(t,T_0,T_n)%20=%20%5Cfrac%7BB(t,T_0)%20-%20B(t,T_n)%7D%7B%5Csum_%7Bi=1%7D%5En%20B(t,T_i)(T_i%20-%20T_%7Bi-1%7D)%7D.%0A"></p>
</div>
</div>
</section>
<section id="capfloor" class="level2">
<h2 class="anchored" data-anchor-id="capfloor">Cap/Floor</h2>
<p>A caplet (resp. floorlet) is an option that gives the right to the buyer to receive (resp. pay) the excess of the floating rate <img src="https://latex.codecogs.com/png.latex?L(T_%7Bi-1%7D,T_i)"> over a strike <img src="https://latex.codecogs.com/png.latex?K"> on a nominal <img src="https://latex.codecogs.com/png.latex?N">. The caplet payoff is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN%20(T_i%20-%20T_%7Bi-1%7D)%20%5Cmax(L(T_%7Bi-1%7D,T_i)%20-%20K,%200).%0A"></p>
<p>The floorlet payoff is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN%20(T_i%20-%20T_%7Bi-1%7D)%20%5Cmax(K%20-%20L(T_%7Bi-1%7D,T_i),%200).%0A"></p>
<p>Hence a cap is a chain of caplets for <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cdots,%20n">, and a floor is a chain of floorlets for <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cdots,%20n">.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Caplet/Floorlet and Cap/Floor parity
</div>
</div>
<div class="callout-body-container callout-body">
<p>There exists a parity called the <strong>caplet–floorlet parity</strong>.</p>
<p>It means that a long caplet and a short floorlet with the same strike <img src="https://latex.codecogs.com/png.latex?K"> and maturity structure is equivalent to entering a payer interest rate swap with fixed rate <img src="https://latex.codecogs.com/png.latex?K">.</p>
<p>Mathematically,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5CPi%5E%7B%5Ctext%7BCaplet%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1)-%5CPi%5E%7B%5Ctext%7BFloorlet%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1)%20&amp;=%5CPi%5E%7B%5Ctext%7BFRA%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1).%5C%5C%0A%5CLeftrightarrow%0A%5CPi%5E%7B%5Ctext%7BCap%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1)-%5CPi%5E%7B%5Ctext%7BFloor%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1)%20&amp;=%5CPi%5E%7B%5Ctext%7BSW%7D%7D%20(t,T,T%20+%20%5Cdelta,K,1).%0A%5Cend%7Balign%7D%0A"></p>
</div>
</div>
</section>
<section id="swaption" class="level2">
<h2 class="anchored" data-anchor-id="swaption">Swaption</h2>
<p>A swaption is an option based on interest rate swap of maturity <img src="https://latex.codecogs.com/png.latex?T"> and fixed rate <img src="https://latex.codecogs.com/png.latex?R">. Assuming <img src="https://latex.codecogs.com/png.latex?T=T_0"> is the starting date of the swap, the payoff of a swaption is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A(%20%5CPi%5E%7B%5Ctext%7BSW%7D%7D%20(T_0,T_0,T_n,R,N)%20)_+%20&amp;=%20N(1%20-%5Csum_%7Bi=1%7D%5En%20c_i%20B(T_0,T_i))_+%20(1)%5C%5C%0A&amp;=%20N%20%5Csum_%7Bi=1%7D%5En%20B(T_0,T_i)%20(T_i,T_%7Bi-1%7D)%20(%20S(T_0,T_0,T_n)%20-%20R)_+%20.%0A%5Cend%7Balign%7D%0A"></p>
<p>Thus, the swaption can be seen as a put option on a classical bond with coupon <img src="https://latex.codecogs.com/png.latex?c_i"></p>
</section>
</section>
<section id="take-away" class="level1">
<h1>Take away</h1>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Take away
</div>
</div>
<div class="callout-body-container callout-body">
<p>To summarize everything covered in this article, a visual recap is provided below: <img src="https://cheryl-kdio.github.io/posts/m2mo/yield_curves/covers/summary_rates_types.png" class="img-fluid" alt="Summary poster"></p>
</div>
</div>


</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>rates</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/yield_curves/00_intro_yield_curve.html</guid>
  <pubDate>Fri, 13 Feb 2026 23:00:00 GMT</pubDate>
  <media:content url="https://cheryl-kdio.github.io/posts/m2mo/yield_curves/covers/rates_types_cover.png" medium="image" type="image/png" height="61" width="144"/>
</item>
<item>
  <title>Mathematical foundations of financial market modeling</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/derivatives/01_intro_market.html</link>
  <description><![CDATA[ 




<p>A financial market is an institutional framework in which financial instruments are issued and exchanged. In the primary market, newly created securities such as shares or bonds are sold for the first time by firms or governments to investors. The objective of this article is to introduce the economic role of financial markets and to present the fundamental structural properties required for coherent financial modeling.</p>
<p>For theoretical clarity, we adopt a discrete-time framework. Time is indexed by <img src="https://latex.codecogs.com/png.latex?%0At%20%5Cin%20%5C%7B0,1,%5Cdots,T%5C%7D,%0A"> where <img src="https://latex.codecogs.com/png.latex?T"> denotes a fixed time horizon.</p>
<section id="financial-markets-and-traded-assets" class="level1">
<h1>Financial markets and traded assets</h1>
<p>Financial markets encompass several types of instruments. The equity market consists of ownership claims on firms, such as common or preferred shares. Firms raise capital by issuing equity securities, and these securities are subsequently traded on organized exchanges such as Euronext Paris or the New York Stock Exchange. Prices are determined by supply and demand and reflect expectations regarding firm performance and economic conditions.</p>
<p>The bond market consists of debt instruments issued by governments or corporations. A bond is a contract specifying that the issuer will repay a principal amount at maturity and pay periodic interest, referred to as coupons. Bonds provide predictable income streams and are fundamental instruments for public and private financing.</p>
<p>The derivatives market includes financial instruments whose value depends on an underlying asset. Typical examples are options, forwards, futures, and swaps. Derivatives are used for risk management, speculative positioning, and arbitrage strategies across markets.</p>
<p>The primary market enables capital allocation within the economy. Firms obtain financing for investment and expansion, while investors allocate resources in expectation of future returns. Financial markets also facilitate risk transfer. For example, firms exposed to commodity price risk may use derivative contracts to hedge adverse fluctuations.</p>
<section id="structural-properties-of-a-financial-market" class="level2">
<h2 class="anchored" data-anchor-id="structural-properties-of-a-financial-market">Structural properties of a financial market</h2>
<p>For a financial market to function efficiently, several properties are desirable. Liquidity ensures that assets can be traded without significant price impact. Transparency guarantees that relevant information is accessible to participants. Fairness implies equal access to market information. Efficiency requires that prices reflect available information and that arbitrage opportunities do not persist.</p>
<p>The absence of arbitrage is a central assumption in modern financial mathematics and underlies pricing theory.</p>
</section>
</section>
<section id="mathematical-framework" class="level1">
<h1>Mathematical Framework</h1>
<p>We consider a financial market composed of one risk-free asset and <img src="https://latex.codecogs.com/png.latex?q"> risky assets.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?S_t%5E0"> denote the price of the risk-free asset. In discrete time, we assume it evolves deterministically according to <img src="https://latex.codecogs.com/png.latex?%0AS_%7Bt+1%7D%5E0%20=%20(1%20+%20r_t)%20S_t%5E0,%0A"> where <img src="https://latex.codecogs.com/png.latex?r_t"> denotes the short-term interest rate.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?S_t%5Ei"> for <img src="https://latex.codecogs.com/png.latex?i=1,%5Cdots,q"> denote the prices of risky assets. Their evolution is stochastic and will be specified through an appropriate probabilistic model.</p>
<section id="trading-strategies-and-wealth-process" class="level2">
<h2 class="anchored" data-anchor-id="trading-strategies-and-wealth-process">Trading strategies and wealth process</h2>
<p>A trading strategy is a predictable process <img src="https://latex.codecogs.com/png.latex?%0A%5Cdelta_t%20=%20(%5Cdelta_t%5E0,%20%5Cdelta_t%5E1,%20%5Cdots,%20%5Cdelta_t%5Eq),%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cdelta_t%5Ei"> represents the number of units of asset <img src="https://latex.codecogs.com/png.latex?i"> held at time <img src="https://latex.codecogs.com/png.latex?t">.</p>
<p>The associated wealth process is defined by <img src="https://latex.codecogs.com/png.latex?%0AV_t%20=%20%5Csum_%7Bi=0%7D%5Eq%20%5Cdelta_t%5Ei%20S_t%5Ei.%0A"></p>
<p>A strategy is said to be self-financing if changes in wealth are solely due to variations in asset prices. In discrete time, this condition reads <img src="https://latex.codecogs.com/png.latex?%0AV_%7Bt+1%7D%20-%20V_t%20=%20%5Cdelta_t%5E0%20%5C,%20dS_t%5E0%20%20+%20%5Csum_%7Bi=1%7D%5Eq%20%5Cdelta_t%5Ei%20(S_%7Bt+1%7D%5Ei%20-%20S_t%5Ei).%0A"> Equivalently, <img src="https://latex.codecogs.com/png.latex?%0AV_t%20=%20V_0%20+%20%5Cdelta_t%5E0%20%5C,%20dS_t%5E0%20%20+%20%5Csum_%7Bs=1%7D%5E%7Bt-1%7D%20%5Csum_%7Bi=0%7D%5Eq%20%5Cdelta_s%5Ei%20(S_%7Bs+1%7D%5Ei%20-%20S_s%5Ei).%0A"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Self financing condition in continuous time
</div>
</div>
<div class="callout-body-container callout-body">
<p>In continuous time, the self-financing condition is expressed as <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0AdV_t%20&amp;=%20%5Cdelta_t%5E0%20%5C,%20dS_t%5E0%20%20+%20%5Csum_%7Bi=1%7D%5Eq%20%5Cdelta_t%5Ei%20%5C,%20dS_t%5Ei.%20%5C%5C%0A%5CLeftrightarrow%0AV_t%20&amp;=%20V_0%20+%20%5Cdelta_t%5E0%20%5C,%20dS_t%5E0%20%20+%20%5Cint_1%5Et%20%5Csum_%7Bi=0%7D%5Eq%20%5Cdelta_s%5Ei%20%5C,%20dS_s%5Ei.%0A%5Cend%7Balign%7D%0A"></p>
<p>In the case where the risky assets give dividends, the self-financing condition must be modified to account for dividend payments. Let <img src="https://latex.codecogs.com/png.latex?D_t%5Ei"> denote the cumulative dividends paid by asset <img src="https://latex.codecogs.com/png.latex?i"> up to time <img src="https://latex.codecogs.com/png.latex?t">. The wealth process then satisfies : <img src="https://latex.codecogs.com/png.latex?%0AdV_t%20=%20%5Cdelta_t%5E0%20%5C,%20dS_t%5E0%20%20+%20%5Csum_%7Bi=1%7D%5Eq%20%5Cdelta_t%5Ei%20%5C,%20dS_t%5Ei%20+%20%5Csum_%7Bi=1%7D%5Eq%20%5Cdelta_t%5Ei%20%5C,%20dD_t%5Ei.%0A"></p>
<p>One can talk in term of discounted wealth process <img src="https://latex.codecogs.com/png.latex?%5Ctilde%20V_t%20=%20%5Cfrac%7BV_t%7D%7BS_t%5E0%7D">, which satisfies <img src="https://latex.codecogs.com/png.latex?%0Ad%5Ctilde%20V_t%20=%20%5Csum_%7Bi=1%7D%5Eq%20%5Cdelta_t%5Ei%20%5C,%20d%5Ctilde%20S_t%5Ei,%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%20S_t%5Ei%20=%20%5Cfrac%7BS_t%5Ei%7D%7BS_t%5E0%7D%20=%20%5Cbeta_t%20S_t%5Ei"> is the discounted price of the risky asset <img src="https://latex.codecogs.com/png.latex?i">.</p>
</div>
</div>
</section>
</section>
<section id="arbitrage" class="level1">
<h1>Arbitrage</h1>
<p>An arbitrage opportunity is defined as a self-financing strategy satisfying <img src="https://latex.codecogs.com/png.latex?%0AV_0%20=%200,%20%5Cqquad%20V_T%20%5Cge%200%20%5Ctext%7B%20almost%20surely%7D,%20%5Cqquad%20%5Cmathbb%7BP%7D(V_T%20%3E%200)%20%3E%200.%0A"> Such a strategy generates profit without risk or initial investment. Markets admitting arbitrage are economically inconsistent, and therefore we assume the market is arbitrage-free.</p>
<section id="risk-neutral-measure" class="level2">
<h2 class="anchored" data-anchor-id="risk-neutral-measure">Risk-neutral measure</h2>
<p>Under the absence of arbitrage, there exists an equivalent probability measure <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BQ%7D"> under which discounted asset prices are martingales. Let <img src="https://latex.codecogs.com/png.latex?%0A%5Ctilde%20S_t%5Ei%20=%20%5Cfrac%7BS_t%5Ei%7D%7BS_t%5E0%7D%0A"> denote the discounted price process. Then <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5E%7B%5Cmathcal%7BQ%7D%7D%5B%5Ctilde%20S_%7Bt+1%7D%5Ei%20%5Cmid%20%5Cmathcal%7BF%7D_t%5D%20=%20%5Ctilde%20S_t%5Ei.%0A"> This result allows derivative pricing via expectation: <img src="https://latex.codecogs.com/png.latex?%0A%5CPi_0%20=%20S_0%5E0%20%5C,%20%5Cmathbb%7BE%7D%5E%7B%5Cmathcal%7BQ%7D%7D%5Cleft%5B%5Cfrac%7B%5Cvarepsilon%7D%7BS_T%5E0%7D%5Cright%5D,%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> denotes the contingent claim payoff at maturity.</p>
</section>
<section id="market-completeness" class="level2">
<h2 class="anchored" data-anchor-id="market-completeness">Market completeness</h2>
<p>The market is said to be complete if every contingent claim <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> can be replicated by a self-financing strategy. In a complete market, the risk-neutral measure is unique and every derivative admits a unique arbitrage-free price. In an incomplete market, multiple risk-neutral measures may exist and perfect replication is not always possible, leading to pricing and hedging challenges.</p>
<p>As an illustration, the payoff of a European call option with strike <img src="https://latex.codecogs.com/png.latex?K"> and maturity <img src="https://latex.codecogs.com/png.latex?T"> is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Cvarepsilon%20=%20%5Cmax(S_T%20-%20K,%200).%0A"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Take away
</div>
</div>
<div class="callout-body-container callout-body">
<p>Financial markets play a fundamental role in capital allocation, risk transfer, and price discovery within the economy. From a mathematical perspective, their analysis relies on a structured framework built upon trading strategies, wealth dynamics, and probabilistic modeling.</p>
<p>The <strong>absence of arbitrage</strong> constitutes the cornerstone of modern financial theory. It guarantees the existence of a risk-neutral measure under which discounted asset prices behave as martingales, thereby providing a coherent foundation for derivative pricing.</p>
<p>Finally, the notion of <strong>market completeness</strong> determines whether contingent claims can be perfectly replicated and uniquely priced. These structural concepts form the basis of quantitative finance and underpin the development of more advanced pricing and hedging models.</p>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>derivatives</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/derivatives/01_intro_market.html</guid>
  <pubDate>Mon, 09 Feb 2026 23:00:00 GMT</pubDate>
  <media:content url="https://cheryl-kdio.github.io/posts/m2mo/derivatives/featured.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Finite difference Methods for American Options</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <dc:creator>Marilene Kougoum</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt.html</link>
  <description><![CDATA[ 




<p>Dans ce TP, on étudie des schémas numériques permettant d’approcher la solution de l’équation de Black-Scholes pour les options américaines. On considerera en particulier deux cas de payoff pour les options américaines :</p>
<ul>
<li><p><strong>payoff 1:</strong> le cas de put standard de payoff <img src="https://latex.codecogs.com/png.latex?%5Cphi_1(s)%20=%20(K-s)%5E+">, avec <img src="https://latex.codecogs.com/png.latex?K"> le prix d’exercice de l’option. Ce payoff est connu pour avoir de bonnes propriétés de régularité, i.e.&nbsp;il est continu et même lipschitzien, ce qui facilite l’analyse numérique.</p></li>
<li><p><strong>payoff 2:</strong> le cas d’un produit de payoff <img src="https://latex.codecogs.com/png.latex?%5Cphi_2(s)%20=%20K%20%5Cmathbf%7B1%7D_%7B%5Cfrac%7BK%7D%7B2%7D%20%5Cleq%20s%20%5Cleq%20K%20%7D">, qui correspond à une option de type “cash-or-nothing” (payoff en cash) avec barrière. Ce payoff est plus irrégulier, puisqu’il est discontinu, ce qui rend l’analyse numérique plus délicate.</p></li>
</ul>
<p>Dans les deux cas, l’EDP correspond à un système non linéaire d’équations aux dérivées partielles suivant : <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cmin%20%5Cleft(%20%5Cfrac%7B%5Cpartial%20v%7D%7B%5Cpartial%20t%7D%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2%20s%5E2%20%5Cfrac%7B%5Cpartial%5E2%20v%7D%7B%5Cpartial%20s%5E2%7D%20-%20r%20s%20%5Cfrac%7B%5Cpartial%20v%7D%7B%5Cpartial%20s%7D%20+%20r%20v,%20v%20-%20%5Cphi%20%5Cright)%20=%200,%20%5Cquad%20(t,s)%20%5Cin%20(0,T)%20%5Ctimes%20(S_%7Bmin%7D,%20S_%7Bmax%7D),%20%5C%5C%0Av(t,%20S_%7Bmin%7D)%20=%20v_l(t),%20%5Cquad%20t%20%5Cin%20(0,T),%20%5C%5C%0Av(t,%20S_%7Bmax%7D)%20=%20v_r(t)%20:=%200,%20%5Cquad%20t%20%5Cin%20(0,T),%20%5C%5C%0Av(0,s)%20=%20g(s),%20%5Cquad%20s%20%5Cin%20(S_%7Bmin%7D,%20S_%7Bmax%7D),%0A%5Cend%7Bcases%7D%0A"></p>
<p>Dans le cas du payoff 1, on a <img src="https://latex.codecogs.com/png.latex?v_l(t)%20:=%20%5Cphi(S_%7Bmin%7D)%20=%20K%20-%20S_%7Bmin%7D">, tandis que dans le cas du payoff 2, on a <img src="https://latex.codecogs.com/png.latex?v_l(t)%20:=%20%5Cphi(S_%7Bmin%7D)%20=%200">.</p>
<p>Ces schémas nous permettront d’obtenir une approximation numérique de la fonction de prix d’un put européen <img src="https://latex.codecogs.com/png.latex?v(t,s)">, avec <img src="https://latex.codecogs.com/png.latex?t%20%5Cin%20%5B0,T%5D"> et <img src="https://latex.codecogs.com/png.latex?s%20%5Cin%20%5BS_%7Bmin%7D,%20S_%7Bmax%7D%5D">.</p>
<p>Les schémas numériques permettant d’approcher cette EDP abordés ici sont : 1. le schéma d’Euler explicite 2. le schéma d’Euler implicite (et plus généralement pour tout schéma implicite pour les options américaines), on est conduit à un systeme non linéaire discret à résoudre à chaque pas de temps, qui à la forme d’un “problème d’obstacle”. 2. On s’intéressera à diverses méthodes numériques de résolution de ce système, telles que la méthode PSOR (projected successive over-relaxation), une méthode de type Newton.</p>
<section id="méthodes-numériques" class="level1">
<h1>Méthodes numériques</h1>
<p>Nous considérons la grille discrète suivante : <img src="https://latex.codecogs.com/png.latex?h%20=%20%5Cfrac%7BS_%7Bmax%7D%20-%20S_%7Bmin%7D%7D%7BJ%20+%201%7D"> et <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t%20=%20%5Cfrac%7BT%7D%7BN%7D">, avec <img src="https://latex.codecogs.com/png.latex?J"> et <img src="https://latex.codecogs.com/png.latex?N"> des entiers positifs, et : - <img src="https://latex.codecogs.com/png.latex?s_j%20=%20S_%7Bmin%7D%20+%20j%20h">, pour <img src="https://latex.codecogs.com/png.latex?j%20=%200,%20%5Cldots,%20J%20+%201">, - <img src="https://latex.codecogs.com/png.latex?t_n%20=%20n%20%5CDelta%20t">, pour <img src="https://latex.codecogs.com/png.latex?n%20=%200,%20%5Cldots,%20N">.</p>
<p>On cherche une approximation <img src="https://latex.codecogs.com/png.latex?U_j%5En%20%5Capprox%20v(t_n,%20s_j)"> pour <img src="https://latex.codecogs.com/png.latex?j%20=%201,%20%5Cldots,%20J"> et <img src="https://latex.codecogs.com/png.latex?n%20=%200,%20%5Cldots,%20N">. Les schémas aux différences finies nous permettront de discrétiser l’équation de Black-Scholes du prix d’un put américain sur cette grille.</p>
<section id="explicit-euler-scheme-or-euler-forward-scheme" class="level2">
<h2 class="anchored" data-anchor-id="explicit-euler-scheme-or-euler-forward-scheme">Explicit Euler Scheme or Euler Forward Scheme</h2>
<p>Le schéma d’Euler explicite est un schéma basé sur une discrétisation explicite en temps. La discrétisation de l’EDP est basée sur des approximations centrées. Dès lors, on approxime les dérivées partielles de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Amin(%5Cfrac%7BU_j%5E%7Bn+1%7D%20-%20U_j%5En%7D%7B%5CDelta%20t%7D%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2%20s_j%5E2%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%202%20U_j%5En%20+%20U_%7Bj-1%7D%5En%7D%7Bh%5E2%7D%20-%20r%20s_j%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%20U_%7Bj-1%7D%5En%7D%7B2%20h%7D%20+%20r%20U_j%5En,%20U%5E%7Bn+1%7D_j%20-%20%5Cphi(s_j))=%200,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%20%5C%5C%0AU_0%5En%20=%20v_l(t_n),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%20%5C%5C%0AU_%7BJ+1%7D%5En%20=%20v_r(t_n),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%0A%5Cend%7Bcases%7D%0A"></p>
<p>On peut la réécrire sous la forme matricielle afin d’extraire une solution numérique dite explicite : Sous forma matricielle, le schéma s’écrit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Amin(%5Cfrac%7BU%5E%7Bn+1%7D%20-%20U%5En%7D%7B%5CDelta%20t%7D%20+%20%20A%20U%5En%20+%20q(t_n),%20U%5E%7Bn+1%7D%20-%20g)%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%20%5C%5C%0AU%5E0%20=%20(%5Cphi(s_i))_%7B1%20%5Cleq%20i%20%5Cleq%20J%7D,%0A%5Cend%7Bcases%7D%0A"></p>
<p>où - g est un vecteur de <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5EJ"> défini par <img src="https://latex.codecogs.com/png.latex?g_j%20=%20%5Cphi(s_j)"> pour <img src="https://latex.codecogs.com/png.latex?j%20=%201,%20%5Cldots,%20J">,</p>
<ul>
<li><p><img src="https://latex.codecogs.com/png.latex?A"> est une matrice carrée tridiagonale de taille <img src="https://latex.codecogs.com/png.latex?J%20%5Ctimes%20J">. En posant <img src="https://latex.codecogs.com/png.latex?%5Calpha_j%20=%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%20%5Cfrac%7Bs_j%5E2%7D%7Bh%5E2%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Cbeta_j%20=%20r%20%5Cfrac%7Bs_j%7D%7B2%20h%7D">, les coefficients de la matrice <img src="https://latex.codecogs.com/png.latex?A"> sont donnés par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AA_%7Bj,j-1%7D%20=%20-%5Calpha_j%20+%20%5Cbeta_j,%20%5Cquad%20j=%202,%20%5Cldots,%20J,%20%5C%5C%0AA_%7Bj,j%7D%20=%202%5Calpha_j%20+%20r,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%20%5C%5C%0AA_%7Bj,j+1%7D%20=%20-%5Calpha_j%20-%20%5Cbeta_j,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%20%5C%5C%0A%5Cend%7Bcases%7D%0A"></p></li>
<li><p><img src="https://latex.codecogs.com/png.latex?q(t_n)"> un vecteur de <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5EJ"> qui dépendent des paramètres du modèle et de la discrétisation spatiale donné par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aq_j(t_n)%20=%0A%5Cbegin%7Bcases%7D%0A(-%5Calpha_1%20+%20%5Cbeta_1)%20U_0%5En,%20%5Cquad%20j%20=%201,%20%5C%5C%0A0,%20%5Cquad%20j%20=%202,%20%5Cldots,%20J-1,%20%5C%5C%0A(-%5Calpha_J%20+%20%5Cbeta_J)%20U_%7BJ+1%7D%5En,%20%5Cquad%20j%20=%20J.%0A%5Cend%7Bcases%7D%0A"></p></li>
</ul>
<p>De fait, on obtient la relation de récurrence explicite permettant de calculer <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1%7D"> en fonction de <img src="https://latex.codecogs.com/png.latex?U%5En"> :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AU%5E%7Bn+1%7D%20=%20max(U%5En%20-%20%5CDelta%20t%20(%20A%20U%5En%20+%20q(t_n)),%20g)),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%0A"></p>
</section>
<section id="implicit-euler-scheme" class="level2">
<h2 class="anchored" data-anchor-id="implicit-euler-scheme">Implicit Euler Scheme</h2>
<p>Pour des raisons de stabilité, on peut également utiliser un schéma d’Euler implicite, qui est basé sur une discrétisation implicite en temps.</p>
<section id="splitting-scheme" class="level3">
<h3 class="anchored" data-anchor-id="splitting-scheme">Splitting scheme</h3>
<p>Une première approche est de considérer le “splitting scheme”, qui consiste à séparer la partie linéaire de l’EDP de la partie non linéaire : (i) On calcule <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1,(1)%7D"> tel que <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1,(1)%7D%20-%20U%5En%20+%20%5CDelta%20t%20(A%20U%5E%7Bn+1,(1)%7D%20+%20q(t_%7Bn+1%7D))%20=%200">, (ii) On calcule <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1%7D"> tel que <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1%7D%20=%20max(U%5E%7Bn+1,(1)%7D,%20g)">.</p>
</section>
<section id="méthodes-numériques-de-résolution" class="level3">
<h3 class="anchored" data-anchor-id="méthodes-numériques-de-résolution">Méthodes numériques de résolution</h3>
<p>Une seconde approche est de considérer un schéma d’Euler implicite “fully implicit”, qui consiste à discrétiser l’EDP de manière implicite en temps, ce qui conduit à un système non linéaire à résoudre à chaque pas de temps : <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bcases%7D%0Amin(%5Cfrac%7BU%5E%7Bn+1%7D%20-%20U%5En%7D%7B%5CDelta%20t%7D%20+%20%20A%20U%5E%7Bn+1%7D%20+%20q(t_%7Bn+1%7D),%20U%5E%7Bn+1%7D%20-%20g)%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%20%5C%5C%0AU%5E0%20=%20(%5Cphi(s_i))_%7B1%20%5Cleq%20i%20%5Cleq%20J%7D.%0A%5Cend%7Bcases%7D"></p>
<p>En posant <img src="https://latex.codecogs.com/png.latex?B%20=%20I%20+%20%5CDelta%20t%20A"> et <img src="https://latex.codecogs.com/png.latex?b%20=%20U%5En%20-%20%5CDelta%20t%20q(t_%7Bn+1%7D)">, le problème se transforme en la résolution d’un problème d’obstacle à chaque pas de temps ou il s’agit de trouver <img src="https://latex.codecogs.com/png.latex?x%20%5Cin%20%5Cmathbb%7BR%7D%5EJ"> tel que <img src="https://latex.codecogs.com/png.latex?min(B%20x%20-%20b,%20x%20-%20g)%20=%200">. De fait, on a recours à des méthodes numériques de résolution de ce type de problème, telles que la méthode PSOR (projected successive over-relaxation) ou une méthode de type Newton.</p>
<section id="psor-algorithm" class="level4">
<h4 class="anchored" data-anchor-id="psor-algorithm">PSOR Algorithm</h4>
<p>La méthode PSOR est une méthode itérative de résolution de problèmes d’obstacle. Elle consiste à itérer sur les composantes du vecteur <img src="https://latex.codecogs.com/png.latex?x"> en appliquant une relaxation projetée.</p>
<p>Tout d’abord, on décompose la matrice <img src="https://latex.codecogs.com/png.latex?B"> en une partie triangulaire inférieure <img src="https://latex.codecogs.com/png.latex?L">, et une partie triangulaire strictement supérieure <img src="https://latex.codecogs.com/png.latex?U">, de sorte que <img src="https://latex.codecogs.com/png.latex?B%20=%20L%20+%20U">. Ensuite, pour un temps fixé <img src="https://latex.codecogs.com/png.latex?n">, on cherche à trouver une solution <img src="https://latex.codecogs.com/png.latex?x"> du problème d’obstacle en itérant sur les composantes du vecteur <img src="https://latex.codecogs.com/png.latex?x"> au temps fixé en appliquant la formule suivante : <img src="https://latex.codecogs.com/png.latex?%0Ax_j%5E%7B(k+1)%7D%20=%20max%5Cleft(g_j,%20%5Cfrac%7B%5Comega%7D%7BB_%7Bj,j%7D%7D%20%5Cleft(b_j%20-%20%5Csum_%7Bi=1%7D%5E%7Bj-1%7D%20B_%7Bj,i%7D%20x_i%5E%7B(k+1)%7D%20-%20%5Csum_%7Bi=j+1%7D%5E%7BJ%7D%20B_%7Bj,i%7D%20x_i%5E%7B(k)%7D%20%5Cright)%20+%20(1%20-%20%5Comega)%20x_j%5Ek%20%5Cright)%20,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?k"> est l’indice de l’itération. La méthode converge vers la solution du problème d’obstacle, et le taux de convergence dépend du choix du paramètre de relaxation, i.e.&nbsp;du choix de la valeur de <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Cin%20(0,2)"> dans la formule de relaxation. Lorsque <img src="https://latex.codecogs.com/png.latex?%5Comega%20=%201">, la méthode PSOR correspond à la méthode classique de Gauss-Seidel projetée. Lorsque <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Cneq%201">, la méthode PSOR peut accélérer la convergence, mais le choix optimal de <img src="https://latex.codecogs.com/png.latex?%5Comega"> dépend du problème spécifique et peut nécessiter une expérimentation.</p>
</section>
<section id="semi-smooth-newtons-method" class="level4">
<h4 class="anchored" data-anchor-id="semi-smooth-newtons-method">Semi-smooth Newton’s method</h4>
<p>La méthode de type Newton est une méthode itérative de résolution de problèmes non linéaires. Elle consiste à linéariser le problème d’obstacle autour d’une solution approximative <img src="https://latex.codecogs.com/png.latex?x%5E%7B(k)%7D"> à chaque itération, et à résoudre le problème linéarisé pour obtenir une nouvelle approximation <img src="https://latex.codecogs.com/png.latex?x%5E%7B(k+1)%7D">.</p>
<p>On cherche à résoudre le problème d’obstacle <img src="https://latex.codecogs.com/png.latex?F(x)%20=%20min(B%20x%20-%20b,%20x%20-%20g)%20=%200"> en itérant sur les approximations successives de la solution. On linéarise le problème d’obstacle autour de l’approximation <img src="https://latex.codecogs.com/png.latex?x%5E%7B(k)%7D"> en utilisant la formule de Taylor : <img src="https://latex.codecogs.com/png.latex?%0AF(x)%20%5Capprox%20F(x%5E%7B(k)%7D)%20+%20F'(x%5E%7B(k)%7D)(x%20-%20x%5E%7B(k)%7D),%0A"></p>
<p>On cherche à résoudre <img src="https://latex.codecogs.com/png.latex?F(x)%20=%200">. Dès lors, à chaque itération, on résout le problème linéarisé <img src="https://latex.codecogs.com/png.latex?F'(x%5E%7B(k)%7D)(x%20-%20x%5E%7B(k)%7D)%20=%20-F(x%5E%7B(k)%7D)"> pour obtenir une nouvelle approximation <img src="https://latex.codecogs.com/png.latex?x%5E%7B(k+1)%7D">. La méthode de type Newton converge quadratiquement vers la solution du problème d’obstacle, ce qui en fait une méthode très efficace pour résoudre ce type de problème.</p>
<p><img src="https://latex.codecogs.com/png.latex?F'(x)"> est la matrice jacobienne de <img src="https://latex.codecogs.com/png.latex?F"> à l’approximation <img src="https://latex.codecogs.com/png.latex?x">. La matrice jacobienne de <img src="https://latex.codecogs.com/png.latex?F"> est donnée par : <img src="https://latex.codecogs.com/png.latex?%0AF'(x)_%7Bi,j%7D%20:=%20%5Cbegin%7Bcases%7D%0AB_%7Bi,j%7D,%20%5Cquad%20%5Ctext%7Bsi%20%7D%20B%20x%20-%20b%20%3C%20x%20-%20g,%20%5C%5C%0A%5Cdelta_%7Bi,j%7D,%20%5Cquad%20%5Ctext%7Bsinon%7D.%0A%5Cend%7Bcases%7D%0A"></p>
</section>
</section>
</section>
<section id="higher-order-schemes" class="level2">
<h2 class="anchored" data-anchor-id="higher-order-schemes">Higher order schemes</h2>
<p>Enfin, nous envisagerons de comparer les schémas d’Euler implicite, les schémas de type Crank-Nicolson, et un schéma d’ordre 2 tel que le BDF2.</p>
</section>
</section>
<section id="implémentations" class="level1">
<h1>Implémentations</h1>
<p>Les différents schémas numériques étudiés pour l’équation de Black–Scholes avec option américaine partagent une structure commune. Nous introduisons donc une classe de base abstraite SchemeBase qui regroupe les paramètres financiers et numériques, la construction de la grille discrète, les conditions initiales et aux limites, ainsi que la discrétisation spatiale de l’opérateur de Black-Scholes à travers la matrice A et le vecteur q(t).</p>
<p>Les schémas spécifiques héritent de cette classe de base et implémentent la méthode solve(). En particulier, nous définissons les classes suivantes : - <code>SchemeEE</code> pour le schéma d’Euler explicite, - <code>SplittingScheme</code> pour le schéma splitting d’Euler implicite, - <code>SchemeCN</code> pour le schéma de Crank-Nicolson, - <code>PSOR</code> pour la méthode PSOR, - <code>NewtonSemiSmooth</code> pour la méthode de résolution newton semi smooth, et - <code>BdfScheme</code> pour le schema de résolution BDF2.</p>
<div id="fde8478d" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Package imports</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy.linalg <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> lng</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> diags,eye</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csr_matrix <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sparse</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse.linalg <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> spsolve</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-12"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb1-13"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-14"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy.linalg <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> lng</span>
<span id="cb1-15"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span>
<span id="cb1-16"></span>
<span id="cb1-17">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span></code></pre></div>
</div>
<div id="0774d7d9" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeBase(ABC):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Classe de base pour les schémas numériques de l'équation de Black-Scholes.</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb2-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Financial parameters</span></span>
<span id="cb2-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r</span>
<span id="cb2-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma</span>
<span id="cb2-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K</span>
<span id="cb2-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T</span>
<span id="cb2-11"></span>
<span id="cb2-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numerical parameters</span></span>
<span id="cb2-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N</span>
<span id="cb2-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J</span>
<span id="cb2-15">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin</span>
<span id="cb2-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smax</span>
<span id="cb2-17">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb2-18"></span>
<span id="cb2-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Grids</span></span>
<span id="cb2-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb2-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Smin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-23"></span>
<span id="cb2-24"></span>
<span id="cb2-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Operator</span></span>
<span id="cb2-26">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._build_matrix_A()</span>
<span id="cb2-27"></span>
<span id="cb2-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>,s):</span>
<span id="cb2-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition initiale (payoff) pour un put américain.</span></span>
<span id="cb2-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        𝜙(s) = max(K - s, 0) si payoff=1</span></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">             = K si K * 0.5 &lt;= s &lt;= K sinon 0 si payoff=2</span></span>
<span id="cb2-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb2-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.maximum(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.payoff  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>:</span>
<span id="cb2-37">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.where((<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K), <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-38"></span>
<span id="cb2-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à gauche pour un put américain.</span></span>
<span id="cb2-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        u( t, Smin ) = K * exp(-r * t) - S</span></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.payoff  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> :</span>
<span id="cb2-45">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb2-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.payoff  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> :</span>
<span id="cb2-47">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb2-48"></span>
<span id="cb2-49"></span>
<span id="cb2-50">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-51">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à droite pour un put américain.</span></span>
<span id="cb2-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        u( t, Smax ) = 0</span></span>
<span id="cb2-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb2-56"></span>
<span id="cb2-57">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _build_matrix_A(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-58">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Matrice d'amplification A.</span></span>
<span id="cb2-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-61">        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-62">        beta  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h)</span>
<span id="cb2-63"></span>
<span id="cb2-64">        lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sous-diagonale</span></span>
<span id="cb2-65">        main  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># diagonale principale</span></span>
<span id="cb2-66">        upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sur-diagonale</span></span>
<span id="cb2-67"></span>
<span id="cb2-68">        A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> diags(</span>
<span id="cb2-69">            diagonals<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lower, main, upper],</span>
<span id="cb2-70">            offsets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb2-71">            shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J),</span>
<span id="cb2-72">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span></span>
<span id="cb2-73">        )</span>
<span id="cb2-74"></span>
<span id="cb2-75">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> A, alpha, beta</span>
<span id="cb2-76"></span>
<span id="cb2-77"></span>
<span id="cb2-78">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> q(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-79">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Vecteur des conditions aux limites.</span></span>
<span id="cb2-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-82">        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J)</span>
<span id="cb2-83">        y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(t)</span>
<span id="cb2-84">        y[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(t)</span>
<span id="cb2-85">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> y</span>
<span id="cb2-86"></span>
<span id="cb2-87">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> interpolate(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, Sval, U):</span>
<span id="cb2-88">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-89"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Interpolation linéaire pour obtenir la valeur approximée d'un put</span></span>
<span id="cb2-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        en un point spot Sval donné.</span></span>
<span id="cb2-91"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-92">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin:</span>
<span id="cb2-93">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb2-94">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax:</span>
<span id="cb2-95">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb2-96">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb2-97">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.interp(Sval, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s, U)</span>
<span id="cb2-98"></span>
<span id="cb2-99">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract method</span></span>
<span id="cb2-100">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb2-101">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-102">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-103"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Méthode abstraite de résolution du schéma numérique.</span></span>
<span id="cb2-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-105">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">NotImplementedError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode solve() à implémenter dans la classe fille"</span>)</span></code></pre></div>
</div>
<div id="39b0ad94" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeEE(SchemeBase):</span>
<span id="cb3-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler explicite.</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb3-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb3-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EE"</span></span>
<span id="cb3-8"></span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb3-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Résolution du schéma d'Euler explicite.</span></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb3-13">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb3-14">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb3-15"></span>
<span id="cb3-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb3-17">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb3-18">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t)), g)</span>
<span id="cb3-19"></span>
<span id="cb3-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="380f7eb2" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SplittingScheme(SchemeBase):</span>
<span id="cb4-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler implicite "Splitting scheme".</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb4-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb4-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb4-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EI-AMER-SPLIT"</span></span>
<span id="cb4-8"></span>
<span id="cb4-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb4-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb4-11">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb4-12">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb4-13"></span>
<span id="cb4-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb4-15">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb4-16">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb4-17">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(U, g)</span>
<span id="cb4-18"></span>
<span id="cb4-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="3311bfee" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeCN(SchemeBase):</span>
<span id="cb5-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma de Crank-Nicolson.</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb5-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb5-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb5-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CN-AMER-SPLIT"</span></span>
<span id="cb5-8"></span>
<span id="cb5-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb5-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb5-11">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb5-12">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb5-13">        factor_minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb5-14">        factor_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb5-15"></span>
<span id="cb5-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb5-17">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb5-18">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(factor_plus, factor_minus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb5-19">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(U, g)</span>
<span id="cb5-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="3cc80206" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> PSOR(SchemeBase):</span>
<span id="cb6-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma PSOR</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb6-5"></span>
<span id="cb6-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb6-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb6-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EI-AMER-PSOR"</span></span>
<span id="cb6-9"></span>
<span id="cb6-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, omega<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, tol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, max_iter_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>):</span>
<span id="cb6-11"></span>
<span id="cb6-12">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s).copy()</span>
<span id="cb6-13">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb6-14">        B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb6-15">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> g.copy()</span>
<span id="cb6-16"></span>
<span id="cb6-17">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle en temps</span></span>
<span id="cb6-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb6-19"></span>
<span id="cb6-20">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb6-21"></span>
<span id="cb6-22">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Second membre f = U^{n+1} + dt * q(t_n)</span></span>
<span id="cb6-23">            b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t)</span>
<span id="cb6-24"></span>
<span id="cb6-25">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation PSOR</span></span>
<span id="cb6-26">            U_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U.copy()</span>
<span id="cb6-27"></span>
<span id="cb6-28">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle PSOR</span></span>
<span id="cb6-29">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(max_iter_):</span>
<span id="cb6-30"></span>
<span id="cb6-31">                U_old <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U_new.copy()</span>
<span id="cb6-32">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle spatiale</span></span>
<span id="cb6-33">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J):</span>
<span id="cb6-34">                    s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb6-35">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(i):</span>
<span id="cb6-36">                        s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> B[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U_new[j]</span>
<span id="cb6-37"></span>
<span id="cb6-38">                    s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb6-39">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J):</span>
<span id="cb6-40">                        s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> B[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U_old[j]</span>
<span id="cb6-41"></span>
<span id="cb6-42">                    ci <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (b[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B[i, i]</span>
<span id="cb6-43">                    U_new[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(</span>
<span id="cb6-44">                        (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> omega )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U_old[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ci     ,</span>
<span id="cb6-45">                        g[i]</span>
<span id="cb6-46">                    )</span>
<span id="cb6-47"></span>
<span id="cb6-48">                error1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lng.norm(U_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> U_old)</span>
<span id="cb6-49">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># error2 = lng.norm(np.minimum(B @ U_new - b, U_new - g))</span></span>
<span id="cb6-50">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Critère de convergence PSOR</span></span>
<span id="cb6-51">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> error1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> tol:</span>
<span id="cb6-52">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb6-53">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Passage au temps suivant</span></span>
<span id="cb6-54">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U_new.copy()</span>
<span id="cb6-55"></span>
<span id="cb6-56">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T</span></code></pre></div>
</div>
<div id="8922a532" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> NewtonSemiSmooth(SchemeBase):</span>
<span id="cb7-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Euler implicite + Newton semi-smooth</span></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    pour option américaine (problème d'obstacle)</span></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb7-6"></span>
<span id="cb7-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb7-8">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb7-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EI-AMER-NEWTON-SS"</span></span>
<span id="cb7-10"></span>
<span id="cb7-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, tol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>, max_iter_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>):</span>
<span id="cb7-12"></span>
<span id="cb7-13">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Obstacle (payoff)</span></span>
<span id="cb7-14">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s).copy()</span>
<span id="cb7-15"></span>
<span id="cb7-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Matrice implicite</span></span>
<span id="cb7-17">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb7-18">        B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb7-19"></span>
<span id="cb7-20">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Condition terminale</span></span>
<span id="cb7-21">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> g.copy()</span>
<span id="cb7-22"></span>
<span id="cb7-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb7-24"></span>
<span id="cb7-25">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb7-26">            b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t)</span>
<span id="cb7-27">            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U.copy()</span>
<span id="cb7-28"></span>
<span id="cb7-29">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle Newton semi-smooth</span></span>
<span id="cb7-30">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(max_iter_):</span>
<span id="cb7-31"></span>
<span id="cb7-32">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># F(x)</span></span>
<span id="cb7-33">                Bx_b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> b</span>
<span id="cb7-34">                x_g  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> g</span>
<span id="cb7-35">                F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.minimum(Bx_b, x_g)</span>
<span id="cb7-36"></span>
<span id="cb7-37">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test d'arrêt</span></span>
<span id="cb7-38">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> lng.norm(F, np.inf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> tol:</span>
<span id="cb7-39">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb7-40"></span>
<span id="cb7-41">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Construction de F'(x) (jacobienne généralisée)</span></span>
<span id="cb7-42">                rows, cols, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [], [], []</span>
<span id="cb7-43"></span>
<span id="cb7-44">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J):</span>
<span id="cb7-45">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> Bx_b[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> x_g[i]:</span>
<span id="cb7-46">                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ligne i de B</span></span>
<span id="cb7-47">                        row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B.getrow(i)</span>
<span id="cb7-48">                        rows.extend([i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(row.indices))</span>
<span id="cb7-49">                        cols.extend(row.indices)</span>
<span id="cb7-50">                        data.extend(row.data)</span>
<span id="cb7-51">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb7-52">                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ligne i de I</span></span>
<span id="cb7-53">                        rows.append(i)</span>
<span id="cb7-54">                        cols.append(i)</span>
<span id="cb7-55">                        data.append(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb7-56"></span>
<span id="cb7-57">                DF <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sparse((data, (rows, cols)), shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J))</span>
<span id="cb7-58"></span>
<span id="cb7-59">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Étape de Newton : DF * delta = F</span></span>
<span id="cb7-60">                delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(DF, F)</span>
<span id="cb7-61"></span>
<span id="cb7-62">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mise à jour</span></span>
<span id="cb7-63">                x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta</span>
<span id="cb7-64"></span>
<span id="cb7-65">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Passage au temps suivant</span></span>
<span id="cb7-66">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x.copy()</span>
<span id="cb7-67"></span>
<span id="cb7-68">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T</span></code></pre></div>
</div>
<div id="07d47efa" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> BdfScheme(SchemeBase):</span>
<span id="cb8-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma BDF2</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb8-5"></span>
<span id="cb8-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb8-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax, payoff)</span>
<span id="cb8-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BDF2-Obstacle-Newton"</span></span>
<span id="cb8-9"></span>
<span id="cb8-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb8-11"></span>
<span id="cb8-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Obstacle</span></span>
<span id="cb8-13">        g <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s).copy()</span>
<span id="cb8-14">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb8-15"></span>
<span id="cb8-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- Initialisation ----------</span></span>
<span id="cb8-17">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># U^0</span></span>
<span id="cb8-18">        U_nm1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> g.copy()</span>
<span id="cb8-19"></span>
<span id="cb8-20">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># U^1 via Euler implicite (avec obstacle)</span></span>
<span id="cb8-21">        B_euler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb8-22">        b_euler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U_nm1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt)</span>
<span id="cb8-23">        U_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(B_euler, b_euler)</span>
<span id="cb8-24">        U_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(U_n, g)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># projection autorisée uniquement ici (Euler)</span></span>
<span id="cb8-25"></span>
<span id="cb8-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- Boucle BDF2 ----------</span></span>
<span id="cb8-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb8-28"></span>
<span id="cb8-29">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb8-30">            factor1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb8-31">            factor2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> U_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> U_nm1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t)</span>
<span id="cb8-32"></span>
<span id="cb8-33">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(factor1, factor2)</span>
<span id="cb8-34">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(U, g)</span>
<span id="cb8-35"></span>
<span id="cb8-36">            U_nm1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U_n.copy()</span>
<span id="cb8-37">            U_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U.copy()</span>
<span id="cb8-38"></span>
<span id="cb8-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T</span></code></pre></div>
</div>
</section>
<section id="résultats-des-schémas-numériques" class="level1">
<h1>Résultats des schémas numériques</h1>
<section id="euler-explicit-scheme" class="level2">
<h2 class="anchored" data-anchor-id="euler-explicit-scheme">Euler Explicit Scheme</h2>
<p>Nous étudions ici le comportement du schéma d’Euler explicite appliqué à l’équation de Black–Scholes pour une option américaine de type put. Les simulations sont réalisées en faisant varier les paramètres de discrétisation spatiale J et temporelle N, tout en imposant la contrainte d’obstacle à chaque pas de temps.</p>
<p>Les figures présentées correspondent aux cas suivants (pour les deux payoffs considérés) :</p>
<ul>
<li>N = 20, J = 50</li>
<li>N = 20, J = 20</li>
<li>N = 50, J = 20</li>
</ul>
<p>Pour chaque configuration, le prix numérique de l’option est comparé au payoff, et la valeur associée à la condition de CFL est calculée.</p>
<div id="43215ab6" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">r_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb9-2">sigma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb9-3">K_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb9-4">T_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-5">Smin_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb9-6">Smax_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span></span>
<span id="cb9-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb9-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb9-9"></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definition des paramètres dans un dictionnaire</span></span>
<span id="cb9-11">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(</span>
<span id="cb9-12">    r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r_,</span>
<span id="cb9-13">    sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_,</span>
<span id="cb9-14">    K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K_,</span>
<span id="cb9-15">    T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T_,</span>
<span id="cb9-16">    N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur à définir plus tard</span></span>
<span id="cb9-17">    J<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur à définir plus tard</span></span>
<span id="cb9-18">    Smin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Smin_,</span>
<span id="cb9-19">    Smax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Smax_</span>
<span id="cb9-20">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.30 K=100 T=1</code></pre>
</div>
</div>
<div id="a196213e" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">J_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>]</span>
<span id="cb11-2">N_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]</span>
<span id="cb11-3"></span>
<span id="cb11-4">cfl_records1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb11-5"></span>
<span id="cb11-6">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb11-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, (N_, J_) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(N_values, J_values)):</span>
<span id="cb11-8">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb11-9">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_</span>
<span id="cb11-10"></span>
<span id="cb11-11">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-12">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb11-13">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb11-14">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb11-15"></span>
<span id="cb11-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#CFL condition</span></span>
<span id="cb11-17">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-18"></span>
<span id="cb11-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enregistrement dans la table</span></span>
<span id="cb11-20">    cfl_records1.append({</span>
<span id="cb11-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_,</span>
<span id="cb11-22">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_,</span>
<span id="cb11-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb11-24">    })</span>
<span id="cb11-25"></span>
<span id="cb11-26">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb11-27">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb11-28">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb11-29"></span>
<span id="cb11-30">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>J_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-31">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb11-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb11-33">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb11-34">    ax.legend()</span>
<span id="cb11-35"></span>
<span id="cb11-36">plt.suptitle(</span>
<span id="cb11-37">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du put américain -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb11-38">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb11-39">)</span>
<span id="cb11-40">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb11-41">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-11-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="18523a2c" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">J_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>]</span>
<span id="cb12-2">N_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]</span>
<span id="cb12-3"></span>
<span id="cb12-4">cfl_records2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb12-5"></span>
<span id="cb12-6">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb12-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, (N_, J_) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(N_values, J_values)):</span>
<span id="cb12-8">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb12-9">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_</span>
<span id="cb12-10"></span>
<span id="cb12-11">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-12">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb12-13">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb12-14">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb12-15"></span>
<span id="cb12-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#CFL condition</span></span>
<span id="cb12-17">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-18"></span>
<span id="cb12-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enregistrement dans la table</span></span>
<span id="cb12-20">    cfl_records2.append({</span>
<span id="cb12-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_,</span>
<span id="cb12-22">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_,</span>
<span id="cb12-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb12-24">    })</span>
<span id="cb12-25"></span>
<span id="cb12-26">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb12-27">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb12-28">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb12-29"></span>
<span id="cb12-30">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>J_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-31">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb12-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb12-33">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb12-34">    ax.legend()</span>
<span id="cb12-35"></span>
<span id="cb12-36">plt.suptitle(</span>
<span id="cb12-37">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du put américain -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, N fixé à </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb12-38">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb12-39">)</span>
<span id="cb12-40">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb12-41">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-12-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Les figures ci - dessus représentent le prix numérique de l’option au temps final, comparé au payoff, pour différentes configurations de maillage.</p>
<p>Les résultats montrent que le comportement du schéma dépend fortement du choix de N et J. Lorsque le maillage spatial est fin ( J=50) et que le nombre de pas de temps est relativement faible ( N=20), la solution numérique devient instable et présente de fortes oscillations, avec des valeurs non physiques. Ce phénomène traduit une instabilité du schéma explicite. En revanche, pour un maillage spatial plus grossier ( J=20) et un pas de temps suffisamment petit ( N=20 ou N=50), la solution obtenue est plus régulière et respecte correctement la contrainte d’option américaine, la solution restant au-dessus du payoff.</p>
<p>Ces observations suggèrent que la stabilité du schéma d’Euler explicite est étroitement liée à la relation entre le pas de temps et le pas d’espace, ce qui motive l’analyse de la condition de stabilité de type CFL.</p>
<div id="3fc192bc" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">cfl_df1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(cfl_records1)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 1 : N fixé</span></span>
<span id="cb13-2">cfl_df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(cfl_records2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 2 : N = J</span></span>
<span id="cb13-3"></span>
<span id="cb13-4">cfl_df1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cfl_df1.rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=10)"</span>})</span>
<span id="cb13-5">cfl_df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cfl_df2.rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=J)"</span>})</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff standard:"</span>)</span>
<span id="cb13-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb13-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(cfl_df1)</span>
<span id="cb13-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff tronqué:"</span>)</span>
<span id="cb13-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb13-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(cfl_df2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Payoff standard:
====================
    N   J  CFL (N=10)
0  20  50   18.288281
1  20  20    3.100781
2  50  20    1.240312
Payoff tronqué:
====================
    N   J  CFL (N=J)
0  20  50  18.288281
1  20  20   3.100781
2  50  20   1.240312</code></pre>
</div>
</div>
<p>L’étude de la condition de CFL met en évidence le caractère conditionnellement stable du schéma d’Euler explicite. Comme observé sur les figures, lorsque le maillage spatial est raffiné sans diminution suffisante du pas de temps, la condition de stabilité est violée et la solution numérique présente des oscillations marquées. À l’inverse, lorsque N et J sont augmentés conjointement, la condition de CFL est mieux respectée et le comportement du schéma s’améliore.</p>
<p>On remarque par ailleurs que les valeurs calculées de la CFL sont identiques pour le payoff standard et pour le payoff tronqué. Ce résultat est attendu, puisque la condition de stabilité dépend uniquement des paramètres numériques et du modèle, en particulier du pas de temps Δt, du pas d’espace h, de la volatilité σ et de la borne supérieure Smax et non de la régularité du payoff.</p>
<div id="87861e0d" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_convergence_table(N_grid, J_grid, params, Sval, scheme_class):</span>
<span id="cb15-2"></span>
<span id="cb15-3">    est_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-4">    errex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-5">    errors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-6">    cpu_times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-7"></span>
<span id="cb15-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> N, J <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(N_grid, J_grid):</span>
<span id="cb15-9">        params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N</span>
<span id="cb15-10">        params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J</span>
<span id="cb15-11"></span>
<span id="cb15-12">        start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb15-13">        scheme <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme_class(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb15-14">        U, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme.solve()</span>
<span id="cb15-15">        tcpu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start</span>
<span id="cb15-16"></span>
<span id="cb15-17">        price_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme.interpolate(Sval, U)</span>
<span id="cb15-18"></span>
<span id="cb15-19">        est_prices.append(price_est)</span>
<span id="cb15-20">        cpu_times.append(tcpu)</span>
<span id="cb15-21"></span>
<span id="cb15-22">    est_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(est_prices)</span>
<span id="cb15-23">    errors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(est_prices))</span>
<span id="cb15-24">    errors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(np.diff(est_prices))</span>
<span id="cb15-25">    cpu_times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(cpu_times)</span>
<span id="cb15-26"></span>
<span id="cb15-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ordre de convergence global</span></span>
<span id="cb15-28">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(errors))</span>
<span id="cb15-29">    h_step <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smax"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smin"</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb15-30">    alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(</span>
<span id="cb15-31">        (errors[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (errors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (h_step[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> h_step[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]),</span>
<span id="cb15-32">        np.log(errors[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> errors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.log(h_step[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> h_step[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]),</span>
<span id="cb15-33">        <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb15-34">    )</span>
<span id="cb15-35">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb15-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_grid,</span>
<span id="cb15-37">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_grid,</span>
<span id="cb15-38">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U(s)"</span>: est_prices,</span>
<span id="cb15-39">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>: errors,</span>
<span id="cb15-40">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alpha"</span>: alpha,</span>
<span id="cb15-41">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tcpu"</span>: cpu_times</span>
<span id="cb15-42">    })</span>
<span id="cb15-43"></span>
<span id="cb15-44">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div>
</div>
<div id="1517e2a2" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span></span>
<span id="cb16-2">J_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">320</span>])</span>
<span id="cb16-3"></span>
<span id="cb16-4">N_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> J_grid]).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb16-5"></span>
<span id="cb16-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cas N = J</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>)</span>
<span id="cb16-7"></span>
<span id="cb16-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Convergence Table for Scheme EE:"</span>)</span>
<span id="cb16-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, params, Sval, SchemeEE))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cas N = J
 ===========================================================================
Convergence Table for Scheme EE:
     J      N       U(s)     error     alpha      tcpu
0   19     80  12.947098  0.000000  0.000000  0.000573
1   39    320  13.064717  0.117619  0.000000  0.001415
2   79   1280  13.109572  0.044855  1.390781  0.005432
3  159   5120  13.117805  0.008233  2.445784  0.019646
4  319  20480  13.119987  0.002183  1.915301  0.082647</code></pre>
</div>
</div>
<p>ordre h^2 + dt ==&gt; division de l’erreur par 4.</p>
<!-- ### Condition de stabilité (CFL) et convergence -->
<!-- L’étude de la condition de CFL met en évidence le caractère conditionnellement stable du schéma d’Euler explicite. Comme observé sur les figures, lorsque le maillage spatial est raffiné sans diminution suffisante du pas de temps, la condition de stabilité est violée et la solution numérique présente des oscillations marquées. À l’inverse, lorsque
N et J sont augmentés conjointement, la condition de CFL est mieux respectée et le comportement du schéma s’améliore.

On remarque par ailleurs que les valeurs calculées de la CFL sont identiques pour le payoff standard et pour le payoff tronqué. Ce résultat est attendu, puisque la condition de stabilité dépend uniquement des paramètres numériques et du modèle, en particulier du pas de temps
Δt, du pas d’espace h, de la volatilité
σ et de la borne supérieure
Smax et non de la régularité du payoff. -->
<p>Pour compléter l’analyse de la stabilité, il est également intéressant d’étudier la convergence du schéma. Pour étudier l’ordre de convergence du schéma, nous avons utilisé une grille pour J qui varie en doublant à chaque fois, et une grille pour N qui varie selon la règle <img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%20%5Cfrac%7BJ%5E2%7D%7B10%7D">. Etant donné le fait que l’ordre théorique de convergence du schéma d’Euler explicite pour l’équation de Black-Scholes est de 2 en espace et de 1 en temps, on s’attend à ce que l’erreur diminue d’un facteur de 4 à chaque évolution de la grille. La table de convergence présentée ci-dessous montre l’évolution de la solution numérique U(s) lorsque le nombre de points spatiaux J et le nombre de pas temporels N sont augmentés.</p>
<p>En respectant la règle de variation des grilles pour J et N, on s’assure que les deux contributions à l’erreur, i.e.&nbsp;l’erreur spatiale et l’erreur temporelle, diminuent de manière cohérente, ce qui permet d’observer une convergence plus régulière du schéma et donc un ordre de convergence plus clair.</p>
<p>On observe que l’erreur diminue progressivement au fur et à mesure que le maillage spatial et temporel est raffiné, tandis que le facteur α (estimant l’ordre de convergence en espace) tend vers la valeur théorique attendue (2), ce qui confirme que le schéma d’Euler explicite converge correctement sous la condition de CFL.</p>
<p>La colonne tcpu montre que ce raffinement s’accompagne d’un coût computationnel croissant, le temps de calcul augmentant rapidement lorsque le maillage spatial et temporel est affiné.</p>
</section>
<section id="schemas-implicites" class="level2">
<h2 class="anchored" data-anchor-id="schemas-implicites">Schemas implicites</h2>
<p>Après avoir étudié le schéma d’Euler explicite, nous nous intéressons à des schémas implicites et semi-implicites (Euler implicite, Crank-Nicolson…). Ce passage est principalement motivé par le fait que le schéma explicite est conditionnellement stable et impose une limitation relativement stricte sur le pas de temps Δt et le maillage via la condition CFL. Les schémas implicites, eux, offrent une stabilité plus grande, permettant d’utiliser des pas de temps plus importants.</p>
<p>Dans le cas du schéma d’Euler implicite (et plus généralement pour tout schéma implicite appliqué aux options américaines), on est conduit à résoudre à chaque pas de temps un système non linéaire discret, correspondant à un problème d’obstacle v≥ϕ. Pour traiter ce système, nous utilisons diverses méthodes numériques, telles que la méthode PSOR (Projected Successive Over-Relaxation) ou une méthode de type Newton semi-smooth.</p>
<p>Ces schémas complémentaires nous permettent donc de comparer précision, stabilité et coût computationnel, tout en gérant correctement la contrainte inhérente aux options américaines.</p>
<section id="splitting-euler-implicit-scheme-splitting-crank-nicolson-scheme" class="level3">
<h3 class="anchored" data-anchor-id="splitting-euler-implicit-scheme-splitting-crank-nicolson-scheme">Splitting Euler Implicit scheme &amp; Splitting Crank-Nicolson Scheme</h3>
<p>Pour étudier le comportement du schéma splitting d’Euler implicite, nous nous sommes directement placés dans le cas où N=20 et J=50, qui avait montré des oscillations dans le schéma explicite. Les résultats obtenus sont présentés dans le graphique ci-dessous. Comme on peut le constater, le schéma d’Euler implicite produit une approximation stable et sans oscillations du prix du put européen, même pour des valeurs élevées de N et J. Cela confirme la stabilité inconditionnelle du schéma implicite, qui ne dépend pas de la relation entre le pas de temps et le pas d’espace.</p>
<div id="ccd4d4f9" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb18-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb18-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb18-6"></span>
<span id="cb18-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb18-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.30 K=100 T=1
Paramètres numériques:
J=20 N=50</code></pre>
</div>
</div>
<div id="f68ee015" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">ei_split <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SplittingScheme(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb20-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei_split.solve()</span>
<span id="cb20-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei_split.s</span>
<span id="cb20-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei_split.dt</span>
<span id="cb20-5"></span>
<span id="cb20-6">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt))</span>
<span id="cb20-7">plt.plot(s,ei_split.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb20-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb20-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb20-10">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put au cours du temps [Splitting Euler Implicite]"</span>)</span>
<span id="cb20-11">plt.legend()</span>
<span id="cb20-12">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-17-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Le schéma de splitting d’Euler implicite est un schéma implicite qui permet de séparer la partie linéaire de l’EDP de la partie non linéaire. En appliquant ce schéma, on observe, ci dessus, une évolution du prix du put au cours du temps qui est plus stable que celle obtenue avec le schéma d’Euler explicite.</p>
<p>Bien qu’ils puissent être moins précis qu’un vrai schéma d’Euler implicite “fully implicit”, les schémas de splitting d’Euler implicite et de splitting de Crank-Nicolson sont plus simples à implémenter et peuvent offrir une bonne approximation du prix du put américain, tout en étant plus stables que le schéma d’Euler explicite. Par ailleurs, en ce qui concerne les ordres de convergence, les schémas de splitting d’Euler implicite et de splitting de Crank-Nicolson peuvent présenter des ordres de convergence similaires à ceux des schémas d’Euler implicite et de Crank-Nicolson classiques.</p>
<div id="55101c6a" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">N_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>k) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Splitting scheme"</span>)</span>
<span id="cb21-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme EI:"</span>)</span>
<span id="cb21-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, params, Sval, SplittingScheme))</span>
<span id="cb21-6"></span>
<span id="cb21-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme CN:"</span>)</span>
<span id="cb21-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, params, Sval, SchemeCN))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Splitting scheme
=========================================================================== 
Convergence Table for Scheme EI:
     J    N       U(s)     error     alpha      tcpu
0   19   20  12.771151  0.000000  0.000000  0.001442
1   39   40  12.958929  0.187778  0.000000  0.001753
2   79   80  13.054834  0.095904  0.969364  0.004257
3  159  160  13.089790  0.034957  1.456030  0.010195
4  319  320  13.105599  0.015809  1.144811  0.026634
=========================================================================== 
Convergence Table for Scheme CN:
     J    N       U(s)     error     alpha      tcpu
0   19   20  12.888852  0.000000  0.000000  0.000767
1   39   40  13.021900  0.133049  0.000000  0.001050
2   79   80  13.090984  0.069084  0.945528  0.002674
3  159  160  13.107807  0.016823  2.037952  0.006654
4  319  320  13.114979  0.007172  1.229912  0.019724</code></pre>
</div>
</div>
</section>
<section id="psor-algorithm-1" class="level3">
<h3 class="anchored" data-anchor-id="psor-algorithm-1">PSOR Algorithm</h3>
<p>Comme attendu, le schéma PSOR prend plus de temps à converger que les schémas d’Euler implicite et de Crank-Nicolson, en raison de la nature itérative de la méthode PSOR. Cependant, il offre une bonne approximation du prix du put américain, tout en étant plus stable que le schéma d’Euler explicite.</p>
<div id="c4f81aaf" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb23-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb23-3"></span>
<span id="cb23-4"></span>
<span id="cb23-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb23-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb23-7"></span>
<span id="cb23-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb23-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.30 K=100 T=1
Paramètres numériques:
J=99 N=10</code></pre>
</div>
</div>
<div id="e9b9967b" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">psor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PSOR(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb25-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psor.solve() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># omega = 1.5 pour une convergence rapide</span></span>
<span id="cb25-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psor.s</span>
<span id="cb25-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psor.dt</span>
<span id="cb25-5"></span>
<span id="cb25-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb25-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t))</span>
<span id="cb25-8">plt.plot(s,psor.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb25-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb25-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb25-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put européen au cours du temps [PSOR]"</span>)</span>
<span id="cb25-12">plt.legend()</span>
<span id="cb25-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-20-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Cependant, elle peut être accélérée en choisissant un paramètre de relaxation <img src="https://latex.codecogs.com/png.latex?%5Comega"> approprié, ce qui peut réduire le nombre d’itérations nécessaires pour atteindre la convergence. Nous constatons que pour des valeurs de <img src="https://latex.codecogs.com/png.latex?%5Comega"> proches de 1.5, la méthode PSOR converge plus rapidement que pour des valeurs plus proches de 1, ce qui suggère que l’over-relaxation peut être bénéfique pour accélérer la convergence de la méthode PSOR.</p>
<div id="6cf089cd" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># comparing time</span></span>
<span id="cb26-2"></span>
<span id="cb26-3">omegas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.9</span>]</span>
<span id="cb26-4"></span>
<span id="cb26-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> omega <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> omegas:</span>
<span id="cb26-6">    psor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PSOR(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb26-7">    start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb26-8">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psor.solve(omega<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>omega)</span>
<span id="cb26-9">    end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb26-10">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Omega: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>omega<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Time taken: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> seconds"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Omega: 1.2, Time taken: 50.6897 seconds
Omega: 1.5, Time taken: 44.9621 seconds
Omega: 1.8, Time taken: 27.1073 seconds
Omega: 1.9, Time taken: 50.4578 seconds</code></pre>
</div>
</div>
</section>
<section id="semi-smooth-newtons-method-1" class="level3">
<h3 class="anchored" data-anchor-id="semi-smooth-newtons-method-1">Semi-smooth Newton’s method</h3>
<p>En général, la méthode PSOR est une méthode efficace pour résoudre les problèmes d’obstacle associés aux options américaines, mais elle peut être moins rapide que les méthodes de type Newton pour des problèmes de grande taille ou pour des schémas d’ordre supérieur.</p>
<p>Pour ce faire, nous avons implémenté une méthode de type Newton pour résoudre le problème d’obstacle à chaque pas de temps. Cette méthode consiste à linéariser le problème d’obstacle autour d’une solution approximative à chaque itération, et à résoudre le problème linéarisé pour obtenir une nouvelle approximation.</p>
<div id="55c766e2" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb28-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb28-3"></span>
<span id="cb28-4"></span>
<span id="cb28-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb28-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb28-7"></span>
<span id="cb28-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb28-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.30 K=100 T=1
Paramètres numériques:
J=99 N=10</code></pre>
</div>
</div>
<div id="bfc9dbda" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">newtonss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> NewtonSemiSmooth(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb30-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> newtonss.solve()</span>
<span id="cb30-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> newtonss.s</span>
<span id="cb30-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> newtonss.dt</span>
<span id="cb30-5"></span>
<span id="cb30-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb30-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t))</span>
<span id="cb30-8">plt.plot(s,newtonss.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb30-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb30-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb30-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put européen au cours du temps [Newton Semi-Smooth]"</span>)</span>
<span id="cb30-12">plt.legend()</span>
<span id="cb30-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-23-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="mini-conclusion-sur-les-schéma-implicites" class="level3">
<h3 class="anchored" data-anchor-id="mini-conclusion-sur-les-schéma-implicites">Mini-conclusion sur les schéma implicites</h3>
<p>L’étude des schémas implicites et semi-implicites montre que, lorsque le maillage spatial J et le pas temporel N sont raffinés, toutes les méthodes testées produisent des solutions numériques de plus en plus précises. Les ordres de convergence estimés (α) sont cohérents avec les prédictions théoriques : proches de 1 pour Euler implicite et proches de 2 pour Crank-Nicolson.</p>
<p>Le raffinement du maillage et du pas de temps s’accompagne d’une augmentation notable du temps de calcul, ce que mettent en évidence les tables de convergence et qui illustre clairement le compromis entre précision et coût computationnel.</p>
<p>En termes de qualité des solutions, les méthodes numériques explorées (PSOR et Newton semi-smooth) produisent des résultats lisses et cohérents, respectant correctement la contrainte d’obstacle.</p>
</section>
</section>
<section id="higher-order-schemes-1" class="level2">
<h2 class="anchored" data-anchor-id="higher-order-schemes-1">Higher order schemes</h2>
<p>Après avoir étudié les schémas implicites classiques et les méthodes de résolution pour le problème d’obstacle, nous considérons également un schéma d’ordre 2, le BDF2, qui améliore la précision temporelle tout en prenant en compte l’obstacle.</p>
<section id="bdf-scheme" class="level3">
<h3 class="anchored" data-anchor-id="bdf-scheme">BDF Scheme</h3>
<p>Le schéma BDF2 est testé avec les mêmes paramètres de maillage spatial et de pas de temps que les schémas précédents. Le graphique ci-dessous montre que le prix du put européen reste lisse et cohérent, respectant correctement la contrainte d’obstacle, de manière similaire aux autres méthodes implicites étudiées.</p>
<div id="89b22ccc" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb31-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb31-3"></span>
<span id="cb31-4">bdf2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BdfScheme(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params, payoff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb31-5">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bdf2.solve()</span>
<span id="cb31-6">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bdf2.s</span>
<span id="cb31-7">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bdf2.dt</span>
<span id="cb31-8"></span>
<span id="cb31-9">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb31-10">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t))</span>
<span id="cb31-11">plt.plot(s,bdf2.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb31-12">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb31-13">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb31-14">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put européen au cours du temps [BDF2]"</span>)</span>
<span id="cb31-15">plt.legend()</span>
<span id="cb31-16">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt_files/figure-html/cell-24-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>
</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>edp</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/edp/edp_american_opt.html</guid>
  <pubDate>Sat, 07 Feb 2026 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Introduction to financial markets</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/derivatives/00_references.html</link>
  <description><![CDATA[ 




<p>In the financial sector, a market refers to the place where the supply and demand for financial products and services meet. It brings together numerous participants, including banks, financial institutions, corporations, and individual investors.</p>
<p>Among the various financial instruments traded, one finds equities, bonds, derivatives, currencies, and commodities.</p>
<p>In this article, we examine three major types of financial markets: the equity market, the bond market, and the derivatives market, along with their characteristics and operating mechanisms.</p>
<section id="equity-markets" class="level2">
<h2 class="anchored" data-anchor-id="equity-markets">Equity markets</h2>
<p>The equity market enables companies to raise capital by issuing ownership securities (shares) that investors can purchase. These shares are subsequently traded on stock exchanges such as euronext paris or the new york stock exchange.</p>
<p>Share prices fluctuate according to supply and demand, influenced by corporate performance, macroeconomic conditions, and market sentiment.</p>
<p>The main participants in this market include issuing companies, institutional investors such as pension funds and insurance companies, and retail investors. To buy or sell shares, investors typically use online brokers or investment banks. They submit buy orders (bid) or sell orders (ask), which can be observed in the order book and are then executed on the market.</p>
<p>A market order corresponds to an immediate purchase or sale at the best available price.</p>
<p>A limit order allows the purchase or sale of a specified quantity of securities subject to a price condition. It provides control over the execution price and enables investors to set a target buying or selling price.</p>
<p>A stop order is executed only if the price reaches a specified threshold. It is often used to limit losses or secure gains. For a purchase, it is generally placed above the current market price; for a sale, below.</p>
<p>A stop-limit order combines a stop trigger with a limit condition and may include time restrictions.</p>
<p>A trailing stop order functions similarly to a stop order but is defined as a percentage variation rather than a fixed price level.</p>
<p>Additional tactical order types allow the implementation of more sophisticated investment strategies.</p>
<p>In france, the autorité des marchés financiers supervises financial markets to ensure transparency and proper functioning.</p>
<section id="price-formation" class="level3">
<h3 class="anchored" data-anchor-id="price-formation">Price formation</h3>
<p>The price of a share is determined by the interaction of supply and demand. When demand increases, the price tends to rise. Conversely, when supply exceeds demand, the price falls. The traded price corresponds to the effective intersection between a bid and an ask, that is, where an order is matched.</p>
<p>Fundamental factors also influence prices, including corporate earnings, dividend distributions, growth prospects, and general economic conditions.</p>
<p>At market close, the closing price corresponds to the last traded price of the session. This price may differ from the next opening price due to events occurring outside trading hours.</p>
</section>
</section>
<section id="bond-market" class="level2">
<h2 class="anchored" data-anchor-id="bond-market">Bond market</h2>
<p>The bond market is where investors buy and sell debt securities. Bonds are debt instruments issued by governments or corporations to raise capital. A bond is a contract between an issuer and an investor in which the issuer commits to repaying the principal at maturity while paying periodic interest payments, known as coupons.</p>
<p>Through bond issuance, the issuer contracts debt on the market. The investor lends funds by purchasing the bond and receives interest payments until maturity, at which point the principal, also called the face value, is repaid.</p>
<p>Bonds are generally considered lower-risk investments compared to equities, as they typically provide fixed returns and exhibit lower volatility. However, their expected return is usually lower than that of stocks.</p>
<p>Bonds can be classified according to their issuer, maturity, and interest structure. Government bonds are issued by sovereign states. Corporate bonds are issued by private or public companies. Short-term bonds typically have maturities below five years, whereas long-term bonds exceed ten years. Interest rates may be fixed or variable. Bonds are also categorized according to credit rating, which assesses the issuer’s ability to repay its debt.</p>
<section id="bond-valuation" class="level3">
<h3 class="anchored" data-anchor-id="bond-valuation">Bond valuation</h3>
<p>Bond valuation consists of determining its present value, that is, the amount an investor is willing to pay today. The price of a bond is given by</p>
<p><img src="https://latex.codecogs.com/png.latex?B(t,T)%20=%20%5Csum_%7Bu=1%7D%5E%7BT%7D%20%5Cfrac%7BC%7D%7B(1+r)%5Eu%7D%20+%20%5Cfrac%7BN%7D%7B(1+r)%5ET%7D.%0A"></p>
<p>The bond value depends on the market interest rate (r), the remaining maturity (T-t), the coupon rate, and the issuer’s credit quality. When market interest rates increase, existing bond prices decline, as new bonds offer higher yields.</p>
<p>A zero-coupon bond does not pay periodic interest. Its value simplifies to</p>
<p><img src="https://latex.codecogs.com/png.latex?B(t,T)%20=%20%5Cfrac%7BN%7D%7B(1+r)%5E%7BT-t%7D%7D."></p>
</section>
</section>
<section id="derivatives-market" class="level2">
<h2 class="anchored" data-anchor-id="derivatives-market">Derivatives market</h2>
<p>Derivatives are financial instruments whose value depends on an underlying asset. Examples include options, futures, forwards, and swaps. These instruments are used for hedging, speculation, and arbitrage.</p>
<p>Hedging involves taking a derivative position to offset an existing exposure. Speculation consists of taking a position based on expected future price movements without prior exposure. Arbitrage involves exploiting price discrepancies across markets to generate risk-free profit.</p>
<p>Derivatives may be traded on organized exchanges or over-the-counter markets. Organized markets are centralized and regulated, offering standardized contracts and daily settlement through clearinghouses. Margin requirements and marking-to-market procedures reduce counterparty risk.</p>
<p>Over-the-counter markets are decentralized and allow customized contracts between counterparties.</p>
<p>Prices are determined by bid and ask quotes. The bid-ask spread reflects market liquidity.</p>
<section id="forward-contracts" class="level3">
<h3 class="anchored" data-anchor-id="forward-contracts">Forward contracts</h3>
<p>A forward contract is an agreement between two parties to buy or sell an asset at a price (K) at a future date (T). The payoff for a long position is <img src="https://latex.codecogs.com/png.latex?S_T%20-%20K,"> and for a short position, <img src="https://latex.codecogs.com/png.latex?K%20-%20S_T."></p>
<p>Forwards are primarily used for hedging purposes.</p>
</section>
<section id="options" class="level3">
<h3 class="anchored" data-anchor-id="options">Options</h3>
<p>An option gives the holder the right, but not the obligation, to buy or sell an underlying asset at a strike price (K) before or at maturity (T).</p>
<p>The payoff of a european call is <img src="https://latex.codecogs.com/png.latex?%5Cmax(S_T%20-%20K,%200),"> while the payoff of a european put is <img src="https://latex.codecogs.com/png.latex?%5Cmax(K%20-%20S_T,%200)."></p>
<p>Options may be used for hedging, speculation, or income generation.</p>
</section>
<section id="futures" class="level3">
<h3 class="anchored" data-anchor-id="futures">Futures</h3>
<p>Futures contracts are standardized agreements traded on organized exchanges. They are marked to market daily, and margin accounts are adjusted accordingly. Futures prices converge to spot prices at maturity to prevent arbitrage.</p>
</section>
<section id="swaps" class="level3">
<h3 class="anchored" data-anchor-id="swaps">Swaps</h3>
<p>A swap is a derivative contract in which two parties exchange cash flows based on a notional amount over a specified period. The most common type is the interest rate swap, in which one party pays a fixed rate while receiving a floating rate, typically linked to libor or a similar benchmark.</p>
<p>The swap can be viewed as a combination of a fixed-rate bond and a floating-rate bond. Its value equals the difference between the present values of fixed and floating cash flows:</p>
<p><img src="https://latex.codecogs.com/png.latex?V_%7B%5Ctext%7Bswap%7D%7D%20=%20B_%7B%5Ctext%7Bfixed%7D%7D%20-%20B_%7B%5Ctext%7Bfloating%7D%7D.%0A"></p>
<p>When the fixed rate equals the swap rate, the contract initially has zero net value.</p>
<p>Swap markets provide quoted bid and ask fixed rates across maturities, which are used to construct the zero-coupon yield curve through bootstrapping techniques.</p>
<p>Although swap rates are not strictly risk-free, they are often used as reference discount rates for long maturities under normal market conditions.</p>
<p>Source: john c.&nbsp;hull, <em>options, futures, and other derivatives</em>, 11th edition.</p>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>derivatives</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/derivatives/00_references.html</guid>
  <pubDate>Sat, 31 Jan 2026 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Finite difference Methods for European Options</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <dc:creator>Marilene Kougoum</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Dans ce tp, nous étudion des schémas aux différences finies pour l’équation de Black-Scholes posée dans les variables primitives, après localisation dans un rectangle dans les variables temps-spot.</p>
<p>Ces schémas nous permettront d’obtenir une approximation numérique de la fonction de prix d’un put européen <img src="https://latex.codecogs.com/png.latex?v(t,s)">, avec <img src="https://latex.codecogs.com/png.latex?t%20%5Cin%20%5B0,T%5D"> et <img src="https://latex.codecogs.com/png.latex?s%20%5Cin%20%5BS_%7Bmin%7D,%20S_%7Bmax%7D%5D">. Cette fonction satisfait l’équation de Black-Scholes sur le domaine tronqué <img src="https://latex.codecogs.com/png.latex?%5COmega%20=%20%5B0,T%5D%20%5Ctimes%20%5BS_%7Bmin%7D,%20S_%7Bmax%7D%5D"> :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cfrac%7B%5Cpartial%20v%7D%7B%5Cpartial%20t%7D%0A+%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2%20s%5E2%20%5Cfrac%7B%5Cpartial%5E2%20v%7D%7B%5Cpartial%20s%5E2%7D%0A+%20r%20s%20%5Cfrac%7B%5Cpartial%20v%7D%7B%5Cpartial%20s%7D%0A-%20r%20v%20=%200,%20%5Cquad%20(t,s)%20%5Cin%20%5COmega,%20%5C%5C%0Av(T,s)%20=%20%5Cphi(s)%20=%20%5Cmax(K%20-%20s,%200),%0A%5Cquad%20s%20%5Cin%20%5BS_%7B%5Cmin%7D,%20S_%7B%5Cmax%7D%5D,%20%5C%5C%0Av(t,S_%7B%5Cmin%7D)%20=%20K%20e%5E%7B-r(T%20-%20t)%7D%20-%20S_%7B%5Cmin%7D,%0A%5Cquad%20t%20%5Cin%20(0,T),%20%5C%5C%0Av(t,S_%7B%5Cmax%7D)%20=%200,%0A%5Cquad%20t%20%5Cin%20(0,T).%0A%5Cend%7Bcases%7D%0A"></p>
<p>Nous considérons la grille discrète suivante : <img src="https://latex.codecogs.com/png.latex?h%20=%20%5Cfrac%7BS_%7Bmax%7D%20-%20S_%7Bmin%7D%7D%7BJ%20+%201%7D"> et <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t%20=%20%5Cfrac%7BT%7D%7BN%7D">, avec <img src="https://latex.codecogs.com/png.latex?J"> et <img src="https://latex.codecogs.com/png.latex?N"> des entiers positifs, et : - <img src="https://latex.codecogs.com/png.latex?s_j%20=%20S_%7Bmin%7D%20+%20j%20h">, pour <img src="https://latex.codecogs.com/png.latex?j%20=%200,%20%5Cldots,%20J%20+%201">, - <img src="https://latex.codecogs.com/png.latex?t_n%20=%20n%20%5CDelta%20t">, pour <img src="https://latex.codecogs.com/png.latex?n%20=%200,%20%5Cldots,%20N">.</p>
<p>On cherche une approximation <img src="https://latex.codecogs.com/png.latex?U_j%5En%20%5Capprox%20v(t_n,%20s_j)"> pour <img src="https://latex.codecogs.com/png.latex?j%20=%201,%20%5Cldots,%20J"> et <img src="https://latex.codecogs.com/png.latex?n%20=%200,%20%5Cldots,%20N">.</p>
<p>Les schémas aux différences finies nous permettront de discrétiser l’équation de Black-Scholes du prix d’un put européen sur cette grille. Dans ce TP, nous considérons divers <img src="https://latex.codecogs.com/png.latex?%5Ctheta">-schémas numériques pour ce problème: Le schéma d’Euler explicite, puis Euler implicite, et enfin de Crank-Nicolson. On étudie l’erreur de consistance pour ces schémas. Ces schémas conduisent à des relations de récurrence dans <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5En">, chaque pas consistant éventuellement à résoudre un système linéaire dans le cas des schémas implicites.</p>
<p>On étudie enfin la convergence de ces <img src="https://latex.codecogs.com/png.latex?%5Ctheta">-schémas. On verra apparaître la notion de stabilité conditionnelle (et de condition CFL) pour certains schémas tels que le schéma d’Euler Explicite, et la notion de stabilité inconditionnelle pour d’autres schémas tels que le schéma d’Euler Implicite ou de Crank-Nicolson.</p>
</section>
<section id="méthodes-numériques" class="level1">
<h1>Méthodes numériques</h1>
<section id="schéma-deuler-explicite-ee" class="level2">
<h2 class="anchored" data-anchor-id="schéma-deuler-explicite-ee">Schéma d’Euler explicite (EE)</h2>
<p>Le schéma d’Euler explicite est un schéma basé sur une discrétisation explicite en temps. La discrétisation de l’EDP est basée sur des approximations centrées. Dès lors, on approxime les dérivées partielles de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cfrac%7BU_j%5E%7Bn+1%7D%20-%20U_j%5En%7D%7B%5CDelta%20t%7D%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2%20s_j%5E2%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%202%20U_j%5En%20+%20U_%7Bj-1%7D%5En%7D%7Bh%5E2%7D%20+%20r%20s_j%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%20U_%7Bj-1%7D%5En%7D%7B2%20h%7D%20-%20r%20U_j%5En%20=%200,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%20%5C%5C%0AU_j%5E0%20=%20%5Cphi(s_j),%20%5Cquad%20j%20=%201,%20%5Cldots,%20J.%20%5C%5C%0AU_0%5En%20=%20K%20e%5E%7B-r(T%20-%20t_n)%7D%20-%20S_%7Bmin%7D,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%20%5C%5C%0AU_%7BJ+1%7D%5En%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%0A%5Cend%7Bcases%7D%0A"></p>
<p>On peut la réécrire sous la forme matricielle afin d’extraire une solution numérique dite explicite : Sous forme matricielle, le schéma s’écrit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Barray%7D%7Bl%7D%0A%5Cfrac%7BU%5E%7Bn+1%7D%20-%20A%20U%5En%7D%7B%5CDelta%20t%7D%20+%20%20A%20U%5En%20+%20q(t_n)%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%20%5C%5C%0AU%5E0%20=%20(%5Cphi(s_i))_%7B1%20%5Cleq%20i%20%5Cleq%20J%7D,%0A%5Cend%7Barray%7D%0A"></p>
<p>où</p>
<ul>
<li><p><img src="https://latex.codecogs.com/png.latex?A"> est une matrice carrée tridiagonale de taille <img src="https://latex.codecogs.com/png.latex?J%20%5Ctimes%20J">. En posant <img src="https://latex.codecogs.com/png.latex?%5Calpha_j%20=%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%20%5Cfrac%7Bs_j%5E2%7D%7Bh%5E2%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Cbeta_j%20=%20r%20%5Cfrac%7Bs_j%7D%7B2%20h%7D">, les coefficients de la matrice <img src="https://latex.codecogs.com/png.latex?A"> sont donnés par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AA_%7Bj,j-1%7D%20=%20-%5Calpha_j%20+%20%5Cbeta_j,%20%5Cquad%20j=%202,%20%5Cldots,%20J,%20%5C%5C%0AA_%7Bj,j%7D%20=%202%5Calpha_j%20+%20r,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%20%5C%5C%0AA_%7Bj,j+1%7D%20=%20-%5Calpha_j%20-%20%5Cbeta_j,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J.%0A%5Cend%7Bcases%7D%0A"></p></li>
<li><p><img src="https://latex.codecogs.com/png.latex?q(t_n)"> un vecteur de <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5EJ"> qui dépendent des paramètres du modèle et de la discrétisation spatiale donné par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aq_j(t_n)%20=%0A%5Cbegin%7Bcases%7D%0A(-%5Calpha_1%20+%20%5Cbeta_1)%20U_0%5En,%20%5Cquad%20j%20=%201,%20%5C%5C%0A0,%20%5Cquad%20j%20=%202,%20%5Cldots,%20J-1,%20%5C%5C%0A(-%5Calpha_J%20+%20%5Cbeta_J)%20U_%7BJ+1%7D%5En,%20%5Cquad%20j%20=%20J.%0A%5Cend%7Bcases%7D%0A"></p></li>
</ul>
<p>De fait, on obtient la relation de récurrence explicite permettant de calculer <img src="https://latex.codecogs.com/png.latex?U%5E%7Bn+1%7D"> en fonction de <img src="https://latex.codecogs.com/png.latex?U%5En"> :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AU%5E%7Bn+1%7D%20=%20A%20U%5En%20-%20%5CDelta%20t%20(%20A%20U%5En%20+%20q(t_n)%20),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%0A"></p>
</section>
<section id="schéma-deuler-implicite-ei" class="level2">
<h2 class="anchored" data-anchor-id="schéma-deuler-implicite-ei">Schéma d’Euler implicite (EI)</h2>
<p>Le schéma d’Euler implicite est un schéma basé sur une discrétisation implicite en temps. La discrétisation de l’EDP est basée sur des approximations centrées. Dès lors, on approxime les dérivées partielles de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bcases%7D%0A%5Cfrac%7BU_j%5E%7Bn+1%7D%20-%20U_j%5En%7D%7B%5CDelta%20t%7D%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2%20s_j%5E2%20%5Cmathbf%7B%5Cfrac%7BU_%7Bj+1%7D%5E%7Bn+1%7D%20-%202%20U_j%5E%7Bn+1%7D%20+%20U_%7Bj-1%7D%5E%7Bn+1%7D%7D%7Bh%5E2%7D%20+%20r%20s_j%20%5Cfrac%7BU_%7Bj+1%7D%5E%7Bn+1%7D%20-%20U_%7Bj-1%7D%5E%7Bn+1%7D%7D%7B2%20h%7D%20-%20r%20U_j%5E%7Bn+1%7D%7D%20=%200,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%20%5C%5C%0AU_j%5E0%20=%20%5Cphi(s_j),%20%5Cquad%20j%20=%201,%20%5Cldots,%20J.%20%5C%5C%20%20%0AU_0%5En%20=%20K%20e%5E%7B-r(T%20-%20t_n)%7D%20-%20S_%7Bmin%7D,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%20%5C%5C%0AU_%7BJ+1%7D%5En%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%0A%5Cend%7Bcases%7D%0A"></p>
<p>Dans ce cas, la relation de récurrence s’écrit sous la forme matricielle suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Barray%7D%7Bl%7D%0A%5Cfrac%7BU%5E%7Bn+1%7D%20-%20A%20U%5En%7D%7B%5CDelta%20t%7D%20+%20%20A%20U%5E%7Bn+1%7D%20+%20q(t_%7Bn+1%7D)%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%20%5C%5C%0AU%5E0%20=%20(%5Cphi(s_i))_%7B1%20%5Cleq%20i%20%5Cleq%20J%7D,%0A%5Cend%7Barray%7D%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?A"> et <img src="https://latex.codecogs.com/png.latex?q(t_n)"> sont définis comme précédemment. On obtient ainsi la relation de récurrence implicite suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(%20I%20+%20%5CDelta%20t%20A%20)%20U%5E%7Bn+1%7D%20=%20U%5En%20-%20%5CDelta%20t%20q(t_%7Bn+1%7D),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%0A"> où <img src="https://latex.codecogs.com/png.latex?I"> est la matrice identité de taille <img src="https://latex.codecogs.com/png.latex?J%20%5Ctimes%20J">.</p>
</section>
<section id="schéma-de-crank-nicolson-cn" class="level2">
<h2 class="anchored" data-anchor-id="schéma-de-crank-nicolson-cn">Schéma de Crank-Nicolson (CN)</h2>
<p>Le schéma de Crank-Nicolson est un schéma basé sur une discrétisation implicite en temps. La discrétisation de l’EDP est basée sur des approximations centrées. Dès lors, on approxime les dérivées partielles de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cfrac%7BU_j%5E%7Bn+1%7D%20-%20U_j%5En%7D%7B%5CDelta%20t%7D%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%20-%20%5Csigma%5E2%20s_j%5E2%20%5Cfrac%7BU_%7Bj+1%7D%5E%7Bn+1%7D%20-%202%20U_j%5E%7Bn+1%7D%20+%20U_%7Bj-1%7D%5E%7Bn+1%7D%7D%7Bh%5E2%7D%20%20-%20r%20s_j%20%5Cfrac%7BU_%7Bj+1%7D%5E%7Bn+1%7D%20-%20U_%7Bj-1%7D%5E%7Bn+1%7D%7D%7B2%20h%7D%20+%20r%20U_j%5E%7Bn+1%7D%20%5Cright.%20%5C%5C%0A%5Cquad%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%20-%20%5Csigma%5E2%20s_j%5E2%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%202%20U_j%5En%20+%20U_%7Bj-1%7D%5En%7D%7Bh%5E2%7D%20-%20r%20s_j%20%5Cfrac%7BU_%7Bj+1%7D%5En%20-%20U_%7Bj-1%7D%5En%7D%7B2%20h%7D%20+%20r%20U_j%5En%20%5Cright)%20=%200,%20%5Cquad%20j%20=%201,%20%5Cldots,%20J,%5Cquad%20n%20=%200,%20%5Cldots,%20N-1.%20%5C%5C%0AU_j%5E0%20=%20%5Cphi(s_j),%20%5Cquad%20j%20=%201,%20%5Cldots,%20J.%20%5C%5C%0AU_0%5En%20=%20K%20e%5E%7B-r(T%20-%20t_n)%7D%20-%20S_%7Bmin%7D,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%20%5C%5C%0AU_%7BJ+1%7D%5En%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N.%0A%5Cend%7Bcases%7D%0A"></p>
<p>La relation de récurrence s’écrit sous la forme matricielle suivante : <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Barray%7D%7Bl%7D%0A%5Cfrac%7BU%5E%7Bn+1%7D%20-%20A%20U%5En%7D%7B%5CDelta%20t%7D%20+%20%20%5Cfrac%7B1%7D%7B2%7D%20A%20(%20U%5E%7Bn+1%7D%20+%20U%5En%20)%20+%20%5Cfrac%7B1%7D%7B2%7D%20(%20q(t_%7Bn+1%7D)%20+%20q(t_n)%20)%20=%200,%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%20%5C%5C%0AU%5E0%20=%20(%5Cphi(s_i))_%7B1%20%5Cleq%20i%20%5Cleq%20J%7D,%0A%5Cend%7Barray%7D%0A"> où <img src="https://latex.codecogs.com/png.latex?A"> et <img src="https://latex.codecogs.com/png.latex?q(t_n)"> sont définis comme précédemment. On obtient ainsi la relation de récurrence implicite suivante : <img src="https://latex.codecogs.com/png.latex?%5Cleft(%20I%20+%20%5Cfrac%7B%5CDelta%20t%7D%7B2%7D%20A%20%5Cright)%20U%5E%7Bn+1%7D%20=%20%5Cleft(%20I%20-%20%5Cfrac%7B%5CDelta%20t%7D%7B2%7D%20A%20%5Cright)%20U%5En%20-%20%5Cfrac%7B%5CDelta%20t%7D%7B2%7D%20(%20q(t_%7Bn+1%7D)%20+%20q(t_n)%20),%20%5Cquad%20n%20=%200,%20%5Cldots,%20N-1,%0A"> où <img src="https://latex.codecogs.com/png.latex?I"> est la matrice identité de taille <img src="https://latex.codecogs.com/png.latex?J%20%5Ctimes%20J">.</p>
</section>
<section id="choix-dimplémentations" class="level2">
<h2 class="anchored" data-anchor-id="choix-dimplémentations">Choix d’implémentations</h2>
<p>Puisque les schémas implémentés partagent de nombreuses caractéristiques communes, nous définissons une classe de base abstraite <code>SchemeBase</code> qui encapsule les paramètres financiers, la grille discrète, les conditions initiales et aux limites, ainsi que la construction de la matrice <img src="https://latex.codecogs.com/png.latex?A"> et du vecteur <img src="https://latex.codecogs.com/png.latex?q(t)">. Les schémas spécifiques hériteront de cette classe de base et implémenteront la méthode <code>solve()</code> pour résoudre le schéma numérique particulier.</p>
<p>Trois classes filles seront ensuite définies : <code>SchemeEE</code> pour le schéma d’Euler explicite, <code>SchemeEI</code> pour le schéma d’Euler implicite, et <code>SchemeCN</code> pour le schéma de Crank-Nicolson. Chacune de ces classes implémentera la méthode <code>solve()</code> en fonction de la nature explicite ou implicite du schéma.</p>
<p>Les codes sont retrouvés sur ce lien github <a href="https://github.com/cheryl-kdio/EDP-M2MO/tree/main/TP1">github</a>.</p>
<div id="daeb275a" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Package imports</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy.linalg <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> lng</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> diags,eye</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ABC, abstractmethod</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csr_matrix <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sparse</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.sparse.linalg <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> spsolve</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-12"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb1-13"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span></code></pre></div>
</div>
<div id="fa5c2a50" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeBase(ABC):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Classe de base pour les schémas numériques de l'équation de Black-Scholes.</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb2-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Financial parameters</span></span>
<span id="cb2-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r</span>
<span id="cb2-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma</span>
<span id="cb2-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K</span>
<span id="cb2-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T</span>
<span id="cb2-11"></span>
<span id="cb2-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numerical parameters</span></span>
<span id="cb2-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N</span>
<span id="cb2-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J</span>
<span id="cb2-15">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin</span>
<span id="cb2-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smax</span>
<span id="cb2-17"></span>
<span id="cb2-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Grids</span></span>
<span id="cb2-19">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb2-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Smin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-22"></span>
<span id="cb2-23">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Operator</span></span>
<span id="cb2-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._build_matrix_A()</span>
<span id="cb2-25"></span>
<span id="cb2-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, s):</span>
<span id="cb2-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition initiale (payoff) pour un put européen.</span></span>
<span id="cb2-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        𝜙(s) = max(K - s, 0)</span></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.maximum(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-32"></span>
<span id="cb2-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à gauche pour un put européen.</span></span>
<span id="cb2-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        u( t, Smin ) = K * exp(-r * t) - S</span></span>
<span id="cb2-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin</span>
<span id="cb2-39"></span>
<span id="cb2-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à droite pour un put européen.</span></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        u( t, Smax ) = 0</span></span>
<span id="cb2-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb2-46"></span>
<span id="cb2-47">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _build_matrix_A(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-48">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Matrice d'amplification A.</span></span>
<span id="cb2-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-51">        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-52">        beta  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h)</span>
<span id="cb2-53"></span>
<span id="cb2-54">        lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sous-diagonale</span></span>
<span id="cb2-55">        main  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># diagonale principale</span></span>
<span id="cb2-56">        upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sur-diagonale</span></span>
<span id="cb2-57"></span>
<span id="cb2-58">        A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> diags(</span>
<span id="cb2-59">            diagonals<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lower, main, upper],</span>
<span id="cb2-60">            offsets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb2-61">            shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J),</span>
<span id="cb2-62">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span></span>
<span id="cb2-63">        )</span>
<span id="cb2-64"></span>
<span id="cb2-65">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> A, alpha, beta</span>
<span id="cb2-66"></span>
<span id="cb2-67"></span>
<span id="cb2-68">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> q(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb2-69">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Vecteur des conditions aux limites.</span></span>
<span id="cb2-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-72">        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J)</span>
<span id="cb2-73">        y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(t)</span>
<span id="cb2-74">        y[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(t)</span>
<span id="cb2-75">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> y</span>
<span id="cb2-76"></span>
<span id="cb2-77">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> interpolate(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, Sval, U):</span>
<span id="cb2-78">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-79"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Interpolation linéaire pour obtenir la valeur approximée d'un put</span></span>
<span id="cb2-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        en un point spot Sval donné.</span></span>
<span id="cb2-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-82">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin:</span>
<span id="cb2-83">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb2-84">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax:</span>
<span id="cb2-85">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb2-86">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb2-87">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.interp(Sval, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s, U)</span>
<span id="cb2-88"></span>
<span id="cb2-89">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract method</span></span>
<span id="cb2-90">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb2-91">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-92">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-93"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Méthode abstraite de résolution du schéma numérique.</span></span>
<span id="cb2-94"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb2-95">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">NotImplementedError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode solve() à implémenter dans la classe fille"</span>)</span></code></pre></div>
</div>
<div id="803cce32" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeEE(SchemeBase):</span>
<span id="cb3-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler explicite.</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb3-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb3-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Euler Explicite"</span></span>
<span id="cb3-8"></span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb3-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Résolution du schéma d'Euler explicite.</span></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb3-13">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb3-14"></span>
<span id="cb3-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb3-16">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb3-17">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb3-18"></span>
<span id="cb3-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="8a7401c9" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeEI(SchemeBase):</span>
<span id="cb4-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler implicite.</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb4-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb4-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb4-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Euler Implicite"</span></span>
<span id="cb4-8"></span>
<span id="cb4-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb4-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb4-11">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb4-12"></span>
<span id="cb4-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb4-14">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb4-15">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb4-16"></span>
<span id="cb4-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="5ae805c2" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeCN(SchemeBase):</span>
<span id="cb5-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma de Crank-Nicolson.</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb5-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb5-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb5-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Crank-Nicolson"</span></span>
<span id="cb5-8"></span>
<span id="cb5-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb5-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb5-11">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb5-12">        factor_minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb5-13">        factor_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb5-14"></span>
<span id="cb5-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb5-16">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb5-17">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(factor_plus, factor_minus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb5-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
</section>
</section>
<section id="résultats-des-schémas-numériques" class="level1">
<h1>Résultats des schémas numériques</h1>
<div id="f5a9be83" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">r_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb6-2">sigma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span></span>
<span id="cb6-3">K_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb6-4">T_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-5">Smin_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb6-6">Smax_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb6-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb6-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definition des paramètres dans un dictionnaire</span></span>
<span id="cb6-11">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(</span>
<span id="cb6-12">    r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r_,</span>
<span id="cb6-13">    sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_,</span>
<span id="cb6-14">    K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K_,</span>
<span id="cb6-15">    T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T_,</span>
<span id="cb6-16">    N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur à définir plus tard</span></span>
<span id="cb6-17">    J<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur à définir plus tard</span></span>
<span id="cb6-18">    Smin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Smin_,</span>
<span id="cb6-19">    Smax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Smax_</span>
<span id="cb6-20">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.20 K=100 T=1</code></pre>
</div>
</div>
<section id="résultat-du-schéma-deuler-explicite" class="level2">
<h2 class="anchored" data-anchor-id="résultat-du-schéma-deuler-explicite">Résultat du schéma d’Euler explicite</h2>
<p>Pour étudier le comportement du schéma d’Euler explicite, nous avons réalisé des simulations en faisant varier les paramètres <img src="https://latex.codecogs.com/png.latex?J"> (nombre de points spatiaux) et <img src="https://latex.codecogs.com/png.latex?N"> (nombre de points temporels). Nous avons considéré deux cas distincts :</p>
<ul>
<li>Cas 1 : <img src="https://latex.codecogs.com/png.latex?N"> fixé à 10 et <img src="https://latex.codecogs.com/png.latex?J"> variant parmi les valeurs <img src="https://latex.codecogs.com/png.latex?%5C%7B10,%2020,%2050%5C%7D">.</li>
<li>Cas 2 : <img src="https://latex.codecogs.com/png.latex?N"> et <img src="https://latex.codecogs.com/png.latex?J"> variant simultanément parmi les valeurs <img src="https://latex.codecogs.com/png.latex?%5C%7B10,%2020,%2050%5C%7D">, avec <img src="https://latex.codecogs.com/png.latex?N%20=%20J">.</li>
</ul>
<p>Les résultats obtenus sont présentés dans les graphiques ci-dessous. Par ailleurs, pour aider à l’interprétation des résultats, le résultat de la condition de CFL (Courant-Friedrichs-Lewy) est également affiché sur chaque graphique.</p>
<div id="10f74d9a" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 1 : N = 10, J varie dans `values`</span></span>
<span id="cb8-2"></span>
<span id="cb8-3">values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]</span>
<span id="cb8-4">N_fixed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb8-5"></span>
<span id="cb8-6">cfl_records1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-7"></span>
<span id="cb8-8">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb8-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, J_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(values):</span>
<span id="cb8-10">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_fixed</span>
<span id="cb8-11">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_</span>
<span id="cb8-12"></span>
<span id="cb8-13">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb8-14">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb8-15">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb8-16">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb8-17"></span>
<span id="cb8-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#CFL condition</span></span>
<span id="cb8-19">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-20"></span>
<span id="cb8-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enregistrement dans la table</span></span>
<span id="cb8-22">    cfl_records1.append({</span>
<span id="cb8-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_fixed,</span>
<span id="cb8-24">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_,</span>
<span id="cb8-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb8-26">    })</span>
<span id="cb8-27"></span>
<span id="cb8-28">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb8-29">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb8-30">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb8-31"></span>
<span id="cb8-32">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_fixed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>J_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb8-33">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb8-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb8-35">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb8-36">    ax.legend()</span>
<span id="cb8-37"></span>
<span id="cb8-38">plt.suptitle(</span>
<span id="cb8-39">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du put européen -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, N fixé à </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_fixed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb8-40">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb8-41">)</span>
<span id="cb8-42">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb8-43">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-8-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="52085352" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 2 : N = J avec N, J dans `values`</span></span>
<span id="cb9-2">values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]</span>
<span id="cb9-3">cfl_records2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb9-4"></span>
<span id="cb9-5">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb9-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, N_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(values):</span>
<span id="cb9-7">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb9-8">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb9-9"></span>
<span id="cb9-10">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb9-11">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb9-12">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb9-13">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb9-14"></span>
<span id="cb9-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#CFL condition</span></span>
<span id="cb9-16">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb9-17"></span>
<span id="cb9-18">    cfl_records2.append({</span>
<span id="cb9-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_,</span>
<span id="cb9-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: N_,</span>
<span id="cb9-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb9-22">    })</span>
<span id="cb9-23"></span>
<span id="cb9-24">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb9-25">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb9-26">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb9-27"></span>
<span id="cb9-28">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, CFL=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>CFL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-29">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb9-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb9-31">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb9-32">    ax.legend()</span>
<span id="cb9-33"></span>
<span id="cb9-34">plt.suptitle(</span>
<span id="cb9-35">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du put européen -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, N = J"</span>,</span>
<span id="cb9-36">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb9-37">)</span>
<span id="cb9-38">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb9-39">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En analysant les graphiques ci-dessus, nous constatons que le choix de <img src="https://latex.codecogs.com/png.latex?J"> et <img src="https://latex.codecogs.com/png.latex?N">, i.e.&nbsp;le maillage spatial et temporel, influence la stabilité de l’approximation du prix du put européen.</p>
<p>Dans le cas 1, avec <img src="https://latex.codecogs.com/png.latex?N"> fixé à 10 et <img src="https://latex.codecogs.com/png.latex?J"> variant, nous constatons que l’approximation de prix est très proche du prix du put européen pour N=J=10, ce qui est attendu pour un schéma stable. Cependant, lorsque <img src="https://latex.codecogs.com/png.latex?J"> augmente, des oscillations apparaissent dans la solution numérique, indiquant une instabilité du schéma.</p>
<p>Par ailleurs, dans le cas 2, où <img src="https://latex.codecogs.com/png.latex?N"> et <img src="https://latex.codecogs.com/png.latex?J"> varient simultanément avec <img src="https://latex.codecogs.com/png.latex?N=J">, nous observons également des oscillations et des instabilités pour des valeurs plus élevées de <img src="https://latex.codecogs.com/png.latex?N"> et <img src="https://latex.codecogs.com/png.latex?J">. Ce comportement instable est similaire à celui observé dans le cas 1, lorsque <img src="https://latex.codecogs.com/png.latex?J"> augmente pour un <img src="https://latex.codecogs.com/png.latex?N"> fixé.</p>
<p>Ces oscillations et instabilités observées dans les deux cas étudiés sont caractéristiques des schémas explicites lorsqu’ils ne respectent pas la condition de stabilité requise, dite condition CFL. En effet,le schéma d’euler explicite est stable conditionnellement au respect de la condition CFL dépendant du rapport entre <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t"> et <img src="https://latex.codecogs.com/png.latex?h%5E2">.</p>
<p>La condition CFL qui, pour le schéma explicite appliqué à l’équation de Black-Scholes, peut être exprimée comme suit : <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5CDelta%20t%7D%7Bh%5E2%7D%20%5Cleq%20%5Cfrac%7B1%7D%7B2%20%5Csigma%5E2%20Smax%5E2%7D">.Cette condition impose une relation entre le pas de temps <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t"> et le pas d’espace h. De fait, lorsque J augmente et N diminue, cette condition de stabilité peut être violée. Dans notre cas, lorsque J=50 et N=10 ou N=50, la condition CFL n’est pas respectée comme on peut l’observer dans le tableau ci-dessous, ce qui explique les oscillations et l’instabilité observées dans les résultats numériques.</p>
<div id="957a823a" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">cfl_df1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(cfl_records1)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 1 : N fixé</span></span>
<span id="cb10-2">cfl_df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(cfl_records2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 2 : N = J</span></span>
<span id="cb10-3"></span>
<span id="cb10-4">cfl_df1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cfl_df1.rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=10)"</span>})</span>
<span id="cb10-5">cfl_df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cfl_df2.rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=J)"</span>})</span>
<span id="cb10-6"></span>
<span id="cb10-7">cfl_table <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.merge(</span>
<span id="cb10-8">    cfl_df1[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=10)"</span>]],</span>
<span id="cb10-9">    cfl_df2[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL (N=J)"</span>]],</span>
<span id="cb10-10">    on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>,</span>
<span id="cb10-11">    how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inner"</span></span>
<span id="cb10-12">)</span>
<span id="cb10-13"></span>
<span id="cb10-14">cfl_table</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">J</th>
<th data-quarto-table-cell-role="th">CFL (N=10)</th>
<th data-quarto-table-cell-role="th">CFL (N=J)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>10</td>
<td>0.484</td>
<td>0.4840</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>20</td>
<td>1.764</td>
<td>0.8820</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>50</td>
<td>10.404</td>
<td>2.0808</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Pour mieux comprendre l’origine des oscillations, nous avons étudié la matrice d’amplification du schéma explicite pour N=10 et J=50. La matrice d’amplification est définie par <img src="https://latex.codecogs.com/png.latex?B:=%20I%20-%20dt*A"> où A est la matrice tridiagonale associée au schéma implicite. En calculant les valeurs propres de cette matrice, nous pouvons analyser la stabilité du schéma.</p>
<p>En effet, cette matrice intervient dans l’évolution de la solution numérique à chaque pas de temps, et on a : <img src="https://latex.codecogs.com/png.latex?%0A%7C%7CU%5En%7C%7C_2%20%3C=%20%7C%7CB%7C%7C%5En%20%7C%7CU%5E0%7C%7C_2,%20%5Cquad%20%5Cforall%20n%20%5Cgeq%200."></p>
<p>Si la norme de B est supérieure à 1, alors les valeurs prises par la solution numérique peuvent croître de manière exponentielle par rapport à la condition initiale, conduisant à des erreurs d’approximation possiblement importantes, des oscillations et ainsi une instabilité dans la solution numérique. Pour savoir si la norme de B est supérieure à 1, nous avons calculé le spectre de B et constaté que la valeur absolue de la plus grande valeur propre dépasse 1. Cela confirme que le schéma explicite est instable pour les paramètres choisis, ce qui explique les oscillations observées dans les résultats numériques.</p>
<p>Pour cette raison, nous avons testé d’autres schémas numériques tels que le schéma d’Euler implicite et le schéma de Crank-Nicolson, qui sont connus pour leur stabilité inconditionnelle.</p>
<div id="e019baf3" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres</span></span>
<span id="cb11-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb11-3">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb11-4"></span>
<span id="cb11-5">ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb11-6">ee.solve()</span>
<span id="cb11-7"></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Matrice d'amplification</span></span>
<span id="cb11-9">A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.A.toarray()</span>
<span id="cb11-10">B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.eye(ee.J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ee.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A</span>
<span id="cb11-11"></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeurs propres</span></span>
<span id="cb11-13">eigenvalues <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lng.eigvals(B)</span>
<span id="cb11-14">spectral_radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(eigenvalues))</span>
<span id="cb11-15">norm_B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lng.norm(B, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-16"></span>
<span id="cb11-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tableau récapitulatif</span></span>
<span id="cb11-18">df_spectrum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb11-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Valeur propre"</span>: eigenvalues,</span>
<span id="cb11-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|Valeur propre|"</span>: np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(eigenvalues)</span>
<span id="cb11-21">}).sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|Valeur propre|"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb11-22"></span>
<span id="cb11-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage</span></span>
<span id="cb11-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=== Analyse de stabilité du schéma explicite ===</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-25"></span>
<span id="cb11-26"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques :"</span>)</span>
<span id="cb11-27"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">,  J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>J<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">,  dt = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">,  h  = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-28"></span>
<span id="cb11-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10 plus grandes valeurs propres de B (triées par module décroissant) :"</span>)</span>
<span id="cb11-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_spectrum.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).to_string(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb11-31"></span>
<span id="cb11-32"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Norme matricielle induite (norme 2) :"</span>)</span>
<span id="cb11-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  ||B||₂ = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm_B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>=== Analyse de stabilité du schéma explicite ===

Paramètres numériques :
  N = 10,  J = 50,  dt = 0.10,  h  = 3.92

10 plus grandes valeurs propres de B (triées par module décroissant) :
 Valeur propre  |Valeur propre|
    -16.469570        16.469570
    -14.218162        14.218162
    -12.525602        12.525602
    -11.138073        11.138073
     -9.955409         9.955409
     -8.924546         8.924546
     -8.012865         8.012865
     -7.198391         7.198391
     -6.465369         6.465369
     -5.801977         5.801977
Norme matricielle induite (norme 2) :
  ||B||₂ = 16.473976
</code></pre>
</div>
</div>
</section>
<section id="résultat-du-schéma-deuler-implicite-et-de-crank-nicolson" class="level2">
<h2 class="anchored" data-anchor-id="résultat-du-schéma-deuler-implicite-et-de-crank-nicolson">Résultat du schéma d’Euler implicite et de Crank-Nicolson</h2>
<section id="cas-du-schéma-deuler-implicite" class="level3">
<h3 class="anchored" data-anchor-id="cas-du-schéma-deuler-implicite">Cas du schéma d’Euler implicite</h3>
<p>Pour étudier le comportement du schéma d’Euler implicite, nous nous sommes directement placés dans le cas où N=10 et J=50, qui avait montré des oscillations dans le schéma explicite. Les résultats obtenus sont présentés dans le graphique ci-dessous. Comme on peut le constater, le schéma d’Euler implicite produit une approximation stable et sans oscillations du prix du put européen, même pour des valeurs élevées de N et J. Cela confirme la stabilité inconditionnelle du schéma implicite, qui ne dépend pas de la relation entre le pas de temps et le pas spot.</p>
<div id="779e8bb2" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb13-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb13-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb13-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.20 K=100 T=1
Paramètres numériques:
J=50 N=10</code></pre>
</div>
</div>
<div id="2697280d" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">ei <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEI(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb15-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.solve()</span>
<span id="cb15-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.s</span>
<span id="cb15-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.dt</span>
<span id="cb15-5"></span>
<span id="cb15-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb15-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt))</span>
<span id="cb15-8">plt.plot(s,ei.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb15-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb15-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb15-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put européen au cours du temps [Euler Implicite]"</span>)</span>
<span id="cb15-12">plt.legend()</span>
<span id="cb15-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="cas-du-schéma-de-crank-nicolson" class="level3">
<h3 class="anchored" data-anchor-id="cas-du-schéma-de-crank-nicolson">Cas du schéma de Crank-Nicolson</h3>
<p>Le schéma de Crank-Nicolson, quant à lui, est une méthode implicite qui combine les avantages des schémas explicites et implicites. Il est basé sur une moyenne pondérée des valeurs aux temps n et n+1, ce qui permet d’obtenir une meilleure précision temporelle tout en maintenant la stabilité. Lors de l’étude du schéma de Crank-Nicolson, nous avons également choisi les paramètres N=10 et J=50. Les résultats obtenus sont présentés dans le graphique ci-dessous. Comme on peut le constater, le schéma de Crank-Nicolson produit également une approximation stable et sans oscillations du prix du put européen, confirmant ainsi sa stabilité inconditionnelle.</p>
<div id="a8024875" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb16-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb16-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb16-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.20 K=100 T=1
Paramètres numériques:
J=50 N=10</code></pre>
</div>
</div>
<div id="3a5bf5c8" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">cn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeCN(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb18-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.solve()</span>
<span id="cb18-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.s</span>
<span id="cb18-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.dt</span>
<span id="cb18-5"></span>
<span id="cb18-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb18-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt))</span>
<span id="cb18-8">plt.plot(s,cn.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb18-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb18-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb18-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du put européen au cours du temps [Crank-Nicolson]"</span>)</span>
<span id="cb18-12">plt.legend()</span>
<span id="cb18-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-15-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="complément-1-black-and-scholes" class="level1">
<h1>Complément 1: Black and Scholes</h1>
<section id="méthodes" class="level2">
<h2 class="anchored" data-anchor-id="méthodes">Méthodes</h2>
<p>Afin de comparer les résultats numériques obtenus avec les différents <img src="https://latex.codecogs.com/png.latex?%5Ctheta-">schémas au prix théorique du put européen, nous avons implémenté la formule de Black and Scholes dans une fonction Python. Cette fonction calcule le prix du put en utilisant les paramètres financiers tels que le prix de l’actif sous-jacent, le prix d’exercice, le taux d’intérêt sans risque, la volatilité et le temps jusqu’à l’échéance.</p>
<p>Nous analysons la convergence des schémas numériques considérés, ainsi que les ordres de convergence. La convergence est ensuite étudiée numériquement en comparant les prix obtenus par les schémas avec la solution analytique du put européen, et les ordres de convergence sont estimés à partir du comportement asymptotique de l’erreur lorsque les pas de temps et d’espace tendent vers zéro.</p>
<p>Pour estimer numériquement l’ordre de convergence des schémas, on utilise la relation <img src="https://latex.codecogs.com/png.latex?e_k%20%5Csim%20C%20h_k%5E%5Calpha">, où <img src="https://latex.codecogs.com/png.latex?C"> est une constante indépendante de <img src="https://latex.codecogs.com/png.latex?h_k"> et <img src="https://latex.codecogs.com/png.latex?%5Calpha"> est l’ordre de convergence du schéma, <img src="https://latex.codecogs.com/png.latex?h_k"> étant le pas de discrétisation spatial, défini par <img src="https://latex.codecogs.com/png.latex?h_k%20=%20%5Cfrac%7BS_%7Bmax%7D%20-%20S_%7Bmin%7D%7D%7BJ_k%20+%201%7D">, et <img src="https://latex.codecogs.com/png.latex?J_k"> le nombre de points spatiaux utilisés dans la discrétisation. De fait, on obtient l’ordre de convergence <img src="https://latex.codecogs.com/png.latex?%5Calpha"> en comparant les erreurs <img src="https://latex.codecogs.com/png.latex?e_k"> et <img src="https://latex.codecogs.com/png.latex?e_%7Bk+1%7D"> pour deux maillages successifs. D’où <img src="https://latex.codecogs.com/png.latex?%5Calpha_k%0A=%20%5Cfrac%7B%5Clog%5C!%5Cleft(%5Cfrac%7Be_%7Bk+1%7D%7D%7Be_k%7D%5Cright)%7D%0A%20%20%20%20%20%20%20%7B%5Clog%5C!%5Cleft(%5Cfrac%7Bh_%7Bk+1%7D%7D%7Bh_k%7D%5Cright)%7D.%0A"></p>
<p>Nous considérons deux grilles de discrétisation successives définies par les couples <img src="https://latex.codecogs.com/png.latex?(N_k,%20J_k)">, à savoir <img src="https://latex.codecogs.com/png.latex?N=J"> et <img src="https://latex.codecogs.com/png.latex?N%20=%20%5Cfrac%7BJ%5E2%7D%7B10%7D">, avec <img src="https://latex.codecogs.com/png.latex?J%20%5Cin%20%5C%7B10,%2020,%2040,%2080,%20160%5C%7D">.</p>
<div id="018b2ee7" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb19-2">Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span></span>
<span id="cb19-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sval=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>Sval)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.20 K=100 T=1 Sval=80.00</code></pre>
</div>
</div>
<div id="00ec9a73" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Implementation de la formule de Black and Scholes</span></span>
<span id="cb21-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> dplus(x,K,r,sigma,T,t):</span>
<span id="cb21-3">    tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t</span>
<span id="cb21-4">    numerator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.log(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb21-5">    denominator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(tau)</span>
<span id="cb21-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> numerator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denominator</span>
<span id="cb21-7"></span>
<span id="cb21-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> dmoins(dplus_,sigma,tau):</span>
<span id="cb21-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> dplus_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(tau)</span>
<span id="cb21-10"></span>
<span id="cb21-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compute_BS_price(x,K,r,sigma,T,t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,type_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'call'</span>):</span>
<span id="cb21-12">    tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t</span>
<span id="cb21-13">    dplus_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dplus(x,K,r,sigma,T,t)</span>
<span id="cb21-14">    dmoins_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dmoins(dplus_,sigma,tau)</span>
<span id="cb21-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> type_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'call'</span>:</span>
<span id="cb21-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>stats.norm.cdf(dplus_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> stats.norm.cdf(dmoins_)</span>
<span id="cb21-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> type_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'put'</span>:</span>
<span id="cb21-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>tau)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> stats.norm.cdf(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dmoins_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> stats.norm.cdf(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dplus_)</span></code></pre></div>
</div>
<div id="64c91640" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">price_BS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_BS_price(Sval, params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>], params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>], params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sigma'</span>], params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, type_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'put'</span>)</span>
<span id="cb22-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix théorique du put pour S=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.4f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(Sval, price_BS))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix théorique du put pour S=80.00 : 13.2737</code></pre>
</div>
</div>
<div id="e8b7556e" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_convergence_table(N_grid, J_grid, params, Sval, scheme_class):</span>
<span id="cb24-2"></span>
<span id="cb24-3">    est_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb24-4">    errex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb24-5">    errors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb24-6">    cpu_times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb24-7"></span>
<span id="cb24-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix exact (identique pour tous)</span></span>
<span id="cb24-9">    BS_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_BS_price(</span>
<span id="cb24-10">        Sval, params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>], params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>],</span>
<span id="cb24-11">        params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sigma'</span>], params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>],</span>
<span id="cb24-12">        t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, type_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'put'</span></span>
<span id="cb24-13">    )</span>
<span id="cb24-14"></span>
<span id="cb24-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> N, J <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(N_grid, J_grid):</span>
<span id="cb24-16">        params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N</span>
<span id="cb24-17">        params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J</span>
<span id="cb24-18"></span>
<span id="cb24-19">        start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb24-20">        scheme <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme_class(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb24-21">        U, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme.solve()</span>
<span id="cb24-22">        tcpu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start</span>
<span id="cb24-23"></span>
<span id="cb24-24">        price_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scheme.interpolate(Sval, U)</span>
<span id="cb24-25"></span>
<span id="cb24-26">        est_prices.append(price_est)</span>
<span id="cb24-27">        errex.append(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(price_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> BS_price))</span>
<span id="cb24-28">        cpu_times.append(tcpu)</span>
<span id="cb24-29"></span>
<span id="cb24-30">    est_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(est_prices)</span>
<span id="cb24-31">    errors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(est_prices))</span>
<span id="cb24-32">    errors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(np.diff(est_prices))</span>
<span id="cb24-33">    errex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(errex)</span>
<span id="cb24-34">    cpu_times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(cpu_times)</span>
<span id="cb24-35"></span>
<span id="cb24-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ordre de convergence global</span></span>
<span id="cb24-37">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(errors))</span>
<span id="cb24-38">    h_step <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smax"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smin"</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb24-39">    alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.log(errex[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> errex[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.log(h_step[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> h_step[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:])</span>
<span id="cb24-40"></span>
<span id="cb24-41">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb24-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_grid,</span>
<span id="cb24-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_grid,</span>
<span id="cb24-44">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U(s)"</span>: est_prices,</span>
<span id="cb24-45">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>: errors,</span>
<span id="cb24-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alpha"</span>: alpha,</span>
<span id="cb24-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"errex"</span>: errex,</span>
<span id="cb24-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tcpu"</span>: cpu_times</span>
<span id="cb24-49">    })</span>
<span id="cb24-50"></span>
<span id="cb24-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div>
</div>
</section>
<section id="résultats" class="level2">
<h2 class="anchored" data-anchor-id="résultats">Résultats</h2>
<!-- On constate que par rapport au schema d'euler implicite, le schema de Crank-Nicolson offre une meilleure précision temporelle tout en maintenant la stabilité, ce qui le rend particulièrement adapté pour la résolution des EDP en finance.
C'est le schéma qui a par ailleurs l'ordre de convergence le plus élevé parmi les deux schémas étudiés. -->
<p>On constate que les trois schémas présentent des comportements très contrastés en termes de stabilité, ordre de convergence et coût de calcul lorsque l’on impose <img src="https://latex.codecogs.com/png.latex?N%20=%20J">.</p>
<p>On observe tout d’abord que le schéma d’Euler explicite ne peut pas être considéré comme fiable dans ce cadre, comme précisé précédemment. Pour des maillages grossiers, l’erreur semble initialement décroître et l’ordre estimé est proche de 2, ce qui pourrait suggérer une bonne convergence. Cependant, dès que la discrétisation devient plus fine, la solution diverge et on observe une explosion numérique à partir de <img src="https://latex.codecogs.com/png.latex?N%20=%20J%20=%2080"> qui montre clairement que la condition de stabilité n’est plus satisfaite lorsque <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t"> et <img src="https://latex.codecogs.com/png.latex?%5CDelta%20s"> sont raffinés simultanément.</p>
<p>Le schéma d’Euler implicite est parfaitement stable sur toute la plage de discrétisations considérée. L’erreur décroît de façon régulière lorsque <img src="https://latex.codecogs.com/png.latex?N%20=%20J"> augmente, sans aucune instabilité numérique. De plus, l’ordre de convergence estimé reste inférieur à 2 et tend progressivement vers une valeur proche de 1, ce qui est cohérent avec le fait que, lorsque <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t%20%5Csim%20%5CDelta%20s">, l’erreur temporelle d’ordre 1 domine l’erreur spatiale.</p>
<p>Enfin, on constate que le schéma de Crank–Nicolson présente le comportement le plus satisfaisant. Il est stable sur toute la grille, l’erreur décroît rapidement et l’ordre de convergence estimé est très proche de 2 dès les maillages intermédiaires, puis se stabilise autour de cette valeur pour les maillages fins. Ce résultat est parfaitement conforme à la théorie, puisque le schéma est d’ordre 2 à la fois en temps et en espace.</p>
<p>Pour conclure, lorsque <img src="https://latex.codecogs.com/png.latex?N%20=%20J">, en termes de temps de calcul, le schéma EE est certes le plus rapide sur les grilles grossières, avec des temps de simulation de l’ordre de la milliseconde, mais cet avantage est sans intérêt pratique puisque la solution devient totalement inutilisable pour des maillages fins. Le schéma implicite constitue une solution robuste mais relativement coûteuse et moins précise. En effet, il nécessite la résolution d’un système linéaire à chaque pas de temps. Cela se reflète clairement dans les temps de calcul, qui augmentent sensiblement avec le raffinement et deviennent significatifs pour les maillages fins, atteignant plus de 0.1 seconde pour <img src="https://latex.codecogs.com/png.latex?N%20=%20J%20=%20160">.</p>
<p>Le schéma de Crank–Nicolson apparaît comme le meilleur compromis, combinant stabilité, convergence d’ordre 2 et temps de calcul raisonnables. En termes de coût de calcul, le schéma CN est plus cher que le schéma explicite mais reste nettement plus efficace que le schéma implicite pur. Les temps de simulation restent modérés, même pour des valeurs élevées de <img src="https://latex.codecogs.com/png.latex?N%20=%20J">, et demeurent largement compatibles avec une utilisation pratique, tout en offrant une précision bien supérieure.</p>
<div id="04594a31" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">N_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>])</span>
<span id="cb25-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cas N = J</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>)</span>
<span id="cb25-3"></span>
<span id="cb25-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Convergence Table for Scheme EE:"</span>)</span>
<span id="cb25-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeEE))</span>
<span id="cb25-6"></span>
<span id="cb25-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme EI:"</span>)</span>
<span id="cb25-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeEI))</span>
<span id="cb25-9"></span>
<span id="cb25-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme CN:"</span>)</span>
<span id="cb25-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeCN))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cas N = J
 ===========================================================================
Convergence Table for Scheme EE:
     J    N          U(s)         error       alpha         errex      tcpu
0   10   10  1.425509e+01  0.000000e+00    0.000000  9.814290e-01  0.000212
1   20   20  1.351532e+01  7.397760e-01    2.167407  2.416540e-01  0.000158
2   40   40  1.332033e+01  1.949870e-01    2.457937  4.666600e-02  0.000257
3   80   80 -1.061474e+04  1.062806e+04  -18.117778  1.062801e+04  0.000558
4  160  160 -8.121408e+69  8.121408e+69 -220.831903  8.121408e+69  0.001096
=========================================================================== 
Convergence Table for Scheme EI:
     J    N       U(s)     error     alpha     errex      tcpu
0   10   10  14.448406  0.000000  0.000000  1.174743  0.000530
1   20   20  13.642159  0.806247  1.792956  0.368496  0.000878
2   40   40  13.386145  0.256015  1.773621  0.112482  0.001978
3   80   80  13.310502  0.075642  1.639391  0.036839  0.003961
4  160  160  13.287066  0.023436  1.471804  0.013403  0.009982
=========================================================================== 
Convergence Table for Scheme CN:
     J    N       U(s)     error     alpha     errex      tcpu
0   10   10  14.353451  0.000000  0.000000  1.079788  0.000380
1   20   20  13.578892  0.774559  1.953920  0.305229  0.000576
2   40   40  13.353140  0.225752  2.011209  0.079477  0.001089
3   80   80  13.293944  0.059195  2.005878  0.020281  0.002566
4  160  160  13.278788  0.015156  2.002416  0.005125  0.006933</code></pre>
</div>
</div>
<!-- Avec $N = \frac{J^2}{10}$, on constate que le schéma explicite respecte la condition de stabilité de CFL, ce qui garantit la stabilité numérique de la solution. En revanche, lorsque $N = J$, la condition de CFL n'est pas respectée, ce qui peut entraîner des instabilités dans la solution numérique.
Par ailleurs, les ordres de convergence obtenus dans les deux cas sont similaires, ce qui suggère que le choix de la relation entre $N$ et $J$ n'affecte pas significativement la précision du schéma dans ce contexte particulier.
De même, nous avons des erreurs similaires pour les deux relations entre N et J, ce qui indique que le schéma de Crank-Nicolson est robuste par rapport à ces choix de maillage temporel et spatial. -->
<p>On constate que le choix <img src="https://latex.codecogs.com/png.latex?N%20=%20J%5E2/10"> modifie l’analyse par rapport au cas <img src="https://latex.codecogs.com/png.latex?N%20=%20J">, en particulier pour le schéma explicite. Alors que ce dernier était instable lorsque les pas de temps et d’espace étaient raffinés simultanément, il devient ici parfaitement stable. L’erreur décroît régulièrement et l’ordre de convergence est très proche de 2 sur l’ensemble des maillages, ce qui montre que la condition de stabilité est désormais respectée et que l’erreur spatiale domine. Le schéma EE retrouve ainsi une convergence théoriquement optimale, tout en conservant un coût de calcul globalement inférieur à celui des schémas implicites.</p>
<p>Le schéma d’Euler implicite reste, comme précédemment, inconditionnellement stable. Cependant, contrairement au cas <img src="https://latex.codecogs.com/png.latex?N%20=%20J">, l’ordre de convergence n’est plus limité par l’erreur temporelle et tend lui aussi vers 2 lorsque <img src="https://latex.codecogs.com/png.latex?J"> augmente. Cette amélioration de la précision se fait au prix d’un coût de calcul très élevé, les temps de simulation augmentant fortement avec <img src="https://latex.codecogs.com/png.latex?N">, ce qui rend le schéma peu compétitif pour des grilles fines.</p>
<p>Le schéma de Crank–Nicolson conserve un comportement très régulier et conforme à la théorie. Il est stable, présente une convergence d’ordre 2 et offre des erreurs comparables à celles du schéma explicite stabilisé. En revanche, son coût de calcul augmente sensiblement avec le raffinement et devient proche de celui du schéma implicite pour les maillages les plus fins.</p>
<p>Ainsi, contrairement au cas <img src="https://latex.codecogs.com/png.latex?N%20=%20J"> où Crank–Nicolson apparaissait clairement comme le meilleur compromis, le régime <img src="https://latex.codecogs.com/png.latex?N%20=%20J%5E2/10"> met en évidence l’efficacité du schéma explicite, qui combine ici stabilité, convergence d’ordre 2 et temps de calcul plus faibles, à condition d’accepter une contrainte forte sur le pas de temps.</p>
<div id="d60cf68f" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1">N_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([(j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> J_grid]).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb27-2"></span>
<span id="cb27-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cas N = J^2/10</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>)</span>
<span id="cb27-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Convergence Table for Scheme EE:"</span>)</span>
<span id="cb27-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeEE))</span>
<span id="cb27-6"></span>
<span id="cb27-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme EI:"</span>)</span>
<span id="cb27-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeEI))</span>
<span id="cb27-9"></span>
<span id="cb27-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Convergence Table for Scheme CN:"</span>)</span>
<span id="cb27-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(get_convergence_table(N_grid, J_grid, params, Sval, SchemeCN))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cas N = J^2/10
 ===========================================================================
Convergence Table for Scheme EE:
     J     N       U(s)     error     alpha     errex      tcpu
0   10    10  14.255092  0.000000  0.000000  0.981429  0.000155
1   20    40  13.547634  0.707459  1.973300  0.273971  0.000214
2   40   160  13.345106  0.202528  2.009007  0.071443  0.000719
3   80   640  13.291930  0.053175  2.002965  0.018267  0.002883
4  160  2560  13.278284  0.013646  2.000790  0.004621  0.010337
=========================================================================== 
Convergence Table for Scheme EI:
     J     N       U(s)     error     alpha     errex      tcpu
0   10    10  14.448406  0.000000  0.000000  1.174743  0.000565
1   20    40  13.611061  0.837345  1.929307  0.337398  0.001650
2   40   160  13.361562  0.249499  2.010421  0.087899  0.006941
3   80   640  13.296062  0.065500  2.007975  0.022399  0.031444
4  160  2560  13.279318  0.016744  2.003733  0.005655  0.157797
=========================================================================== 
Convergence Table for Scheme CN:
     J     N       U(s)     error     alpha     errex      tcpu
0   10    10  14.353451  0.000000  0.000000  1.079788  0.000538
1   20    40  13.579386  0.774065  1.951418  0.305723  0.001003
2   40   160  13.353328  0.226058  2.010092  0.079665  0.003859
3   80   640  13.293996  0.059332  2.005652  0.020333  0.019154
4  160  2560  13.278801  0.015195  2.002383  0.005138  0.106421</code></pre>
</div>
</div>
</section>
</section>
<section id="complément-2-call-option" class="level1">
<h1>Complément 2 : Call option</h1>
<p>Dans ce complément, nous étendons l’étude précédente au cas de l’option call européenne, en adaptant les conditions initiales et aux limites du problème de Black–Scholes. La structure des schémas numériques aux différences finies (Euler explicite, Euler implicite et Crank–Nicolson) reste inchangée, seule la nature du payoff et des conditions aux frontières étant modifiée. Notre objectif est de vérifier que les propriétés observées précédemment pour le put européen (en particulier en termes de stabilité et de convergence) se retrouvent également dans le cas du call. Les résultats numériques obtenus et présentés ci après permettent de confirmer la robustesse des schémas étudiés vis-à-vis du type d’option considérée.</p>
<section id="choix-dimplémentations-1" class="level2">
<h2 class="anchored" data-anchor-id="choix-dimplémentations-1">Choix d’implémentations</h2>
<p>L’implémentation des schémas numériques est restée globalement inchangée par rapport au cas du put européen. La même classe de base SchemeBase est utilisée pour définir la grille de discrétisation et les opérateurs numériques, tandis que seules la condition initiale, correspondant au payoff du call européen, ainsi que les conditions aux limites ont été adaptées. Les schémas d’Euler explicite, d’Euler implicite et de Crank–Nicolson sont implémentés de manière identique via les classes SchemeEE, SchemeEI et SchemeCN.</p>
<div id="074854d1" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeBase(ABC):</span>
<span id="cb29-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Classe de base pour les schémas numériques de l'équation de Black-Scholes.</span></span>
<span id="cb29-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb29-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb29-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Financial parameters</span></span>
<span id="cb29-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r</span>
<span id="cb29-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma</span>
<span id="cb29-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> K</span>
<span id="cb29-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T</span>
<span id="cb29-11"></span>
<span id="cb29-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numerical parameters</span></span>
<span id="cb29-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N</span>
<span id="cb29-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J</span>
<span id="cb29-15">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin</span>
<span id="cb29-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smax</span>
<span id="cb29-17"></span>
<span id="cb29-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Grids</span></span>
<span id="cb29-19">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb29-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Smin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb29-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Smin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb29-22"></span>
<span id="cb29-23">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Operator</span></span>
<span id="cb29-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._build_matrix_A()</span>
<span id="cb29-25"></span>
<span id="cb29-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, s):</span>
<span id="cb29-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition initiale (payoff) pour un call européen.</span></span>
<span id="cb29-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        𝜙(s) = max(s - K, 0)</span></span>
<span id="cb29-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.maximum(s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb29-32"></span>
<span id="cb29-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb29-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à gauche pour un call européen.</span></span>
<span id="cb29-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb29-38"></span>
<span id="cb29-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb29-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Condition aux limites à droite pour un put européen.</span></span>
<span id="cb29-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb29-44"></span>
<span id="cb29-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _build_matrix_A(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb29-46">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Matrice d'amplification A.</span></span>
<span id="cb29-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-49">        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb29-50">        beta  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.h)</span>
<span id="cb29-51"></span>
<span id="cb29-52">        lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sous-diagonale</span></span>
<span id="cb29-53">        main  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.r            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># diagonale principale</span></span>
<span id="cb29-54">        upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sur-diagonale</span></span>
<span id="cb29-55"></span>
<span id="cb29-56">        A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> diags(</span>
<span id="cb29-57">            diagonals<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lower, main, upper],</span>
<span id="cb29-58">            offsets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb29-59">            shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J),</span>
<span id="cb29-60">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span></span>
<span id="cb29-61">        )</span>
<span id="cb29-62"></span>
<span id="cb29-63">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> A, alpha, beta</span>
<span id="cb29-64"></span>
<span id="cb29-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> q(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t):</span>
<span id="cb29-66">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Vecteur des conditions aux limites.</span></span>
<span id="cb29-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-69">        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J)</span>
<span id="cb29-70">        y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(t)</span>
<span id="cb29-71">        y[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.beta[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(t)</span>
<span id="cb29-72">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> y</span>
<span id="cb29-73"></span>
<span id="cb29-74">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> interpolate(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, Sval, U):</span>
<span id="cb29-75">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Interpolation linéaire pour obtenir la valeur approximée d'un put</span></span>
<span id="cb29-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        en un point spot Sval donné.</span></span>
<span id="cb29-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-79">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smin:</span>
<span id="cb29-80">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uleft(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb29-81">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> Sval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.Smax:</span>
<span id="cb29-82">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.uright(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.T)</span>
<span id="cb29-83">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb29-84">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.interp(Sval, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s, U)</span>
<span id="cb29-85"></span>
<span id="cb29-86">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Abstract method</span></span>
<span id="cb29-87">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@abstractmethod</span></span>
<span id="cb29-88">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb29-89">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb29-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Méthode abstraite de résolution du schéma numérique.</span></span>
<span id="cb29-91"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb29-92">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">NotImplementedError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode solve() à implémenter dans la classe fille"</span>)</span></code></pre></div>
</div>
<div id="bd22373c" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeEE(SchemeBase):</span>
<span id="cb30-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb30-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler explicite.</span></span>
<span id="cb30-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb30-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb30-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb30-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Euler Explicite"</span></span>
<span id="cb30-8"></span>
<span id="cb30-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb30-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb30-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Résolution du schéma d'Euler explicite.</span></span>
<span id="cb30-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb30-13">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb30-14"></span>
<span id="cb30-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb30-16">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb30-17">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb30-18"></span>
<span id="cb30-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="ea595d24" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeEI(SchemeBase):</span>
<span id="cb31-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb31-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma d'Euler implicite.</span></span>
<span id="cb31-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb31-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb31-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb31-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Euler Implicite"</span></span>
<span id="cb31-8"></span>
<span id="cb31-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb31-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb31-11">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb31-12"></span>
<span id="cb31-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb31-14">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb31-15">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A, U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb31-16"></span>
<span id="cb31-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
<div id="2a00a9eb" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SchemeCN(SchemeBase):</span>
<span id="cb32-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Schéma de Crank-Nicolson.</span></span>
<span id="cb32-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, r, sigma, K, T, N, J, Smin, Smax):</span>
<span id="cb32-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(r, sigma, K, T, N, J, Smin, Smax)</span>
<span id="cb32-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.scheme_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Crank-Nicolson"</span></span>
<span id="cb32-8"></span>
<span id="cb32-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> solve(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb32-10">        U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s)</span>
<span id="cb32-11">        I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eye(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.J, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csr"</span>)</span>
<span id="cb32-12">        factor_minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb32-13">        factor_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> I <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.A</span>
<span id="cb32-14"></span>
<span id="cb32-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.N):</span>
<span id="cb32-16">            t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt</span>
<span id="cb32-17"></span>
<span id="cb32-18">            U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spsolve(factor_plus, factor_minus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q(t))</span>
<span id="cb32-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U,t</span></code></pre></div>
</div>
</section>
<section id="résultats-des-schémas-numériques-1" class="level2">
<h2 class="anchored" data-anchor-id="résultats-des-schémas-numériques-1">Résultats des schémas numériques</h2>
<section id="cas-du-schéma-deuler-explicite" class="level3">
<h3 class="anchored" data-anchor-id="cas-du-schéma-deuler-explicite">Cas du schéma d’Euler explicite</h3>
<p>Les résultats précédents ont montré que le schéma d’Euler explicite est conditionnellement stable en fonction de <img src="https://latex.codecogs.com/png.latex?J">, <img src="https://latex.codecogs.com/png.latex?N"> et de la condition CFL. Ici, nous l’appliquons à un call européen en utilisant les mêmes configurations que pour le put : - (1) <img src="https://latex.codecogs.com/png.latex?N%20=%2010"> et <img src="https://latex.codecogs.com/png.latex?J%20%5Cin%20%7B10,%2020,%2050%7D">, - (2) <img src="https://latex.codecogs.com/png.latex?N%20=%20J%20%5Cin%20%7B10,%2020,%2050%7D">.</p>
<p>Les résultats, accompagnés de l’indication de la condition CFL, sont présentés dans les graphiques ci‑dessous.</p>
<div id="cef2cf4f" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas 1 : N = 10, J varie dans `values` pour le call européen</span></span>
<span id="cb33-2">values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]</span>
<span id="cb33-3">N_fixed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb33-4">cfl_records1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb33-5"></span>
<span id="cb33-6">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb33-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, J_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(values):</span>
<span id="cb33-8">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_fixed</span>
<span id="cb33-9">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> J_</span>
<span id="cb33-10"></span>
<span id="cb33-11">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb33-12">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb33-13">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb33-14">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb33-15"></span>
<span id="cb33-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Condition CFL</span></span>
<span id="cb33-17">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb33-18"></span>
<span id="cb33-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enregistrement</span></span>
<span id="cb33-20">    cfl_records1.append({</span>
<span id="cb33-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_fixed,</span>
<span id="cb33-22">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: J_,</span>
<span id="cb33-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb33-24">    })</span>
<span id="cb33-25"></span>
<span id="cb33-26">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb33-27">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb33-28">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb33-29"></span>
<span id="cb33-30">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_fixed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>J_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, CFL=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>CFL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb33-31">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb33-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb33-33">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb33-34">    ax.legend()</span>
<span id="cb33-35"></span>
<span id="cb33-36">plt.suptitle(</span>
<span id="cb33-37">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du call européen -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, N fixé à </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_fixed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb33-38">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb33-39">)</span>
<span id="cb33-40">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb33-41">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-26-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="e12a1fbd" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Cas 2 : N = J avec N, J dans `values` pour le call européen</span></span>
<span id="cb34-2">cfl_records2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb34-3"></span>
<span id="cb34-4">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb34-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, N_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(values):</span>
<span id="cb34-6">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb34-7">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_</span>
<span id="cb34-8"></span>
<span id="cb34-9">    ee <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEE(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb34-10">    U, t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.solve()</span>
<span id="cb34-11">    s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.s</span>
<span id="cb34-12">    dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ee.dt</span>
<span id="cb34-13"></span>
<span id="cb34-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Condition CFL</span></span>
<span id="cb34-15">    CFL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ee.h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ee.Smax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb34-16"></span>
<span id="cb34-17">    cfl_records2.append({</span>
<span id="cb34-18">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>: N_,</span>
<span id="cb34-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>: N_,</span>
<span id="cb34-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CFL"</span>: CFL</span>
<span id="cb34-21">    })</span>
<span id="cb34-22"></span>
<span id="cb34-23">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[j]</span>
<span id="cb34-24">    ax.plot(s, U, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix option"</span>)</span>
<span id="cb34-25">    ax.plot(s, ee.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Payoff"</span>)</span>
<span id="cb34-26"></span>
<span id="cb34-27">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, CFL=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>CFL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb34-28">    ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb34-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb34-30">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb34-31">    ax.legend()</span>
<span id="cb34-32"></span>
<span id="cb34-33">plt.suptitle(</span>
<span id="cb34-34">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Evolution du prix du call européen -- Scheme </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ee<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scheme_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, N = J"</span>,</span>
<span id="cb34-35">    fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb34-36">)</span>
<span id="cb34-37">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>])</span>
<span id="cb34-38">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-27-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Comme pour le put, le schéma d’Euler explicite pour le call est stable pour de petites valeurs de <img src="https://latex.codecogs.com/png.latex?J"> et <img src="https://latex.codecogs.com/png.latex?N">, et devient instable lorsque la condition CFL n’est pas respectée, entraînant des oscillations.</p>
</section>
<section id="cas-des-schémas-deuler-implicite-et-de-crank-nicolson" class="level3">
<h3 class="anchored" data-anchor-id="cas-des-schémas-deuler-implicite-et-de-crank-nicolson">Cas des schémas d’Euler implicite et de Crank-Nicolson</h3>
<p>Les schémas d’Euler implicite et de Crank-Nikolson, appliqués au cas du call européen, demeurent inconditionnellement stables. Ils sont testés sur la configuration N= 10 et J= 50, qui avait conduit à des instabilités et à des oscillations pour le schema explicite. Les résultats obtenus montrent une approximation stable et sans oscillations du prix du call.</p>
<div id="f49045f0" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb35-2">params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb35-3"></span>
<span id="cb35-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres financiers:"</span>)</span>
<span id="cb35-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>r_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>sigma_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>K_, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>T_)</span>
<span id="cb35-6"></span>
<span id="cb35-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres numériques:"</span>)</span>
<span id="cb35-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N=%.0f"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres financiers:
r=0.10 sigma=0.20 K=100 T=1
Paramètres numériques:
J=50 N=10</code></pre>
</div>
</div>
<div id="f0153138" class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1">ei <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeEI(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb37-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.solve()</span>
<span id="cb37-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.s</span>
<span id="cb37-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ei.dt</span>
<span id="cb37-5"></span>
<span id="cb37-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb37-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt))</span>
<span id="cb37-8">plt.plot(s,ei.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb37-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb37-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb37-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du call européen au cours du temps [Euler Implicite]"</span>)</span>
<span id="cb37-12">plt.legend()</span>
<span id="cb37-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-29-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="79632e55" class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">cn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SchemeCN(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb38-2">U,t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.solve()</span>
<span id="cb38-3">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.s</span>
<span id="cb38-4">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cn.dt</span>
<span id="cb38-5"></span>
<span id="cb38-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb38-7">plt.plot(s,U,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.2f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt))</span>
<span id="cb38-8">plt.plot(s,cn.phi(s), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"payoff"</span>)</span>
<span id="cb38-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>)</span>
<span id="cb38-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u(t,s)"</span>)</span>
<span id="cb38-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix du call européen au cours du temps [Crank-Nicolson]"</span>)</span>
<span id="cb38-12">plt.legend()</span>
<span id="cb38-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt_files/figure-html/cell-30-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>
</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>m2mo</category>
  <category>edp</category>
  <guid>https://cheryl-kdio.github.io/posts/m2mo/edp/edp_european_opt.html</guid>
  <pubDate>Tue, 13 Jan 2026 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Detecting the Cure Model Appropriateness in Randomized Clinical Trials With Long-Term Survivors</title>
  <link>https://cheryl-kdio.github.io/</link>
  <description>To evaluate the appropriateness of a cure model when analyzing right-censored end points of a randomized clinical trial (RCT) in malignancy in the presence of long-term survivors. We aim to derive how the ratio estimation of censored cured subjects (RECeUS), previously proposed for a homogeneous population, could be extended for use in RCTs.</description>
  <category>news</category>
  <guid>https://cheryl-kdio.github.io/</guid>
  <pubDate>Sun, 14 Dec 2025 23:00:00 GMT</pubDate>
  <media:content url="https://ascopubs.org/cms/10.1200/CCI-25-00084/asset/d0c5b157-fe4e-42e3-b911-27a37c390406/assets/images/large/cci-25-00084f1.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Risque de valorisation d’obligation</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3.html</link>
  <description><![CDATA[ 




<p>Le risque de valorisation est le risque que la valeur d’un actif financier soit inférieure à sa valeur comptable ou à sa valeur de marché. Ce risque peut être causé par des fluctuations des taux d’intérêt, des changements dans la qualité de crédit de l’émetteur, ou d’autres facteurs économiques et financiers. Dans le cadre de nos travaux, nous allons nous interesser à la valorisation d’une obligation.</p>
<section id="définition-dune-obligation" class="level4">
<h4 class="anchored" data-anchor-id="définition-dune-obligation">Définition d’une obligation</h4>
<p>Une obligation est un titre de créance émis par une entreprise ou une collectivité publique afin de financer un projet ou de refinancer une dette. Contrairement aux actions, qui confèrent un droit de propriété, les obligations sont des instruments de dette qui garantissent aux investisseurs des paiements périodiques sous forme d’intérêts (coupons) et le remboursement du capital à une échéance déterminée.</p>
<p>Pour la valoriser, comme pour valoriser n’importe quel produit, nous devons faire l’espérance de la valeur actualisée des flux futurs sous la probabilité risque neutre. En effet, cela permet de se mettre sous la perspective d’un investisseur qui est indifférent au risque et qui ne demande qu’à être rémunéré pour le temps qu’il prête son argent.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX_%7Bt=0%7D%20=%20%5Cmathbb%7BE%7D%5EQ%20%5Cleft%5B%20e%5E%7B-r(T-t)%7D%20X_T%20%5Cright%5D%0A"></p>
<p>La valorisation d’une obligation repose principalement sur deux notions fondamentales :</p>
<ul>
<li>Le taux d’intérêt : il représente le coût du capital et reflète la rémunération exigée par les investisseurs pour prêter leur argent. Il peut être perçu comme le “loyer” du capital prêté ou encore comme la compensation pour le risque associé à l’investissement. Ce taux influence directement la valeur d’une obligation, car une hausse des taux entraîne généralement une baisse du prix des obligations existantes sur le marché secondaire.</li>
<li>Le taux de recouvrement : il correspond au pourcentage du capital que les investisseurs peuvent espérer récupérer en cas de défaut de l’émetteur. Un taux de recouvrement élevé signifie que, même en cas de faillite, les créanciers peuvent récupérer une grande partie de leur investissement, tandis qu’un taux faible suggère un risque de perte plus important. Historiquement, il est fixé à <img src="https://latex.codecogs.com/png.latex?R%20=%2040%5C%25">.</li>
</ul>
<p>Valoriser une obligation revient donc à calculer la valeur actualisée des flux futurs de paiement. De ce fait, cela revient à valoriser les coupons, le nominal en cas de non défaut de la part de l’émetteur et le recouvrement en cas de défaut de l’émetteur.</p>
</section>
<section id="a.-valorisation-des-coupons" class="level4">
<h4 class="anchored" data-anchor-id="a.-valorisation-des-coupons">a. Valorisation des coupons</h4>
<p>Le coupon est le montant que l’émetteur de l’obligation s’engage à verser à l’investisseur à intervalles réguliers (généralement annuels ou semestriels) jusqu’à l’échéance de l’obligation. Il est exprimé en pourcentage de la valeur nominale de l’obligation et peut être fixe ou variable, en fonction des modalités de l’émission.</p>
<p>Vu à <img src="https://latex.codecogs.com/png.latex?t">, la valeur d’un coupon <img src="https://latex.codecogs.com/png.latex?c"> qui sera payé à <img src="https://latex.codecogs.com/png.latex?T_i"> est donnée par : <img src="https://latex.codecogs.com/png.latex?%0AN%20%5Cmathbb%7BE%7D%5EQ%20%5Cleft%5B%20e%5E%7B-r(Ti-t)%7D%20c%20%5Cright%5D,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?c"> est le coupon, <img src="https://latex.codecogs.com/png.latex?r"> est le taux d’intérêt sans risque et <img src="https://latex.codecogs.com/png.latex?T_i"> est la date de paiement du coupon. Comme le taux d’intérêt est constant, et le paiement du coupon dépend de la probabilité de survie de l’émetteur, on peut réécrire la formule précédente comme suit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN%20e%5E%7B-r(Ti-t)%7D%20c%20%5Cmathbb%7BP%7D(%5Ctau%20%3E%20T_i),%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(%5Ctau%20%3E%20T_i)"> est la probabilité de survie de l’émetteur à la date <img src="https://latex.codecogs.com/png.latex?T_i">. Le modèle utilisé pour la probabilité de survie est le modèle à intensité <img src="https://latex.codecogs.com/png.latex?%5Clambda"> de Jarrow et Turnbull. La probabilité de survie est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BP%7D(%5Ctau%20%3E%20T_i)%20=%20e%5E%7B-%5Cint_%7Bt%7D%5E%7BT_i%7D%20%5Clambda(s)%20ds%7D%20=%20e%5E%7B-%5Clambda%20%5Cint_%7Bt%7D%5E%7BT_i%7D%20ds%7D%20=%20e%5E%7B-%5Clambda%20(T_i%20-%20t)%7D.%0A"></p>
<p>De ce fait, la valorisation du coupon est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC_%7Bt%7D%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20N%20%5Ctimes%20c%20%5Ctimes%20e%5E%7B-r(T_i-t)%7D%20%5Ctimes%20e%5E%7B-%5Clambda%20(T_i%20-%20t)%7D%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20N%20%5Ctimes%20c%20%5Ctimes%20e%5E%7B-(r%20+%20%5Clambda)%20%5Ctimes%20(T_i-t)%7D%20%5Cmathbb%7B1%7D_%7BT_i%20%5Cgeq%20t%7D%0A"></p>
</section>
<section id="b.-valorisation-du-nominal" class="level4">
<h4 class="anchored" data-anchor-id="b.-valorisation-du-nominal">b. Valorisation du nominal</h4>
<p>Le nominal est le montant demandé par l’émetteur. Sa valorisation est donnée par : <img src="https://latex.codecogs.com/png.latex?%0AN_t%20=%20N%20%5Ctimes%20e%5E%7B-r(T-t)%7D%20%5Ctimes%20e%5E%7B-%5Clambda%20(T-t)%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%0A"></p>
</section>
<section id="c.-valorisation-du-recouvrement" class="level4">
<h4 class="anchored" data-anchor-id="c.-valorisation-du-recouvrement">c.&nbsp;Valorisation du recouvrement</h4>
<p>Pour valoriser le recouvrement, il est nécessaire de connaitre la probabilité de défaut à l’instant précis <img src="https://latex.codecogs.com/png.latex?t">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(%5Ctau%20=%20u)%20&amp;=%20%5Cmathbb%7BP%7D(u%20%5Cleq%20%5Ctau%20%5Cleq%20u%20+%20du)%20%5C%5C%0A&amp;=%20%5Cmathbb%7BP%7D(%5Ctau%20%5Cgeq%20u)%20-%20%5Cmathbb%7BP%7D(%5Ctau%20%5Cgeq%20u%20+%20du)%5C%5C%0A&amp;=%20-%20%5Cfrac%7B%20%5Cmathbb%7BP%7D(%5Ctau%20%5Cgeq%20u%20+%20du)%20-%20%5Cmathbb%7BP%7D(%5Ctau%20%5Cgeq%20u)%20%7D%7Bdu%7D%20du%5C%5C%0A&amp;=%20-%5Cfrac%7B%5Cmathbb%7BP%7D(%5Ctau%20%5Cgeq%20u)%7D%7Bdu%7Ddu%20%5C%5C%0A&amp;=%20%5Clambda%20e%5E%7B%5Clambda%20(u-t)%7D%20du%0A%5Cend%7Baligned%7D%0A"></p>
<p>De ce fait, la valorisation du recouvrement à t est : <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AR_t%20&amp;=%20%5Cint_t%5ET%20N%20R%20e%5E%7B-rt%7D%20%5Cmathbb%7BP%7D(%5Ctau%20=%20u)%20%20%5C%5C%0A&amp;=%20%5Cint_t%5ET%20N%20R%20e%5E%7B-rt%7D%20%5Clambda%20e%5E%7B%5Clambda%20(u-t)%7D%20du%20%5C%5C%0A&amp;=%20N%20%5Clambda%20R%20%5Ctimes%20%5Cfrac%7B1%20-%20e%5E%7B-(r+%5Clambda)(T-t)%7D%7D%7Br+%5Clambda%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%0A%5Cend%7Baligned%7D%0A"></p>
En se servant de ces formules, nous pouvons valoriser une obligation de la manière suivante : $$
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0AB_t%20&amp;=%20C_t%20+%20N_t%20+%20R_t%20%5C%5C%0A&amp;=%20N%20%5Cleft%5B%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20%20c%20%5Ctimes%20e%5E%7B-(r%20+%20%5Clambda)%20%5Ctimes%20(T_i%0A-t)%7D%20%5Cmathbb%7B1%7D_%7BT_i%20%5Cgeq%20t%7D%20+%20e%5E%7B-(r+%5Clambda)(T-t)%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%20+%20%20%5Clambda%20R%20%5Ctimes%20%5Cfrac%7B1%20-%20e%5E%7B-(r+%5Clambda)(T-t)%7D%7D%7Br+%5Clambda%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%20%5Cright%5D%0A%5Cend%7Baligned%7D">
<div id="548af1df" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------- Package Imports ---------#</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf </span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> bisect</span></code></pre></div>
</div>
<div id="85310a59" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pricing_bond(t,c,T,r,lambda_,R,N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Fonction qui calcule la valeur d'une obligation.</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    t : date actuelle</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    c : coupon</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    T : date de maturité</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    r : taux sans risque</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    lambda_ : intensité de défaut</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    R : taux de recouvrement</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    N : nominal</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-12"></span>
<span id="cb2-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### Valorisation du coupon</span></span>
<span id="cb2-14">    Ct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>lambda_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span>t) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> Ti <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(dt, T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dt,dt)])</span>
<span id="cb2-15"></span>
<span id="cb2-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### Valorisation du nominal</span></span>
<span id="cb2-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Proba de survie</span></span>
<span id="cb2-18">    PS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp( <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t))</span>
<span id="cb2-19">    Nt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp( <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> PS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span>t)</span>
<span id="cb2-20"></span>
<span id="cb2-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### Valorisation du recouvrement</span></span>
<span id="cb2-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>lambda_)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> :</span>
<span id="cb2-23">        R_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t</span>
<span id="cb2-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> :</span>
<span id="cb2-25">        R_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ( <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp( <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>lambda_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>lambda_)</span>
<span id="cb2-26">    Rt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> R_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> t)</span>
<span id="cb2-27"></span>
<span id="cb2-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### Valorisation de l'obligation</span></span>
<span id="cb2-29">    Bond <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Ct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Nt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Rt</span>
<span id="cb2-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Bond</span></code></pre></div>
</div>
</section>
<section id="i.-valorisation-dune-obligation-sans-risque-de-défaut" class="level2">
<h2 class="anchored" data-anchor-id="i.-valorisation-dune-obligation-sans-risque-de-défaut">I. Valorisation d’une obligation sans risque de défaut</h2>
<p>Nous souhaitons valoriser une obligation sans risque de défaut, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200">. Pour ce faire, nous fixons les paramètres suivants :</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?N%20=%201"> : le nominal de l’obligation</li>
<li><img src="https://latex.codecogs.com/png.latex?r%20=%200.02"> : le taux d’intérêt sans risque</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200"> : l’intensité de défaut</li>
<li><img src="https://latex.codecogs.com/png.latex?T%20=%2010"> : l’échéance de l’obligation</li>
<li><img src="https://latex.codecogs.com/png.latex?c%20=%200.02"> : le coupon annuel</li>
<li><img src="https://latex.codecogs.com/png.latex?R%20=%200.4"> : le taux de recouvrement</li>
<li><img src="https://latex.codecogs.com/png.latex?n%20=%2010"> : le nombre de coupons</li>
<li><img src="https://latex.codecogs.com/png.latex?t%20=%200"> : l’instant présent</li>
</ul>
<div id="a679d77a" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb3-5">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-6">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-7">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-8"></span>
<span id="cb3-9">B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb3-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix de l'obligation vu à t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>B_t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix de l'obligation vu à t=0 : 0.9981933497987289</code></pre>
</div>
</div>
<p>En valorisant l’obligation, nous obtenons un prix de <img src="https://latex.codecogs.com/png.latex?B_0%20%5Capprox%200.99">. Ce prix est proche du nominal, car les taux de coupons sont égaux au taux de marché. L’obligation est dite au pair (Prix = Nominal) car elle rémunère au taux de marché. Si <img src="https://latex.codecogs.com/png.latex?c%20%3E%20r">, le prix de l’obligation sera supérieur au nominal, car le marché se serait rué sur cette obligation, car elle offrirait plus que le taux de marché. Sinon, le prix de l’obligation sera inférieur au nominal, car le marché serait plus réticent.</p>
<blockquote class="blockquote">
<p>💡 La condition dans laquelle l’obligation émise vaut 100% du nominal, i.e.&nbsp;au pair, est <img src="https://latex.codecogs.com/png.latex?c%20%5Capprox%20r%20+%20%5Clambda">. C’est une obligation qui permet de recouvrir ausi bien le risque de taux (<img src="https://latex.codecogs.com/png.latex?r">) et le risque de crédit lié à l’intensité de défaut (<img src="https://latex.codecogs.com/png.latex?%5Clambda">). Pour voir ceci, nous avons implémenté ci-dessous une fonction qui permet d’extrait le coupon qui permet d’avoir une obligation au pair, avec les mêmes paramètres que précédemment.</p>
</blockquote>
<div id="027ced9f" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb5-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb5-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Méthode de dichotomie</span></span>
<span id="cb5-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trouver_coupon(t, T, r, lambda_, R, N, dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, tol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>):</span>
<span id="cb5-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Trouve le coupon c tel que la valeur de l'obligation soit égale à N.</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb5-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> equation(c):</span>
<span id="cb5-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pricing_bond(t, c, T, r, lambda_, R, N, dt) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> N</span>
<span id="cb5-15">    </span>
<span id="cb5-16">    c_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bisect(equation, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, xtol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tol)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recherche de c dans l'intervalle [0,1]</span></span>
<span id="cb5-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> c_opt</span>
<span id="cb5-18"></span>
<span id="cb5-19">c_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> trouver_coupon(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t, T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T, r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r, lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_, R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R, N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb5-20">c_opt</span>
<span id="cb5-21"></span>
<span id="cb5-22"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Coupon pour avoir une obligation au pair vu à t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>c_opt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-23">B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c_opt,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb5-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix de l'obligation vu à t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>B_t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Coupon pour avoir une obligation au pair vu à t=0 : 2.64%
Prix de l'obligation vu à t=0 : 1.000007783902349</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>De plus, lorsque l’intensité de défaut est très grande, on retrouve un prix à peu près égal au taux de recouvrement. En effet, la probabilité de défaut est très grande et donc la probabilité qu’il y ait un recouvrement est très élevée. (voir exemple ci-dessous)</p>
</blockquote>
<div id="fc21ba8b" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb7-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb7-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb7-5">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-6">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-7">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb7-8"></span>
<span id="cb7-9"></span>
<span id="cb7-10">B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb7-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix de l'obligation vu à t=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>B_t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Taux de recouvrement : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix de l'obligation vu à t=0 : 0.3992
Taux de recouvrement : 0.4000</code></pre>
</div>
</div>
</section>
<section id="ii.-evolution-du-prix-de-lobligation-en-fonction-du-temps" class="level2">
<h2 class="anchored" data-anchor-id="ii.-evolution-du-prix-de-lobligation-en-fonction-du-temps">II. Evolution du prix de l’obligation en fonction du temps</h2>
<p>Durant la vie de l’obligation, son prix évolue en fonction des paiements de coupons. À chaque distribution de coupon, une chute du prix de l’obligation est observée. Cette baisse s’explique par le fait que, juste avant le versement, le prix de l’obligation intègre la valeur du coupon à percevoir. Une fois le coupon payé aux détenteurs, cette valeur disparaît, entraînant mécaniquement une diminution du prix de l’obligation, jusqu’à atteindre le nominal de l’obligation ainsi que le dernier coupon.</p>
<p>Toutefois, après cette chute liée au détachement du coupon, la valeur de l’obligation remonte progressivement à mesure que l’échéance du prochain coupon approche. Ce phénomène crée une évolution en dents de scie, où chaque baisse correspond à un paiement de coupon et chaque remontée traduit l’accumulation de la valeur du prochain paiement attendu.</p>
<p>Bien que ce phénomène soit logique et attendu, il peut être perçu négativement car la forme en dents de scie pourrait donner l’impression d’une dégradation de la qualité de l’obligation. C’est pourquoi on distingue deux types de prix :</p>
<ul>
<li>Le dirty price (prix sale) : il correspond au prix de l’obligation tel qu’affiché sur le marché, intégrant les variations dues aux paiements de coupons.</li>
<li>Le clean price (prix net ou pied de coupon) : il correspond au prix de l’obligation “nettoyé” des coupons accumulés. Ce prix est obtenu en soustrayant les intérêts courus au dirty price. Ainsi, le clean price permet d’évaluer plus précisément la valeur intrinsèque de l’obligation sans être pollué par les variations dues aux paiements périodiques de coupons. C’est cette valeur qui est généralement utilisée pour comparer les obligations entre elles.</li>
</ul>
<p>Pour illustrer cette évolution, nous avons tracé l’évolution du prix de l’obligation en fonction du temps. Nous avons fixé les paramètres suivants :</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?N%20=%201"> : le nominal de l’obligation</li>
<li><img src="https://latex.codecogs.com/png.latex?r%20=%200.02"> : le taux d’intérêt sans risque</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200.01"> : l’intensité de défaut</li>
<li><img src="https://latex.codecogs.com/png.latex?T%20=%2010"> : l’échéance de l’obligation</li>
<li><img src="https://latex.codecogs.com/png.latex?c%20=%200.03"> : le coupon annuel</li>
<li><img src="https://latex.codecogs.com/png.latex?R%20=%200.4"> : le taux de recouvrement</li>
<li><img src="https://latex.codecogs.com/png.latex?n%20=%2010"> : le nombre de coupons</li>
<li><img src="https://latex.codecogs.com/png.latex?t%20=%200"> : l’instant présent</li>
</ul>
<div id="d4a5a955" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> myFloor(x):</span>
<span id="cb9-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb9-3">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span>np.floor(x):</span>
<span id="cb9-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.floor(x)</span>
<span id="cb9-7"></span>
<span id="cb9-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> clean_price(t,c,T,r,lambda_,R,N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb9-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Fonction qui calcule le prix d'une obligation propre.</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb9-12">    B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r,lambda_,R,N,dt)</span>
<span id="cb9-13">    cc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> myFloor(t))</span>
<span id="cb9-14"></span>
<span id="cb9-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cc</span></code></pre></div>
</div>
<div id="8d16247d" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb10-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb10-5">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-6">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-7">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-8"></span>
<span id="cb10-9"></span>
<span id="cb10-10">dirty_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb10-11">clean_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb10-12">grid_values_c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb10-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_c:</span>
<span id="cb10-14">    dirty_prices.append(pricing_bond(t,c,T,r,lambda_,R,N))</span>
<span id="cb10-15">    clean_prices.append(clean_price(t,c,T,r,lambda_,R,N))</span>
<span id="cb10-16"></span>
<span id="cb10-17"></span>
<span id="cb10-18"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb10-19">plt.plot(grid_values_c,dirty_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb10-20">plt.plot(grid_values_c,clean_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean prices"</span>)</span>
<span id="cb10-21">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices vs Clean prices"</span>)</span>
<span id="cb10-22">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>)</span>
<span id="cb10-23">plt.grid()</span>
<span id="cb10-24">plt.legend()</span>
<span id="cb10-25">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix de l'obligation"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="7">
<pre><code>Text(0, 0.5, "Prix de l'obligation")</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-8-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="a.-cas-extrêmes" class="level3">
<h3 class="anchored" data-anchor-id="a.-cas-extrêmes">a. Cas extrêmes</h3>
<p>Nous avons analysé l’évolution du prix de l’obligation dans deux cas extrêmes afin d’observer l’impact du coupon sur la dynamique des prix. Toutes choses égales par ailleurs, nous avons modifié le coupon de l’obligation tout en conservant les autres paramètres constants. Les deux scénarios étudiés sont les suivants :</p>
<ul>
<li>$ c = 0.01 $ : le coupon est inférieur au taux d’intérêt sans risque.<br>
</li>
<li>$ c = 0.05 $ : le coupon est supérieur au taux d’intérêt sans risque.</li>
</ul>
<section id="cas-1-c-0.01" class="level4">
<h4 class="anchored" data-anchor-id="cas-1-c-0.01">Cas 1 : $ c = 0.01 $</h4>
<p>Lorsque $ c = 1% $ et que ce coupon est inférieur au taux d’intérêt sans risque $ r $, la valeur de l’obligation évolue de manière spécifique :</p>
<ul>
<li>Au départ, l’obligation est escomptée car le coupon est faible, et les investisseurs anticipent un rendement global inférieur au taux du marché.<br>
</li>
<li>À mesure que l’échéance approche, l’incertitude sur le paiement du coupon disparaît progressivement. Les investisseurs deviennent de plus en plus certains que le paiement aura bien lieu.<br>
</li>
<li>À la veille du paiement, l’obligation converge vers un prix proche de $ N + c $ pour le dirty price (car elle inclut le coupon accumulé) et $ N $ pour le clean price (qui exclut le coupon accumulé).</li>
</ul>
<p>Cela signifie que l’obligation s’apprécie au fil du temps en raison de la certitude croissante du paiement des flux futurs. En d’autres termes, plus l’échéance se rapproche, plus l’investisseur est assuré de recevoir ses paiements, ce qui entraîne une augmentation progressive de la valeur de l’obligation.</p>
<div id="ae9fe8b7" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb12-2">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb12-3">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb12-4">c1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb12-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb12-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb12-7"></span>
<span id="cb12-8">clean_prices1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb12-9">dirty_prices1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb12-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_c:</span>
<span id="cb12-11">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c1,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb12-12">    B_t_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> clean_price(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c1,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb12-13">    clean_prices1.append(B_t_clean)</span>
<span id="cb12-14">    dirty_prices1.append(B_t_dirty)</span>
<span id="cb12-15"></span>
<span id="cb12-16">plt.plot(grid_values_c,clean_prices1, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean prices"</span>)</span>
<span id="cb12-17">plt.plot(grid_values_c,dirty_prices1, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb12-18">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean prices vs Dirty prices"</span>)</span>
<span id="cb12-19">plt.legend()</span>
<span id="cb12-20">plt.grid()</span>
<span id="cb12-21">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>)</span>
<span id="cb12-22">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix de l'obligation"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>Text(0, 0.5, "Prix de l'obligation")</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-9-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="cas-2-c-0.05" class="level4">
<h4 class="anchored" data-anchor-id="cas-2-c-0.05">Cas 2 : $ c = 0.05 $</h4>
<p>Lorsque $ c = 5% $ et que ce coupon est supérieur au taux d’intérêt sans risque $ r $, la dynamique du prix de l’obligation suit une évolution inverse :</p>
<ul>
<li>Au départ, l’obligation est prisée au-dessus du nominal (elle se négocie avec une prime). Cela s’explique par le fait que son coupon généreux attire les investisseurs, qui considèrent que le rendement offert par l’obligation compense largement le risque de crédit et est plus attractif que les opportunités de placement à taux sans risque.<br>
</li>
<li>À mesure que l’échéance approche, la valeur de l’obligation se deprécie progressivement. En effet, à chaque période, l’investisseur reçoit un coupon élevé, mais à l’échéance, il ne récupère que le nominal $ N $, ce qui entraîne une correction progressive du prix de marché.<br>
</li>
<li>À la veille du remboursement, l’obligation converge vers $ N + c $ pour le dirty price (qui inclut le dernier coupon à verser) et vers $ N $ pour le clean price.</li>
</ul>
<p>Ainsi, cette obligation se déprécie progressivement jusqu’à l’échéance, car l’effet attractif du coupon élevé s’amenuise à mesure que le remboursement du capital nominal devient imminent. En d’autres termes, l’obligation part d’une valeur supérieure à son nominal mais perd progressivement sa prime à l’approche de l’échéance.</p>
<div id="d1ed4ba9" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb14-2">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb14-3">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb14-4">c2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb14-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb14-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb14-7"></span>
<span id="cb14-8">clean_prices2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb14-9">dirty_prices2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb14-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_c:</span>
<span id="cb14-11">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c2,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb14-12">    B_t_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> clean_price(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c2,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb14-13">    clean_prices2.append(B_t_clean)</span>
<span id="cb14-14">    dirty_prices2.append(B_t_dirty)</span>
<span id="cb14-15"></span>
<span id="cb14-16">plt.plot(grid_values_c,clean_prices2, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean prices"</span>)</span>
<span id="cb14-17">plt.plot(grid_values_c,dirty_prices2, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb14-18">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean prices vs Dirty prices"</span>)</span>
<span id="cb14-19">plt.legend()</span>
<span id="cb14-20">plt.grid()</span>
<span id="cb14-21">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>)</span>
<span id="cb14-22">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix de l'obligation"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>Text(0, 0.5, "Prix de l'obligation")</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-10-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="iii.-évolution-du-prix-en-fonction-du-taux-dintérêt" class="level2">
<h2 class="anchored" data-anchor-id="iii.-évolution-du-prix-en-fonction-du-taux-dintérêt">III. Évolution du prix en fonction du taux d’intérêt</h2>
<p>Le prix d’une obligation est une fonction décroissante du taux d’intérêt. En effet, plus le taux d’intérêt est élevé, plus la valeur actualisée des flux futurs (coupons et remboursement du nominal) est faible, ce qui réduit mécaniquement le prix de l’obligation.</p>
<p>Dans cette analyse, il est inutile de distinguer le dirty price et le clean price, car la différence entre les deux ne dépend pas du taux d’intérêt. De plus, en considérant $t = 0 <img src="https://latex.codecogs.com/png.latex?,%20il%20n'y%20a%20pas%20encore%20d%E2%80%99int%C3%A9r%C3%AAts%20courus%20(">c = 0 $), donc les deux prix coïncident.</p>
<section id="obligation-au-pair" class="level3">
<h3 class="anchored" data-anchor-id="obligation-au-pair">Obligation au pair</h3>
<p>Autour de $c - = 2% $, le prix de l’obligation est égal au nominal. Cela s’explique par le fait que le taux de coupon est exactement égal au taux de marché. L’obligation est alors dite “au pair”, car les investisseurs n’ont ni prime ni décote à appliquer sur son prix.</p>
</section>
<section id="explication-de-la-relation-négative-entre-prix-et-taux" class="level3">
<h3 class="anchored" data-anchor-id="explication-de-la-relation-négative-entre-prix-et-taux">Explication de la relation négative entre prix et taux</h3>
<p>La relation négative entre le prix d’une obligation et le taux d’intérêt s’explique par l’effet de substitution avec les nouvelles émissions obligataires.</p>
<ul>
<li>Lorsque les taux d’intérêt augmentent, de nouvelles obligations sont émises avec des coupons plus attractifs.<br>
</li>
<li>En conséquence, les obligations existantes, qui offrent un coupon fixe plus faible, deviennent moins intéressantes pour les investisseurs. Leur prix diminue afin d’ajuster leur rendement effectif au nouveau niveau des taux du marché.<br>
</li>
<li>Inversement, si les taux d’intérêt baissent, les obligations existantes deviennent plus attractives puisqu’elles offrent un coupon plus élevé que les nouvelles émissions, ce qui entraîne une hausse de leur prix sur le marché secondaire.</li>
</ul>
<p>Ainsi, la sensibilité d’une obligation aux variations de taux d’intérêt, appelée “duration”, est un élément clé dans l’évaluation du risque de taux et la gestion de portefeuille obligataire.</p>
<div id="badce34a" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb16-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb16-3">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb16-4">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb16-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb16-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb16-7"></span>
<span id="cb16-8">dirty_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb16-9">grid_values_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>) </span>
<span id="cb16-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_r:</span>
<span id="cb16-11">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N)</span>
<span id="cb16-12">    dirty_prices.append(B_t_dirty)</span>
<span id="cb16-13"></span>
<span id="cb16-14">plt.plot(grid_values_r,dirty_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb16-15">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix de l'obligation en fonction du taux d'intérêt"</span>)</span>
<span id="cb16-16">plt.legend()</span>
<span id="cb16-17">plt.grid()</span>
<span id="cb16-18">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span>
<span id="cb16-19">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix de l'obligation"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<pre><code>Text(0, 0.5, "Prix de l'obligation")</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-11-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="iv.-sensibilité-du-prix-de-lobligation-au-taux-dintérêt-duration" class="level2">
<h2 class="anchored" data-anchor-id="iv.-sensibilité-du-prix-de-lobligation-au-taux-dintérêt-duration">IV. Sensibilité du prix de l’obligation au taux d’intérêt / Duration</h2>
<p>La duration est une mesure de la sensibilité du prix d’une obligation aux variations du taux d’intérêt. Elle permet d’évaluer le risque de taux, c’est-à-dire l’impact d’une variation des taux sur la valeur de l’obligation.</p>
<blockquote class="blockquote">
<p>Dans le cas de l’évolution du prix en fonction du taux d’intérêt, la duration sera donc la pente de la courbe représentant cette relation.</p>
</blockquote>
<p>Mathématiquement, la duration est définie comme la dérivée du prix de l’obligation par rapport au taux d’intérêt :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cdelta%20=%20-%20%5Cfrac%7Bd%20B_t%7D%7Bd%20r%7D%20%5Ctimes%20%5Cfrac%7B1%7D%7BB_t%7D%0A"></p>
<p>En utilisant une approximation en différences finies, on exprime cette dérivée de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bd%20B_t%7D%7Bd%20r%7D%20%20%5Capprox%20%5Cfrac%7BB_t(r+%5CDelta%20r)%20-%20B_t(r)%7D%7B%5CDelta%20r%7D%0A"></p>
<p>Cette sensibilité permet de mesurer la variation du prix de l’obligation en réponse à une fluctuation du taux d’intérêt, offrant ainsi une évaluation directe du risque de taux auquel est exposé l’investisseur. De ce fait, si les taux d’intérêt bouge de <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r"> = 1%, alors les prix bougeront de -sensibilité * <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r"> .</p>
<p>Nous allons implémenter ce calcul en Python, en prenant <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r%20=%201"> bp (soit <img src="https://latex.codecogs.com/png.latex?0.0001"> en notation décimale).</p>
<div id="c2fbab8d" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sensivity_to_rate(t,c,T,r,lambda_,R,N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,dr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>):</span>
<span id="cb18-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Fonction qui calcule la sensibilité d'une obligation à un taux d'intérêt.</span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb18-5">    B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r,lambda_,R,N,dt)</span>
<span id="cb18-6">    B_t_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>dr,lambda_,R,N,dt)</span>
<span id="cb18-7">    sensivity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>((B_t_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> B_t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>dr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>B_t)</span>
<span id="cb18-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sensivity</span></code></pre></div>
</div>
<p>Si les taux d’intérêt bouge de <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r"> = 1%, alors les prix bougeront de -sensibilité * <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r"> = -8,64 * 1%. La duration va être souvent proche de la maturité.</p>
<div id="59cf8e12" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb19-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb19-3">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb19-4">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb19-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb19-6">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb19-7">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb19-8"></span>
<span id="cb19-9">sensivity_to_rate(t,c,T,r,lambda_,R,N)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="12">
<pre><code>np.float64(8.643982489102903)</code></pre>
</div>
</div>
<p>D’un point de vue graphique, il existe une certaine identité entre la maturité et la duration, car cette dernière peut être interprétée comme le barycentre des flux futurs de l’obligation. Plus ces flux sont concentrés dans le temps, plus leur pondération affecte la sensibilité du prix aux variations des taux d’intérêt.</p>
<section id="lien-entre-duration-et-maturité" class="level4">
<h4 class="anchored" data-anchor-id="lien-entre-duration-et-maturité">Lien entre duration et maturité</h4>
<ul>
<li>La duration est une approximation de la durée d’exposition au risque, ajustée en fonction des flux de paiements.<br>
</li>
<li>Elle est souvent proche de la maturité moyenne de l’obligation, bien que légèrement inférieure (environ 80% de la maturité totale, en fonction des conditions de marché et du niveau des coupons).</li>
</ul>
<p>Ainsi, plus l’échéance de l’obligation se rapproche, plus la duration tend à augmenter, car les flux futurs deviennent plus proches dans le temps, rendant l’obligation plus sensible aux variations des taux d’intérêt.</p>
<blockquote class="blockquote">
<p>Lorsque la maturité de l’obligation approche, sa sensibilité au taux d’intérêt tend à augmenter. En effet, la duration peut être interprétée comme une mesure du temps moyen pondéré pendant lequel l’investisseur est exposé au risque de taux.</p>
</blockquote>
<div id="ed567e4e" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb21-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb21-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb21-4">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb21-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb21-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-7"></span>
<span id="cb21-8">dirty_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb21-9">grid_values_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> T <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_T:</span>
<span id="cb21-11">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensivity_to_rate(t,c,T,r,lambda_,R,N)</span>
<span id="cb21-12">    dirty_prices.append(B_t_dirty)</span>
<span id="cb21-13"></span>
<span id="cb21-14">plt.plot(grid_values_T,dirty_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb21-15">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité en fonction de la maturité"</span>)</span>
<span id="cb21-16">plt.legend()</span>
<span id="cb21-17">plt.grid()</span>
<span id="cb21-18">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb21-19">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<pre><code>Text(0, 0.5, 'Sensibilité')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-14-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="cas-particulier-absence-de-coupon-de-taux-dintérêt-et-de-risque-de-défaut" class="level4">
<h4 class="anchored" data-anchor-id="cas-particulier-absence-de-coupon-de-taux-dintérêt-et-de-risque-de-défaut">Cas particulier : absence de coupon, de taux d’intérêt et de risque de défaut</h4>
<p>Lorsque le coupon, le taux d’intérêt et l’intensité de défaut sont nuls, la duration est exactement égale à la maturité de l’obligation. Puisque la duration peut être interprétée comme le barycentre des flux futurs de l’obligation, lorsque le coupon et l’intensité de défaut sont nuls, tous les flux sont concentrés à l’échéance, ce qui équivaut à la maturité de l’obligation.</p>
<div id="62d0b679" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb23-2">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10e-6</span></span>
<span id="cb23-3">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb23-4">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb23-5"></span>
<span id="cb23-6"></span>
<span id="cb23-7">dirty_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb23-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> T <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_T:</span>
<span id="cb23-9">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensivity_to_rate(t,c,T,r,lambda_,R,N)</span>
<span id="cb23-10">    dirty_prices.append(B_t_dirty)</span>
<span id="cb23-11"></span>
<span id="cb23-12">plt.plot(grid_values_T,dirty_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb23-13">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité au taux en fonction de la maturité"</span>)</span>
<span id="cb23-14">plt.legend()</span>
<span id="cb23-15">plt.grid()</span>
<span id="cb23-16">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb23-17">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<pre><code>Text(0, 0.5, 'Sensibilité')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-15-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>Dans le risque de taux, le principale indicateur est la duration.</p>
</blockquote>
</section>
</section>
<section id="v.-modèlisation-de-la-var" class="level2">
<h2 class="anchored" data-anchor-id="v.-modèlisation-de-la-var">V. Modèlisation de la VaR</h2>
<section id="v.1.-approche-par-la-sensibilité" class="level3">
<h3 class="anchored" data-anchor-id="v.1.-approche-par-la-sensibilité">V.1. Approche par la sensibilité</h3>
<p>Selon le modèle de Hull et White, le taux d’intérêt est modélisé par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Adr%20=%20%5Ctheta%20(%20%5Cmu%20-%20r)%20dt%20+%20%5Csigma%20dW%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> est le coefficient de vitesse de réversion, <img src="https://latex.codecogs.com/png.latex?%5Cmu"> est le taux d’intérêt moyen, <img src="https://latex.codecogs.com/png.latex?%5Csigma"> est la volatilité du taux d’intérêt et <img src="https://latex.codecogs.com/png.latex?dW"> est un mouvement brownien. Ce modèle a la spécifité d’être normale. De ce fait, <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r%20%5Csim%20N(0,%20%5Csigma%5E2%20%5CDelta%20t)">. En faisant l’approximation de la variation du prix, il est possible d’approcher une VaR par la sensibilité :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0ADuration%20&amp;=%20-%20%5Cfrac%7Bd%20B_t%7D%7Bd%20r%7D%20%5Ctimes%20%5Cfrac%7B1%7D%7BB_t%7D%20%5C%5C%0A%5Cfrac%7Bd%20B_t%7D%7BB_t%7D%20&amp;%5Capprox%20-%20Duration%20%5Ctimes%20%20%5CDelta%20r%20%5C%5C%0A%5Cend%7Baligned%7D%0A"></p>
<div id="c6303e61" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : écrire une fonction qui calcule la VaR avec l'approche par duration</span></span>
<span id="cb25-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb25-3"></span>
<span id="cb25-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sensitive_VaR(mu,sigma,t,c,T,r,lambda_,R,N,h,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>) :</span>
<span id="cb25-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb25-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb25-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb25-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb25-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb25-10">    Duration <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensivity_to_rate(t,c,T,r,lambda_,R,N,dt)</span>
<span id="cb25-11"></span>
<span id="cb25-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Duration <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(h) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span>
<span id="cb25-13"></span>
<span id="cb25-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#--------------------------------------</span></span>
<span id="cb25-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres du modèle de taux</span></span>
<span id="cb25-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------------------</span></span>
<span id="cb25-17"></span>
<span id="cb25-18">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb25-19">h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="cb25-20">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb25-21"></span>
<span id="cb25-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#--------------------------------------</span></span>
<span id="cb25-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de la valorisation du bond</span></span>
<span id="cb25-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------------------</span></span>
<span id="cb25-25"></span>
<span id="cb25-26">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb25-27">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb25-28">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb25-29">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb25-30">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb25-31">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb25-32">T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb25-33"></span>
<span id="cb25-34">VaR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensitive_VaR(mu,sigma,t,c,T,r,lambda_,R,N,h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>h,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>)</span>
<span id="cb25-35"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR estimé par l'approche par la sensibilité : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>VaR estimé par l'approche par la sensibilité : 5.8049%</code></pre>
</div>
</div>
<p>Pour cette estimation, nous avions supposé que la volatilité <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%201%5C%25">. Cependant, cette hypothèse pourrait s’écarter de la réalité. Pour ce faire, nous allons extraire les données de taux d’intérêt et calculer la volatilité empirique. L’ESTR est un taux un jour collatéralisé,c’est donc quasiment sans risque. Nous allons donc utiliser ce taux pour calculer la volatilité empirique. Pour cela, nous allons utiliser l’historique des taux d’intérêt de l’ESTR sur une période de 5 ans, i.e.&nbsp;10/03/2025 - 12/03/2020, disponible sur ce <a href="data/estr.xlsx">lien</a>.</p>
<p>Pour ce faire, nous utilisons la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma%20=%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn-1%7D%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20(r_i%20-%20%5Cbar%7Br%7D)%5E2%7D%0A"></p>
<p>Nous constatons que la volatilité empirique sur les 5 ans est de 25.45%. Cette valeur est très élevée, ce qui signifie que les taux d’intérêt ont connu des variations importantes sur cette période. Cela peut être du à la trop grande période utilisée pour la calibration de la volatilité. Pour ce faire, nous avons restreint la période à 1 an, i.e.&nbsp;10/03/2025 - 10/03/2024. La volatilité empirique sur cette période est de 1.04%. Cette valeur est plus cohérente avec les taux d’intérêt sans risque.</p>
<div id="698ebb08" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb27-2">estr_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_excel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/estr.xlsx"</span>, skiprows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb27-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#date as date</span></span>
<span id="cb27-4">estr_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(estr_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb27-5">estr_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> estr_df.set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>)</span>
<span id="cb27-6">estr_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> estr_df.sort_index()</span>
<span id="cb27-7"></span>
<span id="cb27-8">estr_df.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">PX_LAST</th>
<th data-quarto-table-cell-role="th">CHG_PCT_1D</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-12</td>
<td>-0.540</td>
<td>0.5525</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2020-03-13</td>
<td>-0.541</td>
<td>-0.1852</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-16</td>
<td>-0.536</td>
<td>0.9242</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2020-03-17</td>
<td>-0.531</td>
<td>0.9328</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-18</td>
<td>-0.529</td>
<td>0.3766</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="417607b8" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">vol_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(estr_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PX_LAST"</span>].pct_change())</span>
<span id="cb28-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité estimée sur 5 ans: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>vol_est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-3"></span>
<span id="cb28-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># volatilité sur 1 an</span></span>
<span id="cb28-5">vol_est<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(estr_df.loc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-03-10"</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-03-10"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PX_LAST"</span>].pct_change())</span>
<span id="cb28-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité estimée sur 1 an : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>vol_est<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité estimée sur 5 ans: 0.2545
Volatilité estimée sur 1 an : 0.0104</code></pre>
</div>
</div>
</section>
<section id="v.2.-approche-avec-un-repricing" class="level3">
<h3 class="anchored" data-anchor-id="v.2.-approche-avec-un-repricing">V.2. Approche avec un repricing</h3>
<p>Cette approche consiste à revaloriser, sous l’hypothèse de normalité des taux, l’obligation à l’instant <img src="https://latex.codecogs.com/png.latex?t"> pour un taux <img src="https://latex.codecogs.com/png.latex?r%20+%20%5CDelta%20r"> et de calculer la perte maximale possible. De ce fait, elle est plus précise que l’approche par la sensibilité.</p>
<p>Puisque dans le cas d’une obligation, ce dont on veut se prémunir c’est de la hausse des taux (puisqu’elle fait baisser le taux d’intérêt). De ce fait, la VaR est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVaR%7D%20=%20%20-%20%5Cfrac%7BB_t(r%20+%20%5CDelta%20r)%20-%20B_t%20%7D%7BB_t%7D,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5CDelta%20r"> est la variation des taux d’intérêt.</p>
<p>Par définition, la VaR estimée sera plus basse que l’autre approche en raison de la convexité de l’évolution du prix de l’obligation en fonction des taux d’intérêt. La Var par l’approche de la sensibilité, quant à elle, suppose une linéarité de l’évolution du prix de l’obligation en fonction des taux d’intérêt.</p>
<div id="f22cdcfd" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> repricing_VaR(mu,sigma,t,c,T,r,lambda_,R,N,h,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>) :</span>
<span id="cb30-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb30-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb30-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb30-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb30-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb30-7">    delta_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(h) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(alpha)</span>
<span id="cb30-8">    P_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r,lambda_,R,N,dt)</span>
<span id="cb30-9">    P_t_shocked <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>delta_r,lambda_,R,N,dt)</span>
<span id="cb30-10"></span>
<span id="cb30-11">    VAR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>( (P_t_shocked <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>P_t)</span>
<span id="cb30-12"></span>
<span id="cb30-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> VAR, delta_r</span>
<span id="cb30-14"></span>
<span id="cb30-15"></span>
<span id="cb30-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#--------------------------------------</span></span>
<span id="cb30-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres du modèle de taux</span></span>
<span id="cb30-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------------------</span></span>
<span id="cb30-19">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb30-20">h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="cb30-21">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb30-22"></span>
<span id="cb30-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#--------------------------------------</span></span>
<span id="cb30-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de la valorisation du bond</span></span>
<span id="cb30-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------------------</span></span>
<span id="cb30-26">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb30-27">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb30-28">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb30-29">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb30-30">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb30-31">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb30-32">T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb30-33"></span>
<span id="cb30-34">VaR, delta_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> repricing_VaR(mu,sigma,t,c,T,r,lambda_,R,N,h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>h,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>)</span>
<span id="cb30-35"></span>
<span id="cb30-36"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR estimé par l'approche par le réajustement : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-37"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Choc de taux : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>delta_r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>VaR estimé par l'approche par le réajustement : 5.6272%
Choc de taux : 0.67%</code></pre>
</div>
</div>
</section>
</section>
<section id="vi.-focus-risque-de-crédit-contrepartie" class="level2">
<h2 class="anchored" data-anchor-id="vi.-focus-risque-de-crédit-contrepartie">VI. Focus risque de crédit &amp; contrepartie</h2>
<p>Le risque de crédit est le risque que l’émetteur d’une obligation ne soit pas en mesure de respecter ses engagements financiers, entraînant ainsi des pertes pour les investisseurs. Ce risque est particulièrement pertinent dans le contexte des obligations, car il peut affecter la valeur de l’obligation et la capacité de l’investisseur à récupérer son capital. Le risque de contrepartie, quant à lui, est le risque que la contrepartie d’une transaction financière ne respecte pas ses obligations contractuelles. Dans le cas des obligations, cela peut se traduire par le non-paiement des coupons ou du capital à l’échéance. Ce risque est particulièrement important dans les transactions de gré à gré (OTC), où les parties ne sont pas soumises aux mêmes exigences réglementaires que celles des marchés organisés.</p>
<section id="vi.1.-comment-estimer-lintensité-de-défaut" class="level3">
<h3 class="anchored" data-anchor-id="vi.1.-comment-estimer-lintensité-de-défaut">VI.1. Comment estimer l’intensité de défaut ?</h3>
<p>Précedemment, nous avons valoriser l’obligation de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AB_t%20&amp;=%20C_t%20+%20N_t%20+%20R_t%20%5C%5C%0A&amp;=%20N%20%5Cleft%5B%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20%20c%20%5Ctimes%20e%5E%7B-(r%20+%20%5Clambda)%20%5Ctimes%20(T_i%0A-t)%7D%20%5Cmathbb%7B1%7D_%7BT_i%20%5Cgeq%20t%7D%20+%20e%5E%7B-(r+%5Clambda)(T-t)%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%20+%20%20%5Clambda%20R%20%5Ctimes%20%5Cfrac%7B1%20-%20e%5E%7B-(r+%5Clambda)(T-t)%7D%7D%7Br+%5Clambda%7D%20%5Cmathbb%7B1%7D_%7BT%20%5Cgeq%20t%7D%20%5Cright%5D%0A%5Cend%7Baligned%7D%0A"></p>
<p>et nous avons signfié la condition dans laquelle l’obligation émise vaut 100% du nominal, i.e.&nbsp;au pair, est <img src="https://latex.codecogs.com/png.latex?c%20%5Capprox%20r%20+%20%5Clambda">. Cependant, ce n’est pas exact.</p>
<p>Soit un coupon payé en continu, la valorisation de l’obligation est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AB_t%20&amp;=%20N%20%5Cleft%5B%20%5Cint_%7Bt%7D%5E%7BT%7D%20c%20e%5E%7B-(r%20+%20%5Clambda)%20%5Ctimes%20(u-t)%7D%20du%20+%20e%5E%7B-(r+%5Clambda)(T-t)%7D%20+%20%20%5Clambda%20R%20%5Ctimes%20%5Cfrac%7B1%20-%20e%5E%7B-(r+%5Clambda)(T-t)%7D%7D%7Br+%5Clambda%7D%20%5Cright%5D%5C%5C%0A&amp;=%20N%20%5Cleft%5B%20%5Cfrac%7Bc%7D%7Br%20+%20%5Clambda%7D%20%5Cleft(1%20-%20e%5E%7B-(r%20+%20%5Clambda)(T-t)%7D%20%5Cright)%20+%20e%5E%7B-(r+%5Clambda)(T-t)%7D%20+%20%20%5Clambda%20R%20%5Ctimes%20%5Cfrac%7B1%20-%20e%5E%7B-(r+%5Clambda)(T-t)%7D%7D%7Br+%5Clambda%7D%20%5Cright%5D%5C%5C%0AB_t%20=%20N%20&amp;%5CLeftrightarrow%20c%20=%20r%20+%20%5Clambda%20(1%20-%20R)%20%5C%5C%0A&amp;%20c%20-%20r%20=%20%5Clambda%20(1%20-%20R)%0A%5Cend%7Baligned%7D%0A"></p>
<p>De ce fait, en extrayant <img src="https://latex.codecogs.com/png.latex?c">, la condition dans laquelle l’obligation émise vaut 100% du nominal, i.e.&nbsp;au pair, est $c = r + (1 - R) $.</p>
<blockquote class="blockquote">
<p>💡 <img src="https://latex.codecogs.com/png.latex?s%20=%20c-r"> est la prime de crédit ou encore le spread de crédit. C’est la prime que l’investisseur demande pour le risque de crédit. Si <img src="https://latex.codecogs.com/png.latex?c%20%3E%20r">, l’obligation est émise à un prix supérieur à 100% du nominal. Si <img src="https://latex.codecogs.com/png.latex?c%20%3C%20r">, l’obligation est émise à un prix inférieur à 100% du nominal. Ce spread permet de faire la relation entre la PD exprimé par <img src="https://latex.codecogs.com/png.latex?%5Clambda"> et la LGD exprimé par <img src="https://latex.codecogs.com/png.latex?1%20-R">. Cette information est plus facile à avoir que l’intensité de défaut car le spread est coté sur le marché à travers les CDS. Par définition, on en déduit facilement que plus cettre prime est élevé, plus l’emetteur est risqué.</p>
</blockquote>
<blockquote class="blockquote">
<p>Take away : Les notions de duration et de spread sont très importants dans la modélisation du risque de taux</p>
</blockquote>
<section id="cas-de-largentine-pays-risqué" class="level4">
<h4 class="anchored" data-anchor-id="cas-de-largentine-pays-risqué">cas de l’argentine, pays risqué</h4>
<div id="12568131" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1031</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> </span>
<span id="cb32-2">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb32-3">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R)</span>
<span id="cb32-4">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb32-5"></span>
<span id="cb32-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Spread de crédit : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Recouvrement : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Intensité de défaut : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>lambda_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-9">PS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [np.exp( <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> T <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>)]</span>
<span id="cb32-10"></span>
<span id="cb32-11">plt.plot(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>),PS)</span>
<span id="cb32-12">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probabilité de survie en fonction de la maturité"</span>)</span>
<span id="cb32-13">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb32-14">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probabilité de survie"</span>)</span>
<span id="cb32-15">plt.grid()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Spread de crédit : 10.31%
Recouvrement : 40.00%
Intensité de défaut : 17.18%</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-20-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="cas-de-la-france" class="level4">
<h4 class="anchored" data-anchor-id="cas-de-la-france">cas de la France</h4>
<div id="0c027ef3" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cas de la France</span></span>
<span id="cb34-2">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> </span>
<span id="cb34-3">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb34-4">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R)</span>
<span id="cb34-5">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb34-6"></span>
<span id="cb34-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Spread de crédit : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb34-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Recouvrement : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb34-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Intensité de défaut : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>lambda_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb34-10">PS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [np.exp( <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> T <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>)]</span>
<span id="cb34-11"></span>
<span id="cb34-12">plt.plot(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>),PS)</span>
<span id="cb34-13">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probabilité de survie en fonction de la maturité"</span>)</span>
<span id="cb34-14">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb34-15">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probabilité de survie"</span>)</span>
<span id="cb34-16">plt.grid()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Spread de crédit : 0.32%
Recouvrement : 40.00%
Intensité de défaut : 0.54%</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-21-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En comparant les deux pays, l’Argentine et la France. On sait que l’argentine est un pays plus risqué que la France. Cela se voit également au niveau des spreads de crédit. En effet, le spread de l’Argentine est plus élevé que celui de la France. Cela signifie que les investisseurs demandent une prime de risque plus élevée pour investir dans des obligations argentines que dans des obligations françaises. De plus, en regardant la probabilité de survie des deux pays, on constate que la probabilité de survie de l’Argentine est plus faible que celle de la France. Cela signifie que les investisseurs considèrent que l’Argentine est plus susceptible de faire défaut que la France.</p>
</section>
</section>
<section id="vi.2-sensibilité-crédit" class="level3">
<h3 class="anchored" data-anchor-id="vi.2-sensibilité-crédit">VI.2 Sensibilité crédit</h3>
<div id="b2c7d27c" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sensivity_to_credit(t,c,T,r,lambda_,R,N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,dlambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>):</span>
<span id="cb36-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb36-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Fonction qui calcule la sensibilité d'une obligation à un taux d'intérêt.</span></span>
<span id="cb36-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb36-5">    B_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r,lambda_,R,N,dt)</span>
<span id="cb36-6">    B_t_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t,c,T,r,lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dlambda_,R,N,dt)</span>
<span id="cb36-7">    </span>
<span id="cb36-8">    sensivity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>((B_t_plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> B_t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>dlambda_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>B_t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>R))</span>
<span id="cb36-9"></span>
<span id="cb36-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sensivity</span></code></pre></div>
</div>
<div id="11fc057c" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb37-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb37-3">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb37-4">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb37-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb37-6">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb37-7">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb37-8"></span>
<span id="cb37-9">sensivity_to_credit(t,c,T,r,lambda_,R,N,dlambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="22">
<pre><code>np.float64(8.821190868023761)</code></pre>
</div>
</div>
<div id="bd8bd207" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb39-2">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb39-3">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb39-4">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb39-5">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb39-6">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb39-7"></span>
<span id="cb39-8">dirty_prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb39-9">grid_values_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb39-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> T <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grid_values_T:</span>
<span id="cb39-11">    B_t_dirty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensivity_to_credit(t,c,T,r,lambda_,R,N)</span>
<span id="cb39-12">    dirty_prices.append(B_t_dirty)</span>
<span id="cb39-13"></span>
<span id="cb39-14">plt.plot(grid_values_T,dirty_prices, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirty prices"</span>)</span>
<span id="cb39-15">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité crédit en fonction de la maturité"</span>)</span>
<span id="cb39-16">plt.legend()</span>
<span id="cb39-17">plt.grid()</span>
<span id="cb39-18">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>)</span>
<span id="cb39-19">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensibilité"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="23">
<pre><code>Text(0, 0.5, 'Sensibilité')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-24-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Soit le modèle normale pour le taux :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Adr_t%20=%20%5Ctheta%20(%5Cmu%20-%20r_t)%20dt%20+%20%5Csigma_r%20dW_t%0A"></p>
<p>et un modèle log normale pour le spread de crédit, puisqu’il ne peut être négatif : <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bds_t%7D%7Bs_t%7D%20=%20%5Csigma_s%20dZ_t%0A"></p>
<p>Ces deux modèles sont liés par <img src="https://latex.codecogs.com/png.latex?dW_t%20dZ_t%20=%20%5Crho%20dt">. On peut exprimer cette relation par <img src="https://latex.codecogs.com/png.latex?Z_t%20=%20%5Crho%20W_t%20+%20%5Csqrt%7B1%20-%20%5Crho%5E2%7D%20V_t">, où <img src="https://latex.codecogs.com/png.latex?V_t"> est un mouvement brownien standard indépendant de <img src="https://latex.codecogs.com/png.latex?W_t">. Cette corrélation est positive. &gt; 💡 si les taux d’intérêt montent, le risque de crédit augmente puisque les entreprises ont plus de mal à rembourser leur dette. De ce fait, le spread de crédit augmente.</p>
<p>Supposons qu’on cherche à calculer une VaR d’horizon <img src="https://latex.codecogs.com/png.latex?h">. Pour cela, il faudra faire des simulations de Monte Carlo pour les deux modèles.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ar_%7Bt+h%7D%20&amp;=%20r_t%20+%20%5Ctheta%20(%5Cmu%20-%20r_t)%20h%20+%20%5Csigma_r%20%5Csqrt%7Bh%7D%20W_t%20%5C%5C%0As_%7Bt+h%7D%20&amp;=%20s_t%20%5Cexp(%5Csigma_s%20%5Csqrt%7Bh%7D%20Z_t)%20%5C%5C%0Aou%20%5Cquad%20s_%7Bt+h%7D%20&amp;=%20s_t%20(1%20+%20%5Csigma_s%20%5Csqrt%7Bh%7D%20Z_t)%20%5Cquad%20(%5Ctext%7Bpar%20DL%7D)%0A%5Cend%7Baligned%7D%0A"></p>
<p>Posons les paramètres suivants :</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%200.1"> : le coefficient de vitesse de réversion</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%200.02"> : le taux d’intérêt moyen</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Csigma_r%20=%200.01"> : la volatilité du taux d’intérêt</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Csigma_s%20=%200.4"> : la volatilité du spread de crédit</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200.4"> : la corrélation entre les deux mouvements browniens</li>
<li><img src="https://latex.codecogs.com/png.latex?r%20=%200.02"> : le taux d’intérêt initial</li>
<li><img src="https://latex.codecogs.com/png.latex?h%20=%201/12"> : l’horizon de calcul de la VaR</li>
<li><img src="https://latex.codecogs.com/png.latex?c%20=%203%5C%25"> : le coupon annuel</li>
<li><img src="https://latex.codecogs.com/png.latex?R%20=%2040%5C%25"> : le taux de recouvrement</li>
<li><img src="https://latex.codecogs.com/png.latex?N%20=%201"> : le nominal de l’obligation</li>
<li><img src="https://latex.codecogs.com/png.latex?T%20=%2010"> : l’échéance de l’obligation</li>
</ul>
<div id="865b403b" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb41-2"></span>
<span id="cb41-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> MC_VaR(c,T,r,lambda_,R,N,h,sigma_r, sigma_s, rho, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, N_MC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb41-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("Parameters")</span></span>
<span id="cb41-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"coupon : {c:.2%}")</span></span>
<span id="cb41-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"maturity : {T} years")</span></span>
<span id="cb41-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"risk free rate : {r:.2%}")</span></span>
<span id="cb41-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"credit spread : {lambda_:.2%}")</span></span>
<span id="cb41-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"recovery rate : {R:.2%}")</span></span>
<span id="cb41-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"nominal : {N}")</span></span>
<span id="cb41-11"></span>
<span id="cb41-12">    prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb41-13">    </span>
<span id="cb41-14">    P_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r,lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dt)</span>
<span id="cb41-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"P0 : {P_0}")</span></span>
<span id="cb41-16"></span>
<span id="cb41-17">    s_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R)</span>
<span id="cb41-18">    r_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r</span>
<span id="cb41-19"></span>
<span id="cb41-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(N_MC):</span>
<span id="cb41-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate correlated Brownian motions</span></span>
<span id="cb41-22">        W1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal()</span>
<span id="cb41-23">        W2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.random.normal()</span>
<span id="cb41-24"></span>
<span id="cb41-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Euler discretization </span></span>
<span id="cb41-26">        r_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(h) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1</span>
<span id="cb41-27">        s_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ( <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(h) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W2)</span>
<span id="cb41-28">        lambda_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R)</span>
<span id="cb41-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("="*30)</span></span>
<span id="cb41-30">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("Parameters")</span></span>
<span id="cb41-31">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"coupon : {c:.2%}")</span></span>
<span id="cb41-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"maturity : {T} years")</span></span>
<span id="cb41-33">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"risk free rate : {r_h:.2%}")</span></span>
<span id="cb41-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"credit spread : {lambda_h:.2%}")</span></span>
<span id="cb41-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"recovery rate : {R:.2%}")</span></span>
<span id="cb41-36">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"nominal : {N}")</span></span>
<span id="cb41-37"></span>
<span id="cb41-38">        P_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pricing_bond(t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>h,c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>T,r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r_h,lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lambda_h,R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>R,N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dt)</span>
<span id="cb41-39">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"Price : {P_h}")</span></span>
<span id="cb41-40">        variation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (P_h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_0)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>P_0</span>
<span id="cb41-41">        prices.append(variation)</span>
<span id="cb41-42"></span>
<span id="cb41-43"></span>
<span id="cb41-44">    VaR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.quantile(prices, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha)</span>
<span id="cb41-45"></span>
<span id="cb41-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> VaR</span>
<span id="cb41-47"></span>
<span id="cb41-48">sigma_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb41-49">sigma_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb41-50">rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span></span>
<span id="cb41-51"></span>
<span id="cb41-52">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb41-53">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb41-54">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb41-55">R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb41-56">N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb41-57">h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="cb41-58">T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb41-59"></span>
<span id="cb41-60">VaR_monte_carlo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MC_VaR(c,T,r,lambda_,R,N,h,sigma_r, sigma_s, rho, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, N_MC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb41-61"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR estimé par Monte Carlo : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_monte_carlo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>VaR estimé par Monte Carlo : -6.3438%</code></pre>
</div>
</div>
<p>Lorsque $ $ (le coefficient de corrélation entre les taux d’intérêt et le spread de crédit) est positif, une hausse des taux d’intérêt entraîne également une hausse du spread de crédit. Cette dynamique amplifie le risque global, car les pertes dues à la hausse des taux s’ajoutent aux pertes induites par l’élargissement du spread de crédit. Ainsi, la Value at Risk (VaR) est plus élevée, reflétant l’accumulation des risques liés aux deux facteurs.</p>
<p>En revanche, lorsque $ $ est négatif, une hausse des taux d’intérêt tend à réduire le spread de crédit, et inversement. Il se crée alors un effet de compensation : les pertes générées par l’évolution des taux sont partiellement absorbées par les gains résultant de la contraction du spread (ou inversement). C’est le principe de diversification. Dans ce cas, la VaR est plus faible, car les effets du taux d’intérêt et du spread de crédit s’annulent en partie, réduisant ainsi l’ampleur des pertes potentielles.</p>
<div id="1fd2d3b6" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR en fonction de rho</span></span>
<span id="cb43-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm </span>
<span id="cb43-3"></span>
<span id="cb43-4">rhos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb43-5">VaRs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [MC_VaR(c,T,r,lambda_,R,N,h,sigma_r, sigma_s, rho, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, N_MC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,dt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> rho <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tqdm(rhos)]</span>
<span id="cb43-6"></span>
<span id="cb43-7">plt.plot(rhos,VaRs)</span>
<span id="cb43-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VaR en fonction de la corrélation"</span>)</span>
<span id="cb43-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rho"</span>)</span>
<span id="cb43-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VaR"</span>)</span>
<span id="cb43-11">plt.grid()</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>  0%|          | 0/100 [00:00&lt;?, ?it/s] 11%|█         | 11/100 [00:00&lt;00:00, 101.45it/s] 22%|██▏       | 22/100 [00:00&lt;00:00, 102.17it/s] 33%|███▎      | 33/100 [00:00&lt;00:00, 102.04it/s] 44%|████▍     | 44/100 [00:00&lt;00:00, 101.93it/s] 55%|█████▌    | 55/100 [00:00&lt;00:00, 102.33it/s] 66%|██████▌   | 66/100 [00:00&lt;00:00, 102.45it/s] 77%|███████▋  | 77/100 [00:00&lt;00:00, 102.70it/s] 88%|████████▊ | 88/100 [00:00&lt;00:00, 103.00it/s] 99%|█████████▉| 99/100 [00:00&lt;00:00, 103.20it/s]100%|██████████| 100/100 [00:00&lt;00:00, 102.64it/s]</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3_files/figure-html/cell-26-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="estimation-de-la-volatilité-du-spread-de-crédit" class="level4">
<h4 class="anchored" data-anchor-id="estimation-de-la-volatilité-du-spread-de-crédit">Estimation de la volatilité du spread de crédit</h4>
<p>Pour la volatilité du spread, nous avons supposé que <img src="https://latex.codecogs.com/png.latex?%5Csigma_s%20=%2040%5C%25">. Nous allons maintenant estimer cette volatilité à partir des données de marché, en utilisant l’historique des spreads de crédit sur une période de 5 ans, i.e.&nbsp;11/03/2025 - 11/03/2020, disponible sur ce <a href="data/cds.xlsx">lien</a>.</p>
<p>La volatilité du spread est une volatilité annualisée. De ce fait, la formulation de la volatilité empirique, lorsque la fréquence est quotidienne, est la suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_s%20=%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn-1%7D%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20(s_i%20-%20%5Cbar%7Bs%7D)%5E2%7D%20%5Ctimes%20%5Csqrt%7B255%7D%0A"></p>
<div id="59ad58fa" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1">cds_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_excel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cds.xlsx"</span>, skiprows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb45-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#date as date</span></span>
<span id="cb45-3">cds_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(cds_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb45-4">cds_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cds_df.set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>)</span>
<span id="cb45-5">cds_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cds_df.sort_index()</span>
<span id="cb45-6"></span>
<span id="cb45-7">cds_df.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="26">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">PX_LAST</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-11</td>
<td>30.772</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2020-03-12</td>
<td>36.977</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-13</td>
<td>37.869</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2020-03-16</td>
<td>44.432</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2020-03-17</td>
<td>49.923</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="72bac931" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb46-1">vol_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(cds_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PX_LAST"</span>].pct_change())</span>
<span id="cb46-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité estimée sur 5 ans: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>vol_est<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb46-3"></span>
<span id="cb46-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># volatilité sur 1 an</span></span>
<span id="cb46-5">vol_est<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(cds_df.loc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-03-11"</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-03-11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PX_LAST"</span>].pct_change())</span>
<span id="cb46-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité estimée sur 1 an : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>vol_est<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité estimée sur 5 ans: 0.4512
Volatilité estimée sur 1 an : 0.5181</code></pre>
</div>
</div>
</section>
</section>
</section>
<section id="asset-management-autres-types-de-risques" class="level1">
<h1>Asset management : autres types de risques</h1>
<p>En dehors du risque de crédit et du risque de marché, il existe d’autres types de risques qui peuvent affecter les institutions financières et les marchés. Deux d’entre eux sont le risque de modèle et le risque climatique.</p>
<section id="risque-de-modèle" class="level2">
<h2 class="anchored" data-anchor-id="risque-de-modèle">Risque de modèle</h2>
<p>Le risque de modèle est le risque associé à une mauvaise utilisation d’un modèle dans le processus de prise de décision. Il peut être causé par :</p>
<ul>
<li>une incertitude sur les paramètres,</li>
<li>une qualité insuffisante des données,</li>
<li>ou une inadéquation structurelle du modèle par rapport à la réalité observée.</li>
</ul>
<p>Ce risque peut conduire à des erreurs de prévision, à des décisions inappropriées ou à des pertes financières. Il est donc essentiel de bien comprendre les hypothèses et limites des modèles employés.</p>
<p>C’est une activité où l’intervention humaine reste indispensable : il est nécessaire de procéder à des <em>sanity checks</em>, du <em>backtesting</em>, et de challenger les modèles à l’aide de versions plus simples (<em>dégénérées</em>) ou plus riches, afin d’en comparer les résultats et de mieux cerner leurs faiblesses.</p>
<p>Pour réduire le risque de modèle, il est courant de mettre en place une fonction indépendante de validation, généralement assurée par les équipes MRM (Model Risk Management). Ces dernières sont chargées :</p>
<ul>
<li>d’auditer les modèles,</li>
<li>de mener des tests de robustesse,</li>
<li>et de recommander des provisions en cas de risque de surévaluation des actifs.</li>
</ul>
<p>Les compétences clés pour cette activité incluent : - les mathématiques financières, - la connaissance des marchés financiers et des modèles de risque, - la programmation et l’utilisation d’outils quantitatifs, - ainsi que des aptitudes à la communication, au travail en équipe et au raisonnement critique.</p>
</section>
<section id="risque-climatique" class="level2">
<h2 class="anchored" data-anchor-id="risque-climatique">Risque climatique</h2>
<p>Le risque climatique désigne les impacts potentiels des changements climatiques et des politiques environnementales sur les entreprises et les marchés financiers. Contrairement aux risques classiques, les trajectoires de référence sont définies par des organismes scientifiques comme le GIEC, ce qui limite l’appropriation directe des modèles par les institutions financières. Il s’agit donc d’un domaine où le risque de modèle est indirect.</p>
<p>Le réseau NGFS (Network for Greening the Financial System) a été mis en place pour aider les régulateurs et les banques centrales à mieux intégrer les risques climatiques dans leurs cadres prudentiels.</p>
<p>À l’heure actuelle, il n’existe pas de consensus clair sur la manière d’intégrer pleinement le risque climatique dans les modèles financiers ; il s’agit plutôt de tentatives progressives d’adaptation. Le NGFS propose plusieurs scénarios climatiques, parmi lesquels :</p>
<ul>
<li>Current Policies : continuité des politiques actuelles sans nouvel engagement.</li>
<li>NDC (Nationally Determined Contributions) : politiques actuelles + engagements annoncés par les États.</li>
<li>Disorderly Transition (1.5°C) : les engagements sont mis en œuvre avec retard ou de façon désorganisée.</li>
<li>Net Zero / 2°C : scénario optimisé pour limiter le réchauffement à 2°C — le cadre le plus ambitieux et le plus stable.</li>
</ul>
<p>On distingue généralement deux types de risques climatiques :</p>
<section id="risque-physique" class="level3">
<h3 class="anchored" data-anchor-id="risque-physique">1. Risque physique</h3>
<p>Ce risque est lié aux conséquences directes des changements climatiques sur les infrastructures et l’environnement : - Risques aigus : événements extrêmes (inondations, sécheresses, tempêtes, etc.). - Risques chroniques : évolutions lentes (élévation du niveau des mers, hausse des températures, etc.).</p>
<p>Ces risques peuvent entraîner : - des pertes matérielles, - des interruptions d’activité, - des coûts de réparation et d’adaptation.</p>
</section>
<section id="risque-de-transition" class="level3">
<h3 class="anchored" data-anchor-id="risque-de-transition">2. Risque de transition</h3>
<p>Ce risque est associé aux mesures prises pour réduire les émissions de gaz à effet de serre et passer à une économie bas carbone. Il peut provoquer : - des pertes de valeur sur certains actifs, - des coûts de transition élevés, - des bouleversements sectoriels et technologiques.</p>
<p>Il comprend deux composantes : - le risque politique (durcissement réglementaire, interdictions, fiscalité verte, etc.), - et le risque/opportunité technologique (émergence de nouvelles technologies, changement dans les préférences de consommation, etc.).</p>
<p>On peut formuler ce risque comme une relation :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BRisque%20de%20transition%7D%20=%20%5Ctext%7BRisque%20politique%7D%20-%20%5Ctext%7BOpportunit%C3%A9s%20technologiques%7D%0A"></p>
<p>Ainsi, une entreprise bien positionnée sur les technologies vertes peut compenser tout ou partie du risque politique subi.</p>
<ul>
<li>Le risque physique est plus élevé dans les scénarios Current Policies, NDC, et Disorderly Transition, car ils impliquent une action climatique insuffisante ou retardée.</li>
<li>À l’inverse, le risque de transition est plus important dans les scénarios ambitieux comme Net Zero, où les ajustements politiques et économiques sont rapides et profonds.</li>
</ul>


</section>
</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>asset-management</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-3.html</guid>
  <pubDate>Sat, 22 Mar 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Projet de gestion de risques multiples</title>
  <dc:creator>Cheryl Kouadio</dc:creator>
  <dc:creator>Mariyam Ouyassin</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules.html</link>
  <description><![CDATA[ 




<p>Les banques jouent un rôle central dans l’économie nationale et internationale. En effet, elles assurent l’intermédiation entre les agents disposant d’un excédent de financement et ceux ayant un besoin de financement, facilitant ainsi les transactions et soutenant l’investissement, et donc la croissance économique. Toutefois, cette activité les expose à divers risques majeurs, notamment le risque de crédit, le risque de liquidité et le risque de marché. La quantification de ces risques est essentielle pour permettre aux institutions bancaires de s’en prémunir et de les surveiller efficacement. Dans ce cadre, la Value-at-Risk (VaR) s’impose comme une mesure de référence. Elle permet d’évaluer la perte potentielle maximale qu’une institution pourrait subir, avec un certain niveau de confiance, sur un horizon temporel donné et pour un portefeuille spécifique.</p>
<p>Dans l’analyse de la VaR, un portefeuille est souvent composé d’au moins deux créances. Il est donc indispensable de prendre en compte les dépendances entre les facteurs de risque. Une approche classique consiste à supposer que le vecteur des risques individuels suit une distribution normale multivariée et à utiliser le coefficient de corrélation linéaire de Pearson comme mesure de dépendance. Cependant, cette hypothèse est souvent trop restrictive en finance : les distributions des facteurs de risque ne sont pas nécessairement gaussiennes et le coefficient de Pearson ne permet pas toujours de capturer les structures de dépendance non linéaires. De plus, cette mesure de corrélation est pertinente uniquement dans un cadre gaussien, qui représente rarement les dynamiques financières réelles.</p>
<p>Dans ce contexte, la théorie des copules constitue un outil statistique puissant permettant de modéliser la dépendance entre les risques sans se limiter à l’hypothèse de normalité. Une copule est une fonction qui caractérise la structure de dépendance entre plusieurs variables aléatoires indépendamment de leurs distributions marginales. En séparant la modélisation des distributions marginales et celle de la dépendance conjointe, les copules offrent une flexibilité accrue pour l’analyse du risque et permettent de mieux représenter les interactions entre les actifs financiers.</p>
<p>L’objectif de ce projet est d’évaluer le risque de crédit en calculant une CreditVaR avec un niveau de confiance de 99% sur un portefeuille composé de deux obligations bancaires. Ces obligations, bien que différentes en termes de subordination et de risque de recouvrement, appartiennent au même secteur, ce qui accroît le risque global du portefeuille. Il est donc essentiel de modéliser adéquatement la dépendance entre ces actifs pour obtenir une estimation réaliste du risque de crédit.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Le rapport de ce projet est disponible <a href="../rapports/Rapport_Kouadio_Ouyassin_copules.pdf">ici</a>.</p>
</div>
</div>
<section id="i.-données" class="level1">
<h1>I. Données</h1>
<p>Nous disposons des prix des actions des composantes d’un portefeuille constitué de deux créances issues du secteur bancaire, ayant un même notionnel de <img src="https://latex.codecogs.com/png.latex?N%20=%201000"> euros et une maturité de <img src="https://latex.codecogs.com/png.latex?T%20=%204"> ans. En l’absence de lignes de crédit, l’exposition en cas de défaut de ces banques est égale au notionnel, soit <img src="https://latex.codecogs.com/png.latex?EAD%20=%20N%20=%201000"> euros.</p>
<p>La première obligation, de type senior, est émise par BNP et présente un taux de recouvrement moyen de 60% ainsi qu’une volatilité de 15%. La seconde, de type junior (ou subordonnée), est émise par la Société Générale et se caractérise par un taux de recouvrement moyen de 30% et une volatilité de 25%. On suppose qu’il n’existe aucune dépendance entre les taux de recouvrement.</p>
<p>Par ailleurs, nous disposons d’informations sur les spreads des Credit Default Swaps (CDS) des deux banques, qui permettent d’évaluer le risque de crédit en fonction de la maturité des actifs. En effet, les CDS sont des contrats d’assurance contre le défaut d’une entreprise. Le spread d’un CDS représente le coût de cette assurance : plus ce spread est élevé, plus le marché perçoit l’entreprise comme risquée.</p>
<p>On considère que les spreads de CDS de BNP et de Société Générale pour une maturité de <img src="https://latex.codecogs.com/png.latex?T%20=%204"> ans sont respectivement de 100 bps et 120 bps, tandis que pour une maturité de <img src="https://latex.codecogs.com/png.latex?T%20=%205"> ans, ils s’élèvent à 120 bps et 150 bps. Ces CDS ont été évalués avec un taux de recouvrement <img src="https://latex.codecogs.com/png.latex?R"> fixé à 40%.</p>
<p>Puisque les spreads de Société Générale sont systématiquement plus élevés, quelle que soit la maturité, on en déduit que l’obligation émise par SG est plus risquée que celle émise par BNP. Cette observation est ainsi cohérente avec la hiérarchie des obligations considérées.</p>
<p>Pour extraire les probabilités de défauts, nous allons utilisé les CDS étant plus liquides que les obligations. Les CDS sont des contrats d’assurance contre le défaut d’une entreprise. Le spread de CDS est le prix de cette assurance. Plus le spread est élevé, plus le marché estime que l’entreprise est risquée. Le fair spread est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0As*%20=%20(1-%5Cdelta)%20%5Ctimes%20%5Clambda%0A"></p>
<p>Avec <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> le taux de recouvrement et <img src="https://latex.codecogs.com/png.latex?%5Clambda"> le hazard rate. Le hazard rate est la probabilité de défaut de l’entreprise. On peut donc isoler le hazard rate :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clambda%20=%20%5Cfrac%7Bs*%7D%7B1-%5Cdelta%7D%0A"></p>
<p>De ce fait, on peut calculer la probabilité de défaut donnée par : <img src="https://latex.codecogs.com/png.latex?%0APD%20=%20P(%5Ctau%20%3C%20T)%20=%201%20-%20e%5E%7B-%5Clambda%20%5Ctimes%20T%7D%0A"></p>
<p>car <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Csim%20Exp(%5Clambda)"></p>
<div id="efb409ff" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Noms des entreprises</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4">entreprises <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>]</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> PD(T,spread,Recouvrement):</span>
<span id="cb1-7">    lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spread <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Recouvrement)</span>
<span id="cb1-8">    PD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>T)</span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> PD,lambda_</span>
<span id="cb1-10"></span>
<span id="cb1-11">spread_4Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 100 et 120 points de base</span></span>
<span id="cb1-12">spread_5Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 120 et 150 points de base</span></span>
<span id="cb1-13">tx_recouvrement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">proba_defaut_quatre <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(spread_4Y))</span>
<span id="cb1-16">proba_defaut_cinq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(spread_5Y))</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(spread_4Y)) :</span>
<span id="cb1-19">    proba_defaut_quatre[i],_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PD(spread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spread_4Y[i],T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,Recouvrement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb1-20">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(_)</span>
<span id="cb1-21">    proba_defaut_cinq[i],_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PD(spread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spread_5Y[i],T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,Recouvrement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb1-22">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(_)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création d'un dictionnaire pour stocker les probabilités de défaut</span></span>
<span id="cb1-25">probabilites_defaut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remplir le dictionnaire</span></span>
<span id="cb1-28"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, entreprise <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(entreprises):</span>
<span id="cb1-29">    probabilites_defaut[entreprise] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4 ans"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(proba_defaut_quatre[i]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb1-31">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5 ans"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(proba_defaut_cinq[i]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-32">    }</span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher le dictionnaire</span></span>
<span id="cb1-35">pprint(probabilites_defaut)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>0.016666666666666666
0.02
0.02
0.025
{'BNP': {'4 ans': np.float64(6.4493), '5 ans': np.float64(9.5163)},
 'SG': {'4 ans': np.float64(7.6884), '5 ans': np.float64(11.7503)}}</code></pre>
</div>
</div>
<p>Nous constatons que les probabilités de défaut pour les deux entreprises pour une même maturité sont généralement plus élevés pour la Société Générale que pour BNP Paribas. Cela est cohérent avec le type d’obligation émise par les deux entreprises. En effet, une obligation junior est plus risquée qu’une obligation senior.</p>
</section>
<section id="ii.-caractérisation-les-distributions-des-taux-de-recouvrement-de-chacune-des-deux-créances" class="level1">
<h1>II. Caractérisation les distributions des taux de recouvrement de chacune des deux créances</h1>
<p>Pour caractériser les distributions des taux de recouvrement, on va utiliser la loi Beta(a,b). La loi Beta est une loi de probabilité continue définie sur l’intervalle [0, 1]. Elle est souvent utilisée pour modéliser des variables aléatoires qui prennent des valeurs dans un intervalle borné en forme de U ce qui est le cas des taux de recouvrement. L’expression de sa densité est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)%20=%20%5Cfrac%7Bx%5E%7Ba-1%7D%20%5Ctimes%20(1-x)%5E%7Bb-1%7D%7D%7BB(a,b)%7D%201_%7B%5B0,1%5D%7D(x)%0A"></p>
<p>Puisque nous possédons des taux de recouvrement de moyenne 60% et de volatilité 15% pour la première créance et de moyenne 30% et de volatilité 25% pour la seconde créance, nous allons utiliser la méthode des moments pour déterminer les paramètres a et b de la loi Beta. De ce fait, les paramètres sont calibrés de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aa%20=%20%5Cfrac%7B%5Cmu%5E2%20%5Ctimes%20(1-%5Cmu)%7D%7B%5Csigma%5E2%7D%20-%20%5Cmu;%20%5Cquad%20b%20=%20%5Cfrac%7B%5Cmu%5E2%20%5Ctimes%20(1-%5Cmu)%7D%7B%20%5Cmu%20%5Csigma%5E2%7D%20-%20(1-%5Cmu%20)%0A"></p>
<p>La première est une obligation BNP senior de taux de recouvrement de moyenne 60% et de volatilité 15%, et la seconde est une obligation Société Générale junior (ou subordonnée) de taux de recouvrement de moyenne 30% et de volatilité 25%.</p>
<section id="ii.1.-caractérisation-de-la-distribution-des-tx-de-recouvrement-bnp" class="level3">
<h3 class="anchored" data-anchor-id="ii.1.-caractérisation-de-la-distribution-des-tx-de-recouvrement-bnp">II.1. Caractérisation de la distribution des tx de recouvrement BNP</h3>
<div id="d84ea86b" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb3-2"></span>
<span id="cb3-3">tx_recouvrement_BNP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span></span>
<span id="cb3-4">vol_recouvrement_BNP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calibration_beta(mu,sigma):</span>
<span id="cb3-7">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((mu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu</span>
<span id="cb3-8">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((mu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (mu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>mu)</span>
<span id="cb3-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a,b</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># def moment_beta(moyenne, variance):</span></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     alpha = (moyenne * (1 - moyenne) / variance**2 - 1) * moyenne</span></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     beta = (moyenne * (1 - moyenne) / variance**2 - 1) * (1 - moyenne)</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     return alpha, beta</span></span>
<span id="cb3-15"></span>
<span id="cb3-16">a_BNP,b_BNP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calibration_beta(tx_recouvrement_BNP,vol_recouvrement_BNP)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a_BNP = "</span>, a_BNP)</span>
<span id="cb3-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b_BNP = "</span>, b_BNP)</span>
<span id="cb3-20"></span>
<span id="cb3-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># densité beta</span></span>
<span id="cb3-22"></span>
<span id="cb3-23"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> beta</span>
<span id="cb3-24"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb3-25"></span>
<span id="cb3-26">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb3-27">y_BNP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> beta.pdf(x,a_BNP,b_BNP)</span>
<span id="cb3-28"></span>
<span id="cb3-29">plt.plot(x,y_BNP)</span>
<span id="cb3-30">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Densité de probabilité du taux de recouvrement de BNP"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>a_BNP =  5.8
b_BNP =  3.8666666666666676</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="2">
<pre><code>Text(0.5, 1.0, 'Densité de probabilité du taux de recouvrement de BNP')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-3-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Nous constatons que la distribution des taux de recouvrement de BNP est légèrement asymétrique à droite. En effet, le paramètre a, contrôle la concentration des valeurs proches de 1, est supérieur au paramètre b, contrôle la concentration des valeurs proches de 0. De ce fait, la distribution est asymétrique à gauche. Cependant que la majorité des valeurs sont concentrées autour de la moyenne et il y a une <strong>faible probabilité de recouvrement très bas ou très élevé</strong>.</p>
</section>
<section id="ii.2.-caractérisation-de-la-distribution-des-tx-de-recouvrement-sg" class="level3">
<h3 class="anchored" data-anchor-id="ii.2.-caractérisation-de-la-distribution-des-tx-de-recouvrement-sg">II.2. Caractérisation de la distribution des tx de recouvrement SG</h3>
<div id="5aa8bf60" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">tx_recouvrement_SG <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb6-2">vol_recouvrement_SG <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"></span>
<span id="cb6-5">a_SG,b_SG <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calibration_beta(tx_recouvrement_SG,vol_recouvrement_SG)</span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a_SG = "</span>, a_SG)</span>
<span id="cb6-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b_SG = "</span>, b_SG)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># densité beta</span></span>
<span id="cb6-11">y_SG <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> beta.pdf(x,a_SG,b_SG)</span>
<span id="cb6-12"></span>
<span id="cb6-13">plt.plot(x,y_SG)</span>
<span id="cb6-14">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Densité de probabilité du taux de recouvrement de BNP"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>a_SG =  0.708
b_SG =  1.652</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>Text(0.5, 1.0, 'Densité de probabilité du taux de recouvrement de BNP')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-4-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En ce qui concerne la distribution des taux de recouvrement de la Société Générale, nous constatons une asymétrie à gauche également.</p>
<p>La densité est <strong>très asymétrique</strong>, décroissant fortement à partir de 0. On constate une forte <strong>concentration des valeurs proches de 0</strong> indique un <strong>risque élevé de faible recouvrement</strong>. La longue traîne vers la droite signifie que, bien que des recouvrements plus élevés soient possibles, ils sont peu probables.</p>
</section>
<section id="ii.3.-comparaison-des-distributions" class="level2">
<h2 class="anchored" data-anchor-id="ii.3.-comparaison-des-distributions">II.3. Comparaison des distributions</h2>
<div id="ae65be4d" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb9-2"></span>
<span id="cb9-3">plt.plot(x, y_BNP, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BNP'</span>)</span>
<span id="cb9-4">plt.plot(x, y_SG, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SG'</span>)</span>
<span id="cb9-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Densités de probabilité des taux de recouvrement"</span>)</span>
<span id="cb9-6">plt.legend()</span>
<span id="cb9-7">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li><p><strong>BNP senior</strong> : L’obligation senior bénéficie d’un <strong>taux de recouvrement plus élevé et plus stable</strong>, donc <strong>moins risqué</strong>.</p></li>
<li><p><strong>SG junior</strong> : L’obligation junior a une <strong>probabilité élevée d’un recouvrement très faible</strong>, ce qui reflète un <strong>risque plus important</strong>.</p></li>
</ul>
<p>Cela est cohérent avec la catégorisation des obligations. Dans la hiérarchie des dettes, une obligation peut être classée comme senior ou junior (subordonnée) en fonction de la priorité de remboursement en cas de faillite de l’émetteur. Cette distinction est essentielle pour évaluer le risque de crédit et le taux de recouvrement attendu. Une obligation classée senior est moins risquée qu’une obligation junior, car elle est remboursée en premier en cas de défaut de l’émetteur, cependant le rendement attendu est moins élevé. Par conséquent, les obligations senior ont un taux de recouvrement plus élevé et plus stable que les obligations junior.</p>
</section>
</section>
<section id="iii.-caractérisation-des-lois-de-défauts" class="level1">
<h1>III. Caractérisation des lois de défauts</h1>
<p>Pour caractériser les lois de défaut, nous avons besoin de prendre en compte la structure de dépendance en plus des lois marginales présentées dans la section I. Pour ce faire, nous devons utiliser un produit dnt le prix dépendant de la dépendance entre les défauts des deux entreprises. Dans notre cas, nous utiliserons le prix des actions des deux entreprises pour déterminer la dépendance entre les défauts.</p>
<section id="iii.1.-analyse-exploratoire-univariée-des-données-actions-de-ces-deux-entreprises." class="level2">
<h2 class="anchored" data-anchor-id="iii.1.-analyse-exploratoire-univariée-des-données-actions-de-ces-deux-entreprises.">III.1. Analyse exploratoire univariée des données actions de ces deux entreprises.</h2>
<p>En observant le prix des actions BNP et SG, nous constatons que les actions BNP ont un prix plus élevés que les actions SG. Cela est cohérent avec la capitalisation boursière des deux entreprises. De plus, nous constatons que les rendements de BNP sont semblables à ceux de SG. Cela est cohérent avec le fait que les deux entreprises sont des banques françaises et sont donc exposées aux mêmes risques macroéconomiques. Néanmoins, les actions de BNP présentent une volatilité légèrement plus élevé que celles de SG.</p>
<div id="687b9a36" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read data.txt</span></span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb10-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb10-4">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data_copules.txt'</span>, sep<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-5">data.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">BNP</th>
<th data-quarto-table-cell-role="th">SG</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>42.36</td>
<td>55.24</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>42.72</td>
<td>55.59</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>43.20</td>
<td>56.45</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>42.67</td>
<td>55.55</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>41.81</td>
<td>54.50</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="b3c88a9e" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb11-2">plt.plot(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BNP'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BNP'</span>)</span>
<span id="cb11-3">plt.plot(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SG'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SG'</span>)</span>
<span id="cb11-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix des actions"</span>)</span>
<span id="cb11-5">plt.legend()</span>
<span id="cb11-6">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-7-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="034da347" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">returns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame()</span>
<span id="cb12-2">returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>].pct_change().dropna()</span>
<span id="cb12-3">returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>].pct_change().dropna()</span>
<span id="cb12-4"></span>
<span id="cb12-5">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), sharey<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Premier sous-graphique : Rendements BNP</span></span>
<span id="cb12-8">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab:blue'</span>)</span>
<span id="cb12-9">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rendements BNP"</span>)</span>
<span id="cb12-10">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-11"></span>
<span id="cb12-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Deuxième sous-graphique : Rendements SG</span></span>
<span id="cb12-13">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].plot(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab:orange'</span>)</span>
<span id="cb12-14">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rendements SG"</span>)</span>
<span id="cb12-15">axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-16"></span>
<span id="cb12-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ajustement automatique pour éviter les chevauchements</span></span>
<span id="cb12-18">plt.tight_layout()</span>
<span id="cb12-19"></span>
<span id="cb12-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage</span></span>
<span id="cb12-21">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-8-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="1632167f" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">returns.describe()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">BNP</th>
<th data-quarto-table-cell-role="th">SG</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">count</td>
<td>999.000000</td>
<td>999.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">mean</td>
<td>-0.000590</td>
<td>-0.000473</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">std</td>
<td>0.024340</td>
<td>0.020690</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">min</td>
<td>-0.116199</td>
<td>-0.093616</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">25%</td>
<td>-0.013771</td>
<td>-0.011760</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">50%</td>
<td>-0.000358</td>
<td>-0.000547</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">75%</td>
<td>0.012605</td>
<td>0.011286</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">max</td>
<td>0.086786</td>
<td>0.079479</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="iii.2.-modélisation-des-distributions-univariées-des-facteurs-de-risques" class="level2">
<h2 class="anchored" data-anchor-id="iii.2.-modélisation-des-distributions-univariées-des-facteurs-de-risques">III.2. Modélisation des distributions univariées des facteurs de risques</h2>
<p>Dans le cadre des prix des actions, le seul facteur de risque est le rendement. Nous allons donc modéliser les rendements des actions de BNP et SG par des lois normales, lois student, skew student, et Normal Inverse Gaussian afin de déterminer la loi qui s’ajuste le mieux aux données.</p>
<section id="iii.2.1.-modélisation-des-rendements-de-bnp" class="level3">
<h3 class="anchored" data-anchor-id="iii.2.1.-modélisation-des-rendements-de-bnp">III.2.1. Modélisation des rendements de BNP</h3>
<p>En ce qui concerne les rendements de BNP, nous constatons que les lois de student et normal inverse gaussian sont les plus adaptées pour modéliser les rendements de BNP. En effet, lorsqu’on compare les QQ-plot des rendements de BNP avec les lois normales, student, skew student et normal inverse gaussian, on constate que les quantiles empiriques des rendements de BNP sont plus proches des quantiles théoriques des lois student et normal inverse gaussian. De plus, les densités ajustées semblent également mieux coller aux données.</p>
<p>Si l’on devait choisir une loi pour modéliser les rendements de BNP, nous choisirions la loi normal inverse gaussian. En effet, bien qu’elle soit plus complexe à modéliser, elle permet d’avoir un p-value, au test de Kolmogorov-Smirnov, plus élevé que la loi student. Cela signifie que la loi normal inverse gaussian est plus adaptée pour modéliser les rendements de BNP.</p>
<div id="0031cf02" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb14-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation de données pour l'exemple (remplace par tes données réelles)</span></span>
<span id="cb14-6">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>]</span>
<span id="cb14-7"></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création de la figure et des axes pour 4 subplots (2 lignes, 2 colonnes)</span></span>
<span id="cb14-9">fig, axs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb14-10"></span>
<span id="cb14-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi normale ########################</span></span>
<span id="cb14-12">params_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norm.fit(data)</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histogramme avec densité théorique de la loi normale (subplot 0,0)</span></span>
<span id="cb14-15">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(data), np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb14-16">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb14-17">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.norm.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_norm), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Normal Distribution'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb14-18">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi normale'</span>)</span>
<span id="cb14-19">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb14-20"></span>
<span id="cb14-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot (subplot 0,1)</span></span>
<span id="cb14-22">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"norm"</span>,sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_norm), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb14-23">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi normale'</span>)</span>
<span id="cb14-24"></span>
<span id="cb14-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi de student ########################</span></span>
<span id="cb14-26"></span>
<span id="cb14-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation des paramètres de la distribution de Student pour vos données.</span></span>
<span id="cb14-28">params_std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.t.fit(data)</span>
<span id="cb14-29"></span>
<span id="cb14-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histogramme avec densité théorique de la loi de Student.</span></span>
<span id="cb14-31">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(data), np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb14-32">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb14-33">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.t.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_std), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Fitted t-Distribution'</span>,color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'orange'</span>)</span>
<span id="cb14-34">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi de student'</span>)</span>
<span id="cb14-35">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb14-36"></span>
<span id="cb14-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot avec une loi de Student.</span></span>
<span id="cb14-38">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_std), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb14-39">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi de student'</span>)</span>
<span id="cb14-40"></span>
<span id="cb14-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi de Normal Inverse Gaussian ########################</span></span>
<span id="cb14-42">params_nig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norminvgauss.fit(data)</span>
<span id="cb14-43"></span>
<span id="cb14-44">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb14-45">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.norminvgauss.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_nig), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Fitted normal inverse gaussian'</span>,color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb14-46">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi normale inverse gaussienne'</span>)</span>
<span id="cb14-47">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb14-48"></span>
<span id="cb14-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot avec une loi de NIG.</span></span>
<span id="cb14-50">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"norminvgauss"</span>, sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_nig), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb14-51">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi normale inverse gaussienne'</span>)</span>
<span id="cb14-52"></span>
<span id="cb14-53">plt.tight_layout()</span>
<span id="cb14-54"></span>
<span id="cb14-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage des graphiques</span></span>
<span id="cb14-56">plt.show()</span>
<span id="cb14-57"></span>
<span id="cb14-58">U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>returns.index, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>returns.columns)</span>
<span id="cb14-59"></span>
<span id="cb14-60">U[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BNP'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norminvgauss.cdf(data,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_nig)</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-10-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="47a86659" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(params_norm)</span>
<span id="cb15-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(params_std)</span>
<span id="cb15-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(params_nig)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(np.float64(-0.0005897932128196069), np.float64(0.024327449407557697))
(np.float64(4.605552436800593), np.float64(-0.00046153919290846087), np.float64(0.018734588738427177))
(np.float64(1.114374971099747), np.float64(-0.01637147555477723), np.float64(-0.00021011775496697913), np.float64(0.025828797181227353))</code></pre>
</div>
</div>
<div id="e368db98" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######## Test de kolmogorov-smirnov ########</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">ks_stat_norm, ks_p_value_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'norm'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_norm))</span>
<span id="cb17-4">ks_stat_std, ks_p_value_std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_std))</span>
<span id="cb17-5">ks_stat_nig, ks_p_value_nig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'norminvgauss'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_nig))</span>
<span id="cb17-6"></span>
<span id="cb17-7">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb17-8">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Statistic"</span>: [ks_stat_norm, ks_stat_std, ks_stat_nig],</span>
<span id="cb17-9">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>: [ks_p_value_norm, ks_p_value_std, ks_p_value_nig]</span>
<span id="cb17-10">            }, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normal Inverse Gaussian"</span>])</span>
<span id="cb17-11"></span>
<span id="cb17-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb17-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test de Kolmogorov-Smirnov"</span>)</span>
<span id="cb17-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb17-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(res)</span>
<span id="cb17-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>==================================================
Test de Kolmogorov-Smirnov
==================================================
                         Statistic   p-value
Normal                    0.051878  0.008908
Student                   0.027225  0.441752
Normal Inverse Gaussian   0.026466  0.477953
==================================================</code></pre>
</div>
</div>
</section>
<section id="iii.2.1.-modélisation-des-rendements-de-sg" class="level3">
<h3 class="anchored" data-anchor-id="iii.2.1.-modélisation-des-rendements-de-sg">III.2.1. Modélisation des rendements de SG</h3>
<p>Comme les rendements de BNP, les rendements de SG sont mieux modélisés par les lois student et normal inverse gaussian.</p>
<div id="ab3912de" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb19-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb19-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb19-4"></span>
<span id="cb19-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation de données pour l'exemple (remplace par tes données réelles)</span></span>
<span id="cb19-6">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>]</span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création de la figure et des axes pour 4 subplots (2 lignes, 2 colonnes)</span></span>
<span id="cb19-9">fig, axs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb19-10"></span>
<span id="cb19-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi normale ########################</span></span>
<span id="cb19-12">params_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norm.fit(data)</span>
<span id="cb19-13"></span>
<span id="cb19-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histogramme avec densité théorique de la loi normale (subplot 0,0)</span></span>
<span id="cb19-15">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(data), np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb19-16">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb19-17">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.norm.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_norm), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Normal Distribution'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb19-18">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi normale'</span>)</span>
<span id="cb19-19">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb19-20"></span>
<span id="cb19-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot (subplot 0,1)</span></span>
<span id="cb19-22">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"norm"</span>,sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_norm), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb19-23">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi normale'</span>)</span>
<span id="cb19-24"></span>
<span id="cb19-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi de student ########################</span></span>
<span id="cb19-26"></span>
<span id="cb19-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation des paramètres de la distribution de Student pour vos données.</span></span>
<span id="cb19-28">params_std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.t.fit(data)</span>
<span id="cb19-29"></span>
<span id="cb19-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histogramme avec densité théorique de la loi de Student.</span></span>
<span id="cb19-31">xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(data), np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb19-32">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb19-33">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.t.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_std), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Fitted t-Distribution'</span>,color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'orange'</span>)</span>
<span id="cb19-34">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi de student'</span>)</span>
<span id="cb19-35">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb19-36"></span>
<span id="cb19-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot avec une loi de Student.</span></span>
<span id="cb19-38">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_std), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb19-39">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi de student'</span>)</span>
<span id="cb19-40"></span>
<span id="cb19-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######################## Loi de Normal Inverse Gaussian ########################</span></span>
<span id="cb19-42">params_nig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norminvgauss.fit(data)</span>
<span id="cb19-43"></span>
<span id="cb19-44">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].hist(data, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>)</span>
<span id="cb19-45">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(xs, stats.norminvgauss.pdf(xs, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_nig), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Fitted normal inverse gaussian'</span>,color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb19-46">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité ajustée - loi normale inverse gaussienne'</span>)</span>
<span id="cb19-47">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb19-48"></span>
<span id="cb19-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Q-Q plot avec une loi de NIG.</span></span>
<span id="cb19-50">stats.probplot(data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"norminvgauss"</span>, sparams<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_nig), plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb19-51">axs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q-Q Plot - loi normale inverse gaussienne'</span>)</span>
<span id="cb19-52"></span>
<span id="cb19-53">plt.tight_layout()</span>
<span id="cb19-54"></span>
<span id="cb19-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage des graphiques</span></span>
<span id="cb19-56">plt.show()</span>
<span id="cb19-57"></span>
<span id="cb19-58">U[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SG'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.norminvgauss.cdf(data,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_nig)</span>
<span id="cb19-59"></span>
<span id="cb19-60">U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U.to_numpy()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="81d39935" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">######## Test de kolmogorov-smirnov ########</span></span>
<span id="cb20-2"></span>
<span id="cb20-3">ks_stat_norm, ks_p_value_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'norm'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_norm))</span>
<span id="cb20-4">ks_stat_std, ks_p_value_std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_std))</span>
<span id="cb20-5">ks_stat_nig, ks_p_value_nig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.kstest(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'norminvgauss'</span>, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(params_nig))</span>
<span id="cb20-6"></span>
<span id="cb20-7">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb20-8">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Statistic"</span>: [ks_stat_norm, ks_stat_std, ks_stat_nig],</span>
<span id="cb20-9">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>: [ks_p_value_norm, ks_p_value_std, ks_p_value_nig]</span>
<span id="cb20-10">            }, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normal Inverse Gaussian"</span>])</span>
<span id="cb20-11"></span>
<span id="cb20-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb20-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test de Kolmogorov-Smirnov"</span>)</span>
<span id="cb20-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb20-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(res)</span>
<span id="cb20-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>==================================================
Test de Kolmogorov-Smirnov
==================================================
                         Statistic   p-value
Normal                    0.047345  0.021967
Student                   0.025635  0.519206
Normal Inverse Gaussian   0.022462  0.685866
==================================================</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.3.-etude-de-la-structure-de-dépendance" class="level2">
<h2 class="anchored" data-anchor-id="iii.3.-etude-de-la-structure-de-dépendance">III.3. Etude de la structure de dépendance</h2>
<p>L’évaluation de la dépendance entre les facteurs de risque sera réalisée en utilisant des outils graphiques basés sur des critères non paramétriques, tels que les nuages de points, les ajustements linéaires et le dépendogramme.</p>
<p>Ces méthodes, choisies pour leur capacité à traiter des données sans présupposer une distribution spécifique, offrent une approche flexible et visuelle pour identifier et analyser les relations entre les variables de risque. Par exemple, les nuages de points permettent de visualiser la dispersion et la relation potentielle entre deux variables, tandis que les ajustements linéaires cherchent à modéliser la relation par une ligne droite, facilitant ainsi la compréhension des tendances générales.</p>
<p>Le dépendogramme, quant à lui, représente la structure de dépendance sous la forme du nuage de points des marges uniformes extraites de l’échantillon n couples de données <img src="https://latex.codecogs.com/png.latex?%5Cleft(%5Cleft(x_%7B1,1%7D%20;%20x_%7B2,1%7D%5Cright),%20%5Ccdots,%5Cleft(x_%7B1,%20n%7D%20;%20x_%7B2,%20n%7D%5Cright)%5Cright)">, i.e.&nbsp;:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Au_%7Bi,%20j%7D=%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bk=1%7D%5En%201_%7B%5Cleft%5C%7Bx_%7Bj,%20k%7D%20%5Cleq%20x_%7Bj,i%7D%5Cright%5C%7D%7D,%20%5Cquad%20i%20%5Cin%5B1,%20n%5D,%20%5Cquad%20%5Cforall%20j%20%5Cin%5B1,2%5D%0A"></p>
<p>Le dépendogramme de l’échantillon est donc la représentation de n couples <img src="https://latex.codecogs.com/png.latex?%5Cleft(%5Cleft(u_%7B1,1%7D%20;%20u_%7B2,1%7D%5Cright),%20%5Ccdots,%5Cleft(u_%7B1,%20n%7D%20;%20u_%7B2,%20n%7D%5Cright)%5Cright)">. Il permet d’observer le caractère plus ou moins simultané des réalisations issues de l’échantillon.</p>
<div id="5dbc0f0c" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tableax avec tx de pearson, spearman et kendall</span></span>
<span id="cb22-2"></span>
<span id="cb22-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pearsonr, spearmanr, kendalltau</span>
<span id="cb22-4"></span>
<span id="cb22-5">pearson <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pearsonr(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>])</span>
<span id="cb22-6">spearman <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spearmanr(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>])</span>
<span id="cb22-7">kendall <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kendalltau(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>])</span>
<span id="cb22-8"></span>
<span id="cb22-9">tableau_correlation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb22-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pearson"</span>: pearson,</span>
<span id="cb22-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spearman"</span>: spearman,</span>
<span id="cb22-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kendall"</span>: kendall</span>
<span id="cb22-13">}, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Coefficient"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>])</span>
<span id="cb22-14"></span>
<span id="cb22-15">tableau_correlation</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Pearson</th>
<th data-quarto-table-cell-role="th">Spearman</th>
<th data-quarto-table-cell-role="th">Kendall</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">Coefficient</td>
<td>8.646588e-01</td>
<td>8.409565e-01</td>
<td>6.690959e-01</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">p-value</td>
<td>2.383438e-300</td>
<td>3.609139e-268</td>
<td>9.880880e-220</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>En analysant la corrélation entre les rendements de BNP et SG, nous constatons qu’il y a une corrélation positive significative peu importe le test de corrélation effectué. La corrélation de spearman et de pearson indique qu’il y a une liaison monotone et linéaire forte d’au moins 84% entre les rendements des deux entreprises. En ce qui concerne le taux de kendall, nous constatons une corrélation positive significative de 67% environ. Cela signifie que les rendements des actions de BNP et SG sont positivement corrélés.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Attention la corrélation de pearson n’est pas une mesure de concordance contrairement au coefficient de spearman. Dans notre cas, il indique une corrélation linéaire forte.</p>
</div>
</div>
<div id="34f334f6" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dependogramme</span></span>
<span id="cb23-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Posons x1, u1 = BNP et x2, u2 = SG</span></span>
<span id="cb23-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb23-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> rankdata</span>
<span id="cb23-5"></span>
<span id="cb23-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb23-7"></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Transformation en pseudo-observations</span></span>
<span id="cb23-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pseudo_observations(X):</span>
<span id="cb23-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Transforme les données en pseudo-observations U dans [0,1]."""</span></span>
<span id="cb23-11">    n, d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X.shape</span>
<span id="cb23-12">    U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((n, d))</span>
<span id="cb23-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(d):</span>
<span id="cb23-14">        U[:, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rankdata(X[:, j]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pour éviter les 1 stricts</span></span>
<span id="cb23-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> U</span>
<span id="cb23-16"></span>
<span id="cb23-17">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns.to_numpy()</span>
<span id="cb23-18">u_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(X)</span>
<span id="cb23-19"></span>
<span id="cb23-20"></span>
<span id="cb23-21">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb23-22">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 ligne, 2 colonnes, premier graphique</span></span>
<span id="cb23-23"></span>
<span id="cb23-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># nuages de points des données BNP et SG</span></span>
<span id="cb23-25">plt.scatter(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>], cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>)</span>
<span id="cb23-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ajout la droite qui s'ajuste aux données</span></span>
<span id="cb23-27">plt.plot(np.unique(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>]), np.poly1d(np.polyfit(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>], returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SG"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))(np.unique(returns[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BNP"</span>])), color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Droite d'ajustement"</span>)</span>
<span id="cb23-28">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nuages de points des rendements"</span>)</span>
<span id="cb23-29">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x1"</span>)</span>
<span id="cb23-30">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>)</span>
<span id="cb23-31">plt.legend()</span>
<span id="cb23-32">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb23-33"></span>
<span id="cb23-34">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb23-35">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb23-36">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb23-37">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb23-38">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb23-39">plt.legend()</span>
<span id="cb23-40">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb23-41">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-16-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En observant le dépendogramme, nous constatons la même dépendance positive entre les deux entreprises. Cela signifie que si l’une des entreprises fait défaut, l’autre a plus de chance de faire défaut également. Cela est cohérent avec la corrélation positive observée entre les rendements des actions de BNP et SG.</p>
<p>De plus, en observant le dépendogramme, il semble avoir des dépendances à gauche et à droite entre les deux entreprises. Nous allons tout de même tester un éventail de copules afin de vérifier laquelle des copules est la plus adaptée à notre cas : - Copules elliptiques : gaussienne, Student. - Copules archimédiennes : Clayton, Gumbel, Frank.</p>
<p>Il faudrait au préalable estimer les paramètres des copules archimédiennes et elliptiques pour déterminer laquelle des copules est la plus adaptée à notre cas. Précedemment, nous avons estimé les paramètres des lois marginales des rendements de BNP et SG. Nous allons utiliser ces paramètres pour estimer les paramètres des copules. Nous allons utiliser ces paramètres pour la modélisation des différentes copules.</p>
<div id="3e54eaa7" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize</span>
<span id="cb24-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.distributions.copula.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (</span>
<span id="cb24-3">    GaussianCopula, StudentTCopula, ClaytonCopula, GumbelCopula, FrankCopula</span>
<span id="cb24-4">)</span>
<span id="cb24-5"></span>
<span id="cb24-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_copula(copula_type, params):</span>
<span id="cb24-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>:</span>
<span id="cb24-8">        rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb24-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GaussianCopula(corr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.array([[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rho], [rho, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]))</span>
<span id="cb24-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"student"</span>:</span>
<span id="cb24-11">        rho, nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params</span>
<span id="cb24-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> StudentTCopula(corr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.array([[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rho], [rho, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]), df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu)</span>
<span id="cb24-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>:</span>
<span id="cb24-14">        theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb24-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ClaytonCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span>
<span id="cb24-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>:</span>
<span id="cb24-17">        theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb24-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GumbelCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span>
<span id="cb24-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>:</span>
<span id="cb24-20">        theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb24-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> FrankCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span>
<span id="cb24-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb24-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Copula type </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>copula_type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> not supported"</span>)</span>
<span id="cb24-24"></span>
<span id="cb24-25"></span>
<span id="cb24-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-vraisemblance négative pour estimation MLE</span></span>
<span id="cb24-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> negative_log_likelihood(params, U, copula_type):</span>
<span id="cb24-28">    copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_copula(copula_type, params)</span>
<span id="cb24-29">    log_likelihood <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> copula.logpdf(U)</span>
<span id="cb24-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(log_likelihood)</span>
<span id="cb24-31"></span>
<span id="cb24-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ajustement de la copule (MLE)</span></span>
<span id="cb24-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fit_copula(U, copula_type):</span>
<span id="cb24-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> copula_type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>]:</span>
<span id="cb24-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>:</span>
<span id="cb24-36">            x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>]</span>
<span id="cb24-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb24-38">            x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb24-39">        bounds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> [(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>)]</span>
<span id="cb24-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"student"</span>:</span>
<span id="cb24-41">        x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rho et df</span></span>
<span id="cb24-42">        bounds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)]</span>
<span id="cb24-43"></span>
<span id="cb24-44">    result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize(negative_log_likelihood, x0, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(U, copula_type), bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bounds, method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Nelder-Mead'</span>)</span>
<span id="cb24-45"></span>
<span id="cb24-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> result.success:</span>
<span id="cb24-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">RuntimeError</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"MLE failed for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>copula_type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> copula: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>message<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb24-48"></span>
<span id="cb24-49">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result.x</span></code></pre></div>
</div>
</section>
<section id="iii.4.-modélisation-la-structure-de-dépendance-au-moyen-des-copules-paramétriques" class="level2">
<h2 class="anchored" data-anchor-id="iii.4.-modélisation-la-structure-de-dépendance-au-moyen-des-copules-paramétriques">III.4. Modélisation la structure de dépendance au moyen des copules paramétriques</h2>
<p>Pour modéliser la structure de dépendance entre les défauts, de manière précise, nous utiliserons les copules. Une copule est une fonction de répartition multivariée de marginales uniformes sur <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">. Dans le cas bivarié, on a:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BC%7D%5Cleft(%5Cmathrm%7Bu%7D_1,%20%5Cmathrm%7Bu%7D_%7B%5Cmathrm%7B2%7D%7D%5Cright)=%5Cmathrm%7BP%7D%5Cleft%5B%5Cmathrm%7BU%7D_1%20%5Cleq%20%5Cmathrm%7Bu%7D_1,%5Cmathrm%7BU%7D_%7B%5Cmathrm%7B2%7D%7D%20%5Cleq%20%5Cmathrm%7Bu%7D_%7B%5Cmathrm%7B2%7D%7D%5Cright%5D%0A"></p>
<p>Dans le cadre de ce projet, nous allons étudier deux principales familles de copules présentés dans le tableau :</p>
<ul>
<li>Copules elliptiques : gaussienne, Student</li>
<li>Copules archimédiennes : Clayton, Gumbel, Frank</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Btable%7D%5BH%5D%0A%5Ccentering%0A%5Cbegin%7Btabular%7D%7Bl%7Cc%7Ccc%7D%0A%5Chline%0AFamille%20&amp;%20Nom%20%20%20%20&amp;%20Copule%20$C(u,v)$%20%20%20&amp;%20Param%C3%A8tres%20%5C%5C%20%5Chline%0A%5Cmultirow%7B2%7D%7B*%7D%7BElliptique%7D%20&amp;%20Gaussien%20&amp;%20$%5CPhi_%7B%5CSigma%7D%5Cleft(%5CPhi%5E%7B-1%7D(u),%5CPhi%5E%7B-1%7D(v)%5Cright)$%20&amp;%20$%5Crho$%20%5C%5C%0A&amp;%20Student%20&amp;%20%20$T_%7B%5CSigma%7D%5Cleft(T_%7B%5Cnu%7D%5E%7B-1%7D(u),T_%7B%5Cnu%7D%5E%7B-1%7D(v)%5Cright)$%20&amp;%20$%5Crho,%20%5Cnu$%20%5C%5C%0A%5Cmidrule%0A%5Cmultirow%7B3%7D%7B*%7D%7BArchim%C3%A9dienne%7D%20&amp;%20Frank%20%20&amp;%20$%5Cfrac%7B1%7D%7B%5Ctheta%7D%20%5Cleft(-%5Cln(1+%20%5Cfrac%7B(e%5E%7B-%5Ctheta%20u%7D%20%20-%201)(e%5E%7B-%5Ctheta%20v%7D%20-%201)%7D%7Be%5E%7B-%5Ctheta%20v%7D%20-%201%7D)%5Cright)$%20&amp;%20$%5Ctheta%20%5Cne%200$%20%5C%5C%0A&amp;%20Gumbel%20&amp;%20$%5Cexp%5Cleft(-%5Cleft((-%20%5Cln(u))%5E%7B%5Ctheta%7D%20+%20(-%20%5Cln(v))%5E%7B%5Ctheta%7D%5Cright)%5E%7B%5Cfrac%7B1%7D%7B%5Ctheta%7D%7D%5Cright)$%20&amp;%20$%5Ctheta%20%5Cgeq%201$%20%5C%5C%0A&amp;%20Clayton%20%20&amp;%20$(u%5E%7B-%5Ctheta%7D%20+%20v%5E%7B-%5Ctheta%7D%20-1)%5E%7B-%5Cfrac%7B1%7D%7B%5Ctheta%7D%7D$%20&amp;%20$%5Ctheta%20%3E%200$%0A%5C%5C%20%5Chline%0A%5Cend%7Btabular%7D%5C%5C%0A%7B%5Cfootnotesize%20*$%5CSigma$%20est%20la%20matrice%20de%20variance%20covariance.%20%7D%0A%5Ccaption%7BCopules%20archim%C3%A9diennes%20bivari%C3%A9es%20les%20plus%20courantes.%7D%0A%5Clabel%7Btab:copule_family%7D%0A%5Cend%7Btable%7D%0A"></p>
<p>Il s’agira après estimation des paramètres et des tests d’ajustement, la copule la plus adéquate pour modéliser au mieux la dépendance entre les variables étudiées.</p>
<p>Pour l’estimation des paramètres des copules sélectionnées, plusieurs approches méthodologiques s’offrent à nous: la méthode des moments, la méthode du maximum de vraisemblance et l’approche IFM. Nous privilégierons l’approche IFM (Inference Functions for Margins) présenté ci dessous (algo <img src="https://latex.codecogs.com/png.latex?%5Cref%7BIFM%7D">). Cet algorithme a l’avantage d’être plus rapide que la méthode du maximum de vraisemblance.</p>
<p>Pour l’évaluation de l’ajustement des copules à la structure de dépendance d’un échantillon, nous utiliserons des outils graphiques tels que le dépendogramme, présenté précedemment, et le Kendall plot.</p>
<p>Le Kendall plot permet une comparaison directe entre la copule empirique et la copule théorique. Plus le Kendall plot se rapproche d’une droite, plus l’ajustement entre la structure de dépendance de l’échantillon et la copule estimée sur ce même échantillon est bon.</p>
<section id="iii.4.1-copule-gaussienne" class="level3">
<h3 class="anchored" data-anchor-id="iii.4.1-copule-gaussienne">III.4.1 Copule gaussienne</h3>
<section id="iii.4.1.a.-estimation-des-paramètres-de-la-copule-gaussienne" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.1.a.-estimation-des-paramètres-de-la-copule-gaussienne">III.4.1.a. Estimation des paramètres de la copule gaussienne</h4>
<p>Dans le cadre de la copule gaussienne, il nous faut la matrice de variance qui est donnée par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bpmatrix%7D%0A1%20&amp;%20%5Crho%20%5C%5C%0A%5Crho%20&amp;%201%0A%5Cend%7Bpmatrix%7D%0A"></p>
<p>Dans notre cas, le seul paramètre à estimer est le coefficient de corrélation de pearson. Nous allons donc estimer le coefficient de corrélation de pearson entre les rendements des actions de BNP et SG.</p>
<div id="cc0fd4d8" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(u_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-2"></span>
<span id="cb25-3">mat<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rho], [rho, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb25-4">mat</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="17">
<pre><code>array([[1.        , 0.86097656],
       [0.86097656, 1.        ]])</code></pre>
</div>
</div>
</section>
<section id="iii.4.1.b.-simulation-de-la-copule-gaussienne" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.1.b.-simulation-de-la-copule-gaussienne">III.4.1.b. Simulation de la copule gaussienne</h4>
<p>Pour simuler la copule gaussienne, nous allons utiliser la méthode de distribution puisque la loi est facile à implémenter.</p>
<div id="6fa3de95" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># # Simuler réalisation W suivant une loi normale centrée multivariée</span></span>
<span id="cb27-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># import scipy.stats as stats</span></span>
<span id="cb27-3"></span>
<span id="cb27-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># np.random.seed(0)</span></span>
<span id="cb27-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n = 1000</span></span>
<span id="cb27-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># W = np.random.multivariate_normal([0, 0], mat, n)</span></span>
<span id="cb27-7"></span>
<span id="cb27-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># # Calculer U1 et U2</span></span>
<span id="cb27-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># U = np.zeros((n, 2))</span></span>
<span id="cb27-10"></span>
<span id="cb27-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for i in range(n):</span></span>
<span id="cb27-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     U[i,0] = stats.norm.cdf(W[i,0])</span></span>
<span id="cb27-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     U[i,1] = stats.norm.cdf(W[i,1])</span></span>
<span id="cb27-14"></span>
<span id="cb27-15"></span>
<span id="cb27-16">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_obs.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb27-17">_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GaussianCopula(corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho).rvs(n)</span>
<span id="cb27-18">u_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(_)</span></code></pre></div>
</div>
</section>
<section id="iii.4.1.c.-test-dajustement" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.1.c.-test-dajustement">III.4.1.c. Test d’ajustement</h4>
<div id="9696ca51" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb28-2">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données empiriques"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb28-3">plt.scatter(u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copule gaussienne"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb28-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb28-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb28-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb28-7">plt.legend()</span>
<span id="cb28-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb28-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-20-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="8c2caf1e" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kendall plot</span></span>
<span id="cb29-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb29-3"></span>
<span id="cb29-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calculer_Hi_vect(U):</span>
<span id="cb29-5">    n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(U)</span>
<span id="cb29-6">    H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb29-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Comparaison de chaque paire une seule fois</span></span>
<span id="cb29-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n):</span>
<span id="cb29-9">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Créer des masques booléens pour les conditions</span></span>
<span id="cb29-10">        cond1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> U[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># u_{1,j} &lt;= u_{1,i}</span></span>
<span id="cb29-11">        cond2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> U[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># u_{2,j} &lt;= u_{2,i}</span></span>
<span id="cb29-12"></span>
<span id="cb29-13">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Appliquer les conditions et exclure le cas où i == j</span></span>
<span id="cb29-14">        H[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(np.logical_and(cond1, cond2)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb29-15"></span>
<span id="cb29-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> H<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(U)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb29-17"></span>
<span id="cb29-18"></span>
<span id="cb29-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> kendall_plot(U,S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>,rho<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,nu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb29-20"></span>
<span id="cb29-21">    H_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calculer_Hi_vect(U)</span>
<span id="cb29-22">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(U)</span>
<span id="cb29-23"></span>
<span id="cb29-24">    H_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((S, n)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># S lignes et n colonnes</span></span>
<span id="cb29-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(S):</span>
<span id="cb29-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gaussian'</span>:</span>
<span id="cb29-27">            X_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GaussianCopula(corr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rho).rvs(n)</span>
<span id="cb29-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'student'</span>:</span>
<span id="cb29-29">            X_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StudentTCopula(df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nu, corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho).rvs(n)</span>
<span id="cb29-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>:</span>
<span id="cb29-31">            X_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GumbelCopula(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb29-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>:</span>
<span id="cb29-33">            X_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ClaytonCopula(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb29-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>:</span>
<span id="cb29-35">            X_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FrankCopula(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb29-36">        U_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pseudo_observations(X_)</span>
<span id="cb29-37">        H_means[s] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(calculer_Hi_vect(U_))</span>
<span id="cb29-38"></span>
<span id="cb29-39">    H_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(H_means, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># axis=0 pour moyenne par colonne</span></span>
<span id="cb29-40"></span>
<span id="cb29-41">    x,y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(H_i), np.sort(H_mean)</span>
<span id="cb29-42">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(x.shape, y.shape)</span>
<span id="cb29-43"></span>
<span id="cb29-44">    plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb29-45">    plt.plot(x, y)</span>
<span id="cb29-46">    plt.plot([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>)</span>
<span id="cb29-47">    plt.title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Kendall plot for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>copula<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> copula"</span>)</span>
<span id="cb29-48">    plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"i"</span>)</span>
<span id="cb29-49">    plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kendall"</span>)</span>
<span id="cb29-50">    plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb29-51">    plt.show()</span>
<span id="cb29-52"></span>
<span id="cb29-53">S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb29-54">kendall_plot(u_obs,S,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>,rho<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rho)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(999,) (999,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-21-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="iii.4.1.d.-test-dadéquation" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.1.d.-test-dadéquation">III.4.1.d. Test d’adéquation</h4>
<div id="6b99a0d2" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copule empirique Cn(u)</span></span>
<span id="cb31-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> empirical_copula_cdf(U, u):</span>
<span id="cb31-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Calcule la copule empirique Cn(u)."""</span></span>
<span id="cb31-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.mean(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> u, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb31-5"></span>
<span id="cb31-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Statistique de Cramér-von Mises</span></span>
<span id="cb31-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cramer_von_mises_stat(U, copula):</span>
<span id="cb31-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Calcule la statistique de test Tn."""</span></span>
<span id="cb31-9">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(U)</span>
<span id="cb31-10">    Tn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb31-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n):</span>
<span id="cb31-12">        u_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> U[i]</span>
<span id="cb31-13">        Cn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> empirical_copula_cdf(U, u_i)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copule empirique Cn(u_i)</span></span>
<span id="cb31-14">        C_theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> copula.cdf([u_i])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copule théorique estimée Cθ(u_i)</span></span>
<span id="cb31-15">        Tn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (Cn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> C_theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb31-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Tn</span>
<span id="cb31-17"></span>
<span id="cb31-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test d’adéquation complet avec bootstrap paramétrique</span></span>
<span id="cb31-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>, M<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>):</span>
<span id="cb31-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb31-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Test d'adéquation de Genest &amp; Rémillard (2008) pour une copule avec</span></span>
<span id="cb31-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    statistique de Cramér-von Mises et bootstrap paramétrique.</span></span>
<span id="cb31-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb31-24">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pseudo-observations</span></span>
<span id="cb31-25">    U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(X)</span>
<span id="cb31-26">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(U)</span>
<span id="cb31-27"></span>
<span id="cb31-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation MLE de la copule sur les données</span></span>
<span id="cb31-29">    params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(U, copula_type)</span>
<span id="cb31-30">    copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_copula(copula_type, params)</span>
<span id="cb31-31"></span>
<span id="cb31-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la statistique observée Tn</span></span>
<span id="cb31-33">    T_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cramer_von_mises_stat(U, copula)</span>
<span id="cb31-34"></span>
<span id="cb31-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bootstrap paramétrique</span></span>
<span id="cb31-36">    T_boot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb31-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(M):</span>
<span id="cb31-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Simulation d’un échantillon sous la copule ajustée</span></span>
<span id="cb31-39">        U_boot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> copula.rvs(n)</span>
<span id="cb31-40"></span>
<span id="cb31-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Ré-estimation de la copule sur U_boot</span></span>
<span id="cb31-42">        params_boot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(U_boot, copula_type)</span>
<span id="cb31-43">        copula_boot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_copula(copula_type, params_boot)</span>
<span id="cb31-44"></span>
<span id="cb31-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Calcul de Tn pour cet échantillon bootstrap</span></span>
<span id="cb31-46">        T_boot.append(cramer_von_mises_stat(U_boot, copula_boot))</span>
<span id="cb31-47"></span>
<span id="cb31-48">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la p-value (proportion des T_boot supérieurs à T_obs)</span></span>
<span id="cb31-49">    p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.array(T_boot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> T_obs)</span>
<span id="cb31-50"></span>
<span id="cb31-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {</span>
<span id="cb31-52">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"copula_type"</span>: copula_type,</span>
<span id="cb31-53">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"params"</span>: params,</span>
<span id="cb31-54">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T_obs"</span>: T_obs,</span>
<span id="cb31-55">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p_value"</span>: p_value</span>
<span id="cb31-56">    }</span>
<span id="cb31-57"></span>
<span id="cb31-58"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb31-59">result_gaussian <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>)</span>
<span id="cb31-60">pprint(result_gaussian)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'T_obs': np.float64(0.05456423166335546),
 'copula_type': 'gaussian',
 'p_value': np.float64(0.892),
 'params': array([0.86097656])}</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.4.2-copule-de-student" class="level3">
<h3 class="anchored" data-anchor-id="iii.4.2-copule-de-student">III.4.2 Copule de student</h3>
<section id="iii.4.2.a.-estimation-des-paramètres-de-la-copule-student" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.2.a.-estimation-des-paramètres-de-la-copule-student">III.4.2.a. Estimation des paramètres de la copule student</h4>
<div id="dbe5070c" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1">rho,nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(u_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"student"</span>)</span>
<span id="cb33-2"></span>
<span id="cb33-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rho = "</span>, rho)</span>
<span id="cb33-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu = "</span>, nu)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>rho =  0.8496143388748167
nu =  2.0</code></pre>
</div>
</div>
</section>
<section id="iii.4.2.b.-simulation-de-la-copule-de-student" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.2.b.-simulation-de-la-copule-de-student">III.4.2.b. Simulation de la copule de student</h4>
<div id="92eeb004" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_obs.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb35-2">_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StudentTCopula(corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho, df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu).rvs(n)</span>
<span id="cb35-3">u_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(_)</span></code></pre></div>
</div>
</section>
<section id="iii.4.2.c.-test-dajustement" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.2.c.-test-dajustement">III.4.2.c. Test d’ajustement</h4>
<div id="d8fc4aac" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb36-2">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données empiriques"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb36-3">plt.scatter(u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copule de student"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb36-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb36-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb36-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb36-7">plt.legend()</span>
<span id="cb36-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb36-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-25-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="c576ff19" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1">kendall_plot(u_obs,S,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"student"</span>,rho<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rho,nu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(999,) (999,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-26-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Attention, nous avons utilisé le package copula.api de statsmodels pour implémenter les copules. Cependant, la classe correspondant à la copule de Student ne permet pas d’obtenir une fonction de répartition (voir <a href="https://www.statsmodels.org/dev/_modules/statsmodels/distributions/copula/elliptical.html#StudentTCopula.cdf">lien</a>).</p>
<p>En utilisant un environnement virtuel, il a été possible de modifier le fichier statsmodels/distributions/copula/elliptical.py du package afin d’implémenter la méthode pour la fonction de répartition. Vous trouverez ce fichier <a href="utiles/elliptical.py">ci-joint</a> afin de garantir le bon fonctionnement du code si vous devez le relancer.</p>
</div>
</div>
</section>
<section id="iii.4.2.d.-test-dadéquation" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.2.d.-test-dadéquation">III.4.2.d. Test d’adéquation</h4>
<div id="24b4d009" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb39-2">result_std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"student"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">ATTENTION</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> DIFFICULTE POUR ESSTIMER COPULE STUDENT</span></span>
<span id="cb39-3">pprint(result_std)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'T_obs': np.float64(0.05937889644009823),
 'copula_type': 'student',
 'p_value': np.float64(0.85),
 'params': array([0.84961434, 2.        ])}</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.4.3-copule-de-clayton" class="level3">
<h3 class="anchored" data-anchor-id="iii.4.3-copule-de-clayton">III.4.3 Copule de clayton</h3>
<section id="iii.4.3.a.-estimation-des-paramètres-de-la-copule-clayton" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.3.a.-estimation-des-paramètres-de-la-copule-clayton">III.4.3.a. Estimation des paramètres de la copule clayton</h4>
<div id="edf88af7" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1">theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(u_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb41-2"></span>
<span id="cb41-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta = "</span>, theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>theta =  2.4768310546874988</code></pre>
</div>
</div>
</section>
<section id="iii.4.3.b.-simulation-de-la-copule-de-clayton" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.3.b.-simulation-de-la-copule-de-clayton">III.4.3.b. Simulation de la copule de clayton</h4>
<div id="eb84fd42" class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_obs.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb43-2">_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ClaytonCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb43-3">u_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(_)</span></code></pre></div>
</div>
</section>
<section id="iii.4.3.c.-test-dajustement" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.3.c.-test-dajustement">III.4.3.c. Test d’ajustement</h4>
<div id="95731f90" class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb44-2">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données empiriques"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb44-3">plt.scatter(u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copule de clayton"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb44-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb44-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb44-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb44-7">plt.legend()</span>
<span id="cb44-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb44-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-30-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="1cf5c130" class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1">S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb45-2">kendall_plot(u_obs,S,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>,theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(999,) (999,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-31-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="iii.4.3.d.-test-dadéquation" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.3.d.-test-dadéquation">III.4.3.d. Test d’adéquation</h4>
<div id="5816c26b" class="cell" data-execution_count="31">
<div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb47-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb47-2">result_clayton<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayton"</span>) </span>
<span id="cb47-3">pprint(result_clayton)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'T_obs': array([0.86477661]),
 'copula_type': 'clayton',
 'p_value': np.float64(0.004),
 'params': array([2.47683105])}</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.4.4-copule-de-gumbel" class="level3">
<h3 class="anchored" data-anchor-id="iii.4.4-copule-de-gumbel">III.4.4 Copule de gumbel</h3>
<section id="iii.4.4.a.-estimation-des-paramètres-de-la-copule-gumbel" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.a.-estimation-des-paramètres-de-la-copule-gumbel">III.4.4.a. Estimation des paramètres de la copule gumbel</h4>
<div id="a42a4379" class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb49-1">theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(u_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb49-2"></span>
<span id="cb49-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta = "</span>, theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>theta =  2.9939208984374996</code></pre>
</div>
</div>
</section>
<section id="iii.4.4.b.-simulation-de-la-copule-de-clayton" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.b.-simulation-de-la-copule-de-clayton">III.4.4.b. Simulation de la copule de clayton</h4>
<div id="107ca25e" class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb51-1">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_obs.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb51-2">_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GumbelCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb51-3">u_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(_)</span></code></pre></div>
</div>
</section>
<section id="iii.4.4.c.-test-dajustement" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.c.-test-dajustement">III.4.4.c. Test d’ajustement</h4>
<div id="a6682d08" class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb52-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb52-2">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données empiriques"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb52-3">plt.scatter(u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copule de gumbel"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb52-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb52-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb52-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb52-7">plt.legend()</span>
<span id="cb52-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb52-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-35-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="c3b56a5e" class="cell" data-execution_count="35">
<div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb53-1">kendall_plot(u_obs,S,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>,theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(999,) (999,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-36-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="iii.4.4.d.-test-dadéquation" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.d.-test-dadéquation">III.4.4.d. Test d’adéquation</h4>
<div id="f1674f67" class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb55-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb55-2">result_gumbel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>) </span>
<span id="cb55-3">pprint(result_gumbel)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'T_obs': array([0.03485682]),
 'copula_type': 'gumbel',
 'p_value': np.float64(0.988),
 'params': array([2.9939209])}</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.4.4-copule-de-frank" class="level3">
<h3 class="anchored" data-anchor-id="iii.4.4-copule-de-frank">III.4.4 Copule de frank</h3>
<section id="iii.4.4.a.-estimation-des-paramètres-de-la-copule-frank" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.a.-estimation-des-paramètres-de-la-copule-frank">III.4.4.a. Estimation des paramètres de la copule frank</h4>
<div id="c86253c2" class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb57-1">theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(u_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb57-2"></span>
<span id="cb57-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta = "</span>, theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>theta =  10.0</code></pre>
</div>
</div>
</section>
<section id="iii.4.4.b.-simulation-de-la-copule-de-clayton-1" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.b.-simulation-de-la-copule-de-clayton-1">III.4.4.b. Simulation de la copule de clayton</h4>
<div id="3fec9362" class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb59-1">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_obs.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb59-2">_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FrankCopula(theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta).rvs(n)</span>
<span id="cb59-3">u_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pseudo_observations(_)</span></code></pre></div>
</div>
</section>
<section id="iii.4.4.c.-test-dajustement-1" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.c.-test-dajustement-1">III.4.4.c. Test d’ajustement</h4>
<div id="3a9b643f" class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb60-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb60-2">plt.scatter(u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_obs[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données empiriques"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb60-3">plt.scatter(u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], u_est[:,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copule de frank"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb60-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dépendogrammes"</span>)</span>
<span id="cb60-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u1"</span>)</span>
<span id="cb60-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"u2"</span>)</span>
<span id="cb60-7">plt.legend()</span>
<span id="cb60-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb60-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-40-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="87cebd33" class="cell" data-execution_count="40">
<div class="sourceCode cell-code" id="cb61" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb61-1">kendall_plot(u_obs,S,copula<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>,theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>theta)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>(999,) (999,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-41-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="iii.4.4.d.-test-dadéquation-1" class="level4">
<h4 class="anchored" data-anchor-id="iii.4.4.d.-test-dadéquation-1">III.4.4.d. Test d’adéquation</h4>
<div id="f9ded077" class="cell" data-execution_count="41">
<div class="sourceCode cell-code" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb63-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pprint <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb63-2">result_frank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adequation_test(X, copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frank"</span>) </span>
<span id="cb63-3">pprint(result_frank)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'T_obs': array([0.12454173]),
 'copula_type': 'frank',
 'p_value': np.float64(0.492),
 'params': array([10.])}</code></pre>
</div>
</div>
</section>
</section>
<section id="iii.5.-résultats" class="level3">
<h3 class="anchored" data-anchor-id="iii.5.-résultats">III.5. Résultats</h3>
<p>Nos résultats mettent en évidence l’importance de la structure de dépendance dans l’évaluation du risque de crédit. Après avoir testé plusieurs copules paramétriques, nous avons retenu la copule de Gumbel comme la plus appropriée, en raison de son bon ajustement aux données et de sa capacité à capturer les asymétries et les queues de distribution lourdes, essentielles dans un contexte de crise financière.</p>
<div id="17291739" class="cell" data-execution_count="42">
<div class="sourceCode cell-code" id="cb65" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb65-1">dict_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [result_gaussian, result_std, result_clayton, result_gumbel, result_frank]</span>
<span id="cb65-2"></span>
<span id="cb65-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to DataFrame</span></span>
<span id="cb65-4">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(dict_list)</span>
<span id="cb65-5">df</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="42">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">copula_type</th>
<th data-quarto-table-cell-role="th">params</th>
<th data-quarto-table-cell-role="th">T_obs</th>
<th data-quarto-table-cell-role="th">p_value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>gaussian</td>
<td>[0.8609765625000002]</td>
<td>0.054564</td>
<td>0.892</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>student</td>
<td>[0.8496143388748167, 2.0]</td>
<td>0.059379</td>
<td>0.850</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>clayton</td>
<td>[2.4768310546874988]</td>
<td>[0.8647766118220173]</td>
<td>0.004</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>gumbel</td>
<td>[2.9939208984374996]</td>
<td>[0.034856823632549765]</td>
<td>0.988</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>frank</td>
<td>[10.0]</td>
<td>[0.12454172887238589]</td>
<td>0.492</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
</section>
</section>
<section id="iv.-estimation-de-la-credit-var" class="level1">
<h1>IV. Estimation de la Credit VaR</h1>
<p>La simulation Monte Carlo nous a permis d’estimer la distribution des pertes du portefeuille et de déterminer une CreditVaR à 99% de 1286,61 euros. L’analyse de la convergence des résultats confirme la robustesse de cette estimation, qui reste stable malgré les variations de taille d’échantillon.</p>
<p>Ainsi, cette étude illustre la pertinence des copules pour modéliser de manière plus réaliste la dépendance entre les actifs financiers, là où les approches classiques fondées sur la corrélation linéaire sont insuffisantes. Une perspective d’amélioration pourrait consister à explorer des copules dynamiques ou à affiner l’estimation des paramètres via des méthodes bayésiennes.</p>
<div id="e1872b99" class="cell" data-execution_count="43">
<div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb66-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la credit var par monte carlo</span></span>
<span id="cb66-2"></span>
<span id="cb66-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> credit_var(params_R1,params_R2,lambda_1,lambda_2,EAD,params,S,tau,copula_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>,alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>):</span>
<span id="cb66-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb66-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Cette fonction est Var de niveau alpha qui estime la perte maximale sur l'horizon tau</span></span>
<span id="cb66-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        et retourne le quantile de niveau 99% pour dim=2.</span></span>
<span id="cb66-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb66-8"></span>
<span id="cb66-9">    copula <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_copula(copula_type, params)</span>
<span id="cb66-10">    U <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> copula.rvs(S)</span>
<span id="cb66-11">    a1,b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params_R1</span>
<span id="cb66-12">    a2,b2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params_R2</span>
<span id="cb66-13"></span>
<span id="cb66-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Générer des échantillons pour lgd_bnp et lgd_sg avec la loi Beta, vectorisée</span></span>
<span id="cb66-15">    LGD1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.random.beta(a1, b1, S)</span>
<span id="cb66-16">    LGD2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.random.beta(a2, b2, S)</span>
<span id="cb66-17"></span>
<span id="cb66-18"></span>
<span id="cb66-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simuler tau_bnp et tau_sg en utilisant la fonction de répartition inverse de la loi exponentielle</span></span>
<span id="cb66-20">    tau_bnp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.expon.ppf(U[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lambda_1)</span>
<span id="cb66-21">    tau_sg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.expon.ppf(U[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lambda_2)</span>
<span id="cb66-22"></span>
<span id="cb66-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer les pertes, vectorisée</span></span>
<span id="cb66-24">    L1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(tau_bnp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> tau, LGD1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> EAD, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb66-25">    L2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(tau_sg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> tau, LGD2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> EAD, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb66-26"></span>
<span id="cb66-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Somme des pertes</span></span>
<span id="cb66-28">    L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> L2</span>
<span id="cb66-29"></span>
<span id="cb66-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer et retourner le quantile de niveau 99%</span></span>
<span id="cb66-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.percentile(L, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div>
</div>
<div id="808ea6e4" class="cell" data-execution_count="44">
<div class="sourceCode cell-code" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb67-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tracer la courbe de la VaR en fonction de S</span></span>
<span id="cb67-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set seed</span></span>
<span id="cb67-3">np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb67-4"></span>
<span id="cb67-5">S_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb67-6">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_copula(U, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gumbel"</span>)</span>
<span id="cb67-7">EAD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb67-8">tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb67-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> PD(T,spread,Recouvrement):</span>
<span id="cb67-10">    lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spread <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Recouvrement)</span>
<span id="cb67-11">    PD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>T)</span>
<span id="cb67-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> PD,lambda_</span>
<span id="cb67-13"></span>
<span id="cb67-14">_,lambda_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PD(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,spread_4Y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],tx_recouvrement)</span>
<span id="cb67-15">_,lambda_2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PD(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,spread_5Y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],tx_recouvrement)</span>
<span id="cb67-16"></span>
<span id="cb67-17">params_R1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a_BNP,b_BNP</span>
<span id="cb67-18">params_R2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a_SG,b_SG</span>
<span id="cb67-19">VaR_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [credit_var(params_R1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_R1,params_R2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_R2, lambda_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_1, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb67-20">                         lambda_2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_2, EAD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>EAD, params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params, S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S, tau<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tau) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> S <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> S_values]</span>
<span id="cb67-21"></span>
<span id="cb67-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Je veux tracer une droite horizontale pour la mediane de la VaR et mettre ça valeur sur l'axe des ordonnées</span></span>
<span id="cb67-23">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb67-24">plt.plot(S_values, VaR_values)</span>
<span id="cb67-25">plt.axhline(np.mean(VaR_values), color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>)</span>
<span id="cb67-26">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>)</span>
<span id="cb67-27">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VaR"</span>)</span>
<span id="cb67-28">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb67-29">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules_files/figure-html/cell-45-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="521b2f12" class="cell" data-execution_count="45">
<div class="sourceCode cell-code" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb68-1">np.mean(VaR_values)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="45">
<pre><code>np.float64(1286.2298311022887)</code></pre>
</div>
</div>
<div id="c74e0cd2" class="cell" data-execution_count="46">
<div class="sourceCode cell-code" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb70-1">S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40000</span></span>
<span id="cb70-2">credit_var(params_R1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_R1,params_R2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_R2, lambda_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_1, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb70-3">                         lambda_2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lambda_2, EAD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>EAD, params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params, S<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>S, tau<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tau)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="46">
<pre><code>np.float64(1290.3822356040268)</code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>paper</category>
  <category>risques-fin</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/pj_copules.html</guid>
  <pubDate>Wed, 12 Mar 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Modèles de courbe de taux</title>
  <link>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Que ce soit pour les banques, les assureurs ou les fonds de pension, la courbe de taux zéro-coupon constitue la brique de base pour la valorisation de nombreux instruments financiers, de la détermination du prix des obligations aux produits dérivés plus complexes tels que les swaps de taux, les caplets, les swaptions ou encore les produits structurés de taux. De ce fait, la construction fiable, cohérente et régulièrement mise à jour d’une courbe de taux zéro-coupon représente un enjeu majeur.</p>
<p>Dans ce contexte, ce projet propose une méthodologie complète de reconstitution de la courbe de taux zéro-coupon implicite, élaborée à partir des cotations de marché disponibles sur différents segments : Money Market (marché monétaire), Futures et Swaps. L’approche adoptée repose sur une combinaison de bootstrapping, permettant d’extraire les taux zéro-coupon pour chaque maturité observable, et d’interpolations par spline cubique, afin d’assurer la lissité et la continuité de la courbe sur l’ensemble de l’échéancier, y compris sur les maturités non directement observées.</p>
<p>La courbe obtenue sert ensuite de référence pour la valorisation de produits dérivés de taux, en particulier les caplets et les swaptions, via le modèle classique de Black, largement utilisé sur les marchés. Toutefois, afin de mieux capturer la dynamique temporelle des taux d’intérêt et de prendre en compte la structure temporelle de la volatilité implicite, la seconde partie du projet repose sur la calibration d’un modèle de Hull-White, un modèle de taux affine avec retour à la moyenne. La calibration est réalisée à partir des cotations de caplets at-the-money (ATM), à l’aide d’une procédure de recherche numérique par dichotomie.</p>
<p>Le projet met également en évidence la sensibilité de la courbe de taux forward et des prix d’options aux paramètres de marché, notamment la volatilité et le paramètre de mean reversion du modèle Hull-White. Cette analyse de sensibilité illustre comment la structure de la courbe de taux et son évolution future sont influencées par les hypothèses de modélisation, ce qui est particulièrement crucial pour les desks de trading, les gestionnaires d’actifs ou les équipes de gestion actif-passif (ALM).</p>
<p>Enfin, le projet se prolonge par une extension appliquée aux produits structurés : la valorisation de caplets à barrière désactivante (knock-out caplets), qui nécessite une approche par simulation Monte-Carlo. Cette extension illustre comment la dynamique du taux court, simulée sous la mesure forward neutre, peut être exploitée pour évaluer des produits de plus en plus complexes, répondant à des besoins spécifiques d’investisseurs ou de gestionnaires de risques.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Le rapport de ce projet est disponible <a href="../rapports/Rapport_KOUADIO_OUYASSIN_courbes_de_taux.pdf">ici</a>.</p>
</div>
</div>
</section>
<section id="i.-reconstitution-de-la-courbe-de-taux" class="level2">
<h2 class="anchored" data-anchor-id="i.-reconstitution-de-la-courbe-de-taux">I. Reconstitution de la courbe de taux</h2>
<section id="i.1.-formules-de-valorisation-des-taux-de-marché" class="level3">
<h3 class="anchored" data-anchor-id="i.1.-formules-de-valorisation-des-taux-de-marché">I.1. Formules de valorisation des taux de marché</h3>
<p>La courbe interbancaire est une courbe de taux qui représente les taux d’intérêt auxquels les banques se prêtent de l’argent entre elles. Elle est utilisée pour déterminer les taux d’intérêt des prêts et des emprunts à court terme. Elle est construite sur le court terme (maturité&lt;6M) à partir des taux du marchés monétaire (Money Market) basés sur les dépots non garantis entre banques. Sur le moyen terme (6m - 3y) elle est construite à partir des contrats futures, i.e.&nbsp;des forwards sur un marché OTC (Over The Counter) et sur le long terme (&gt;3y) elle est construite à partir des contrats de swap euribor (Euro Interbank Offered Rate) 3M ou 6M.</p>
<p>Ci dessous, nous disposons de ces données de taux de marché cotés sur le marché interbancaire. Nous allons essayer de reconstituer la courbe de taux zero coupon implicite, qui ne cote pas directement sur le marché. Le fichier de données contient trois variables : - Type d’instruments (Money Market, Futures, Swap) - Maturité (en années) - Taux d’intérêt</p>
<div id="229e14ed" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-4">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ignore'</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_excel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/Data_tx.xlsx'</span>, sheet_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx_marche'</span>)</span>
<span id="cb1-7">data.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>]</span>
<span id="cb1-8">data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">type</th>
<th data-quarto-table-cell-role="th">T</th>
<th data-quarto-table-cell-role="th">tx</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>MM</td>
<td>0.25</td>
<td>0.030698</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>MM</td>
<td>0.50</td>
<td>0.026191</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>MM</td>
<td>0.75</td>
<td>0.023958</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>MM</td>
<td>1.00</td>
<td>0.022979</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>FUT</td>
<td>1.25</td>
<td>0.978691</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>FUT</td>
<td>1.50</td>
<td>0.977094</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>FUT</td>
<td>1.75</td>
<td>0.974981</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">7</td>
<td>FUT</td>
<td>2.00</td>
<td>0.972911</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">8</td>
<td>FUT</td>
<td>2.25</td>
<td>0.970984</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">9</td>
<td>FUT</td>
<td>2.50</td>
<td>0.969711</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">10</td>
<td>FUT</td>
<td>2.75</td>
<td>0.968436</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">11</td>
<td>SWAP</td>
<td>3.00</td>
<td>0.026112</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">12</td>
<td>SWAP</td>
<td>4.00</td>
<td>0.028117</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">13</td>
<td>SWAP</td>
<td>5.00</td>
<td>0.029680</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">14</td>
<td>SWAP</td>
<td>6.00</td>
<td>0.031107</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">15</td>
<td>SWAP</td>
<td>7.00</td>
<td>0.032313</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">16</td>
<td>SWAP</td>
<td>8.00</td>
<td>0.033382</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">17</td>
<td>SWAP</td>
<td>9.00</td>
<td>0.034385</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">18</td>
<td>SWAP</td>
<td>10.00</td>
<td>0.035312</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">19</td>
<td>SWAP</td>
<td>11.00</td>
<td>0.036197</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">20</td>
<td>SWAP</td>
<td>12.00</td>
<td>0.037003</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">21</td>
<td>SWAP</td>
<td>13.00</td>
<td>0.037668</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">22</td>
<td>SWAP</td>
<td>14.00</td>
<td>0.038201</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">23</td>
<td>SWAP</td>
<td>15.00</td>
<td>0.038624</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">24</td>
<td>SWAP</td>
<td>20.00</td>
<td>0.039380</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">25</td>
<td>SWAP</td>
<td>25.00</td>
<td>0.038501</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">26</td>
<td>SWAP</td>
<td>30.00</td>
<td>0.037668</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>En l’absence d’oppotunité d’arbitrage, les valorisations des instruments de marché s’expriment en fonction des taux zéro coupon implicites suivantes :</p>
<ul>
<li><p>Sur le segment <strong>Money Market</strong>, on cote en taux monétaires :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20L_t(T,T+%5Cdelta)%20=%20%5Cfrac%7B1%7D%7B%5Cdelta%7D%20%5Cleft(%20%5Cfrac%7BB(t,T)%7D%7BB(t,T+%5Cdelta)%7D%20-%201%20%5Cright),%0A%20%20"></p>
<p>avec t le temps courant, T la maturité et <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> la période de capitalisation. Dans notre cas, t=T=0 et <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> varie en fonction de la maturité.</p></li>
<li><p>Sur le segment <strong>Future</strong>, on côte en 1 - tx forward :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20future(T,%20T+%5Cdelta)%20=%201%20-%20L_t(T,T+%5Cdelta).%0A%20%20"> Dans notre cas, t=0, T= maturité - 3m et <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20=%203m">.</p></li>
<li><p>Sur le segment <strong>Swap</strong>, on côte en taux swap :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20Swap(t,%20T_0,%20T_n)%20=%20B(t,%20T_0)%E2%88%92B(t,%20T_n)%E2%88%92K%20%C3%97lvl(t),%0A%20%20"></p>
<p>avec K le taux fixe du swap qui égalise la PV du swap vaut 0 et <img src="https://latex.codecogs.com/png.latex?lvl(t)=%5Csum_%7Bi=1%7D%5E%7Bn%7D%20%5Cdelta_i%20B(t,%20T_i)"> le taux de marché à la maturité <img src="https://latex.codecogs.com/png.latex?T_n">. Dans notre cas, la date de départ est le spot, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?T_0=0"> et <img src="https://latex.codecogs.com/png.latex?T_n"> est la maturité du swap, et t=0 (vu d’aujourd’hui).</p></li>
</ul>
<p>De ce fait, le données ne sont pas homogènes en taux du fait de la différente cotations des instruments. Nous allons donc les transformer en taux monétaires pour les homogénéiser.</p>
<p><strong>Remarques préliminaires</strong> : - En zone EURO, les swaps standards côtés sur le marché ont une fréquence de paiement semestrielle pour la patte variable et annuelle pour la patte fixe. Ainsi pour le calcul du level du swap, <img src="https://latex.codecogs.com/png.latex?%5Cdelta=1"> et on ajoute progressivement les taux de marché. - Pour simplifier les calculs, nous supposerons que les dates de départ des taux monétaires et des taux de swap sont spot (i.e.&nbsp;T0 = 0 et non 1 ou 2 jours).</p>
<p><strong>Methode de bootstrapping &amp; stripping</strong> :</p>
<p>Pour extraire les taux zéro coupon implicites, nous allons utiliser la méthode de bootstrapping. Cette méthode consiste à calculer les taux zéro coupon implicites à partir des taux de marché. Pour cela, nous allons utiliser les formules des taux monétaires présentées ci-dessus, qui sont vu comme des fonctions de taux zéro coupon implicites.</p>
<p>Comme les taux de swap ne sont pas nécessairement disponibles pour toutes les maturités annuelles, il faut interpoler les taux intermédiaires. Cela permettra de simplifier la méthode de bootstrapping. Nous allons utiliser une interpolation par spline cubic afin d’avoir des taux swap par an. Une interpolation par spline permet d’avoir des bonnes propriétés en terme de dérivabilité et de continuité de la courbe de taux.</p>
<p>Il s’agira donc de construire une nouvelle courbe de taux de marché discrète avec des cotations annuelles de taux swap à l’aide d’une méthode d’interpolation par spline, en plus des autres instruments. Par la suite, on supposera que cette nouvelle courbe est la courbe de marché de référence, i.e.&nbsp;la courbe utilisée pour impliciter les taux zéro coupon.</p>
<p>Enfin, nous allons faire du stripping afin de reconstituer une courbe de taux zero coupon implicite plus lisse à l’aide de différentes méthodes d’interpolations (linéaire, spline, etc).</p>
</section>
<section id="i.2.-construction-de-la-courbe-des-taux-zéro-coupon" class="level3">
<h3 class="anchored" data-anchor-id="i.2.-construction-de-la-courbe-des-taux-zéro-coupon">I.2. Construction de la courbe des taux zéro-coupon</h3>
<div id="5ee4e138" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.interpolate <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> interp1d</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> interpolate_and_update_df(data, col_x, col_y, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cubic'</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb2-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Interpole les taux SWAP et met à jour le DataFrame avec les nouvelles valeurs interpolées.</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Paramètres :</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - data : DataFrame d'origine contenant une colonne 'type' avec 'SWAP', 'T' et 'tx'.</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - kind : Type d'interpolation (par défaut 'cubic', peut être 'linear', 'quadratic', etc.).</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - start : Valeur minimale de T pour l'interpolation (par défaut 3).</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - end : Valeur maximale de T pour l'interpolation (par défaut 31).</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - step : Pas d'incrémentation pour la grille interpolée (par défaut 1).</span></span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - new_df : DataFrame mis à jour avec les taux SWAP interpolés.</span></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-19"></span>
<span id="cb2-20">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.copy()</span>
<span id="cb2-21">    x, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[col_x].values, data[col_y].values</span>
<span id="cb2-22"></span>
<span id="cb2-23">    f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interp1d(x, y, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>kind)</span>
<span id="cb2-24">    xnew <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(start, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>step, step)</span>
<span id="cb2-25">    tx_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(xnew)</span>
<span id="cb2-26">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({col_x: xnew, col_y: tx_new})</span>
<span id="cb2-27"></span>
<span id="cb2-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df</span>
<span id="cb2-29"></span>
<span id="cb2-30">df_interp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interpolate_and_update_df(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>) </span>
<span id="cb2-31">df_interp[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SWAP'</span></span>
<span id="cb2-32">df_interp.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">T</th>
<th data-quarto-table-cell-role="th">tx</th>
<th data-quarto-table-cell-role="th">type</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>3</td>
<td>0.026112</td>
<td>SWAP</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>4</td>
<td>0.028117</td>
<td>SWAP</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>5</td>
<td>0.029680</td>
<td>SWAP</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>6</td>
<td>0.031107</td>
<td>SWAP</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>7</td>
<td>0.032313</td>
<td>SWAP</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="e98d35fa" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Le rajoiuter dans le df</span></span>
<span id="cb3-2">new_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([data[data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SWAP"</span>], df_interp], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb3-3">new_df.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">type</th>
<th data-quarto-table-cell-role="th">T</th>
<th data-quarto-table-cell-role="th">tx</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>MM</td>
<td>0.25</td>
<td>0.030698</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>MM</td>
<td>0.50</td>
<td>0.026191</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>MM</td>
<td>0.75</td>
<td>0.023958</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>MM</td>
<td>1.00</td>
<td>0.022979</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>FUT</td>
<td>1.25</td>
<td>0.978691</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="4dd5e163" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage d'une courbe homogène de taux de marché en fonction de la maturité</span></span>
<span id="cb4-2">new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx_h'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'FUT'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb4-5">plt.plot(new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>], new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_h"</span>], marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>)</span>
<span id="cb4-6">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Maturité'</span>)</span>
<span id="cb4-7">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux de marché'</span>)</span>
<span id="cb4-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux de marché'</span>)</span>
<span id="cb4-9">plt.grid()</span>
<span id="cb4-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="extraction-du-taux-zero-coupon-sur-le-segment-money-market" class="level4">
<h4 class="anchored" data-anchor-id="extraction-du-taux-zero-coupon-sur-le-segment-money-market">Extraction du taux zero coupon sur le segment Money Market</h4>
<p>Les taux zéro-coupon continus sont définis par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar(t,T)%20=%20-%5Cfrac%7B1%7D%7BT-t%7D%20%5Cln%20B(t,T),%0A"></p>
<p>où B(t,T) est le facteur d’actualisation (<img src="https://latex.codecogs.com/png.latex?B(t,T)%20=%20exp(-r(t,T)%20%5Ctimes%20%20T)"> ), i.e.&nbsp;le prix d’une obligation zéro-coupon de maturité T à la date t.</p>
<p>Ils sont la brique de base pour la valorisation des produits dérivés et des obligations. De ce fait, nous allons essayer de reconstituer la courbe des taux zéro-coupon implicite à partir de la courbe des taux de marché à l’aide de la méthode du bootstrapping. Cette méthode consiste à calculer les taux zéro-coupon implicites à partir des taux de marché en utilisant la formule suivante selon le segment Money Market :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AL_0(0,%5Cdelta)%20=%20%5Cfrac%7B1%7D%7B%5Cdelta%7D%20%5Cleft(%20%5Cfrac%7B1%7D%7BB(0,%5Cdelta)%7D%20-%201%20%5Cright)%0A"></p>
<p>De ce fait, le facteur d’actualisation est : <img src="https://latex.codecogs.com/png.latex?%0AB(0,%5Cdelta)%20=%20%5Cfrac%7B1%7D%7B1%20+%20%5Cdelta%20L_0(0,%5Cdelta)%7D%0A"></p>
<div id="d2394b02" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des facteurs d'actualisation pour les Money Market</span></span>
<span id="cb5-2">mm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MM'</span>]</span>
<span id="cb5-3"></span>
<span id="cb5-4">mm.loc[:, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mm[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mm[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>])</span>
<span id="cb5-5">mm.loc[:, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(mm[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mm[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>]</span>
<span id="cb5-6"></span>
<span id="cb5-7">df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mm</span></code></pre></div>
</div>
</section>
<section id="extraction-du-taux-zero-coupon-sur-le-segment-future" class="level4">
<h4 class="anchored" data-anchor-id="extraction-du-taux-zero-coupon-sur-le-segment-future">Extraction du taux zero coupon sur le segment Future</h4>
<p>Sur le segment Future, on a :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AFuture%20=%201%20-%20L_0(T,T+%5Cdelta)%20=%201%20-%20%5Cfrac%7B1%7D%7B%5Cdelta%7D%20%5Cleft(%20%5Cfrac%7BB(0,T)%7D%7BB(0,T+%5Cdelta)%7D%20-%201%20%5Cright)%0A"></p>
<p>De ce fait, le facteur d’actualisation est : <img src="https://latex.codecogs.com/png.latex?%0AB(0,T+%5Cdelta)%20=%20%5Cfrac%7BB(0,T)%7D%7B1%20+%20%5Cdelta%20(1-%20Future)%7D%0A"></p>
<div id="de84dd5e" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des facteurs d'actualisation pour les Futures</span></span>
<span id="cb6-2"></span>
<span id="cb6-3">fut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'FUT'</span>]</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># concat à mm</span></span>
<span id="cb6-6">df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([df_ZC, fut], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb6-7"></span>
<span id="cb6-8">mm_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(mm)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(mm_len, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df_ZC)):</span>
<span id="cb6-11">    df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb6-12">    df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>]</span></code></pre></div>
</div>
</section>
<section id="extraction-du-taux-zero-coupon-sur-le-segment-swap" class="level4">
<h4 class="anchored" data-anchor-id="extraction-du-taux-zero-coupon-sur-le-segment-swap">Extraction du taux zero coupon sur le segment swap</h4>
<p>Pour le segment swap payeur, on a :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0ASwap(t,%20T_0,%20T_n)%20=%20B(t,%20T_0)%E2%88%92B(t,%20T_n)%E2%88%92K%20%C3%97lvl(t)%20=%200,%0A"></p>
<p>avec K le taux fixe du swap qui fait que la PV du swap vaut 0 et <img src="https://latex.codecogs.com/png.latex?lvl(t)=%5Csum_%7Bi=1%7D%5E%7Bn%7D%20%5Cdelta_i%20B(t,%20T_i)"> le taux de marché à la maturité <img src="https://latex.codecogs.com/png.latex?T_n">. De ce fait, le facteur d’actualisation est : <img src="https://latex.codecogs.com/png.latex?%0AB(0,T_n)%20=%20%5Cfrac%7B1%20-%20K%20%5Csum_%7Bi=1%7D%5E%7Bn-1%7D%20%5Cdelta_i%20B(0,T_i)%7D%7B1%20+%20K%7D%0A"></p>
<div id="99d72d61" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des facteurs d'actualisation pour les Swaps</span></span>
<span id="cb7-2">swap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SWAP'</span>]</span>
<span id="cb7-3">fut_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(fut)</span>
<span id="cb7-4"></span>
<span id="cb7-5">df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([df_ZC, swap], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(mm_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>fut_len, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df_ZC)):</span>
<span id="cb7-8">    T_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Récupère la valeur de T actuelle</span></span>
<span id="cb7-9">    mask <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> T_n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sélectionne uniquement les T entiers &lt; T_n</span></span>
<span id="cb7-10">    df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(df_ZC.loc[mask, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].fillna(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>])</span>
<span id="cb7-11">    df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> T_n</span></code></pre></div>
</div>
<div id="52810de8" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb8-2">plt.plot(df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe de taux zéro coupon'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb8-3">plt.plot(new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>], new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_h"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux de marché'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb8-4">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb8-5">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>)</span>
<span id="cb8-6">plt.legend()</span>
<span id="cb8-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe de taux zéro coupon discrétisée'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>Text(0.5, 1.0, 'Courbe de taux zéro coupon discrétisée')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-9-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Comme on peut le constater, le mode d’interpolation a un impact significatif sur le calcul des taux de marché car il affecte la forme de la courbe des taux et donc la valorisation des instruments financiers.</p>
</section>
</section>
<section id="i.3.-construction-de-la-courbe-des-taux-forward" class="level3">
<h3 class="anchored" data-anchor-id="i.3.-construction-de-la-courbe-des-taux-forward">I.3. Construction de la courbe des taux forward</h3>
<p>A partir de la courbe des taux zéro-coupon issue de la méthode de bootstrapping, nous souhaitons tracer la courbe des taux forwards de tenor 3M en fonction de la maturité à l’aide des méthodes d’interpolation linéaire et par spline, avec une discrétisation de 0.1 an.</p>
<p>Pour tracer la courbe taux forward, on utilisera la formule suivante pour calculer les taux forward :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AL_0(T,T+%5Cdelta)%20=%20%5Cfrac%7B1%7D%7B%5Cdelta%7D%20(%5Cfrac%7BB(0,T)%7D%7BB(0,T+%5Cdelta)%7D%20-%201)%0A"> avec <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20=%200.25">.</p>
<p>Pour le segment swap, il s’agira d’interpoler les taux zéro-coupon implicites pour avoir des tx forwards 3M. # changer la discretisatio à 1an ce qui est différent du ténor.</p>
<div id="8dfb90bb" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]}), df_ZC], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
</div>
<div id="c03baeff" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">new_df_ZC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interpolate_and_update_df(df_ZC, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'linear'</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb11-2"></span>
<span id="cb11-3">tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 mois = 0.25 an</span></span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compute_forward_rates(R, T_range, tau):</span>
<span id="cb11-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">""" Calcule les taux forward pour chaque maturité """</span></span>
<span id="cb11-7">    fwd_rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb11-8">    T_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb11-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(T_range)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb11-10">        T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T_range[i]</span>
<span id="cb11-11">        T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tau</span>
<span id="cb11-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(T_range):</span>
<span id="cb11-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Éviter d'extrapoler au-delà des données disponibles</span></span>
<span id="cb11-14">        B_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>R[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T)</span>
<span id="cb11-15">        R_T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.interp(T_tau, T_range, R) </span>
<span id="cb11-16">        B_T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>R_T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T_tau)</span>
<span id="cb11-17"></span>
<span id="cb11-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Formule du taux forward instantané</span></span>
<span id="cb11-19">        fwd_rate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (B_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_T_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau</span>
<span id="cb11-20">        fwd_rates.append(fwd_rate)</span>
<span id="cb11-21">        T_values.append(T)</span>
<span id="cb11-22"></span>
<span id="cb11-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pd.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>: T_values, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_fwd"</span>: fwd_rates})</span>
<span id="cb11-24"></span>
<span id="cb11-25">fwd_rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_forward_rates(new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], tau)</span>
<span id="cb11-26"></span>
<span id="cb11-27">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb11-28">plt.plot(fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>],fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_fwd"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb11-29">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb11-30">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb11-31">plt.legend()</span>
<span id="cb11-32">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux forward avec interpolation linéaire'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<pre><code>Text(0.5, 1.0, 'Courbe des taux forward avec interpolation linéaire')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-11-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="1f4ffa0d" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">new_df_ZC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interpolate_and_update_df(df_ZC, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cubic'</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb13-2"></span>
<span id="cb13-3">tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 mois = 0.25 an</span></span>
<span id="cb13-4">fwd_rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_forward_rates(new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], tau)</span>
<span id="cb13-5"></span>
<span id="cb13-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb13-7">plt.plot(fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>],fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_fwd"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb13-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb13-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb13-10">plt.legend()</span>
<span id="cb13-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux forward avec interpolation par spline'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="11">
<pre><code>Text(0.5, 1.0, 'Courbe des taux forward avec interpolation par spline')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-12-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Lorsqu’on utilise une interpolation linéaire, on obtient une courbe plus discontinue. La courbe a une structure en marches d’escalier. Il y a des sauts brusques lorsque l’on passe d’un intervalle à un autre. En effet, par nature, l’interpolation linéaire qui ne prend pas en compte les points intermédiaires. En interpolant avec une fonction spline, on obtient une courbe plus lisse et continue.Elle est plus cohérente avec l’évolution naturelle des taux d’intérêt. En effet, la fonction spline est une fonction polynomiale qui passe par tous les points de la courbe. Elle est plus flexible et permet de mieux capturer les variations des taux d’intérêt.</p>
<p>Nous sommes intéressés à ce qui pourrait se passer lorsque nous shiftons le taux de swap 5Y de 10 points de base. Cela permet de déterminer la sensibilité de la courbe des taux forward aux variations des taux de swap et donc donner des indications sur comment hedger ce risque.</p>
<p>Nous allons donc calculer le taux forward 3M pour les deux courbes de taux forward et comparer les résultats.</p>
<div id="697cbf23" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">choc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb15-2">new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_s"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx_h'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>choc <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx_h'</span>], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb15-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb15-6">plt.plot(new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>], new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_s"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux de marché shiftés'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span>
<span id="cb15-7">plt.plot(new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>], new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_h"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux de marché'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>)</span>
<span id="cb15-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Maturité'</span>)</span>
<span id="cb15-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux de marché'</span>)</span>
<span id="cb15-10">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux de marché'</span>)</span>
<span id="cb15-11">plt.legend()</span>
<span id="cb15-12">plt.grid()</span>
<span id="cb15-13">plt.show()</span>
<span id="cb15-14"></span>
<span id="cb15-15">new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_s"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>choc <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tx'</span>], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="3b1d5be1" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des facteurs d'actualisation pour les Money Market</span></span>
<span id="cb16-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb16-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb16-4"></span>
<span id="cb16-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compute_discount_factors(new_df, col_T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>, col_tx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx"</span>):</span>
<span id="cb16-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcule les facteurs d'actualisation (B) et les taux zéro-coupon (R) </span></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    à partir des taux du marché pour les instruments MM, FUT et SWAP.</span></span>
<span id="cb16-9"></span>
<span id="cb16-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Paramètres :</span></span>
<span id="cb16-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - new_df : DataFrame contenant les taux du marché avec les colonnes spécifiées.</span></span>
<span id="cb16-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - col_T : Nom de la colonne contenant les maturités (ex: "T").</span></span>
<span id="cb16-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - col_tx : Nom de la colonne contenant les taux du marché (ex: "tx").</span></span>
<span id="cb16-14"></span>
<span id="cb16-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - df_ZC : DataFrame contenant les facteurs d'actualisation et les taux zéro-coupon.</span></span>
<span id="cb16-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb16-18"></span>
<span id="cb16-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Extraction des données du marché monétaire (MM) ---</span></span>
<span id="cb16-20">    mm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MM'</span>].copy()</span>
<span id="cb16-21">    mm.loc[:, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mm[col_tx] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mm[col_T])</span>
<span id="cb16-22">    mm.loc[:, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(mm[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mm[col_T]</span>
<span id="cb16-23"></span>
<span id="cb16-24">    df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mm.copy()</span>
<span id="cb16-25"></span>
<span id="cb16-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Extraction des données Futures (FUT) ---</span></span>
<span id="cb16-27">    fut <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'FUT'</span>].copy()</span>
<span id="cb16-28">    df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([df_ZC, fut], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb16-29"></span>
<span id="cb16-30">    mm_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(mm)</span>
<span id="cb16-31"></span>
<span id="cb16-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Calcul des facteurs d'actualisation pour les Futures ---</span></span>
<span id="cb16-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(mm_len, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df_ZC)):</span>
<span id="cb16-34">        df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> df_ZC.loc[i, col_tx]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb16-35">        df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> df_ZC.loc[i, col_T]</span>
<span id="cb16-36"></span>
<span id="cb16-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Extraction des données Swaps (SWAP) ---</span></span>
<span id="cb16-38">    swap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_df[new_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'type'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SWAP'</span>].copy()</span>
<span id="cb16-39">    fut_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(fut)</span>
<span id="cb16-40">    df_ZC <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([df_ZC, swap], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb16-41"></span>
<span id="cb16-42">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Calcul des facteurs d'actualisation pour les Swaps ---</span></span>
<span id="cb16-43">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(mm_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> fut_len, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df_ZC)):</span>
<span id="cb16-44">        T_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[i, col_T]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maturité actuelle</span></span>
<span id="cb16-45">        mask <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (df_ZC[col_T] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> T_n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (df_ZC[col_T] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sélection des T entiers &lt; T_n</span></span>
<span id="cb16-46"></span>
<span id="cb16-47">        sum_B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(df_ZC.loc[mask, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].fillna(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb16-48">        df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> df_ZC.loc[i, col_tx] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sum_B) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> df_ZC.loc[i, col_tx])</span>
<span id="cb16-49">        df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.log(df_ZC.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> T_n</span>
<span id="cb16-50"></span>
<span id="cb16-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df_ZC</span>
<span id="cb16-52"></span>
<span id="cb16-53"></span>
<span id="cb16-54">df_ZC_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_discount_factors(new_df, col_T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>, col_tx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_s"</span>)</span>
<span id="cb16-55">df_ZC_s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.concat([pd.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]}), df_ZC_s], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb16-56"></span>
<span id="cb16-57">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb16-58">plt.plot(df_ZC_s.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], df_ZC_s.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe de taux zéro coupon shifté'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb16-59">plt.plot(df_ZC.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>], df_ZC.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe de taux zéro coupon non shifté'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb16-60">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb16-61">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>)</span>
<span id="cb16-62">plt.legend()</span>
<span id="cb16-63">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe de taux zéro coupon discrétisée'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<pre><code>Text(0.5, 1.0, 'Courbe de taux zéro coupon discrétisée')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-14-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="be28652f" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">new_df_ZC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interpolate_and_update_df(df_ZC_s, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'linear'</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb18-2"></span>
<span id="cb18-3">tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 mois = 0.25 an</span></span>
<span id="cb18-4">fwd_rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_forward_rates(new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], tau)</span>
<span id="cb18-5"></span>
<span id="cb18-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb18-7">plt.plot(fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>],fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_fwd"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb18-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb18-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb18-10">plt.legend()</span>
<span id="cb18-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux forward avec interpolation par spline'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<pre><code>Text(0.5, 1.0, 'Courbe des taux forward avec interpolation par spline')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-15-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="3760de2a" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">new_df_ZC<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> interpolate_and_update_df(df_ZC_s, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cubic'</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb20-2"></span>
<span id="cb20-3">tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 mois = 0.25 an</span></span>
<span id="cb20-4">fwd_rates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_forward_rates(new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>], new_df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>], tau)</span>
<span id="cb20-5"></span>
<span id="cb20-6">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb20-7">plt.plot(fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T"</span>],fwd_rates[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tx_fwd"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb20-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>)</span>
<span id="cb20-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Taux forward'</span>)</span>
<span id="cb20-10">plt.legend()</span>
<span id="cb20-11">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Courbe des taux forward avec interpolation par spline'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>Text(0.5, 1.0, 'Courbe des taux forward avec interpolation par spline')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-16-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En shiftant le taux de swap 5Y, la courbe de forward baisse brusquement pour T=5Y. Il y a une déformation locale de la courbe des taux forward. Cela signifie que la courbe des taux forward est sensible aux variations des taux de swap. En effet, les taux swap sont des instruments financiers qui permettent de se couvrir contre les variations des taux d’intérêt.</p>
</section>
</section>
<section id="ii.-valorisation-de-swaptions-et-de-caplets" class="level2">
<h2 class="anchored" data-anchor-id="ii.-valorisation-de-swaptions-et-de-caplets">II. Valorisation de swaptions et de caplets</h2>
<ol type="1">
<li><p>Pour coter les caplets/floorlets et swaptions, le modèle de Black est souvent utilisé. Ce modèle est basé sur l’hypothèse que les taux d’intérêt sont log-normalement distribués. Il permet de calculer le prix d’un caplet/floorlet et d’un swaption en fonction des taux d’intérêt et de la volatilité implicite.</p></li>
<li><p>L’EDS (Equation Différentielle Stochastique) de Black est donnée par :</p></li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0AdL(t)%20=%20%5Csigma%20L(t)%20dW(t)%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?L(t)"> le taux, <img src="https://latex.codecogs.com/png.latex?%5Csigma"> la volatilité du taux et <img src="https://latex.codecogs.com/png.latex?W(t)"> un mouvement brownien. En utilisant le changement de numéraire, ce taux est une martingale sous la mesure du numeraire (probabilité risque neutre). Cela permet de calculer le prix d’un caplet/floorlet ou d’une swaption.</p>
<p><strong>Caplets et Floorlets</strong></p>
<ol start="3" type="1">
<li>Pour un caplet, le prix est donné par la formule suivante :</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0ACaplet(t,T_%7Bi-1%7D,T_i)%20=%20N%20%5Cdelta_i%20B(t,T_i)%20%5Cleft%5B%20L_i(t)%20%5Cphi(d)%20-%20K%20%5Cphi%20(d%20-%20%5Csigma_i%20%5Csqrt%7BT_%7Bi-1%7D-t%7D%20)%5Cright%5D%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?d%20=%20%5Cfrac%7B1%7D%7B%5Csigma%20%5Csqrt%7BT_%7Bi-1%7D-t%7D%7D%20%5Cleft(%20%5Cln%20%5Cleft(%20%5Cfrac%7BL_i(t)%7D%7BK%7D%20%5Cright)%20+%20%5Cfrac%7B%5Csigma%5E2(T_%7Bi-1%7D-t)%7D%7B2%7D%20%5Cright)">, <img src="https://latex.codecogs.com/png.latex?L_i(t)"> le taux forward 3M à la date t, <img src="https://latex.codecogs.com/png.latex?K"> le strike du caplet, <img src="https://latex.codecogs.com/png.latex?N"> le nominal, <img src="https://latex.codecogs.com/png.latex?%5Cdelta_i"> la période de capitalisation, <img src="https://latex.codecogs.com/png.latex?B(t,T_i)"> le facteur d’actualisation à la maturité <img src="https://latex.codecogs.com/png.latex?T_i">, <img src="https://latex.codecogs.com/png.latex?%5Csigma_i"> la volatilité du taux forward 3M à la maturité <img src="https://latex.codecogs.com/png.latex?T_i"> et <img src="https://latex.codecogs.com/png.latex?%5Cphi"> la fonction de répartition de la loi normale standard.</p>
<p>Pour un floorlet, le prix est donné par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AFloorlet(t,T_%7Bi-1%7D,T_i)%20=%20N%20%5Cdelta_i%20B(t,T_i)%20%5Cleft%5B%20K%20%5Cphi%20(d%20-%20%5Csigma_i%20%5Csqrt(T_%7Bi-1%7D-t)%20)%20-%20L_i(t)%20%5Cphi(d)%20%5Cright%5D%0A"></p>
<p><strong>Swaptions</strong></p>
<p>Pour un swaption donneur, le prix est donné par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSwaption%7D_t%20=%20%5Cleft(%20%5Csum_%7Bj=1%7D%5E%7Bn%7D%20N%20%5Cdelta%20B(t,%20T_j)%20%5Cright)%20%5Cleft%5B%20F_S(t)%20%5CPhi(d)%20-%20K%20%5CPhi(d%20-%20%5Csigma_S%20%5Csqrt%7BT_0%20-%20t%7D)%20%5Cright%5D%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?F_S(t)"> le taux swap à la date t, <img src="https://latex.codecogs.com/png.latex?K"> le strike du swaption, <img src="https://latex.codecogs.com/png.latex?N"> le nominal, <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> la période de capitalisation, <img src="https://latex.codecogs.com/png.latex?B(t,T_j)"> le facteur d’actualisation à la maturité <img src="https://latex.codecogs.com/png.latex?T_j">, <img src="https://latex.codecogs.com/png.latex?%5Csigma_S"> la volatilité du taux swap et <img src="https://latex.codecogs.com/png.latex?%5CPhi"> la fonction de répartition de la loi normale standard.</p>
<p>d est donné par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%20=%20%5Cfrac%7B1%7D%7B%5Csigma_S%20%5Csqrt%7BT_0%20-%20t%7D%7D%20%5Cleft(%20%5Cln%20%5Cleft(%20%5Cfrac%7BF_S(t)%7D%7BK%7D%20%5Cright)%20+%20%5Cfrac%7B%5Csigma_S%5E2(T_0%20-%20t)%7D%7B2%7D%20%5Cright)%0A"></p>
<p>Pour un swaption receveur, le prix est donné par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSwaption%7D_t%20=%20%5Cleft(%20%5Csum_%7Bj=1%7D%5E%7Bn%7D%20N%20%5Cdelta%20B(t,%20T_j)%20%5Cright)%20%5Cleft%5B%20K%20%5CPhi(d%20-%20%5Csigma_S%20%5Csqrt%7BT_0%20-%20t%7D)%20-%20F_S(t)%20%5CPhi(d)%20%5Cright%5D%0A"></p>
<p>Il s’agit, à partir des cotations décrites dans le tableau ci-dessous et de la courbe des taux zéro-coupon construite précédemment, calculer les prix de marché de caplets sur euribor12M, ce qui implique une période de capitalisation annuelle, de maturité T = 5Y, i.e.&nbsp;payé à 6Y, et de strikes K associés au tableau. Nous souhaitons ainsi calculer : - Le prix des caplets Caplet(t, 5Y, 6Y) pour les strikes du tableau ci-dessous. - Le prix des swaptions Swaption(t, 5Y, 6Y) pour les strikes du tableau ci-dessous.</p>
<div id="6f63b268" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">vol_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_excel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/Data_tx.xlsx'</span>, sheet_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'vol'</span>)</span>
<span id="cb22-2">vol_data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Strike en bps et en rel. / fwd</th>
<th data-quarto-table-cell-role="th">Vols Caplets</th>
<th data-quarto-table-cell-role="th">Vols Swaptions</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>-100</td>
<td>0.311859</td>
<td>0.311859</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>-50</td>
<td>0.283274</td>
<td>0.283274</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>-25</td>
<td>0.265921</td>
<td>0.265921</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>0</td>
<td>0.250000</td>
<td>0.250000</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>25</td>
<td>0.243451</td>
<td>0.243451</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>50</td>
<td>0.249019</td>
<td>0.249019</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>100</td>
<td>0.271828</td>
<td>0.271828</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="3d96f7f3" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb23-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> price_oplet(N, delta_i, B_t_Ti, L_i_t, K, sigma_i, Ti, t, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>):</span>
<span id="cb23-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb23-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcule la valeur d'un caplet selon le modèle de Black.</span></span>
<span id="cb23-7"></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Paramètres :</span></span>
<span id="cb23-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - N : Notional</span></span>
<span id="cb23-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - delta_i : Période du caplet </span></span>
<span id="cb23-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - B_t_Ti : Facteur d'actualisation B(t, Ti)</span></span>
<span id="cb23-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - L_i_t : Taux forward Li(t)</span></span>
<span id="cb23-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - K : Strike du caplet</span></span>
<span id="cb23-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - sigma_i : Volatilité implicite</span></span>
<span id="cb23-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - Ti_1 : Date de début de la période</span></span>
<span id="cb23-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - Ti : Date de fin de la période</span></span>
<span id="cb23-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - t : Temps actuel</span></span>
<span id="cb23-18"></span>
<span id="cb23-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb23-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - Valeur du caplet</span></span>
<span id="cb23-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb23-22">    Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta_i</span>
<span id="cb23-23">    d1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (np.log(L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sigma_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t))</span>
<span id="cb23-24">    d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d1</span>
<span id="cb23-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> option_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>:</span>
<span id="cb23-26">        price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.cdf(d1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.cdf(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>d2))</span>
<span id="cb23-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> option_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'floorlet'</span>:</span>
<span id="cb23-28">        price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.cdf(d2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.cdf(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>d1))</span>
<span id="cb23-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> price</span>
<span id="cb23-30"></span>
<span id="cb23-31"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vol_data.index:</span>
<span id="cb23-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Notional</span></span>
<span id="cb23-33">    N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  </span>
<span id="cb23-34"></span>
<span id="cb23-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Période du caplet</span></span>
<span id="cb23-36">    delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb23-37"></span>
<span id="cb23-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maturité du caplet</span></span>
<span id="cb23-39">    T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> </span>
<span id="cb23-40"></span>
<span id="cb23-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facteur d'actualisation B(t, Ti)</span></span>
<span id="cb23-42">    B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb23-43"></span>
<span id="cb23-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Taux forward Li(t)</span></span>
<span id="cb23-45">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>delta_i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> T, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-46"></span>
<span id="cb23-47">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike du caplet</span></span>
<span id="cb23-48">    K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike en bps et en rel. / fwd'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb23-49"></span>
<span id="cb23-50">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Volatilité implicite</span></span>
<span id="cb23-51">    sigma_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vols Caplets"</span>]  </span>
<span id="cb23-52"></span>
<span id="cb23-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Date de début de la période</span></span>
<span id="cb23-54">    Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta_i</span>
<span id="cb23-55"></span>
<span id="cb23-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Temps actuel</span></span>
<span id="cb23-57">    t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>  </span>
<span id="cb23-58"></span>
<span id="cb23-59">    caplet_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet(N, delta_i, B_t_Ti, L_i_t, K, sigma_i, Ti, t, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>)</span>
<span id="cb23-60">    vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Caplet Price MKT'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> caplet_price</span>
<span id="cb23-61"></span>
<span id="cb23-62">vol_data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="17">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Strike en bps et en rel. / fwd</th>
<th data-quarto-table-cell-role="th">Vols Caplets</th>
<th data-quarto-table-cell-role="th">Vols Swaptions</th>
<th data-quarto-table-cell-role="th">Caplet Price MKT</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>-100</td>
<td>0.311859</td>
<td>0.311859</td>
<td>0.012511</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>-50</td>
<td>0.283274</td>
<td>0.283274</td>
<td>0.009788</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>-25</td>
<td>0.265921</td>
<td>0.265921</td>
<td>0.008419</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>0</td>
<td>0.250000</td>
<td>0.250000</td>
<td>0.007137</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>25</td>
<td>0.243451</td>
<td>0.243451</td>
<td>0.006185</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>50</td>
<td>0.249019</td>
<td>0.249019</td>
<td>0.005661</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>100</td>
<td>0.271828</td>
<td>0.271828</td>
<td>0.005167</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="ccc3e980" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pricer les swaptions</span></span></code></pre></div>
</div>
</section>
<section id="iii.-modèle-de-hull-white" class="level2">
<h2 class="anchored" data-anchor-id="iii.-modèle-de-hull-white">III. Modèle de Hull-White</h2>
<section id="iii.1-du-modèle-hjm-vers-le-modèle-hullwhite" class="level4">
<h4 class="anchored" data-anchor-id="iii.1-du-modèle-hjm-vers-le-modèle-hullwhite">III.1 Du modèle HJM vers le modèle Hull&amp;White</h4>
<p>Pour une maturité <img src="https://latex.codecogs.com/png.latex?T"> fixée, Heath, Jarrow et Morton ont supposé que le taux forward instantané évolue selon la dynamique suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Adf(t,%20T)%20=%20%5Calpha(t,%20T)%5C,%20dt%20+%20%5Csigma(t,%20T)%5C,%20dW_t%20%5Cquad%20(1)%0A"></p>
<p>La dynamique (1) ne se place pas forcément dans un cadre sans opportunité d’arbitrage. Les auteurs ont montré que le processus <img src="https://latex.codecogs.com/png.latex?%5Calpha"> ne pouvait pas être choisi arbitrairement et que, pour qu’il existe une unique mesure martingale équivalente, <img src="https://latex.codecogs.com/png.latex?%5Calpha"> devait être lié à la volatilité du zéro coupon.</p>
<p>Supposons donc l’existence d’une unique mesure martingale équivalente <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D"> (mesure risque-neutre) dont le numéraire est l’actif sans risque.</p>
<p>On suppose que le prix du zéro coupon (payant une unité de devise en date <img src="https://latex.codecogs.com/png.latex?T">) évolue sous <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D"> selon l’EDS :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7BdB(t,%20T)%7D%7BB(t,%20T)%7D%20=%20r_t%5C,%20dt%20+%20%5CGamma(t,%20T)%5C,%20dW_t%5EQ%20%5Cquad%20(2)%0A"></p>
<p>Par définition, on sait que $ B(t, T) = e^{-_t^T f(t,s), ds}$ et que $ f(t, T) = -_T (B(t, T)). $</p>
<p>En appliquant le lemme d’Itô, on obtient : $ df(t, T) = (t, T), _T (t, T), dt - _T (t, T), dW_t^Q. $</p>
<p>En posant $ -_T (t, T) = (t, T), $ nous obtenons :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Adf(t,%20T)%20=%20%5Cgamma(t,%20T)%20%5Cint_t%5ET%20%5Cgamma(t,%20u)%5C,%20du%20%5C,%20dt%20+%20%5Cgamma(t,%20T)%5C,%20dW_t%5EQ%20%5Cquad%20(3)%0A"></p>
<p>Après intégration, on retrouve finalement :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(t,%20T)%20=%20f(0,%20T)%20+%20%5Cint_0%5Et%20%5Cgamma(s,%20T)%20%5Cleft(%20%5Cint_s%5ET%20%5Cgamma(s,%20u)%5C,%20du%20%5Cright)%20ds%20+%20%5Cint_0%5Et%20%5Cgamma(s,%20T)%5C,%20dW_s%5EQ%20%5Cquad%20(4)%0A"></p>
</section>
<section id="iii.2-hypothèses-du-modèle-hullwhite" class="level4">
<h4 class="anchored" data-anchor-id="iii.2-hypothèses-du-modèle-hullwhite">III.2 Hypothèses du modèle Hull&amp;White</h4>
<p>On suppose que le modèle HJM est gaussien, linéaire et calibrable. Ces hypothèses permettent d’écrire :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma(t,%20T)%20=%20%5Csigma(t)%5C,%20e%5E%7B-%5Clambda%20(T-t)%7D%20%20%5Cquad%20%5Ctext%7B%20et%7D%20%5Cquad%20%5CGamma(t,%20T)%20=%20%5Cfrac%7B%5Csigma(t)%7D%7B%5Clambda%7D%5CBigl(e%5E%7B-%5Clambda%20(T-t)%7D%20-%201%5CBigr)%0A"></p>
<p>où la fonction de volatilité instantanée <img src="https://latex.codecogs.com/png.latex?%5Csigma(t)"> est constante par morceaux.</p>
</section>
<section id="iii.3-construction-de-la-formule-zéro-coupon" class="level4">
<h4 class="anchored" data-anchor-id="iii.3-construction-de-la-formule-zéro-coupon">III.3 Construction de la formule zéro-coupon</h4>
<p>Dans le cadre du modèle Hull&amp;White, la dynamique du taux court instantané <img src="https://latex.codecogs.com/png.latex?r_t"> s’écrit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Adr_t%20=%20%5Cleft%5B%5Clambda%5Cbigl(f(0,t)%20-%20r_t%5Cbigr)%20+%20%5Cpartial_t%20f(0,t)%20+%20%5Cint_0%5Et%20%5Csigma%5E2(s)%5C,%20e%5E%7B-2%5Clambda%20(t-s)%7D%20ds%20%5Cright%5D%20dt%20+%20%5Csigma(t)%5C,%20dW_t%5EQ%20%5Cquad%20(5)%0A"></p>
<p>On introduit alors une nouvelle variable d’état : $ X_t = r_t - f(0,t)$</p>
<p>La dynamique de <img src="https://latex.codecogs.com/png.latex?X_t"> devient :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdX_t%20=%20%5Cleft%5B%5Cvarphi(t)%20-%20%5Clambda%20X_t%5Cright%5D%20dt%20+%20%5Csigma(t)%5C,%20dW_t%5EQ%20%5Cquad%20(8)%0A"></p>
<p>avec $ (t) = _0^t ^2(s), e^{-2(t-s)} ds. $</p>
<p>La formule du prix du zéro coupon s’exprime alors comme une fonction déterministe de <img src="https://latex.codecogs.com/png.latex?X_t"> :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB(t,%20T)%20=%20%5Cfrac%7BB(0,T)%7D%7BB(0,t)%7D%20%5Cexp%5C!%5CBiggl%5C%7B%20-%5Cfrac%7B1%7D%7B2%5Cbeta%5E2(t,T)%7D%20%5Cvarphi(t)%20-%20%5Cbeta(t,T)%20X_t%20%5CBiggr%5C%7D%20%5Cquad%20(9)%0A"></p>
<p>où $ (t, T) = . $</p>
<ol type="1">
<li>A quelle catégorie de modèle appartient le modèle Hull&amp;White? Justifier.</li>
</ol>
<p>Le modèle Hull &amp; White est un modèle à structure à terme aﬃne, i.e.&nbsp;un modèle de taux d’intérêt pour lequel le taux zéro-coupon continu R(t, T ) est une fonction aﬃne du taux court r (t).</p>
<p>Il ressemble à un processus d’Ornstein-Uhlenbeck ou mean reversing process, qui est un processus gaussien définit de la manière suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdY_t%20=%20-%20%5Ctheta%20%5Cleft%5BY_t%20-%20%5Cmu%20%5Cright%5D%20dt%20+%20%5Csigma%20dW_t,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Ctheta,%20%5Cmu,%20%5Csigma"> sont des paramètres déterministes et <img src="https://latex.codecogs.com/png.latex?W_t"> est le processus de Wiener.</p>
<p>Dans notre cas, on a <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Clambda">, <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20%5Cfrac%7B%5Cphi(t)%7D%7B%5Clambda%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%20%5Csigma(t)">. De ce fait, la moyenne et la variance dépend du temps et le paramètre de vitesse de retour à la moyenne est constant.</p>
<ol start="2" type="1">
<li>Déterminer la loi du processus <img src="https://latex.codecogs.com/png.latex?X_t%7CX_s">?</li>
</ol>
<p>Sous la probabilité risque neute <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BQ%7D">, le processus <img src="https://latex.codecogs.com/png.latex?X_t"> s’ecrit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AdX_t%20=%20%5Cleft(%5Cphi(t)-%20%5Clambda%20X_t%20%5Cright)%20dt%20+%20%5Csigma(t)%20dW_t%5EQ,%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?%5Cphi(t)%20=%20%5Cint_0%5Et%20%5Csigma%5E2(s)%20e%5E%7B-2%5Clambda(t-s)%7D%20ds">.</p>
<p>Posons <img src="https://latex.codecogs.com/png.latex?K_t%20=%20e%5E%7B%5Clambda%20t%7D%20X_t%20%20=%20f(X_t,%20t)%5Cimplies%20X_t%20=%20e%5E%7B-%5Clambda%20t%7D%20K_t">, par la formule d’Itô, on a :</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0Adf(X_t,%20t)%20&amp;=%20e%5E%7B%5Clambda%20t%7D%20dX_t%20+%20de%5E%7B%5Clambda%20t%7D%20X_t%20%5C%5C%0A&amp;=%20e%5E%7B%5Clambda%20t%7D%20%5Cleft(%20%5Cphi(t)%20-%20%5Clambda%20X_t%20%5Cright)%20dt%20+%20e%5E%7B%5Clambda%20t%7D%20%5Csigma(t)%20dW_t%5EQ%20+%20e%5E%7B%5Clambda%20t%7D%20X_t%20dt%20%5C%5C%0A&amp;=%20e%5E%7B%5Clambda%20t%7D%5Cphi(t)%20dt%20+%20e%5E%7B%5Clambda%20t%7D%5Csigma(t)dW_t%5EQ%20%5C%5C%0A&amp;%5Cimplies%20f(X_t,%20t)%20=%20K_t%20=%20e%5E%7B-%20%5Clambda%20t%7DK_s%20%20+%20%5Cint_s%5Et%20e%5E%7B-%5Clambda(t-u)%7D%5Cphi(u)%20du%20+%20%5Cint_s%5Et%20e%5E%7B-%5Clambda(t-u)%7D%5Csigma(u)dW_u%5EQ%20%5C%5C%0A&amp;%5CLeftrightarrow%20X_t%20=%20X_s%20e%5E%7B-%5Clambda%20(t-s)%7D%20+%20%5Cint_s%5Et%20e%5E%7B-%5Clambda%20(t-u)%7D%20%5Cphi(u)%20%20du%20+%20%5Cint_s%5Et%20e%5E%7B-%5Clambda%20(t-u)%7D%20%5Csigma(u)%20dW_u%5ET%0A%5Cend%7Baligned%7D">
<p>De ce fait, on en déduit que $X_t|X_s ( X_s e^{-(t-s)} + _s^t e^{-(t-u)} (u) du, _s^t e^{-2 (t-u)} (u)^2 d ) $.</p>
</section>
<section id="iii.4-dynamique-des-taux-forwards" class="level4">
<h4 class="anchored" data-anchor-id="iii.4-dynamique-des-taux-forwards">III.4 Dynamique des taux forwards</h4>
<p>On note ensuite <img src="https://latex.codecogs.com/png.latex?L_i(t)"> le taux LIBOR forward à la date <img src="https://latex.codecogs.com/png.latex?t"> qui fixe en <img src="https://latex.codecogs.com/png.latex?T_i"> et paie en <img src="https://latex.codecogs.com/png.latex?T_%7Bi+1%7D">. Sous l’hypothèse d’absence d’opportunité d’arbitrage, ce taux s’exprime à partir de la courbe de taux : Î <img src="https://latex.codecogs.com/png.latex?%0AL_i(t)%20=%20%5Cfrac%7B1%7D%7B%5Cdelta_i%7D%5Cleft(%5Cfrac%7BB(t,%20T_i)%7D%7BB(t,%20T_%7Bi+1%7D)%7D%20-%201%5Cright)%20%20=%20%5Cfrac%7B1%7D%7B%5Cdelta_i%7D%5Cleft(Z_t-%201%5Cright)%20,%0A"></p>
<p>Pour connaitre la dynamique des taux forwards, on applique le lemme d’Itô au processus :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AZ_t%20=%20%5Cfrac%7BB(t,%20T_i)%7D%7BB(t,%20T_%7Bi+1%7D)%7D.%0A"></p>
<blockquote class="blockquote">
<p>Rappel du lemme d’Itô : Considérons deux actifs <img src="https://latex.codecogs.com/png.latex?X"> et <img src="https://latex.codecogs.com/png.latex?Y"> et posons <img src="https://latex.codecogs.com/png.latex?Z%20=%20%5Cfrac%7BX%7D%7BY%7D"> (la valeur de <img src="https://latex.codecogs.com/png.latex?X"> exprimée en numéraire <img src="https://latex.codecogs.com/png.latex?Y">). Le lemme d’Itô nous donne l’évolution de <img src="https://latex.codecogs.com/png.latex?Z"> par :</p>
<img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7BdZ%7D%7BZ%7D%20=%20%5Cleft(%5Cfrac%7BdX%7D%7BX%7D%20-%20%5Cfrac%7BdY%7D%7BY%7D%5Cright)%20-%20%5Cleft%5Clangle%20%5Cfrac%7BdX%7D%7BX%7D%20-%20%5Cfrac%7BdY%7D%7BY%7D,%5C,%20%5Cfrac%7BdY%7D%7BY%7D%5Cright%5Crangle.%0A">
</blockquote>
<p>En appliquant le lemme d’Itô à <img src="https://latex.codecogs.com/png.latex?Z_t">, on obtient :</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0A%5Cfrac%7BdZ_t%7D%7BZ_t%7D%20&amp;=%20%5Cfrac%7BdB(t,%20T_i)%7D%7BB(t,%20T_i)%7D%20-%20%5Cfrac%7BdB(t,%20T_%7Bi+1%7D)%7D%7BB(t,%20T_%7Bi+1%7D)%7D%20%5C%5C%0A&amp;-%20%5Cfrac%7B1%7D%7B%5Ccancel%7BB(t,T_%7Bi+1%7D)%5E2%7D%7D%20%5Ccancel%7BB(t,T_%7Bi+1%7D)%5E2%7D%20%5C,%20%5CGamma(t,T_i,T_%7Bi+1%7D)%5E2%20%5C,%20dt%20%5C%5C%0A&amp;-%20%20%5Cfrac%7B-1%7D%7B%5Ccancel%7BB(t,T_i)B(t,T_%7Bi+1%7D)%7D%7D%20%5Ccancel%7BB(t,T_i)B(t,T_%7Bi+1%7D)%7D%5C,%5CGamma(t,T_i)%5CGamma(t,T_%7Bi+1%7D)%5C,dt%5C%5C%0A&amp;=%20%5CGamma(t,T_i,T_%7Bi+1%7D)%5Cleft(%5CGamma(t,T_%7Bi+1%7D)%20-%20%5CGamma(t,T_i)%5Cright)dt%20+%20%5Cleft(%5CGamma(t,T_i)%20-%20%5CGamma(t,T_%7Bi+1%7D)%5Cright)dW_t%5EQ%5C%5C%0A&amp;%5Cimplies%20%5Cfrac%7BdZ_t%7D%7BZ_t%7D%20=%20%5Cmu(t,T_i,T_%7Bi+1%7D)%5C,dt%20+%20%5Csigma(t,T_i,T_%7Bi+1%7D)%5C,dW_t%5EQ%0A%5Cend%7Baligned%7D">
<p>avec</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bcases%7D%0A%5Csigma(t,T_i,T_%7Bi+1%7D)%20=%20%5CGamma(t,T_i)%20-%20%5CGamma(t,T_%7Bi+1%7D)%5C%5C%0A%5Cmu(t,T_i,T_%7Bi+1%7D)%20=%20%5CGamma(t,T_i)%5Cleft(%5CGamma(t,T_%7Bi+1%7D)%20-%20%5CGamma(t,T_i)%20%5Cright)%20=%20-%20%5CGamma(t,T_i)%20%5Csigma(t,T_i,T_%7Bi+1%7D)%0A%5Cend%7Bcases%7D">
<p>De ce fait, on a :</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0A%5Cfrac%7BdZ_t%7D%7BZ_t%7D%20&amp;=%20-%20%5CGamma(t,T_i)%20%5Csigma(t,T_i,T_%7Bi+1%7D)%5C,dt%20+%20%5Csigma(t,T_i,T_%7Bi+1%7D)%5C,dW_t%5EQ%5C%5C%0A&amp;=%20%5Csigma(t,T_i,T_%7Bi+1%7D)%20%5Cunderbrace%7B%5Cleft(%20-%20%5CGamma(t,T_i)%5C,dt%20+%20dW_t%5EQ%20%5Cright)%7D_%7Bd%5Ctilde%7BW_t%7D%7D%5C%5C%0A&amp;=%20%5Csigma(t,T_i,T_%7Bi+1%7D)%20d%5Ctilde%7BW_t%7D%0A%5Cend%7Baligned%7D">
<p>où <img src="https://latex.codecogs.com/png.latex?d%5Ctilde%7BW_t%7D"> est un mouvement brownien selon le théorème de Girsanov.</p>
<p>La diffusion de <img src="https://latex.codecogs.com/png.latex?Z_t"> est une loi log-normale, sans drift sous la probabilité risque forward. De ce fait, il suit le modèle de Black pour la valorisation des options.</p>
<p>On peut écrire ainsi la dynamique du taux forward <img src="https://latex.codecogs.com/png.latex?L_i(t)"> sous la probabilité risque forward :</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0AdL_i(t)%20&amp;=%20%5Cfrac%7BZ_t%7D%7B%5Cdelta_i%7D%20%5Csigma(t,T_i,T_%7Bi+1%7D)%20%20d%5Ctilde%7BW_t%7D%20%20%5C%5C%0A&amp;=%20(L_i(t)%20+%20%5Cfrac%7B1%7D%7B%5Cdelta_i%7D)%20%5Csigma(t,T_i,T_%7Bi+1%7D)%20%20d%5Ctilde%7BW_t%7D%0A%5Cend%7Baligned%7D">
</section>
<section id="iii.5-valorisation-des-instruments-de-calibration" class="level4">
<h4 class="anchored" data-anchor-id="iii.5-valorisation-des-instruments-de-calibration">III.5 Valorisation des instruments de calibration</h4>
<ol type="1">
<li><strong>Payoff d’un caplet vanille :</strong><br>
Le payoff d’un caplet sur le taux LIBOR <img src="https://latex.codecogs.com/png.latex?L_i(T_i)">, de maturité <img src="https://latex.codecogs.com/png.latex?T_i">, avec paiement en <img src="https://latex.codecogs.com/png.latex?T_%7Bi+1%7D"> et de strike <img src="https://latex.codecogs.com/png.latex?K"> est donné par :</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D%20=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr).%0A"></p>
<ol start="2" type="1">
<li><strong>Formule de valorisation dans le cadre du modèle H&amp;W :</strong><br>
Il peut être démontré que la formule de valorisation de ce caplet s’exprime de la manière suivante :</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0AC%5CBigl(%20Z_t,%5C,%20%5Ctilde%7BK%7D,%5C,%20T_i,%5C,%20%5Csigma_i%5E*,%5C,%20B(t,%20T_%7Bi+1%7D)%20%5CBigr)%20%5Cquad%20(11)%0A"></p>
<p>avec :</p>
<ul>
<li>$ Z_t = $,</li>
<li>$ (_i^*)^2 = <em>t^{T_i} ( (s, T_i) - (s, T</em>{i+1}) )^2 ds = ^2(T_i, T_{i+1}), (T_i) $,</li>
<li>$ = 1 + _i K $,</li>
<li>$ C() $ désigne le prix d’un Call selon le cadre Black, en fonction du forward, du strike, de la maturité, de la volatilité et du facteur d’actualisation.</li>
</ul>
<p>En effet, on peut réécrire le payoff d’un caplet sous la forme d’un Call sur <img src="https://latex.codecogs.com/png.latex?Z_t"> qui suit un modèle de Black :</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0A%5Ctext%7BPayoff%7D%20&amp;=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr)%20%5C%5C%0A&amp;=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20%5Cfrac%7BZ_t%7D%7B%5Cdelta_i%7D%20-%201%20-%20K;%200%20%5CBigr)%20%5C%5C%0A&amp;=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20%5Cfrac%7BZ_t%20-%201%20-%20%5Cdelta_i%20K%7D%7B%5Cdelta_i%7D;%200%20%5CBigr)%20%5C%5C%0A&amp;=%20%5Cmax%5CBigl(Z_t%20-%201%20-%20%5Cdelta_i%20K;%200%20%5CBigr)%20%5C%5C%0A%5Ctext%7BPayoff%7D%20&amp;=%20%5Cmax%5CBigl(%20Z_t%20-%20%5Ctilde%7BK%7D,%5C;%200%20%5CBigr)%20%5C%5C%0A%5Cend%7Baligned%7D">
</section>
<section id="iii.6.-calibration-du-modèle" class="level3">
<h3 class="anchored" data-anchor-id="iii.6.-calibration-du-modèle">III.6. Calibration du modèle</h3>
<p>Le modèle de Hull White permet d’avoir une formule fermée pour le prix des caplets. De fait, puisqu’on a calculé les prix de marché de caplets sur euribor12M, ce qui implique une période de capitalisation annuelle, de maturité T = 5Y, nous pouvons desormais calibrer le paramètre de volatilité <img src="https://latex.codecogs.com/png.latex?%5Csigma_i%5E*"> avec la méthode de dichotomie et aussi extraire de manière analytique la volatilité instantanée <img src="https://latex.codecogs.com/png.latex?%5Csigma(t)"> du modèle Hull&amp;White, qu’on supposera constante, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Csigma(t)%20=%20%5Csigma">.</p>
<p>On pose également, pour la calibration, <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%205%5C%25">.</p>
<p>Pour extraire la volatilité spot, nous utiliserons uniquement le prix de marche ATM.</p>
<div id="0ce72a16" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">caplet_price_MKT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vol_data.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Caplet Price MKT'</span>]</span>
<span id="cb25-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Le prix de marché caplet sur euribor 12M de maturité T=5Y est de </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>caplet_price_MKT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Le prix de marché caplet sur euribor 12M de maturité T=5Y est de 0.7137%</code></pre>
</div>
</div>
<div id="e891e3ff" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb27-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb27-3"></span>
<span id="cb27-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_i, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'HW'</span>):</span>
<span id="cb27-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HW"</span> :</span>
<span id="cb27-6">        Z_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb27-7">        K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> K</span>
<span id="cb27-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> price_oplet(N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N, delta_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta_i, B_t_Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>B_t_Ti, L_i_t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Z_t, K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K, sigma_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_i, Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Ti, t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type)</span>
<span id="cb27-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb27-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> price_oplet(N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>N, delta_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta_i, B_t_Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>B_t_Ti, L_i_t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>L_i_t, K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K, sigma_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma_i, Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Ti, t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type)</span></code></pre></div>
</div>
<div id="619bcf42" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> Dichotomie(N, delta_i, B_t_Ti, L_i_t, K, Ti, t,caplet_price_MKT,option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HW"</span>,tol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, sigma_low<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, sigma_high<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb28-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb28-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Extrait la volatilité implicite sigma en utilisant la méthode de dichotomie.</span></span>
<span id="cb28-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb28-5">    fmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_low, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>model)</span>
<span id="cb28-6">    fmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_high,option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>model)</span>
<span id="cb28-7">    price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> caplet_price_MKT</span>
<span id="cb28-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> fmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>price :</span>
<span id="cb28-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sigma_low</span>
<span id="cb28-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> fmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>price :</span>
<span id="cb28-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sigma_high</span>
<span id="cb28-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb28-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> sigma_high<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sigma_low<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>tol:</span>
<span id="cb28-14">            sigma_mid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (sigma_low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma_high) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb28-15">            fmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_low, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>model)</span>
<span id="cb28-16">            fmid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_mid,option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>option_type, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>model)</span>
<span id="cb28-17">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ((fmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> price) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (fmid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> price) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) : <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># jette la moitié de gauche</span></span>
<span id="cb28-18">                sigma_low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_mid</span>
<span id="cb28-19">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>: <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># jette la moitié de droite</span></span>
<span id="cb28-20">                sigma_high <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_mid</span>
<span id="cb28-21">        sigma_mid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (sigma_low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma_high) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb28-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sigma_mid</span>
<span id="cb28-23">    </span>
<span id="cb28-24"></span>
<span id="cb28-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Notional</span></span>
<span id="cb28-26">N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  </span>
<span id="cb28-27"></span>
<span id="cb28-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Période du caplet</span></span>
<span id="cb28-29">delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb28-30"></span>
<span id="cb28-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maturité du caplet</span></span>
<span id="cb28-32">Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb28-33"></span>
<span id="cb28-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facteur d'actualisation B(t, Ti)</span></span>
<span id="cb28-35">B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb28-36"></span>
<span id="cb28-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Taux forward Li(t)</span></span>
<span id="cb28-38">L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>delta_i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb28-39"></span>
<span id="cb28-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike du caplet</span></span>
<span id="cb28-41">K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> vol_data.loc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike en bps et en rel. / fwd'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb28-42"></span>
<span id="cb28-43">lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb28-44"></span>
<span id="cb28-45">sigma_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Dichotomie(N, delta_i, B_t_Ti, L_i_t, K, Ti, t,caplet_price_MKT, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>,model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HW"</span>)</span>
<span id="cb28-46"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité implicite (en %) :"</span>, sigma_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité implicite (en %) : 0.9271045541763303</code></pre>
</div>
</div>
<div id="f4846b16" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">beta_Ti_Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>  ((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> delta_i))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lambda_)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb30-2">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta_i</span>
<span id="cb30-3">phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(Ti_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>lambda_)</span>
<span id="cb30-4">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt((sigma_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Ti_1)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (beta_Ti_Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi))</span>
<span id="cb30-5"></span>
<span id="cb30-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité instantanée (en %) :"</span>, sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité instantanée (en %) : 1.071446257025021</code></pre>
</div>
</div>
<ol start="2" type="1">
<li>Cette volatilité spot nous permet de valoriiser les caplets pour des strikes différents de l’ATM à l’aide de la formule de valorisation fermée du modèle Hull et White.</li>
</ol>
<div id="d44885fe" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vol_data.index:</span>
<span id="cb32-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Notional</span></span>
<span id="cb32-3">    N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  </span>
<span id="cb32-4"></span>
<span id="cb32-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Période du caplet</span></span>
<span id="cb32-6">    delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb32-7"></span>
<span id="cb32-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maturité du caplet</span></span>
<span id="cb32-9">    Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb32-10"></span>
<span id="cb32-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facteur d'actualisation B(t, Ti)</span></span>
<span id="cb32-12">    B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb32-13"></span>
<span id="cb32-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Taux forward Li(t)</span></span>
<span id="cb32-15">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>delta_i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb32-16"></span>
<span id="cb32-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike du caplet</span></span>
<span id="cb32-18">    K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike en bps et en rel. / fwd'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb32-19"></span>
<span id="cb32-20">    sigma_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_i</span>
<span id="cb32-21"></span>
<span id="cb32-22">    lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb32-23"></span>
<span id="cb32-24">    caplet_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_oplet_HW(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, sigma_i, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'HW'</span>)</span>
<span id="cb32-25">    vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Caplet Price HW'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> caplet_price</span>
<span id="cb32-26"></span>
<span id="cb32-27">vol_data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="23">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Strike en bps et en rel. / fwd</th>
<th data-quarto-table-cell-role="th">Vols Caplets</th>
<th data-quarto-table-cell-role="th">Vols Swaptions</th>
<th data-quarto-table-cell-role="th">Caplet Price MKT</th>
<th data-quarto-table-cell-role="th">Caplet Price HW</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>-100</td>
<td>0.311859</td>
<td>0.311859</td>
<td>0.012511</td>
<td>0.012015</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>-50</td>
<td>0.283274</td>
<td>0.283274</td>
<td>0.009788</td>
<td>0.009389</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>-25</td>
<td>0.265921</td>
<td>0.265921</td>
<td>0.008419</td>
<td>0.008215</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>0</td>
<td>0.250000</td>
<td>0.250000</td>
<td>0.007137</td>
<td>0.007137</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>25</td>
<td>0.243451</td>
<td>0.243451</td>
<td>0.006185</td>
<td>0.006156</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>50</td>
<td>0.249019</td>
<td>0.249019</td>
<td>0.005661</td>
<td>0.005269</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>100</td>
<td>0.271828</td>
<td>0.271828</td>
<td>0.005167</td>
<td>0.003771</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>L’une des faiblesses du modèle de Hull et White est le fait qu’il n’arrive pas à capter le smile de volatilité. En effet, la volatilité implicite extraite est un skew. Pour constater ce phénomène, nous inverserons la formule de Black pour les caplets et nous en déduirons la volatilité implicite pour chaque strike. Nous utiliserons toujours la méthode de dichotomie pour trouver la volatilité implicite.</p>
<div id="db972575" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vol_data.index:</span>
<span id="cb33-2">    N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb33-3">    delta_i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb33-4">    Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb33-5">    B_t_Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb33-6">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> delta_i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb33-7">    K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike en bps et en rel. / fwd'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb33-8">    lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb33-9">    caplet_price_MKT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Caplet Price HW'</span>]</span>
<span id="cb33-10"></span>
<span id="cb33-11">    sigma_extracted <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>  Dichotomie(N, delta_i, B_t_Ti, L_i_t, K, Ti, t, caplet_price_MKT, option_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'caplet'</span>, model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Black'</span>)</span>
<span id="cb33-12">    vol_data.loc[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sigma_HW'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigma_extracted</span>
<span id="cb33-13"></span>
<span id="cb33-14">vol_data</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="24">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Strike en bps et en rel. / fwd</th>
<th data-quarto-table-cell-role="th">Vols Caplets</th>
<th data-quarto-table-cell-role="th">Vols Swaptions</th>
<th data-quarto-table-cell-role="th">Caplet Price MKT</th>
<th data-quarto-table-cell-role="th">Caplet Price HW</th>
<th data-quarto-table-cell-role="th">Sigma_HW</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>-100</td>
<td>0.311859</td>
<td>0.311859</td>
<td>0.012511</td>
<td>0.012015</td>
<td>0.288669</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>-50</td>
<td>0.283274</td>
<td>0.283274</td>
<td>0.009788</td>
<td>0.009389</td>
<td>0.267386</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>-25</td>
<td>0.265921</td>
<td>0.265921</td>
<td>0.008419</td>
<td>0.008215</td>
<td>0.258287</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>0</td>
<td>0.250000</td>
<td>0.250000</td>
<td>0.007137</td>
<td>0.007137</td>
<td>0.250009</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>25</td>
<td>0.243451</td>
<td>0.243451</td>
<td>0.006185</td>
<td>0.006156</td>
<td>0.242433</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>50</td>
<td>0.249019</td>
<td>0.249019</td>
<td>0.005661</td>
<td>0.005269</td>
<td>0.235466</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>100</td>
<td>0.271828</td>
<td>0.271828</td>
<td>0.005167</td>
<td>0.003771</td>
<td>0.223060</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="8c197db0" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb34-2">plt.plot(vol_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Strike en bps et en rel. / fwd"</span>], vol_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sigma_HW"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Volatilité extraite'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb34-3">plt.plot(vol_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Strike en bps et en rel. / fwd"</span>], vol_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vols Caplets"</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Volatilité de marché'</span>, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb34-4">plt.legend()</span>
<span id="cb34-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Comparaison de la volatilité extraite par le modèle HW et la volatilité de marché'</span>)</span>
<span id="cb34-6">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Strike en bps et en rel. / fwd '</span>)</span>
<span id="cb34-7">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Volatilité'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="25">
<pre><code>Text(0, 0.5, 'Volatilité')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-26-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="0ba03b2d" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1">ATM  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L_i_t</span></code></pre></div>
</div>
</section>
<section id="valorisation-dun-produit-structuré" class="level3">
<h3 class="anchored" data-anchor-id="valorisation-dun-produit-structuré">3.7 Valorisation d’un produit structuré</h3>
<blockquote class="blockquote">
<p>Remarques préliminaires :</p>
<ul>
<li>Nous garderons dans un premier temps la calibration ATM effectuée avec <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%205%5C%25">.</li>
<li>Pour la partie Monte-Carlo, nous admettrons que l’EDS pour le processus <img src="https://latex.codecogs.com/png.latex?X_t"> sous la probabilité forward neutre <img src="https://latex.codecogs.com/png.latex?Q%5ET"> associée au numéraire <img src="https://latex.codecogs.com/png.latex?B(t,T)"> s’écrit comme :</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0AdX_t%20=%20%5Cleft%5B%5Cphi(t)%20+%20%5Csigma(t)%5CGamma(t,T)%20-%20%5Clambda%20X_t%5Cright%5D%20dt%20+%20%5Csigma(t)%20dW_t%5ET%20%5Ctag%7B12%7D%0A"></p>
Où <img src="https://latex.codecogs.com/png.latex?W_t%5ET"> est un brownien sous <img src="https://latex.codecogs.com/png.latex?Q%5ET">.
</blockquote>
<p>Nous souhaitons valoriser un caplet de strike <img src="https://latex.codecogs.com/png.latex?K">, de dates de fixing <img src="https://latex.codecogs.com/png.latex?T_i%20=%205Y"> et de paiement <img src="https://latex.codecogs.com/png.latex?T_%7Bi+1%7D%20=%206Y"> et de barrière désactivante <img src="https://latex.codecogs.com/png.latex?B"> (avec <img src="https://latex.codecogs.com/png.latex?B%20%3E%20K">).</p>
<ol type="1">
<li>Écrire le payoff de l’option et tracer la fonction de payoff en fonction de <img src="https://latex.codecogs.com/png.latex?L_i(T_i)">. Cette option est-elle plus ou moins chère qu’un simple caplet de strike <img src="https://latex.codecogs.com/png.latex?K"> ?</li>
</ol>
<p>Le payoff de l’option est donné par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D%20=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr)%20%5Cmathbb%7B1%7D_%7BL_i(T_i)%20%3C%20B%7D%0A"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/attachment:image-2.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>image-2.png</figcaption>
</figure>
</div>
<p>Il est possible de décomposer le payoff à partir d’options vanilles et digitales :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D%20=%20C_K%20-%20CB%20-%20(B-K)%20%5Ctimes%20D_B%0A"></p>
<blockquote class="blockquote">
<p>Une option digitale est une option qui paie 1 si le sous-jacent est au-dessus d’un certain seuil et 0 sinon. De ce fait, le payoff de l’option est donné par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AD_B%20=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr)%20%5Cmathbb%7B1%7D_%7BL_i(T_i)%20%3E%20K%7D%0A"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/attachment:image.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>image.png</figcaption>
</figure>
</div>
<p>Une option vanille est un contrat financier standardisé qui donne le droit, mais non l’obligation, d’acheter (call) ou de vendre (put) un actif sous-jacent à un prix fixé (strike) à une date donnée (maturité). De ce fait, dans le cas d’un call, le payoff de l’option est donné par :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC_K%20=%20%5Cdelta_i%5C,%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr)%0A"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/attachment:Capture%20d’écran%202025-03-05%20à%2000.17.09.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>Capture d’écran 2025-03-05 à 00.17.09.png</figcaption>
</figure>
</div>
</blockquote>
<ol start="3" type="1">
<li>Rappeler les principes du pricing par méthode de Monte-Carlo.</li>
</ol>
<p>La méthode de Monte-Carlo est une méthode numérique qui permet de pricer des produits financiers complexes lorsque les formules fermées ne sont pas disponibles. Elle consiste à simuler un grand nombre <img src="https://latex.codecogs.com/png.latex?N"> de trajectoires du processus stochastique et à calculer la moyenne empiriques des payoffs actualisés pour obtenir le prix de l’option.</p>
<ol start="4" type="1">
<li>Rappeler comment on simule une loi gaussienne à partir d’une loi uniforme.</li>
</ol>
<p>Pour simuler une variable aléatoire suivant une loi gaussienne standard <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,1)"> à partir d’une variable uniforme <img src="https://latex.codecogs.com/png.latex?U"> sur <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">, on applique l’<strong>inverse de la fonction de répartition de la loi gaussienne standard</strong> (aussi appelée la fonction quantile de la loi normale) :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX%20=%20F%5E%7B-1%7D(U)%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?F"> est la fonction de répartition de la loi normale standard.</p>
<ol start="5" type="1">
<li>On considère un caplet sur euribor12M à barrière désactivante de strike $ K = ATM - 100 bps$, de barrière <img src="https://latex.codecogs.com/png.latex?B%20=%20ATM%20+%20100%20bps"> et de maturité <img src="https://latex.codecogs.com/png.latex?T_i%20=%205Y">. Pour valoriser cette option, nous allons utiliser une méthode numérique de type Monte-Carlo. Pour cela, il est necessaire de connaire la loi de X_t sachant X_s. En nous aidant de la question précédente, on peut déduire que la loi de <img src="https://latex.codecogs.com/png.latex?X_t%7CX_s"> est une loi normale de paramètres :</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0AX_t%7CX_s%20%5Csim%20%5Cmathcal%7BN%7D%5Cleft(%20X_s%20e%5E%7B-%5Clambda%20(t-s)%7D%20+%20%5Cint_s%5Et%20e%5E%7B-%5Clambda%20(t-u)%7D%20%5Cleft(%20%5Cphi(u)%20-%20%5Csigma%20%5CGamma(u,T)%20%5Cright)%20du,%20%5Cquad%20%5Cint_s%5Et%20e%5E%7B-2%20%5Clambda%20(t-u)%7D%20%5Csigma%5E2%20d%20%5Cright)%0A"></p>
<p>Pour valoriser cette option, nous pouvons directement utiliser la loi de <img src="https://latex.codecogs.com/png.latex?X_5%7CX_0"> pour simuler les trajectoires du taux court et calculer le payoff de l’option ou diffuser progressivement le taux court en utilisant la loi de <img src="https://latex.codecogs.com/png.latex?X_t%7CX_s"> pour chaque pas de temps. Ensuite, il s’agira de calculer le payoff de l’option à chaque date, en faire la moyenne et l’actualiser pour obtenir le prix de l’option.</p>
<section id="méthode-1-simulation-de-la-loi-de-x_5x_0" class="level5">
<h5 class="anchored" data-anchor-id="méthode-1-simulation-de-la-loi-de-x_5x_0">Méthode 1 : Simulation de la loi de <img src="https://latex.codecogs.com/png.latex?X_5%7CX_0"></h5>
<div id="63fa14d7" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb37-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.integrate <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> quad</span>
<span id="cb37-3"></span>
<span id="cb37-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction phi(t) - variance cumulée</span></span>
<span id="cb37-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> phi(t, sigma, lambda_):</span>
<span id="cb37-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lambda_)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t))</span>
<span id="cb37-7"></span>
<span id="cb37-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction beta(t, T)</span></span>
<span id="cb37-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> beta(t, T, lambda_):</span>
<span id="cb37-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t))) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> lambda_</span>
<span id="cb37-11"></span>
<span id="cb37-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction gamma(t, T)</span></span>
<span id="cb37-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> gamma(t, T, sigma, lambda_):</span>
<span id="cb37-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> lambda_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> t)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb37-15"></span>
<span id="cb37-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction B(t, T)</span></span>
<span id="cb37-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> B_t_T(t, T, B0_T, B0_t, X_t, sigma, lambda_):</span>
<span id="cb37-18">    beta_t_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> beta(t, T, lambda_)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb37-19">    phi_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(t, sigma, lambda_)</span>
<span id="cb37-20">    exponent <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> beta_t_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta_t_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X_t</span>
<span id="cb37-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (B0_T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B0_t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(exponent)</span>
<span id="cb37-22"></span>
<span id="cb37-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction d'intégration avec paramètres supplémentaires</span></span>
<span id="cb37-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> integrand_mean(u, t, Xs, s, sigma, lambda_, T):</span>
<span id="cb37-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># t = borne sup</span></span>
<span id="cb37-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># s = borne inf</span></span>
<span id="cb37-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># T = maturité</span></span>
<span id="cb37-28">    Xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s))</span>
<span id="cb37-29">    exp_part <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u))</span>
<span id="cb37-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Xt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> exp_part <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (phi(u, sigma, lambda_) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gamma(u, T, sigma, lambda_))</span>
<span id="cb37-31"></span>
<span id="cb37-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres</span></span>
<span id="cb37-33">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Ti_1  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb37-34">Xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>  </span>
<span id="cb37-35">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb37-36">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Ti_1 </span>
<span id="cb37-37"></span>
<span id="cb37-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la moyenne conditionnelle</span></span>
<span id="cb37-39">mean_5_given_0, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quad(integrand_mean, s, t, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(t, Xs, s, sigma, lambda_, T))</span>
<span id="cb37-40"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Moyenne conditionnelle de X_5 | X_0 : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mean_5_given_0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb37-41"></span>
<span id="cb37-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la variance conditionnelle (indépendant de gamma ici)</span></span>
<span id="cb37-43"></span>
<span id="cb37-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> compute_variance(sigma, lambda_, t, s):</span>
<span id="cb37-45">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.exp(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lambda_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s))) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lambda_)</span>
<span id="cb37-46">var_5_given_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> compute_variance(sigma, lambda_, t, s)</span>
<span id="cb37-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># var_5_given_0 = (sigma**2) * (1 - np.exp(-2 * lambda_ * (t - s))) / (2 * lambda_)</span></span>
<span id="cb37-48"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Variance conditionnelle de X_5 | X_0 : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_5_given_0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Moyenne conditionnelle de X_5 | X_0 : 0.000000
Variance conditionnelle de X_5 | X_0 : 0.000452</code></pre>
</div>
</div>
<div id="25430e16" class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb39-2">Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb39-3"></span>
<span id="cb39-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation Monte Carlo</span></span>
<span id="cb39-5">n_simulations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb39-6"></span>
<span id="cb39-7">payoffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n_simulations)</span>
<span id="cb39-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_simulations):</span>
<span id="cb39-9">    phi_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(Ti_1, sigma, lambda_)</span>
<span id="cb39-10">    gamma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gamma(Ti_1,Ti, sigma, lambda_)</span>
<span id="cb39-11"></span>
<span id="cb39-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Moyenne</span></span>
<span id="cb39-13">    mu_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_5_given_0</span>
<span id="cb39-14"></span>
<span id="cb39-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ecart-type</span></span>
<span id="cb39-16">    sigma_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(var_5_given_0)</span>
<span id="cb39-17"></span>
<span id="cb39-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X_5|X_0</span></span>
<span id="cb39-19">    X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal(mu_X, sigma_X)  </span>
<span id="cb39-20"></span>
<span id="cb39-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb39-22">    B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb39-23">    B0_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti_1, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb39-24"></span>
<span id="cb39-25">    B_5_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, B0_6, B0_5, X, sigma, lambda_)</span>
<span id="cb39-26">    B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, B0_5, B0_5, X, sigma, lambda_)</span>
<span id="cb39-27"></span>
<span id="cb39-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du taux forward L_i_t</span></span>
<span id="cb39-29">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_5_6) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb39-30">    bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb39-31"></span>
<span id="cb39-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition du strike</span></span>
<span id="cb39-33">    strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> bp</span>
<span id="cb39-34"></span>
<span id="cb39-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrière</span></span>
<span id="cb39-36">    B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bp</span>
<span id="cb39-37"></span>
<span id="cb39-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Payoff de l'option</span></span>
<span id="cb39-39">    payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> strike, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> B)</span>
<span id="cb39-40">    payoffs[sim] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb39-41"></span>
<span id="cb39-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix de l'option call</span></span>
<span id="cb39-43">call_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.mean(payoffs)</span>
<span id="cb39-44"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix du call : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix du call : 0.003007</code></pre>
</div>
</div>
</section>
<section id="méthode-2-methode-de-diffusion" class="level5">
<h5 class="anchored" data-anchor-id="méthode-2-methode-de-diffusion">Méthode 2 : Methode de diffusion</h5>
<div id="5daac680" class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation Monte Carlo</span></span>
<span id="cb41-2">n_simulations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb41-3"></span>
<span id="cb41-4">payoffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n_simulations)</span>
<span id="cb41-5">X0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb41-6">Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb41-7">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb41-8">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(Ti)</span>
<span id="cb41-9">X[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X0</span>
<span id="cb41-10"></span>
<span id="cb41-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_simulations):</span>
<span id="cb41-12">    phi_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(Ti_1, sigma, lambda_)</span>
<span id="cb41-13">    gamma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gamma(Ti_1,Ti, sigma, lambda_)</span>
<span id="cb41-14"></span>
<span id="cb41-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,Ti):</span>
<span id="cb41-16">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i</span>
<span id="cb41-17">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb41-18">        T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> </span>
<span id="cb41-19">        Xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X[i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb41-20"></span>
<span id="cb41-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la moyenne conditionnelle</span></span>
<span id="cb41-22">        mu_X, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quad(integrand_mean, s, t, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(t, Xs, s, sigma, lambda_, T))</span>
<span id="cb41-23">        sigma_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(compute_variance(sigma, lambda_, t, s))</span>
<span id="cb41-24">        X[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal(mu_X, sigma_X)</span>
<span id="cb41-25"></span>
<span id="cb41-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb41-27">    B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb41-28">    B0_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti_1, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb41-29"></span>
<span id="cb41-30">    B_5_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, B0_6, B0_5, X[Ti_1], sigma, lambda_)</span>
<span id="cb41-31">    B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, B0_5, B0_5, X[Ti_1], sigma, lambda_)</span>
<span id="cb41-32"></span>
<span id="cb41-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du taux forward L_i_t</span></span>
<span id="cb41-34">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_5_6) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb41-35">    bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb41-36"></span>
<span id="cb41-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition du strike</span></span>
<span id="cb41-38">    strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> bp</span>
<span id="cb41-39"></span>
<span id="cb41-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrière</span></span>
<span id="cb41-41">    B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bp</span>
<span id="cb41-42"></span>
<span id="cb41-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Payoff de l'option</span></span>
<span id="cb41-44">    payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> strike, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> B)</span>
<span id="cb41-45">    payoffs[sim] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb41-46"></span>
<span id="cb41-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix de l'option call</span></span>
<span id="cb41-48">call_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.mean(payoffs)</span>
<span id="cb41-49"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix du call : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix du call : 0.002987</code></pre>
</div>
</div>
<p>Nous constatons qu’avec les deux méthodes, nous obtenons des prix d’options similaires (différent de 0.2bps). Cela confirme que les deux méthodes convergent vers le même résultat.</p>
<p>En dégénérant le produit en faisant tendre la barrière à <img src="https://latex.codecogs.com/png.latex?+%5Cinfty">, nous constatons que le prix de l’option call est égal au prix de marché du forward. En dégénérant le produit en faisant tendre la barrière à 0, nous constatons que le prix de l’option call est égal à 0.</p>
<p>Cela est cohérent car lorsque la barrière est très élevée, le produit est équivalent à un forward et lorsque la barrière est nulle, le produit est équivalent à un call classique. La fonction que nous avons implémenté est donc cohérente et bien implémentée.</p>
<div id="2dd13c2c" class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb43-2">Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb43-3"></span>
<span id="cb43-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation Monte Carlo</span></span>
<span id="cb43-5">n_simulations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb43-6"></span>
<span id="cb43-7">payoffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n_simulations)</span>
<span id="cb43-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_simulations):</span>
<span id="cb43-9">    phi_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(Ti_1, sigma, lambda_)</span>
<span id="cb43-10">    gamma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gamma(Ti_1,Ti, sigma, lambda_)</span>
<span id="cb43-11"></span>
<span id="cb43-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Moyenne</span></span>
<span id="cb43-13">    mu_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_5_given_0</span>
<span id="cb43-14"></span>
<span id="cb43-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ecart-type</span></span>
<span id="cb43-16">    sigma_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(var_5_given_0)</span>
<span id="cb43-17"></span>
<span id="cb43-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X_5|X_0</span></span>
<span id="cb43-19">    X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal(mu_X, sigma_X)  </span>
<span id="cb43-20"></span>
<span id="cb43-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb43-22">    B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb43-23">    B0_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti_1, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb43-24"></span>
<span id="cb43-25">    B_5_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, B0_6, B0_5, X, sigma, lambda_)</span>
<span id="cb43-26">    B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, B0_5, B0_5, X, sigma, lambda_)</span>
<span id="cb43-27"></span>
<span id="cb43-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du taux forward L_i_t</span></span>
<span id="cb43-29">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_5_6) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb43-30">    bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb43-31"></span>
<span id="cb43-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition du strike</span></span>
<span id="cb43-33">    strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - bp</span></span>
<span id="cb43-34"></span>
<span id="cb43-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrière</span></span>
<span id="cb43-36">    B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.inf<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#ATM + bp</span></span>
<span id="cb43-37"></span>
<span id="cb43-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Payoff de l'option</span></span>
<span id="cb43-39">    payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> strike, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> B)</span>
<span id="cb43-40">    payoffs[sim] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb43-41"></span>
<span id="cb43-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix de l'option call</span></span>
<span id="cb43-43">call_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.mean(payoffs)</span>
<span id="cb43-44"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix du call : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix du call : 0.007089</code></pre>
</div>
</div>
<p>Nous rendons la barrière ‘bermudéenne’ en étendant la condition de désactivation aux dates 1Y, 2Y, 3Y, 4Y et 5Y. De ce fait, le payoff de cette option s’écrit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPayoff%7D%20=%20%5Cdelta_i%20%5Cmax%5CBigl(%20L_i(T_i)%20-%20K,%5C;%200%20%5CBigr)%20%5Cmathbb%7B1%7D_%7Bmax_%7Bi=1,%5Cdots,5%7D(L_i(T_i)%20%3C%20B)%7D%0A"></p>
<div id="aa3259a1" class="cell" data-execution_count="31">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Option bermudienne</span></span>
<span id="cb45-2"></span>
<span id="cb45-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation Monte Carlo</span></span>
<span id="cb45-4">n_simulations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb45-5"></span>
<span id="cb45-6">payoffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n_simulations)</span>
<span id="cb45-7">X0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb45-8">Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb45-9">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb45-10">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(Ti)</span>
<span id="cb45-11">X[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X0</span>
<span id="cb45-12"></span>
<span id="cb45-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_simulations):</span>
<span id="cb45-14">    phi_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(Ti_1, sigma, lambda_)</span>
<span id="cb45-15">    gamma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gamma(Ti_1,Ti, sigma, lambda_)</span>
<span id="cb45-16">    L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(Ti_1)</span>
<span id="cb45-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,Ti):</span>
<span id="cb45-18">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i</span>
<span id="cb45-19">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb45-20">        T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> </span>
<span id="cb45-21">        Xs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X[s]</span>
<span id="cb45-22"></span>
<span id="cb45-23">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la moyenne conditionnelle</span></span>
<span id="cb45-24">        mu_X, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quad(integrand_mean, s, t, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(t, Xs, s, sigma, lambda_, T))</span>
<span id="cb45-25">        sigma_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(compute_variance(sigma, lambda_, t, s))</span>
<span id="cb45-26">        X[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal(mu_X, sigma_X)</span>
<span id="cb45-27"></span>
<span id="cb45-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb45-29">        Bi_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb45-30">        Bi_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> s, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb45-31"></span>
<span id="cb45-32">        B_s_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(s, t, Bi_t, Bi_s, X[i], sigma, lambda_)</span>
<span id="cb45-33">        B_s_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(s, s, Bi_s, Bi_s, X[i], sigma, lambda_)</span>
<span id="cb45-34"></span>
<span id="cb45-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du taux forward L_i_t</span></span>
<span id="cb45-36">        L_i_t[i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>s)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((B_s_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_s_t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb45-37">    </span>
<span id="cb45-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb45-39">    B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb45-40">    B0_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti_1, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb45-41"></span>
<span id="cb45-42">    B_5_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, B0_6, B0_5, X[Ti_1], sigma, lambda_)</span>
<span id="cb45-43">    B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, B0_5, B0_5, X[Ti_1], sigma, lambda_)</span>
<span id="cb45-44"></span>
<span id="cb45-45">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du taux forward L_i_t</span></span>
<span id="cb45-46">    L_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_5_6) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb45-47">    bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb45-48"></span>
<span id="cb45-49"></span>
<span id="cb45-50">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition du strike</span></span>
<span id="cb45-51">    strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> bp</span>
<span id="cb45-52"></span>
<span id="cb45-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrière</span></span>
<span id="cb45-54">    B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bp</span>
<span id="cb45-55"></span>
<span id="cb45-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Payoff de l'option</span></span>
<span id="cb45-57">    payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(L_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> strike, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(L_i_t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> B)</span>
<span id="cb45-58">    payoffs[sim] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb45-59"></span>
<span id="cb45-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix de l'option call</span></span>
<span id="cb45-61">call_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.mean(payoffs)</span>
<span id="cb45-62"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Prix du call : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>call_price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.6f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Prix du call : 0.003046</code></pre>
</div>
</div>
<p>L’un des paramètres important du modèle de Hull et White est la mean reversion <img src="https://latex.codecogs.com/png.latex?%5Clambda">, qui caractérise la force de rappel à la moyenne du processus. Ce paramètre a un impact positive sur la valorisation de l’option, comme nous pouvons l’observer dans la figure ci dessous .</p>
<div id="01c6ed8c" class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb47-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Liste des lambda à tester</span></span>
<span id="cb47-2">lambdas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exemple de grille de lambda</span></span>
<span id="cb47-3">results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb47-4"></span>
<span id="cb47-5">Ti_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb47-6">Ti <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb47-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle principale sur les lambdas</span></span>
<span id="cb47-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> lambda_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lambdas:</span>
<span id="cb47-9">    n_touched <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb47-10">    payoffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(n_simulations)</span>
<span id="cb47-11"></span>
<span id="cb47-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_simulations):</span>
<span id="cb47-13">        phi_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi(Ti_1, sigma, lambda_)</span>
<span id="cb47-14">        gamma_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gamma(Ti_1,Ti, sigma, lambda_)</span>
<span id="cb47-15"></span>
<span id="cb47-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Moyenne</span></span>
<span id="cb47-17">        mu_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_5_given_0</span>
<span id="cb47-18"></span>
<span id="cb47-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ecart-type</span></span>
<span id="cb47-20">        sigma_X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(var_5_given_0)</span>
<span id="cb47-21"></span>
<span id="cb47-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X_5|X_0</span></span>
<span id="cb47-23">        X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal(mu_X, sigma_X)  </span>
<span id="cb47-24"></span>
<span id="cb47-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix B(5,6) selon Hull-White</span></span>
<span id="cb47-26">        B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,6)</span></span>
<span id="cb47-27">        B0_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_ZC.loc[df_ZC[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> Ti_1, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span>].values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B(0,5)</span></span>
<span id="cb47-28"></span>
<span id="cb47-29">        B_5_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, B0_6, B0_5, X, sigma, lambda_)</span>
<span id="cb47-30">        B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B_t_T(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, B0_5, B0_5, X, sigma, lambda_)</span>
<span id="cb47-31"></span>
<span id="cb47-32">        L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((B_5_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B_5_6) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb47-33">        bp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb47-34">        strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> bp</span>
<span id="cb47-35">        B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ATM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bp</span>
<span id="cb47-36"></span>
<span id="cb47-37">        payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> strike, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (L_i_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> B)</span>
<span id="cb47-38">        payoffs[sim] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> payoff</span>
<span id="cb47-39"></span>
<span id="cb47-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Vérification de la barrière</span></span>
<span id="cb47-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">any</span>(L_i_t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> B):</span>
<span id="cb47-42">            n_touched <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb47-43"></span>
<span id="cb47-44">    prob_toucher_barriere <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n_touched <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_simulations</span>
<span id="cb47-45"></span>
<span id="cb47-46">    call_price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> B0_6 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.mean(payoffs)</span>
<span id="cb47-47">    results.append((lambda_, call_price,prob_toucher_barriere))</span></code></pre></div>
</div>
<div id="8ebfc2c1" class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb48-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Optionnel : Graphique de la sensibilité</span></span>
<span id="cb48-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb48-3"></span>
<span id="cb48-4">lambdas, prices, probabilities <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>results)</span>
<span id="cb48-5">plt.plot(lambdas, prices, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb48-6">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lambda (Mean Reversion)'</span>)</span>
<span id="cb48-7">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Prix de l</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\'</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">option'</span>)</span>
<span id="cb48-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sensibilité du prix à la mean reversion (λ)'</span>)</span>
<span id="cb48-9">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb48-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-34-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>De plus, plus le paramètre de mean reversion <img src="https://latex.codecogs.com/png.latex?%5Clambda"> est élevé, plus la probabilité de toucher la barrière est faible. Inversement, une faible mean reversion laisse plus de liberté au processus pour explorer des valeurs extrêmes, augmentant ainsi la probabilité de franchir la barrière.</p>
<div id="9ef01162" class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb49-1">plt.plot(lambdas, probabilities, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb49-2">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lambda (Mean Reversion)'</span>)</span>
<span id="cb49-3">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Probabilité de toucher la barrière'</span>)</span>
<span id="cb49-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Probabilité de toucher la barrière en fonction de la mean reversion (λ)'</span>)</span>
<span id="cb49-5">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb49-6">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx_files/figure-html/cell-35-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>
</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>proc-sto</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/pj_courbe_tx.html</guid>
  <pubDate>Mon, 03 Mar 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>TP3:Méthodes d’une calcul d’une VaR dynamique (basé sur GARCH)</title>
  <dc:creator>Cheryl Kouadio</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch.html</link>
  <description><![CDATA[ 




<p>Ce TP est une continuité du TP-1 et du TP-2 dans lequel on souhaitait implémenter la VaR (Value at Risk) et l’ES (Expected Shortfall) en utilisant les méthodes classiques proposées dans la réglementation bâloise, i.e.&nbsp;la méthode historique, paramétrique et bootstrap (TP1). Cependant, une limite de ces méthodes est qu’elles ne prennent pas en compte la queue de distribution de la perte. Pour remédier à cela, nous avons utilisé des méthodes avec la théorie des valeurs extrêmes, i.e.&nbsp;l’approche Block Maxima et l’approche Peaks Over Threshold (TP2). Jusqu’à maintenant, on considérait que la série est iid. Cependant, dans la réalité, les séries financières sont souvent caractérisées par une dépendance temporelle et une volatilité conditionnelle.</p>
<p>Dans le cadre du TP3, il s’agira de prendre en compte la dépendance temporelle et la volatilité conditionnelle dans les séries temporelles financières. Pour ce faire, nous utiliserons un modèle de VAR dynamique avec le modèle GARCH.</p>
<p>Le modèle GARCH (Generalized Autoregressive Conditional Heteroskedasticity) est un modèle de volatilité conditionnelle qui permet de modéliser la volatilité des rendements financiers. Il a été introduit par Bollerslev en 1986. Le modèle GARCH est une extension du modèle ARCH (Autoregressive Conditional Heteroskedasticity) introduit par Engle en 1982. Le modèle GARCH est défini par les équations suivantes:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_t%20=%20%5Cmu_t%20+%20%5Cepsilon_t%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cepsilon_t%20=%20%5Csigma_t%20z_t%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_t%5E2%20=%20%5Comega%20+%20%5Csum%20%5Calpha_i%20%5Cepsilon_%7Bt-i%7D%5E2%20+%20%5Csum%20%5Cbeta_i%20%5Csigma_%7Bt-i%7D%5E2%0A"></p>
<p>Dans ce modèle <img src="https://latex.codecogs.com/png.latex?%5Cmu_t"> est un paramètre de tendance moyenne à identifier, <img src="https://latex.codecogs.com/png.latex?%5Cepsilon_t"> est le résidu, <img src="https://latex.codecogs.com/png.latex?%5Csigma_t%5E2"> est la variance conditionnelle, <img src="https://latex.codecogs.com/png.latex?z_t"> est un bruit blanc, <img src="https://latex.codecogs.com/png.latex?%5Comega"> est un paramètre de constante, <img src="https://latex.codecogs.com/png.latex?%5Calpha_i"> et <img src="https://latex.codecogs.com/png.latex?%5Cbeta_i"> sont les paramètres du modèle GARCH à identifier.</p>
<div id="1c2c7d17" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des librairies</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span></code></pre></div>
</div>
<div id="3f60d09d" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import des données du CAC 40</span></span>
<span id="cb2-2">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>,start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2008-01-01"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des rendements logarithmiques</span></span>
<span id="cb2-5">data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.log(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>].shift(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retirer la première ligne</span></span>
<span id="cb2-8">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dropna()</span>
<span id="cb2-9">data.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.columns.get_level_values(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="e067cd30" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'15-10-2008'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'26-07-2022'</span>]</span>
<span id="cb4-2">data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span>
<span id="cb4-3"></span>
<span id="cb4-4">test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'27-07-2022'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11-06-2024'</span>]</span>
<span id="cb4-5">data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span></code></pre></div>
</div>
<section id="i.-implémentation-de-la-var-dynamique" class="level2">
<h2 class="anchored" data-anchor-id="i.-implémentation-de-la-var-dynamique">I. Implémentation de la VaR dynamique</h2>
<section id="i.1.-pertinence-du-modèle-ar1-garch11" class="level3">
<h3 class="anchored" data-anchor-id="i.1.-pertinence-du-modèle-ar1-garch11">I.1. Pertinence du modèle AR(1)-GARCH(1,1)</h3>
<p>Le modèle AR(1)-GARCH(1,1) est le modèle qui, en pratique, est utilisé pour réaliser la VaR dynamique. Cependant, il n’est pas tout le temps adapté aux données financières. Dans ce TP, nous allons commencer par tester l’éligibilité de ce modèle dans le cadre des données que nous possédons.</p>
<div id="c32bd353" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb5-2">plt.plot(data_train, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Train'</span>)</span>
<span id="cb5-3">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CAC 40 Log Returns'</span>)</span>
<span id="cb5-4">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="afffaccb" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## ACF et PACF</span></span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.graphics.tsaplots <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> plot_acf, plot_pacf</span>
<span id="cb6-3"></span>
<span id="cb6-4">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb6-5">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">221</span>)</span>
<span id="cb6-6">plot_acf(data_train, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca(), lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span>
<span id="cb6-7">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">222</span>)</span>
<span id="cb6-8">plot_pacf(data_train, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca(), lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span>
<span id="cb6-9">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-6-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Dans la série temporelle que nous possédons, nous constatons que la série peut être modéliser par un AR(1). Pour un test plus rigoureux de cette hypothèse, nous allons utiliser la méthode de Lljung Box afin de déterminer le meilleur modèle qui puisse modéliser la série. Ainsi, pour un ordre pmax = 2 et qmax=2, nous allons : 1. Estimer les paramètres du modèle ARMA(p,q) pour chaque combinaison de p et q 2. Calculer la statistique de Ljung Box pour chaque combinaison de p et q afin d’examiner si les résidus d’un modèle sont du bruit blanc 3. Filtrer les modèles pour lesquels les résidus sont du bruit blanc 4. Choisir le meilleur modèle en utilisant le critère d’Akaike</p>
<div id="ebdd8eac" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb7-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statsmodels.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sm</span>
<span id="cb7-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.tsa.arima.model <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ARIMA</span>
<span id="cb7-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> boxcox</span>
<span id="cb7-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.stats.diagnostic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> acorr_ljungbox</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres du modèle</span></span>
<span id="cb7-9">p_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb7-10">q_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb7-11">best_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.inf</span>
<span id="cb7-12">best_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb7-13"></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chargement de la série temporelle (remplacer par la vraie série data_unindex)</span></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exemple fictif avec des données aléatoires</span></span>
<span id="cb7-16">np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb7-17">data_unindex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data_train.copy()</span>
<span id="cb7-18">data_unindex.reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, inplace<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb7-19"></span>
<span id="cb7-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création de la matrice pour stocker les AIC</span></span>
<span id="cb7-21">aic_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(np.nan, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(p_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)], </span>
<span id="cb7-22">                          columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> q <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(q_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)])</span>
<span id="cb7-23"></span>
<span id="cb7-24">bb_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(p_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)], </span>
<span id="cb7-25">                          columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> q <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(q_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)])</span>
<span id="cb7-26"></span>
<span id="cb7-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boucle pour estimer les modèles et stocker les AIC</span></span>
<span id="cb7-28"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(p_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb7-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> q <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(q_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb7-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-31">            model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ARIMA(data_unindex, order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q))</span>
<span id="cb7-32">            out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.fit()</span>
<span id="cb7-33">            aic_matrix.loc[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> out.aic  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stockage de l'AIC</span></span>
<span id="cb7-34">            </span>
<span id="cb7-35">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test de la blancheur des résidus</span></span>
<span id="cb7-36">            ljung_box_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> acorr_ljungbox(out.resid, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], return_df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb7-37">            p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ljung_box_result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lb_pvalue'</span>].iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb7-38"></span>
<span id="cb7-39">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>:</span>
<span id="cb7-40">                bb_test.loc[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb7-41">            </span>
<span id="cb7-42">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mise à jour du meilleur modèle</span></span>
<span id="cb7-43">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> out.aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> best_aic :</span>
<span id="cb7-44">                best_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> out.aic</span>
<span id="cb7-45">                best_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q)</span>
<span id="cb7-46">                </span>
<span id="cb7-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb7-48">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Erreur avec (p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">): </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-49"></span>
<span id="cb7-50"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Meilleur modèle ARIMA: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>best_order<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> avec AIC=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>best_aic<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-51"></span>
<span id="cb7-52"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb7-53"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Matrice des AIC:"</span>)</span>
<span id="cb7-54"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(aic_matrix)</span>
<span id="cb7-55"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb7-56"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Matrice des test de Lljung box (1 lorsque résidus non autocorrélés):"</span>)</span>
<span id="cb7-57"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(bb_test)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Meilleur modèle ARIMA: (0, 0, 0) avec AIC=-20100.176479566246
==============================
Matrice des AIC:
              q=0           q=1           q=2
p=0 -20100.176480 -20098.205891 -20097.679059
p=1 -20098.227385 -20099.862840 -20097.046957
p=2 -20097.887027 -20098.545030 -20094.033191
==============================
Matrice des test de Lljung box (1 lorsque résidus non autocorrélés):
     q=0  q=1  q=2
p=0    1    1    1
p=1    1    1    1
p=2    1    1    1</code></pre>
</div>
</div>
<div id="23af8a54" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb9-2">q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-3"></span>
<span id="cb9-4">AR1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ARIMA(data_unindex, order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q))</span>
<span id="cb9-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(AR1.fit().summary())</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>                               SARIMAX Results                                
==============================================================================
Dep. Variable:             log_return   No. Observations:                 3523
Model:                 ARIMA(1, 0, 0)   Log Likelihood               10052.114
Date:                Wed, 11 Feb 2026   AIC                         -20098.227
Time:                        03:02:56   BIC                         -20079.726
Sample:                             0   HQIC                        -20091.627
                               - 3523                                         
Covariance Type:                  opg                                         
==============================================================================
                 coef    std err          z      P&gt;|z|      [0.025      0.975]
------------------------------------------------------------------------------
const          0.0001      0.000      0.621      0.535      -0.000       0.001
ar.L1         -0.0037      0.012     -0.321      0.748      -0.027       0.019
sigma2         0.0002   2.16e-06     89.947      0.000       0.000       0.000
===================================================================================
Ljung-Box (L1) (Q):                   0.00   Jarque-Bera (JB):              7957.49
Prob(Q):                              1.00   Prob(JB):                         0.00
Heteroskedasticity (H):               0.61   Skew:                            -0.30
Prob(H) (two-sided):                  0.00   Kurtosis:                        10.34
===================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).</code></pre>
</div>
</div>
<p>En utilisant la méthode énoncée plus haut, nous constatons que le modèle ARMA(0,0) est le meilleur modèle. En effet, c’est le modèle avec le critère d’Akaike le plus faible. Cela porte à croire que la tendance moyenne de la série est constante. Nous allons tout de même utiliser un modèle AR(1) pour la modéliser. En effet, c’est le deuxième modèle avec un AIC faible.</p>
<p>Dans la série des résidus, nous constatons des clusters de volatilité ce qui est signe d’une volatilité conditionnelle, et donc de la présence d’un GARCH. De plus, dans la série des résidus du log-rendement, nous constatons une faible autocorrélation, ce qui les fait ressembler à du bruit blanc. Toutefois, lorsque l’on examine ces résidus au carré, la série temporelle présente généralement une forte autocorrélation, mise en évidence par la présence de grappes de volatilité. Cela suggère que les rendements représentent un processus hétéroscédastique, ce qui rend le modèle GARCH particulièrement pertinent dans le cadre de notre étude.</p>
<div id="845e1e1c" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">AR1_resid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AR1.fit().resid</span>
<span id="cb11-2">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb11-3">plt.plot(AR1_resid)</span>
<span id="cb11-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résidus du modèle AR(1)"</span>)</span>
<span id="cb11-5">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="5a439764" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb12-2">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">221</span>)</span>
<span id="cb12-3">plot_acf(AR1_resid, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca())</span>
<span id="cb12-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACF des résidus AR(1)"</span>)</span>
<span id="cb12-5">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">222</span>)</span>
<span id="cb12-6">plot_acf(AR1_resid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca())</span>
<span id="cb12-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACF des résidus AR(1) au carré"</span>)</span>
<span id="cb12-8">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-10-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Motivés par les commentaires de (Franke, Härdle et Hafner 2004) suggérant que, dans les applications pratiques, les modèles GARCH avec des ordres plus petits décrivent souvent suffisamment les données et que dans la plupart des cas GARCH(1,1) est adéquat, nous avons considéré quatre combinaisons différentes de p=0, 1 et q=1, 2 pour chaque période afin d’entraîner le modèle GARCH, en supposant que les résidus standardisés suivent une distribution normale.</p>
<div id="ebb581f4" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb13-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb13-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> arch <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arch_model</span>
<span id="cb13-4"></span>
<span id="cb13-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> find_garch(p_min, p_max, q_min, q_max, data, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span>):</span>
<span id="cb13-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Trouve le meilleur modèle GARCH(p, q) en minimisant l'AIC.</span></span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Paramètres :</span></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - p_min, p_max : Bornes pour p (ordre de l'AR dans la variance)</span></span>
<span id="cb13-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - q_min, q_max : Bornes pour q (ordre de MA dans la variance)</span></span>
<span id="cb13-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - data : Série temporelle utilisée pour l'estimation</span></span>
<span id="cb13-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - dist : Distribution des erreurs ("normal", "t", "ged", etc.)</span></span>
<span id="cb13-14"></span>
<span id="cb13-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retour :</span></span>
<span id="cb13-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - DataFrame contenant les valeurs de AIC pour chaque combinaison (p, q)</span></span>
<span id="cb13-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - Meilleur modèle GARCH trouvé en fonction du critère AIC</span></span>
<span id="cb13-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb13-19">    </span>
<span id="cb13-20">    best_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.inf</span>
<span id="cb13-21">    best_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb13-22">    </span>
<span id="cb13-23">    results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb13-24"></span>
<span id="cb13-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(p_min, p_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb13-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> q <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(q_min, q_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb13-27">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb13-28">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Spécification du modèle GARCH(p, q)</span></span>
<span id="cb13-29">                garch_spec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arch_model(data, vol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Garch'</span>, p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>p, q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>q, mean<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Zero'</span>, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dist)</span>
<span id="cb13-30">                out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch_spec.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb13-31">                </span>
<span id="cb13-32">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de l'AIC</span></span>
<span id="cb13-33">                current_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> out.aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data)</span>
<span id="cb13-34"></span>
<span id="cb13-35">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mettre à jour le meilleur modèle si un plus petit AIC est trouvé</span></span>
<span id="cb13-36">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> current_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> best_aic:</span>
<span id="cb13-37">                    best_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> current_aic</span>
<span id="cb13-38">                    best_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q)</span>
<span id="cb13-39">                </span>
<span id="cb13-40">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ajouter les résultats dans la liste</span></span>
<span id="cb13-41">                results.append({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>: p, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'q'</span>: q, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'aic'</span>: current_aic, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relative_gap'</span>: np.nan})</span>
<span id="cb13-42">            </span>
<span id="cb13-43">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb13-44">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Erreur pour (p=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, q=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">): </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb13-45">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">continue</span></span>
<span id="cb13-46">    </span>
<span id="cb13-47">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convertir en DataFrame</span></span>
<span id="cb13-48">    results_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(results)</span>
<span id="cb13-49"></span>
<span id="cb13-50">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer l'écart relatif par rapport au meilleur AIC</span></span>
<span id="cb13-51">    results_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relative_gap'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (results_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'aic'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> best_aic) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> best_aic</span>
<span id="cb13-52">    </span>
<span id="cb13-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> results_df, best_order</span>
<span id="cb13-54"></span>
<span id="cb13-55">results_df, best_garch_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> find_garch(p_min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, p_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, q_min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, q_max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_unindex, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span>)</span>
<span id="cb13-56"></span>
<span id="cb13-57"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Meilleur modèle GARCH: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>best_garch_order<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> avec AIC=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>best_aic<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb13-58"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb13-59"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résultats pour les modèles testés:"</span>)</span>
<span id="cb13-60">results_df.sort_values(by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relative_gap'</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Meilleur modèle GARCH: (1, 0, 1) avec AIC=-20100.176479566246
==============================
Résultats pour les modèles testés:</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="10">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">p</th>
<th data-quarto-table-cell-role="th">q</th>
<th data-quarto-table-cell-role="th">aic</th>
<th data-quarto-table-cell-role="th">relative_gap</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">1</td>
<td>1</td>
<td>1</td>
<td>-7.493455e+07</td>
<td>-0.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">4</td>
<td>2</td>
<td>1</td>
<td>-7.490963e+07</td>
<td>-0.033254</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>1</td>
<td>2</td>
<td>-7.486519e+07</td>
<td>-0.092567</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>2</td>
<td>2</td>
<td>-7.483014e+07</td>
<td>-0.139333</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">3</td>
<td>2</td>
<td>0</td>
<td>-7.189514e+07</td>
<td>-4.056091</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>0</td>
<td>-7.073946e+07</td>
<td>-5.598342</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>En utilisant le critère AIC pour sélectionner le meilleur modèle, nous avons conclu que GARCH(1,1) est effectivement le meilleur modèle.</p>
<div id="19c942a1" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">garch11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arch_model(data_unindex, vol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Garch'</span>, p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, mean<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Zero'</span>, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'normal'</span>)</span>
<span id="cb15-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb15-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résumé du modèle GARCH(1,1)"</span>)</span>
<span id="cb15-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb15-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).summary())</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>==============================================================================
Résumé du modèle GARCH(1,1)
==============================================================================
                       Zero Mean - GARCH Model Results                        
==============================================================================
Dep. Variable:             log_return   R-squared:                       0.000
Mean Model:                 Zero Mean   Adj. R-squared:                  0.000
Vol Model:                      GARCH   Log-Likelihood:                10638.0
Distribution:                  Normal   AIC:                          -21270.1
Method:            Maximum Likelihood   BIC:                          -21251.6
                                        No. Observations:                 3523
Date:                Wed, Feb 11 2026   Df Residuals:                     3523
Time:                        03:02:56   Df Model:                            0
                              Volatility Model                              
============================================================================
                 coef    std err          t      P&gt;|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega      3.8930e-06  4.514e-10   8624.704      0.000 [3.892e-06,3.894e-06]
alpha[1]       0.1000  5.098e-03     19.615  1.161e-85   [9.001e-02,  0.110]
beta[1]        0.8800  7.520e-03    117.017      0.000     [  0.865,  0.895]
============================================================================

Covariance estimator: robust</code></pre>
</div>
</div>
<div id="4a26022f" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1">cond_resid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).conditional_volatility <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Volatilité conditionnelle =&gt; sigma_t</span></span>
<span id="cb17-2">resid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).resid <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># résidus du modèle =&gt; eps_t</span></span>
<span id="cb17-3">resid_std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).std_resid  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># résidus studentisés =&gt; eta_t</span></span>
<span id="cb17-4"></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># jarque bera test</span></span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> jarque_bera</span>
<span id="cb17-8"></span>
<span id="cb17-9">jb_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> jarque_bera(resid_std)</span>
<span id="cb17-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"H0: Les résidus studentisés suivent une loi normale"</span>)</span>
<span id="cb17-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Test de Jarque-Bera sur les résidus studentisés: JB=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>jb_test[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, p-value=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>jb_test[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reject the null hypothesis of normality for the distribution of the residuals, </span></span>
<span id="cb17-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># as a rule of thumb, which implies that the data to be fitted is not</span></span>
<span id="cb17-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normally distributed</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>H0: Les résidus studentisés suivent une loi normale
Test de Jarque-Bera sur les résidus studentisés: JB=848.8557767883675, p-value=4.71313744144075e-185</code></pre>
</div>
</div>
<div id="72102b00" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### y revenir</span></span>
<span id="cb19-2"></span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### coeff &lt;1</span></span></code></pre></div>
</div>
<div id="9288da36" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test d'homoscédasticité</span></span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ljung-Box test sur résidus</span></span>
<span id="cb20-3">lb_test_resid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> acorr_ljungbox(resid_std, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)], return_df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb20-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ljung-Box Test sur résidus:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, lb_test_resid)</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ljung-Box test sur carrés des résidus</span></span>
<span id="cb20-7">lb_test_resid_sq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> acorr_ljungbox(resid_std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)], return_df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb20-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ljung-Box Test sur carrés des résidus:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, lb_test_resid_sq)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Ljung-Box Test sur résidus:
      lb_stat  lb_pvalue
1   0.028087   0.866904
2   0.572026   0.751253
3   0.690100   0.875530
4   1.235029   0.872298
5   2.199461   0.820914
6   2.491801   0.869384
7   2.828137   0.900433
8   2.941010   0.938005
9   3.793237   0.924486
10  4.644433   0.913631
11  4.727144   0.943657
12  6.763448   0.872842
Ljung-Box Test sur carrés des résidus:
       lb_stat  lb_pvalue
1    0.280711   0.596235
2    0.339634   0.843819
3    6.670837   0.083163
4    7.395445   0.116409
5    8.091586   0.151260
6    8.233789   0.221471
7    8.724987   0.273009
8    9.386238   0.310768
9    9.938908   0.355454
10  11.579309   0.314198
11  13.394501   0.268325
12  13.845698   0.310670</code></pre>
</div>
</div>
<div id="e9956e1b" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LM test pour les effets ARCH</span></span>
<span id="cb22-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.stats.diagnostic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> het_arch</span>
<span id="cb22-3"></span>
<span id="cb22-4">lm_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> het_arch(resid_std)</span>
<span id="cb22-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'LM Test Statistique: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.3f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, p-value: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.3f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> (lm_test[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], lm_test[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>LM Test Statistique: 12.218, p-value: 0.271</code></pre>
</div>
</div>
<div id="c9535184" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb24-2">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">221</span>)</span>
<span id="cb24-3">plot_acf(resid_std, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca())</span>
<span id="cb24-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACF des résidus studentisés"</span>)</span>
<span id="cb24-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résidus studentisés du modèle GARCH(1,1)"</span>)</span>
<span id="cb24-6">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">222</span>)</span>
<span id="cb24-7">plot_pacf(resid_std, lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca())</span>
<span id="cb24-8">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-17-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Le modèle AR(1)-GARCH(1,1) estimé est le suivant :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_t%20=%20%5Cmu_t%20+%20%5Cepsilon_t%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Cmu_t%20=%200.0001%20-%200.0037%20r_%7Bt-1%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cepsilon_t%20=%20%5Csigma_t%20%5Ceta_t%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_t%5E2%20=%203.89%20%5Ctimes%2010%5E%7B-6%7D%20+%200.10%20%5Ctimes%20%5Cepsilon_%7Bt-i%7D%5E2%20+%200.88%20%5Ctimes%20%5Csigma_%7Bt-i%7D%5E2%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?%5Ceta_t"> un bruit blanc supposée gaussien.</p>
<p>Dans ce cas, nous rencontrons des problèmes au niveau de la significativité du coefficient AR(1). En effet, il aurait été plus judicieux de ne pas modéliser la tendance moyenne du rendement et la supposer constante. De plus, au niveau du GARCH(1,1), les résidus sont bien des bruits blancs homoscédastiques (test de lljung box et test LM). Cependant, nous avons supposé que <img src="https://latex.codecogs.com/png.latex?%5Ceta_t"> est un bruit blanc gaussien. Cela n’est pas vérifié. Il aurait été judicieux de tester d’autres distributions telles que Students’s t (’t’, ‘studentst’), Skewed Student’s t (‘skewstudent’, ‘skewt’) ou encore Generalized Error Distribution (GED).</p>
<pre><code>**Test de Lagrange Multiplier (LM) pour l'effet ARCH**

Le test de Lagrange Multiplier (LM) pour l'effet ARCH est un outil statistique qui vérifie la présence d'effets ARCH (AutoRegressive Conditional Heteroskedasticity) dans une série temporelle.

L'effet ARCH se manifeste lorsque la variance d'une erreur est une fonction de ses erreurs passées. Cette propriété est courante dans les séries temporelles financières, où de grandes variations des rendements sont souvent suivies par de grandes variations et vice versa.

Le test de LM vérifie l'hypothèse nulle que les erreurs sont homoscédastiques (variance constante). Si la p-value du test est inférieure à un seuil prédéfini (généralement 0,05), l'hypothèse nulle est rejetée, indiquant la présence d'effets ARCH.</code></pre>
<div id="26f4e731" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création de la figure avec des sous-graphiques alignés verticalement</span></span>
<span id="cb26-2">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb26-3"></span>
<span id="cb26-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Premier graphique : CAC 40</span></span>
<span id="cb26-5">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">311</span>)</span>
<span id="cb26-6">plt.plot(resid) </span>
<span id="cb26-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résidus du modèle AR(1)"</span>)</span>
<span id="cb26-8"></span>
<span id="cb26-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Deuxième graphique : Résidus du modèle AR(1)</span></span>
<span id="cb26-10">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">312</span>)</span>
<span id="cb26-11">plt.plot(cond_resid)</span>
<span id="cb26-12">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatile conditionnelle du modèle GARCH(1,1)"</span>)</span>
<span id="cb26-13"></span>
<span id="cb26-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Troisième graphique : Résidus studentisés du modèle GARCH(1,1)</span></span>
<span id="cb26-15">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">313</span>)</span>
<span id="cb26-16">plt.plot(resid_std, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Résidus studentisés du modèle GARCH(1,1)'</span>)</span>
<span id="cb26-17">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Résidus studentisés du modèle GARCH(1,1)"</span>)</span>
<span id="cb26-18"></span>
<span id="cb26-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage des graphiques</span></span>
<span id="cb26-20">plt.tight_layout()</span>
<span id="cb26-21">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-18-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="i.2.-dynamique-historique-de-mu_t-et-sigma_t" class="level3">
<h3 class="anchored" data-anchor-id="i.2.-dynamique-historique-de-mu_t-et-sigma_t">I.2. Dynamique historique de <img src="https://latex.codecogs.com/png.latex?%5Cmu_t"> et <img src="https://latex.codecogs.com/png.latex?%5Csigma_t"></h3>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_t%20=%20%5Cmu_t%20+%20%5Csigma_t%20%5Ctimes%20%5Cepsilon_t%0A%5Cquad%0A%5Ctext%7Bavec%7D%20%5Cquad%0A%5Cbegin%7Bcases%7D%0A%20%20%20%20%5Cmu_t%20=%20%5Cmu%20+%20%5Cvarphi%20r_%7Bt-1%7D%20%5C%5C%0A%20%20%20%20%5Csigma_t%5E2%20=%20%5Comega%20+%20a%20(r_%7Bt-1%7D%20-%20%5Cmu_%7Bt-1%7D)%5E2%20+%20b%20%5Csigma_%7Bt-1%7D%5E2%0A%5Cend%7Bcases%7D%0A"></p>
<p>Pour avoir la dynamique historique de <img src="https://latex.codecogs.com/png.latex?%5Cmu_t"> et <img src="https://latex.codecogs.com/png.latex?%5Csigma_t">, nous allons utiliser les données historiques de la série temporelle ainsi que les estimations des paramètres <img src="https://latex.codecogs.com/png.latex?%5CTheta%20=%20(%5Cmu,%20%5Cvarphi,%20%5Comega,%20a,%20b)"> du modèle AR(1)-GARCH(1,1) que nous avons estimé précédemment par maximum de vraisemblance.</p>
<p>Pour <img src="https://latex.codecogs.com/png.latex?t=1">, nous allons initialiser <img src="https://latex.codecogs.com/png.latex?%5Cmu_1"> par la moyenne <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Csigma_1"> par la variance à long terme <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Comega%7D%7B1%20-%20a%20-%20b%7D">.</p>
<div id="199501fd" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(AR1.fit().summary())</span>
<span id="cb27-2"></span>
<span id="cb27-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tester arima avec arch_model ou arch</span></span>
<span id="cb27-4">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AR1.fit().params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb27-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paramètre mu: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb27-6">phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AR1.fit().params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb27-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paramètre phi: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>phi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>                               SARIMAX Results                                
==============================================================================
Dep. Variable:             log_return   No. Observations:                 3523
Model:                 ARIMA(1, 0, 0)   Log Likelihood               10052.114
Date:                Wed, 11 Feb 2026   AIC                         -20098.227
Time:                        03:02:56   BIC                         -20079.726
Sample:                             0   HQIC                        -20091.627
                               - 3523                                         
Covariance Type:                  opg                                         
==============================================================================
                 coef    std err          z      P&gt;|z|      [0.025      0.975]
------------------------------------------------------------------------------
const          0.0001      0.000      0.621      0.535      -0.000       0.001
ar.L1         -0.0037      0.012     -0.321      0.748      -0.027       0.019
sigma2         0.0002   2.16e-06     89.947      0.000       0.000       0.000
===================================================================================
Ljung-Box (L1) (Q):                   0.00   Jarque-Bera (JB):              7957.49
Prob(Q):                              1.00   Prob(JB):                         0.00
Heteroskedasticity (H):               0.61   Skew:                            -0.30
Prob(H) (two-sided):                  0.00   Kurtosis:                        10.34
===================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
Paramètre mu: 0.00014959052741773347
Paramètre phi: -0.003743634042716415</code></pre>
</div>
</div>
<div id="d8ac4b44" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).summary())</span>
<span id="cb29-2">omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb29-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paramètre omega: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>omega<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb29-4">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb29-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paramètre alpha: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb29-6">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> garch11.fit(disp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>).params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb29-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paramètre beta: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>                       Zero Mean - GARCH Model Results                        
==============================================================================
Dep. Variable:             log_return   R-squared:                       0.000
Mean Model:                 Zero Mean   Adj. R-squared:                  0.000
Vol Model:                      GARCH   Log-Likelihood:                10638.0
Distribution:                  Normal   AIC:                          -21270.1
Method:            Maximum Likelihood   BIC:                          -21251.6
                                        No. Observations:                 3523
Date:                Wed, Feb 11 2026   Df Residuals:                     3523
Time:                        03:02:57   Df Model:                            0
                              Volatility Model                              
============================================================================
                 coef    std err          t      P&gt;|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega      3.8930e-06  4.514e-10   8624.704      0.000 [3.892e-06,3.894e-06]
alpha[1]       0.1000  5.098e-03     19.615  1.161e-85   [9.001e-02,  0.110]
beta[1]        0.8800  7.520e-03    117.017      0.000     [  0.865,  0.895]
============================================================================

Covariance estimator: robust
Paramètre omega: 3.892997741815931e-06
Paramètre alpha: 0.1
Paramètre beta: 0.88</code></pre>
</div>
</div>
<div id="dd3db506" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">T_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_train)</span>
<span id="cb31-2">T_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test)</span>
<span id="cb31-3"></span>
<span id="cb31-4">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> T_test</span>
<span id="cb31-5"></span>
<span id="cb31-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des séries</span></span>
<span id="cb31-7">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([data_train, data_test], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb31-8">mu_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(T)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Composante moyenne</span></span>
<span id="cb31-9">sigma2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(T)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variance conditionnelle</span></span>
<span id="cb31-10"></span>
<span id="cb31-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditions initiales</span></span>
<span id="cb31-12">mu_t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mu</span>
<span id="cb31-13">sigma2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variance de long terme</span></span>
<span id="cb31-14"></span>
<span id="cb31-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulation du modèle</span></span>
<span id="cb31-16"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, T):</span>
<span id="cb31-17">    mu_t[t] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Partie moyenne</span></span>
<span id="cb31-18">    sigma2[t] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (r[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu_t[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma2[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variance conditionnelle</span></span>
<span id="cb31-19"></span>
<span id="cb31-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage des résultats</span></span>
<span id="cb31-21">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb31-22"></span>
<span id="cb31-23">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].plot(r, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb31-24">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rendements $r_t$"</span>)</span>
<span id="cb31-25">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend()</span>
<span id="cb31-26"></span>
<span id="cb31-27">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].plot(mu_t, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb31-28">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Composante moyenne $\mu_t$"</span>)</span>
<span id="cb31-29">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].legend()</span>
<span id="cb31-30"></span>
<span id="cb31-31">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>].plot(np.sqrt(sigma2), color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb31-32">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité conditionnelle $\sigma_t$"</span>)</span>
<span id="cb31-33">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>].legend()</span>
<span id="cb31-34"></span>
<span id="cb31-35">plt.tight_layout()</span>
<span id="cb31-36">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-21-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>En analysant la dynamique de <img src="https://latex.codecogs.com/png.latex?%5Cmu_t">, nous constatons que la tendance moyenne est très semblable à la série des log-rendements. Cela est dû au fait que le modèle AR(1) n’est pas pertinent pour modéliser la série. En effet, la série des log-rendements ressemble déjà à un bruit blanc. Par ailleurs, nous observons de fortes périodes de volatilité dans la série des log-rendements pendant les périodes de crises, i.e.&nbsp;2008-2009 qui correspond à la crise des subprimes et 2020 qui correspond à la crise du Covid-19. Le modèle GARCH semble bien capturer ces périodes de volatilité dans la volatilité conditionnelle calibrée.</p>
</section>
<section id="i.3.-estimation-de-la-var" class="level3">
<h3 class="anchored" data-anchor-id="i.3.-estimation-de-la-var">I.3. Estimation de la VaR</h3>
<div id="734c03e8" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR historique</span></span>
<span id="cb32-2"></span>
<span id="cb32-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> historical_var(data, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>):</span>
<span id="cb32-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR historique</span></span>
<span id="cb32-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb32-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb32-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.percentile(data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span>
<span id="cb32-10"></span>
<span id="cb32-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR gaussienne</span></span>
<span id="cb32-12"></span>
<span id="cb32-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> gaussian_var(data, alpha):</span>
<span id="cb32-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb32-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb32-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb32-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-19">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb32-20"></span>
<span id="cb32-21">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(data)</span>
<span id="cb32-22">    sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(data)</span>
<span id="cb32-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span>
<span id="cb32-24"></span>
<span id="cb32-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loi de Skew Student par maximum de vraisemblance.</span></span>
<span id="cb32-26"></span>
<span id="cb32-27"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize</span>
<span id="cb32-28"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> t</span>
<span id="cb32-29"></span>
<span id="cb32-30"></span>
<span id="cb32-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_pdf(x, mu, sigma, gamma, nu ):</span>
<span id="cb32-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Compute the Skew Student-t probability density function (PDF).</span></span>
<span id="cb32-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-35">    </span>
<span id="cb32-36"></span>
<span id="cb32-37">    t_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gamma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt((nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ((x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb32-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PDF of the standard Student-t distribution</span></span>
<span id="cb32-39">    pdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.pdf(x , df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu,  loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mu, scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma)</span>
<span id="cb32-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CDF of the transformed Student-t distribution</span></span>
<span id="cb32-41">    cdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.cdf(t_x, df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb32-42"></span>
<span id="cb32-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skew Student density function</span></span>
<span id="cb32-44">    density <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> cdf_t</span>
<span id="cb32-45"></span>
<span id="cb32-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> density</span>
<span id="cb32-47"></span>
<span id="cb32-48"></span>
<span id="cb32-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_log_likelihood(params, data):</span>
<span id="cb32-50">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la log-vraisemblance de la loi de Skew Student</span></span>
<span id="cb32-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    params [mu, sigma, gamma, nu]: les paramètres de la loi</span></span>
<span id="cb32-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb32-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-55">    mu, sigma, gamma, nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params</span>
<span id="cb32-56">    density <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> skew_student_pdf(data , mu, sigma, gamma, nu)</span>
<span id="cb32-57">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># log-vraisemblance</span></span>
<span id="cb32-58">    loglik <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(np.log(density))</span>
<span id="cb32-59">    </span>
<span id="cb32-60">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> loglik</span>
<span id="cb32-61"></span>
<span id="cb32-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Optimisation des paramètres avec contraintes de positivité sur sigma et nu</span></span>
<span id="cb32-63"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_fit(data):</span>
<span id="cb32-64">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Estimation des paramètres de la loi de Skew Student</span></span>
<span id="cb32-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-67">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initial guess</span></span>
<span id="cb32-68">    x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([np.mean(data), np.std(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb32-69"></span>
<span id="cb32-70">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># contraintes</span></span>
<span id="cb32-71">    bounds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)]</span>
<span id="cb32-72"></span>
<span id="cb32-73">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># optimisation</span></span>
<span id="cb32-74">    res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize(skew_student_log_likelihood, x0, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(data), bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bounds)</span>
<span id="cb32-75"></span>
<span id="cb32-76">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> res.x</span>
<span id="cb32-77"></span>
<span id="cb32-78">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> skew_student_fit(resid_std)</span>
<span id="cb32-79"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb32-80"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Les paramètres estimés de la loi de Skew Student sont : "</span>)</span>
<span id="cb32-81"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb32-82"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mu : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb32-83"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sigma : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb32-84"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gamma : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb32-85"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nu : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb32-86"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb32-87"></span>
<span id="cb32-88"></span>
<span id="cb32-89">params_sstd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb32-90">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], </span>
<span id="cb32-91">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb32-92">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gamma"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb32-93">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb32-94">}</span>
<span id="cb32-95"></span>
<span id="cb32-96"></span>
<span id="cb32-97"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## Intégration de la fonction de densité</span></span>
<span id="cb32-98"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> integrate</span>
<span id="cb32-99"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize_scalar</span>
<span id="cb32-100"></span>
<span id="cb32-101"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> integrale_SkewStudent(x,params):</span>
<span id="cb32-102">    borne_inf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.inf</span>
<span id="cb32-103">    resultat_integration, erreur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> integrate.quad(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: skew_student_pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params), borne_inf, x)</span>
<span id="cb32-104">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> resultat_integration</span>
<span id="cb32-105"></span>
<span id="cb32-106"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fonc_minimize(x, alpha,params):</span>
<span id="cb32-107">    value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> integrale_SkewStudent(x,params)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha</span>
<span id="cb32-108">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(value)</span>
<span id="cb32-109"></span>
<span id="cb32-110"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_quantile(alpha,mu, sigma, gamma, nu ):</span>
<span id="cb32-111">    params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb32-112">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span> : mu ,</span>
<span id="cb32-113">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span> : sigma,</span>
<span id="cb32-114">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gamma"</span> : gamma,</span>
<span id="cb32-115">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span> : nu</span>
<span id="cb32-116">    }</span>
<span id="cb32-117"></span>
<span id="cb32-118">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb32-119">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Veuillez entrer un niveau alpha entre 0 et 1"</span>)</span>
<span id="cb32-120">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb32-121">        resultat_minimisation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize_scalar(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: fonc_minimize(x, alpha,params))</span>
<span id="cb32-122">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> resultat_minimisation.x</span>
<span id="cb32-123">    </span>
<span id="cb32-124"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : écrire une fonction qui calcule la VaR skew-student</span></span>
<span id="cb32-125"></span>
<span id="cb32-126"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sstd_var(alpha, params):</span>
<span id="cb32-127">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-128"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR skew student</span></span>
<span id="cb32-129"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb32-130"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb32-131"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-132"></span>
<span id="cb32-133">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>skew_student_quantile(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb32-134"></span>
<span id="cb32-135"></span>
<span id="cb32-136"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#### A FAIRE VAR POT et BM</span></span>
<span id="cb32-137"></span>
<span id="cb32-138"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> genextreme <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gev</span>
<span id="cb32-139"></span>
<span id="cb32-140"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb32-141"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb32-142">neg_resid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>resid_std</span>
<span id="cb32-143"></span>
<span id="cb32-144"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_extremes(returns, block_size, min_last_block<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>):</span>
<span id="cb32-145">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-146"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Extrait les valeurs extrêmes d'une série de rendements par blocs.</span></span>
<span id="cb32-147"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb32-148"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Arguments :</span></span>
<span id="cb32-149"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    returns : pandas Series (index = dates, valeurs = rendements)</span></span>
<span id="cb32-150"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    block_size : int, taille du bloc en nombre de jours</span></span>
<span id="cb32-151"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    min_last_block : float, proportion minimale pour inclure le dernier bloc incomplet</span></span>
<span id="cb32-152"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb32-153"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb32-154"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    maxima_sample : liste des valeurs maximales par bloc</span></span>
<span id="cb32-155"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    maxima_dates : liste des dates associées aux valeurs maximales</span></span>
<span id="cb32-156"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-157">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(returns)</span>
<span id="cb32-158">    num_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> block_size</span>
<span id="cb32-159"></span>
<span id="cb32-160">    maxima_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb32-161">    maxima_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb32-162"></span>
<span id="cb32-163">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(num_blocks):</span>
<span id="cb32-164">        block_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb32-165">        block_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb32-166">        block_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns.iloc[block_start:block_end]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sélectionner le bloc avec les index</span></span>
<span id="cb32-167"></span>
<span id="cb32-168">        max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb32-169">        max_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.idxmax()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Récupérer l'index de la valeur max</span></span>
<span id="cb32-170"></span>
<span id="cb32-171">        maxima_sample.append(max_value)</span>
<span id="cb32-172">        maxima_dates.append(max_date)</span>
<span id="cb32-173"></span>
<span id="cb32-174">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gestion du dernier bloc s'il reste des données suffisantes</span></span>
<span id="cb32-175">    block_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> num_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb32-176">    block_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns.iloc[block_start:]</span>
<span id="cb32-177"></span>
<span id="cb32-178">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(block_data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> min_last_block <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size:</span>
<span id="cb32-179">        max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb32-180">        max_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.idxmax()</span>
<span id="cb32-181">        </span>
<span id="cb32-182">        maxima_sample.append(max_value)</span>
<span id="cb32-183">        maxima_dates.append(max_date)</span>
<span id="cb32-184">        </span>
<span id="cb32-185">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pd.Series(maxima_sample, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>maxima_dates)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retourner une Series avec les dates comme index</span></span>
<span id="cb32-186"></span>
<span id="cb32-187"></span>
<span id="cb32-188"></span>
<span id="cb32-189"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> BM_var(alpha,s,shape,loc,scale):</span>
<span id="cb32-190">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-191"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb32-192"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance de la VaR</span></span>
<span id="cb32-193"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    s : le nombre de jours dans un bloc</span></span>
<span id="cb32-194"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-195">    alpha_bm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha)</span>
<span id="cb32-196"></span>
<span id="cb32-197">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gev.ppf(alpha_bm, shape, loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scale),alpha_bm</span>
<span id="cb32-198"></span>
<span id="cb32-199">extremes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_extremes(neg_resid, block_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, min_last_block<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb32-200">params_gev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gev.fit(extremes)</span>
<span id="cb32-201"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb32-202"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Les paramètres estimés de la loi de GEV sont : "</span>)</span>
<span id="cb32-203"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb32-204"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Shape (xi) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gev[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-205"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Localisation (mu) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gev[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-206"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Echelle (sigma) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gev[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-207"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb32-208"></span>
<span id="cb32-209"></span>
<span id="cb32-210"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> POT_var(data,alpha,u,shape,loc,scale):</span>
<span id="cb32-211">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb32-212"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb32-213"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance de la VaR</span></span>
<span id="cb32-214"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    s : le nombre de jours dans un bloc</span></span>
<span id="cb32-215"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb32-216">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data)</span>
<span id="cb32-217">    excess_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> u]</span>
<span id="cb32-218">    nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(excess_values)</span>
<span id="cb32-219"></span>
<span id="cb32-220">    alpha_pot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nu</span>
<span id="cb32-221"></span>
<span id="cb32-222">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> genpareto.ppf(alpha_pot, shape, loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scale) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u,alpha_pot</span>
<span id="cb32-223"></span>
<span id="cb32-224"></span>
<span id="cb32-225">u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb32-226">excess_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> neg_resid <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> u]</span>
<span id="cb32-227"></span>
<span id="cb32-228"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> genpareto</span>
<span id="cb32-229"></span>
<span id="cb32-230">params_gpd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> genpareto.fit(excess_values)</span>
<span id="cb32-231"></span>
<span id="cb32-232"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher les paramètres estimés</span></span>
<span id="cb32-233"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb32-234"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres estimés de la distribution GPD:"</span>)</span>
<span id="cb32-235"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Shape (xi) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-236"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Localisation (mu) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-237"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Echelle (sigma) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-238"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Les paramètres estimés de la loi de Skew Student sont : 
---------------
Mu :  0.4250705574852528
Sigma :  0.8686243849485155
Gamma :  -0.6074103171572136
Nu :  5.607563514180729
================================================================================
================================================================================
Les paramètres estimés de la loi de GEV sont : 
---------------
Shape (xi) = -0.01
Localisation (mu) = 1.64
Echelle (sigma) = 0.72
================================================================================
================================================================================
Paramètres estimés de la distribution GPD:
Shape (xi) = -0.04
Localisation (mu) = 0.00
Echelle (sigma) = 0.80
================================================================================</code></pre>
</div>
</div>
<div id="3eaf6947" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb34-2"></span>
<span id="cb34-3">var_hist_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> historical_var(resid_std, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb34-4">var_gauss_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gaussian_var(resid_std, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb34-5">var_sstd_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sstd_var(alpha, params_sstd)</span>
<span id="cb34-6">var_BM_train,_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BM_var(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gev)</span>
<span id="cb34-7">var_POT_train,_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> POT_var(neg_resid, alpha, u,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gpd)</span>
<span id="cb34-8"></span>
<span id="cb34-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in a df</span></span>
<span id="cb34-10">var <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb34-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Historique'</span>: [var_hist_train],</span>
<span id="cb34-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Gaussienne'</span>: [var_gauss_train],</span>
<span id="cb34-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Skew Student'</span>: [var_sstd_train],</span>
<span id="cb34-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Block Maxima'</span>: [var_BM_train],</span>
<span id="cb34-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peak Over Threshold'</span>: [var_POT_train]</span>
<span id="cb34-16">})</span>
<span id="cb34-17"></span>
<span id="cb34-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb34-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value at Risk sur les résidus studentisés (en %) pour h=1j"</span>)</span>
<span id="cb34-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb34-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Value at Risk sur les résidus studentisés (en %) pour h=1j
   Historique  Gaussienne  Skew Student  Block Maxima  Peak Over Threshold
0      264.12      229.76        280.29        268.68               284.98
================================================================================</code></pre>
</div>
</div>
<section id="a.-var-historique-dynamique" class="level4">
<h4 class="anchored" data-anchor-id="a.-var-historique-dynamique">a. VaR historique dynamique</h4>
<div id="ce51448a" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1">var_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(T_test)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Composante moyenne</span></span>
<span id="cb36-2">nb_exp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb36-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(T_test):</span>
<span id="cb36-4">    var_t[t] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (mu_t[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>T_train] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.sqrt(sigma2[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>T_train])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var_hist_train)</span>
<span id="cb36-5">    nb_exp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (r[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>T_train] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> var_t[t]).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb36-6">    </span>
<span id="cb36-7">var_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.Series(var_t, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_test.index)</span>
<span id="cb36-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Nombre d'exceptions = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>nb_exp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> sur </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>T_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> jours"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Nombre d'exceptions = 4 sur 586 jours</code></pre>
</div>
</div>
<div id="33fb3547" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb38-2">plt.plot(data_train, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Train'</span>)</span>
<span id="cb38-3">plt.plot(data_test, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Test'</span>)</span>
<span id="cb38-4">plt.plot(var_t, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>,label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'VaR dynamique'</span>)</span>
<span id="cb38-5">plt.axvline(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_test.index[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>)</span>
<span id="cb38-6">plt.legend()</span>
<span id="cb38-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Série des log-rendements et VaR dynamique'</span>)</span>
<span id="cb38-8">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-25-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="e676e120" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb39-2">plt.plot(data_test, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>)</span>
<span id="cb39-3">plt.plot(var_t, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb39-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Zoom sur la VaR dynamique'</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="25">
<pre><code>Text(0.5, 1.0, 'Zoom sur la VaR dynamique')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch_files/figure-html/cell-26-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="60666494" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># backtest à faire (optionnel)</span></span></code></pre></div>
</div>


</section>
</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>risques-fin</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_garch.html</guid>
  <pubDate>Thu, 27 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>TP2:Méthodes basées sur la théorie des valeurs extrêmes</title>
  <dc:creator>Cheryl Kouadio</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt.html</link>
  <description><![CDATA[ 




<p>Ce TP est une continuité du TP-1 dans lequel on souhaitait implémenter la VaR (Value at Risk) et l’ES (Expected Shortfall) en utilisant les méthodes classiques proposées dans la réglementation bâloise, i.e.&nbsp;la méthode historique, paramétrique et bootstrap. Cependant, une limite de ces méthodes est qu’elles ne prennent pas en compte la queue de distribution de la perte. Pour remédier à cela, on peut utiliser des méthodes avec la théorie des valeurs extrêmes, i.e.&nbsp;l’approche Block Maxima et l’approche Peaks Over Threshold.</p>
<div id="37b4a204" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des librairies</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span></code></pre></div>
</div>
<div id="3d66944c" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import des données du CAC 40</span></span>
<span id="cb2-2">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>,start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2008-01-01"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des rendements logarithmiques</span></span>
<span id="cb2-5">data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.log(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>].shift(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retirer la première ligne</span></span>
<span id="cb2-8">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dropna()</span>
<span id="cb2-9">data.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.columns.get_level_values(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="9b49fdeb" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'15-10-2008'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'26-07-2022'</span>]</span>
<span id="cb4-2">data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span>
<span id="cb4-3">neg_data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>data_train</span>
<span id="cb4-4"></span>
<span id="cb4-5">test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'27-07-2022'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11-06-2024'</span>]</span>
<span id="cb4-6">data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span>
<span id="cb4-7">neg_data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>data_test</span></code></pre></div>
</div>
<section id="i.-implémentation-de-la-var-avec-la-théorie-des-valeurs-extrêmes" class="level2">
<h2 class="anchored" data-anchor-id="i.-implémentation-de-la-var-avec-la-théorie-des-valeurs-extrêmes">I. Implémentation de la VaR avec la théorie des valeurs extrêmes</h2>
<section id="i.1.-var-tve-approche-maxima-par-bloc" class="level3">
<h3 class="anchored" data-anchor-id="i.1.-var-tve-approche-maxima-par-bloc">I.1. VaR TVE : Approche Maxima par bloc</h3>
<p>L’approche des Block Maxima (BM) est une méthode modélise les maxima des rendements sur des blocs de taille fixe <img src="https://latex.codecogs.com/png.latex?s"> en utilisant la distribution GEV. Le seuil de confiance <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Ctext%7BGEV%7D%7D"> est ajusté pour correspondre à l’horizon temporel de la VaR via la relation :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7B1-%5Calpha_%7B%5Ctext%7BVaR%7D%7D%7D%20=%20s%20%5Ctimes%20%5Cfrac%7B1%7D%7B1-%5Calpha_%7B%5Ctext%7BGEV%7D%7D%7D.%0A"></p>
<p>Nous allons dans ce projet une taille de blocs <img src="https://latex.codecogs.com/png.latex?s%20=%2021"> jours ouvrés comme ce qui souvent utilisé en pratique. De ce fait, nous parvenons à construire 239 blocs de taille 21 et un bloc de taille De fait, la Value-at-Risk sur un horizon 1 et pour un niveau de confiance $ _{}$ est :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVaR%7D_h(%5Calpha_%7B%5Ctext%7BVaR%7D%7D)%20=%20G%5E%7B-1%7D_%7B(%5Chat%20%5Cmu,%20%5Chat%20%5Csigma,%20%5Chat%20%5Cxi)%7D(%5Calpha_%7B%5Ctext%7BGEV%7D%7D),%0A"></p>
<p>où G est la fonction de répartition de la GEV (<img src="https://latex.codecogs.com/png.latex?%5Chat%20%5Cmu,%20%5Chat%20%5Csigma,%20%5Chat%20%5Cxi">) estimée.</p>
</section>
<section id="i.1.1.-construction-de-léchantillon-de-maxima-sur-data_train" class="level3">
<h3 class="anchored" data-anchor-id="i.1.1.-construction-de-léchantillon-de-maxima-sur-data_train">I.1.1. Construction de l’échantillon de maxima sur data_train</h3>
<div id="6bf1bfce" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb5-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_extremes(returns, block_size, min_last_block<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>):</span>
<span id="cb5-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Extrait les valeurs extrêmes d'une série de rendements par blocs.</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Arguments :</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    returns : pandas Series (index = dates, valeurs = rendements)</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    block_size : int, taille du bloc en nombre de jours</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    min_last_block : float, proportion minimale pour inclure le dernier bloc incomplet</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    maxima_sample : liste des valeurs maximales par bloc</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    maxima_dates : liste des dates associées aux valeurs maximales</span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb5-17">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(returns)</span>
<span id="cb5-18">    num_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> block_size</span>
<span id="cb5-19"></span>
<span id="cb5-20">    maxima_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb5-21">    maxima_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb5-22"></span>
<span id="cb5-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(num_blocks):</span>
<span id="cb5-24">        block_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb5-25">        block_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb5-26">        block_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns.iloc[block_start:block_end]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sélectionner le bloc avec les index</span></span>
<span id="cb5-27"></span>
<span id="cb5-28">        max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb5-29">        max_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.idxmax()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Récupérer l'index de la valeur max</span></span>
<span id="cb5-30"></span>
<span id="cb5-31">        maxima_sample.append(max_value)</span>
<span id="cb5-32">        maxima_dates.append(max_date)</span>
<span id="cb5-33"></span>
<span id="cb5-34">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gestion du dernier bloc s'il reste des données suffisantes</span></span>
<span id="cb5-35">    block_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> num_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size</span>
<span id="cb5-36">    block_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> returns.iloc[block_start:]</span>
<span id="cb5-37"></span>
<span id="cb5-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(block_data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> min_last_block <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> block_size:</span>
<span id="cb5-39">        max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb5-40">        max_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> block_data.idxmax()</span>
<span id="cb5-41">        </span>
<span id="cb5-42">        maxima_sample.append(max_value)</span>
<span id="cb5-43">        maxima_dates.append(max_date)</span>
<span id="cb5-44">        </span>
<span id="cb5-45">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pd.Series(maxima_sample, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>maxima_dates)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retourner une Series avec les dates comme index</span></span>
<span id="cb5-46"></span>
<span id="cb5-47"></span>
<span id="cb5-48">extremes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_extremes(neg_data_train, block_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, min_last_block<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb5-49"></span>
<span id="cb5-50">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb5-51">plt.plot(data_train, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb5-52">plt.plot(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>extremes,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span></span>
<span id="cb5-53">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Series des rendements du CAC 40 avec les pertes extrêmes"</span>)</span>
<span id="cb5-54">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>)</span>
<span id="cb5-55">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rendements"</span>)</span>
<span id="cb5-56">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Pour avoir une idée de la distribution GEV de la serie des pertes maximales de rendements du CAC 40 pour <img src="https://latex.codecogs.com/png.latex?s=21">, nous utilisons un Gumbel plot qui est un outil graphique pour juger de l’hypothèse <img src="https://latex.codecogs.com/png.latex?%5Cxi=0">, i.e.&nbsp;la distribution GEV se réduit à la distribution de Gumbel.</p>
<p>Pour le construire, nous devons suivre les étapes suivantes :</p>
<ol type="1">
<li>calculer l’abscisse avec la série des maximas ordonées <img src="https://latex.codecogs.com/png.latex?R_%7B(1)%7D%20%5Cleq%20R_%7B(2)%7D%20%5Cleq%20%5Cldots%20%5Cleq%20R_%7B(n)%7D">.</li>
<li>calculer l’ordonnée de la manière suivante :</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A-%20log(-log(%5Cfrac%7Bi%20-%200.5%7D%7Bk%7D)),%20%5Cquad%20i%20=%201,%20%5Cldots,%20k.%0A"></p>
<p>Lorsque la distribution adaptée est celle de Gumbel alors le Gumbel plot est linéaire. Dans notre cas, nous constatons une courbure ce qui nous pousse à conclure qu’une distribution Gumbel n’est pas adaptée dans la modélisation des maxima des pertes de rendements du CAC 40. Une distribution Fréchet ou de Weibull serait plus adaptée.</p>
<div id="9421c345" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">quantiles_theoriques_gumbel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb6-2">k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(extremes)</span>
<span id="cb6-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(extremes)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb6-4">    val <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.log(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.log((i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k))</span>
<span id="cb6-5">    quantiles_theoriques_gumbel.append(val)</span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tracer le Gumbel plot</span></span>
<span id="cb6-8">plt.scatter(quantiles_theoriques_gumbel, np.sort(extremes), marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>)</span>
<span id="cb6-9">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rendements CAC 40 - Gumbel plot'</span>)</span>
<span id="cb6-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-6-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="i.1.2.-estimation-des-paramètres-de-la-loi-de-gev" class="level3">
<h3 class="anchored" data-anchor-id="i.1.2.-estimation-des-paramètres-de-la-loi-de-gev">I.1.2. Estimation des paramètres de la loi de GEV</h3>
<p>En estimant les paramètres de la loi GEV, nous utilisons la méthode du maximum de vraisemblance. Les paramètres estimés par maximisation de la fonction de vraisemblance sont les suivants <img src="https://latex.codecogs.com/png.latex?%5Cxi%20=%20-0.15,%20%5Cmu=0.02,%20%5Csigma=0.01">. Nous constatons par ailleurs que le paramètre de forme <img src="https://latex.codecogs.com/png.latex?%5Cxi"> est négatif ce qui est cohérent avec notre observation précédente.</p>
<div id="445b584c" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> genextreme <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gev</span>
<span id="cb7-2"></span>
<span id="cb7-3">params_gev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gev.fit(extremes)</span>
<span id="cb7-4"></span>
<span id="cb7-5">shape, loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params_gev</span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher les paramètres estimés</span></span>
<span id="cb7-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb7-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres estimés de la distribution GEV"</span>)</span>
<span id="cb7-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb7-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Shape (xi) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Loc (mu) =  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Scale (sigma) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>scale<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>==================================================
Paramètres estimés de la distribution GEV
==================================================
Shape (xi) = -0.15
Loc (mu) =  0.02
Scale (sigma) = 0.01
==================================================</code></pre>
</div>
</div>
<p>Pour accorder plus de poids à cette observation, nous avons calculé un intervalle de confiance profilé à 95% pour le paramètre de forme <img src="https://latex.codecogs.com/png.latex?%5Cxi">. Pour ce faire, nous avons suivi les étapes suivantes : 1. Estimation des paramètres par maximum de vraisemblance : Nous avons estimé <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cxi%7D">, <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Csigma%7D"> en maximisant la log-vraisemblance de la loi GEV.</p>
<ol start="2" type="1">
<li><p>Construction du profil de vraisemblance : Nous avons fixé <img src="https://latex.codecogs.com/png.latex?%5Cxi"> à différentes valeurs autour de <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cxi%7D"> et, pour chacune, réestimé <img src="https://latex.codecogs.com/png.latex?%5Cmu"> et <img src="https://latex.codecogs.com/png.latex?%5Csigma"> afin d’obtenir une log-vraisemblance profilée.</p></li>
<li><p>Seuil basé sur le test du rapport de vraisemblance : Le seuil critique est déterminé par la statistique $ ^2(1) $ :<br>
<img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BL%7D_%7B%5Cmax%7D%20-%20%5Cfrac%7B%5Cchi%5E2_%7B0.95,%201%7D%7D%7B2%7D%0A"></p></li>
<li><p>Détermination des bornes de l’IC : L’intervalle est formé par les valeurs de <img src="https://latex.codecogs.com/png.latex?%5Cxi"> pour lesquelles la log-vraisemblance reste au-dessus de ce seuil.</p></li>
</ol>
<p>Cette approche permet une meilleure prise en compte de l’incertitude en évitant les approximations asymptotiques classiques. La modélisation des maxima des pertes de rendements du CAC 40 par une distribution de Weibull serait plus adaptée.</p>
<p>Nous obtenons ainsi un intervalle de confiance à 95% pour le paramètre de forme <img src="https://latex.codecogs.com/png.latex?%5Cxi"> de <img src="https://latex.codecogs.com/png.latex?%5B-0.284,%20-0.039%5D">. Comme 0 n’appartient pas à cet intervalle, nous pouvons rejeter l’hypothèse <img src="https://latex.codecogs.com/png.latex?%5Cxi=0">. De ce fait, la distribution de Weibull est plus adaptée pour modéliser les maxima des pertes de rendements du CAC 40 car <img src="https://latex.codecogs.com/png.latex?%5Cxi"> est négatif.</p>
<div id="147a57e1" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> chi2</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonction de log-vraisemblance</span></span>
<span id="cb9-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> gev_neg_log_likelihood(params, shape_fixed, data):</span>
<span id="cb9-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcule la log-vraisemblance négative de la distribution GEV</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    en fixant le paramètre 'shape'.</span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb9-10">    loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params</span>
<span id="cb9-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contrainte pour éviter des valeurs invalides</span></span>
<span id="cb9-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.inf</span>
<span id="cb9-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(gev.logpdf(data, shape_fixed, loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc, scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>scale))</span>
<span id="cb9-14"></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-vraisemblance maximale</span></span>
<span id="cb9-16">log_likelihood_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>gev_neg_log_likelihood([loc, scale], shape, extremes)</span>
<span id="cb9-17"></span>
<span id="cb9-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des IC profilés pour le paramètre shape</span></span>
<span id="cb9-19">shape_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plage autour de la valeur estimée</span></span>
<span id="cb9-20">profile_likelihood <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb9-21"></span>
<span id="cb9-22"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> shape_grid:</span>
<span id="cb9-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Réoptimiser loc et scale en fixant shape</span></span>
<span id="cb9-24">    result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize(</span>
<span id="cb9-25">        gev_neg_log_likelihood,</span>
<span id="cb9-26">        x0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[loc, scale],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial guess for loc and scale</span></span>
<span id="cb9-27">        args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(s, extremes),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fixer 'shape' à la valeur actuelle</span></span>
<span id="cb9-28">        bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-5</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contraintes sur loc et scale</span></span>
<span id="cb9-29">        method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L-BFGS-B'</span></span>
<span id="cb9-30">    )</span>
<span id="cb9-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> result.success:</span>
<span id="cb9-32">        profile_likelihood.append(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>result.fun)</span>
<span id="cb9-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-34">        profile_likelihood.append(np.nan)</span>
<span id="cb9-35"></span>
<span id="cb9-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du seuil pour les IC</span></span>
<span id="cb9-37">chi2_threshold <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_likelihood_max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> chi2.ppf(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb9-38"></span>
<span id="cb9-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Déterminer les bornes des IC</span></span>
<span id="cb9-40">profile_likelihood <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(profile_likelihood)</span>
<span id="cb9-41">valid_points <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(profile_likelihood <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> chi2_threshold)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb9-42"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(valid_points) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb9-43">    lower_bound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> shape_grid[valid_points[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]]</span>
<span id="cb9-44">    upper_bound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> shape_grid[valid_points[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb9-45">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"IC profilé pour shape: [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>lower_bound<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>upper_bound<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span>
<span id="cb9-46"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-47">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Impossible de déterminer des IC profilés avec les paramètres actuels."</span>)</span>
<span id="cb9-48"></span>
<span id="cb9-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tracé du profil de log-vraisemblance</span></span>
<span id="cb9-50">plt.plot(shape_grid, profile_likelihood, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Log-likelihood"</span>)</span>
<span id="cb9-51">plt.axhline(chi2_threshold, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% Confidence threshold"</span>)</span>
<span id="cb9-52">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shape"</span>)</span>
<span id="cb9-53">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profile log-likelihood"</span>)</span>
<span id="cb9-54">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profile log-likelihood for shape parameter"</span>)</span>
<span id="cb9-55">plt.legend()</span>
<span id="cb9-56">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>IC profilé pour shape: [-0.287, -0.042]</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-8-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="a.-validation-ex-ante" class="level4">
<h4 class="anchored" data-anchor-id="a.-validation-ex-ante">a. Validation ex-ante</h4>
<p>On remarque la loi GEV estimée par une weibull semble coller à la distribution des rendements extrêmes du CAC 40. De plus, en utilisant un QQ-plot, nous constatons que les quantiles théoriques de la GEV-Weibull et empiriques sembelnt alignés sauf pour les quantiles élévés où l’on constate un décrochage.</p>
<div id="a28cdc63" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb11-2">plt.hist(extremes, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Maxima observées'</span>)</span>
<span id="cb11-3"></span>
<span id="cb11-4">xmin, xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.xlim()</span>
<span id="cb11-5">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(xmin, xmax, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb11-6">p_gev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gev.pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gev)</span>
<span id="cb11-7">plt.plot(x, p_gev, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'GEV ajustée'</span>)</span>
<span id="cb11-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ajustement de la distribution GEV"</span>)</span>
<span id="cb11-9">plt.legend()</span>
<span id="cb11-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="c14cfe3d" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">niveaux_quantiles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb12-2">quantiles_empiriques_TVE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.quantile(extremes, niveaux_quantiles) </span>
<span id="cb12-3">quantiles_theoriques_GEV <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gev.ppf(niveaux_quantiles, shape, loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scale)</span>
<span id="cb12-4"></span>
<span id="cb12-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb12-6">plt.scatter(quantiles_theoriques_GEV, quantiles_empiriques_TVE)</span>
<span id="cb12-7">plt.plot(quantiles_theoriques_GEV, quantiles_theoriques_GEV, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dashed'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Première bissectrice'</span>)</span>
<span id="cb12-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QQ Plot d'une modélisation par loi GEV"</span>)</span>
<span id="cb12-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles théoriques (Loi GEV)'</span>)</span>
<span id="cb12-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles empiriques'</span>)</span>
<span id="cb12-11">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-12">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-10-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="b.-calcul-de-la-var-tve-par-mb" class="level4">
<h4 class="anchored" data-anchor-id="b.-calcul-de-la-var-tve-par-mb">b. Calcul de la VaR TVE par MB</h4>
<p>Pour calculer la VaR TVE pour un horizon de 1jour par MB, nous utilisons la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVaR%7D_h(%5Calpha_%7B%5Ctext%7BVaR%7D%7D)%20=%20G%5E%7B-1%7D_%7B(%5Chat%20%5Cmu,%20%5Chat%20%5Csigma,%20%5Chat%20%5Cxi)%7D(%5Calpha_%7B%5Ctext%7BGEV%7D%7D),%0A"></p>
<p>où G est la fonction de répartition de la GEV<img src="https://latex.codecogs.com/png.latex?(%5Chat%20%5Cmu,%20%5Chat%20%5Csigma,%20%5Chat%20%5Cxi)"> estimée, et <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Ctext%7BGEV%7D%7D"> est ajusté pour correspondre à l’horizon temporel de la VaR via la relation :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7B1-%5Calpha_%7B%5Ctext%7BVaR%7D%7D%7D%20=%20s%20%5Ctimes%20%5Cfrac%7B1%7D%7B1-%5Calpha_%7B%5Ctext%7BGEV%7D%7D%7D.%0A"></p>
<p>Pour convertir la VaR à horizon 1jour en VaR à horizon T jours, la méthode de scaling soulève quelques questions, car elle repose essentiellement sur la normalité et l’indépendance des rendements ce qui n’est pas le cas en pratique. De ce fait, nous utiliserons la méthode alternative reposant sur la théorie des valeurs extrêmes.</p>
<p>y revenir</p>
<div id="778b6ae0" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> BM_var(alpha,s,shape,loc,scale):</span>
<span id="cb13-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance de la VaR</span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    s : le nombre de jours dans un bloc</span></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb13-7">    alpha_bm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha)</span>
<span id="cb13-8"></span>
<span id="cb13-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gev.ppf(alpha_bm, shape, loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scale),alpha_bm</span>
<span id="cb13-10"></span>
<span id="cb13-11">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb13-12">var_BM_train,alpha_bm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BM_var(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, shape, loc, scale)</span>
<span id="cb13-13"></span>
<span id="cb13-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_BM_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb13-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE pour h=10j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>alpha_bm)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var_BM_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR TVE pour h=1j et alpha=0.99 est : 3.3274%
La VaR TVE pour h=10j et alpha=0.99 est : 20.5167%</code></pre>
</div>
</div>
</section>
</section>
<section id="i.1.3.-estimation-des-paramètres-de-la-loi-de-ev" class="level3">
<h3 class="anchored" data-anchor-id="i.1.3.-estimation-des-paramètres-de-la-loi-de-ev">I.1.3. Estimation des paramètres de la loi de EV</h3>
<p>Bien que l’intervalle de confiance à 95% pour le paramètre de forme <img src="https://latex.codecogs.com/png.latex?%5Cxi"> de <img src="https://latex.codecogs.com/png.latex?%5B-0.284,%20-0.039%5D"> ne contienne pas 0, nous avons tout de même estimé les paramètres de la loi EV pour comparer les résultats avec ceux de la loi GEV. En estimant tout de même les paramètres de la loi EV, nous obtenons les paramètres suivants : <img src="https://latex.codecogs.com/png.latex?%5Cmu=0.02,%20%5Csigma=0.01,%20%5Cxi=0">.</p>
<p>Nous constatons que la loi EV ne semble pas mal s’adapter à la distribution des rendements extrêmes du CAC 40.</p>
<div id="9235a30f" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> gumbel_r</span>
<span id="cb15-2"></span>
<span id="cb15-3">params_gumbel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gumbel_r.fit(extremes)</span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher les paramètres estimés</span></span>
<span id="cb15-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb15-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres estimés de la distribution GEV GUMBEL"</span>)</span>
<span id="cb15-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb15-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Loc (mu) =  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gumbel[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb15-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Scale (sigma) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gumbel[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb15-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>==================================================
Paramètres estimés de la distribution GEV GUMBEL
==================================================
Loc (mu) =  0.02
Scale (sigma) = 0.01
==================================================</code></pre>
</div>
</div>
<div id="e8a2ec52" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb17-2">plt.hist(extremes, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Maxima observées'</span>)</span>
<span id="cb17-3"></span>
<span id="cb17-4">xmin, xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.xlim()</span>
<span id="cb17-5">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(xmin, xmax, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité GEV ajustée</span></span>
<span id="cb17-8">p_gev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gev.pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gev)</span>
<span id="cb17-9">plt.plot(x, p_gev, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'GEV ajustée'</span>)</span>
<span id="cb17-10"></span>
<span id="cb17-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité Gumbel ajustée</span></span>
<span id="cb17-12">p_gumbel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gumbel_r.pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gumbel)</span>
<span id="cb17-13">plt.plot(x, p_gumbel, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Gumbel ajustée'</span>)</span>
<span id="cb17-14">title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparaison GEV vs Gumbel"</span></span>
<span id="cb17-15">plt.title(title)</span>
<span id="cb17-16">plt.legend()</span>
<span id="cb17-17">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="81a72935" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">quantiles_theoriques_Gumb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gumbel_r.ppf(niveaux_quantiles, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gumbel)</span>
<span id="cb18-2"></span>
<span id="cb18-3">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb18-4">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb18-5">plt.scatter(quantiles_theoriques_GEV, quantiles_empiriques_TVE)</span>
<span id="cb18-6">plt.plot(quantiles_theoriques_GEV, quantiles_theoriques_GEV, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dashed'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Première bissectrice'</span>)</span>
<span id="cb18-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QQ Plot d'une modélisation par loi GEV Weibull"</span>)</span>
<span id="cb18-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles théoriques'</span>)</span>
<span id="cb18-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles empiriques'</span>)</span>
<span id="cb18-10">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb18-11"></span>
<span id="cb18-12"></span>
<span id="cb18-13">plt.subplot(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-14">plt.scatter(quantiles_theoriques_Gumb, quantiles_empiriques_TVE)</span>
<span id="cb18-15">plt.plot(quantiles_theoriques_Gumb, quantiles_theoriques_Gumb, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dashed'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Première bissectrice'</span>)</span>
<span id="cb18-16">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QQ Plot d'une modélisation par loi Gumbel"</span>)</span>
<span id="cb18-17">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles théoriques'</span>)</span>
<span id="cb18-18">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles empiriques'</span>)</span>
<span id="cb18-19">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb18-20"></span>
<span id="cb18-21">plt.tight_layout()</span>
<span id="cb18-22">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-14-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="1111bad6" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb19-2">var_BM_train,alpha_bm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BM_var(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_gumbel[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>params_gumbel[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE Gumbel pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_BM_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE Gumbel pour h=10j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>alpha_bm)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var_BM_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR TVE Gumbel pour h=1j et alpha=0.99 est : 3.3513%
La VaR TVE Gumbel pour h=10j et alpha=0.99 est : 20.6638%</code></pre>
</div>
</div>
<p>De plus, les résultats en terme de VaR sont très proches entre les deux modèles.</p>
</section>
</section>
<section id="i.2.-var-tve-approche-peak-over-threshold" class="level2">
<h2 class="anchored" data-anchor-id="i.2.-var-tve-approche-peak-over-threshold">I.2. VaR TVE : Approche Peak over threshold</h2>
<section id="i.2.1.-choix-du-seuil-u" class="level3">
<h3 class="anchored" data-anchor-id="i.2.1.-choix-du-seuil-u">I.2.1. Choix du seuil u</h3>
<p>Cette méthode est basée sur la modélisation de la distribution des excès au-dessus d’un seuil élevé de log-rendement négatif (<img src="https://latex.codecogs.com/png.latex?u">), seuil déterminé de manière subjective à partir de l’analyse du mean residual life plot, en ajustant une distribution de Pareto généralisée (GPD). Dans le mean residual life plot, si les excès au-delà de 𝒖 suivent une loi GPD, alors le mean-excess plot a un comportement linéaire. On cherche alors la valeur du seuil $$ pour laquelle le mean-excess plot est linéaire. Nous ne privilégions pas les seuils <img src="https://latex.codecogs.com/png.latex?u"> élevés puisque la moyenne est faite sur peu d’observations.</p>
<p>Nous allons choisir un seuil <img src="https://latex.codecogs.com/png.latex?u%20=%200.03"> pour lequel le mean residual life plot est linéaire. Nous allons ensuite ajuster une distribution GPD pour les excès au-dessus de ce seuil.</p>
<div id="e61484c3" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb21-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb21-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm, genpareto</span>
<span id="cb21-4"></span>
<span id="cb21-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> mean_residual_life_plot(data, tlim<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, pscale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, nt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, conf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, return_values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>):</span>
<span id="cb21-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb21-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Trace le Mean Residual Life (MRL) Plot pour identifier un seuil optimal pour une GPD.</span></span>
<span id="cb21-8"></span>
<span id="cb21-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Paramètres :</span></span>
<span id="cb21-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - data : array-like, données d'entrée.</span></span>
<span id="cb21-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - tlim : tuple (min, max), limites des seuils (si None, calculé automatiquement).</span></span>
<span id="cb21-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - pscale : bool, si True, utilise des quantiles au lieu de valeurs absolues.</span></span>
<span id="cb21-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - nt : int, nombre de seuils à considérer.</span></span>
<span id="cb21-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - conf : float, niveau de confiance pour l'intervalle (ex: 0.95 pour 95%).</span></span>
<span id="cb21-15"></span>
<span id="cb21-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Retourne :</span></span>
<span id="cb21-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - Un graphique MRL avec l'intervalle de confiance.</span></span>
<span id="cb21-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb21-19"></span>
<span id="cb21-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Trier et filtrer les données</span></span>
<span id="cb21-21">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(data[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>np.isnan(data)])</span>
<span id="cb21-22">    nn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data)</span>
<span id="cb21-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> nn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>:</span>
<span id="cb21-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Les données contiennent trop peu de valeurs valides."</span>)</span>
<span id="cb21-25"></span>
<span id="cb21-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des seuils</span></span>
<span id="cb21-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> tlim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb21-28">        tlim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], data[nn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Évite les 4 plus grandes valeurs</span></span>
<span id="cb21-29"></span>
<span id="cb21-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]):</span>
<span id="cb21-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"La borne supérieure du seuil est trop élevée."</span>)</span>
<span id="cb21-32"></span>
<span id="cb21-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pscale:</span>
<span id="cb21-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Travailler en quantiles au lieu de valeurs absolues</span></span>
<span id="cb21-35">        tlim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (np.mean(data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), np.mean(data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb21-36">        pvec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], nt)</span>
<span id="cb21-37">        thresholds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.quantile(data, pvec)</span>
<span id="cb21-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb21-39">        thresholds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], tlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], nt)</span>
<span id="cb21-40"></span>
<span id="cb21-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialiser les résultats</span></span>
<span id="cb21-42">    mean_excess <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(nt)</span>
<span id="cb21-43">    lower_conf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(nt)</span>
<span id="cb21-44">    upper_conf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(nt)</span>
<span id="cb21-45"></span>
<span id="cb21-46">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du Mean Excess et de l'IC</span></span>
<span id="cb21-47">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(thresholds):</span>
<span id="cb21-48">        exceedances <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> u] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Excès au-dessus du seuil</span></span>
<span id="cb21-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(exceedances) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb21-50">            mean_excess[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.nan</span>
<span id="cb21-51">            lower_conf[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.nan</span>
<span id="cb21-52">            upper_conf[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.nan</span>
<span id="cb21-53">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">continue</span></span>
<span id="cb21-54">        </span>
<span id="cb21-55">        mean_excess[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(exceedances)</span>
<span id="cb21-56">        std_dev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(exceedances, ddof<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-57">        margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> norm.ppf((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> conf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> std_dev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.sqrt(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(exceedances))</span>
<span id="cb21-58">        </span>
<span id="cb21-59">        lower_conf[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_excess[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> margin</span>
<span id="cb21-60">        upper_conf[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_excess[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> margin</span>
<span id="cb21-61"></span>
<span id="cb21-62">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tracé du Mean Residual Life Plot</span></span>
<span id="cb21-63">    plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb21-64">    plt.plot(thresholds, mean_excess, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean Excess"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb21-65">    plt.fill_between(thresholds, lower_conf, upper_conf, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>conf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">% Confidence Interval"</span>)</span>
<span id="cb21-66">    plt.axhline(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb21-67">    plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> pscale <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Threshold Probability"</span>)</span>
<span id="cb21-68">    plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean Excess"</span>)</span>
<span id="cb21-69">    plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean Residual Life Plot"</span>)</span>
<span id="cb21-70">    plt.legend()</span>
<span id="cb21-71">    plt.show()</span>
<span id="cb21-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> return_values:</span>
<span id="cb21-73">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> thresholds, mean_excess, lower_conf, upper_conf</span>
<span id="cb21-74"></span>
<span id="cb21-75">mean_residual_life_plot(neg_data_train, tlim<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>])</span>
<span id="cb21-76"></span>
<span id="cb21-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># regarder quantile à 5%</span></span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-16-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="i.2.2.-estimation-des-paramètres-de-la-loi-gpd" class="level3">
<h3 class="anchored" data-anchor-id="i.2.2.-estimation-des-paramètres-de-la-loi-gpd">I.2.2. Estimation des paramètres de la loi GPD</h3>
<p>En estimant les paramètres de la loi GPD, nous utilisons la méthode du maximum de vraisemblance. Les paramètres estimés par maximisation de la fonction de vraisemblance sont les suivants <img src="https://latex.codecogs.com/png.latex?%5Cxi%20=%201.33,%20%5Cmu%20%5Capprox%200.00,%20%5Csigma=0.01">. De ce fait, la distribution de Pareto généralisée est adaptée pour modéliser les excès au-dessus du seuil <img src="https://latex.codecogs.com/png.latex?u%20=%200.03">.</p>
<div id="30468b6a" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb22-2">excess_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> neg_data_train <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> u]</span>
<span id="cb22-3"></span>
<span id="cb22-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> genpareto</span>
<span id="cb22-5"></span>
<span id="cb22-6">params_gpd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> genpareto.fit(excess_values)</span>
<span id="cb22-7"></span>
<span id="cb22-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher les paramètres estimés</span></span>
<span id="cb22-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paramètres estimés de la distribution GPD:"</span>)</span>
<span id="cb22-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Shape (xi) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb22-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Localisation (mu) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb22-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Echelle (sigma) = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>params_gpd[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Paramètres estimés de la distribution GPD:
Shape (xi) = 1.33
Localisation (mu) = 0.00
Echelle (sigma) = 0.01</code></pre>
</div>
</div>
</section>
<section id="i.2.3.-validation-ex-ante" class="level3">
<h3 class="anchored" data-anchor-id="i.2.3.-validation-ex-ante">I.2.3. Validation ex-ante</h3>
<p>En comparant la distribution GPD estimée et la distribution empirique des excès, nous constatons que la distribution ne semble pas correspondre. De plus, le QQ-plot estimé indique que les quantiles théoriques de la loi GPD sont beaucoup plus grands que les quantiles empiriques observés dans notre distribution des excès. Nous concluons que la distribution GPD n’est pas adaptée pour modéliser les excès au-dessus du seuil <img src="https://latex.codecogs.com/png.latex?u%20=%200.03">. Cela peut être dû à un mauvais choix du seuil <img src="https://latex.codecogs.com/png.latex?u">, une analyse plus aprofondie aurait été nécessaire pour choisir un seuil plus adapté.</p>
<div id="0f8baa49" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb24-2">plt.hist(excess_values, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Données observées des excès'</span>)</span>
<span id="cb24-3"></span>
<span id="cb24-4">xmin, xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.xlim()</span>
<span id="cb24-5">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(xmin, xmax, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb24-6"></span>
<span id="cb24-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité GPD ajustée</span></span>
<span id="cb24-8">p_gpd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> genpareto.pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gpd)</span>
<span id="cb24-9">plt.plot(x, p_gpd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'GPD ajustée'</span>)</span>
<span id="cb24-10"></span>
<span id="cb24-11">title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distribution GPD"</span></span>
<span id="cb24-12">plt.title(title)</span>
<span id="cb24-13">plt.legend()</span>
<span id="cb24-14">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-18-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="c1a7867c" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">niveaux_quantiles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb25-2">quantiles_empiriques_POT <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.quantile(excess_values, niveaux_quantiles)</span>
<span id="cb25-3">quantiles_theoriques_GDP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> genpareto.ppf(niveaux_quantiles, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gpd)</span>
<span id="cb25-4"></span>
<span id="cb25-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb25-6"></span>
<span id="cb25-7">plt.scatter(quantiles_theoriques_GDP, quantiles_empiriques_POT)</span>
<span id="cb25-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QQ Plot d'une modélisation par loi GPD"</span>)</span>
<span id="cb25-9">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles théoriques'</span>)</span>
<span id="cb25-10">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles empiriques'</span>)</span>
<span id="cb25-11">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb25-12">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt_files/figure-html/cell-19-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="i.2.3.-calcul-de-la-var-pot-par-pot" class="level3">
<h3 class="anchored" data-anchor-id="i.2.3.-calcul-de-la-var-pot-par-pot">I.2.3. Calcul de la VaR POT par POT</h3>
<p>La Value-at-Risk sur un horizon 1 jour et pour un niveau de confiance <img src="https://latex.codecogs.com/png.latex?%7B%5Calpha%7D"> est alors obtenue par la formule :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVaR%7D_h(%5Calpha)%20=%20%5Chat%7BH%7D_%7B(%5Chat%7B%5Csigma%7D,%20%5Chat%7B%5Cxi%7D)%20%7D(%5Calpha_%7B%5Ctext%7BPOT%7D%7D)%5E%7B-1%7D%20+%20u,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Chat%7BH%7D(%5Chat%7B%5Csigma%7D,%20%5Chat%7B%5Cxi%7D)"> est la fonction de répartition de la GPD(<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Csigma%7D,%5Chat%7B%5Cxi%7D">) estimée, <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Ctext%7BPOT%7D%7D"> est le quantile ajusté, nécessaire pour adapter le calcul de la VaR dans le cadre de la distribution GPD.</p>
<p>Comme on ne se concentre que sur l’échantillon des excès dans cette modélisation, l’estimation de la VaR à partir de la GPD ne doit pas se faire au niveau <img src="https://latex.codecogs.com/png.latex?%5Calpha">, mais à un niveau ajusté <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7B%5Ctext%7BPOT%7D%7D"> défini par la relation suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A1%20-%20%5Calpha_%7B%5Ctext%7BPOT%7D%7D%20=%20%5Cfrac%7Bn%7D%7BN_u%7D%20%5Ctimes%20(1%20-%20%5Calpha),%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?n"> représente le nombre total d’observations, <img src="https://latex.codecogs.com/png.latex?N_u"> correspond au nombre d’excès au-delà du seuil <img src="https://latex.codecogs.com/png.latex?u">,</p>
<div id="cb60b2f0" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> POT_var(data,alpha,u,shape,loc,scale):</span>
<span id="cb26-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb26-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb26-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance de la VaR</span></span>
<span id="cb26-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    s : le nombre de jours dans un bloc</span></span>
<span id="cb26-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb26-7">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data)</span>
<span id="cb26-8">    excess_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> u <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> u]</span>
<span id="cb26-9">    nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(excess_values)</span>
<span id="cb26-10"></span>
<span id="cb26-11">    alpha_pot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nu</span>
<span id="cb26-12"></span>
<span id="cb26-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> genpareto.ppf(alpha_pot, shape, loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loc, scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scale) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u,alpha_pot</span>
<span id="cb26-14"></span>
<span id="cb26-15">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb26-16">var_POT_train,alpha_pot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> POT_var(neg_data_train, alpha, u,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>params_gpd)</span>
<span id="cb26-17"></span>
<span id="cb26-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_POT_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb26-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR TVE pour h=10j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>alpha_pot)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var_POT_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR TVE pour h=1j et alpha=0.99 est : 4.3634%
La VaR TVE pour h=10j et alpha=0.99 est : 17.3572%</code></pre>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>risques-fin</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_evt.html</guid>
  <pubDate>Thu, 27 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>TP1:Méthodes traditionnelles de calcul de la VaR et Expected Shortfall (ES)</title>
  <dc:creator>Cheryl Kouadio</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques.html</link>
  <description><![CDATA[ 




<p>Ce TP est fait dans le but de modéliser la Value at Risk qui est une mesure de risque financier. La Value at Risk (VaR) est une mesure essentielle du risque de marché qui estime la perte potentielle maximale d’un portefeuille sur un horizon h donné, avec un certain niveau de confiance <img src="https://latex.codecogs.com/png.latex?%5Calpha">. La VaR est utilisée par les institutions financières et les gestionnaires de risques pour évaluer l’exposition aux pertes extrêmes et ajuster leurs stratégies d’investissement. Elle est définie comme suit :</p>
<p><img src="https://latex.codecogs.com/png.latex?VaR_%7B%5Calpha%7D(h)%20=%20inf%20%5C%7B%20l%20%5Cin%20%5Cmathbb%7BR%7D%20%7C%20P(PnL%20%5Cgeq%20-l)%20%5Cgeq%201%20-%20%5Calpha%20%5C%7D"></p>
<p>Le PnL représente le profit and loss, c’est-à-dire la variation de la valeur du portefeuille. Dans notre cas, nous utilserons les rendements logarithmiques des actifs financiers, qui est stationnaire, pour calculer la VaR. Les rendements logarithmiques sont calculés comme suit :</p>
<p><img src="https://latex.codecogs.com/png.latex?r_t%20=%20log(%5Cfrac%7BP_t%7D%7BP_%7Bt-1%7D%7D)%20%5Capprox%20%5Cfrac%7BP_t%20-%20P_%7Bt-1%7D%7D%7BP_%7Bt-1%7D%7D."></p>
<p>Cette mesure est préférée aux rendements simples car sa décomposition en termes additifs permet de mieux modéliser les variations de prix des actifs financiers. De plus, en supposant la distribution identique et indépendante des rendements, on peut facilement déterminer sa loi de probabilité et calculer la VaR.</p>
<p>Dans ce projet, nous explorerons plusieurs méthodes pour calculer la VaR, en tenant compte de la nature des rendements financiers et des hypothèses sous-jacentes :</p>
<ul>
<li><strong>Méthodes non paramétriques</strong> : VaR historique et VaR bootstrap.</li>
<li><strong>Méthodes paramétriques</strong>: VaR gaussienne et VaR basée sur une distribution Skew Student.</li>
<li><strong>Méthodes avancées</strong> : VaR avec pondération exponentielle EWMA et VaR calculée par diffusion d’actifs. En complément, nous calculerons également l’Expected Shortfall (ES), qui mesure la perte moyenne au-delà du seuil de la VaR. Cette mesure est plus robuste, car elle intègre les queues de distribution et est particulièrement utile pour évaluer les risques extrêmes.</li>
</ul>
<div id="c449f234" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des librairies</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> bootstrap</span>
<span id="cb1-8"></span>
<span id="cb1-9">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span></code></pre></div>
</div>
<div id="fb148365" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import des données du CAC 40</span></span>
<span id="cb2-2">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>,start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2008-01-01"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des rendements logarithmiques</span></span>
<span id="cb2-5">data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.log(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>].shift(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retirer la première ligne</span></span>
<span id="cb2-8">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dropna()</span>
<span id="cb2-9">data.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.columns.get_level_values(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="56992603" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">data.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th">Price</th>
<th data-quarto-table-cell-role="th">Close</th>
<th data-quarto-table-cell-role="th">High</th>
<th data-quarto-table-cell-role="th">Low</th>
<th data-quarto-table-cell-role="th">Open</th>
<th data-quarto-table-cell-role="th">Volume</th>
<th data-quarto-table-cell-role="th">log_return</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2008-01-03</td>
<td>5546.080078</td>
<td>5559.049805</td>
<td>5515.609863</td>
<td>5538.200195</td>
<td>117504500</td>
<td>-0.000771</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2008-01-04</td>
<td>5446.790039</td>
<td>5567.089844</td>
<td>5417.529785</td>
<td>5543.689941</td>
<td>162947200</td>
<td>-0.018065</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2008-01-07</td>
<td>5452.830078</td>
<td>5475.250000</td>
<td>5429.319824</td>
<td>5432.000000</td>
<td>181161800</td>
<td>0.001108</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2008-01-08</td>
<td>5495.669922</td>
<td>5533.930176</td>
<td>5471.140137</td>
<td>5476.509766</td>
<td>174857100</td>
<td>0.007826</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2008-01-09</td>
<td>5435.419922</td>
<td>5463.069824</td>
<td>5420.060059</td>
<td>5459.399902</td>
<td>198041500</td>
<td>-0.011024</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<section id="i.-data-wrangling" class="level1">
<h1>I. Data wrangling</h1>
<p>Le jeu de données est constitué de 6 variables, à savoir, le prix de clôture (Close), le volume des transactions (Volume), le prix d’ouverture (Open), le prix le plus bas (Low), le prix le plus élevé (High) et les rendements logarithmiques (log-return). Pour calculer les rendements sur lesquels nous nous sommes baser pour mesurer le risque du portefeuille, nous avons utilisé le prix de clôture. Il aurait été idéal de disposer des prix de clôture ajustés, i.e.&nbsp;les prix de clôture qui tiennent compte des dividendes et des éventuels splits. Cependant, en raison de l’indispobilité de ces données, nous avons utilisé les prix de clôture bruts.</p>
<p>Sur tout l’historique du CAC 40, nous n’observons aucune donnée manquante et aucune valeur aberrante (i.e.&nbsp;Prix de clôture négatif). Nous avons donc pu calculer les rendements logarithmiques sans problème.</p>
<div id="bade16ba" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">data.info()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;class 'pandas.core.frame.DataFrame'&gt;
DatetimeIndex: 4630 entries, 2008-01-03 to 2026-02-10
Data columns (total 6 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   Close       4630 non-null   float64
 1   High        4630 non-null   float64
 2   Low         4630 non-null   float64
 3   Open        4630 non-null   float64
 4   Volume      4630 non-null   int64  
 5   log_return  4630 non-null   float64
dtypes: float64(5), int64(1)
memory usage: 253.2 KB</code></pre>
</div>
</div>
<div id="93de6eb3" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">data.describe()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th">Price</th>
<th data-quarto-table-cell-role="th">Close</th>
<th data-quarto-table-cell-role="th">High</th>
<th data-quarto-table-cell-role="th">Low</th>
<th data-quarto-table-cell-role="th">Open</th>
<th data-quarto-table-cell-role="th">Volume</th>
<th data-quarto-table-cell-role="th">log_return</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">count</td>
<td>4630.000000</td>
<td>4630.000000</td>
<td>4630.000000</td>
<td>4630.000000</td>
<td>4.630000e+03</td>
<td>4630.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">mean</td>
<td>5159.397158</td>
<td>5191.392396</td>
<td>5122.058257</td>
<td>5158.404029</td>
<td>1.059394e+08</td>
<td>0.000088</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">std</td>
<td>1461.767094</td>
<td>1464.656510</td>
<td>1461.496378</td>
<td>1462.353717</td>
<td>5.310210e+07</td>
<td>0.013605</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">min</td>
<td>2519.290039</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000e+00</td>
<td>-0.130983</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">25%</td>
<td>3994.122559</td>
<td>4019.992432</td>
<td>3965.000000</td>
<td>3995.302490</td>
<td>6.965910e+07</td>
<td>-0.005908</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">50%</td>
<td>4917.965088</td>
<td>4952.119873</td>
<td>4880.074951</td>
<td>4923.284912</td>
<td>9.485980e+07</td>
<td>0.000465</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">75%</td>
<td>6231.207642</td>
<td>6266.062378</td>
<td>6175.047607</td>
<td>6217.902588</td>
<td>1.333100e+08</td>
<td>0.006684</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">max</td>
<td>8362.089844</td>
<td>8396.719727</td>
<td>8327.219727</td>
<td>8368.990234</td>
<td>5.312476e+08</td>
<td>0.105946</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="d513fa99" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check na</span></span>
<span id="cb8-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb8-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(data.isna().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>())</span>
<span id="cb8-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Price
Close         0
High          0
Low           0
Open          0
Volume        0
log_return    0
dtype: int64
================================================================================</code></pre>
</div>
</div>
<p>Sur tout l’historique du CAC40, i.e.&nbsp;entre 1990 jusqu’à récemment, nous avons enregistré 5 périodes de crise financière. Ces périodes sont les suivantes :</p>
<ul>
<li>1994 : Crise obligataire</li>
<li>2000 : Bulle Internet</li>
<li>2008 : Crise des subprimes</li>
<li>2011 : Crise de la dette souveraine</li>
<li>2020 : Crise du Covid-19</li>
</ul>
<p>Cela peut se réperer par les clusters de rendements observables sur le graphique ci-dessous. Comme on peut l’observer, ces clusters de volatilité sont généralement associés à des périodes où le rendement journalier chute brutalemment. Pour mesurer le risque de marché, il peut être utile de modéliser ces clusters de volatilité et de prendre en compte les queues de distribution à travers des modèles GARCH par exemple.</p>
<p>Pour modéliser la VaR comme mesure de risque, il serait utile également de prendre les événements extrêmes en considération. En effet, en observant la densité des rendements, on peut voir que la distribution des rendements est leptokurtique, c’est-à-dire que les queues de distribution sont plus épaisses que celles d’une distribution normale (kurtosis &gt; 3). Cela signifie que les événements extrêmes sont plus fréquents que ce que la distribution normale ne le suggère. De plus, le coefficient d’asytmmétrie est négatif, ce qui signifie que la distribution des rendements est asymétrique. Cela peut être problématique pour les méthodes paramétriques qui supposent, parfois, une distribution symétrique des rendements.</p>
<div id="5fc2d393" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">fig, ax1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb10-2"></span>
<span id="cb10-3">ax1.plot(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rendements logarithmiques'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb10-4">ax1.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date'</span>)</span>
<span id="cb10-5">ax1.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rendements logarithmiques'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb10-6">ax1.tick_params(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>, labelcolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb10-7"></span>
<span id="cb10-8">ax2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax1.twinx()</span>
<span id="cb10-9">ax2.plot(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Prix historiques'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb10-10">ax2.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Prix historiques'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb10-11">ax2.tick_params(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>, labelcolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb10-12"></span>
<span id="cb10-13">fig.legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>, bbox_to_anchor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.89</span>))</span>
<span id="cb10-14">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution des prix de clôture, rendements logarithmiques du CAC 40"</span>)</span>
<span id="cb10-15">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-8-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="a975d26a" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité des rendements logarithmiques</span></span>
<span id="cb11-2">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb11-3">data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>].plot(kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'kde'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb11-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Densité des rendements logarithmiques du CAC 40"</span>)</span>
<span id="cb11-5">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="e7065a40" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skewness</span></span>
<span id="cb12-2">skewness <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>].skew()</span>
<span id="cb12-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Skewness: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>skewness<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kurtosis</span></span>
<span id="cb12-6">kurtosis <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>].kurtosis()</span>
<span id="cb12-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Kurtosis: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>kurtosis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Skewness: -0.28
Kurtosis: 8.48</code></pre>
</div>
</div>
<div id="48ab4a9f" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Autocorrélation et pacf</span></span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.graphics.tsaplots <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> plot_acf, plot_pacf</span>
<span id="cb14-3"></span>
<span id="cb14-4">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb14-5"></span>
<span id="cb14-6">plot_acf(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>], lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax1)</span>
<span id="cb14-7">plot_pacf(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>], lags<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax2)</span>
<span id="cb14-8"></span>
<span id="cb14-9">plt.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Autocorrélation et PACF des rendements logarithmiques du CAC 40"</span>)</span>
<span id="cb14-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-11-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="f98cfdff" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check for stationarity</span></span>
<span id="cb15-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.tsa.stattools <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> adfuller</span>
<span id="cb15-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> adf_test(series, title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>):</span>
<span id="cb15-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb15-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Pass in a time series and an optional title, returns an Adataset report</span></span>
<span id="cb15-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb15-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Augmented Dickey-Fuller Test: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(title))</span>
<span id="cb15-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># .dropna() handles differenced data</span></span>
<span id="cb15-11">    result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> adfuller(series.dropna(),autolag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'AIC'</span>)</span>
<span id="cb15-12"></span>
<span id="cb15-13">    labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Adataset test statistic'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'# lags used'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'# observations'</span>]</span>
<span id="cb15-14">    out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.Series(result[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>labels)</span>
<span id="cb15-15"></span>
<span id="cb15-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> key,val <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> result[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>].items():</span>
<span id="cb15-17">        out[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'critical value (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)'</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(key)]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>val</span>
<span id="cb15-18"></span>
<span id="cb15-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># .to_string() removes the line "dtype: float64"</span></span>
<span id="cb15-20">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb15-21"></span>
<span id="cb15-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> result[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>:</span>
<span id="cb15-23">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Strong evidence against the null hypothesis"</span>)</span>
<span id="cb15-24">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reject the null hypothesis"</span>)</span>
<span id="cb15-25">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data has no unit root and is stationary"</span>)</span>
<span id="cb15-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb15-27">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weak evidence against the null hypothesis"</span>)</span>
<span id="cb15-28">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fail to reject the null hypothesis"</span>)</span>
<span id="cb15-29">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data has a unit root and is non-stationary"</span>)</span>
<span id="cb15-30"></span>
<span id="cb15-31"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb15-32">adf_test(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>], title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Prix de clôture'</span>)</span>
<span id="cb15-33"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb15-34">adf_test(data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log_return"</span>], title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Log-rendements'</span>)</span>
<span id="cb15-35"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Augmented Dickey-Fuller Test: Prix de clôture

Adataset test statistic      -0.664665
p-value                       0.855691
# lags used                   0.000000
# observations             4629.000000
critical value (1%)          -3.431763
critical value (5%)          -2.862165
critical value (10%)         -2.567102

Weak evidence against the null hypothesis
Fail to reject the null hypothesis
Data has a unit root and is non-stationary
================================================================================
Augmented Dickey-Fuller Test: Log-rendements

Adataset test statistic   -1.238290e+01
p-value                    4.982874e-23
# lags used                2.900000e+01
# observations             4.600000e+03
critical value (1%)       -3.431772e+00
critical value (5%)       -2.862169e+00
critical value (10%)      -2.567105e+00

Strong evidence against the null hypothesis
Reject the null hypothesis
Data has no unit root and is stationary
================================================================================</code></pre>
</div>
</div>
</section>
<section id="ii.-implémentation-de-la-var" class="level1">
<h1>II. Implémentation de la VaR</h1>
<p>On va prendre comme période d’apprentissage où la BCE fait baisser ses taux directeurs et comme période de (back-)test où la BCE fait augmenter ses taux directeurs. De ce fait, nous avons le découpage suivant : - Train : 15 octobre 2008 - 26 juillet 2022 - Test : 28 juillet 2022 - 11 juin 2024</p>
<p>On va implémenter les différentes méthodes de calcul de la VaR et de l’ES sur le jeu de données du CAC 40. On va comparer les performances de ces méthodes en utilisant la période de test. On s’attend à ce que les méthodes paramétriques soient moins performantes que les méthodes non paramétriques, car elles supposent une distribution des rendements qui n’est pas toujours vérifiée. De plus, les méthodes avancées, comme l’EWMA et la diffusion d’actifs, devraient être plus performantes que les méthodes non paramétriques, car elles prennent en compte la dynamique des rendements et les corrélations entre les actifs. En général, le backtest devrait être vérifié puisque la période de test ne comporte aucune crise financière.</p>
<div id="a1be9c32" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'15-10-2008'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'26-07-2022'</span>]</span>
<span id="cb17-2">data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span>
<span id="cb17-3"></span>
<span id="cb17-4">test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>]][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'27-07-2022'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11-06-2024'</span>]</span>
<span id="cb17-5">data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_return'</span>]</span></code></pre></div>
</div>
<section id="ii.1.-statistiques-descriptives" class="level2">
<h2 class="anchored" data-anchor-id="ii.1.-statistiques-descriptives">II.1. Statistiques descriptives</h2>
<p>Nous constatons que dans la période d’entrainement est plus volatile (std=1.39%). Cela s’explique par le fait que la période d’entrainement prend en compte deux crises majeures : la crise des subprimes et la crise du Covid-19. Cela peut également se voir à travers les clusters de volatilité observables à la suite de ces crises. Dans la période de test, aucun évènement majeur n’est observé, avec une plus faible volatilité observée (std=0.08%).</p>
<p>On s’attend à ce que la VaR entrainée sur la période d’entrainement performe très bien sur la période de test, mais on s’attend également à ce que la VaR entrainée sur la période de test performe moins bien sur la période d’entrainement.</p>
<div id="fac3bbd1" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb18-2">plt.plot(data_train, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Train'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'grey'</span>)</span>
<span id="cb18-3">plt.plot(data_test, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Test'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb18-4">plt.legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb18-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Données d'entrainement et de test"</span>)</span>
<span id="cb18-6">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-14-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="2561fe16" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">data_train.describe()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<pre><code>count    3523.000000
mean        0.000153
std         0.013953
min        -0.130983
25%        -0.006099
50%         0.000580
75%         0.006855
max         0.096169
Name: log_return, dtype: float64</code></pre>
</div>
</div>
<div id="3f4ec0fc" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">data_test.describe()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>count    586.000000
mean       0.000292
std        0.008947
min       -0.036484
25%       -0.004763
50%        0.000642
75%        0.005612
max        0.041504
Name: log_return, dtype: float64</code></pre>
</div>
</div>
</section>
<section id="ii.2.-var-non-paramétrique" class="level2">
<h2 class="anchored" data-anchor-id="ii.2.-var-non-paramétrique">II.2. VaR non paramétrique</h2>
<p>La VaR est une mesure de risque qui donne une estimation de la perte maximale que l’on peut subir avec un certain niveau de confiance <img src="https://latex.codecogs.com/png.latex?%5Calpha"> sur un horizon de temps donné. Par exemple, une VaR à 5% sur 1 jour de 1000 euros signifie que 95% du temps, on ne perdra pas plus de 1000 euros sur un jour.</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7BLoss%7D%20%3C%20%5Ctext%7BVaR%7D)%20=%20%5Calpha."></p>
<p>On peut également raisonner en terme de gain, i.e.&nbsp;Profit and Loss (PnL).</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7BPnL%7D%20%3E%20-%20%5Ctext%7BVaR%7D)%20=%20%5Calpha."></p>
<p>La VaR peut se calculer suivant trois approches : 1. <strong>Approche historique</strong> : On se base sur les rendements passés selon l’horizon fixé pour estimer la VaR, à l’aide d’un quantile empirique d’ordre <img src="https://latex.codecogs.com/png.latex?%5Calpha">. Autrement, on peut se baser sur les rendements journaliers et utiliser la méthode de <strong>scaling</strong>. 2. <strong>Approche bootstrap</strong> : On tire aléatoirement des échantillons de rendements passés avec remise, puis on prend le quantile empirique d’ordre <img src="https://latex.codecogs.com/png.latex?%5Calpha"> pour calculer la VaR de chaque échantillon. La VaR finale est la moyenne des VaR obtenues.</p>
<section id="ii.2.1.-historique" class="level3">
<h3 class="anchored" data-anchor-id="ii.2.1.-historique">II.2.1. Historique</h3>
<div id="d02fc6bb" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : implémenter une fonction calculant la VaR historique</span></span>
<span id="cb23-2"></span>
<span id="cb23-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> historical_var(data, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>):</span>
<span id="cb23-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb23-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR historique</span></span>
<span id="cb23-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb23-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb23-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.percentile(data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span></code></pre></div>
</div>
<div id="b1b0fc65" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la VaR historique sur l'échantillon d'entrainement pour h=1j et alpha=0.99</span></span>
<span id="cb24-2">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb24-3">var_hist_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> historical_var(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb24-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR historique pour h=1j et alpha=0.99 est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_hist_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR historique pour h=1j et alpha=0.99 est : 4.0850%</code></pre>
</div>
</div>
<p>On constate que la VaR historique pour une horizon de 1 jour et un niveau de confiance de 99% est de -4,09%. De ce fait, la perte maximale que l’on peut subir avec un niveau de confiance de 99% sur un jour est de 4,09%. Autrement dit, il y a 1 chance sur 100 que la perte soit supérieure à 4,09%. Cette perte peut se produire 2 à 3 ans fois en une année (252 jours de trading).</p>
</section>
<section id="ii.2.2.-bootstrap" class="level3">
<h3 class="anchored" data-anchor-id="ii.2.2.-bootstrap">II.2.2. Bootstrap</h3>
<p>Pour l’implémentation de la VaR bootstrap, nous faisons le choix de faire un tirage de taille n=la taille de la série des rendements, avec remise. Ce choix est fait pour des raisons de simplicité. En ce qui concerne le choix du nombre d’échantillons, nous allons observer l’évolution de de l’estimation de la VaR en fonction du nombre d’échantillons. Nous limiterons à des échantillons compris entre 1000 et 10000, pour des raisons de temps computationnels, en ayant conscience que plus le nombre d’échantillons est grand, plus l’estimation de la VaR sera précise.</p>
<div id="5da3e799" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : implémenter une fonction calculant la VaR bootstrap et un IC</span></span>
<span id="cb26-2"></span>
<span id="cb26-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bootstrap_var(data, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, M<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, seuil<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>):</span>
<span id="cb26-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb26-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR bootstrap</span></span>
<span id="cb26-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb26-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance de la VaR</span></span>
<span id="cb26-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    n : le nombre de simulations</span></span>
<span id="cb26-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    seuil : le seuil de l'intervalle de confiance</span></span>
<span id="cb26-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb26-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set seed</span></span>
<span id="cb26-12">    np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb26-13"></span>
<span id="cb26-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation du vecteur des VaR</span></span>
<span id="cb26-15">    var <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(M)</span>
<span id="cb26-16"></span>
<span id="cb26-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la VaR bootstrap</span></span>
<span id="cb26-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(M):</span>
<span id="cb26-19">        sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.choice(data, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data), replace<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb26-20">        var[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.percentile(sample, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span>
<span id="cb26-21"></span>
<span id="cb26-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de l'intervalle de confiance</span></span>
<span id="cb26-23">    lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(var, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>seuil)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb26-24">    upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(var, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(seuil <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>seuil)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb26-25"></span>
<span id="cb26-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.mean(var), lower, upper</span></code></pre></div>
</div>
<div id="1a41bce3" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observer la variation de la VaR en fonction de M</span></span>
<span id="cb27-2">M_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb27-3">var_bs_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb27-4"></span>
<span id="cb27-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> M <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tqdm(M_values):</span>
<span id="cb27-6">    var_bs_train, _, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bootstrap_var(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha, M<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>M)</span>
<span id="cb27-7">    var_bs_values.append(var_bs_train)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>  0%|          | 0/900 [00:00&lt;?, ?it/s]  0%|          | 2/900 [00:00&lt;00:57, 15.54it/s]  0%|          | 4/900 [00:00&lt;00:58, 15.20it/s]  1%|          | 6/900 [00:00&lt;00:58, 15.25it/s]  1%|          | 8/900 [00:00&lt;00:58, 15.17it/s]  1%|          | 10/900 [00:00&lt;00:59, 15.02it/s]  1%|▏         | 12/900 [00:00&lt;01:00, 14.72it/s]  2%|▏         | 14/900 [00:00&lt;01:01, 14.52it/s]  2%|▏         | 16/900 [00:01&lt;01:01, 14.34it/s]  2%|▏         | 18/900 [00:01&lt;01:02, 14.16it/s]  2%|▏         | 20/900 [00:01&lt;01:03, 13.92it/s]  2%|▏         | 22/900 [00:01&lt;01:03, 13.73it/s]  3%|▎         | 24/900 [00:01&lt;01:05, 13.47it/s]  3%|▎         | 26/900 [00:01&lt;01:05, 13.29it/s]  3%|▎         | 28/900 [00:01&lt;01:06, 13.10it/s]  3%|▎         | 30/900 [00:02&lt;01:07, 12.88it/s]  4%|▎         | 32/900 [00:02&lt;01:08, 12.70it/s]  4%|▍         | 34/900 [00:02&lt;01:09, 12.52it/s]  4%|▍         | 36/900 [00:02&lt;01:10, 12.26it/s]  4%|▍         | 38/900 [00:02&lt;01:11, 12.07it/s]  4%|▍         | 40/900 [00:03&lt;01:12, 11.91it/s]  5%|▍         | 42/900 [00:03&lt;01:13, 11.72it/s]  5%|▍         | 44/900 [00:03&lt;01:14, 11.57it/s]  5%|▌         | 46/900 [00:03&lt;01:15, 11.38it/s]  5%|▌         | 48/900 [00:03&lt;01:15, 11.22it/s]  6%|▌         | 50/900 [00:03&lt;01:16, 11.05it/s]  6%|▌         | 52/900 [00:04&lt;01:17, 10.90it/s]  6%|▌         | 54/900 [00:04&lt;01:18, 10.75it/s]  6%|▌         | 56/900 [00:04&lt;01:19, 10.62it/s]  6%|▋         | 58/900 [00:04&lt;01:20, 10.47it/s]  7%|▋         | 60/900 [00:04&lt;01:21, 10.32it/s]  7%|▋         | 62/900 [00:05&lt;01:22, 10.18it/s]  7%|▋         | 64/900 [00:05&lt;01:23, 10.04it/s]  7%|▋         | 66/900 [00:05&lt;01:24,  9.92it/s]  7%|▋         | 67/900 [00:05&lt;01:24,  9.87it/s]  8%|▊         | 68/900 [00:05&lt;01:25,  9.78it/s]  8%|▊         | 69/900 [00:05&lt;01:25,  9.72it/s]  8%|▊         | 70/900 [00:05&lt;01:26,  9.63it/s]  8%|▊         | 71/900 [00:06&lt;01:26,  9.56it/s]  8%|▊         | 72/900 [00:06&lt;01:27,  9.50it/s]  8%|▊         | 73/900 [00:06&lt;01:27,  9.41it/s]  8%|▊         | 74/900 [00:06&lt;01:28,  9.36it/s]  8%|▊         | 75/900 [00:06&lt;01:28,  9.31it/s]  8%|▊         | 76/900 [00:06&lt;01:29,  9.24it/s]  9%|▊         | 77/900 [00:06&lt;01:29,  9.17it/s]  9%|▊         | 78/900 [00:06&lt;01:30,  9.12it/s]  9%|▉         | 79/900 [00:06&lt;01:30,  9.08it/s]  9%|▉         | 80/900 [00:07&lt;01:30,  9.01it/s]  9%|▉         | 81/900 [00:07&lt;01:31,  8.98it/s]  9%|▉         | 82/900 [00:07&lt;01:31,  8.90it/s]  9%|▉         | 83/900 [00:07&lt;01:32,  8.87it/s]  9%|▉         | 84/900 [00:07&lt;01:32,  8.82it/s]  9%|▉         | 85/900 [00:07&lt;01:32,  8.77it/s] 10%|▉         | 86/900 [00:07&lt;01:33,  8.70it/s] 10%|▉         | 87/900 [00:07&lt;01:34,  8.64it/s] 10%|▉         | 88/900 [00:07&lt;01:34,  8.59it/s] 10%|▉         | 89/900 [00:08&lt;01:34,  8.54it/s] 10%|█         | 90/900 [00:08&lt;01:35,  8.49it/s] 10%|█         | 91/900 [00:08&lt;01:35,  8.47it/s] 10%|█         | 92/900 [00:08&lt;01:35,  8.45it/s] 10%|█         | 93/900 [00:08&lt;01:35,  8.41it/s] 10%|█         | 94/900 [00:08&lt;01:36,  8.37it/s] 11%|█         | 95/900 [00:08&lt;01:36,  8.32it/s] 11%|█         | 96/900 [00:08&lt;01:37,  8.25it/s] 11%|█         | 97/900 [00:09&lt;01:37,  8.20it/s] 11%|█         | 98/900 [00:09&lt;01:38,  8.17it/s] 11%|█         | 99/900 [00:09&lt;01:38,  8.10it/s] 11%|█         | 100/900 [00:09&lt;01:38,  8.08it/s] 11%|█         | 101/900 [00:09&lt;01:39,  8.03it/s] 11%|█▏        | 102/900 [00:09&lt;01:39,  8.01it/s] 11%|█▏        | 103/900 [00:09&lt;01:39,  7.98it/s] 12%|█▏        | 104/900 [00:09&lt;01:40,  7.95it/s] 12%|█▏        | 105/900 [00:10&lt;01:40,  7.90it/s] 12%|█▏        | 106/900 [00:10&lt;01:40,  7.87it/s] 12%|█▏        | 107/900 [00:10&lt;01:41,  7.81it/s] 12%|█▏        | 108/900 [00:10&lt;01:42,  7.70it/s] 12%|█▏        | 109/900 [00:10&lt;01:43,  7.66it/s] 12%|█▏        | 110/900 [00:10&lt;01:43,  7.63it/s] 12%|█▏        | 111/900 [00:10&lt;01:44,  7.58it/s] 12%|█▏        | 112/900 [00:10&lt;01:44,  7.56it/s] 13%|█▎        | 113/900 [00:11&lt;01:45,  7.48it/s] 13%|█▎        | 114/900 [00:11&lt;01:45,  7.43it/s] 13%|█▎        | 115/900 [00:11&lt;01:49,  7.18it/s] 13%|█▎        | 116/900 [00:11&lt;01:48,  7.23it/s] 13%|█▎        | 117/900 [00:11&lt;01:47,  7.28it/s] 13%|█▎        | 118/900 [00:11&lt;01:48,  7.19it/s] 13%|█▎        | 119/900 [00:11&lt;01:48,  7.22it/s] 13%|█▎        | 120/900 [00:12&lt;01:48,  7.18it/s] 13%|█▎        | 121/900 [00:12&lt;01:48,  7.20it/s] 14%|█▎        | 122/900 [00:12&lt;01:48,  7.16it/s] 14%|█▎        | 123/900 [00:12&lt;01:49,  7.09it/s] 14%|█▍        | 124/900 [00:12&lt;01:49,  7.11it/s] 14%|█▍        | 125/900 [00:12&lt;01:50,  6.98it/s] 14%|█▍        | 126/900 [00:12&lt;01:50,  7.01it/s] 14%|█▍        | 127/900 [00:13&lt;01:51,  6.95it/s] 14%|█▍        | 128/900 [00:13&lt;01:51,  6.92it/s] 14%|█▍        | 129/900 [00:13&lt;01:53,  6.82it/s] 14%|█▍        | 130/900 [00:13&lt;01:53,  6.81it/s] 15%|█▍        | 131/900 [00:13&lt;01:52,  6.81it/s] 15%|█▍        | 132/900 [00:13&lt;01:53,  6.79it/s] 15%|█▍        | 133/900 [00:13&lt;01:52,  6.81it/s] 15%|█▍        | 134/900 [00:14&lt;01:53,  6.75it/s] 15%|█▌        | 135/900 [00:14&lt;01:53,  6.72it/s] 15%|█▌        | 136/900 [00:14&lt;01:54,  6.66it/s] 15%|█▌        | 137/900 [00:14&lt;01:56,  6.56it/s] 15%|█▌        | 138/900 [00:14&lt;01:55,  6.61it/s] 15%|█▌        | 139/900 [00:14&lt;01:56,  6.53it/s] 16%|█▌        | 140/900 [00:15&lt;01:56,  6.53it/s] 16%|█▌        | 141/900 [00:15&lt;01:55,  6.56it/s] 16%|█▌        | 142/900 [00:15&lt;01:56,  6.48it/s] 16%|█▌        | 143/900 [00:15&lt;01:56,  6.49it/s] 16%|█▌        | 144/900 [00:15&lt;01:57,  6.44it/s] 16%|█▌        | 145/900 [00:15&lt;01:57,  6.43it/s] 16%|█▌        | 146/900 [00:15&lt;01:57,  6.44it/s] 16%|█▋        | 147/900 [00:16&lt;01:58,  6.37it/s] 16%|█▋        | 148/900 [00:16&lt;01:58,  6.33it/s] 17%|█▋        | 149/900 [00:16&lt;02:02,  6.11it/s] 17%|█▋        | 150/900 [00:16&lt;02:03,  6.09it/s] 17%|█▋        | 151/900 [00:16&lt;02:01,  6.15it/s] 17%|█▋        | 152/900 [00:16&lt;02:00,  6.21it/s] 17%|█▋        | 153/900 [00:17&lt;02:01,  6.12it/s] 17%|█▋        | 154/900 [00:17&lt;02:01,  6.14it/s] 17%|█▋        | 155/900 [00:17&lt;02:00,  6.19it/s] 17%|█▋        | 156/900 [00:17&lt;02:00,  6.20it/s] 17%|█▋        | 157/900 [00:17&lt;02:01,  6.13it/s] 18%|█▊        | 158/900 [00:17&lt;02:00,  6.14it/s] 18%|█▊        | 159/900 [00:18&lt;02:00,  6.16it/s] 18%|█▊        | 160/900 [00:18&lt;02:03,  5.99it/s] 18%|█▊        | 161/900 [00:18&lt;02:03,  5.97it/s] 18%|█▊        | 162/900 [00:18&lt;02:03,  5.98it/s] 18%|█▊        | 163/900 [00:18&lt;02:02,  6.02it/s] 18%|█▊        | 164/900 [00:18&lt;02:01,  6.05it/s] 18%|█▊        | 165/900 [00:19&lt;02:02,  6.02it/s] 18%|█▊        | 166/900 [00:19&lt;02:02,  5.99it/s] 19%|█▊        | 167/900 [00:19&lt;02:02,  6.00it/s] 19%|█▊        | 168/900 [00:19&lt;02:02,  6.00it/s] 19%|█▉        | 169/900 [00:19&lt;02:02,  5.99it/s] 19%|█▉        | 170/900 [00:19&lt;02:02,  5.98it/s] 19%|█▉        | 171/900 [00:20&lt;02:02,  5.97it/s] 19%|█▉        | 172/900 [00:20&lt;02:03,  5.92it/s] 19%|█▉        | 173/900 [00:20&lt;02:03,  5.89it/s] 19%|█▉        | 174/900 [00:20&lt;02:04,  5.85it/s] 19%|█▉        | 175/900 [00:20&lt;02:03,  5.85it/s] 20%|█▉        | 176/900 [00:21&lt;02:13,  5.42it/s] 20%|█▉        | 177/900 [00:21&lt;02:11,  5.49it/s] 20%|█▉        | 178/900 [00:21&lt;02:09,  5.56it/s] 20%|█▉        | 179/900 [00:21&lt;02:08,  5.60it/s] 20%|██        | 180/900 [00:21&lt;02:10,  5.53it/s] 20%|██        | 181/900 [00:21&lt;02:16,  5.27it/s] 20%|██        | 182/900 [00:22&lt;02:13,  5.38it/s] 20%|██        | 183/900 [00:22&lt;02:11,  5.46it/s] 20%|██        | 184/900 [00:22&lt;02:09,  5.52it/s] 21%|██        | 185/900 [00:22&lt;02:09,  5.51it/s] 21%|██        | 186/900 [00:22&lt;02:09,  5.51it/s] 21%|██        | 187/900 [00:23&lt;02:09,  5.51it/s] 21%|██        | 188/900 [00:23&lt;02:08,  5.53it/s] 21%|██        | 189/900 [00:23&lt;02:08,  5.53it/s] 21%|██        | 190/900 [00:23&lt;02:08,  5.53it/s] 21%|██        | 191/900 [00:23&lt;02:08,  5.53it/s] 21%|██▏       | 192/900 [00:23&lt;02:08,  5.49it/s] 21%|██▏       | 193/900 [00:24&lt;02:08,  5.48it/s] 22%|██▏       | 194/900 [00:24&lt;02:08,  5.48it/s] 22%|██▏       | 195/900 [00:24&lt;02:08,  5.47it/s] 22%|██▏       | 196/900 [00:24&lt;02:11,  5.35it/s] 22%|██▏       | 197/900 [00:24&lt;02:11,  5.35it/s] 22%|██▏       | 198/900 [00:25&lt;02:11,  5.35it/s] 22%|██▏       | 199/900 [00:25&lt;02:11,  5.35it/s] 22%|██▏       | 200/900 [00:25&lt;02:10,  5.36it/s] 22%|██▏       | 201/900 [00:25&lt;02:10,  5.36it/s] 22%|██▏       | 202/900 [00:25&lt;02:10,  5.35it/s] 23%|██▎       | 203/900 [00:25&lt;02:10,  5.34it/s] 23%|██▎       | 204/900 [00:26&lt;02:10,  5.33it/s] 23%|██▎       | 205/900 [00:26&lt;02:14,  5.16it/s] 23%|██▎       | 206/900 [00:26&lt;02:17,  5.05it/s] 23%|██▎       | 207/900 [00:26&lt;02:16,  5.07it/s] 23%|██▎       | 208/900 [00:26&lt;02:15,  5.11it/s] 23%|██▎       | 209/900 [00:27&lt;02:15,  5.12it/s] 23%|██▎       | 210/900 [00:27&lt;02:14,  5.11it/s] 23%|██▎       | 211/900 [00:27&lt;02:14,  5.13it/s] 24%|██▎       | 212/900 [00:27&lt;02:22,  4.83it/s] 24%|██▎       | 213/900 [00:27&lt;02:22,  4.84it/s] 24%|██▍       | 214/900 [00:28&lt;02:19,  4.91it/s] 24%|██▍       | 215/900 [00:28&lt;02:23,  4.77it/s] 24%|██▍       | 216/900 [00:28&lt;02:25,  4.69it/s] 24%|██▍       | 217/900 [00:28&lt;02:23,  4.75it/s] 24%|██▍       | 218/900 [00:29&lt;02:24,  4.72it/s] 24%|██▍       | 219/900 [00:29&lt;02:25,  4.67it/s] 24%|██▍       | 220/900 [00:29&lt;02:23,  4.74it/s] 25%|██▍       | 221/900 [00:29&lt;02:20,  4.82it/s] 25%|██▍       | 222/900 [00:29&lt;02:31,  4.47it/s] 25%|██▍       | 223/900 [00:30&lt;02:28,  4.55it/s] 25%|██▍       | 224/900 [00:30&lt;02:25,  4.63it/s] 25%|██▌       | 225/900 [00:30&lt;02:33,  4.41it/s] 25%|██▌       | 226/900 [00:30&lt;02:31,  4.44it/s] 25%|██▌       | 227/900 [00:31&lt;02:27,  4.55it/s] 25%|██▌       | 228/900 [00:31&lt;02:25,  4.63it/s] 25%|██▌       | 229/900 [00:31&lt;02:23,  4.69it/s] 26%|██▌       | 230/900 [00:31&lt;02:29,  4.48it/s] 26%|██▌       | 231/900 [00:31&lt;02:28,  4.51it/s] 26%|██▌       | 232/900 [00:32&lt;02:25,  4.59it/s] 26%|██▌       | 233/900 [00:32&lt;02:23,  4.65it/s] 26%|██▌       | 234/900 [00:32&lt;02:21,  4.70it/s] 26%|██▌       | 235/900 [00:32&lt;02:20,  4.73it/s] 26%|██▌       | 236/900 [00:32&lt;02:19,  4.75it/s] 26%|██▋       | 237/900 [00:33&lt;02:20,  4.72it/s] 26%|██▋       | 238/900 [00:33&lt;02:28,  4.45it/s] 27%|██▋       | 239/900 [00:33&lt;02:27,  4.48it/s] 27%|██▋       | 240/900 [00:33&lt;02:25,  4.55it/s] 27%|██▋       | 241/900 [00:34&lt;02:30,  4.36it/s] 27%|██▋       | 242/900 [00:34&lt;02:30,  4.38it/s] 27%|██▋       | 243/900 [00:34&lt;02:27,  4.44it/s] 27%|██▋       | 244/900 [00:34&lt;02:25,  4.51it/s] 27%|██▋       | 245/900 [00:34&lt;02:24,  4.53it/s] 27%|██▋       | 246/900 [00:35&lt;02:24,  4.54it/s] 27%|██▋       | 247/900 [00:35&lt;02:22,  4.57it/s] 28%|██▊       | 248/900 [00:35&lt;02:22,  4.59it/s] 28%|██▊       | 249/900 [00:35&lt;02:21,  4.59it/s] 28%|██▊       | 250/900 [00:36&lt;02:21,  4.59it/s] 28%|██▊       | 251/900 [00:36&lt;02:25,  4.47it/s] 28%|██▊       | 252/900 [00:36&lt;02:33,  4.21it/s] 28%|██▊       | 253/900 [00:36&lt;02:32,  4.23it/s] 28%|██▊       | 254/900 [00:37&lt;02:29,  4.32it/s] 28%|██▊       | 255/900 [00:37&lt;02:28,  4.36it/s] 28%|██▊       | 256/900 [00:37&lt;02:27,  4.38it/s] 29%|██▊       | 257/900 [00:37&lt;02:26,  4.39it/s] 29%|██▊       | 258/900 [00:37&lt;02:32,  4.22it/s] 29%|██▉       | 259/900 [00:38&lt;02:37,  4.07it/s] 29%|██▉       | 260/900 [00:38&lt;02:34,  4.15it/s] 29%|██▉       | 261/900 [00:38&lt;02:30,  4.24it/s] 29%|██▉       | 262/900 [00:38&lt;02:35,  4.10it/s] 29%|██▉       | 263/900 [00:39&lt;02:37,  4.05it/s] 29%|██▉       | 264/900 [00:39&lt;02:34,  4.11it/s] 29%|██▉       | 265/900 [00:39&lt;02:37,  4.03it/s] 30%|██▉       | 266/900 [00:39&lt;02:36,  4.06it/s] 30%|██▉       | 267/900 [00:40&lt;02:32,  4.14it/s] 30%|██▉       | 268/900 [00:40&lt;02:34,  4.09it/s] 30%|██▉       | 269/900 [00:40&lt;02:39,  3.97it/s] 30%|███       | 270/900 [00:40&lt;02:36,  4.02it/s] 30%|███       | 271/900 [00:41&lt;02:38,  3.96it/s] 30%|███       | 272/900 [00:41&lt;02:40,  3.92it/s] 30%|███       | 273/900 [00:41&lt;02:35,  4.02it/s] 30%|███       | 274/900 [00:41&lt;02:33,  4.07it/s] 31%|███       | 275/900 [00:42&lt;02:31,  4.13it/s] 31%|███       | 276/900 [00:42&lt;02:30,  4.16it/s] 31%|███       | 277/900 [00:42&lt;02:39,  3.90it/s] 31%|███       | 278/900 [00:42&lt;02:37,  3.94it/s] 31%|███       | 279/900 [00:43&lt;02:34,  4.01it/s] 31%|███       | 280/900 [00:43&lt;02:32,  4.06it/s] 31%|███       | 281/900 [00:43&lt;02:30,  4.10it/s] 31%|███▏      | 282/900 [00:43&lt;02:29,  4.13it/s] 31%|███▏      | 283/900 [00:44&lt;02:37,  3.91it/s] 32%|███▏      | 284/900 [00:44&lt;02:36,  3.95it/s] 32%|███▏      | 285/900 [00:44&lt;02:33,  4.01it/s] 32%|███▏      | 286/900 [00:44&lt;02:31,  4.04it/s] 32%|███▏      | 287/900 [00:45&lt;02:33,  3.99it/s] 32%|███▏      | 288/900 [00:45&lt;02:36,  3.90it/s] 32%|███▏      | 289/900 [00:45&lt;02:34,  3.94it/s] 32%|███▏      | 290/900 [00:45&lt;02:32,  3.99it/s] 32%|███▏      | 291/900 [00:46&lt;02:45,  3.67it/s] 32%|███▏      | 292/900 [00:46&lt;02:41,  3.76it/s] 33%|███▎      | 293/900 [00:46&lt;02:38,  3.83it/s] 33%|███▎      | 294/900 [00:47&lt;02:39,  3.80it/s] 33%|███▎      | 295/900 [00:47&lt;02:41,  3.75it/s] 33%|███▎      | 296/900 [00:47&lt;02:38,  3.82it/s] 33%|███▎      | 297/900 [00:47&lt;02:35,  3.87it/s] 33%|███▎      | 298/900 [00:48&lt;02:33,  3.92it/s] 33%|███▎      | 299/900 [00:48&lt;02:41,  3.73it/s] 33%|███▎      | 300/900 [00:48&lt;02:39,  3.75it/s] 33%|███▎      | 301/900 [00:48&lt;02:36,  3.83it/s] 34%|███▎      | 302/900 [00:49&lt;02:44,  3.65it/s] 34%|███▎      | 303/900 [00:49&lt;02:43,  3.65it/s] 34%|███▍      | 304/900 [00:49&lt;02:39,  3.73it/s] 34%|███▍      | 305/900 [00:49&lt;02:40,  3.70it/s] 34%|███▍      | 306/900 [00:50&lt;02:37,  3.76it/s] 34%|███▍      | 307/900 [00:50&lt;02:39,  3.72it/s] 34%|███▍      | 308/900 [00:50&lt;02:43,  3.62it/s] 34%|███▍      | 309/900 [00:51&lt;02:40,  3.69it/s] 34%|███▍      | 310/900 [00:51&lt;02:37,  3.75it/s] 35%|███▍      | 311/900 [00:51&lt;02:38,  3.73it/s] 35%|███▍      | 312/900 [00:51&lt;02:36,  3.76it/s] 35%|███▍      | 313/900 [00:52&lt;02:35,  3.78it/s] 35%|███▍      | 314/900 [00:52&lt;02:40,  3.65it/s] 35%|███▌      | 315/900 [00:52&lt;02:42,  3.60it/s] 35%|███▌      | 316/900 [00:52&lt;02:39,  3.66it/s] 35%|███▌      | 317/900 [00:53&lt;02:42,  3.59it/s] 35%|███▌      | 318/900 [00:53&lt;02:41,  3.60it/s] 35%|███▌      | 319/900 [00:53&lt;02:38,  3.67it/s] 36%|███▌      | 320/900 [00:54&lt;02:37,  3.68it/s] 36%|███▌      | 321/900 [00:54&lt;02:35,  3.72it/s] 36%|███▌      | 322/900 [00:54&lt;02:34,  3.75it/s] 36%|███▌      | 323/900 [00:54&lt;02:39,  3.62it/s] 36%|███▌      | 324/900 [00:55&lt;02:40,  3.59it/s] 36%|███▌      | 325/900 [00:55&lt;02:38,  3.64it/s] 36%|███▌      | 326/900 [00:55&lt;02:36,  3.66it/s] 36%|███▋      | 327/900 [00:55&lt;02:35,  3.67it/s] 36%|███▋      | 328/900 [00:56&lt;02:34,  3.69it/s] 37%|███▋      | 329/900 [00:56&lt;02:34,  3.70it/s] 37%|███▋      | 330/900 [00:56&lt;02:33,  3.71it/s] 37%|███▋      | 331/900 [00:57&lt;02:35,  3.67it/s] 37%|███▋      | 332/900 [00:57&lt;02:34,  3.68it/s] 37%|███▋      | 333/900 [00:57&lt;02:34,  3.67it/s] 37%|███▋      | 334/900 [00:57&lt;02:41,  3.50it/s] 37%|███▋      | 335/900 [00:58&lt;02:39,  3.53it/s] 37%|███▋      | 336/900 [00:58&lt;02:38,  3.56it/s] 37%|███▋      | 337/900 [00:58&lt;02:37,  3.58it/s] 38%|███▊      | 338/900 [00:58&lt;02:37,  3.57it/s] 38%|███▊      | 339/900 [00:59&lt;02:41,  3.48it/s] 38%|███▊      | 340/900 [00:59&lt;02:39,  3.52it/s] 38%|███▊      | 341/900 [00:59&lt;02:44,  3.41it/s] 38%|███▊      | 342/900 [01:00&lt;02:41,  3.45it/s] 38%|███▊      | 343/900 [01:00&lt;02:46,  3.35it/s] 38%|███▊      | 344/900 [01:00&lt;02:46,  3.34it/s] 38%|███▊      | 345/900 [01:01&lt;02:42,  3.41it/s] 38%|███▊      | 346/900 [01:01&lt;02:50,  3.25it/s] 39%|███▊      | 347/900 [01:01&lt;02:48,  3.28it/s] 39%|███▊      | 348/900 [01:01&lt;02:44,  3.36it/s] 39%|███▉      | 349/900 [01:02&lt;02:49,  3.24it/s] 39%|███▉      | 350/900 [01:02&lt;02:46,  3.31it/s] 39%|███▉      | 351/900 [01:02&lt;02:42,  3.38it/s] 39%|███▉      | 352/900 [01:03&lt;02:40,  3.42it/s] 39%|███▉      | 353/900 [01:03&lt;02:38,  3.45it/s] 39%|███▉      | 354/900 [01:03&lt;02:37,  3.48it/s] 39%|███▉      | 355/900 [01:04&lt;02:36,  3.48it/s] 40%|███▉      | 356/900 [01:04&lt;02:35,  3.49it/s] 40%|███▉      | 357/900 [01:04&lt;02:38,  3.43it/s] 40%|███▉      | 358/900 [01:04&lt;02:37,  3.43it/s] 40%|███▉      | 359/900 [01:05&lt;02:43,  3.31it/s] 40%|████      | 360/900 [01:05&lt;02:44,  3.28it/s] 40%|████      | 361/900 [01:05&lt;02:41,  3.34it/s] 40%|████      | 362/900 [01:06&lt;02:39,  3.37it/s] 40%|████      | 363/900 [01:06&lt;02:45,  3.24it/s] 40%|████      | 364/900 [01:06&lt;02:47,  3.21it/s] 41%|████      | 365/900 [01:07&lt;02:43,  3.27it/s] 41%|████      | 366/900 [01:07&lt;02:41,  3.31it/s] 41%|████      | 367/900 [01:07&lt;02:39,  3.35it/s] 41%|████      | 368/900 [01:08&lt;02:47,  3.18it/s] 41%|████      | 369/900 [01:08&lt;02:43,  3.24it/s] 41%|████      | 370/900 [01:08&lt;02:49,  3.13it/s] 41%|████      | 371/900 [01:08&lt;02:44,  3.21it/s] 41%|████▏     | 372/900 [01:09&lt;02:42,  3.25it/s] 41%|████▏     | 373/900 [01:09&lt;02:49,  3.11it/s] 42%|████▏     | 374/900 [01:09&lt;02:47,  3.15it/s] 42%|████▏     | 375/900 [01:10&lt;02:43,  3.21it/s] 42%|████▏     | 376/900 [01:10&lt;02:41,  3.24it/s] 42%|████▏     | 377/900 [01:10&lt;02:39,  3.28it/s] 42%|████▏     | 378/900 [01:11&lt;02:38,  3.30it/s] 42%|████▏     | 379/900 [01:11&lt;02:36,  3.32it/s] 42%|████▏     | 380/900 [01:11&lt;02:38,  3.28it/s] 42%|████▏     | 381/900 [01:12&lt;02:37,  3.30it/s] 42%|████▏     | 382/900 [01:12&lt;02:36,  3.31it/s] 43%|████▎     | 383/900 [01:12&lt;02:36,  3.31it/s] 43%|████▎     | 384/900 [01:12&lt;02:35,  3.31it/s] 43%|████▎     | 385/900 [01:13&lt;02:36,  3.30it/s] 43%|████▎     | 386/900 [01:13&lt;02:35,  3.30it/s] 43%|████▎     | 387/900 [01:13&lt;02:35,  3.30it/s] 43%|████▎     | 388/900 [01:14&lt;02:35,  3.30it/s] 43%|████▎     | 389/900 [01:14&lt;02:35,  3.29it/s] 43%|████▎     | 390/900 [01:14&lt;02:34,  3.29it/s] 43%|████▎     | 391/900 [01:15&lt;02:41,  3.15it/s] 44%|████▎     | 392/900 [01:15&lt;02:40,  3.16it/s] 44%|████▎     | 393/900 [01:15&lt;02:39,  3.18it/s] 44%|████▍     | 394/900 [01:16&lt;02:38,  3.19it/s] 44%|████▍     | 395/900 [01:16&lt;02:38,  3.19it/s] 44%|████▍     | 396/900 [01:16&lt;02:44,  3.06it/s] 44%|████▍     | 397/900 [01:17&lt;02:44,  3.07it/s] 44%|████▍     | 398/900 [01:17&lt;02:41,  3.11it/s] 44%|████▍     | 399/900 [01:17&lt;02:49,  2.96it/s] 44%|████▍     | 400/900 [01:18&lt;02:46,  3.00it/s] 45%|████▍     | 401/900 [01:18&lt;02:49,  2.95it/s] 45%|████▍     | 402/900 [01:18&lt;02:46,  3.00it/s] 45%|████▍     | 403/900 [01:19&lt;02:43,  3.04it/s] 45%|████▍     | 404/900 [01:19&lt;02:50,  2.91it/s] 45%|████▌     | 405/900 [01:19&lt;02:46,  2.97it/s] 45%|████▌     | 406/900 [01:20&lt;02:43,  3.02it/s] 45%|████▌     | 407/900 [01:20&lt;02:41,  3.05it/s] 45%|████▌     | 408/900 [01:20&lt;02:40,  3.07it/s] 45%|████▌     | 409/900 [01:20&lt;02:38,  3.10it/s] 46%|████▌     | 410/900 [01:21&lt;02:39,  3.07it/s] 46%|████▌     | 411/900 [01:21&lt;02:38,  3.09it/s] 46%|████▌     | 412/900 [01:21&lt;02:37,  3.10it/s] 46%|████▌     | 413/900 [01:22&lt;02:37,  3.10it/s] 46%|████▌     | 414/900 [01:22&lt;02:36,  3.11it/s] 46%|████▌     | 415/900 [01:22&lt;02:35,  3.11it/s] 46%|████▌     | 416/900 [01:23&lt;02:35,  3.10it/s] 46%|████▋     | 417/900 [01:23&lt;02:35,  3.11it/s] 46%|████▋     | 418/900 [01:23&lt;02:35,  3.10it/s] 47%|████▋     | 419/900 [01:24&lt;02:35,  3.09it/s] 47%|████▋     | 420/900 [01:24&lt;02:35,  3.08it/s] 47%|████▋     | 421/900 [01:24&lt;02:35,  3.08it/s] 47%|████▋     | 422/900 [01:25&lt;02:34,  3.08it/s] 47%|████▋     | 423/900 [01:25&lt;02:35,  3.07it/s] 47%|████▋     | 424/900 [01:25&lt;02:35,  3.06it/s] 47%|████▋     | 425/900 [01:26&lt;02:36,  3.04it/s] 47%|████▋     | 426/900 [01:26&lt;02:36,  3.03it/s] 47%|████▋     | 427/900 [01:26&lt;02:36,  3.03it/s] 48%|████▊     | 428/900 [01:27&lt;02:36,  3.02it/s] 48%|████▊     | 429/900 [01:27&lt;02:37,  3.00it/s] 48%|████▊     | 430/900 [01:27&lt;02:36,  3.00it/s] 48%|████▊     | 431/900 [01:28&lt;02:36,  3.00it/s] 48%|████▊     | 432/900 [01:28&lt;02:36,  3.00it/s] 48%|████▊     | 433/900 [01:28&lt;02:35,  3.00it/s] 48%|████▊     | 434/900 [01:29&lt;02:35,  2.99it/s] 48%|████▊     | 435/900 [01:29&lt;02:35,  2.99it/s] 48%|████▊     | 436/900 [01:29&lt;02:35,  2.98it/s] 49%|████▊     | 437/900 [01:30&lt;02:34,  2.99it/s] 49%|████▊     | 438/900 [01:30&lt;02:35,  2.97it/s] 49%|████▉     | 439/900 [01:30&lt;02:35,  2.96it/s] 49%|████▉     | 440/900 [01:31&lt;02:35,  2.96it/s] 49%|████▉     | 441/900 [01:31&lt;02:35,  2.95it/s] 49%|████▉     | 442/900 [01:31&lt;02:35,  2.95it/s] 49%|████▉     | 443/900 [01:32&lt;02:35,  2.94it/s] 49%|████▉     | 444/900 [01:32&lt;02:35,  2.93it/s] 49%|████▉     | 445/900 [01:32&lt;02:35,  2.94it/s] 50%|████▉     | 446/900 [01:33&lt;02:34,  2.93it/s] 50%|████▉     | 447/900 [01:33&lt;02:35,  2.92it/s] 50%|████▉     | 448/900 [01:33&lt;02:36,  2.89it/s] 50%|████▉     | 449/900 [01:34&lt;02:38,  2.85it/s] 50%|█████     | 450/900 [01:34&lt;02:39,  2.83it/s] 50%|█████     | 451/900 [01:35&lt;02:39,  2.82it/s] 50%|█████     | 452/900 [01:35&lt;02:37,  2.84it/s] 50%|█████     | 453/900 [01:35&lt;02:36,  2.85it/s] 50%|█████     | 454/900 [01:36&lt;02:36,  2.86it/s] 51%|█████     | 455/900 [01:36&lt;02:35,  2.87it/s] 51%|█████     | 456/900 [01:36&lt;02:36,  2.84it/s] 51%|█████     | 457/900 [01:37&lt;02:35,  2.85it/s] 51%|█████     | 458/900 [01:37&lt;02:35,  2.85it/s] 51%|█████     | 459/900 [01:37&lt;02:34,  2.85it/s] 51%|█████     | 460/900 [01:38&lt;02:34,  2.85it/s] 51%|█████     | 461/900 [01:38&lt;02:34,  2.85it/s] 51%|█████▏    | 462/900 [01:38&lt;02:33,  2.85it/s] 51%|█████▏    | 463/900 [01:39&lt;02:33,  2.86it/s] 52%|█████▏    | 464/900 [01:39&lt;02:32,  2.86it/s] 52%|█████▏    | 465/900 [01:39&lt;02:33,  2.84it/s] 52%|█████▏    | 466/900 [01:40&lt;02:35,  2.78it/s] 52%|█████▏    | 467/900 [01:40&lt;02:35,  2.78it/s] 52%|█████▏    | 468/900 [01:41&lt;02:34,  2.79it/s] 52%|█████▏    | 469/900 [01:41&lt;02:43,  2.63it/s] 52%|█████▏    | 470/900 [01:41&lt;02:42,  2.65it/s] 52%|█████▏    | 471/900 [01:42&lt;02:39,  2.69it/s] 52%|█████▏    | 472/900 [01:42&lt;02:41,  2.65it/s] 53%|█████▎    | 473/900 [01:42&lt;02:40,  2.66it/s] 53%|█████▎    | 474/900 [01:43&lt;02:43,  2.61it/s] 53%|█████▎    | 475/900 [01:43&lt;02:40,  2.64it/s] 53%|█████▎    | 476/900 [01:44&lt;02:44,  2.58it/s] 53%|█████▎    | 477/900 [01:44&lt;02:41,  2.62it/s] 53%|█████▎    | 478/900 [01:44&lt;02:43,  2.59it/s] 53%|█████▎    | 479/900 [01:45&lt;02:44,  2.56it/s] 53%|█████▎    | 480/900 [01:45&lt;02:41,  2.60it/s] 53%|█████▎    | 481/900 [01:46&lt;02:46,  2.52it/s] 54%|█████▎    | 482/900 [01:46&lt;02:47,  2.50it/s] 54%|█████▎    | 483/900 [01:46&lt;02:42,  2.56it/s] 54%|█████▍    | 484/900 [01:47&lt;02:44,  2.52it/s] 54%|█████▍    | 485/900 [01:47&lt;02:42,  2.56it/s] 54%|█████▍    | 486/900 [01:48&lt;02:39,  2.60it/s] 54%|█████▍    | 487/900 [01:48&lt;02:36,  2.63it/s] 54%|█████▍    | 488/900 [01:48&lt;02:37,  2.61it/s] 54%|█████▍    | 489/900 [01:49&lt;02:35,  2.64it/s] 54%|█████▍    | 490/900 [01:49&lt;02:34,  2.66it/s] 55%|█████▍    | 491/900 [01:49&lt;02:33,  2.67it/s] 55%|█████▍    | 492/900 [01:50&lt;02:32,  2.68it/s] 55%|█████▍    | 493/900 [01:50&lt;02:35,  2.61it/s] 55%|█████▍    | 494/900 [01:51&lt;02:34,  2.63it/s] 55%|█████▌    | 495/900 [01:51&lt;02:33,  2.64it/s] 55%|█████▌    | 496/900 [01:51&lt;02:32,  2.65it/s] 55%|█████▌    | 497/900 [01:52&lt;02:31,  2.66it/s] 55%|█████▌    | 498/900 [01:52&lt;02:30,  2.67it/s] 55%|█████▌    | 499/900 [01:52&lt;02:30,  2.67it/s] 56%|█████▌    | 500/900 [01:53&lt;02:30,  2.66it/s] 56%|█████▌    | 501/900 [01:53&lt;02:29,  2.66it/s] 56%|█████▌    | 502/900 [01:54&lt;02:29,  2.65it/s] 56%|█████▌    | 503/900 [01:54&lt;02:29,  2.65it/s] 56%|█████▌    | 504/900 [01:54&lt;02:29,  2.65it/s] 56%|█████▌    | 505/900 [01:55&lt;02:28,  2.65it/s] 56%|█████▌    | 506/900 [01:55&lt;02:28,  2.65it/s] 56%|█████▋    | 507/900 [01:55&lt;02:28,  2.65it/s] 56%|█████▋    | 508/900 [01:56&lt;02:28,  2.64it/s] 57%|█████▋    | 509/900 [01:56&lt;02:28,  2.64it/s] 57%|█████▋    | 510/900 [01:57&lt;02:27,  2.64it/s] 57%|█████▋    | 511/900 [01:57&lt;02:28,  2.63it/s] 57%|█████▋    | 512/900 [01:57&lt;02:27,  2.63it/s] 57%|█████▋    | 513/900 [01:58&lt;02:27,  2.62it/s] 57%|█████▋    | 514/900 [01:58&lt;02:27,  2.62it/s] 57%|█████▋    | 515/900 [01:58&lt;02:27,  2.62it/s] 57%|█████▋    | 516/900 [01:59&lt;02:27,  2.60it/s] 57%|█████▋    | 517/900 [01:59&lt;02:27,  2.60it/s] 58%|█████▊    | 518/900 [02:00&lt;02:27,  2.59it/s] 58%|█████▊    | 519/900 [02:00&lt;02:27,  2.58it/s] 58%|█████▊    | 520/900 [02:00&lt;02:27,  2.58it/s] 58%|█████▊    | 521/900 [02:01&lt;02:26,  2.58it/s] 58%|█████▊    | 522/900 [02:01&lt;02:26,  2.58it/s] 58%|█████▊    | 523/900 [02:02&lt;02:26,  2.57it/s] 58%|█████▊    | 524/900 [02:02&lt;02:26,  2.57it/s] 58%|█████▊    | 525/900 [02:02&lt;02:26,  2.56it/s] 58%|█████▊    | 526/900 [02:03&lt;02:26,  2.56it/s] 59%|█████▊    | 527/900 [02:03&lt;02:25,  2.56it/s] 59%|█████▊    | 528/900 [02:04&lt;02:26,  2.55it/s] 59%|█████▉    | 529/900 [02:04&lt;02:25,  2.54it/s] 59%|█████▉    | 530/900 [02:04&lt;02:25,  2.54it/s] 59%|█████▉    | 531/900 [02:05&lt;02:25,  2.53it/s] 59%|█████▉    | 532/900 [02:05&lt;02:25,  2.54it/s] 59%|█████▉    | 533/900 [02:06&lt;02:25,  2.53it/s] 59%|█████▉    | 534/900 [02:06&lt;02:24,  2.52it/s] 59%|█████▉    | 535/900 [02:06&lt;02:24,  2.52it/s] 60%|█████▉    | 536/900 [02:07&lt;02:24,  2.52it/s] 60%|█████▉    | 537/900 [02:07&lt;02:24,  2.52it/s] 60%|█████▉    | 538/900 [02:08&lt;02:23,  2.52it/s] 60%|█████▉    | 539/900 [02:08&lt;02:23,  2.52it/s] 60%|██████    | 540/900 [02:08&lt;02:23,  2.51it/s] 60%|██████    | 541/900 [02:09&lt;02:22,  2.51it/s] 60%|██████    | 542/900 [02:09&lt;02:22,  2.51it/s] 60%|██████    | 543/900 [02:10&lt;02:22,  2.51it/s] 60%|██████    | 544/900 [02:10&lt;02:22,  2.49it/s] 61%|██████    | 545/900 [02:10&lt;02:23,  2.48it/s] 61%|██████    | 546/900 [02:11&lt;02:23,  2.47it/s] 61%|██████    | 547/900 [02:11&lt;02:25,  2.43it/s] 61%|██████    | 548/900 [02:12&lt;02:29,  2.36it/s] 61%|██████    | 549/900 [02:12&lt;02:27,  2.38it/s] 61%|██████    | 550/900 [02:12&lt;02:25,  2.40it/s] 61%|██████    | 551/900 [02:13&lt;02:25,  2.41it/s] 61%|██████▏   | 552/900 [02:13&lt;02:24,  2.41it/s] 61%|██████▏   | 553/900 [02:14&lt;02:23,  2.41it/s] 62%|██████▏   | 554/900 [02:14&lt;02:23,  2.41it/s] 62%|██████▏   | 555/900 [02:15&lt;02:22,  2.41it/s] 62%|██████▏   | 556/900 [02:15&lt;02:22,  2.41it/s] 62%|██████▏   | 557/900 [02:15&lt;02:22,  2.41it/s] 62%|██████▏   | 558/900 [02:16&lt;02:22,  2.40it/s] 62%|██████▏   | 559/900 [02:16&lt;02:22,  2.40it/s] 62%|██████▏   | 560/900 [02:17&lt;02:22,  2.39it/s] 62%|██████▏   | 561/900 [02:17&lt;02:21,  2.39it/s] 62%|██████▏   | 562/900 [02:17&lt;02:21,  2.39it/s] 63%|██████▎   | 563/900 [02:18&lt;02:21,  2.39it/s] 63%|██████▎   | 564/900 [02:18&lt;02:20,  2.39it/s] 63%|██████▎   | 565/900 [02:19&lt;02:20,  2.39it/s] 63%|██████▎   | 566/900 [02:19&lt;02:19,  2.39it/s] 63%|██████▎   | 567/900 [02:20&lt;02:19,  2.39it/s] 63%|██████▎   | 568/900 [02:20&lt;02:19,  2.37it/s] 63%|██████▎   | 569/900 [02:20&lt;02:19,  2.37it/s] 63%|██████▎   | 570/900 [02:21&lt;02:19,  2.37it/s] 63%|██████▎   | 571/900 [02:21&lt;02:19,  2.36it/s] 64%|██████▎   | 572/900 [02:22&lt;02:19,  2.36it/s] 64%|██████▎   | 573/900 [02:22&lt;02:19,  2.35it/s] 64%|██████▍   | 574/900 [02:23&lt;02:22,  2.29it/s] 64%|██████▍   | 575/900 [02:23&lt;02:21,  2.29it/s] 64%|██████▍   | 576/900 [02:23&lt;02:20,  2.31it/s] 64%|██████▍   | 577/900 [02:24&lt;02:18,  2.33it/s] 64%|██████▍   | 578/900 [02:24&lt;02:18,  2.33it/s] 64%|██████▍   | 579/900 [02:25&lt;02:18,  2.31it/s] 64%|██████▍   | 580/900 [02:25&lt;02:18,  2.31it/s] 65%|██████▍   | 581/900 [02:26&lt;02:17,  2.31it/s] 65%|██████▍   | 582/900 [02:26&lt;02:17,  2.32it/s] 65%|██████▍   | 583/900 [02:26&lt;02:16,  2.32it/s] 65%|██████▍   | 584/900 [02:27&lt;02:16,  2.31it/s] 65%|██████▌   | 585/900 [02:27&lt;02:16,  2.31it/s] 65%|██████▌   | 586/900 [02:28&lt;02:17,  2.29it/s] 65%|██████▌   | 587/900 [02:28&lt;02:16,  2.29it/s] 65%|██████▌   | 588/900 [02:29&lt;02:15,  2.30it/s] 65%|██████▌   | 589/900 [02:29&lt;02:15,  2.30it/s] 66%|██████▌   | 590/900 [02:29&lt;02:15,  2.29it/s] 66%|██████▌   | 591/900 [02:30&lt;02:14,  2.29it/s] 66%|██████▌   | 592/900 [02:30&lt;02:14,  2.29it/s] 66%|██████▌   | 593/900 [02:31&lt;02:15,  2.26it/s] 66%|██████▌   | 594/900 [02:31&lt;02:14,  2.27it/s] 66%|██████▌   | 595/900 [02:32&lt;02:14,  2.27it/s] 66%|██████▌   | 596/900 [02:32&lt;02:14,  2.27it/s] 66%|██████▋   | 597/900 [02:33&lt;02:15,  2.23it/s] 66%|██████▋   | 598/900 [02:33&lt;02:14,  2.24it/s] 67%|██████▋   | 599/900 [02:33&lt;02:13,  2.25it/s] 67%|██████▋   | 600/900 [02:34&lt;02:15,  2.22it/s] 67%|██████▋   | 601/900 [02:34&lt;02:13,  2.24it/s] 67%|██████▋   | 602/900 [02:35&lt;02:12,  2.24it/s] 67%|██████▋   | 603/900 [02:35&lt;02:16,  2.18it/s] 67%|██████▋   | 604/900 [02:36&lt;02:14,  2.20it/s] 67%|██████▋   | 605/900 [02:36&lt;02:15,  2.18it/s] 67%|██████▋   | 606/900 [02:37&lt;02:17,  2.14it/s] 67%|██████▋   | 607/900 [02:37&lt;02:16,  2.15it/s] 68%|██████▊   | 608/900 [02:38&lt;02:13,  2.18it/s] 68%|██████▊   | 609/900 [02:38&lt;02:11,  2.21it/s] 68%|██████▊   | 610/900 [02:39&lt;02:12,  2.19it/s] 68%|██████▊   | 611/900 [02:39&lt;02:11,  2.20it/s] 68%|██████▊   | 612/900 [02:40&lt;02:18,  2.07it/s] 68%|██████▊   | 613/900 [02:40&lt;02:18,  2.07it/s] 68%|██████▊   | 614/900 [02:40&lt;02:18,  2.07it/s] 68%|██████▊   | 615/900 [02:41&lt;02:14,  2.11it/s] 68%|██████▊   | 616/900 [02:41&lt;02:14,  2.12it/s] 69%|██████▊   | 617/900 [02:42&lt;02:12,  2.14it/s] 69%|██████▊   | 618/900 [02:42&lt;02:10,  2.16it/s] 69%|██████▉   | 619/900 [02:43&lt;02:09,  2.17it/s] 69%|██████▉   | 620/900 [02:43&lt;02:09,  2.17it/s] 69%|██████▉   | 621/900 [02:44&lt;02:08,  2.18it/s] 69%|██████▉   | 622/900 [02:44&lt;02:07,  2.18it/s] 69%|██████▉   | 623/900 [02:45&lt;02:07,  2.17it/s] 69%|██████▉   | 624/900 [02:45&lt;02:06,  2.18it/s] 69%|██████▉   | 625/900 [02:46&lt;02:05,  2.19it/s] 70%|██████▉   | 626/900 [02:46&lt;02:05,  2.18it/s] 70%|██████▉   | 627/900 [02:46&lt;02:04,  2.18it/s] 70%|██████▉   | 628/900 [02:47&lt;02:04,  2.19it/s] 70%|██████▉   | 629/900 [02:47&lt;02:03,  2.19it/s] 70%|███████   | 630/900 [02:48&lt;02:04,  2.17it/s] 70%|███████   | 631/900 [02:48&lt;02:03,  2.18it/s] 70%|███████   | 632/900 [02:49&lt;02:03,  2.18it/s] 70%|███████   | 633/900 [02:49&lt;02:02,  2.18it/s] 70%|███████   | 634/900 [02:50&lt;02:01,  2.18it/s] 71%|███████   | 635/900 [02:50&lt;02:01,  2.18it/s] 71%|███████   | 636/900 [02:51&lt;02:01,  2.17it/s] 71%|███████   | 637/900 [02:51&lt;02:01,  2.17it/s] 71%|███████   | 638/900 [02:52&lt;02:00,  2.17it/s] 71%|███████   | 639/900 [02:52&lt;02:00,  2.17it/s] 71%|███████   | 640/900 [02:52&lt;02:00,  2.16it/s] 71%|███████   | 641/900 [02:53&lt;02:00,  2.16it/s] 71%|███████▏  | 642/900 [02:53&lt;02:00,  2.15it/s] 71%|███████▏  | 643/900 [02:54&lt;02:00,  2.14it/s] 72%|███████▏  | 644/900 [02:54&lt;01:59,  2.14it/s] 72%|███████▏  | 645/900 [02:55&lt;01:59,  2.14it/s] 72%|███████▏  | 646/900 [02:55&lt;01:58,  2.14it/s] 72%|███████▏  | 647/900 [02:56&lt;01:58,  2.14it/s] 72%|███████▏  | 648/900 [02:56&lt;01:58,  2.13it/s] 72%|███████▏  | 649/900 [02:57&lt;01:57,  2.14it/s] 72%|███████▏  | 650/900 [02:57&lt;01:57,  2.13it/s] 72%|███████▏  | 651/900 [02:58&lt;01:56,  2.13it/s] 72%|███████▏  | 652/900 [02:58&lt;01:56,  2.13it/s] 73%|███████▎  | 653/900 [02:59&lt;01:56,  2.13it/s] 73%|███████▎  | 654/900 [02:59&lt;01:56,  2.12it/s] 73%|███████▎  | 655/900 [02:59&lt;01:55,  2.12it/s] 73%|███████▎  | 656/900 [03:00&lt;01:55,  2.11it/s] 73%|███████▎  | 657/900 [03:00&lt;01:54,  2.11it/s] 73%|███████▎  | 658/900 [03:01&lt;01:54,  2.11it/s] 73%|███████▎  | 659/900 [03:01&lt;01:54,  2.11it/s] 73%|███████▎  | 660/900 [03:02&lt;01:53,  2.11it/s] 73%|███████▎  | 661/900 [03:02&lt;01:53,  2.11it/s] 74%|███████▎  | 662/900 [03:03&lt;01:53,  2.10it/s] 74%|███████▎  | 663/900 [03:03&lt;01:52,  2.10it/s] 74%|███████▍  | 664/900 [03:04&lt;01:52,  2.10it/s] 74%|███████▍  | 665/900 [03:04&lt;01:52,  2.10it/s] 74%|███████▍  | 666/900 [03:05&lt;01:52,  2.09it/s] 74%|███████▍  | 667/900 [03:05&lt;01:51,  2.08it/s] 74%|███████▍  | 668/900 [03:06&lt;01:51,  2.08it/s] 74%|███████▍  | 669/900 [03:06&lt;01:51,  2.07it/s] 74%|███████▍  | 670/900 [03:07&lt;01:51,  2.07it/s] 75%|███████▍  | 671/900 [03:07&lt;01:50,  2.07it/s] 75%|███████▍  | 672/900 [03:08&lt;01:50,  2.07it/s] 75%|███████▍  | 673/900 [03:08&lt;01:49,  2.07it/s] 75%|███████▍  | 674/900 [03:09&lt;01:49,  2.06it/s] 75%|███████▌  | 675/900 [03:09&lt;01:49,  2.06it/s] 75%|███████▌  | 676/900 [03:10&lt;01:49,  2.05it/s] 75%|███████▌  | 677/900 [03:10&lt;01:48,  2.05it/s] 75%|███████▌  | 678/900 [03:11&lt;01:48,  2.05it/s] 75%|███████▌  | 679/900 [03:11&lt;01:47,  2.05it/s] 76%|███████▌  | 680/900 [03:12&lt;01:47,  2.05it/s] 76%|███████▌  | 681/900 [03:12&lt;01:46,  2.05it/s] 76%|███████▌  | 682/900 [03:13&lt;01:46,  2.04it/s] 76%|███████▌  | 683/900 [03:13&lt;01:46,  2.04it/s] 76%|███████▌  | 684/900 [03:13&lt;01:45,  2.04it/s] 76%|███████▌  | 685/900 [03:14&lt;01:45,  2.04it/s] 76%|███████▌  | 686/900 [03:14&lt;01:45,  2.03it/s] 76%|███████▋  | 687/900 [03:15&lt;01:44,  2.04it/s] 76%|███████▋  | 688/900 [03:15&lt;01:44,  2.04it/s] 77%|███████▋  | 689/900 [03:16&lt;01:44,  2.03it/s] 77%|███████▋  | 690/900 [03:16&lt;01:43,  2.03it/s] 77%|███████▋  | 691/900 [03:17&lt;01:43,  2.03it/s] 77%|███████▋  | 692/900 [03:17&lt;01:43,  2.02it/s] 77%|███████▋  | 693/900 [03:18&lt;01:43,  2.01it/s] 77%|███████▋  | 694/900 [03:18&lt;01:42,  2.01it/s] 77%|███████▋  | 695/900 [03:19&lt;01:42,  2.00it/s] 77%|███████▋  | 696/900 [03:19&lt;01:41,  2.00it/s] 77%|███████▋  | 697/900 [03:20&lt;01:41,  2.00it/s] 78%|███████▊  | 698/900 [03:20&lt;01:41,  2.00it/s] 78%|███████▊  | 699/900 [03:21&lt;01:40,  2.00it/s] 78%|███████▊  | 700/900 [03:21&lt;01:40,  2.00it/s] 78%|███████▊  | 701/900 [03:22&lt;01:39,  1.99it/s] 78%|███████▊  | 702/900 [03:22&lt;01:39,  1.99it/s] 78%|███████▊  | 703/900 [03:23&lt;01:38,  1.99it/s] 78%|███████▊  | 704/900 [03:23&lt;01:38,  1.99it/s] 78%|███████▊  | 705/900 [03:24&lt;01:38,  1.99it/s] 78%|███████▊  | 706/900 [03:24&lt;01:38,  1.98it/s] 79%|███████▊  | 707/900 [03:25&lt;01:37,  1.98it/s] 79%|███████▊  | 708/900 [03:25&lt;01:37,  1.97it/s] 79%|███████▉  | 709/900 [03:26&lt;01:36,  1.97it/s] 79%|███████▉  | 710/900 [03:27&lt;01:36,  1.97it/s] 79%|███████▉  | 711/900 [03:27&lt;01:35,  1.97it/s] 79%|███████▉  | 712/900 [03:28&lt;01:35,  1.97it/s] 79%|███████▉  | 713/900 [03:28&lt;01:34,  1.97it/s] 79%|███████▉  | 714/900 [03:29&lt;01:34,  1.97it/s] 79%|███████▉  | 715/900 [03:29&lt;01:34,  1.96it/s] 80%|███████▉  | 716/900 [03:30&lt;01:33,  1.96it/s] 80%|███████▉  | 717/900 [03:30&lt;01:33,  1.96it/s] 80%|███████▉  | 718/900 [03:31&lt;01:33,  1.95it/s] 80%|███████▉  | 719/900 [03:31&lt;01:32,  1.95it/s] 80%|████████  | 720/900 [03:32&lt;01:32,  1.94it/s] 80%|████████  | 721/900 [03:32&lt;01:32,  1.94it/s] 80%|████████  | 722/900 [03:33&lt;01:31,  1.95it/s] 80%|████████  | 723/900 [03:33&lt;01:30,  1.95it/s] 80%|████████  | 724/900 [03:34&lt;01:30,  1.94it/s] 81%|████████  | 725/900 [03:34&lt;01:30,  1.93it/s] 81%|████████  | 726/900 [03:35&lt;01:29,  1.93it/s] 81%|████████  | 727/900 [03:35&lt;01:29,  1.94it/s] 81%|████████  | 728/900 [03:36&lt;01:28,  1.94it/s] 81%|████████  | 729/900 [03:36&lt;01:28,  1.93it/s] 81%|████████  | 730/900 [03:37&lt;01:28,  1.93it/s] 81%|████████  | 731/900 [03:37&lt;01:27,  1.92it/s] 81%|████████▏ | 732/900 [03:38&lt;01:27,  1.92it/s] 81%|████████▏ | 733/900 [03:38&lt;01:27,  1.91it/s] 82%|████████▏ | 734/900 [03:39&lt;01:27,  1.90it/s] 82%|████████▏ | 735/900 [03:39&lt;01:26,  1.90it/s] 82%|████████▏ | 736/900 [03:40&lt;01:26,  1.90it/s] 82%|████████▏ | 737/900 [03:40&lt;01:25,  1.91it/s] 82%|████████▏ | 738/900 [03:41&lt;01:25,  1.90it/s] 82%|████████▏ | 739/900 [03:42&lt;01:24,  1.90it/s] 82%|████████▏ | 740/900 [03:42&lt;01:24,  1.90it/s] 82%|████████▏ | 741/900 [03:43&lt;01:23,  1.90it/s] 82%|████████▏ | 742/900 [03:43&lt;01:23,  1.90it/s] 83%|████████▎ | 743/900 [03:44&lt;01:22,  1.90it/s] 83%|████████▎ | 744/900 [03:44&lt;01:22,  1.90it/s] 83%|████████▎ | 745/900 [03:45&lt;01:21,  1.90it/s] 83%|████████▎ | 746/900 [03:45&lt;01:21,  1.89it/s] 83%|████████▎ | 747/900 [03:46&lt;01:21,  1.89it/s] 83%|████████▎ | 748/900 [03:46&lt;01:21,  1.87it/s] 83%|████████▎ | 749/900 [03:47&lt;01:20,  1.87it/s] 83%|████████▎ | 750/900 [03:47&lt;01:20,  1.87it/s] 83%|████████▎ | 751/900 [03:48&lt;01:19,  1.87it/s] 84%|████████▎ | 752/900 [03:48&lt;01:19,  1.87it/s] 84%|████████▎ | 753/900 [03:49&lt;01:18,  1.87it/s] 84%|████████▍ | 754/900 [03:50&lt;01:18,  1.87it/s] 84%|████████▍ | 755/900 [03:50&lt;01:17,  1.86it/s] 84%|████████▍ | 756/900 [03:51&lt;01:17,  1.86it/s] 84%|████████▍ | 757/900 [03:51&lt;01:16,  1.86it/s] 84%|████████▍ | 758/900 [03:52&lt;01:16,  1.86it/s] 84%|████████▍ | 759/900 [03:52&lt;01:15,  1.86it/s] 84%|████████▍ | 760/900 [03:53&lt;01:15,  1.86it/s] 85%|████████▍ | 761/900 [03:53&lt;01:14,  1.86it/s] 85%|████████▍ | 762/900 [03:54&lt;01:14,  1.85it/s] 85%|████████▍ | 763/900 [03:54&lt;01:13,  1.85it/s] 85%|████████▍ | 764/900 [03:55&lt;01:13,  1.85it/s] 85%|████████▌ | 765/900 [03:55&lt;01:13,  1.83it/s] 85%|████████▌ | 766/900 [03:56&lt;01:12,  1.84it/s] 85%|████████▌ | 767/900 [03:57&lt;01:12,  1.84it/s] 85%|████████▌ | 768/900 [03:57&lt;01:11,  1.84it/s] 85%|████████▌ | 769/900 [03:58&lt;01:12,  1.81it/s] 86%|████████▌ | 770/900 [03:58&lt;01:11,  1.82it/s] 86%|████████▌ | 771/900 [03:59&lt;01:11,  1.81it/s] 86%|████████▌ | 772/900 [03:59&lt;01:10,  1.81it/s] 86%|████████▌ | 773/900 [04:00&lt;01:10,  1.81it/s] 86%|████████▌ | 774/900 [04:00&lt;01:09,  1.81it/s] 86%|████████▌ | 775/900 [04:01&lt;01:08,  1.81it/s] 86%|████████▌ | 776/900 [04:02&lt;01:08,  1.81it/s] 86%|████████▋ | 777/900 [04:02&lt;01:07,  1.81it/s] 86%|████████▋ | 778/900 [04:03&lt;01:07,  1.81it/s] 87%|████████▋ | 779/900 [04:03&lt;01:06,  1.81it/s] 87%|████████▋ | 780/900 [04:04&lt;01:06,  1.80it/s] 87%|████████▋ | 781/900 [04:04&lt;01:05,  1.81it/s] 87%|████████▋ | 782/900 [04:05&lt;01:05,  1.80it/s] 87%|████████▋ | 783/900 [04:05&lt;01:04,  1.80it/s] 87%|████████▋ | 784/900 [04:06&lt;01:04,  1.80it/s] 87%|████████▋ | 785/900 [04:07&lt;01:03,  1.81it/s] 87%|████████▋ | 786/900 [04:07&lt;01:03,  1.80it/s] 87%|████████▋ | 787/900 [04:08&lt;01:02,  1.81it/s] 88%|████████▊ | 788/900 [04:08&lt;01:02,  1.80it/s] 88%|████████▊ | 789/900 [04:09&lt;01:01,  1.80it/s] 88%|████████▊ | 790/900 [04:09&lt;01:01,  1.80it/s] 88%|████████▊ | 791/900 [04:10&lt;01:00,  1.79it/s] 88%|████████▊ | 792/900 [04:10&lt;01:00,  1.79it/s] 88%|████████▊ | 793/900 [04:11&lt;00:59,  1.79it/s] 88%|████████▊ | 794/900 [04:12&lt;00:59,  1.79it/s] 88%|████████▊ | 795/900 [04:12&lt;00:58,  1.79it/s] 88%|████████▊ | 796/900 [04:13&lt;00:58,  1.79it/s] 89%|████████▊ | 797/900 [04:13&lt;00:57,  1.79it/s] 89%|████████▊ | 798/900 [04:14&lt;00:57,  1.79it/s] 89%|████████▉ | 799/900 [04:14&lt;00:56,  1.79it/s] 89%|████████▉ | 800/900 [04:15&lt;00:56,  1.78it/s] 89%|████████▉ | 801/900 [04:15&lt;00:55,  1.77it/s] 89%|████████▉ | 802/900 [04:16&lt;00:55,  1.77it/s] 89%|████████▉ | 803/900 [04:17&lt;00:54,  1.76it/s] 89%|████████▉ | 804/900 [04:17&lt;00:54,  1.76it/s] 89%|████████▉ | 805/900 [04:18&lt;00:53,  1.76it/s] 90%|████████▉ | 806/900 [04:18&lt;00:53,  1.76it/s] 90%|████████▉ | 807/900 [04:19&lt;00:52,  1.76it/s] 90%|████████▉ | 808/900 [04:19&lt;00:52,  1.76it/s] 90%|████████▉ | 809/900 [04:20&lt;00:51,  1.75it/s] 90%|█████████ | 810/900 [04:21&lt;00:51,  1.75it/s] 90%|█████████ | 811/900 [04:21&lt;00:50,  1.75it/s] 90%|█████████ | 812/900 [04:22&lt;00:50,  1.76it/s] 90%|█████████ | 813/900 [04:22&lt;00:49,  1.75it/s] 90%|█████████ | 814/900 [04:23&lt;00:49,  1.75it/s] 91%|█████████ | 815/900 [04:23&lt;00:48,  1.75it/s] 91%|█████████ | 816/900 [04:24&lt;00:48,  1.75it/s] 91%|█████████ | 817/900 [04:25&lt;00:47,  1.74it/s] 91%|█████████ | 818/900 [04:25&lt;00:46,  1.74it/s] 91%|█████████ | 819/900 [04:26&lt;00:46,  1.74it/s] 91%|█████████ | 820/900 [04:26&lt;00:45,  1.74it/s] 91%|█████████ | 821/900 [04:27&lt;00:45,  1.73it/s] 91%|█████████▏| 822/900 [04:27&lt;00:45,  1.73it/s] 91%|█████████▏| 823/900 [04:28&lt;00:44,  1.73it/s] 92%|█████████▏| 824/900 [04:29&lt;00:43,  1.73it/s] 92%|█████████▏| 825/900 [04:29&lt;00:43,  1.73it/s] 92%|█████████▏| 826/900 [04:30&lt;00:42,  1.73it/s] 92%|█████████▏| 827/900 [04:30&lt;00:42,  1.72it/s] 92%|█████████▏| 828/900 [04:31&lt;00:41,  1.72it/s] 92%|█████████▏| 829/900 [04:32&lt;00:41,  1.72it/s] 92%|█████████▏| 830/900 [04:32&lt;00:40,  1.72it/s] 92%|█████████▏| 831/900 [04:33&lt;00:40,  1.72it/s] 92%|█████████▏| 832/900 [04:33&lt;00:39,  1.72it/s] 93%|█████████▎| 833/900 [04:34&lt;00:39,  1.72it/s] 93%|█████████▎| 834/900 [04:34&lt;00:38,  1.71it/s] 93%|█████████▎| 835/900 [04:35&lt;00:37,  1.71it/s] 93%|█████████▎| 836/900 [04:36&lt;00:37,  1.71it/s] 93%|█████████▎| 837/900 [04:36&lt;00:36,  1.71it/s] 93%|█████████▎| 838/900 [04:37&lt;00:36,  1.71it/s] 93%|█████████▎| 839/900 [04:37&lt;00:35,  1.70it/s] 93%|█████████▎| 840/900 [04:38&lt;00:35,  1.71it/s] 93%|█████████▎| 841/900 [04:39&lt;00:34,  1.71it/s] 94%|█████████▎| 842/900 [04:39&lt;00:33,  1.71it/s] 94%|█████████▎| 843/900 [04:40&lt;00:33,  1.71it/s] 94%|█████████▍| 844/900 [04:40&lt;00:32,  1.70it/s] 94%|█████████▍| 845/900 [04:41&lt;00:32,  1.70it/s] 94%|█████████▍| 846/900 [04:41&lt;00:31,  1.70it/s] 94%|█████████▍| 847/900 [04:42&lt;00:31,  1.70it/s] 94%|█████████▍| 848/900 [04:43&lt;00:30,  1.70it/s] 94%|█████████▍| 849/900 [04:43&lt;00:30,  1.70it/s] 94%|█████████▍| 850/900 [04:44&lt;00:29,  1.69it/s] 95%|█████████▍| 851/900 [04:44&lt;00:29,  1.69it/s] 95%|█████████▍| 852/900 [04:45&lt;00:28,  1.68it/s] 95%|█████████▍| 853/900 [04:46&lt;00:28,  1.67it/s] 95%|█████████▍| 854/900 [04:46&lt;00:27,  1.66it/s] 95%|█████████▌| 855/900 [04:47&lt;00:27,  1.67it/s] 95%|█████████▌| 856/900 [04:47&lt;00:26,  1.66it/s] 95%|█████████▌| 857/900 [04:48&lt;00:25,  1.67it/s] 95%|█████████▌| 858/900 [04:49&lt;00:25,  1.67it/s] 95%|█████████▌| 859/900 [04:49&lt;00:24,  1.67it/s] 96%|█████████▌| 860/900 [04:50&lt;00:23,  1.67it/s] 96%|█████████▌| 861/900 [04:50&lt;00:23,  1.67it/s] 96%|█████████▌| 862/900 [04:51&lt;00:22,  1.67it/s] 96%|█████████▌| 863/900 [04:52&lt;00:22,  1.67it/s] 96%|█████████▌| 864/900 [04:52&lt;00:21,  1.66it/s] 96%|█████████▌| 865/900 [04:53&lt;00:21,  1.66it/s] 96%|█████████▌| 866/900 [04:53&lt;00:20,  1.65it/s] 96%|█████████▋| 867/900 [04:54&lt;00:19,  1.66it/s] 96%|█████████▋| 868/900 [04:55&lt;00:19,  1.65it/s] 97%|█████████▋| 869/900 [04:55&lt;00:18,  1.65it/s] 97%|█████████▋| 870/900 [04:56&lt;00:18,  1.65it/s] 97%|█████████▋| 871/900 [04:57&lt;00:17,  1.64it/s] 97%|█████████▋| 872/900 [04:57&lt;00:17,  1.65it/s] 97%|█████████▋| 873/900 [04:58&lt;00:16,  1.64it/s] 97%|█████████▋| 874/900 [04:58&lt;00:15,  1.64it/s] 97%|█████████▋| 875/900 [04:59&lt;00:15,  1.63it/s] 97%|█████████▋| 876/900 [05:00&lt;00:14,  1.64it/s] 97%|█████████▋| 877/900 [05:00&lt;00:14,  1.63it/s] 98%|█████████▊| 878/900 [05:01&lt;00:13,  1.63it/s] 98%|█████████▊| 879/900 [05:01&lt;00:12,  1.63it/s] 98%|█████████▊| 880/900 [05:02&lt;00:12,  1.63it/s] 98%|█████████▊| 881/900 [05:03&lt;00:11,  1.63it/s] 98%|█████████▊| 882/900 [05:03&lt;00:11,  1.62it/s] 98%|█████████▊| 883/900 [05:04&lt;00:10,  1.62it/s] 98%|█████████▊| 884/900 [05:05&lt;00:09,  1.62it/s] 98%|█████████▊| 885/900 [05:05&lt;00:09,  1.62it/s] 98%|█████████▊| 886/900 [05:06&lt;00:08,  1.62it/s] 99%|█████████▊| 887/900 [05:06&lt;00:08,  1.62it/s] 99%|█████████▊| 888/900 [05:07&lt;00:07,  1.62it/s] 99%|█████████▉| 889/900 [05:08&lt;00:06,  1.62it/s] 99%|█████████▉| 890/900 [05:08&lt;00:06,  1.62it/s] 99%|█████████▉| 891/900 [05:09&lt;00:05,  1.61it/s] 99%|█████████▉| 892/900 [05:09&lt;00:04,  1.61it/s] 99%|█████████▉| 893/900 [05:10&lt;00:04,  1.61it/s] 99%|█████████▉| 894/900 [05:11&lt;00:03,  1.61it/s] 99%|█████████▉| 895/900 [05:11&lt;00:03,  1.60it/s]100%|█████████▉| 896/900 [05:12&lt;00:02,  1.61it/s]100%|█████████▉| 897/900 [05:13&lt;00:01,  1.60it/s]100%|█████████▉| 898/900 [05:13&lt;00:01,  1.60it/s]100%|█████████▉| 899/900 [05:14&lt;00:00,  1.60it/s]100%|██████████| 900/900 [05:14&lt;00:00,  1.59it/s]100%|██████████| 900/900 [05:14&lt;00:00,  2.86it/s]</code></pre>
</div>
</div>
<div id="5e4f5922" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb29-2">plt.plot(M_values, var_bs_values, marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb29-3">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Nombre de simulations'</span>)</span>
<span id="cb29-4">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'VaR bootstrap'</span>)</span>
<span id="cb29-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation de la VaR bootstrap en fonction du nombre de simulations"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="20">
<pre><code>Text(0.5, 1.0, 'Variation de la VaR bootstrap en fonction du nombre de simulations')</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-21-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Pour la taille de l’echantillon bootstrap, nous allons prendre M=8000 étant donné que la courbe semble se stabiliser à partir de cette valeur. Avec ce choix, la VaR estimé est de 4.11%% avec un intervalle de confiance à 5% de [4.05%, 4.09%]. De plus, en ce qui concerne la VaR historique, nous constatons que l’estimation est contenu dans l’intervalle de confiance.</p>
<div id="67702b22" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">M<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8000</span></span>
<span id="cb31-2">var_bs_train, lower_ic,upper_ic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bootstrap_var(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha, M<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>M)</span>
<span id="cb31-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR bootstrap pour h=1j et alpha=0.99 est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_bs_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb31-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"L'intervalle de confiance est : [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>lower_ic<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>upper_ic<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR bootstrap pour h=1j et alpha=0.99 est : 4.1065%
L'intervalle de confiance est : [4.0536%, 4.0939%]</code></pre>
</div>
</div>
</section>
<section id="ii.2.3.-backtest" class="level3">
<h3 class="anchored" data-anchor-id="ii.2.3.-backtest">II.2.3. Backtest</h3>
<p>Pour l’exercice de backtest, il s’agit de : 1. Déterminer si la proportion <img src="https://latex.codecogs.com/png.latex?p"> de violations de la VaR est cohérente avec le niveau de confiance, i.e.&nbsp;égale à <img src="https://latex.codecogs.com/png.latex?1-%5Calpha">. Cela permet de vérifier si la mesure de risque est bien calibrée. Pour cela, nous pouvons avoir recours à un test de proportion ou un test de ratio de vraisemblance.</p>
<pre><code>**Unconditional coverage test** :
Soit I la variable indicatrice de violation de la VaR, i.e. $I=1$ si la perte est supérieure à la VaR, et $I=0$ sinon. La proportion de violations de la VaR est donnée par :

$$p = \frac{1}{n} \sum_{i=1}^{n} I_i = \frac{Z}{n}$$

Sous H0, i.e. p=1-$\alpha$, Z $\sim$ Binomiale(n, 1-$\alpha$). En supposant que n est suffisamment grand, on peut approximer Z par une loi normale. Ainsi donc :
$$\frac{Z - n (1-\alpha)}{\sqrt{\alpha (1-\alpha) n}} \sim \mathcal{N}(n(1-\alpha), n\alpha(1-\alpha))$$

Sous cette hypothèse asymptotique, on peut calculer la statistique du ratio de vraisemblance suivant :
$$LR = -2 ln \left( \frac{L(H1)}{L(H0)} \right) =-2 ln \left( 1- (1-\alpha))^{n-e}(1-\alpha)^e \right) + 2 ln \left( (1-\frac{e}{n})^{n-e} (\frac{e}{n})^e  \right)  \sim \chi^2(1)$$

où e est le nombre de violations de la VaR. On rejette H0 si LR &gt; $\chi^2(1-\alpha)$.

**Test de proportion** :
$$
H_0 : p = p_0 = 1-\alpha \\ H_1 : p &gt; 1-\alpha
$$
On peut également utiliser un test binomial pour tester si la proportion de violations de la VaR est égale à $1-\alpha$. On peut calculer la statistique du test suivant :
$$Z = \frac{p - p_0}{\sqrt{p_0 (1-p_0) / n}} \sim \mathcal{N}(0,1)$$

On rejette H0 si Z &gt; $p_0+ \phi^{-1}(1-\alpha) \sqrt{p_0 (1-p_0)/n}$, où $\phi$ est la quantile de la loi normale standard.</code></pre>
<ol start="2" type="1">
<li><p>Déterminer si, lorsqu’il y en a, les violations de VaR à deux différents jours sont indépendantes. Cela permet si la mesure de risque est capable de réagir aux chocs de marché affectant la volatilité des rendements. Pour cela, nous utilisons un conditional coverage test.</p>
<p><strong>Conditional coverage test</strong> : y revenir</p></li>
</ol>
<div id="954b7432" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb34-2"></span>
<span id="cb34-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : implémenter une fonction calculant le nombre d'exception sur l'échantillon test</span></span>
<span id="cb34-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> exceptions(data, var):</span>
<span id="cb34-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb34-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul du nombre d'exception</span></span>
<span id="cb34-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb34-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    var : la VaR</span></span>
<span id="cb34-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb34-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>var)</span>
<span id="cb34-11"></span>
<span id="cb34-12"></span>
<span id="cb34-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : test de proportion binomiale</span></span>
<span id="cb34-14"></span>
<span id="cb34-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binomial_test(n, p, p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>):</span>
<span id="cb34-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb34-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Test de proportion binomiale</span></span>
<span id="cb34-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    H0 : p = p0</span></span>
<span id="cb34-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    H1 : p &gt; p0</span></span>
<span id="cb34-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    n : le nombre d'essais</span></span>
<span id="cb34-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    p : la proportion</span></span>
<span id="cb34-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb34-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb34-24"></span>
<span id="cb34-25">    z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.sqrt(p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb34-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#reject_zone = p0 + stats.norm.ppf(1 - alpha) * np.sqrt(p0 * (1 - p0) / n)</span></span>
<span id="cb34-27">    p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> stats.norm.cdf(z)</span>
<span id="cb34-28">    reject <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> alpha</span>
<span id="cb34-29"></span>
<span id="cb34-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des IC</span></span>
<span id="cb34-31">    lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> stats.norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb34-32">    upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> stats.norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb34-33"></span>
<span id="cb34-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> p_value, reject, lower, upper</span>
<span id="cb34-35"></span>
<span id="cb34-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unconditionnal coverage test ==&gt; to do.</span></span></code></pre></div>
</div>
<div id="a660e3eb" class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Backtest de la VaR historique</span></span>
<span id="cb35-2"></span>
<span id="cb35-3">exceptions_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> exceptions(data_test, var_hist_train)</span>
<span id="cb35-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Le nombre d'exceptions sur l'échantillon de test est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exceptions_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb35-5"></span>
<span id="cb35-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb35-7">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test)</span>
<span id="cb35-8">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> exceptions_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb35-9">p_value, reject, lower,upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> binomial_test(n, p)</span>
<span id="cb35-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"H0 : le nombre d'exceptions est inférieur ou égale à </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb35-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"IC : [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>lower<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">,</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>upper<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span>
<span id="cb35-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La p-value du test de proportion binomiale est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb35-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Rejet de l'hypothèse nulle : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>reject<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb35-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Le nombre d'exceptions sur l'échantillon de test est : 0
================================================================================
H0 : le nombre d'exceptions est inférieur ou égale à 1.00%
IC : [0.00%,0.00%]
La p-value du test de proportion binomiale est : 0.9925
Rejet de l'hypothèse nulle : False
================================================================================</code></pre>
</div>
</div>
</section>
</section>
<section id="ii.3.-var-paramétrique" class="level2">
<h2 class="anchored" data-anchor-id="ii.3.-var-paramétrique">II.3. VaR paramétrique</h2>
<section id="ii.3.1.-validation-ex-ante" class="level3">
<h3 class="anchored" data-anchor-id="ii.3.1.-validation-ex-ante">II.3.1. Validation ex-ante</h3>
<p>Visuellement, les données ne semblent pas suivre une loi normale. En effet, les quantiles théoriques d’une loi normale ne collent pas avec les quantiles empiriques des rendements. Cela peut être dû à la présence de queues épaisses observables sur l’estimation de la densité des rendements sur l’échantillon d’apprentissage, de pics, de clusters de volatilité que nous avons observées plus haut. De plus, le skewness est négatif ce qui indique une asymétrie négative des rendements. Enfin, le kurtosis est supérieur à 3, ce qui indique une distribution leptokurtique des rendements.</p>
<p>Nous allons tout de même implémenter une VaR gaussienne pour voir comment elle se comporte dans le backtest.</p>
<div id="781daeeb" class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test visuel d'adéquation de la loi normale</span></span>
<span id="cb37-2"></span>
<span id="cb37-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Créer un Q-Q plot</span></span>
<span id="cb37-4">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb37-5">stats.probplot(data_train, dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"norm"</span>, plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax)</span>
<span id="cb37-6"></span>
<span id="cb37-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Personnalisation du graphique</span></span>
<span id="cb37-8">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q-Q Plot (Normal Distribution)"</span>)</span>
<span id="cb37-9">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Theoretical Quantiles"</span>)</span>
<span id="cb37-10">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample Quantiles"</span>)</span>
<span id="cb37-11"></span>
<span id="cb37-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Afficher le graphique</span></span>
<span id="cb37-13">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-25-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="8b0a95a8" class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité de l'echantillon train et l'échantillon de test</span></span>
<span id="cb38-2"></span>
<span id="cb38-3">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb38-4">data_train.plot(kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'kde'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Train'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb38-5">plt.legend(loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper left'</span>)</span>
<span id="cb38-6">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Densité de l'échantillon d'entrainement"</span>)</span>
<span id="cb38-7">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-26-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="7b671a3f" class="cell" data-execution_count="26">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skewness et kurtosis</span></span>
<span id="cb39-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb39-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skewness de l'échantillon d'entrainement : "</span>, data_train.skew())</span>
<span id="cb39-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kurtosis de l'échantillon d'entrainement : "</span>, data_train.kurt())</span>
<span id="cb39-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Skewness de l'échantillon d'entrainement :  -0.2981820421484688
Kurtosis de l'échantillon d'entrainement :  7.353960005618779
================================================================================</code></pre>
</div>
</div>
<div id="b9670dff" class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> kstest</span>
<span id="cb41-2"></span>
<span id="cb41-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test de Kolmogorov-Smirnov</span></span>
<span id="cb41-4">ks_stat, ks_p_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kstest(data_train, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'norm'</span>)</span>
<span id="cb41-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb41-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"H0 : Les données suivent une loi normale"</span>)</span>
<span id="cb41-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Statistique de test : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ks_stat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb41-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"P-value : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ks_p_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb41-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
H0 : Les données suivent une loi normale
Statistique de test : 0.4775
P-value : 0.0000
================================================================================</code></pre>
</div>
</div>
</section>
<section id="ii.3.2.-implémentation-de-la-var" class="level3">
<h3 class="anchored" data-anchor-id="ii.3.2.-implémentation-de-la-var">II.3.2. Implémentation de la VaR</h3>
<section id="a.-méthode-scaling" class="level4">
<h4 class="anchored" data-anchor-id="a.-méthode-scaling">a. Méthode scaling</h4>
<div id="1d8aff3c" class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : écrire une fonction qui calcule la VaR gaussienne</span></span>
<span id="cb43-2"></span>
<span id="cb43-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> gaussian_var(data, alpha):</span>
<span id="cb43-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb43-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne</span></span>
<span id="cb43-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb43-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb43-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb43-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span>
<span id="cb43-10"></span>
<span id="cb43-11">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(data)</span>
<span id="cb43-12">    sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(data)</span>
<span id="cb43-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span></code></pre></div>
</div>
<div id="7a456048" class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1">var_gauss_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gaussian_var(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb44-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR gaussienne pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_gauss_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb44-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR historique pour h=10j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var_gauss_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR gaussienne pour h=1j et alpha=0.99 est : 3.2302%
La VaR historique pour h=10j et alpha=0.99 est : 10.2148%</code></pre>
</div>
</div>
</section>
<section id="b.-méthode-de-diffusion-dun-actif" class="level4">
<h4 class="anchored" data-anchor-id="b.-méthode-de-diffusion-dun-actif">b. Méthode de diffusion d’un actif</h4>
<p>Pour calculer la <strong>VaR gaussienne à 10 jours</strong> par <strong>méthode de diffusion d’un actif</strong>, nous allons suivre les étapes suivantes :</p>
<p>L’évolution du prix d’un actif suit un processus de type mouvement brownien géométrique : <img src="https://latex.codecogs.com/png.latex?%0AdS_t%20=%20%5Cmu%20S_t%20dt%20+%20%5Csigma%20S_t%20dW_t%20%3C=%3E%20S_t%20=%20S_%7Bt-1%7D%20e%5E%7B(%5Cmu%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Csigma%5E2)%20+%20%5Csigma%20W_t%7D%0A"> où $ S_t$ est le prix de l’actif à l’instant$ t$, $ $ est le rendement moyen estimé (drift), $ $ est la volatilité du rendement, $ dW_t$ est un mouvement brownien standard.</p>
<p>On peut de ce fait calculer plusieurs trajectoires de rendements de <img src="https://latex.codecogs.com/png.latex?S_0"> et <img src="https://latex.codecogs.com/png.latex?S_%7B10%7D">, puis calculer la VaR à partir de la série des rendements $ r_{10j} = (S_{10} / S_{0}) $ obtenus avec ces trajectoires.</p>
<p>En utilisant la méthode de scaling et la méthode de diffusion, nous obtenons sensiblement la même VaR.</p>
<div id="c4561a82" class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb46-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb46-2"></span>
<span id="cb46-3">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(data_train)</span>
<span id="cb46-4">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(data_train)</span>
<span id="cb46-5"></span>
<span id="cb46-6">return_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb46-7">T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb46-8">M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb46-9">S0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>].iloc[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb46-10"></span>
<span id="cb46-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(M):</span>
<span id="cb46-12">    S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros(T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb46-13">    S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S0</span>
<span id="cb46-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb46-15">        Wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.normal()</span>
<span id="cb46-16">        S[t] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S[t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.exp((mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Wt)</span>
<span id="cb46-17"></span>
<span id="cb46-18">    return_sim.append(np.log(S[T]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>S0))</span>
<span id="cb46-19"></span>
<span id="cb46-20">var_gauss_diffusion <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gaussian_var(return_sim, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb46-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR gaussienne pour h=10j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_gauss_diffusion<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR gaussienne pour h=10j et alpha=0.99 est : 10.0053%</code></pre>
</div>
</div>
</section>
<section id="c.-méthode-ewma" class="level4">
<h4 class="anchored" data-anchor-id="c.-méthode-ewma">c.&nbsp;Méthode EWMA</h4>
<p>La VaR EWMA est une méthode qui permet de calculer la VaR en surpondérant les rendements les plus récents. Cela permet de donner plus de poids aux rendements les plus récents, et donc de mieux capturer les changements de volatilité. La VaR EWMA est donnée par la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AVaR_%7Bt+1%7D%20=%20%5Cmu%20+%20%5Csigma%20%5Ctimes%20z_%7B%5Calpha%7D%0A"></p>
<div id="db2eae5a" class="cell" data-execution_count="31">
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb48-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR la méthode EWMA (Exponential Weighting Moving Average)</span></span>
<span id="cb48-2"></span>
<span id="cb48-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> gaussian_var_ewma(data, alpha, lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.94</span>):</span>
<span id="cb48-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb48-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR gaussienne EWMA</span></span>
<span id="cb48-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb48-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb48-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    lambda_ : le paramètre de lissage</span></span>
<span id="cb48-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb48-10"></span>
<span id="cb48-11">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lambda_)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>i) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data))])</span>
<span id="cb48-12">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(weights)</span>
<span id="cb48-13"></span>
<span id="cb48-14">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(data[::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weights)</span>
<span id="cb48-15">    sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (data[::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb48-16"></span>
<span id="cb48-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> stats.norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha)), mu, sigma</span>
<span id="cb48-18"></span>
<span id="cb48-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y revenir</span></span></code></pre></div>
</div>
<div id="1505b88f" class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb49-1">lambdas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>]</span>
<span id="cb49-2">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb49-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb49-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lambdas:</span>
<span id="cb49-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb49-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lambda : "</span>, l)</span>
<span id="cb49-7">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb49-8">    var_gauss_emwa, mu, sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gaussian_var_ewma(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha, lambda_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>l)</span>
<span id="cb49-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR gaussienne EWMA pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_gauss_emwa<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb49-10">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La moyenne est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb49-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"L'écart-type est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb49-12"></span>
<span id="cb49-13">    n_exceptions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> exceptions(data_test, var_gauss_emwa)</span>
<span id="cb49-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Le nombre d'exceptions sur l'échantillon de test est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_exceptions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Lambda :  0.9
---------------
La VaR gaussienne EWMA pour h=1j et alpha=0.99 est : 2.3751%
La moyenne est : 0.1898%
L'écart-type est : 1.1025%
Le nombre d'exceptions sur l'échantillon de test est : 5
================================================================================
Lambda :  0.95
---------------
La VaR gaussienne EWMA pour h=1j et alpha=0.99 est : 2.8969%
La moyenne est : 0.0735%
L'écart-type est : 1.2768%
Le nombre d'exceptions sur l'échantillon de test est : 4
================================================================================
Lambda :  0.99
---------------
La VaR gaussienne EWMA pour h=1j et alpha=0.99 est : 3.3511%
La moyenne est : -0.0321%
L'écart-type est : 1.4267%
Le nombre d'exceptions sur l'échantillon de test est : 1</code></pre>
</div>
</div>
<p>Avec la méthode EWMA, nous observons une que la VaR diminue plus <img src="https://latex.codecogs.com/png.latex?%5Clambda"> augmente. Cela est dû au fait que plus <img src="https://latex.codecogs.com/png.latex?%5Clambda"> est grand, plus les rendements les plus récents sont surpondérés, et donc la volatilité est plus faible, en raison de la fin de la période d’apprentissage.</p>
</section>
</section>
</section>
<section id="ii.4.-var-skew-student" class="level2">
<h2 class="anchored" data-anchor-id="ii.4.-var-skew-student">II.4. VaR skew-student</h2>
<section id="ii.4.1.-validation-ex-ante" class="level3">
<h3 class="anchored" data-anchor-id="ii.4.1.-validation-ex-ante">II.4.1. Validation ex-ante</h3>
<div id="22cd24b7" class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb51-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ecrire une fonction permettant d’estimer les paramètres d’une loi de Skew Student par maximum de vraisemblance.</span></span>
<span id="cb51-2"></span>
<span id="cb51-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize</span>
<span id="cb51-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> t</span>
<span id="cb51-5"></span>
<span id="cb51-6"></span>
<span id="cb51-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_pdf(x, mu, sigma, gamma, nu ):</span>
<span id="cb51-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb51-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Compute the Skew Student-t probability density function (PDF).</span></span>
<span id="cb51-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb51-11"></span>
<span id="cb51-12"></span>
<span id="cb51-13">    t_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gamma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt((nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ((x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb51-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PDF of the standard Student-t distribution</span></span>
<span id="cb51-15">    pdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.pdf(x , df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu,  loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mu, scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sigma)</span>
<span id="cb51-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CDF of the transformed Student-t distribution</span></span>
<span id="cb51-17">    cdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.cdf(t_x, df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, scale<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb51-18"></span>
<span id="cb51-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skew Student density function</span></span>
<span id="cb51-20">    density <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pdf_t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> cdf_t</span>
<span id="cb51-21"></span>
<span id="cb51-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> density</span>
<span id="cb51-23"></span>
<span id="cb51-24"></span>
<span id="cb51-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_log_likelihood(params, data):</span>
<span id="cb51-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb51-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la log-vraisemblance de la loi de Skew Student</span></span>
<span id="cb51-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    params [mu, sigma, gamma, nu]: les paramètres de la loi</span></span>
<span id="cb51-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb51-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb51-31">    mu, sigma, gamma, nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params</span>
<span id="cb51-32">    density <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> skew_student_pdf(data , mu, sigma, gamma, nu)</span>
<span id="cb51-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># log-vraisemblance</span></span>
<span id="cb51-34">    loglik <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(np.log(density))</span>
<span id="cb51-35"></span>
<span id="cb51-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> loglik</span>
<span id="cb51-37"></span>
<span id="cb51-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Optimisation des paramètres avec contraintes de positivité sur sigma et nu</span></span>
<span id="cb51-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_fit(data):</span>
<span id="cb51-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb51-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Estimation des paramètres de la loi de Skew Student</span></span>
<span id="cb51-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb51-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initial guess</span></span>
<span id="cb51-44">    x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([np.mean(data), np.std(data), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb51-45"></span>
<span id="cb51-46">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># contraintes</span></span>
<span id="cb51-47">    bounds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>), (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)]</span>
<span id="cb51-48"></span>
<span id="cb51-49">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># optimisation</span></span>
<span id="cb51-50">    res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize(skew_student_log_likelihood, x0, args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(data), bounds<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bounds)</span>
<span id="cb51-51"></span>
<span id="cb51-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> res.x</span>
<span id="cb51-53"></span>
<span id="cb51-54">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> skew_student_fit(data_train)</span>
<span id="cb51-55"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb51-56"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Les paramètres estimés de la loi de Skew Student sont : "</span>)</span>
<span id="cb51-57"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb51-58"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mu : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb51-59"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sigma : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb51-60"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gamma : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb51-61"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nu : "</span>, params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb51-62"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb51-63"></span>
<span id="cb51-64"></span>
<span id="cb51-65">params_sstd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb51-66">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb51-67">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb51-68">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gamma"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb51-69">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span> : params[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb51-70">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Les paramètres estimés de la loi de Skew Student sont : 
---------------
Mu :  0.0023247899830732325
Sigma :  0.008823850410822238
Gamma :  -0.23183614178494735
Nu :  2.9618421112435653
================================================================================</code></pre>
</div>
</div>
<div id="33712ed5" class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb53-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Superposition de la densité théorique et des données</span></span>
<span id="cb53-2"></span>
<span id="cb53-3">x_values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(data_train), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(data_train), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb53-4"></span>
<span id="cb53-5">theoretical_density <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> skew_student_pdf(x_values, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params_sstd)</span>
<span id="cb53-6">plt.figure(figsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb53-7">plt.hist(data_train, bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, density<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Données empiriques'</span>)</span>
<span id="cb53-8">plt.plot(x_values, theoretical_density, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité Skew Student'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb53-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Densité normale</span></span>
<span id="cb53-10">plt.plot(x_values, stats.norm.pdf(x_values, np.mean(data_train), np.std(data_train)), label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité normale'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb53-11">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rendements logarithmiques'</span>)</span>
<span id="cb53-12">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Densité'</span>)</span>
<span id="cb53-13">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparaison entre les données et la densité théorique d'une loi de Skew Student"</span>)</span>
<span id="cb53-14">plt.legend()</span>
<span id="cb53-15"></span>
<span id="cb53-16">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-35-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Le graphique ci-dessus est satisfaisant. La densité théorique de la skew-student semble bien s’ajuster aux données. La loi skew-student de paramètres (<img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%200.002,%20%5Csigma%20=%200.009,%20%5Cgamma%20=%20-0.23,%20%5Cnu%20=%202.96">). Le <img src="https://latex.codecogs.com/png.latex?%5Cmu"> est le rendement moyen, <img src="https://latex.codecogs.com/png.latex?%5Csigma"> est l’écart-type, <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> est le coefficient d’asymétrie et <img src="https://latex.codecogs.com/png.latex?%5Cnu"> est le degré de liberté. Le skewness est négatif, ce qui indique une asymétrie négative des rendements, comme ce qu’on a observé plus haut.</p>
<p>Nous allons appuyer cette validation en utilisant le QQ-plot. La fonction quantile d’une loi skew-student n’est pas analytique. Pour ce faire, nous allons construire la fonction de repartition de la skew-student ainsi que la fonction quantile qui est l’inverse de cette fonction de repartition. Nous allons ensuite comparer les quantiles théoriques de la skew-student avec les quantiles empiriques des rendements.</p>
<p>En observant le QQ-plot, on constate que les quantiles théoriques de la skew-student collent bien avec les quantiles empiriques des rendements. Cela confirme que la skew-student est une bonne approximation de la distribution des rendements. Pour une validation plus rigoureuse, on peut utiliser un test de Kolmogorov-Smirnov pour tester si les rendements suivent une loi skew-student.</p>
<div id="52bc6f76" class="cell" data-execution_count="35">
<div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb54-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## Intégration de la fonction de densité</span></span>
<span id="cb54-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> integrate</span>
<span id="cb54-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> minimize_scalar</span>
<span id="cb54-4"></span>
<span id="cb54-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> integrale_SkewStudent(x,params):</span>
<span id="cb54-6">    borne_inf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.inf</span>
<span id="cb54-7">    resultat_integration, erreur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> integrate.quad(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: skew_student_pdf(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params), borne_inf, x)</span>
<span id="cb54-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> resultat_integration</span>
<span id="cb54-9"></span>
<span id="cb54-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fonc_minimize(x, alpha,params):</span>
<span id="cb54-11">    value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> integrale_SkewStudent(x,params)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha</span>
<span id="cb54-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(value)</span>
<span id="cb54-13"></span>
<span id="cb54-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_student_quantile(alpha,mu, sigma, gamma, nu ):</span>
<span id="cb54-15">    params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb54-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span> : mu ,</span>
<span id="cb54-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span> : sigma,</span>
<span id="cb54-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gamma"</span> : gamma,</span>
<span id="cb54-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span> : nu</span>
<span id="cb54-20">    }</span>
<span id="cb54-21"></span>
<span id="cb54-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb54-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Veuillez entrer un niveau alpha entre 0 et 1"</span>)</span>
<span id="cb54-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb54-25">        resultat_minimisation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minimize_scalar(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: fonc_minimize(x, alpha,params))</span>
<span id="cb54-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> resultat_minimisation.x</span></code></pre></div>
</div>
<div id="87e10501" class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb55-1">niveaux_quantiles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb55-2">quantiles_empiriques <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.quantile(data_train, niveaux_quantiles)</span>
<span id="cb55-3">quantiles_theoriques <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [skew_student_quantile(alpha,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params_sstd) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> alpha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tqdm(niveaux_quantiles)]</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>  0%|          | 0/999 [00:00&lt;?, ?it/s]  0%|          | 1/999 [00:00&lt;04:09,  3.99it/s]  0%|          | 2/999 [00:00&lt;04:01,  4.13it/s]  0%|          | 3/999 [00:00&lt;04:07,  4.02it/s]  0%|          | 4/999 [00:01&lt;04:47,  3.47it/s]  1%|          | 5/999 [00:01&lt;05:03,  3.28it/s]  1%|          | 6/999 [00:01&lt;05:04,  3.26it/s]  1%|          | 7/999 [00:02&lt;05:03,  3.26it/s]  1%|          | 8/999 [00:02&lt;05:09,  3.21it/s]  1%|          | 9/999 [00:02&lt;05:07,  3.22it/s]  1%|          | 10/999 [00:03&lt;05:12,  3.16it/s]  1%|          | 11/999 [00:03&lt;05:03,  3.25it/s]  1%|          | 12/999 [00:03&lt;05:08,  3.19it/s]  1%|▏         | 13/999 [00:03&lt;05:15,  3.13it/s]  1%|▏         | 14/999 [00:04&lt;05:22,  3.05it/s]  2%|▏         | 15/999 [00:04&lt;05:07,  3.20it/s]  2%|▏         | 16/999 [00:04&lt;05:09,  3.18it/s]  2%|▏         | 17/999 [00:05&lt;05:10,  3.17it/s]  2%|▏         | 18/999 [00:05&lt;05:13,  3.13it/s]  2%|▏         | 19/999 [00:05&lt;05:18,  3.08it/s]  2%|▏         | 20/999 [00:06&lt;05:10,  3.15it/s]  2%|▏         | 21/999 [00:06&lt;05:10,  3.15it/s]  2%|▏         | 22/999 [00:06&lt;05:17,  3.08it/s]  2%|▏         | 23/999 [00:07&lt;05:10,  3.14it/s]  2%|▏         | 24/999 [00:07&lt;05:07,  3.17it/s]  3%|▎         | 25/999 [00:07&lt;05:08,  3.15it/s]  3%|▎         | 26/999 [00:08&lt;05:14,  3.09it/s]  3%|▎         | 27/999 [00:08&lt;05:12,  3.11it/s]  3%|▎         | 28/999 [00:08&lt;05:12,  3.11it/s]  3%|▎         | 29/999 [00:09&lt;05:19,  3.04it/s]  3%|▎         | 30/999 [00:09&lt;05:15,  3.07it/s]  3%|▎         | 31/999 [00:09&lt;05:23,  2.99it/s]  3%|▎         | 32/999 [00:10&lt;05:31,  2.91it/s]  3%|▎         | 33/999 [00:10&lt;05:36,  2.87it/s]  3%|▎         | 34/999 [00:10&lt;05:40,  2.83it/s]  4%|▎         | 35/999 [00:11&lt;05:44,  2.80it/s]  4%|▎         | 36/999 [00:11&lt;05:56,  2.70it/s]  4%|▎         | 37/999 [00:12&lt;06:03,  2.65it/s]  4%|▍         | 38/999 [00:12&lt;06:13,  2.58it/s]  4%|▍         | 39/999 [00:12&lt;06:19,  2.53it/s]  4%|▍         | 40/999 [00:13&lt;06:07,  2.61it/s]  4%|▍         | 41/999 [00:13&lt;06:08,  2.60it/s]  4%|▍         | 42/999 [00:13&lt;06:12,  2.57it/s]  4%|▍         | 43/999 [00:14&lt;06:13,  2.56it/s]  4%|▍         | 44/999 [00:14&lt;06:09,  2.59it/s]  5%|▍         | 45/999 [00:15&lt;06:03,  2.63it/s]  5%|▍         | 46/999 [00:15&lt;05:58,  2.66it/s]  5%|▍         | 47/999 [00:15&lt;05:45,  2.75it/s]  5%|▍         | 48/999 [00:16&lt;05:49,  2.72it/s]  5%|▍         | 49/999 [00:16&lt;06:01,  2.62it/s]  5%|▌         | 50/999 [00:16&lt;05:54,  2.68it/s]  5%|▌         | 51/999 [00:17&lt;05:52,  2.69it/s]  5%|▌         | 52/999 [00:17&lt;05:54,  2.67it/s]  5%|▌         | 53/999 [00:18&lt;05:54,  2.67it/s]  5%|▌         | 54/999 [00:18&lt;05:42,  2.76it/s]  6%|▌         | 55/999 [00:18&lt;05:40,  2.78it/s]  6%|▌         | 56/999 [00:19&lt;05:44,  2.73it/s]  6%|▌         | 57/999 [00:19&lt;05:54,  2.66it/s]  6%|▌         | 58/999 [00:19&lt;05:51,  2.68it/s]  6%|▌         | 59/999 [00:20&lt;05:42,  2.75it/s]  6%|▌         | 60/999 [00:20&lt;05:45,  2.71it/s]  6%|▌         | 61/999 [00:21&lt;05:42,  2.74it/s]  6%|▌         | 62/999 [00:21&lt;05:49,  2.68it/s]  6%|▋         | 63/999 [00:21&lt;05:50,  2.67it/s]  6%|▋         | 64/999 [00:22&lt;05:54,  2.64it/s]  7%|▋         | 65/999 [00:22&lt;05:47,  2.69it/s]  7%|▋         | 66/999 [00:22&lt;05:45,  2.70it/s]  7%|▋         | 67/999 [00:23&lt;05:49,  2.66it/s]  7%|▋         | 68/999 [00:23&lt;05:48,  2.67it/s]  7%|▋         | 69/999 [00:24&lt;05:58,  2.59it/s]  7%|▋         | 70/999 [00:24&lt;05:54,  2.62it/s]  7%|▋         | 71/999 [00:24&lt;06:05,  2.54it/s]  7%|▋         | 72/999 [00:25&lt;05:56,  2.60it/s]  7%|▋         | 73/999 [00:25&lt;05:58,  2.59it/s]  7%|▋         | 74/999 [00:25&lt;05:55,  2.60it/s]  8%|▊         | 75/999 [00:26&lt;05:56,  2.59it/s]  8%|▊         | 76/999 [00:26&lt;06:00,  2.56it/s]  8%|▊         | 77/999 [00:27&lt;05:56,  2.59it/s]  8%|▊         | 78/999 [00:27&lt;05:59,  2.56it/s]  8%|▊         | 79/999 [00:27&lt;06:04,  2.53it/s]  8%|▊         | 80/999 [00:28&lt;06:01,  2.54it/s]  8%|▊         | 81/999 [00:28&lt;05:58,  2.56it/s]  8%|▊         | 82/999 [00:29&lt;05:57,  2.56it/s]  8%|▊         | 83/999 [00:29&lt;05:51,  2.61it/s]  8%|▊         | 84/999 [00:29&lt;05:48,  2.63it/s]  9%|▊         | 85/999 [00:30&lt;05:41,  2.67it/s]  9%|▊         | 86/999 [00:30&lt;05:41,  2.67it/s]  9%|▊         | 87/999 [00:30&lt;05:47,  2.63it/s]  9%|▉         | 88/999 [00:31&lt;05:48,  2.62it/s]  9%|▉         | 89/999 [00:31&lt;05:43,  2.65it/s]  9%|▉         | 90/999 [00:32&lt;05:34,  2.72it/s]  9%|▉         | 91/999 [00:32&lt;05:35,  2.70it/s]  9%|▉         | 92/999 [00:32&lt;05:31,  2.73it/s]  9%|▉         | 93/999 [00:33&lt;05:22,  2.81it/s]  9%|▉         | 94/999 [00:33&lt;05:24,  2.79it/s] 10%|▉         | 95/999 [00:33&lt;05:35,  2.70it/s] 10%|▉         | 96/999 [00:34&lt;05:36,  2.69it/s] 10%|▉         | 97/999 [00:34&lt;05:44,  2.62it/s] 10%|▉         | 98/999 [00:35&lt;05:42,  2.63it/s] 10%|▉         | 99/999 [00:35&lt;05:38,  2.66it/s] 10%|█         | 100/999 [00:35&lt;05:27,  2.74it/s] 10%|█         | 101/999 [00:36&lt;05:27,  2.74it/s] 10%|█         | 102/999 [00:36&lt;05:24,  2.77it/s] 10%|█         | 103/999 [00:36&lt;05:26,  2.74it/s] 10%|█         | 104/999 [00:37&lt;05:23,  2.77it/s] 11%|█         | 105/999 [00:37&lt;05:20,  2.79it/s] 11%|█         | 106/999 [00:37&lt;05:28,  2.72it/s] 11%|█         | 107/999 [00:38&lt;05:30,  2.70it/s] 11%|█         | 108/999 [00:38&lt;05:37,  2.64it/s] 11%|█         | 109/999 [00:39&lt;05:36,  2.64it/s] 11%|█         | 110/999 [00:39&lt;05:40,  2.61it/s] 11%|█         | 111/999 [00:39&lt;05:45,  2.57it/s] 11%|█         | 112/999 [00:40&lt;05:50,  2.53it/s] 11%|█▏        | 113/999 [00:40&lt;05:56,  2.49it/s] 11%|█▏        | 114/999 [00:41&lt;05:52,  2.51it/s] 12%|█▏        | 115/999 [00:41&lt;05:52,  2.51it/s] 12%|█▏        | 116/999 [00:41&lt;05:49,  2.52it/s] 12%|█▏        | 117/999 [00:42&lt;05:52,  2.50it/s] 12%|█▏        | 118/999 [00:42&lt;05:49,  2.52it/s] 12%|█▏        | 119/999 [00:43&lt;05:52,  2.49it/s] 12%|█▏        | 120/999 [00:43&lt;05:48,  2.52it/s] 12%|█▏        | 121/999 [00:43&lt;05:50,  2.50it/s] 12%|█▏        | 122/999 [00:44&lt;06:05,  2.40it/s] 12%|█▏        | 123/999 [00:44&lt;05:59,  2.44it/s] 12%|█▏        | 124/999 [00:45&lt;05:57,  2.45it/s] 13%|█▎        | 125/999 [00:45&lt;05:58,  2.44it/s] 13%|█▎        | 126/999 [00:46&lt;05:58,  2.44it/s] 13%|█▎        | 127/999 [00:46&lt;05:57,  2.44it/s] 13%|█▎        | 128/999 [00:46&lt;05:54,  2.45it/s] 13%|█▎        | 129/999 [00:47&lt;05:55,  2.44it/s] 13%|█▎        | 130/999 [00:47&lt;05:53,  2.46it/s] 13%|█▎        | 131/999 [00:48&lt;05:48,  2.49it/s] 13%|█▎        | 132/999 [00:48&lt;05:40,  2.55it/s] 13%|█▎        | 133/999 [00:48&lt;05:36,  2.58it/s] 13%|█▎        | 134/999 [00:49&lt;05:29,  2.63it/s] 14%|█▎        | 135/999 [00:49&lt;05:30,  2.61it/s] 14%|█▎        | 136/999 [00:49&lt;05:32,  2.59it/s] 14%|█▎        | 137/999 [00:50&lt;05:36,  2.56it/s] 14%|█▍        | 138/999 [00:50&lt;05:34,  2.57it/s] 14%|█▍        | 139/999 [00:51&lt;05:45,  2.49it/s] 14%|█▍        | 140/999 [00:51&lt;05:50,  2.45it/s] 14%|█▍        | 141/999 [00:51&lt;05:40,  2.52it/s] 14%|█▍        | 142/999 [00:52&lt;05:34,  2.56it/s] 14%|█▍        | 143/999 [00:52&lt;05:40,  2.51it/s] 14%|█▍        | 144/999 [00:53&lt;05:31,  2.58it/s] 15%|█▍        | 145/999 [00:53&lt;05:25,  2.62it/s] 15%|█▍        | 146/999 [00:53&lt;05:19,  2.67it/s] 15%|█▍        | 147/999 [00:54&lt;05:25,  2.62it/s] 15%|█▍        | 148/999 [00:54&lt;05:46,  2.46it/s] 15%|█▍        | 149/999 [00:55&lt;05:50,  2.43it/s] 15%|█▌        | 150/999 [00:55&lt;05:54,  2.40it/s] 15%|█▌        | 151/999 [00:55&lt;05:46,  2.45it/s] 15%|█▌        | 152/999 [00:56&lt;05:38,  2.50it/s] 15%|█▌        | 153/999 [00:56&lt;05:42,  2.47it/s] 15%|█▌        | 154/999 [00:57&lt;05:47,  2.43it/s] 16%|█▌        | 155/999 [00:57&lt;05:50,  2.41it/s] 16%|█▌        | 156/999 [00:57&lt;05:53,  2.39it/s] 16%|█▌        | 157/999 [00:58&lt;05:53,  2.38it/s] 16%|█▌        | 158/999 [00:58&lt;05:47,  2.42it/s] 16%|█▌        | 159/999 [00:59&lt;05:57,  2.35it/s] 16%|█▌        | 160/999 [00:59&lt;06:03,  2.31it/s] 16%|█▌        | 161/999 [01:00&lt;05:58,  2.34it/s] 16%|█▌        | 162/999 [01:00&lt;06:08,  2.27it/s] 16%|█▋        | 163/999 [01:01&lt;06:10,  2.25it/s] 16%|█▋        | 164/999 [01:01&lt;06:17,  2.21it/s] 17%|█▋        | 165/999 [01:01&lt;06:13,  2.23it/s] 17%|█▋        | 166/999 [01:02&lt;06:08,  2.26it/s] 17%|█▋        | 167/999 [01:02&lt;06:04,  2.28it/s] 17%|█▋        | 168/999 [01:03&lt;06:09,  2.25it/s] 17%|█▋        | 169/999 [01:03&lt;06:07,  2.26it/s] 17%|█▋        | 170/999 [01:04&lt;06:06,  2.26it/s] 17%|█▋        | 171/999 [01:04&lt;05:58,  2.31it/s] 17%|█▋        | 172/999 [01:05&lt;06:04,  2.27it/s] 17%|█▋        | 173/999 [01:05&lt;06:04,  2.27it/s] 17%|█▋        | 174/999 [01:05&lt;05:58,  2.30it/s] 18%|█▊        | 175/999 [01:06&lt;05:55,  2.32it/s] 18%|█▊        | 176/999 [01:06&lt;06:04,  2.26it/s] 18%|█▊        | 177/999 [01:07&lt;06:04,  2.25it/s] 18%|█▊        | 178/999 [01:07&lt;05:53,  2.32it/s] 18%|█▊        | 179/999 [01:08&lt;05:45,  2.37it/s] 18%|█▊        | 180/999 [01:08&lt;05:50,  2.34it/s] 18%|█▊        | 181/999 [01:08&lt;05:52,  2.32it/s] 18%|█▊        | 182/999 [01:09&lt;06:00,  2.26it/s] 18%|█▊        | 183/999 [01:09&lt;06:10,  2.20it/s] 18%|█▊        | 184/999 [01:10&lt;06:03,  2.24it/s] 19%|█▊        | 185/999 [01:10&lt;06:11,  2.19it/s] 19%|█▊        | 186/999 [01:11&lt;06:19,  2.14it/s] 19%|█▊        | 187/999 [01:11&lt;06:18,  2.14it/s] 19%|█▉        | 188/999 [01:12&lt;06:17,  2.15it/s] 19%|█▉        | 189/999 [01:12&lt;06:02,  2.23it/s] 19%|█▉        | 190/999 [01:13&lt;06:02,  2.23it/s] 19%|█▉        | 191/999 [01:13&lt;05:41,  2.37it/s] 19%|█▉        | 192/999 [01:13&lt;05:49,  2.31it/s] 19%|█▉        | 193/999 [01:14&lt;05:46,  2.32it/s] 19%|█▉        | 194/999 [01:14&lt;05:45,  2.33it/s] 20%|█▉        | 195/999 [01:15&lt;05:42,  2.35it/s] 20%|█▉        | 196/999 [01:15&lt;05:42,  2.35it/s] 20%|█▉        | 197/999 [01:15&lt;05:45,  2.32it/s] 20%|█▉        | 198/999 [01:16&lt;05:42,  2.34it/s] 20%|█▉        | 199/999 [01:16&lt;05:47,  2.30it/s] 20%|██        | 200/999 [01:17&lt;05:49,  2.28it/s] 20%|██        | 201/999 [01:17&lt;05:45,  2.31it/s] 20%|██        | 202/999 [01:18&lt;05:50,  2.27it/s] 20%|██        | 203/999 [01:18&lt;05:47,  2.29it/s] 20%|██        | 204/999 [01:19&lt;05:39,  2.34it/s] 21%|██        | 205/999 [01:19&lt;05:35,  2.37it/s] 21%|██        | 206/999 [01:19&lt;05:29,  2.41it/s] 21%|██        | 207/999 [01:20&lt;05:36,  2.35it/s] 21%|██        | 208/999 [01:20&lt;05:40,  2.32it/s] 21%|██        | 209/999 [01:21&lt;05:47,  2.28it/s] 21%|██        | 210/999 [01:21&lt;05:45,  2.28it/s] 21%|██        | 211/999 [01:22&lt;05:58,  2.20it/s] 21%|██        | 212/999 [01:22&lt;05:53,  2.23it/s] 21%|██▏       | 213/999 [01:22&lt;05:43,  2.29it/s] 21%|██▏       | 214/999 [01:23&lt;05:46,  2.26it/s] 22%|██▏       | 215/999 [01:23&lt;05:49,  2.24it/s] 22%|██▏       | 216/999 [01:24&lt;05:43,  2.28it/s] 22%|██▏       | 217/999 [01:24&lt;05:43,  2.28it/s] 22%|██▏       | 218/999 [01:25&lt;05:43,  2.28it/s] 22%|██▏       | 219/999 [01:25&lt;05:25,  2.39it/s] 22%|██▏       | 220/999 [01:25&lt;05:27,  2.38it/s] 22%|██▏       | 221/999 [01:26&lt;05:31,  2.35it/s] 22%|██▏       | 222/999 [01:26&lt;05:40,  2.28it/s] 22%|██▏       | 223/999 [01:27&lt;05:46,  2.24it/s] 22%|██▏       | 224/999 [01:27&lt;05:44,  2.25it/s] 23%|██▎       | 225/999 [01:28&lt;05:50,  2.21it/s] 23%|██▎       | 226/999 [01:28&lt;05:42,  2.25it/s] 23%|██▎       | 227/999 [01:29&lt;05:42,  2.26it/s] 23%|██▎       | 228/999 [01:29&lt;05:44,  2.24it/s] 23%|██▎       | 229/999 [01:30&lt;05:55,  2.17it/s] 23%|██▎       | 230/999 [01:30&lt;06:04,  2.11it/s] 23%|██▎       | 231/999 [01:31&lt;06:10,  2.07it/s] 23%|██▎       | 232/999 [01:31&lt;06:01,  2.12it/s] 23%|██▎       | 233/999 [01:31&lt;05:50,  2.19it/s] 23%|██▎       | 234/999 [01:32&lt;05:43,  2.22it/s] 24%|██▎       | 235/999 [01:32&lt;05:39,  2.25it/s] 24%|██▎       | 236/999 [01:33&lt;05:49,  2.18it/s] 24%|██▎       | 237/999 [01:33&lt;05:58,  2.13it/s] 24%|██▍       | 238/999 [01:34&lt;06:08,  2.06it/s] 24%|██▍       | 239/999 [01:34&lt;06:19,  2.00it/s] 24%|██▍       | 240/999 [01:35&lt;06:23,  1.98it/s] 24%|██▍       | 241/999 [01:35&lt;06:23,  1.98it/s] 24%|██▍       | 242/999 [01:36&lt;06:25,  1.96it/s] 24%|██▍       | 243/999 [01:36&lt;06:32,  1.92it/s] 24%|██▍       | 244/999 [01:37&lt;06:36,  1.90it/s] 25%|██▍       | 245/999 [01:37&lt;06:31,  1.92it/s] 25%|██▍       | 246/999 [01:38&lt;06:34,  1.91it/s] 25%|██▍       | 247/999 [01:39&lt;06:32,  1.92it/s] 25%|██▍       | 248/999 [01:39&lt;06:45,  1.85it/s] 25%|██▍       | 249/999 [01:40&lt;06:53,  1.81it/s] 25%|██▌       | 250/999 [01:40&lt;06:50,  1.82it/s] 25%|██▌       | 251/999 [01:41&lt;06:54,  1.81it/s] 25%|██▌       | 252/999 [01:41&lt;06:45,  1.84it/s] 25%|██▌       | 253/999 [01:42&lt;06:51,  1.81it/s] 25%|██▌       | 254/999 [01:42&lt;06:58,  1.78it/s] 26%|██▌       | 255/999 [01:43&lt;06:59,  1.77it/s] 26%|██▌       | 256/999 [01:44&lt;06:55,  1.79it/s] 26%|██▌       | 257/999 [01:44&lt;06:35,  1.87it/s] 26%|██▌       | 258/999 [01:45&lt;06:27,  1.91it/s] 26%|██▌       | 259/999 [01:45&lt;06:28,  1.90it/s] 26%|██▌       | 260/999 [01:46&lt;06:20,  1.94it/s] 26%|██▌       | 261/999 [01:46&lt;06:12,  1.98it/s] 26%|██▌       | 262/999 [01:47&lt;05:59,  2.05it/s] 26%|██▋       | 263/999 [01:47&lt;05:55,  2.07it/s] 26%|██▋       | 264/999 [01:47&lt;05:50,  2.10it/s] 27%|██▋       | 265/999 [01:48&lt;05:47,  2.11it/s] 27%|██▋       | 266/999 [01:48&lt;05:46,  2.12it/s] 27%|██▋       | 267/999 [01:49&lt;05:42,  2.13it/s] 27%|██▋       | 268/999 [01:49&lt;05:49,  2.09it/s] 27%|██▋       | 269/999 [01:50&lt;05:48,  2.10it/s] 27%|██▋       | 270/999 [01:50&lt;05:52,  2.07it/s] 27%|██▋       | 271/999 [01:51&lt;05:49,  2.08it/s] 27%|██▋       | 272/999 [01:51&lt;05:58,  2.03it/s] 27%|██▋       | 273/999 [01:52&lt;05:59,  2.02it/s] 27%|██▋       | 274/999 [01:52&lt;05:55,  2.04it/s] 28%|██▊       | 275/999 [01:53&lt;05:55,  2.03it/s] 28%|██▊       | 276/999 [01:53&lt;06:01,  2.00it/s] 28%|██▊       | 277/999 [01:54&lt;05:53,  2.04it/s] 28%|██▊       | 278/999 [01:54&lt;05:59,  2.01it/s] 28%|██▊       | 279/999 [01:55&lt;06:01,  1.99it/s] 28%|██▊       | 280/999 [01:55&lt;06:07,  1.96it/s] 28%|██▊       | 281/999 [01:56&lt;06:07,  1.95it/s] 28%|██▊       | 282/999 [01:56&lt;06:14,  1.91it/s] 28%|██▊       | 283/999 [01:57&lt;06:15,  1.91it/s] 28%|██▊       | 284/999 [01:58&lt;06:32,  1.82it/s] 29%|██▊       | 285/999 [01:58&lt;06:44,  1.76it/s] 29%|██▊       | 286/999 [01:59&lt;06:34,  1.81it/s] 29%|██▊       | 287/999 [01:59&lt;06:41,  1.78it/s] 29%|██▉       | 288/999 [02:00&lt;06:31,  1.81it/s] 29%|██▉       | 289/999 [02:00&lt;06:35,  1.80it/s] 29%|██▉       | 290/999 [02:01&lt;06:26,  1.83it/s] 29%|██▉       | 291/999 [02:01&lt;06:34,  1.79it/s] 29%|██▉       | 292/999 [02:02&lt;06:23,  1.84it/s] 29%|██▉       | 293/999 [02:02&lt;06:15,  1.88it/s] 29%|██▉       | 294/999 [02:03&lt;06:25,  1.83it/s] 30%|██▉       | 295/999 [02:04&lt;06:16,  1.87it/s] 30%|██▉       | 296/999 [02:04&lt;06:26,  1.82it/s] 30%|██▉       | 297/999 [02:05&lt;06:19,  1.85it/s] 30%|██▉       | 298/999 [02:05&lt;06:23,  1.83it/s] 30%|██▉       | 299/999 [02:06&lt;06:19,  1.85it/s] 30%|███       | 300/999 [02:06&lt;06:27,  1.80it/s] 30%|███       | 301/999 [02:07&lt;06:31,  1.78it/s] 30%|███       | 302/999 [02:07&lt;06:30,  1.78it/s] 30%|███       | 303/999 [02:08&lt;06:39,  1.74it/s] 30%|███       | 304/999 [02:09&lt;06:29,  1.79it/s] 31%|███       | 305/999 [02:09&lt;06:18,  1.83it/s] 31%|███       | 306/999 [02:10&lt;06:08,  1.88it/s] 31%|███       | 307/999 [02:10&lt;06:05,  1.89it/s] 31%|███       | 308/999 [02:11&lt;05:53,  1.95it/s] 31%|███       | 309/999 [02:11&lt;05:59,  1.92it/s] 31%|███       | 310/999 [02:12&lt;06:05,  1.89it/s] 31%|███       | 311/999 [02:12&lt;06:11,  1.85it/s] 31%|███       | 312/999 [02:13&lt;06:12,  1.84it/s] 31%|███▏      | 313/999 [02:13&lt;06:14,  1.83it/s] 31%|███▏      | 314/999 [02:14&lt;06:08,  1.86it/s] 32%|███▏      | 315/999 [02:14&lt;05:59,  1.90it/s] 32%|███▏      | 316/999 [02:15&lt;05:59,  1.90it/s] 32%|███▏      | 317/999 [02:15&lt;05:59,  1.90it/s] 32%|███▏      | 318/999 [02:16&lt;05:55,  1.92it/s] 32%|███▏      | 319/999 [02:16&lt;06:00,  1.89it/s] 32%|███▏      | 320/999 [02:17&lt;06:01,  1.88it/s] 32%|███▏      | 321/999 [02:18&lt;06:20,  1.78it/s] 32%|███▏      | 322/999 [02:18&lt;06:07,  1.84it/s] 32%|███▏      | 323/999 [02:19&lt;06:21,  1.77it/s] 32%|███▏      | 324/999 [02:19&lt;06:19,  1.78it/s] 33%|███▎      | 325/999 [02:20&lt;06:11,  1.81it/s] 33%|███▎      | 326/999 [02:20&lt;06:06,  1.84it/s] 33%|███▎      | 327/999 [02:21&lt;06:21,  1.76it/s] 33%|███▎      | 328/999 [02:22&lt;06:11,  1.81it/s] 33%|███▎      | 329/999 [02:22&lt;06:22,  1.75it/s] 33%|███▎      | 330/999 [02:23&lt;06:13,  1.79it/s] 33%|███▎      | 331/999 [02:23&lt;06:11,  1.80it/s] 33%|███▎      | 332/999 [02:24&lt;06:11,  1.79it/s] 33%|███▎      | 333/999 [02:24&lt;06:11,  1.79it/s] 33%|███▎      | 334/999 [02:25&lt;06:02,  1.84it/s] 34%|███▎      | 335/999 [02:25&lt;06:01,  1.83it/s] 34%|███▎      | 336/999 [02:26&lt;06:03,  1.82it/s] 34%|███▎      | 337/999 [02:26&lt;06:02,  1.83it/s] 34%|███▍      | 338/999 [02:27&lt;06:04,  1.81it/s] 34%|███▍      | 339/999 [02:28&lt;05:54,  1.86it/s] 34%|███▍      | 340/999 [02:28&lt;05:47,  1.90it/s] 34%|███▍      | 341/999 [02:29&lt;05:49,  1.88it/s] 34%|███▍      | 342/999 [02:29&lt;05:48,  1.88it/s] 34%|███▍      | 343/999 [02:30&lt;05:48,  1.88it/s] 34%|███▍      | 344/999 [02:30&lt;05:50,  1.87it/s] 35%|███▍      | 345/999 [02:31&lt;05:45,  1.89it/s] 35%|███▍      | 346/999 [02:31&lt;05:45,  1.89it/s] 35%|███▍      | 347/999 [02:32&lt;05:47,  1.88it/s] 35%|███▍      | 348/999 [02:32&lt;05:46,  1.88it/s] 35%|███▍      | 349/999 [02:33&lt;05:40,  1.91it/s] 35%|███▌      | 350/999 [02:33&lt;05:38,  1.92it/s] 35%|███▌      | 351/999 [02:34&lt;05:44,  1.88it/s] 35%|███▌      | 352/999 [02:34&lt;05:46,  1.87it/s] 35%|███▌      | 353/999 [02:35&lt;05:47,  1.86it/s] 35%|███▌      | 354/999 [02:36&lt;05:43,  1.88it/s] 36%|███▌      | 355/999 [02:36&lt;05:40,  1.89it/s] 36%|███▌      | 356/999 [02:37&lt;05:40,  1.89it/s] 36%|███▌      | 357/999 [02:37&lt;05:37,  1.90it/s] 36%|███▌      | 358/999 [02:38&lt;05:43,  1.86it/s] 36%|███▌      | 359/999 [02:38&lt;05:47,  1.84it/s] 36%|███▌      | 360/999 [02:39&lt;05:40,  1.87it/s] 36%|███▌      | 361/999 [02:39&lt;05:40,  1.87it/s] 36%|███▌      | 362/999 [02:40&lt;05:36,  1.89it/s] 36%|███▋      | 363/999 [02:40&lt;05:29,  1.93it/s] 36%|███▋      | 364/999 [02:41&lt;05:23,  1.97it/s] 37%|███▋      | 365/999 [02:41&lt;05:30,  1.92it/s] 37%|███▋      | 366/999 [02:42&lt;05:29,  1.92it/s] 37%|███▋      | 367/999 [02:42&lt;05:26,  1.93it/s] 37%|███▋      | 368/999 [02:43&lt;05:23,  1.95it/s] 37%|███▋      | 369/999 [02:43&lt;05:18,  1.98it/s] 37%|███▋      | 370/999 [02:44&lt;05:17,  1.98it/s] 37%|███▋      | 371/999 [02:44&lt;05:14,  2.00it/s] 37%|███▋      | 372/999 [02:45&lt;05:17,  1.98it/s] 37%|███▋      | 373/999 [02:45&lt;05:17,  1.97it/s] 37%|███▋      | 374/999 [02:46&lt;05:16,  1.98it/s] 38%|███▊      | 375/999 [02:46&lt;05:07,  2.03it/s] 38%|███▊      | 376/999 [02:47&lt;05:04,  2.05it/s] 38%|███▊      | 377/999 [02:47&lt;05:09,  2.01it/s] 38%|███▊      | 378/999 [02:48&lt;05:05,  2.03it/s] 38%|███▊      | 379/999 [02:48&lt;05:08,  2.01it/s] 38%|███▊      | 380/999 [02:49&lt;05:14,  1.97it/s] 38%|███▊      | 381/999 [02:49&lt;05:13,  1.97it/s] 38%|███▊      | 382/999 [02:50&lt;05:07,  2.01it/s] 38%|███▊      | 383/999 [02:50&lt;05:02,  2.04it/s] 38%|███▊      | 384/999 [02:51&lt;05:07,  2.00it/s] 39%|███▊      | 385/999 [02:51&lt;05:08,  1.99it/s] 39%|███▊      | 386/999 [02:52&lt;05:11,  1.97it/s] 39%|███▊      | 387/999 [02:52&lt;04:56,  2.06it/s] 39%|███▉      | 388/999 [02:53&lt;05:05,  2.00it/s] 39%|███▉      | 389/999 [02:53&lt;05:03,  2.01it/s] 39%|███▉      | 390/999 [02:54&lt;05:04,  2.00it/s] 39%|███▉      | 391/999 [02:54&lt;05:11,  1.95it/s] 39%|███▉      | 392/999 [02:55&lt;05:14,  1.93it/s] 39%|███▉      | 393/999 [02:55&lt;05:16,  1.91it/s] 39%|███▉      | 394/999 [02:56&lt;05:13,  1.93it/s] 40%|███▉      | 395/999 [02:56&lt;05:09,  1.95it/s] 40%|███▉      | 396/999 [02:57&lt;05:13,  1.92it/s] 40%|███▉      | 397/999 [02:57&lt;05:14,  1.91it/s] 40%|███▉      | 398/999 [02:58&lt;05:12,  1.92it/s] 40%|███▉      | 399/999 [02:59&lt;05:21,  1.87it/s] 40%|████      | 400/999 [02:59&lt;05:18,  1.88it/s] 40%|████      | 401/999 [03:00&lt;05:18,  1.88it/s] 40%|████      | 402/999 [03:00&lt;05:15,  1.89it/s] 40%|████      | 403/999 [03:01&lt;05:14,  1.90it/s] 40%|████      | 404/999 [03:01&lt;05:10,  1.92it/s] 41%|████      | 405/999 [03:02&lt;05:04,  1.95it/s] 41%|████      | 406/999 [03:02&lt;05:02,  1.96it/s] 41%|████      | 407/999 [03:03&lt;05:02,  1.96it/s] 41%|████      | 408/999 [03:03&lt;04:57,  1.98it/s] 41%|████      | 409/999 [03:04&lt;04:57,  1.98it/s] 41%|████      | 410/999 [03:04&lt;05:00,  1.96it/s] 41%|████      | 411/999 [03:05&lt;05:04,  1.93it/s] 41%|████      | 412/999 [03:05&lt;04:59,  1.96it/s] 41%|████▏     | 413/999 [03:06&lt;04:59,  1.96it/s] 41%|████▏     | 414/999 [03:06&lt;04:56,  1.97it/s] 42%|████▏     | 415/999 [03:07&lt;05:01,  1.93it/s] 42%|████▏     | 416/999 [03:07&lt;05:05,  1.91it/s] 42%|████▏     | 417/999 [03:08&lt;05:09,  1.88it/s] 42%|████▏     | 418/999 [03:08&lt;05:03,  1.92it/s] 42%|████▏     | 419/999 [03:09&lt;05:07,  1.89it/s] 42%|████▏     | 420/999 [03:09&lt;05:08,  1.88it/s] 42%|████▏     | 421/999 [03:10&lt;05:14,  1.84it/s] 42%|████▏     | 422/999 [03:11&lt;05:17,  1.82it/s] 42%|████▏     | 423/999 [03:11&lt;05:19,  1.80it/s] 42%|████▏     | 424/999 [03:12&lt;05:13,  1.84it/s] 43%|████▎     | 425/999 [03:12&lt;05:13,  1.83it/s] 43%|████▎     | 426/999 [03:13&lt;05:11,  1.84it/s] 43%|████▎     | 427/999 [03:13&lt;05:13,  1.82it/s] 43%|████▎     | 428/999 [03:14&lt;05:11,  1.83it/s] 43%|████▎     | 429/999 [03:14&lt;05:23,  1.76it/s] 43%|████▎     | 430/999 [03:15&lt;05:17,  1.79it/s] 43%|████▎     | 431/999 [03:16&lt;05:08,  1.84it/s] 43%|████▎     | 432/999 [03:16&lt;05:06,  1.85it/s] 43%|████▎     | 433/999 [03:17&lt;05:00,  1.88it/s] 43%|████▎     | 434/999 [03:17&lt;04:56,  1.91it/s] 44%|████▎     | 435/999 [03:18&lt;05:06,  1.84it/s] 44%|████▎     | 436/999 [03:18&lt;05:14,  1.79it/s] 44%|████▎     | 437/999 [03:19&lt;05:20,  1.75it/s] 44%|████▍     | 438/999 [03:19&lt;05:18,  1.76it/s] 44%|████▍     | 439/999 [03:20&lt;05:10,  1.80it/s] 44%|████▍     | 440/999 [03:20&lt;05:10,  1.80it/s] 44%|████▍     | 441/999 [03:21&lt;05:00,  1.86it/s] 44%|████▍     | 442/999 [03:22&lt;04:57,  1.87it/s] 44%|████▍     | 443/999 [03:22&lt;04:58,  1.86it/s] 44%|████▍     | 444/999 [03:23&lt;04:53,  1.89it/s] 45%|████▍     | 445/999 [03:23&lt;04:54,  1.88it/s] 45%|████▍     | 446/999 [03:24&lt;04:49,  1.91it/s] 45%|████▍     | 447/999 [03:24&lt;04:54,  1.87it/s] 45%|████▍     | 448/999 [03:25&lt;04:52,  1.89it/s] 45%|████▍     | 449/999 [03:25&lt;04:52,  1.88it/s] 45%|████▌     | 450/999 [03:26&lt;04:57,  1.84it/s] 45%|████▌     | 451/999 [03:26&lt;05:00,  1.82it/s] 45%|████▌     | 452/999 [03:27&lt;05:03,  1.80it/s] 45%|████▌     | 453/999 [03:27&lt;05:04,  1.79it/s] 45%|████▌     | 454/999 [03:28&lt;05:02,  1.80it/s] 46%|████▌     | 455/999 [03:29&lt;05:00,  1.81it/s] 46%|████▌     | 456/999 [03:29&lt;04:54,  1.84it/s] 46%|████▌     | 457/999 [03:30&lt;04:53,  1.85it/s] 46%|████▌     | 458/999 [03:30&lt;04:52,  1.85it/s] 46%|████▌     | 459/999 [03:31&lt;05:03,  1.78it/s] 46%|████▌     | 460/999 [03:31&lt;05:03,  1.77it/s] 46%|████▌     | 461/999 [03:32&lt;05:02,  1.78it/s] 46%|████▌     | 462/999 [03:32&lt;05:00,  1.79it/s] 46%|████▋     | 463/999 [03:33&lt;05:00,  1.78it/s] 46%|████▋     | 464/999 [03:34&lt;05:04,  1.76it/s] 47%|████▋     | 465/999 [03:34&lt;04:55,  1.81it/s] 47%|████▋     | 466/999 [03:35&lt;04:57,  1.79it/s] 47%|████▋     | 467/999 [03:35&lt;04:48,  1.84it/s] 47%|████▋     | 468/999 [03:36&lt;04:43,  1.87it/s] 47%|████▋     | 469/999 [03:36&lt;04:45,  1.86it/s] 47%|████▋     | 470/999 [03:37&lt;04:46,  1.85it/s] 47%|████▋     | 471/999 [03:37&lt;04:48,  1.83it/s] 47%|████▋     | 472/999 [03:38&lt;04:55,  1.78it/s] 47%|████▋     | 473/999 [03:39&lt;04:57,  1.77it/s] 47%|████▋     | 474/999 [03:39&lt;04:44,  1.84it/s] 48%|████▊     | 475/999 [03:40&lt;04:42,  1.85it/s] 48%|████▊     | 476/999 [03:40&lt;04:39,  1.87it/s] 48%|████▊     | 477/999 [03:41&lt;04:42,  1.84it/s] 48%|████▊     | 478/999 [03:41&lt;04:47,  1.81it/s] 48%|████▊     | 479/999 [03:42&lt;04:51,  1.79it/s] 48%|████▊     | 480/999 [03:42&lt;04:41,  1.84it/s] 48%|████▊     | 481/999 [03:43&lt;04:43,  1.83it/s] 48%|████▊     | 482/999 [03:43&lt;04:39,  1.85it/s] 48%|████▊     | 483/999 [03:44&lt;04:46,  1.80it/s] 48%|████▊     | 484/999 [03:45&lt;04:39,  1.84it/s] 49%|████▊     | 485/999 [03:45&lt;04:35,  1.87it/s] 49%|████▊     | 486/999 [03:46&lt;04:31,  1.89it/s] 49%|████▊     | 487/999 [03:46&lt;04:30,  1.90it/s] 49%|████▉     | 488/999 [03:47&lt;04:26,  1.91it/s] 49%|████▉     | 489/999 [03:47&lt;04:22,  1.94it/s] 49%|████▉     | 490/999 [03:48&lt;04:21,  1.95it/s] 49%|████▉     | 491/999 [03:48&lt;04:15,  1.99it/s] 49%|████▉     | 492/999 [03:49&lt;04:15,  1.99it/s] 49%|████▉     | 493/999 [03:49&lt;04:19,  1.95it/s] 49%|████▉     | 494/999 [03:50&lt;04:22,  1.92it/s] 50%|████▉     | 495/999 [03:50&lt;04:22,  1.92it/s] 50%|████▉     | 496/999 [03:51&lt;04:20,  1.93it/s] 50%|████▉     | 497/999 [03:51&lt;04:24,  1.90it/s] 50%|████▉     | 498/999 [03:52&lt;04:24,  1.89it/s] 50%|████▉     | 499/999 [03:52&lt;04:14,  1.97it/s] 50%|█████     | 500/999 [03:53&lt;04:14,  1.96it/s] 50%|█████     | 501/999 [03:53&lt;04:05,  2.03it/s] 50%|█████     | 502/999 [03:54&lt;04:13,  1.96it/s] 50%|█████     | 503/999 [03:54&lt;04:11,  1.97it/s] 50%|█████     | 504/999 [03:55&lt;04:09,  1.98it/s] 51%|█████     | 505/999 [03:55&lt;04:10,  1.97it/s] 51%|█████     | 506/999 [03:56&lt;04:15,  1.93it/s] 51%|█████     | 507/999 [03:56&lt;04:12,  1.95it/s] 51%|█████     | 508/999 [03:57&lt;03:59,  2.05it/s] 51%|█████     | 509/999 [03:57&lt;03:57,  2.06it/s] 51%|█████     | 510/999 [03:58&lt;04:00,  2.03it/s] 51%|█████     | 511/999 [03:58&lt;03:56,  2.06it/s] 51%|█████▏    | 512/999 [03:59&lt;03:52,  2.09it/s] 51%|█████▏    | 513/999 [03:59&lt;03:52,  2.09it/s] 51%|█████▏    | 514/999 [04:00&lt;03:54,  2.07it/s] 52%|█████▏    | 515/999 [04:00&lt;04:03,  1.99it/s] 52%|█████▏    | 516/999 [04:01&lt;04:00,  2.01it/s] 52%|█████▏    | 517/999 [04:01&lt;04:01,  2.00it/s] 52%|█████▏    | 518/999 [04:02&lt;04:03,  1.98it/s] 52%|█████▏    | 519/999 [04:02&lt;04:00,  1.99it/s] 52%|█████▏    | 520/999 [04:03&lt;04:00,  1.99it/s] 52%|█████▏    | 521/999 [04:03&lt;03:58,  2.00it/s] 52%|█████▏    | 522/999 [04:04&lt;03:56,  2.02it/s] 52%|█████▏    | 523/999 [04:04&lt;04:01,  1.97it/s] 52%|█████▏    | 524/999 [04:05&lt;04:03,  1.95it/s] 53%|█████▎    | 525/999 [04:05&lt;04:09,  1.90it/s] 53%|█████▎    | 526/999 [04:06&lt;04:08,  1.90it/s] 53%|█████▎    | 527/999 [04:06&lt;04:01,  1.95it/s] 53%|█████▎    | 528/999 [04:07&lt;04:06,  1.91it/s] 53%|█████▎    | 529/999 [04:07&lt;03:55,  2.00it/s] 53%|█████▎    | 530/999 [04:08&lt;04:03,  1.93it/s] 53%|█████▎    | 531/999 [04:08&lt;04:03,  1.92it/s] 53%|█████▎    | 532/999 [04:09&lt;04:05,  1.90it/s] 53%|█████▎    | 533/999 [04:09&lt;04:04,  1.91it/s] 53%|█████▎    | 534/999 [04:10&lt;04:00,  1.93it/s] 54%|█████▎    | 535/999 [04:10&lt;03:57,  1.95it/s] 54%|█████▎    | 536/999 [04:11&lt;03:58,  1.94it/s] 54%|█████▍    | 537/999 [04:11&lt;03:55,  1.96it/s] 54%|█████▍    | 538/999 [04:12&lt;03:54,  1.97it/s] 54%|█████▍    | 539/999 [04:12&lt;03:56,  1.95it/s] 54%|█████▍    | 540/999 [04:13&lt;03:54,  1.96it/s] 54%|█████▍    | 541/999 [04:13&lt;03:53,  1.96it/s] 54%|█████▍    | 542/999 [04:14&lt;03:53,  1.96it/s] 54%|█████▍    | 543/999 [04:14&lt;03:52,  1.96it/s] 54%|█████▍    | 544/999 [04:15&lt;03:57,  1.91it/s] 55%|█████▍    | 545/999 [04:16&lt;04:00,  1.89it/s] 55%|█████▍    | 546/999 [04:16&lt;03:57,  1.90it/s] 55%|█████▍    | 547/999 [04:17&lt;03:56,  1.91it/s] 55%|█████▍    | 548/999 [04:17&lt;03:58,  1.89it/s] 55%|█████▍    | 549/999 [04:18&lt;03:54,  1.92it/s] 55%|█████▌    | 550/999 [04:18&lt;03:50,  1.95it/s] 55%|█████▌    | 551/999 [04:19&lt;03:55,  1.90it/s] 55%|█████▌    | 552/999 [04:19&lt;03:51,  1.93it/s] 55%|█████▌    | 553/999 [04:20&lt;03:53,  1.91it/s] 55%|█████▌    | 554/999 [04:20&lt;03:51,  1.92it/s] 56%|█████▌    | 555/999 [04:21&lt;03:52,  1.91it/s] 56%|█████▌    | 556/999 [04:21&lt;03:52,  1.91it/s] 56%|█████▌    | 557/999 [04:22&lt;03:50,  1.92it/s] 56%|█████▌    | 558/999 [04:22&lt;03:49,  1.92it/s] 56%|█████▌    | 559/999 [04:23&lt;03:47,  1.93it/s] 56%|█████▌    | 560/999 [04:23&lt;03:48,  1.92it/s] 56%|█████▌    | 561/999 [04:24&lt;03:46,  1.93it/s] 56%|█████▋    | 562/999 [04:24&lt;03:43,  1.96it/s] 56%|█████▋    | 563/999 [04:25&lt;03:52,  1.88it/s] 56%|█████▋    | 564/999 [04:26&lt;03:55,  1.85it/s] 57%|█████▋    | 565/999 [04:26&lt;04:02,  1.79it/s] 57%|█████▋    | 566/999 [04:27&lt;03:58,  1.82it/s] 57%|█████▋    | 567/999 [04:27&lt;03:52,  1.86it/s] 57%|█████▋    | 568/999 [04:28&lt;03:59,  1.80it/s] 57%|█████▋    | 569/999 [04:28&lt;04:08,  1.73it/s] 57%|█████▋    | 570/999 [04:29&lt;04:03,  1.77it/s] 57%|█████▋    | 571/999 [04:29&lt;03:56,  1.81it/s] 57%|█████▋    | 572/999 [04:30&lt;03:53,  1.83it/s] 57%|█████▋    | 573/999 [04:31&lt;03:54,  1.82it/s] 57%|█████▋    | 574/999 [04:31&lt;03:54,  1.81it/s] 58%|█████▊    | 575/999 [04:32&lt;03:47,  1.86it/s] 58%|█████▊    | 576/999 [04:32&lt;03:42,  1.90it/s] 58%|█████▊    | 577/999 [04:33&lt;03:44,  1.88it/s] 58%|█████▊    | 578/999 [04:33&lt;03:42,  1.89it/s] 58%|█████▊    | 579/999 [04:34&lt;03:35,  1.94it/s] 58%|█████▊    | 580/999 [04:34&lt;03:37,  1.93it/s] 58%|█████▊    | 581/999 [04:35&lt;03:43,  1.87it/s] 58%|█████▊    | 582/999 [04:35&lt;03:35,  1.93it/s] 58%|█████▊    | 583/999 [04:36&lt;03:35,  1.93it/s] 58%|█████▊    | 584/999 [04:36&lt;03:35,  1.92it/s] 59%|█████▊    | 585/999 [04:37&lt;03:36,  1.91it/s] 59%|█████▊    | 586/999 [04:37&lt;03:40,  1.87it/s] 59%|█████▉    | 587/999 [04:38&lt;03:41,  1.86it/s] 59%|█████▉    | 588/999 [04:38&lt;03:40,  1.87it/s] 59%|█████▉    | 589/999 [04:39&lt;03:34,  1.91it/s] 59%|█████▉    | 590/999 [04:39&lt;03:35,  1.90it/s] 59%|█████▉    | 591/999 [04:40&lt;03:40,  1.85it/s] 59%|█████▉    | 592/999 [04:41&lt;03:33,  1.91it/s] 59%|█████▉    | 593/999 [04:41&lt;03:36,  1.88it/s] 59%|█████▉    | 594/999 [04:42&lt;03:39,  1.84it/s] 60%|█████▉    | 595/999 [04:42&lt;03:42,  1.82it/s] 60%|█████▉    | 596/999 [04:43&lt;03:43,  1.80it/s] 60%|█████▉    | 597/999 [04:43&lt;03:47,  1.77it/s] 60%|█████▉    | 598/999 [04:44&lt;03:44,  1.79it/s] 60%|█████▉    | 599/999 [04:45&lt;03:44,  1.78it/s] 60%|██████    | 600/999 [04:45&lt;03:42,  1.80it/s] 60%|██████    | 601/999 [04:46&lt;03:37,  1.83it/s] 60%|██████    | 602/999 [04:46&lt;03:34,  1.85it/s] 60%|██████    | 603/999 [04:47&lt;03:33,  1.86it/s] 60%|██████    | 604/999 [04:47&lt;03:31,  1.87it/s] 61%|██████    | 605/999 [04:48&lt;03:30,  1.87it/s] 61%|██████    | 606/999 [04:48&lt;03:36,  1.82it/s] 61%|██████    | 607/999 [04:49&lt;03:31,  1.86it/s] 61%|██████    | 608/999 [04:49&lt;03:26,  1.89it/s] 61%|██████    | 609/999 [04:50&lt;03:23,  1.92it/s] 61%|██████    | 610/999 [04:50&lt;03:20,  1.94it/s] 61%|██████    | 611/999 [04:51&lt;03:18,  1.96it/s] 61%|██████▏   | 612/999 [04:51&lt;03:21,  1.92it/s] 61%|██████▏   | 613/999 [04:52&lt;03:17,  1.96it/s] 61%|██████▏   | 614/999 [04:52&lt;03:20,  1.92it/s] 62%|██████▏   | 615/999 [04:53&lt;03:11,  2.00it/s] 62%|██████▏   | 616/999 [04:53&lt;03:18,  1.93it/s] 62%|██████▏   | 617/999 [04:54&lt;03:17,  1.93it/s] 62%|██████▏   | 618/999 [04:54&lt;03:18,  1.92it/s] 62%|██████▏   | 619/999 [04:55&lt;03:13,  1.96it/s] 62%|██████▏   | 620/999 [04:55&lt;03:16,  1.93it/s] 62%|██████▏   | 621/999 [04:56&lt;03:13,  1.96it/s] 62%|██████▏   | 622/999 [04:56&lt;03:11,  1.97it/s] 62%|██████▏   | 623/999 [04:57&lt;03:12,  1.96it/s] 62%|██████▏   | 624/999 [04:57&lt;03:10,  1.97it/s] 63%|██████▎   | 625/999 [04:58&lt;03:12,  1.94it/s] 63%|██████▎   | 626/999 [04:59&lt;03:12,  1.94it/s] 63%|██████▎   | 627/999 [04:59&lt;03:10,  1.96it/s] 63%|██████▎   | 628/999 [05:00&lt;03:11,  1.93it/s] 63%|██████▎   | 629/999 [05:00&lt;03:12,  1.92it/s] 63%|██████▎   | 630/999 [05:01&lt;03:12,  1.91it/s] 63%|██████▎   | 631/999 [05:01&lt;03:10,  1.94it/s] 63%|██████▎   | 632/999 [05:02&lt;03:02,  2.01it/s] 63%|██████▎   | 633/999 [05:02&lt;03:02,  2.00it/s] 63%|██████▎   | 634/999 [05:03&lt;03:02,  2.00it/s] 64%|██████▎   | 635/999 [05:03&lt;03:01,  2.00it/s] 64%|██████▎   | 636/999 [05:04&lt;03:03,  1.97it/s] 64%|██████▍   | 637/999 [05:04&lt;02:59,  2.01it/s] 64%|██████▍   | 638/999 [05:05&lt;03:02,  1.98it/s] 64%|██████▍   | 639/999 [05:05&lt;03:02,  1.97it/s] 64%|██████▍   | 640/999 [05:06&lt;03:02,  1.96it/s] 64%|██████▍   | 641/999 [05:06&lt;03:07,  1.91it/s] 64%|██████▍   | 642/999 [05:07&lt;03:08,  1.90it/s] 64%|██████▍   | 643/999 [05:07&lt;03:06,  1.91it/s] 64%|██████▍   | 644/999 [05:08&lt;03:05,  1.92it/s] 65%|██████▍   | 645/999 [05:08&lt;03:01,  1.95it/s] 65%|██████▍   | 646/999 [05:09&lt;03:04,  1.91it/s] 65%|██████▍   | 647/999 [05:09&lt;03:03,  1.92it/s] 65%|██████▍   | 648/999 [05:10&lt;02:56,  1.98it/s] 65%|██████▍   | 649/999 [05:10&lt;02:53,  2.01it/s] 65%|██████▌   | 650/999 [05:11&lt;02:48,  2.07it/s] 65%|██████▌   | 651/999 [05:11&lt;02:50,  2.04it/s] 65%|██████▌   | 652/999 [05:12&lt;02:50,  2.03it/s] 65%|██████▌   | 653/999 [05:12&lt;02:53,  1.99it/s] 65%|██████▌   | 654/999 [05:13&lt;02:48,  2.05it/s] 66%|██████▌   | 655/999 [05:13&lt;02:44,  2.10it/s] 66%|██████▌   | 656/999 [05:14&lt;02:48,  2.03it/s] 66%|██████▌   | 657/999 [05:14&lt;02:49,  2.01it/s] 66%|██████▌   | 658/999 [05:15&lt;02:46,  2.05it/s] 66%|██████▌   | 659/999 [05:15&lt;02:44,  2.07it/s] 66%|██████▌   | 660/999 [05:16&lt;02:46,  2.04it/s] 66%|██████▌   | 661/999 [05:16&lt;02:44,  2.05it/s] 66%|██████▋   | 662/999 [05:17&lt;02:45,  2.04it/s] 66%|██████▋   | 663/999 [05:17&lt;02:47,  2.00it/s] 66%|██████▋   | 664/999 [05:18&lt;02:49,  1.97it/s] 67%|██████▋   | 665/999 [05:18&lt;02:47,  2.00it/s] 67%|██████▋   | 666/999 [05:19&lt;02:46,  1.99it/s] 67%|██████▋   | 667/999 [05:19&lt;02:43,  2.03it/s] 67%|██████▋   | 668/999 [05:20&lt;02:43,  2.02it/s] 67%|██████▋   | 669/999 [05:20&lt;02:46,  1.98it/s] 67%|██████▋   | 670/999 [05:21&lt;02:46,  1.97it/s] 67%|██████▋   | 671/999 [05:21&lt;02:43,  2.01it/s] 67%|██████▋   | 672/999 [05:22&lt;02:38,  2.07it/s] 67%|██████▋   | 673/999 [05:22&lt;02:36,  2.08it/s] 67%|██████▋   | 674/999 [05:23&lt;02:35,  2.09it/s] 68%|██████▊   | 675/999 [05:23&lt;02:34,  2.09it/s] 68%|██████▊   | 676/999 [05:23&lt;02:35,  2.08it/s] 68%|██████▊   | 677/999 [05:24&lt;02:36,  2.05it/s] 68%|██████▊   | 678/999 [05:24&lt;02:37,  2.04it/s] 68%|██████▊   | 679/999 [05:25&lt;02:36,  2.05it/s] 68%|██████▊   | 680/999 [05:25&lt;02:38,  2.02it/s] 68%|██████▊   | 681/999 [05:26&lt;02:35,  2.04it/s] 68%|██████▊   | 682/999 [05:26&lt;02:32,  2.08it/s] 68%|██████▊   | 683/999 [05:27&lt;02:35,  2.04it/s] 68%|██████▊   | 684/999 [05:27&lt;02:35,  2.02it/s] 69%|██████▊   | 685/999 [05:28&lt;02:34,  2.04it/s] 69%|██████▊   | 686/999 [05:28&lt;02:34,  2.02it/s] 69%|██████▉   | 687/999 [05:29&lt;02:38,  1.97it/s] 69%|██████▉   | 688/999 [05:29&lt;02:39,  1.94it/s] 69%|██████▉   | 689/999 [05:30&lt;02:37,  1.96it/s] 69%|██████▉   | 690/999 [05:30&lt;02:38,  1.95it/s] 69%|██████▉   | 691/999 [05:31&lt;02:37,  1.95it/s] 69%|██████▉   | 692/999 [05:32&lt;02:36,  1.96it/s] 69%|██████▉   | 693/999 [05:32&lt;02:36,  1.96it/s] 69%|██████▉   | 694/999 [05:32&lt;02:32,  1.99it/s] 70%|██████▉   | 695/999 [05:33&lt;02:35,  1.96it/s] 70%|██████▉   | 696/999 [05:34&lt;02:38,  1.91it/s] 70%|██████▉   | 697/999 [05:34&lt;02:39,  1.90it/s] 70%|██████▉   | 698/999 [05:35&lt;02:38,  1.90it/s] 70%|██████▉   | 699/999 [05:35&lt;02:39,  1.88it/s] 70%|███████   | 700/999 [05:36&lt;02:37,  1.90it/s] 70%|███████   | 701/999 [05:36&lt;02:36,  1.91it/s] 70%|███████   | 702/999 [05:37&lt;02:40,  1.85it/s] 70%|███████   | 703/999 [05:37&lt;02:39,  1.85it/s] 70%|███████   | 704/999 [05:38&lt;02:37,  1.87it/s] 71%|███████   | 705/999 [05:38&lt;02:45,  1.77it/s] 71%|███████   | 706/999 [05:39&lt;02:39,  1.84it/s] 71%|███████   | 707/999 [05:40&lt;02:37,  1.85it/s] 71%|███████   | 708/999 [05:40&lt;02:33,  1.89it/s] 71%|███████   | 709/999 [05:41&lt;02:34,  1.87it/s] 71%|███████   | 710/999 [05:41&lt;02:37,  1.83it/s] 71%|███████   | 711/999 [05:42&lt;02:37,  1.83it/s] 71%|███████▏  | 712/999 [05:42&lt;02:35,  1.85it/s] 71%|███████▏  | 713/999 [05:43&lt;02:30,  1.90it/s] 71%|███████▏  | 714/999 [05:43&lt;02:32,  1.87it/s] 72%|███████▏  | 715/999 [05:44&lt;02:31,  1.87it/s] 72%|███████▏  | 716/999 [05:44&lt;02:33,  1.84it/s] 72%|███████▏  | 717/999 [05:45&lt;02:38,  1.78it/s] 72%|███████▏  | 718/999 [05:46&lt;02:37,  1.79it/s] 72%|███████▏  | 719/999 [05:46&lt;02:39,  1.75it/s] 72%|███████▏  | 720/999 [05:47&lt;02:43,  1.70it/s] 72%|███████▏  | 721/999 [05:47&lt;02:47,  1.66it/s] 72%|███████▏  | 722/999 [05:48&lt;02:50,  1.62it/s] 72%|███████▏  | 723/999 [05:49&lt;02:50,  1.62it/s] 72%|███████▏  | 724/999 [05:49&lt;02:48,  1.63it/s] 73%|███████▎  | 725/999 [05:50&lt;02:46,  1.65it/s] 73%|███████▎  | 726/999 [05:50&lt;02:40,  1.70it/s] 73%|███████▎  | 727/999 [05:51&lt;02:41,  1.68it/s] 73%|███████▎  | 728/999 [05:52&lt;02:41,  1.67it/s] 73%|███████▎  | 729/999 [05:52&lt;02:41,  1.67it/s] 73%|███████▎  | 730/999 [05:53&lt;02:43,  1.64it/s] 73%|███████▎  | 731/999 [05:53&lt;02:41,  1.66it/s] 73%|███████▎  | 732/999 [05:54&lt;02:43,  1.64it/s] 73%|███████▎  | 733/999 [05:55&lt;02:41,  1.65it/s] 73%|███████▎  | 734/999 [05:55&lt;02:37,  1.69it/s] 74%|███████▎  | 735/999 [05:56&lt;02:34,  1.71it/s] 74%|███████▎  | 736/999 [05:56&lt;02:40,  1.64it/s] 74%|███████▍  | 737/999 [05:57&lt;02:40,  1.63it/s] 74%|███████▍  | 738/999 [05:58&lt;02:54,  1.49it/s] 74%|███████▍  | 739/999 [05:59&lt;02:56,  1.47it/s] 74%|███████▍  | 740/999 [05:59&lt;02:56,  1.47it/s] 74%|███████▍  | 741/999 [06:00&lt;02:56,  1.46it/s] 74%|███████▍  | 742/999 [06:01&lt;02:53,  1.48it/s] 74%|███████▍  | 743/999 [06:01&lt;02:48,  1.52it/s] 74%|███████▍  | 744/999 [06:02&lt;02:49,  1.51it/s] 75%|███████▍  | 745/999 [06:03&lt;02:44,  1.54it/s] 75%|███████▍  | 746/999 [06:03&lt;02:41,  1.57it/s] 75%|███████▍  | 747/999 [06:04&lt;02:40,  1.57it/s] 75%|███████▍  | 748/999 [06:04&lt;02:44,  1.53it/s] 75%|███████▍  | 749/999 [06:05&lt;02:41,  1.55it/s] 75%|███████▌  | 750/999 [06:06&lt;02:43,  1.52it/s] 75%|███████▌  | 751/999 [06:06&lt;02:43,  1.52it/s] 75%|███████▌  | 752/999 [06:07&lt;02:39,  1.54it/s] 75%|███████▌  | 753/999 [06:08&lt;02:40,  1.53it/s] 75%|███████▌  | 754/999 [06:08&lt;02:41,  1.52it/s] 76%|███████▌  | 755/999 [06:09&lt;02:40,  1.52it/s] 76%|███████▌  | 756/999 [06:10&lt;02:37,  1.55it/s] 76%|███████▌  | 757/999 [06:10&lt;02:42,  1.49it/s] 76%|███████▌  | 758/999 [06:11&lt;02:42,  1.48it/s] 76%|███████▌  | 759/999 [06:12&lt;02:38,  1.51it/s] 76%|███████▌  | 760/999 [06:12&lt;02:40,  1.49it/s] 76%|███████▌  | 761/999 [06:13&lt;02:43,  1.46it/s] 76%|███████▋  | 762/999 [06:14&lt;02:43,  1.45it/s] 76%|███████▋  | 763/999 [06:15&lt;02:46,  1.42it/s] 76%|███████▋  | 764/999 [06:15&lt;02:49,  1.38it/s] 77%|███████▋  | 765/999 [06:16&lt;02:45,  1.41it/s] 77%|███████▋  | 766/999 [06:17&lt;02:43,  1.43it/s] 77%|███████▋  | 767/999 [06:17&lt;02:43,  1.42it/s] 77%|███████▋  | 768/999 [06:18&lt;02:42,  1.42it/s] 77%|███████▋  | 769/999 [06:19&lt;02:41,  1.43it/s] 77%|███████▋  | 770/999 [06:20&lt;02:41,  1.42it/s] 77%|███████▋  | 771/999 [06:20&lt;02:35,  1.46it/s] 77%|███████▋  | 772/999 [06:21&lt;02:32,  1.49it/s] 77%|███████▋  | 773/999 [06:21&lt;02:31,  1.50it/s] 77%|███████▋  | 774/999 [06:22&lt;02:32,  1.48it/s] 78%|███████▊  | 775/999 [06:23&lt;02:30,  1.49it/s] 78%|███████▊  | 776/999 [06:23&lt;02:31,  1.47it/s] 78%|███████▊  | 777/999 [06:24&lt;02:31,  1.47it/s] 78%|███████▊  | 778/999 [06:25&lt;02:33,  1.44it/s] 78%|███████▊  | 779/999 [06:26&lt;02:29,  1.47it/s] 78%|███████▊  | 780/999 [06:26&lt;02:27,  1.48it/s] 78%|███████▊  | 781/999 [06:27&lt;02:30,  1.44it/s] 78%|███████▊  | 782/999 [06:28&lt;02:29,  1.46it/s] 78%|███████▊  | 783/999 [06:28&lt;02:29,  1.45it/s] 78%|███████▊  | 784/999 [06:29&lt;02:28,  1.45it/s] 79%|███████▊  | 785/999 [06:30&lt;02:32,  1.41it/s] 79%|███████▊  | 786/999 [06:31&lt;02:35,  1.37it/s] 79%|███████▉  | 787/999 [06:31&lt;02:32,  1.39it/s] 79%|███████▉  | 788/999 [06:32&lt;02:31,  1.39it/s] 79%|███████▉  | 789/999 [06:33&lt;02:30,  1.39it/s] 79%|███████▉  | 790/999 [06:33&lt;02:29,  1.39it/s] 79%|███████▉  | 791/999 [06:34&lt;02:25,  1.43it/s] 79%|███████▉  | 792/999 [06:35&lt;02:22,  1.45it/s] 79%|███████▉  | 793/999 [06:35&lt;02:20,  1.47it/s] 79%|███████▉  | 794/999 [06:36&lt;02:21,  1.45it/s] 80%|███████▉  | 795/999 [06:37&lt;02:17,  1.48it/s] 80%|███████▉  | 796/999 [06:37&lt;02:17,  1.48it/s] 80%|███████▉  | 797/999 [06:38&lt;02:17,  1.47it/s] 80%|███████▉  | 798/999 [06:39&lt;02:20,  1.43it/s] 80%|███████▉  | 799/999 [06:40&lt;02:24,  1.38it/s] 80%|████████  | 800/999 [06:40&lt;02:26,  1.36it/s] 80%|████████  | 801/999 [06:41&lt;02:24,  1.37it/s] 80%|████████  | 802/999 [06:42&lt;02:23,  1.37it/s] 80%|████████  | 803/999 [06:43&lt;02:24,  1.36it/s] 80%|████████  | 804/999 [06:43&lt;02:25,  1.34it/s] 81%|████████  | 805/999 [06:44&lt;02:24,  1.34it/s] 81%|████████  | 806/999 [06:45&lt;02:23,  1.34it/s] 81%|████████  | 807/999 [06:46&lt;02:20,  1.37it/s] 81%|████████  | 808/999 [06:46&lt;02:17,  1.38it/s] 81%|████████  | 809/999 [06:47&lt;02:15,  1.40it/s] 81%|████████  | 810/999 [06:48&lt;02:11,  1.44it/s] 81%|████████  | 811/999 [06:48&lt;02:11,  1.43it/s] 81%|████████▏ | 812/999 [06:49&lt;02:10,  1.43it/s] 81%|████████▏ | 813/999 [06:50&lt;02:10,  1.42it/s] 81%|████████▏ | 814/999 [06:50&lt;02:13,  1.39it/s] 82%|████████▏ | 815/999 [06:51&lt;02:15,  1.36it/s] 82%|████████▏ | 816/999 [06:52&lt;02:16,  1.34it/s] 82%|████████▏ | 817/999 [06:53&lt;02:16,  1.33it/s] 82%|████████▏ | 818/999 [06:54&lt;02:14,  1.35it/s] 82%|████████▏ | 819/999 [06:54&lt;02:17,  1.31it/s] 82%|████████▏ | 820/999 [06:55&lt;02:15,  1.32it/s] 82%|████████▏ | 821/999 [06:56&lt;02:13,  1.33it/s] 82%|████████▏ | 822/999 [06:57&lt;02:10,  1.35it/s] 82%|████████▏ | 823/999 [06:57&lt;02:09,  1.35it/s] 82%|████████▏ | 824/999 [06:58&lt;02:09,  1.35it/s] 83%|████████▎ | 825/999 [06:59&lt;02:10,  1.33it/s] 83%|████████▎ | 826/999 [06:59&lt;02:06,  1.37it/s] 83%|████████▎ | 827/999 [07:00&lt;02:01,  1.42it/s] 83%|████████▎ | 828/999 [07:01&lt;02:00,  1.41it/s] 83%|████████▎ | 829/999 [07:02&lt;02:01,  1.39it/s] 83%|████████▎ | 830/999 [07:02&lt;01:59,  1.42it/s] 83%|████████▎ | 831/999 [07:03&lt;01:56,  1.44it/s] 83%|████████▎ | 832/999 [07:04&lt;01:56,  1.43it/s] 83%|████████▎ | 833/999 [07:04&lt;01:51,  1.48it/s] 83%|████████▎ | 834/999 [07:05&lt;01:49,  1.51it/s] 84%|████████▎ | 835/999 [07:06&lt;01:50,  1.48it/s] 84%|████████▎ | 836/999 [07:06&lt;01:50,  1.48it/s] 84%|████████▍ | 837/999 [07:07&lt;01:51,  1.45it/s] 84%|████████▍ | 838/999 [07:08&lt;01:51,  1.44it/s] 84%|████████▍ | 839/999 [07:08&lt;01:51,  1.43it/s] 84%|████████▍ | 840/999 [07:09&lt;01:50,  1.44it/s] 84%|████████▍ | 841/999 [07:10&lt;01:48,  1.46it/s] 84%|████████▍ | 842/999 [07:10&lt;01:47,  1.46it/s] 84%|████████▍ | 843/999 [07:11&lt;01:45,  1.48it/s] 84%|████████▍ | 844/999 [07:12&lt;01:43,  1.50it/s] 85%|████████▍ | 845/999 [07:12&lt;01:40,  1.53it/s] 85%|████████▍ | 846/999 [07:13&lt;01:43,  1.48it/s] 85%|████████▍ | 847/999 [07:14&lt;01:42,  1.49it/s] 85%|████████▍ | 848/999 [07:14&lt;01:41,  1.48it/s] 85%|████████▍ | 849/999 [07:15&lt;01:42,  1.46it/s] 85%|████████▌ | 850/999 [07:16&lt;01:40,  1.48it/s] 85%|████████▌ | 851/999 [07:16&lt;01:41,  1.46it/s] 85%|████████▌ | 852/999 [07:17&lt;01:40,  1.46it/s] 85%|████████▌ | 853/999 [07:18&lt;01:38,  1.49it/s] 85%|████████▌ | 854/999 [07:18&lt;01:34,  1.53it/s] 86%|████████▌ | 855/999 [07:19&lt;01:34,  1.52it/s] 86%|████████▌ | 856/999 [07:20&lt;01:32,  1.55it/s] 86%|████████▌ | 857/999 [07:20&lt;01:29,  1.58it/s] 86%|████████▌ | 858/999 [07:21&lt;01:29,  1.57it/s] 86%|████████▌ | 859/999 [07:22&lt;01:29,  1.57it/s] 86%|████████▌ | 860/999 [07:22&lt;01:31,  1.52it/s] 86%|████████▌ | 861/999 [07:23&lt;01:32,  1.50it/s] 86%|████████▋ | 862/999 [07:24&lt;01:29,  1.54it/s] 86%|████████▋ | 863/999 [07:24&lt;01:29,  1.52it/s] 86%|████████▋ | 864/999 [07:25&lt;01:30,  1.48it/s] 87%|████████▋ | 865/999 [07:26&lt;01:31,  1.47it/s] 87%|████████▋ | 866/999 [07:26&lt;01:30,  1.48it/s] 87%|████████▋ | 867/999 [07:27&lt;01:30,  1.46it/s] 87%|████████▋ | 868/999 [07:28&lt;01:27,  1.50it/s] 87%|████████▋ | 869/999 [07:28&lt;01:26,  1.51it/s] 87%|████████▋ | 870/999 [07:29&lt;01:24,  1.53it/s] 87%|████████▋ | 871/999 [07:30&lt;01:24,  1.51it/s] 87%|████████▋ | 872/999 [07:30&lt;01:24,  1.50it/s] 87%|████████▋ | 873/999 [07:31&lt;01:24,  1.48it/s] 87%|████████▋ | 874/999 [07:32&lt;01:23,  1.50it/s] 88%|████████▊ | 875/999 [07:32&lt;01:23,  1.49it/s] 88%|████████▊ | 876/999 [07:33&lt;01:22,  1.48it/s] 88%|████████▊ | 877/999 [07:34&lt;01:23,  1.47it/s] 88%|████████▊ | 878/999 [07:34&lt;01:22,  1.47it/s] 88%|████████▊ | 879/999 [07:35&lt;01:21,  1.47it/s] 88%|████████▊ | 880/999 [07:36&lt;01:20,  1.47it/s] 88%|████████▊ | 881/999 [07:36&lt;01:20,  1.47it/s] 88%|████████▊ | 882/999 [07:37&lt;01:20,  1.46it/s] 88%|████████▊ | 883/999 [07:38&lt;01:18,  1.47it/s] 88%|████████▊ | 884/999 [07:38&lt;01:17,  1.48it/s] 89%|████████▊ | 885/999 [07:39&lt;01:15,  1.52it/s] 89%|████████▊ | 886/999 [07:40&lt;01:14,  1.51it/s] 89%|████████▉ | 887/999 [07:40&lt;01:14,  1.51it/s] 89%|████████▉ | 888/999 [07:41&lt;01:16,  1.46it/s] 89%|████████▉ | 889/999 [07:42&lt;01:14,  1.48it/s] 89%|████████▉ | 890/999 [07:42&lt;01:12,  1.50it/s] 89%|████████▉ | 891/999 [07:43&lt;01:10,  1.54it/s] 89%|████████▉ | 892/999 [07:44&lt;01:08,  1.56it/s] 89%|████████▉ | 893/999 [07:44&lt;01:07,  1.57it/s] 89%|████████▉ | 894/999 [07:45&lt;01:07,  1.56it/s] 90%|████████▉ | 895/999 [07:46&lt;01:06,  1.56it/s] 90%|████████▉ | 896/999 [07:46&lt;01:05,  1.57it/s] 90%|████████▉ | 897/999 [07:47&lt;01:05,  1.57it/s] 90%|████████▉ | 898/999 [07:48&lt;01:04,  1.56it/s] 90%|████████▉ | 899/999 [07:48&lt;01:03,  1.57it/s] 90%|█████████ | 900/999 [07:49&lt;01:02,  1.57it/s] 90%|█████████ | 901/999 [07:49&lt;01:02,  1.56it/s] 90%|█████████ | 902/999 [07:50&lt;01:02,  1.56it/s] 90%|█████████ | 903/999 [07:51&lt;01:00,  1.58it/s] 90%|█████████ | 904/999 [07:51&lt;01:00,  1.58it/s] 91%|█████████ | 905/999 [07:52&lt;01:00,  1.55it/s] 91%|█████████ | 906/999 [07:53&lt;00:59,  1.56it/s] 91%|█████████ | 907/999 [07:53&lt;00:58,  1.56it/s] 91%|█████████ | 908/999 [07:54&lt;00:57,  1.57it/s] 91%|█████████ | 909/999 [07:55&lt;00:58,  1.54it/s] 91%|█████████ | 910/999 [07:55&lt;00:59,  1.51it/s] 91%|█████████ | 911/999 [07:56&lt;00:58,  1.51it/s] 91%|█████████▏| 912/999 [07:57&lt;00:58,  1.50it/s] 91%|█████████▏| 913/999 [07:57&lt;00:57,  1.49it/s] 91%|█████████▏| 914/999 [07:58&lt;00:56,  1.52it/s] 92%|█████████▏| 915/999 [07:59&lt;00:55,  1.51it/s] 92%|█████████▏| 916/999 [07:59&lt;00:55,  1.51it/s] 92%|█████████▏| 917/999 [08:00&lt;00:54,  1.49it/s] 92%|█████████▏| 918/999 [08:01&lt;00:54,  1.49it/s] 92%|█████████▏| 919/999 [08:01&lt;00:53,  1.48it/s] 92%|█████████▏| 920/999 [08:02&lt;00:52,  1.51it/s] 92%|█████████▏| 921/999 [08:03&lt;00:50,  1.55it/s] 92%|█████████▏| 922/999 [08:03&lt;00:49,  1.56it/s] 92%|█████████▏| 923/999 [08:04&lt;00:48,  1.56it/s] 92%|█████████▏| 924/999 [08:04&lt;00:47,  1.57it/s] 93%|█████████▎| 925/999 [08:05&lt;00:47,  1.56it/s] 93%|█████████▎| 926/999 [08:06&lt;00:47,  1.55it/s] 93%|█████████▎| 927/999 [08:06&lt;00:46,  1.54it/s] 93%|█████████▎| 928/999 [08:07&lt;00:46,  1.54it/s] 93%|█████████▎| 929/999 [08:08&lt;00:45,  1.53it/s] 93%|█████████▎| 930/999 [08:08&lt;00:45,  1.53it/s] 93%|█████████▎| 931/999 [08:09&lt;00:44,  1.53it/s] 93%|█████████▎| 932/999 [08:10&lt;00:43,  1.54it/s] 93%|█████████▎| 933/999 [08:10&lt;00:43,  1.52it/s] 93%|█████████▎| 934/999 [08:11&lt;00:42,  1.53it/s] 94%|█████████▎| 935/999 [08:12&lt;00:41,  1.55it/s] 94%|█████████▎| 936/999 [08:12&lt;00:40,  1.54it/s] 94%|█████████▍| 937/999 [08:13&lt;00:39,  1.56it/s] 94%|█████████▍| 938/999 [08:13&lt;00:37,  1.61it/s] 94%|█████████▍| 939/999 [08:14&lt;00:37,  1.58it/s] 94%|█████████▍| 940/999 [08:15&lt;00:36,  1.60it/s] 94%|█████████▍| 941/999 [08:15&lt;00:35,  1.62it/s] 94%|█████████▍| 942/999 [08:16&lt;00:35,  1.60it/s] 94%|█████████▍| 943/999 [08:17&lt;00:35,  1.59it/s] 94%|█████████▍| 944/999 [08:17&lt;00:34,  1.58it/s] 95%|█████████▍| 945/999 [08:18&lt;00:34,  1.58it/s] 95%|█████████▍| 946/999 [08:19&lt;00:33,  1.59it/s] 95%|█████████▍| 947/999 [08:19&lt;00:30,  1.68it/s] 95%|█████████▍| 948/999 [08:20&lt;00:30,  1.65it/s] 95%|█████████▍| 949/999 [08:20&lt;00:30,  1.65it/s] 95%|█████████▌| 950/999 [08:21&lt;00:30,  1.62it/s] 95%|█████████▌| 951/999 [08:22&lt;00:30,  1.56it/s] 95%|█████████▌| 952/999 [08:22&lt;00:30,  1.52it/s] 95%|█████████▌| 953/999 [08:23&lt;00:30,  1.51it/s] 95%|█████████▌| 954/999 [08:24&lt;00:30,  1.46it/s] 96%|█████████▌| 955/999 [08:24&lt;00:30,  1.45it/s] 96%|█████████▌| 956/999 [08:25&lt;00:29,  1.45it/s] 96%|█████████▌| 957/999 [08:26&lt;00:29,  1.43it/s] 96%|█████████▌| 958/999 [08:27&lt;00:28,  1.43it/s] 96%|█████████▌| 959/999 [08:27&lt;00:28,  1.42it/s] 96%|█████████▌| 960/999 [08:28&lt;00:26,  1.45it/s] 96%|█████████▌| 961/999 [08:29&lt;00:26,  1.44it/s] 96%|█████████▋| 962/999 [08:29&lt;00:25,  1.44it/s] 96%|█████████▋| 963/999 [08:30&lt;00:25,  1.41it/s] 96%|█████████▋| 964/999 [08:31&lt;00:25,  1.37it/s] 97%|█████████▋| 965/999 [08:32&lt;00:24,  1.39it/s] 97%|█████████▋| 966/999 [08:32&lt;00:23,  1.41it/s] 97%|█████████▋| 967/999 [08:33&lt;00:22,  1.44it/s] 97%|█████████▋| 968/999 [08:34&lt;00:21,  1.45it/s] 97%|█████████▋| 969/999 [08:34&lt;00:21,  1.41it/s] 97%|█████████▋| 970/999 [08:35&lt;00:20,  1.44it/s] 97%|█████████▋| 971/999 [08:36&lt;00:19,  1.44it/s] 97%|█████████▋| 972/999 [08:36&lt;00:18,  1.42it/s] 97%|█████████▋| 973/999 [08:37&lt;00:18,  1.42it/s] 97%|█████████▋| 974/999 [08:38&lt;00:18,  1.35it/s] 98%|█████████▊| 975/999 [08:39&lt;00:17,  1.36it/s] 98%|█████████▊| 976/999 [08:39&lt;00:16,  1.41it/s] 98%|█████████▊| 977/999 [08:40&lt;00:16,  1.36it/s] 98%|█████████▊| 978/999 [08:41&lt;00:15,  1.34it/s] 98%|█████████▊| 979/999 [08:42&lt;00:14,  1.37it/s] 98%|█████████▊| 980/999 [08:42&lt;00:13,  1.40it/s] 98%|█████████▊| 981/999 [08:43&lt;00:13,  1.38it/s] 98%|█████████▊| 982/999 [08:44&lt;00:12,  1.38it/s] 98%|█████████▊| 983/999 [08:44&lt;00:11,  1.41it/s] 98%|█████████▊| 984/999 [08:45&lt;00:10,  1.40it/s] 99%|█████████▊| 985/999 [08:46&lt;00:10,  1.39it/s] 99%|█████████▊| 986/999 [08:46&lt;00:09,  1.43it/s] 99%|█████████▉| 987/999 [08:47&lt;00:08,  1.42it/s] 99%|█████████▉| 988/999 [08:48&lt;00:07,  1.40it/s] 99%|█████████▉| 989/999 [08:49&lt;00:07,  1.42it/s] 99%|█████████▉| 990/999 [08:49&lt;00:06,  1.43it/s] 99%|█████████▉| 991/999 [08:50&lt;00:05,  1.41it/s] 99%|█████████▉| 992/999 [08:51&lt;00:04,  1.42it/s] 99%|█████████▉| 993/999 [08:51&lt;00:04,  1.38it/s] 99%|█████████▉| 994/999 [08:52&lt;00:03,  1.39it/s]100%|█████████▉| 995/999 [08:53&lt;00:02,  1.42it/s]100%|█████████▉| 996/999 [08:54&lt;00:02,  1.41it/s]100%|█████████▉| 997/999 [08:54&lt;00:01,  1.39it/s]100%|█████████▉| 998/999 [08:55&lt;00:00,  1.34it/s]100%|██████████| 999/999 [08:56&lt;00:00,  1.39it/s]100%|██████████| 999/999 [08:56&lt;00:00,  1.86it/s]</code></pre>
</div>
</div>
<div id="ebe9eacf" class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb57-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb57-2">plt.scatter(quantiles_theoriques, quantiles_empiriques)</span>
<span id="cb57-3">plt.plot(quantiles_theoriques, quantiles_theoriques, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Première bissectrice'</span>)</span>
<span id="cb57-4">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'QQ Plot - Quantiles empiriques vs Quantiles théoriques'</span>)</span>
<span id="cb57-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles théoriques (distribution Skew Student)'</span>)</span>
<span id="cb57-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles empiriques'</span>)</span>
<span id="cb57-7">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb57-8">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-38-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="20dd39f6" class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb58-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># # kstest y revenir</span></span>
<span id="cb58-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ks_stat, ks_p_value = kstest(data_train, skew_student_pdf, args=(**params_sstd,))</span></span>
<span id="cb58-3"></span>
<span id="cb58-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("="*80)</span></span>
<span id="cb58-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("H0 : Les données suivent une loi de Skew Student")</span></span>
<span id="cb58-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"Statistique de test : {ks_stat:.4f}")</span></span>
<span id="cb58-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print(f"P-value : {ks_p_value:.4f}")</span></span>
<span id="cb58-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print("="*80)</span></span>
<span id="cb58-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A revoir</span></span></code></pre></div>
</div>
</section>
<section id="ii.4.2.-calcul-de-la-var-skew-student" class="level3">
<h3 class="anchored" data-anchor-id="ii.4.2.-calcul-de-la-var-skew-student">II.4.2. Calcul de la VaR Skew Student</h3>
<div id="5a000c54" class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb59-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Objectif : écrire une fonction qui calcule la VaR skew-student</span></span>
<span id="cb59-2"></span>
<span id="cb59-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sstd_var_fct(alpha, params):</span>
<span id="cb59-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb59-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de la VaR skew student</span></span>
<span id="cb59-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb59-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    alpha : le niveau de confiance</span></span>
<span id="cb59-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb59-9"></span>
<span id="cb59-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>skew_student_quantile(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span></code></pre></div>
</div>
<div id="86892cb9" class="cell" data-execution_count="40">
<div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb60-1">sstd_var <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sstd_var_fct(alpha, params_sstd)</span>
<span id="cb60-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"La VaR skew student pour h=1j et alpha=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sstd_var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>La VaR skew student pour h=1j et alpha=0.99 est : 4.2575%</code></pre>
</div>
</div>
</section>
</section>
</section>
<section id="iii.-implémentation-de-les" class="level1">
<h1>III. Implémentation de l’ES</h1>
<p>L’expected shortfall (ES) est une mesure de risque qui donne une estimation de la perte moyenne au-delà du seuil de la VaR. Par exemple, un ES à 5% sur 1 jour de 1000 euros signifie que si la perte dépasse 1000 euros, alors la perte moyenne sera de 1500 euros. L’ES est une mesure plus robuste que la VaR, car elle prend en compte les queues de distribution et est particulièrement utile pour évaluer les risques extrêmes. Elle est définie comme suit :</p>
<p><img src="https://latex.codecogs.com/png.latex?ES_%7B%5Calpha%7D(h)%20=%20E(PnL%20%7C%20PnL%20%5Cleq%20-VaR_%7B%5Calpha%7D(h))"></p>
<p>/!&nbsp;La ES du cours est différente de celle ci. Elle est définie comme suit : <img src="https://latex.codecogs.com/png.latex?ES_%7B%5Calpha%7D(h)%20=%20E(PnL%20-%20VaR%20%7C%20PnL%20%5Cleq%20-VaR_%7B%5Calpha%7D(h))%20=%20CTE%20-%20VaR"></p>
<p>Voir ce qui est fait dans le cadre réglementaire.</p>
<div id="79cc0359" class="cell" data-execution_count="41">
<div class="sourceCode cell-code" id="cb62" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb62-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected Shortfall pour la VaR historique, bootstrap et gaussienne, et skew-student</span></span>
<span id="cb62-2"></span>
<span id="cb62-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> expected_shortfall(data, var):</span>
<span id="cb62-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb62-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcul de l'Expected Shortfall</span></span>
<span id="cb62-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    data : les rendements logarithmiques</span></span>
<span id="cb62-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    var : la VaR</span></span>
<span id="cb62-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb62-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>data[data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>var].mean()</span>
<span id="cb62-10"></span>
<span id="cb62-11">es_hist_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall(data_train, var_hist_train)</span>
<span id="cb62-12">es_bs_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall(data_train, var_bs_train)</span>
<span id="cb62-13">es_gauss_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall(data_train, var_gauss_train)</span>
<span id="cb62-14">es_sstd_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall(data_train, sstd_var)</span>
<span id="cb62-15"></span>
<span id="cb62-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in a df</span></span>
<span id="cb62-17">es <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb62-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Historique'</span>: [es_hist_train],</span>
<span id="cb62-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bootstrap'</span>: [es_bs_train],</span>
<span id="cb62-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Gaussienne'</span>: [es_gauss_train],</span>
<span id="cb62-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Skew Student'</span>: [es_sstd_train]</span>
<span id="cb62-22">})</span>
<span id="cb62-23"></span>
<span id="cb62-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb62-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected Shortfall empirique (en %) pour h=1j"</span>)</span>
<span id="cb62-26"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>es,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb62-27"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Expected Shortfall empirique (en %) pour h=1j
   Historique  Bootstrap  Gaussienne  Skew Student
0        5.47       5.51        4.47          5.68
================================================================================</code></pre>
</div>
</div>
<div id="f55c5a18" class="cell" data-execution_count="42">
<div class="sourceCode cell-code" id="cb64" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb64-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> stats</span>
<span id="cb64-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scipy.integrate <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> integrate</span>
<span id="cb64-3"></span>
<span id="cb64-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> expected_shortfall_theoretical(alpha, quantile_function, density_function,params):</span>
<span id="cb64-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb64-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Computes the Expected Shortfall (Conditional VaR) for a given distribution.</span></span>
<span id="cb64-7"></span>
<span id="cb64-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Parameters:</span></span>
<span id="cb64-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - alpha (float): Confidence level (e.g., 0.99 for 99%)</span></span>
<span id="cb64-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - quantile_function (callable): Function to compute quantiles (e.g., skew_student_quantile)</span></span>
<span id="cb64-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - params (dict): Parameters of the distribution</span></span>
<span id="cb64-12"></span>
<span id="cb64-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Returns:</span></span>
<span id="cb64-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    - float: Expected Shortfall value</span></span>
<span id="cb64-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb64-16"></span>
<span id="cb64-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the Value at Risk (VaR) at level alpha</span></span>
<span id="cb64-18">    var_alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quantile_function(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>alpha, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params)</span>
<span id="cb64-19"></span>
<span id="cb64-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected Shortfall integral</span></span>
<span id="cb64-21">    integral_value, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> integrate.quad(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> density_function(x, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>params), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inf"</span>), var_alpha)</span>
<span id="cb64-22"></span>
<span id="cb64-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute Expected Shortfall</span></span>
<span id="cb64-24">    es_alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>integral_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha)</span>
<span id="cb64-25"></span>
<span id="cb64-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> es_alpha</span>
<span id="cb64-27"></span>
<span id="cb64-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute Expected Shortfall using the corrected function</span></span>
<span id="cb64-29">alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span></span>
<span id="cb64-30">es_sstd_theoretical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall_theoretical(alpha,skew_student_quantile,skew_student_pdf, params_sstd)</span>
<span id="cb64-31"></span>
<span id="cb64-32">params_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb64-33">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loc"</span> : np.mean(data_train),</span>
<span id="cb64-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale"</span> : np.std(data_train)</span>
<span id="cb64-35">}</span>
<span id="cb64-36"></span>
<span id="cb64-37">es_gauss_theoretical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expected_shortfall_theoretical(alpha,stats.norm.ppf,stats.norm.pdf, params_norm)</span>
<span id="cb64-38"></span>
<span id="cb64-39">es_theoretical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb64-40">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Gaussienne'</span>: [es_gauss_theoretical],</span>
<span id="cb64-41">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Skew Student'</span>: [es_sstd_theoretical]</span>
<span id="cb64-42">})</span>
<span id="cb64-43"></span>
<span id="cb64-44"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb64-45"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected Shortfall théorique (en %) pour h=1j"</span>)</span>
<span id="cb64-46"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>es_theoretical,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb64-47"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Expected Shortfall théorique (en %) pour h=1j
   Gaussienne  Skew Student
0         3.7          6.71
================================================================================</code></pre>
</div>
</div>
</section>
<section id="iv.protocole-de-backtesting" class="level1">
<h1>IV.Protocole de Backtesting</h1>
<p>L’exercice de backtesting consiste à vérifier la capacité d’adaptation du modèle et sa viabilité en dehors de son cadre d’entraînement. Il permet d’évaluer la performance des modèles dans le temps. Idéalement, il faudrait le realiser quotidiennement afin d’évaluer s’il est utile ou non de recalibrer le modèle.</p>
<p>Dans le cadre de TP, nous n’avons pas observé d’exceptions avec l’utilisation de la VaR historique. Cela est probablement du à la période d’apprentissage trop stricte. Notre modèle était donc trop restrictif/conservateur ce qui peut témoigner de son incapacité d’adaptation. Pour ce faire, il aurait été necessaire de le recalibrer.</p>
<p>Sachant que les modèles sont évalués quotidiennement, à quel moment doit-on considérer qu’il est nécessaire de recalibrer le modèle ? Quels indicateurs pourrait-on mettre en place pour déterminer si tout est sous contrôle (VaR trop haute, trop basse) ou si un recalibrage est requis, sachant que le modèle est backtesté chaque jour ?</p>
<p>Pour repondre à ces questions, nous proposons le protocole de backtest ci-dessous :</p>
<ul>
<li><p>Dans la réglementation baloise, il est demandé de tester les performances de la VaR sur les 250 jours suivants la date de réalisation du modèle. Dans ces 250 jours, on est censé observé 2 à 3 exceptions, soit 1%. Lorsqu’il n’y a aucune exception sur cette période de temps, nous jugerons que le modèle est mal calibré.</p></li>
<li><p>Lorsqu’il y a des exceptions, nous pouvons comparer ces exceptions théoriques aux exceptions observées à l’aide d’un test de Kupiec/Unconditionnal coverage [1], une variante du test binomiale qui permet de tester sous l’hypothèse nulle que le taux d’exception(p) est égal au niveau de confiance de la VaR (1-<img src="https://latex.codecogs.com/png.latex?%5Calpha">). La statistique de test suit une loi de khi-deux à 1 degré de liberté. Lorsque cette hypothèse est rejetée, cela signifie que le modèle est mal calibré. En effet, si une banque a trop d’exceptions, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?p%20%5Cgeq%201%20-%20%5Calpha">, cela signifie que son modèle sous-estime le risque, ce qui peut conduire à une exigence de recalibrage ou une augmentation du capital réglementaire. Si une banque a trop peu d’exceptions (voire 0), i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?p%20%5Cleq%201%20-%20%5Calpha">, cela peut indiquer que son modèle est trop conservateur, ce qui immobilise trop de capital inutilement.</p></li>
<li><p>Puisqu’un seul test de backtesting n’est pas suffisant[2] pour juger la qualité d’un modèle de VaR, il serait préférable de combiner différentes méthodes. Pour ce faire, un autre test a été utilisé pour juger de la necessité de recalibrage du modèle. Il s’agit du test de Christoffersen’s Interval Forecast/Independance test [3]. Ce test permet de vérifier si les exceptions sont indépendantes et donc bien réparties dans le temps. En effet, si les exceptions sont concentrées dans un intervalle de temps, cela peut indiquer que le modèle est mal calibré. Sous l’hypothèse nulle, la statistique de test suit asymptotiquement une loi de khi-deux à 1 degré de liberté.</p></li>
<li><p>En couplant le test de Kupiec au test de Christoffersen [3], nous avons le test de conditional coverage mixte[3] qui suit une loi de khi-deux à 2 degrés de liberté.</p></li>
</ul>
<p>Pour la mise en place de ces tests statistiques, nous avons besoin d’une échantillon de taille suffisamment grande, i.e <img src="https://latex.codecogs.com/png.latex?N=30">. Pour ce faire, nous ne pourrons mettre en place ce protocole qu’à partir du 30ème jour de backtesting. Pour le backtest, nous avons fait le choix d’utiliser un expanding window, i.e.&nbsp;une fenêtre d’apprentissage qui s’agrandit à chaque période de recalibrage. Il aurait été plus pertinent de privilégier un rolling window, i.e.&nbsp;une fenêtre d’apprentissage qui se déplace à chaque période de recalibrage, pour éviter de bruiter les données.</p>
<p>PROBLÈME : au bout de 30j, s’il n’y a pas d’exceptions, on ne recalibre pas le modèle mais si au 31e jour il y a une exception, le test de kupiec echoue=&gt; recalibrage. <strong><em>References</em></strong>: - [1] Kupiec, P. “Techniques for Verifying the Accuracy of Risk Management Models.” Journal of Derivatives. Vol. 3, 1995, pp.&nbsp;73–84. - [2] Haas, M. “New Methods in Backtesting.” Financial Engineering, Research Center Caesar, Bonn, 2001. - [3] Christoffersen, P. “Evaluating Interval Forecasts.” International Economic Review. Vol. 39, 1998, pp.&nbsp;841–862.</p>
<div id="f9751825" class="cell" data-execution_count="43">
<div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb66-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb66-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb66-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> chi2</span>
<span id="cb66-4"></span>
<span id="cb66-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Backtest:</span>
<span id="cb66-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, actual, forecast, alpha, alpha_test):</span>
<span id="cb66-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> actual.index</span>
<span id="cb66-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.actual <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> actual.values</span>
<span id="cb66-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.forecast <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> forecast</span>
<span id="cb66-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alpha</span>
<span id="cb66-11">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alpha_test</span>
<span id="cb66-12"></span>
<span id="cb66-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> hit_series(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb66-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">""" Returns a series of 1s (VaR breaches) and 0s (no breach) """</span></span>
<span id="cb66-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.actual <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.forecast).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb66-16">    </span>
<span id="cb66-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lr_bt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb66-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">""" Back-test protocol """</span></span>
<span id="cb66-19">        hits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.hit_series()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Série des violations de la VaR (1 si violation, 0 sinon)</span></span>
<span id="cb66-20">        transitions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hits[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Différences entre valeurs successives (1 si passage de 0 à 1, -1 si passage de 1 à 0)</span></span>
<span id="cb66-21"></span>
<span id="cb66-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transition counts</span></span>
<span id="cb66-23">        n01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(transitions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0 -&gt; 1</span></span>
<span id="cb66-24">        n10 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(transitions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 -&gt; 0</span></span>
<span id="cb66-25">        n11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((transitions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (hits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 -&gt; 1</span></span>
<span id="cb66-26">        n00 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>((transitions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (hits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0 -&gt; 0</span></span>
<span id="cb66-27">        </span>
<span id="cb66-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of observations in each state</span></span>
<span id="cb66-29">        n0, n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n00, n10 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n11</span>
<span id="cb66-30">        total_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n1</span>
<span id="cb66-31">        </span>
<span id="cb66-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Probability estimates</span></span>
<span id="cb66-33">        p01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n00 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n01) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n00 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n01) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb66-34">        p11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n10) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n10) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb66-35">        p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> total_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb66-36">        </span>
<span id="cb66-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb66-38">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb66-39"></span>
<span id="cb66-40">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unconditional Coverage Test/Kupiec</span></span>
<span id="cb66-41">            uc_h0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha)</span>
<span id="cb66-42">            uc_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(p)</span>
<span id="cb66-43">            uc_stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (uc_h0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> uc_h1)</span>
<span id="cb66-44">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># uc_critical_val = chi2.ppf(self.alpha_test, 1)</span></span>
<span id="cb66-45">            uc_pval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> chi2.cdf(uc_stat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb66-46">            </span>
<span id="cb66-47">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Independence Test/Christoffersen</span></span>
<span id="cb66-48">            ind_h0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n00 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n01) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (n01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n11) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(p)</span>
<span id="cb66-49">            ind_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb66-50">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>: ind_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n01 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(p01)</span>
<span id="cb66-51">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p01) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>: ind_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n00 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p01)</span>
<span id="cb66-52">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>: ind_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(p11)</span>
<span id="cb66-53">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p11) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>: ind_h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n10 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.log(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p11)</span>
<span id="cb66-54">            ind_stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ind_h0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ind_h1)</span>
<span id="cb66-55">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ind_critical_val = chi2.ppf(self.alpha_test, 1)</span></span>
<span id="cb66-56">            ind_pval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> chi2.cdf(ind_stat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb66-57">            </span>
<span id="cb66-58">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conditional Coverage Test/ Christoffersen &amp; Kupiec mixture</span></span>
<span id="cb66-59">            cc_stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uc_stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ind_stat</span>
<span id="cb66-60">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cc_critical_val = chi2.ppf(self.alpha_test, 2)</span></span>
<span id="cb66-61">            cc_pval <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> chi2.cdf(cc_stat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb66-62">            </span>
<span id="cb66-63">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store results in a DataFrame</span></span>
<span id="cb66-64">            df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb66-65">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Statistic"</span>: [uc_stat, cc_stat, ind_stat],</span>
<span id="cb66-66">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Critical Value": [uc_critical_val, ind_critical_val, cc_critical_val],</span></span>
<span id="cb66-67">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>: [uc_pval, cc_pval, ind_pval]</span>
<span id="cb66-68">            }, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unconditional"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conditional"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Independence"</span>])</span>
<span id="cb66-69"></span>
<span id="cb66-70">            binomial_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.binomtest(n1, total_n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha, alternative<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'two-sided'</span>)</span>
<span id="cb66-71">            df.loc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Binomial"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [binomial_test.statistic, binomial_test.pvalue]</span>
<span id="cb66-72">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb66-73">            df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(np.nan, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unconditional"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conditional"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Independence"</span>], columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Statistic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>])</span>
<span id="cb66-74">            binomial_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stats.binomtest(n1, total_n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.alpha, alternative<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'two-sided'</span>)</span>
<span id="cb66-75">            df.loc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Binomial"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [binomial_test.statistic, binomial_test.pvalue]</span>
<span id="cb66-76">        </span>
<span id="cb66-77">        </span>
<span id="cb66-78">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df</span></code></pre></div>
</div>
<div id="00b66711" class="cell" data-execution_count="44">
<div class="sourceCode cell-code" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb67-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> daily_backtest(actual, forecast, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, alpha_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>,min_days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, max_days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>):</span>
<span id="cb67-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">""" Runs a growing backtest daily, starting at min_days up to max_days """</span></span>
<span id="cb67-3">    recalibration_needed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb67-4">    end_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(max_days,<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(actual))</span>
<span id="cb67-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> end <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(min_days, end_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb67-6">        data_to_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> actual[:end]</span>
<span id="cb67-7">        var_to_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> forecast</span>
<span id="cb67-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_to_test) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>:</span>
<span id="cb67-9">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">continue</span></span>
<span id="cb67-10">        </span>
<span id="cb67-11">        bt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Backtest(actual<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_to_test, forecast<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>var_to_test, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha, alpha_test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha_test)</span>
<span id="cb67-12">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bt.lr_bt()</span>
<span id="cb67-13">        </span>
<span id="cb67-14">        nb_exceptions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bt.hit_series().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb67-15">        </span>
<span id="cb67-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># res=bootstrap((data_to_test,), statistic=lambda x: exceptions(x, var_to_test)/len(x), method='percentile', n_resamples=1000)</span></span>
<span id="cb67-17">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ci_lower, ci_upper = res.confidence_interval</span></span>
<span id="cb67-18"></span>
<span id="cb67-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if ci_lower &lt;= 1-alpha &lt;= ci_upper:</span></span>
<span id="cb67-20">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     print(f"Recalibration needed at {end} days: The conditional test failed.")</span></span>
<span id="cb67-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     recalibration_needed = True</span></span>
<span id="cb67-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     return recalibration_needed, end</span></span>
<span id="cb67-23"></span>
<span id="cb67-24">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if "conditional" in result.index and result.loc["Independence", "p-value"] &lt; 0.05:</span></span>
<span id="cb67-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     print(f"Recalibration needed at {end} days: The Independence test failed.")</span></span>
<span id="cb67-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     recalibration_needed = True</span></span>
<span id="cb67-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     return recalibration_needed, end</span></span>
<span id="cb67-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># failed_tests = result.iloc[2:][result.iloc[2:]["p-value"] &lt; 0.05].index.tolist()</span></span>
<span id="cb67-29">        failed_tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result[result[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>].index.tolist()</span>
<span id="cb67-30"></span>
<span id="cb67-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> failed_tests:</span>
<span id="cb67-32">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Recalibration needed at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>end<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> days: The following tests failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">', '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>join(failed_tests)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb67-33">            recalibration_needed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-34">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> recalibration_needed, end</span>
<span id="cb67-35">            </span>
<span id="cb67-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> max_days <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> nb_exceptions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb67-37">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Recalibration needed at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>end<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> days: No exceptions observed in the test window."</span>)</span>
<span id="cb67-38">            recalibration_needed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb67-39">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> recalibration_needed, end</span>
<span id="cb67-40">    </span>
<span id="cb67-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> recalibration_needed:</span>
<span id="cb67-42">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No recalibration needed in the tested period."</span>)</span></code></pre></div>
</div>
<div id="66855bba" class="cell" data-execution_count="45">
<div class="sourceCode cell-code" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb68-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> iterative_backtest(data_train, data_test, var_model, alpha):</span>
<span id="cb68-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">""" Runs an iterative backtest with recalibration """</span></span>
<span id="cb68-3">    data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data_train.copy()</span>
<span id="cb68-4">    data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data_test.copy()</span>
<span id="cb68-5"></span>
<span id="cb68-6">    var_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> var_model(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb68-7">    var_to_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> var_result[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(var_result, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> var_result </span>
<span id="cb68-8">    </span>
<span id="cb68-9">    recalibration_points <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb68-10">    vars_stored <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [var_to_test]</span>
<span id="cb68-11">    </span>
<span id="cb68-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>:</span>
<span id="cb68-13">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb68-14">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Old VaR to test: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_to_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb68-15"></span>
<span id="cb68-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run daily backtest correctly</span></span>
<span id="cb68-17">        res_backtest, index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> daily_backtest(data_test, var_to_test, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha, alpha_test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, min_days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, max_days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>)</span>
<span id="cb68-18"></span>
<span id="cb68-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the index of recalibration</span></span>
<span id="cb68-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> res_backtest:</span>
<span id="cb68-21">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test))</span>
<span id="cb68-22">            recalibration_points.append(index)</span>
<span id="cb68-23"></span>
<span id="cb68-24">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expand training data and shrink test data</span></span>
<span id="cb68-25">            data_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([data_train, data_test.iloc[:index,]])</span>
<span id="cb68-26">            data_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data_test.iloc[index:,]</span>
<span id="cb68-27">            var_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> var_model(data_train, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span>
<span id="cb68-28">            var_to_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> var_result[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(var_result, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> var_result </span>
<span id="cb68-29">            vars_stored.append(var_to_test)</span>
<span id="cb68-30"></span>
<span id="cb68-31">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"New VaR to test: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>var_to_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb68-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb68-33">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No recalibration needed."</span>)</span>
<span id="cb68-34">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb68-35"></span>
<span id="cb68-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> recalibration_points, vars_stored</span>
<span id="cb68-37"></span>
<span id="cb68-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Execute iterative backtest</span></span>
<span id="cb68-39">recalibration_points, vars_stored <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> iterative_backtest(data_train, data_test, gaussian_var_ewma, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Old VaR to test: 2.7763%
Recalibration needed at 103 days: The following tests failed: Unconditional, Conditional
586
New VaR to test: 2.5925%
================================================================================
Old VaR to test: 2.5925%
Recalibration needed at 61 days: The following tests failed: Unconditional, Conditional
483
New VaR to test: 2.6675%
================================================================================
Old VaR to test: 2.6675%
Recalibration needed at 30 days: The following tests failed: Unconditional, Conditional
422
New VaR to test: 1.8885%
================================================================================
Old VaR to test: 1.8885%
Recalibration needed at 50 days: The following tests failed: Unconditional, Conditional
392
New VaR to test: 2.5369%
================================================================================
Old VaR to test: 2.5369%
Recalibration needed at 240 days: The following tests failed: Unconditional, Conditional
342
New VaR to test: 2.7075%</code></pre>
</div>
</div>
<div id="fc763664" class="cell" data-execution_count="46">
<div class="sourceCode cell-code" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb70-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb70-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb70-3"></span>
<span id="cb70-4">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb70-5">data_test.reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, inplace<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb70-6">plt.plot(data_test, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Returns"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb70-7">recab <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.cumsum(np.array(recalibration_points))</span>
<span id="cb70-8">vars_stored_neg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.array(vars_stored)</span>
<span id="cb70-9"></span>
<span id="cb70-10">plt.scatter(recab, vars_stored_neg, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Recalibration points"</span>, zorder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb70-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(recab) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb70-12">    plt.step(recab, vars_stored_neg, where<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'post'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'solid'</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VaR Adjusted"</span>)</span>
<span id="cb70-13">    plt.hlines(vars_stored_neg[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], xmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>recab[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], xmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_test), color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'solid'</span>)</span>
<span id="cb70-14"></span>
<span id="cb70-15">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>)</span>
<span id="cb70-16">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VaR / Returns"</span>)</span>
<span id="cb70-17">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Backtesting VaR with Recalibration Points"</span>)</span>
<span id="cb70-18">plt.legend()</span>
<span id="cb70-19">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb70-20"></span>
<span id="cb70-21">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques_files/figure-html/cell-47-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>paper</category>
  <category>risques-fin</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/risques_financiers/value-at-risk/var_classiques.html</guid>
  <pubDate>Thu, 27 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>APF filter</title>
  <dc:creator>-Cheryl Kouadio -Safa Bouzayene -Mariyam Ouyassin</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter.html</link>
  <description><![CDATA[ 




<section id="méthodes-étude-de-simulation-sur-la-performance-du-filtre-apf" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Méthodes : étude de simulation sur la performance du filtre APF</h1>
<section id="modèle-utilisé" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="modèle-utilisé"><span class="header-section-number">1.1</span> Modèle utilisé</h2>
<p>Le filtre particulaire APF (Auxiliary Particle Filter) est un filtre particulaire qui utilise des particules auxiliaires pour estimer la densité de probabilité de l’état caché. Il est utilisé pour estimer l’état caché d’un système dynamique non linéaire. Dans cet article, nous allons étudier la performance du filtre APF en utilisant un exemple simple.</p>
<p>Comme tout filtre particulaire, il est necessaire de spécifier la distribution a priori de l’état, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?p(x_0)">, la distribution de transition, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?p(v_t%7Cv_%7Bt-1%7D)"> et la vraisemblance, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?p(y_t%7Cv_t)">.</p>
<p>Dans le modèle de Heston spécifié en (1), on considère que : <img src="https://latex.codecogs.com/png.latex?%0Ap(v_t%7Cv_%7Bt-1%7D)=2c%5Cchi%5E2(2cx_k;%202q%20+%202;%202ce%5E%7B-%5Ckappa%20%5CDelta%7D%20x_%7Bk%E2%88%921%7D),%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?p(v_1)%20=%20%5CGamma(v_1;%20a,b)"> où <img src="https://latex.codecogs.com/png.latex?a%20=%20%5Cfrac%7B2%20%5Ckappa%20%5Ctheta%7D%7B%5Csigma%5E2%7D"> et <img src="https://latex.codecogs.com/png.latex?b%20=%20%5Cfrac%7B2%20%5Ckappa%7D%7B%5Csigma%5E2%7D">,</p>
<p>et <img src="https://latex.codecogs.com/png.latex?p(y_t)%7Cv_t)%20=%20N(0,h)">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AdS_t%20=%20S_t%20%5Cleft(%20rds%20+%20%5Csqrt%7Bv_t%7D%20dW_t%5E1%20%5Cright)%20%5C%5C%0Adv_t%20=%20%5Ckappa%20(%5Ctheta%20-%20v_t)%20ds%20+%20%5Csigma%20%5Csqrt%7Bv_t%7D%20dW_t%5E2%20%5C%5C%0AdW_t%5E1%20dW_t%5E2%20=%20%5Crho%20ds%0A%5Cend%7Bcases%7D%0A%5Cquad%20(1)%0A"></p>
<p>Pour tester la pertinence de l’APF, nous allons utiliser les paramètres suivants <img src="https://latex.codecogs.com/png.latex?%5CPhi%20=%20(%5Ctheta%20=%200.03,%20%5Ckappa%20=%204,%20%5Csigma%20=%200.4,%20%5Ckappa%20=%20-0.87,%20%5Crho%20=%200.5)">. Pour passer en temps discret et assurer la positivité de la volatilité, nous utilisosn le schema d’euler (à <img src="https://latex.codecogs.com/png.latex?%7Cv_t%7C">) suivant :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Ay_t%20=%20C(t,%20%5Ctheta,%20v_t,%20S_t,%20K,%20%5Ctau)%20+%20%5Cvarepsilon_t,%20%5C%5C%5B10pt%5D%0Av_t%20=%20%5Cleft%7C%20v_%7Bt-1%7D%20+%20%5Ckappa%20%5CDelta%20(%5Ctheta%20-%20v_%7Bt-1%7D)%20+%20%5Csigma%20%5Csqrt%7Bv_%7Bt-1%7D%5CDelta%7D%20(%5Crho%20w_t%5E1%20+%20%5Csqrt%7B1-%5Crho%5E2%7Dw_t%5E2)%20%5Cright%7C,%20%5C%5C%5B10pt%5D%0AS_t%20=%20S_%7Bt-1%7D%20%5Cleft(1%20+%20%5Cmu%20%5CDelta%20+%20%5Csqrt%7B%5CDelta%20v_t%7D%20w_t%5E1%20%5Cright),%20%5C%5C%5B10pt%5D%0A%5Cend%7Bcases%7D%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_t%20%5Csim%20N(0,h=0.01)">, et <img src="https://latex.codecogs.com/png.latex?w_t%5E1,%20w_t%5E2"> sont des variables aléatoires gaussiennes et indépendantes.</p>
<div class="{callout-tip}">
<p>Nous avons utilisé un schéma d’euler modifié pour garantir la positivité de la volatilité. En effet, la volatilité doit être positive dans le modèle de Heston, et le schéma d’euler standard peut produire des valeurs négatives. En prenant la valeur absolue de la volatilité à chaque étape, nous nous assurons que les valeurs restent positives. Il aurait été également possible d’utiliser le schéma d’euler à <img src="https://latex.codecogs.com/png.latex?ln(v_t)"> (via le lemme de ito) pour garantir la positivité de la volatilité.</p>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################ Simulation de la trajectoire de St et vt ################ </span></span>
<span id="cb1-4">Heston_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(N, kappa, theta, sigma, rho, v0, mu, tau, S0){</span>
<span id="cb1-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># N: Number of time steps</span></span>
<span id="cb1-6">  dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tau <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time step</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store stock prices and volatilities</span></span>
<span id="cb1-9">  S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-10">  v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-11">  </span>
<span id="cb1-12">  S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S0</span>
<span id="cb1-13">  v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v0</span>
<span id="cb1-14">  </span>
<span id="cb1-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N){</span>
<span id="cb1-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate correlated Brownian motions</span></span>
<span id="cb1-17">    W1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-18">    W2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-19">    </span>
<span id="cb1-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Euler discretization of variance process (ensure non-negativity)</span></span>
<span id="cb1-21">    v[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(v[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v[t]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W2)</span>
<span id="cb1-22">    </span>
<span id="cb1-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Euler discretization of the stock price process </span></span>
<span id="cb1-24">    S[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>dt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1)</span>
<span id="cb1-25">  }</span>
<span id="cb1-26">  </span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v_t =</span> v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S_t=</span>S))</span>
<span id="cb1-28">}</span></code></pre></div>
</div>
<p>Avec les étapes 1 et 2, nous obtenons les trajectoires de volatilité instantanée et de prix d’action suivantes :</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb2-2">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span></span>
<span id="cb2-3">theta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb2-4">kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb2-5">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb2-6">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb2-7">v0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb2-8">mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb2-9">tau<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-10">S0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb2-11"></span>
<span id="cb2-12">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Heston_sim</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa=</span>kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span>theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma=</span>sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho=</span>rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0=</span>v0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu=</span>mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau=</span>tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0=</span>S0)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb2-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>v_t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Processus de volatilité simulé"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time step"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>) </span>
<span id="cb2-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S_t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Processus de prix du sous-jacent simulé"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time step"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix"</span>) </span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/heston-sim-plots-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="1152"></p>
</figure>
</div>
</div>
</div>
<section id="procédure-de-simulation" class="level3" data-number="1.1.1">
<h3 data-number="1.1.1" class="anchored" data-anchor-id="procédure-de-simulation"><span class="header-section-number">1.1.1</span> Procédure de simulation</h3>
<p>La procédure de simulation est la suivante :</p>
<ol type="1">
<li><p>Tout d’abord, une trajectoire de 300 pas de temps de la variance instantanée sera simulée pour un pas de 1 jour, en commençant par <img src="https://latex.codecogs.com/png.latex?v_0"> = 0,03.</p></li>
<li><p>Conditionnellement à cette trajectoire, une trajectoire correspondante du prix de l’action sera alors générée.</p></li>
<li><p>Nous calculons, à l’aide du modèle de Heston, les prix d’options bruités pour trois types d’options différents : une option à la monnaie (ATM), une option dans la monnaie (ITM) et une option mixte (50 % ITM / 50 % OTM).</p></li>
</ol>
<p>Nous avons choisi trois types d’options afin d’observer comment le filtre APF se comporte dans différentes conditions de marché :</p>
<ul>
<li>Option à la monnaie (ATM) :
<ul>
<li>Prix d’exercice : <code>K = 1 × S_t</code></li>
<li>Temps avant l’échéance : <code>τ = 0.5</code> Les options ATM sont souvent utilisées pour l’estimation de la volatilité implicite, car elles sont les plus liquides et présentent un delta proche de 0.5, ce qui les rend sensibles aux variations du sous-jacent.</li>
</ul></li>
<li>Option dans la monnaie (ITM) :
<ul>
<li>Prix d’exercice : <code>K = 0.95 × S_t</code></li>
<li>Temps avant l’échéance : <code>τ = 0.5</code> Les options ITM ont une valeur intrinsèque élevée et une volatilité implicite plus stable. Elles sont moins sensibles aux fluctuations immédiates du marché mais permettent d’évaluer l’impact du filtre APF dans des conditions de faible variance du prix d’option.</li>
</ul></li>
<li>Option mixte (50 % ITM / 50 % OTM) :
<ul>
<li>Prix d’exercice : <code>K = 117</code></li>
<li>Temps avant l’échéance : <code>τ = 0.5</code> Cette approche permet de tester le filtre APF dans un scénario réaliste de portefeuille d’options où des positions ITM et OTM sont combinées. L’objectif est d’analyser si le filtre reste stable lorsque l’on mélange des options avec des sensibilités différentes aux mouvements du sous-jacent et aux variations de volatilité.</li>
</ul></li>
</ul>
<p><strong>Pourquoi tester différentes configurations d’options ?</strong></p>
<p>L’objectif de cette analyse est de vérifier comment le filtre APF se comporte en présence de conditions de marché variées :</p>
<ul>
<li>Options ATM : impact fort de la volatilité, mais moins sujettes au risque de gamma.</li>
<li>Options ITM : faible sensibilité à la volatilité implicite, mais risque de couverture plus limité.</li>
<li>Options mixtes : évaluation de la robustesse du filtre lorsque plusieurs types d’options coexistent dans un même portefeuille.</li>
</ul>
<p>Ces tests permettent de comparer la précision du filtre en fonction de la position de l’option par rapport au prix du sous-jacent.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################ Simulation du prix des options ATM ################ </span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/Heston_Call_Function.R"</span>)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">v_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>v_t </span>
<span id="cb3-6">S_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S_t</span>
<span id="cb3-7">h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix d'option K=1*S, tau = 0.5 =&gt; A la monnaie</span></span>
<span id="cb3-10">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-11">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb3-12">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-13">ATM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)){</span>
<span id="cb3-16">ATM[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> S_t[i])</span>
<span id="cb3-17">}</span>
<span id="cb3-18"></span>
<span id="cb3-19">res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ATM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ATM <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(h))</span>
<span id="cb3-20"></span>
<span id="cb3-21"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################ Simulation du prix des options ITM ################ </span></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix d'option K=0.95*S, tau = 0.5 =&gt; Hors de la monnaie</span></span>
<span id="cb3-23">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-24">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb3-25">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span></span>
<span id="cb3-26"></span>
<span id="cb3-27">ITM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-28"></span>
<span id="cb3-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)){</span>
<span id="cb3-30">ITM[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> S_t[i])</span>
<span id="cb3-31">}</span>
<span id="cb3-32">res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ITM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ITM <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(h))</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################ Simulation du prix des options mixte ################ </span></span>
<span id="cb3-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(S_t)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  99.49  104.36  117.23  119.24  128.88  156.89 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix d'option K=117, tau = 0.5  =&gt; moitié ITM et moitié OTM</span></span>
<span id="cb5-2">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-3">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-4">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">117</span></span>
<span id="cb5-5"></span>
<span id="cb5-6">MIXTE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)){</span>
<span id="cb5-9">MIXTE[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S_t[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K)</span>
<span id="cb5-10">}</span>
<span id="cb5-11">res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>MIXTE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MIXTE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(h))</span></code></pre></div>
</div>
<section id="prix-doptions-simulés" class="level4" data-number="1.1.1.1">
<h4 data-number="1.1.1.1" class="anchored" data-anchor-id="prix-doptions-simulés"><span class="header-section-number">1.1.1.1</span> Prix d’options simulés</h4>
<p>En simulant le prix de ces options, nous constatons sans surprise que les options mixte ont un prix plus élevé plus on se rapproche de la maturité. Cependant, les options ITM et ATM ont des prix plus stables, avec une légère augmentation pour les options ITM.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ITM)  </span>
<span id="cb6-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tau <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb6-3">time_axis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> N)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Axe des temps, de 0 à tau</span></span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(time_axis,res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ITM, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution du prix des options "</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>)</span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(time_axis,res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ATM, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(time_axis,res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>MIXTE, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>)</span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># legend</span></span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixte"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'darkgreen'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Types d'option"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="filtres-utilisés" class="level4" data-number="1.1.1.2">
<h4 data-number="1.1.1.2" class="anchored" data-anchor-id="filtres-utilisés"><span class="header-section-number">1.1.1.2</span> Filtres utilisés</h4>
<p>L’objectif est de comparer les résultats de l’APF sur du filtre particulaire bootstrap en termes d’erreurs d’ajustement, mesurées par les erreurs quadratiques moyennes (RMSE) de l’ajustement de la variance et de l’ajustement des prix des options, dans différents cas de données.</p>
<p><strong>Filtre bootstrap :</strong></p>
<p>Le filtre boostrap fonctionne de la manière suivante :</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/data/bootstrap_filter.png" class="img-fluid figure-img"></p>
<figcaption>Bootstrap filter</figcaption>
</figure>
</div>
<p>Dans le cadre du modèle de heston, nous avons implémenté le filtre bootstrap de la manière suivante :</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">BootstrapParticleFilter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(y, S, v, K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>) {</span>
<span id="cb7-2">  </span>
<span id="cb7-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres supplémentaires</span></span>
<span id="cb7-4">  sigma_epsilon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(h)</span>
<span id="cb7-5"></span>
<span id="cb7-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de la loi stationnaire de v</span></span>
<span id="cb7-7">  alpha1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-8">  alpha2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa)</span>
<span id="cb7-9"></span>
<span id="cb7-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation</span></span>
<span id="cb7-11">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)</span>
<span id="cb7-12">  v_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(n)</span>
<span id="cb7-13">  v_particle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb7-14">  w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb7-15">  w_normalized <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb7-16"></span>
<span id="cb7-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtre particulaire bootstrap</span></span>
<span id="cb7-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb7-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb7-20">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des particules à t = 0</span></span>
<span id="cb7-21">      v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgamma</span>(M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha2)</span>
<span id="cb7-22">      </span>
<span id="cb7-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids initiaux basés sur la densité de la loi stationnaire</span></span>
<span id="cb7-24">      w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dgamma</span>(v_particle[t, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha2)</span>
<span id="cb7-25">      </span>
<span id="cb7-26">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalisation des poids</span></span>
<span id="cb7-27">      w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb7-28">      </span>
<span id="cb7-29">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation initiale</span></span>
<span id="cb7-30">      v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb7-31">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-32">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Étape de prédiction (propagation des particules)</span></span>
<span id="cb7-33">      v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(M, </span>
<span id="cb7-34">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt, </span>
<span id="cb7-35">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt)))</span>
<span id="cb7-36">      </span>
<span id="cb7-37">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Calcul du prix du Call pour chaque particule</span></span>
<span id="cb7-38">      C <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(M)</span>
<span id="cb7-39">      </span>
<span id="cb7-40">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Si K est un unique scalaire (strike constant), on le conserve</span></span>
<span id="cb7-41">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Si K est un vecteur, on prend K[t] comme strike au temps t</span></span>
<span id="cb7-42">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(K) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb7-43">          Kt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike constant</span></span>
<span id="cb7-44">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-45">          Kt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K[t]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike spécifique au temps t</span></span>
<span id="cb7-46">      }</span>
<span id="cb7-47">      </span>
<span id="cb7-48">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul pour chaque particule avec le bon strike</span></span>
<span id="cb7-49">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M) {</span>
<span id="cb7-50">          C[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, </span>
<span id="cb7-51">                                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_particle[t, i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> Kt)</span>
<span id="cb7-52">      }</span>
<span id="cb7-53"></span>
<span id="cb7-54">      </span>
<span id="cb7-55">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mise à jour des poids (vraisemblance observation-conditionnelle)</span></span>
<span id="cb7-56">      w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(y[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> C, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(sigma_epsilon))</span>
<span id="cb7-57">      </span>
<span id="cb7-58">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalisation des poids</span></span>
<span id="cb7-59">      w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb7-60">      </span>
<span id="cb7-61">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rééchantillonnage</span></span>
<span id="cb7-62">      index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> w_normalized[t, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-63">      v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v_particle[t, index]</span>
<span id="cb7-64">      </span>
<span id="cb7-65">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids uniformes après resampling</span></span>
<span id="cb7-66">      w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> M, M)</span>
<span id="cb7-67"></span>
<span id="cb7-68">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation de la volatilité instantanée</span></span>
<span id="cb7-69">      v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb7-70">    }</span>
<span id="cb7-71">  }</span>
<span id="cb7-72"></span>
<span id="cb7-73">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(v_hat)</span>
<span id="cb7-74">}</span></code></pre></div>
</div>
<p><strong>Filtre APF :</strong></p>
<p>Le filtre particulaire auxilaire fonctionne de la manière suivante :</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/data/APF.png" class="img-fluid figure-img"></p>
<figcaption>APF</figcaption>
</figure>
</div>
<p>Dans le cadre du modèle de heston, nous avons implémenté le filtre APF de la manière suivante :</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">APF_Heston <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(y, S, v,K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, </span>
<span id="cb8-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.87</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>) {</span>
<span id="cb8-3">  </span>
<span id="cb8-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de la loi stationnaire de v</span></span>
<span id="cb8-5">  sigma_epsilon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(h)</span>
<span id="cb8-6">  alpha1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-7">  alpha2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> kappa)</span>
<span id="cb8-8"></span>
<span id="cb8-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des paramètres</span></span>
<span id="cb8-10">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)</span>
<span id="cb8-11">  </span>
<span id="cb8-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des vecteurs/matrices</span></span>
<span id="cb8-13">  v_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(n)                   </span>
<span id="cb8-14">  v_particle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb8-15">  mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)        </span>
<span id="cb8-16">  py_mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M) </span>
<span id="cb8-17">  w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb8-18">  w_normalized <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)</span>
<span id="cb8-19"></span>
<span id="cb8-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtre particulaire APF</span></span>
<span id="cb8-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb8-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb8-23">      </span>
<span id="cb8-24">      v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgamma</span>(M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha2)</span>
<span id="cb8-25">      w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> M, M)</span>
<span id="cb8-26">      w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb8-27">      v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb8-28">      </span>
<span id="cb8-29">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-30">      </span>
<span id="cb8-31">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Si K est un unique scalaire (strike constant), on le conserve</span></span>
<span id="cb8-32">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Si K est un vecteur, on prend K[t] comme strike au temps t</span></span>
<span id="cb8-33">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(K) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb8-34">          Kt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike constant</span></span>
<span id="cb8-35">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-36">          Kt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K[t]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Strike spécifique au temps t</span></span>
<span id="cb8-37">      }</span>
<span id="cb8-38">      </span>
<span id="cb8-39">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 - Pré-sélection</span></span>
<span id="cb8-40">      mu[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt)</span>
<span id="cb8-41">      </span>
<span id="cb8-42">      mean_pymu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(M)</span>
<span id="cb8-43">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M) {</span>
<span id="cb8-44">        mean_pymu[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, </span>
<span id="cb8-45">                                             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> mu[t, i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> Kt)</span>
<span id="cb8-46">      }</span>
<span id="cb8-47">      py_mu[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(y[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> mean_pymu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sigma_epsilon)</span>
<span id="cb8-48"></span>
<span id="cb8-49">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids pour la pré-sélection</span></span>
<span id="cb8-50">      w[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> py_mu[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> w_normalized[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ]</span>
<span id="cb8-51">      </span>
<span id="cb8-52">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 - Resampling (échantillonnage)</span></span>
<span id="cb8-53">      index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> w[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ])</span>
<span id="cb8-54"></span>
<span id="cb8-55">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mise à jour des particules</span></span>
<span id="cb8-56">      v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, index]</span>
<span id="cb8-57">      </span>
<span id="cb8-58">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 - Propagation (évolution des particules)</span></span>
<span id="cb8-59">      v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(M, </span>
<span id="cb8-60">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt, </span>
<span id="cb8-61">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt)))</span>
<span id="cb8-62"></span>
<span id="cb8-63">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mise à jour des poids</span></span>
<span id="cb8-64">      C <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(M)</span>
<span id="cb8-65">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M) {</span>
<span id="cb8-66">        C[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, </span>
<span id="cb8-67">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_particle[t, i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> Kt)</span>
<span id="cb8-68">      }</span>
<span id="cb8-69">      likelihood <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(y[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> C, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sigma_epsilon)</span>
<span id="cb8-70">      w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> likelihood <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (py_mu[t, index] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-12</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Protection pour éviter la division par zéro</span></span>
<span id="cb8-71">      </span>
<span id="cb8-72">      w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb8-73">      v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb8-74">    }</span>
<span id="cb8-75">  }</span>
<span id="cb8-76"></span>
<span id="cb8-77">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retourner les résultats</span></span>
<span id="cb8-78">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(v_hat)</span>
<span id="cb8-79">}</span></code></pre></div>
</div>
<p><strong>Différence entre le filtre bootstrap et le filtre APF :</strong></p>
<p>Le filtre bootstrap et le filtre APF sont deux méthodes de filtrage particulaire qui permettent d’estimer l’état caché d’un système dynamique non linéaire. La principale différence entre ces deux méthodes réside dans la manière dont elles mettent à jour les poids des particules, et ainsi comme la distribution d’importance q est construite. Par définition, les poids sont définis par : <img src="https://latex.codecogs.com/png.latex?%0Aw(x_t)%20=%20w(x_%7Bt-1%7D)%20%5Cfrac%7Bp(y_t%7Cx_t)p(x_t%7Cx_%7Bt-1%7D)%7D%7Bq(x_t%7Cx_%7Bt-1%7D,y_t)%7D,%0A"> où <img src="https://latex.codecogs.com/png.latex?p(y_t%7Cx_t)"> est la vraisemblance, <img src="https://latex.codecogs.com/png.latex?p(x_t%7Cx_%7Bt-1%7D)"> est la distribution de transition, et <img src="https://latex.codecogs.com/png.latex?q(x_t%7Cx_%7Bt-1%7D,y_t)"> est la distribution d’importance.</p>
<p>Dans le cadre du filtre bootstrap, la distribution d’importance est définie comme suit : <img src="https://latex.codecogs.com/png.latex?%0Aq(x_t%7Cx_%7Bt-1%7D,y_t)%20=%20p(x_t%7Cx_%7Bt-1%7D).%0A"> De ce fait, <img src="https://latex.codecogs.com/png.latex?w(x_t)%20=%20w(x_%7Bt-1%7D)%20p(y_t%7Cx_t)">.Cela signifie que les poids sont mis à jour en fonction de la distribution de transition et de la vraisemblance.</p>
<p>Dans le cadre du filtre APF, la distribution d’importance se rapproche de la distribution optimale, vu dans la littérature comme étant <img src="https://latex.codecogs.com/png.latex?q(x_t%7Cx_%7Bt-1%7D,y_t)%20=%20p(x_t%7Cx_%7Bt-1%7D,y_t)">. De ce fait, les poids sont mis à jour en fonction de la vraisemblance conditionnelle, ce qui permet d’améliorer la précision de l’estimation de l’état caché, et donc de l’estimation de la volatilité dans notre cas.</p>
</section>
</section>
</section>
</section>
<section id="résultats" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Résultats</h1>
<section id="option-à-la-monnaie-atm" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="option-à-la-monnaie-atm"><span class="header-section-number">2.1</span> Option à la monnaie (ATM)</h2>
<section id="filtre-bootstrap" class="level3" data-number="2.1.1">
<h3 data-number="2.1.1" class="anchored" data-anchor-id="filtre-bootstrap"><span class="header-section-number">2.1.1</span> Filtre bootstrap</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##################### Définition des paramètres #####################</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des paramètres phi</span></span>
<span id="cb9-3">theta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb9-4">kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb9-5">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb9-6">lambda <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.87</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prime de risque</span></span>
<span id="cb9-7">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de Heston </span></span>
<span id="cb9-10">h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb9-11">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb9-12"></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prix de call (y), du sous-jacent (S), et volatilité instantanée (v)</span></span>
<span id="cb9-14">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ATM</span>
<span id="cb9-15">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S_t</span>
<span id="cb9-16">v<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>v_t</span>
<span id="cb9-17"></span>
<span id="cb9-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des paramètres</span></span>
<span id="cb9-19">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nombre d'observations</span></span>
<span id="cb9-20">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nombre de particules</span></span>
<span id="cb9-21">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres de l'option </span></span>
<span id="cb10-4">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> S</span>
<span id="cb10-5">tau<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exemple d'appel avec tes données "res"</span></span>
<span id="cb10-8">et_boot_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb10-9">boot_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BootstrapParticleFilter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb10-10">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre bootstrap"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(boot_atm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_boot_atm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     12.028       0.739      12.971 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">rmse_boot_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((boot_atm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_boot_atm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.001402773</code></pre>
</div>
</div>
</section>
<section id="filtre-apf" class="level3" data-number="2.1.2">
<h3 data-number="2.1.2" class="anchored" data-anchor-id="filtre-apf"><span class="header-section-number">2.1.2</span> Filtre APF</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb16-2">et_apf_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb16-3">apf_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">APF_Heston</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb16-4">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre APF"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(apf_atm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb17-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_apf_atm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     23.965       1.410      25.782 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">rmse_apf_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((apf_atm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_apf_atm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.001889304</code></pre>
</div>
</div>
</section>
</section>
<section id="option-à-la-monnaie-itm" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="option-à-la-monnaie-itm"><span class="header-section-number">2.2</span> Option à la monnaie (ITM)</h2>
<section id="filtre-bootstrap-1" class="level3" data-number="2.2.1">
<h3 data-number="2.2.1" class="anchored" data-anchor-id="filtre-bootstrap-1"><span class="header-section-number">2.2.1</span> Filtre bootstrap</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb22-2">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ITM</span>
<span id="cb22-3">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> S</span>
<span id="cb22-4"></span>
<span id="cb22-5">et_boot_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb22-6">boot_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BootstrapParticleFilter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb22-7">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre bootstrap"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(boot_itm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb23-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_boot_itm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     11.420       0.646      12.123 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">rmse_boot_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((boot_itm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_boot_itm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.00152023</code></pre>
</div>
</div>
</section>
<section id="apf" class="level3" data-number="2.2.2">
<h3 data-number="2.2.2" class="anchored" data-anchor-id="apf"><span class="header-section-number">2.2.2</span> APF</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">et_apf_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb28-2">apf_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">APF_Heston</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb28-3">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre APF"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb29-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(apf_itm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb29-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_apf_itm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     22.690       1.271      24.052 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">rmse_apf_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((apf_itm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb32-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_apf_itm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.001850903</code></pre>
</div>
</div>
</section>
</section>
<section id="options-mixtes" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="options-mixtes"><span class="header-section-number">2.3</span> Options mixtes</h2>
<section id="filtre-bootstrap-2" class="level3" data-number="2.3.1">
<h3 data-number="2.3.1" class="anchored" data-anchor-id="filtre-bootstrap-2"><span class="header-section-number">2.3.1</span> Filtre bootstrap</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb34-2">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>MIXTE</span>
<span id="cb34-3">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">117</span></span>
<span id="cb34-4"></span>
<span id="cb34-5">et_boot_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb34-6">boot_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BootstrapParticleFilter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb34-7">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre bootstrap"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb35-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(boot_mixte, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb35-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_boot_mixte)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     12.057       0.692      12.879 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">rmse_boot_mixte<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((boot_mixte <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb38-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_boot_mixte)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.01595637</code></pre>
</div>
</div>
</section>
<section id="apf-1" class="level3" data-number="2.3.2">
<h3 data-number="2.3.2" class="anchored" data-anchor-id="apf-1"><span class="header-section-number">2.3.2</span> APF</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">et_apf_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb40-2">apf_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">APF_Heston</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S=</span>S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v=</span>v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span>K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kappa =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> sigma,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> h)</span>
<span id="cb40-3">})</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre APF"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb41-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(apf_mixte, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb41-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(et_apf_mixte)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utilisateur     système      écoulé 
     23.758       1.300      25.226 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">rmse_apf_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((apf_mixte <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb44-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE :"</span>, rmse_apf_mixte)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>RMSE : 0.01475175</code></pre>
</div>
</div>
</section>
</section>
<section id="comparaison-des-performances" class="level2" data-number="2.4">
<h2 data-number="2.4" class="anchored" data-anchor-id="comparaison-des-performances"><span class="header-section-number">2.4</span> Comparaison des performances</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création du data.frame avec les colonnes demandées</span></span>
<span id="cb48-2">comp_perf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb48-3">    Méthode <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>),</span>
<span id="cb48-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type_Option =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>),</span>
<span id="cb48-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">RMSE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_apf_atm, rmse_apf_itm, rmse_apf_mixte, rmse_boot_atm, rmse_boot_itm, rmse_boot_mixte)</span>
<span id="cb48-6">)</span>
<span id="cb48-7"></span>
<span id="cb48-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conversion : Méthode comme colonne</span></span>
<span id="cb48-9">comp_perf_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(</span>
<span id="cb48-10">    comp_perf,</span>
<span id="cb48-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> Méthode,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ce qui devient les colonnes</span></span>
<span id="cb48-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> RMSE      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ce qui remplit les cases</span></span>
<span id="cb48-13">)</span>
<span id="cb48-14"></span>
<span id="cb48-15">comp_perf_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(</span>
<span id="cb48-16">    comp_perf_wide,</span>
<span id="cb48-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb48-18">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type_Option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Moyenne"</span>,</span>
<span id="cb48-19">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">APF =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_apf_atm, rmse_apf_itm, rmse_apf_mixte), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb48-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BOOT =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_boot_atm, rmse_boot_itm, rmse_boot_mixte), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb48-21">    )</span>
<span id="cb48-22">)</span>
<span id="cb48-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage du résultat</span></span>
<span id="cb48-24">comp_perf_wide</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 3
  Type_Option     APF    BOOT
  &lt;chr&gt;         &lt;dbl&gt;   &lt;dbl&gt;
1 ATM         0.00189 0.00140
2 ITM         0.00185 0.00152
3 MIXTE       0.0148  0.0160 
4 Moyenne     0.00616 0.00629</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Comparaison des temps d'exécution</span></span>
<span id="cb50-2">comp_temps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb50-3">    Méthode <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOOT"</span>),</span>
<span id="cb50-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type_Option =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>),</span>
<span id="cb50-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Temps =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(et_apf_atm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_apf_itm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_apf_mixte[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_boot_atm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_boot_itm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_boot_mixte[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb50-6">)</span>
<span id="cb50-7"></span>
<span id="cb50-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conversion : Méthode comme colonne</span></span>
<span id="cb50-9">comp_temps_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(</span>
<span id="cb50-10">    comp_temps,</span>
<span id="cb50-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> Méthode,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ce qui devient les colonnes</span></span>
<span id="cb50-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> Temps      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ce qui remplit les cases</span></span>
<span id="cb50-13">)</span>
<span id="cb50-14"></span>
<span id="cb50-15">comp_temps_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(</span>
<span id="cb50-16">    comp_temps_wide,</span>
<span id="cb50-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb50-18">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type_Option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Moyenne"</span>,</span>
<span id="cb50-19">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">APF =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(et_apf_atm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_apf_itm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_apf_mixte[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb50-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BOOT =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(et_boot_atm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_boot_itm[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], et_boot_mixte[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb50-21">    )</span>
<span id="cb50-22">)</span>
<span id="cb50-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Affichage du résultat</span></span>
<span id="cb50-24">comp_temps_wide</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 3
  Type_Option   APF  BOOT
  &lt;chr&gt;       &lt;dbl&gt; &lt;dbl&gt;
1 ATM          25.8  13.0
2 ITM          24.1  12.1
3 MIXTE        25.2  12.9
4 Moyenne      25.0  12.7</code></pre>
</div>
</div>
<p>En comparant les performances de l’APF à celles du filtre bootstrap, nous constatons que les deux filtres ont des performances assez similaires en termes d’erreurs d’ajustement (RMSE) pour les options ATM et ITM. Cependant, l’APF semble être plus efficace pour les options mixtes, avec des erreurs d’ajustement plus faibles que le filtre bootstrap. Cependant, en termes de temps d’exécution, le filtre bootstrap est plus rapide que l’APF pour les trois types d’options. En moyenne, l’APF est plus lent que le filtre bootstrap, mais offre une meilleure précision tout option confondue.</p>
<p>Pou connaitre la robustesse de ces estimations, nous avons fait du bootstrap pour estimer la distribution des RMSE, et ainsi obtenir des intervalles de confiance pour ces estimations, de même que des p-valeurs. Le code ayant servi à la simulation est disponible ici.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb52-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb52-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb52-4"></span>
<span id="cb52-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Charger les résultats du bootstrap</span></span>
<span id="cb52-6">res_ATM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"results_sim/res_ATM_min.rds"</span>)</span>
<span id="cb52-7">res_ITM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"results_sim/res_ITM_min.rds"</span>)</span>
<span id="cb52-8">res_MIXTE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"results_sim/res_MIXTE_min.rds"</span>)</span>
<span id="cb52-9"></span>
<span id="cb52-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraire les RMSE</span></span>
<span id="cb52-11">rmse_bootstrap_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_ATM<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb52-12">rmse_apf_atm_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_ATM<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb52-13"></span>
<span id="cb52-14">rmse_bootstrap_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_ITM<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb52-15">rmse_apf_itm_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_ITM<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb52-16"></span>
<span id="cb52-17">rmse_bootstrap_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_MIXTE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb52-18">rmse_apf_mixte_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res_MIXTE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb52-19"></span>
<span id="cb52-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Créer un dataframe avec les valeurs RMSE pour les trois scénarios</span></span>
<span id="cb52-21">df_rmse_boxplot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb52-22">  Méthode <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bootstrap Filter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"APF-Heston"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_atm), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_atm_b), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_itm), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_itm_b),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_mixte), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_mixte_b))),</span>
<span id="cb52-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">RMSE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_bootstrap_atm, rmse_apf_atm_b,</span>
<span id="cb52-24">           rmse_bootstrap_itm, rmse_apf_itm_b,</span>
<span id="cb52-25">           rmse_bootstrap_mixte, rmse_apf_mixte_b),</span>
<span id="cb52-26">  Scénario <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ATM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ITM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIXTE"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_atm), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_atm_b),</span>
<span id="cb52-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_itm), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_itm_b),</span>
<span id="cb52-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_bootstrap_mixte), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(rmse_apf_mixte_b)))</span>
<span id="cb52-29">)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in rep(c("Bootstrap Filter", "APF-Heston"), each =
c(length(rmse_bootstrap_atm), : seul le premier élément de l'argument 'each'
est utilisé</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in rep(c("ATM", "ATM", "ITM", "ITM", "MIXTE", "MIXTE"), each =
c(length(rmse_bootstrap_atm), : seul le premier élément de l'argument 'each'
est utilisé</code></pre>
</div>
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des p-valeurs</span></span>
<span id="cb55-2"></span>
<span id="cb55-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># kruskall wallis</span></span>
<span id="cb55-4">test_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rmse_bootstrap_atm, rmse_apf_atm_b))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb55-5">test_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rmse_bootstrap_itm, rmse_apf_itm_b))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb55-6">test_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rmse_bootstrap_mixte, rmse_apf_mixte_b))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb55-7"></span>
<span id="cb55-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test_atm &lt;- t.test(rmse_bootstrap_atm, rmse_apf_atm_b)$p.value</span></span>
<span id="cb55-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test_itm &lt;- t.test(rmse_bootstrap_itm, rmse_apf_itm_b)$p.value</span></span>
<span id="cb55-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># test_mixte &lt;- t.test(rmse_bootstrap_mixte, rmse_apf_mixte_b)$p.value</span></span>
<span id="cb55-11"></span>
<span id="cb55-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création du boxplot avec facet_wrap et ajout des p-valeurs en caption</span></span>
<span id="cb55-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_rmse_boxplot, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Méthode, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> RMSE, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Méthode)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Scénario, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparaison des RMSE (Bootstrap vs APF) selon les scénarios"</span>,</span>
<span id="cb55-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode de Filtrage"</span>,</span>
<span id="cb55-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE"</span>,</span>
<span id="cb55-19">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-values t-test: ATM ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(test_atm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb55-20">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"| ITM ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(test_itm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb55-21">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"| MIXTE ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(test_mixte, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb55-24">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-27-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concaténer toutes les RMSE</span></span>
<span id="cb56-2">total_rmse_bootstrap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_bootstrap_atm, rmse_bootstrap_itm, rmse_bootstrap_mixte)</span>
<span id="cb56-3">total_rmse_apf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_apf_atm_b, rmse_apf_itm_b, rmse_apf_mixte_b)</span>
<span id="cb56-4"></span>
<span id="cb56-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kruskal wallis test</span></span>
<span id="cb56-6">global_pval <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kruskal.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(total_rmse_bootstrap, total_rmse_apf))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb56-7"></span>
<span id="cb56-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># global_pval &lt;- t.test(total_rmse_bootstrap, total_rmse_apf)$p.value</span></span>
<span id="cb56-9"></span>
<span id="cb56-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boxplot global</span></span>
<span id="cb56-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggboxplot</span>(df_rmse_boxplot, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode"</span>,</span>
<span id="cb56-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Méthode de Filtrage"</span>,</span>
<span id="cb56-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparaison des RMSE (Bootstrap vs APF) pour les trois scénarios"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-values: Global ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(global_pval, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Approche pvalue</strong> :</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1">rmse_diff_atm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_bootstrap_atm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_atm_b</span>
<span id="cb57-2">rmse_diff_atm_obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_boot_atm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_atm</span>
<span id="cb57-3"></span>
<span id="cb57-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(rmse_diff_atm_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> rmse_diff_atm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.84</code></pre>
</div>
<div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1">rmse_diff_itm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_bootstrap_itm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_itm_b</span>
<span id="cb59-2">rmse_diff_itm_obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_boot_itm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_itm</span>
<span id="cb59-3"></span>
<span id="cb59-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(rmse_diff_itm_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> rmse_diff_itm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.38</code></pre>
</div>
<div class="sourceCode cell-code" id="cb61" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1">rmse_diff_mixte <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_bootstrap_mixte <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_mixte_b</span>
<span id="cb61-2">rmse_diff_mixte_obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rmse_boot_mixte <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rmse_apf_mixte</span>
<span id="cb61-3"></span>
<span id="cb61-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(rmse_diff_mixte_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> rmse_diff_mixte)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div>
<div class="{callout-tip}">
<p><strong>Conclusion :</strong> L’APF est une méthode plus précise que le filtre bootstrap pour estimer la volatilité instantanée dans le cadre d’un modèle de Heston où le prix est bruité <img src="https://latex.codecogs.com/png.latex?h=0.01">, mais elle est également plus lente en termes de temps d’exécution. Le choix entre ces deux méthodes dépendra donc des besoins spécifiques de l’analyse, en fonction de la précision et de la vitesse d’exécution requises.</p>
<p>Pour tester la robustesse de ces estimations, il aurait fallu faire du bootstrap afin d’estimer la distribution des RMSE, et ainsi obtenir des intervalles de confiance pour ces estimations.</p>
<p>Il aurait été interessant de tester les filtres dans d’autres cas de figures.</p>
</div>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>proc-sto</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/APF_filter.html</guid>
  <pubDate>Tue, 25 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Corrélation de Spearman vs corrélation de Pearson</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson.html</link>
  <description><![CDATA[ 




<section id="corrélation-de-spearman-vs-corrélation-de-pearson" class="level1">
<h1>Corrélation de Spearman vs corrélation de Pearson</h1>
<p>La corrélation de Spearman est une mesure de corrélation non paramétrique qui permet de mesurer la relation monotone entre deux variables. Elle est souvent utilisée pour mesurer la dépendance entre les variables aléatoires. La corrélation de Spearman est basée sur les rangs des observations et est moins sensible aux valeurs extrêmes que la corrélation de Pearson. Elle est donc plus robuste et plus adaptée pour mesurer la dépendance entre les variables aléatoires.</p>
<p>La corrélation de Pearson, quant à elle, ne permet de comparer les dépendances <strong>linéaires</strong> des variables. De plus, elle ne permet de comparer les corrélation que lorsque les variables aléatoires sont normales. En effet, soit (<img src="https://latex.codecogs.com/png.latex?X_1,X_2,X_3">), si <img src="https://latex.codecogs.com/png.latex?%5Crho(X_1,X_2)%20%3E%20%5Crho(X_1,X_3)">, cela ne veut dire que la corrélation entre <img src="https://latex.codecogs.com/png.latex?X_1"> et <img src="https://latex.codecogs.com/png.latex?X_2"> est plus forte que celle entre <img src="https://latex.codecogs.com/png.latex?X_1"> et <img src="https://latex.codecogs.com/png.latex?X_3"> que si ces variables sont gaussiennes.</p>
<p>Nous verrons dans la suite de ce document que la corrélation de Spearman est plus adaptée pour mesurer la dépendance entre les variables aléatoires, car elle est une <strong>mesure de concordance</strong>. De ce fait, elle est définit par une copule. Il n’en est pas ainsi pour la corrélation de Pearson.</p>
<section id="quest-ce-quune-mesure-de-concordance" class="level3">
<h3 class="anchored" data-anchor-id="quest-ce-quune-mesure-de-concordance">Qu’est ce qu’une mesure de concordance ?</h3>
<p>Une mesure de concordance est une mesure qui permet de quantifier la relation entre deux variables aléatoires. Cinq propriétés sont généralement attribuées à une mesure de concordance :</p>
<ul>
<li><p><strong>Symétrie</strong> : la mesure de concordance entre X et Y est la même que celle entre Y et X.</p></li>
<li><p><strong>Normalisation</strong> : la mesure de concordance est comprise entre -1 et 1.</p></li>
<li><p><img src="https://latex.codecogs.com/png.latex?%5Cdelta(X_1,%20X_2)%20=%201%20%5CLeftrightarrow%20(X_1,X_2)%20%5Coverset%7B%5Cmathcal%7BL%7D%7D%7B=%7D%20%20%5Cleft(%20F_1%5E%7B-1%7D(U),F_2%5E%7B-1%7D(U)%20%5Cright)"></p></li>
<li><p><img src="https://latex.codecogs.com/png.latex?%5Cdelta(X_1,%20X_2)%20=%20-1%20%5CLeftrightarrow%20(X_1,X_2)%20%5Coverset%7B%5Cmathcal%7BL%7D%7D%7B=%7D%20%20%5Cleft(%20F_1%5E%7B-1%7D(U),F_2%5E%7B-1%7D(1-U)%20%5Cright)"></p></li>
<li><p><img src="https://latex.codecogs.com/png.latex?%5Cdelta(f(X_1),%20X_2)%20=%20%5Cdelta(X_1,%20X_2)"> si <img src="https://latex.codecogs.com/png.latex?f"> est croissante; <img src="https://latex.codecogs.com/png.latex?%5Cdelta(f(X_1),%20X_2)%20=%20-%5Cdelta(X_1,%20X_2)"> si <img src="https://latex.codecogs.com/png.latex?f"> est décroissante.</p></li>
</ul>
<p>En raison des propriétés 3 et 4, la corrélation de pearson n’est pas une mesure de concordance lorsque les variables aléatoires ne sont pas gaussiennes. En effet, la corrélation de pearson ne vérifie pas la propriété 3. C’est pourquoi, la corrélation de spearman est plus adaptée pour mesurer la dépendance entre les variables aléatoires. Une mesure encore plus appropriée est la copule. La corrélation de spearman, quant à elle, est une mesure de concordance.</p>
</section>
<section id="exemple" class="level3">
<h3 class="anchored" data-anchor-id="exemple">Exemple :</h3>
<p>Soit un vecteur gaussien X = (<img src="https://latex.codecogs.com/png.latex?X_1,%20X_2,%20X_3">) suivant une loi N(0,<img src="https://latex.codecogs.com/png.latex?%5CSigma">) avec :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CSigma%20=%20%5Cbegin%7Bpmatrix%7D%0A1%20&amp;%200.4%20&amp;%200.2%20%5C%5C%0A0.4%20&amp;%201%20&amp;%20-0.8%20%5C%5C%0A0.2%20&amp;%20-0.8%20&amp;%201%0A%5Cend%7Bpmatrix%7D%0A"></p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span></span>
<span id="cb1-3">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-4">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mvrnorm</span>(n,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,m),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sigma=</span>sigma)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(X,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spearman"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
</div>
<p>On crée ensuite un vecteur Z constitué des fonctions de répartition des éléments de X. On constate que la corrélation de Spearman entre les éléments de Z ne change pas. Celà s’explique par le fait que nous appliquons une fonction croissante à chaque élément de X. Cependant, la distribution de Z est différente de celle de X. En effet, les composantes de Z suivent une loi uniforme <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BU%7D(0,1)">.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">Z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(X, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(Z,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spearman"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(Z,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-2-2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
</div>
<p>A partir de Z, nous construisons un vecteur W tel que les composantes de W suivent des lois marginales différentes, resp <img src="https://latex.codecogs.com/png.latex?%5Cbeta(0,1),%20%5Cgamma(2,1),%20%5Cbeta(2,1)">. La corrélation de Spearman entre les éléments de W ne change pas bien que la distribution des marginales ait changé, puisque nous appliquons une fonction croissante à chaque élément de Z.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">w1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qbeta</span>(Z[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-2">w2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qgamma</span>(Z[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-3">w3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qbeta</span>(Z[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-4">W <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(w1,w2,w3)</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(W,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spearman"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(W,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-3-2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
</div>
<p>On en conclut que la structure de dépendance d’un vecteur de variables aléatoire peut être isolée, caractériséee, et modélisée indépendamment des lois marginales/distributions univariées des composantes du vecteur aléatoire. Le concept de copule permet de modéliser cette structure de dépendance.</p>
<p>Si l’on utilise la correlation de Pearson, on constate que la corrélation entre les éléments de W et Z change. En effet, la corrélation de Pearson est une mesure de corrélation linéaire et ne permet pas de comparer, xdans tous les cas, les dépendances entre les variables aléatoires.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(X,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(Z,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spearman"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-4-2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs.panels</span>(W,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spearman"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson_files/figure-html/unnamed-chunk-4-3.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="384"></p>
</figure>
</div>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>other</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/others/spearman-pearson.html</guid>
  <pubDate>Thu, 06 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Calibration avec le modèle d’Heston</title>
  <link>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston.html</link>
  <description><![CDATA[ 




<p>Le but de ce TP est de calculer des prix d’options sous le modèle d’Heston puis de calibrer ce modèle par filtrage. On considère le modèle suivant :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AdS_s%20=%20S_s%20%5Cleft(%20rds%20+%20%5Csqrt%7Bv_s%7D%20dW_s%5E1%20%5Cright)%20%5C%5C%0Adv_s%20=%20%5Ckappa%20(%5Cbeta%20-%20v_s)%20ds%20+%20%5Csigma%20%5Csqrt%7Bv_s%7D%20dW_s%5E2%20%5C%5C%0AdW_s%5E1%20dW_s%5E2%20=%20%5Crho%20ds%0A%5Cend%7Bcases%7D%0A%5Cquad%20(1)%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?W_s%5E1"> et <img src="https://latex.codecogs.com/png.latex?W_s%5E2"> sont deux mouvements browniens et <img src="https://latex.codecogs.com/png.latex?r"> est le taux sans risque. Pour ce modèle, les rendements sont modélisés par un mouvement brownien géométrique avec une variance stochastique.</p>
<p>La volatilité non observée <img src="https://latex.codecogs.com/png.latex?v_t"> est déterminée par un processus stochastique de retour à la moyenne (1) introduit en 1985 par Cox, Ingersoll et Ross pour la modélisation des taux d’intérêt à court terme.</p>
<p>Le paramètre <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> est le paramètre de retour à la moyenne positive, <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> est le paramètre positif à long terme et <img src="https://latex.codecogs.com/png.latex?%5Ceta"> la volatilité positive du paramètre de variance. De plus, Heston a introduit une corrélation entre les deux mouvements browniens <img src="https://latex.codecogs.com/png.latex?W_s%5E1"> et <img src="https://latex.codecogs.com/png.latex?W_s%5E2">, représentée par le paramètre <img src="https://latex.codecogs.com/png.latex?%5Crho"> appartenant à <img src="https://latex.codecogs.com/png.latex?%5B-1,1%5D">.</p>
<section id="pricing-dun-call" class="level1">
<h1>Pricing d’un call</h1>
<section id="avec-la-forme-close" class="level2">
<h2 class="anchored" data-anchor-id="avec-la-forme-close">Avec la forme close</h2>
<p>Soit un Call de strike K et à échéance <img src="https://latex.codecogs.com/png.latex?%5Ctau"> sous le modèle (1) avec les paramètres suivants : <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> = 4,<img src="https://latex.codecogs.com/png.latex?%5Cbeta"> = 0.03,<img src="https://latex.codecogs.com/png.latex?%5Csigma"> = 0.4,r =0.05,<img src="https://latex.codecogs.com/png.latex?%5Crho">=−0.5,<img src="https://latex.codecogs.com/png.latex?%5Ctau"> = 1, <img src="https://latex.codecogs.com/png.latex?S_0"> = K=100,<img src="https://latex.codecogs.com/png.latex?v_0"> = <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
<p>Pour calculer le prix d’un Call, on peut utiliser la formule close de Heston (Heston 1993) :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC(S_0,%20K,%20%5Ctau)%20=%20S_0%20P_1%20-%20K%20e%5E%7B-r%20%5Ctau%7D%20P_2%0A"></p>
<p>avec :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_j(x,%20%5Cnu,%20T,%20%5Cln(K))%20=%20%5Cfrac%7B1%7D%7B2%7D%20+%20%5Cfrac%7B1%7D%7B%5Cpi%7D%20%5Cint_0%5E%5Cinfty%20%5CRe%20%5Cleft(%20%5Cfrac%7Be%5E%7B-i%20%5Cln(K)%20u%7D%20f_j(x,%5Cnu,t,u)%7D%7Bi%20u%7D%20%5Cright)%20du%0A"></p>
<p>où :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax%20=%20%5Cln(S_t),%20%5Cquad%20f(x,%5Cnu,t,u)%20=%20%5Cexp(C(t,u)%20+%20D(t,u)%20%5Cnu%20+%20i%20%5Cphi%20x)%0A"></p>
<p>et :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC(T-t%20=%20%5Ctau,%20%5Cphi)%20=%20r%20i%20%5Cphi%20t%20+%20%5Cfrac%7Ba%7D%7B%5Csigma%5E2%7D%20%5Cleft(%20(bj%20-%20%5Crho%20%5Csigma%20%5Cphi%20i%20+%20d)%5Ctau%20-%202%20%5Cln%20%5Cleft(%20%5Cfrac%7B1%20-%20g%20e%5E%7Bd%20%5Ctau%7D%7D%7B1%20-%20g%7D%20%5Cright)%20%5Cright)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AD(T-t%20=%20%5Ctau,%20%5Cphi)%20=%20%5Cleft(%20%5Cfrac%7Bbj%20-%20%5Crho%20%5Csigma%20%5Cphi%20i%20+%20d%7D%7B%5Csigma%5E2%7D%20%5Cright)%20%5Cleft(%20%5Cfrac%7B1%20-%20e%5E%7Bd%20%5Ctau%7D%7D%7B1%20-%20g%20e%5E%7Bd%20%5Ctau%7D%7D%20%5Cright)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ag%20=%20%5Cfrac%7Bbj%20-%20%5Crho%20%5Csigma%20%5Cphi%20i%20+%20d%7D%7Bbj%20-%20%5Crho%20%5Csigma%20%5Cphi%20i%20-%20d%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%20=%20%5Csqrt%7B(%5Crho%20%5Csigma%20%5Cphi%20i%20-%20bj)%5E2%20-%20%5Csigma%5E2%20(2%20u_j%20%5Cphi%20i%20-%20%5Cphi%5E2)%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Au_1%20=%201/2,%20%5Cquad%20u_2%20=%20-1/2,%20a%20=%20%5Clambda,%20b%20=%20%5Ckappa%20%5Cbeta,%20%5Cquad%20t_1%20=%20%5Ckappa%20-%20%5Crho%20%5Csigma,%20%5Cquad%20t_2%20=%20%5Ckappa%0A"></p>
<p>Pour ce faire, nous allons utiliser la fonction <code>Heston_Call_Function.R</code> qui permet de calculer le prix d’un Call sous le modèle d’Heston avec la formule close.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres</span></span>
<span id="cb1-2">kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb1-3">beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb1-4">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb1-5">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb1-6">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb1-7">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-8">S0<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb1-9">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb1-10">v0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import Heston_Call_Function.R</span></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/Heston_Call_Function.R"</span>)</span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du prix du Call</span></span>
<span id="cb1-16">Call_Heston <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> beta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K)</span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Le prix du Call est de "</span>, Call_Heston)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Le prix du Call est de  9.410405</code></pre>
</div>
</div>
</section>
<section id="avec-la-méthode-de-monte-carlo-schéma-deuler" class="level2">
<h2 class="anchored" data-anchor-id="avec-la-méthode-de-monte-carlo-schéma-deuler">Avec la méthode de Monte Carlo (Schéma d’Euler)</h2>
<p>Lorsqu’on a pas accès à la formule close, on peut utiliser la méthode de Monte Carlo pour calculer le prix d’un Call. Il s’agit de simuler le modèle (1) et de calculer le prix du Call à partir des simulations. Pour simuler le modèle (1), on peut utiliser la discrétisation d’Euler du modèle de Heston (Euler and Milstein Discretization, Fabrice Douglas Rouah) ou utiliser la formule de Ito pour le modèle de Heston.</p>
<p>Dans notre cas, nous allons utiliser la discrétisation d’Euler du modèle de Heston pour simuler le modèle (1) comme suit : <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AS_t%20=%20S_%7Bt-1%7D%20%5Cleft(1%20+%20r%20%5CDelta%20+%20%5Csqrt%7B%5CDelta%20v_t%7D%20W_t%5E1%20%5Cright)%20%5C%5C%5B10pt%5D%0Av_t%20=%20%5Cleft%7C%20v_%7Bt-1%7D%20+%20%5Ckappa%20%5CDelta%20(%5Cbeta%20-%20v_%7Bt-1%7D)%20+%20%5Csigma%20%5Csqrt%7Bv_%7Bt-1%7D%7D%20%5CDelta%20W_t%5E2%20%5Cright%7C%20%5C%5C%5B10pt%5D%0A%5Ctext%7BCov%7D(W_t%5E1,%20W_t%5E2)%20=%20%5Crho%0A%5Cend%7Bcases%7D%0A"></p>
<p>avec <img src="https://latex.codecogs.com/png.latex?W_t%5E1"> et <img src="https://latex.codecogs.com/png.latex?W_t%5E2"> des variables aléatoires gaussiennes centrées réduites et corrélées entre elles telles que <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(W_t%5E1,%20W_t%5E2)%20=%20%5Crho">. De plus, <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%20%5Cfrac%7B%5Ctau%7D%7Bn%7D"> est le pas de discrétisation, avec <img src="https://latex.codecogs.com/png.latex?n"> le nombre de pas de discrétisation.</p>
<p>Dans notre cas, on définit <img src="https://latex.codecogs.com/png.latex?n%20=%20100"> et on simule <img src="https://latex.codecogs.com/png.latex?M%20=%201000"> modèle (1) pour calculer le prix d’un Call.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">HestonCallMC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(M, N, lambda, vbar, eta, rho, v0, r, tau, S0, K){</span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># M: Number of Monte Carlo simulations</span></span>
<span id="cb3-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># N: Number of time steps</span></span>
<span id="cb3-4">  </span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb3-6">  dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tau <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time step</span></span>
<span id="cb3-7"></span>
<span id="cb3-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store final stock prices</span></span>
<span id="cb3-9">  ST <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(M)</span>
<span id="cb3-10">  </span>
<span id="cb3-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M){</span>
<span id="cb3-12">    S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-13">    v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-14">    </span>
<span id="cb3-15">    S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S0</span>
<span id="cb3-16">    v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v0</span>
<span id="cb3-17">    </span>
<span id="cb3-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N){</span>
<span id="cb3-19">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate correlated Brownian motions</span></span>
<span id="cb3-20">      W1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-21">      W2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-22">      </span>
<span id="cb3-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Euler discretization of variance process (ensure non-negativity)</span></span>
<span id="cb3-24">      v[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(v[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (vbar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v[t]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W1)</span>
<span id="cb3-25">      </span>
<span id="cb3-26">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Euler discretization of the stock price process (log-normal form)</span></span>
<span id="cb3-27">      S[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>((r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v[t]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v[t] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W2)</span>
<span id="cb3-28">    }</span>
<span id="cb3-29">    </span>
<span id="cb3-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store final stock price</span></span>
<span id="cb3-31">    ST[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S[N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-32">  }</span>
<span id="cb3-33"></span>
<span id="cb3-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute Call option price using Monte Carlo method</span></span>
<span id="cb3-35">  Call <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> tau) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(ST <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-36">  </span>
<span id="cb3-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(Call)</span>
<span id="cb3-38">}</span>
<span id="cb3-39"></span>
<span id="cb3-40">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb3-41">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-42">Call_Heston <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallMC</span>(M,N, kappa, beta, sigma, rho, v0, r, tau, S0, K)</span>
<span id="cb3-43"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Le prix du Call est de "</span>, Call_Heston)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Le prix du Call est de  9.797915</code></pre>
</div>
</div>
</section>
</section>
<section id="calibration-du-modèle-dheston-avec-les-prix-doptions" class="level1">
<h1>Calibration du modèle d’Heston avec les prix d’options</h1>
<p>On souhaite désormais calibrer la volatilité sous le modèle d’Heston à partir de prix d’options. Pour cela, nous considérons la version discrétisée par un schéma d’Euler (modifié) de (1). Plus précisément, soit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Ay_t%20=%20C(t,%20%5Ctheta,%20v_t,%20S_t,%20K,%20%5Ctau)%20+%20%5Csigma_%5Cvarepsilon%20%5Cvarepsilon_t%20%5C%5C%5B10pt%5D%0AS_t%20=%20S_%7Bt-1%7D%20%5Cleft(1%20+%20r%20%5CDelta%20+%20%5Csqrt%7B%5CDelta%20v_t%7D%20Z_t%5E1%20%5Cright)%20%5C%5C%5B10pt%5D%0Av_t%20=%20%5Cleft%7C%20v_%7Bt-1%7D%20+%20%5Clambda%20%5CDelta%20(%5Cbar%7Bv%7D%20-%20v_%7Bt-1%7D)%20+%20%5Ceta%20%5Csqrt%7Bv_%7Bt-1%7D%7D%20%5CDelta%20Z_t%5E2%20%5Cright%7C%20%5C%5C%5B10pt%5D%0A%5Ctext%7BCov%7D(Z_t%5E1,%20Z_t%5E2)%20=%20%5Crho%0A%5Cend%7Bcases%7D%0A"></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Importation des données</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb5-3">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/dataP_B.csv"</span>)</span>
<span id="cb5-4">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/dataS.csv"</span>)</span>
<span id="cb5-5">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/dataV.csv"</span>)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>HestonPB, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix des Calls"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(S<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sous_jacent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix du sous-jacent"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité obs."</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Prenons <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20(%5Ckappa%20=%204,%20%5Csigma%20=%200.4,%20%5Cbeta%20=%200.03,%20%5Crho%20=%20-0.5)">, <img src="https://latex.codecogs.com/png.latex?%5Csigma_%5Cvarepsilon%20=%200.1">, <img src="https://latex.codecogs.com/png.latex?%5Ctau=1"> et <img src="https://latex.codecogs.com/png.latex?K=100">. Pour estimer la volatilité, nous allons utiliser un filtre particulaire bootstrap. Il est défini comme suit :</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/data/bootstrap_filter.png" class="img-fluid figure-img"></p>
<figcaption>Filtre bootstrap particulaire</figcaption>
</figure>
</div>
<p>La loi stationnaire de <img src="https://latex.codecogs.com/png.latex?v_t"> est une loi gamma de paramètres <img src="https://latex.codecogs.com/png.latex?%5Calpha_1%20=%20%5Cfrac%7B2%20%5Ckappa%20%5Cbeta%7D%7B%5Csigma%5E2%7D"> et <img src="https://latex.codecogs.com/png.latex?%5Calpha_2%20=%20%5Cfrac%7B%5Csigma%5E2%7D%7B2%20%5Ckappa%7D">.</p>
<p>De plus, la densité de transition de <img src="https://latex.codecogs.com/png.latex?v_t%20%7C%20v_%7Bt-1%7D"> est une loi normale de moyenne <img src="https://latex.codecogs.com/png.latex?v_%7Bt-1%7D%20+%20%5Ckappa%20(%5Cbeta%20-%20v_%7Bt-1%7D)%20%5Ctau"> et de variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%20%5Csqrt%7Bv_%7Bt-1%7D%20%5Ctau%7D">. En effet, nous utilisons le schéma d’Euler pour simuler <img src="https://latex.codecogs.com/png.latex?v_t">.</p>
<p>En ce qui concerne la loi de <img src="https://latex.codecogs.com/png.latex?y_t%20%7C%20v_t">, on suppose que <img src="https://latex.codecogs.com/png.latex?y_t"> suit une loi normale de moyenne <img src="https://latex.codecogs.com/png.latex?C(t,%20%5Ctheta,%20v_t,%20S_t,%20K,%20%5Ctau)"> et de variance <img src="https://latex.codecogs.com/png.latex?%5Csigma_%5Cvarepsilon%5E2">.</p>
<p>Ainsi, nous initialisons les particules <img src="https://latex.codecogs.com/png.latex?v_0"> selon la loi stationnaire de <img src="https://latex.codecogs.com/png.latex?v_t"> et nous utilisons le schéma d’Euler pour simuler <img src="https://latex.codecogs.com/png.latex?v_t"> et <img src="https://latex.codecogs.com/png.latex?S_t">. Ensuite, nous calculons les poids des particules en utilisant la vraisemblance de l’observation <img src="https://latex.codecogs.com/png.latex?y_t"> et nous rééchantillonnons les particules selon leurs poids. Enfin, nous estimons la volatilité <img src="https://latex.codecogs.com/png.latex?v_t"> à l’instant <img src="https://latex.codecogs.com/png.latex?t"> en utilisant les particules rééchantillonnées.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definition des variables</span></span>
<span id="cb9-2">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>HestonPB</span>
<span id="cb9-3">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> S<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sous_jacent</span>
<span id="cb9-4">v<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Var</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pour rendre les simulations reproductibles</span></span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Définition des paramètres theta</span></span>
<span id="cb10-4">kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb10-5">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb10-6">beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span></span>
<span id="cb10-7">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb10-8">sigma_epsilon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb10-9">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-10">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-11">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import Heston_Call_Function.R</span></span>
<span id="cb10-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/Heston_Call_Function.R"</span>)</span>
<span id="cb10-15"></span>
<span id="cb10-16"></span>
<span id="cb10-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paramètres loi stationnaire de v</span></span>
<span id="cb10-18">alpha1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>kappa<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>beta)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb10-19">alpha2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>kappa)</span>
<span id="cb10-20"></span>
<span id="cb10-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des paramètres</span></span>
<span id="cb10-22">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nombre d'observations</span></span>
<span id="cb10-23">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nombre de particules</span></span>
<span id="cb10-24"></span>
<span id="cb10-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des matrices et vecteurs</span></span>
<span id="cb10-26">v_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(n)                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation de x</span></span>
<span id="cb10-27">v_particle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Particules</span></span>
<span id="cb10-28">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids des particules</span></span>
<span id="cb10-29">w_normalized <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> M) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids normalisés</span></span>
<span id="cb10-30"></span>
<span id="cb10-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtre particulaire bootstrap</span></span>
<span id="cb10-32"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb10-33">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb10-34">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des particules à t = 0</span></span>
<span id="cb10-35">    v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgamma</span>(M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha2)</span>
<span id="cb10-36">    </span>
<span id="cb10-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Poids initiaux (basés sur la distribution de l'état initial)</span></span>
<span id="cb10-38">    w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dgamma</span>(v_particle[t, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha2)</span>
<span id="cb10-39">    </span>
<span id="cb10-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalisation des poids</span></span>
<span id="cb10-41">    w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb10-42">    </span>
<span id="cb10-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimation initiale</span></span>
<span id="cb10-44">    v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb10-45">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb10-46">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Étape de prédiction (échantillonnage de nouvelles particules)</span></span>
<span id="cb10-47">    v_particle[t,] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (beta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v_particle[t<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> tau)))</span>
<span id="cb10-48">    </span>
<span id="cb10-49">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Calcul du prix du Call pour chaque particule</span></span>
<span id="cb10-50">    C <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(M)</span>
<span id="cb10-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M) {</span>
<span id="cb10-52">      C[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HestonCallClosedForm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> kappa, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vbar =</span> beta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> v_particle[t,i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S0 =</span> S[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K)</span>
<span id="cb10-53">    }</span>
<span id="cb10-54">    </span>
<span id="cb10-55">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Mise à jour des poids avec la vraisemblance de l'observation</span></span>
<span id="cb10-56">    w[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(y[t], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> C , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(sigma_epsilon))</span>
<span id="cb10-57">    </span>
<span id="cb10-58">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Normalisation des poids</span></span>
<span id="cb10-59">    w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w[t, ])</span>
<span id="cb10-60">    </span>
<span id="cb10-61">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Rééchantillonnage des particules selon leurs poids</span></span>
<span id="cb10-62">    index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> M, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> w_normalized[t, ], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb10-63">    v_particle[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v_particle[t, index]</span>
<span id="cb10-64">    </span>
<span id="cb10-65">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Réinitialisation des poids après rééchantillonnage (uniforme)</span></span>
<span id="cb10-66">    w_normalized[t, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> M</span>
<span id="cb10-67">    </span>
<span id="cb10-68">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Estimation de x à l'instant t (pondérée)</span></span>
<span id="cb10-69">    v_hat[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w_normalized[t, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_particle[t, ])</span>
<span id="cb10-70">  }</span>
<span id="cb10-71">}</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimation de la volatilité avec un filtre bootstrap"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volatilité"</span>)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(v_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. obs."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vol. est."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>paper</category>
  <category>proc-sto</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/proc_stochastique/modele_heston.html</guid>
  <pubDate>Wed, 05 Feb 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Le risque de marché en asset management</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-1.html</link>
  <description><![CDATA[ 




<p>La gestion d’actifs (ou <em>asset management</em>) est l’art de gérer un portefeuille d’actifs financiers. Il s’agit de gérer l’argent de clients — particuliers ou institutionnels — avec pour objectif principal de maximiser le rendement tout en minimisant le risque.</p>
<p><strong>Les étapes usuelles de gestion du risque</strong></p>
<ol type="1">
<li>Identification des risques</li>
</ol>
<p>On distingue généralement quatre grandes catégories de risques :</p>
<ul>
<li>Risque de marché : risque de perte en capital dû à la variation des prix des actifs financiers.</li>
<li>Risque de crédit : risque de défaut d’un émetteur, par exemple une entreprise qui ne rembourse pas ses obligations.</li>
<li>Risque de liquidité : risque de ne pas pouvoir acheter ou vendre rapidement un actif sans influencer son prix de manière significative.</li>
<li>Risque opérationnel : risque de perte résultant de processus internes défaillants, d’erreurs humaines ou de pannes de systèmes.</li>
</ul>
<ol start="2" type="1">
<li>Métriques de risque</li>
</ol>
<p>Ce sont des mesures quantitatives du risque. Parmi les plus utilisées : la volatilité, qui mesure la variation des rendements d’un actif par rapport à sa moyenne; la Value at Risk (VaR), qui mesure la perte maximale anticipée sur un portefeuille, avec un certain niveau de confiance <img src="https://latex.codecogs.com/png.latex?%5Calpha">, sur un horizon <img src="https://latex.codecogs.com/png.latex?T">, la Tracking Error, qui mesure l’écart de performance entre un portefeuille et son indice de référence.</p>
<ol start="3" type="1">
<li>Encadrement du risque</li>
</ol>
<p>Il s’agit de mettre en place des règles de gestion ou des limites pour éviter des dérives du portefeuille. Cela peut inclure :</p>
<ul>
<li>des limites de taille de position,</li>
<li>la diversification (éviter la concentration sur un petit nombre d’actifs),</li>
<li>l’usage de produits dérivés (options, futures) pour se couvrir contre les risques identifiés.</li>
</ul>
<p><strong>Constitution du portefeuille</strong></p>
<p>Dans notre étude, nous allons constituer un portefeuille de 10 actions choisies dans l’indice CAC 40, en leur attribuant des poids aléatoires.</p>
<p>Les actifs retenus sont les suivants :</p>
<ul>
<li>Sanofi<br>
</li>
<li>Société Générale<br>
</li>
<li>Thales<br>
</li>
<li>Engie<br>
</li>
<li>Capgemini<br>
</li>
<li>Carrefour<br>
</li>
<li>Orange<br>
</li>
<li>Accor<br>
</li>
<li>L’Oréal<br>
</li>
<li>Crédit Agricole</li>
</ul>
<p>Puisque nous travaillons avec un portefeuille d’actions, donc le principal risque est le risque de marché actions. Nous allons donc nous intéresser à trois indicateurs de risque : - la volatilité ex-ante, - la Value at Risk ex-ante, - la Tracking Error ex-ante<br>
c’est-à-dire des mesures anticipées, basées sur la composition actuelle du portefeuille, et non sur des données historiques (ex-post).</p>
<section id="récupération-des-données" class="level2">
<h2 class="anchored" data-anchor-id="récupération-des-données">Récupération des données</h2>
<div id="e04410fe" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------- Package Imports ---------#</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf </span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-12"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> norm</span></code></pre></div>
</div>
<div id="5bdb0f3c" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_data(start_date, end_date, index_ticker, tickers):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Extraction de données de cours d'actions</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des prix historiques des composants</span></span>
<span id="cb2-6">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(tickers, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_date, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>end_date, auto_adjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>]</span>
<span id="cb2-7"></span>
<span id="cb2-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des prix historiques de l'indice CAC 40</span></span>
<span id="cb2-9">    index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(index_ticker, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_date, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>end_date, auto_adjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Close'</span>]</span>
<span id="cb2-10"></span>
<span id="cb2-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {</span>
<span id="cb2-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>: data,</span>
<span id="cb2-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"benchmark_data"</span>: index,</span>
<span id="cb2-14">    }</span></code></pre></div>
</div>
<div id="fdb028d3" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-01-27"</span>)</span>
<span id="cb3-2">start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4">selected_assets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb3-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAN.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sanofi"</span>,</span>
<span id="cb3-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GLE.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Société générale"</span>,</span>
<span id="cb3-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HO.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thales"</span>,</span>
<span id="cb3-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ENGI.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Engie"</span>,</span>
<span id="cb3-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAP.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Capgemini"</span>,</span>
<span id="cb3-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carrefour"</span>,</span>
<span id="cb3-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Orange"</span>,</span>
<span id="cb3-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AC.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accor"</span>,</span>
<span id="cb3-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OR.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L'Oreal"</span>,</span>
<span id="cb3-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Crédit agricole"</span></span>
<span id="cb3-15">}</span>
<span id="cb3-16"></span>
<span id="cb3-17">index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span></span>
<span id="cb3-18"></span>
<span id="cb3-19">assets_ticker  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(selected_assets.keys())</span>
<span id="cb3-20"></span>
<span id="cb3-21">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_data(start_date,end_date, index, assets_ticker)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][**********************50%                       ]  5 of 10 completed[**********************50%                       ]  5 of 10 completed[**********************70%*********              ]  7 of 10 completed[**********************80%*************          ]  8 of 10 completed[**********************90%******************     ]  9 of 10 completed[*********************100%***********************]  10 of 10 completed
[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="8f6d7311" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">portfolio_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>]</span>
<span id="cb5-2">portfolio_data.tail()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th">Ticker</th>
<th data-quarto-table-cell-role="th">AC.PA</th>
<th data-quarto-table-cell-role="th">ACA.PA</th>
<th data-quarto-table-cell-role="th">CA.PA</th>
<th data-quarto-table-cell-role="th">CAP.PA</th>
<th data-quarto-table-cell-role="th">ENGI.PA</th>
<th data-quarto-table-cell-role="th">GLE.PA</th>
<th data-quarto-table-cell-role="th">HO.PA</th>
<th data-quarto-table-cell-role="th">OR.PA</th>
<th data-quarto-table-cell-role="th">ORA.PA</th>
<th data-quarto-table-cell-role="th">SAN.PA</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2025-01-20</td>
<td>46.959301</td>
<td>13.270039</td>
<td>12.445763</td>
<td>158.253357</td>
<td>14.549852</td>
<td>28.472115</td>
<td>147.275269</td>
<td>334.734375</td>
<td>9.728536</td>
<td>94.679825</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2025-01-21</td>
<td>47.017685</td>
<td>13.265357</td>
<td>12.381419</td>
<td>159.719574</td>
<td>14.453160</td>
<td>28.331999</td>
<td>148.801163</td>
<td>335.127289</td>
<td>9.761625</td>
<td>94.842651</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2025-01-22</td>
<td>47.728012</td>
<td>13.167060</td>
<td>12.082685</td>
<td>158.839844</td>
<td>14.296611</td>
<td>28.331999</td>
<td>149.785629</td>
<td>343.426880</td>
<td>9.671809</td>
<td>94.679825</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2025-01-23</td>
<td>47.270679</td>
<td>13.335569</td>
<td>12.156219</td>
<td>160.403824</td>
<td>14.273589</td>
<td>28.974592</td>
<td>150.474777</td>
<td>348.239655</td>
<td>9.648173</td>
<td>95.675957</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2025-01-24</td>
<td>47.659897</td>
<td>13.316846</td>
<td>12.225158</td>
<td>163.727234</td>
<td>14.273589</td>
<td>29.051897</td>
<td>149.933319</td>
<td>352.070282</td>
<td>9.600902</td>
<td>95.934570</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="489ed357" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">benchmark_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"benchmark_data"</span>]</span>
<span id="cb6-2">benchmark_data.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th">Ticker</th>
<th data-quarto-table-cell-role="th">^FCHI</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-01-30</td>
<td>7082.009766</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2023-01-31</td>
<td>7082.419922</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-02-01</td>
<td>7077.109863</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2023-02-02</td>
<td>7166.270020</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-02-03</td>
<td>7233.939941</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="bb3ff0c8" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On attribue des poids équitables pour chaque action</span></span>
<span id="cb7-2">weights_by_asset <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {ticker: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(assets_ticker) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> assets_ticker}</span></code></pre></div>
</div>
<p>Pour connaitre la valeur totale des actifs du portefeuille, nous allons utiliser la notion d’asset under management (AUM) défini comme suit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AAUM(T_n)%20=%20%5Csum_%7Bi=1%7D%5E%7B10%7D%20%5Comega_i%20%5Ctimes%20P_i(T_n),%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Comega_i"> est le poids de l’actif <img src="https://latex.codecogs.com/png.latex?i"> dans le portefeuille et <img src="https://latex.codecogs.com/png.latex?P_i(T_n)"> est le prix de l’actif <img src="https://latex.codecogs.com/png.latex?i"> à la date <img src="https://latex.codecogs.com/png.latex?T_n">.</p>
<p>Puisque les rendements sont les seuls facteurs de risque de l’AUM, nous allons nous intéresser à la variation de l’AUM entre deux dates <img src="https://latex.codecogs.com/png.latex?T_n"> et <img src="https://latex.codecogs.com/png.latex?T_%7Bn+1%7D">, soit :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CDelta%20AUM(T_n,%20T_%7Bn+1%7D)%20=%20AUM(T_%7Bn+1%7D)%20-%20AUM(T_n)%20=%20%5Csum_%7Bi=1%7D%5E%7B10%7D%20%5Comega_i%20%5Ctimes%20(P_i(T_%7Bn+1%7D)%20-%20P_i(T_n)).%0A"></p>
<div id="ee2f561a" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">aum_series <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> portfolio_data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> row: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(weights_by_asset[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> row[ticker] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> weights_by_asset), axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb8-2">aum_series</span>
<span id="cb8-3"></span>
<span id="cb8-4">AUM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(aum_series, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUM"</span>])</span>
<span id="cb8-5">AUM.head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="7">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">AUM</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-01-30</td>
<td>81.287687</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2023-01-31</td>
<td>81.069776</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-02-01</td>
<td>80.829767</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2023-02-02</td>
<td>81.927702</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2023-02-03</td>
<td>82.645898</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="b7e5ceee" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------------------- #</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evolution de la valeur totale du portefeuille</span></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------------------------- #</span></span>
<span id="cb9-4"></span>
<span id="cb9-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb9-6">plt.plot(AUM, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUM"</span>)</span>
<span id="cb9-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Evolution de l'actif sous gestion"</span>)</span>
<span id="cb9-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>)</span>
<span id="cb9-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Valeur"</span>)</span>
<span id="cb9-10">plt.legend()</span>
<span id="cb9-11">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-1_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="73becae3" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On s'interesse aux variations/rendements de l'AUM</span></span>
<span id="cb10-2">AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUM"</span>].pct_change()</span>
<span id="cb10-3">AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].head()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>Date
2023-01-30         NaN
2023-01-31   -0.002681
2023-02-01   -0.002961
2023-02-02    0.013583
2023-02-03    0.008766
Name: Variation, dtype: float64</code></pre>
</div>
</div>
<section id="estimation-de-la-volatilité" class="level3">
<h3 class="anchored" data-anchor-id="estimation-de-la-volatilité">Estimation de la volatilité</h3>
<p>Pour estimer la volatilité du portefeuille, on peut calculer l’écart-type des variations de l’AUM. On fait le choix de calculer une volatilité ex-ante en se basant sur les variation historiques des prix des actifs avec une profondeur historique de 2 ans. Vu qu’on a une volatilité quotidienne, on va l’annualiser en multipliant par <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B252%7D">.</p>
<p>En général, sur le marché action, la volatilité quotidienne est environ de 1% et la volatilité annuelle est entre 10% et 20%.</p>
<div id="7f6cd7f8" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la volatilité du portefeuille</span></span>
<span id="cb12-2">volatility_portfolio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>])</span>
<span id="cb12-3">annualized_volatility_portfolio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> volatility_portfolio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">252</span>)</span>
<span id="cb12-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité de la performance quotidienne : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>volatility_portfolio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité de la performance annuelle : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>annualized_volatility_portfolio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité de la performance quotidienne :  0.87%
Volatilité de la performance annuelle :  13.76%</code></pre>
</div>
</div>
<div id="b871dc34" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la volatilité de l'indice CAC 40</span></span>
<span id="cb14-2"></span>
<span id="cb14-3">benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>].pct_change()</span>
<span id="cb14-4">volatility_benchmark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>])</span>
<span id="cb14-5">annualized_volatility_benchmark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> volatility_benchmark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">252</span>)</span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité de l'indice CAC 40 : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>volatility_benchmark <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb14-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Volatilité de l'indice CAC 40 annuelle : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>annualized_volatility_benchmark <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Volatilité de l'indice CAC 40 :  0.84%
Volatilité de l'indice CAC 40 annuelle :  13.37%</code></pre>
</div>
</div>
<p>Notre portefeuille nous fournit une volatatilité quotidienne supérieure de 3bps à la volatilité du CAC 40. On retrouve sur à peu près la même volatilité du portefeuille et celle du CAC 40. Il y a donc une certaine homogénéité dans le portefeuille que nous avons constitué.</p>
<blockquote class="blockquote">
<p>💡 <em>Note : bp = 0,01%</em></p>
</blockquote>
</section>
<section id="estimation-de-la-tracking-errorerreur-de-suivi" class="level3">
<h3 class="anchored" data-anchor-id="estimation-de-la-tracking-errorerreur-de-suivi">Estimation de la tracking error/erreur de suivi</h3>
<p>La tracking error est une mesure de l’écart entre la performance d’un portefeuille et celle de son indice de référence. Elle est calculée comme la volatilité de la différence entre les rendements du portefeuille et de l’indice de référence :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0ATE%20=%20%5Csqrt%7BVar(R_p%20-%20R_b)%7D%0A"></p>
<p>La tracking error mesure l’incercitude du portefeuille par rapport à l’indice de référence, c’est une mesure relative. Plus la tracking error est élevée, plus le portefeuille est risqué. On ne souhaite sous ou sur-performer l’indice de référence. On souhaite suivre véritablement l’indice de référence.</p>
<p>Pour l’annualiser, on multiplie par <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B252%7D"> en supposant que les performances quotidiennes sont indépendantes et donc un utilise l’additivité des variances.</p>
<div id="f9c02566" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">performance_relative <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>]</span>
<span id="cb16-2"></span>
<span id="cb16-3">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb16-4">plt.plot(performance_relative, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Performance"</span>)</span>
<span id="cb16-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Performance du portefeuille par rapport à l'indice CAC 40"</span>)</span>
<span id="cb16-6">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>)</span>
<span id="cb16-7">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Performance"</span>)</span>
<span id="cb16-8">plt.legend()</span>
<span id="cb16-9"></span>
<span id="cb16-10">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-1_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="d6716176" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la tracking error</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">TE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>]) </span>
<span id="cb17-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Tracking error : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>TE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-5"></span>
<span id="cb17-6">TE_annualized <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> TE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">252</span>)</span>
<span id="cb17-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Tracking error annualisé : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>TE_annualized <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Tracking error :  0.51%
Tracking error annualisé :  8.05%</code></pre>
</div>
</div>
</section>
<section id="estimation-de-la-value-at-risk-var" class="level3">
<h3 class="anchored" data-anchor-id="estimation-de-la-value-at-risk-var">Estimation de la Value-at-Risk (VaR)</h3>
<p>La VaR est une mesure de risque qui donne une estimation de la perte maximale que l’on peut subir avec un certain niveau de confiance <img src="https://latex.codecogs.com/png.latex?%5Calpha"> sur un horizon de temps donné. Par exemple, une VaR à 5% sur 1 jour de 1000 euros signifie que 95% du temps, on ne perdra pas plus de 1000 euros sur un jour.</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7BLoss%7D%20%3C%20%5Ctext%7BVaR%7D)%20=%20%5Calpha."></p>
<p>On peut également raisonner en terme de gain, i.e.&nbsp;Profit and Loss (PnL).</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7BPnL%7D%20%3E%20-%20%5Ctext%7BVaR%7D)%20=%20%5Calpha."></p>
<p>La VaR peut se calculer suivant trois approches : 1. Approche historique : On se base sur les rendements passés selon l’horizon fixé pour estimer la VaR, à l’aide d’un quantile empirique d’ordre <img src="https://latex.codecogs.com/png.latex?%5Calpha">. Autrement, on peut se baser sur les rendements journaliers et utiliser la méthode de rescaling, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?VaR%20=%20%5Csigma%20%5Ctimes%20%5CPhi%5E%7B-1%7D(%5Calpha)">. 2. Approche paramétrique : On suppose que les rendements suivent une loi normale. 3. Approche Monte Carlo : On simule les rendements futurs.</p>
<p>Puisqu’on s’intéresse à un portefeuille d’actions qui a un indice de référence, on peut également calculer la VaR relative. La VaR relative suit une philosophie proche du tracking error. Elle se calcule sur les écarts entre le portefeuille et le benchmark. Elle sert à mesurer de combien mon portefeuille sous-performe par rapport à l’indice de référence.</p>
<div id="25d7d064" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------- #</span></span>
<span id="cb19-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR historique</span></span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --------------------------------- #</span></span>
<span id="cb19-4"></span>
<span id="cb19-5">seuil <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb19-6"></span>
<span id="cb19-7">VaR_hist_portfolio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna(), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb19-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> VaR_hist_portfolio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique sur 20 jours sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>VaR_hist_portfolio<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-10"></span>
<span id="cb19-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=*="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb19-12"></span>
<span id="cb19-13">VaR_hist_benchmark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna(), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb19-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique sur l'indice CAC 40 : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>VaR_hist_benchmark <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique sur 20 jours sur l'indice CAC 40 : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>VaR_hist_benchmark<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>VaR historique sur le portefeuille :  2.31%
VaR historique sur 20 jours sur le portefeuille :  10.32%
=*==*==*==*==*==*==*==*==*==*=
VaR historique sur l'indice CAC 40 :  2.13%
VaR historique sur 20 jours sur l'indice CAC 40 :  9.53%</code></pre>
</div>
</div>
<div id="fbbd0ac9" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------------- #</span></span>
<span id="cb21-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR paramétrique</span></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------------- #</span></span>
<span id="cb21-4"></span>
<span id="cb21-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PnL ~ N(mu, sigma) ==&gt; PnL = mu + sigma * Z, où Z ~ N(0,1)</span></span>
<span id="cb21-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P(PnL &gt; -VaR) = alpha &lt;=&gt; P(mu + sigma * Z &gt; -VaR) = alpha &lt;=&gt; P(Z &lt; (-VaR - mu) / sigma) = 1 - alpha</span></span>
<span id="cb21-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Donc, -VaR = mu + sigma * quantile(1 - alpha), où quantile(1 - alpha) est le quantile de la loi normale standard</span></span>
<span id="cb21-8"></span>
<span id="cb21-9"></span>
<span id="cb21-10">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna())</span>
<span id="cb21-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"mu sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-12">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(AUM[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna())</span>
<span id="cb21-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"sigma sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-14"></span>
<span id="cb21-15">VaR_param_portfolio  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb21-16"></span>
<span id="cb21-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_portfolio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique sur 20 jours sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_portfolio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-19"></span>
<span id="cb21-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=*="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb21-21"></span>
<span id="cb21-22">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna())</span>
<span id="cb21-23"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"mu sur le benchmark: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-24">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(benchmark_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>].dropna())</span>
<span id="cb21-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"sigma sur le benchmark : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-26"></span>
<span id="cb21-27">VaR_param_benchmark  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb21-28"></span>
<span id="cb21-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_benchmark <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique sur 20 jours sur le portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_benchmark <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>mu sur le portefeuille :  0.00021
sigma sur le portefeuille :  0.0087
VaR paramétrique sur le portefeuille :  1.99%
VaR paramétrique sur 20 jours sur le portefeuille :  8.92%
=*==*==*==*==*==*==*==*==*==*=
mu sur le benchmark:  0.00026
sigma sur le benchmark :  0.0084
VaR paramétrique sur le portefeuille :  1.93%
VaR paramétrique sur 20 jours sur le portefeuille :  8.65%</code></pre>
</div>
</div>
<div id="77259174" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------------- #</span></span>
<span id="cb23-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VaR relative</span></span>
<span id="cb23-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------------- #</span></span>
<span id="cb23-4"></span>
<span id="cb23-5">VaR_hist_relative <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(performance_relative.dropna(), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb23-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique relative : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> VaR_hist_relative <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR historique relative sur 20 jours : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>VaR_hist_relative<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-8"></span>
<span id="cb23-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=*="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb23-10"></span>
<span id="cb23-11">mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(performance_relative.dropna())</span>
<span id="cb23-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"mu des performances relatives: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-13">sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.std(performance_relative.dropna())</span>
<span id="cb23-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"sigma des performances relatives : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-15"></span>
<span id="cb23-16">VaR_param_relative  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm.ppf(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> seuil))</span>
<span id="cb23-17"></span>
<span id="cb23-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique relative : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_relative <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"VaR paramétrique relative sur 20 jours : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>VaR_param_relative <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: .2%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>VaR historique relative :  1.08%
VaR historique relative sur 20 jours :  4.83%
=*==*==*==*==*==*==*==*==*==*=
mu des performances relatives: -4.7e-05
sigma des performances relatives :  0.0051
VaR paramétrique relative :  1.18%
VaR paramétrique relative sur 20 jours :  5.29%</code></pre>
</div>
</div>
</section>
</section>
<section id="stress-test" class="level1">
<h1>Stress test</h1>
<p>Pour tester la robustesse du portefeuille, on peut réaliser des stress tests. Ces tests permettent d’évaluer la performance du portefeuille dans des conditions extrêmes de marché. Ils sont souvent utilisés pour évaluer la résilience d’un portefeuille face à des événements de marché inattendus ou extrêmes.</p>
<p>Ils sont de deux natures :</p>
<ol type="1">
<li><p>Stress test historique : On soumet le portefeuille à une période historique ou on estime avoir eu une condition extrême (Covid, Subprime crisis). On rejoue un scénario qui s’est déjà passé.</p></li>
<li><p>Stress test hypothétique : On joue un scénario qui ne s’est jamais réalisé. Exemple, si les actions chutent de 40%, notre portefeuille d’action chute de 40%.</p></li>
</ol>
<p>Dans notre cas, nous allons réaliser un stress test historique sur la crise de covid. Pour cela, nous allons extraire le prix des actifs du 19 février 2020 et du 18 mars 2020. Nous allons ensuite calculer la performance du portefeuille sur cette période. On va également calculer la performance du CAC 40 sur cette période pour voir si notre portefeuille a été plus ou moins résilient que le marché.</p>
<div id="44a44227" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recuperons les prix des actifs le 19/02/2020 et le 18/03/2020</span></span>
<span id="cb25-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On va valoriser notre portefeuille à ces dates et calculer les performances</span></span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A ces dates, le CAC 40 a connu de fortes pertes pendant la COVID-19</span></span>
<span id="cb25-4"></span>
<span id="cb25-5"></span>
<span id="cb25-6">start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"19-02-2020"</span>, dayfirst<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb25-7">end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb25-8"></span>
<span id="cb25-9"></span>
<span id="cb25-10">data_1902 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_data(start_date,end_date, index, assets_ticker)</span>
<span id="cb25-11">portfolio_data_1902<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_1902[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>]</span>
<span id="cb25-12">benchmark_data_1902<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_1902[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'benchmark_data'</span>]</span>
<span id="cb25-13"></span>
<span id="cb25-14">start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18-03-2020"</span>, dayfirst<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb25-15">end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb25-16"></span>
<span id="cb25-17"></span>
<span id="cb25-18">data_1803 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_data(start_date,end_date, index, assets_ticker)</span>
<span id="cb25-19">portfolio_data_1803<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_1803[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>]</span>
<span id="cb25-20">benchmark_data_1803<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data_1803[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'benchmark_data'</span>]</span>
<span id="cb25-21"></span>
<span id="cb25-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concaténer les données des deux dates pour le portefeuille et le benchmark</span></span>
<span id="cb25-23">portfolio_data_stress <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([portfolio_data_1902, portfolio_data_1803], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb25-24">benchmark_data_stress <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([benchmark_data_1902, benchmark_data_1803], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[                       0%                       ][**********            20%                       ]  2 of 10 completed[**********            20%                       ]  2 of 10 completed[**********            20%                       ]  2 of 10 completed[**********            20%                       ]  2 of 10 completed[**********************60%****                   ]  6 of 10 completed[**********************70%*********              ]  7 of 10 completed[**********************80%*************          ]  8 of 10 completed[**********************90%******************     ]  9 of 10 completed[*********************100%***********************]  10 of 10 completed
[*********************100%***********************]  1 of 1 completed
[                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][**********************70%*********              ]  7 of 10 completed[**********************80%*************          ]  8 of 10 completed[**********************80%*************          ]  8 of 10 completed[*********************100%***********************]  10 of 10 completed
[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="6bf196ea" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stress test covid sur le portefeuille</span></span>
<span id="cb27-2">aum_series_stress <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> portfolio_data_stress.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> row: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(weights_by_asset[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> row[ticker] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> weights_by_asset), axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb27-3"></span>
<span id="cb27-4">AUM_stress <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(aum_series_stress, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUM"</span>])</span>
<span id="cb27-5">AUM_stress[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AUM_stress[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AUM"</span>].pct_change()</span>
<span id="cb27-6"></span>
<span id="cb27-7">AUM_stress.iloc[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="18">
<pre><code>AUM          42.829420
Variation    -0.305734
Name: 2020-03-18 00:00:00, dtype: float64</code></pre>
</div>
</div>
<div id="7f222860" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">benchmark_data_stress[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variation"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>benchmark_data_stress[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>].pct_change()</span>
<span id="cb29-2">benchmark_data_stress.iloc[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="19">
<pre><code>Ticker
^FCHI        3754.840088
Variation      -0.385585
Name: 2020-03-18 00:00:00, dtype: float64</code></pre>
</div>
</div>
<p>Dans ce cas, nous constatons une perte d’environ 30.64% sur le portefeuille et une perte de 38.55% sur le CAC 40. De ce fait, notre portefeuille a été plus résilient que le marché. En effet, la perte du portefeuille est inférieure à celle du CAC 40. On peut donc conclure que notre portefeuille permet de mieux resister au stress test covid que le CAC 40.</p>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Le risque de marché constitue un enjeu central dans la gestion d’actifs. Il est inhérent à toute exposition aux marchés financiers et doit, à ce titre, être mesuré, surveillé et encadré avec rigueur afin de préserver les intérêts des investisseurs.</p>
<p>Des outils tels que la volatilité, la tracking error et la Value at Risk (VaR) permettent de quantifier l’incertitude liée aux rendements du portefeuille et d’anticiper les pertes potentielles dans des conditions normales de marché.</p>
<p>En complément, les stress tests jouent un rôle fondamental : ils permettent d’évaluer la résilience du portefeuille face à des scénarios extrêmes, souvent absents des données historiques, mais pourtant plausibles dans un contexte de crise.</p>


</section>
</section>

 ]]></description>
  <category>paper</category>
  <category>asset-management</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-1.html</guid>
  <pubDate>Sun, 26 Jan 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Risques de liquidité (profil d’écoulement / de liquidation d’un portefeuille d’actifs)</title>
  <dc:creator>Cheryl KOUADIO</dc:creator>
  <link>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2.html</link>
  <description><![CDATA[ 




<p>Le risque de liquidité est l’une des quatre grandes catégories de risques auxquels une banque peut être exposée. Il concerne à la fois la liquidité du marché et le risque opérationnel de financement.</p>
<ul>
<li>La liquidité du marché fait référence à la capacité d’un investisseur à acheter ou vendre un actif sans en affecter significativement le prix.</li>
<li>Le risque opérationnel de financement concerne la capacité à lever des fonds pour financer ses activités.</li>
</ul>
<p>Ainsi, le risque de liquidité est crucial dans la gestion d’un portefeuille, notamment en période de tensions sur les marchés. Si un investisseur doit liquider un actif, il peut être contraint de le vendre à un prix inférieur à sa valeur fondamentale, entraînant ainsi une perte importante.</p>
<p>Pour évaluer ce risque, plusieurs indicateurs sont utilisés :</p>
<ul>
<li>le coût de liquidité,</li>
<li>le profil d’écoulement (ou profil de liquidation),</li>
<li>les gaps de liquidité,</li>
<li>les stress tests de liquidité.</li>
</ul>
<p>Dans le cadre de notre étude, nous nous concentrons sur le profil d’écoulement d’un portefeuille composé de 10 actifs, et ce dans les quatre scénarios suivants :</p>
<p><strong>1. Conditions normales avec déformation :</strong> On considère les volumes moyens de marché, que l’on déforme pour obtenir les quantités effectivement liquidées.<br>
La liquidation débute avec les actifs les plus liquides et se termine avec les plus illiquides.<br>
Ce processus entraîne une déformation du portefeuille :<br>
les premiers investisseurs récupèrent une part plus liquide, tandis que ceux qui restent se retrouvent avec des actifs plus illiquides.</p>
<p><strong>2. Conditions normales sans déformation :</strong> Dans ce cas, on cherche à préserver l’équilibre du portefeuille, afin de ne pas pénaliser les investisseurs restants.<br>
La liquidation est répartie uniformément, sans privilégier les actifs les plus liquides.</p>
<p><strong>3. Conditions stressées avec déformation :</strong> Les marchés sont en tension, les volumes chutent drastiquement.<br>
On liquide en priorité les actifs les plus liquides, ce qui accentue l’illiquidité résiduelle du portefeuille.</p>
<p><strong>4. Conditions stressées sans déformation :</strong> Même en période de stress, on cherche à conserver une proportionnalité équitable dans la liquidation, pour éviter une concentration d’actifs illiquides pour les investisseurs restants.</p>
<p><strong>Étapes de mise en œuvre</strong></p>
<p>Pour chaque scénario, on procède selon les étapes suivantes :</p>
<ol type="1">
<li><p>Récupération des volumes journaliers sur une période historique de 3 mois, à compter de la date d’aujourd’hui, i.e.&nbsp;le 28 mars 2025.</p></li>
<li><p>Calcul de l’ADV sur 3 mois <em>(Average Daily Volume)</em> :<br>
<img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BADV%7D_%7BT=3M%7D%20=%20%5Cfrac%7B1%7D%7BT%7D%20%5Csum_%7Bt=1%7D%5E%7BT%7D%20V_t,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?V_t"> est le volume quotidien et <img src="https://latex.codecogs.com/png.latex?T"> le nombre de jours ouvrés sur les 3 mois.</p></li>
<li><p>Génération des quantités associées à chaque actif dans le portefeuille.</p></li>
<li><p>Pour obtenir un portefeuille ni trop liquide, ni trop illiquide, on applique la formule : <img src="https://latex.codecogs.com/png.latex?%0AQ_i%20=%201.5%20%5Ctimes%20%5Ctext%7Brandn%7D()%20%5Ctimes%20%5Ctext%7BADV%7D_%7B3M%7D%5E%7B(i)%7D,%0A"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Brandn%7D()%20%5Csim%20%5Cmathcal%7BU%7D%5B0,%201%5D"> est une variable aléatoire uniforme, et <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BADV%7D_%7B3M%7D%5E%7B(i)%7D"> l’ADV de l’actif <img src="https://latex.codecogs.com/png.latex?i">.</p></li>
<li><p>Calcul du profil de liquidation : Ce profil est construit en fonction de la quantité liquidée chaque jour, en tenant compte de la limite fixée par le volume moyen quotidien de chaque actif.</p></li>
</ol>
<p>Nous utiliserons un portefeuille fictif de 10 actifs du CAC, à savoir :</p>
<ul>
<li>Sanofi</li>
<li>Societe Generale</li>
<li>Thales</li>
<li>Engie</li>
<li>Capgemini</li>
<li>Carrefour</li>
<li>Orange</li>
<li>Accor</li>
<li>L’Oreal</li>
<li>Crédit agricole</li>
</ul>
<section id="récupération-des-données" class="level3">
<h3 class="anchored" data-anchor-id="récupération-des-données">Récupération des données</h3>
<div id="bed95df7" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------- Package Imports ---------#</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#------------------------------------#</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb1-6">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> yfinance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> yf </span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span></code></pre></div>
</div>
<div id="7874b44e" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_data(start_date, end_date, index_ticker, tickers, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>):</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Extraction de prix de clôture d'actions</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb2-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des volumes historiques des composants</span></span>
<span id="cb2-6">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(tickers, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_date, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>end_date, auto_adjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)[column]</span>
<span id="cb2-7"></span>
<span id="cb2-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extraction des volumes historiques de l'indice de reférence</span></span>
<span id="cb2-9">    index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> yf.download(index_ticker, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_date, end<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>end_date, auto_adjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)[column]</span>
<span id="cb2-10"></span>
<span id="cb2-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {</span>
<span id="cb2-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>: data,</span>
<span id="cb2-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"benchmark_data"</span>: index,</span>
<span id="cb2-14">    }</span>
<span id="cb2-15"></span>
<span id="cb2-16">end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now()</span>
<span id="cb2-17">start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb2-18"></span>
<span id="cb2-19">selected_assets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAN.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sanofi"</span>,</span>
<span id="cb2-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GLE.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Société générale"</span>,</span>
<span id="cb2-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HO.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thales"</span>,</span>
<span id="cb2-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ENGI.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Engie"</span>,</span>
<span id="cb2-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAP.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Capgemini"</span>,</span>
<span id="cb2-25">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carrefour"</span>,</span>
<span id="cb2-26">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Orange"</span>,</span>
<span id="cb2-27">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AC.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accor"</span>,</span>
<span id="cb2-28">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OR.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L'Oreal"</span>,</span>
<span id="cb2-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACA.PA"</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Crédit agricole"</span>,</span>
<span id="cb2-30">    }</span>
<span id="cb2-31"></span>
<span id="cb2-32">index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span></span>
<span id="cb2-33"></span>
<span id="cb2-34">assets_ticker  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(selected_assets.keys())</span>
<span id="cb2-35"></span>
<span id="cb2-36">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_data(start_date,end_date, index, assets_ticker, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Volume"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][                       0%                       ][**********************80%*************          ]  8 of 10 completed[**********************90%******************     ]  9 of 10 completed[*********************100%***********************]  10 of 10 completed
[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
</div>
<div id="a532a5bb" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">portfolio_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>]</span>
<span id="cb4-2">portfolio_data.tail()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th">Ticker</th>
<th data-quarto-table-cell-role="th">AC.PA</th>
<th data-quarto-table-cell-role="th">ACA.PA</th>
<th data-quarto-table-cell-role="th">CA.PA</th>
<th data-quarto-table-cell-role="th">CAP.PA</th>
<th data-quarto-table-cell-role="th">ENGI.PA</th>
<th data-quarto-table-cell-role="th">GLE.PA</th>
<th data-quarto-table-cell-role="th">HO.PA</th>
<th data-quarto-table-cell-role="th">OR.PA</th>
<th data-quarto-table-cell-role="th">ORA.PA</th>
<th data-quarto-table-cell-role="th">SAN.PA</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Date</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">2026-02-04</td>
<td>652002</td>
<td>8139828</td>
<td>4003844</td>
<td>1055505</td>
<td>7024229</td>
<td>3211658</td>
<td>262612</td>
<td>309740</td>
<td>8085866</td>
<td>2767517</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2026-02-05</td>
<td>677533</td>
<td>5694454</td>
<td>2079899</td>
<td>583137</td>
<td>6766158</td>
<td>3552763</td>
<td>320840</td>
<td>465105</td>
<td>4898256</td>
<td>2838101</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2026-02-06</td>
<td>464918</td>
<td>3678669</td>
<td>1912522</td>
<td>859927</td>
<td>4315366</td>
<td>4256053</td>
<td>287196</td>
<td>319279</td>
<td>5470913</td>
<td>2139471</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">2026-02-09</td>
<td>565243</td>
<td>4255601</td>
<td>1525658</td>
<td>495323</td>
<td>2945488</td>
<td>2629828</td>
<td>219148</td>
<td>319083</td>
<td>4828547</td>
<td>1966157</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2026-02-10</td>
<td>649779</td>
<td>4146587</td>
<td>1789102</td>
<td>849739</td>
<td>3005299</td>
<td>2399520</td>
<td>201360</td>
<td>409816</td>
<td>3572279</td>
<td>2483671</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>L’Average Daily Volume (ADV) ou ATV (Average Traded Volume) est un indicateur clé en finance, qui permet de mesurer la liquidité d’un actif. Il est calculé en prenant la moyenne des volumes échangés sur une période donnée. Plus l’ADV est élevé, plus l’actif est liquide, et plus il est facile de le vendre sans impacter le prix du marché.</p>
<p>En général, c’est l’ADV sur 3 mois qui est utilisé, car il permet de lisser les variations quotidiennes et de donner une vision plus précise de la liquidité d’un actif.</p>
<p><img src="https://latex.codecogs.com/png.latex?ADV%20=%20%5Cfrac%7B%5Csum_%7Bi=1%7D%5E%7Bn%7D%20V_i%7D%7Bn%7D,"></p>
<p>où <img src="https://latex.codecogs.com/png.latex?V_i"> est le volume échangé le jour <img src="https://latex.codecogs.com/png.latex?i">, et <img src="https://latex.codecogs.com/png.latex?n"> est le nombre de jours sur la période considérée.</p>
<p>Dans le code ci-dessous, nous avons calculé l’ADV sur 3 mois pour chaque actif du portefeuille. Nous constatons que l’actif le plus liquide est <strong>Orange</strong>, avec un ADV d’environ 7,7 million d’actions échangées par jour, tandis que l’actif le moins liquide est <strong>Thales</strong>, avec un ADV d’environ 0,4 million d’actions échangées par jour. De ce fait, durant ces trois mois, l’action Orange a été plus plus liquide que l’action Thales.</p>
<div id="c502e83c" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------#</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des ADV 3Mois#</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------#</span></span>
<span id="cb5-4"></span>
<span id="cb5-5">adv_3m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {ticker : np.mean(portfolio_data[ticker]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> assets_ticker}</span>
<span id="cb5-6">adv_3m</span>
<span id="cb5-7"></span>
<span id="cb5-8">ADV <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame.from_dict(adv_3m, orient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'index'</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ADV'</span>])</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># trier par ADV</span></span>
<span id="cb5-11">ADV.sort_values(by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ADV'</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">ADV</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>4.132509e+06</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ORA.PA</td>
<td>3.985574e+06</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">ACA.PA</td>
<td>3.587703e+06</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CA.PA</td>
<td>2.196726e+06</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>2.101232e+06</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>1.956711e+06</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">AC.PA</td>
<td>5.840311e+05</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>4.504017e+05</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">OR.PA</td>
<td>3.043389e+05</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>2.370661e+05</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Pour générer le volume détenu dans notre portefeuille, nous allons utiliser la formule suivante :</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVolume%20d%C3%A9tenu%7D%20=%201.5%20%5Ctimes%20%5Ctext%7Brandn()%7D%20%5Ctimes%20%5Ctext%7BADV%203M%7D%0A"></p>
<p>Cette formule sert à simuler un portefeuille réaliste où les tailles de position sont ajustées à la liquidité de chaque actif, avec une variabilité aléatoire maîtrisée. Elle est simple, mais très utile pour tester des hypothèses de liquidation, de stress, ou de concentration.</p>
<div id="84ecf470" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Génération des quantités</span></span>
<span id="cb6-2">np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb6-3">ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.random.rand(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ADV"</span>])) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ADV"</span>])</span>
<span id="cb6-4">ADV</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">ADV</th>
<th data-quarto-table-cell-role="th">Quantity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>1.956711e+06</td>
<td>2044183.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>2.101232e+06</td>
<td>901868.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>2.370661e+05</td>
<td>80668.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>4.132509e+06</td>
<td>3417470.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>4.504017e+05</td>
<td>486075.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CA.PA</td>
<td>2.196726e+06</td>
<td>1394173.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">ORA.PA</td>
<td>3.985574e+06</td>
<td>5863363.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">AC.PA</td>
<td>5.840311e+05</td>
<td>599943.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">OR.PA</td>
<td>3.043389e+05</td>
<td>219549.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ACA.PA</td>
<td>3.587703e+06</td>
<td>2110202.0</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>En plus de la génération des quantités, il est utile de connaitre la quantité qu’on peut liquider en une journée. En effet, la liquidité d’un actif n’est pas infinie, et il est important de savoir combien on peut vendre sans impacter le prix de manière significative. Pour cela, on utilise la notion de profondeur de marché.</p>
<p>La profondeur de marché est une quantité à partir de laquelle on ne peut plus vendre sans impacter le prix de façon considérable. On fait l’hypothèse que la profondeur de marché est de 20%. Cela signifie que l’on peut vendre 20% de la quantité sans impacter le prix de façon considérable. Au-delà, le prix est impacté. Cette profondeur est ce qui est observé en pratique dans les carnets d’ordre à tel point que l’AMF le recommande.</p>
<p>A l’aide de cette profondeur de marché, on peut déterminer la quantité liquidable en une journée de la manière suivante : <img src="https://latex.codecogs.com/png.latex?%0AADV%203M%20%5Ctimes%200.2%0A"></p>
<p>Le nombre de jours de liquidation peut être ainsi déterminé : <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BQuantit%C3%A9%20%C3%A0%20liquider%7D%7D%7B%5Ctext%7BQuantit%C3%A9%20liquidable%20en%20une%20journ%C3%A9e%7D%7D%0A"></p>
<p>Ce nombre de jours est généralement arrondi à l’entier supérieur et flooré à 1. Plus ce nombre est élevé, plus la liquidation sera longue, et plus le risque de marché sera élevé. En effet, plus la liquidation est longue, plus le risque de marché est élevé, car il y a plus de chances que le prix de l’actif évolue entre le moment où l’investisseur décide de liquider et le moment où la liquidation est terminée.</p>
<div id="de911882" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quantité liquidable en 1 jour</span></span>
<span id="cb7-2">market_depth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-3">ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ADV"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> market_depth)</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du nombre de jours de liquidation</span></span>
<span id="cb7-6">ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>]</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># floor to 1 and round</span></span>
<span id="cb7-9">ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(x)))</span>
<span id="cb7-10">ADV</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="6">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">ADV</th>
<th data-quarto-table-cell-role="th">Quantity</th>
<th data-quarto-table-cell-role="th">Quantity in 1day</th>
<th data-quarto-table-cell-role="th">Days of liquidation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>1.956711e+06</td>
<td>2044183.0</td>
<td>391342.0</td>
<td>5</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>2.101232e+06</td>
<td>901868.0</td>
<td>420246.0</td>
<td>2</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>2.370661e+05</td>
<td>80668.0</td>
<td>47413.0</td>
<td>2</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>4.132509e+06</td>
<td>3417470.0</td>
<td>826502.0</td>
<td>4</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>4.504017e+05</td>
<td>486075.0</td>
<td>90080.0</td>
<td>5</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CA.PA</td>
<td>2.196726e+06</td>
<td>1394173.0</td>
<td>439345.0</td>
<td>3</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">ORA.PA</td>
<td>3.985574e+06</td>
<td>5863363.0</td>
<td>797115.0</td>
<td>7</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">AC.PA</td>
<td>5.840311e+05</td>
<td>599943.0</td>
<td>116806.0</td>
<td>5</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">OR.PA</td>
<td>3.043389e+05</td>
<td>219549.0</td>
<td>60868.0</td>
<td>4</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ACA.PA</td>
<td>3.587703e+06</td>
<td>2110202.0</td>
<td>717541.0</td>
<td>3</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="192e4eb1" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Days of liquidation'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb8-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Temps de liquidation du portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>time_elapsed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> jours"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille : 7 jours</code></pre>
</div>
</div>
<p>En calcucant le nombre de jours de liquidation pour chaque actif, on constate que les actifs qui se liquide le plus vite sont les actifs Thales et Société Générale. L’actif qui quant à lui prend le plus de temps à se liquider est l’actif Orange. Cela peut sembler paradoxal, car Orange est l’actif le plus liquide,compte tenu de son ADV. Cependant, cela s’explique par le fait que la quantité à liquider est dépendante de l’ADV, et est de ce fait très importante, et que la profondeur de marché est faible. En effet, même si l’ADV est élevé, la profondeur de marché est faible, ce qui rend la liquidation plus difficile. Un actif avec un petit ADV peut se liquider plus vite simplement parce qu’on lui aura affecté d’un petit coefficient aléatoire, dans la génération des quantités initiales.</p>
<blockquote class="blockquote">
<p>💡 La liquidité d’un portefeuille dépend de la liquidité intrinsèque des titres et la quantité de titres.</p>
</blockquote>
</section>
<section id="présence-de-déformation" class="level2">
<h2 class="anchored" data-anchor-id="présence-de-déformation">Présence de déformation</h2>
<p>La déformation dans le cadre de la liquidation d’un portefeuille d’actifs fait référence à la manière dont les quantités liquidées sont réparties entre les différents actifs du portefeuille. En effet, lorsque l’on liquide un portefeuille, on ne peut pas simplement vendre tous les actifs en même temps, compte tenu de leur liquidité respective. La déformation se produit lorsque l’on liquide d’abord les actifs les plus liquides, ce qui entraîne une concentration d’actifs illiquides dans le portefeuille restant. Cela peut avoir un impact significatif sur la valeur du portefeuille et sur le risque de liquidité.</p>
<section id="sous-conditions-normales-avec-déformation-waterfall-liquidation" class="level3">
<h3 class="anchored" data-anchor-id="sous-conditions-normales-avec-déformation-waterfall-liquidation">Sous conditions normales avec déformation (waterfall liquidation)</h3>
<p>Pour connaître la quantité d’actifs liquidés chaque jour, on fait l’hypothèse que les liquidations futures se feront aux prix observés aujourd’hui. En pratique, ce que l’on peut réellement liquider en une journée correspond à la quantité que l’on peut vendre sans impacter le prix, c’est-à-dire min(quantité liquidable en 1 jour, quantité restant dans le portefeuille).</p>
<p>Sur cette base, on peut calculer la valeur de marché du portefeuille, aussi bien au moment initial qu’à chaque jour de liquidation. Cette valeur est généralement exprimée en pourcentage de l’encours total.</p>
<p>On peut ensuite calculer le cumul des pourcentages liquidés au fil des jours. Cette courbe cumulative représente ce que l’on appelle le profil d’écoulement du portefeuille.</p>
<p>Dans le code ci-dessous, nous présentons les quantités liquidées par jour pour chaque actif.</p>
<blockquote class="blockquote">
<p>💡 Par convention, le jour 0 correspond à la quantité initialement détenue dans le portefeuille, avant toute opération de liquidation.</p>
</blockquote>
<div id="990d2342" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Liquidation du portefeuille</span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb10-4"></span>
<span id="cb10-5">ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialement, rien n'est liquidé</span></span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Au jour 0, on a liquidé 0. La colonne 0 sert de quantité initiale</span></span>
<span id="cb10-8">quantity_liquidated_per_day <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]]</span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> : <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tant qu'il y a des liquidations à faire</span></span>
<span id="cb10-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer la quantité liquide au jour i</span></span>
<span id="cb10-12">    liquidated_today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.minimum(ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>], ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>])</span>
<span id="cb10-13">    </span>
<span id="cb10-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mettre à jour les quantités liquidées dans le DataFrame</span></span>
<span id="cb10-15">    ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> liquidated_today</span>
<span id="cb10-16">    </span>
<span id="cb10-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stocker les quantités liquidées ce jour dans une liste</span></span>
<span id="cb10-18">    quantity_liquidated_per_day.append(liquidated_today)</span>
<span id="cb10-19"></span>
<span id="cb10-20">liquidation_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(quantity_liquidated_per_day).T</span>
<span id="cb10-21">liquidation_df.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(quantity_liquidated_per_day))]</span>
<span id="cb10-22"></span>
<span id="cb10-23">liquidation_df</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">0</th>
<th data-quarto-table-cell-role="th">1</th>
<th data-quarto-table-cell-role="th">2</th>
<th data-quarto-table-cell-role="th">3</th>
<th data-quarto-table-cell-role="th">4</th>
<th data-quarto-table-cell-role="th">5</th>
<th data-quarto-table-cell-role="th">6</th>
<th data-quarto-table-cell-role="th">7</th>
<th data-quarto-table-cell-role="th">8</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>2044183.0</td>
<td>391342.0</td>
<td>391342.0</td>
<td>391342.0</td>
<td>391342.0</td>
<td>391342.0</td>
<td>87473.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>901868.0</td>
<td>420246.0</td>
<td>420246.0</td>
<td>61376.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>80668.0</td>
<td>47413.0</td>
<td>33255.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>3417470.0</td>
<td>826502.0</td>
<td>826502.0</td>
<td>826502.0</td>
<td>826502.0</td>
<td>111462.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>486075.0</td>
<td>90080.0</td>
<td>90080.0</td>
<td>90080.0</td>
<td>90080.0</td>
<td>90080.0</td>
<td>35675.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CA.PA</td>
<td>1394173.0</td>
<td>439345.0</td>
<td>439345.0</td>
<td>439345.0</td>
<td>76138.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">ORA.PA</td>
<td>5863363.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>797115.0</td>
<td>283558.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">AC.PA</td>
<td>599943.0</td>
<td>116806.0</td>
<td>116806.0</td>
<td>116806.0</td>
<td>116806.0</td>
<td>116806.0</td>
<td>15913.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">OR.PA</td>
<td>219549.0</td>
<td>60868.0</td>
<td>60868.0</td>
<td>60868.0</td>
<td>36945.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ACA.PA</td>
<td>2110202.0</td>
<td>717541.0</td>
<td>717541.0</td>
<td>675120.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
<td>0.0</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="059e12c7" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now()</span>
<span id="cb11-2">start_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> end_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb11-3">price_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_data(start_date, end_date, index, assets_ticker, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>)</span>
<span id="cb11-4"></span>
<span id="cb11-5">price_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>].head()</span>
<span id="cb11-6">price_dict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> price_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"portfolio_data"</span>].iloc[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].to_dict()</span>
<span id="cb11-7"></span>
<span id="cb11-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb11-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prix des actifs à la date du jour"</span>)</span>
<span id="cb11-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb11-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker, price <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> price_dict.items():</span>
<span id="cb11-12">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Le prix de l'actif </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>selected_assets[ticker]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> est de </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> €"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>[                       0%                       ][**********            20%                       ]  2 of 10 completed[**************        30%                       ]  3 of 10 completed[**************        30%                       ]  3 of 10 completed[**********************50%                       ]  5 of 10 completed[**********************60%****                   ]  6 of 10 completed[**********************60%****                   ]  6 of 10 completed[**********************80%*************          ]  8 of 10 completed[**********************90%******************     ]  9 of 10 completed[*********************100%***********************]  10 of 10 completed
[*********************100%***********************]  1 of 1 completed</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>==================================================
Prix des actifs à la date du jour
==================================================
Le prix de l'actif Accor est de 49.15999984741211 €
Le prix de l'actif Crédit agricole est de 18.040000915527344 €
Le prix de l'actif Carrefour est de 14.880000114440918 €
Le prix de l'actif Capgemini est de 113.5999984741211 €
Le prix de l'actif Engie est de 26.09000015258789 €
Le prix de l'actif Société générale est de 73.41999816894531 €
Le prix de l'actif Thales est de 252.5 €
Le prix de l'actif L'Oreal est de 391.3999938964844 €
Le prix de l'actif Orange est de 16.459999084472656 €
Le prix de l'actif Sanofi est de 82.19999694824219 €</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
</div>
<div id="929fb98a" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur liquide des actions par jour de liquidation</span></span>
<span id="cb15-2">market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb15-3">    price_dict[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> liquidation_df.loc[ticker]</span>
<span id="cb15-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> selected_assets</span>
<span id="cb15-5">]</span>
<span id="cb15-6"></span>
<span id="cb15-7">market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(market_value, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>liquidation_df.columns)</span>
<span id="cb15-8">market_value</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">0</th>
<th data-quarto-table-cell-role="th">1</th>
<th data-quarto-table-cell-role="th">2</th>
<th data-quarto-table-cell-role="th">3</th>
<th data-quarto-table-cell-role="th">4</th>
<th data-quarto-table-cell-role="th">5</th>
<th data-quarto-table-cell-role="th">6</th>
<th data-quarto-table-cell-role="th">7</th>
<th data-quarto-table-cell-role="th">8</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>1.680318e+08</td>
<td>3.216831e+07</td>
<td>3.216831e+07</td>
<td>3.216831e+07</td>
<td>3.216831e+07</td>
<td>3.216831e+07</td>
<td>7.190280e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>6.621515e+07</td>
<td>3.085446e+07</td>
<td>3.085446e+07</td>
<td>4.506226e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>2.036867e+07</td>
<td>1.197178e+07</td>
<td>8.396888e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>8.916179e+07</td>
<td>2.156344e+07</td>
<td>2.156344e+07</td>
<td>2.156344e+07</td>
<td>2.156344e+07</td>
<td>2.908044e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>5.521812e+07</td>
<td>1.023309e+07</td>
<td>1.023309e+07</td>
<td>1.023309e+07</td>
<td>1.023309e+07</td>
<td>1.023309e+07</td>
<td>4.052680e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">CA.PA</td>
<td>2.074529e+07</td>
<td>6.537454e+06</td>
<td>6.537454e+06</td>
<td>6.537454e+06</td>
<td>1.132933e+06</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">ORA.PA</td>
<td>9.651095e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>1.312051e+07</td>
<td>4.667364e+06</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">AC.PA</td>
<td>2.949320e+07</td>
<td>5.742183e+06</td>
<td>5.742183e+06</td>
<td>5.742183e+06</td>
<td>5.742183e+06</td>
<td>5.742183e+06</td>
<td>7.822831e+05</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">OR.PA</td>
<td>8.593148e+07</td>
<td>2.382373e+07</td>
<td>2.382373e+07</td>
<td>2.382373e+07</td>
<td>1.446027e+07</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ACA.PA</td>
<td>3.806805e+07</td>
<td>1.294444e+07</td>
<td>1.294444e+07</td>
<td>1.217917e+07</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
<td>0.000000e+00</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="c0d795c9" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur de marché initiale et totale</span></span>
<span id="cb16-2">market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb16-3">total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_0.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb16-4"></span>
<span id="cb16-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f" La valeur de marché initiale est de </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_market_value_0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">€"</span>)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur de marché cumulée (à partir de la colonne 1)</span></span>
<span id="cb16-8">cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].cumsum(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-9">cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).cumsum()</span>
<span id="cb16-10">cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.DataFrame(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>market_value.index, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_market_value], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-11">cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.Series(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_total_market_value])</span>
<span id="cb16-12"></span>
<span id="cb16-13">weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb16-14"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> assets_ticker :</span>
<span id="cb16-15">    weights[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (market_value_0.loc[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_market_value.loc[ticker]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_total_market_value)</span>
<span id="cb16-16"></span>
<span id="cb16-17">weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(weights).T</span>
<span id="cb16-18">weights.head()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> La valeur de marché initiale est de 669744530.421916€</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="11">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">0</th>
<th data-quarto-table-cell-role="th">1</th>
<th data-quarto-table-cell-role="th">2</th>
<th data-quarto-table-cell-role="th">3</th>
<th data-quarto-table-cell-role="th">4</th>
<th data-quarto-table-cell-role="th">5</th>
<th data-quarto-table-cell-role="th">6</th>
<th data-quarto-table-cell-role="th">7</th>
<th data-quarto-table-cell-role="th">8</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">SAN.PA</td>
<td>0.250889</td>
<td>0.271301</td>
<td>0.309168</td>
<td>0.348018</td>
<td>0.367474</td>
<td>0.167474</td>
<td>0.0</td>
<td>0.0</td>
<td>NaN</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">GLE.PA</td>
<td>0.098866</td>
<td>0.070610</td>
<td>0.013435</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.0</td>
<td>NaN</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">HO.PA</td>
<td>0.030413</td>
<td>0.016767</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.0</td>
<td>NaN</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">ENGI.PA</td>
<td>0.133128</td>
<td>0.134985</td>
<td>0.137254</td>
<td>0.119067</td>
<td>0.027151</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.0</td>
<td>NaN</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">CAP.PA</td>
<td>0.082447</td>
<td>0.089829</td>
<td>0.103613</td>
<td>0.119298</td>
<td>0.133380</td>
<td>0.094394</td>
<td>0.0</td>
<td>0.0</td>
<td>NaN</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="972ab2c1" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barplot empilé</span></span>
<span id="cb18-4">bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb18-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> asset <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> weights.index:</span>
<span id="cb18-6">    plt.bar(</span>
<span id="cb18-7">        pd.to_numeric(weights.columns),  </span>
<span id="cb18-8">        weights.loc[asset],  </span>
<span id="cb18-9">        bottom<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bottom,  </span>
<span id="cb18-10">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets[asset]  </span>
<span id="cb18-11">    )</span>
<span id="cb18-12">    bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weights.loc[asset] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> bottom <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> weights.loc[asset]</span>
<span id="cb18-13"></span>
<span id="cb18-14">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of Liquidation"</span>)</span>
<span id="cb18-15">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Portfolio Weights"</span>)</span>
<span id="cb18-16">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Déformation du portefeuille"</span>)</span>
<span id="cb18-17">plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb18-18">plt.legend(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Assets"</span>, bbox_to_anchor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper left"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, ncol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-19">plt.tight_layout()</span>
<span id="cb18-20"></span>
<span id="cb18-21">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Nous constatons avec ce profile de liquidation que les actifs les plus liquides sont liquidés en premier, ce qui entraîne une concentration d’actifs illiquides dans le portefeuille restant. Cela peut avoir un impact significatif sur la valeur du portefeuille et sur le risque de liquidité. En effet, à la fin de la période de liquidation, le portefeuille est uniquement constitué de l’actif Orange, qui nous avons vu précédemment est l’actif le plus illiquide.</p>
<blockquote class="blockquote">
💡 Bon à savoir : Pour un fonds de droit français réglementé, il est interdit d’investir plus de 5 % du portefeuille dans un seul émetteur. Cependant, à titre exceptionnel, il est possible d’investir jusqu’à 10 % dans certains titres, à condition que la somme des expositions supérieures à 5 % ne dépasse pas 40 % du portefeuille. C’est ce que l’on appelle la règle des 5/10/40, un ratio réglementaire applicable aux OPC (organismes de placement collectif). Toutes les pertes liées à un non-respect de ce ratio doivent être supportées par la société de gestion. De plus, tout dépassement doit être déclaré à l’AMF.
</blockquote>
<div id="bef455f5" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valeur liquide du portefeuille</span></span>
<span id="cb19-2">market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame()</span>
<span id="cb19-3"></span>
<span id="cb19-4">market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb19-5"></span>
<span id="cb19-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer la valeur liquide relative par rapport au jour 0</span></span>
<span id="cb19-7">market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>].iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb19-8"></span>
<span id="cb19-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculer la valeur cumulée liquide relative du portefeuille</span></span>
<span id="cb19-10">market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>].cumsum() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb19-11"></span>
<span id="cb19-12">market_value_df</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">market_value</th>
<th data-quarto-table-cell-role="th">relative value</th>
<th data-quarto-table-cell-role="th">cumulative value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>6.697445e+08</td>
<td>1.000000</td>
<td>0.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>1.689594e+08</td>
<td>0.252274</td>
<td>0.252274</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>1.653845e+08</td>
<td>0.246937</td>
<td>0.499211</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>1.298741e+08</td>
<td>0.193916</td>
<td>0.693127</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>9.842074e+07</td>
<td>0.146953</td>
<td>0.840080</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">5</td>
<td>6.417214e+07</td>
<td>0.095816</td>
<td>0.935896</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">6</td>
<td>2.514576e+07</td>
<td>0.037545</td>
<td>0.973441</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">7</td>
<td>1.312051e+07</td>
<td>0.019590</td>
<td>0.993031</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">8</td>
<td>4.667364e+06</td>
<td>0.006969</td>
<td>1.000000</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div id="4a26781b" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df.iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]</span>
<span id="cb20-2"></span>
<span id="cb20-3">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb20-4">bars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.bar(market_value_df.index, market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>)</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bar <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bars:</span>
<span id="cb20-7">    height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bar.get_height()</span>
<span id="cb20-8">    plt.text(</span>
<span id="cb20-9">        bar.get_x() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bar.get_width() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center text</span></span>
<span id="cb20-10">        height,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position slightly above the bar</span></span>
<span id="cb20-11">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format with 2 decimal places</span></span>
<span id="cb20-12">        ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'center'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center horizontally</span></span>
<span id="cb20-13">        va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bottom'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position text at the bottom</span></span>
<span id="cb20-14">        fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span></span>
<span id="cb20-15">    )</span>
<span id="cb20-16"></span>
<span id="cb20-17">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days"</span>)</span>
<span id="cb20-18">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cumulative Value (%)"</span>)</span>
<span id="cb20-19">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profil de liquidation du portefeuille"</span>)</span>
<span id="cb20-20">plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb20-21"></span>
<span id="cb20-22">plt.show()</span></code></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-15-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Pour voir ce qui arrive au profil d’écoulement lorsque les quantités varient, on va utiliser un facteur de modulation de la quantité. Cela permet de déterminer quelle est la taille cible du portefeuille qui permet d’avoir la liquidité pour un certain niveau en nombre de jours qu’on se fixe. Cet exercice est fait une seule fois à l’initialisation du portefeuille.</p>
<p>Pour facilier l’implémentation, nous allons utiliser une fonction <code>liquidation_profile</code> qui va nous permettre de calculer le profil d’écoulement du portefeuille, en intégrant le facteur de modulation. Lorsqu’on ne souhaite pas de modulation, on peut simplement passer un facteur de modulation de 1.</p>
<div id="e1d99b5a" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> waterfall_liquidation(ADV, price_dict, selected_assets, fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb21-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcule le profil de liquidation et visualise les graphiques des poids et des valeurs cumulées.</span></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb21-5">    </span>
<span id="cb21-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialisation des quantités liquidées</span></span>
<span id="cb21-7">    ADV <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV.copy()</span>
<span id="cb21-8">    ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> fact_modulation)</span>
<span id="cb21-9"></span>
<span id="cb21-10">    ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb21-11">    quantity_liquidated_per_day <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]]</span>
<span id="cb21-12">    </span>
<span id="cb21-13">    time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> </span>
<span id="cb21-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des quantités liquidées par jour</span></span>
<span id="cb21-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> :</span>
<span id="cb21-16">        liquidated_today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.minimum(</span>
<span id="cb21-17">            ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>], </span>
<span id="cb21-18">            ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>]</span>
<span id="cb21-19">        )</span>
<span id="cb21-20">        ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> liquidated_today</span>
<span id="cb21-21">        quantity_liquidated_per_day.append(liquidated_today)</span>
<span id="cb21-22">        time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-23">    </span>
<span id="cb21-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Temps de liquidation du portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>time_elapsed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> jours"</span>)</span>
<span id="cb21-25">    </span>
<span id="cb21-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conversion des résultats en DataFrame</span></span>
<span id="cb21-27">    liquidation_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(quantity_liquidated_per_day).T</span>
<span id="cb21-28">    liquidation_df.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(quantity_liquidated_per_day))]</span>
<span id="cb21-29">    </span>
<span id="cb21-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur liquide par actif et par jour</span></span>
<span id="cb21-31">    market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb21-32">        price_dict[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> liquidation_df.loc[ticker]</span>
<span id="cb21-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> selected_assets</span>
<span id="cb21-34">    ]</span>
<span id="cb21-35">    market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(market_value, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>liquidation_df.columns)</span>
<span id="cb21-36">    </span>
<span id="cb21-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb21-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des poids par jour #</span></span>
<span id="cb21-39">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb21-40"></span>
<span id="cb21-41">    market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb21-42">    total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_0.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb21-43"></span>
<span id="cb21-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur de marché cumulée (à partir de la colonne 1)</span></span>
<span id="cb21-45">    cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].cumsum(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-46">    cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).cumsum()</span>
<span id="cb21-47">    cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.DataFrame(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>market_value.index, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_market_value], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb21-48">    cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.Series(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_total_market_value])</span>
<span id="cb21-49"></span>
<span id="cb21-50">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb21-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> selected_assets :</span>
<span id="cb21-52">        weights[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (market_value_0.loc[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_market_value.loc[ticker]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_total_market_value)</span>
<span id="cb21-53"></span>
<span id="cb21-54">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(weights).T</span>
<span id="cb21-55">    </span>
<span id="cb21-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualisation des poids</span></span>
<span id="cb21-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> plot_graphs:</span>
<span id="cb21-58">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialiser le graphique</span></span>
<span id="cb21-59">        plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb21-60">        </span>
<span id="cb21-61"></span>
<span id="cb21-62">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barplot empilé</span></span>
<span id="cb21-63">        bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb21-64">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> asset <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> weights.index:</span>
<span id="cb21-65">            plt.bar(</span>
<span id="cb21-66">                pd.to_numeric(weights.columns), </span>
<span id="cb21-67">                weights.loc[asset],  </span>
<span id="cb21-68">                bottom<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bottom, </span>
<span id="cb21-69">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets[asset]  </span>
<span id="cb21-70">            )</span>
<span id="cb21-71">            bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weights.loc[asset] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> bottom <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> weights.loc[asset]</span>
<span id="cb21-72"></span>
<span id="cb21-73">        plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of Liquidation"</span>)</span>
<span id="cb21-74">        plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Portfolio Weights"</span>)</span>
<span id="cb21-75">        plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Déformation du portefeuille"</span>)</span>
<span id="cb21-76">        plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb21-77">        plt.legend(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Assets"</span>, bbox_to_anchor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper left"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, ncol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb21-78">        plt.tight_layout()</span>
<span id="cb21-79"></span>
<span id="cb21-80">        plt.show()</span>
<span id="cb21-81"></span>
<span id="cb21-82">    </span>
<span id="cb21-83">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb21-84">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   Profil de liquidation   #</span></span>
<span id="cb21-85">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb21-86"></span>
<span id="cb21-87">    market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame()</span>
<span id="cb21-88">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb21-89">    </span>
<span id="cb21-90">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des valeurs relatives et cumulées</span></span>
<span id="cb21-91">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>].iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb21-92">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>].cumsum() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-93">    market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df.iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retirer le jour 0 pour l'analyse cumulée</span></span>
<span id="cb21-94">    </span>
<span id="cb21-95">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualisation de la valeur cumulative (barplot)</span></span>
<span id="cb21-96">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> plot_graphs:</span>
<span id="cb21-97">        plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb21-98">        bars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.bar(market_value_df.index, market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>)</span>
<span id="cb21-99">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bar <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bars:</span>
<span id="cb21-100">            height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bar.get_height()</span>
<span id="cb21-101">            plt.text(</span>
<span id="cb21-102">                bar.get_x() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bar.get_width() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center text</span></span>
<span id="cb21-103">                height,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position slightly above the bar</span></span>
<span id="cb21-104">                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format with 2 decimal places</span></span>
<span id="cb21-105">                ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'center'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center horizontally</span></span>
<span id="cb21-106">                va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bottom'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position text at the bottom</span></span>
<span id="cb21-107">                fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span></span>
<span id="cb21-108">            )</span>
<span id="cb21-109">        plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days"</span>)</span>
<span id="cb21-110">        plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cumulative Value (%)"</span>)</span>
<span id="cb21-111">        plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profil de liquidation du portefeuille"</span>)</span>
<span id="cb21-112">        plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb21-113">        plt.show()</span>
<span id="cb21-114"></span>
<span id="cb21-115">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> market_value_df, market_value, weights</span></code></pre></div>
</div>
<p>Nous constatons qu’en utilisant un facteur de modulation de 0.3, le temps de liquidation est réduit à 3 jours, ce qui est plus rapide que le temps de liquidation initial de 8 jours. Cela montre l’impact significatif de la modulation sur le profil de liquidation du portefeuille.</p>
<div id="29484170" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb22-2">new_market_value_df, new_market_value, new_weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> waterfall_liquidation(ADV, price_dict, selected_assets, fact_modulation, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille : 3 jours</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-17-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-17-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="sous-conditions-stressées-avec-déformation" class="level3">
<h3 class="anchored" data-anchor-id="sous-conditions-stressées-avec-déformation">Sous conditions stressées avec déformation</h3>
<p>Pour stresser le portefeuille, à la baisse, on va diviser la profondeur de marché par 2. Cela signifie que l’on peut vendre moins d’actifs sans impacter le prix de manière significative. En conséquence, le temps de liquidation augmente, car il faut plus de temps pour liquider la même quantité d’actifs. Il est également possible d’avoir des conditions stressées à la hausse, où l’on multiplie la profondeur de marché par 2. Cela signifie que l’on peut vendre plus d’actifs sans impacter le prix de manière significative. En conséquence, le temps de liquidation diminue, car il faut moins de temps pour liquider la même quantité d’actifs.</p>
<p>Dans notre cas, en stressant le portefeuille à la baisse, nous constatons que le temps de liquidation augmente. On passe de 8 jours de liquidatin à 15 jours.</p>
<div id="a0269a81" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb24-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stress Test</span></span>
<span id="cb24-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb24-4">ADV_stressed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV.copy()</span>
<span id="cb24-5"></span>
<span id="cb24-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quantité journalière</span></span>
<span id="cb24-7">market_depth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On stresse la liquidité à la baisse</span></span>
<span id="cb24-8">ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ADV"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> market_depth)</span>
<span id="cb24-9"></span>
<span id="cb24-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul du nombre de jours de liquidation</span></span>
<span id="cb24-11">ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>]</span>
<span id="cb24-12"></span>
<span id="cb24-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># floor to 1 and round</span></span>
<span id="cb24-14">ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of liquidation"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(x)))</span>
<span id="cb24-15"></span>
<span id="cb24-16">time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV_stressed[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Days of liquidation'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb24-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Temps de liquidation du portefeuille stressé: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>time_elapsed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> jours"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille stressé: 15 jours</code></pre>
</div>
</div>
<div id="5edb266f" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1">stressed_market_value_df, stressed_market_value, stressed_weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> waterfall_liquidation(ADV<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ADV_stressed, price_dict<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>price_dict, selected_assets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille : 15 jours</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-19-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-19-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="absence-de-déformation-du-portefeuille-pro-forma" class="level2">
<h2 class="anchored" data-anchor-id="absence-de-déformation-du-portefeuille-pro-forma">Absence de déformation du portefeuille (pro forma)</h2>
<p>Nous avons vu dans la section précédente que, lors de la liquidation d’un portefeuille, on a tendance à commencer par les actifs les plus liquides. Cela conduit à une concentration progressive d’actifs illiquides dans le portefeuille résiduel, ce qui peut affecter significativement sa valeur et accroître le risque de liquidité.</p>
<p>Dans cette section, nous allons étudier comment éviter cette déformation en procédant à une liquidation proportionnelle : l’objectif est de préserver la répartition initiale du portefeuille tout au long du processus de liquidation.</p>
<p>Pour cela, on commence par estimer, comme précédemment, la quantité liquidable en un jour pour chaque titre. Cette estimation permet d’en déduire le pourcentage liquidable quotidien par rapport à la position totale sur chaque actif. Si l’on souhaite que tous les titres soient liquidés à la même vitesse, il faut adopter le rythme de liquidation de l’actif le plus lent. Ainsi, on calcule le pourcentage liquidable en un jour pour chaque titre, puis on en retient le minimum. Ce minimum définit alors le pourcentage quotidien de liquidation appliqué à l’ensemble du portefeuille. Ce procédé allonge la durée totale de liquidation, mais il permet de conserver une structure de portefeuille stable. Néanmoins, une légère déformation peut subsister, notamment en raison des arrondis et des limites pratiques de liquidité sur certains actifs.</p>
<div id="3581d48b" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb28-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb28-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb28-4"></span>
<span id="cb28-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pro_forma_liquidation(ADV, price_dict, selected_assets, fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb28-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb28-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calcule le profil de liquidation pro forma et visualise les graphiques des poids et des valeurs cumulées.</span></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb28-9"></span>
<span id="cb28-10">    ADV <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV.copy()</span>
<span id="cb28-11">    ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> fact_modulation</span>
<span id="cb28-12"></span>
<span id="cb28-13">    time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Days of liquidation'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb28-14"></span>
<span id="cb28-15">    ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb28-16">    quantity_liquidated_per_day <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]]</span>
<span id="cb28-17">    </span>
<span id="cb28-18">    min_liquidated_today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.ones(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]))</span>
<span id="cb28-19">    time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> </span>
<span id="cb28-20"></span>
<span id="cb28-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des quantités liquidées par jour</span></span>
<span id="cb28-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> min_liquidated_today.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:  </span>
<span id="cb28-23"></span>
<span id="cb28-24">        liquidated_today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.minimum(</span>
<span id="cb28-25">            ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity in 1day"</span>], </span>
<span id="cb28-26">            ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>]</span>
<span id="cb28-27">        ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>]</span>
<span id="cb28-28">        min_liquidated_today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(liquidated_today) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>])<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On liquide à la vitesse de l'actif le moins liquide</span></span>
<span id="cb28-29">        ADV[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity liquidated"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> min_liquidated_today</span>
<span id="cb28-30">        quantity_liquidated_per_day.append(min_liquidated_today)</span>
<span id="cb28-31">        time_elapsed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb28-32">    </span>
<span id="cb28-33">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Temps de liquidation du portefeuille : </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>time_elapsed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> jours"</span>)</span>
<span id="cb28-34">    </span>
<span id="cb28-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conversion des résultats en DataFrame</span></span>
<span id="cb28-36">    liquidation_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(quantity_liquidated_per_day).T</span>
<span id="cb28-37">    liquidation_df.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(quantity_liquidated_per_day))]</span>
<span id="cb28-38">    </span>
<span id="cb28-39">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur liquide par actif et par jour</span></span>
<span id="cb28-40">    market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb28-41">        price_dict[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> liquidation_df.loc[ticker]</span>
<span id="cb28-42">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> selected_assets</span>
<span id="cb28-43">    ]</span>
<span id="cb28-44">    market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(market_value, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>liquidation_df.columns)</span>
<span id="cb28-45">    </span>
<span id="cb28-46">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb28-47">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des poids par jour</span></span>
<span id="cb28-48">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb28-49"></span>
<span id="cb28-50">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur de marché initiale et totale</span></span>
<span id="cb28-51">    market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb28-52">    total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_0.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb28-53"></span>
<span id="cb28-54">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul de la valeur de marché cumulée (à partir de la colonne 1)</span></span>
<span id="cb28-55">    cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].cumsum(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb28-56">    cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.iloc[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).cumsum()</span>
<span id="cb28-57">    cumsum_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.DataFrame(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>market_value.index, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_market_value], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb28-58">    cumsum_total_market_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.Series(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), cumsum_total_market_value])</span>
<span id="cb28-59">    </span>
<span id="cb28-60">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb28-61">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ticker <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> selected_assets :</span>
<span id="cb28-62">        weights[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (market_value_0.loc[ticker] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_market_value.loc[ticker]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (total_market_value_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cumsum_total_market_value)</span>
<span id="cb28-63"></span>
<span id="cb28-64">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(weights).T</span>
<span id="cb28-65"></span>
<span id="cb28-66">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> plot_graphs:</span>
<span id="cb28-67">        plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb28-68"></span>
<span id="cb28-69">        bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb28-70">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> asset <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> weights.index:</span>
<span id="cb28-71">            plt.bar(</span>
<span id="cb28-72">                pd.to_numeric(weights.columns), </span>
<span id="cb28-73">                weights.loc[asset],  </span>
<span id="cb28-74">                bottom<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bottom, </span>
<span id="cb28-75">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets[asset]  </span>
<span id="cb28-76">            )</span>
<span id="cb28-77">            bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weights.loc[asset] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> bottom <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> bottom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> weights.loc[asset]</span>
<span id="cb28-78"></span>
<span id="cb28-79">        plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days of Liquidation"</span>)</span>
<span id="cb28-80">        plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Portfolio Weights"</span>)</span>
<span id="cb28-81">        plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Déformation du portefeuille"</span>)</span>
<span id="cb28-82">        plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb28-83">        plt.legend(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Assets"</span>, bbox_to_anchor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper left"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, ncol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb28-84">        plt.tight_layout()</span>
<span id="cb28-85">        plt.show()</span>
<span id="cb28-86"></span>
<span id="cb28-87">    </span>
<span id="cb28-88">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb28-89">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   Profil de liquidation   #</span></span>
<span id="cb28-90">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#---------------------------#</span></span>
<span id="cb28-91">    </span>
<span id="cb28-92">    market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame()</span>
<span id="cb28-93">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb28-94">    </span>
<span id="cb28-95">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calcul des valeurs relatives et cumulées</span></span>
<span id="cb28-96">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"market_value"</span>].iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb28-97">    market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative value"</span>].cumsum() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb28-98">    market_value_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> market_value_df.iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retirer le jour 0 pour l'analyse cumulée</span></span>
<span id="cb28-99">    </span>
<span id="cb28-100">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualisation de la valeur cumulative (barplot)</span></span>
<span id="cb28-101">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> plot_graphs:</span>
<span id="cb28-102">        plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb28-103">        bars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.bar(market_value_df.index, market_value_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cumulative value"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>)</span>
<span id="cb28-104">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bar <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bars:</span>
<span id="cb28-105">            height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bar.get_height()</span>
<span id="cb28-106">            plt.text(</span>
<span id="cb28-107">                bar.get_x() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bar.get_width() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center text</span></span>
<span id="cb28-108">                height,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position slightly above the bar</span></span>
<span id="cb28-109">                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format with 2 decimal places</span></span>
<span id="cb28-110">                ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'center'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Center horizontally</span></span>
<span id="cb28-111">                va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bottom'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Position text at the bottom</span></span>
<span id="cb28-112">                fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span></span>
<span id="cb28-113">            )</span>
<span id="cb28-114">        plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days"</span>)</span>
<span id="cb28-115">        plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cumulative Value (%)"</span>)</span>
<span id="cb28-116">        plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profil de liquidation du portefeuille"</span>)</span>
<span id="cb28-117">        plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span>
<span id="cb28-118">        plt.show()</span>
<span id="cb28-119">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> market_value_df, market_value, weights</span></code></pre></div>
</div>
<section id="sous-condition-normale-sans-déformation" class="level3">
<h3 class="anchored" data-anchor-id="sous-condition-normale-sans-déformation">Sous condition normale sans déformation</h3>
<div id="310b4371" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">proforma_market_value_df, proforma_market_value, proforma_weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pro_forma_liquidation(ADV<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ADV, price_dict<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>price_dict, selected_assets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille : 9 jours</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-21-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-21-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="sous-conditions-stressées-sans-déformation" class="level3">
<h3 class="anchored" data-anchor-id="sous-conditions-stressées-sans-déformation">Sous conditions stressées sans déformation</h3>
<div id="e83d383e" class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">proforma_s_market_value_df, proforma_s_market_value, proforma_s_weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pro_forma_liquidation(ADV<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ADV_stressed, price_dict<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>price_dict, selected_assets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>selected_assets, fact_modulation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, plot_graphs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Temps de liquidation du portefeuille : 16 jours</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-22-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2_files/figure-html/cell-22-output-3.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>La liquidation d’un portefeuille est un exercice complexe qui nécessite de prendre en compte plusieurs facteurs : la liquidité des actifs, la taille du portefeuille, et les conditions de marché. En utilisant des outils tels que le profil d’écoulement (ou profil de liquidation), les investisseurs peuvent mieux anticiper l’impact potentiel de leurs transactions sur les prix de marché, et ainsi minimiser l’effet de marché.</p>
<p>Le processus de liquidation choisi peut conduire à des rythmes de sortie différents, et il est essentiel d’en tenir compte dans la gestion active du portefeuille.</p>
<p>Pour préserver la liquidité d’un portefeuille, notamment en période de tension sur les marchés, plusieurs mécanismes réglementaires et opérationnels existent. Ils visent à protéger les investisseurs restants et à maintenir la qualité du portefeuille. En cas de crise financière ou de forte volatilité, les sociétés de gestion de portefeuille (SGP) peuvent activer ces dispositifs, conformément aux dispositions prévues par la réglementation.</p>
<p>Ces mesures permettent d’éviter des ventes forcées d’actifs et de limiter les effets de contagion sur le reste du portefeuille.</p>
<p><strong>Principaux mécanismes de gestion de la liquidité :</strong></p>
<ol type="1">
<li><p>Les gates (plafonnement de rachat)</p>
<p>Les gates permettent de limiter les rachats quotidiens à un certain pourcentage de l’actif net du fonds. En général, si les rachats dépassent 5 % de l’actif net, la SGP a le droit (mais non l’obligation) de n’honorer que les premiers 5 % et de reporter le reste sur les jours suivants, en fonction des conditions de marché.</p>
<p>Ce mécanisme permet de réduire l’impact sur les prix de marché, en évitant une vente massive d’actifs en un seul jour. C’est une mesure de protection des porteurs restants, bien que son activation soit souvent perçue comme un signal négatif.</p>
<p>L’existence du mécanisme des gates doit figurer dans le prospectus du fonds, sauf justification spécifique de la SGP. Il convient de noter que l’activation des gates est optionnelle.</p></li>
<li><p><strong>La suspension des souscriptions et des rachats</strong></p>
<p>Il s’agit d’une mesure plus radicale, qui consiste à geler temporairement les opérations d’entrée et de sortie du fonds. Elle est utilisée dans des situations exceptionnelles, telles que des crises de marché ou une volatilité extrême, lorsque la valorisation des actifs devient incertaine ou que la liquidité disparaît. Ce mécanisme vise à : préserver l’égalité de traitement entre les investisseurs, éviter des ventes précipitées d’actifs, stabiliser la structure du portefeuille. La suspension doit être justifiée et temporaire, et elle est levée dès que les conditions de marché se normalisent.</p></li>
<li><p><strong>Les mécanismes de déformation du portefeuille</strong></p>
<p>Il s’agit de mesures proactives consistant à adapter la composition du portefeuille lors des rachats, pour préserver la liquidité résiduelle. Concrètement, la SGP peut choisir de vendre en priorité les actifs les plus liquides, ce qui permet de répondre rapidement aux demandes de rachat sans impacter significativement les prix. Ces mécanismes sont particulièrement utilisés en cas de rachats massifs, pour : limiter l’impact de marché, protéger les investisseurs restants, éviter une déstabilisation du portefeuille.</p>
<p>Néanmoins, cela entraîne une déformation du portefeuille, c’est-à-dire que les investisseurs qui restent se retrouvent avec une part moins liquide du portefeuille initial. Ce compromis doit être géré avec prudence.</p></li>
</ol>


</section>

 ]]></description>
  <category>paper</category>
  <category>asset-management</category>
  <category>ensai-gdr</category>
  <guid>https://cheryl-kdio.github.io/posts/ensai/gestion_actifs/TP-2.html</guid>
  <pubDate>Sun, 26 Jan 2025 23:00:00 GMT</pubDate>
</item>
</channel>
</rss>
