<div dir="ltr"><div><div><font face="courier new, monospace">I've added a trapezoidal distribution to numpy.random for consideration, pull request 3770:</font></div><div><font face="courier new, monospace"><a href="https://github.com/numpy/numpy/pull/3770">https://github.com/numpy/numpy/pull/3770</a><br>
</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">Similar to the triangular distribution, the trapezoidal distribution may be used where the underlying distribution is not known, but some knowledge of the limits and mode exists. The trapezoidal distribution generalizes the triangular distribution by allowing the modal values to be expressed as a range instead of a point estimate.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">The trapezoidal distribution implemented, known as the "generalized trapezoidal distribution," has three additional parameters: growth, decay, and boundary ratio. Adjusting these from the default values create trapezoidal-like distributions with non-linear behavior. Examples can be seen in an R vignette ( <a href="http://cran.r-project.org/web/packages/trapezoid/vignettes/trapezoid.pdf">http://cran.r-project.org/web/packages/trapezoid/vignettes/trapezoid.pdf</a> ), as well as these papers by J.R. van Dorp and colleagues:</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">1) van Dorp, J. R. and Kotz, S. (2003) Generalized trapezoidal distributions. Metrika. 58(1):85–97. Preprint available: <a href="http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/Metrika2003VanDorp.pdf">http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/Metrika2003VanDorp.pdf</a></font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">2) van Dorp, J. R., Rambaud, S.C., Perez, J. G., and Pleguezuelo, R. H. (2007) An elicitation procedure for the generalized trapezoidal distribution with a uniform central stage. Decision Analysis Journal. 4:156–166. Preprint available: <a href="http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/DA2007.pdf">http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/DA2007.pdf</a></font></div>
<div><font face="courier new, monospace"><br></font></div></div><div><font face="courier new, monospace">The docstring for the proposed numpy.random.trapezoidal() is as follows:</font></div><div><font face="courier new, monospace"><br>
</font></div><div><div><font face="courier new, monospace">"""</font></div><div><font face="courier new, monospace"> trapezoidal(left, mode1, mode2, right, size=None, m=2, n=2, alpha=1)</font></div>
<div>
<font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Draw samples from the generalized trapezoidal distribution.</font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace"> The trapezoidal distribution is defined by minimum (``left``), lower mode (``mode1``), upper</font></div><div><font face="courier new, monospace"> mode (``mode1``), and maximum (``right``) parameters. The generalized trapezoidal distribution</font></div>
<div><font face="courier new, monospace"> adds three more parameters: the growth rate (``m``), decay rate (``n``), and boundary</font></div><div><font face="courier new, monospace"> ratio (``alpha``) parameters. The generalized trapezoidal distribution simplifies </font></div>
<div><font face="courier new, monospace"> to the trapezoidal distribution when ``m = n = 2`` and ``alpha = 1``. It further </font></div><div><font face="courier new, monospace"> simplifies to a triangular distribution when ``mode1 == mode2``.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Parameters</font></div><div><font face="courier new, monospace"> ----------</font></div><div><font face="courier new, monospace"> left : scalar</font></div>
<div><font face="courier new, monospace"> Lower limit.</font></div><div><font face="courier new, monospace"> mode1 : scalar</font></div><div><font face="courier new, monospace"> The value where the first peak of the distribution occurs.</font></div>
<div><font face="courier new, monospace"> The value should fulfill the condition ``left <= mode1 <= mode2``.</font></div><div><font face="courier new, monospace"> mode2 : scalar</font></div><div><font face="courier new, monospace"> The value where the first peak of the distribution occurs.</font></div>
<div><font face="courier new, monospace"> The value should fulfill the condition ``mode1 <= mode2 <= right``. </font></div><div><font face="courier new, monospace"> right : scalar</font></div><div>
<font face="courier new, monospace"> Upper limit, should be larger than or equal to `mode2`.</font></div><div><font face="courier new, monospace"> size : int or tuple of ints, optional</font></div><div>
<font face="courier new, monospace"> Output shape. Default is None, in which case a single value is</font></div>
<div><font face="courier new, monospace"> returned.</font></div><div><font face="courier new, monospace"><div> m : scalar, optional</div><div> Growth parameter.</div><div> n : scalar, optional</div>
<div> Decay parameter.</div><div> alpha : scalar, optional</div><div> Boundary ratio parameter.</div></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Returns</font></div>
<div><font face="courier new, monospace"> -------</font></div><div><font face="courier new, monospace"> samples : ndarray or scalar</font></div><div><font face="courier new, monospace"> The returned samples all lie in the interval [left, right].</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Notes</font></div><div><font face="courier new, monospace"> -----</font></div><div><font face="courier new, monospace"> With ``left``, ``mode1``, ``mode2``, ``right``, ``m``, ``n``, and ``alpha`` parametrized as </font></div>
<div><font face="courier new, monospace"> :math:`a, b, c, d, m, n, \\text{ and } \\alpha`, respectively, </font></div><div><font face="courier new, monospace"> the probability density function for the generalized trapezoidal distribution is</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> .. math:: </font></div><div><font face="courier new, monospace"> f{\\scriptscriptstyle X}(x\mid\theta) = \\mathcal{C}(\\Theta) \\times</font></div>
<div><font face="courier new, monospace"> \\begin{cases}</font></div><div><font face="courier new, monospace"> \\alpha \\left(\\frac{x - \\alpha}{b - \\alpha} \\right)^{m - 1}, & \\text{for } a \\leq x < b \\\\</font></div>
<div><font face="courier new, monospace"> (1 - \\alpha) \\left(\frac{x - b}{c - b} \\right) + \\alpha, & \\text{for } b \\leq x < c \\\\</font></div><div><font face="courier new, monospace"> \\left(\\frac{d - x}{d - c} \\right)^{n-1}, & \\text{for } c \\leq x \\leq d</font></div>
<div><font face="courier new, monospace"> \\end{cases}</font></div><div><font face="courier new, monospace"> </font></div><div><font face="courier new, monospace"> with the normalizing constant :math:`\\mathcal{C}(\\Theta)` defined as</font></div>
<div><font face="courier new, monospace"> </font></div><div><font face="courier new, monospace"> ..math::</font></div><div><font face="courier new, monospace"> \\mathcal{C}(\\Theta) = </font></div>
<div><font face="courier new, monospace"> \\frac{2mn}</font></div><div><font face="courier new, monospace"> {2 \\alpha \\left(b - a\\right) n + </font></div><div><font face="courier new, monospace"> \\left(\\alpha + 1 \\right) \\left(c - b \\right)mn +</font></div>
<div><font face="courier new, monospace"> 2 \\left(d - c \\right)m}</font></div><div><font face="courier new, monospace"> </font></div><div><font face="courier new, monospace"> and where the parameter vector :math:`\\Theta = \\{a, b, c, d, m, n, \\alpha \\}, \\text{ } a \\leq b \\leq c \\leq d, \\text{ and } m, n, \\alpha >0`. </font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Similar to the triangular distribution, the trapezoidal distribution may be used where the</font></div><div><font face="courier new, monospace"> underlying distribution is not known, but some knowledge of the limits and</font></div>
<div><font face="courier new, monospace"> mode exists. The trapezoidal distribution generalizes the triangular distribution by allowing </font></div><div><font face="courier new, monospace"> the modal values to be expressed as a range instead of a point estimate. The growth, decay, and </font></div>
<div><font face="courier new, monospace"> boundary ratio parameters of the generalized trapezoidal distribution further allow for non-linear</font></div><div><font face="courier new, monospace"> behavior to be specified.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> References</font></div><div><font face="courier new, monospace"> ----------</font></div><div><font face="courier new, monospace"> .. [1] van Dorp, J. R. and Kotz, S. (2003) Generalized trapezoidal distributions. </font></div>
<div><font face="courier new, monospace"> Metrika. 58(1):85–97. </font></div><div><font face="courier new, monospace"> Preprint available: <a href="http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/Metrika2003VanDorp.pdf">http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/Metrika2003VanDorp.pdf</a></font></div>
<div><font face="courier new, monospace"> .. [2] van Dorp, J. R., Rambaud, S.C., Perez, J. G., and Pleguezuelo, R. H. (2007) </font></div><div><font face="courier new, monospace"> An elicitation proce-dure for the generalized trapezoidal distribution with a uniform central stage. </font></div>
<div><font face="courier new, monospace"> Decision AnalysisJournal. 4:156–166. </font></div><div><font face="courier new, monospace"> Preprint available: <a href="http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/DA2007.pdf">http://www.seas.gwu.edu/~dorpjr/Publications/JournalPapers/DA2007.pdf</a></font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> Examples</font></div><div><font face="courier new, monospace"> --------</font></div><div><font face="courier new, monospace"> Draw values from the distribution and plot the histogram:</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> >>> import matplotlib.pyplot as plt</font></div><div><font face="courier new, monospace"> >>> h = plt.hist(np.random.triangular(0, 0.25, 0.75, 1, 100000), bins=200,</font></div>
<div><font face="courier new, monospace"> ... normed=True)</font></div><div><font face="courier new, monospace"> >>> plt.show()</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">"""<br></font></div></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">I am unsure if NumPy encourages incorporation of new distributions into numpy.random or instead into separate modules, but found the exercise to be helpful regardless.</font></div>
<div><br></div><div><font face="courier new, monospace">Thanks,</font></div><div><font face="courier new, monospace">Jeremy</font></div><div><font face="courier new, monospace"><br></font></div>
</div>