PHPackages                             brianzeligson/mabandit - 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. brianzeligson/mabandit

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

brianzeligson/mabandit
======================

Implements the Multi Armed Bandit algorithm

7171[1 issues](https://github.com/beezee/multiarmedbandit/issues)PHP

Since Dec 20Pushed 10y ago1 watchersCompare

[ Source](https://github.com/beezee/multiarmedbandit)[ Packagist](https://packagist.org/packages/brianzeligson/mabandit)[ RSS](/packages/brianzeligson-mabandit/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

\#Multi-Armed Bandit

This library provides a simple interface for using the [Multi-Armed Bandit](http://en.wikipedia.org/wiki/Multi-armed_bandit)algorithm to dynamically test and optimize between a set of possible options.

\###Installation

Install composer

```
curl -s https://getcomposer.org/installer | php
```

Add a composer.json to your project root

```
{
  "require": {
    "brianzeligson/mabandit": "dev-master"
  }
}
```

Install using composer

```
php composer.phar install
```

Add this line to your main application file:

```
require 'vendor/autoload.php';
```

\###Basic Use

All interaction is done through an instance of the MaBandit\\MaBandit class. In order to create an instance, you need to provide a [Strategy](http://en.wikipedia.org/wiki/Multi-armed_bandit#Bandit_strategies)and a Persistor.

Included are EpsilonGreedy or EpsilonFirst strategies, and ArrayPersistor or RedisPersistor for persistence. Note that persistence is required for the algorithm to work, and ArrayPersistor is a simple in-memory array, suitable only for long-running single process tasks, or testing. For anything requiring maintenance of state, RedisPersistor is the way to go.

Create a bandit as follows:

```
$strategy = \MaBandit\EpsilonGreedy::withExplorationEvery(3) //experiment every 3rd time
// OR
$strategy = \MaBandit\EpsilonFirst::withExploitationAfter(100) //only experiments til 100
$persistor = new \MaBandit\Persistence\RedisPersistor();
$bandit = \MaBandit\MaBandit::withStrategy($strategy)->withPersistor($persistor);
```

Now you will need an experiment to work with. You can create one as follows:

```
$experiment = $bandit->createExperiment('cta-size', array('sm', 'med', 'lg'));
```

To load a persisted experiment, use getExperiment. This method throws an ExperimentNotFoundException if your experiment is not found, so it's best to handle this. The following example checks for a persisted experiment and initializes if it is not found:

```
try {
  $ex = $bandit->getExperiment('cta-size')
} catch(\MaBandit\Exception\ExperimentNotFoundException $e) {
  $ex = $bandit->createExperiment('cta-size', array('sm', 'med', 'lg'));
}
```

With your experiment you can now let the bandit do it's job. To ask the bandit for the next value for use in your experiment, call chooseLever as follows:

```
$nextValue = $bandit->chooseLever($experiment)->getValue();
```

Note that this method mutates the "lever" (representation of a value in an experiment) to keep track of the number of times it has been tested, so it is important to call it only when you are going to use the value.

In order for the algorithm to successfully determine the winning choice, you need to inform it of a conversion. When a value converts, use it to fetch the corresponding lever and pass that to registerConversion, as follows:

```
$lever = $bandit->getLeverByExperimentAndValue('cta-size', $convertedValue);
$bandit->registerConversion($lever);
```

A small sample app demonstrating use is available [here](https://github.com/beezee/mabanditdemo/blob/master/index.php)

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

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

### Community

Maintainers

![](https://www.gravatar.com/avatar/e365fcacf11c1ae5dc86d18acb3dd570d13e07f3e18ff69b3ae4c4b1228e27ac?d=identicon)[beezee](/maintainers/beezee)

---

Top Contributors

[![Shelob9](https://avatars.githubusercontent.com/u/1994311?v=4)](https://github.com/Shelob9 "Shelob9 (3 commits)")[![beezee](https://avatars.githubusercontent.com/u/682587?v=4)](https://github.com/beezee "beezee (1 commits)")

### Embed Badge

![Health badge](/badges/brianzeligson-mabandit/health.svg)

```
[![Health](https://phpackages.com/badges/brianzeligson-mabandit/health.svg)](https://phpackages.com/packages/brianzeligson-mabandit)
```

###  Alternatives

[kop/yii2-scroll-pager

Infinite AJAX scrolling for Yii2 ListView widget

181732.8k10](/packages/kop-yii2-scroll-pager)

PHPackages © 2026

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