Skip to main content

Calculate with and analyze probability densities.

Project description

License build-with-hatch

probability_calculator

Calculate with and analyze random variables / probability densities.

Usage

Install the package, e.g.: pip install probability_calculator.

Initialization and plotting

The package provides simple ways to define random variables with discrete outcomes, for instance a die:

from probability_calculator import FairDie

density = FairDie(6) # initialize a fair die with 6 sides
fig, ax = density.plot_outcomes() # plot the outcomes using matplotlib

png

For the general case, the class RandomVariables can be used:

from probability_calculator import RandomVariable
from fractions import Fraction

density = RandomVariable(outcomes=[
    { "value": 0, "p": Fraction(2, 10) },
    { "value": 1, "p": Fraction(3, 10) },
    { "value": Fraction(5, 2), "p": Fraction(1, 10) },
    { "value": 3, "p": Fraction(4, 10) },
]) # initialize a discrete density with 4 different outcomes
fig, ax = density.plot_outcomes() # plot the density using matplotlib

png

Note that only integers and fractions can be used to assure exact calculations.

Combine random variables

The discrete density of throwing a die two times can be modelled by adding the random variable with itself:

density_for_one_throw = FairDie(6)
density_sum_of_two_throws = density_for_one_throw + density_for_one_throw # same as density_for_one_throw*2
fig, ax = density_sum_of_two_throws.plot_outcomes()

png

Note that the operations (+ and *) on random variables are always assuming independed random variables, even when using the same variable multiple times.

Limitations

Continuous densities / random variables are not supported at the moment.

Adding many random variables together might get stuck due to a lot of possible outcomes. In general, adding 10 densities with 10 outcomes each lead to $10^{10}$ outcomes. However, simple cases like the die work, so even Die(10)*100 is no problem.

Contributing

We greatly appreciate fixes and new features for probability_calculator. All contributions to this project should be sent as pull requests on github.

License

Apache License 2.0

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

probability_calculator-0.3.0.tar.gz (59.6 kB view details)

Uploaded Source

Built Distribution

probability_calculator-0.3.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file probability_calculator-0.3.0.tar.gz.

File metadata

File hashes

Hashes for probability_calculator-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e12b06bf6f2120f81e48e531fda8679ef63c0a8b50921bd4aba0d6e77a1e4036
MD5 d84baeab17e4ebed3c576f5783542a78
BLAKE2b-256 554c2c1d06208f240bffdd562935ea61e3b6839d65b79c20dc8eaa7d4b19e1b0

See more details on using hashes here.

File details

Details for the file probability_calculator-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for probability_calculator-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8d285360b7ce54eaafa3eb8b8ecbafbe8d0ab3f673a9d16dc00e6ce93968b8f
MD5 d2b43a9fcc1f0199116324c10fb26553
BLAKE2b-256 e1739fe1e2a24925c862a492ff2dfdcd1262155eb8064981d590072e89d3714c

See more details on using hashes here.

Supported by

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