blob: 6b74caf0601d8253b00c62e312e93395449ff090 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Triangular Distribution</title>
<link rel="stylesheet" href="../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.77.1">
<link rel="home" href="../../../index.html" title="Math Toolkit 2.2.0">
<link rel="up" href="../dists.html" title="Distributions">
<link rel="prev" href="students_t_dist.html" title="Students t Distribution">
<link rel="next" href="uniform_dist.html" title="Uniform Distribution">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="math_toolkit.dist_ref.dists.triangular_dist"></a><a class="link" href="triangular_dist.html" title="Triangular Distribution">Triangular
Distribution</a>
</h4></div></div></div>
<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>
<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>
<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>
<span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter&#160;14.&#160;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>
<span class="keyword">class</span> <span class="identifier">triangular_distribution</span><span class="special">;</span>
<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>
<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&#160;14.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">triangular_distribution</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
<span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">Policy</span> <span class="identifier">policy_type</span><span class="special">;</span>
<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>
<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 triangular distribution.</span>
<span class="comment">// Accessor functions.</span>
<span class="identifier">RealType</span> <span class="identifier">lower</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
<span class="identifier">RealType</span> <span class="identifier">mode</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
<span class="identifier">RealType</span> <span class="identifier">upper</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
<span class="special">};</span> <span class="comment">// class triangular_distribution</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<p>
The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
distribution</a> is a <a href="http://en.wikipedia.org/wiki/Continuous_distribution" target="_top">continuous</a>
<a href="http://en.wikipedia.org/wiki/Probability_distribution" target="_top">probability
distribution</a> with a lower limit a, <a href="http://en.wikipedia.org/wiki/Mode_%28statistics%29" target="_top">mode
c</a>, and upper limit b.
</p>
<p>
The triangular distribution is often used where the distribution is only
vaguely known, but, like the <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29" target="_top">uniform
distribution</a>, upper and limits are 'known', but a 'best guess',
the mode or center point, is also added. It has been recommended as a
<a href="http://www.worldscibooks.com/mathematics/etextbook/5720/5720_chap1.pdf" target="_top">proxy
for the beta distribution.</a> The distribution is used in business
decision making and project planning.
</p>
<p>
The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
distribution</a> is a distribution with the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
density function</a>:
</p>
<p>
f(x) =
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
2(x-a)/(b-a) (c-a) for a &lt;= x &lt;= c
</li>
<li class="listitem">
2(b-x)/(b-a)(b-c) for c &lt; x &lt;= b
</li>
</ul></div>
<p>
Parameter a (lower) can be any finite value. Parameter b (upper) can be
any finite value &gt; a (lower). Parameter c (mode) a &lt;= c &lt;= b.
This is the most probable value.
</p>
<p>
The <a href="http://en.wikipedia.org/wiki/Random_variate" target="_top">random variate</a>
x must also be finite, and is supported lower &lt;= x &lt;= upper.
</p>
<p>
The triangular distribution may be appropriate when an assumption of a
normal distribution is unjustified because uncertainty is caused by rounding
and quantization from analog to digital conversion. Upper and lower limits
are known, and the most probable value lies midway.
</p>
<p>
The distribution simplifies when the 'best guess' is either the lower or
upper limit - a 90 degree angle triangle. The default chosen is the 001
triangular distribution which expresses an estimate that the lowest value
is the most likely; for example, you believe that the next-day quoted delivery
date is most likely (knowing that a quicker delivery is impossible - the
postman only comes once a day), and that longer delays are decreasingly
likely, and delivery is assumed to never take more than your upper limit.
</p>
<p>
The following graph illustrates how the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
density function PDF</a> varies with the various parameters:
</p>
<p>
<span class="inlinemediaobject"><img src="../../../../graphs/triangular_pdf.svg" align="middle"></span>
</p>
<p>
and cumulative distribution function
</p>
<p>
<span class="inlinemediaobject"><img src="../../../../graphs/triangular_cdf.svg" align="middle"></span>
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h0"></a>
<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
Functions</a>
</h5>
<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>
</pre>
<p>
Constructs a <a href="http://en.wikipedia.org/wiki/triangular_distribution" target="_top">triangular
distribution</a> with lower <span class="emphasis"><em>lower</em></span> (a) and upper
<span class="emphasis"><em>upper</em></span> (b).
</p>
<p>
Requires that the <span class="emphasis"><em>lower</em></span>, <span class="emphasis"><em>mode</em></span>
and <span class="emphasis"><em>upper</em></span> parameters are all finite, otherwise calls
<a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
</p>
<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>
</pre>
<p>
Returns the <span class="emphasis"><em>lower</em></span> parameter of this distribution (default
-1).
</p>
<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>
</pre>
<p>
Returns the <span class="emphasis"><em>mode</em></span> parameter of this distribution (default
0).
</p>
<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>
</pre>
<p>
Returns the <span class="emphasis"><em>upper</em></span> parameter of this distribution (default+1).
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h1"></a>
<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
Accessors</a>
</h5>
<p>
All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member accessor
functions</a> that are generic to all distributions are supported:
<a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function</a>,
<a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function</a>,
<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>,
<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>,
<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>,
<a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation</a>,
<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>,
<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>.
</p>
<p>
The domain of the random variable is \lowerto \upper, and the supported
range is lower &lt;= x &lt;= upper.
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h2"></a>
<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>
</h5>
<p>
The triangular distribution is implemented with simple arithmetic operators
and so should have errors within an epsilon or two, except quantiles with
arguments nearing the extremes of zero and unity.
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h3"></a>
<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>
</h5>
<p>
In the following table, a is the <span class="emphasis"><em>lower</em></span> parameter of
the distribution, c is the <span class="emphasis"><em>mode</em></span> parameter, b is the
<span class="emphasis"><em>upper</em></span> parameter, <span class="emphasis"><em>x</em></span> is the random
variate, <span class="emphasis"><em>p</em></span> is the probability and <span class="emphasis"><em>q = 1-p</em></span>.
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Function
</p>
</th>
<th>
<p>
Implementation Notes
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
pdf
</p>
</td>
<td>
<p>
Using the relation: pdf = 0 for x &lt; mode, 2(x-a)/(b-a)(c-a)
else 2*(b-x)/((b-a)(b-c))
</p>
</td>
</tr>
<tr>
<td>
<p>
cdf
</p>
</td>
<td>
<p>
Using the relation: cdf = 0 for x &lt; mode (x-a)<sup>2</sup>/((b-a)(c-a))
else 1 - (b-x)<sup>2</sup>/((b-a)(b-c))
</p>
</td>
</tr>
<tr>
<td>
<p>
cdf complement
</p>
</td>
<td>
<p>
Using the relation: q = 1 - p
</p>
</td>
</tr>
<tr>
<td>
<p>
quantile
</p>
</td>
<td>
<p>
let p0 = (c-a)/(b-a) the point of inflection on the cdf, then
given probability p and q = 1-p:
</p>
<p>
x = sqrt((b-a)(c-a)p) + a ; for p &lt; p0
</p>
<p>
x = c ; for p == p0
</p>
<p>
x = b - sqrt((b-a)(b-c)q) ; for p &gt; p0
</p>
<p>
(See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
for details.)
</p>
</td>
</tr>
<tr>
<td>
<p>
quantile from the complement
</p>
</td>
<td>
<p>
As quantile (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
for details.)
</p>
</td>
</tr>
<tr>
<td>
<p>
mean
</p>
</td>
<td>
<p>
(a + b + 3) / 3
</p>
</td>
</tr>
<tr>
<td>
<p>
variance
</p>
</td>
<td>
<p>
(a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup> - ab - ac - bc)/18
</p>
</td>
</tr>
<tr>
<td>
<p>
mode
</p>
</td>
<td>
<p>
c
</p>
</td>
</tr>
<tr>
<td>
<p>
skewness
</p>
</td>
<td>
<p>
(See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
for details).
</p>
</td>
</tr>
<tr>
<td>
<p>
kurtosis
</p>
</td>
<td>
<p>
12/5
</p>
</td>
</tr>
<tr>
<td>
<p>
kurtosis excess
</p>
</td>
<td>
<p>
-3/5
</p>
</td>
</tr>
</tbody>
</table></div>
<p>
Some 'known good' test values were obtained from <a href="http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm" target="_top">Statlet:
Calculate and plot probability distributions</a>
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h4"></a>
<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>
</h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">Wikpedia
triangular distribution</a>
</li>
<li class="listitem">
<a href="http://mathworld.wolfram.com/TriangularDistribution.html" target="_top">Weisstein,
Eric W. "Triangular Distribution." From MathWorld--A Wolfram
Web Resource.</a>
</li>
<li class="listitem">
Evans, M.; Hastings, N.; and Peacock, B. "Triangular Distribution."
Ch. 40 in Statistical Distributions, 3rd ed. New York: Wiley, pp. 187-188,
2000, ISBN - 0471371246.
</li>
<li class="listitem">
<a href="http://www.brighton-webs.co.uk/distributions/triangular.asp" target="_top">Brighton
Webs Ltd. BW D-Calc 1.0 Distribution Calculator</a>
</li>
<li class="listitem">
<a href="http://www.worldscibooks.com/mathematics/etextbook/5720/5720_chap1.pdf" target="_top">The
Triangular Distribution including its history.</a>
</li>
<li class="listitem">
<a href="http://www.measurement.sk/2002/S1/Wimmer2.pdf" target="_top">Gejza Wimmer,
Viktor Witkovsky and Tomas Duby, Measurement Science Review, Volume
2, Section 1, 2002, Proper Rounding Of The Measurement Results Under
The Assumption Of Triangular Distribution.</a>
</li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2010, 2012-2014 Nikhar Agrawal,
Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
Holin, Bruno Lalande, John Maddock, Johan R&#229;de, Gautam Sewani, Benjamin Sobotta,
Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>