Skip to main content

Easily truncate scipy distributions.

Project description

dist-truncate

PyPI - Version PyPI - License Coverage Status

A small python package for truncating (continuous) scipy distributions.

Installation

From pypi

To install this package from pypi simply run

pip install dist-truncate

From source

To install from source clone this repository and install via pip:

git clone https://github.com/schoennenbeck/dist-truncate.git
cd dist-truncate
pip install .

Usage

Introduction

After installation simply import the package

import dist_truncate

This adds a property truncated to all distributions of type scipy.stats.rv_continuous. The property is itself of type scipy.stats.rv_continuous and works the same way as the distribution that was started with but with two additional shape-arguments trunc_min and trunc_max that can be used to truncated the support of the distribution to the interval trunc_min <= x <= trunc_max.

Note that some distributions (e.g. the normal distribution) already have an explicit truncated version implemented. In this case the explicit version should be used since it is most likely numerically more stable than this generic implementation.

Example

Let us truncate the standard normal distribution to the interval -0.5 <= x <= 2.0. As noted above the truncated normal is already implemented so we can compare the results and make sure that this generic version actually computes the right outputs.

import dist_truncate
from scipy import stats
import numpy as np

Comparing the cummulative density functions at multiple points:

stats.truncnorm.cdf(np.arange(-3, 3, 0.5), -0.5, 2.0)
# Output: array([0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.28631514, 0.57263027, 0.79676594, 0.93411656,
       1.        , 1.        ])
stats.norm.truncated.cdf(np.arange(-3, 3, 0.5), -0.5, 2.0)
# Output: array([0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.28631514, 0.57263027, 0.79676594, 0.93411656,
       1.        , 1.        ])

Comparing the inverse of the cdf:

stats.truncnorm.ppf(np.arange(0, 1, 0.2), -0.5, 2.0)
# Output: array([-0.5       , -0.14519108,  0.19172827,  0.55269814,  1.00897798])
stats.norm.truncated.ppf(np.arange(0, 1, 0.2), -0.5, 2.0)
# Output: array([-0.5       , -0.14519108,  0.19172827,  0.55269814,  1.00897798])

The minimum and maximum for the truncation can also be called as keyword-arguments:

stats.loguniform.truncated.support(1, 100, trunc_max=80, trunc_min=20)
# Output: (20, 80)

Broadcasting of shapes works the same as for all other arguments:

stats.loguniform.truncated.support(
    [1, 50],
    [50, 100],
    trunc_min=20,
    trunc_max=[[30, 40], [60, 100]]
)
# Output: (array([[20, 50], [20, 50]]), array([[ 30,  40], [ 50, 100]]))

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

dist-truncate-0.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

dist_truncate-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file dist-truncate-0.2.0.tar.gz.

File metadata

  • Download URL: dist-truncate-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.12

File hashes

Hashes for dist-truncate-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7171497f71fdf1da242d71962a5c70a9534fd3906348d6ed253d5364882a1286
MD5 1e09b9502497736380b27ee439cb37df
BLAKE2b-256 a2acec081ee073de148c484d878841a325dc1d26a33db919774b66b580ba7389

See more details on using hashes here.

File details

Details for the file dist_truncate-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dist_truncate-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.12

File hashes

Hashes for dist_truncate-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91e310002f90f0024fd7e989baacf6545e8fcba45bb20fec5217c8cb9f506485
MD5 d23eba52c9e752efad84404de18b54f3
BLAKE2b-256 d1ee0f59c8f7bb3a7a7865a304d84fc971f57a405069d90ae199229b81ce1271

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