# Quartile

In statistics, quartiles are a type of quantiles which divide the number of data points into four parts, or quarters, of more-or-less equal size. The data must be ordered from smallest to largest to compute quartiles; as such, quartiles are a form of order statistic. The three quartiles, resulting in four data divisions, are as follows:
* The first quartile (Q$_1$) is defined as the 25th percentile where lowest 25% data is below this point. It is also known as the lower quartile.
* The second quartile (Q$_2$) is the median of a data set; thus 50% of the data lies below this point.
* The third quartile (Q$_3$) is the 75th percentile where lowest 75% data is below this point. It is known as the upper quartile, as 75% of the data lies below this point.
Along with the minimum and maximum of the data (which are also quartiles), the three quartiles described above provide a five-number summary of the data. This summary is important in statistics because it provides information about both the center and the spread of the data. Knowing the lower and upper quartile provides information on how big the spread is and if the dataset is skewed toward one side. Since quartiles divide the number of data points evenly, the range is generally not the same between adjacent quartiles (i.e. usually (Q$_3$ - Q$_2$) ≠ (Q$_2$ - Q$_1$)). Interquartile range (IQR) is defined as the difference between the 75th and 25th percentiles or Q$_3$ - Q$_1$. While the maximum and minimum also show the spread of the data, the upper and lower quartiles can provide more detailed information on the location of specific data points, the presence of outliers in the data, and the difference in spread between the middle 50% of the data and the outer data points.

## Definitions

| Symbol | Names | Definition |
| Q$_1$ | First quartile Lower quartile 25th percentile | Splits off the lowest 25% of data from the highest 75% |
| Q$_2$ | Second quartile Median 50th percentile | Cuts data set in half |
| Q$_3$ | Third quartile Upper quartile 75th percentile | Splits off the highest 25% of data from the lowest 75% |

## Computing methods
### Discrete distributions

For discrete distributions, there is no universal agreement on selecting the quartile values.

#### Method 1

* Use the median to divide the ordered data set into two halves. The median becomes the second quartile.
** If there are an odd number of data points in the original ordered data set, do not include the median (the central value in the ordered list) in either half.
** If there are an even number of data points in the original ordered data set, split this data set exactly in half.
* The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data.
This rule is employed by the TI-83 calculator boxplot and "1-Var Stats" functions.

#### Method 2

* Use the median to divide the ordered data set into two halves. The median becomes the second quartile.
** If there are an odd number of data points in the original ordered data set, include the median (the central value in the ordered list) in both halves.
** If there are an even number of data points in the original ordered data set, split this data set exactly in half.
* The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data.
The values found by this method are also known as "Tukey's hinges"; see also midhinge.

#### Method 3

* Use the median to divide the ordered data set into two halves. The median becomes the second quartile.
** If there are odd numbers of data points, then go to the next step.
** If there are even numbers of data points, then the Method 3 starts off the same as the Method 1 or the Method 2 above and you can choose to include or not include the median as a new datapoint. If you choose to include the median as the new datapoint, then proceed to the step 2 or 3 below because you now have an odd number of datapoints. If you do not choose the median as the new data point, then continue the Method 1 or 2 where you have started.
* If there are (4n+1) data points, then the lower quartile is 25% of the nth data value plus 75% of the (n+1)th data value; the upper quartile is 75% of the (3n+1)th data point plus 25% of the (3n+2)th data point.
* If there are (4n+3) data points, then the lower quartile is 75% of the (n+1)th data value plus 25% of the (n+2)th data value; the upper quartile is 25% of the (3n+2)th data point plus 75% of the (3n+3)th data point.

#### Method 4

If we have an ordered dataset $x_{1},x_{2},...,x_{n}$, then we can interpolate between data points to find the $p$th empirical quantile if $x_{i}$ is in the $i/(n+1)$ quantile. If we denote the integer part of a number $a$ by $\lfloor a\rfloor$, then the empirical quantile function is given by,
$q(p/4)=x_{k}+\alpha (x_{k+1}-x_{k})$,
$x_{k}$ is the last data point in quartile p, and $x_{k+1}$ is the first data point in quartile p+1.
$\alpha$ measures where the quartile falls between $x_{k}$ and $x_{k+1}$. If $\alpha$ = 0 then the quartile falls exactly on $x_{k}$. If $\alpha$ = 0.5 then the quartile falls exactly half way between $x_{k}$ and $x_{k+1}$.
$q(p/4)=x_{k}+\alpha (x_{k+1}-x_{k})$,
where $k=\lfloor p(n+1)/4\rfloor$ and $\alpha =p(n+1)/4-\lfloor p(n+1)/4\rfloor$.
To find the first, second, and third quartiles of the dataset we would evaluate $q(0.25)$, $q(0.5)$, and $q(0.75)$ respectively.

#### Example 1

Ordered Data Set (of an odd number of data points): 6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49.
The bold number (40) is the median splitting the data set into two halves with equal number of data points.
| | Method 1 | Method 2 | Method 3 | Method 4 |
| Q$_1$ | 15 | 25.5 | 20.25 | 15 |
| Q$_2$ | 40 | 40 | 40 | 40 |
| Q$_3$ | 43 | 42.5 | 42.75 | 43 |

#### Example 2

Ordered Data Set (of an even number of data points): 7, 15, 36, 39, 40, 41.
The bold numbers (36, 39) are used to calculate the median as their average. As there are an even number of data points, the first three methods all give the same results. (The Method 3 is executed such that the median is not chosen as a new data point and the Method 1 started.)
| | Method 1 | Method 2 | Method 3 | Method 4 |
| Q$_1$ | 15 | 15 | 15 | 13 |
| Q$_2$ | 37.5 | 37.5 | 37.5 | 37.5 |
| Q$_3$ | 40 | 40 | 40 | 40.25 |

### Continuous probability distributions

If we define a continuous probability distributions as $P(X)$ where $X$ is a real valued random variable, its cumulative distribution function (CDF) is given by
$F_{X}(x)=P(X\leq x)$.
The CDF gives the probability that the random variable $X$ is less than or equal to the value $x$. Therefore, the first quartile is the value of $x$ when $F_{X}(x)=0.25$, the second quartile is $x$ when $F_{X}(x)=0.5$, and the third quartile is $x$ when $F_{X}(x)=0.75$. The values of $x$ can be found with the quantile function $Q(p)$ where $p=0.25$ for the first quartile, $p=0.5$ for the second quartile, and $p=0.75$ for the third quartile. The quantile function is the inverse of the cumulative distribution function if the cumulative distribution function is monotonically increasing because the one-to-one correspondence between the input and output of the cumulative distribution function holds.

## Outliers

There are methods by which to check for outliers in the discipline of statistics and statistical analysis. Outliers could be a result from a shift in the location (mean) or in the scale (variability) of the process of interest. Outliers could also be evidence of a sample population that has a non-normal distribution or of a contaminated population data set. Consequently, as is the basic idea of descriptive statistics, when encountering an outlier, we have to explain this value by further analysis of the cause or origin of the outlier. In cases of extreme observations, which are not an infrequent occurrence, the typical values must be analyzed. The Interquartile Range (IQR), defined as the difference between the upper and lower quartiles (Q$_3$−Q$_1${\textstyle Q_{3}-Q_{1}}), may be used to characterize the data when there may be extremities that skew the data; the interquartile range is a relatively robust statistic (also sometimes called "resistance") compared to the range and standard deviation. There is also a mathematical method to check for outliers and determining "fences", upper and lower limits from which to check for outliers.
After determining the first (lower) and third (upper) quartiles (Q$_1${\textstyle Q_{1}} and Q$_3${\textstyle Q_{3}} respectively) and the interquartile range (IQR=Q$_3$−Q$_1${\textstyle {\textrm {IQR}}=Q_{3}-Q_{1}}) as outlined above, then fences are calculated using the following formula:
    ${\text{Lower fence}}=Q_{1}-(1.5\times \mathrm {IQR} )$
    ${\text{Upper fence}}=Q_{3}+(1.5\times \mathrm {IQR} )$Boxplot Diagram with Outliers
The lower fence is the "lower limit" and the upper fence is the "upper limit" of data, and any data lying outside these defined bounds can be considered an outlier. The fences provide a guideline by which to define an outlier, which may be defined in other ways. The fences define a "range" outside which an outlier exists; a way to picture this is a boundary of a fence. It is common for the lower and upper fences along with the outliers to be represented by a boxplot. For the boxplot shown on the right, only the vertical heights correspond to the visualized data set while horizontal width of the box is irrelevant. Outliers located outside the fences in a boxplot can be marked as any choice of symbol, such as an "x" or "o". The fences are sometimes also referred to as "whiskers" while the entire plot visual is called a "box-and-whisker" plot.
When spotting an outlier in the data set by calculating the interquartile ranges and boxplot features, it might be easy to mistakenly view it as evidence that the population is non-normal or that the sample is contaminated. However, this method should not take place of a hypothesis test for determining normality of the population. The significance of the outliers varies depending on the sample size. If the sample is small, then it is more probable to get interquartile ranges that are unrepresentatively small, leading to narrower fences. Therefore, it would be more likely to find data that are marked as outliers.

## Computer software for quartiles

| Environment | Function | Quartile Method |
| Microsoft Excel | QUARTILE.EXC | Method 4 |
| Microsoft Excel | QUARTILE.INC | Method 3 |
| TI-8X series calculators | 1-Var Stats | Method 1 |
| R | fivenum | Method 2 |
| R | quantile (default) | Method 4 |
| Python | numpy.percentile | Method 4 (with n−1) |
| Python | pandas.DataFrame.describe | Method 3 |

### Excel

The Excel function QUARTILE.INC(array, quart) provides the desired quartile value for a given array of data, using Method 3 from above. The QUARTILE function is a legacy function from Excel 2007 or earlier, giving the same output of the function QUARTILE.INC. In the function, array is the dataset of numbers that is being analyzed and quart is any of the following 5 values depending on which quartile is being calculated.
| Quart | Output QUARTILE Value |
| 0 | Minimum value |
| 1 | Lower Quartile (25th percentile) |
| 2 | Median |
| 3 | Upper Quartile (75th percentile) |
| 4 | Maximum value |

### MATLAB

In order to calculate quartiles in Matlab, the function quantile(A,p) can be used. Where A is the vector of data being analyzed and p is the percentage that relates to the quartiles as stated below.
| p | Output QUARTILE Value |
| 0 | Minimum value |
| 0.25 | Lower Quartile (25th percentile) |
| 0.5 | Median |
| 0.75 | Upper Quartile (75th percentile) |
| 1 | Maximum value |