Deviation

Overview

Deviation is a statistical concept that measures the variability or volatility of data points in a dataset. It is a crucial tool in data analysis, helping to understand how much individual data points differ from the average value. This measure is particularly useful in time series analysis, where it helps to assess the variability of data points over time. Standard deviation, a common measure of deviation, quantifies the average amount that values deviate from the mean, providing insights into the dispersion or spread of the dataset. A low standard deviation indicates that data points are close to the mean, suggesting calm and orderly data, while a high standard deviation indicates more volatility and spread.

Standard Deviation Calculation

To compute the standard deviation, we can use a function that builds on the average function. Below is a JavaScript implementation of a standard deviation function, which is part of a statistics toolkit:

Listing 9.5 A standard deviation function for our toolkit (toolkit/statistics.js)

// ... sum and average functions omitted ...

function std (values) {    #1  
 const avg = average(values);    #2  
 const squaredDiffsFromAvg = values    #3  
 .map(v => Math.pow(v – avg, 2))    #3  
 const avgDiff = average(squaredDiffsFromAvg);    #4  
 return Math.sqrt(avgDiff);    #5  
}

module.exports = {
    sum: sum,
    average: average,
    std: std,
};

#1   This is a toolkit function to compute the standard deviation for a set of values.
#2   Computes the average of the values
#3   Computes the squared difference from the average for each value
#4   Averages the squared differences
#5   Takes the square root and we have our standard deviation

Example: Monthly Rainfall Standard Deviation

An example of using the standard deviation function is to compute the standard deviation of monthly rainfall data for a specific year, such as 2016. This can help in understanding the variability in rainfall over the months.

Listing 9.6 Computing standard deviation of monthly rainfall for 2016 (listing-9.6.js)

const std = require('./toolkit/statistics.js').std;

const monthlyRainfall = [
    // ... hard-coded data omitted ...
];

const monthlyRainfallStdDeviation = std(monthlyRainfall);    #1  
console.log("Monthly rainfall standard deviation: " + ➥monthlyRainfallStdDeviation + "mm");

#1   Computes the standard deviation of monthly rainfall for 2016

Running this code should yield a standard deviation of approximately 40.92 mm, indicating the extent of variability in monthly rainfall for that year.

Rolling Standard Deviation

In time series analysis, a rolling standard deviation can be used to measure the variability of data points over a specified period. This approach helps visualize fluctuations over time, providing insights into trends and patterns. For instance, a rolling standard deviation of temperature data can reveal periods of increased or decreased variability.

Figure 9.11 Twenty-year rolling standard deviation of NYC temperatures Figure 9.11 Twenty-year rolling standard deviation of NYC temperatures

Figure 9.11 illustrates the twenty-year rolling standard deviation of NYC temperatures, showing how temperature fluctuations have changed over time. This visualization indicates that temperature variability declined and stabilized in the 1960s but has been on the rise since the 1970s, suggesting potential for more extreme fluctuations in the future.

FAQ (Frequently asked questions)