# Using Pythagorean Theorem to Calculate the RMS of a Time Series Data Set

Pythagorean Theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. It is used to calculate the length of the opposite side (hypotenuse) of a right (90-degree) triangle. It is also used in many other situations, for example, to calculate the distance between two points in space. Distance calculation in navigation is a common example.

However, what you don’t learn in basic math, is that Pythagorean Theorem works in more than two dimensions. In fact, it works in any number of dimensions. It can be used to calculate the straight-line distance between two points in 3D space, by simply adding in the third dimension.

RMS (Root-Mean-Square) is a statistical measure of the magnitude of a varying quantity. For time-series data, RMS can give you the overall magnitude of the samples, considered as a whole.

For a continuous sine wave, the RMS value is equal to the peak value, multiplied by the square root of 0.5. This magic number, approximately 0.70710678118655, is also equal to the sin(45), or the sine of a 45-degree angle. For a pure, continuous sine wave of a consistent peak amplitude, the RMS amplitude of that sine wave can be found by multiplying its peak amplitude by this magic number to arrive at its equivalent continuous RMS power. Why is this important? Because the RMS amplitude (in volts) of an electrical sine wave tells you what the amplitude (in volts) of an equivalent, continuous Direct Current signal would be. In other words, it allows you to calculate the effective, continuous DC voltage of an AC sine wave.

So…we have Pythagorean Theorem which is used to calculate the distance between vectors, and RMS which is used to calculate the true amplitude of a set of time-series samples. How do they relate? Very closely, as you’ll see shortly.

Consider the following piece of PHP code:

// Calculate the RMS of a sine wave of amplitude 1.0.
\$pi = pi();
\$twopi = \$pi*2.0;
\$nsamples = 0;
\$total = 0.0;
\$total += \$sin*\$sin;
\$nsamples++;
}
\$rms = sqrt(\$total/\$nsamples);
echo “\$rms\n”;

This little piece of code simply iterates over a sine wave of amplitude 1.0, in increments of 1/10,000th of 360 degrees, sums up the time-series samples, then calculates the RMS value by taking the square root of the averages of the squares. This very closely matches Pythagorean Theorem. We’re treating the time-series data points as a vector on N-dimensional space, where N is the number of data points (samples). With Pythagorean Theorem (PT), the distance (the length of the hypotenuse) is the square root of the sum of the squares of the other two sides of a right triangle. The only difference between the above code and Pythagorean Theorem is that we’re dividing the sum of the squares by number of samples, hence Root-Mean-Square, or the square root of the mean of the squares. PT is the square root of the sum of the squares.

The above code sample outputs 0.70710678118651, very close to our magic number of 0.70710678118655. As the number of samples is increased beyond 10,000, the output will come closer to matching our magic number.

To calculate the magic number, we can calculate either the square root of 0.5, or the sine of 45 degrees, as follows:

// Calculate the square root of 0.5.
echo sqrt(0.5).”\n”;

// Calculate the sine of 45 degrees.
echo sin(pi()*0.25).”\n”;

In the comments section, let me know how you use Pythagorean Theorem and/or RMS to solve real-world problems.