PHPackages                             square1/laravel-collection-rolling-average - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. square1/laravel-collection-rolling-average

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

square1/laravel-collection-rolling-average
==========================================

Extend collections to support rolling average functionality.

13.0.0(2mo ago)2310.0k↓36.6%MITPHPPHP ^8.3CI passing

Since Feb 17Pushed 2mo ago5 watchersCompare

[ Source](https://github.com/square1-io/laravel-collection-rolling-average)[ Packagist](https://packagist.org/packages/square1/laravel-collection-rolling-average)[ Docs](https://github.com/square1-io/laravel-collection-rolling-average)[ RSS](/packages/square1-laravel-collection-rolling-average/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (6)Used By (0)

[![Build and Test](https://github.com/square1-io/laravel-collection-rolling-average/actions/workflows/laravel.yml/badge.svg)](https://github.com/square1-io/laravel-collection-rolling-average/actions/workflows/laravel.yml/badge.svg)

Add Rolling Average Functionality To Collections
================================================

[](#add-rolling-average-functionality-to-collections)

This package adds a rolling average functionality to Laravel's `Collection` facade. Rolling averages (also known as moving averages) are a statistic to capture the average change in a data range over time. For example, looking at a graph of daily temperature, or stock market movement, the graph may appear very noisy and erratic. The graph can be smoothed out to show longer-term trends by replacing each daily data point with an average of the previous N days.

[![Rolling Average graph](https://github.com/square1-io/laravel-collection-rolling-average/raw/main/average-graph.png?raw=true)](https://github.com/square1-io/laravel-collection-rolling-average/blob/main/average-graph.png?raw=true)

Take this example, where we want the rolling average of the last 5 days

DayValueRolling Average1552763354555106635.6755.2This is a rolling average of all values in the set. While we want the rolling average of the latest 5 in this example, where there are less than 5 entries, the average is over the available entries.

It's possible to force this function to be more strict, and only start calculating the average once we have the minimum number of entries we want (5, in this case). This would mean that the resulting data changes a bit:

DayValueRolling Average15-27-33-45-5106635.6755.2Install
-------

[](#install)

Via Composer

```
$ composer require square1/laravel-collection-rolling-average
```

The package will be automatically registered.

Versioning
----------

[](#versioning)

This package is built on top of the collections in `Illuminate\Support`. With the release of `12.0.0`, this package follows the version constraints of `Illuminate\Support`, which itself mirrors Laravel's release versioning.

Support for earlier versions prior to this change is listed in the table below.

Package VersionLaravel Version13.\*1312.\*122.0.011, 10, 9, 8.71.0.010, 9, 8.7Usage
-----

[](#usage)

### Rolling average

[](#rolling-average)

This will show an average of all entries to date. Each returned value shall be the average of all values up to that point in the collection. This can mean that the early values retain some of their "noisiness", before the average really kicks in.

```
    $data = new Collection([1, 2, 3, 4, 5]);

    $averages = $data->rollingAverage();
    /**
     *   1,      // 1
     *   1.5,    // 1+2 / 2 (only 2 entries seen so far)
     *   2,      // 1+2+3 / 3
     *   2.5,    // 1+2+3+4 / 4
     *   3       // 1+2+3+4+5 / 5
     */
```

### Rolling average with a limit

[](#rolling-average-with-a-limit)

Calculate the rolling average based on the N previous entries to each data point. The collection returned will be the same size as the one supplied. This means that for positions in the collection before N values have been seen, the rolling average is the average of all values seen to date. If you want to exclude all values before N entries, take a look at the [Rolling average with limited lookback](#rolling-average-with-limited-lookback) section.

```
    $data = new Collection([1, 2, 3, 4, 5, 6]);
    // How many entries should we consider in our average?
    $lookback = 2;
    $averages = $data->rollingAverage($lookback);
    /**
     * Looking back over last 2 entries, averages should be:
     *   1,      // 1
     *   1.5,    // 1+2 / 2
     *   2.5,    // 2+3 / 2
     *   3.5,    // 3+4 / 2
     *   4.5,    // 4+5 / 2
     *   5.5     // 5+6 / 2
     */
```

### Rolling average with limited lookback

[](#rolling-average-with-limited-lookback)

By default the package will return a collection with the same number of values supplied to it. This can lead to some noisiness at the start of the collection. For example, if we hav a dataset of 30 entries, and want a rolling average over the last 5 values, the first 4 values will be averages of the first N values, where N &lt; 5. To avoid this, the parameter `$includeAll` can be set to `false`. This will return a collection with fewer results than the original. This will remove the noisiness from the start of the collection, but will mean a mismatch in the number of values returned.

```
    $data = new Collection([1, 2, 3, 4, 5, 6]);
    $averages = $data->rollingAverage(4, $enforceLimitedLookback = true);

    /**
     * 2.5,
     * 3.5,
     * 4.5
     */
```

OriginalAverage (default)Average (limited lookback)11-21.5-32-42.52.553.53.564.54.5### Apply weightings

[](#apply-weightings)

Add weightings to increase or decrease the relevance of the most recent entries.

```
    $data = new Collection([1, 2, 2, 4, 5, 6]);
    // Weights will be applied in the order of current data point N, then N-1, and so on.
    // Where no weighting exists, a default of 1 is used.
    $weights = new Collection([5, 2]);

    $averages = $data->rollingAverage(4, $enforceLimitedLookback = false, $weights);

    /**
      * collect([
      *   5,      // 1 * 5
      *   6,      // (2 * 5) + (1 * 2) / 2
      *   5,      // (2 * 5) + (2 * 2) + 1 / 3
      *   6.75,   // (4 * 5) + (2 * 2) + 2 + 1 / 4
      *   9.25,   // (5 * 5) + (4 * 2) + 2 + 2 / 4
      *   11.5,   // (6 * 5) + (5 * 2) + 4 + 2 / 4
      * ]);
      */
```

Tests
-----

[](#tests)

```
$ composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](License.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~281 days

Total

5

Last Release

61d ago

Major Versions

1.0.0 → 2.0.02024-03-21

2.0.0 → 12.0.02025-02-24

12.1.0 → 13.0.02026-03-18

PHP version history (4 changes)1.0.0PHP ^8.0

2.0.0PHP ^8.2

12.0.0PHP ^8.1

13.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a6096b0455d662c5963ed82e1f0968bc4a18bdfedd332f7fd2598480b4c17bd?d=identicon)[conroyp](/maintainers/conroyp)

---

Top Contributors

[![conroyp](https://avatars.githubusercontent.com/u/143244?v=4)](https://github.com/conroyp "conroyp (30 commits)")

---

Tags

collectionilluminatesquare1rolling-averagemoving-average

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/square1-laravel-collection-rolling-average/health.svg)

```
[![Health](https://phpackages.com/badges/square1-laravel-collection-rolling-average/health.svg)](https://phpackages.com/packages/square1-laravel-collection-rolling-average)
```

###  Alternatives

[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)[dutchcodingcompany/csv-collection

Read and write large csv files using Laravel's Illuminate Collections

117.5k](/packages/dutchcodingcompany-csv-collection)[zizaco/lessy

Lessy is a simple and lean LESS compiler for Laravel

2116.7k](/packages/zizaco-lessy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
