# Working with Statistics: Calculating Descriptive Statistics

Ahoy there, mateys! If you’re sailing the seas of data analysis, you’re going to need to know how to calculate descriptive statistics. That’s right, we’re talking about the basic measures that help you understand the distribution of your data, such as the mean, median, and standard deviation. But fear not, with Apache Commons Math, you won’t have to chart a course through the treacherous waters of manual calculations. Let’s set sail and explore how to use this library for calculating descriptive statistics.

## Getting Started

Before we dive into the code, let’s make sure we have our compass set correctly. You’ll need to have Apache Commons Math installed and set up in your Java environment. If you’re not sure how to do that, check out the “Apache Commons Math Installation” section of this website.

Once you have Apache Commons Math ready to go, you’ll need to import the appropriate classes in your code. In this case, we’ll need the `DescriptiveStatistics`

class. Here’s an example of how to import it:

```
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
```

## Mean, Median, and Standard Deviation

Now that we’ve got our bearings, let’s start calculating some descriptive statistics! The first measures we’ll explore are the mean, median, and standard deviation. The mean, or average, is the sum of all values in a dataset divided by the number of values. The median is the middle value of a dataset when it’s sorted in order. The standard deviation is a measure of the spread of the dataset around the mean.

To calculate these measures using Apache Commons Math, we’ll create a new instance of the `DescriptiveStatistics`

class and add our data to it. Here’s an example:

```
double[] data = {1.0, 2.0, 3.0, 4.0, 5.0};
DescriptiveStatistics stats = new DescriptiveStatistics();
for (double d : data) {
stats.addValue(d);
}
double mean = stats.getMean();
double median = stats.getPercentile(50);
double std = stats.getStandardDeviation();
```

In this example, we have a dataset of five values: 1.0, 2.0, 3.0, 4.0, and 5.0. We create a new instance of `DescriptiveStatistics`

and add each value to it using a loop. Then, we use the `getMean()`

method to calculate the mean, the `getPercentile(50)`

method to calculate the median (since it’s the 50th percentile), and the `getStandardDeviation()`

method to calculate the standard deviation.

## Quartiles and Range

Another set of descriptive statistics that can be helpful to calculate are quartiles and the range. Quartiles divide a dataset into four equal parts, with the first quartile (Q1) being the value that’s 25% of the way through the sorted dataset, the second quartile (Q2) being the median, and the third quartile (Q3) being the value that’s 75% of the way through the sorted dataset. The range is simply the difference between the maximum and minimum values in the dataset.

To calculate quartiles and range with Apache Commons Math, we’ll use the `getPercentile()`

method again. Here’s an example:

```
double[] data = {1.0, 2.0, 3.0, 4.0, 5.0};
DescriptiveStatistics stats = new DescriptiveStatistics();
for (double d : data) {
stats.addValue(d); } double q1 = stats.getPercentile(25); double median = stats.getPercentile(50); double q3 = stats.getPercentile(75); double range = stats.getMax() - stats.getMin();
In this example, we use the same dataset as before and add it to a new instance of `DescriptiveStatistics`. Then, we use the `getPercentile()` method with the argument of 25 to calculate Q1, 50 to calculate the median, and 75 to calculate Q3. We also use the `getMax()` and `getMin()` methods to calculate the range by subtracting the minimum value from the maximum value.
## Skewness and Kurtosis
Skewness and kurtosis are two measures of the shape of a distribution. Skewness measures the degree of asymmetry in the distribution, with positive skewness indicating a tail that extends to the right and negative skewness indicating a tail that extends to the left. Kurtosis measures the degree of peakedness in the distribution, with high kurtosis indicating a sharp peak and low kurtosis indicating a flat peak.
To calculate skewness and kurtosis with Apache Commons Math, we'll again use the `DescriptiveStatistics` class. Here's an example:
```java
double[] data = {1.0, 2.0, 3.0, 4.0, 5.0};
DescriptiveStatistics stats = new DescriptiveStatistics();
for (double d : data) {
stats.addValue(d);
}
double skewness = stats.getSkewness();
double kurtosis = stats.getKurtosis();
```

In this example, we add our dataset to a new instance of `DescriptiveStatistics`

. Then, we use the `getSkewness()`

method to calculate the skewness and the `getKurtosis()`

method to calculate the kurtosis.

## Wrapping Up

Well done, mateys! We’ve sailed through the choppy waters of calculating descriptive statistics with Apache Commons Math. We’ve learned how to calculate the mean, median, and standard deviation, as well as quartiles, range, skewness, and kurtosis. But wait, there’s more! In the next section, we’ll explore how to use Apache Commons Math for inferential statistics and working with statistical distributions. So, batten down the hatches and let’s set sail!

## Performing Inferential Statistics

Now that we’ve covered calculating descriptive statistics, let’s set our sights on inferential statistics. Inferential statistics use a sample of data to make inferences or predictions about a larger population. This can include hypothesis testing, confidence intervals, and regression analysis. Apache Commons Math provides a range of tools for performing these kinds of statistical analyses.

### Hypothesis Testing

Hypothesis testing is a way to determine whether a hypothesis about a population is likely to be true or not. It involves formulating a null hypothesis, which is the assumption that there’s no difference between the sample and the population, and an alternative hypothesis, which is the assumption that there is a difference. Then, we collect data from a sample and use statistical tests to determine whether the null hypothesis can be rejected or not.

To perform hypothesis testing in Apache Commons Math, we can use the `TTest`

class. Here’s an example:

```
double[] sample = {1.0, 2.0, 3.0, 4.0, 5.0};
double populationMean = 3.0;
TTest tTest = new TTest();
double pValue = tTest.tTest(populationMean, sample);
```

In this example, we have a sample of data and a hypothesized population mean of 3.0. We create a new instance of `TTest`

and use the `tTest()`

method to calculate the p-value, which is the probability of obtaining a sample mean as extreme or more extreme than the one we observed, assuming the null hypothesis is true. If the p-value is less than a predetermined significance level (such as 0.05), we can reject the null hypothesis.

### Confidence Intervals

A confidence interval is a range of values that we can be reasonably confident contains the true value of a population parameter. For example, if we want to estimate the mean weight of all sea turtles in the world, we might take a sample of sea turtles and calculate a confidence interval for the mean weight. A 95% confidence interval means that if we took many different samples of the same size, we would expect the true population mean to fall within the interval in 95% of cases.

To calculate confidence intervals in Apache Commons Math, we can use the `ConfidenceInterval`

class. Here’s an example:

```
double[] sample = {1.0, 2.0, 3.0, 4.0, 5.0};
ConfidenceInterval ci = new ConfidenceInterval(sample, 0.05);
double lower = ci.getLowerBound();
double upper = ci.getUpperBound();
```

In this example, we have a sample of data and a desired confidence level of 95% (which corresponds to a significance level of 0.05). We create a new instance of `ConfidenceInterval`

and use the `getLowerBound()`

and `getUpperBound()`

methods to calculate the lower and upper bounds of the confidence interval.

### Regression Analysis

Regression analysis is a way to model the relationship between two or more variables. It can be used for prediction, identifying important predictors, and understanding the direction and strength of relationships. There are many different types of regression analyses, such as linear regression, logistic regression, and Poisson regression.

To perform regression analysis in Apache Commons Math, we can use the `RegressionResults`

class. Here’s an example:

```
double[] x = {1.0, 2.0, 3.0, 4.0, 5.0};
double[] y = {2.0, 4.0, 6.0, 8.0, 10.0};
SimpleRegression regression =new SimpleRegression();
for (int i = 0; i < x.length; i++) {
regression.addData(x[i], y[i]);
}
RegressionResults results = regression.regress();
double slope = results.getParameterEstimate(1);
double intercept = results.getParameterEstimate(0);
```

In this example, we have two arrays of data, `x`

and `y`

, that represent a linear relationship. We create a new instance of `SimpleRegression`

and add each data point using the `addData()`

method. Then, we use the `regress()`

method to calculate the regression results, which include the slope and intercept of the regression line.

## Working with Statistical Distributions

Statistical distributions are mathematical functions that describe the probabilities of different outcomes in a population or sample. There are many different types of distributions, such as normal distributions, Poisson distributions, and binomial distributions. Understanding the properties of these distributions is important for many statistical analyses.

Apache Commons Math provides a range of classes for working with statistical distributions. Here are some examples:

### Normal Distribution

The normal distribution is one of the most commonly used distributions in statistics. It’s a continuous distribution that’s symmetrical and bell-shaped. The mean and standard deviation of a normal distribution determine its shape and position.

To work with normal distributions in Apache Commons Math, we can use the `NormalDistribution`

class. Here’s an example:

```
double mean = 5.0;
double sd = 2.0;
NormalDistribution normal = new NormalDistribution(mean, sd);
double probability = normal.cumulativeProbability(7.0);
```

In this example, we create a new instance of `NormalDistribution`

with a mean of 5.0 and a standard deviation of 2.0. We use the `cumulativeProbability()`

method to calculate the probability of observing a value of 7.0 or less in this distribution.

### Poisson Distribution

The Poisson distribution is a discrete distribution that’s often used to model the number of times an event occurs in a fixed interval of time or space. It has a single parameter, the mean, which determines its shape.

To work with Poisson distributions in Apache Commons Math, we can use the `PoissonDistribution`

class. Here’s an example:

```
double mean = 2.0;
PoissonDistribution poisson = new PoissonDistribution(mean);
int probability = poisson.probability(3);
```

In this example, we create a new instance of `PoissonDistribution`

with a mean of 2.0. We use the `probability()`

method to calculate the probability of observing a value of 3 in this distribution.

## Conclusion

We’ve explored how to use Apache Commons Math for calculating descriptive statistics, performing inferential statistics, and working with statistical distributions. With the tools provided by this library, you can navigate the choppy waters of statistical analysis with ease. Keep practicing and exploring the different functions and classes that Apache Commons Math has to offer, and you’ll be a master of statistical analysis in no time. Until then, happy coding, and may the wind always be at your back!