πŸ“˜ Computing the moving average using Perl 6

Calculate the moving average for the given array of numbers.

For each element of an array, the moving average is the average value of the last few items or the few elements around it. This kind of analysis is often used to smooth the curve.

Let us first generate some random dataβ€”an array of a hundred values between 0 and 1:

my @data = map {rand}, 1..100;

Now, calculate the average values for (almost) each point using three items before and three items after the current item:

my @average = map {
    sum(@data[$_ - 3 .. $_ + 3]) / 7
}, 3..96;

The beginning and the end of an initial array do not have enough neighbouring elements; that’s why they are skipped.

Inside theΒ map function, the code block calculates the sum of an array slice:

sum(@data[$_ - 3 .. $_ + 3]).

Here is a graph with the results of a test run of this program. The inner curve corresponds to the values of theΒ @average array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s