Skip to content

TrulsHenriksson/ProbabilityDistributionFunction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

ProbabilityDistributionFunction

A class representing an integer probability distribution function (PDF) of a random variable.

Provides methods to add, subtract, multiply, divide, and much more between instances of PDFs, as well as visualizing the result as a bar chart.

Sum of two six-sided dice

Notice that the sum of two D6 (six-sided dice) is not equal to rolling one D6 and doubling the result.

D6 times two

Mathematical background

A discrete Probability Distribution Function (PDF) in mathematics is a function $f$ where $f(x)$ is zero everywhere except for on a countable set $S\subset\mathbb{R}$ and has total weight $1$, i.e. $$ \sum_{x\in S} f(x) = 1. $$ This can be interpreted as $f(x)$ being the probability that some underlying random process (a random variable) takes on the value $x$. For example, a (perfect) six-sided die has the PDF $$ f_{\text{D6}}(x) = \begin{cases} 1/6 & \text{if } x \in S={1, 2, 3, 4, 5, 6}, \ 0 & \text{otherwise.} \end{cases} $$ The set $S$ is the support of $f$.

In the real world, we are often interested in the sum (or other combination) of two (or more) random variables. For example, if $A$ is one D6 (six-sided die) and $B$ is another, what is the distribution of $A+B$? For example, what's the probability of $A+B=4$? Well, there are three combinations of values for $A$ and $B$ whose sum is $4$, namely $(1, 3)$, $(2, 2)$, and $(3, 1)$. Each of these happen with probability $\frac{1}{6}\cdot\frac{1}{6}$, so the total probability is $3\cdot\frac{1}{6}\cdot\frac{1}{6}=1/12$. In general, the probability of $A+B=x$ is $$ f_{A+B}(x) = \sum_{a\in S_A}\ \sum_{b\in S_B:; a+b=x} f_A(a)\cdot f_B(b). $$ This generalizes to other binary operations as well.

The PDF class

A PDF is an object with just two member attributes: a dictionary of outcomes {value: weight, ...} and an integer total. All values are integers, and the probabilities are represented by an integer weight, as a fraction of total.

For example, PDF([1, 2, 3], [1, 1, 4]) represents a three-sided die (D3) where you are four times as likely to get a three compared to a two or a three. This is the same as a six-sided die with sides labeled 1, 2, 3, 3, 3, 3, which means the same PDF is obtained via PDF([1, 2, 3, 3, 3, 3]).

Initializing

The following are all equivalent:

PDF({1: 1, 2: 1, 3: 4})
PDF([1, 2, 3], [1, 1, 4])  # This one is returned by __repr__
PDF([1, 2, 3, 3, 3, 3], [1, 1, 1, 1, 1, 1])
PDF([1, 2, 3, 3, 3, 3])

Methods

The following operations and methods are supported:

  • Binary operations:
    • Arithmetic operations: __add__, __sub__, __mul__, __floordiv__, __mod__, __pow__, and all __r variants
    • Bit shift operations: __lshift__, __rshift__, __rlshift__, __rrshift__
    • Bitwise operations: __or__, __and__, __xor__, and all __r variants
    • Comparison operations: __eq__, __ne__, __lt__, __le__, __gt__, __ge__
    • Max/min operations: max, min
    • Repeated sum: summed
  • Unary operations:
    • Sign change operations: __abs__, __pos__, __neg__
    • Casts to "booleans": bool_, not_
    • Bitwise not: __invert__
  • Statistical properties:
    • mean, median, mode, equivalent
  • Evaluation, sampling, introspection, plotting:
    • __call__, sample, as_lists, plot

Usage

If all you want is to explore the look of different probability distributions (perhaps for a new TTRPG system), there is a simple CLI (command-line interface) implemented as well. Where you would normally write in Python:

>>> A = d(20)
>>> B = d(12)
>>> (A % B + 1).plot()

you can instead type in the terminal:

$ python ./pdf.py "D20 % D12 + 1"

The shorthand [Dd]{n} is expanded to d({n}) and the resulting PDF is plotted. In the latter case, a title is also added with the input expression:

D20 mod D12 plus one

More examples

2D20 mod 2D10:

Middle of 3D20:

Second-smallest of 4D6:

Really uneven distribution:

The same as above, summed twice:

After three sums, it clearly approaches a normal distribution:

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages