2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

#11768: Skewness formula for triangular distribution corrected, tests added and docs updated.

This commit is contained in:
pabristow
2015-10-29 18:19:46 +00:00
parent 5eb74b83c0
commit 57a71ba5f8
16 changed files with 197 additions and 103 deletions

View File

@@ -463,7 +463,7 @@ The default is to use `NTL::RR` unless you define an alternate macro, for exampl
and probably to your chosen arbitrary precision type library.
4. You need to add `libs\math\include_private` to your compiler's include path as the needed
header is not installed in the usual places by default (this avoids a cyclic dependency between
header is not installed in the usual places by default (this avoids a cyclic dependency between
the Math and Multiprecision library's headers).
5. The complete program to generate the constant `half_pi` using function `calculate_half_pi` is then:
@@ -660,7 +660,7 @@ so `std::numeric_limits<quad_float>::digits10()` should be 32.
(which seems to agree with experiments).
We output constants (including some noisy bits,
an approximation to `std::numeric_limits<RR>::max_digits10()`)
by adding 2 extra decimal digits, so using `quad_float::SetOutputPrecision(32 + 2);`
by adding 2 or 3 extra decimal digits, so using `quad_float::SetOutputPrecision(32 + 3);`
Apple Mac/Darwin uses a similar ['doubledouble] 106-bit for its built-in `long double` type.

View File

@@ -183,7 +183,9 @@ and the maximum number of possibly significant decimal digits using
std::numeric_limits<boost::floatmax_t>::max_digits10
[tip `max_digits10` is not always supported, but can be calculated at compile-time using the Kahan formula.]
[tip `max_digits10` is not always supported,
but can be calculated at compile-time using the Kahan formula.
]
[note One could test

View File

@@ -4,10 +4,10 @@
``#include <boost/math/distributions/triangular.hpp>``
namespace boost{ namespace math{
template <class RealType = double,
template <class RealType = double,
class ``__Policy`` = ``__policy_class`` >
class triangular_distribution;
typedef triangular_distribution<> triangular;
template <class RealType, class ``__Policy``>
@@ -18,15 +18,15 @@
typedef Policy policy_type;
triangular_distribution(RealType lower = -1, RealType mode = 0) RealType upper = 1); // Constructor.
: m_lower(lower), m_mode(mode), m_upper(upper) // Default is -1, 0, +1 triangular distribution.
: m_lower(lower), m_mode(mode), m_upper(upper) // Default is -1, 0, +1 symmetric triangular distribution.
// Accessor functions.
RealType lower()const;
RealType mode()const;
RealType upper()const;
}; // class triangular_distribution
}} // namespaces
The [@http://en.wikipedia.org/wiki/Triangular_distribution triangular distribution]
is a [@http://en.wikipedia.org/wiki/Continuous_distribution continuous]
[@http://en.wikipedia.org/wiki/Probability_distribution probability distribution]
@@ -42,18 +42,18 @@ It has been recommended as a
The distribution is used in business decision making and project planning.
The [@http://en.wikipedia.org/wiki/Triangular_distribution triangular distribution]
is a distribution with the
is a distribution with the
[@http://en.wikipedia.org/wiki/Probability_density_function probability density function]:
f(x) =
__spaces f(x) =
* 2(x-a)/(b-a) (c-a) for a <= x <= c
* 2(b-x)/(b-a)(b-c) for c < x <= b
Parameter a (lower) can be any finite value.
Parameter b (upper) can be any finite value > a (lower).
Parameter c (mode) a <= c <= b. This is the most probable value.
Parameter ['a] (lower) can be any finite value.
Parameter ['b] (upper) can be any finite value > a (lower).
Parameter ['c] (mode) a <= c <= b. This is the most probable value.
The [@http://en.wikipedia.org/wiki/Random_variate random variate] x must also be finite, and is supported lower <= x <= upper.
@@ -62,7 +62,7 @@ is unjustified because uncertainty is caused by rounding and quantization from a
Upper and lower limits are known, and the most probable value lies midway.
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;
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,
@@ -81,24 +81,38 @@ and cumulative distribution function
[h4 Member Functions]
triangular_distribution(RealType lower = 0, RealType mode = 0 RealType upper = 1);
Constructs a [@http://en.wikipedia.org/wiki/triangular_distribution triangular distribution]
with lower /lower/ (a) and upper /upper/ (b).
Requires that the /lower/, /mode/ and /upper/ parameters are all finite,
Requires that the /lower/, /mode/ and /upper/ parameters are all finite,
otherwise calls __domain_error.
[warning These constructors are slightly different from the analogs provided by __Mathworld
[@http://reference.wolfram.com/language/ref/TriangularDistribution.html Triangular distribution],
where
[^TriangularDistribution\[{min, max}\]] represents a [*symmetric] triangular statistical distribution giving values between min and max.[br]
[^TriangularDistribution\[\]] represents a [*symmetric] triangular statistical distribution giving values between 0 and 1.[br]
[^TriangularDistribution\[{min, max}, c\]] represents a triangular distribution with mode at c (usually [*asymmetric]).[br]
So, for example, to compute a variance using __WolframAlpha, use
[^N\[variance\[TriangularDistribution{1, +2}\], 50\]]
]
The parameters of a distribution can be obtained using these member functions:
RealType lower()const;
Returns the /lower/ parameter of this distribution (default -1).
Returns the ['lower] parameter of this distribution (default -1).
RealType mode()const;
Returns the /mode/ parameter of this distribution (default 0).
Returns the ['mode] parameter of this distribution (default 0).
RealType upper()const;
Returns the /upper/ parameter of this distribution (default+1).
Returns the ['upper] parameter of this distribution (default+1).
[h4 Non-member Accessors]
@@ -111,11 +125,11 @@ and the supported range is lower <= x <= upper.
[h4 Accuracy]
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.
except quantiles with arguments nearing the extremes of zero and unity.
[h4 Implementation]
In the following table, a is the /lower/ parameter of the distribution,
In the following table, a is the /lower/ parameter of the distribution,
c is the /mode/ parameter,
b is the /upper/ parameter,
/x/ is the random variate, /p/ is the probability and /q = 1-p/.
@@ -144,22 +158,19 @@ x = b - sqrt((b-a)(b-c)q) ; for p > p0
[[kurtosis excess][-3\/5]]
]
Some 'known good' test values were obtained from
[@http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm Statlet: Calculate and plot probability distributions]
Some 'known good' test values were obtained using __WolframAlpha.
[h4 References]
* [@http://en.wikipedia.org/wiki/Triangular_distribution Wikpedia triangular distribution]
* [@http://mathworld.wolfram.com/TriangularDistribution.html Weisstein, Eric W. "Triangular Distribution." From MathWorld--A Wolfram Web Resource.]
* 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.
* [@http://www.brighton-webs.co.uk/distributions/triangular.asp Brighton Webs Ltd. BW D-Calc 1.0 Distribution Calculator]
* [@http://www.worldscibooks.com/mathematics/etextbook/5720/5720_chap1.pdf The Triangular Distribution including its history.]
* [@http://www.measurement.sk/2002/S1/Wimmer2.pdf 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.]
[endsect][/section:triangular_dist triangular]
[/
[/
Copyright 2006 John Maddock and Paul A. Bristow.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at

View File

@@ -112,7 +112,7 @@ This manual is also available in <a href="http://sourceforge.net/projects/boost/
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: October 15, 2015 at 09:37:07 GMT</small></p></td>
<td align="left"><p><small>Last revised: October 29, 2015 at 18:14:57 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>

View File

@@ -24,7 +24,7 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1684620"></a>Function Index</h2></div></div></div>
<a name="id1802910"></a>Function Index</h2></div></div></div>
<p><a class="link" href="s01.html#idx_id_0">A</a> <a class="link" href="s01.html#idx_id_1">B</a> <a class="link" href="s01.html#idx_id_2">C</a> <a class="link" href="s01.html#idx_id_3">D</a> <a class="link" href="s01.html#idx_id_4">E</a> <a class="link" href="s01.html#idx_id_5">F</a> <a class="link" href="s01.html#idx_id_6">G</a> <a class="link" href="s01.html#idx_id_7">H</a> <a class="link" href="s01.html#idx_id_8">I</a> <a class="link" href="s01.html#idx_id_9">J</a> <a class="link" href="s01.html#idx_id_10">K</a> <a class="link" href="s01.html#idx_id_11">L</a> <a class="link" href="s01.html#idx_id_12">M</a> <a class="link" href="s01.html#idx_id_13">N</a> <a class="link" href="s01.html#idx_id_14">O</a> <a class="link" href="s01.html#idx_id_15">P</a> <a class="link" href="s01.html#idx_id_16">Q</a> <a class="link" href="s01.html#idx_id_17">R</a> <a class="link" href="s01.html#idx_id_18">S</a> <a class="link" href="s01.html#idx_id_19">T</a> <a class="link" href="s01.html#idx_id_20">U</a> <a class="link" href="s01.html#idx_id_21">V</a> <a class="link" href="s01.html#idx_id_23">Z</a></p>
<div class="variablelist"><dl class="variablelist">
<dt>

View File

@@ -24,7 +24,7 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1702319"></a>Class Index</h2></div></div></div>
<a name="id1824971"></a>Class Index</h2></div></div></div>
<p><a class="link" href="s02.html#idx_id_24">A</a> <a class="link" href="s02.html#idx_id_25">B</a> <a class="link" href="s02.html#idx_id_26">C</a> <a class="link" href="s02.html#idx_id_27">D</a> <a class="link" href="s02.html#idx_id_28">E</a> <a class="link" href="s02.html#idx_id_29">F</a> <a class="link" href="s02.html#idx_id_30">G</a> <a class="link" href="s02.html#idx_id_31">H</a> <a class="link" href="s02.html#idx_id_32">I</a> <a class="link" href="s02.html#idx_id_35">L</a> <a class="link" href="s02.html#idx_id_36">M</a> <a class="link" href="s02.html#idx_id_37">N</a> <a class="link" href="s02.html#idx_id_38">O</a> <a class="link" href="s02.html#idx_id_39">P</a> <a class="link" href="s02.html#idx_id_40">Q</a> <a class="link" href="s02.html#idx_id_41">R</a> <a class="link" href="s02.html#idx_id_42">S</a> <a class="link" href="s02.html#idx_id_43">T</a> <a class="link" href="s02.html#idx_id_44">U</a> <a class="link" href="s02.html#idx_id_46">W</a></p>
<div class="variablelist"><dl class="variablelist">
<dt>

View File

@@ -24,7 +24,7 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1706673"></a>Typedef Index</h2></div></div></div>
<a name="id1828383"></a>Typedef Index</h2></div></div></div>
<p><a class="link" href="s03.html#idx_id_48">A</a> <a class="link" href="s03.html#idx_id_49">B</a> <a class="link" href="s03.html#idx_id_50">C</a> <a class="link" href="s03.html#idx_id_51">D</a> <a class="link" href="s03.html#idx_id_52">E</a> <a class="link" href="s03.html#idx_id_53">F</a> <a class="link" href="s03.html#idx_id_54">G</a> <a class="link" href="s03.html#idx_id_55">H</a> <a class="link" href="s03.html#idx_id_56">I</a> <a class="link" href="s03.html#idx_id_59">L</a> <a class="link" href="s03.html#idx_id_61">N</a> <a class="link" href="s03.html#idx_id_62">O</a> <a class="link" href="s03.html#idx_id_63">P</a> <a class="link" href="s03.html#idx_id_65">R</a> <a class="link" href="s03.html#idx_id_66">S</a> <a class="link" href="s03.html#idx_id_67">T</a> <a class="link" href="s03.html#idx_id_68">U</a> <a class="link" href="s03.html#idx_id_69">V</a> <a class="link" href="s03.html#idx_id_70">W</a></p>
<div class="variablelist"><dl class="variablelist">
<dt>

View File

@@ -24,7 +24,7 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1709055"></a>Macro Index</h2></div></div></div>
<a name="id1831404"></a>Macro Index</h2></div></div></div>
<p><a class="link" href="s04.html#idx_id_73">B</a> <a class="link" href="s04.html#idx_id_77">F</a></p>
<div class="variablelist"><dl class="variablelist">
<dt>

View File

@@ -23,7 +23,7 @@
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1712845"></a>Index</h2></div></div></div>
<a name="id1830496"></a>Index</h2></div></div></div>
<p><a class="link" href="s05.html#idx_id_96">A</a> <a class="link" href="s05.html#idx_id_97">B</a> <a class="link" href="s05.html#idx_id_98">C</a> <a class="link" href="s05.html#idx_id_99">D</a> <a class="link" href="s05.html#idx_id_100">E</a> <a class="link" href="s05.html#idx_id_101">F</a> <a class="link" href="s05.html#idx_id_102">G</a> <a class="link" href="s05.html#idx_id_103">H</a> <a class="link" href="s05.html#idx_id_104">I</a> <a class="link" href="s05.html#idx_id_105">J</a> <a class="link" href="s05.html#idx_id_106">K</a> <a class="link" href="s05.html#idx_id_107">L</a> <a class="link" href="s05.html#idx_id_108">M</a> <a class="link" href="s05.html#idx_id_109">N</a> <a class="link" href="s05.html#idx_id_110">O</a> <a class="link" href="s05.html#idx_id_111">P</a> <a class="link" href="s05.html#idx_id_112">Q</a> <a class="link" href="s05.html#idx_id_113">R</a> <a class="link" href="s05.html#idx_id_114">S</a> <a class="link" href="s05.html#idx_id_115">T</a> <a class="link" href="s05.html#idx_id_116">U</a> <a class="link" href="s05.html#idx_id_117">V</a> <a class="link" href="s05.html#idx_id_118">W</a> <a class="link" href="s05.html#idx_id_119">Z</a></p>
<div class="variablelist"><dl class="variablelist">
<dt>

View File

@@ -310,8 +310,8 @@
should be 32. (the default <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">RR</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">()</span></code>
should be about 40). (which seems to agree with experiments). We output constants
(including some noisy bits, an approximation to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">RR</span><span class="special">&gt;::</span><span class="identifier">max_digits10</span><span class="special">()</span></code>)
by adding 2 extra decimal digits, so using <code class="computeroutput"><span class="identifier">quad_float</span><span class="special">::</span><span class="identifier">SetOutputPrecision</span><span class="special">(</span><span class="number">32</span> <span class="special">+</span>
<span class="number">2</span><span class="special">);</span></code>
by adding 2 or 3 extra decimal digits, so using <code class="computeroutput"><span class="identifier">quad_float</span><span class="special">::</span><span class="identifier">SetOutputPrecision</span><span class="special">(</span><span class="number">32</span> <span class="special">+</span>
<span class="number">3</span><span class="special">);</span></code>
</p>
<p>
Apple Mac/Darwin uses a similar <span class="emphasis"><em>doubledouble</em></span> 106-bit for

View File

@@ -27,7 +27,7 @@
<a name="math_toolkit.conventions"></a><a class="link" href="conventions.html" title="Document Conventions">Document Conventions</a>
</h2></div></div></div>
<p>
<a class="indexterm" name="id833792"></a>
<a class="indexterm" name="id956206"></a>
</p>
<p>
This documentation aims to use of the following naming and formatting conventions.

View File

@@ -43,7 +43,7 @@
<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="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>
<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>
@@ -74,7 +74,7 @@
density function</a>:
</p>
<p>
f(x) =
&#8192;&#8192; f(x) =
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
@@ -85,9 +85,10 @@
</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.
Parameter <span class="emphasis"><em>a</em></span> (lower) can be any finite value. Parameter
<span class="emphasis"><em>b</em></span> (upper) can be any finite value &gt; a (lower).
Parameter <span class="emphasis"><em>c</em></span> (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>
@@ -101,12 +102,12 @@
</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.
upper limit - a 90 degree angle triangle. 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
@@ -138,6 +139,36 @@
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>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top">
<p>
These constructors are slightly different from the analogs provided by
<a href="http://mathworld.wolfram.com" target="_top">Wolfram MathWorld</a>
<a href="http://reference.wolfram.com/language/ref/TriangularDistribution.html" target="_top">Triangular
distribution</a>, where
</p>
<p>
<code class="literal">TriangularDistribution[{min, max}]</code> represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical distribution
giving values between min and max.<br> <code class="literal">TriangularDistribution[]</code>
represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical
distribution giving values between 0 and 1.<br> <code class="literal">TriangularDistribution[{min,
max}, c]</code> represents a triangular distribution with mode at
c (usually <span class="bold"><strong>asymmetric</strong></span>).<br>
</p>
<p>
So, for example, to compute a variance using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
Alpha</a>, use <code class="literal">N[variance[TriangularDistribution{1, +2}],
50]</code>
</p>
</td></tr>
</table></div>
<p>
The parameters of a distribution can be obtained using these member functions:
</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>
@@ -366,8 +397,8 @@
</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>
Some 'known good' test values were obtained using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
Alpha</a>.
</p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h4"></a>
@@ -388,14 +419,6 @@
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

View File

@@ -27,7 +27,7 @@
<a name="math_toolkit.navigation"></a><a class="link" href="navigation.html" title="Navigation">Navigation</a>
</h2></div></div></div>
<p>
<a class="indexterm" name="id833654"></a>
<a class="indexterm" name="id956084"></a>
</p>
<p>
Boost.Math documentation is provided in both HTML and PDF formats.

View File

@@ -8,6 +8,12 @@
#define BOOST_STATS_TRIANGULAR_HPP
// http://mathworld.wolfram.com/TriangularDistribution.html
// Note that the 'constructors' defined by Wolfram are difference from those here,
// for example
// N[variance[triangulardistribution{1, +2}, 1.5], 50] computes
// 0.041666666666666666666666666666666666666666666666667
// TriangularDistribution{1, +2}, 1.5 is the analog of triangular_distribution(1, 1.5, 2)
// http://en.wikipedia.org/wiki/Triangular_distribution
#include <boost/math/distributions/fwd.hpp>
@@ -449,7 +455,7 @@ namespace boost{ namespace math
}
RealType lower = dist.lower();
RealType upper = dist.upper();
if (mode < (upper - lower) / 2)
if (mode >= (upper + lower) / 2)
{
return lower + sqrt((upper - lower) * (mode - lower)) / constants::root_two<RealType>();
}
@@ -475,7 +481,9 @@ namespace boost{ namespace math
return result;
}
return root_two<RealType>() * (lower + upper - 2 * mode) * (2 * lower - upper - mode) * (lower - 2 * upper + mode) /
(5 * pow((lower * lower + upper + upper + mode * mode - lower * upper - lower * mode - upper * mode), RealType(3)/RealType(2)));
(5 * pow((lower * lower + upper * upper + mode * mode
- lower * upper - lower * mode - upper * mode), RealType(3)/RealType(2)));
// #11768: Skewness formula for triangular distribution is incorrect - corrected 29 Oct 2015 for release 1.61.
} // RealType skewness(const triangular_distribution<RealType, Policy>& dist)
template <class RealType, class Policy>

View File

@@ -63,7 +63,7 @@ inline T bernoulli_b2n(const int i, const Policy &pol)
if(i < 0)
return policies::raise_domain_error<T>("boost::math::bernoulli_b2n<%1%>", "Index should be >= 0 but got %1%", T(i), pol);
T result = 0; // The = 0 is just to silence compiler warings :-(
T result = static_cast<T>(0); // The = 0 is just to silence compiler warnings :-(
boost::math::detail::bernoulli_number_imp<T>(&result, static_cast<std::size_t>(i), 1u, pol, tag_type());
return result;
}

View File

@@ -432,46 +432,96 @@ void test_spots(RealType)
boost::math::tools::epsilon<RealType>(),
static_cast<RealType>(boost::math::tools::epsilon<double>())) * 10; // 10 eps as a fraction.
cout << "Tolerance (as fraction) for type " << typeid(RealType).name() << " is " << tolerance << "." << endl;
triangular_distribution<RealType> tridef; // (-1, 0, 1) // default
RealType x = static_cast<RealType>(0.5);
using namespace std; // ADL of std names.
// mean:
BOOST_CHECK_CLOSE_FRACTION(
mean(tridef), static_cast<RealType>(0), tolerance);
// variance:
BOOST_CHECK_CLOSE_FRACTION(
variance(tridef), static_cast<RealType>(0.16666666666666666666666666666666666666666667L), tolerance);
// was 0.0833333333333333333333333333333333333333333L
triangular_distribution<RealType> tridef; // (-1, 0, 1) // Default distribution.
RealType x = static_cast<RealType>(0.5);
using namespace std; // ADL of std names.
// mean:
BOOST_CHECK_CLOSE_FRACTION(
mean(tridef), static_cast<RealType>(0), tolerance);
// variance:
BOOST_CHECK_CLOSE_FRACTION(
variance(tridef), static_cast<RealType>(0.16666666666666666666666666666666666666666667L), tolerance);
// was 0.0833333333333333333333333333333333333333333L
// std deviation:
BOOST_CHECK_CLOSE_FRACTION(
standard_deviation(tridef), sqrt(variance(tridef)), tolerance);
// hazard:
BOOST_CHECK_CLOSE_FRACTION(
hazard(tridef, x), pdf(tridef, x) / cdf(complement(tridef, x)), tolerance);
// cumulative hazard:
BOOST_CHECK_CLOSE_FRACTION(
chf(tridef, x), -log(cdf(complement(tridef, x))), tolerance);
// coefficient_of_variation:
if (mean(tridef) != 0)
{
BOOST_CHECK_CLOSE_FRACTION(
coefficient_of_variation(tridef), standard_deviation(tridef) / mean(tridef), tolerance);
}
// mode:
BOOST_CHECK_CLOSE_FRACTION(
mode(tridef), static_cast<RealType>(0), tolerance);
// skewness:
BOOST_CHECK_CLOSE_FRACTION(
median(tridef), static_cast<RealType>(0), tolerance);
// https://reference.wolfram.com/language/ref/Skewness.html skewness{-1, 0, +1} = 0
// skewness[triangulardistribution{-1, 0, +1}] does not compute a result.
// skewness[triangulardistribution{0, +1}] result == 0
// skewness[normaldistribution{0,1}] result == 0
BOOST_CHECK_EQUAL(
skewness(tridef), static_cast<RealType>(0));
// kurtosis:
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tridef), kurtosis(tridef) - static_cast<RealType>(3L), tolerance);
// kurtosis excess = kurtosis - 3;
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tridef), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
// std deviation:
BOOST_CHECK_CLOSE_FRACTION(
standard_deviation(tridef), sqrt(variance(tridef)), tolerance);
// hazard:
BOOST_CHECK_CLOSE_FRACTION(
hazard(tridef, x), pdf(tridef, x) / cdf(complement(tridef, x)), tolerance);
// cumulative hazard:
BOOST_CHECK_CLOSE_FRACTION(
chf(tridef, x), -log(cdf(complement(tridef, x))), tolerance);
// coefficient_of_variation:
if (mean(tridef) != 0)
{
BOOST_CHECK_CLOSE_FRACTION(
coefficient_of_variation(tridef), standard_deviation(tridef) / mean(tridef), tolerance);
}
// mode:
BOOST_CHECK_CLOSE_FRACTION(
mode(tridef), static_cast<RealType>(0), tolerance);
// skewness:
BOOST_CHECK_CLOSE_FRACTION(
median(trim12), static_cast<RealType>(-0.13397459621556151), tolerance);
BOOST_CHECK_EQUAL(
skewness(tridef), static_cast<RealType>(0));
// kurtosis:
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tridef), kurtosis(tridef) - static_cast<RealType>(3L), tolerance);
// kurtosis excess = kurtosis - 3;
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tridef), static_cast<RealType>(-0.6), tolerance); // for all distributions.
triangular_distribution<RealType> tri01(0, 1, 1); // Asymmetric 0, 1, 1 distribution.
RealType x = static_cast<RealType>(0.5);
using namespace std; // ADL of std names.
// mean:
BOOST_CHECK_CLOSE_FRACTION(
mean(tri01), static_cast<RealType>(0.66666666666666666666666666666666666666666666666667L), tolerance);
// variance: N[variance[triangulardistribution{0, 1}, 1], 50]
BOOST_CHECK_CLOSE_FRACTION(
variance(tri01), static_cast<RealType>(0.055555555555555555555555555555555555555555555555556L), tolerance);
// std deviation:
BOOST_CHECK_CLOSE_FRACTION(
standard_deviation(tri01), sqrt(variance(tri01)), tolerance);
// hazard:
BOOST_CHECK_CLOSE_FRACTION(
hazard(tri01, x), pdf(tri01, x) / cdf(complement(tri01, x)), tolerance);
// cumulative hazard:
BOOST_CHECK_CLOSE_FRACTION(
chf(tri01, x), -log(cdf(complement(tri01, x))), tolerance);
// coefficient_of_variation:
if (mean(tri01) != 0)
{
BOOST_CHECK_CLOSE_FRACTION(
coefficient_of_variation(tri01), standard_deviation(tri01) / mean(tri01), tolerance);
}
// mode:
BOOST_CHECK_CLOSE_FRACTION(
mode(tri01), static_cast<RealType>(1), tolerance);
// skewness:
BOOST_CHECK_CLOSE_FRACTION(
median(tri01), static_cast<RealType>(0.70710678118654752440084436210484903928483593768847L), tolerance);
// https://reference.wolfram.com/language/ref/Skewness.html
// N[skewness[triangulardistribution{0, 1}, 1], 50]
BOOST_CHECK_CLOSE_FRACTION(
skewness(tri01), static_cast<RealType>(-0.56568542494923801952067548968387923142786875015078L), tolerance);
// kurtosis:
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tri01), kurtosis(tri01) - static_cast<RealType>(3L), tolerance);
// kurtosis excess = kurtosis - 3;
BOOST_CHECK_CLOSE_FRACTION(
kurtosis_excess(tri01), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
} // tri01 tests
if(std::numeric_limits<RealType>::has_infinity)
{ // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()