PHPackages                             centrex/laravel-btyd - 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. centrex/laravel-btyd

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

centrex/laravel-btyd
====================

Laravel package for BTYD (BG/NBD + Gamma-Gamma) CLV prediction

v0.1.0(9mo ago)00MITPHPPHP ^8.2|^8.3|^8.4CI passing

Since Sep 6Pushed 5d agoCompare

[ Source](https://github.com/centrex/laravel-btyd)[ Packagist](https://packagist.org/packages/centrex/laravel-btyd)[ Docs](https://github.com/centrex/btyd)[ RSS](/packages/centrex-laravel-btyd/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (15)Versions (2)Used By (0)

Laravel BTYD — BG/NBD + Gamma-Gamma CLV Prediction
==================================================

[](#laravel-btyd--bgnbd--gamma-gamma-clv-prediction)

[![Latest Version on Packagist](https://camo.githubusercontent.com/638d7c3c96b3017ea3403ef737b90f12b43c6337f3aa4f562fb97881ddc259ff/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63656e747265782f6c61726176656c2d627479642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/centrex/laravel-btyd)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7311d5c2dd256d6285bbc952170fa7978acc55dfbbd552b64abd59a44fcb7bc1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63656e747265782f6c61726176656c2d627479642f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/centrex/laravel-btyd/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/cea82b2d7ce8fa6e6927d7e946a4339eab8c1832dcbc4aa6cd2b42b1d9327d6c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63656e747265782f6c61726176656c2d627479642f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/centrex/laravel-btyd/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b17f1168b5324491d3830909f0615e3fa30fb3e1c622402a348c190521c3b9ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63656e747265782f6c61726176656c2d627479643f7374796c653d666c61742d737175617265)](https://packagist.org/packages/centrex/laravel-btyd)

Implements the **Buy 'Til You Die** model for customer lifetime value (CLV) prediction. Fits BG/NBD parameters (purchase frequency + churn) and Gamma-Gamma parameters (monetary value) using MLE via Nelder-Mead optimisation. Supports persisting fitted parameters to the database for reuse.

Installation
------------

[](#installation)

```
composer require centrex/laravel-btyd
php artisan vendor:publish --tag="laravel-btyd-migrations"
php artisan migrate
```

Usage
-----

[](#usage)

### 1. Build customer summaries from transaction history

[](#1-build-customer-summaries-from-transaction-history)

```
use Centrex\Btyd\Btyd;

// Each transaction: ['date' => Carbon|string, 'amount' => float]
$transactions = [
    ['date' => '2024-01-15', 'amount' => 120.00],
    ['date' => '2024-03-02', 'amount' => 85.50],
    ['date' => '2024-06-18', 'amount' => 200.00],
];

$summary = Btyd::transactionsToSummary($transactions);
// returns: frequency, recency (days), T (days since first purchase), monetary, n_transactions, total_revenue
```

### 2. Fit the models on a cohort

[](#2-fit-the-models-on-a-cohort)

```
$btyd = new Btyd();

// Fit BG/NBD on cohort summaries (frequency, recency, T required per customer)
$bgnbdParams = $btyd->fitBgNbd($cohortSummaries);
// returns: ['r' => ..., 'alpha' => ..., 'a' => ..., 'b' => ...]

// Fit Gamma-Gamma on customers with at least 1 repeat purchase (frequency, monetary required)
$ggParams = $btyd->fitGammaGamma($cohortSummaries);
// returns: ['p' => ..., 'q' => ..., 'v' => ...]
```

### 3. Predict for individual customers

[](#3-predict-for-individual-customers)

```
// Expected number of transactions over the next 12 months
$expectedTx = $btyd->expectedTransactions($customerSummary, horizonMonths: 12);

// Expected monetary value per transaction
$expectedMonetary = $btyd->expectedMonetary($customerSummary);

// Customer lifetime value (expectedTx × expectedMonetary)
$clv = $btyd->customerClv($customerSummary, horizonMonths: 12);
```

### 4. Persist fitted parameters

[](#4-persist-fitted-parameters)

```
use Centrex\Btyd\Models\BtydParam;

// Save fitted params for a given model class
BtydParam::updateOrCreate(
    ['model' => App\Models\Customer::class],
    ['params' => array_merge($bgnbdParams, $ggParams)],
);

// Load later
$params = BtydParam::getParams(App\Models\Customer::class);
```

### Full workflow example

[](#full-workflow-example)

```
$btyd = new Btyd();

// Build summaries for all customers
$summaries = Customer::all()->map(fn ($c) =>
    Btyd::transactionsToSummary($c->orders->map(fn ($o) => [
        'date' => $o->created_at,
        'amount' => $o->total,
    ])->toArray())
)->toArray();

// Fit
$btyd->fitBgNbd($summaries);
$btyd->fitGammaGamma($summaries);

// Predict CLV for a single customer
$clv = $btyd->customerClv($summaries[0], 12);
echo "12-month CLV: {$clv}";
```

Testing
-------

[](#testing)

```
composer test        # full suite
composer test:unit   # pest only
composer test:types  # phpstan
composer lint        # pint
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [centrex](https://github.com/centrex)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance81

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 67.6% 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

Unknown

Total

1

Last Release

276d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29769944?v=4)[Raisul Islam](/maintainers/rochi88)[@rochi88](https://github.com/rochi88)

---

Top Contributors

[![rochi88](https://avatars.githubusercontent.com/u/29769944?v=4)](https://github.com/rochi88 "rochi88 (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (9 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

---

Tags

laravelcentrexbtyd

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/centrex-laravel-btyd/health.svg)

```
[![Health](https://phpackages.com/badges/centrex-laravel-btyd/health.svg)](https://phpackages.com/packages/centrex-laravel-btyd)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[wearepixel/laravel-cart

A cart implementation for Laravel

1355.6k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

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