# scipy.stats.triang¶

scipy.stats.triang = <scipy.stats._continuous_distns.triang_gen object>[source]

A triangular continuous random variable.

As an instance of the rv_continuous class, triang object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.

Notes

The triangular distribution can be represented with an up-sloping line from loc to (loc + c*scale) and then downsloping for (loc + c*scale) to (loc+scale).

triang takes $$c$$ as a shape parameter.

The probability density above is defined in the “standardized” form. To shift and/or scale the distribution use the loc and scale parameters. Specifically, triang.pdf(x, c, loc, scale) is identically equivalent to triang.pdf(y, c) / scale with y = (x - loc) / scale.

The standard form is in the range [0, 1] with c the mode. The location parameter shifts the start to loc. The scale parameter changes the width from 1 to scale.

Examples

>>> from scipy.stats import triang
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1, 1)


Calculate a few first moments:

>>> c = 0.158
>>> mean, var, skew, kurt = triang.stats(c, moments='mvsk')


Display the probability density function (pdf):

>>> x = np.linspace(triang.ppf(0.01, c),
...                 triang.ppf(0.99, c), 100)
>>> ax.plot(x, triang.pdf(x, c),
...        'r-', lw=5, alpha=0.6, label='triang pdf')


Alternatively, the distribution object can be called (as a function) to fix the shape, location and scale parameters. This returns a “frozen” RV object holding the given parameters fixed.

Freeze the distribution and display the frozen pdf:

>>> rv = triang(c)
>>> ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')


Check accuracy of cdf and ppf:

>>> vals = triang.ppf([0.001, 0.5, 0.999], c)
>>> np.allclose([0.001, 0.5, 0.999], triang.cdf(vals, c))
True


Generate random numbers:

>>> r = triang.rvs(c, size=1000)


And compare the histogram:

>>> ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
>>> ax.legend(loc='best', frameon=False)
>>> plt.show() Methods

 rvs(c, loc=0, scale=1, size=1, random_state=None) Random variates. pdf(x, c, loc=0, scale=1) Probability density function. logpdf(x, c, loc=0, scale=1) Log of the probability density function. cdf(x, c, loc=0, scale=1) Cumulative distribution function. logcdf(x, c, loc=0, scale=1) Log of the cumulative distribution function. sf(x, c, loc=0, scale=1) Survival function (also defined as 1 - cdf, but sf is sometimes more accurate). logsf(x, c, loc=0, scale=1) Log of the survival function. ppf(q, c, loc=0, scale=1) Percent point function (inverse of cdf — percentiles). isf(q, c, loc=0, scale=1) Inverse survival function (inverse of sf). moment(n, c, loc=0, scale=1) Non-central moment of order n stats(c, loc=0, scale=1, moments=’mv’) Mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’). entropy(c, loc=0, scale=1) (Differential) entropy of the RV. fit(data, c, loc=0, scale=1) Parameter estimates for generic data. expect(func, args=(c,), loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds) Expected value of a function (of one argument) with respect to the distribution. median(c, loc=0, scale=1) Median of the distribution. mean(c, loc=0, scale=1) Mean of the distribution. var(c, loc=0, scale=1) Variance of the distribution. std(c, loc=0, scale=1) Standard deviation of the distribution. interval(alpha, c, loc=0, scale=1) Endpoints of the range that contains alpha percent of the distribution

#### Previous topic

scipy.stats.trapz

#### Next topic

scipy.stats.truncexpon
{"ft_lang_label":"__label__en","ft_lang_prob":0.55788827,"math_prob":0.99738467,"size":3467,"snap":"2023-40-2023-50","text_gpt3_token_len":1004,"char_repetition_ratio":0.19780537,"word_repetition_ratio":0.012658228,"special_character_ratio":0.2976637,"punctuation_ratio":0.23385014,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99936837,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-02T08:23:35Z\",\"WARC-Record-ID\":\"<urn:uuid:397ad7ea-5f8c-42a1-a501-20c2dac5fd42>\",\"Content-Length\":\"18729\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c8bbdd3b-9e49-4f69-b2d2-985803239963>\",\"WARC-Concurrent-To\":\"<urn:uuid:b46e4f93-ccf0-4f22-a928-4d4214e54e83>\",\"WARC-IP-Address\":\"50.17.248.72\",\"WARC-Target-URI\":\"https://docs.scipy.org/doc/scipy-1.1.0/reference/generated/scipy.stats.triang.html\",\"WARC-Payload-Digest\":\"sha1:R7IMYEN5AMWN3ACE22HUUX7HF4N7UH7P\",\"WARC-Block-Digest\":\"sha1:O5YQN5JEG52MPCQE7HH2YDBZUE6T4FVF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510983.45_warc_CC-MAIN-20231002064957-20231002094957-00078.warc.gz\"}"}