Skip to main content

Implementation of the Irwin-Hall (the uniform sum) distribution

Project description

UniformSumDistribution

A implementation of the Irwin-Hall (the uniform sum) distribution - https://randomservices.org/random/special/IrwinHall.html

How to use

Install it from pip (depends on scipy and numpy)

pip install UniformSumDistribution

The package provides one class called UniformSumDistribution, which implements the distribution.

from UniformSumDistribution import UniformSumDistribution

distribution = UniformSumDistribution(n)

the distribution object has methods:

  • rvs(size=1, *args, **kwds) - Random variates of given type.
  • pdf(x, *args, **kwds) - Probability density function at x of the given RV.
  • logpdf(x, *args, **kwds) - Log of the probability density function at x of the given RV.
  • cdf(x, *args, **kwds) - Cumulative distribution function of the given RV.
  • logcdf(x, *args, **kwds) - Log of the cumulative distribution function at x of the given RV.
  • sf(x, *args, **kwds) - Survival function (1 - cdf) at x of the given RV.
  • logsf(x, *args, **kwds) - Log of the survival function of the given RV.
  • ppf(q, *args, **kwds) - Percent point function (inverse of cdf) at q of the given RV.
  • isf(q, *args, **kwds) - Inverse survival function (inverse of sf) at q of the given RV.
  • moment(n, *args, **kwds) - n-th order non-central moment of distribution.
  • stats(*args, **kwds) - Some statistics of the given RV.
  • entropy(*args, **kwds) - Differential entropy of the RV.
  • expect([func, args, loc, scale, lb, ub, …]) - Calculate expected value of a function with respect to the distribution by numerical integration.
  • median(*args, **kwds) - Median of the distribution.
  • mean(*args, **kwds) - Mean of the distribution.
  • std(*args, **kwds) - Standard deviation of the distribution.
  • var(*args, **kwds) - Variance of the distribution.
  • interval(alpha, *args, **kwds) - Confidence interval with equal areas around the median.
  • __call__(*args, **kwds) - Freeze the distribution for the given arguments.
  • fit(data, *args, **kwds) - Return estimates of shape (if applicable), location, and scale parameters from data.
  • fit_loc_scale(data, *args) - Estimate loc and scale parameters from data using 1st and 2nd moments.
  • nnlf(theta, x) - Negative loglikelihood function.
  • support(*args, **kwargs) - Support of the distribution.

Read more

Usage example

size = 8196
# Build PDF
plt.figure(figsize=(10, 6))
for n in range(1, 14):
    distribution = UniformSumDistribution(n)

    start = distribution.ppf(0.0001)
    end = distribution.ppf(0.9999)

    x = np.linspace(start, end, size)
    y = distribution.pdf(x)
    pdf = pd.Series(y, x)

    ax = pdf.plot(kind = 'line', label = 'n=%s' % n, legend = True, lw = 2)
    ax.set_title('PDF')

the result of the code above

# Build CDF
plt.figure(figsize=(10, 6))
for n in range(1, 14):
    distribution = UniformSumDistribution(n)

    start = distribution.ppf(0.0001)
    end = distribution.ppf(0.9999)

    x = np.linspace(start, end, size)
    y = distribution.cdf(x)
    cdf = pd.Series(y, x)

    ax = cdf.plot(kind = 'line', label = 'n=%s' % n, legend = True, lw = 2)
    ax.set_title('CDF')

the result of the code above

distribution = UniformSumDistribution(n = 2)
mean, variance, skew, kurtosis = distribution.stats(moments = 'mvsk')
mean, variance, skew, kurtosis

[out]: (array(1.), array(0.16666667), array(0.), array(-0.6))

size = 5000
bins = 32
n = 2
distribution = UniformSumDistribution(n = n)

# Get start and end points of distribution
start = distribution.ppf(0.0001)
end = distribution.ppf(0.9999)

# Build PDF
x = np.linspace(start, end, size)
y = distribution.pdf(x)
pdf = pd.Series(y, x)

# Create random data
rv = pd.Series(distribution.rvs(size = size))

# Get histogram of random data
b = np.linspace(start, end, bins + 1)
y, x = np.histogram(rv, bins = b, density = True)
x = [(a + x[i + 1]) / 2.0 for i, a in enumerate(x[0:-1])]
hist = pd.Series(y, x)

w = abs(abs(hist.index[0]) - abs(hist.index[1]))

plt.figure(figsize=(10, 6))
ax = pdf.plot(kind = 'line', label = 'PDF', legend = True, lw = 2, color = 'r')
ax.bar(hist.index, hist.values, label = 'Random Sample', width = w, alpha = 0.5, color = 'c')
legend = plt.legend()

the result of the code above

License

Copyright (c) 2022 Artyom Zolotarevskiy.

UniformSumDistribution is free software made available under the MIT License. For details see the LICENSE file.

License MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

UniformSumDistribution-1.0.3.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

UniformSumDistribution-1.0.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file UniformSumDistribution-1.0.3.tar.gz.

File metadata

  • Download URL: UniformSumDistribution-1.0.3.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for UniformSumDistribution-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ba4321ece4582f726c853ef0b1787e0531b6e99af478e6ca2e9cb610c1ebfae2
MD5 c5524c09ab8c1888d184b9ae851e6540
BLAKE2b-256 8ae5ab290c947c17b7c769b4100b25e5c5385199f1689f44e7e34dce7a815c41

See more details on using hashes here.

File details

Details for the file UniformSumDistribution-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for UniformSumDistribution-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0e73f298ec31c736ff4d31f20fd5952fb429cdbda804741cdbe726a4dcdb7b0c
MD5 acd2e7abaa754ce38fd268ac014e3a03
BLAKE2b-256 e2fbd9b92b0789151c8494ce4aaf70f38384ccb82cc411a36c7c8c91b739ed58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page