Download the PHP package cleatsquad/php-bandit without Composer

On this page you can find all versions of the php package cleatsquad/php-bandit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-bandit

PHP Bandit

Latest Version

Beta-Bernoulli Thompson Sampling for multi-armed bandit problems, in dependency-free PHP.

You have several options and no idea which performs best. Testing them evenly wastes traffic on the losers; committing early to the leader may commit to a fluke. Thompson Sampling resolves that trade-off by drawing from each option's posterior belief and picking the winner of that draw — good options get chosen more often, uncertain ones keep getting explored.

Installation

Requires PHP 8.2 or later. No runtime dependencies.

Usage

Pick an option

The result is deliberately stochastic. An arm that looks worse still gets picked occasionally — that is the exploration that stops the bandit from locking onto an early fluke.

Keeping the draws makes a decision explainable after the fact: log $result->samples and you can replay why that arm won, without drawing again. When you only need the key, use the shorthand:

There is no "was this exploration or exploitation?" flag, because Thompson Sampling has no such mode — every pick is one draw per arm. If you want to know whether the winner was the current front-runner, compare it yourself:

Run the loop

Selecting is one fifth of the job. The full cycle is select → execute → observe → update → persist:

Three rules hold that loop together:

If your storage counts trials rather than failures, build the state directly:

Persist it yourself, concurrently

The library is stateless: the policy remembers nothing between calls, ArmState is immutable, and nothing here touches storage. That is deliberate — keys, transactions, serialization and increment semantics belong to your application, not to a bandit.

Which leaves one mistake worth naming. Loading an ArmState, calling withSuccess(), and writing both counts back is a read-modify-write: two concurrent processes write the same value and one observation vanishes. Under concurrency, increment in the store instead:

withSuccess() and withFailure() remain the right API in memory — in a single process, a simulation, or a test.

Two things you do not need to protect against:

Inspect the posterior

Reproducible runs

Design notes

Beta(1,1) prior. Successes and failures are offset by an uninformative prior, so an arm with no data yet behaves as a coin flip rather than dividing by zero.

Gamma draws via Marsaglia & Tsang (2000). Constant-time rejection sampling, valid for shape ≥ 1 — which the Beta(1,1) prior guarantees by construction. Beta samples are then formed as X / (X + Y) from two Gamma draws.

Native randomness. Uses PHP's \Random\Randomizer. Inject your own engine, or use withSeed() for a Mt19937 engine with a fixed seed.

Invalid counts are rejected, not repaired. A negative success or failure count is a caller bug, never data: ArmState and every statistic throw InvalidArmStateException rather than silently produce a wrong posterior. Every exception in this package implements BanditException, so catch (BanditException $e) catches all of them — and each one still extends its natural SPL class.

Exception Thrown when
InvalidArmStateException a count is negative, or successes exceed trials
EmptyArmSetException select() is given no candidate arm
InvalidSelectionException a SelectionResult is built from a decision that could not have happened

A result cannot contradict itself. SelectionResult validates its own bookkeeping: the samples are non-empty, the selected arm was drawn, and $sample is the draw that arm got. The winning draw is not required to be the largest one — a policy other than Thompson Sampling may pick against its own samples, and SelectionResult is the shared return type of BanditPolicyInterface.

Statistical validation

A sampler can pass every unit test and still draw from the wrong curve, so the suite checks the distribution itself. Every run below is seeded: the assertions are deterministic, not flaky.

Performance

Measured with PHPBench on PHP 8.4, one decision per operation:

Operation Cost
select() over 2 arms ~3.3 μs
select() over 10 arms ~14 μs
select() over 100 arms ~146 μs
sample() ~1.3 μs
posteriorMean() ~0.2 μs

Cost is linear in the number of arms and flat in the amount of evidence: a posterior backed by ten thousand observations draws as fast as an uninformed one. Run composer bench for the numbers on your own hardware.

When to use it

Good fits: A/B and multivariate testing, traffic allocation, ranking candidate strategies, model or provider selection, any explore-versus-exploit choice with a binary outcome.

Poor fits: rewards that are not success/failure (this is the Bernoulli variant), or a setting where a single decision must be reproducible without a fixed seed.

Stability

1.0.0 freezes the public API. From here on the package follows Semantic Versioning strictly: nothing listed below changes without a major release.

Three things are explicitly not covered, and may change in a minor release:

Upgrading to 1.0.0

1.0.0 removes selectBestArm(), deprecated since 0.2.0. Replace it with selectArm(); the behaviour is identical.

Two smaller changes, neither of which affects correct code:

Upgrading from 0.1.0

0.2.0 reshapes the public API. The maths are untouched: same algorithm, same draws, same numbers.

0.1.0 0.2.0
$policy->selectBestArm($arms) $policy->selectArm($arms), or $policy->select($arms)->selectedArm
BanditPolicyInterface declared the four statistics it declares select(array $arms): SelectionResult; the statistics stay on ThompsonSamplingPolicy
type-hinting the interface to call posteriorWeight() type-hint ThompsonSamplingPolicy, or declare your own application interface
new ArmState($s->successes + 1, $s->failures) $s->withSuccess()
new ArmState($successes, $trials - $successes) ArmState::fromTrials($trials, $successes)
negative counts were clamped to zero they throw InvalidArmStateException

selectBestArm() still works in 0.2.0, deprecated. It is gone in 1.0.0: the name promised an argmax, while what it returns is a posterior draw.

Testing

bench and mutation install their own toolchain under tools/ on first use. They are kept out of require-dev on purpose: the package is tested on PHP 8.2 through 8.5, and neither tool has to be installable on all of them for the matrix to run.

License

MIT. See LICENSE.


All versions of php-bandit with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cleatsquad/php-bandit contains the following files

Loading the files please wait ...