• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Triangular Distribution</title>
5<link rel="stylesheet" href="../../../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../dists.html" title="Distributions">
9<link rel="prev" href="students_t_dist.html" title="Students t Distribution">
10<link rel="next" href="uniform_dist.html" title="Uniform Distribution">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="students_t_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="uniform_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="math_toolkit.dist_ref.dists.triangular_dist"></a><a class="link" href="triangular_dist.html" title="Triangular Distribution">Triangular
28        Distribution</a>
29</h4></div></div></div>
30<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">triangular</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
31<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
32 <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RealType</span> <span class="special">=</span> <span class="keyword">double</span><span class="special">,</span>
33           <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a>   <span class="special">=</span> <a class="link" href="../../pol_ref/pol_ref_ref.html" title="Policy Class Reference">policies::policy&lt;&gt;</a> <span class="special">&gt;</span>
34 <span class="keyword">class</span> <span class="identifier">triangular_distribution</span><span class="special">;</span>
35
36 <span class="keyword">typedef</span> <span class="identifier">triangular_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">triangular</span><span class="special">;</span>
37
38 <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
39 <span class="keyword">class</span> <span class="identifier">triangular_distribution</span>
40 <span class="special">{</span>
41 <span class="keyword">public</span><span class="special">:</span>
42    <span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span>
43    <span class="keyword">typedef</span> <span class="identifier">Policy</span>   <span class="identifier">policy_type</span><span class="special">;</span>
44
45    <span class="identifier">triangular_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">lower</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">mode</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="identifier">RealType</span> <span class="identifier">upper</span> <span class="special">=</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Constructor.</span>
46       <span class="special">:</span> <span class="identifier">m_lower</span><span class="special">(</span><span class="identifier">lower</span><span class="special">),</span> <span class="identifier">m_mode</span><span class="special">(</span><span class="identifier">mode</span><span class="special">),</span> <span class="identifier">m_upper</span><span class="special">(</span><span class="identifier">upper</span><span class="special">)</span> <span class="comment">// Default is -1, 0, +1 symmetric triangular distribution.</span>
47    <span class="comment">// Accessor functions.</span>
48    <span class="identifier">RealType</span> <span class="identifier">lower</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
49    <span class="identifier">RealType</span> <span class="identifier">mode</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
50    <span class="identifier">RealType</span> <span class="identifier">upper</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
51 <span class="special">};</span> <span class="comment">// class triangular_distribution</span>
52
53<span class="special">}}</span> <span class="comment">// namespaces</span>
54</pre>
55<p>
56          The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
57          distribution</a> is a <a href="http://en.wikipedia.org/wiki/Continuous_distribution" target="_top">continuous</a>
58          <a href="http://en.wikipedia.org/wiki/Probability_distribution" target="_top">probability
59          distribution</a> with a lower limit a, <a href="http://en.wikipedia.org/wiki/Mode_%28statistics%29" target="_top">mode
60          c</a>, and upper limit b.
61        </p>
62<p>
63          The triangular distribution is often used where the distribution is only
64          vaguely known, but, like the <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29" target="_top">uniform
65          distribution</a>, upper and limits are 'known', but a 'best guess',
66          the mode or center point, is also added. It has been recommended as a
67          <a href="http://www.worldscibooks.com/mathematics/etextbook/5720/5720_chap1.pdf" target="_top">proxy
68          for the beta distribution.</a> The distribution is used in business
69          decision making and project planning.
70        </p>
71<p>
72          The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
73          distribution</a> is a distribution with the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
74          density function</a>:
75        </p>
76<div class="blockquote"><blockquote class="blockquote"><p>
77            <span class="serif_italic">f(x) = 2(x-a)/(b-a) (c-a)    for a &lt;= x &lt;=
78            c</span>
79          </p></blockquote></div>
80<div class="blockquote"><blockquote class="blockquote"><p>
81            <span class="serif_italic">f(x) = 2(b-x)/(b-a) (b-c)   for c &lt; x &lt;=
82            b</span>
83          </p></blockquote></div>
84<p>
85          Parameter <span class="emphasis"><em>a</em></span> (lower) can be any finite value. Parameter
86          <span class="emphasis"><em>b</em></span> (upper) can be any finite value &gt; a (lower).
87          Parameter <span class="emphasis"><em>c</em></span> (mode) a &lt;= c &lt;= b. This is the
88          most probable value.
89        </p>
90<p>
91          The <a href="http://en.wikipedia.org/wiki/Random_variate" target="_top">random variate</a>
92          x must also be finite, and is supported lower &lt;= x &lt;= upper.
93        </p>
94<p>
95          The triangular distribution may be appropriate when an assumption of a
96          normal distribution is unjustified because uncertainty is caused by rounding
97          and quantization from analog to digital conversion. Upper and lower limits
98          are known, and the most probable value lies midway.
99        </p>
100<p>
101          The distribution simplifies when the 'best guess' is either the lower or
102          upper limit - a 90 degree angle triangle. The 001 triangular distribution
103          which expresses an estimate that the lowest value is the most likely; for
104          example, you believe that the next-day quoted delivery date is most likely
105          (knowing that a quicker delivery is impossible - the postman only comes
106          once a day), and that longer delays are decreasingly likely, and delivery
107          is assumed to never take more than your upper limit.
108        </p>
109<p>
110          The following graph illustrates how the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
111          density function PDF</a> varies with the various parameters:
112        </p>
113<div class="blockquote"><blockquote class="blockquote"><p>
114            <span class="inlinemediaobject"><img src="../../../../graphs/triangular_pdf.svg" align="middle"></span>
115
116          </p></blockquote></div>
117<p>
118          and cumulative distribution function
119        </p>
120<div class="blockquote"><blockquote class="blockquote"><p>
121            <span class="inlinemediaobject"><img src="../../../../graphs/triangular_cdf.svg" align="middle"></span>
122
123          </p></blockquote></div>
124<h5>
125<a name="math_toolkit.dist_ref.dists.triangular_dist.h0"></a>
126          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.member_functions"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.member_functions">Member
127          Functions</a>
128        </h5>
129<pre class="programlisting"><span class="identifier">triangular_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">lower</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">mode</span> <span class="special">=</span> <span class="number">0</span> <span class="identifier">RealType</span> <span class="identifier">upper</span> <span class="special">=</span> <span class="number">1</span><span class="special">);</span>
130</pre>
131<p>
132          Constructs a <a href="http://en.wikipedia.org/wiki/triangular_distribution" target="_top">triangular
133          distribution</a> with lower <span class="emphasis"><em>lower</em></span> (a) and upper
134          <span class="emphasis"><em>upper</em></span> (b).
135        </p>
136<p>
137          Requires that the <span class="emphasis"><em>lower</em></span>, <span class="emphasis"><em>mode</em></span>
138          and <span class="emphasis"><em>upper</em></span> parameters are all finite, otherwise calls
139          <a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
140        </p>
141<div class="warning"><table border="0" summary="Warning">
142<tr>
143<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../../doc/src/images/warning.png"></td>
144<th align="left">Warning</th>
145</tr>
146<tr><td align="left" valign="top">
147<p>
148            These constructors are slightly different from the analogs provided by
149            <a href="http://mathworld.wolfram.com" target="_top">Wolfram MathWorld</a>
150            <a href="http://reference.wolfram.com/language/ref/TriangularDistribution.html" target="_top">Triangular
151            distribution</a>, where
152          </p>
153<p>
154            <code class="literal">TriangularDistribution[{min, max}]</code> represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical distribution
155            giving values between min and max.<br> <code class="literal">TriangularDistribution[]</code>
156            represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical
157            distribution giving values between 0 and 1.<br> <code class="literal">TriangularDistribution[{min,
158            max}, c]</code> represents a triangular distribution with mode at
159            c (usually <span class="bold"><strong>asymmetric</strong></span>).<br>
160          </p>
161<p>
162            So, for example, to compute a variance using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
163            Alpha</a>, use <code class="literal">N[variance[TriangularDistribution{1, +2}],
164            50]</code>
165          </p>
166</td></tr>
167</table></div>
168<p>
169          The parameters of a distribution can be obtained using these member functions:
170        </p>
171<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">lower</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
172</pre>
173<p>
174          Returns the <span class="emphasis"><em>lower</em></span> parameter of this distribution (default
175          -1).
176        </p>
177<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">mode</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
178</pre>
179<p>
180          Returns the <span class="emphasis"><em>mode</em></span> parameter of this distribution (default
181          0).
182        </p>
183<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">upper</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
184</pre>
185<p>
186          Returns the <span class="emphasis"><em>upper</em></span> parameter of this distribution (default+1).
187        </p>
188<h5>
189<a name="math_toolkit.dist_ref.dists.triangular_dist.h1"></a>
190          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.non_member_accessors"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.non_member_accessors">Non-member
191          Accessors</a>
192        </h5>
193<p>
194          All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member accessor
195          functions</a> that are generic to all distributions are supported:
196          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function</a>,
197          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function</a>,
198          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.hazard">Hazard Function</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.chf">Cumulative Hazard Function</a>,
199          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mean">mean</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.median">median</a>,
200          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mode">mode</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.variance">variance</a>,
201          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation</a>,
202          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.skewness">skewness</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis">kurtosis</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis_excess">kurtosis_excess</a>,
203          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.range">range</a> and <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.support">support</a>.
204        </p>
205<p>
206          The domain of the random variable is \lowerto \upper, and the supported
207          range is lower &lt;= x &lt;= upper.
208        </p>
209<h5>
210<a name="math_toolkit.dist_ref.dists.triangular_dist.h2"></a>
211          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.accuracy"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.accuracy">Accuracy</a>
212        </h5>
213<p>
214          The triangular distribution is implemented with simple arithmetic operators
215          and so should have errors within an epsilon or two, except quantiles with
216          arguments nearing the extremes of zero and unity.
217        </p>
218<h5>
219<a name="math_toolkit.dist_ref.dists.triangular_dist.h3"></a>
220          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.implementation"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.implementation">Implementation</a>
221        </h5>
222<p>
223          In the following table, a is the <span class="emphasis"><em>lower</em></span> parameter of
224          the distribution, c is the <span class="emphasis"><em>mode</em></span> parameter, b is the
225          <span class="emphasis"><em>upper</em></span> parameter, <span class="emphasis"><em>x</em></span> is the random
226          variate, <span class="emphasis"><em>p</em></span> is the probability and <span class="emphasis"><em>q = 1-p</em></span>.
227        </p>
228<div class="informaltable"><table class="table">
229<colgroup>
230<col>
231<col>
232</colgroup>
233<thead><tr>
234<th>
235                  <p>
236                    Function
237                  </p>
238                </th>
239<th>
240                  <p>
241                    Implementation Notes
242                  </p>
243                </th>
244</tr></thead>
245<tbody>
246<tr>
247<td>
248                  <p>
249                    pdf
250                  </p>
251                </td>
252<td>
253                  <p>
254                    Using the relation: pdf = 0 for x &lt; mode, 2(x-a)/(b-a)(c-a)
255                    else 2*(b-x)/((b-a)(b-c))
256                  </p>
257                </td>
258</tr>
259<tr>
260<td>
261                  <p>
262                    cdf
263                  </p>
264                </td>
265<td>
266                  <p>
267                    Using the relation: cdf = 0 for x &lt; mode (x-a)<sup>2</sup>/((b-a)(c-a))
268                    else 1 - (b-x)<sup>2</sup>/((b-a)(b-c))
269                  </p>
270                </td>
271</tr>
272<tr>
273<td>
274                  <p>
275                    cdf complement
276                  </p>
277                </td>
278<td>
279                  <p>
280                    Using the relation: q = 1 - p
281                  </p>
282                </td>
283</tr>
284<tr>
285<td>
286                  <p>
287                    quantile
288                  </p>
289                </td>
290<td>
291                  <p>
292                    let p0 = (c-a)/(b-a) the point of inflection on the cdf, then
293                    given probability p and q = 1-p:
294                  </p>
295                  <p>
296                    x = sqrt((b-a)(c-a)p) + a ; for p &lt; p0
297                  </p>
298                  <p>
299                    x = c ; for p == p0
300                  </p>
301                  <p>
302                    x = b - sqrt((b-a)(b-c)q) ; for p &gt; p0
303                  </p>
304                  <p>
305                    (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
306                    for details.)
307                  </p>
308                </td>
309</tr>
310<tr>
311<td>
312                  <p>
313                    quantile from the complement
314                  </p>
315                </td>
316<td>
317                  <p>
318                    As quantile (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
319                    for details.)
320                  </p>
321                </td>
322</tr>
323<tr>
324<td>
325                  <p>
326                    mean
327                  </p>
328                </td>
329<td>
330                  <p>
331                    (a + b + 3) / 3
332                  </p>
333                </td>
334</tr>
335<tr>
336<td>
337                  <p>
338                    variance
339                  </p>
340                </td>
341<td>
342                  <p>
343                    (a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup> - ab - ac - bc)/18
344                  </p>
345                </td>
346</tr>
347<tr>
348<td>
349                  <p>
350                    mode
351                  </p>
352                </td>
353<td>
354                  <p>
355                    c
356                  </p>
357                </td>
358</tr>
359<tr>
360<td>
361                  <p>
362                    skewness
363                  </p>
364                </td>
365<td>
366                  <p>
367                    (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
368                    for details).
369                  </p>
370                </td>
371</tr>
372<tr>
373<td>
374                  <p>
375                    kurtosis
376                  </p>
377                </td>
378<td>
379                  <p>
380                    12/5
381                  </p>
382                </td>
383</tr>
384<tr>
385<td>
386                  <p>
387                    kurtosis excess
388                  </p>
389                </td>
390<td>
391                  <p>
392                    -3/5
393                  </p>
394                </td>
395</tr>
396</tbody>
397</table></div>
398<p>
399          Some 'known good' test values were obtained using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
400          Alpha</a>.
401        </p>
402<h5>
403<a name="math_toolkit.dist_ref.dists.triangular_dist.h4"></a>
404          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.references"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.references">References</a>
405        </h5>
406<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
407<li class="listitem">
408              <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">Wikipedia
409              triangular distribution</a>
410            </li>
411<li class="listitem">
412              <a href="http://mathworld.wolfram.com/TriangularDistribution.html" target="_top">Weisstein,
413              Eric W. "Triangular Distribution." From MathWorld--A Wolfram
414              Web Resource.</a>
415            </li>
416<li class="listitem">
417              Evans, M.; Hastings, N.; and Peacock, B. "Triangular Distribution."
418              Ch. 40 in Statistical Distributions, 3rd ed. New York: Wiley, pp. 187-188,
419              2000, ISBN - 0471371246.
420            </li>
421<li class="listitem">
422              <a href="http://www.measurement.sk/2002/S1/Wimmer2.pdf" target="_top">Gejza Wimmer,
423              Viktor Witkovsky and Tomas Duby, Measurement Science Review, Volume
424              2, Section 1, 2002, Proper Rounding Of The Measurement Results Under
425              The Assumption Of Triangular Distribution.</a>
426            </li>
427</ul></div>
428</div>
429<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
430<td align="left"></td>
431<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
432      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
433      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
434      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
435      Daryle Walker and Xiaogang Zhang<p>
436        Distributed under the Boost Software License, Version 1.0. (See accompanying
437        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
438      </p>
439</div></td>
440</tr></table>
441<hr>
442<div class="spirit-nav">
443<a accesskey="p" href="students_t_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="uniform_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
444</div>
445</body>
446</html>
447