![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
egreedy
Advanced tools
Readme
An epsilon-greedy multi-armed bandit algorithm
This implementation is based on Bandit Algorithms for Website Optimization and related empirical research in "Algorithms for the multi-armed bandit problem".
This module conforms to the BanditLab/1.0 specification.
First, install this module in your project:
npm install egreedy --save
Then, use the algorithm:
Create a bandit with 3 arms and epsilon 0.25:
var Bandit = require('egreedy');
var bandit = new Bandit({
arms: 3,
epsilon: 0.25
});
Select an arm (for exploration or exploitation, according to the algorithm):
bandit.select().then(function (arm) {
...
});
Report the reward earned from a chosen arm:
bandit.reward(armId, value).then(function (n) {
...
});
Bandit([config])
Create a new optimization algorithm.
Arguments
config
(Object, Optional): algorithm instance parametersThe config
object supports two parameters:
arms
: (Number:Integer, Optional), default=2, the number of arms over which the optimization will operateepsilon
: (Number:Float, Optional), default=0.5, from 0 (never explore/always exploit) to 1 (always explore/never exploit)Returns
An instance of the egreedy optimization algorithm.
Example
> var Bandit = require('egreedy');
> var bandit = new Bandit();
> assert.equal(bandit.arms, 3);
> assert.equal(bandit.epsilon, 0.5);
Or, with a passed config
:
> var Bandit = require('egreedy');
> var bandit = new Bandit({arms: 4, epsilon: 0.75});
> assert.equal(bandit.arms, 4);
> assert.equal(bandit.epsilon, 0.75);
Bandit#select()
Choose an arm to play, according to the specified bandit algorithm.
Arguments
None
Returns
A promise that resolves to a Number corresponding to the associated arm index.
Example
> var Bandit = require('egreedy');
> var bandit = new Bandit();
> bandit.select().then(function (arm) { console.log(arm); });
0
Bandit#reward(arm, reward)
Inform the algorithm about the payoff from a given arm.
Arguments
arm
(Integer): the arm index (provided from bandit.select()
)reward
(Number): the observed reward value (which can be 0, to indicate no reward)Returns
A promise that resolves to a Number representing the count of observed rounds.
Example
> var Bandit = require('egreedy');
> var bandit = new Bandit();
> bandit.reward(0, 1).then(function (n) { console.log(n); });
1
Bandit#serialize()
Obtain a plain object representing the internal state of the algorithm.
Arguments
None
Returns
A promise that resolves to an Object representing parameters required to reconstruct algorithm state.
Example
> var Bandit = require('egreedy');
> var bandit = new Bandit();
> bandit.serialize().then(function (state) { console.log(state); });
{
arms: 2,
epsilon: 0.5,
counts: [0, 0],
values: [0, 0]
}
Bandit#load(state)
Restore an instance of a bandit to a previously serialized algorithm state. This method overrides any options parameters passed at instantiation.
Arguments
state
(Object): a serialized algorithm state (provided from bandit.serialize()
)Returns
A promise that resolves to a Number representing the count of observed rounds.
Example
> var state = {arms: 2, epsilon: 0.5, counts: [1, 2], values: [1, 0.5]};
> var Bandit = require('egreedy');
> var bandit = new Bandit();
> bandit.load(state).then(function (n) { console.log(n); });
3
To run the unit test suite:
npm test
Or, to run the test suite and view test coverage:
npm run coverage
Note: tests against stochastic methods (e.g. bandit.select()
) are inherently tricky to test with deterministic assertions. The approach here is to iterate across a semi-random set of conditions to verify that each run produces valid output. So, strictly speaking, each call to npm test
is executing a slightly different test suite. At some point, the test suite may be expanded to include a more robust test of the distribution's properties – though because of the number of runs required, would be triggered with an optional flag.
PRs are welcome! For bugs, please include a failing test which passes when your PR is applied. Travis CI provides on-demand testing for commits and pull requests.
Currently, this implementation relies on the native Math.random() which uses a seeded "random" number generator. In addition, the underlying calculations often encounter extended floating point numbers. Arm selection is therefore subject to JavaScript's floating point precision limitations. For general information about floating point issues see the floating point guide.
While these factors generally do not impede commercial application, I would consider the implementation suspect in an academic setting.
FAQs
An epsilon-greedy multi-armed bandit algorithm
The npm package egreedy receives a total of 2 weekly downloads. As such, egreedy popularity was classified as not popular.
We found that egreedy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.