Quartiles of Data Set

quartile_value(data, q)

Determines the first, second, or third quartile values of a data set

Parameters
  • data (list of int or float) – List of numbers to analyze

  • q (int) – Number determining which quartile to provide

Raises
  • TypeError – First argument must be a 1-dimensional list

  • TypeError – Elements of first argument must be integers or floats

  • ValueError – Second argument must be an integer contained within the set [1, 2, 3]

Returns

quartile – Quartile value of the data set

Return type

int or float

Notes

  • Ordered set of numbers: \(a_i = ( a_1, a_2, \cdots, a_n )\)

  • For sets with an odd amount of numbers:

    • First quartile: \(Q_1 = a_{\lceil n/4 \rceil}\)

    • Second quartile: \(Q_2 = a_{\lceil n/2 \rceil}\)

    • Third quartile: \(Q_3 = a_{\lceil 3n/4 \rceil}\)

  • For sets with an even amount of numbers:

    • If \(n \text{ mod } 4 \neq 0\):

      • First quartile: \(Q_1 = a_{\lceil n/4 \rceil}\)

      • Second quartile: \(Q_2 = \frac{a_{n/2} + a_{n/2 + 1}}{2}\)

      • Third quartile: \(Q_3 = a_{\lceil 3n/4 \rceil}\)

    • If \(n \text{ mod } 4 = 0\):

      • First quartile: \(Q_1 = \frac{a_{n/4} + a_{n/4 + 1}}{2}\)

      • Second quartile: \(Q_2 = \frac{a_{n/2} + a_{n/2 + 1}}{2}\)

      • Third quartile: \(Q_3 = \frac{a_{3n/4} + a_{3n/4 + 1}}{2}\)

  • Quartiles

Examples

Import quartile_value function from regressions library
>>> from regressions.statistics.quartiles import quartile_value
Determine the first quartile of the set [21, 53, 3, 68, 43, 9, 72, 19, 20, 1]
>>> quartile_1 = quartile_value([21, 53, 3, 68, 43, 9, 72, 19, 20, 1], 1)
>>> print(quartile_1)
9
Determine the third quartile of the set [12, 81, 13, 8, 42, 72, 91, 20, 20]
>>> quartile_3 = quartile_value([12, 81, 13, 8, 42, 72, 91, 20, 20], 3)
>>> print(quartile_3)
76.5