<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Philben - Ms Access &#187; Excel</title>
	<atom:link href="https://blog.developpez.com/philben/ptag/excel/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/philben</link>
	<description></description>
	<lastBuildDate>Thu, 26 Sep 2013 19:43:53 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Approximer, en double précision, la loi normale gaussienne et son inverse</title>
		<link>https://blog.developpez.com/philben/p11198/vba-access/approximer_en_double_precision_la_loi_no</link>
		<comments>https://blog.developpez.com/philben/p11198/vba-access/approximer_en_double_precision_la_loi_no#comments</comments>
		<pubDate>Sat, 04 Aug 2012 08:38:53 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Loi normale]]></category>
		<category><![CDATA[Numérique]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vous propose deux fonctions en VBA pour Access qui estiment la loi normale (ou loi de Laplace-Gauss) et son inverse en double précision. Pour rappel, Excel propose en standard ces fonctions (NORMDIST, NORMINV, NORMSDIST, NORMSINV) et des fonctions encore &#8230; <a href="https://blog.developpez.com/philben/p11198/vba-access/approximer_en_double_precision_la_loi_no">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vous propose deux fonctions en VBA pour Access qui estiment la loi normale (ou loi de Laplace-Gauss) et son inverse en double précision. Pour rappel, Excel propose en standard ces fonctions (NORMDIST, NORMINV, NORMSDIST, NORMSINV) et des fonctions encore plus précises depuis Excel 2010 (NORM_DIST, NORM_INV, NORM_S_DIST, NORM_S_INV).</p>
<p><img src="http://philben.developpez.com/CourbeGauss.png" alt="Courbe de Gauss" title="Courbe de Gauss" /><br />
<span id="more-13"></span><br />
<strong>Loi normale</strong><br />
Cette fonction permet de trouver la probabilité cumulée (aire sous la courbe de la distribution gaussienne) d&rsquo;une variable aléatoire X.<br />
Pour ce faire, je me suis appuyé sur l&rsquo;un des meilleurs algorithmes (dixit G. Marsaglia) pour calculer la fonction d&rsquo;erreur complémentaire  qui permet d&rsquo;exprimer la fonction de répartition de la loi normale.<br />
Cet algorithme est dû à <a href="http://www.kurims.kyoto-u.ac.jp/~ooura/index.html"><strong>Takuya OOURA</strong> (1996)</a> que j&rsquo;ai implémenté en VBA :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> derfc(<span style="color: #151B8D; font-weight: bold;">ByVal</span> X <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
<span style="color: #008000;">'Complementary error function in double precision <br />
</span><span style="color: #008000;">'http://www.kurims.kyoto-u.ac.jp/~ooura/index.html <br />
</span><span style="color: #008000;">'Copyright(C) 1996 Takuya OOURA : You may use, copy, modify this code for any purpose and without fee. <br />
</span><span style="color: #008000;">'Implementation en VBA par Philben - v1.0 <br />
</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp;Const C0 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.27109764952614E-03 + 9.2E-19, C1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.1931402283834E-04 + 9.44E-19, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 3.96385097360513E-03 + 5E-18, C3 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 8.70779635317295E-04 + 8.28E-19, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C4 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 7.73672528313526E-03 + 6.68E-18, C5 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 3.83335126264887E-03 + 3.03E-18, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C6 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.27223813782122E-02 + 7.55E-17, C7 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.013382364453346 + 6.9E-18, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C8 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.61315329733252E-02 + 2.48E-17, C9 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 3.90976845588484E-02 + 3.5E-18, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C10 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 2.49367200053503E-03 + 3.04E-18 <br />
&nbsp; &nbsp;Const C11 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 8.38864557023001E-02 + 9.92E-17, C12 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.119463959964325 + 4.15E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C13 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.66207924969367E-02 + 3.56E-17, C14 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.357524274449531 + 4.3E-17, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C15 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.80527640875291 + 5.67E-16, C16 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.18902982909273 + 3.33E-15, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C17 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.37040217682338 + 1.67E-15, C18 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.31314653831023 + 9.8E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C19 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.07925515155856 + 6.77E-15, C20 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.774368199119538 + 6.09E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C21 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.490165080585318 + 4.24E-16, C22 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.275374741597376 + 7.82E-16 <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> t <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, u <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
&nbsp;<br />
&nbsp; &nbsp;t = 3.97886080735226 / (Abs(X) + 3.97886080735226) <br />
&nbsp; &nbsp;u = t - 0.5 <br />
&nbsp; &nbsp;y = (((((((((C0 * u + C1) * u - C2) * u - C3) * u + C4) * u + C5) * u - C6) * u - C7) * u + C8) * u + C9) * u + C10 <br />
&nbsp; &nbsp;y = ((((((((((((y * u - C11) * u - C12) * u + C13) * u + C14) * u + C15) * u + C16) * u + C17) * u + C18) * u + C19) * _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; u + C20) * u + C21) * u + C22) * t * Exp(-X * X) <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> X &lt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> derfc = 2 - y <span style="color: #8D38C9; font-weight: bold;">Else</span>: derfc = y <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>La précision de la version VBA est certainement moins bonne que la version en C de Takura Ooura car il utilise des constantes avec plus de 15 chiffres significatifs. En effet, VBA n&rsquo;affiche que 15 chiffres pour le type Double bien qu&rsquo;il conserve en faite 16 chiffres en interne&#8230;<br />
Pour cette raison, chaque constante est définie comme la somme de deux nombres pour conserver ce 16ème chiffre de précision.</p>
<p>La fonction VBA pour calculer la loi normale est :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'--------------------------------------------------------------------------------------- <br />
</span><span style="color: #008000;">' Procédure &nbsp; &nbsp;: LoiNormale &nbsp; [Function] <br />
</span><span style="color: #008000;">' Auteur &nbsp; &nbsp; &nbsp; : Philben - Free to use <br />
</span><span style="color: #008000;">' Retour &nbsp; &nbsp; &nbsp; : Double <br />
</span><span style="color: #008000;">' Version &nbsp; &nbsp; &nbsp;: 2.0 <br />
</span><span style="color: #008000;">' Objet &nbsp; &nbsp; &nbsp; &nbsp;: Détermine la probabilité cumulée ou non d'une distribution normale en fonction <br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: d'une variable X, de la moyenne et de l'écart-type de la distribution <br />
</span><span style="color: #008000;">' Historique &nbsp; : v2.0 : Double précision (07/2012) <br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v1.0 : Simple précision (07/2007) <br />
</span><span style="color: #008000;">'--------------------------------------------------------------------------------------- <br />
</span><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> LoiNormale(<span style="color: #151B8D; font-weight: bold;">ByVal</span> X <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> Esperance <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> EcartType <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Optional</span> CumulProbabilite <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span> = <span style="color: #00C2FF; font-weight: bold;">True</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> EcartType &lt;= 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; LoiNormale = -1 <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> CumulProbabilite <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LoiNormale = derfc((Esperance - X) / (EcartType * Sqr(2))) * 0.5 <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;X = (X - Esperance) / EcartType <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LoiNormale = Exp(-X * X * 0.5) / (EcartType * Sqr(8 * Atn(1))) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Les paramètres sont la variable aléatoire <strong>X</strong> pour laquelle on cherche la probabilité correspondante, <strong>l&rsquo;espérance</strong> de la distribution (moyenne mu), <strong>l&rsquo;écart-type</strong> de la distribution (standard deviation in english) et <strong>&lsquo;CumulProbabilite&rsquo;</strong> un booléen à vrai (par défaut) si on souhaite obtenir la probabilité cumulée de la fonction de répartition.<br />
Par défaut, l&rsquo;espérance est 0 et l&rsquo;écart-type à 1 ce qui correspond aux paramètres la loi normale centrée réduite (ou standard) N(0,1) d&rsquo;espérance égale à 0 et de variance égale à 1.</p>
<p><strong>Loi normale inverse</strong><br />
Cette fonction permet de trouver la variable aléatoire X associée à une probabilité cumulée. Ooura propose aussi un algorithme qui calcul l&rsquo;inverse de la fonction d&rsquo;erreur complémentaire qui permet d&rsquo;obtenir des résultats meilleurs qu&rsquo;une recherche par bisection avec la fonction derfc.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> dierfc(<span style="color: #151B8D; font-weight: bold;">ByVal</span> y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
<span style="color: #008000;">'Inverse of complementary error function in double precision <br />
</span><span style="color: #008000;">'http://www.kurims.kyoto-u.ac.jp/~ooura/index.html <br />
</span><span style="color: #008000;">'Copyright(C) 1996 Takuya OOURA : You may use, copy, modify this code for any purpose and without fee. <br />
</span><span style="color: #008000;">'Implementation en VBA par Philben - v1.0 <br />
</span> <br />
&nbsp; &nbsp;Const C0 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.12648096188977E-03 + 9.22E-18, C1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.05739299623423E-04 + 4.7E-20, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.003512871461291 + 2.5E-19, C3 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 7.7170835895412E-04 + 9.39E-19, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C4 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 6.85649426074558E-03 + 6.12E-18, C5 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 3.39721910367775E-03 + 8.61E-18, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C6 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.12749169332504E-02 + 8.7E-17, C7 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.18598117047771E-02 + 1.04E-17, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C8 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.42961988697898E-02 + 1.8E-18, C9 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 3.46494207789099E-02 + 9.22E-17, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C10 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 2.20995927012179E-03 + 6.7E-19 <br />
&nbsp; &nbsp;Const C11 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 7.43424357241784E-02 + 8.61E-17, C12 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.105872177941595 + 4.88E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C13 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.47297938331485E-02 + 1.21E-17, C14 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.316847638520135 + 9.44E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C15 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.71365763586873 + 3.64E-16, C16 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.05375024970847 + 1.38E-15, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C17 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.21448730779995 + 2.37E-15, C18 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1.1637458193156 + 8.31E-15, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C19 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.956464974744799 + 6E-18, C20 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.686265948274097 + 8.16E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C21 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.43439749233143 + 1.15E-16, C22 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.24404451059319 + 9.35E-16, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C23 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0.120782237635245 + 2.22E-16 <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, t <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, u <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, w <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, X <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, z <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> y &gt; 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> z = 2 - y <span style="color: #8D38C9; font-weight: bold;">Else</span>: z = y <br />
&nbsp; &nbsp;w = 0.916461398268964 - Log(z) <br />
&nbsp; &nbsp;u = Sqr(w) <br />
&nbsp; &nbsp;s = (Log(u) + 0.488826640273108) / w <br />
&nbsp; &nbsp;t = 1 / (u + 0.231729200323405) <br />
&nbsp; &nbsp;X = u * (1 - s * (s * 0.124610454613712 + 0.5)) - ((((-7.28846765585675E-02 * t + 0.269999308670029) * _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t + 0.150689047360223) * t + 0.116065025341614) * t + 0.499999303439796) * t <br />
&nbsp; &nbsp;t = 3.97886080735226 / (X + 3.97886080735226) <br />
&nbsp; &nbsp;u = t - 0.5 <br />
&nbsp; &nbsp;s = (((((((((C0 * u + C1) * u - C2) * u - C3) * u + C4) * u + C5) * u - C6) * u - C7) * u + C8) * u + C9) * u + C10 <br />
&nbsp; &nbsp;s = ((((((((((((s * u - C11) * u - C12) * u + C13) * u + C14) * u + C15) * u + C16) * u + C17) * u + _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C18) * u + C19) * u + C20) * u + C21) * u + C22) * t - z * Exp(X * X - C23) <br />
&nbsp; &nbsp;X = X + (s * (X * s + 1)) <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> y &gt; 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> dierfc = -X <span style="color: #8D38C9; font-weight: bold;">Else</span>: dierfc = X <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>La fonction VBA pour calculer l&rsquo;inverse de la loi normale :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'--------------------------------------------------------------------------------------- <br />
</span><span style="color: #008000;">' Procédure &nbsp; &nbsp;: LoiNormaleInverse &nbsp; [Function] <br />
</span><span style="color: #008000;">' Auteur &nbsp; &nbsp; &nbsp; : Philben - Free to use <br />
</span><span style="color: #008000;">' Retour &nbsp; &nbsp; &nbsp; : Double <br />
</span><span style="color: #008000;">' Version &nbsp; &nbsp; &nbsp;: 2.0 <br />
</span><span style="color: #008000;">' Objet &nbsp; &nbsp; &nbsp; &nbsp;: Détermine la variable X d'une distribution normale en fonction de <br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: sa probabilité cumulée Pc, de la moyenne et de l'écart-type de la distribution <br />
</span><span style="color: #008000;">' Historique &nbsp; : v2.0 : Double précision (07/2012) <br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v1.0 : Simple précision (07/2007) <br />
</span><span style="color: #008000;">'--------------------------------------------------------------------------------------- <br />
</span><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> LoiNormaleInverse(<span style="color: #151B8D; font-weight: bold;">ByVal</span> Pc <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span>, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> Esperance <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 0, _ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> EcartType <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> = 1) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> Pc &lt;= 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; LoiNormaleInverse = -99 <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">ElseIf</span> Pc &gt;= 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; LoiNormaleInverse = 99 <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; LoiNormaleInverse = (-dierfc(Pc * 2) * EcartType * Sqr(2)) + Esperance <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Les paramètres sont la probabilité cumulée, l&rsquo;espérance et l&rsquo;écart-type de la distribution gaussienne pour lesquels on recherche X.</p>
<p><strong>Précision des résultats</strong><br />
Par comparaison avec les résultats de la fonction Excel NORM_S_DIST (notre référence Excel 2010) sur la plage de valeurs entre -8 et 8 de la loi normale standard avec un incrément de 1E-05, la plus grande différence relevée est de <strong>2,220E-16</strong> et une erreur relative maximum de 1,490E-14.</p>
<p><strong>Exemples</strong><br />
Coller l&rsquo;ensemble des fonctions dans un module standard VBA. Dans la fenêtre d&rsquo;exécution de l&rsquo;éditeur visual basic (CTRL + G pour la faire apparaître) coller la ou les lignes d&rsquo;exemples qui commence par ?, mettre le focus sur la ligne à exécuter puis appuyer sur la touche &lsquo;Entrée&rsquo; pour voir le résultat.</p>
<p><strong>Quelle est la probabilité pour qu&rsquo;une variable aléatoire X soit inférieure ou égale à 14 si elle suit la loi normale N(10,16) (espérance = 10 et variance = 16) ?</strong><br />
L&rsquo;aire sous la courbe de couleur bleue est la probabilité attendue :<br />
<img src="http://philben.developpez.com/LoiNormale1.png" alt="Exemple loi normale" title="Exemple loi normale" /></p>
<p>Sachant que l&rsquo;écart-type est la racine carrée de la variance :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 0.841344746068543[/cci]<br />
</span>?LoiNormale(14,10,SQR(16))</div></div>
<p>Remarque : La loi normale est une loi continue de &#8211; l&rsquo;infini à + l&rsquo;infini et la probabilité que X soit égal à une valeur est pratiquement nulle (P(X = 14) = 0). Donc P(X <= 14) = P(X < 14).

<strong>Inversément, comment retrouver la variable X de cette probabilité ?</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'X attendu : 14<br />
</span>?LoiNormaleinverse(0.841344746068543,10,sqr(16))</div></div>
<p><strong>Quelle est la probabilité complémentaire de X : P(X > 14) (Aire verte de la courbe) ?</strong><br />
La loi normale est une loi symétrique, on peut donc écrire :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 0.158655253931457<br />
</span>?1-LoiNormale(14,10,sqr(16))</div></div>
<p>ou encore :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 0.158655253931457<br />
</span>?LoiNormale(10-(14-10),10,SQR(16))</div></div>
<p>Cette deuxième écriture est plus simple dans le cas d&rsquo;une loi normale centrée réduite N(0,1) :<br />
Exemple pour la probabilité complémentaire de X = 2 (P = 0.977249868051821 et 1-P = 2.27501319481792E-02)</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 2,27501319481792E-02<br />
</span>?LoiNormale(-2)</div></div>
<p><strong>Remarque </strong>: Sous certaines conditions, la loi normale permet d&rsquo;approximer les lois de Poisson et Binomiale.</p>
<p><strong>Quelle est la probabilité que 7 <= X <= 9 sachant que X suit une loi normale N(8;2,25) (espérance = 8 et variance = 2,25) ?</strong><br />
La probabilité recherchée est l&rsquo;aire de la courbe verte :<br />
<img src="http://philben.developpez.com/LoiNormale2.png" alt="Loi normale" title="loi normale" /></p>
<p>Dans cet exemple, les écarts à l&rsquo;espérance sont symétrique (7 + 1 = 8 et 9 &#8211; 1 = 8) on peut écrire :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 0.495014924906154 <br />
</span>?2*loinormale(9,8,sqr(2.25))-1<br />
<span style="color: #008000;">'Probabilité attendue : 0.495014924906154 <br />
</span>?1-2*loinormale(7,8,sqr(2.25))</div></div>
<p>Dans le cas général, il faut soustraire les deux probabilités. Par exemple pour 3 <= X <= 7 sachant que X suit une loi normale N(8;9), la formule devient :


<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Probabilité attendue : 0.321650987908949<br />
</span>?loinormale(7,8,3)-loinormale(3,8,3)</div></div>
<p><img src="http://philben.developpez.com/LoiNormale3.png" alt="Courbe en cloche" title="Courbe en cloche" /></p>
<p><strong>Exactitude des résultats</strong><br />
L&rsquo;objectif est d&rsquo;étudier succinctement l&rsquo;exactitude des résultats fournis par les fonctions Excel et les fonctions décrites ci-dessus.<br />
Pour ce faire je me suis servi comme référence des probabilités cumulées fournies par Maple 7 de la loi normale standard (avec plus de 16 chiffres significatifs) pour Z<=-1, Z<=-2, Z<=-3, ..., Z<=-37

Le 1er graphe affiche l'erreur relative commise sur l'approximation des probabilités en fonction de Z.
L'ordonnée est exprimée en log10 des erreurs relatives. Par exemple, le calcul de l'ordonnée avec la fonction Excel 2010 pour Z = -1 est :


<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Résultat attendu : 15.489451329628 <br />
</span>-Log(Abs(WorksheetFunction.Norm_S_Dist(-1, <span style="color: #00C2FF; font-weight: bold;">True</span>) - (0.158655253931457 + 5.141E-17))/ (0.158655253931457 + 5.141E-17)) / Log(10)</div></div>
<p>En pratique, il faut vérifier que la différence soit supérieure à zéro car Log(0) provoque une erreur&#8230;</p>
<p>Théoriquement, la valeur maximale est <strong>16</strong> lorsqu&rsquo;il n&rsquo;existe <strong>aucune différence</strong> entre les deux valeurs car le type Double a une précision de 16 chiffres (15 visibles + 1) : -Log(1E-16 / 1) / Log(10). Si on ne considère que les 15 chiffres visibles, l&rsquo;exactitude est déjà parfaite pour une valeur de 15.</p>
<p><strong>En résumé, plus la valeur du log de l&rsquo;erreur relative est élevée plus le résultat est exact.</strong></p>
<p><img src="http://philben.developpez.com/ErreurRelativeN.png" alt="Erreurs relatives sur N(0;1)" title="Erreurs relatives sur N(0;1)" /></p>
<p><strong>Les résultats entre Excel et Access sont très proches.</strong></p>
<p>Le 2ème graphe affiche l&rsquo;erreur relative commise sur l&rsquo;approximation de z en fonction des probabilités pour comparer les fonctions Norm_S_Inv() d&rsquo;Excel 2010 et LoiNormaleInverse().</p>
<p><img src="http://philben.developpez.com/ErreurRelativeZ.png" alt="Erreurs relatives sur Z" title="Erreurs relatives sur Z" /></p>
<p><strong>Les résultats entre Excel et Access sont très proches, on peut noter une petite faiblesse d&rsquo;Excel sur l&rsquo;approximation de z pour la 1ère probabilité (z=-1 théoriquement).</strong></p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
