dhp.math package

Module contents

handy math and statistics routines

Supported Number sets

  • {int} = Set of integers
  • {float} = Set of float
  • {decimal} = Set of Decimal
  • {mixed-float} = {float} + {int}
  • {mixed-decimal} = {decimal} + {int}

Return Type Precedence

The type returned is based on the function, input type(s), The simplest meaningful type is returned.

  • bool
  • int
  • float
  • Decimal
exception dhp.math.MathError

Bases: exceptions.ValueError

general math error

exception dhp.math.UndefinedError

Bases: dhp.math.MathError

When the calculation is undefined for the given input.

dhp.math.choose(n, k)

Return the number of combinations of n choose k (nCk).

\(\dbinom{n}{k} = \dfrac{n!}{k!(n-k)!}\) when \(k<n\) and 0 for \(k>n\)

Parameters:
  • n (int) – the size of the pool of choices
  • k (int) – the size of the sample
Returns:

the number of k-combinations of pool size n

Return type:

(int)

dhp.math.fequal(num1, num2, delta=1e-06)

Compare equivalency of two numbers to a given delta.

Both num1 and num2 must be from the same set of {mixed-float} OR {mixed-decimal}.

\(num1 \equiv num2 \iff |num1 - num2| < delta\)

Parameters:
  • num1 ({mixed-float}|{mixed-decimal}) – The first number to compare.
  • num2 (num1) – The second number to compare.
  • delta (float) – The amount of difference allowed for equivalence. (default: 0.000001)
Returns:

True if the absolute difference between num1 and num2 is

less than delta, else False.

Return type:

(bool)

Raises:

TypeError – If testing a float and a Decimal.

dhp.math.gmean(nums)

Return the geometric mean of the list of numbers.

\(G = (x_1 * x_2 * ... * x_N)^\tfrac{1}{N}\) \(= (\prod_{i=1}^N x_i)^\tfrac{1}{N}\)

Parameters:

nums (list) – list of numbers ({mixed-float}|{mixed-decimal})

Returns:

Geometric Mean of the list.

Return type:

(float|decimal)

Raises:
  • (UndefinedError) – If nums is empty. \(N = 0\)
  • (TypeError) – If nums contains both float and Decimal numbers.
dhp.math.hmean(nums)

Return the harmonic mean of a list of numbers.

\(H = \dfrac{N}{\tfrac{1}{x_1}+\tfrac{1}{x_1}+...+\tfrac{1}{x_N}} =\dfrac{N}{\sum_{i=1}^N \tfrac{1}{x_i}}\)

Parameters:nums (list) – list of numbers ({mixed-float}|{mixed-decimal})
Returns:Harmonic Mean of the list.
Return type:(float|decimal)
Raises:(UndefinedError) – If the list is empty. \(N = 0\)
dhp.math.is_even(num)

Return True if num is even, else False.

An integer is even if it is ‘evenly divisible’ by two.

\(Even = \{ 2k : k \in Z\}\)

Parameters:num (int) – The num to check.
Returns:True if num is even, else False.
Return type:(bool)
Raises:(MathError) – If num is not an integer.
dhp.math.is_odd(num)

rReturn True if num is odd, else False.

An integer is odd if it is not even.

\(Odd = \{ 2k+1 : k \in Z\}\)

A number expressed in the binary is odd if its last digit is 1 and even if its last digit is 0.

Parameters:num (int) – The num to check.
Returns:True if num is odd, else False.
Return type:(bool)
Raises:(MathError) – If num is not an integer.
dhp.math.log_nfactorial(n)

calculate approximation of log n! using Srinivasa Ramanujan’s approximation of log n!

\(\log n! \approx n\log n - n+\dfrac{\log(n(1+4n(1+2n)))} {6}+\dfrac{\log(\pi)}{2}\)

Parameters:n (int) – a very large integer
Returns:log n!
Return type:(float)
dhp.math.mean(nums)

Return the arithmetic mean of the list of numbers

\(\bar{X} = \dfrac{x_1 + x_2 + ... +x_N}{N}\) \(= \dfrac{\sum_{i=1}^N x_i}{N}\)

Parameters:

nums (list) – list of numbers ({mixed-float}|{mixed-Decimal})

Returns:

Arithmetic Mean of the list.

Return type:

(float|decimal)

Raises:
  • (UndefinedError) – If nums is empty. \(N = 0\)
  • (TypeError) – If nums contains both float and Decimal numbers.
dhp.math.median(nums)

Return the median value from the list.

Given: \(a < b < c < d\) The median of the list [a, b, c] is b, and, the median of the list [a, b, c, d] is the mean of b and c; i.e. \(\dfrac{b + c}{2}\)

Parameters:nums (list) – list of numbers ({mixed-float}|{mixed-decimal})
Returns:The median of the list of numbers.
Return type:(int|float|decimal)
dhp.math.mode(lst)

Return the mode (most common element value) from the list.

Parameters:

lst (list) – list of hashable objects to search for the mode.

Returns:

The most common value in lst.

Return type:

(list element)

Raises:
  • (UndefinedError) – If lst is empty.
  • (MathError) – If lst is multi-modal.
dhp.math.prob_unique(nvals, ssize)

return the Probability of Uniqueness given:

where N is nvals and r is ssize:

\(p = \dfrac {N!} {N^r (N-r)!}\)

Parameters:
  • nvals (int) – number of unique points (i.e. birthdays=365)
  • ssize (int) – size of sample (i.e. people)
Returns:

probability that all samples are unique

Return type:

(float)

dhp.math.pstddev(lst)

return the population standard deviation of the elements in the list

dhp.math.pvariance(lst)

return the population variance for the list of numbers

dhp.math.sstddev(lst)

return the sample standard deviation of the elements in the list

dhp.math.svariance(lst)

return the sample population variance for the list of numbers

dhp.math.ttest_independent(lst1, lst2)

calc the ttest for two independent samples